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 )
62 void ForwardEnableTextChangedEvents(bool enable
)
64 EnableTextChangedEvents(enable
);
67 void OnChar( wxKeyEvent
& event
)
69 // Allows processing the tab key to go to the next control
70 if (event
.GetKeyCode() == WXK_TAB
)
72 wxNavigationKeyEvent NavEvent
;
73 NavEvent
.SetEventObject(this);
74 NavEvent
.SetDirection(true);
75 NavEvent
.SetWindowChange(false);
77 // Get the parent of the combo and have it process the navigation?
78 if (m_cb
->GetParent()->HandleWindowEvent(NavEvent
))
82 // send the event to the combobox class in case the user has bound EVT_CHAR
83 wxKeyEvent
kevt(event
);
84 kevt
.SetEventObject(m_cb
);
85 if (m_cb
->HandleWindowEvent(kevt
))
86 // If the event was handled and not skipped then we're done
89 if ( event
.GetKeyCode() == WXK_RETURN
)
91 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
92 event
.SetString( GetValue() );
93 event
.SetInt( m_cb
->GetSelection() );
94 event
.SetEventObject( m_cb
);
96 // This will invoke the dialog default action,
97 // such as the clicking the default button.
98 if (!m_cb
->HandleWindowEvent( event
))
100 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
101 if ( tlw
&& tlw
->GetDefaultItem() )
103 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
104 if ( def
&& def
->IsEnabled() )
106 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
107 event
.SetEventObject(def
);
119 void OnKeyUp( wxKeyEvent
& event
)
121 event
.SetEventObject(m_cb
);
122 event
.SetId(m_cb
->GetId());
123 if (! m_cb
->HandleWindowEvent(event
))
127 void OnKeyDown( wxKeyEvent
& event
)
129 event
.SetEventObject(m_cb
);
130 event
.SetId(m_cb
->GetId());
131 if (! m_cb
->HandleWindowEvent(event
))
135 void OnText( wxCommandEvent
& event
)
137 event
.SetEventObject(m_cb
);
138 event
.SetId(m_cb
->GetId());
139 if (! m_cb
->HandleWindowEvent(event
))
143 void OnFocus( wxFocusEvent
& event
)
145 // in case the textcontrol gets the focus we propagate
146 // it to the parent's handlers.
147 wxFocusEvent
evt2(event
.GetEventType(),m_cb
->GetId());
148 evt2
.SetEventObject(m_cb
);
149 m_cb
->GetEventHandler()->ProcessEvent(evt2
);
157 DECLARE_EVENT_TABLE()
160 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
161 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
162 EVT_CHAR(wxComboBoxText::OnChar
)
163 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
164 EVT_SET_FOCUS(wxComboBoxText::OnFocus
)
165 EVT_KILL_FOCUS(wxComboBoxText::OnFocus
)
166 EVT_TEXT(wxID_ANY
, wxComboBoxText::OnText
)
169 class wxComboBoxChoice
: public wxChoice
172 wxComboBoxChoice( wxComboBox
*cb
, int style
)
173 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
178 int GetPopupWidth() const
180 switch ( GetWindowVariant() )
182 case wxWINDOW_VARIANT_NORMAL
:
183 case wxWINDOW_VARIANT_LARGE
:
192 void OnChoice( wxCommandEvent
& e
)
194 wxString s
= e
.GetString();
196 m_cb
->DelegateChoice( s
);
197 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
198 event2
.SetInt(m_cb
->GetSelection());
199 event2
.SetEventObject(m_cb
);
200 event2
.SetString(m_cb
->GetStringSelection());
201 m_cb
->ProcessCommand(event2
);
203 // For consistency with MSW and GTK, also send a text updated event
204 // After all, the text is updated when a selection is made
205 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
206 TextEvent
.SetString( m_cb
->GetStringSelection() );
207 TextEvent
.SetEventObject( m_cb
);
208 m_cb
->ProcessCommand( TextEvent
);
211 virtual wxSize
DoGetBestSize() const
213 wxSize sz
= wxChoice::DoGetBestSize() ;
214 if (! m_cb
->HasFlag(wxCB_READONLY
) )
215 sz
.x
= GetPopupWidth() ;
223 friend class wxComboBox
;
225 DECLARE_EVENT_TABLE()
228 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
229 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
232 wxComboBox::~wxComboBox()
234 // delete the controls now, don't leave them alive even though they would
235 // still be eventually deleted by our parent - but it will be too late, the
236 // user code expects them to be gone now
243 if (m_choice
!= NULL
)
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 wxSize
wxComboBox::DoGetBestSize() const
256 if (!m_choice
&& !m_text
)
259 wxSize size
= m_choice
->GetBestSize();
261 if ( m_text
!= NULL
)
263 wxSize sizeText
= m_text
->GetBestSize();
264 if (sizeText
.y
+ 2 * TEXTFOCUSBORDER
> size
.y
)
265 size
.y
= sizeText
.y
+ 2 * TEXTFOCUSBORDER
;
267 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
268 size
.x
+= TEXTFOCUSBORDER
;
272 // clipping is too tight
279 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
281 wxControl::DoMoveWindow( x
, y
, width
, height
);
283 if ( m_text
== NULL
)
285 // we might not be fully constructed yet, therefore watch out...
287 m_choice
->SetSize(0, 0 , width
, -1);
291 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
292 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
293 wxSize tSize
= m_text
->GetSize();
294 wxSize cSize
= m_choice
->GetSize();
296 int yOffset
= ( tSize
.y
+ 2 * TEXTFOCUSBORDER
- cSize
.y
) / 2;
298 // put it at an inset of 1 to have outer area shadows drawn as well
299 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , yOffset
, m_choice
->GetPopupWidth() , -1);
303 // ----------------------------------------------------------------------------
304 // operations forwarded to the subcontrols
305 // ----------------------------------------------------------------------------
307 bool wxComboBox::Enable(bool enable
)
309 if ( !wxControl::Enable(enable
) )
313 m_text
->Enable(enable
);
318 bool wxComboBox::Show(bool show
)
320 if ( !wxControl::Show(show
) )
326 void wxComboBox::DelegateTextChanged( const wxString
& value
)
328 SetStringSelection( value
);
331 void wxComboBox::DelegateChoice( const wxString
& value
)
333 SetStringSelection( value
);
336 void wxComboBox::Init()
338 WX_INIT_CONTROL_CONTAINER();
341 bool wxComboBox::Create(wxWindow
*parent
,
343 const wxString
& value
,
346 const wxArrayString
& choices
,
348 const wxValidator
& validator
,
349 const wxString
& name
)
351 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
352 style
, validator
, name
) )
360 bool wxComboBox::Create(wxWindow
*parent
,
362 const wxString
& value
,
366 const wxString choices
[],
368 const wxValidator
& validator
,
369 const wxString
& name
)
371 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
378 if ( style
& wxCB_READONLY
)
384 m_text
= new wxComboBoxText(this);
387 csize
.y
= m_text
->GetSize().y
;
388 csize
.y
+= 2 * TEXTFOCUSBORDER
;
391 m_choice
= new wxComboBoxChoice(this, style
);
393 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
395 Append( n
, choices
);
397 // Needed because it is a wxControlWithItems
398 SetInitialSize(size
);
399 SetStringSelection(value
);
404 void wxComboBox::EnableTextChangedEvents(bool enable
)
407 m_text
->ForwardEnableTextChangedEvents(enable
);
410 wxString
wxComboBox::DoGetValue() const
412 wxCHECK_MSG( m_text
, wxString(), "can't be called for read-only combobox" );
414 return m_text
->GetValue();
417 wxString
wxComboBox::GetValue() const
421 if ( m_text
== NULL
)
422 result
= m_choice
->GetString( m_choice
->GetSelection() );
424 result
= m_text
->GetValue();
429 unsigned int wxComboBox::GetCount() const
431 return m_choice
->GetCount() ;
434 void wxComboBox::SetValue(const wxString
& value
)
436 if ( HasFlag(wxCB_READONLY
) )
437 SetStringSelection( value
) ;
439 m_text
->SetValue( value
);
442 void wxComboBox::WriteText(const wxString
& text
)
444 m_text
->WriteText(text
);
447 void wxComboBox::GetSelection(long *from
, long *to
) const
449 m_text
->GetSelection(from
, to
);
452 // Clipboard operations
454 void wxComboBox::Copy()
456 if ( m_text
!= NULL
)
460 void wxComboBox::Cut()
462 if ( m_text
!= NULL
)
466 void wxComboBox::Paste()
468 if ( m_text
!= NULL
)
472 void wxComboBox::SetEditable(bool editable
)
474 if ( ( m_text
== NULL
) && editable
)
476 m_text
= new wxComboBoxText( this );
478 else if ( ( m_text
!= NULL
) && !editable
)
484 int currentX
, currentY
;
485 GetPosition( ¤tX
, ¤tY
);
487 int currentW
, currentH
;
488 GetSize( ¤tW
, ¤tH
);
490 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
493 void wxComboBox::SetInsertionPoint(long pos
)
496 m_text
->SetInsertionPoint(pos
);
499 void wxComboBox::SetInsertionPointEnd()
502 m_text
->SetInsertionPointEnd();
505 long wxComboBox::GetInsertionPoint() const
508 return m_text
->GetInsertionPoint();
512 wxTextPos
wxComboBox::GetLastPosition() const
515 return m_text
->GetLastPosition();
519 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
522 m_text
->Replace(from
,to
,value
);
525 void wxComboBox::Remove(long from
, long to
)
528 m_text
->Remove(from
,to
);
531 void wxComboBox::SetSelection(long from
, long to
)
534 m_text
->SetSelection(from
,to
);
537 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
540 wxClientDataType type
)
542 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
545 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
547 return m_choice
->DoSetItemClientData( n
, clientData
) ;
550 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
552 return m_choice
->DoGetItemClientData( n
) ;
555 wxClientDataType
wxComboBox::GetClientDataType() const
557 return m_choice
->GetClientDataType();
560 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
562 m_choice
->SetClientDataType(clientDataItemsType
);
565 void wxComboBox::DoDeleteOneItem(unsigned int n
)
567 m_choice
->DoDeleteOneItem( n
);
570 void wxComboBox::DoClear()
575 int wxComboBox::GetSelection() const
577 return m_choice
->GetSelection();
580 void wxComboBox::SetSelection(int n
)
582 m_choice
->SetSelection( n
);
584 if ( m_text
!= NULL
)
585 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
588 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
590 return m_choice
->FindString( s
, bCase
);
593 wxString
wxComboBox::GetString(unsigned int n
) const
595 return m_choice
->GetString( n
);
598 wxString
wxComboBox::GetStringSelection() const
600 int sel
= GetSelection();
601 if (sel
!= wxNOT_FOUND
)
602 return wxString(this->GetString((unsigned int)sel
));
604 return wxEmptyString
;
607 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
609 m_choice
->SetString( n
, s
);
612 bool wxComboBox::IsEditable() const
614 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
617 void wxComboBox::Undo()
623 void wxComboBox::Redo()
629 void wxComboBox::SelectAll()
635 bool wxComboBox::CanCopy() const
638 return m_text
->CanCopy();
643 bool wxComboBox::CanCut() const
646 return m_text
->CanCut();
651 bool wxComboBox::CanPaste() const
654 return m_text
->CanPaste();
659 bool wxComboBox::CanUndo() const
662 return m_text
->CanUndo();
667 bool wxComboBox::CanRedo() const
670 return m_text
->CanRedo();
675 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
678 For consistency with other platforms, clicking in the text area does not constitute a selection
679 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
680 event.SetInt(GetSelection());
681 event.SetEventObject(this);
682 event.SetString(GetStringSelection());
683 ProcessCommand(event);
689 #endif // wxUSE_COMBOBOX