From 9e878d2c162c560c6d8c8dc2c5d5337e3b99c6ea Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 9 Dec 2015 17:00:17 +0300 Subject: [PATCH] add inputbox dialog; add New Folder button to file dialog --- dlangui-monod-linux.dproj | 1 + dlangui-monod-osx.dproj | 1 + dlangui-msvc.visualdproj | 1 + src/dlangui/core/stdaction.d | 2 ++ src/dlangui/dialogs/filedlg.d | 24 ++++++++++++- src/dlangui/dialogs/inputbox.d | 48 +++++++++++++++++++++++++ src/dlangui/platforms/common/platform.d | 12 ++++++- views/res/i18n/std_en.ini | 5 +++ views/res/i18n/std_ru.ini | 5 +++ 9 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/dlangui/dialogs/inputbox.d diff --git a/dlangui-monod-linux.dproj b/dlangui-monod-linux.dproj index 9870f768..b319c843 100644 --- a/dlangui-monod-linux.dproj +++ b/dlangui-monod-linux.dproj @@ -260,6 +260,7 @@ + diff --git a/dlangui-monod-osx.dproj b/dlangui-monod-osx.dproj index 427c4153..981ade2f 100644 --- a/dlangui-monod-osx.dproj +++ b/dlangui-monod-osx.dproj @@ -120,6 +120,7 @@ + diff --git a/dlangui-msvc.visualdproj b/dlangui-msvc.visualdproj index acd884f3..bad7d76f 100644 --- a/dlangui-msvc.visualdproj +++ b/dlangui-msvc.visualdproj @@ -578,6 +578,7 @@ + diff --git a/src/dlangui/core/stdaction.d b/src/dlangui/core/stdaction.d index 600647e3..eee32bec 100644 --- a/src/dlangui/core/stdaction.d +++ b/src/dlangui/core/stdaction.d @@ -36,6 +36,7 @@ enum StandardAction : int { OpenUrl, Apply, OpenDirectory, + CreateDirectory, } const Action ACTION_OK = new Action(StandardAction.Ok, "ACTION_OK"c, "dialog-ok"); @@ -49,6 +50,7 @@ const Action ACTION_RETRY = new Action(StandardAction.Retry, "ACTION_RETRY"c); const Action ACTION_IGNORE = new Action(StandardAction.Ignore, "ACTION_IGNORE"c); const Action ACTION_OPEN = new Action(StandardAction.Open, "ACTION_OPEN"c); const Action ACTION_OPEN_DIRECTORY = new Action(StandardAction.OpenDirectory, "ACTION_OPEN_DIRECTORY"c); +const Action ACTION_CREATE_DIRECTORY = new Action(StandardAction.CreateDirectory, "ACTION_CREATE_DIRECTORY"c); const Action ACTION_SAVE = new Action(StandardAction.Save, "ACTION_SAVE"c); const Action ACTION_SAVE_ALL = new Action(StandardAction.SaveAll, "ACTION_SAVE_ALL"c); const Action ACTION_DISCARD_CHANGES = new Action(StandardAction.DiscardChanges, "ACTION_DISCARD_CHANGES"c); diff --git a/src/dlangui/dialogs/filedlg.d b/src/dlangui/dialogs/filedlg.d index b8a1dbc5..368399b4 100644 --- a/src/dlangui/dialogs/filedlg.d +++ b/src/dlangui/dialogs/filedlg.d @@ -315,12 +315,30 @@ class FileDialog : Dialog, CustomGridCellAdapter { } } + protected void createAndEnterDirectory(string name) { + string newdir = buildNormalizedPath(_path, name); + try { + mkdirRecurse(newdir); + openDirectory(newdir, null); + } catch (Exception e) { + window.showMessageBox(UIString("CREATE_FOLDER_ERROR_TITLE"c), UIString("CREATE_FOLDER_ERROR_MESSAGE"c)); + } + } + /// Custom handling of actions override bool handleAction(const Action action) { if (action.id == StandardAction.Cancel) { super.handleAction(action); return true; } + if (action.id == StandardAction.CreateDirectory) { + // show editor popup + window.showInputBox(UIString("CREATE_NEW_FOLDER"c), UIString("INPUT_NAME_FOR_FOLDER"c), ""d, delegate(dstring s) { + if (!s.empty) + createAndEnterDirectory(toUTF8(s)); + }); + return true; + } if (action.id == StandardAction.Open || action.id == StandardAction.OpenDirectory || action.id == StandardAction.Save) { if (_filename.length > 0 || action.id == StandardAction.OpenDirectory) { Action result = _action; @@ -411,7 +429,11 @@ class FileDialog : Dialog, CustomGridCellAdapter { addChild(content); - addChild(createButtonsPanel([cast(immutable)_action, ACTION_CANCEL], 0, 0)); + if (_flags & FileDialogFlag.EnableCreateDirectory) { + addChild(createButtonsPanel([ACTION_CREATE_DIRECTORY, cast(immutable)_action, ACTION_CANCEL], 1, 1)); + } else { + addChild(createButtonsPanel([cast(immutable)_action, ACTION_CANCEL], 0, 0)); + } _fileList.customCellAdapter = this; _fileList.onCellActivated = delegate(GridWidgetBase source, int col, int row) { diff --git a/src/dlangui/dialogs/inputbox.d b/src/dlangui/dialogs/inputbox.d new file mode 100644 index 00000000..237797c5 --- /dev/null +++ b/src/dlangui/dialogs/inputbox.d @@ -0,0 +1,48 @@ +module dlangui.dialogs.inputbox; + +import dlangui.core.i18n; +import dlangui.core.signals; +import dlangui.core.stdaction; +import dlangui.widgets.layouts; +import dlangui.widgets.controls; +import dlangui.widgets.editors; +import dlangui.platforms.common.platform; +import dlangui.dialogs.dialog; + +/// Message box +class InputBox : Dialog { + protected UIString _message; + protected const(Action)[] _actions; + protected int _defaultButtonIndex; + protected dstring _text; + this(UIString caption, UIString message, Window parentWindow, dstring initialText, void delegate(dstring result) handler) { + super(caption, parentWindow, DialogFlag.Modal | DialogFlag.Popup); + _message = message; + _actions = [ACTION_OK, ACTION_CANCEL]; + _defaultButtonIndex = 0; + _text = initialText; + if (handler) { + dialogResult = delegate (Dialog dlg, const Action action) { + if (action.id == ACTION_OK.id) { + handler(_text); + } + }; + } + } + /// override to implement creation of dialog controls + override void init() { + TextWidget msg = new MultilineTextWidget("msg", _message); + padding(Rect(10, 10, 10, 10)); + msg.padding(Rect(10, 10, 10, 10)); + EditLine editor = new EditLine("inputbox_editor"); + editor.layoutWidth = FILL_PARENT; + editor.text = _text; + editor.contentChange = delegate(EditableContent content) { + _text = content.text; + }; + addChild(msg); + addChild(editor); + addChild(createButtonsPanel(_actions, _defaultButtonIndex, 0)); + } + +} diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index 44e7b904..ad5d57d5 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -26,7 +26,6 @@ import dlangui.widgets.widget; import dlangui.widgets.popup; import dlangui.graphics.drawbuf; import dlangui.core.stdaction; -import dlangui.dialogs.msgbox; private import dlangui.graphics.gldrawbuf; private import std.algorithm; @@ -1065,6 +1064,7 @@ class Window { /// Show message box with specified title and message (title and message as UIString) void showMessageBox(UIString title, UIString message, const (Action)[] actions = [ACTION_OK], int defaultActionIndex = 0, bool delegate(const Action result) handler = null) { + import dlangui.dialogs.msgbox; MessageBox dlg = new MessageBox(title, message, this, actions, defaultActionIndex, handler); dlg.show(); } @@ -1074,6 +1074,16 @@ class Window { showMessageBox(UIString(title), UIString(message), actions, defaultActionIndex, handler); } + void showInputBox(UIString title, UIString message, dstring initialText, void delegate(dstring result) handler) { + import dlangui.dialogs.inputbox; + InputBox dlg = new InputBox(title, message, this, initialText, handler); + dlg.show(); + } + + void showInputBox(dstring title, dstring message, dstring initialText, void delegate(dstring result) handler) { + showInputBox(UIString(title), UIString(message), initialText, handler); + } + protected TimerQueue _timerQueue; diff --git a/views/res/i18n/std_en.ini b/views/res/i18n/std_en.ini index 7aeb2940..1e216bd8 100644 --- a/views/res/i18n/std_en.ini +++ b/views/res/i18n/std_en.ini @@ -15,3 +15,8 @@ ACTION_SAVE=Save ACTION_SAVE_ALL=Save All ACTION_DISCARD_CHANGES=Discard ACTION_DISCARD_ALL=Discard all +ACTION_CREATE_DIRECTORY=New folder +CREATE_NEW_FOLDER=Create new folder +INPUT_NAME_FOR_FOLDER=Input folder name +CREATE_FOLDER_ERROR_TITLE=Cannot create folder +CREATE_FOLDER_ERROR_MESSAGE=Folder creation is failed diff --git a/views/res/i18n/std_ru.ini b/views/res/i18n/std_ru.ini index 76a4ccb8..7e65cb9e 100644 --- a/views/res/i18n/std_ru.ini +++ b/views/res/i18n/std_ru.ini @@ -11,3 +11,8 @@ ACTION_IGNORE=Игнорировать ACTION_OPEN=Открыть ACTION_OPEN_DIRECTORY=Выбрать папку ACTION_SAVE=Сохранить +ACTION_CREATE_DIRECTORY=Новая папка +CREATE_NEW_FOLDER=Создать папку +INPUT_NAME_FOR_FOLDER=Введите имя для папки +CREATE_FOLDER_ERROR_TITLE=Ошибка сосдания папки +CREATE_FOLDER_ERROR_MESSAGE=Не удалось создать папку