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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
23 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26 static int nextPopUpMenuId
= 1000 ;
27 MenuHandle
NewUniqueMenu()
29 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the margin between the text control and the choice
40 static const wxCoord MARGIN
= 2;
41 static const int POPUPWIDTH
= 18;
42 static const int POPUPHEIGHT
= 23;
45 // ----------------------------------------------------------------------------
46 // wxComboBoxText: text control forwards events to combobox
47 // ----------------------------------------------------------------------------
49 class wxComboBoxText
: public wxTextCtrl
52 wxComboBoxText( wxComboBox
* cb
)
53 : wxTextCtrl( cb
, 1 )
59 void OnChar( wxKeyEvent
& event
)
61 if ( event
.GetKeyCode() == WXK_RETURN
)
63 wxString value
= GetValue();
65 if ( m_cb
->GetCount() == 0 )
67 // make Enter generate "selected" event if there is only one item
68 // in the combobox - without it, it's impossible to select it at
70 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
72 event
.SetString( value
);
73 event
.SetEventObject( m_cb
);
74 m_cb
->GetEventHandler()->ProcessEvent( event
);
78 // add the item to the list if it's not there yet
79 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
82 m_cb
->SetStringSelection(value
);
84 // and generate the selected event for it
85 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
86 event
.SetInt( m_cb
->GetCount() - 1 );
87 event
.SetString( value
);
88 event
.SetEventObject( m_cb
);
89 m_cb
->GetEventHandler()->ProcessEvent( event
);
92 // This will invoke the dialog default action, such
93 // as the clicking the default button.
95 wxWindow
*parent
= GetParent();
96 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
97 parent
= parent
->GetParent() ;
99 if ( parent
&& parent
->GetDefaultItem() )
101 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
103 if ( def
&& def
->IsEnabled() )
105 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
106 event
.SetEventObject(def
);
122 DECLARE_EVENT_TABLE()
125 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
126 EVT_CHAR( wxComboBoxText::OnChar
)
129 class wxComboBoxChoice
: public wxChoice
132 wxComboBoxChoice(wxComboBox
*cb
, int style
)
139 void OnChoice( wxCommandEvent
& e
)
141 wxString s
= e
.GetString();
143 m_cb
->DelegateChoice( s
);
144 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
145 event2
.SetInt(m_cb
->GetSelection());
146 event2
.SetEventObject(m_cb
);
147 event2
.SetString(m_cb
->GetStringSelection());
148 m_cb
->ProcessCommand(event2
);
154 DECLARE_EVENT_TABLE()
157 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
158 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
161 wxComboBox::~wxComboBox()
163 // delete client objects
166 // delete the controls now, don't leave them alive even though they would
167 // still be eventually deleted by our parent - but it will be too late, the
168 // user code expects them to be gone now
169 if (m_text
!= NULL
) {
173 if (m_choice
!= NULL
) {
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 wxSize
wxComboBox::DoGetBestSize() const
186 wxSize size
= m_choice
->GetBestSize();
188 if ( m_text
!= NULL
)
190 wxSize sizeText
= m_text
->GetBestSize();
192 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
198 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
199 height
= POPUPHEIGHT
;
201 wxControl::DoMoveWindow(x
, y
, width
, height
);
203 if ( m_text
== NULL
)
205 m_choice
->SetSize(0, 0 , width
, -1);
209 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
210 m_text
->SetSize(0, 0, wText
, height
);
211 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
217 // ----------------------------------------------------------------------------
218 // operations forwarded to the subcontrols
219 // ----------------------------------------------------------------------------
221 bool wxComboBox::Enable(bool enable
)
223 if ( !wxControl::Enable(enable
) )
229 bool wxComboBox::Show(bool show
)
231 if ( !wxControl::Show(show
) )
237 void wxComboBox::SetFocus()
239 if ( m_text
!= NULL
) {
245 void wxComboBox::DelegateTextChanged( const wxString
& value
)
247 SetStringSelection( value
);
251 void wxComboBox::DelegateChoice( const wxString
& value
)
253 SetStringSelection( value
);
257 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
258 const wxString
& value
,
261 const wxArrayString
& choices
,
263 const wxValidator
& validator
,
264 const wxString
& name
)
266 wxCArrayString
chs( choices
);
268 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
269 chs
.GetStrings(), style
, validator
, name
);
273 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
274 const wxString
& value
,
277 int n
, const wxString choices
[],
279 const wxValidator
& validator
,
280 const wxString
& name
)
282 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
283 wxDefaultValidator
, name
) )
288 m_choice
= new wxComboBoxChoice(this, style
);
291 if ( style
& wxCB_READONLY
)
297 m_text
= new wxComboBoxText(this);
298 if ( size
.y
== -1 ) {
299 csize
.y
= m_text
->GetSize().y
;
303 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
305 for ( int i
= 0 ; i
< n
; i
++ )
307 m_choice
->DoAppend( choices
[ i
] );
313 wxString
wxComboBox::GetValue() const
317 if ( m_text
== NULL
)
319 result
= m_choice
->GetString( m_choice
->GetSelection() );
323 result
= m_text
->GetValue();
329 void wxComboBox::SetValue(const wxString
& value
)
331 int s
= FindString (value
);
332 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
334 m_choice
->Append(value
) ;
336 SetStringSelection( value
) ;
339 // Clipboard operations
340 void wxComboBox::Copy()
342 if ( m_text
!= NULL
)
348 void wxComboBox::Cut()
350 if ( m_text
!= NULL
)
356 void wxComboBox::Paste()
358 if ( m_text
!= NULL
)
364 void wxComboBox::SetEditable(bool editable
)
366 if ( ( m_text
== NULL
) && editable
)
368 m_text
= new wxComboBoxText( this );
370 else if ( ( m_text
!= NULL
) && !editable
)
376 int currentX
, currentY
;
377 GetPosition( ¤tX
, ¤tY
);
379 int currentW
, currentH
;
380 GetSize( ¤tW
, ¤tH
);
382 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
385 void wxComboBox::SetInsertionPoint(long pos
)
390 void wxComboBox::SetInsertionPointEnd()
395 long wxComboBox::GetInsertionPoint() const
401 wxTextPos
wxComboBox::GetLastPosition() const
407 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
412 void wxComboBox::Remove(long from
, long to
)
417 void wxComboBox::SetSelection(long from
, long to
)
422 int wxComboBox::DoAppend(const wxString
& item
)
424 return m_choice
->DoAppend( item
) ;
427 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
429 return m_choice
->DoInsert( item
, pos
) ;
432 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
434 return m_choice
->DoSetItemClientData( n
, clientData
) ;
437 void* wxComboBox::DoGetItemClientData(int n
) const
439 return m_choice
->DoGetItemClientData( n
) ;
442 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
444 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
447 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
449 return m_choice
->DoGetItemClientObject( n
) ;
452 void wxComboBox::FreeData()
454 if ( HasClientObjectData() )
456 size_t count
= GetCount();
457 for ( size_t n
= 0; n
< count
; n
++ )
459 SetClientObject( n
, NULL
);
464 void wxComboBox::Delete(int n
)
466 // force client object deletion
467 if( HasClientObjectData() )
468 SetClientObject( n
, NULL
);
469 m_choice
->Delete( n
);
472 void wxComboBox::Clear()
478 int wxComboBox::GetSelection() const
480 return m_choice
->GetSelection();
483 void wxComboBox::SetSelection(int n
)
485 m_choice
->SetSelection( n
);
487 if ( m_text
!= NULL
)
489 m_text
->SetValue( GetString( n
) );
493 int wxComboBox::FindString(const wxString
& s
) const
495 return m_choice
->FindString( s
);
498 wxString
wxComboBox::GetString(int n
) const
500 return m_choice
->GetString( n
);
503 wxString
wxComboBox::GetStringSelection() const
505 int sel
= GetSelection ();
507 return wxString(this->GetString (sel
));
509 return wxEmptyString
;
512 void wxComboBox::SetString(int n
, const wxString
& s
)
514 m_choice
->SetString( n
, s
) ;
517 bool wxComboBox::IsEditable() const
519 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
522 void wxComboBox::Undo()
528 void wxComboBox::Redo()
534 void wxComboBox::SelectAll()
540 bool wxComboBox::CanCopy() const
543 return m_text
->CanCopy();
548 bool wxComboBox::CanCut() const
551 return m_text
->CanCut();
556 bool wxComboBox::CanPaste() const
559 return m_text
->CanPaste();
564 bool wxComboBox::CanUndo() const
567 return m_text
->CanUndo();
572 bool wxComboBox::CanRedo() const
575 return m_text
->CanRedo();
580 void wxComboBox::MacHandleControlClick( WXWidget
WXUNUSED(control
) , wxInt16
WXUNUSED(controlpart
) , bool WXUNUSED(mouseStillDown
))
582 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
583 event
.SetInt(GetSelection());
584 event
.SetEventObject(this);
585 event
.SetString(GetStringSelection());
586 ProcessCommand(event
);