From b07d0aae9b82dc45f9a70b1305e20cc8a8a11b21 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 17 Oct 2016 10:09:01 +0300 Subject: [PATCH] make tooltips working for list items - fix #303 --- src/dlangui/core/events.d | 8 +++++++ src/dlangui/dialogs/filedlg.d | 1 + src/dlangui/widgets/lists.d | 44 ++++++++++++++++++++++++++++++++++- src/dlangui/widgets/widget.d | 4 ++-- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/dlangui/core/events.d b/src/dlangui/core/events.d index 5a4b6ea5..c4bf586d 100644 --- a/src/dlangui/core/events.d +++ b/src/dlangui/core/events.d @@ -868,6 +868,12 @@ class MouseEvent { /// returns point for mouse cursor position @property Point pos() { return Point(_x, _y); } + /// returns true if no modifier flags are set + @property bool noModifiers() { return (_flags & (MouseFlag.Control | MouseFlag.Alt | MouseFlag.Shift)) == 0; } + /// returns true if any modifier flag is set + @property bool hasModifiers() { return !noModifiers; } + + /// Returns true for ButtonDown event when button is pressed second time in short interval after pressing first time @property bool doubleClick() { if (_action != MouseAction.ButtonDown) @@ -1239,6 +1245,8 @@ class KeyEvent { /// returns true if no modifier flags are set @property bool noModifiers() { return (_flags & (KeyFlag.Control | KeyFlag.Alt | KeyFlag.Menu | KeyFlag.Shift)) == 0; } + /// returns true if any modifier flag is set + @property bool hasModifiers() { return !noModifiers; } /// create key event this(KeyAction action, uint keyCode, uint flags, dstring text = null) { diff --git a/src/dlangui/dialogs/filedlg.d b/src/dlangui/dialogs/filedlg.d index 30bb2172..a634222c 100644 --- a/src/dlangui/dialogs/filedlg.d +++ b/src/dlangui/dialogs/filedlg.d @@ -322,6 +322,7 @@ class FileDialog : Dialog, CustomGridCellAdapter { btn.orientation = Orientation.Vertical; btn.styleId = STYLE_TRANSPARENT_BUTTON_BACKGROUND; btn.focusable = false; + btn.tooltipText = root.path.toUTF32; adapter.add(btn); } res.ownAdapter = adapter; diff --git a/src/dlangui/widgets/lists.d b/src/dlangui/widgets/lists.d index a3e6973b..ce66a4f1 100644 --- a/src/dlangui/widgets/lists.d +++ b/src/dlangui/widgets/lists.d @@ -57,6 +57,11 @@ interface ListAdapter { /// called when theme is changed void onThemeChanged(); + + /// return true to receive mouse events + @property bool wantMouseEvents(); + /// return true to receive keyboard events + @property bool wantKeyEvents(); } /// List adapter for simple list of widget instances @@ -122,6 +127,16 @@ class ListAdapterBase : ListAdapter { /// called when theme is changed void onThemeChanged() { } + + /// return true to receive mouse events + override @property bool wantMouseEvents() { + return false; + } + + /// return true to receive keyboard events + override @property bool wantKeyEvents() { + return false; + } } /// List adapter for simple list of widget instances @@ -176,6 +191,11 @@ class WidgetListAdapter : ListAdapterBase { ~this() { //Log.d("Destroying WidgetListAdapter"); } + + /// return true to receive mouse events + override @property bool wantMouseEvents() { + return true; + } } /** List adapter providing strings only. */ @@ -1283,6 +1303,9 @@ class ListWidget : WidgetGroup, OnScrollHandler, OnAdapterChangeHandler { itemrc.top += rc.top - scrollOffset.y; itemrc.bottom += rc.top - scrollOffset.y; if (itemrc.isPointInside(Point(event.x, event.y))) { + Widget itemWidget; + if (_adapter.wantMouseEvents) + itemWidget = _adapter.itemWidget(i); //Log.d("mouse event action=", event.action, " button=", event.button, " flags=", event.flags); if ((event.flags & (MouseFlag.LButton || MouseFlag.RButton)) || _selectOnHover) { if (_selectedItemIndex != i && itemEnabled(i)) { @@ -1304,12 +1327,31 @@ class ListWidget : WidgetGroup, OnScrollHandler, OnAdapterChangeHandler { } } } + if (itemWidget) { + Widget oldParent = itemWidget.parent; + itemWidget.parent = this; + if (event.action == MouseAction.Move && event.noModifiers && itemWidget.hasTooltip) { + itemWidget.scheduleTooltip(200); + } + //itemWidget.onMouseEvent(event); + itemWidget.parent = oldParent; + } return true; } } return true; } - + /// returns true if item is child of this widget (when deepSearch == true - returns true if item is this widget or one of children inside children tree). + override bool isChild(Widget item, bool deepSearch = true) { + if (_adapter && _adapter.wantMouseEvents) { + for (int i = 0; i < itemCount; i++) { + auto itemWidget = _adapter.itemWidget(i); + if (itemWidget is item) + return true; + } + } + return super.isChild(item, deepSearch); + } } class StringListWidget : ListWidget { diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index f7741d25..fbb28240 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -790,7 +790,7 @@ public: } /// schedule tooltip - protected void scheduleTooltip(long delay = 300, uint alignment = 2 /*PopupAlign.Below*/, int x = 0, int y = 0) { + void scheduleTooltip(long delay = 300, uint alignment = 2 /*PopupAlign.Below*/, int x = 0, int y = 0) { if (auto w = window) w.scheduleTooltip(this, delay, alignment, x, y); } @@ -1253,7 +1253,7 @@ public: return true; } } - if (event.action == MouseAction.Move && event.flags == 0 && hasTooltip) { + if (event.action == MouseAction.Move && !event.hasModifiers && hasTooltip) { scheduleTooltip(200); } if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Right) {