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