]> git.saurik.com Git - wxWidgets.git/blob - src/common/pickerbase.cpp
Move the generic picker files to the GUI common section.
[wxWidgets.git] / src / common / pickerbase.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/pickerbase.h"
28
29 #include "wx/textctrl.h"
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxWindow)
36
37 // ----------------------------------------------------------------------------
38 // wxPickerBase
39 // ----------------------------------------------------------------------------
40
41 wxPickerBase::~wxPickerBase()
42 {
43 // destroy the windows we are managing: these are not automatically
44 // destroyed by wxWindow because they are not built as our children
45 // but rather as children of the parent of the wxPickerBase class
46 // (since wxPickerBase does not represent a real window)
47 if (m_text) m_text->Destroy();
48 if (m_picker) m_picker->Destroy();
49 }
50
51 bool wxPickerBase::CreateBase(wxWindow *parent,
52 wxWindowID id,
53 const wxString &text,
54 const wxPoint& pos,
55 const wxSize& size,
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 // remove any border style from our style as wxPickerBase's window must be
61 // invisible (user styles must be set on the textctrl or the platform-dependent picker)
62 style &= ~wxBORDER_MASK;
63 if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL,
64 validator, name))
65 return false;
66
67 if (HasFlag(wxPB_USE_TEXTCTRL))
68 {
69 // NOTE: the style of this class (wxPickerBase) and the style of the
70 // attached text control are different: GetTextCtrlStyle() extracts
71 // the styles related to the textctrl from the styles passed here
72 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxPoint(0, 0),
73 wxSize(40, size.GetHeight()), GetTextCtrlStyle(style));
74 if (!m_text)
75 {
76 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
77 return false;
78 }
79
80 // set the maximum lenght allowed for this textctrl.
81 // This is very important since any change to it will trigger an update in
82 // the m_picker; for very long strings, this real-time synchronization could
83 // become a CPU-blocker and thus should be avoided.
84 // 32 characters will be more than enough for all common uses.
85 m_text->SetMaxLength(32);
86
87 // set the initial contents of the textctrl
88 m_text->SetValue(text);
89
90 m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED,
91 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate),
92 NULL, this);
93 m_text->Connect(wxEVT_KILL_FOCUS,
94 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus),
95 NULL, this);
96
97 m_text->Connect(wxEVT_DESTROY,
98 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete),
99 NULL, this);
100 }
101
102 return true;
103 }
104
105 void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent &)
106 {
107 wxASSERT(m_text);
108
109 // don't leave the textctrl empty
110 if (m_text->GetValue().IsEmpty())
111 UpdateTextCtrlFromPicker();
112 }
113
114 void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &)
115 {
116 // the textctrl has been deleted; our pointer is invalid!
117 m_text = NULL;
118 }
119
120 void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &)
121 {
122 // for each text-change, update the picker
123 UpdatePickerFromTextCtrl();
124 }
125
126 int wxPickerBase::GetTextCtrlWidth(int given)
127 {
128 // compute the width of m_text like a wxBoxSizer(wxHORIZONTAL) would do
129 // NOTE: the proportion of m_picker is fixed to 1
130 return ((given - m_margin) / (m_textProportion + 1)) * m_textProportion;
131 }
132
133 void wxPickerBase::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
134 {
135 wxControl::DoSetSizeHints(minW, minH, maxW, maxH, incW, incH);
136
137 if (m_text)
138 {
139 // compute minWidth and maxWidth of the ausiliary textctrl
140 int textCtrlMinW = -1, textCtrlMaxW = -1;
141 if (minW != -1)
142 {
143 textCtrlMinW = GetTextCtrlWidth(minW);
144 minW -= textCtrlMinW + m_margin;
145 }
146
147 if (maxW != -1)
148 {
149 textCtrlMaxW = GetTextCtrlWidth(maxW);
150 maxW -= textCtrlMaxW + m_margin;
151 }
152
153 m_text->SetSizeHints(textCtrlMinW, minH, textCtrlMaxW, maxH, incW, incH);
154 }
155
156 if (m_picker)
157 m_picker->SetSizeHints(minW, minH, maxW, maxH, incW, incH);
158 }
159
160 void wxPickerBase::DoSetSize(int x, int y, int width, int height, int sizeFlags)
161 {
162 wxControl::DoSetSize(x, y, width, height, sizeFlags);
163
164 int pickerx = 0;
165 if (m_text)
166 {
167 // compute width of the ausiliary textctrl
168 int textCtrlW = GetTextCtrlWidth(width);
169
170 // set the m_text's position relatively to this window
171 m_text->SetSize(0, 0, textCtrlW, height, sizeFlags);
172
173 // change position of the real picker
174 pickerx += textCtrlW + m_margin;
175 width -= textCtrlW + m_margin;
176 }
177
178 if (m_picker)
179 m_picker->SetSize(pickerx, 0, width, height, sizeFlags);
180 }
181
182 wxSize wxPickerBase::DoGetBestSize() const
183 {
184 wxSize ret = m_picker->GetBestSize();
185
186 if (m_text)
187 {
188 wxSize sz = m_text->GetBestSize();
189
190 ret.SetWidth( ret.GetWidth() + sz.GetWidth() + m_margin );
191 ret.SetHeight( wxMax(ret.GetHeight(), sz.GetHeight()) );
192 }
193
194 return ret;
195 }