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/mac/uma.h"
21 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
24 static int nextPopUpMenuId
= 1000 ;
26 MenuHandle
NewUniqueMenu()
28 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the margin between the text control and the choice
40 #if TARGET_API_MAC_OSX
41 // margin should be bigger on OS X due to blue highlight
42 // around text control.
43 static const wxCoord MARGIN
= 4;
44 // this is the border a focus rect on OSX is needing
45 static const int TEXTFOCUSBORDER
= 3 ;
47 static const wxCoord MARGIN
= 2;
48 static const int TEXTFOCUSBORDER
= 0 ;
50 static const int POPUPHEIGHT
= 23;
53 // ----------------------------------------------------------------------------
54 // wxComboBoxText: text control forwards events to combobox
55 // ----------------------------------------------------------------------------
57 class wxComboBoxText
: public wxTextCtrl
60 wxComboBoxText( wxComboBox
* cb
)
61 : wxTextCtrl( cb
, 1 )
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()->GetEventHandler()->ProcessEvent(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
->GetEventHandler()->ProcessEvent(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
->GetEventHandler()->ProcessEvent( event
))
100 wxWindow
*parent
= GetParent();
101 while ( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
)
102 parent
= parent
->GetParent() ;
104 if ( parent
&& parent
->GetDefaultItem() )
106 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(), wxButton
);
107 if ( def
&& def
->IsEnabled() )
109 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
110 event
.SetEventObject(def
);
122 void OnKeyUp( wxKeyEvent
& event
)
124 event
.SetEventObject(m_cb
);
125 event
.SetId(m_cb
->GetId());
126 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
130 void OnKeyDown( wxKeyEvent
& event
)
132 event
.SetEventObject(m_cb
);
133 event
.SetId(m_cb
->GetId());
134 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
138 void OnText( wxCommandEvent
& event
)
140 event
.SetEventObject(m_cb
);
141 event
.SetId(m_cb
->GetId());
142 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
149 DECLARE_EVENT_TABLE()
152 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
153 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
154 EVT_CHAR(wxComboBoxText::OnChar
)
155 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
156 EVT_TEXT(-1, wxComboBoxText::OnText
)
159 class wxComboBoxChoice
: public wxChoice
162 wxComboBoxChoice( wxComboBox
*cb
, int style
)
163 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
168 int GetPopupWidth() const
170 switch ( GetWindowVariant() )
172 case wxWINDOW_VARIANT_NORMAL
:
173 case wxWINDOW_VARIANT_LARGE
:
182 void OnChoice( wxCommandEvent
& e
)
184 wxString s
= e
.GetString();
186 m_cb
->DelegateChoice( s
);
187 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
188 event2
.SetInt(m_cb
->GetSelection());
189 event2
.SetEventObject(m_cb
);
190 event2
.SetString(m_cb
->GetStringSelection());
191 m_cb
->ProcessCommand(event2
);
193 // For consistency with MSW and GTK, also send a text updated event
194 // After all, the text is updated when a selection is made
195 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
196 TextEvent
.SetString( m_cb
->GetStringSelection() );
197 TextEvent
.SetEventObject( m_cb
);
198 m_cb
->ProcessCommand( TextEvent
);
201 virtual wxSize
DoGetBestSize() const
203 wxSize sz
= wxChoice::DoGetBestSize() ;
204 if (! m_cb
->HasFlag(wxCB_READONLY
) )
205 sz
.x
= GetPopupWidth() ;
213 friend class wxComboBox
;
215 DECLARE_EVENT_TABLE()
218 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
219 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
222 wxComboBox::~wxComboBox()
224 // delete client objects
227 // delete the controls now, don't leave them alive even though they would
228 // still be eventually deleted by our parent - but it will be too late, the
229 // user code expects them to be gone now
236 if (m_choice
!= NULL
)
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxSize
wxComboBox::DoGetBestSize() const
249 if (!m_choice
&& !m_text
)
252 wxSize size
= m_choice
->GetBestSize();
254 if ( m_text
!= NULL
)
256 wxSize sizeText
= m_text
->GetBestSize();
257 if (sizeText
.y
> size
.y
)
260 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
261 size
.x
+= TEXTFOCUSBORDER
;
262 size
.y
+= 2 * TEXTFOCUSBORDER
;
266 // clipping is too tight
273 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
275 wxControl::DoMoveWindow( x
, y
, width
, height
);
277 if ( m_text
== NULL
)
279 // we might not be fully constructed yet, therefore watch out...
281 m_choice
->SetSize(0, 0 , width
, -1);
285 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
286 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
288 // put it at an inset of 1 to have outer area shadows drawn as well
289 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
293 // ----------------------------------------------------------------------------
294 // operations forwarded to the subcontrols
295 // ----------------------------------------------------------------------------
297 bool wxComboBox::Enable(bool enable
)
299 if ( !wxControl::Enable(enable
) )
303 m_text
->Enable(enable
);
308 bool wxComboBox::Show(bool show
)
310 if ( !wxControl::Show(show
) )
316 void wxComboBox::SetFocus()
322 void wxComboBox::DelegateTextChanged( const wxString
& value
)
324 SetStringSelection( value
);
327 void wxComboBox::DelegateChoice( const wxString
& value
)
329 SetStringSelection( value
);
332 bool wxComboBox::Create(wxWindow
*parent
,
334 const wxString
& value
,
337 const wxArrayString
& choices
,
339 const wxValidator
& validator
,
340 const wxString
& name
)
342 wxCArrayString
chs( choices
);
344 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
345 chs
.GetStrings(), style
, validator
, name
);
348 bool wxComboBox::Create(wxWindow
*parent
,
350 const wxString
& value
,
354 const wxString choices
[],
356 const wxValidator
& validator
,
357 const wxString
& name
)
359 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
365 m_choice
= new wxComboBoxChoice(this, style
);
367 if ( style
& wxCB_READONLY
)
373 m_text
= new wxComboBoxText(this);
376 csize
.y
= m_text
->GetSize().y
;
377 csize
.y
+= 2 * TEXTFOCUSBORDER
;
381 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
383 for ( int i
= 0 ; i
< n
; i
++ )
385 m_choice
->DoAppend( choices
[ i
] );
388 // Needed because it is a wxControlWithItems
390 SetStringSelection(value
);
395 wxString
wxComboBox::GetValue() const
399 if ( m_text
== NULL
)
400 result
= m_choice
->GetString( m_choice
->GetSelection() );
402 result
= m_text
->GetValue();
407 int wxComboBox::GetCount() const
409 return m_choice
->GetCount() ;
412 void wxComboBox::SetValue(const wxString
& value
)
414 if ( HasFlag(wxCB_READONLY
) )
415 SetStringSelection( value
) ;
417 m_text
->SetValue( value
);
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
)
466 void wxComboBox::SetInsertionPointEnd()
471 long wxComboBox::GetInsertionPoint() const
477 wxTextPos
wxComboBox::GetLastPosition() const
483 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
488 void wxComboBox::Remove(long from
, long to
)
493 void wxComboBox::SetSelection(long from
, long to
)
498 int wxComboBox::DoAppend(const wxString
& item
)
500 return m_choice
->DoAppend( item
) ;
503 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
505 return m_choice
->DoInsert( item
, pos
) ;
508 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
510 return m_choice
->DoSetItemClientData( n
, clientData
) ;
513 void* wxComboBox::DoGetItemClientData(int n
) const
515 return m_choice
->DoGetItemClientData( n
) ;
518 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
520 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
523 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
525 return m_choice
->DoGetItemClientObject( n
) ;
528 void wxComboBox::FreeData()
530 if ( HasClientObjectData() )
532 size_t count
= GetCount();
533 for ( size_t n
= 0; n
< count
; n
++ )
535 SetClientObject( n
, NULL
);
540 void wxComboBox::Delete(int n
)
542 // force client object deletion
543 if( HasClientObjectData() )
544 SetClientObject( n
, NULL
);
545 m_choice
->Delete( n
);
548 void wxComboBox::Clear()
554 int wxComboBox::GetSelection() const
556 return m_choice
->GetSelection();
559 void wxComboBox::SetSelection(int n
)
561 m_choice
->SetSelection( n
);
563 if ( m_text
!= NULL
)
564 m_text
->SetValue( GetString( n
) );
567 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
569 return m_choice
->FindString( s
, bCase
);
572 wxString
wxComboBox::GetString(int n
) const
574 return m_choice
->GetString( n
);
577 wxString
wxComboBox::GetStringSelection() const
579 int sel
= GetSelection();
581 return wxString(this->GetString (sel
));
583 return wxEmptyString
;
586 void wxComboBox::SetString(int n
, const wxString
& s
)
588 m_choice
->SetString( n
, s
) ;
591 bool wxComboBox::IsEditable() const
593 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
596 void wxComboBox::Undo()
602 void wxComboBox::Redo()
608 void wxComboBox::SelectAll()
614 bool wxComboBox::CanCopy() const
617 return m_text
->CanCopy();
622 bool wxComboBox::CanCut() const
625 return m_text
->CanCut();
630 bool wxComboBox::CanPaste() const
633 return m_text
->CanPaste();
638 bool wxComboBox::CanUndo() const
641 return m_text
->CanUndo();
646 bool wxComboBox::CanRedo() const
649 return m_text
->CanRedo();
654 wxInt32
wxComboBox::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
657 For consistency with other platforms, clicking in the text area does not constitute a selection
658 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
659 event.SetInt(GetSelection());
660 event.SetEventObject(this);
661 event.SetString(GetStringSelection());
662 ProcessCommand(event);