Event Handlers
For normal EA panels, use the Event Bus: MuiEventSink, .Id(...), and OnMuiEvent(...).
This page is for advanced ModernUI handler APIs that still exist for compatibility, reusable controls, and specialised behaviour.
When to use the event bus
Use the event bus when an EA needs to react to common user actions:
- button clicks
- checkbox changes
- spin edit changes
- slider changes
- confirm dialog results
- drawer open/close state
m_bSave=Mui::ButtonPrimary(parent,"Save");
m_bSave.Id(APP_ACTION_SAVE);
When to use low-level handlers
Use handler classes when you are building advanced library-style code, custom reusable controls, or using an API that specifically requires a handler object.
Examples include some legacy button paths, combo paths, tabs, accordions, table/list/tree intent callbacks, date/color callbacks, and custom reusable components.
Ownership rule
The most important rule is ownership:
Some APIs own and delete the handler pointer.
Some APIs store a non-owning pointer.
Check the control or wrapper docs before deciding whether to pass new HandlerClass() or a static/global handler instance.
Button click handler
Buttons can still use MuiClickHandler directly.
class SaveHandler : public MuiClickHandler
{
public:
virtual void OnChanged(MuiRoot &root,MuiEvent &e)
{
Mui::ToastSuccess(root,"Saved");
}
};
Attach to a button:
MuiButtonAction *save=Mui::Button(parent,"Save",new SaveHandler(),160.0,0.0);
Or after creation:
if(button!=NULL)
button.SetHandler(new SaveHandler());
MuiButtonAction owns handlers assigned through its owned handler path. Use new for that path.
Owned vs non-owned examples
| Area | Typical ownership |
|---|---|
Button SetHandler |
Owned |
ButtonRow button handlers |
Owned through the button |
ButtonGroup internal pick handlers |
Owned by the buttons |
| List/table/tree selection intent handlers | Not owned |
MuiTabs::SetHandler(MuiTabChangedHandler*) |
Owned |
MuiTabs::SetTabChangedHandler(...) |
Not owned |
MuiComboBoxHost::SetSelectionChangedHandler |
Not owned |
MuiDialog::SetResultHandler |
Not owned |
MuiDrawer::SetStateChangedHandler |
Not owned |
Safe patterns
For owned handler APIs:
button.SetHandler(new SaveHandler());
For non-owned handler APIs:
static MySelectionHandler s_handler;
table.SetSelectionChangedHandler(&s_handler);
Avoid this for owned APIs:
SaveHandler handler;
button.SetHandler(&handler);
The pointer may be deleted by the control, or may outlive the stack object.