]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: wxComboBox class | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
465605e0 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "combobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/combobox.h" | |
03e11df5 | 17 | #include "wx/menu.h" |
519cb848 | 18 | #include "wx/mac/uma.h" |
e9576ca5 | 19 | |
2f1ae414 | 20 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 21 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
2f1ae414 | 22 | #endif |
e9576ca5 | 23 | |
12f31626 SC |
24 | // composite combobox implementation by Dan "Bud" Keith bud@otsys.com |
25 | ||
519cb848 | 26 | |
0e03d1fa | 27 | static int nextPopUpMenuId = 1000 ; |
892e461e SC |
28 | MenuHandle NewUniqueMenu() |
29 | { | |
30 | MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; | |
31 | nextPopUpMenuId++ ; | |
32 | return handle ; | |
33 | } | |
519cb848 | 34 | |
12f31626 SC |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // constants | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // the margin between the text control and the choice | |
41 | static const wxCoord MARGIN = 2; | |
42 | static const int POPUPWIDTH = 18; | |
43 | static const int POPUPHEIGHT = 23; | |
44 | ||
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // wxComboBoxText: text control forwards events to combobox | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | class wxComboBoxText : public wxTextCtrl | |
51 | { | |
52 | public: | |
53 | wxComboBoxText( wxComboBox * cb ) | |
327788ac | 54 | : wxTextCtrl( cb , 1 ) |
12f31626 SC |
55 | { |
56 | m_cb = cb; | |
57 | } | |
58 | ||
59 | protected: | |
60 | void OnTextChange( wxCommandEvent& event ) | |
61 | { | |
62 | wxString s = GetValue(); | |
22a70443 RR |
63 | |
64 | if (!s.IsEmpty()) | |
65 | m_cb->DelegateTextChanged( s ); | |
12f31626 SC |
66 | |
67 | event.Skip(); | |
68 | } | |
69 | ||
70 | private: | |
71 | wxComboBox *m_cb; | |
72 | ||
73 | DECLARE_EVENT_TABLE() | |
74 | }; | |
75 | ||
76 | BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl) | |
77 | EVT_TEXT(-1, wxComboBoxText::OnTextChange) | |
78 | END_EVENT_TABLE() | |
79 | ||
80 | class wxComboBoxChoice : public wxChoice | |
81 | { | |
82 | public: | |
83 | wxComboBoxChoice(wxComboBox *cb, int style) | |
327788ac | 84 | : wxChoice( cb , 1 ) |
12f31626 SC |
85 | { |
86 | m_cb = cb; | |
87 | } | |
88 | ||
89 | protected: | |
90 | void OnChoice( wxCommandEvent& e ) | |
91 | { | |
92 | wxString s = e.GetString(); | |
93 | ||
94 | m_cb->DelegateChoice( s ); | |
95 | } | |
96 | ||
97 | private: | |
98 | wxComboBox *m_cb; | |
99 | ||
100 | DECLARE_EVENT_TABLE() | |
101 | }; | |
102 | ||
103 | BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) | |
104 | EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) | |
105 | END_EVENT_TABLE() | |
106 | ||
12f31626 SC |
107 | wxComboBox::~wxComboBox() |
108 | { | |
f5bb2251 | 109 | // delete the controls now, don't leave them alive even though they would |
12f31626 SC |
110 | // still be eventually deleted by our parent - but it will be too late, the |
111 | // user code expects them to be gone now | |
f5bb2251 GD |
112 | if (m_text != NULL) { |
113 | delete m_text; | |
114 | m_text = NULL; | |
115 | } | |
116 | if (m_choice != NULL) { | |
117 | delete m_choice; | |
118 | m_choice = NULL; | |
119 | } | |
12f31626 SC |
120 | } |
121 | ||
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // geometry | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | wxSize wxComboBox::DoGetBestSize() const | |
128 | { | |
129 | wxSize size = m_choice->GetBestSize(); | |
130 | ||
131 | if ( m_text != 0 ) | |
132 | { | |
133 | wxSize sizeText = m_text->GetBestSize(); | |
134 | ||
135 | size.x = POPUPWIDTH + sizeText.x + MARGIN; | |
136 | } | |
137 | ||
138 | return size; | |
139 | } | |
140 | ||
141 | void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { | |
142 | height = POPUPHEIGHT; | |
143 | ||
144 | wxControl::DoMoveWindow(x, y, width, height); | |
145 | ||
146 | if ( m_text == 0 ) | |
147 | { | |
327788ac | 148 | m_choice->SetSize(0, 0 , width, -1); |
12f31626 SC |
149 | } |
150 | else | |
151 | { | |
152 | wxCoord wText = width - POPUPWIDTH; | |
327788ac SC |
153 | m_text->SetSize(0, 0, wText, height); |
154 | m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1); | |
12f31626 SC |
155 | } |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // operations forwarded to the subcontrols | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | bool wxComboBox::Enable(bool enable) | |
165 | { | |
166 | if ( !wxControl::Enable(enable) ) | |
167 | return FALSE; | |
168 | ||
12f31626 SC |
169 | return TRUE; |
170 | } | |
171 | ||
172 | bool wxComboBox::Show(bool show) | |
173 | { | |
174 | if ( !wxControl::Show(show) ) | |
175 | return FALSE; | |
176 | ||
12f31626 SC |
177 | return TRUE; |
178 | } | |
179 | ||
465605e0 RR |
180 | void wxComboBox::SetFocus() |
181 | { | |
182 | m_text->SetFocus(); | |
183 | } | |
184 | ||
12f31626 SC |
185 | |
186 | void wxComboBox::DelegateTextChanged( const wxString& value ) { | |
187 | } | |
188 | ||
189 | ||
190 | void wxComboBox::DelegateChoice( const wxString& value ) | |
191 | { | |
192 | SetStringSelection( value ); | |
193 | } | |
194 | ||
195 | ||
e9576ca5 SC |
196 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
197 | const wxString& value, | |
198 | const wxPoint& pos, | |
199 | const wxSize& size, | |
465605e0 RR |
200 | int n, const wxString choices[], |
201 | long style, | |
e9576ca5 SC |
202 | const wxValidator& validator, |
203 | const wxString& name) | |
204 | { | |
12f31626 SC |
205 | |
206 | Rect bounds ; | |
207 | Str255 title ; | |
208 | ||
327788ac | 209 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style , |
12f31626 SC |
210 | wxDefaultValidator, name) ) |
211 | { | |
212 | return FALSE; | |
213 | } | |
214 | ||
327788ac | 215 | m_choice = new wxComboBoxChoice(this, style ); |
12f31626 SC |
216 | |
217 | wxSize csize = size; | |
218 | if ( style & wxCB_READONLY ) | |
219 | { | |
220 | m_text = 0; | |
221 | } | |
222 | else | |
223 | { | |
224 | m_text = new wxComboBoxText(this); | |
225 | if ( size.y == -1 ) { | |
226 | csize.y = m_text->GetSize().y ; | |
227 | } | |
228 | } | |
229 | ||
230 | DoSetSize(pos.x, pos.y, csize.x, csize.y); | |
327788ac | 231 | |
12f31626 SC |
232 | for ( int i = 0 ; i < n ; i++ ) |
233 | { | |
465605e0 | 234 | m_choice->DoAppend( choices[ i ] ); |
12f31626 SC |
235 | } |
236 | ||
12f31626 | 237 | return TRUE; |
e9576ca5 SC |
238 | } |
239 | ||
240 | wxString wxComboBox::GetValue() const | |
241 | { | |
12f31626 SC |
242 | wxString result; |
243 | ||
244 | if ( m_text == 0 ) | |
245 | { | |
246 | result = m_choice->GetString( m_choice->GetSelection() ); | |
247 | } | |
248 | else | |
249 | { | |
250 | result = m_text->GetValue(); | |
251 | } | |
252 | ||
253 | return result; | |
e9576ca5 SC |
254 | } |
255 | ||
256 | void wxComboBox::SetValue(const wxString& value) | |
257 | { | |
519cb848 | 258 | SetStringSelection( value ) ; |
e9576ca5 SC |
259 | } |
260 | ||
261 | // Clipboard operations | |
262 | void wxComboBox::Copy() | |
263 | { | |
12f31626 SC |
264 | if ( m_text != 0 ) |
265 | { | |
266 | m_text->Copy(); | |
267 | } | |
e9576ca5 SC |
268 | } |
269 | ||
270 | void wxComboBox::Cut() | |
271 | { | |
12f31626 SC |
272 | if ( m_text != 0 ) |
273 | { | |
274 | m_text->Cut(); | |
275 | } | |
e9576ca5 SC |
276 | } |
277 | ||
278 | void wxComboBox::Paste() | |
279 | { | |
12f31626 SC |
280 | if ( m_text != 0 ) |
281 | { | |
282 | m_text->Paste(); | |
283 | } | |
e9576ca5 SC |
284 | } |
285 | ||
286 | void wxComboBox::SetEditable(bool editable) | |
287 | { | |
12f31626 SC |
288 | if ( ( m_text == 0 ) && editable ) |
289 | { | |
290 | m_text = new wxComboBoxText( this ); | |
291 | } | |
292 | else if ( ( m_text != 0 ) && !editable ) | |
293 | { | |
294 | delete m_text; | |
295 | m_text = 0; | |
296 | } | |
297 | ||
298 | int currentX, currentY; | |
299 | GetPosition( ¤tX, ¤tY ); | |
300 | ||
301 | int currentW, currentH; | |
302 | GetSize( ¤tW, ¤tH ); | |
303 | ||
304 | DoMoveWindow( currentX, currentY, currentW, currentH ); | |
e9576ca5 SC |
305 | } |
306 | ||
307 | void wxComboBox::SetInsertionPoint(long pos) | |
308 | { | |
309 | // TODO | |
310 | } | |
311 | ||
312 | void wxComboBox::SetInsertionPointEnd() | |
313 | { | |
314 | // TODO | |
315 | } | |
316 | ||
317 | long wxComboBox::GetInsertionPoint() const | |
318 | { | |
319 | // TODO | |
320 | return 0; | |
321 | } | |
322 | ||
323 | long wxComboBox::GetLastPosition() const | |
324 | { | |
325 | // TODO | |
326 | return 0; | |
327 | } | |
328 | ||
329 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
330 | { | |
331 | // TODO | |
332 | } | |
333 | ||
334 | void wxComboBox::Remove(long from, long to) | |
335 | { | |
336 | // TODO | |
337 | } | |
338 | ||
339 | void wxComboBox::SetSelection(long from, long to) | |
340 | { | |
341 | // TODO | |
342 | } | |
343 | ||
344 | void wxComboBox::Append(const wxString& item) | |
345 | { | |
22a70443 RR |
346 | // I am not sure what other ports do, |
347 | // but wxMac chokes on empty entries. | |
348 | ||
349 | if (!item.IsEmpty()) | |
350 | m_choice->DoAppend( item ); | |
e9576ca5 SC |
351 | } |
352 | ||
353 | void wxComboBox::Delete(int n) | |
354 | { | |
12f31626 | 355 | m_choice->Delete( n ); |
e9576ca5 SC |
356 | } |
357 | ||
358 | void wxComboBox::Clear() | |
359 | { | |
12f31626 | 360 | m_choice->Clear(); |
e9576ca5 SC |
361 | } |
362 | ||
363 | int wxComboBox::GetSelection() const | |
364 | { | |
12f31626 | 365 | return m_choice->GetSelection(); |
e9576ca5 SC |
366 | } |
367 | ||
368 | void wxComboBox::SetSelection(int n) | |
369 | { | |
12f31626 SC |
370 | m_choice->SetSelection( n ); |
371 | ||
372 | if ( m_text != 0 ) | |
373 | { | |
374 | m_text->SetValue( GetString( n ) ); | |
375 | } | |
e9576ca5 SC |
376 | } |
377 | ||
378 | int wxComboBox::FindString(const wxString& s) const | |
379 | { | |
12f31626 | 380 | return m_choice->FindString( s ); |
e9576ca5 SC |
381 | } |
382 | ||
383 | wxString wxComboBox::GetString(int n) const | |
384 | { | |
12f31626 | 385 | return m_choice->GetString( n ); |
e9576ca5 SC |
386 | } |
387 | ||
388 | wxString wxComboBox::GetStringSelection() const | |
389 | { | |
519cb848 SC |
390 | int sel = GetSelection (); |
391 | if (sel > -1) | |
392 | return wxString(this->GetString (sel)); | |
393 | else | |
394 | return wxString(""); | |
e9576ca5 SC |
395 | } |
396 | ||
397 | bool wxComboBox::SetStringSelection(const wxString& sel) | |
398 | { | |
519cb848 SC |
399 | int s = FindString (sel); |
400 | if (s > -1) | |
401 | { | |
402 | SetSelection (s); | |
403 | return TRUE; | |
404 | } | |
405 | else | |
406 | return FALSE; | |
407 | } | |
408 | ||
76a5e5d2 | 409 | void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
519cb848 SC |
410 | { |
411 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
465605e0 | 412 | event.SetInt(GetSelection()); |
519cb848 | 413 | event.SetEventObject(this); |
0a67a93b | 414 | event.SetString(GetStringSelection()); |
519cb848 | 415 | ProcessCommand(event); |
e9576ca5 | 416 | } |
519cb848 | 417 |