]> git.saurik.com Git - wxWidgets.git/blame - src/common/clrpickercmn.cpp
Allow absent checkbox when transferring dimension data
[wxWidgets.git] / src / common / clrpickercmn.cpp
CommitLineData
ec376c8f
VZ
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
ec376c8f
VZ
7// Copyright: (c) Vadim Zeitlin, Francesco Montorsi
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
4ce7b1e4
WS
26#if wxUSE_COLOURPICKERCTRL
27
ec376c8f
VZ
28#include "wx/clrpicker.h"
29
fec9cc08
WS
30#ifndef WX_PRECOMP
31 #include "wx/textctrl.h"
32#endif
ec376c8f 33
f36e602b
VZ
34const char wxColourPickerCtrlNameStr[] = "colourpicker";
35const char wxColourPickerWidgetNameStr[] = "colourpickerwidget";
ec376c8f
VZ
36
37// ============================================================================
38// implementation
39// ============================================================================
40
ce7fe42e 41wxDEFINE_EVENT(wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent);
ec376c8f
VZ
42IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
43IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
44
45// ----------------------------------------------------------------------------
46// wxColourPickerCtrl
47// ----------------------------------------------------------------------------
48
49#define M_PICKER ((wxColourPickerWidget*)m_picker)
50
51bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
52 const wxColour &col,
53 const wxPoint &pos, const wxSize &size,
54 long style, const wxValidator& validator,
55 const wxString &name )
56{
57 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
58 style, validator, name))
59 return false;
60
61 // we are not interested to the ID of our picker as we connect
62 // to its "changed" event dynamically...
e8427971
WS
63 m_picker = new wxColourPickerWidget(this, wxID_ANY, col,
64 wxDefaultPosition, wxDefaultSize,
ec376c8f 65 GetPickerStyle(style));
a65ffcb2
VZ
66
67 // complete sizer creation
68 wxPickerBase::PostCreation();
69
ce7fe42e 70 m_picker->Connect(wxEVT_COLOURPICKER_CHANGED,
ec376c8f
VZ
71 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange),
72 NULL, this);
73
74 return true;
75}
76
77void wxColourPickerCtrl::SetColour(const wxColour &col)
78{
79 M_PICKER->SetColour(col);
80 UpdateTextCtrlFromPicker();
81}
82
83bool wxColourPickerCtrl::SetColour(const wxString &text)
84{
85 wxColour col(text); // smart wxString->wxColour conversion
a1b806b9 86 if ( !col.IsOk() )
ec376c8f
VZ
87 return false;
88 M_PICKER->SetColour(col);
89 UpdateTextCtrlFromPicker();
90
91 return true;
92}
93
94void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
95{
96 wxASSERT(m_text);
97
ec376c8f
VZ
98 // wxString -> wxColour conversion
99 wxColour col(m_text->GetValue());
a1b806b9 100 if ( !col.IsOk() )
ec376c8f
VZ
101 return; // invalid user input
102
103 if (M_PICKER->GetColour() != col)
104 {
105 M_PICKER->SetColour(col);
106
107 // fire an event
108 wxColourPickerEvent event(this, GetId(), col);
109 GetEventHandler()->ProcessEvent(event);
110 }
111}
112
113void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
114{
115 if (!m_text)
116 return; // no textctrl to update
117
44b72116
VZ
118 // Take care to use ChangeValue() here and not SetValue() to avoid
119 // infinite recursion.
120 m_text->ChangeValue(M_PICKER->GetColour().GetAsString());
ec376c8f
VZ
121}
122
123
124
125// ----------------------------------------------------------------------------
126// wxColourPickerCtrl - event handlers
127// ----------------------------------------------------------------------------
128
129void 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