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 )
65 void OnChar( wxKeyEvent
& event
)
67 if ( event
.GetKeyCode() == WXK_RETURN
)
69 wxString value
= GetValue();
71 if ( m_cb
->GetCount() == 0 )
73 // make Enter generate "selected" event if there is only one item
74 // in the combobox - without it, it's impossible to select it at
76 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
78 event
.SetString( value
);
79 event
.SetEventObject( m_cb
);
80 m_cb
->GetEventHandler()->ProcessEvent( event
);
84 // add the item to the list if it's not there yet
85 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
88 m_cb
->SetStringSelection(value
);
90 // and generate the selected event for it
91 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
92 event
.SetInt( m_cb
->GetCount() - 1 );
93 event
.SetString( value
);
94 event
.SetEventObject( m_cb
);
95 m_cb
->GetEventHandler()->ProcessEvent( event
);
98 // This will invoke the dialog default action, such
99 // as the clicking the default button.
101 wxWindow
*parent
= GetParent();
102 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
103 parent
= parent
->GetParent() ;
105 if ( parent
&& parent
->GetDefaultItem() )
107 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
109 if ( def
&& def
->IsEnabled() )
111 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
112 event
.SetEventObject(def
);
128 DECLARE_EVENT_TABLE()
131 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
132 EVT_CHAR( wxComboBoxText::OnChar
)
135 class wxComboBoxChoice
: public wxChoice
138 wxComboBoxChoice(wxComboBox
*cb
, int style
)
145 void OnChoice( wxCommandEvent
& e
)
147 wxString s
= e
.GetString();
149 m_cb
->DelegateChoice( s
);
150 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
151 event2
.SetInt(m_cb
->GetSelection());
152 event2
.SetEventObject(m_cb
);
153 event2
.SetString(m_cb
->GetStringSelection());
154 m_cb
->ProcessCommand(event2
);
156 virtual wxSize
DoGetBestSize() const
158 wxSize sz
= wxChoice::DoGetBestSize() ;
166 DECLARE_EVENT_TABLE()
169 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
170 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
173 wxComboBox::~wxComboBox()
175 // delete client objects
178 // delete the controls now, don't leave them alive even though they would
179 // still be eventually deleted by our parent - but it will be too late, the
180 // user code expects them to be gone now
181 if (m_text
!= NULL
) {
185 if (m_choice
!= NULL
) {
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 wxSize
wxComboBox::DoGetBestSize() const
198 wxSize size
= m_choice
->GetBestSize();
200 if ( m_text
!= NULL
)
202 wxSize sizeText
= m_text
->GetBestSize();
204 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
210 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
211 height
= POPUPHEIGHT
;
213 wxControl::DoMoveWindow(x
, y
, width
, height
);
215 if ( m_text
== NULL
)
217 // we might not be fully constructed yet, therefore watch out...
219 m_choice
->SetSize(0, 0 , width
, -1);
223 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
224 m_text
->SetSize(0, 0, wText
, height
);
225 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
231 // ----------------------------------------------------------------------------
232 // operations forwarded to the subcontrols
233 // ----------------------------------------------------------------------------
235 bool wxComboBox::Enable(bool enable
)
237 if ( !wxControl::Enable(enable
) )
243 bool wxComboBox::Show(bool show
)
245 if ( !wxControl::Show(show
) )
251 void wxComboBox::SetFocus()
253 if ( m_text
!= NULL
) {
259 void wxComboBox::DelegateTextChanged( const wxString
& value
)
261 SetStringSelection( value
);
265 void wxComboBox::DelegateChoice( const wxString
& value
)
267 SetStringSelection( value
);
271 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
272 const wxString
& value
,
275 const wxArrayString
& choices
,
277 const wxValidator
& validator
,
278 const wxString
& name
)
280 wxCArrayString
chs( choices
);
282 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
283 chs
.GetStrings(), style
, validator
, name
);
287 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
288 const wxString
& value
,
291 int n
, const wxString choices
[],
293 const wxValidator
& validator
,
294 const wxString
& name
)
296 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
297 wxDefaultValidator
, name
) )
302 m_choice
= new wxComboBoxChoice(this, style
);
303 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
305 if ( style
& wxCB_READONLY
)
311 m_text
= new wxComboBoxText(this);
312 if ( size
.y
== -1 ) {
313 csize
.y
= m_text
->GetSize().y
;
317 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
319 for ( int i
= 0 ; i
< n
; i
++ )
321 m_choice
->DoAppend( choices
[ i
] );
324 SetBestSize(csize
); // Needed because it is a wxControlWithItems
329 wxString
wxComboBox::GetValue() const
333 if ( m_text
== NULL
)
335 result
= m_choice
->GetString( m_choice
->GetSelection() );
339 result
= m_text
->GetValue();
345 void wxComboBox::SetValue(const wxString
& value
)
347 int s
= FindString (value
);
348 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
350 m_choice
->Append(value
) ;
352 SetStringSelection( value
) ;
355 // Clipboard operations
356 void wxComboBox::Copy()
358 if ( m_text
!= NULL
)
364 void wxComboBox::Cut()
366 if ( m_text
!= NULL
)
372 void wxComboBox::Paste()
374 if ( m_text
!= NULL
)
380 void wxComboBox::SetEditable(bool editable
)
382 if ( ( m_text
== NULL
) && editable
)
384 m_text
= new wxComboBoxText( this );
386 else if ( ( m_text
!= NULL
) && !editable
)
392 int currentX
, currentY
;
393 GetPosition( ¤tX
, ¤tY
);
395 int currentW
, currentH
;
396 GetSize( ¤tW
, ¤tH
);
398 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
401 void wxComboBox::SetInsertionPoint(long pos
)
406 void wxComboBox::SetInsertionPointEnd()
411 long wxComboBox::GetInsertionPoint() const
417 long wxComboBox::GetLastPosition() const
423 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
428 void wxComboBox::Remove(long from
, long to
)
433 void wxComboBox::SetSelection(long from
, long to
)
438 int wxComboBox::DoAppend(const wxString
& item
)
440 return m_choice
->DoAppend( item
) ;
443 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
445 return m_choice
->DoInsert( item
, pos
) ;
448 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
450 return m_choice
->DoSetItemClientData( n
, clientData
) ;
453 void* wxComboBox::DoGetItemClientData(int n
) const
455 return m_choice
->DoGetItemClientData( n
) ;
458 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
460 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
463 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
465 return m_choice
->DoGetItemClientObject( n
) ;
468 void wxComboBox::FreeData()
470 if ( HasClientObjectData() )
472 size_t count
= GetCount();
473 for ( size_t n
= 0; n
< count
; n
++ )
475 SetClientObject( n
, NULL
);
480 void wxComboBox::Delete(int n
)
482 // force client object deletion
483 if( HasClientObjectData() )
484 SetClientObject( n
, NULL
);
485 m_choice
->Delete( n
);
488 void wxComboBox::Clear()
494 int wxComboBox::GetSelection() const
496 return m_choice
->GetSelection();
499 void wxComboBox::SetSelection(int n
)
501 m_choice
->SetSelection( n
);
503 if ( m_text
!= NULL
)
505 m_text
->SetValue( GetString( n
) );
509 int wxComboBox::FindString(const wxString
& s
) const
511 return m_choice
->FindString( s
);
514 wxString
wxComboBox::GetString(int n
) const
516 return m_choice
->GetString( n
);
519 wxString
wxComboBox::GetStringSelection() const
521 int sel
= GetSelection ();
523 return wxString(this->GetString (sel
));
525 return wxEmptyString
;
528 bool wxComboBox::SetStringSelection(const wxString
& sel
)
530 int s
= FindString (sel
);
540 void wxComboBox::SetString(int n
, const wxString
& s
)
542 m_choice
->SetString( n
, s
) ;
546 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
548 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
549 event
.SetInt(GetSelection());
550 event
.SetEventObject(this);
551 event
.SetString(GetStringSelection());
552 ProcessCommand(event
);