From 8a7fec4674731eee5d0f977a6d7c7456b7f73ce1 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 18 May 2016 14:02:38 +0300 Subject: [PATCH] fix menu checkboxes and radio buttons --- src/dlangui/core/events.d | 4 ++++ src/dlangui/widgets/menu.d | 19 +++++++++++++------ src/dlangui/widgets/widget.d | 6 +++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/dlangui/core/events.d b/src/dlangui/core/events.d index 3bfa9e42..614b1981 100644 --- a/src/dlangui/core/events.d +++ b/src/dlangui/core/events.d @@ -319,6 +319,10 @@ class Action { } return this; } + @property const(Action) checked(bool newValue) const { + state = ActionState(_state.enabled, _state.visible, newValue); + return this; + } /// returns optional string parameter @property string stringParam() const { diff --git a/src/dlangui/widgets/menu.d b/src/dlangui/widgets/menu.d index eb9abb51..90d1c116 100644 --- a/src/dlangui/widgets/menu.d +++ b/src/dlangui/widgets/menu.d @@ -114,6 +114,10 @@ class MenuItem { /// get check for checkbox or radio button item @property bool checked() { + //if (_checked) { + // Log.d("Menu item is checked"); + // return true; + //} return _checked; } /// check radio button with specified index, uncheck other radio buttons in group (group consists of sequence of radio button items; other item type - end of group) @@ -135,6 +139,8 @@ class MenuItem { @property MenuItem checked(bool flg) { if (_checked == flg) return this; + if (_action) + _action.checked = flg; _checked = flg; if (flg && _parent && type == MenuItemType.Radio) { int index = _parent.subitemIndex(this); @@ -238,9 +244,10 @@ class MenuItem { //if (_action.id == EditorActions.Copy) { // Log.d("Requesting Copy action. Old state: ", _action.state); //} - w.updateActionState(_action, true); + bool actionStateProcessed = w.updateActionState(_action, true, false); _enabled = _action.state.enabled; - _checked = _action.state.checked; + if (actionStateProcessed) + _checked = _action.state.checked; } for (int i = 0; i < _subitems.length; i++) { _subitems[i].updateActionState(w); @@ -620,7 +627,7 @@ class MenuWidgetBase : ListWidget { _parentMenu.onMenuItem(item); else { // top level handling - Log.d("onMenuItem ", item.id); + debug(DebugMenus) Log.d("onMenuItem ", item.id); selectItem(-1); setHoverItem(-1); selectOnHover = false; @@ -691,9 +698,9 @@ class MenuWidgetBase : ListWidget { /// override to handle specific actions state (e.g. change enabled state for supported actions) override bool handleActionStateRequest(const Action a) { if (_menuTogglePreviousFocus) { - Log.d("Menu.handleActionStateRequest forwarding to ", _menuTogglePreviousFocus); + debug(DebugMenus) Log.d("Menu.handleActionStateRequest forwarding to ", _menuTogglePreviousFocus); bool res = _menuTogglePreviousFocus.handleActionStateRequest(a); - Log.d("Menu.handleActionStateRequest forwarding handled successful: ", a.state.toString); + debug(DebugMenus) Log.d("Menu.handleActionStateRequest forwarding handled successful: ", a.state.toString); return res; } return false; @@ -916,7 +923,7 @@ class MainMenu : MenuWidgetBase { // activating! _menuTogglePreviousFocus = window.focusedWidget; //updateActionState(true); - Log.d("MainMenu: updateActionState"); + debug(DebugMenus) Log.d("MainMenu: updateActionState"); _item.updateActionState(this); } super.handleFocusChange(focused); diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index b1b2b0b6..ff1b893a 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -691,7 +691,7 @@ public: /// action to emit on click @property void action(Action action) { _action = action; handleActionStateChanged(); } /// ask for update state of some action (unles force=true, checks window flag actionsUpdateRequested), returns true if action state is changed - bool updateActionState(Action a, bool force = false) { + bool updateActionState(Action a, bool force = false, bool allowDefault = true) { if (Window w = window) { if (!force && !w.actionsUpdateRequested()) return false; @@ -703,7 +703,11 @@ public: if (w.dispatchActionStateRequest(a, this)) { // state is updated //Log.d("updateActionState ", a.label, " found state: ", a.state.toString); + if (allowDefault) + return true; // return 'request dispatched' flag instead of 'changed' } else { + if (!allowDefault) + return false; a.state = a.defaultState; //Log.d("updateActionState ", a.label, " using default state: ", a.state.toString); }