Add wxEVT_COLOURPICKER_CURRENT_CHANGED and DIALOG_CANCELLED events

Send events from generic wxColourPickerCtrl when the currently selected
colour in the dialog shown by it changes and when this dialog is
cancelled.

Notice that currently this only works on wxMSW as it relies on
wxEVT_COLOUR_CHANGED support in wxColourDialog which is only available
there.

Based on work of Trylz, see https://github.com/wxWidgets/wxWidgets/pull/1219
This commit is contained in:
Vadim Zeitlin
2019-04-20 21:50:21 +02:00
parent 35c16935f1
commit 807d95e07d
6 changed files with 80 additions and 10 deletions

View File

@@ -159,13 +159,15 @@ private:
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent );
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COLOURPICKER_CURRENT_CHANGED, wxColourPickerEvent );
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COLOURPICKER_DIALOG_CANCELLED, wxColourPickerEvent );
class WXDLLIMPEXP_CORE wxColourPickerEvent : public wxCommandEvent class WXDLLIMPEXP_CORE wxColourPickerEvent : public wxCommandEvent
{ {
public: public:
wxColourPickerEvent() {} wxColourPickerEvent() {}
wxColourPickerEvent(wxObject *generator, int id, const wxColour &col) wxColourPickerEvent(wxObject *generator, int id, const wxColour &col, wxEventType commandType = wxEVT_COLOURPICKER_CHANGED)
: wxCommandEvent(wxEVT_COLOURPICKER_CHANGED, id), : wxCommandEvent(commandType, id),
m_colour(col) m_colour(col)
{ {
SetEventObject(generator); SetEventObject(generator);
@@ -196,6 +198,11 @@ typedef void (wxEvtHandler::*wxColourPickerEventFunction)(wxColourPickerEvent&);
#define EVT_COLOURPICKER_CHANGED(id, fn) \ #define EVT_COLOURPICKER_CHANGED(id, fn) \
wx__DECLARE_EVT1(wxEVT_COLOURPICKER_CHANGED, id, wxColourPickerEventHandler(fn)) wx__DECLARE_EVT1(wxEVT_COLOURPICKER_CHANGED, id, wxColourPickerEventHandler(fn))
#define EVT_COLOURPICKER_CURRENT_CHANGED(id, fn) \
wx__DECLARE_EVT1(wxEVT_COLOURPICKER_CURRENT_CHANGED, id, wxColourPickerEventHandler(fn))
#define EVT_COLOURPICKER_DIALOG_CANCELLED(id, fn) \
wx__DECLARE_EVT1(wxEVT_COLOURPICKER_DIALOG_CANCELLED, id, wxColourPickerEventHandler(fn))
// old wxEVT_COMMAND_* constant // old wxEVT_COMMAND_* constant
#define wxEVT_COMMAND_COLOURPICKER_CHANGED wxEVT_COLOURPICKER_CHANGED #define wxEVT_COMMAND_COLOURPICKER_CHANGED wxEVT_COLOURPICKER_CHANGED

View File

@@ -15,6 +15,8 @@
#include "wx/bmpbuttn.h" #include "wx/bmpbuttn.h"
#include "wx/colourdata.h" #include "wx/colourdata.h"
class wxColourDialogEvent;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxGenericColourButton: a button which brings up a wxColourDialog // wxGenericColourButton: a button which brings up a wxColourDialog
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -75,6 +77,8 @@ protected:
static wxColourData ms_data; static wxColourData ms_data;
private: private:
void OnColourChanged(wxColourDialogEvent& event);
wxDECLARE_DYNAMIC_CLASS(wxGenericColourButton); wxDECLARE_DYNAMIC_CLASS(wxGenericColourButton);
}; };

View File

@@ -44,7 +44,20 @@ wxEventType wxEVT_COLOURPICKER_CHANGED;
The user changed the colour selected in the control either using the The user changed the colour selected in the control either using the
button or using text control (see @c wxCLRP_USE_TEXTCTRL; note that button or using text control (see @c wxCLRP_USE_TEXTCTRL; note that
in this case the event is fired only if the users input is valid, in this case the event is fired only if the users input is valid,
i.e. recognizable). i.e. recognizable). When using a popup dialog for changing the
colour, this event is sent only when the changes in the dialog are
accepted by the user, unlike @c EVT_COLOURPICKER_CURRENT_CHANGED.
@event{EVT_COLOURPICKER_CURRENT_CHANGED(id, func)}
The user changed the currently selected colour in the dialog
associated with the control. This event is sent immediately when the
selection changes and you must also handle @c EVT_COLOUR_CANCELLED
to revert to the previously selected colour if the selection ends up
not being accepted. This event is new since wxWidgets 3.1.3 and
currently is only implemented in wxMSW.
@event{EVT_COLOURPICKER_DIALOG_CANCELLED(id, func)}
The user cancelled the colour dialog associated with the control,
i.e. closed it without accepting the selection. This event is new
since wxWidgets 3.1.3 and currently is only implemented in wxMSW.
@endEventTable @endEventTable
@library{wxcore} @library{wxcore}
@@ -124,6 +137,15 @@ public:
@beginEventTable{wxColourPickerEvent} @beginEventTable{wxColourPickerEvent}
@event{EVT_COLOURPICKER_CHANGED(id, func)} @event{EVT_COLOURPICKER_CHANGED(id, func)}
Generated whenever the selected colour changes. Generated whenever the selected colour changes.
@event{EVT_COLOURPICKER_CURRENT_CHANGED(id, func)}
Generated whenever the currently selected colour in the dialog shown
by the picker changes. This event is new since wxWidgets 3.1.3 and
currently is only implemented in wxMSW.
@event{EVT_COLOURPICKER_DIALOG_CANCELLED(id, func)}
Generated when the user cancels the colour dialog associated with
the control, i.e. closes it without accepting the selection. This
event is new since wxWidgets 3.1.3 and currently is only implemented
in wxMSW.
@endEventTable @endEventTable
@library{wxcore} @library{wxcore}

View File

@@ -83,6 +83,9 @@ protected:
void OnColourChange(wxColourPickerEvent &ev); void OnColourChange(wxColourPickerEvent &ev);
void OnColourCurrentChanged(wxColourPickerEvent &ev);
void OnColourDialogCancelled(wxColourPickerEvent &ev);
void OnCheckBox(wxCommandEvent &ev); void OnCheckBox(wxCommandEvent &ev);
void OnButtonReset(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev);
@@ -111,6 +114,8 @@ wxBEGIN_EVENT_TABLE(ColourPickerWidgetsPage, WidgetsPage)
EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset) EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset)
EVT_COLOURPICKER_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourChange) EVT_COLOURPICKER_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourChange)
EVT_COLOURPICKER_CURRENT_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourCurrentChanged)
EVT_COLOURPICKER_DIALOG_CANCELLED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourDialogCancelled)
EVT_CHECKBOX(wxID_ANY, ColourPickerWidgetsPage::OnCheckBox) EVT_CHECKBOX(wxID_ANY, ColourPickerWidgetsPage::OnCheckBox)
wxEND_EVENT_TABLE() wxEND_EVENT_TABLE()
@@ -221,6 +226,18 @@ void ColourPickerWidgetsPage::OnColourChange(wxColourPickerEvent& event)
event.GetColour().GetAsString(wxC2S_CSS_SYNTAX)); event.GetColour().GetAsString(wxC2S_CSS_SYNTAX));
} }
void ColourPickerWidgetsPage::OnColourCurrentChanged(wxColourPickerEvent& event)
{
wxLogMessage("The currently selected colour changed to '%s'",
event.GetColour().GetAsString(wxC2S_CSS_SYNTAX));
}
void ColourPickerWidgetsPage::OnColourDialogCancelled(wxColourPickerEvent& event)
{
wxLogMessage("Colour selection dialog cancelled, current colour is '%s'",
event.GetColour().GetAsString(wxC2S_CSS_SYNTAX));
}
void ColourPickerWidgetsPage::OnCheckBox(wxCommandEvent &event) void ColourPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
{ {
if (event.GetEventObject() == m_chkColourTextCtrl || if (event.GetEventObject() == m_chkColourTextCtrl ||

View File

@@ -39,6 +39,9 @@ const char wxColourPickerWidgetNameStr[] = "colourpickerwidget";
// ============================================================================ // ============================================================================
wxDEFINE_EVENT(wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent); wxDEFINE_EVENT(wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent);
wxDEFINE_EVENT(wxEVT_COLOURPICKER_CURRENT_CHANGED, wxColourPickerEvent);
wxDEFINE_EVENT(wxEVT_COLOURPICKER_DIALOG_CANCELLED, wxColourPickerEvent);
wxIMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase); wxIMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase);
wxIMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent); wxIMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent);

