1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor, Dan "Bud" Keith (composite combobox)
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #if wxUSE_COMBOBOX && wxOSX_USE_CARBON
15 #include "wx/combobox.h"
18 #include "wx/button.h"
20 #include "wx/containr.h"
21 #include "wx/toplevel.h"
22 #include "wx/textctrl.h"
25 #include "wx/osx/private.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
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 ;
39 // ----------------------------------------------------------------------------
40 // wxComboBoxText: text control forwards events to combobox
41 // ----------------------------------------------------------------------------
43 class wxComboBoxText
: public wxTextCtrl
46 wxComboBoxText( wxComboBox
* cb
)
47 : wxTextCtrl( cb
, 1 )
53 void OnChar( wxKeyEvent
& event
)
55 // Allows processing the tab key to go to the next control
56 if (event
.GetKeyCode() == WXK_TAB
)
58 wxNavigationKeyEvent NavEvent
;
59 NavEvent
.SetEventObject(this);
60 NavEvent
.SetDirection(!event
.ShiftDown());
61 NavEvent
.SetWindowChange(false);
63 // Get the parent of the combo and have it process the navigation?
64 if (m_cb
->GetParent()->HandleWindowEvent(NavEvent
))
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
75 if ( event
.GetKeyCode() == WXK_RETURN
)
77 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_cb
->GetId());
78 event
.SetString( GetValue() );
79 event
.SetInt( m_cb
->GetSelection() );
80 event
.SetEventObject( m_cb
);
82 // This will invoke the dialog default action,
83 // such as the clicking the default button.
84 if (!m_cb
->HandleWindowEvent( event
))
86 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
87 if ( tlw
&& tlw
->GetDefaultItem() )
89 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
90 if ( def
&& def
->IsEnabled() )
92 wxCommandEvent
event( wxEVT_BUTTON
, def
->GetId() );
93 event
.SetEventObject(def
);
105 void OnKeyUp( wxKeyEvent
& event
)
107 event
.SetEventObject(m_cb
);
108 event
.SetId(m_cb
->GetId());
109 if (! m_cb
->HandleWindowEvent(event
))
113 void OnKeyDown( wxKeyEvent
& event
)
115 event
.SetEventObject(m_cb
);
116 event
.SetId(m_cb
->GetId());
117 if (! m_cb
->HandleWindowEvent(event
))
121 void OnText( wxCommandEvent
& event
)
123 event
.SetEventObject(m_cb
);
124 event
.SetId(m_cb
->GetId());
125 if (! m_cb
->HandleWindowEvent(event
))
129 void OnFocus( wxFocusEvent
& event
)
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
);
143 DECLARE_EVENT_TABLE()
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
)
155 class wxComboBoxChoice
: public wxChoice
158 wxComboBoxChoice( wxComboBox
*cb
, int style
)
159 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
164 int GetPopupWidth() const
166 switch ( GetWindowVariant() )
168 case wxWINDOW_VARIANT_NORMAL
:
169 case wxWINDOW_VARIANT_LARGE
:
178 void OnChoice( wxCommandEvent
& e
)
180 wxString s
= e
.GetString();
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
);
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
);
197 virtual wxSize
DoGetBestSize() const
199 wxSize sz
= wxChoice::DoGetBestSize() ;
200 if (! m_cb
->HasFlag(wxCB_READONLY
) )
201 sz
.x
= GetPopupWidth() ;
209 friend class wxComboBox
;
211 DECLARE_EVENT_TABLE()
214 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
215 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
218 wxComboBox::~wxComboBox()
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
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 wxSize
wxComboBox::DoGetBestSize() const
233 if (!m_choice
&& !m_text
)
236 wxSize size
= m_choice
->GetBestSize();
238 if ( m_text
!= NULL
)
240 wxSize sizeText
= m_text
->GetBestSize();
241 if (sizeText
.y
+ 2 * TEXTFOCUSBORDER
> size
.y
)
242 size
.y
= sizeText
.y
+ 2 * TEXTFOCUSBORDER
;
244 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
245 size
.x
+= TEXTFOCUSBORDER
;
249 // clipping is too tight
256 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
258 wxControl::DoMoveWindow( x
, y
, width
, height
);
260 if ( m_text
== NULL
)
262 // we might not be fully constructed yet, therefore watch out...
264 m_choice
->SetSize(0, 0 , width
, -1);
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();
273 int yOffset
= ( tSize
.y
+ 2 * TEXTFOCUSBORDER
- cSize
.y
) / 2;
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);
280 // ----------------------------------------------------------------------------
281 // operations forwarded to the subcontrols
282 // ----------------------------------------------------------------------------
284 bool wxComboBox::Enable(bool enable
)
286 if ( !wxControl::Enable(enable
) )
290 m_text
->Enable(enable
);
295 bool wxComboBox::Show(bool show
)
297 if ( !wxControl::Show(show
) )
303 void wxComboBox::DelegateTextChanged( const wxString
& value
)
305 SetStringSelection( value
);
308 void wxComboBox::DelegateChoice( const wxString
& value
)
310 SetStringSelection( value
);
313 bool wxComboBox::Create(wxWindow
*parent
,
315 const wxString
& value
,
318 const wxArrayString
& choices
,
320 const wxValidator
& validator
,
321 const wxString
& name
)
323 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
324 style
, validator
, name
) )
332 bool wxComboBox::Create(wxWindow
*parent
,
334 const wxString
& value
,
338 const wxString choices
[],
340 const wxValidator
& validator
,
341 const wxString
& name
)
343 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
350 if ( style
& wxCB_READONLY
)
356 m_text
= new wxComboBoxText(this);
359 csize
.y
= m_text
->GetSize().y
;
360 csize
.y
+= 2 * TEXTFOCUSBORDER
;
363 m_choice
= new wxComboBoxChoice(this, style
);
365 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
367 Append( n
, choices
);
369 // Needed because it is a wxControlWithItems
370 SetInitialSize(size
);
371 SetStringSelection(value
);
376 void wxComboBox::EnableTextChangedEvents(bool enable
)
379 m_text
->ForwardEnableTextChangedEvents(enable
);
382 wxString
wxComboBox::DoGetValue() const
384 wxCHECK_MSG( m_text
, wxString(), "can't be called for read-only combobox" );
386 return m_text
->GetValue();
389 wxString
wxComboBox::GetValue() const
393 if ( m_text
== NULL
)
394 result
= m_choice
->GetString( m_choice
->GetSelection() );
396 result
= m_text
->GetValue();
401 unsigned int wxComboBox::GetCount() const
403 return m_choice
->GetCount() ;
406 void wxComboBox::SetValue(const wxString
& value
)
408 if ( HasFlag(wxCB_READONLY
) )
409 SetStringSelection( value
) ;
411 m_text
->SetValue( value
);
414 void wxComboBox::WriteText(const wxString
& text
)
416 m_text
->WriteText(text
);
419 void wxComboBox::GetSelection(long *from
, long *to
) const
421 m_text
->GetSelection(from
, to
);
424 // Clipboard operations
426 void wxComboBox::Copy()
428 if ( m_text
!= NULL
)
432 void wxComboBox::Cut()
434 if ( m_text
!= NULL
)
438 void wxComboBox::Paste()
440 if ( m_text
!= NULL
)
444 void wxComboBox::SetEditable(bool editable
)
446 if ( ( m_text
== NULL
) && editable
)
448 m_text
= new wxComboBoxText( this );
450 else if ( !editable
)
455 int currentX
, currentY
;
456 GetPosition( ¤tX
, ¤tY
);
458 int currentW
, currentH
;
459 GetSize( ¤tW
, ¤tH
);
461 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
464 void wxComboBox::SetInsertionPoint(long pos
)
467 m_text
->SetInsertionPoint(pos
);
470 void wxComboBox::SetInsertionPointEnd()
473 m_text
->SetInsertionPointEnd();
476 long wxComboBox::GetInsertionPoint() const
479 return m_text
->GetInsertionPoint();
483 wxTextPos
wxComboBox::GetLastPosition() const
486 return m_text
->GetLastPosition();
490 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
493 m_text
->Replace(from
,to
,value
);
496 void wxComboBox::Remove(long from
, long to
)
499 m_text
->Remove(from
,to
);
502 void wxComboBox::SetSelection(long from
, long to
)
505 m_text
->SetSelection(from
,to
);
508 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
511 wxClientDataType type
)
513 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
516 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
518 return m_choice
->DoSetItemClientData( n
, clientData
) ;
521 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
523 return m_choice
->DoGetItemClientData( n
) ;
526 wxClientDataType
wxComboBox::GetClientDataType() const
528 return m_choice
->GetClientDataType();
531 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
533 m_choice
->SetClientDataType(clientDataItemsType
);
536 void wxComboBox::DoDeleteOneItem(unsigned int n
)
538 m_choice
->DoDeleteOneItem( n
);
541 void wxComboBox::DoClear()
546 int wxComboBox::GetSelection() const
548 return m_choice
->GetSelection();
551 void wxComboBox::SetSelection(int n
)
553 m_choice
->SetSelection( n
);
555 if ( m_text
!= NULL
)
556 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
559 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
561 return m_choice
->FindString( s
, bCase
);
564 wxString
wxComboBox::GetString(unsigned int n
) const
566 return m_choice
->GetString( n
);
569 wxString
wxComboBox::GetStringSelection() const
571 int sel
= GetSelection();
572 if (sel
!= wxNOT_FOUND
)
573 return wxString(this->GetString((unsigned int)sel
));
575 return wxEmptyString
;
578 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
580 m_choice
->SetString( n
, s
);
583 bool wxComboBox::IsEditable() const
585 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
588 void wxComboBox::Undo()
594 void wxComboBox::Redo()
600 void wxComboBox::SelectAll()
606 bool wxComboBox::CanCopy() const
609 return m_text
->CanCopy();
614 bool wxComboBox::CanCut() const
617 return m_text
->CanCut();
622 bool wxComboBox::CanPaste() const
625 return m_text
->CanPaste();
630 bool wxComboBox::CanUndo() const
633 return m_text
->CanUndo();
638 bool wxComboBox::CanRedo() const
641 return m_text
->CanRedo();
646 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
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);
660 wxTextWidgetImpl
* wxComboBox::GetTextPeer() const
663 return m_text
->GetTextPeer();
668 #endif // wxUSE_COMBOBOX && wxOSX_USE_CARBON