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