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