1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/common/clrpickercmn.cpp 
   3 // Purpose:     wxColourPickerCtrl class implementation 
   4 // Author:      Francesco Montorsi (readapted code written by Vadim Zeitlin) 
   8 // Copyright:   (c) Vadim Zeitlin, Francesco Montorsi 
   9 // Licence:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  27 #if wxUSE_COLOURPICKERCTRL 
  29 #include "wx/clrpicker.h" 
  32     #include "wx/textctrl.h" 
  35 const char wxColourPickerCtrlNameStr
[] = "colourpicker"; 
  36 const char wxColourPickerWidgetNameStr
[] = "colourpickerwidget"; 
  38 // ============================================================================ 
  40 // ============================================================================ 
  42 wxDEFINE_EVENT(wxEVT_COMMAND_COLOURPICKER_CHANGED
, wxColourPickerEvent
); 
  43 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl
, wxPickerBase
) 
  44 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent
, wxEvent
) 
  46 // ---------------------------------------------------------------------------- 
  48 // ---------------------------------------------------------------------------- 
  50 #define M_PICKER     ((wxColourPickerWidget*)m_picker) 
  52 bool wxColourPickerCtrl::Create( wxWindow 
*parent
, wxWindowID id
, 
  54                         const wxPoint 
&pos
, const wxSize 
&size
, 
  55                         long style
, const wxValidator
& validator
, 
  56                         const wxString 
&name 
) 
  58     if (!wxPickerBase::CreateBase(parent
, id
, col
.GetAsString(), pos
, size
, 
  59                                   style
, validator
, name
)) 
  62     // we are not interested to the ID of our picker as we connect 
  63     // to its "changed" event dynamically... 
  64     m_picker 
= new wxColourPickerWidget(this, wxID_ANY
, col
, 
  65                                         wxDefaultPosition
, wxDefaultSize
, 
  66                                         GetPickerStyle(style
)); 
  68     // complete sizer creation 
  69     wxPickerBase::PostCreation(); 
  71     m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
, 
  72             wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
), 
  78 void wxColourPickerCtrl::SetColour(const wxColour 
&col
) 
  80     M_PICKER
->SetColour(col
); 
  81     UpdateTextCtrlFromPicker(); 
  84 bool wxColourPickerCtrl::SetColour(const wxString 
&text
) 
  86     wxColour 
col(text
);     // smart wxString->wxColour conversion 
  89     M_PICKER
->SetColour(col
); 
  90     UpdateTextCtrlFromPicker(); 
  95 void wxColourPickerCtrl::UpdatePickerFromTextCtrl() 
  99     if (m_bIgnoreNextTextCtrlUpdate
) 
 101         // ignore this update 
 102         m_bIgnoreNextTextCtrlUpdate 
= false; 
 106     // wxString -> wxColour conversion 
 107     wxColour 
col(m_text
->GetValue()); 
 109         return;     // invalid user input 
 111     if (M_PICKER
->GetColour() != col
) 
 113         M_PICKER
->SetColour(col
); 
 116         wxColourPickerEvent 
event(this, GetId(), col
); 
 117         GetEventHandler()->ProcessEvent(event
); 
 121 void wxColourPickerCtrl::UpdateTextCtrlFromPicker() 
 124         return;     // no textctrl to update 
 126     // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED 
 127     //       which will trigger a unneeded UpdateFromTextCtrl(); thus before using 
 128     //       SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag... 
 129     m_bIgnoreNextTextCtrlUpdate 
= true; 
 130     m_text
->SetValue(M_PICKER
->GetColour().GetAsString()); 
 135 // ---------------------------------------------------------------------------- 
 136 // wxColourPickerCtrl - event handlers 
 137 // ---------------------------------------------------------------------------- 
 139 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent 
&ev
) 
 141     UpdateTextCtrlFromPicker(); 
 143     // the wxColourPickerWidget sent us a colour-change notification. 
 144     // forward this event to our parent 
 145     wxColourPickerEvent 
event(this, GetId(), ev
.GetColour()); 
 146     GetEventHandler()->ProcessEvent(event
); 
 149 #endif  // wxUSE_COLOURPICKERCTRL