| 1 |
var MessageBox = { |
| 2 |
Ok: 0x0001, |
| 3 |
Cancel: 0x0002, |
| 4 |
Yes: 0x0004, |
| 5 |
No: 0x0008, |
| 6 |
DontShowAgain: 0x8000, |
| 7 |
new: func |
| 8 |
{ |
| 9 |
return { |
| 10 |
parents: [MessageBox], |
| 11 |
_title: "Message", |
| 12 |
_standard_buttons: MessageBox.Ok, |
| 13 |
_remember_selection: 0 |
| 14 |
}; |
| 15 |
}, |
| 16 |
setTitle: func(title) |
| 17 |
{ |
| 18 |
me._title = title; |
| 19 |
return me; |
| 20 |
}, |
| 21 |
setImage: func(img) |
| 22 |
{ |
| 23 |
if( img != nil and img.find('/') < 0 ) |
| 24 |
me._img = style._dir_icons ~ "/" ~ img ~ ".png"; |
| 25 |
else |
| 26 |
me._img = img; |
| 27 |
return me; |
| 28 |
}, |
| 29 |
setText: func(text) |
| 30 |
{ |
| 31 |
me._text = text; |
| 32 |
return me; |
| 33 |
}, |
| 34 |
setStandardButtons: func(mask) |
| 35 |
{ |
| 36 |
me._standard_buttons = mask; |
| 37 |
return me; |
| 38 |
}, |
| 39 |
exec: func(cb = nil) |
| 40 |
{ |
| 41 |
if( me._standard_buttons & me.DontShowAgain ) |
| 42 |
if( (var sel = me._loadSelection()) != nil ) |
| 43 |
{ |
| 44 |
if( cb != nil ) |
| 45 |
cb(sel); |
| 46 |
return me; |
| 47 |
} |
| 48 |
|
| 49 |
var MARGIN = 12; |
| 50 |
var dlg = canvas.Window.new([300,120], "dialog") |
| 51 |
.setTitle(me._title); |
| 52 |
var root = dlg.getCanvas(1) |
| 53 |
.set("background", style.getColor("bg_color")) |
| 54 |
.createGroup(); |
| 55 |
var vbox = VBoxLayout.new(); |
| 56 |
vbox.setContentsMargin(MARGIN); |
| 57 |
dlg.setLayout(vbox); |
| 58 |
|
| 59 |
var text_box = HBoxLayout.new(); |
| 60 |
vbox.addItem(text_box); |
| 61 |
text_box.setSpacing(MARGIN); |
| 62 |
|
| 63 |
if( me._img != nil ) |
| 64 |
{ |
| 65 |
text_box.addItem( |
| 66 |
gui.widgets.Label.new(root, style, {}) |
| 67 |
.setFixedSize(48, 48) |
| 68 |
.setImage(me._img) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
var label_text = gui.widgets.Label.new(root, style, {wordWrap: 1}) |
| 73 |
.setText(me._text); |
| 74 |
text_box.addItem(label_text, 1); |
| 75 |
|
| 76 |
vbox.addStretch(1); |
| 77 |
|
| 78 |
var button_box = HBoxLayout.new(); |
| 79 |
vbox.addItem(button_box); |
| 80 |
|
| 81 |
if( me._standard_buttons & me.DontShowAgain ) |
| 82 |
{ |
| 83 |
button_box.addItem( |
| 84 |
gui.widgets.CheckBox.new(root, style, {}) |
| 85 |
.setText("Don't show again.") |
| 86 |
.listen("toggled", func(e) { |
| 87 |
me._remember_selection = e.detail.checked |
| 88 |
}) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
button_box.addStretch(1); |
| 93 |
foreach(var button; [me.Ok, me.Cancel, me.Yes, me.No]) |
| 94 |
{ |
| 95 |
if( !(me._standard_buttons & button) ) |
| 96 |
continue; |
| 97 |
|
| 98 |
(func{ |
| 99 |
var b_id = button; |
| 100 |
button_box.addItem( |
| 101 |
gui.widgets.Button.new(root, style, {}) |
| 102 |
.setText(me._button_names[button]) |
| 103 |
.listen("clicked", func { |
| 104 |
dlg.del(); |
| 105 |
if( me._remember_selection and b_id != me.Cancel) |
| 106 |
me._saveSelection(b_id); |
| 107 |
if( cb != nil ) |
| 108 |
cb(b_id); |
| 109 |
}) |
| 110 |
); |
| 111 |
})(); |
| 112 |
} |
| 113 |
|
| 114 |
var w = math.max(300, vbox.sizeHint()[0]); |
| 115 |
dlg.setSize(w, math.max(130, vbox.heightForWidth(w))); |
| 116 |
|
| 117 |
return me; |
| 118 |
}, |
| 119 |
show: func(title, text, icon = nil, cb = nil, buttons = nil) |
| 120 |
{ |
| 121 |
var msg_box = MessageBox.new(); |
| 122 |
msg_box.setTitle(title); |
| 123 |
msg_box.setText(text); |
| 124 |
|
| 125 |
if( buttons == nil or buttons == MessageBox.DontShowAgain ) |
| 126 |
buttons = (buttons or 0) | MessageBox.Ok; |
| 127 |
msg_box.setStandardButtons(buttons); |
| 128 |
|
| 129 |
if( icon != nil ) |
| 130 |
msg_box.setImage(icon); |
| 131 |
|
| 132 |
return msg_box.exec(cb); |
| 133 |
}, |
| 134 |
# Show an error/critical message in a message box |
| 135 |
# |
| 136 |
# @param title |
| 137 |
# @param text |
| 138 |
# @param cb Dialog close callback |
| 139 |
# @param buttons Mask indicating the buttons to show |
| 140 |
# (default: MessageBox.Ok) |
| 141 |
critical: func(title, text, cb = nil, buttons = nil) |
| 142 |
{ |
| 143 |
MessageBox.show(title, text, "dialog-error", cb, buttons); |
| 144 |
}, |
| 145 |
# Show a warning message in a message box |
| 146 |
# |
| 147 |
# @param title |
| 148 |
# @param text |
| 149 |
# @param cb Dialog close callback |
| 150 |
# @param buttons Mask indicating the buttons to show |
| 151 |
# (default: MessageBox.Ok) |
| 152 |
warning: func(title, text, cb = nil, buttons = nil) |
| 153 |
{ |
| 154 |
MessageBox.show(title, text, "dialog-warning", cb, buttons); |
| 155 |
}, |
| 156 |
# Show an informative message in a message box |
| 157 |
# |
| 158 |
# @param title |
| 159 |
# @param text |
| 160 |
# @param cb Dialog close callback |
| 161 |
# @param buttons Mask indicating the buttons to show |
| 162 |
# (default: MessageBox.Ok) |
| 163 |
information: func(title, text, cb = nil, buttons = nil) |
| 164 |
{ |
| 165 |
MessageBox.show(title, text, "dialog-info", cb, buttons); |
| 166 |
}, |
| 167 |
# Show a question in a message box |
| 168 |
# |
| 169 |
# @param title |
| 170 |
# @param text |
| 171 |
# @param cb Dialog close callback |
| 172 |
# @param buttons Mask indicating the buttons to show |
| 173 |
# (default: MessageBox.Yes | MessageBox.No) |
| 174 |
question: func(title, text, cb = nil, buttons = nil) |
| 175 |
{ |
| 176 |
if( buttons == nil or buttons == MessageBox.DontShowAgain ) |
| 177 |
buttons = (buttons or 0) | MessageBox.Yes | MessageBox.No; |
| 178 |
|
| 179 |
MessageBox.show(title, text, "dialog-question", cb, buttons); |
| 180 |
}, |
| 181 |
# private: |
| 182 |
_button_names: {}, |
| 183 |
_loadSelection: func |
| 184 |
{ |
| 185 |
var dlg_id = md5(me._title ~ me._text ~ me._standard_buttons); |
| 186 |
me._save_path = getprop("/sim/fg-home") ~ "/cache/MessageBox/sel-" ~ dlg_id; |
| 187 |
|
| 188 |
if( io.stat(me._save_path) != nil ) |
| 189 |
{ |
| 190 |
logprint(LOG_INFO, "Load dialog selection from '" ~ me._save_path ~ "'"); |
| 191 |
return int(io.readfile(me._save_path)); |
| 192 |
} |
| 193 |
|
| 194 |
return nil; |
| 195 |
}, |
| 196 |
_saveSelection: func(sel) |
| 197 |
{ |
| 198 |
logprint(LOG_INFO, "Saving dialog selection to '" ~ me._save_path ~ "'"); |
| 199 |
|
| 200 |
# ensure directory exists |
| 201 |
os.path.new(me._save_path).create_dir(); |
| 202 |
|
| 203 |
var fh = io.open(me._save_path, "w"); |
| 204 |
io.write(fh, '' ~ sel); |
| 205 |
io.close(fh); |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
# Standard button names |
| 210 |
MessageBox._button_names[ MessageBox.Ok ] = "Ok"; |
| 211 |
MessageBox._button_names[ MessageBox.Cancel ] = "Cancel"; |
| 212 |
MessageBox._button_names[ MessageBox.Yes ] = "Yes"; |
| 213 |
MessageBox._button_names[ MessageBox.No ] = "No"; |