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"
19 #include "wx/button.h"
21 #include "wx/containr.h"
22 #include "wx/toplevel.h"
23 #include "wx/textctrl.h"
26 #include "wx/mac/uma.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 static int nextPopUpMenuId
= 1000 ;
39 MenuHandle
NewUniqueMenu()
41 MenuHandle handle
= UMANewMenu(nextPopUpMenuId
, wxString(wxT("Menu")), wxFont::GetDefaultEncoding() );
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the margin between the text control and the choice
53 #if TARGET_API_MAC_OSX
54 // margin should be bigger on OS X due to blue highlight
55 // around text control.
56 static const wxCoord MARGIN
= 4;
57 // this is the border a focus rect on OSX is needing
58 static const int TEXTFOCUSBORDER
= 3 ;
60 static const wxCoord MARGIN
= 2;
61 static const int TEXTFOCUSBORDER
= 0 ;
65 // ----------------------------------------------------------------------------
66 // wxComboBoxText: text control forwards events to combobox
67 // ----------------------------------------------------------------------------
69 class wxComboBoxText
: public wxTextCtrl
72 wxComboBoxText( wxComboBox
* cb
)
73 : wxTextCtrl( cb
, 1 )
76 SetTriggerOnSetValue( false );
80 void OnChar( wxKeyEvent
& event
)
82 // Allows processing the tab key to go to the next control
83 if (event
.GetKeyCode() == WXK_TAB
)
85 wxNavigationKeyEvent NavEvent
;
86 NavEvent
.SetEventObject(this);
87 NavEvent
.SetDirection(true);
88 NavEvent
.SetWindowChange(false);
90 // Get the parent of the combo and have it process the navigation?
91 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
95 // send the event to the combobox class in case the user has bound EVT_CHAR
96 wxKeyEvent
kevt(event
);
97 kevt
.SetEventObject(m_cb
);
98 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
99 // If the event was handled and not skipped then we're done
102 if ( event
.GetKeyCode() == WXK_RETURN
)
104 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
105 event
.SetString( GetValue() );
106 event
.SetInt( m_cb
->GetSelection() );
107 event
.SetEventObject( m_cb
);
109 // This will invoke the dialog default action,
110 // such as the clicking the default button.
111 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
113 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
114 if ( tlw
&& tlw
->GetDefaultItem() )
116 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
117 if ( def
&& def
->IsEnabled() )
119 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
120 event
.SetEventObject(def
);
132 void OnKeyUp( wxKeyEvent
& event
)
134 event
.SetEventObject(m_cb
);
135 event
.SetId(m_cb
->GetId());
136 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
140 void OnKeyDown( wxKeyEvent
& event
)
142 event
.SetEventObject(m_cb
);
143 event
.SetId(m_cb
->GetId());
144 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
148 void OnText( wxCommandEvent
& event
)
150 event
.SetEventObject(m_cb
);
151 event
.SetId(m_cb
->GetId());
152 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
159 DECLARE_EVENT_TABLE()
162 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
163 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
164 EVT_CHAR(wxComboBoxText::OnChar
)
165 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
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
> 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 WX_INIT_CONTROL_CONTAINER();
338 bool wxComboBox::Create(wxWindow
*parent
,
340 const wxString
& value
,
343 const wxArrayString
& choices
,
345 const wxValidator
& validator
,
346 const wxString
& name
)
348 if ( !Create( parent
, id
, value
, pos
, size
, 0, NULL
,
349 style
, validator
, name
) )
357 bool wxComboBox::Create(wxWindow
*parent
,
359 const wxString
& value
,
363 const wxString choices
[],
365 const wxValidator
& validator
,
366 const wxString
& name
)
368 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
374 m_choice
= new wxComboBoxChoice(this, style
);
376 if ( style
& wxCB_READONLY
)
382 m_text
= new wxComboBoxText(this);
385 csize
.y
= m_text
->GetSize().y
;
386 csize
.y
+= 2 * TEXTFOCUSBORDER
;
390 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
392 Append( n
, choices
);
394 // Needed because it is a wxControlWithItems
395 SetInitialSize(size
);
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 unsigned 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 void wxComboBox::WriteText(const wxString
& text
)
428 m_text
->WriteText(text
);
431 void wxComboBox::GetSelection(long *from
, long *to
) const
433 m_text
->GetSelection(from
, to
);
436 // Clipboard operations
438 void wxComboBox::Copy()
440 if ( m_text
!= NULL
)
444 void wxComboBox::Cut()
446 if ( m_text
!= NULL
)
450 void wxComboBox::Paste()
452 if ( m_text
!= NULL
)
456 void wxComboBox::SetEditable(bool editable
)
458 if ( ( m_text
== NULL
) && editable
)
460 m_text
= new wxComboBoxText( this );
462 else if ( ( m_text
!= NULL
) && !editable
)
468 int currentX
, currentY
;
469 GetPosition( ¤tX
, ¤tY
);
471 int currentW
, currentH
;
472 GetSize( ¤tW
, ¤tH
);
474 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
477 void wxComboBox::SetInsertionPoint(long pos
)
480 m_text
->SetInsertionPoint(pos
);
483 void wxComboBox::SetInsertionPointEnd()
486 m_text
->SetInsertionPointEnd();
489 long wxComboBox::GetInsertionPoint() const
492 return m_text
->GetInsertionPoint();
496 wxTextPos
wxComboBox::GetLastPosition() const
499 return m_text
->GetLastPosition();
503 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
506 m_text
->Replace(from
,to
,value
);
509 void wxComboBox::Remove(long from
, long to
)
512 m_text
->Remove(from
,to
);
515 void wxComboBox::SetSelection(long from
, long to
)
518 m_text
->SetSelection(from
,to
);
521 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
524 wxClientDataType type
)
526 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
529 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
531 return m_choice
->DoSetItemClientData( n
, clientData
) ;
534 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
536 return m_choice
->DoGetItemClientData( n
) ;
539 wxClientDataType
wxComboBox::GetClientDataType() const
541 return m_choice
->GetClientDataType();
544 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
546 m_choice
->SetClientDataType(clientDataItemsType
);
549 void wxComboBox::DoDeleteOneItem(unsigned int n
)
551 m_choice
->DoDeleteOneItem( n
);
554 void wxComboBox::DoClear()
559 int wxComboBox::GetSelection() const
561 return m_choice
->GetSelection();
564 void wxComboBox::SetSelection(int n
)
566 m_choice
->SetSelection( n
);
568 if ( m_text
!= NULL
)
569 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
572 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
574 return m_choice
->FindString( s
, bCase
);
577 wxString
wxComboBox::GetString(unsigned int n
) const
579 return m_choice
->GetString( n
);
582 wxString
wxComboBox::GetStringSelection() const
584 int sel
= GetSelection();
585 if (sel
!= wxNOT_FOUND
)
586 return wxString(this->GetString((unsigned int)sel
));
588 return wxEmptyString
;
591 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
593 m_choice
->SetString( n
, s
);
596 bool wxComboBox::IsEditable() const
598 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
601 void wxComboBox::Undo()
607 void wxComboBox::Redo()
613 void wxComboBox::SelectAll()
619 bool wxComboBox::CanCopy() const
622 return m_text
->CanCopy();
627 bool wxComboBox::CanCut() const
630 return m_text
->CanCut();
635 bool wxComboBox::CanPaste() const
638 return m_text
->CanPaste();
643 bool wxComboBox::CanUndo() const
646 return m_text
->CanUndo();
651 bool wxComboBox::CanRedo() const
654 return m_text
->CanRedo();
659 wxInt32
wxComboBox::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
662 For consistency with other platforms, clicking in the text area does not constitute a selection
663 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
664 event.SetInt(GetSelection());
665 event.SetEventObject(this);
666 event.SetString(GetStringSelection());
667 ProcessCommand(event);
673 #endif // wxUSE_COMBOBOX