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