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"
25 #include "wx/mac/uma.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
29 WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox
, wxControl
)
31 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
32 WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox
)
36 static int nextPopUpMenuId
= 1000 ;
38 MenuHandle
NewUniqueMenu()
40 MenuHandle handle
= UMANewMenu(nextPopUpMenuId
, wxString(wxT("Menu")), wxFont::GetDefaultEncoding() );
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // the margin between the text control and the choice
52 #if TARGET_API_MAC_OSX
53 // margin should be bigger on OS X due to blue highlight
54 // around text control.
55 static const wxCoord MARGIN
= 4;
56 // this is the border a focus rect on OSX is needing
57 static const int TEXTFOCUSBORDER
= 3 ;
59 static const wxCoord MARGIN
= 2;
60 static const int TEXTFOCUSBORDER
= 0 ;
64 // ----------------------------------------------------------------------------
65 // wxComboBoxText: text control forwards events to combobox
66 // ----------------------------------------------------------------------------
68 class wxComboBoxText
: public wxTextCtrl
71 wxComboBoxText( wxComboBox
* cb
)
72 : wxTextCtrl( cb
, 1 )
75 SetTriggerOnSetValue( false );
79 void OnChar( wxKeyEvent
& event
)
81 // Allows processing the tab key to go to the next control
82 if (event
.GetKeyCode() == WXK_TAB
)
84 wxNavigationKeyEvent NavEvent
;
85 NavEvent
.SetEventObject(this);
86 NavEvent
.SetDirection(true);
87 NavEvent
.SetWindowChange(false);
89 // Get the parent of the combo and have it process the navigation?
90 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
94 // send the event to the combobox class in case the user has bound EVT_CHAR
95 wxKeyEvent
kevt(event
);
96 kevt
.SetEventObject(m_cb
);
97 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
98 // If the event was handled and not skipped then we're done
101 if ( event
.GetKeyCode() == WXK_RETURN
)
103 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
104 event
.SetString( GetValue() );
105 event
.SetInt( m_cb
->GetSelection() );
106 event
.SetEventObject( m_cb
);
108 // This will invoke the dialog default action,
109 // such as the clicking the default button.
110 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
112 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
113 if ( tlw
&& tlw
->GetDefaultItem() )
115 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
116 if ( def
&& def
->IsEnabled() )
118 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
119 event
.SetEventObject(def
);
131 void OnKeyUp( wxKeyEvent
& event
)
133 event
.SetEventObject(m_cb
);
134 event
.SetId(m_cb
->GetId());
135 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
139 void OnKeyDown( wxKeyEvent
& event
)
141 event
.SetEventObject(m_cb
);
142 event
.SetId(m_cb
->GetId());
143 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
147 void OnText( wxCommandEvent
& event
)
149 event
.SetEventObject(m_cb
);
150 event
.SetId(m_cb
->GetId());
151 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
158 DECLARE_EVENT_TABLE()
161 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
162 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown
)
163 EVT_CHAR(wxComboBoxText::OnChar
)
164 EVT_KEY_UP(wxComboBoxText::OnKeyUp
)
165 EVT_TEXT(wxID_ANY
, wxComboBoxText::OnText
)
168 class wxComboBoxChoice
: public wxChoice
171 wxComboBoxChoice( wxComboBox
*cb
, int style
)
172 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
177 int GetPopupWidth() const
179 switch ( GetWindowVariant() )
181 case wxWINDOW_VARIANT_NORMAL
:
182 case wxWINDOW_VARIANT_LARGE
:
191 void OnChoice( wxCommandEvent
& e
)
193 wxString s
= e
.GetString();
195 m_cb
->DelegateChoice( s
);
196 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
197 event2
.SetInt(m_cb
->GetSelection());
198 event2
.SetEventObject(m_cb
);
199 event2
.SetString(m_cb
->GetStringSelection());
200 m_cb
->ProcessCommand(event2
);
202 // For consistency with MSW and GTK, also send a text updated event
203 // After all, the text is updated when a selection is made
204 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
205 TextEvent
.SetString( m_cb
->GetStringSelection() );
206 TextEvent
.SetEventObject( m_cb
);
207 m_cb
->ProcessCommand( TextEvent
);
210 virtual wxSize
DoGetBestSize() const
212 wxSize sz
= wxChoice::DoGetBestSize() ;
213 if (! m_cb
->HasFlag(wxCB_READONLY
) )
214 sz
.x
= GetPopupWidth() ;
222 friend class wxComboBox
;
224 DECLARE_EVENT_TABLE()
227 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
228 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
231 wxComboBox::~wxComboBox()
233 // delete the controls now, don't leave them alive even though they would
234 // still be eventually deleted by our parent - but it will be too late, the
235 // user code expects them to be gone now
242 if (m_choice
!= NULL
)
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 wxSize
wxComboBox::DoGetBestSize() const
255 if (!m_choice
&& !m_text
)
258 wxSize size
= m_choice
->GetBestSize();
260 if ( m_text
!= NULL
)
262 wxSize sizeText
= m_text
->GetBestSize();
263 if (sizeText
.y
> size
.y
)
266 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
267 size
.x
+= TEXTFOCUSBORDER
;
268 size
.y
+= 2 * TEXTFOCUSBORDER
;
272 // clipping is too tight
279 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
281 wxControl::DoMoveWindow( x
, y
, width
, height
);
283 if ( m_text
== NULL
)
285 // we might not be fully constructed yet, therefore watch out...
287 m_choice
->SetSize(0, 0 , width
, -1);
291 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
292 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1);
294 // put it at an inset of 1 to have outer area shadows drawn as well
295 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
299 // ----------------------------------------------------------------------------
300 // operations forwarded to the subcontrols
301 // ----------------------------------------------------------------------------
303 bool wxComboBox::Enable(bool enable
)
305 if ( !wxControl::Enable(enable
) )
309 m_text
->Enable(enable
);
314 bool wxComboBox::Show(bool show
)
316 if ( !wxControl::Show(show
) )
322 void wxComboBox::DelegateTextChanged( const wxString
& value
)
324 SetStringSelection( value
);
327 void wxComboBox::DelegateChoice( const wxString
& value
)
329 SetStringSelection( value
);
332 void wxComboBox::Init()
334 WX_INIT_CONTROL_CONTAINER();
337 bool wxComboBox::Create(wxWindow
*parent
,
339 const wxString
& value
,
342 const wxArrayString
& choices
,
344 const wxValidator
& validator
,
345 const wxString
& name
)
347 return Create( parent
, id
, value
, pos
, size
, 0, NULL
,
348 style
, validator
, name
);
351 bool wxComboBox::Create(wxWindow
*parent
,
353 const wxString
& value
,
357 const wxString choices
[],
359 const wxValidator
& validator
,
360 const wxString
& name
)
362 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
368 m_choice
= new wxComboBoxChoice(this, style
);
370 if ( style
& wxCB_READONLY
)
376 m_text
= new wxComboBoxText(this);
379 csize
.y
= m_text
->GetSize().y
;
380 csize
.y
+= 2 * TEXTFOCUSBORDER
;
384 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
386 Append( n
, choices
);
388 // Needed because it is a wxControlWithItems
389 SetInitialSize(size
);
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 unsigned 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
)
464 m_text
->SetInsertionPoint(pos
);
467 void wxComboBox::SetInsertionPointEnd()
470 m_text
->SetInsertionPointEnd();
473 long wxComboBox::GetInsertionPoint() const
476 return m_text
->GetInsertionPoint();
480 wxTextPos
wxComboBox::GetLastPosition() const
483 return m_text
->GetLastPosition();
487 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
490 m_text
->Replace(from
,to
,value
);
493 void wxComboBox::Remove(long from
, long to
)
496 m_text
->Remove(from
,to
);
499 void wxComboBox::SetSelection(long from
, long to
)
502 m_text
->SetSelection(from
,to
);
505 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
508 wxClientDataType type
)
510 return m_choice
->DoInsertItems(items
, pos
, clientData
, type
);
513 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
515 return m_choice
->DoSetItemClientData( n
, clientData
) ;
518 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
520 return m_choice
->DoGetItemClientData( n
) ;
523 wxClientDataType
wxComboBox::GetClientDataType() const
525 return m_choice
->GetClientDataType();
528 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
530 m_choice
->SetClientDataType(clientDataItemsType
);
533 void wxComboBox::DoDeleteOneItem(unsigned int n
)
535 m_choice
->DoDeleteOneItem( n
);
538 void wxComboBox::DoClear()
543 int wxComboBox::GetSelection() const
545 return m_choice
->GetSelection();
548 void wxComboBox::SetSelection(int n
)
550 m_choice
->SetSelection( n
);
552 if ( m_text
!= NULL
)
553 m_text
->SetValue(n
!= wxNOT_FOUND
? GetString(n
) : wxString(wxEmptyString
));
556 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
558 return m_choice
->FindString( s
, bCase
);
561 wxString
wxComboBox::GetString(unsigned int n
) const
563 return m_choice
->GetString( n
);
566 wxString
wxComboBox::GetStringSelection() const
568 int sel
= GetSelection();
569 if (sel
!= wxNOT_FOUND
)
570 return wxString(this->GetString((unsigned int)sel
));
572 return wxEmptyString
;
575 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
577 m_choice
->SetString( n
, s
);
580 bool wxComboBox::IsEditable() const
582 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
585 void wxComboBox::Undo()
591 void wxComboBox::Redo()
597 void wxComboBox::SelectAll()
603 bool wxComboBox::CanCopy() const
606 return m_text
->CanCopy();
611 bool wxComboBox::CanCut() const
614 return m_text
->CanCut();
619 bool wxComboBox::CanPaste() const
622 return m_text
->CanPaste();
627 bool wxComboBox::CanUndo() const
630 return m_text
->CanUndo();
635 bool wxComboBox::CanRedo() const
638 return m_text
->CanRedo();
643 wxInt32
wxComboBox::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
646 For consistency with other platforms, clicking in the text area does not constitute a selection
647 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
648 event.SetInt(GetSelection());
649 event.SetEventObject(this);
650 event.SetString(GetStringSelection());
651 ProcessCommand(event);
657 #endif // wxUSE_COMBOBOX