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
= 6;
46 static const int POPUPWIDTH
= 24;
48 static const wxCoord MARGIN
= 2;
49 static const int POPUPWIDTH
= 18;
51 static const int POPUPHEIGHT
= 23;
54 // ----------------------------------------------------------------------------
55 // wxComboBoxText: text control forwards events to combobox
56 // ----------------------------------------------------------------------------
58 class wxComboBoxText
: public wxTextCtrl
61 wxComboBoxText( wxComboBox
* cb
)
62 : wxTextCtrl( cb
, 1 )
66 // remove the default minsize, the combobox will have one instead
71 void OnChar( wxKeyEvent
& event
)
73 if ( event
.GetKeyCode() == WXK_RETURN
)
75 wxString value
= GetValue();
77 if ( m_cb
->GetCount() == 0 )
79 // make Enter generate "selected" event if there is only one item
80 // in the combobox - without it, it's impossible to select it at
82 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
84 event
.SetString( value
);
85 event
.SetEventObject( m_cb
);
86 m_cb
->GetEventHandler()->ProcessEvent( event
);
90 // add the item to the list if it's not there yet
91 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
94 m_cb
->SetStringSelection(value
);
96 // and generate the selected event for it
97 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
98 event
.SetInt( m_cb
->GetCount() - 1 );
99 event
.SetString( value
);
100 event
.SetEventObject( m_cb
);
101 m_cb
->GetEventHandler()->ProcessEvent( event
);
104 // This will invoke the dialog default action, such
105 // as the clicking the default button.
107 wxWindow
*parent
= GetParent();
108 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
109 parent
= parent
->GetParent() ;
111 if ( parent
&& parent
->GetDefaultItem() )
113 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
115 if ( def
&& def
->IsEnabled() )
117 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
118 event
.SetEventObject(def
);
134 DECLARE_EVENT_TABLE()
137 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
138 EVT_CHAR( wxComboBoxText::OnChar
)
141 class wxComboBoxChoice
: public wxChoice
144 wxComboBoxChoice(wxComboBox
*cb
, int style
)
149 // remove the default minsize, the combobox will have one instead
154 void OnChoice( wxCommandEvent
& e
)
156 wxString s
= e
.GetString();
158 m_cb
->DelegateChoice( s
);
159 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
160 event2
.SetInt(m_cb
->GetSelection());
161 event2
.SetEventObject(m_cb
);
162 event2
.SetString(m_cb
->GetStringSelection());
163 m_cb
->ProcessCommand(event2
);
165 virtual wxSize
DoGetBestSize() const
167 wxSize sz
= wxChoice::DoGetBestSize() ;
168 if (! m_cb
->HasFlag(wxCB_READONLY
) )
176 DECLARE_EVENT_TABLE()
179 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
180 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
183 wxComboBox::~wxComboBox()
185 // delete client objects
188 // delete the controls now, don't leave them alive even though they would
189 // still be eventually deleted by our parent - but it will be too late, the
190 // user code expects them to be gone now
191 if (m_text
!= NULL
) {
195 if (m_choice
!= NULL
) {
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 wxSize
wxComboBox::DoGetBestSize() const
208 if (!m_choice
|| !m_text
)
210 wxSize size
= m_choice
->GetBestSize();
212 if ( m_text
!= NULL
)
214 wxSize sizeText
= m_text
->GetBestSize();
215 if (sizeText
.y
> size
.y
)
217 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
223 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
224 height
= POPUPHEIGHT
;
226 #if TARGET_API_MAC_OSX
227 // give the controls some padding so that the text ctrl's borders
228 // and blue highlight can appear
232 wxControl::DoMoveWindow(x
, y
, width
+ origin
, height
+ origin
);
234 if ( m_text
== NULL
)
236 // we might not be fully constructed yet, therefore watch out...
238 m_choice
->SetSize(0, 0 , width
, -1);
242 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
243 #if TARGET_API_MAC_OSX
244 // also, we need to shrink the size of the wxTextCtrl a bit
245 // to make it appear properly on OS X.
249 m_text
->SetSize(origin
, origin
, wText
, height
);
250 m_choice
->SetSize(origin
+ wText
+ MARGIN
, 0, POPUPWIDTH
, -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
);
328 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
330 if ( style
& wxCB_READONLY
)
336 m_text
= new wxComboBoxText(this);
337 if ( size
.y
== -1 ) {
338 csize
.y
= m_text
->GetSize().y
;
342 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
344 for ( int i
= 0 ; i
< n
; i
++ )
346 m_choice
->DoAppend( choices
[ i
] );
349 SetBestSize(csize
); // Needed because it is a wxControlWithItems
354 wxString
wxComboBox::GetValue() const
358 if ( m_text
== NULL
)
360 result
= m_choice
->GetString( m_choice
->GetSelection() );
364 result
= m_text
->GetValue();
370 int wxComboBox::GetCount() const
372 return m_choice
->GetCount() ;
375 void wxComboBox::SetValue(const wxString
& value
)
377 int s
= FindString (value
);
378 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
380 m_choice
->Append(value
) ;
382 SetStringSelection( value
) ;
385 // Clipboard operations
386 void wxComboBox::Copy()
388 if ( m_text
!= NULL
)
394 void wxComboBox::Cut()
396 if ( m_text
!= NULL
)
402 void wxComboBox::Paste()
404 if ( m_text
!= NULL
)
410 void wxComboBox::SetEditable(bool editable
)
412 if ( ( m_text
== NULL
) && editable
)
414 m_text
= new wxComboBoxText( this );
416 else if ( ( m_text
!= NULL
) && !editable
)
422 int currentX
, currentY
;
423 GetPosition( ¤tX
, ¤tY
);
425 int currentW
, currentH
;
426 GetSize( ¤tW
, ¤tH
);
428 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
431 void wxComboBox::SetInsertionPoint(long pos
)
436 void wxComboBox::SetInsertionPointEnd()
441 long wxComboBox::GetInsertionPoint() const
447 long wxComboBox::GetLastPosition() const
453 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
458 void wxComboBox::Remove(long from
, long to
)
463 void wxComboBox::SetSelection(long from
, long to
)
468 int wxComboBox::DoAppend(const wxString
& item
)
470 return m_choice
->DoAppend( item
) ;
473 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
475 return m_choice
->DoInsert( item
, pos
) ;
478 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
480 return m_choice
->DoSetItemClientData( n
, clientData
) ;
483 void* wxComboBox::DoGetItemClientData(int n
) const
485 return m_choice
->DoGetItemClientData( n
) ;
488 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
490 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
493 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
495 return m_choice
->DoGetItemClientObject( n
) ;
498 void wxComboBox::FreeData()
500 if ( HasClientObjectData() )
502 size_t count
= GetCount();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 SetClientObject( n
, NULL
);
510 void wxComboBox::Delete(int n
)
512 // force client object deletion
513 if( HasClientObjectData() )
514 SetClientObject( n
, NULL
);
515 m_choice
->Delete( n
);
518 void wxComboBox::Clear()
524 int wxComboBox::GetSelection() const
526 return m_choice
->GetSelection();
529 void wxComboBox::SetSelection(int n
)
531 m_choice
->SetSelection( n
);
533 if ( m_text
!= NULL
)
535 m_text
->SetValue( GetString( n
) );
539 int wxComboBox::FindString(const wxString
& s
) const
541 return m_choice
->FindString( s
);
544 wxString
wxComboBox::GetString(int n
) const
546 return m_choice
->GetString( n
);
549 wxString
wxComboBox::GetStringSelection() const
551 int sel
= GetSelection ();
553 return wxString(this->GetString (sel
));
555 return wxEmptyString
;
558 bool wxComboBox::SetStringSelection(const wxString
& sel
)
560 int s
= FindString (sel
);
570 void wxComboBox::SetString(int n
, const wxString
& s
)
572 m_choice
->SetString( n
, s
) ;
576 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
578 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
579 event
.SetInt(GetSelection());
580 event
.SetEventObject(this);
581 event
.SetString(GetStringSelection());
582 ProcessCommand(event
);