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
))
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_TEXT(wxID_ANY
, wxComboBoxText::OnText
)
153 class wxComboBoxChoice
: public wxChoice
156 wxComboBoxChoice( wxComboBox
*cb
, int style
)
157 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
162 int GetPopupWidth() const
164 switch ( GetWindowVariant() )
166 case wxWINDOW_VARIANT_NORMAL
:
167 case wxWINDOW_VARIANT_LARGE
:
176 void OnChoice( wxCommandEvent
& e
)
178 wxString s
= e
.GetString();
180 m_cb
->DelegateChoice( s
);
181 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
182 event2
.SetInt(m_cb
->GetSelection());
183 event2
.SetEventObject(m_cb
);
184 event2
.SetString(m_cb
->GetStringSelection());
185 m_cb
->ProcessCommand(event2
);
187 // For consistency with MSW and GTK, also send a text updated event
188 // After all, the text is updated when a selection is made
189 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
190 TextEvent
.SetString( m_cb
->GetStringSelection() );
191 TextEvent
.SetEventObject( m_cb
);
192 m_cb
->ProcessCommand( TextEvent
);
195 virtual wxSize
DoGetBestSize() const
197 wxSize sz
= wxChoice::DoGetBestSize() ;
198 if (! m_cb
->HasFlag(wxCB_READONLY
) )
199 sz
.x
= GetPopupWidth() ;
207 friend class wxComboBox
;
209 DECLARE_EVENT_TABLE()
212 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
213 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
216 wxComboBox::~wxComboBox()
218 // delete the controls now, don't leave them alive even though they would
219 // still be eventually deleted by our parent - but it will be too late, the
220 // user code expects them to be gone now
227 if (m_choice
!= NULL
)
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 wxSize
wxComboBox::DoGetBestSize() const
240 if (!m_choice
&& !m_text
)
243 wxSize size
= m_choice
->GetBestSize();
245 if ( m_text
!= NULL
)
247 wxSize sizeText
= m_text
->GetBestSize();
248 if (sizeText
.y
> size
.y
)
251 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
252 size
.x
+= TEXTFOCUSBORDER
;
253 size
.y
+= 2 * 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);
279 // put it at an inset of 1 to have outer area shadows drawn as well
280 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
284 // ----------------------------------------------------------------------------
285 // operations forwarded to the subcontrols
286 // ----------------------------------------------------------------------------
288 bool wxComboBox::Enable(bool enable
)
290 if ( !wxControl::Enable(enable
) )
294 m_text
->Enable(enable
);
299 bool wxComboBox::Show(bool show
)
301 if ( !wxControl::Show(show
) )
307 void wxComboBox::DelegateTextChanged( const wxString
& value
)
309 SetStringSelection( value
);
312 void wxComboBox::DelegateChoice( const wxString
& value
)
314 SetStringSelection( value
);
317 void wxComboBox::Init()
319 WX_INIT_CONTROL_CONTAINER();
322 bool wxComboBox::Create(wxWindow
*parent
,
324 const wxString
& value
,
327 const wxArrayString
& choices
,
329 const wxValidator
& validator
,
330 const wxString
& name
)
332 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
333 style
, validator
, name
) )
341 bool wxComboBox::Create(wxWindow
*parent
,
343 const wxString
& value
,
347 const wxString choices
[],
349 const wxValidator
& validator
,
350 const wxString
& name
)
352 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
359 if ( style
& wxCB_READONLY
)
365 m_text
= new wxComboBoxText(this);
368 csize
.y
= m_text
->GetSize().y
;
369 csize
.y
+= 2 * TEXTFOCUSBORDER
;
372 m_choice
= new wxComboBoxChoice(this, style
);
374 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
376 Append( n
, choices
);
378 // Needed because it is a wxControlWithItems
379 SetInitialSize(size
);
380 SetStringSelection(value
);
385 wxString
wxComboBox::GetValue() const
389 if ( m_text
== NULL
)
390 result
= m_choice
->GetString( m_choice
->GetSelection() );
392 result
= m_text
->GetValue();
397 unsigned int wxComboBox::GetCount() const
399 return m_choice
->GetCount() ;
402 void wxComboBox::SetValue(const wxString
& value
)
404 if ( HasFlag(wxCB_READONLY
) )
405 SetStringSelection( value
) ;
407 m_text
->SetValue( value
);
410 void wxComboBox::WriteText(const wxString
& text
)
412 m_text
->WriteText(text
);
415 void wxComboBox::GetSelection(long *from
, long *to
) const
417 m_text
->GetSelection(from
, to
);
420 // Clipboard operations
422 void wxComboBox::Copy()
424 if ( m_text
!= NULL
)
428 void wxComboBox::Cut()
430 if ( m_text
!= NULL
)
434 void wxComboBox::Paste()
436 if ( m_text
!= NULL
)
440 void wxComboBox::SetEditable(bool editable
)
442 if ( ( m_text
== NULL
) && editable
)
444 m_text
= new wxComboBoxText( this );
446 else if ( ( m_text
!= NULL
) && !editable
)
452 int currentX
, currentY
;
453 GetPosition( ¤tX
, ¤tY
);
455 int currentW
, currentH
;
456 GetSize( ¤tW
, ¤tH
);
458 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
461 void wxComboBox::SetInsertionPoint(long pos
)
464 m_text
->SetInsertionPoint(pos
);
467 void wxComboBox::SetInsertionPointEnd()
470 m_text
->SetInsertionPointEnd();
473 long wxComboBox::GetInsertionPoint() const
476 return m_text
->GetInsertionPoint();
480 wxTextPos
wxComboBox::GetLastPosition() const
483 return m_text
->GetLastPosition();
487 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
490 m_text
->Replace(from
,to
,value
);
493 void wxComboBox::Remove(long from
, long to
)
496 m_text
->Remove(from
,to
);
499 void wxComboBox::SetSelection(long from
, long to
)
502 m_text
->SetSelection(from
,to
);
505 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
508 wxClientDataType type
)
510 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
513 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
515 return m_choice
->DoSetItemClientData( n
, clientData
) ;
518 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
520 return m_choice
->DoGetItemClientData( n
) ;
523 wxClientDataType
wxComboBox::GetClientDataType() const
525 return m_choice
->GetClientDataType();
528 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
530 m_choice
->SetClientDataType(clientDataItemsType
);
533 void wxComboBox::DoDeleteOneItem(unsigned int n
)
535 m_choice
->DoDeleteOneItem( n
);
538 void wxComboBox::DoClear()
543 int wxComboBox::GetSelection() const
545 return m_choice
->GetSelection();
548 void wxComboBox::SetSelection(int n
)
550 m_choice
->SetSelection( n
);
552 if ( m_text
!= NULL
)
553 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
556 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
558 return m_choice
->FindString( s
, bCase
);
561 wxString
wxComboBox::GetString(unsigned int n
) const
563 return m_choice
->GetString( n
);
566 wxString
wxComboBox::GetStringSelection() const
568 int sel
= GetSelection();
569 if (sel
!= wxNOT_FOUND
)
570 return wxString(this->GetString((unsigned int)sel
));
572 return wxEmptyString
;
575 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
577 m_choice
->SetString( n
, s
);
580 bool wxComboBox::IsEditable() const
582 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
585 void wxComboBox::Undo()
591 void wxComboBox::Redo()
597 void wxComboBox::SelectAll()
603 bool wxComboBox::CanCopy() const
606 return m_text
->CanCopy();
611 bool wxComboBox::CanCut() const
614 return m_text
->CanCut();
619 bool wxComboBox::CanPaste() const
622 return m_text
->CanPaste();
627 bool wxComboBox::CanUndo() const
630 return m_text
->CanUndo();
635 bool wxComboBox::CanRedo() const
638 return m_text
->CanRedo();
643 bool wxComboBox::HandleClicked( double timestampsec
)
646 For consistency with other platforms, clicking in the text area does not constitute a selection
647 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
648 event.SetInt(GetSelection());
649 event.SetEventObject(this);
650 event.SetString(GetStringSelection());
651 ProcessCommand(event);
657 #endif // wxUSE_COMBOBOX