View File

@@ -86,19 +86,36 @@ void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
// create the colour dialog and display it // create the colour dialog and display it
wxColourDialog dlg(this, &ms_data); wxColourDialog dlg(this, &ms_data);
dlg.Bind(wxEVT_COLOUR_CHANGED, &wxGenericColourButton::OnColourChanged, this);
wxEventType eventType;
if (dlg.ShowModal() == wxID_OK) if (dlg.ShowModal() == wxID_OK)
{ {
ms_data = dlg.GetColourData(); ms_data = dlg.GetColourData();
SetColour(ms_data.GetColour()); SetColour(ms_data.GetColour());
// Fire the corresponding event: note that we want it to appear as eventType = wxEVT_COLOURPICKER_CHANGED;
// originating from our parent, which is the user-visible window, and not
// this button itself, which is just an implementation detail.
wxWindow* const parent = GetParent();
wxColourPickerEvent event(parent, parent->GetId(), m_colour);
ProcessWindowEvent(event);
} }
else
{
eventType = wxEVT_COLOURPICKER_DIALOG_CANCELLED;
}
// Fire the corresponding event: note that we want it to appear as
// originating from our parent, which is the user-visible window, and not
// this button itself, which is just an implementation detail.
wxWindow* const parent = GetParent();
wxColourPickerEvent event(parent, parent->GetId(), m_colour, eventType);
ProcessWindowEvent(event);
}
void wxGenericColourButton::OnColourChanged(wxColourDialogEvent& ev)
{
wxWindow* const parent = GetParent();
wxColourPickerEvent event(parent, parent->GetId(), ev.GetColour(),
wxEVT_COLOURPICKER_CURRENT_CHANGED);
parent->ProcessWindowEvent(event);
} }
void wxGenericColourButton::UpdateColour() void wxGenericColourButton::UpdateColour()