]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/clrpickercmn.cpp
don't crash on weird line endings like \r\r\n
[wxWidgets.git] / src / common / clrpickercmn.cpp
... / ...
CommitLineData
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#if wxUSE_COLOURPICKERCTRL
28
29#include "wx/clrpicker.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33#endif
34
35
36// ============================================================================
37// implementation
38// ============================================================================
39
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED)
41IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
42IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
43
44// ----------------------------------------------------------------------------
45// wxColourPickerCtrl
46// ----------------------------------------------------------------------------
47
48#define M_PICKER ((wxColourPickerWidget*)m_picker)
49
50bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
51 const wxColour &col,
52 const wxPoint &pos, const wxSize &size,
53 long style, const wxValidator& validator,
54 const wxString &name )
55{
56 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
57 style, validator, name))
58 return false;
59
60 // we are not interested to the ID of our picker as we connect
61 // to its "changed" event dynamically...
62 m_picker = new wxColourPickerWidget(this, wxID_ANY, col,
63 wxDefaultPosition, wxDefaultSize,
64 GetPickerStyle(style));
65
66 // complete sizer creation
67 wxPickerBase::PostCreation();
68
69 m_picker->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED,
70 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange),
71 NULL, this);
72
73 return true;
74}
75
76void wxColourPickerCtrl::SetColour(const wxColour &col)
77{
78 M_PICKER->SetColour(col);
79 UpdateTextCtrlFromPicker();
80}
81
82bool wxColourPickerCtrl::SetColour(const wxString &text)
83{
84 wxColour col(text); // smart wxString->wxColour conversion
85 if ( !col.Ok() )
86 return false;
87 M_PICKER->SetColour(col);
88 UpdateTextCtrlFromPicker();
89
90 return true;
91}
92
93void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
94{
95 wxASSERT(m_text);
96
97 if (m_bIgnoreNextTextCtrlUpdate)
98 {
99 // ignore this update
100 m_bIgnoreNextTextCtrlUpdate = false;
101 return;
102 }
103
104 // wxString -> wxColour conversion
105 wxColour col(m_text->GetValue());
106 if ( !col.Ok() )
107 return; // invalid user input
108
109 if (M_PICKER->GetColour() != col)
110 {
111 M_PICKER->SetColour(col);
112
113 // fire an event
114 wxColourPickerEvent event(this, GetId(), col);
115 GetEventHandler()->ProcessEvent(event);
116 }
117}
118
119void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
120{
121 if (!m_text)
122 return; // no textctrl to update
123
124 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
125 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
126 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
127 m_bIgnoreNextTextCtrlUpdate = true;
128 m_text->SetValue(M_PICKER->GetColour().GetAsString());
129}
130
131
132
133// ----------------------------------------------------------------------------
134// wxColourPickerCtrl - event handlers
135// ----------------------------------------------------------------------------
136
137void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
138{
139 UpdateTextCtrlFromPicker();
140
141 // the wxColourPickerWidget sent us a colour-change notification.
142 // forward this event to our parent
143 wxColourPickerEvent event(this, GetId(), ev.GetColour());
144 GetEventHandler()->ProcessEvent(event);
145}
146
147#endif // wxUSE_COLOURPICKERCTRL