1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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"
17 #include "wx/button.h"
19 #include "wx/containr.h"
20 #include "wx/mac/uma.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
24 WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox
)
26 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
27 WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox
)
31 static int nextPopUpMenuId
= 1000 ;
33 MenuHandle
NewUniqueMenu()
35 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the margin between the text control and the choice
47 #if TARGET_API_MAC_OSX
48 // margin should be bigger on OS X due to blue highlight
49 // around text control.
50 static const wxCoord MARGIN
= 4;
51 // this is the border a focus rect on OSX is needing
52 static const int TEXTFOCUSBORDER
= 3 ;
54 static const wxCoord MARGIN
= 2;
55 static const int TEXTFOCUSBORDER
= 0 ;
57 static const int POPUPHEIGHT
= 23;
60 // ----------------------------------------------------------------------------
61 // wxComboBoxText: text control forwards events to combobox
62 // ----------------------------------------------------------------------------
64 class wxComboBoxText
: public wxTextCtrl
67 wxComboBoxText( wxComboBox
* cb
)
68 : wxTextCtrl( cb
, 1 )
74 void OnChar( wxKeyEvent
& event
)
76 // Allows processing the tab key to go to the next control
77 if (event
.GetKeyCode() == WXK_TAB
)
79 wxNavigationKeyEvent NavEvent
;
80 NavEvent
.SetEventObject(this);
81 NavEvent
.SetDirection(true);
82 NavEvent
.SetWindowChange(false);
84 // Get the parent of the combo and have it process the navigation?
85 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
89 // send the event to the combobox class in case the user has bound EVT_CHAR
90 wxKeyEvent
kevt(event
);
91 kevt
.SetEventObject(m_cb
);
92 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
93 // If the event was handled and not skipped then we're done
96 if ( event
.GetKeyCode() == WXK_RETURN
)
98 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
99 event
.SetString( GetValue() );
100 event
.SetInt( m_cb
->GetSelection() );
101 event
.SetEventObject( m_cb
);
103 // This will invoke the dialog default action,
104 // such as the clicking the default button.
105 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
107 wxWindow
*parent
= GetParent();
108 while ( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
)
109 parent
= parent
->GetParent() ;
111 if ( parent
&& parent
->GetDefaultItem() )
113 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(), wxButton
);
114 if ( def
&& def
->IsEnabled() )
116 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
117 event
.SetEventObject(def
);
129 void OnKeyUp( wxKeyEvent
& event
)
131 event
.SetEventObject(m_cb
);
132 event
.SetId(m_cb
->GetId());
133 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
137 void OnKeyDown( wxKeyEvent
& event
)
139 event
.SetEventObject(m_cb
);
140 event
.SetId(m_cb
->GetId());
141 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
145 void OnText( wxCommandEvent
& event
)
147 event
.SetEventObject(m_cb
);
148 event
.SetId(m_cb
->GetId());
149 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
156 DECLARE_EVENT_TABLE()
159 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
160 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
161 EVT_CHAR(wxComboBoxText::OnChar
)
162 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
163 EVT_TEXT(-1, wxComboBoxText::OnText
)
166 class wxComboBoxChoice
: public wxChoice
169 wxComboBoxChoice( wxComboBox
*cb
, int style
)
170 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
175 int GetPopupWidth() const
177 switch ( GetWindowVariant() )
179 case wxWINDOW_VARIANT_NORMAL
:
180 case wxWINDOW_VARIANT_LARGE
:
189 void OnChoice( wxCommandEvent
& e
)
191 wxString s
= e
.GetString();
193 m_cb
->DelegateChoice( s
);
194 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
195 event2
.SetInt(m_cb
->GetSelection());
196 event2
.SetEventObject(m_cb
);
197 event2
.SetString(m_cb
->GetStringSelection());
198 m_cb
->ProcessCommand(event2
);
200 // For consistency with MSW and GTK, also send a text updated event
201 // After all, the text is updated when a selection is made
202 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
203 TextEvent
.SetString( m_cb
->GetStringSelection() );
204 TextEvent
.SetEventObject( m_cb
);
205 m_cb
->ProcessCommand( TextEvent
);
208 virtual wxSize
DoGetBestSize() const
210 wxSize sz
= wxChoice::DoGetBestSize() ;
211 if (! m_cb
->HasFlag(wxCB_READONLY
) )
212 sz
.x
= GetPopupWidth() ;
220 friend class wxComboBox
;
222 DECLARE_EVENT_TABLE()
225 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
226 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
229 wxComboBox::~wxComboBox()
231 // delete client objects
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
> size
.y
)
267 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
268 size
.x
+= TEXTFOCUSBORDER
;
269 size
.y
+= 2 * TEXTFOCUSBORDER
;
273 // clipping is too tight
280 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
282 wxControl::DoMoveWindow( x
, y
, width
, height
);
284 if ( m_text
== NULL
)
286 // we might not be fully constructed yet, therefore watch out...
288 m_choice
->SetSize(0, 0 , width
, -1);
292 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
293 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
295 // put it at an inset of 1 to have outer area shadows drawn as well
296 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
300 // ----------------------------------------------------------------------------
301 // operations forwarded to the subcontrols
302 // ----------------------------------------------------------------------------
304 bool wxComboBox::Enable(bool enable
)
306 if ( !wxControl::Enable(enable
) )
310 m_text
->Enable(enable
);
315 bool wxComboBox::Show(bool show
)
317 if ( !wxControl::Show(show
) )
323 void wxComboBox::DelegateTextChanged( const wxString
& value
)
325 SetStringSelection( value
);
328 void wxComboBox::DelegateChoice( const wxString
& value
)
330 SetStringSelection( value
);
333 void wxComboBox::Init()
335 m_container
.SetContainerWindow(this);
338 bool wxComboBox::Create(wxWindow
*parent
,
340 const wxString
& value
,
343 const wxArrayString
& choices
,
345 const wxValidator
& validator
,
346 const wxString
& name
)
348 wxCArrayString
chs( choices
);
350 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
351 chs
.GetStrings(), style
, validator
, name
);
354 bool wxComboBox::Create(wxWindow
*parent
,
356 const wxString
& value
,
360 const wxString choices
[],
362 const wxValidator
& validator
,
363 const wxString
& name
)
365 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
371 m_choice
= new wxComboBoxChoice(this, style
);
373 if ( style
& wxCB_READONLY
)
379 m_text
= new wxComboBoxText(this);
382 csize
.y
= m_text
->GetSize().y
;
383 csize
.y
+= 2 * TEXTFOCUSBORDER
;
387 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
389 for ( int i
= 0 ; i
< n
; i
++ )
391 m_choice
->DoAppend( choices
[ i
] );
394 // Needed because it is a wxControlWithItems
396 SetStringSelection(value
);
401 wxString
wxComboBox::GetValue() const
405 if ( m_text
== NULL
)
406 result
= m_choice
->GetString( m_choice
->GetSelection() );
408 result
= m_text
->GetValue();
413 int wxComboBox::GetCount() const
415 return m_choice
->GetCount() ;
418 void wxComboBox::SetValue(const wxString
& value
)
420 if ( HasFlag(wxCB_READONLY
) )
421 SetStringSelection( value
) ;
423 m_text
->SetValue( value
);
426 // Clipboard operations
428 void wxComboBox::Copy()
430 if ( m_text
!= NULL
)
434 void wxComboBox::Cut()
436 if ( m_text
!= NULL
)
440 void wxComboBox::Paste()
442 if ( m_text
!= NULL
)
446 void wxComboBox::SetEditable(bool editable
)
448 if ( ( m_text
== NULL
) && editable
)
450 m_text
= new wxComboBoxText( this );
452 else if ( ( m_text
!= NULL
) && !editable
)
458 int currentX
, currentY
;
459 GetPosition( ¤tX
, ¤tY
);
461 int currentW
, currentH
;
462 GetSize( ¤tW
, ¤tH
);
464 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
467 void wxComboBox::SetInsertionPoint(long pos
)
472 void wxComboBox::SetInsertionPointEnd()
477 long wxComboBox::GetInsertionPoint() const
483 wxTextPos
wxComboBox::GetLastPosition() const
489 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
494 void wxComboBox::Remove(long from
, long to
)
499 void wxComboBox::SetSelection(long from
, long to
)
504 int wxComboBox::DoAppend(const wxString
& item
)
506 return m_choice
->DoAppend( item
) ;
509 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
511 return m_choice
->DoInsert( item
, pos
) ;
514 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
516 return m_choice
->DoSetItemClientData( n
, clientData
) ;
519 void* wxComboBox::DoGetItemClientData(int n
) const
521 return m_choice
->DoGetItemClientData( n
) ;
524 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
526 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
529 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
531 return m_choice
->DoGetItemClientObject( n
) ;
534 void wxComboBox::FreeData()
536 if ( HasClientObjectData() )
538 size_t count
= GetCount();
539 for ( size_t n
= 0; n
< count
; n
++ )
541 SetClientObject( n
, NULL
);
546 void wxComboBox::Delete(int n
)
548 // force client object deletion
549 if( HasClientObjectData() )
550 SetClientObject( n
, NULL
);
551 m_choice
->Delete( n
);
554 void wxComboBox::Clear()
560 int wxComboBox::GetSelection() const
562 return m_choice
->GetSelection();
565 void wxComboBox::SetSelection(int n
)
567 m_choice
->SetSelection( n
);
569 if ( m_text
!= NULL
)
570 m_text
->SetValue( GetString( n
) );
573 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
575 return m_choice
->FindString( s
, bCase
);
578 wxString
wxComboBox::GetString(int n
) const
580 return m_choice
->GetString( n
);
583 wxString
wxComboBox::GetStringSelection() const
585 int sel
= GetSelection();
587 return wxString(this->GetString (sel
));
589 return wxEmptyString
;
592 void wxComboBox::SetString(int n
, const wxString
& s
)
594 m_choice
->SetString( n
, s
) ;
597 bool wxComboBox::IsEditable() const
599 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
602 void wxComboBox::Undo()
608 void wxComboBox::Redo()
614 void wxComboBox::SelectAll()
620 bool wxComboBox::CanCopy() const
623 return m_text
->CanCopy();
628 bool wxComboBox::CanCut() const
631 return m_text
->CanCut();
636 bool wxComboBox::CanPaste() const
639 return m_text
->CanPaste();
644 bool wxComboBox::CanUndo() const
647 return m_text
->CanUndo();
652 bool wxComboBox::CanRedo() const
655 return m_text
->CanRedo();
660 wxInt32
wxComboBox::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
663 For consistency with other platforms, clicking in the text area does not constitute a selection
664 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
665 event.SetInt(GetSelection());
666 event.SetEventObject(this);
667 event.SetString(GetStringSelection());
668 ProcessCommand(event);