1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/combobox.h"
13 #include "wx/button.h"
15 #include "wx/mac/uma.h"
17 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
19 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
22 static int nextPopUpMenuId
= 1000 ;
23 MenuHandle
NewUniqueMenu()
25 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // the margin between the text control and the choice
36 static const wxCoord MARGIN
= 2;
37 static const int POPUPWIDTH
= 18;
38 static const int POPUPHEIGHT
= 23;
41 // ----------------------------------------------------------------------------
42 // wxComboBoxText: text control forwards events to combobox
43 // ----------------------------------------------------------------------------
45 class wxComboBoxText
: public wxTextCtrl
48 wxComboBoxText( wxComboBox
* cb
)
49 : wxTextCtrl( cb
, 1 )
55 void OnChar( wxKeyEvent
& event
)
57 if ( event
.GetKeyCode() == WXK_RETURN
)
59 wxString value
= GetValue();
61 if ( m_cb
->GetCount() == 0 )
63 // make Enter generate "selected" event if there is only one item
64 // in the combobox - without it, it's impossible to select it at
66 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
68 event
.SetString( value
);
69 event
.SetEventObject( m_cb
);
70 m_cb
->GetEventHandler()->ProcessEvent( event
);
74 // add the item to the list if it's not there yet
75 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
78 m_cb
->SetStringSelection(value
);
80 // and generate the selected event for it
81 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
82 event
.SetInt( m_cb
->GetCount() - 1 );
83 event
.SetString( value
);
84 event
.SetEventObject( m_cb
);
85 m_cb
->GetEventHandler()->ProcessEvent( event
);
88 // This will invoke the dialog default action, such
89 // as the clicking the default button.
91 wxWindow
*parent
= GetParent();
92 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
93 parent
= parent
->GetParent() ;
95 if ( parent
&& parent
->GetDefaultItem() )
97 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
99 if ( def
&& def
->IsEnabled() )
101 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
102 event
.SetEventObject(def
);
118 DECLARE_EVENT_TABLE()
121 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
122 EVT_CHAR( wxComboBoxText::OnChar
)
125 class wxComboBoxChoice
: public wxChoice
128 wxComboBoxChoice(wxComboBox
*cb
, int style
)
135 void OnChoice( wxCommandEvent
& e
)
137 wxString s
= e
.GetString();
139 m_cb
->DelegateChoice( s
);
140 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
141 event2
.SetInt(m_cb
->GetSelection());
142 event2
.SetEventObject(m_cb
);
143 event2
.SetString(m_cb
->GetStringSelection());
144 m_cb
->ProcessCommand(event2
);
150 DECLARE_EVENT_TABLE()
153 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
154 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
157 wxComboBox::~wxComboBox()
159 // delete client objects
162 // delete the controls now, don't leave them alive even though they would
163 // still be eventually deleted by our parent - but it will be too late, the
164 // user code expects them to be gone now
165 if (m_text
!= NULL
) {
169 if (m_choice
!= NULL
) {
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxSize
wxComboBox::DoGetBestSize() const
182 wxSize size
= m_choice
->GetBestSize();
184 if ( m_text
!= NULL
)
186 wxSize sizeText
= m_text
->GetBestSize();
188 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
194 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
195 height
= POPUPHEIGHT
;
197 wxControl::DoMoveWindow(x
, y
, width
, height
);
199 if ( m_text
== NULL
)
201 m_choice
->SetSize(0, 0 , width
, -1);
205 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
206 m_text
->SetSize(0, 0, wText
, height
);
207 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
213 // ----------------------------------------------------------------------------
214 // operations forwarded to the subcontrols
215 // ----------------------------------------------------------------------------
217 bool wxComboBox::Enable(bool enable
)
219 if ( !wxControl::Enable(enable
) )
225 bool wxComboBox::Show(bool show
)
227 if ( !wxControl::Show(show
) )
233 void wxComboBox::SetFocus()
235 if ( m_text
!= NULL
) {
241 void wxComboBox::DelegateTextChanged( const wxString
& value
)
243 SetStringSelection( value
);
247 void wxComboBox::DelegateChoice( const wxString
& value
)
249 SetStringSelection( value
);
253 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
254 const wxString
& value
,
257 const wxArrayString
& choices
,
259 const wxValidator
& validator
,
260 const wxString
& name
)
262 wxCArrayString
chs( choices
);
264 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
265 chs
.GetStrings(), style
, validator
, name
);
269 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
270 const wxString
& value
,
273 int n
, const wxString choices
[],
275 const wxValidator
& validator
,
276 const wxString
& name
)
278 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
279 wxDefaultValidator
, name
) )
284 m_choice
= new wxComboBoxChoice(this, style
);
287 if ( style
& wxCB_READONLY
)
293 m_text
= new wxComboBoxText(this);
294 if ( size
.y
== -1 ) {
295 csize
.y
= m_text
->GetSize().y
;
299 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
301 for ( int i
= 0 ; i
< n
; i
++ )
303 m_choice
->DoAppend( choices
[ i
] );
309 wxString
wxComboBox::GetValue() const
313 if ( m_text
== NULL
)
315 result
= m_choice
->GetString( m_choice
->GetSelection() );
319 result
= m_text
->GetValue();
325 void wxComboBox::SetValue(const wxString
& value
)
327 int s
= FindString (value
);
328 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
330 m_choice
->Append(value
) ;
332 SetStringSelection( value
) ;
335 // Clipboard operations
336 void wxComboBox::Copy()
338 if ( m_text
!= NULL
)
344 void wxComboBox::Cut()
346 if ( m_text
!= NULL
)
352 void wxComboBox::Paste()
354 if ( m_text
!= NULL
)
360 void wxComboBox::SetEditable(bool editable
)
362 if ( ( m_text
== NULL
) && editable
)
364 m_text
= new wxComboBoxText( this );
366 else if ( ( m_text
!= NULL
) && !editable
)
372 int currentX
, currentY
;
373 GetPosition( ¤tX
, ¤tY
);
375 int currentW
, currentH
;
376 GetSize( ¤tW
, ¤tH
);
378 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
381 void wxComboBox::SetInsertionPoint(long pos
)
386 void wxComboBox::SetInsertionPointEnd()
391 long wxComboBox::GetInsertionPoint() const
397 wxTextPos
wxComboBox::GetLastPosition() const
403 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
408 void wxComboBox::Remove(long from
, long to
)
413 void wxComboBox::SetSelection(long from
, long to
)
418 int wxComboBox::DoAppend(const wxString
& item
)
420 return m_choice
->DoAppend( item
) ;
423 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
425 return m_choice
->DoInsert( item
, pos
) ;
428 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
430 return m_choice
->DoSetItemClientData( n
, clientData
) ;
433 void* wxComboBox::DoGetItemClientData(int n
) const
435 return m_choice
->DoGetItemClientData( n
) ;
438 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
440 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
443 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
445 return m_choice
->DoGetItemClientObject( n
) ;
448 void wxComboBox::FreeData()
450 if ( HasClientObjectData() )
452 size_t count
= GetCount();
453 for ( size_t n
= 0; n
< count
; n
++ )
455 SetClientObject( n
, NULL
);
460 void wxComboBox::Delete(int n
)
462 // force client object deletion
463 if( HasClientObjectData() )
464 SetClientObject( n
, NULL
);
465 m_choice
->Delete( n
);
468 void wxComboBox::Clear()
474 int wxComboBox::GetSelection() const
476 return m_choice
->GetSelection();
479 void wxComboBox::SetSelection(int n
)
481 m_choice
->SetSelection( n
);
483 if ( m_text
!= NULL
)
485 m_text
->SetValue( GetString( n
) );
489 int wxComboBox::FindString(const wxString
& s
) const
491 return m_choice
->FindString( s
);
494 wxString
wxComboBox::GetString(int n
) const
496 return m_choice
->GetString( n
);
499 wxString
wxComboBox::GetStringSelection() const
501 int sel
= GetSelection ();
503 return wxString(this->GetString (sel
));
505 return wxEmptyString
;
508 void wxComboBox::SetString(int n
, const wxString
& s
)
510 m_choice
->SetString( n
, s
) ;
513 bool wxComboBox::IsEditable() const
515 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
518 void wxComboBox::Undo()
524 void wxComboBox::Redo()
530 void wxComboBox::SelectAll()
536 bool wxComboBox::CanCopy() const
539 return m_text
->CanCopy();
544 bool wxComboBox::CanCut() const
547 return m_text
->CanCut();
552 bool wxComboBox::CanPaste() const
555 return m_text
->CanPaste();
560 bool wxComboBox::CanUndo() const
563 return m_text
->CanUndo();
568 bool wxComboBox::CanRedo() const
571 return m_text
->CanRedo();
576 void wxComboBox::MacHandleControlClick( WXWidget
WXUNUSED(control
) , wxInt16
WXUNUSED(controlpart
) , bool WXUNUSED(mouseStillDown
))
578 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
579 event
.SetInt(GetSelection());
580 event
.SetEventObject(this);
581 event
.SetString(GetStringSelection());
582 ProcessCommand(event
);