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
239 if (m_choice
!= NULL
)
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 wxSize
wxComboBox::DoGetBestSize() const
252 if (!m_choice
&& !m_text
)
255 wxSize size
= m_choice
->GetBestSize();
257 if ( m_text
!= NULL
)
259 wxSize sizeText
= m_text
->GetBestSize();
260 if (sizeText
.y
+ 2 * TEXTFOCUSBORDER
> size
.y
)
261 size
.y
= sizeText
.y
+ 2 * TEXTFOCUSBORDER
;
263 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
264 size
.x
+= TEXTFOCUSBORDER
;
268 // clipping is too tight
275 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
277 wxControl::DoMoveWindow( x
, y
, width
, height
);
279 if ( m_text
== NULL
)
281 // we might not be fully constructed yet, therefore watch out...
283 m_choice
->SetSize(0, 0 , width
, -1);
287 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
288 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
289 wxSize tSize
= m_text
->GetSize();
290 wxSize cSize
= m_choice
->GetSize();
292 int yOffset
= ( tSize
.y
+ 2 * TEXTFOCUSBORDER
- cSize
.y
) / 2;
294 // put it at an inset of 1 to have outer area shadows drawn as well
295 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , yOffset
, m_choice
->GetPopupWidth() , -1);
299 // ----------------------------------------------------------------------------
300 // operations forwarded to the subcontrols
301 // ----------------------------------------------------------------------------
303 bool wxComboBox::Enable(bool enable
)
305 if ( !wxControl::Enable(enable
) )
309 m_text
->Enable(enable
);
314 bool wxComboBox::Show(bool show
)
316 if ( !wxControl::Show(show
) )
322 void wxComboBox::DelegateTextChanged( const wxString
& value
)
324 SetStringSelection( value
);
327 void wxComboBox::DelegateChoice( const wxString
& value
)
329 SetStringSelection( value
);
332 void wxComboBox::Init()
334 WX_INIT_CONTROL_CONTAINER();
337 bool wxComboBox::Create(wxWindow
*parent
,
339 const wxString
& value
,
342 const wxArrayString
& choices
,
344 const wxValidator
& validator
,
345 const wxString
& name
)
347 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
348 style
, validator
, name
) )
356 bool wxComboBox::Create(wxWindow
*parent
,
358 const wxString
& value
,
362 const wxString choices
[],
364 const wxValidator
& validator
,
365 const wxString
& name
)
367 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
374 if ( style
& wxCB_READONLY
)
380 m_text
= new wxComboBoxText(this);
383 csize
.y
= m_text
->GetSize().y
;
384 csize
.y
+= 2 * TEXTFOCUSBORDER
;
387 m_choice
= new wxComboBoxChoice(this, style
);
389 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
391 Append( n
, choices
);
393 // Needed because it is a wxControlWithItems
394 SetInitialSize(size
);
395 SetStringSelection(value
);
400 void wxComboBox::EnableTextChangedEvents(bool enable
)
403 m_text
->ForwardEnableTextChangedEvents(enable
);
406 wxString
wxComboBox::DoGetValue() const
408 wxCHECK_MSG( m_text
, wxString(), "can't be called for read-only combobox" );
410 return m_text
->GetValue();
413 wxString
wxComboBox::GetValue() const
417 if ( m_text
== NULL
)
418 result
= m_choice
->GetString( m_choice
->GetSelection() );
420 result
= m_text
->GetValue();
425 unsigned int wxComboBox::GetCount() const
427 return m_choice
->GetCount() ;
430 void wxComboBox::SetValue(const wxString
& value
)
432 if ( HasFlag(wxCB_READONLY
) )
433 SetStringSelection( value
) ;
435 m_text
->SetValue( value
);
438 void wxComboBox::WriteText(const wxString
& text
)
440 m_text
->WriteText(text
);
443 void wxComboBox::GetSelection(long *from
, long *to
) const
445 m_text
->GetSelection(from
, to
);
448 // Clipboard operations
450 void wxComboBox::Copy()
452 if ( m_text
!= NULL
)
456 void wxComboBox::Cut()
458 if ( m_text
!= NULL
)
462 void wxComboBox::Paste()
464 if ( m_text
!= NULL
)
468 void wxComboBox::SetEditable(bool editable
)
470 if ( ( m_text
== NULL
) && editable
)
472 m_text
= new wxComboBoxText( this );
474 else if ( ( m_text
!= NULL
) && !editable
)
480 int currentX
, currentY
;
481 GetPosition( ¤tX
, ¤tY
);
483 int currentW
, currentH
;
484 GetSize( ¤tW
, ¤tH
);
486 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
489 void wxComboBox::SetInsertionPoint(long pos
)
492 m_text
->SetInsertionPoint(pos
);
495 void wxComboBox::SetInsertionPointEnd()
498 m_text
->SetInsertionPointEnd();
501 long wxComboBox::GetInsertionPoint() const
504 return m_text
->GetInsertionPoint();
508 wxTextPos
wxComboBox::GetLastPosition() const
511 return m_text
->GetLastPosition();
515 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
518 m_text
->Replace(from
,to
,value
);
521 void wxComboBox::Remove(long from
, long to
)
524 m_text
->Remove(from
,to
);
527 void wxComboBox::SetSelection(long from
, long to
)
530 m_text
->SetSelection(from
,to
);
533 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
536 wxClientDataType type
)
538 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
541 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
543 return m_choice
->DoSetItemClientData( n
, clientData
) ;
546 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
548 return m_choice
->DoGetItemClientData( n
) ;
551 wxClientDataType
wxComboBox::GetClientDataType() const
553 return m_choice
->GetClientDataType();
556 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
558 m_choice
->SetClientDataType(clientDataItemsType
);
561 void wxComboBox::DoDeleteOneItem(unsigned int n
)
563 m_choice
->DoDeleteOneItem( n
);
566 void wxComboBox::DoClear()
571 int wxComboBox::GetSelection() const
573 return m_choice
->GetSelection();
576 void wxComboBox::SetSelection(int n
)
578 m_choice
->SetSelection( n
);
580 if ( m_text
!= NULL
)
581 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
584 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
586 return m_choice
->FindString( s
, bCase
);
589 wxString
wxComboBox::GetString(unsigned int n
) const
591 return m_choice
->GetString( n
);
594 wxString
wxComboBox::GetStringSelection() const
596 int sel
= GetSelection();
597 if (sel
!= wxNOT_FOUND
)
598 return wxString(this->GetString((unsigned int)sel
));
600 return wxEmptyString
;
603 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
605 m_choice
->SetString( n
, s
);
608 bool wxComboBox::IsEditable() const
610 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
613 void wxComboBox::Undo()
619 void wxComboBox::Redo()
625 void wxComboBox::SelectAll()
631 bool wxComboBox::CanCopy() const
634 return m_text
->CanCopy();
639 bool wxComboBox::CanCut() const
642 return m_text
->CanCut();
647 bool wxComboBox::CanPaste() const
650 return m_text
->CanPaste();
655 bool wxComboBox::CanUndo() const
658 return m_text
->CanUndo();
663 bool wxComboBox::CanRedo() const
666 return m_text
->CanRedo();
671 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
674 For consistency with other platforms, clicking in the text area does not constitute a selection
675 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
676 event.SetInt(GetSelection());
677 event.SetEventObject(this);
678 event.SetString(GetStringSelection());
679 ProcessCommand(event);
685 wxTextWidgetImpl
* wxComboBox::GetTextPeer() const
688 return m_text
->GetTextPeer();
693 #endif // wxUSE_COMBOBOX && wxOSX_USE_CARBON