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"
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 )
60 SetTriggerOnSetValue( false );
64 void OnChar( wxKeyEvent
& event
)
66 // Allows processing the tab key to go to the next control
67 if (event
.GetKeyCode() == WXK_TAB
)
69 wxNavigationKeyEvent NavEvent
;
70 NavEvent
.SetEventObject(this);
71 NavEvent
.SetDirection(true);
72 NavEvent
.SetWindowChange(false);
74 // Get the parent of the combo and have it process the navigation?
75 if (m_cb
->GetParent()->HandleWindowEvent(NavEvent
))
79 // send the event to the combobox class in case the user has bound EVT_CHAR
80 wxKeyEvent
kevt(event
);
81 kevt
.SetEventObject(m_cb
);
82 if (m_cb
->HandleWindowEvent(kevt
))
83 // If the event was handled and not skipped then we're done
86 if ( event
.GetKeyCode() == WXK_RETURN
)
88 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
89 event
.SetString( GetValue() );
90 event
.SetInt( m_cb
->GetSelection() );
91 event
.SetEventObject( m_cb
);
93 // This will invoke the dialog default action,
94 // such as the clicking the default button.
95 if (!m_cb
->HandleWindowEvent( event
))
97 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
98 if ( tlw
&& tlw
->GetDefaultItem() )
100 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
101 if ( def
&& def
->IsEnabled() )
103 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
104 event
.SetEventObject(def
);
116 void OnKeyUp( wxKeyEvent
& event
)
118 event
.SetEventObject(m_cb
);
119 event
.SetId(m_cb
->GetId());
120 if (! m_cb
->HandleWindowEvent(event
))
124 void OnKeyDown( wxKeyEvent
& event
)
126 event
.SetEventObject(m_cb
);
127 event
.SetId(m_cb
->GetId());
128 if (! m_cb
->HandleWindowEvent(event
))
132 void OnText( wxCommandEvent
& event
)
134 event
.SetEventObject(m_cb
);
135 event
.SetId(m_cb
->GetId());
136 if (! m_cb
->HandleWindowEvent(event
))
140 void OnFocus( wxFocusEvent
& event
)
142 // in case the textcontrol gets the focus we propagate
143 // it to the parent's handlers.
144 wxFocusEvent
evt2(event
.GetEventType(),m_cb
->GetId());
145 evt2
.SetEventObject(m_cb
);
146 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
> size
.y
)
263 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
264 size
.x
+= TEXTFOCUSBORDER
;
265 size
.y
+= 2 * TEXTFOCUSBORDER
;
269 // clipping is too tight
276 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
278 wxControl::DoMoveWindow( x
, y
, width
, height
);
280 if ( m_text
== NULL
)
282 // we might not be fully constructed yet, therefore watch out...
284 m_choice
->SetSize(0, 0 , width
, -1);
288 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
289 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
291 // put it at an inset of 1 to have outer area shadows drawn as well
292 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
296 // ----------------------------------------------------------------------------
297 // operations forwarded to the subcontrols
298 // ----------------------------------------------------------------------------
300 bool wxComboBox::Enable(bool enable
)
302 if ( !wxControl::Enable(enable
) )
306 m_text
->Enable(enable
);
311 bool wxComboBox::Show(bool show
)
313 if ( !wxControl::Show(show
) )
319 void wxComboBox::DelegateTextChanged( const wxString
& value
)
321 SetStringSelection( value
);
324 void wxComboBox::DelegateChoice( const wxString
& value
)
326 SetStringSelection( value
);
329 void wxComboBox::Init()
331 WX_INIT_CONTROL_CONTAINER();
334 bool wxComboBox::Create(wxWindow
*parent
,
336 const wxString
& value
,
339 const wxArrayString
& choices
,
341 const wxValidator
& validator
,
342 const wxString
& name
)
344 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
345 style
, validator
, name
) )
353 bool wxComboBox::Create(wxWindow
*parent
,
355 const wxString
& value
,
359 const wxString choices
[],
361 const wxValidator
& validator
,
362 const wxString
& name
)
364 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
371 if ( style
& wxCB_READONLY
)
377 m_text
= new wxComboBoxText(this);
380 csize
.y
= m_text
->GetSize().y
;
381 csize
.y
+= 2 * TEXTFOCUSBORDER
;
384 m_choice
= new wxComboBoxChoice(this, style
);
386 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
388 Append( n
, choices
);
390 // Needed because it is a wxControlWithItems
391 SetInitialSize(size
);
392 SetStringSelection(value
);
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 ( ( m_text
!= NULL
) && !editable
)
471 int currentX
, currentY
;
472 GetPosition( ¤tX
, ¤tY
);
474 int currentW
, currentH
;
475 GetSize( ¤tW
, ¤tH
);
477 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
480 void wxComboBox::SetInsertionPoint(long pos
)
483 m_text
->SetInsertionPoint(pos
);
486 void wxComboBox::SetInsertionPointEnd()
489 m_text
->SetInsertionPointEnd();
492 long wxComboBox::GetInsertionPoint() const
495 return m_text
->GetInsertionPoint();
499 wxTextPos
wxComboBox::GetLastPosition() const
502 return m_text
->GetLastPosition();
506 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
509 m_text
->Replace(from
,to
,value
);
512 void wxComboBox::Remove(long from
, long to
)
515 m_text
->Remove(from
,to
);
518 void wxComboBox::SetSelection(long from
, long to
)
521 m_text
->SetSelection(from
,to
);
524 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
527 wxClientDataType type
)
529 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
532 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
534 return m_choice
->DoSetItemClientData( n
, clientData
) ;
537 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
539 return m_choice
->DoGetItemClientData( n
) ;
542 wxClientDataType
wxComboBox::GetClientDataType() const
544 return m_choice
->GetClientDataType();
547 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
549 m_choice
->SetClientDataType(clientDataItemsType
);
552 void wxComboBox::DoDeleteOneItem(unsigned int n
)
554 m_choice
->DoDeleteOneItem( n
);
557 void wxComboBox::DoClear()
562 int wxComboBox::GetSelection() const
564 return m_choice
->GetSelection();
567 void wxComboBox::SetSelection(int n
)
569 m_choice
->SetSelection( n
);
571 if ( m_text
!= NULL
)
572 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
575 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
577 return m_choice
->FindString( s
, bCase
);
580 wxString
wxComboBox::GetString(unsigned int n
) const
582 return m_choice
->GetString( n
);
585 wxString
wxComboBox::GetStringSelection() const
587 int sel
= GetSelection();
588 if (sel
!= wxNOT_FOUND
)
589 return wxString(this->GetString((unsigned int)sel
));
591 return wxEmptyString
;
594 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
596 m_choice
->SetString( n
, s
);
599 bool wxComboBox::IsEditable() const
601 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
604 void wxComboBox::Undo()
610 void wxComboBox::Redo()
616 void wxComboBox::SelectAll()
622 bool wxComboBox::CanCopy() const
625 return m_text
->CanCopy();
630 bool wxComboBox::CanCut() const
633 return m_text
->CanCut();
638 bool wxComboBox::CanPaste() const
641 return m_text
->CanPaste();
646 bool wxComboBox::CanUndo() const
649 return m_text
->CanUndo();
654 bool wxComboBox::CanRedo() const
657 return m_text
->CanRedo();
662 bool wxComboBox::OSXHandleClicked( double timestampsec
)
665 For consistency with other platforms, clicking in the text area does not constitute a selection
666 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
667 event.SetInt(GetSelection());
668 event.SetEventObject(this);
669 event.SetString(GetStringSelection());
670 ProcessCommand(event);
676 #endif // wxUSE_COMBOBOX