From 9b7f4deaf4f57d1ee4e199ad368c86d77f26b39e Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 5 May 2015 11:16:48 +0300 Subject: [PATCH] DML support improvement --- dlanguilib.visualdproj | 10 +++---- src/dlangui/dml/parser.d | 40 +++++++++++++++++++++++++++- src/dlangui/graphics/colors.d | 2 +- src/dlangui/widgets/combobox.d | 48 +++++++++++++++++++++++++++------- 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/dlanguilib.visualdproj b/dlanguilib.visualdproj index 8b472667..fed3d09a 100644 --- a/dlanguilib.visualdproj +++ b/dlanguilib.visualdproj @@ -53,8 +53,8 @@ $(CC) -c 1 $(DMDInstallDir)windows\bin\dmd.exe - 3rdparty ../DerelictGL3/source ../DerelictUtil/source ../DerelictFT/source ../de_image/source/interfaces ../de_image/source/png $(SolutionDir)/../dlib $(SolutionDir)/../gl3n $(SolutionDir)/../DSFML/src - $(SolutionDir)/views $(SolutionDir)/views/res $(SolutionDir)/views/res/i18n $(SolutionDir)/views/res/mdpi $(SolutionDir)/views/res/hdpi + $(SolutionDir)/../dlangui/3rdparty $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(SolutionDir)/../dlib + $(SolutionDir)/../dlangui/views $(SolutionDir)/../dlangui/views/res $(SolutionDir)/../dlangui/views/res/i18n $(SolutionDir)/../dlangui/views/res/mdpi $(SolutionDir)/../dlangui/views/res/hdpi $(ConfigurationName) $(OutDir) @@ -72,7 +72,7 @@ 0 DebugFocus FontResources 0 - EmbedStandardResources Unicode USE_FREETYPE USE_OPENGL + EmbedStandardResources Unicode USE_FREETYPE 0 0 1 @@ -154,8 +154,8 @@ $(CC) -c 1 $(DMDInstallDir)windows\bin\dmd.exe - 3rdparty ../DerelictGL3/source ../DerelictUtil/source ../DerelictFT/source ../de_image/source/interfaces ../de_image/source/png $(SolutionDir)/../dlib - $(SolutionDir)/views $(SolutionDir)/views/res $(SolutionDir)/views/res/i18n $(SolutionDir)/views/res/mdpi + $(SolutionDir)/../dlangui/3rdparty $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(SolutionDir)/../dlib + $(SolutionDir)/../dlangui/views $(SolutionDir)/../dlangui/views/res $(SolutionDir)/../dlangui/views/res/i18n $(SolutionDir)/../dlangui/views/res/mdpi $(SolutionDir)/../dlangui/views/res/hdpi $(ConfigurationName) $(OutDir) diff --git a/src/dlangui/dml/parser.d b/src/dlangui/dml/parser.d index 1baae9f5..372dc15b 100644 --- a/src/dlangui/dml/parser.d +++ b/src/dlangui/dml/parser.d @@ -342,6 +342,40 @@ class Tokenizer { return _token; } + protected ref const(Token) parseHex(int prefixLen) { + dchar ch = 0; + for (int i = 0; i < prefixLen; i++) + ch = skipChar(); + + uint n = decodeHexDigit(ch); + if (n == uint.max) + return parseError(); + + for(;;) { + ch = skipChar(); + uint digit = decodeHexDigit(ch); + if (digit == uint.max) + break; + n = (n << 4) + digit; + } + string suffix; + if (ch == '%') { + suffix ~= ch; + ch = skipChar(); + } else { + while (ch >= 'a' && ch <= 'z') { + suffix ~= ch; + ch = skipChar(); + } + } + if (isAlphaNum(ch) || ch == '.') + return parseError(); + _token.type = TokenType.integer; + _token.intvalue = n; + _token.text = suffix; + return _token; + } + protected ref const(Token) parseNumber() { dchar ch = peekChar(); uint n = ch - '0'; @@ -429,6 +463,10 @@ class Tokenizer { return parseString(); if (isAlpha(ch)) return parseIdent(); + if (ch == '0' && peekNextChar == 'x') + return parseHex(2); + if (ch == '#') + return parseHex(1); if (isNum(ch)) return parseNumber(); if (ch == '.' && isNum(peekNextChar())) @@ -821,7 +859,7 @@ class MLParser { /// Parse DlangUI ML code public Widget parseML(T = Widget)(string code, string filename = "", Widget context = null) { - MLParser parser = new MLParser(code, filename); + MLParser parser = new MLParser(code, filename, context); scope(exit) destroy(parser); Widget w = parser.parse(); T res = cast(T) w; diff --git a/src/dlangui/graphics/colors.d b/src/dlangui/graphics/colors.d index fc301fb8..6b288bb9 100644 --- a/src/dlangui/graphics/colors.d +++ b/src/dlangui/graphics/colors.d @@ -164,7 +164,7 @@ bool isFullyTransparentColor(uint color) pure nothrow { } /// decodes hex digit (0..9, a..f, A..F), returns uint.max if invalid -uint decodeHexDigit(char ch) { +uint decodeHexDigit(T)(T ch) { if (ch >= '0' && ch <= '9') return ch - '0'; else if (ch >= 'a' && ch <= 'f') diff --git a/src/dlangui/widgets/combobox.d b/src/dlangui/widgets/combobox.d index a08c203a..007bd5bc 100644 --- a/src/dlangui/widgets/combobox.d +++ b/src/dlangui/widgets/combobox.d @@ -151,6 +151,17 @@ class ComboBoxBase : HorizontalLayout, OnClickHandler { init(); } + void setAdapter(ListAdapter adapter, bool ownAdapter = true) { + if (_adapter) { + if (_ownAdapter) + destroy(_adapter); + removeAllChildren(); + } + _adapter = adapter; + _ownAdapter = ownAdapter; + init(); + } + protected void init() { _body = createSelectedItemWidget(); _body.onClickListener = this; @@ -173,27 +184,46 @@ class ComboBoxBase : HorizontalLayout, OnClickHandler { /** ComboBox with list of strings. */ class ComboBox : ComboBoxBase { - protected StringListAdapter _adapter; - /// empty parameter list constructor - for usage by factory this() { this(null); } /// create with ID parameter this(string ID) { - super(ID, (_adapter = new StringListAdapter()), true); + super(ID, new StringListAdapter(), true); } this(string ID, string[] items) { - super(ID, (_adapter = new StringListAdapter(items)), true); + super(ID, new StringListAdapter(items), true); } this(string ID, dstring[] items) { - super(ID, (_adapter = new StringListAdapter(items)), true); + super(ID, new StringListAdapter(items), true); } this(string ID, StringListValue[] items) { - super(ID, (_adapter = new StringListAdapter(items)), true); + super(ID, new StringListAdapter(items), true); + } + + @property void items(string[] itemResourceIds) { + setAdapter(new StringListAdapter(itemResourceIds)); + } + + @property void items(dstring[] items) { + setAdapter(new StringListAdapter(items)); + } + + @property void items(StringListValue[] items) { + setAdapter(new StringListAdapter(items)); + } + + /// returns list of items + @property ref const(UIStringCollection) items() { + return (cast(StringListAdapter)_adapter).items; + } + + @property StringListAdapter adapter() { + return cast(StringListAdapter)_adapter; } @property override dstring text() { @@ -201,7 +231,7 @@ class ComboBox : ComboBoxBase { } @property override Widget text(dstring txt) { - int idx = _adapter.items.indexOf(txt); + int idx = adapter.items.indexOf(txt); if (idx >= 0) { selectedItemIndex = idx; } else { @@ -213,7 +243,7 @@ class ComboBox : ComboBoxBase { } @property override Widget text(UIString txt) { - int idx = _adapter.items.indexOf(txt); + int idx = adapter.items.indexOf(txt); if (idx >= 0) { selectedItemIndex = idx; } else { @@ -225,7 +255,7 @@ class ComboBox : ComboBoxBase { } override @property ComboBoxBase selectedItemIndex(int index) { - _body.text = _adapter.items[index]; + _body.text = adapter.items[index]; return super.selectedItemIndex(index); }