1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "combobox.h"
16 #include "wx/wxprec.h"
20 #include "wx/combobox.h"
21 #include "wx/button.h"
23 #include "wx/mac/uma.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
27 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
30 static int nextPopUpMenuId
= 1000 ;
31 MenuHandle
NewUniqueMenu()
33 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // the margin between the text control and the choice
44 #if TARGET_API_MAC_OSX
45 // margin should be bigger on OS X due to blue highlight
46 // around text control.
47 static const wxCoord MARGIN
= 4;
48 // this is the border a focus rect on OSX is needing
49 static const int TEXTFOCUSBORDER
= 3 ;
51 static const wxCoord MARGIN
= 2;
52 static const int TEXTFOCUSBORDER
= 0 ;
54 static const int POPUPHEIGHT
= 23;
57 // ----------------------------------------------------------------------------
58 // wxComboBoxText: text control forwards events to combobox
59 // ----------------------------------------------------------------------------
61 class wxComboBoxText
: public wxTextCtrl
64 wxComboBoxText( wxComboBox
* cb
)
65 : wxTextCtrl( cb
, 1 )
71 void OnChar( wxKeyEvent
& event
)
73 // Allows processing the tab key to go to the next control
74 if (event
.GetKeyCode() == WXK_TAB
)
76 wxNavigationKeyEvent NavEvent
;
77 NavEvent
.SetEventObject(this);
78 NavEvent
.SetDirection(true);
79 NavEvent
.SetWindowChange(false);
81 // Get the parent of the combo and have it process the navigation?
82 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
86 // send the event to the combobox class in case the user has bound EVT_CHAR
87 wxKeyEvent
kevt(event
);
88 kevt
.SetEventObject(m_cb
);
89 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
90 // If the event was handled and not skipped then we're done
93 if ( event
.GetKeyCode() == WXK_RETURN
)
95 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
96 event
.SetString( GetValue() );
97 event
.SetInt( m_cb
->GetSelection() );
98 event
.SetEventObject( m_cb
);
100 // This will invoke the dialog default action, such
101 // as the clicking the default button.
103 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
105 wxWindow
*parent
= GetParent();
106 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
107 parent
= parent
->GetParent() ;
109 if ( parent
&& parent
->GetDefaultItem() )
111 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
113 if ( def
&& def
->IsEnabled() )
115 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
116 event
.SetEventObject(def
);
128 void OnKeyUp( wxKeyEvent
& event
)
130 event
.SetEventObject(m_cb
);
131 event
.SetId(m_cb
->GetId());
132 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
136 void OnKeyDown( wxKeyEvent
& event
)
138 event
.SetEventObject(m_cb
);
139 event
.SetId(m_cb
->GetId());
140 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
144 void OnText( wxCommandEvent
& event
)
146 event
.SetEventObject(m_cb
);
147 event
.SetId(m_cb
->GetId());
148 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
155 DECLARE_EVENT_TABLE()
158 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
159 EVT_KEY_DOWN( wxComboBoxText::OnKeyDown
)
160 EVT_CHAR( wxComboBoxText::OnChar
)
161 EVT_KEY_UP( wxComboBoxText::OnKeyUp
)
162 EVT_TEXT( -1, wxComboBoxText::OnText
)
165 class wxComboBoxChoice
: public wxChoice
168 wxComboBoxChoice(wxComboBox
*cb
, int style
)
169 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
173 int GetPopupWidth() const
175 switch ( GetWindowVariant() )
177 case wxWINDOW_VARIANT_NORMAL
:
178 case wxWINDOW_VARIANT_LARGE
:
186 void OnChoice( wxCommandEvent
& e
)
188 wxString s
= e
.GetString();
190 m_cb
->DelegateChoice( s
);
191 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
192 event2
.SetInt(m_cb
->GetSelection());
193 event2
.SetEventObject(m_cb
);
194 event2
.SetString(m_cb
->GetStringSelection());
195 m_cb
->ProcessCommand(event2
);
197 // For consistency with MSW and GTK, also send a text updated event
198 // After all, the text is updated when a selection is made
199 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
200 TextEvent
.SetString( m_cb
->GetStringSelection() );
201 TextEvent
.SetEventObject( m_cb
);
202 m_cb
->ProcessCommand( TextEvent
);
204 virtual wxSize
DoGetBestSize() const
206 wxSize sz
= wxChoice::DoGetBestSize() ;
207 if (! m_cb
->HasFlag(wxCB_READONLY
) )
208 sz
.x
= GetPopupWidth() ;
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
230 if (m_text
!= NULL
) {
234 if (m_choice
!= NULL
) {
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 wxSize
wxComboBox::DoGetBestSize() const
247 if (!m_choice
&& !m_text
)
249 wxSize size
= m_choice
->GetBestSize();
251 if ( m_text
!= NULL
)
253 wxSize sizeText
= m_text
->GetBestSize();
254 if (sizeText
.y
> size
.y
)
256 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
257 size
.x
+= TEXTFOCUSBORDER
;
258 size
.y
+= 2 * TEXTFOCUSBORDER
;
262 // clipping is too tight
268 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
270 wxControl::DoMoveWindow(x
, y
, width
, height
);
272 if ( m_text
== NULL
)
274 // we might not be fully constructed yet, therefore watch out...
276 m_choice
->SetSize(0, 0 , width
, -1);
280 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
281 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
282 // put it at an inset of 1 to have outer area shadows drawn as well
283 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
289 // ----------------------------------------------------------------------------
290 // operations forwarded to the subcontrols
291 // ----------------------------------------------------------------------------
293 bool wxComboBox::Enable(bool enable
)
295 if ( !wxControl::Enable(enable
) )
299 m_text
->Enable(enable
);
304 bool wxComboBox::Show(bool show
)
306 if ( !wxControl::Show(show
) )
312 void wxComboBox::SetFocus()
314 if ( m_text
!= NULL
) {
320 void wxComboBox::DelegateTextChanged( const wxString
& value
)
322 SetStringSelection( value
);
326 void wxComboBox::DelegateChoice( const wxString
& value
)
328 SetStringSelection( value
);
332 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
333 const wxString
& value
,
336 const wxArrayString
& choices
,
338 const wxValidator
& validator
,
339 const wxString
& name
)
341 wxCArrayString
chs( choices
);
343 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
344 chs
.GetStrings(), style
, validator
, name
);
348 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
349 const wxString
& value
,
352 int n
, const wxString choices
[],
354 const wxValidator
& validator
,
355 const wxString
& name
)
357 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
363 m_choice
= new wxComboBoxChoice(this, style
);
365 if ( style
& wxCB_READONLY
)
371 m_text
= new wxComboBoxText(this);
374 csize
.y
= m_text
->GetSize().y
;
375 csize
.y
+= 2 * TEXTFOCUSBORDER
;
379 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
381 for ( int i
= 0 ; i
< n
; i
++ )
383 m_choice
->DoAppend( choices
[ i
] );
386 SetBestSize(size
); // Needed because it is a wxControlWithItems
387 SetStringSelection(value
);
392 wxString
wxComboBox::GetValue() const
396 if ( m_text
== NULL
)
398 result
= m_choice
->GetString( m_choice
->GetSelection() );
402 result
= m_text
->GetValue();
408 int wxComboBox::GetCount() const
410 return m_choice
->GetCount() ;
413 void wxComboBox::SetValue(const wxString
& value
)
415 if ( HasFlag(wxCB_READONLY
) )
416 SetStringSelection( value
) ;
418 m_text
->SetValue( value
);
421 // Clipboard operations
422 void wxComboBox::Copy()
424 if ( m_text
!= NULL
)
430 void wxComboBox::Cut()
432 if ( m_text
!= NULL
)
438 void wxComboBox::Paste()
440 if ( m_text
!= NULL
)
446 void wxComboBox::SetEditable(bool editable
)
448 if ( ( m_text
== NULL
) && editable
)
450 m_text
= new wxComboBoxText( this );
452 else if ( ( m_text
!= NULL
) && !editable
)
458 int currentX
, currentY
;
459 GetPosition( ¤tX
, ¤tY
);
461 int currentW
, currentH
;
462 GetSize( ¤tW
, ¤tH
);
464 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
467 void wxComboBox::SetInsertionPoint(long pos
)
472 void wxComboBox::SetInsertionPointEnd()
477 long wxComboBox::GetInsertionPoint() const
483 wxTextPos
wxComboBox::GetLastPosition() const
489 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
494 void wxComboBox::Remove(long from
, long to
)
499 void wxComboBox::SetSelection(long from
, long to
)
504 int wxComboBox::DoAppend(const wxString
& item
)
506 return m_choice
->DoAppend( item
) ;
509 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
511 return m_choice
->DoInsert( item
, pos
) ;
514 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
516 return m_choice
->DoSetItemClientData( n
, clientData
) ;
519 void* wxComboBox::DoGetItemClientData(int n
) const
521 return m_choice
->DoGetItemClientData( n
) ;
524 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
526 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
529 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
531 return m_choice
->DoGetItemClientObject( n
) ;
534 void wxComboBox::FreeData()
536 if ( HasClientObjectData() )
538 size_t count
= GetCount();
539 for ( size_t n
= 0; n
< count
; n
++ )
541 SetClientObject( n
, NULL
);
546 void wxComboBox::Delete(int n
)
548 // force client object deletion
549 if( HasClientObjectData() )
550 SetClientObject( n
, NULL
);
551 m_choice
->Delete( n
);
554 void wxComboBox::Clear()
560 int wxComboBox::GetSelection() const
562 return m_choice
->GetSelection();
565 void wxComboBox::SetSelection(int n
)
567 m_choice
->SetSelection( n
);
569 if ( m_text
!= NULL
)
571 m_text
->SetValue( GetString( n
) );
575 int wxComboBox::FindString(const wxString
& s
) const
577 return m_choice
->FindString( s
);
580 wxString
wxComboBox::GetString(int n
) const
582 return m_choice
->GetString( n
);
585 wxString
wxComboBox::GetStringSelection() const
587 int sel
= GetSelection ();
589 return wxString(this->GetString (sel
));
591 return wxEmptyString
;
594 void wxComboBox::SetString(int n
, const wxString
& s
)
596 m_choice
->SetString( n
, s
) ;
599 bool wxComboBox::IsEditable() const
601 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
604 void wxComboBox::Undo()
610 void wxComboBox::Redo()
616 void wxComboBox::SelectAll()
622 bool wxComboBox::CanCopy() const
625 return m_text
->CanCopy();
630 bool wxComboBox::CanCut() const
633 return m_text
->CanCut();
638 bool wxComboBox::CanPaste() const
641 return m_text
->CanPaste();
646 bool wxComboBox::CanUndo() const
649 return m_text
->CanUndo();
654 bool wxComboBox::CanRedo() const
657 return m_text
->CanRedo();
662 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);*/