1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 #include "wx/combobox.h"
17 #include "wx/button.h"
19 #include "wx/mac/uma.h"
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
25 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
28 static int nextPopUpMenuId
= 1000 ;
29 MenuHandle
NewUniqueMenu()
31 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the margin between the text control and the choice
42 #if TARGET_API_MAC_OSX
43 // margin should be bigger on OS X due to blue highlight
44 // around text control.
45 static const wxCoord MARGIN
= 4;
46 // this is the border a focus rect on OSX is needing
47 static const int TEXTFOCUSBORDER
= 3 ;
49 static const wxCoord MARGIN
= 2;
50 static const int TEXTFOCUSBORDER
= 0 ;
52 static const int POPUPHEIGHT
= 23;
55 // ----------------------------------------------------------------------------
56 // wxComboBoxText: text control forwards events to combobox
57 // ----------------------------------------------------------------------------
59 class wxComboBoxText
: public wxTextCtrl
62 wxComboBoxText( wxComboBox
* cb
)
63 : wxTextCtrl( cb
, 1 )
69 void OnChar( wxKeyEvent
& event
)
71 if ( event
.GetKeyCode() == WXK_RETURN
)
73 wxString value
= GetValue();
75 if ( m_cb
->GetCount() == 0 )
77 // make Enter generate "selected" event if there is only one item
78 // in the combobox - without it, it's impossible to select it at
80 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
82 event
.SetString( value
);
83 event
.SetEventObject( m_cb
);
84 m_cb
->GetEventHandler()->ProcessEvent( event
);
88 // add the item to the list if it's not there yet
89 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
92 m_cb
->SetStringSelection(value
);
94 // and generate the selected event for it
95 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
96 event
.SetInt( m_cb
->GetCount() - 1 );
97 event
.SetString( value
);
98 event
.SetEventObject( m_cb
);
99 m_cb
->GetEventHandler()->ProcessEvent( event
);
102 // This will invoke the dialog default action, such
103 // as the clicking the default button.
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
);
132 DECLARE_EVENT_TABLE()
135 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
136 EVT_CHAR( wxComboBoxText::OnChar
)
139 class wxComboBoxChoice
: public wxChoice
142 wxComboBoxChoice(wxComboBox
*cb
, int style
)
147 int GetPopupWidth() const
149 switch ( GetWindowVariant() )
151 case wxWINDOW_VARIANT_NORMAL
:
152 case wxWINDOW_VARIANT_LARGE
:
160 void OnChoice( wxCommandEvent
& e
)
162 wxString s
= e
.GetString();
164 m_cb
->DelegateChoice( s
);
165 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
166 event2
.SetInt(m_cb
->GetSelection());
167 event2
.SetEventObject(m_cb
);
168 event2
.SetString(m_cb
->GetStringSelection());
169 m_cb
->ProcessCommand(event2
);
171 virtual wxSize
DoGetBestSize() const
173 wxSize sz
= wxChoice::DoGetBestSize() ;
174 if (! m_cb
->HasFlag(wxCB_READONLY
) )
175 sz
.x
= GetPopupWidth() ;
182 DECLARE_EVENT_TABLE()
185 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
186 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
189 wxComboBox::~wxComboBox()
191 // delete client objects
194 // delete the controls now, don't leave them alive even though they would
195 // still be eventually deleted by our parent - but it will be too late, the
196 // user code expects them to be gone now
197 if (m_text
!= NULL
) {
201 if (m_choice
!= NULL
) {
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 wxSize
wxComboBox::DoGetBestSize() const
214 if (!m_choice
&& !m_text
)
216 wxSize size
= m_choice
->GetBestSize();
218 if ( m_text
!= NULL
)
220 wxSize sizeText
= m_text
->GetBestSize();
221 if (sizeText
.y
> size
.y
)
223 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
224 size
.x
+= TEXTFOCUSBORDER
;
225 size
.y
+= 2 * TEXTFOCUSBORDER
;
229 // clipping is too tight
235 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
237 wxControl::DoMoveWindow(x
, y
, width
, height
);
239 if ( m_text
== NULL
)
241 // we might not be fully constructed yet, therefore watch out...
243 m_choice
->SetSize(0, 0 , width
, -1);
247 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
248 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
249 // put it at an inset of 1 to have outer area shadows drawn as well
250 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
256 // ----------------------------------------------------------------------------
257 // operations forwarded to the subcontrols
258 // ----------------------------------------------------------------------------
260 bool wxComboBox::Enable(bool enable
)
262 if ( !wxControl::Enable(enable
) )
268 bool wxComboBox::Show(bool show
)
270 if ( !wxControl::Show(show
) )
276 void wxComboBox::SetFocus()
278 if ( m_text
!= NULL
) {
284 void wxComboBox::DelegateTextChanged( const wxString
& value
)
286 SetStringSelection( value
);
290 void wxComboBox::DelegateChoice( const wxString
& value
)
292 SetStringSelection( value
);
296 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
297 const wxString
& value
,
300 const wxArrayString
& choices
,
302 const wxValidator
& validator
,
303 const wxString
& name
)
305 wxCArrayString
chs( choices
);
307 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
308 chs
.GetStrings(), style
, validator
, name
);
312 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
313 const wxString
& value
,
316 int n
, const wxString choices
[],
318 const wxValidator
& validator
,
319 const wxString
& name
)
321 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
322 wxDefaultValidator
, name
) )
327 m_choice
= new wxComboBoxChoice(this, style
);
329 if ( style
& wxCB_READONLY
)
335 m_text
= new wxComboBoxText(this);
338 csize
.y
= m_text
->GetSize().y
;
339 csize
.y
+= 2 * TEXTFOCUSBORDER
;
343 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
345 for ( int i
= 0 ; i
< n
; i
++ )
347 m_choice
->DoAppend( choices
[ i
] );
350 SetBestSize(size
); // Needed because it is a wxControlWithItems
355 wxString
wxComboBox::GetValue() const
359 if ( m_text
== NULL
)
361 result
= m_choice
->GetString( m_choice
->GetSelection() );
365 result
= m_text
->GetValue();
371 int wxComboBox::GetCount() const
373 return m_choice
->GetCount() ;
376 void wxComboBox::SetValue(const wxString
& value
)
378 if ( HasFlag(wxCB_READONLY
) )
379 SetStringSelection( value
) ;
381 m_text
->SetValue( value
);
384 // Clipboard operations
385 void wxComboBox::Copy()
387 if ( m_text
!= NULL
)
393 void wxComboBox::Cut()
395 if ( m_text
!= NULL
)
401 void wxComboBox::Paste()
403 if ( m_text
!= NULL
)
409 void wxComboBox::SetEditable(bool editable
)
411 if ( ( m_text
== NULL
) && editable
)
413 m_text
= new wxComboBoxText( this );
415 else if ( ( m_text
!= NULL
) && !editable
)
421 int currentX
, currentY
;
422 GetPosition( ¤tX
, ¤tY
);
424 int currentW
, currentH
;
425 GetSize( ¤tW
, ¤tH
);
427 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
430 void wxComboBox::SetInsertionPoint(long pos
)
435 void wxComboBox::SetInsertionPointEnd()
440 long wxComboBox::GetInsertionPoint() const
446 long wxComboBox::GetLastPosition() const
452 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
457 void wxComboBox::Remove(long from
, long to
)
462 void wxComboBox::SetSelection(long from
, long to
)
467 int wxComboBox::DoAppend(const wxString
& item
)
469 return m_choice
->DoAppend( item
) ;
472 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
474 return m_choice
->DoInsert( item
, pos
) ;
477 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
479 return m_choice
->DoSetItemClientData( n
, clientData
) ;
482 void* wxComboBox::DoGetItemClientData(int n
) const
484 return m_choice
->DoGetItemClientData( n
) ;
487 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
489 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
492 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
494 return m_choice
->DoGetItemClientObject( n
) ;
497 void wxComboBox::FreeData()
499 if ( HasClientObjectData() )
501 size_t count
= GetCount();
502 for ( size_t n
= 0; n
< count
; n
++ )
504 SetClientObject( n
, NULL
);
509 void wxComboBox::Delete(int n
)
511 // force client object deletion
512 if( HasClientObjectData() )
513 SetClientObject( n
, NULL
);
514 m_choice
->Delete( n
);
517 void wxComboBox::Clear()
523 int wxComboBox::GetSelection() const
525 return m_choice
->GetSelection();
528 void wxComboBox::SetSelection(int n
)
530 m_choice
->SetSelection( n
);
532 if ( m_text
!= NULL
)
534 m_text
->SetValue( GetString( n
) );
538 int wxComboBox::FindString(const wxString
& s
) const
540 return m_choice
->FindString( s
);
543 wxString
wxComboBox::GetString(int n
) const
545 return m_choice
->GetString( n
);
548 wxString
wxComboBox::GetStringSelection() const
550 int sel
= GetSelection ();
552 return wxString(this->GetString (sel
));
554 return wxEmptyString
;
557 bool wxComboBox::SetStringSelection(const wxString
& sel
)
559 int s
= FindString (sel
);
569 void wxComboBox::SetString(int n
, const wxString
& s
)
571 m_choice
->SetString( n
, s
) ;
575 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
577 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
578 event
.SetInt(GetSelection());
579 event
.SetEventObject(this);
580 event
.SetString(GetStringSelection());
581 ProcessCommand(event
);