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 static const wxCoord MARGIN
= 2;
43 static const int POPUPWIDTH
= 18;
44 static const int POPUPHEIGHT
= 23;
47 // ----------------------------------------------------------------------------
48 // wxComboBoxText: text control forwards events to combobox
49 // ----------------------------------------------------------------------------
51 class wxComboBoxText
: public wxTextCtrl
54 wxComboBoxText( wxComboBox
* cb
)
55 : wxTextCtrl( cb
, 1 )
61 void OnChar( wxKeyEvent
& event
)
63 if ( event
.GetKeyCode() == WXK_RETURN
)
65 wxString value
= GetValue();
67 if ( m_cb
->GetCount() == 0 )
69 // make Enter generate "selected" event if there is only one item
70 // in the combobox - without it, it's impossible to select it at
72 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
74 event
.SetString( value
);
75 event
.SetEventObject( m_cb
);
76 m_cb
->GetEventHandler()->ProcessEvent( event
);
80 // add the item to the list if it's not there yet
81 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
84 m_cb
->SetStringSelection(value
);
86 // and generate the selected event for it
87 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
88 event
.SetInt( m_cb
->GetCount() - 1 );
89 event
.SetString( value
);
90 event
.SetEventObject( m_cb
);
91 m_cb
->GetEventHandler()->ProcessEvent( event
);
94 // This will invoke the dialog default action, such
95 // as the clicking the default button.
97 wxWindow
*parent
= GetParent();
98 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
99 parent
= parent
->GetParent() ;
101 if ( parent
&& parent
->GetDefaultItem() )
103 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
105 if ( def
&& def
->IsEnabled() )
107 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
108 event
.SetEventObject(def
);
124 DECLARE_EVENT_TABLE()
127 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
128 EVT_CHAR( wxComboBoxText::OnChar
)
131 class wxComboBoxChoice
: public wxChoice
134 wxComboBoxChoice(wxComboBox
*cb
, int style
)
141 void OnChoice( wxCommandEvent
& e
)
143 wxString s
= e
.GetString();
145 m_cb
->DelegateChoice( s
);
146 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
147 event2
.SetInt(m_cb
->GetSelection());
148 event2
.SetEventObject(m_cb
);
149 event2
.SetString(m_cb
->GetStringSelection());
150 m_cb
->ProcessCommand(event2
);
156 DECLARE_EVENT_TABLE()
159 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
160 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
163 wxComboBox::~wxComboBox()
165 // delete client objects
168 // delete the controls now, don't leave them alive even though they would
169 // still be eventually deleted by our parent - but it will be too late, the
170 // user code expects them to be gone now
171 if (m_text
!= NULL
) {
175 if (m_choice
!= NULL
) {
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxSize
wxComboBox::DoGetBestSize() const
188 wxSize size
= m_choice
->GetBestSize();
190 if ( m_text
!= NULL
)
192 wxSize sizeText
= m_text
->GetBestSize();
194 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
200 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
201 height
= POPUPHEIGHT
;
203 wxControl::DoMoveWindow(x
, y
, width
, height
);
205 if ( m_text
== NULL
)
207 // we might not be fully constructed yet, therefore watch out...
209 m_choice
->SetSize(0, 0 , width
, -1);
213 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
214 m_text
->SetSize(0, 0, wText
, height
);
215 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
221 // ----------------------------------------------------------------------------
222 // operations forwarded to the subcontrols
223 // ----------------------------------------------------------------------------
225 bool wxComboBox::Enable(bool enable
)
227 if ( !wxControl::Enable(enable
) )
233 bool wxComboBox::Show(bool show
)
235 if ( !wxControl::Show(show
) )
241 void wxComboBox::SetFocus()
243 if ( m_text
!= NULL
) {
249 void wxComboBox::DelegateTextChanged( const wxString
& value
)
251 SetStringSelection( value
);
255 void wxComboBox::DelegateChoice( const wxString
& value
)
257 SetStringSelection( value
);
261 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
262 const wxString
& value
,
265 const wxArrayString
& choices
,
267 const wxValidator
& validator
,
268 const wxString
& name
)
270 wxCArrayString
chs( choices
);
272 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
273 chs
.GetStrings(), style
, validator
, name
);
277 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
278 const wxString
& value
,
281 int n
, const wxString choices
[],
283 const wxValidator
& validator
,
284 const wxString
& name
)
286 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
287 wxDefaultValidator
, name
) )
292 m_choice
= new wxComboBoxChoice(this, style
);
295 if ( style
& wxCB_READONLY
)
301 m_text
= new wxComboBoxText(this);
302 if ( size
.y
== -1 ) {
303 csize
.y
= m_text
->GetSize().y
;
307 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
309 for ( int i
= 0 ; i
< n
; i
++ )
311 m_choice
->DoAppend( choices
[ i
] );
314 SetBestSize(csize
); // Needed because it is a wxControlWithItems
319 wxString
wxComboBox::GetValue() const
323 if ( m_text
== NULL
)
325 result
= m_choice
->GetString( m_choice
->GetSelection() );
329 result
= m_text
->GetValue();
335 void wxComboBox::SetValue(const wxString
& value
)
337 int s
= FindString (value
);
338 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
340 m_choice
->Append(value
) ;
342 SetStringSelection( value
) ;
345 // Clipboard operations
346 void wxComboBox::Copy()
348 if ( m_text
!= NULL
)
354 void wxComboBox::Cut()
356 if ( m_text
!= NULL
)
362 void wxComboBox::Paste()
364 if ( m_text
!= NULL
)
370 void wxComboBox::SetEditable(bool editable
)
372 if ( ( m_text
== NULL
) && editable
)
374 m_text
= new wxComboBoxText( this );
376 else if ( ( m_text
!= NULL
) && !editable
)
382 int currentX
, currentY
;
383 GetPosition( ¤tX
, ¤tY
);
385 int currentW
, currentH
;
386 GetSize( ¤tW
, ¤tH
);
388 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
391 void wxComboBox::SetInsertionPoint(long pos
)
396 void wxComboBox::SetInsertionPointEnd()
401 long wxComboBox::GetInsertionPoint() const
407 long wxComboBox::GetLastPosition() const
413 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
418 void wxComboBox::Remove(long from
, long to
)
423 void wxComboBox::SetSelection(long from
, long to
)
428 int wxComboBox::DoAppend(const wxString
& item
)
430 return m_choice
->DoAppend( item
) ;
433 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
435 return m_choice
->DoInsert( item
, pos
) ;
438 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
440 return m_choice
->DoSetItemClientData( n
, clientData
) ;
443 void* wxComboBox::DoGetItemClientData(int n
) const
445 return m_choice
->DoGetItemClientData( n
) ;
448 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
450 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
453 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
455 return m_choice
->DoGetItemClientObject( n
) ;
458 void wxComboBox::FreeData()
460 if ( HasClientObjectData() )
462 size_t count
= GetCount();
463 for ( size_t n
= 0; n
< count
; n
++ )
465 SetClientObject( n
, NULL
);
470 void wxComboBox::Delete(int n
)
472 // force client object deletion
473 if( HasClientObjectData() )
474 SetClientObject( n
, NULL
);
475 m_choice
->Delete( n
);
478 void wxComboBox::Clear()
484 int wxComboBox::GetSelection() const
486 return m_choice
->GetSelection();
489 void wxComboBox::SetSelection(int n
)
491 m_choice
->SetSelection( n
);
493 if ( m_text
!= NULL
)
495 m_text
->SetValue( GetString( n
) );
499 int wxComboBox::FindString(const wxString
& s
) const
501 return m_choice
->FindString( s
);
504 wxString
wxComboBox::GetString(int n
) const
506 return m_choice
->GetString( n
);
509 wxString
wxComboBox::GetStringSelection() const
511 int sel
= GetSelection ();
513 return wxString(this->GetString (sel
));
515 return wxEmptyString
;
518 bool wxComboBox::SetStringSelection(const wxString
& sel
)
520 int s
= FindString (sel
);
530 void wxComboBox::SetString(int n
, const wxString
& s
)
532 m_choice
->SetString( n
, s
) ;
536 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
538 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
539 event
.SetInt(GetSelection());
540 event
.SetEventObject(this);
541 event
.SetString(GetStringSelection());
542 ProcessCommand(event
);