canvas.gui: Add MessageBox class.
Use canvas.MessageBox.critical/warning/information/question to show a standard, but also customizable dialog box.
This commit is contained in:
parent
9933398a37
commit
e496ca56ab
9 changed files with 207 additions and 7 deletions
|
@ -16,8 +16,9 @@ loadGUIFile("styles/DefaultStyle.nas");
|
|||
loadWidget("Button");
|
||||
loadWidget("Label");
|
||||
loadWidget("ScrollArea");
|
||||
loadDialog("MessageBox");
|
||||
|
||||
var style = DefaultStyle.new("AmbianceClassic");
|
||||
var style = DefaultStyle.new("AmbianceClassic", "Humanity");
|
||||
var WindowButton = {
|
||||
new: func(parent, name)
|
||||
{
|
||||
|
@ -101,6 +102,10 @@ var Window = {
|
|||
me._node.remove();
|
||||
me._node = nil;
|
||||
},
|
||||
setTitle: func(title)
|
||||
{
|
||||
return me.set("title", title);
|
||||
},
|
||||
# Create the canvas to be used for this Window
|
||||
#
|
||||
# @return The new canvas
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
gui.Style = {
|
||||
new: func(name)
|
||||
new: func(name, name_icon_theme)
|
||||
{
|
||||
var root_node = props.globals.getNode("/sim/gui/canvas", 1)
|
||||
.addChild("style");
|
||||
var path = getprop("/sim/fg-root") ~ "/gui/styles/" ~ name;
|
||||
var gui_path = getprop("/sim/fg-root") ~ "/gui";
|
||||
var style_path = gui_path ~ "/styles/" ~ name;
|
||||
|
||||
var m = {
|
||||
parents: [gui.Style],
|
||||
_path: path,
|
||||
_node: io.read_properties(path ~ "/style.xml", root_node),
|
||||
_path: style_path,
|
||||
_dir_icons: gui_path ~ "/icons/" ~ name_icon_theme,
|
||||
_node: io.read_properties(style_path ~ "/style.xml", root_node),
|
||||
_colors: {}
|
||||
};
|
||||
|
||||
|
|
170
Nasal/canvas/gui/dialogs/MessageBox.nas
Normal file
170
Nasal/canvas/gui/dialogs/MessageBox.nas
Normal file
|
@ -0,0 +1,170 @@
|
|||
var MessageBox = {
|
||||
Ok: 0x0001,
|
||||
Cancel: 0x0002,
|
||||
Yes: 0x0004,
|
||||
No: 0x0008,
|
||||
new: func
|
||||
{
|
||||
return {
|
||||
parents: [MessageBox],
|
||||
_title: "Message",
|
||||
_standard_buttons: MessageBox.Ok
|
||||
};
|
||||
},
|
||||
setTitle: func(title)
|
||||
{
|
||||
me._title = title;
|
||||
},
|
||||
setImage: func(img)
|
||||
{
|
||||
if( img != nil and img.find('/') < 0 )
|
||||
me._img = style._dir_icons ~ "/" ~ img ~ ".png";
|
||||
else
|
||||
me._img = img;
|
||||
return me;
|
||||
},
|
||||
setText: func(text)
|
||||
{
|
||||
me._text = text;
|
||||
return me;
|
||||
},
|
||||
setStandardButtons: func(mask)
|
||||
{
|
||||
me._standard_buttons = mask;
|
||||
return me;
|
||||
},
|
||||
exec: func(cb = nil)
|
||||
{
|
||||
var MARGIN = 8; # TODO implement margin in C++ layouting code
|
||||
var dlg = canvas.Window.new([250,100], "dialog")
|
||||
.setTitle(me._title);
|
||||
var root = dlg.getCanvas(1)
|
||||
.set("background", style.getColor("bg_color"))
|
||||
.createGroup();
|
||||
var vbox = VBoxLayout.new();
|
||||
dlg.setLayout(vbox);
|
||||
vbox.addSpacing(MARGIN);
|
||||
|
||||
var text_box = HBoxLayout.new();
|
||||
vbox.addItem(text_box);
|
||||
|
||||
text_box.addSpacing(MARGIN);
|
||||
|
||||
if( me._img != nil )
|
||||
{
|
||||
text_box.addItem(
|
||||
gui.widgets.Label.new(root, style, {})
|
||||
.setFixedSize(48, 48)
|
||||
.setImage(me._img)
|
||||
);
|
||||
}
|
||||
|
||||
var label_text = gui.widgets.Label.new(root, style, {wordWrap: 1})
|
||||
.setText(me._text);
|
||||
text_box.addItem(label_text, 1);
|
||||
text_box.addSpacing(MARGIN);
|
||||
|
||||
vbox.addStretch(1);
|
||||
|
||||
var button_box = HBoxLayout.new();
|
||||
vbox.addItem(button_box);
|
||||
|
||||
button_box.addStretch(1);
|
||||
foreach(var button; [me.Ok, me.Cancel, me.Yes, me.No])
|
||||
{
|
||||
if( !(me._standard_buttons & button) )
|
||||
continue;
|
||||
|
||||
(func{
|
||||
var b_id = button;
|
||||
button_box.addItem(
|
||||
gui.widgets.Button.new(root, style, {})
|
||||
.setText(me._button_names[button])
|
||||
.listen("clicked", func {
|
||||
dlg.del();
|
||||
if( cb != nil )
|
||||
cb(b_id);
|
||||
})
|
||||
);
|
||||
})();
|
||||
}
|
||||
button_box.addSpacing(MARGIN);
|
||||
|
||||
vbox.addSpacing(MARGIN);
|
||||
|
||||
return me;
|
||||
},
|
||||
show: func(title, text, icon = nil, cb = nil, buttons = nil)
|
||||
{
|
||||
var msg_box = MessageBox.new();
|
||||
msg_box.setTitle(title);
|
||||
msg_box.setText(text);
|
||||
|
||||
if( buttons == nil )
|
||||
buttons = MessageBox.Ok;
|
||||
msg_box.setStandardButtons(buttons);
|
||||
|
||||
if( icon != nil )
|
||||
msg_box.setImage(icon);
|
||||
|
||||
return msg_box.exec(cb);
|
||||
},
|
||||
# Show an error/critical message in a message box
|
||||
#
|
||||
# @param title
|
||||
# @param text
|
||||
# @param cb Dialog close callback
|
||||
# @param buttons Mask indicating the buttons to show
|
||||
# (default: MessageBox.Ok)
|
||||
critical: func(title, text, cb = nil, buttons = nil)
|
||||
{
|
||||
MessageBox.show(title, text, "dialog-error", cb, buttons);
|
||||
},
|
||||
# Show a warning message in a message box
|
||||
#
|
||||
# @param title
|
||||
# @param text
|
||||
# @param cb Dialog close callback
|
||||
# @param buttons Mask indicating the buttons to show
|
||||
# (default: MessageBox.Ok)
|
||||
warning: func(title, text, cb = nil, buttons = nil)
|
||||
{
|
||||
MessageBox.show(title, text, "dialog-warning", cb, buttons);
|
||||
},
|
||||
# Show an informative message in a message box
|
||||
#
|
||||
# @param title
|
||||
# @param text
|
||||
# @param cb Dialog close callback
|
||||
# @param buttons Mask indicating the buttons to show
|
||||
# (default: MessageBox.Ok)
|
||||
information: func(title, text, cb = nil, buttons = nil)
|
||||
{
|
||||
MessageBox.show(title, text, "dialog-info", cb, buttons);
|
||||
},
|
||||
# Show a question in a message box
|
||||
#
|
||||
# @param title
|
||||
# @param text
|
||||
# @param cb Dialog close callback
|
||||
# @param buttons Mask indicating the buttons to show
|
||||
# (default: MessageBox.Yes | MessageBox.No)
|
||||
question: func(title, text, cb = nil, buttons = nil)
|
||||
{
|
||||
MessageBox.show(
|
||||
title,
|
||||
text,
|
||||
"dialog-question",
|
||||
cb,
|
||||
buttons or MessageBox.Yes | MessageBox.No
|
||||
);
|
||||
},
|
||||
# private:
|
||||
_button_names: {}
|
||||
};
|
||||
|
||||
# Standard button names
|
||||
MessageBox._button_names[ MessageBox.Ok ] = "Ok";
|
||||
MessageBox._button_names[ MessageBox.Cancel ] = "Cancel";
|
||||
MessageBox._button_names[ MessageBox.Yes ] = "Yes";
|
||||
MessageBox._button_names[ MessageBox.No ] = "No";
|
|
@ -1,7 +1,10 @@
|
|||
var DefaultStyle = {
|
||||
new: func(name)
|
||||
new: func(name, name_icon_theme)
|
||||
{
|
||||
return { parents: [gui.Style.new(name), DefaultStyle] };
|
||||
return {
|
||||
parents: [ gui.Style.new(name, name_icon_theme),
|
||||
DefaultStyle ]
|
||||
};
|
||||
},
|
||||
createWidget: func(parent, type, cfg)
|
||||
{
|
||||
|
|
20
gui/icons/Humanity/AUTHORS
Normal file
20
gui/icons/Humanity/AUTHORS
Normal file
|
@ -0,0 +1,20 @@
|
|||
####################
|
||||
ABOUT: #
|
||||
####################
|
||||
|
||||
Humanity is designed and developed by :
|
||||
Daniel Foré <Daniel.p.Fore@gmail.com>,
|
||||
K.Vishnoo Charan Reddy<vish@ubuntu.com>,
|
||||
Jonian Guveli <jonian.guveli@gmail.com>.
|
||||
|
||||
GNOME icons and Humanity icons are all licensed under the GPL.
|
||||
|
||||
This package is licensed under GNU General Public License version 2.
|
||||
|
||||
Icons based or directly from GNOME and other GNOME projects, licensed GPL.
|
||||
You can visit the GNOME website here:
|
||||
http://www.gnome.org/
|
||||
|
||||
Icons based on Tango sources or taken from the Tango project are public domain.
|
||||
You can visit the Tango project website here:
|
||||
http://tango.freedesktop.org/Tango_Desktop_Project
|
BIN
gui/icons/Humanity/dialog-error.png
Normal file
BIN
gui/icons/Humanity/dialog-error.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
BIN
gui/icons/Humanity/dialog-info.png
Normal file
BIN
gui/icons/Humanity/dialog-info.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 KiB |
BIN
gui/icons/Humanity/dialog-question.png
Normal file
BIN
gui/icons/Humanity/dialog-question.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3 KiB |
BIN
gui/icons/Humanity/dialog-warning.png
Normal file
BIN
gui/icons/Humanity/dialog-warning.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2 KiB |
Loading…
Reference in a new issue