Debian Codegraph @

Nasal/canvas/gui/dialogs/InputDialog.nas

raw
1 var InputDialog = {
2 Ok: 0x0001,
3 Cancel: 0x0002,
4 new: func( title = "Input",
5 label = "",
6 text = "" )
7 {
8 return {
9 parents: [InputDialog],
10 _title: title,
11 _label: label,
12 _text: text
13 };
14 },
15 setTitle: func(title)
16 {
17 me._title = title;
18 return me;
19 },
20 setLabel: func(label)
21 {
22 me._label = label;
23 return me;
24 },
25 setText: func(text)
26 {
27 me._text = text;
28 return me;
29 },
30 exec: func(cb = nil)
31 {
32 var MARGIN = 12;
33 var dlg = canvas.Window.new([300,120], "dialog")
34 .setTitle(me._title);
35 var root = dlg.getCanvas(1)
36 .set("background", style.getColor("bg_color"))
37 .createGroup();
38 var vbox = VBoxLayout.new();
39 vbox.setContentsMargin(MARGIN);
40 dlg.setLayout(vbox);
41
42 vbox.addItem(
43 gui.widgets.Label.new(root, style, {wordWrap: 1})
44 .setText(me._label)
45 );
46
47 var input = gui.widgets.LineEdit.new(root, style, {});
48 vbox.addItem(input);
49 input.setText(me._text);
50 input.setFocus();
51
52 var button_box = HBoxLayout.new();
53 vbox.addItem(button_box);
54
55 button_box.addStretch(1);
56 foreach(var button; [me.Ok, me.Cancel])
57 {
58 (func{
59 var b_id = button;
60 button_box.addItem(
61 gui.widgets.Button.new(root, style, {})
62 .setText(me._button_names[button])
63 .listen("clicked", func {
64 dlg.del();
65 if( cb != nil )
66 cb(b_id, b_id == me.Ok ? input.text() : nil);
67 })
68 );
69 })();
70 }
71
72 var w = math.max(300, vbox.sizeHint()[0]);
73 dlg.setSize(w, vbox.heightForWidth(w));
74
75 return me;
76 },
77 # Show an input dialog to get a text string
78 #
79 # @param title
80 # @param label
81 # @param cb Dialog close callback
82 # @param text Default text
83 getText: func(title, label, cb, text = "")
84 {
85 var dlg = InputDialog.new(title, label, text);
86 return dlg.exec(cb);
87 },
88 # private:
89 _button_names: {}
90 };
91
92 # Standard button names
93 InputDialog._button_names[ InputDialog.Ok ] = "Ok";
94 InputDialog._button_names[ InputDialog.Cancel ] = "Cancel";