1 module dlangui.dialogs.inputbox;
2 
3 import dlangui.core.i18n;
4 import dlangui.core.signals;
5 import dlangui.core.stdaction;
6 import dlangui.widgets.layouts;
7 import dlangui.widgets.controls;
8 import dlangui.widgets.editors;
9 import dlangui.platforms.common.platform;
10 import dlangui.dialogs.dialog;
11 
12 /// Message box
13 class InputBox : Dialog {
14     protected UIString _message;
15     protected const(Action)[] _actions;
16     protected EditLine _editor;
17     protected dstring _text;
18     this(UIString caption, UIString message, Window parentWindow, dstring initialText, void delegate(dstring result) handler) {
19         super(caption, parentWindow, DialogFlag.Modal | (Platform.instance.uiDialogDisplayMode & DialogDisplayMode.inputBoxInPopup ? DialogFlag.Popup : 0));
20         _message = message;
21         _actions = [ACTION_OK, ACTION_CANCEL];
22         _defaultButtonIndex = 0;
23         _text = initialText;
24         if (handler) {
25             dialogResult = delegate (Dialog dlg, const Action action) {
26                 if (action.id == ACTION_OK.id) {
27                     handler(_text);
28                 }
29             };
30         }
31     }
32     /// override to implement creation of dialog controls
33     override void initialize() {
34         TextWidget msg = new MultilineTextWidget("msg", _message);
35         padding(Rect(10, 10, 10, 10));
36         msg.padding(Rect(10, 10, 10, 10));
37         _editor = new EditLine("inputbox_editor");
38         _editor.layoutWidth = FILL_PARENT;
39         _editor.text = _text;
40         _editor.enterKey = delegate (EditWidgetBase editor) {
41             close(_buttonActions[_defaultButtonIndex]);
42             return true;
43         };
44         _editor.contentChange = delegate(EditableContent content) {
45             _text = content.text;
46         };
47         _editor.setDefaultPopupMenu();
48         addChild(msg);
49         addChild(_editor);
50         addChild(createButtonsPanel(_actions, _defaultButtonIndex, 0));
51     }
52 
53     /// called after window with dialog is shown
54     override void onShow() {
55         super.onShow();
56         _editor.selectAll();
57         _editor.setFocus();
58     }
59 
60     override dstring text() const {
61         return _text;
62     }
63 
64     override Widget text(dstring t) {
65         _text = t;
66         return this;
67     }
68 
69     override Widget text(UIString s) {
70         _text = s;
71         return this;
72     }
73 }