From f0a615ed825f8ad8c0a3cb1db8bbfe78338a6c72 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Thu, 2 Apr 2015 11:06:50 +0300 Subject: [PATCH] ML parser improvements; allow specifying percent size for layoutWidth, layoutHeight in percents --- dlangui.sln | 3 + examples/dmledit/src/dmledit.d | 122 +++++++++++++++++- .../dmledit/views/res/mdpi/document-new.png | Bin 0 -> 516 bytes examples/dmledit/views/resources.list | 1 + src/dlangui/core/parser.d | 55 +++++++- src/dlangui/core/types.d | 23 ++++ src/dlangui/package.d | 1 + src/dlangui/widgets/appframe.d | 15 +-- src/dlangui/widgets/styles.d | 6 - src/dlangui/widgets/widget.d | 8 +- 10 files changed, 215 insertions(+), 19 deletions(-) create mode 100644 examples/dmledit/views/res/mdpi/document-new.png diff --git a/dlangui.sln b/dlangui.sln index c0b832ec..5bba1782 100644 --- a/dlangui.sln +++ b/dlangui.sln @@ -34,6 +34,9 @@ EndProject Project("{002A2DE9-8BB6-484D-9802-7E4AD4084715}") = "dsfml", "..\DSFML\dsfml\dsfml.visualdproj", "{DB490C05-D9F8-431C-91DD-CEE646A64FDA}" EndProject Project("{002A2DE9-8BB6-484D-9802-7E4AD4084715}") = "dmledit", "examples\dmledit\dmledit.visualdproj", "{06D73450-2919-48A8-B2C3-738B12505D74}" + ProjectSection(ProjectDependencies) = postProject + {5FF17402-9997-4D0E-8068-6D84B8769D98} = {5FF17402-9997-4D0E-8068-6D84B8769D98} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/examples/dmledit/src/dmledit.d b/examples/dmledit/src/dmledit.d index d15abbf9..bb52f93e 100644 --- a/examples/dmledit/src/dmledit.d +++ b/examples/dmledit/src/dmledit.d @@ -4,13 +4,131 @@ import dlangui; mixin APP_ENTRY_POINT; +// action codes +enum IDEActions : int { + //ProjectOpen = 1010000, + FileNew = 1010000, + FileOpen, + FileSave, + FileSaveAs, + FileSaveAll, + FileClose, + FileExit, + EditPreferences, +} + +// actions +const Action ACTION_FILE_NEW = new Action(IDEActions.FileNew, "MENU_FILE_NEW"c, "document-new", KeyCode.KEY_N, KeyFlag.Control); +const Action ACTION_FILE_SAVE = (new Action(IDEActions.FileSave, "MENU_FILE_SAVE"c, "document-save", KeyCode.KEY_S, KeyFlag.Control)).disableByDefault(); +const Action ACTION_FILE_SAVE_AS = (new Action(IDEActions.FileSaveAs, "MENU_FILE_SAVE_AS"c)).disableByDefault(); +const Action ACTION_FILE_OPEN = new Action(IDEActions.FileOpen, "MENU_FILE_OPEN"c, "document-open", KeyCode.KEY_O, KeyFlag.Control); +const Action ACTION_FILE_EXIT = new Action(IDEActions.FileExit, "MENU_FILE_EXIT"c, "document-close"c, KeyCode.KEY_X, KeyFlag.Alt); +const Action ACTION_EDIT_COPY = (new Action(EditorActions.Copy, "MENU_EDIT_COPY"c, "edit-copy"c, KeyCode.KEY_C, KeyFlag.Control)).addAccelerator(KeyCode.INS, KeyFlag.Control).disableByDefault(); +const Action ACTION_EDIT_PASTE = (new Action(EditorActions.Paste, "MENU_EDIT_PASTE"c, "edit-paste"c, KeyCode.KEY_V, KeyFlag.Control)).addAccelerator(KeyCode.INS, KeyFlag.Shift).disableByDefault(); +const Action ACTION_EDIT_CUT = (new Action(EditorActions.Cut, "MENU_EDIT_CUT"c, "edit-cut"c, KeyCode.KEY_X, KeyFlag.Control)).addAccelerator(KeyCode.DEL, KeyFlag.Shift).disableByDefault(); +const Action ACTION_EDIT_UNDO = (new Action(EditorActions.Undo, "MENU_EDIT_UNDO"c, "edit-undo"c, KeyCode.KEY_Z, KeyFlag.Control)).disableByDefault(); +const Action ACTION_EDIT_REDO = (new Action(EditorActions.Redo, "MENU_EDIT_REDO"c, "edit-redo"c, KeyCode.KEY_Y, KeyFlag.Control)).addAccelerator(KeyCode.KEY_Z, KeyFlag.Control|KeyFlag.Shift).disableByDefault(); +const Action ACTION_EDIT_INDENT = (new Action(EditorActions.Indent, "MENU_EDIT_INDENT"c, "edit-indent"c, KeyCode.TAB, 0)).addAccelerator(KeyCode.KEY_BRACKETCLOSE, KeyFlag.Control).disableByDefault(); +const Action ACTION_EDIT_UNINDENT = (new Action(EditorActions.Unindent, "MENU_EDIT_UNINDENT"c, "edit-unindent", KeyCode.TAB, KeyFlag.Shift)).addAccelerator(KeyCode.KEY_BRACKETOPEN, KeyFlag.Control).disableByDefault(); +const Action ACTION_EDIT_TOGGLE_LINE_COMMENT = (new Action(EditorActions.ToggleLineComment, "MENU_EDIT_TOGGLE_LINE_COMMENT"c, null, KeyCode.KEY_DIVIDE, KeyFlag.Control)).disableByDefault(); +const Action ACTION_EDIT_TOGGLE_BLOCK_COMMENT = (new Action(EditorActions.ToggleBlockComment, "MENU_EDIT_TOGGLE_BLOCK_COMMENT"c, null, KeyCode.KEY_DIVIDE, KeyFlag.Control|KeyFlag.Shift)).disableByDefault(); +const Action ACTION_EDIT_PREFERENCES = (new Action(IDEActions.EditPreferences, "MENU_EDIT_PREFERENCES"c, null)).disableByDefault(); + +class EditFrame : AppFrame { + + MenuItem mainMenuItems; + + override protected void init() { + _appName = "DMLEdit"; + super.init(); + } + /// create main menu + override protected MainMenu createMainMenu() { + return new MainMenu(new MenuItem()); + mainMenuItems = new MenuItem(); + MenuItem fileItem = new MenuItem(new Action(1, "MENU_FILE")); + fileItem.add(ACTION_FILE_NEW, ACTION_FILE_OPEN, + ACTION_FILE_EXIT); + + MenuItem editItem = new MenuItem(new Action(2, "MENU_EDIT")); + editItem.add(ACTION_EDIT_COPY, ACTION_EDIT_PASTE, + ACTION_EDIT_CUT, ACTION_EDIT_UNDO, ACTION_EDIT_REDO, + ACTION_EDIT_INDENT, ACTION_EDIT_UNINDENT, ACTION_EDIT_TOGGLE_LINE_COMMENT, ACTION_EDIT_TOGGLE_BLOCK_COMMENT); + + editItem.add(ACTION_EDIT_PREFERENCES); + + MainMenu mainMenu = new MainMenu(mainMenuItems); + return mainMenu; + } + + + /// create app toolbars + override protected ToolBarHost createToolbars() { + ToolBarHost res = new ToolBarHost(); + ToolBar tb; + tb = res.getOrAddToolbar("Standard"); + tb.addButtons(ACTION_FILE_NEW, ACTION_FILE_OPEN, ACTION_FILE_SAVE); + + tb = res.getOrAddToolbar("Edit"); + tb.addButtons(ACTION_EDIT_COPY, ACTION_EDIT_PASTE, ACTION_EDIT_CUT, ACTION_SEPARATOR, + ACTION_EDIT_UNDO, ACTION_EDIT_REDO, ACTION_EDIT_INDENT, ACTION_EDIT_UNINDENT); + return res; + } + + /// create app body widget + override protected Widget createBody() { + VerticalLayout bodyWidget = new VerticalLayout(); + bodyWidget.layoutWidth = FILL_PARENT; + bodyWidget.layoutHeight = FILL_PARENT; + HorizontalLayout hlayout = new HorizontalLayout(); + hlayout.layoutWidth = makePercentSize(50); + hlayout.layoutHeight = FILL_PARENT; + SourceEdit editor = new SourceEdit(); + hlayout.addChild(editor); + ScrollWidget preview = new ScrollWidget(); + preview.layoutWidth = FILL_PARENT; + preview.layoutHeight = FILL_PARENT; + hlayout.addChild(preview); + bodyWidget.addChild(hlayout); + return bodyWidget; + } + +} + /// entry point for dlangui based application extern (C) int UIAppMain(string[] args) { + + // embed non-standard resources listed in views/resources.list into executable + embeddedResourceList.addResources(embedResourcesFromList!("resources.list")()); + // create window - Window window = Platform.instance.createWindow("DlangUI example - HelloWorld", null); + Window window = Platform.instance.createWindow("DlangUI ML editor"d, null, WindowFlag.Resizable, 700, 470); // create some widget to show in window - window.mainWidget = (new Button()).text("Hello, world!"d).margins(Rect(20,20,20,20)); + window.windowIcon = drawableCache.getImage("dlangui-logo1"); + + + // create some widget to show in window + window.mainWidget = new EditFrame(); + /* + parseML(q{ + VerticalLayout { + id: vlayout + margins: Rect { left: 5; right: 3; top: 2; bottom: 4 } + padding: Rect { 5, 4, 3, 2 } // same as Rect { left: 5; top: 4; right: 3; bottom: 2 } + TextWidget { + id: myLabel1 + text: "Some text"; padding: 5 + enabled: false + } + TextWidget { + id: myLabel2 + text: SOME_TEXT_RESOURCE_ID; margins: 5 + enabled: true + } + } + }); + */ // show window window.show(); diff --git a/examples/dmledit/views/res/mdpi/document-new.png b/examples/dmledit/views/res/mdpi/document-new.png new file mode 100644 index 0000000000000000000000000000000000000000..a956a809eef79b53266de1afc493c49493ab4b01 GIT binary patch literal 516 zcmV+f0{i`mP)S2M4FYj6tW0irAsvdyYYY6tyKSjP+SyCt#sFg%t}N-kRp_d7I9HfFr=Gc zR^2HEUD%m?%;aMxnItpEdxp8gWNOm`hj0nc^WOJ{h{`q9)l2(Gc8mMPziZ#t{FQer zzvY!Y1R%_+Wmz~gO<2t)TJ1L4oeny-4cl?xbi3HA@1gvqjE&O`Fpt$|>1!#Fm+~MI zoSE|lg|`LdKjm@vs0ilXaS^wU^N<(vBN8-QEwF?&*i~S!*F(SG$C9yx|3#jQ7YR7A z9m9?W2LrgS3(xaVI4WTAwcxfYbN{{I=Al6L;~Nl6U^Q`|V1mQp5Tnru#!mwa2Mf6I zM!=MnVDUf#`5^0&fM5d0QwjRMkMVdM)%ay#xw4GhVGi<9{>W!pPksennbngCYj84| zM4e8jNy!jS1l*TR!&xv00^Re1LHuCcIXyWIhGD3CUZD5p&RJMPJo7u-c0$FJW(02h z;upb= WRAP_CONTENT; -} - /// Align option bit constants enum Align : ubyte { /// alignment is not specified diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index c922cfff..f2dab4cd 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -1223,9 +1223,13 @@ class Widget { // check for fixed size set in layoutWidth, layoutHeight int lh = layoutHeight; int lw = layoutWidth; - if (!isSpecialSize(lh)) + if (isPercentSize(lh) && parentHeight != SIZE_UNSPECIFIED) + dy = fromPercentSize(lh, parentHeight); + else if (!isSpecialSize(lh)) dy = lh; - if (!isSpecialSize(lw)) + if (isPercentSize(lw) && parentWidth != SIZE_UNSPECIFIED) + dy = fromPercentSize(lw, parentWidth); + else if (!isSpecialSize(lw)) dx = lw; // apply min/max width and height constraints int minw = minWidth;