diff --git a/docs/Lua API.rst b/docs/Lua API.rst index c0dec4b6e..04257b49d 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -3832,6 +3832,7 @@ Subclass of Widget; implements a simple edit field. Attributes: +:label_text: The optional text label displayed before the editable text. :text: The current contents of the field. :text_pen: The pen to draw the text with. :on_char: Input validation callback; used as ``on_char(new_char,text)``. @@ -3839,6 +3840,8 @@ Attributes: :on_change: Change notification callback; used as ``on_change(new_text,old_text)``. :on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``. :key: If specified, the field is disabled until this key is pressed. Must be given as a string. +:key_sep: If specified, will be used to customize how the activation key is + displayed. See ``token.key_sep`` in the ``Label`` documentation below. Label class ----------- @@ -3907,8 +3910,8 @@ containing newlines, or a table with the following possible fields: * ``token.key_sep = '...'`` Specifies the separator to place between the keybinding label produced - by ``token.key``, and the main text of the token. If the separator is - '()', the token is formatted as ``text..' ('..binding..')'``. Otherwise + by ``token.key``, and the main text of the token. If the separator starts with + '()', the token is formatted as ``text..' ('..binding..sep:sub(2)``. Otherwise it is simply ``binding..sep..text``. * ``token.enabled``, ``token.disabled`` @@ -3999,6 +4002,8 @@ a hotkey. It has the following attributes: :key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``. +:key_sep: If specified, will be used to customize how the activation key is + displayed. See ``token.key_sep`` in the ``Label`` documentation. :label: The string (or a function that returns a string) to display after the hotkey. :on_activate: If specified, it is the callback that will be called whenever diff --git a/docs/changelog.txt b/docs/changelog.txt index 14fb4db22..325070526 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -48,6 +48,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## API ## Lua +- ``widgets.HotkeyLabel``: the ``key_sep`` string is now configurable +- ``widgets.EditField``: the ``key_sep`` string is now configurable +- ``widgets.EditField``: can now display an optional string label in addition to the activation key # 0.47.05-r5 diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 6a13d2af5..21e03370b 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -178,14 +178,27 @@ end EditField = defclass(EditField, Widget) EditField.ATTRS{ + label_text = DEFAULT_NIL, text = '', text_pen = DEFAULT_NIL, on_char = DEFAULT_NIL, on_change = DEFAULT_NIL, on_submit = DEFAULT_NIL, key = DEFAULT_NIL, + key_sep = DEFAULT_NIL, } +function EditField:init() + self:addviews{HotkeyLabel{frame={t=0,l=0}, + key=self.key, + key_sep=self.key_sep, + label=self.label_text}} +end + +function EditField:postUpdateLayout() + self.text_offset = self.subviews[1]:getTextWidth() +end + function EditField:onRenderBody(dc) dc:pen(self.text_pen or COLOR_LIGHTCYAN):fill(0,0,dc.width-1,0) @@ -194,16 +207,11 @@ function EditField:onRenderBody(dc) cursor = ' ' end local txt = self.text .. cursor - local dx = dc.x - if self.key then - dc:key_string(self.key, '') - end - dx = dc.x - dx - local max_width = dc.width - dx + local max_width = dc.width - self.text_offset if #txt > max_width then txt = string.char(27)..string.sub(txt, #txt-max_width+2) end - dc:string(txt) + dc:advance(self.text_offset):string(txt) end function EditField:onInput(keys) @@ -359,12 +367,13 @@ function render_text(obj,dc,x0,y0,pen,dpen,disabled) x = x + #keystr - if sep == '()' then + if sep:startswith('()') then if dc then dc:string(text) - dc:string(' ('):string(keystr,keypen):string(')') + dc:string(' ('):string(keystr,keypen) + dc:string(sep:sub(2)) end - x = x + 3 + x = x + 1 + #sep else if dc then dc:string(keystr,keypen):string(sep):string(text) @@ -605,13 +614,14 @@ HotkeyLabel = defclass(HotkeyLabel, Label) HotkeyLabel.ATTRS{ key=DEFAULT_NIL, + key_sep=': ', label=DEFAULT_NIL, on_activate=DEFAULT_NIL, } function HotkeyLabel:init() - self:setText{{key=self.key, key_sep=': ', text=self.label, - on_activate=self.on_activate}} + self:setText{{key=self.key, key_sep=self.key_sep, text=self.label, + on_activate=self.on_activate}} end ----------------------