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"
14 #if wxUSE_COMBOBOX && wxOSX_USE_CARBON
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 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 // the margin between the text control and the choice
33 // margin should be bigger on OS X due to blue highlight
34 // around text control.
35 static const wxCoord MARGIN
= 4;
36 // this is the border a focus rect on OSX is needing
37 static const int TEXTFOCUSBORDER
= 3 ;
40 // ----------------------------------------------------------------------------
41 // wxComboBoxText: text control forwards events to combobox
42 // ----------------------------------------------------------------------------
44 class wxComboBoxText
: public wxTextCtrl
47 wxComboBoxText( wxComboBox
* cb
)
48 : wxTextCtrl( cb
, 1 )
54 void OnChar( wxKeyEvent
& event
)
56 // Allows processing the tab key to go to the next control
57 if (event
.GetKeyCode() == WXK_TAB
)
59 wxNavigationKeyEvent NavEvent
;
60 NavEvent
.SetEventObject(this);
61 NavEvent
.SetDirection(true);
62 NavEvent
.SetWindowChange(false);
64 // Get the parent of the combo and have it process the navigation?
65 if (m_cb
->GetParent()->HandleWindowEvent(NavEvent
))
69 // send the event to the combobox class in case the user has bound EVT_CHAR
70 wxKeyEvent
kevt(event
);
71 kevt
.SetEventObject(m_cb
);
72 if (m_cb
->HandleWindowEvent(kevt
))
73 // If the event was handled and not skipped then we're done
76 if ( event
.GetKeyCode() == WXK_RETURN
)
78 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
79 event
.SetString( GetValue() );
80 event
.SetInt( m_cb
->GetSelection() );
81 event
.SetEventObject( m_cb
);
83 // This will invoke the dialog default action,
84 // such as the clicking the default button.
85 if (!m_cb
->HandleWindowEvent( event
))
87 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
88 if ( tlw
&& tlw
->GetDefaultItem() )
90 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
91 if ( def
&& def
->IsEnabled() )
93 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
94 event
.SetEventObject(def
);
106 void OnKeyUp( wxKeyEvent
& event
)
108 event
.SetEventObject(m_cb
);
109 event
.SetId(m_cb
->GetId());
110 if (! m_cb
->HandleWindowEvent(event
))
114 void OnKeyDown( wxKeyEvent
& event
)
116 event
.SetEventObject(m_cb
);
117 event
.SetId(m_cb
->GetId());
118 if (! m_cb
->HandleWindowEvent(event
))
122 void OnText( wxCommandEvent
& event
)
124 event
.SetEventObject(m_cb
);
125 event
.SetId(m_cb
->GetId());
126 if (! m_cb
->HandleWindowEvent(event
))
130 void OnFocus( wxFocusEvent
& event
)
132 // in case the textcontrol gets the focus we propagate
133 // it to the parent's handlers.
134 wxFocusEvent
evt2(event
.GetEventType(),m_cb
->GetId());
135 evt2
.SetEventObject(m_cb
);
136 m_cb
->GetEventHandler()->ProcessEvent(evt2
);
144 DECLARE_EVENT_TABLE()
147 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
148 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
149 EVT_CHAR(wxComboBoxText::OnChar
)
150 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
151 EVT_SET_FOCUS(wxComboBoxText::OnFocus
)
152 EVT_KILL_FOCUS(wxComboBoxText::OnFocus
)
153 EVT_TEXT(wxID_ANY
, wxComboBoxText::OnText
)
156 class wxComboBoxChoice
: public wxChoice
159 wxComboBoxChoice( wxComboBox
*cb
, int style
)
160 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
165 int GetPopupWidth() const
167 switch ( GetWindowVariant() )
169 case wxWINDOW_VARIANT_NORMAL
:
170 case wxWINDOW_VARIANT_LARGE
:
179 void OnChoice( wxCommandEvent
& e
)
181 wxString s
= e
.GetString();
183 m_cb
->DelegateChoice( s
);
184 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
185 event2
.SetInt(m_cb
->GetSelection());
186 event2
.SetEventObject(m_cb
);
187 event2
.SetString(m_cb
->GetStringSelection());
188 m_cb
->ProcessCommand(event2
);
190 // For consistency with MSW and GTK, also send a text updated event
191 // After all, the text is updated when a selection is made
192 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
193 TextEvent
.SetString( m_cb
->GetStringSelection() );
194 TextEvent
.SetEventObject( m_cb
);
195 m_cb
->ProcessCommand( TextEvent
);
198 virtual wxSize
DoGetBestSize() const
200 wxSize sz
= wxChoice::DoGetBestSize() ;
201 if (! m_cb
->HasFlag(wxCB_READONLY
) )
202 sz
.x
= GetPopupWidth() ;
210 friend class wxComboBox
;
212 DECLARE_EVENT_TABLE()
215 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
216 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
219 wxComboBox::~wxComboBox()
221 // delete the controls now, don't leave them alive even though they would
222 // still be eventually deleted by our parent - but it will be too late, the
223 // user code expects them to be gone now
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 wxSize
wxComboBox::DoGetBestSize() const
234 if (!m_choice
&& !m_text
)
237 wxSize size
= m_choice
->GetBestSize();
239 if ( m_text
!= NULL
)
241 wxSize sizeText
= m_text
->GetBestSize();
242 if (sizeText
.y
+ 2 * TEXTFOCUSBORDER
> size
.y
)
243 size
.y
= sizeText
.y
+ 2 * TEXTFOCUSBORDER
;
245 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
246 size
.x
+= TEXTFOCUSBORDER
;
250 // clipping is too tight
257 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
259 wxControl::DoMoveWindow( x
, y
, width
, height
);
261 if ( m_text
== NULL
)
263 // we might not be fully constructed yet, therefore watch out...
265 m_choice
->SetSize(0, 0 , width
, -1);
269 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
270 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
271 wxSize tSize
= m_text
->GetSize();
272 wxSize cSize
= m_choice
->GetSize();
274 int yOffset
= ( tSize
.y
+ 2 * TEXTFOCUSBORDER
- cSize
.y
) / 2;
276 // put it at an inset of 1 to have outer area shadows drawn as well
277 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , yOffset
, m_choice
->GetPopupWidth() , -1);
281 // ----------------------------------------------------------------------------
282 // operations forwarded to the subcontrols
283 // ----------------------------------------------------------------------------
285 bool wxComboBox::Enable(bool enable
)
287 if ( !wxControl::Enable(enable
) )
291 m_text
->Enable(enable
);
296 bool wxComboBox::Show(bool show
)
298 if ( !wxControl::Show(show
) )
304 void wxComboBox::DelegateTextChanged( const wxString
& value
)
306 SetStringSelection( value
);
309 void wxComboBox::DelegateChoice( const wxString
& value
)
311 SetStringSelection( value
);
314 bool wxComboBox::Create(wxWindow
*parent
,
316 const wxString
& value
,
319 const wxArrayString
& choices
,
321 const wxValidator
& validator
,
322 const wxString
& name
)
324 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
325 style
, validator
, name
) )
333 bool wxComboBox::Create(wxWindow
*parent
,
335 const wxString
& value
,
339 const wxString choices
[],
341 const wxValidator
& validator
,
342 const wxString
& name
)
344 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
351 if ( style
& wxCB_READONLY
)
357 m_text
= new wxComboBoxText(this);
360 csize
.y
= m_text
->GetSize().y
;
361 csize
.y
+= 2 * TEXTFOCUSBORDER
;
364 m_choice
= new wxComboBoxChoice(this, style
);
366 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
368 Append( n
, choices
);
370 // Needed because it is a wxControlWithItems
371 SetInitialSize(size
);
372 SetStringSelection(value
);
377 void wxComboBox::EnableTextChangedEvents(bool enable
)
380 m_text
->ForwardEnableTextChangedEvents(enable
);
383 wxString
wxComboBox::DoGetValue() const
385 wxCHECK_MSG( m_text
, wxString(), "can't be called for read-only combobox" );
387 return m_text
->GetValue();
390 wxString
wxComboBox::GetValue() const
394 if ( m_text
== NULL
)
395 result
= m_choice
->GetString( m_choice
->GetSelection() );
397 result
= m_text
->GetValue();
402 unsigned int wxComboBox::GetCount() const
404 return m_choice
->GetCount() ;
407 void wxComboBox::SetValue(const wxString
& value
)
409 if ( HasFlag(wxCB_READONLY
) )
410 SetStringSelection( value
) ;
412 m_text
->SetValue( value
);
415 void wxComboBox::WriteText(const wxString
& text
)
417 m_text
->WriteText(text
);
420 void wxComboBox::GetSelection(long *from
, long *to
) const
422 m_text
->GetSelection(from
, to
);
425 // Clipboard operations
427 void wxComboBox::Copy()
429 if ( m_text
!= NULL
)
433 void wxComboBox::Cut()
435 if ( m_text
!= NULL
)
439 void wxComboBox::Paste()
441 if ( m_text
!= NULL
)
445 void wxComboBox::SetEditable(bool editable
)
447 if ( ( m_text
== NULL
) && editable
)
449 m_text
= new wxComboBoxText( this );
451 else if ( !editable
)
456 int currentX
, currentY
;
457 GetPosition( ¤tX
, ¤tY
);
459 int currentW
, currentH
;
460 GetSize( ¤tW
, ¤tH
);
462 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
465 void wxComboBox::SetInsertionPoint(long pos
)
468 m_text
->SetInsertionPoint(pos
);
471 void wxComboBox::SetInsertionPointEnd()
474 m_text
->SetInsertionPointEnd();
477 long wxComboBox::GetInsertionPoint() const
480 return m_text
->GetInsertionPoint();
484 wxTextPos
wxComboBox::GetLastPosition() const
487 return m_text
->GetLastPosition();
491 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
494 m_text
->Replace(from
,to
,value
);
497 void wxComboBox::Remove(long from
, long to
)
500 m_text
->Remove(from
,to
);
503 void wxComboBox::SetSelection(long from
, long to
)
506 m_text
->SetSelection(from
,to
);
509 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
512 wxClientDataType type
)
514 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
517 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
519 return m_choice
->DoSetItemClientData( n
, clientData
) ;
522 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
524 return m_choice
->DoGetItemClientData( n
) ;
527 wxClientDataType
wxComboBox::GetClientDataType() const
529 return m_choice
->GetClientDataType();
532 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
534 m_choice
->SetClientDataType(clientDataItemsType
);
537 void wxComboBox::DoDeleteOneItem(unsigned int n
)
539 m_choice
->DoDeleteOneItem( n
);
542 void wxComboBox::DoClear()
547 int wxComboBox::GetSelection() const
549 return m_choice
->GetSelection();
552 void wxComboBox::SetSelection(int n
)
554 m_choice
->SetSelection( n
);
556 if ( m_text
!= NULL
)
557 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
560 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
562 return m_choice
->FindString( s
, bCase
);
565 wxString
wxComboBox::GetString(unsigned int n
) const
567 return m_choice
->GetString( n
);
570 wxString
wxComboBox::GetStringSelection() const
572 int sel
= GetSelection();
573 if (sel
!= wxNOT_FOUND
)
574 return wxString(this->GetString((unsigned int)sel
));
576 return wxEmptyString
;
579 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
581 m_choice
->SetString( n
, s
);
584 bool wxComboBox::IsEditable() const
586 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
589 void wxComboBox::Undo()
595 void wxComboBox::Redo()
601 void wxComboBox::SelectAll()
607 bool wxComboBox::CanCopy() const
610 return m_text
->CanCopy();
615 bool wxComboBox::CanCut() const
618 return m_text
->CanCut();
623 bool wxComboBox::CanPaste() const
626 return m_text
->CanPaste();
631 bool wxComboBox::CanUndo() const
634 return m_text
->CanUndo();
639 bool wxComboBox::CanRedo() const
642 return m_text
->CanRedo();
647 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
650 For consistency with other platforms, clicking in the text area does not constitute a selection
651 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
652 event.SetInt(GetSelection());
653 event.SetEventObject(this);
654 event.SetString(GetStringSelection());
655 ProcessCommand(event);
661 wxTextWidgetImpl
* wxComboBox::GetTextPeer() const
664 return m_text
->GetTextPeer();
669 #endif // wxUSE_COMBOBOX && wxOSX_USE_CARBON