]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: combobox.cpp | |
3 | // Purpose: wxComboBox class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "combobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #include "wx/combobox.h" | |
19 | #include "wx/button.h" | |
20 | #include "wx/menu.h" | |
21 | #include "wx/mac/uma.h" | |
22 | ||
23 | #if !USE_SHARED_LIBRARY | |
24 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
25 | #endif | |
26 | ||
27 | // composite combobox implementation by Dan "Bud" Keith bud@otsys.com | |
28 | ||
29 | ||
30 | static int nextPopUpMenuId = 1000 ; | |
31 | MenuHandle NewUniqueMenu() | |
32 | { | |
33 | MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; | |
34 | nextPopUpMenuId++ ; | |
35 | return handle ; | |
36 | } | |
37 | ||
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // constants | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | // the margin between the text control and the choice | |
44 | #if TARGET_API_MAC_OSX | |
45 | // margin should be bigger on OS X due to blue highlight | |
46 | // around text control. | |
47 | static const wxCoord MARGIN = 4; | |
48 | // this is the border a focus rect on OSX is needing | |
49 | static const int TEXTFOCUSBORDER = 3 ; | |
50 | #else | |
51 | static const wxCoord MARGIN = 2; | |
52 | static const int TEXTFOCUSBORDER = 0 ; | |
53 | #endif | |
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 ) | |
65 | : wxTextCtrl( cb , 1 ) | |
66 | { | |
67 | m_cb = cb; | |
68 | } | |
69 | ||
70 | protected: | |
71 | void OnChar( wxKeyEvent& event ) | |
72 | { | |
73 | // Allows processing the tab key to go to the next control | |
74 | if (event.GetKeyCode() == WXK_TAB) | |
75 | { | |
76 | wxNavigationKeyEvent NavEvent; | |
77 | NavEvent.SetEventObject(this); | |
78 | NavEvent.SetDirection(true); | |
79 | NavEvent.SetWindowChange(false); | |
80 | ||
81 | // Get the parent of the combo and have it process the navigation? | |
82 | if (m_cb->GetParent()->GetEventHandler()->ProcessEvent(NavEvent)) | |
83 | return; | |
84 | } | |
85 | if ( event.GetKeyCode() == WXK_RETURN ) | |
86 | { | |
87 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_cb->GetId()); | |
88 | event.SetString( GetValue() ); | |
89 | event.SetInt( m_cb->GetSelection() ); | |
90 | event.SetEventObject( m_cb ); | |
91 | ||
92 | // This will invoke the dialog default action, such | |
93 | // as the clicking the default button. | |
94 | ||
95 | if (!m_cb->GetEventHandler()->ProcessEvent( event )) | |
96 | { | |
97 | wxWindow *parent = GetParent(); | |
98 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { | |
99 | parent = parent->GetParent() ; | |
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 | } | |
111 | } | |
112 | ||
113 | return; | |
114 | } | |
115 | } | |
116 | ||
117 | event.Skip(); | |
118 | } | |
119 | ||
120 | // Use the KeyUp as a naive approximation for TEXT_UPDATED, even though it is somewhat delayed | |
121 | // but this is less complicated than dealing with idle-ness, and is much better than nothing | |
122 | void OnKeyUp( wxKeyEvent& event ) | |
123 | { | |
124 | if ( event.GetKeyCode() != WXK_RETURN && event.GetKeyCode() != WXK_TAB ) | |
125 | { | |
126 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId()); | |
127 | event.SetString( GetValue() ); | |
128 | event.SetEventObject( m_cb ); | |
129 | m_cb->GetEventHandler()->ProcessEvent(event); | |
130 | } | |
131 | } | |
132 | private: | |
133 | wxComboBox *m_cb; | |
134 | ||
135 | DECLARE_EVENT_TABLE() | |
136 | }; | |
137 | ||
138 | BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl) | |
139 | EVT_CHAR( wxComboBoxText::OnChar) | |
140 | EVT_KEY_UP( wxComboBoxText::OnKeyUp) | |
141 | END_EVENT_TABLE() | |
142 | ||
143 | class wxComboBoxChoice : public wxChoice | |
144 | { | |
145 | public: | |
146 | wxComboBoxChoice(wxComboBox *cb, int style) | |
147 | : wxChoice( cb , 1 ) | |
148 | { | |
149 | m_cb = cb; | |
150 | } | |
151 | int GetPopupWidth() const | |
152 | { | |
153 | switch ( GetWindowVariant() ) | |
154 | { | |
155 | case wxWINDOW_VARIANT_NORMAL : | |
156 | case wxWINDOW_VARIANT_LARGE : | |
157 | return 24 ; | |
158 | default : | |
159 | return 21 ; | |
160 | } | |
161 | } | |
162 | ||
163 | protected: | |
164 | void OnChoice( wxCommandEvent& e ) | |
165 | { | |
166 | wxString s = e.GetString(); | |
167 | ||
168 | m_cb->DelegateChoice( s ); | |
169 | wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() ); | |
170 | event2.SetInt(m_cb->GetSelection()); | |
171 | event2.SetEventObject(m_cb); | |
172 | event2.SetString(m_cb->GetStringSelection()); | |
173 | m_cb->ProcessCommand(event2); | |
174 | ||
175 | // For consistency with MSW and GTK, also send a text updated event | |
176 | // After all, the text is updated when a selection is made | |
177 | wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId() ); | |
178 | TextEvent.SetString( m_cb->GetStringSelection() ); | |
179 | TextEvent.SetEventObject( m_cb ); | |
180 | m_cb->ProcessCommand( TextEvent ); | |
181 | } | |
182 | virtual wxSize DoGetBestSize() const | |
183 | { | |
184 | wxSize sz = wxChoice::DoGetBestSize() ; | |
185 | if (! m_cb->HasFlag(wxCB_READONLY) ) | |
186 | sz.x = GetPopupWidth() ; | |
187 | return sz ; | |
188 | } | |
189 | ||
190 | private: | |
191 | wxComboBox *m_cb; | |
192 | ||
193 | DECLARE_EVENT_TABLE() | |
194 | }; | |
195 | ||
196 | BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) | |
197 | EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) | |
198 | END_EVENT_TABLE() | |
199 | ||
200 | wxComboBox::~wxComboBox() | |
201 | { | |
202 | // delete client objects | |
203 | FreeData(); | |
204 | ||
205 | // delete the controls now, don't leave them alive even though they would | |
206 | // still be eventually deleted by our parent - but it will be too late, the | |
207 | // user code expects them to be gone now | |
208 | if (m_text != NULL) { | |
209 | delete m_text; | |
210 | m_text = NULL; | |
211 | } | |
212 | if (m_choice != NULL) { | |
213 | delete m_choice; | |
214 | m_choice = NULL; | |
215 | } | |
216 | } | |
217 | ||
218 | ||
219 | // ---------------------------------------------------------------------------- | |
220 | // geometry | |
221 | // ---------------------------------------------------------------------------- | |
222 | ||
223 | wxSize wxComboBox::DoGetBestSize() const | |
224 | { | |
225 | if (!m_choice && !m_text) | |
226 | return GetSize(); | |
227 | wxSize size = m_choice->GetBestSize(); | |
228 | ||
229 | if ( m_text != NULL ) | |
230 | { | |
231 | wxSize sizeText = m_text->GetBestSize(); | |
232 | if (sizeText.y > size.y) | |
233 | size.y = sizeText.y; | |
234 | size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN; | |
235 | size.x += TEXTFOCUSBORDER ; | |
236 | size.y += 2 * TEXTFOCUSBORDER ; | |
237 | } | |
238 | else | |
239 | { | |
240 | // clipping is too tight | |
241 | size.y += 1 ; | |
242 | } | |
243 | return size; | |
244 | } | |
245 | ||
246 | void wxComboBox::DoMoveWindow(int x, int y, int width, int height) | |
247 | { | |
248 | wxControl::DoMoveWindow(x, y, width , height ); | |
249 | ||
250 | if ( m_text == NULL ) | |
251 | { | |
252 | // we might not be fully constructed yet, therefore watch out... | |
253 | if ( m_choice ) | |
254 | m_choice->SetSize(0, 0 , width, -1); | |
255 | } | |
256 | else | |
257 | { | |
258 | wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN; | |
259 | m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1 ); | |
260 | // put it at an inset of 1 to have outer area shadows drawn as well | |
261 | m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1); | |
262 | } | |
263 | } | |
264 | ||
265 | ||
266 | ||
267 | // ---------------------------------------------------------------------------- | |
268 | // operations forwarded to the subcontrols | |
269 | // ---------------------------------------------------------------------------- | |
270 | ||
271 | bool wxComboBox::Enable(bool enable) | |
272 | { | |
273 | if ( !wxControl::Enable(enable) ) | |
274 | return false; | |
275 | ||
276 | return true; | |
277 | } | |
278 | ||
279 | bool wxComboBox::Show(bool show) | |
280 | { | |
281 | if ( !wxControl::Show(show) ) | |
282 | return false; | |
283 | ||
284 | return true; | |
285 | } | |
286 | ||
287 | void wxComboBox::SetFocus() | |
288 | { | |
289 | if ( m_text != NULL) { | |
290 | m_text->SetFocus(); | |
291 | } | |
292 | } | |
293 | ||
294 | ||
295 | void wxComboBox::DelegateTextChanged( const wxString& value ) | |
296 | { | |
297 | SetStringSelection( value ); | |
298 | } | |
299 | ||
300 | ||
301 | void wxComboBox::DelegateChoice( const wxString& value ) | |
302 | { | |
303 | SetStringSelection( value ); | |
304 | } | |
305 | ||
306 | ||
307 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
308 | const wxString& value, | |
309 | const wxPoint& pos, | |
310 | const wxSize& size, | |
311 | const wxArrayString& choices, | |
312 | long style, | |
313 | const wxValidator& validator, | |
314 | const wxString& name) | |
315 | { | |
316 | wxCArrayString chs( choices ); | |
317 | ||
318 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
319 | chs.GetStrings(), style, validator, name ); | |
320 | } | |
321 | ||
322 | ||
323 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
324 | const wxString& value, | |
325 | const wxPoint& pos, | |
326 | const wxSize& size, | |
327 | int n, const wxString choices[], | |
328 | long style, | |
329 | const wxValidator& validator, | |
330 | const wxString& name) | |
331 | { | |
332 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style , | |
333 | wxDefaultValidator, name) ) | |
334 | { | |
335 | return false; | |
336 | } | |
337 | ||
338 | m_choice = new wxComboBoxChoice(this, style ); | |
339 | wxSize csize = size; | |
340 | if ( style & wxCB_READONLY ) | |
341 | { | |
342 | m_text = NULL; | |
343 | } | |
344 | else | |
345 | { | |
346 | m_text = new wxComboBoxText(this); | |
347 | if ( size.y == -1 ) | |
348 | { | |
349 | csize.y = m_text->GetSize().y ; | |
350 | csize.y += 2 * TEXTFOCUSBORDER ; | |
351 | } | |
352 | } | |
353 | ||
354 | DoSetSize(pos.x, pos.y, csize.x, csize.y); | |
355 | ||
356 | for ( int i = 0 ; i < n ; i++ ) | |
357 | { | |
358 | m_choice->DoAppend( choices[ i ] ); | |
359 | } | |
360 | ||
361 | SetBestSize(size); // Needed because it is a wxControlWithItems | |
362 | ||
363 | return true; | |
364 | } | |
365 | ||
366 | wxString wxComboBox::GetValue() const | |
367 | { | |
368 | wxString result; | |
369 | ||
370 | if ( m_text == NULL ) | |
371 | { | |
372 | result = m_choice->GetString( m_choice->GetSelection() ); | |
373 | } | |
374 | else | |
375 | { | |
376 | result = m_text->GetValue(); | |
377 | } | |
378 | ||
379 | return result; | |
380 | } | |
381 | ||
382 | int wxComboBox::GetCount() const | |
383 | { | |
384 | return m_choice->GetCount() ; | |
385 | } | |
386 | ||
387 | void wxComboBox::SetValue(const wxString& value) | |
388 | { | |
389 | if ( HasFlag(wxCB_READONLY) ) | |
390 | SetStringSelection( value ) ; | |
391 | else | |
392 | m_text->SetValue( value ); | |
393 | } | |
394 | ||
395 | // Clipboard operations | |
396 | void wxComboBox::Copy() | |
397 | { | |
398 | if ( m_text != NULL ) | |
399 | { | |
400 | m_text->Copy(); | |
401 | } | |
402 | } | |
403 | ||
404 | void wxComboBox::Cut() | |
405 | { | |
406 | if ( m_text != NULL ) | |
407 | { | |
408 | m_text->Cut(); | |
409 | } | |
410 | } | |
411 | ||
412 | void wxComboBox::Paste() | |
413 | { | |
414 | if ( m_text != NULL ) | |
415 | { | |
416 | m_text->Paste(); | |
417 | } | |
418 | } | |
419 | ||
420 | void wxComboBox::SetEditable(bool editable) | |
421 | { | |
422 | if ( ( m_text == NULL ) && editable ) | |
423 | { | |
424 | m_text = new wxComboBoxText( this ); | |
425 | } | |
426 | else if ( ( m_text != NULL ) && !editable ) | |
427 | { | |
428 | delete m_text; | |
429 | m_text = NULL; | |
430 | } | |
431 | ||
432 | int currentX, currentY; | |
433 | GetPosition( ¤tX, ¤tY ); | |
434 | ||
435 | int currentW, currentH; | |
436 | GetSize( ¤tW, ¤tH ); | |
437 | ||
438 | DoMoveWindow( currentX, currentY, currentW, currentH ); | |
439 | } | |
440 | ||
441 | void wxComboBox::SetInsertionPoint(long pos) | |
442 | { | |
443 | // TODO | |
444 | } | |
445 | ||
446 | void wxComboBox::SetInsertionPointEnd() | |
447 | { | |
448 | // TODO | |
449 | } | |
450 | ||
451 | long wxComboBox::GetInsertionPoint() const | |
452 | { | |
453 | // TODO | |
454 | return 0; | |
455 | } | |
456 | ||
457 | wxTextPos wxComboBox::GetLastPosition() const | |
458 | { | |
459 | // TODO | |
460 | return 0; | |
461 | } | |
462 | ||
463 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
464 | { | |
465 | // TODO | |
466 | } | |
467 | ||
468 | void wxComboBox::Remove(long from, long to) | |
469 | { | |
470 | // TODO | |
471 | } | |
472 | ||
473 | void wxComboBox::SetSelection(long from, long to) | |
474 | { | |
475 | // TODO | |
476 | } | |
477 | ||
478 | int wxComboBox::DoAppend(const wxString& item) | |
479 | { | |
480 | return m_choice->DoAppend( item ) ; | |
481 | } | |
482 | ||
483 | int wxComboBox::DoInsert(const wxString& item, int pos) | |
484 | { | |
485 | return m_choice->DoInsert( item , pos ) ; | |
486 | } | |
487 | ||
488 | void wxComboBox::DoSetItemClientData(int n, void* clientData) | |
489 | { | |
490 | return m_choice->DoSetItemClientData( n , clientData ) ; | |
491 | } | |
492 | ||
493 | void* wxComboBox::DoGetItemClientData(int n) const | |
494 | { | |
495 | return m_choice->DoGetItemClientData( n ) ; | |
496 | } | |
497 | ||
498 | void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
499 | { | |
500 | return m_choice->DoSetItemClientObject( n , clientData ) ; | |
501 | } | |
502 | ||
503 | wxClientData* wxComboBox::DoGetItemClientObject(int n) const | |
504 | { | |
505 | return m_choice->DoGetItemClientObject( n ) ; | |
506 | } | |
507 | ||
508 | void wxComboBox::FreeData() | |
509 | { | |
510 | if ( HasClientObjectData() ) | |
511 | { | |
512 | size_t count = GetCount(); | |
513 | for ( size_t n = 0; n < count; n++ ) | |
514 | { | |
515 | SetClientObject( n, NULL ); | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | void wxComboBox::Delete(int n) | |
521 | { | |
522 | // force client object deletion | |
523 | if( HasClientObjectData() ) | |
524 | SetClientObject( n, NULL ); | |
525 | m_choice->Delete( n ); | |
526 | } | |
527 | ||
528 | void wxComboBox::Clear() | |
529 | { | |
530 | FreeData(); | |
531 | m_choice->Clear(); | |
532 | } | |
533 | ||
534 | int wxComboBox::GetSelection() const | |
535 | { | |
536 | return m_choice->GetSelection(); | |
537 | } | |
538 | ||
539 | void wxComboBox::SetSelection(int n) | |
540 | { | |
541 | m_choice->SetSelection( n ); | |
542 | ||
543 | if ( m_text != NULL ) | |
544 | { | |
545 | m_text->SetValue( GetString( n ) ); | |
546 | } | |
547 | } | |
548 | ||
549 | int wxComboBox::FindString(const wxString& s) const | |
550 | { | |
551 | return m_choice->FindString( s ); | |
552 | } | |
553 | ||
554 | wxString wxComboBox::GetString(int n) const | |
555 | { | |
556 | return m_choice->GetString( n ); | |
557 | } | |
558 | ||
559 | wxString wxComboBox::GetStringSelection() const | |
560 | { | |
561 | int sel = GetSelection (); | |
562 | if (sel > -1) | |
563 | return wxString(this->GetString (sel)); | |
564 | else | |
565 | return wxEmptyString; | |
566 | } | |
567 | ||
568 | bool wxComboBox::SetStringSelection(const wxString& sel) | |
569 | { | |
570 | int s = FindString (sel); | |
571 | if (s > -1) | |
572 | { | |
573 | SetSelection (s); | |
574 | return true; | |
575 | } | |
576 | else | |
577 | return false; | |
578 | } | |
579 | ||
580 | void wxComboBox::SetString(int n, const wxString& s) | |
581 | { | |
582 | m_choice->SetString( n , s ) ; | |
583 | } | |
584 | ||
585 | bool wxComboBox::IsEditable() const | |
586 | { | |
587 | return m_text != NULL && !HasFlag(wxCB_READONLY); | |
588 | } | |
589 | ||
590 | void wxComboBox::Undo() | |
591 | { | |
592 | if (m_text != NULL) | |
593 | m_text->Undo(); | |
594 | } | |
595 | ||
596 | void wxComboBox::Redo() | |
597 | { | |
598 | if (m_text != NULL) | |
599 | m_text->Redo(); | |
600 | } | |
601 | ||
602 | void wxComboBox::SelectAll() | |
603 | { | |
604 | if (m_text != NULL) | |
605 | m_text->SelectAll(); | |
606 | } | |
607 | ||
608 | bool wxComboBox::CanCopy() const | |
609 | { | |
610 | if (m_text != NULL) | |
611 | return m_text->CanCopy(); | |
612 | else | |
613 | return false; | |
614 | } | |
615 | ||
616 | bool wxComboBox::CanCut() const | |
617 | { | |
618 | if (m_text != NULL) | |
619 | return m_text->CanCut(); | |
620 | else | |
621 | return false; | |
622 | } | |
623 | ||
624 | bool wxComboBox::CanPaste() const | |
625 | { | |
626 | if (m_text != NULL) | |
627 | return m_text->CanPaste(); | |
628 | else | |
629 | return false; | |
630 | } | |
631 | ||
632 | bool wxComboBox::CanUndo() const | |
633 | { | |
634 | if (m_text != NULL) | |
635 | return m_text->CanUndo(); | |
636 | else | |
637 | return false; | |
638 | } | |
639 | ||
640 | bool wxComboBox::CanRedo() const | |
641 | { | |
642 | if (m_text != NULL) | |
643 | return m_text->CanRedo(); | |
644 | else | |
645 | return false; | |
646 | } | |
647 | ||
648 | wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) | |
649 | { | |
650 | /* For consistency with other platforms, clicking in the text area does not constitute a selection | |
651 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
652 | event.SetInt(GetSelection()); | |
653 | event.SetEventObject(this); | |
654 | event.SetString(GetStringSelection()); | |
655 | ProcessCommand(event);*/ | |
656 | return noErr ; | |
657 | } | |
658 |