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 )
71 SetTriggerOnSetValue( false );
75 void OnChar( wxKeyEvent
& event
)
77 // Allows processing the tab key to go to the next control
78 if (event
.GetKeyCode() == WXK_TAB
)
80 wxNavigationKeyEvent NavEvent
;
81 NavEvent
.SetEventObject(this);
82 NavEvent
.SetDirection(true);
83 NavEvent
.SetWindowChange(false);
85 // Get the parent of the combo and have it process the navigation?
86 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
90 // send the event to the combobox class in case the user has bound EVT_CHAR
91 wxKeyEvent
kevt(event
);
92 kevt
.SetEventObject(m_cb
);
93 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
94 // If the event was handled and not skipped then we're done
97 if ( event
.GetKeyCode() == WXK_RETURN
)
99 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
100 event
.SetString( GetValue() );
101 event
.SetInt( m_cb
->GetSelection() );
102 event
.SetEventObject( m_cb
);
104 // This will invoke the dialog default action,
105 // such as the clicking the default button.
106 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
108 wxWindow
*parent
= GetParent();
109 while ( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
)
110 parent
= parent
->GetParent() ;
112 if ( parent
&& parent
->GetDefaultItem() )
114 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(), wxButton
);
115 if ( def
&& def
->IsEnabled() )
117 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
118 event
.SetEventObject(def
);
130 void OnKeyUp( wxKeyEvent
& event
)
132 event
.SetEventObject(m_cb
);
133 event
.SetId(m_cb
->GetId());
134 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
138 void OnKeyDown( wxKeyEvent
& event
)
140 event
.SetEventObject(m_cb
);
141 event
.SetId(m_cb
->GetId());
142 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
146 void OnText( wxCommandEvent
& event
)
148 event
.SetEventObject(m_cb
);
149 event
.SetId(m_cb
->GetId());
150 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
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_TEXT(-1, wxComboBoxText::OnText
)
167 class wxComboBoxChoice
: public wxChoice
170 wxComboBoxChoice( wxComboBox
*cb
, int style
)
171 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
176 int GetPopupWidth() const
178 switch ( GetWindowVariant() )
180 case wxWINDOW_VARIANT_NORMAL
:
181 case wxWINDOW_VARIANT_LARGE
:
190 void OnChoice( wxCommandEvent
& e
)
192 wxString s
= e
.GetString();
194 m_cb
->DelegateChoice( s
);
195 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
196 event2
.SetInt(m_cb
->GetSelection());
197 event2
.SetEventObject(m_cb
);
198 event2
.SetString(m_cb
->GetStringSelection());
199 m_cb
->ProcessCommand(event2
);
201 // For consistency with MSW and GTK, also send a text updated event
202 // After all, the text is updated when a selection is made
203 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
204 TextEvent
.SetString( m_cb
->GetStringSelection() );
205 TextEvent
.SetEventObject( m_cb
);
206 m_cb
->ProcessCommand( TextEvent
);
209 virtual wxSize
DoGetBestSize() const
211 wxSize sz
= wxChoice::DoGetBestSize() ;
212 if (! m_cb
->HasFlag(wxCB_READONLY
) )
213 sz
.x
= GetPopupWidth() ;
221 friend class wxComboBox
;
223 DECLARE_EVENT_TABLE()
226 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
227 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
230 wxComboBox::~wxComboBox()
232 // delete client objects
235 // delete the controls now, don't leave them alive even though they would
236 // still be eventually deleted by our parent - but it will be too late, the
237 // user code expects them to be gone now
244 if (m_choice
!= NULL
)
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 wxSize
wxComboBox::DoGetBestSize() const
257 if (!m_choice
&& !m_text
)
260 wxSize size
= m_choice
->GetBestSize();
262 if ( m_text
!= NULL
)
264 wxSize sizeText
= m_text
->GetBestSize();
265 if (sizeText
.y
> size
.y
)
268 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
269 size
.x
+= TEXTFOCUSBORDER
;
270 size
.y
+= 2 * TEXTFOCUSBORDER
;
274 // clipping is too tight
281 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
283 wxControl::DoMoveWindow( x
, y
, width
, height
);
285 if ( m_text
== NULL
)
287 // we might not be fully constructed yet, therefore watch out...
289 m_choice
->SetSize(0, 0 , width
, -1);
293 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
294 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
296 // put it at an inset of 1 to have outer area shadows drawn as well
297 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
301 // ----------------------------------------------------------------------------
302 // operations forwarded to the subcontrols
303 // ----------------------------------------------------------------------------
305 bool wxComboBox::Enable(bool enable
)
307 if ( !wxControl::Enable(enable
) )
311 m_text
->Enable(enable
);
316 bool wxComboBox::Show(bool show
)
318 if ( !wxControl::Show(show
) )
324 void wxComboBox::DelegateTextChanged( const wxString
& value
)
326 SetStringSelection( value
);
329 void wxComboBox::DelegateChoice( const wxString
& value
)
331 SetStringSelection( value
);
334 void wxComboBox::Init()
336 m_container
.SetContainerWindow(this);
339 bool wxComboBox::Create(wxWindow
*parent
,
341 const wxString
& value
,
344 const wxArrayString
& choices
,
346 const wxValidator
& validator
,
347 const wxString
& name
)
349 wxCArrayString
chs( choices
);
351 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
352 chs
.GetStrings(), style
, validator
, name
);
355 bool wxComboBox::Create(wxWindow
*parent
,
357 const wxString
& value
,
361 const wxString choices
[],
363 const wxValidator
& validator
,
364 const wxString
& name
)
366 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
372 m_choice
= new wxComboBoxChoice(this, style
);
374 if ( style
& wxCB_READONLY
)
380 m_text
= new wxComboBoxText(this);
383 csize
.y
= m_text
->GetSize().y
;
384 csize
.y
+= 2 * TEXTFOCUSBORDER
;
388 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
390 for ( int i
= 0 ; i
< n
; i
++ )
392 m_choice
->DoAppend( choices
[ i
] );
395 // Needed because it is a wxControlWithItems
397 SetStringSelection(value
);
402 wxString
wxComboBox::GetValue() const
406 if ( m_text
== NULL
)
407 result
= m_choice
->GetString( m_choice
->GetSelection() );
409 result
= m_text
->GetValue();
414 unsigned int wxComboBox::GetCount() const
416 return m_choice
->GetCount() ;
419 void wxComboBox::SetValue(const wxString
& value
)
421 if ( HasFlag(wxCB_READONLY
) )
422 SetStringSelection( value
) ;
424 m_text
->SetValue( value
);
427 // Clipboard operations
429 void wxComboBox::Copy()
431 if ( m_text
!= NULL
)
435 void wxComboBox::Cut()
437 if ( m_text
!= NULL
)
441 void wxComboBox::Paste()
443 if ( m_text
!= NULL
)
447 void wxComboBox::SetEditable(bool editable
)
449 if ( ( m_text
== NULL
) && editable
)
451 m_text
= new wxComboBoxText( this );
453 else if ( ( m_text
!= NULL
) && !editable
)
459 int currentX
, currentY
;
460 GetPosition( ¤tX
, ¤tY
);
462 int currentW
, currentH
;
463 GetSize( ¤tW
, ¤tH
);
465 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
468 void wxComboBox::SetInsertionPoint(long pos
)
473 void wxComboBox::SetInsertionPointEnd()
478 long wxComboBox::GetInsertionPoint() const
484 wxTextPos
wxComboBox::GetLastPosition() const
490 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
495 void wxComboBox::Remove(long from
, long to
)
500 void wxComboBox::SetSelection(long from
, long to
)
505 int wxComboBox::DoAppend(const wxString
& item
)
507 return m_choice
->DoAppend( item
) ;
510 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
512 return m_choice
->DoInsert( item
, pos
) ;
515 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
517 return m_choice
->DoSetItemClientData( n
, clientData
) ;
520 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
522 return m_choice
->DoGetItemClientData( n
) ;
525 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
527 return m_choice
->DoSetItemClientObject(n
, clientData
);
530 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
532 return m_choice
->DoGetItemClientObject( n
) ;
535 void wxComboBox::FreeData()
537 if ( HasClientObjectData() )
539 unsigned int count
= GetCount();
540 for ( unsigned int n
= 0; n
< count
; n
++ )
542 SetClientObject( n
, NULL
);
547 void wxComboBox::Delete(unsigned int n
)
549 // force client object deletion
550 if( HasClientObjectData() )
551 SetClientObject( n
, NULL
);
552 m_choice
->Delete( n
);
555 void wxComboBox::Clear()
561 int wxComboBox::GetSelection() const
563 return m_choice
->GetSelection();
566 void wxComboBox::SetSelection(int n
)
568 m_choice
->SetSelection( n
);
570 if ( m_text
!= NULL
)
571 m_text
->SetValue(GetString(n
));
574 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
576 return m_choice
->FindString( s
, bCase
);
579 wxString
wxComboBox::GetString(unsigned int n
) const
581 return m_choice
->GetString( n
);
584 wxString
wxComboBox::GetStringSelection() const
586 int sel
= GetSelection();
587 if (sel
!= wxNOT_FOUND
)
588 return wxString(this->GetString((unsigned int)sel
));
590 return wxEmptyString
;
593 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
595 m_choice
->SetString( n
, s
);
598 bool wxComboBox::IsEditable() const
600 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
603 void wxComboBox::Undo()
609 void wxComboBox::Redo()
615 void wxComboBox::SelectAll()
621 bool wxComboBox::CanCopy() const
624 return m_text
->CanCopy();
629 bool wxComboBox::CanCut() const
632 return m_text
->CanCut();
637 bool wxComboBox::CanPaste() const
640 return m_text
->CanPaste();
645 bool wxComboBox::CanUndo() const
648 return m_text
->CanUndo();
653 bool wxComboBox::CanRedo() const
656 return m_text
->CanRedo();
661 wxInt32
wxComboBox::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
664 For consistency with other platforms, clicking in the text area does not constitute a selection
665 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
666 event.SetInt(GetSelection());
667 event.SetEventObject(this);
668 event.SetString(GetStringSelection());
669 ProcessCommand(event);