]> git.saurik.com Git - wxWidgets.git/blame - src/common/pickerbase.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / pickerbase.cpp
CommitLineData
ec376c8f 1///////////////////////////////////////////////////////////////////////////////
fec9cc08 2// Name: src/common/pickerbase.cpp
ec376c8f
VZ
3// Purpose: wxPickerBase class implementation
4// Author: Francesco Montorsi
5// Modified by:
6// Created: 15/04/2006
7// RCS-ID: $Id$
8// Copyright: (c) 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
9a6384ca
WS
27#if wxUSE_COLOURPICKERCTRL || \
28 wxUSE_DIRPICKERCTRL || \
29 wxUSE_FILEPICKERCTRL || \
30 wxUSE_FONTPICKERCTRL
31
ec376c8f 32#include "wx/pickerbase.h"
a2dc658b 33#include "wx/tooltip.h"
ec376c8f 34
fec9cc08
WS
35#ifndef WX_PRECOMP
36 #include "wx/textctrl.h"
37#endif
ec376c8f 38
a2dc658b 39
ec376c8f
VZ
40// ============================================================================
41// implementation
42// ============================================================================
43
3c3b3558 44IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl)
ec376c8f
VZ
45
46// ----------------------------------------------------------------------------
47// wxPickerBase
48// ----------------------------------------------------------------------------
49
ec376c8f
VZ
50bool wxPickerBase::CreateBase(wxWindow *parent,
51 wxWindowID id,
52 const wxString &text,
53 const wxPoint& pos,
54 const wxSize& size,
55 long style,
56 const wxValidator& validator,
55b43eaa 57 const wxString& name)
ec376c8f
VZ
58{
59 // remove any border style from our style as wxPickerBase's window must be
60 // invisible (user styles must be set on the textctrl or the platform-dependent picker)
61 style &= ~wxBORDER_MASK;
03647350 62
fec9cc08 63 if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL,
a65ffcb2 64 validator, name))
ec376c8f 65 return false;
03647350 66
4dfa1721 67 SetMinSize( size );
03647350 68
a65ffcb2
VZ
69 m_sizer = new wxBoxSizer(wxHORIZONTAL);
70
ec376c8f
VZ
71 if (HasFlag(wxPB_USE_TEXTCTRL))
72 {
73 // NOTE: the style of this class (wxPickerBase) and the style of the
74 // attached text control are different: GetTextCtrlStyle() extracts
75 // the styles related to the textctrl from the styles passed here
5f6475c1
VZ
76 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
77 wxDefaultPosition, wxDefaultSize,
55b43eaa 78 GetTextCtrlStyle(style));
ec376c8f
VZ
79 if (!m_text)
80 {
81 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
82 return false;
83 }
84
25e3f0c6 85 // set the maximum length allowed for this textctrl.
ec376c8f
VZ
86 // This is very important since any change to it will trigger an update in
87 // the m_picker; for very long strings, this real-time synchronization could
88 // become a CPU-blocker and thus should be avoided.
89 // 32 characters will be more than enough for all common uses.
60433f3f 90 m_text->SetMaxLength(32);
ec376c8f
VZ
91
92 // set the initial contents of the textctrl
93 m_text->SetValue(text);
94
e70abc2d 95 m_text->Connect(m_text->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
ec376c8f
VZ
96 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate),
97 NULL, this);
e70abc2d 98 m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS,
ec376c8f
VZ
99 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus),
100 NULL, this);
101
e70abc2d 102 m_text->Connect(m_text->GetId(), wxEVT_DESTROY,
ec376c8f
VZ
103 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete),
104 NULL, this);
a65ffcb2 105
ecd87e5b 106 // the text control's proportion values defaults to 2
a65ffcb2 107 m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5);
ec376c8f 108 }
03647350 109
ec376c8f
VZ
110 return true;
111}
112
a65ffcb2
VZ
113void wxPickerBase::PostCreation()
114{
ecd87e5b
VZ
115 // the picker's proportion value defaults to 1 when there's no text control
116 // associated with it - in that case it defaults to 0
117 m_sizer->Add(m_picker, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5);
a65ffcb2 118
df5f11fe 119 // For aesthetic reasons, make sure the picker is at least as high as the
75bc8b34
VZ
120 // associated text control and is always at least square, unless we are
121 // explicitly using wxPB_SMALL style to force it to take as little space as
122 // possible.
123 if ( !HasFlag(wxPB_SMALL) )
124 {
125 const wxSize pickerBestSize(m_picker->GetBestSize());
126 const wxSize textBestSize( HasTextCtrl() ? m_text->GetBestSize() : wxSize());
127 wxSize pickerMinSize;
128 pickerMinSize.y = wxMax(pickerBestSize.y, textBestSize.y);
129 pickerMinSize.x = wxMax(pickerBestSize.x, pickerMinSize.y);
130 if ( pickerMinSize != pickerBestSize )
131 m_picker->SetMinSize(pickerMinSize);
132 }
df5f11fe 133
a65ffcb2 134 SetSizer(m_sizer);
03647350 135
4dfa1721 136 SetInitialSize( GetMinSize() );
a65ffcb2
VZ
137}
138
8ef74b15
VZ
139#if wxUSE_TOOLTIPS
140
141void wxPickerBase::DoSetToolTip(wxToolTip *tip)
a2dc658b
VZ
142{
143 // don't set the tooltip on us but rather on our two child windows
144 // as otherwise it would appear only when the cursor is placed on the
145 // small area around the child windows which belong to wxPickerBase
146 m_picker->SetToolTip(tip);
147
148 // do a copy as wxWindow will own the pointer we pass
4f690a1d
VZ
149 if ( m_text )
150 m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL);
a2dc658b
VZ
151}
152
8ef74b15 153#endif // wxUSE_TOOLTIPS
a2dc658b
VZ
154
155// ----------------------------------------------------------------------------
156// wxPickerBase - event handlers
157// ----------------------------------------------------------------------------
158
a7b15169 159void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event)
ec376c8f 160{
a7b15169 161 event.Skip();
ec376c8f
VZ
162
163 // don't leave the textctrl empty
bb4b11a2 164 if (m_text && m_text->GetValue().empty())
ec376c8f
VZ
165 UpdateTextCtrlFromPicker();
166}
167
168void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &)
169{
170 // the textctrl has been deleted; our pointer is invalid!
171 m_text = NULL;
172}
173
174void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &)
175{
176 // for each text-change, update the picker
177 UpdatePickerFromTextCtrl();
178}
179
9a6384ca 180#endif // Any picker in use