Merge branch 'updateui-ischeckable'

Add a flag to wxUpdateUIEvent to tell if the item supports the check
action.

See https://github.com/wxWidgets/wxWidgets/pull/2027
This commit is contained in:
Vadim Zeitlin
2020-09-02 19:34:05 +02:00
8 changed files with 53 additions and 7 deletions

View File

@@ -211,6 +211,11 @@ public:
void SetAlignment(int l) { m_alignment = l; }
int GetAlignment() const { return m_alignment; }
bool CanBeToggled() const
{
return m_kind == wxITEM_CHECK || m_kind == wxITEM_RADIO;
}
private:
wxWindow* m_window; // item's associated window

View File

@@ -2892,6 +2892,7 @@ public:
m_setShown =
m_setText =
m_setChecked = false;
m_isCheckable = true;
}
wxUpdateUIEvent(const wxUpdateUIEvent& event)
: wxCommandEvent(event),
@@ -2902,6 +2903,7 @@ public:
m_setShown(event.m_setShown),
m_setText(event.m_setText),
m_setChecked(event.m_setChecked),
m_isCheckable(event.m_isCheckable),
m_text(event.m_text)
{ }
@@ -2919,6 +2921,10 @@ public:
void Show(bool show) { m_shown = show; m_setShown = true; }
void SetText(const wxString& text) { m_text = text; m_setText = true; }
// A flag saying if the item can be checked. True by default.
bool IsCheckable() const { return m_isCheckable; }
void DisallowCheck() { m_isCheckable = false; }
// Sets the interval between updates in milliseconds.
// Set to -1 to disable updates, or to 0 to update as frequently as possible.
static void SetUpdateInterval(long updateInterval) { sm_updateInterval = updateInterval; }
@@ -2951,6 +2957,7 @@ protected:
bool m_setShown;
bool m_setText;
bool m_setChecked;
bool m_isCheckable;
wxString m_text;
#if wxUSE_LONGLONG
static wxLongLong sm_lastUpdate;

View File

@@ -407,6 +407,13 @@ public:
*/
int GetAlignment() const;
/**
Returns whether the toolbar item can be toggled.
@since 3.1.5
*/
bool CanBeToggled() const;
};
/**

View File

@@ -2374,6 +2374,26 @@ public:
*/
bool GetEnabled() const;
/**
Returns @true if the UI element can be checked.
For the event handlers that can be used for multiple items, not all of
which can be checked, this method can be useful to determine whether
to call Check() on the event object or not, i.e. the main use case for
this method is:
@code
void MyWindow::OnUpdateUI(wxUpdateUIEvent& event)
{
....
if ( event.IsCheckable() )
event.Check(...some condition...);
}
@endcode
@since 3.1.5
*/
bool IsCheckable() const;
/**
Static function returning a value specifying how wxWidgets will send update
events: to all windows, or only to those which specify that they will process

View File

@@ -1546,7 +1546,7 @@ void wxAuiToolBar::ToggleTool(int tool_id, bool state)
{
wxAuiToolBarItem* tool = FindTool(tool_id);
if (tool && (tool->m_kind == wxITEM_CHECK || tool->m_kind == wxITEM_RADIO))
if ( tool && tool->CanBeToggled() )
{
if (tool->m_kind == wxITEM_RADIO)
{
@@ -1587,13 +1587,8 @@ bool wxAuiToolBar::GetToolToggled(int tool_id) const
{
wxAuiToolBarItem* tool = FindTool(tool_id);
if (tool)
{
if ( (tool->m_kind != wxITEM_CHECK) && (tool->m_kind != wxITEM_RADIO) )
return false;
if ( tool && tool->CanBeToggled() )
return (tool->m_state & wxAUI_BUTTON_STATE_CHECKED) ? true : false;
}
return false;
}
@@ -2200,6 +2195,9 @@ void wxAuiToolBar::DoIdleUpdate()
wxUpdateUIEvent evt(item.m_toolId);
evt.SetEventObject(this);
if ( !item.CanBeToggled() )
evt.DisallowCheck();
if (handler->ProcessEvent(evt))
{
if (evt.GetSetEnabled())

View File

@@ -617,6 +617,9 @@ void wxMenuBase::UpdateUI(wxEvtHandler* source)
wxUpdateUIEvent event(itemid);
event.SetEventObject( this );
if ( !item->IsCheckable() )
event.DisallowCheck();
if ( source->ProcessEvent(event) )
{
// if anything changed, update the changed attribute

View File

@@ -739,6 +739,9 @@ void wxToolBarBase::UpdateWindowUI(long flags)
wxUpdateUIEvent event(toolid);
event.SetEventObject(this);
if ( !tool->CanBeToggled() )
event.DisallowCheck();
if ( evtHandler->ProcessEvent(event) )
{
if ( event.GetSetEnabled() )

View File

@@ -324,6 +324,9 @@ bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow
wxUpdateUIEvent event(menuid);
event.SetEventObject( this );
if ( !item || !item->IsCheckable() )
event.DisallowCheck();
bool processed = DoProcessEvent(this, event, GetWindow());
if ( !processed && senderWindow != NULL)