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 )
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
)
148 void OnChoice( wxCommandEvent
& e
)
150 wxString s
= e
.GetString();
152 m_cb
->DelegateChoice( s
);
153 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
154 event2
.SetInt(m_cb
->GetSelection());
155 event2
.SetEventObject(m_cb
);
156 event2
.SetString(m_cb
->GetStringSelection());
157 m_cb
->ProcessCommand(event2
);
159 virtual wxSize
DoGetBestSize() const
161 wxSize sz
= wxChoice::DoGetBestSize() ;
162 if (! m_cb
->HasFlag(wxCB_READONLY
) )
170 DECLARE_EVENT_TABLE()
173 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
174 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
177 wxComboBox::~wxComboBox()
179 // delete client objects
182 // delete the controls now, don't leave them alive even though they would
183 // still be eventually deleted by our parent - but it will be too late, the
184 // user code expects them to be gone now
185 if (m_text
!= NULL
) {
189 if (m_choice
!= NULL
) {
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 wxSize
wxComboBox::DoGetBestSize() const
202 if (!m_choice
&& !m_text
)
204 wxSize size
= m_choice
->GetBestSize();
206 if ( m_text
!= NULL
)
208 wxSize sizeText
= m_text
->GetBestSize();
209 if (sizeText
.y
> size
.y
)
211 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
217 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
218 height
= POPUPHEIGHT
;
220 #if TARGET_API_MAC_OSX
221 // give the controls some padding so that the text ctrl's borders
222 // and blue highlight can appear
226 wxControl::DoMoveWindow(x
, y
, width
+ origin
, height
+ origin
);
228 if ( m_text
== NULL
)
230 // we might not be fully constructed yet, therefore watch out...
232 m_choice
->SetSize(0, 0 , width
, -1);
236 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
237 #if TARGET_API_MAC_OSX
238 // also, we need to shrink the size of the wxTextCtrl a bit
239 // to make it appear properly on OS X.
243 m_text
->SetSize(origin
, origin
, wText
, height
);
244 m_choice
->SetSize(origin
+ wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
250 // ----------------------------------------------------------------------------
251 // operations forwarded to the subcontrols
252 // ----------------------------------------------------------------------------
254 bool wxComboBox::Enable(bool enable
)
256 if ( !wxControl::Enable(enable
) )
262 bool wxComboBox::Show(bool show
)
264 if ( !wxControl::Show(show
) )
270 void wxComboBox::SetFocus()
272 if ( m_text
!= NULL
) {
278 void wxComboBox::DelegateTextChanged( const wxString
& value
)
280 SetStringSelection( value
);
284 void wxComboBox::DelegateChoice( const wxString
& value
)
286 SetStringSelection( value
);
290 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
291 const wxString
& value
,
294 const wxArrayString
& choices
,
296 const wxValidator
& validator
,
297 const wxString
& name
)
299 wxCArrayString
chs( choices
);
301 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
302 chs
.GetStrings(), style
, validator
, name
);
306 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
307 const wxString
& value
,
310 int n
, const wxString choices
[],
312 const wxValidator
& validator
,
313 const wxString
& name
)
315 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
316 wxDefaultValidator
, name
) )
321 m_choice
= new wxComboBoxChoice(this, style
);
323 if ( style
& wxCB_READONLY
)
329 m_text
= new wxComboBoxText(this);
330 if ( size
.y
== -1 ) {
331 csize
.y
= m_text
->GetSize().y
;
335 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
337 for ( int i
= 0 ; i
< n
; i
++ )
339 m_choice
->DoAppend( choices
[ i
] );
342 SetBestSize(size
); // Needed because it is a wxControlWithItems
347 wxString
wxComboBox::GetValue() const
351 if ( m_text
== NULL
)
353 result
= m_choice
->GetString( m_choice
->GetSelection() );
357 result
= m_text
->GetValue();
363 int wxComboBox::GetCount() const
365 return m_choice
->GetCount() ;
368 void wxComboBox::SetValue(const wxString
& value
)
370 int s
= FindString (value
);
371 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
373 m_choice
->Append(value
) ;
375 SetStringSelection( value
) ;
378 // Clipboard operations
379 void wxComboBox::Copy()
381 if ( m_text
!= NULL
)
387 void wxComboBox::Cut()
389 if ( m_text
!= NULL
)
395 void wxComboBox::Paste()
397 if ( m_text
!= NULL
)
403 void wxComboBox::SetEditable(bool editable
)
405 if ( ( m_text
== NULL
) && editable
)
407 m_text
= new wxComboBoxText( this );
409 else if ( ( m_text
!= NULL
) && !editable
)
415 int currentX
, currentY
;
416 GetPosition( ¤tX
, ¤tY
);
418 int currentW
, currentH
;
419 GetSize( ¤tW
, ¤tH
);
421 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
424 void wxComboBox::SetInsertionPoint(long pos
)
429 void wxComboBox::SetInsertionPointEnd()
434 long wxComboBox::GetInsertionPoint() const
440 long wxComboBox::GetLastPosition() const
446 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
451 void wxComboBox::Remove(long from
, long to
)
456 void wxComboBox::SetSelection(long from
, long to
)
461 int wxComboBox::DoAppend(const wxString
& item
)
463 return m_choice
->DoAppend( item
) ;
466 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
468 return m_choice
->DoInsert( item
, pos
) ;
471 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
473 return m_choice
->DoSetItemClientData( n
, clientData
) ;
476 void* wxComboBox::DoGetItemClientData(int n
) const
478 return m_choice
->DoGetItemClientData( n
) ;
481 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
483 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
486 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
488 return m_choice
->DoGetItemClientObject( n
) ;
491 void wxComboBox::FreeData()
493 if ( HasClientObjectData() )
495 size_t count
= GetCount();
496 for ( size_t n
= 0; n
< count
; n
++ )
498 SetClientObject( n
, NULL
);
503 void wxComboBox::Delete(int n
)
505 // force client object deletion
506 if( HasClientObjectData() )
507 SetClientObject( n
, NULL
);
508 m_choice
->Delete( n
);
511 void wxComboBox::Clear()
517 int wxComboBox::GetSelection() const
519 return m_choice
->GetSelection();
522 void wxComboBox::SetSelection(int n
)
524 m_choice
->SetSelection( n
);
526 if ( m_text
!= NULL
)
528 m_text
->SetValue( GetString( n
) );
532 int wxComboBox::FindString(const wxString
& s
) const
534 return m_choice
->FindString( s
);
537 wxString
wxComboBox::GetString(int n
) const
539 return m_choice
->GetString( n
);
542 wxString
wxComboBox::GetStringSelection() const
544 int sel
= GetSelection ();
546 return wxString(this->GetString (sel
));
548 return wxEmptyString
;
551 bool wxComboBox::SetStringSelection(const wxString
& sel
)
553 int s
= FindString (sel
);
563 void wxComboBox::SetString(int n
, const wxString
& s
)
565 m_choice
->SetString( n
, s
) ;
569 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
571 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
572 event
.SetInt(GetSelection());
573 event
.SetEventObject(this);
574 event
.SetString(GetStringSelection());
575 ProcessCommand(event
);