]> git.saurik.com Git - wxWidgets.git/blob - src/common/pickerbase.cpp
43215b3cfe7d244adf4ded92d4689574a7285b83
[wxWidgets.git] / src / common / pickerbase.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/pickerbase.cpp
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
27 #if wxUSE_COLOURPICKERCTRL || \
28 wxUSE_DIRPICKERCTRL || \
29 wxUSE_FILEPICKERCTRL || \
30 wxUSE_FONTPICKERCTRL
31
32 #include "wx/pickerbase.h"
33 #include "wx/tooltip.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/textctrl.h"
37 #endif
38
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl)
45
46 BEGIN_EVENT_TABLE(wxPickerBase, wxControl)
47 EVT_SIZE(wxPickerBase::OnSize)
48 END_EVENT_TABLE()
49
50
51 // ----------------------------------------------------------------------------
52 // wxPickerBase
53 // ----------------------------------------------------------------------------
54
55 bool wxPickerBase::CreateBase(wxWindow *parent,
56 wxWindowID id,
57 const wxString &text,
58 const wxPoint& pos,
59 const wxSize& size,
60 long style,
61 const wxValidator& validator,
62 const wxString& name)
63 {
64 // remove any border style from our style as wxPickerBase's window must be
65 // invisible (user styles must be set on the textctrl or the platform-dependent picker)
66 style &= ~wxBORDER_MASK;
67
68 if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL,
69 validator, name))
70 return false;
71
72 SetMinSize( size );
73
74 m_sizer = new wxBoxSizer(wxHORIZONTAL);
75
76 if (HasFlag(wxPB_USE_TEXTCTRL))
77 {
78 // NOTE: the style of this class (wxPickerBase) and the style of the
79 // attached text control are different: GetTextCtrlStyle() extracts
80 // the styles related to the textctrl from the styles passed here
81 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
82 wxDefaultPosition, wxDefaultSize,
83 GetTextCtrlStyle(style));
84 if (!m_text)
85 {
86 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
87 return false;
88 }
89
90 // set the maximum lenght allowed for this textctrl.
91 // This is very important since any change to it will trigger an update in
92 // the m_picker; for very long strings, this real-time synchronization could
93 // become a CPU-blocker and thus should be avoided.
94 // 32 characters will be more than enough for all common uses.
95 m_text->SetMaxLength(32);
96
97 // set the initial contents of the textctrl
98 m_text->SetValue(text);
99
100 m_text->Connect(m_text->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
101 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate),
102 NULL, this);
103 m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS,
104 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus),
105 NULL, this);
106
107 m_text->Connect(m_text->GetId(), wxEVT_DESTROY,
108 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete),
109 NULL, this);
110
111 // the text control's proportion values defaults to 2
112 m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5);
113 }
114
115 return true;
116 }
117
118 void wxPickerBase::PostCreation()
119 {
120 // the picker's proportion value defaults to 1 when there's no text control
121 // associated with it - in that case it defaults to 0
122 m_sizer->Add(m_picker, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5);
123
124 // For aesthetic reasons, make sure the picker is at least as high as the
125 // associated text control and is always at least square.
126 const wxSize pickerBestSize(m_picker->GetBestSize());
127 const wxSize textBestSize( HasTextCtrl() ? m_text->GetBestSize() : wxSize());
128 wxSize pickerMinSize;
129 pickerMinSize.y = wxMax(pickerBestSize.y, textBestSize.y);
130 pickerMinSize.x = wxMax(pickerBestSize.x, pickerMinSize.y);
131 if ( pickerMinSize != pickerBestSize )
132 m_picker->SetMinSize(pickerMinSize);
133
134 SetSizer(m_sizer);
135
136 SetInitialSize( GetMinSize() );
137 }
138
139 #if wxUSE_TOOLTIPS
140
141 void wxPickerBase::DoSetToolTip(wxToolTip *tip)
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
149 if ( m_text )
150 m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL);
151 }
152
153 #endif // wxUSE_TOOLTIPS
154
155 // ----------------------------------------------------------------------------
156 // wxPickerBase - event handlers
157 // ----------------------------------------------------------------------------
158
159 void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event)
160 {
161 event.Skip();
162
163 // don't leave the textctrl empty
164 if (m_text && m_text->GetValue().empty())
165 UpdateTextCtrlFromPicker();
166 }
167
168 void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &)
169 {
170 // the textctrl has been deleted; our pointer is invalid!
171 m_text = NULL;
172 }
173
174 void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &)
175 {
176 // for each text-change, update the picker
177 UpdatePickerFromTextCtrl();
178 }
179
180 void wxPickerBase::OnSize(wxSizeEvent &event)
181 {
182 if (GetAutoLayout())
183 Layout();
184 event.Skip();
185 }
186
187 #endif // Any picker in use