Custom button and container

Started by Nafffen, 16 March 2023, 18:16:02


I want to make a custom widget that is a clickable widget and contains other widgets.
Is this ok to inherits from ClickableWidget AND SubwidgetContainer ?
Is there a better way ?

Also, what is the difference between onClick of ClickableWidget and onPress of Button class ?


You should never inherit from 2 classes which both inherit from the Widget class.

ClickableWidget isn't that complex, you could in theory just copy its code in your custom widget if you really need it. Note that the main purpose of ClickableWidget is to provide a widget with the onClick signal, you don't actually need to inherit from it to support mouse events in your widget.

So you probably don't want to inherit from ClickableWidget and only inherit from SubwidgetContainer. Since SubwidgetContainer already forwards mouse events to the child widgets, the ClickableWidget implementation that consumes all mouse events itself wouldn't even be compatible anyway.

onPress will also be triggered when the button has focus and the user presses the space or return key on the keyboard, onClick will only be triggered as a result of a mouse click.


Ok thank you for the tips

What is the function to override if I want to do stuff when mouse leave widget but with left click pressed ?
I tried mouseNoLongerOnWidget and mouseLeftWidget but no one worked with left click pressed


You currently will have to detect this yourself in your mouseMoved function (by checking if the mouse position lies outside the widget).

Draggable widgets (i.e. those that have "m_draggableWidget = true;" in their constructor, e.g. Slider or Scrollbar) will not receive mouseNoLongerOnWidget or mouseLeftWidget events when the mouse leaves with the button pressed. Instead they will continue to receive mouseMoved events until the mouse is released, even when the mouse is no longer on top of the widget.

Currently, containers (like SubwidgetContainer or Panel) are treated as if they are draggable widgets. This is why you don't receive mouseNoLongerOnWidget when you expect it.

Maybe this is something that should be changed. If I remember correctly then the reason for this is that a container could have a draggable widget inside it, so the mouse event is passed to the container just in case. If this really is the only reason, then a better design inside TGUI could be to check whether the leaf widget is draggable or not, and only pass mouse events to the container when that is the case.