1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor, Dan "Bud" Keith (composite combobox)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if wxUSE_COMBOBOX && wxOSX_USE_CARBON
16 #include "wx/combobox.h"
19 #include "wx/button.h"
21 #include "wx/containr.h"
22 #include "wx/toplevel.h"
23 #include "wx/textctrl.h"
26 #include "wx/osx/private.h"
28 WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox, wxControl)
30 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
31 WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox)
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the margin between the text control and the choice
40 // margin should be bigger on OS X due to blue highlight
41 // around text control.
42 static const wxCoord MARGIN = 4;
43 // this is the border a focus rect on OSX is needing
44 static const int TEXTFOCUSBORDER = 3 ;
47 // ----------------------------------------------------------------------------
48 // wxComboBoxText: text control forwards events to combobox
49 // ----------------------------------------------------------------------------
51 class wxComboBoxText : public wxTextCtrl
54 wxComboBoxText( wxComboBox * cb )
55 : wxTextCtrl( cb , 1 )
61 void OnChar( wxKeyEvent& event )
63 // Allows processing the tab key to go to the next control
64 if (event.GetKeyCode() == WXK_TAB)
66 wxNavigationKeyEvent NavEvent;
67 NavEvent.SetEventObject(this);
68 NavEvent.SetDirection(true);
69 NavEvent.SetWindowChange(false);
71 // Get the parent of the combo and have it process the navigation?
72 if (m_cb->GetParent()->HandleWindowEvent(NavEvent))
76 // send the event to the combobox class in case the user has bound EVT_CHAR
77 wxKeyEvent kevt(event);
78 kevt.SetEventObject(m_cb);
79 if (m_cb->HandleWindowEvent(kevt))
80 // If the event was handled and not skipped then we're done
83 if ( event.GetKeyCode() == WXK_RETURN )
85 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_cb->GetId());
86 event.SetString( GetValue() );
87 event.SetInt( m_cb->GetSelection() );
88 event.SetEventObject( m_cb );
90 // This will invoke the dialog default action,
91 // such as the clicking the default button.
92 if (!m_cb->HandleWindowEvent( event ))
94 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
95 if ( tlw && tlw->GetDefaultItem() )
97 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
98 if ( def && def->IsEnabled() )
100 wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
101 event.SetEventObject(def);
113 void OnKeyUp( wxKeyEvent& event )
115 event.SetEventObject(m_cb);
116 event.SetId(m_cb->GetId());
117 if (! m_cb->HandleWindowEvent(event))
121 void OnKeyDown( wxKeyEvent& event )
123 event.SetEventObject(m_cb);
124 event.SetId(m_cb->GetId());
125 if (! m_cb->HandleWindowEvent(event))
129 void OnText( wxCommandEvent& event )
131 event.SetEventObject(m_cb);
132 event.SetId(m_cb->GetId());
133 if (! m_cb->HandleWindowEvent(event))
137 void OnFocus( wxFocusEvent& event )
139 // in case the textcontrol gets the focus we propagate
140 // it to the parent's handlers.
141 wxFocusEvent evt2(event.GetEventType(),m_cb->GetId());
142 evt2.SetEventObject(m_cb);
143 m_cb->GetEventHandler()->ProcessEvent(evt2);
151 DECLARE_EVENT_TABLE()
154 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
155 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown)
156 EVT_CHAR(wxComboBoxText::OnChar)
157 EVT_KEY_UP(wxComboBoxText::OnKeyUp)
158 EVT_SET_FOCUS(wxComboBoxText::OnFocus)
159 EVT_KILL_FOCUS(wxComboBoxText::OnFocus)
160 EVT_TEXT(wxID_ANY, wxComboBoxText::OnText)
163 class wxComboBoxChoice : public wxChoice
166 wxComboBoxChoice( wxComboBox *cb, int style )
167 : wxChoice( cb , 1 , wxDefaultPosition , wxDefaultSize , 0 , NULL , style & (wxCB_SORT) )
172 int GetPopupWidth() const
174 switch ( GetWindowVariant() )
176 case wxWINDOW_VARIANT_NORMAL :
177 case wxWINDOW_VARIANT_LARGE :
186 void OnChoice( wxCommandEvent& e )
188 wxString s = e.GetString();
190 m_cb->DelegateChoice( s );
191 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
192 event2.SetInt(m_cb->GetSelection());
193 event2.SetEventObject(m_cb);
194 event2.SetString(m_cb->GetStringSelection());
195 m_cb->ProcessCommand(event2);
197 // For consistency with MSW and GTK, also send a text updated event
198 // After all, the text is updated when a selection is made
199 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId() );
200 TextEvent.SetString( m_cb->GetStringSelection() );
201 TextEvent.SetEventObject( m_cb );
202 m_cb->ProcessCommand( TextEvent );
205 virtual wxSize DoGetBestSize() const
207 wxSize sz = wxChoice::DoGetBestSize() ;
208 if (! m_cb->HasFlag(wxCB_READONLY) )
209 sz.x = GetPopupWidth() ;
217 friend class wxComboBox;
219 DECLARE_EVENT_TABLE()
222 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
223 EVT_CHOICE(wxID_ANY, wxComboBoxChoice::OnChoice)
226 wxComboBox::~wxComboBox()
228 // delete the controls now, don't leave them alive even though they would
229 // still be eventually deleted by our parent - but it will be too late, the
230 // user code expects them to be gone now
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 wxSize wxComboBox::DoGetBestSize() const
241 if (!m_choice && !m_text)
244 wxSize size = m_choice->GetBestSize();
246 if ( m_text != NULL )
248 wxSize sizeText = m_text->GetBestSize();
249 if (sizeText.y + 2 * TEXTFOCUSBORDER > size.y)
250 size.y = sizeText.y + 2 * TEXTFOCUSBORDER;
252 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
253 size.x += TEXTFOCUSBORDER ;
257 // clipping is too tight
264 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
266 wxControl::DoMoveWindow( x, y, width , height );
268 if ( m_text == NULL )
270 // we might not be fully constructed yet, therefore watch out...
272 m_choice->SetSize(0, 0 , width, -1);
276 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
277 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1);
278 wxSize tSize = m_text->GetSize();
279 wxSize cSize = m_choice->GetSize();
281 int yOffset = ( tSize.y + 2 * TEXTFOCUSBORDER - cSize.y ) / 2;
283 // put it at an inset of 1 to have outer area shadows drawn as well
284 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , yOffset, m_choice->GetPopupWidth() , -1);
288 // ----------------------------------------------------------------------------
289 // operations forwarded to the subcontrols
290 // ----------------------------------------------------------------------------
292 bool wxComboBox::Enable(bool enable)
294 if ( !wxControl::Enable(enable) )
298 m_text->Enable(enable);
303 bool wxComboBox::Show(bool show)
305 if ( !wxControl::Show(show) )
311 void wxComboBox::DelegateTextChanged( const wxString& value )
313 SetStringSelection( value );
316 void wxComboBox::DelegateChoice( const wxString& value )
318 SetStringSelection( value );
321 void wxComboBox::Init()
323 WX_INIT_CONTROL_CONTAINER();
326 bool wxComboBox::Create(wxWindow *parent,
328 const wxString& value,
331 const wxArrayString& choices,
333 const wxValidator& validator,
334 const wxString& name)
336 if ( !Create( parent, id, value, pos, size, 0, NULL,
337 style, validator, name ) )
345 bool wxComboBox::Create(wxWindow *parent,
347 const wxString& value,
351 const wxString choices[],
353 const wxValidator& validator,
354 const wxString& name)
356 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
363 if ( style & wxCB_READONLY )
369 m_text = new wxComboBoxText(this);
372 csize.y = m_text->GetSize().y ;
373 csize.y += 2 * TEXTFOCUSBORDER ;
376 m_choice = new wxComboBoxChoice(this, style );
378 DoSetSize(pos.x, pos.y, csize.x, csize.y);
380 Append( n, choices );
382 // Needed because it is a wxControlWithItems
383 SetInitialSize(size);
384 SetStringSelection(value);
389 void wxComboBox::EnableTextChangedEvents(bool enable)
392 m_text->ForwardEnableTextChangedEvents(enable);
395 wxString wxComboBox::DoGetValue() const
397 wxCHECK_MSG( m_text, wxString(), "can't be called for read-only combobox" );
399 return m_text->GetValue();
402 wxString wxComboBox::GetValue() const
406 if ( m_text == NULL )
407 result = m_choice->GetString( m_choice->GetSelection() );
409 result = m_text->GetValue();
414 unsigned int wxComboBox::GetCount() const
416 return m_choice->GetCount() ;
419 void wxComboBox::SetValue(const wxString& value)
421 if ( HasFlag(wxCB_READONLY) )
422 SetStringSelection( value ) ;
424 m_text->SetValue( value );
427 void wxComboBox::WriteText(const wxString& text)
429 m_text->WriteText(text);
432 void wxComboBox::GetSelection(long *from, long *to) const
434 m_text->GetSelection(from, to);
437 // Clipboard operations
439 void wxComboBox::Copy()
441 if ( m_text != NULL )
445 void wxComboBox::Cut()
447 if ( m_text != NULL )
451 void wxComboBox::Paste()
453 if ( m_text != NULL )
457 void wxComboBox::SetEditable(bool editable)
459 if ( ( m_text == NULL ) && editable )
461 m_text = new wxComboBoxText( this );
463 else if ( !editable )
468 int currentX, currentY;
469 GetPosition( ¤tX, ¤tY );
471 int currentW, currentH;
472 GetSize( ¤tW, ¤tH );
474 DoMoveWindow( currentX, currentY, currentW, currentH );
477 void wxComboBox::SetInsertionPoint(long pos)
480 m_text->SetInsertionPoint(pos);
483 void wxComboBox::SetInsertionPointEnd()
486 m_text->SetInsertionPointEnd();
489 long wxComboBox::GetInsertionPoint() const
492 return m_text->GetInsertionPoint();
496 wxTextPos wxComboBox::GetLastPosition() const
499 return m_text->GetLastPosition();
503 void wxComboBox::Replace(long from, long to, const wxString& value)
506 m_text->Replace(from,to,value);
509 void wxComboBox::Remove(long from, long to)
512 m_text->Remove(from,to);
515 void wxComboBox::SetSelection(long from, long to)
518 m_text->SetSelection(from,to);
521 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
524 wxClientDataType type)
526 return m_choice->DoInsertItems(items, pos, clientData, type);
529 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
531 return m_choice->DoSetItemClientData( n , clientData ) ;
534 void* wxComboBox::DoGetItemClientData(unsigned int n) const
536 return m_choice->DoGetItemClientData( n ) ;
539 wxClientDataType wxComboBox::GetClientDataType() const
541 return m_choice->GetClientDataType();
544 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType)
546 m_choice->SetClientDataType(clientDataItemsType);
549 void wxComboBox::DoDeleteOneItem(unsigned int n)
551 m_choice->DoDeleteOneItem( n );
554 void wxComboBox::DoClear()
559 int wxComboBox::GetSelection() const
561 return m_choice->GetSelection();
564 void wxComboBox::SetSelection(int n)
566 m_choice->SetSelection( n );
568 if ( m_text != NULL )
569 m_text->SetValue(n != wxNOT_FOUND ? GetString(n) : wxString(wxEmptyString));
572 int wxComboBox::FindString(const wxString& s, bool bCase) const
574 return m_choice->FindString( s, bCase );
577 wxString wxComboBox::GetString(unsigned int n) const
579 return m_choice->GetString( n );
582 wxString wxComboBox::GetStringSelection() const
584 int sel = GetSelection();
585 if (sel != wxNOT_FOUND)
586 return wxString(this->GetString((unsigned int)sel));
588 return wxEmptyString;
591 void wxComboBox::SetString(unsigned int n, const wxString& s)
593 m_choice->SetString( n , s );
596 bool wxComboBox::IsEditable() const
598 return m_text != NULL && !HasFlag(wxCB_READONLY);
601 void wxComboBox::Undo()
607 void wxComboBox::Redo()
613 void wxComboBox::SelectAll()
619 bool wxComboBox::CanCopy() const
622 return m_text->CanCopy();
627 bool wxComboBox::CanCut() const
630 return m_text->CanCut();
635 bool wxComboBox::CanPaste() const
638 return m_text->CanPaste();
643 bool wxComboBox::CanUndo() const
646 return m_text->CanUndo();
651 bool wxComboBox::CanRedo() const
654 return m_text->CanRedo();
659 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec) )
662 For consistency with other platforms, clicking in the text area does not constitute a selection
663 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
664 event.SetInt(GetSelection());
665 event.SetEventObject(this);
666 event.SetString(GetStringSelection());
667 ProcessCommand(event);
673 wxTextWidgetImpl* wxComboBox::GetTextPeer() const
676 return m_text->GetTextPeer();
681 #endif // wxUSE_COMBOBOX && wxOSX_USE_CARBON