From 807d95e07dde19a531a6d16f8ea5b4941428fcd1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 20 Apr 2019 21:50:21 +0200 Subject: [PATCH] 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 --- include/wx/clrpicker.h | 11 +++++++++-- include/wx/generic/clrpickerg.h | 4 ++++ interface/wx/clrpicker.h | 24 +++++++++++++++++++++++- samples/widgets/clrpicker.cpp | 17 +++++++++++++++++ src/common/clrpickercmn.cpp | 3 +++ src/generic/clrpickerg.cpp | 31 ++++++++++++++++++++++++------- 6 files changed, 80 insertions(+), 10 deletions(-) diff --git a/include/wx/clrpicker.h b/include/wx/clrpicker.h index 0530e309de..4b51a96d0e 100644 --- a/include/wx/clrpicker.h +++ b/include/wx/clrpicker.h @@ -159,13 +159,15 @@ private: // ---------------------------------------------------------------------------- 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 { public: wxColourPickerEvent() {} - wxColourPickerEvent(wxObject *generator, int id, const wxColour &col) - : wxCommandEvent(wxEVT_COLOURPICKER_CHANGED, id), + wxColourPickerEvent(wxObject *generator, int id, const wxColour &col, wxEventType commandType = wxEVT_COLOURPICKER_CHANGED) + : wxCommandEvent(commandType, id), m_colour(col) { SetEventObject(generator); @@ -196,6 +198,11 @@ typedef void (wxEvtHandler::*wxColourPickerEventFunction)(wxColourPickerEvent&); #define EVT_COLOURPICKER_CHANGED(id, 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 #define wxEVT_COMMAND_COLOURPICKER_CHANGED wxEVT_COLOURPICKER_CHANGED diff --git a/include/wx/generic/clrpickerg.h b/include/wx/generic/clrpickerg.h index 84076816c0..275c0494a6 100644 --- a/include/wx/generic/clrpickerg.h +++ b/include/wx/generic/clrpickerg.h @@ -15,6 +15,8 @@ #include "wx/bmpbuttn.h" #include "wx/colourdata.h" +class wxColourDialogEvent; + //----------------------------------------------------------------------------- // wxGenericColourButton: a button which brings up a wxColourDialog //----------------------------------------------------------------------------- @@ -75,6 +77,8 @@ protected: static wxColourData ms_data; private: + void OnColourChanged(wxColourDialogEvent& event); + wxDECLARE_DYNAMIC_CLASS(wxGenericColourButton); }; diff --git a/interface/wx/clrpicker.h b/interface/wx/clrpicker.h index 158457f3f7..d8db2adb38 100644 --- a/interface/wx/clrpicker.h +++ b/interface/wx/clrpicker.h @@ -44,7 +44,20 @@ wxEventType wxEVT_COLOURPICKER_CHANGED; The user changed the colour selected in the control either using the button or using text control (see @c wxCLRP_USE_TEXTCTRL; note that in this case the event is fired only if the user’s 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 @library{wxcore} @@ -124,6 +137,15 @@ public: @beginEventTable{wxColourPickerEvent} @event{EVT_COLOURPICKER_CHANGED(id, func)} 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 @library{wxcore} diff --git a/samples/widgets/clrpicker.cpp b/samples/widgets/clrpicker.cpp index 5f166ee61e..c9e1776568 100644 --- a/samples/widgets/clrpicker.cpp +++ b/samples/widgets/clrpicker.cpp @@ -83,6 +83,9 @@ protected: void OnColourChange(wxColourPickerEvent &ev); + void OnColourCurrentChanged(wxColourPickerEvent &ev); + void OnColourDialogCancelled(wxColourPickerEvent &ev); + void OnCheckBox(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev); @@ -111,6 +114,8 @@ wxBEGIN_EVENT_TABLE(ColourPickerWidgetsPage, WidgetsPage) EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset) 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) wxEND_EVENT_TABLE() @@ -221,6 +226,18 @@ void ColourPickerWidgetsPage::OnColourChange(wxColourPickerEvent& event) 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) { if (event.GetEventObject() == m_chkColourTextCtrl || diff --git a/src/common/clrpickercmn.cpp b/src/common/clrpickercmn.cpp index a7597e979e..ecaaf67641 100644 --- a/src/common/clrpickercmn.cpp +++ b/src/common/clrpickercmn.cpp @@ -39,6 +39,9 @@ const char wxColourPickerWidgetNameStr[] = "colourpickerwidget"; // ============================================================================ 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(wxColourPickerEvent, wxEvent); diff --git a/src/generic/clrpickerg.cpp b/src/generic/clrpickerg.cpp index f9c923f1fc..d0f8434168 100644 --- a/src/generic/clrpickerg.cpp +++ b/src/generic/clrpickerg.cpp @@ -86,19 +86,36 @@ void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev)) // create the colour dialog and display it wxColourDialog dlg(this, &ms_data); + dlg.Bind(wxEVT_COLOUR_CHANGED, &wxGenericColourButton::OnColourChanged, this); + + wxEventType eventType; if (dlg.ShowModal() == wxID_OK) { ms_data = dlg.GetColourData(); SetColour(ms_data.GetColour()); - // 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); - - ProcessWindowEvent(event); + eventType = wxEVT_COLOURPICKER_CHANGED; } + 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()