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::GetValue() const
401 if ( m_text
== NULL
)
402 result
= m_choice
->GetString( m_choice
->GetSelection() );
404 result
= m_text
->GetValue();
409 unsigned int wxComboBox::GetCount() const
411 return m_choice
->GetCount() ;
414 void wxComboBox::SetValue(const wxString
& value
)
416 if ( HasFlag(wxCB_READONLY
) )
417 SetStringSelection( value
) ;
419 m_text
->SetValue( value
);
422 void wxComboBox::WriteText(const wxString
& text
)
424 m_text
->WriteText(text
);
427 void wxComboBox::GetSelection(long *from
, long *to
) const
429 m_text
->GetSelection(from
, to
);
432 // Clipboard operations
434 void wxComboBox::Copy()
436 if ( m_text
!= NULL
)
440 void wxComboBox::Cut()
442 if ( m_text
!= NULL
)
446 void wxComboBox::Paste()
448 if ( m_text
!= NULL
)
452 void wxComboBox::SetEditable(bool editable
)
454 if ( ( m_text
== NULL
) && editable
)
456 m_text
= new wxComboBoxText( this );
458 else if ( ( m_text
!= NULL
) && !editable
)
464 int currentX
, currentY
;
465 GetPosition( ¤tX
, ¤tY
);
467 int currentW
, currentH
;
468 GetSize( ¤tW
, ¤tH
);
470 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
473 void wxComboBox::SetInsertionPoint(long pos
)
476 m_text
->SetInsertionPoint(pos
);
479 void wxComboBox::SetInsertionPointEnd()
482 m_text
->SetInsertionPointEnd();
485 long wxComboBox::GetInsertionPoint() const
488 return m_text
->GetInsertionPoint();
492 wxTextPos
wxComboBox::GetLastPosition() const
495 return m_text
->GetLastPosition();
499 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
502 m_text
->Replace(from
,to
,value
);
505 void wxComboBox::Remove(long from
, long to
)
508 m_text
->Remove(from
,to
);
511 void wxComboBox::SetSelection(long from
, long to
)
514 m_text
->SetSelection(from
,to
);
517 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
520 wxClientDataType type
)
522 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
525 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
527 return m_choice
->DoSetItemClientData( n
, clientData
) ;
530 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
532 return m_choice
->DoGetItemClientData( n
) ;
535 wxClientDataType
wxComboBox::GetClientDataType() const
537 return m_choice
->GetClientDataType();
540 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
542 m_choice
->SetClientDataType(clientDataItemsType
);
545 void wxComboBox::DoDeleteOneItem(unsigned int n
)
547 m_choice
->DoDeleteOneItem( n
);
550 void wxComboBox::DoClear()
555 int wxComboBox::GetSelection() const
557 return m_choice
->GetSelection();
560 void wxComboBox::SetSelection(int n
)
562 m_choice
->SetSelection( n
);
564 if ( m_text
!= NULL
)
565 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
568 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
570 return m_choice
->FindString( s
, bCase
);
573 wxString
wxComboBox::GetString(unsigned int n
) const
575 return m_choice
->GetString( n
);
578 wxString
wxComboBox::GetStringSelection() const
580 int sel
= GetSelection();
581 if (sel
!= wxNOT_FOUND
)
582 return wxString(this->GetString((unsigned int)sel
));
584 return wxEmptyString
;
587 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
589 m_choice
->SetString( n
, s
);
592 bool wxComboBox::IsEditable() const
594 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
597 void wxComboBox::Undo()
603 void wxComboBox::Redo()
609 void wxComboBox::SelectAll()
615 bool wxComboBox::CanCopy() const
618 return m_text
->CanCopy();
623 bool wxComboBox::CanCut() const
626 return m_text
->CanCut();
631 bool wxComboBox::CanPaste() const
634 return m_text
->CanPaste();
639 bool wxComboBox::CanUndo() const
642 return m_text
->CanUndo();
647 bool wxComboBox::CanRedo() const
650 return m_text
->CanRedo();
655 bool wxComboBox::HandleClicked( double timestampsec
)
658 For consistency with other platforms, clicking in the text area does not constitute a selection
659 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
660 event.SetInt(GetSelection());
661 event.SetEventObject(this);
662 event.SetString(GetStringSelection());
663 ProcessCommand(event);
669 #endif // wxUSE_COMBOBOX