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 #if TARGET_API_MAC_OSX
44 static const int POPUPWIDTH
= 24;
46 static const int POPUPWIDTH
= 18;
48 static const int POPUPHEIGHT
= 23;
51 // ----------------------------------------------------------------------------
52 // wxComboBoxText: text control forwards events to combobox
53 // ----------------------------------------------------------------------------
55 class wxComboBoxText
: public wxTextCtrl
58 wxComboBoxText( wxComboBox
* cb
)
59 : wxTextCtrl( cb
, 1 )
63 // remove the default minsize, the combobox will have one instead
68 void OnChar( wxKeyEvent
& event
)
70 if ( event
.GetKeyCode() == WXK_RETURN
)
72 wxString value
= GetValue();
74 if ( m_cb
->GetCount() == 0 )
76 // make Enter generate "selected" event if there is only one item
77 // in the combobox - without it, it's impossible to select it at
79 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
81 event
.SetString( value
);
82 event
.SetEventObject( m_cb
);
83 m_cb
->GetEventHandler()->ProcessEvent( event
);
87 // add the item to the list if it's not there yet
88 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
91 m_cb
->SetStringSelection(value
);
93 // and generate the selected event for it
94 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
95 event
.SetInt( m_cb
->GetCount() - 1 );
96 event
.SetString( value
);
97 event
.SetEventObject( m_cb
);
98 m_cb
->GetEventHandler()->ProcessEvent( event
);
101 // This will invoke the dialog default action, such
102 // as the clicking the default button.
104 wxWindow
*parent
= GetParent();
105 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
106 parent
= parent
->GetParent() ;
108 if ( parent
&& parent
->GetDefaultItem() )
110 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
112 if ( def
&& def
->IsEnabled() )
114 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
115 event
.SetEventObject(def
);
131 DECLARE_EVENT_TABLE()
134 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
135 EVT_CHAR( wxComboBoxText::OnChar
)
138 class wxComboBoxChoice
: public wxChoice
141 wxComboBoxChoice(wxComboBox
*cb
, int style
)
146 // remove the default minsize, the combobox will have one instead
151 void OnChoice( wxCommandEvent
& e
)
153 wxString s
= e
.GetString();
155 m_cb
->DelegateChoice( s
);
156 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
157 event2
.SetInt(m_cb
->GetSelection());
158 event2
.SetEventObject(m_cb
);
159 event2
.SetString(m_cb
->GetStringSelection());
160 m_cb
->ProcessCommand(event2
);
162 virtual wxSize
DoGetBestSize() const
164 wxSize sz
= wxChoice::DoGetBestSize() ;
165 if (! m_cb
->HasFlag(wxCB_READONLY
) )
173 DECLARE_EVENT_TABLE()
176 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
177 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
180 wxComboBox::~wxComboBox()
182 // delete client objects
185 // delete the controls now, don't leave them alive even though they would
186 // still be eventually deleted by our parent - but it will be too late, the
187 // user code expects them to be gone now
188 if (m_text
!= NULL
) {
192 if (m_choice
!= NULL
) {
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 wxSize
wxComboBox::DoGetBestSize() const
205 wxSize size
= m_choice
->GetBestSize();
207 if ( m_text
!= NULL
)
209 wxSize sizeText
= m_text
->GetBestSize();
211 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
217 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
218 height
= POPUPHEIGHT
;
220 wxControl::DoMoveWindow(x
, y
, width
, height
);
222 if ( m_text
== NULL
)
224 // we might not be fully constructed yet, therefore watch out...
226 m_choice
->SetSize(0, 0 , width
, -1);
230 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
231 m_text
->SetSize(0, 0, wText
, height
);
232 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
238 // ----------------------------------------------------------------------------
239 // operations forwarded to the subcontrols
240 // ----------------------------------------------------------------------------
242 bool wxComboBox::Enable(bool enable
)
244 if ( !wxControl::Enable(enable
) )
250 bool wxComboBox::Show(bool show
)
252 if ( !wxControl::Show(show
) )
258 void wxComboBox::SetFocus()
260 if ( m_text
!= NULL
) {
266 void wxComboBox::DelegateTextChanged( const wxString
& value
)
268 SetStringSelection( value
);
272 void wxComboBox::DelegateChoice( const wxString
& value
)
274 SetStringSelection( value
);
278 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
279 const wxString
& value
,
282 const wxArrayString
& choices
,
284 const wxValidator
& validator
,
285 const wxString
& name
)
287 wxCArrayString
chs( choices
);
289 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
290 chs
.GetStrings(), style
, validator
, name
);
294 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
295 const wxString
& value
,
298 int n
, const wxString choices
[],
300 const wxValidator
& validator
,
301 const wxString
& name
)
303 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
304 wxDefaultValidator
, name
) )
309 m_choice
= new wxComboBoxChoice(this, style
);
310 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
312 if ( style
& wxCB_READONLY
)
318 m_text
= new wxComboBoxText(this);
319 if ( size
.y
== -1 ) {
320 csize
.y
= m_text
->GetSize().y
;
324 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
326 for ( int i
= 0 ; i
< n
; i
++ )
328 m_choice
->DoAppend( choices
[ i
] );
331 SetBestSize(csize
); // Needed because it is a wxControlWithItems
336 wxString
wxComboBox::GetValue() const
340 if ( m_text
== NULL
)
342 result
= m_choice
->GetString( m_choice
->GetSelection() );
346 result
= m_text
->GetValue();
352 int wxComboBox::GetCount() const
354 return m_choice
->GetCount() ;
357 void wxComboBox::SetValue(const wxString
& value
)
359 int s
= FindString (value
);
360 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
362 m_choice
->Append(value
) ;
364 SetStringSelection( value
) ;
367 // Clipboard operations
368 void wxComboBox::Copy()
370 if ( m_text
!= NULL
)
376 void wxComboBox::Cut()
378 if ( m_text
!= NULL
)
384 void wxComboBox::Paste()
386 if ( m_text
!= NULL
)
392 void wxComboBox::SetEditable(bool editable
)
394 if ( ( m_text
== NULL
) && editable
)
396 m_text
= new wxComboBoxText( this );
398 else if ( ( m_text
!= NULL
) && !editable
)
404 int currentX
, currentY
;
405 GetPosition( ¤tX
, ¤tY
);
407 int currentW
, currentH
;
408 GetSize( ¤tW
, ¤tH
);
410 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
413 void wxComboBox::SetInsertionPoint(long pos
)
418 void wxComboBox::SetInsertionPointEnd()
423 long wxComboBox::GetInsertionPoint() const
429 long wxComboBox::GetLastPosition() const
435 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
440 void wxComboBox::Remove(long from
, long to
)
445 void wxComboBox::SetSelection(long from
, long to
)
450 int wxComboBox::DoAppend(const wxString
& item
)
452 return m_choice
->DoAppend( item
) ;
455 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
457 return m_choice
->DoInsert( item
, pos
) ;
460 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
462 return m_choice
->DoSetItemClientData( n
, clientData
) ;
465 void* wxComboBox::DoGetItemClientData(int n
) const
467 return m_choice
->DoGetItemClientData( n
) ;
470 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
472 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
475 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
477 return m_choice
->DoGetItemClientObject( n
) ;
480 void wxComboBox::FreeData()
482 if ( HasClientObjectData() )
484 size_t count
= GetCount();
485 for ( size_t n
= 0; n
< count
; n
++ )
487 SetClientObject( n
, NULL
);
492 void wxComboBox::Delete(int n
)
494 // force client object deletion
495 if( HasClientObjectData() )
496 SetClientObject( n
, NULL
);
497 m_choice
->Delete( n
);
500 void wxComboBox::Clear()
506 int wxComboBox::GetSelection() const
508 return m_choice
->GetSelection();
511 void wxComboBox::SetSelection(int n
)
513 m_choice
->SetSelection( n
);
515 if ( m_text
!= NULL
)
517 m_text
->SetValue( GetString( n
) );
521 int wxComboBox::FindString(const wxString
& s
) const
523 return m_choice
->FindString( s
);
526 wxString
wxComboBox::GetString(int n
) const
528 return m_choice
->GetString( n
);
531 wxString
wxComboBox::GetStringSelection() const
533 int sel
= GetSelection ();
535 return wxString(this->GetString (sel
));
537 return wxEmptyString
;
540 bool wxComboBox::SetStringSelection(const wxString
& sel
)
542 int s
= FindString (sel
);
552 void wxComboBox::SetString(int n
, const wxString
& s
)
554 m_choice
->SetString( n
, s
) ;
558 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
560 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
561 event
.SetInt(GetSelection());
562 event
.SetEventObject(this);
563 event
.SetString(GetStringSelection());
564 ProcessCommand(event
);