1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
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
)
23 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26 static int nextPopUpMenuId
= 1000 ;
27 MenuHandle
NewUniqueMenu()
29 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, such
97 // as the clicking the default button.
99 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
101 wxWindow
*parent
= GetParent();
102 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
103 parent
= parent
->GetParent() ;
105 if ( parent
&& parent
->GetDefaultItem() )
107 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
109 if ( def
&& def
->IsEnabled() )
111 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
112 event
.SetEventObject(def
);
124 void OnKeyUp( wxKeyEvent
& event
)
126 event
.SetEventObject(m_cb
);
127 event
.SetId(m_cb
->GetId());
128 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
132 void OnKeyDown( wxKeyEvent
& event
)
134 event
.SetEventObject(m_cb
);
135 event
.SetId(m_cb
->GetId());
136 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
140 void OnText( wxCommandEvent
& event
)
142 event
.SetEventObject(m_cb
);
143 event
.SetId(m_cb
->GetId());
144 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
151 DECLARE_EVENT_TABLE()
154 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
155 EVT_KEY_DOWN( wxComboBoxText::OnKeyDown
)
156 EVT_CHAR( wxComboBoxText::OnChar
)
157 EVT_KEY_UP( wxComboBoxText::OnKeyUp
)
158 EVT_TEXT( -1, wxComboBoxText::OnText
)
161 class wxComboBoxChoice
: public wxChoice
164 wxComboBoxChoice(wxComboBox
*cb
, int style
)
165 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
169 int GetPopupWidth() const
171 switch ( GetWindowVariant() )
173 case wxWINDOW_VARIANT_NORMAL
:
174 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
);
200 virtual wxSize
DoGetBestSize() const
202 wxSize sz
= wxChoice::DoGetBestSize() ;
203 if (! m_cb
->HasFlag(wxCB_READONLY
) )
204 sz
.x
= GetPopupWidth() ;
211 DECLARE_EVENT_TABLE()
214 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
215 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
218 wxComboBox::~wxComboBox()
220 // delete client objects
223 // delete the controls now, don't leave them alive even though they would
224 // still be eventually deleted by our parent - but it will be too late, the
225 // user code expects them to be gone now
226 if (m_text
!= NULL
) {
230 if (m_choice
!= NULL
) {
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 wxSize
wxComboBox::DoGetBestSize() const
243 if (!m_choice
&& !m_text
)
245 wxSize size
= m_choice
->GetBestSize();
247 if ( m_text
!= NULL
)
249 wxSize sizeText
= m_text
->GetBestSize();
250 if (sizeText
.y
> size
.y
)
252 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
253 size
.x
+= TEXTFOCUSBORDER
;
254 size
.y
+= 2 * TEXTFOCUSBORDER
;
258 // clipping is too tight
264 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
266 wxControl::DoMoveWindow(x
, y
, width
, height
);
268 if ( m_text
== NULL
)
270 // we might not be fully constructed yet, therefore watch out...
272 m_choice
->SetSize(0, 0 , width
, -1);
276 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
277 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
278 // put it at an inset of 1 to have outer area shadows drawn as well
279 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
285 // ----------------------------------------------------------------------------
286 // operations forwarded to the subcontrols
287 // ----------------------------------------------------------------------------
289 bool wxComboBox::Enable(bool enable
)
291 if ( !wxControl::Enable(enable
) )
295 m_text
->Enable(enable
);
300 bool wxComboBox::Show(bool show
)
302 if ( !wxControl::Show(show
) )
308 void wxComboBox::SetFocus()
310 if ( m_text
!= NULL
) {
316 void wxComboBox::DelegateTextChanged( const wxString
& value
)
318 SetStringSelection( value
);
322 void wxComboBox::DelegateChoice( const wxString
& value
)
324 SetStringSelection( value
);
328 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
329 const wxString
& value
,
332 const wxArrayString
& choices
,
334 const wxValidator
& validator
,
335 const wxString
& name
)
337 wxCArrayString
chs( choices
);
339 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
340 chs
.GetStrings(), style
, validator
, name
);
344 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
345 const wxString
& value
,
348 int n
, const wxString choices
[],
350 const wxValidator
& validator
,
351 const wxString
& name
)
353 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
359 m_choice
= new wxComboBoxChoice(this, style
);
361 if ( style
& wxCB_READONLY
)
367 m_text
= new wxComboBoxText(this);
370 csize
.y
= m_text
->GetSize().y
;
371 csize
.y
+= 2 * TEXTFOCUSBORDER
;
375 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
377 for ( int i
= 0 ; i
< n
; i
++ )
379 m_choice
->DoAppend( choices
[ i
] );
382 SetBestSize(size
); // Needed because it is a wxControlWithItems
383 SetStringSelection(value
);
388 wxString
wxComboBox::GetValue() const
392 if ( m_text
== NULL
)
394 result
= m_choice
->GetString( m_choice
->GetSelection() );
398 result
= m_text
->GetValue();
404 int wxComboBox::GetCount() const
406 return m_choice
->GetCount() ;
409 void wxComboBox::SetValue(const wxString
& value
)
411 if ( HasFlag(wxCB_READONLY
) )
412 SetStringSelection( value
) ;
414 m_text
->SetValue( value
);
417 // Clipboard operations
418 void wxComboBox::Copy()
420 if ( m_text
!= NULL
)
426 void wxComboBox::Cut()
428 if ( m_text
!= NULL
)
434 void wxComboBox::Paste()
436 if ( m_text
!= NULL
)
442 void wxComboBox::SetEditable(bool editable
)
444 if ( ( m_text
== NULL
) && editable
)
446 m_text
= new wxComboBoxText( this );
448 else if ( ( m_text
!= NULL
) && !editable
)
454 int currentX
, currentY
;
455 GetPosition( ¤tX
, ¤tY
);
457 int currentW
, currentH
;
458 GetSize( ¤tW
, ¤tH
);
460 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
463 void wxComboBox::SetInsertionPoint(long pos
)
468 void wxComboBox::SetInsertionPointEnd()
473 long wxComboBox::GetInsertionPoint() const
479 wxTextPos
wxComboBox::GetLastPosition() const
485 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
490 void wxComboBox::Remove(long from
, long to
)
495 void wxComboBox::SetSelection(long from
, long to
)
500 int wxComboBox::DoAppend(const wxString
& item
)
502 return m_choice
->DoAppend( item
) ;
505 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
507 return m_choice
->DoInsert( item
, pos
) ;
510 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
512 return m_choice
->DoSetItemClientData( n
, clientData
) ;
515 void* wxComboBox::DoGetItemClientData(int n
) const
517 return m_choice
->DoGetItemClientData( n
) ;
520 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
522 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
525 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
527 return m_choice
->DoGetItemClientObject( n
) ;
530 void wxComboBox::FreeData()
532 if ( HasClientObjectData() )
534 size_t count
= GetCount();
535 for ( size_t n
= 0; n
< count
; n
++ )
537 SetClientObject( n
, NULL
);
542 void wxComboBox::Delete(int n
)
544 // force client object deletion
545 if( HasClientObjectData() )
546 SetClientObject( n
, NULL
);
547 m_choice
->Delete( n
);
550 void wxComboBox::Clear()
556 int wxComboBox::GetSelection() const
558 return m_choice
->GetSelection();
561 void wxComboBox::SetSelection(int n
)
563 m_choice
->SetSelection( n
);
565 if ( m_text
!= NULL
)
567 m_text
->SetValue( GetString( n
) );
571 int wxComboBox::FindString(const wxString
& s
) const
573 return m_choice
->FindString( s
);
576 wxString
wxComboBox::GetString(int n
) const
578 return m_choice
->GetString( n
);
581 wxString
wxComboBox::GetStringSelection() const
583 int sel
= GetSelection ();
585 return wxString(this->GetString (sel
));
587 return wxEmptyString
;
590 void wxComboBox::SetString(int n
, const wxString
& s
)
592 m_choice
->SetString( n
, s
) ;
595 bool wxComboBox::IsEditable() const
597 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
600 void wxComboBox::Undo()
606 void wxComboBox::Redo()
612 void wxComboBox::SelectAll()
618 bool wxComboBox::CanCopy() const
621 return m_text
->CanCopy();
626 bool wxComboBox::CanCut() const
629 return m_text
->CanCut();
634 bool wxComboBox::CanPaste() const
637 return m_text
->CanPaste();
642 bool wxComboBox::CanUndo() const
645 return m_text
->CanUndo();
650 bool wxComboBox::CanRedo() const
653 return m_text
->CanRedo();
658 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
660 /* For consistency with other platforms, clicking in the text area does not constitute a selection
661 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
662 event.SetInt(GetSelection());
663 event.SetEventObject(this);
664 event.SetString(GetStringSelection());
665 ProcessCommand(event);*/