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