]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: wxComboBox class | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
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" | |
b5a8b32f | 17 | #include "wx/button.h" |
03e11df5 | 18 | #include "wx/menu.h" |
519cb848 | 19 | #include "wx/mac/uma.h" |
e9576ca5 | 20 | |
2f1ae414 | 21 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 22 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
2f1ae414 | 23 | #endif |
e9576ca5 | 24 | |
12f31626 SC |
25 | // composite combobox implementation by Dan "Bud" Keith bud@otsys.com |
26 | ||
519cb848 | 27 | |
0e03d1fa | 28 | static int nextPopUpMenuId = 1000 ; |
892e461e SC |
29 | MenuHandle NewUniqueMenu() |
30 | { | |
31 | MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; | |
32 | nextPopUpMenuId++ ; | |
33 | return handle ; | |
34 | } | |
519cb848 | 35 | |
12f31626 SC |
36 | |
37 | // ---------------------------------------------------------------------------- | |
38 | // constants | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // the margin between the text control and the choice | |
42 | static const wxCoord MARGIN = 2; | |
43 | static const int POPUPWIDTH = 18; | |
44 | static const int POPUPHEIGHT = 23; | |
45 | ||
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxComboBoxText: text control forwards events to combobox | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | class wxComboBoxText : public wxTextCtrl | |
52 | { | |
53 | public: | |
54 | wxComboBoxText( wxComboBox * cb ) | |
327788ac | 55 | : wxTextCtrl( cb , 1 ) |
12f31626 SC |
56 | { |
57 | m_cb = cb; | |
58 | } | |
59 | ||
60 | protected: | |
8095ef23 | 61 | void OnChar( wxKeyEvent& event ) |
12f31626 | 62 | { |
eb22f2a6 | 63 | if ( event.GetKeyCode() == WXK_RETURN ) |
8095ef23 SC |
64 | { |
65 | wxString value = GetValue(); | |
66 | ||
67 | if ( m_cb->GetCount() == 0 ) | |
68 | { | |
69 | // make Enter generate "selected" event if there is only one item | |
70 | // in the combobox - without it, it's impossible to select it at | |
71 | // all! | |
72 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() ); | |
73 | event.SetInt( 0 ); | |
74 | event.SetString( value ); | |
75 | event.SetEventObject( m_cb ); | |
76 | m_cb->GetEventHandler()->ProcessEvent( event ); | |
77 | } | |
78 | else | |
79 | { | |
80 | // add the item to the list if it's not there yet | |
81 | if ( m_cb->FindString(value) == wxNOT_FOUND ) | |
82 | { | |
83 | m_cb->Append(value); | |
84 | m_cb->SetStringSelection(value); | |
85 | ||
86 | // and generate the selected event for it | |
87 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() ); | |
88 | event.SetInt( m_cb->GetCount() - 1 ); | |
89 | event.SetString( value ); | |
90 | event.SetEventObject( m_cb ); | |
91 | m_cb->GetEventHandler()->ProcessEvent( event ); | |
92 | } | |
93 | ||
94 | // This will invoke the dialog default action, such | |
95 | // as the clicking the default button. | |
96 | ||
97 | wxWindow *parent = GetParent(); | |
98 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { | |
e40298d5 | 99 | parent = parent->GetParent() ; |
8095ef23 SC |
100 | } |
101 | if ( parent && parent->GetDefaultItem() ) | |
102 | { | |
103 | wxButton *def = wxDynamicCast(parent->GetDefaultItem(), | |
104 | wxButton); | |
105 | if ( def && def->IsEnabled() ) | |
106 | { | |
107 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
108 | event.SetEventObject(def); | |
109 | def->Command(event); | |
110 | return ; | |
111 | } | |
112 | } | |
113 | ||
114 | return; | |
115 | } | |
116 | } | |
22a70443 | 117 | |
12f31626 SC |
118 | event.Skip(); |
119 | } | |
120 | ||
121 | private: | |
122 | wxComboBox *m_cb; | |
123 | ||
124 | DECLARE_EVENT_TABLE() | |
125 | }; | |
126 | ||
127 | BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl) | |
8095ef23 | 128 | EVT_CHAR( wxComboBoxText::OnChar) |
12f31626 SC |
129 | END_EVENT_TABLE() |
130 | ||
131 | class wxComboBoxChoice : public wxChoice | |
132 | { | |
133 | public: | |
134 | wxComboBoxChoice(wxComboBox *cb, int style) | |
327788ac | 135 | : wxChoice( cb , 1 ) |
12f31626 SC |
136 | { |
137 | m_cb = cb; | |
138 | } | |
139 | ||
140 | protected: | |
141 | void OnChoice( wxCommandEvent& e ) | |
142 | { | |
143 | wxString s = e.GetString(); | |
144 | ||
145 | m_cb->DelegateChoice( s ); | |
8095ef23 SC |
146 | wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() ); |
147 | event2.SetInt(m_cb->GetSelection()); | |
148 | event2.SetEventObject(m_cb); | |
149 | event2.SetString(m_cb->GetStringSelection()); | |
150 | m_cb->ProcessCommand(event2); | |
12f31626 SC |
151 | } |
152 | ||
153 | private: | |
154 | wxComboBox *m_cb; | |
155 | ||
156 | DECLARE_EVENT_TABLE() | |
157 | }; | |
158 | ||
159 | BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) | |
160 | EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) | |
161 | END_EVENT_TABLE() | |
162 | ||
12f31626 SC |
163 | wxComboBox::~wxComboBox() |
164 | { | |
e94e2e95 MB |
165 | // delete client objects |
166 | FreeData(); | |
167 | ||
168 | // delete the controls now, don't leave them alive even though they would | |
12f31626 SC |
169 | // still be eventually deleted by our parent - but it will be too late, the |
170 | // user code expects them to be gone now | |
f5bb2251 GD |
171 | if (m_text != NULL) { |
172 | delete m_text; | |
173 | m_text = NULL; | |
174 | } | |
175 | if (m_choice != NULL) { | |
176 | delete m_choice; | |
177 | m_choice = NULL; | |
178 | } | |
12f31626 SC |
179 | } |
180 | ||
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // geometry | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | wxSize wxComboBox::DoGetBestSize() const | |
187 | { | |
188 | wxSize size = m_choice->GetBestSize(); | |
189 | ||
d99937cd | 190 | if ( m_text != NULL ) |
12f31626 SC |
191 | { |
192 | wxSize sizeText = m_text->GetBestSize(); | |
193 | ||
194 | size.x = POPUPWIDTH + sizeText.x + MARGIN; | |
195 | } | |
196 | ||
197 | return size; | |
198 | } | |
199 | ||
200 | void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { | |
201 | height = POPUPHEIGHT; | |
202 | ||
203 | wxControl::DoMoveWindow(x, y, width, height); | |
204 | ||
d99937cd | 205 | if ( m_text == NULL ) |
12f31626 | 206 | { |
facd6764 SC |
207 | // we might not be fully constructed yet, therefore watch out... |
208 | if ( m_choice ) | |
209 | m_choice->SetSize(0, 0 , width, -1); | |
12f31626 SC |
210 | } |
211 | else | |
212 | { | |
7854746e | 213 | wxCoord wText = width - POPUPWIDTH - MARGIN; |
327788ac SC |
214 | m_text->SetSize(0, 0, wText, height); |
215 | m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1); | |
12f31626 SC |
216 | } |
217 | } | |
218 | ||
219 | ||
220 | ||
221 | // ---------------------------------------------------------------------------- | |
222 | // operations forwarded to the subcontrols | |
223 | // ---------------------------------------------------------------------------- | |
224 | ||
225 | bool wxComboBox::Enable(bool enable) | |
226 | { | |
227 | if ( !wxControl::Enable(enable) ) | |
228 | return FALSE; | |
229 | ||
12f31626 SC |
230 | return TRUE; |
231 | } | |
232 | ||
233 | bool wxComboBox::Show(bool show) | |
234 | { | |
235 | if ( !wxControl::Show(show) ) | |
236 | return FALSE; | |
237 | ||
12f31626 SC |
238 | return TRUE; |
239 | } | |
240 | ||
d99937cd GD |
241 | void wxComboBox::SetFocus() |
242 | { | |
243 | if ( m_text != NULL) { | |
244 | m_text->SetFocus(); | |
245 | } | |
246 | } | |
465605e0 | 247 | |
12f31626 | 248 | |
d99937cd GD |
249 | void wxComboBox::DelegateTextChanged( const wxString& value ) |
250 | { | |
8095ef23 | 251 | SetStringSelection( value ); |
12f31626 SC |
252 | } |
253 | ||
254 | ||
255 | void wxComboBox::DelegateChoice( const wxString& value ) | |
256 | { | |
257 | SetStringSelection( value ); | |
258 | } | |
259 | ||
260 | ||
584ad2a3 MB |
261 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
262 | const wxString& value, | |
263 | const wxPoint& pos, | |
264 | const wxSize& size, | |
265 | const wxArrayString& choices, | |
266 | long style, | |
267 | const wxValidator& validator, | |
268 | const wxString& name) | |
269 | { | |
270 | wxCArrayString chs( choices ); | |
271 | ||
272 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
273 | chs.GetStrings(), style, validator, name ); | |
274 | } | |
275 | ||
276 | ||
e9576ca5 SC |
277 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
278 | const wxString& value, | |
279 | const wxPoint& pos, | |
280 | const wxSize& size, | |
465605e0 RR |
281 | int n, const wxString choices[], |
282 | long style, | |
e9576ca5 SC |
283 | const wxValidator& validator, |
284 | const wxString& name) | |
285 | { | |
327788ac | 286 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style , |
12f31626 SC |
287 | wxDefaultValidator, name) ) |
288 | { | |
289 | return FALSE; | |
290 | } | |
291 | ||
327788ac | 292 | m_choice = new wxComboBoxChoice(this, style ); |
12f31626 SC |
293 | |
294 | wxSize csize = size; | |
295 | if ( style & wxCB_READONLY ) | |
296 | { | |
d99937cd | 297 | m_text = NULL; |
12f31626 SC |
298 | } |
299 | else | |
300 | { | |
301 | m_text = new wxComboBoxText(this); | |
302 | if ( size.y == -1 ) { | |
303 | csize.y = m_text->GetSize().y ; | |
304 | } | |
305 | } | |
306 | ||
307 | DoSetSize(pos.x, pos.y, csize.x, csize.y); | |
327788ac | 308 | |
12f31626 SC |
309 | for ( int i = 0 ; i < n ; i++ ) |
310 | { | |
465605e0 | 311 | m_choice->DoAppend( choices[ i ] ); |
12f31626 SC |
312 | } |
313 | ||
12f31626 | 314 | return TRUE; |
e9576ca5 SC |
315 | } |
316 | ||
317 | wxString wxComboBox::GetValue() const | |
318 | { | |
12f31626 SC |
319 | wxString result; |
320 | ||
d99937cd | 321 | if ( m_text == NULL ) |
12f31626 SC |
322 | { |
323 | result = m_choice->GetString( m_choice->GetSelection() ); | |
324 | } | |
325 | else | |
326 | { | |
327 | result = m_text->GetValue(); | |
328 | } | |
329 | ||
330 | return result; | |
e9576ca5 SC |
331 | } |
332 | ||
333 | void wxComboBox::SetValue(const wxString& value) | |
334 | { | |
d60de516 SC |
335 | int s = FindString (value); |
336 | if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) ) | |
337 | { | |
e40298d5 | 338 | m_choice->Append(value) ; |
d60de516 | 339 | } |
e40298d5 | 340 | SetStringSelection( value ) ; |
e9576ca5 SC |
341 | } |
342 | ||
343 | // Clipboard operations | |
344 | void wxComboBox::Copy() | |
345 | { | |
d99937cd | 346 | if ( m_text != NULL ) |
12f31626 SC |
347 | { |
348 | m_text->Copy(); | |
349 | } | |
e9576ca5 SC |
350 | } |
351 | ||
352 | void wxComboBox::Cut() | |
353 | { | |
d99937cd | 354 | if ( m_text != NULL ) |
12f31626 SC |
355 | { |
356 | m_text->Cut(); | |
357 | } | |
e9576ca5 SC |
358 | } |
359 | ||
360 | void wxComboBox::Paste() | |
361 | { | |
d99937cd | 362 | if ( m_text != NULL ) |
12f31626 SC |
363 | { |
364 | m_text->Paste(); | |
365 | } | |
e9576ca5 SC |
366 | } |
367 | ||
368 | void wxComboBox::SetEditable(bool editable) | |
369 | { | |
d99937cd | 370 | if ( ( m_text == NULL ) && editable ) |
12f31626 SC |
371 | { |
372 | m_text = new wxComboBoxText( this ); | |
373 | } | |
d99937cd | 374 | else if ( ( m_text != NULL ) && !editable ) |
12f31626 SC |
375 | { |
376 | delete m_text; | |
d99937cd | 377 | m_text = NULL; |
12f31626 SC |
378 | } |
379 | ||
380 | int currentX, currentY; | |
381 | GetPosition( ¤tX, ¤tY ); | |
382 | ||
383 | int currentW, currentH; | |
384 | GetSize( ¤tW, ¤tH ); | |
385 | ||
386 | DoMoveWindow( currentX, currentY, currentW, currentH ); | |
e9576ca5 SC |
387 | } |
388 | ||
389 | void wxComboBox::SetInsertionPoint(long pos) | |
390 | { | |
391 | // TODO | |
392 | } | |
393 | ||
394 | void wxComboBox::SetInsertionPointEnd() | |
395 | { | |
396 | // TODO | |
397 | } | |
398 | ||
399 | long wxComboBox::GetInsertionPoint() const | |
400 | { | |
401 | // TODO | |
402 | return 0; | |
403 | } | |
404 | ||
405 | long wxComboBox::GetLastPosition() const | |
406 | { | |
407 | // TODO | |
408 | return 0; | |
409 | } | |
410 | ||
411 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
412 | { | |
413 | // TODO | |
414 | } | |
415 | ||
416 | void wxComboBox::Remove(long from, long to) | |
417 | { | |
418 | // TODO | |
419 | } | |
420 | ||
421 | void wxComboBox::SetSelection(long from, long to) | |
422 | { | |
423 | // TODO | |
424 | } | |
425 | ||
e71a0aa9 | 426 | int wxComboBox::DoAppend(const wxString& item) |
e9576ca5 | 427 | { |
e71a0aa9 SC |
428 | return m_choice->DoAppend( item ) ; |
429 | } | |
430 | ||
431 | int wxComboBox::DoInsert(const wxString& item, int pos) | |
432 | { | |
433 | return m_choice->DoInsert( item , pos ) ; | |
434 | } | |
435 | ||
436 | void wxComboBox::DoSetItemClientData(int n, void* clientData) | |
437 | { | |
f148f2ba | 438 | return m_choice->DoSetItemClientData( n , clientData ) ; |
e71a0aa9 SC |
439 | } |
440 | ||
441 | void* wxComboBox::DoGetItemClientData(int n) const | |
442 | { | |
f148f2ba | 443 | return m_choice->DoGetItemClientData( n ) ; |
e71a0aa9 | 444 | } |
22a70443 | 445 | |
e71a0aa9 SC |
446 | void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData) |
447 | { | |
f148f2ba | 448 | return m_choice->DoSetItemClientObject( n , clientData ) ; |
e71a0aa9 SC |
449 | } |
450 | ||
451 | wxClientData* wxComboBox::DoGetItemClientObject(int n) const | |
452 | { | |
f148f2ba MB |
453 | return m_choice->DoGetItemClientObject( n ) ; |
454 | } | |
455 | ||
456 | void wxComboBox::FreeData() | |
457 | { | |
458 | if ( HasClientObjectData() ) | |
459 | { | |
460 | size_t count = GetCount(); | |
461 | for ( size_t n = 0; n < count; n++ ) | |
462 | { | |
463 | SetClientObject( n, NULL ); | |
464 | } | |
465 | } | |
e9576ca5 SC |
466 | } |
467 | ||
468 | void wxComboBox::Delete(int n) | |
469 | { | |
f148f2ba MB |
470 | // force client object deletion |
471 | if( HasClientObjectData() ) | |
472 | SetClientObject( n, NULL ); | |
12f31626 | 473 | m_choice->Delete( n ); |
e9576ca5 SC |
474 | } |
475 | ||
476 | void wxComboBox::Clear() | |
477 | { | |
f148f2ba | 478 | FreeData(); |
12f31626 | 479 | m_choice->Clear(); |
e9576ca5 SC |
480 | } |
481 | ||
482 | int wxComboBox::GetSelection() const | |
483 | { | |
12f31626 | 484 | return m_choice->GetSelection(); |
e9576ca5 SC |
485 | } |
486 | ||
487 | void wxComboBox::SetSelection(int n) | |
488 | { | |
12f31626 SC |
489 | m_choice->SetSelection( n ); |
490 | ||
d99937cd | 491 | if ( m_text != NULL ) |
12f31626 SC |
492 | { |
493 | m_text->SetValue( GetString( n ) ); | |
494 | } | |
e9576ca5 SC |
495 | } |
496 | ||
497 | int wxComboBox::FindString(const wxString& s) const | |
498 | { | |
12f31626 | 499 | return m_choice->FindString( s ); |
e9576ca5 SC |
500 | } |
501 | ||
502 | wxString wxComboBox::GetString(int n) const | |
503 | { | |
12f31626 | 504 | return m_choice->GetString( n ); |
e9576ca5 SC |
505 | } |
506 | ||
507 | wxString wxComboBox::GetStringSelection() const | |
508 | { | |
519cb848 SC |
509 | int sel = GetSelection (); |
510 | if (sel > -1) | |
511 | return wxString(this->GetString (sel)); | |
512 | else | |
427ff662 | 513 | return wxEmptyString; |
e9576ca5 SC |
514 | } |
515 | ||
516 | bool wxComboBox::SetStringSelection(const wxString& sel) | |
517 | { | |
519cb848 SC |
518 | int s = FindString (sel); |
519 | if (s > -1) | |
520 | { | |
521 | SetSelection (s); | |
522 | return TRUE; | |
523 | } | |
524 | else | |
525 | return FALSE; | |
526 | } | |
527 | ||
e71a0aa9 SC |
528 | void wxComboBox::SetString(int n, const wxString& s) |
529 | { | |
530 | m_choice->SetString( n , s ) ; | |
531 | } | |
532 | ||
533 | ||
534 | void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown)) | |
519cb848 SC |
535 | { |
536 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
465605e0 | 537 | event.SetInt(GetSelection()); |
519cb848 | 538 | event.SetEventObject(this); |
0a67a93b | 539 | event.SetString(GetStringSelection()); |
519cb848 | 540 | ProcessCommand(event); |
e9576ca5 | 541 | } |
519cb848 | 542 |