From 00e4d207d4077b5fd9cda68092816cdd478f17c2 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 19 Jan 2015 13:47:01 +0300 Subject: [PATCH] example1: support opening of file in separate tab with open file dialog --- dlanguilib.visualdproj | 3 ++- examples/example1/example1.visualdproj | 4 +-- examples/example1/src/main.d | 35 ++++++++++++++++++++++---- src/dlangui/graphics/fonts.d | 8 +++--- src/dlangui/widgets/tabs.d | 14 +++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/dlanguilib.visualdproj b/dlanguilib.visualdproj index ba1bdec5..90e7f257 100644 --- a/dlanguilib.visualdproj +++ b/dlanguilib.visualdproj @@ -66,7 +66,7 @@ 0 DebugFocus 0 - USE_OPENGL Unicode + Unicode 0 0 1 @@ -389,6 +389,7 @@ + diff --git a/examples/example1/example1.visualdproj b/examples/example1/example1.visualdproj index d5a2648f..0be3dd3e 100644 --- a/examples/example1/example1.visualdproj +++ b/examples/example1/example1.visualdproj @@ -66,7 +66,7 @@ 0 0 - USE_OPENGL Unicode + Unicode 0 3 0 @@ -89,7 +89,6 @@ $(OutDir)\$(ProjectName).exe 1 - 2 -profile @@ -184,7 +183,6 @@ $(OutDir)\$(ProjectName).exe 1 - 1 diff --git a/examples/example1/src/main.d b/examples/example1/src/main.d index 6be39320..a8b8e250 100644 --- a/examples/example1/src/main.d +++ b/examples/example1/src/main.d @@ -22,6 +22,8 @@ import dlangui.dialogs.msgbox; import std.stdio; import std.conv; import std.utf; +import std.algorithm; +import std.path; mixin APP_ENTRY_POINT; @@ -186,9 +188,10 @@ extern (C) int UIAppMain(string[] args) { // load theme from file "theme_default.xml" Platform.instance.uiTheme = "theme_default"; - // you can override default hinting mode here + // you can override default hinting mode here (Normal, AutoHint, Disabled) FontManager.instance.hintingMode = HintingMode.Normal; - // you can override antialiasing setting here + // you can override antialiasing setting here (0 means antialiasing always on, some big value = always off) + // fonts with size less than specified value will not be antialiased FontManager.instance.minAnitialiasedFontSize = 0; // 0 means always antialiased // create window @@ -197,6 +200,8 @@ extern (C) int UIAppMain(string[] args) { static if (true) { VerticalLayout contentLayout = new VerticalLayout(); + TabWidget tabs = new TabWidget("TABS"); + //========================================================================= // create main menu @@ -303,8 +308,29 @@ extern (C) int UIAppMain(string[] args) { //dlg.filterIndex = 2; dlg.onDialogResult = delegate(Dialog dlg, const Action result) { if (result.id == ACTION_OPEN.id) { - Log.d("FileDialog.onDialogResult: ", result, " param=", result.stringParam); - window.showMessageBox(UIString("FileOpen result"d), UIString(toUTF32(result.stringParam))); + string filename = result.stringParam; + if (filename.endsWith(".d") || filename.endsWith(".txt") || filename.endsWith(".cpp") || filename.endsWith(".h") || filename.endsWith(".c") + || filename.endsWith(".json") || filename.endsWith(".dd") || filename.endsWith(".ddoc") || filename.endsWith(".xml") || filename.endsWith(".html") + || filename.endsWith(".html") || filename.endsWith(".css") || filename.endsWith(".log") || filename.endsWith(".hpp")) { + // open source file in tab + int index = tabs.tabIndex(filename); + if (index >= 0) { + // file is already opened in tab + tabs.selectTab(index, true); + } else { + SourceEdit editor = new SourceEdit(filename); + if (editor.load(filename)) { + tabs.addTab(editor, toUTF32(baseName(filename))); + tabs.selectTab(filename); + } else { + destroy(editor); + window.showMessageBox(UIString("File open error"d), UIString("Cannot open file "d ~ toUTF32(filename))); + } + } + } else { + Log.d("FileDialog.onDialogResult: ", result, " param=", result.stringParam); + window.showMessageBox(UIString("FileOpen result"d), UIString("Filename: "d ~ toUTF32(filename))); + } } }; @@ -321,7 +347,6 @@ extern (C) int UIAppMain(string[] args) { // ========= create tabs =================== - TabWidget tabs = new TabWidget("TABS"); tabs.onTabChangedListener = delegate(string newTabId, string oldTabId) { window.windowCaption = tabs.tab(newTabId).text.value ~ " - dlangui example 1"d; }; diff --git a/src/dlangui/graphics/fonts.d b/src/dlangui/graphics/fonts.d index ca119969..6d8e055c 100644 --- a/src/dlangui/graphics/fonts.d +++ b/src/dlangui/graphics/fonts.d @@ -463,22 +463,22 @@ class FontManager { /// removes entries not used after last call of checkpoint() or cleanup() abstract void cleanup(); - /// get min font size for antialiased fonts + /// get min font size for antialiased fonts (0 means antialiasing always on, some big value = always off) @property int minAnitialiasedFontSize() { return _minAnitialiasedFontSize; } - /// set new min font size for antialiased fonts + /// set new min font size for antialiased fonts - fonts with size >= specified value will be antialiased (0 means antialiasing always on, some big value = always off) @property void minAnitialiasedFontSize(int size) { _minAnitialiasedFontSize = size; } - /// get current hinting mode + /// get current hinting mode (Normal, AutoHint, Disabled) @property HintingMode hintingMode() { return _hintingMode; } - /// set hinting mode + /// set hinting mode (Normal, AutoHint, Disabled) @property void hintingMode(HintingMode mode) { _hintingMode = mode; } diff --git a/src/dlangui/widgets/tabs.d b/src/dlangui/widgets/tabs.d index 99d54392..7c6aa730 100644 --- a/src/dlangui/widgets/tabs.d +++ b/src/dlangui/widgets/tabs.d @@ -476,6 +476,15 @@ class TabHost : FrameLayout, TabHandler { onTabChangedListener(newActiveTabId, previousTabId); } + /// get tab content widget by id + Widget tabBody(string id) { + for (int i = 0; i < _children.count; i++) { + if (_children[i].compareId(id)) + return _children[i]; + } + return null; + } + /// remove tab TabHost removeTab(string id) { assert(_tabControl !is null, "No TabControl set for TabHost"); @@ -588,6 +597,11 @@ class TabWidget : VerticalLayout, TabHandler { _tabControl.selectTab(index, updateAccess); } + /// get tab content widget by id + Widget tabBody(string id) { + return _tabHost.tabBody(id); + } + /// returns tab item by id (null if index out of range) TabItem tab(int index) { return _tabControl.tab(index);