]> git.saurik.com Git - wxWidgets.git/blame - src/common/clrpickercmn.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[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
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
4ce7b1e4
WS
27#if wxUSE_COLOURPICKERCTRL
28
ec376c8f
VZ
29#include "wx/clrpicker.h"
30
fec9cc08
WS
31#ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33#endif
ec376c8f 34
f36e602b
VZ
35const char wxColourPickerCtrlNameStr[] = "colourpicker";
36const char wxColourPickerWidgetNameStr[] = "colourpickerwidget";
ec376c8f
VZ
37
38// ============================================================================
39// implementation
40// ============================================================================
41
9b11752c 42wxDEFINE_EVENT(wxEVT_COMMAND_COLOURPICKER_CHANGED, wxColourPickerEvent);
ec376c8f
VZ
43IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
44IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
45
46// ----------------------------------------------------------------------------
47// wxColourPickerCtrl
48// ----------------------------------------------------------------------------
49
50#define M_PICKER ((wxColourPickerWidget*)m_picker)
51
52bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
53 const wxColour &col,
54 const wxPoint &pos, const wxSize &size,
55 long style, const wxValidator& validator,
56 const wxString &name )
57{
58 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
59 style, validator, name))
60 return false;
61
62 // we are not interested to the ID of our picker as we connect
63 // to its "changed" event dynamically...
e8427971
WS
64 m_picker = new wxColourPickerWidget(this, wxID_ANY, col,
65 wxDefaultPosition, wxDefaultSize,
ec376c8f 66 GetPickerStyle(style));
a65ffcb2
VZ
67
68 // complete sizer creation
69 wxPickerBase::PostCreation();
70
ec376c8f
VZ
71 m_picker->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED,
72 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange),
73 NULL, this);
74
75 return true;
76}
77
78void wxColourPickerCtrl::SetColour(const wxColour &col)
79{
80 M_PICKER->SetColour(col);
81 UpdateTextCtrlFromPicker();
82}
83
84bool wxColourPickerCtrl::SetColour(const wxString &text)
85{
86 wxColour col(text); // smart wxString->wxColour conversion
a1b806b9 87 if ( !col.IsOk() )
ec376c8f
VZ
88 return false;
89 M_PICKER->SetColour(col);
90 UpdateTextCtrlFromPicker();
91
92 return true;
93}
94
95void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
96{
97 wxASSERT(m_text);
98
ec376c8f
VZ
99 // wxString -> wxColour conversion
100 wxColour col(m_text->GetValue());
a1b806b9 101 if ( !col.IsOk() )
ec376c8f
VZ
102 return; // invalid user input
103
104 if (M_PICKER->GetColour() != col)
105 {
106 M_PICKER->SetColour(col);
107
108 // fire an event
109 wxColourPickerEvent event(this, GetId(), col);
110 GetEventHandler()->ProcessEvent(event);
111 }
112}
113
114void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
115{
116 if (!m_text)
117 return; // no textctrl to update
118
44b72116
VZ
119 // Take care to use ChangeValue() here and not SetValue() to avoid
120 // infinite recursion.
121 m_text->ChangeValue(M_PICKER->GetColour().GetAsString());
ec376c8f
VZ
122}
123
124
125
126// ----------------------------------------------------------------------------
127// wxColourPickerCtrl - event handlers
128// ----------------------------------------------------------------------------
129
130void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
131{
132 UpdateTextCtrlFromPicker();
133
134 // the wxColourPickerWidget sent us a colour-change notification.
135 // forward this event to our parent
136 wxColourPickerEvent event(this, GetId(), ev.GetColour());
137 GetEventHandler()->ProcessEvent(event);
138}
139
140#endif // wxUSE_COLOURPICKERCTRL