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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
30 WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox
, wxControl
)
32 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
33 WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox
)
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the margin between the text control and the choice
42 // margin should be bigger on OS X due to blue highlight
43 // around text control.
44 static const wxCoord MARGIN
= 4;
45 // this is the border a focus rect on OSX is needing
46 static const int TEXTFOCUSBORDER
= 3 ;
49 // ----------------------------------------------------------------------------
50 // wxComboBoxText: text control forwards events to combobox
51 // ----------------------------------------------------------------------------
53 class wxComboBoxText
: public wxTextCtrl
56 wxComboBoxText( wxComboBox
* cb
)
57 : wxTextCtrl( cb
, 1 )
63 void OnChar( wxKeyEvent
& event
)
65 // Allows processing the tab key to go to the next control
66 if (event
.GetKeyCode() == WXK_TAB
)
68 wxNavigationKeyEvent NavEvent
;
69 NavEvent
.SetEventObject(this);
70 NavEvent
.SetDirection(true);
71 NavEvent
.SetWindowChange(false);
73 // Get the parent of the combo and have it process the navigation?
74 if (m_cb
->GetParent()->HandleWindowEvent(NavEvent
))
78 // send the event to the combobox class in case the user has bound EVT_CHAR
79 wxKeyEvent
kevt(event
);
80 kevt
.SetEventObject(m_cb
);
81 if (m_cb
->HandleWindowEvent(kevt
))
82 // If the event was handled and not skipped then we're done
85 if ( event
.GetKeyCode() == WXK_RETURN
)
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
);
92 // This will invoke the dialog default action,
93 // such as the clicking the default button.
94 if (!m_cb
->HandleWindowEvent( event
))
96 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
97 if ( tlw
&& tlw
->GetDefaultItem() )
99 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
100 if ( def
&& def
->IsEnabled() )
102 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
103 event
.SetEventObject(def
);
115 void OnKeyUp( wxKeyEvent
& event
)
117 event
.SetEventObject(m_cb
);
118 event
.SetId(m_cb
->GetId());
119 if (! m_cb
->HandleWindowEvent(event
))
123 void OnKeyDown( wxKeyEvent
& event
)
125 event
.SetEventObject(m_cb
);
126 event
.SetId(m_cb
->GetId());
127 if (! m_cb
->HandleWindowEvent(event
))
131 void OnText( wxCommandEvent
& event
)
133 event
.SetEventObject(m_cb
);
134 event
.SetId(m_cb
->GetId());
135 if (! m_cb
->HandleWindowEvent(event
))
139 void OnFocus( wxFocusEvent
& event
)
141 // in case the textcontrol gets the focus we propagate
142 // it to the parent's handlers.
143 wxFocusEvent
evt2(event
.GetEventType(),m_cb
->GetId());
144 evt2
.SetEventObject(m_cb
);
145 m_cb
->GetEventHandler()->ProcessEvent(evt2
);
153 DECLARE_EVENT_TABLE()
156 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
157 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
158 EVT_CHAR(wxComboBoxText::OnChar
)
159 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
160 EVT_SET_FOCUS(wxComboBoxText::OnFocus
)
161 EVT_KILL_FOCUS(wxComboBoxText::OnFocus
)
162 EVT_TEXT(wxID_ANY
, wxComboBoxText::OnText
)
165 class wxComboBoxChoice
: public wxChoice
168 wxComboBoxChoice( wxComboBox
*cb
, int style
)
169 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
174 int GetPopupWidth() const
176 switch ( GetWindowVariant() )
178 case wxWINDOW_VARIANT_NORMAL
:
179 case wxWINDOW_VARIANT_LARGE
:
188 void OnChoice( wxCommandEvent
& e
)
190 wxString s
= e
.GetString();
192 m_cb
->DelegateChoice( s
);
193 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
194 event2
.SetInt(m_cb
->GetSelection());
195 event2
.SetEventObject(m_cb
);
196 event2
.SetString(m_cb
->GetStringSelection());
197 m_cb
->ProcessCommand(event2
);
199 // For consistency with MSW and GTK, also send a text updated event
200 // After all, the text is updated when a selection is made
201 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
202 TextEvent
.SetString( m_cb
->GetStringSelection() );
203 TextEvent
.SetEventObject( m_cb
);
204 m_cb
->ProcessCommand( TextEvent
);
207 virtual wxSize
DoGetBestSize() const
209 wxSize sz
= wxChoice::DoGetBestSize() ;
210 if (! m_cb
->HasFlag(wxCB_READONLY
) )
211 sz
.x
= GetPopupWidth() ;
219 friend class wxComboBox
;
221 DECLARE_EVENT_TABLE()
224 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
225 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
228 wxComboBox::~wxComboBox()
230 // delete the controls now, don't leave them alive even though they would
231 // still be eventually deleted by our parent - but it will be too late, the
232 // user code expects them to be gone now
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 wxSize
wxComboBox::DoGetBestSize() const
243 if (!m_choice
&& !m_text
)
246 wxSize size
= m_choice
->GetBestSize();
248 if ( m_text
!= NULL
)
250 wxSize sizeText
= m_text
->GetBestSize();
251 if (sizeText
.y
+ 2 * TEXTFOCUSBORDER
> size
.y
)
252 size
.y
= sizeText
.y
+ 2 * TEXTFOCUSBORDER
;
254 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
255 size
.x
+= TEXTFOCUSBORDER
;
259 // clipping is too tight
266 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
268 wxControl::DoMoveWindow( x
, y
, width
, height
);
270 if ( m_text
== NULL
)
272 // we might not be fully constructed yet, therefore watch out...
274 m_choice
->SetSize(0, 0 , width
, -1);
278 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
279 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
280 wxSize tSize
= m_text
->GetSize();
281 wxSize cSize
= m_choice
->GetSize();
283 int yOffset
= ( tSize
.y
+ 2 * TEXTFOCUSBORDER
- cSize
.y
) / 2;
285 // put it at an inset of 1 to have outer area shadows drawn as well
286 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , yOffset
, m_choice
->GetPopupWidth() , -1);
290 // ----------------------------------------------------------------------------
291 // operations forwarded to the subcontrols
292 // ----------------------------------------------------------------------------
294 bool wxComboBox::Enable(bool enable
)
296 if ( !wxControl::Enable(enable
) )
300 m_text
->Enable(enable
);
305 bool wxComboBox::Show(bool show
)
307 if ( !wxControl::Show(show
) )
313 void wxComboBox::DelegateTextChanged( const wxString
& value
)
315 SetStringSelection( value
);
318 void wxComboBox::DelegateChoice( const wxString
& value
)
320 SetStringSelection( value
);
323 void wxComboBox::Init()
325 WX_INIT_CONTROL_CONTAINER();
328 bool wxComboBox::Create(wxWindow
*parent
,
330 const wxString
& value
,
333 const wxArrayString
& choices
,
335 const wxValidator
& validator
,
336 const wxString
& name
)
338 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
339 style
, validator
, name
) )
347 bool wxComboBox::Create(wxWindow
*parent
,
349 const wxString
& value
,
353 const wxString choices
[],
355 const wxValidator
& validator
,
356 const wxString
& name
)
358 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
365 if ( style
& wxCB_READONLY
)
371 m_text
= new wxComboBoxText(this);
374 csize
.y
= m_text
->GetSize().y
;
375 csize
.y
+= 2 * TEXTFOCUSBORDER
;
378 m_choice
= new wxComboBoxChoice(this, style
);
380 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
382 Append( n
, choices
);
384 // Needed because it is a wxControlWithItems
385 SetInitialSize(size
);
386 SetStringSelection(value
);
391 void wxComboBox::EnableTextChangedEvents(bool enable
)
394 m_text
->ForwardEnableTextChangedEvents(enable
);
397 wxString
wxComboBox::DoGetValue() const
399 wxCHECK_MSG( m_text
, wxString(), "can't be called for read-only combobox" );
401 return m_text
->GetValue();
404 wxString
wxComboBox::GetValue() const
408 if ( m_text
== NULL
)
409 result
= m_choice
->GetString( m_choice
->GetSelection() );
411 result
= m_text
->GetValue();
416 unsigned int wxComboBox::GetCount() const
418 return m_choice
->GetCount() ;
421 void wxComboBox::SetValue(const wxString
& value
)
423 if ( HasFlag(wxCB_READONLY
) )
424 SetStringSelection( value
) ;
426 m_text
->SetValue( value
);
429 void wxComboBox::WriteText(const wxString
& text
)
431 m_text
->WriteText(text
);
434 void wxComboBox::GetSelection(long *from
, long *to
) const
436 m_text
->GetSelection(from
, to
);
439 // Clipboard operations
441 void wxComboBox::Copy()
443 if ( m_text
!= NULL
)
447 void wxComboBox::Cut()
449 if ( m_text
!= NULL
)
453 void wxComboBox::Paste()
455 if ( m_text
!= NULL
)
459 void wxComboBox::SetEditable(bool editable
)
461 if ( ( m_text
== NULL
) && editable
)
463 m_text
= new wxComboBoxText( this );
465 else if ( !editable
)
470 int currentX
, currentY
;
471 GetPosition( ¤tX
, ¤tY
);
473 int currentW
, currentH
;
474 GetSize( ¤tW
, ¤tH
);
476 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
479 void wxComboBox::SetInsertionPoint(long pos
)
482 m_text
->SetInsertionPoint(pos
);
485 void wxComboBox::SetInsertionPointEnd()
488 m_text
->SetInsertionPointEnd();
491 long wxComboBox::GetInsertionPoint() const
494 return m_text
->GetInsertionPoint();
498 wxTextPos
wxComboBox::GetLastPosition() const
501 return m_text
->GetLastPosition();
505 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
508 m_text
->Replace(from
,to
,value
);
511 void wxComboBox::Remove(long from
, long to
)
514 m_text
->Remove(from
,to
);
517 void wxComboBox::SetSelection(long from
, long to
)
520 m_text
->SetSelection(from
,to
);
523 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
526 wxClientDataType type
)
528 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
531 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
533 return m_choice
->DoSetItemClientData( n
, clientData
) ;
536 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
538 return m_choice
->DoGetItemClientData( n
) ;
541 wxClientDataType
wxComboBox::GetClientDataType() const
543 return m_choice
->GetClientDataType();
546 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
548 m_choice
->SetClientDataType(clientDataItemsType
);
551 void wxComboBox::DoDeleteOneItem(unsigned int n
)
553 m_choice
->DoDeleteOneItem( n
);
556 void wxComboBox::DoClear()
561 int wxComboBox::GetSelection() const
563 return m_choice
->GetSelection();
566 void wxComboBox::SetSelection(int n
)
568 m_choice
->SetSelection( n
);
570 if ( m_text
!= NULL
)
571 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
574 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
576 return m_choice
->FindString( s
, bCase
);
579 wxString
wxComboBox::GetString(unsigned int n
) const
581 return m_choice
->GetString( n
);
584 wxString
wxComboBox::GetStringSelection() const
586 int sel
= GetSelection();
587 if (sel
!= wxNOT_FOUND
)
588 return wxString(this->GetString((unsigned int)sel
));
590 return wxEmptyString
;
593 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
595 m_choice
->SetString( n
, s
);
598 bool wxComboBox::IsEditable() const
600 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
603 void wxComboBox::Undo()
609 void wxComboBox::Redo()
615 void wxComboBox::SelectAll()
621 bool wxComboBox::CanCopy() const
624 return m_text
->CanCopy();
629 bool wxComboBox::CanCut() const
632 return m_text
->CanCut();
637 bool wxComboBox::CanPaste() const
640 return m_text
->CanPaste();
645 bool wxComboBox::CanUndo() const
648 return m_text
->CanUndo();
653 bool wxComboBox::CanRedo() const
656 return m_text
->CanRedo();
661 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
664 For consistency with other platforms, clicking in the text area does not constitute a selection
665 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
666 event.SetInt(GetSelection());
667 event.SetEventObject(this);
668 event.SetString(GetStringSelection());
669 ProcessCommand(event);
675 wxTextWidgetImpl
* wxComboBox::GetTextPeer() const
678 return m_text
->GetTextPeer();
683 #endif // wxUSE_COMBOBOX && wxOSX_USE_CARBON