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
) )
301 bool wxComboBox::Show(bool show
)
303 if ( !wxControl::Show(show
) )
309 void wxComboBox::SetFocus()
311 if ( m_text
!= NULL
) {
317 void wxComboBox::DelegateTextChanged( const wxString
& value
)
319 SetStringSelection( value
);
323 void wxComboBox::DelegateChoice( const wxString
& value
)
325 SetStringSelection( value
);
329 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
330 const wxString
& value
,
333 const wxArrayString
& choices
,
335 const wxValidator
& validator
,
336 const wxString
& name
)
338 wxCArrayString
chs( choices
);
340 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
341 chs
.GetStrings(), style
, validator
, name
);
345 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
346 const wxString
& value
,
349 int n
, const wxString choices
[],
351 const wxValidator
& validator
,
352 const wxString
& name
)
354 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
360 m_choice
= new wxComboBoxChoice(this, style
);
362 if ( style
& wxCB_READONLY
)
368 m_text
= new wxComboBoxText(this);
371 csize
.y
= m_text
->GetSize().y
;
372 csize
.y
+= 2 * TEXTFOCUSBORDER
;
376 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
378 for ( int i
= 0 ; i
< n
; i
++ )
380 m_choice
->DoAppend( choices
[ i
] );
383 SetBestSize(size
); // Needed because it is a wxControlWithItems
384 SetStringSelection(value
);
389 wxString
wxComboBox::GetValue() const
393 if ( m_text
== NULL
)
395 result
= m_choice
->GetString( m_choice
->GetSelection() );
399 result
= m_text
->GetValue();
405 int wxComboBox::GetCount() const
407 return m_choice
->GetCount() ;
410 void wxComboBox::SetValue(const wxString
& value
)
412 if ( HasFlag(wxCB_READONLY
) )
413 SetStringSelection( value
) ;
415 m_text
->SetValue( value
);
418 // Clipboard operations
419 void wxComboBox::Copy()
421 if ( m_text
!= NULL
)
427 void wxComboBox::Cut()
429 if ( m_text
!= NULL
)
435 void wxComboBox::Paste()
437 if ( m_text
!= NULL
)
443 void wxComboBox::SetEditable(bool editable
)
445 if ( ( m_text
== NULL
) && editable
)
447 m_text
= new wxComboBoxText( this );
449 else if ( ( m_text
!= NULL
) && !editable
)
455 int currentX
, currentY
;
456 GetPosition( ¤tX
, ¤tY
);
458 int currentW
, currentH
;
459 GetSize( ¤tW
, ¤tH
);
461 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
464 void wxComboBox::SetInsertionPoint(long pos
)
469 void wxComboBox::SetInsertionPointEnd()
474 long wxComboBox::GetInsertionPoint() const
480 wxTextPos
wxComboBox::GetLastPosition() const
486 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
491 void wxComboBox::Remove(long from
, long to
)
496 void wxComboBox::SetSelection(long from
, long to
)
501 int wxComboBox::DoAppend(const wxString
& item
)
503 return m_choice
->DoAppend( item
) ;
506 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
508 return m_choice
->DoInsert( item
, pos
) ;
511 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
513 return m_choice
->DoSetItemClientData( n
, clientData
) ;
516 void* wxComboBox::DoGetItemClientData(int n
) const
518 return m_choice
->DoGetItemClientData( n
) ;
521 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
523 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
526 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
528 return m_choice
->DoGetItemClientObject( n
) ;
531 void wxComboBox::FreeData()
533 if ( HasClientObjectData() )
535 size_t count
= GetCount();
536 for ( size_t n
= 0; n
< count
; n
++ )
538 SetClientObject( n
, NULL
);
543 void wxComboBox::Delete(int n
)
545 // force client object deletion
546 if( HasClientObjectData() )
547 SetClientObject( n
, NULL
);
548 m_choice
->Delete( n
);
551 void wxComboBox::Clear()
557 int wxComboBox::GetSelection() const
559 return m_choice
->GetSelection();
562 void wxComboBox::SetSelection(int n
)
564 m_choice
->SetSelection( n
);
566 if ( m_text
!= NULL
)
568 m_text
->SetValue( GetString( n
) );
572 int wxComboBox::FindString(const wxString
& s
) const
574 return m_choice
->FindString( s
);
577 wxString
wxComboBox::GetString(int n
) const
579 return m_choice
->GetString( n
);
582 wxString
wxComboBox::GetStringSelection() const
584 int sel
= GetSelection ();
586 return wxString(this->GetString (sel
));
588 return wxEmptyString
;
591 void wxComboBox::SetString(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
) )
661 /* For consistency with other platforms, clicking in the text area does not constitute a selection
662 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
663 event.SetInt(GetSelection());
664 event.SetEventObject(this);
665 event.SetString(GetStringSelection());
666 ProcessCommand(event);*/