added wx{Colour|File|Dir|Font}PickerCtrl (patch 1472329 by Francesco)
[wxWidgets.git] / src / common / clrpickercmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/clrpickercmn.cpp
3 // Purpose: wxColourPickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/clrpicker.h"
28
29
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 #if wxUSE_COLOURPICKERCTRL
36
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED)
38 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
39 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
40
41 // ----------------------------------------------------------------------------
42 // wxColourPickerCtrl
43 // ----------------------------------------------------------------------------
44
45 #define M_PICKER ((wxColourPickerWidget*)m_picker)
46
47 bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
48 const wxColour &col,
49 const wxPoint &pos, const wxSize &size,
50 long style, const wxValidator& validator,
51 const wxString &name )
52 {
53 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
54 style, validator, name))
55 return false;
56
57 // we are not interested to the ID of our picker as we connect
58 // to its "changed" event dynamically...
59 m_picker = new wxColourPickerWidget(this, wxID_ANY, col, wxPoint(40,0), wxSize(30,-1),
60 GetPickerStyle(style));
61 m_picker->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED,
62 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange),
63 NULL, this);
64
65 return true;
66 }
67
68 void wxColourPickerCtrl::SetColour(const wxColour &col)
69 {
70 M_PICKER->SetColour(col);
71 UpdateTextCtrlFromPicker();
72 }
73
74 bool wxColourPickerCtrl::SetColour(const wxString &text)
75 {
76 wxColour col(text); // smart wxString->wxColour conversion
77 if ( !col.Ok() )
78 return false;
79 M_PICKER->SetColour(col);
80 UpdateTextCtrlFromPicker();
81
82 return true;
83 }
84
85 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
86 {
87 wxASSERT(m_text);
88
89 if (m_bIgnoreNextTextCtrlUpdate)
90 {
91 // ignore this update
92 m_bIgnoreNextTextCtrlUpdate = false;
93 return;
94 }
95
96 // wxString -> wxColour conversion
97 wxColour col(m_text->GetValue());
98 if ( !col.Ok() )
99 return; // invalid user input
100
101 if (M_PICKER->GetColour() != col)
102 {
103 M_PICKER->SetColour(col);
104
105 // fire an event
106 wxColourPickerEvent event(this, GetId(), col);
107 GetEventHandler()->ProcessEvent(event);
108 }
109 }
110
111 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
112 {
113 if (!m_text)
114 return; // no textctrl to update
115
116 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
117 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
118 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
119 m_bIgnoreNextTextCtrlUpdate = true;
120 m_text->SetValue(M_PICKER->GetColour().GetAsString());
121 }
122
123
124
125 // ----------------------------------------------------------------------------
126 // wxColourPickerCtrl - event handlers
127 // ----------------------------------------------------------------------------
128
129 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
130 {
131 UpdateTextCtrlFromPicker();
132
133 // the wxColourPickerWidget sent us a colour-change notification.
134 // forward this event to our parent
135 wxColourPickerEvent event(this, GetId(), ev.GetColour());
136 GetEventHandler()->ProcessEvent(event);
137 }
138
139 #endif // wxUSE_COLOURPICKERCTRL