1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 #include "wx/combobox.h"
18 #include "wx/mac/uma.h"
20 #if !USE_SHARED_LIBRARY
21 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
24 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
27 static int nextPopUpMenuId
= 1000 ;
28 MenuHandle
NewUniqueMenu()
30 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the margin between the text control and the choice
41 static const wxCoord MARGIN
= 2;
42 static const int POPUPWIDTH
= 18;
43 static const int POPUPHEIGHT
= 23;
46 // ----------------------------------------------------------------------------
47 // wxComboBoxText: text control forwards events to combobox
48 // ----------------------------------------------------------------------------
50 class wxComboBoxText
: public wxTextCtrl
53 wxComboBoxText( wxComboBox
* cb
)
54 : wxTextCtrl( cb
, 1 )
60 void OnChar( wxKeyEvent
& event
)
62 if ( event
.KeyCode() == WXK_RETURN
)
64 wxString value
= GetValue();
66 if ( m_cb
->GetCount() == 0 )
68 // make Enter generate "selected" event if there is only one item
69 // in the combobox - without it, it's impossible to select it at
71 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
73 event
.SetString( value
);
74 event
.SetEventObject( m_cb
);
75 m_cb
->GetEventHandler()->ProcessEvent( event
);
79 // add the item to the list if it's not there yet
80 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
83 m_cb
->SetStringSelection(value
);
85 // and generate the selected event for it
86 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
87 event
.SetInt( m_cb
->GetCount() - 1 );
88 event
.SetString( value
);
89 event
.SetEventObject( m_cb
);
90 m_cb
->GetEventHandler()->ProcessEvent( event
);
93 // This will invoke the dialog default action, such
94 // as the clicking the default button.
96 wxWindow
*parent
= GetParent();
97 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
98 parent
= parent
->GetParent() ;
100 if ( parent
&& parent
->GetDefaultItem() )
102 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
104 if ( def
&& def
->IsEnabled() )
106 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
107 event
.SetEventObject(def
);
123 DECLARE_EVENT_TABLE()
126 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
127 EVT_CHAR( wxComboBoxText::OnChar
)
130 class wxComboBoxChoice
: public wxChoice
133 wxComboBoxChoice(wxComboBox
*cb
, int style
)
140 void OnChoice( wxCommandEvent
& e
)
142 wxString s
= e
.GetString();
144 m_cb
->DelegateChoice( s
);
145 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
146 event2
.SetInt(m_cb
->GetSelection());
147 event2
.SetEventObject(m_cb
);
148 event2
.SetString(m_cb
->GetStringSelection());
149 m_cb
->ProcessCommand(event2
);
155 DECLARE_EVENT_TABLE()
158 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
159 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
162 wxComboBox::~wxComboBox()
164 // delete the controls now, don't leave them alive even though they would
165 // still be eventually deleted by our parent - but it will be too late, the
166 // user code expects them to be gone now
167 if (m_text
!= NULL
) {
171 if (m_choice
!= NULL
) {
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 wxSize
wxComboBox::DoGetBestSize() const
184 wxSize size
= m_choice
->GetBestSize();
186 if ( m_text
!= NULL
)
188 wxSize sizeText
= m_text
->GetBestSize();
190 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
196 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
197 height
= POPUPHEIGHT
;
199 wxControl::DoMoveWindow(x
, y
, width
, height
);
201 if ( m_text
== NULL
)
203 m_choice
->SetSize(0, 0 , width
, -1);
207 wxCoord wText
= width
- POPUPWIDTH
;
208 m_text
->SetSize(0, 0, wText
, height
);
209 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
215 // ----------------------------------------------------------------------------
216 // operations forwarded to the subcontrols
217 // ----------------------------------------------------------------------------
219 bool wxComboBox::Enable(bool enable
)
221 if ( !wxControl::Enable(enable
) )
227 bool wxComboBox::Show(bool show
)
229 if ( !wxControl::Show(show
) )
235 void wxComboBox::SetFocus()
237 if ( m_text
!= NULL
) {
243 void wxComboBox::DelegateTextChanged( const wxString
& value
)
245 SetStringSelection( value
);
249 void wxComboBox::DelegateChoice( const wxString
& value
)
251 SetStringSelection( value
);
255 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
256 const wxString
& value
,
259 int n
, const wxString choices
[],
261 const wxValidator
& validator
,
262 const wxString
& name
)
268 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
269 wxDefaultValidator
, name
) )
274 m_choice
= new wxComboBoxChoice(this, style
);
277 if ( style
& wxCB_READONLY
)
283 m_text
= new wxComboBoxText(this);
284 if ( size
.y
== -1 ) {
285 csize
.y
= m_text
->GetSize().y
;
289 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
291 for ( int i
= 0 ; i
< n
; i
++ )
293 m_choice
->DoAppend( choices
[ i
] );
299 wxString
wxComboBox::GetValue() const
303 if ( m_text
== NULL
)
305 result
= m_choice
->GetString( m_choice
->GetSelection() );
309 result
= m_text
->GetValue();
315 void wxComboBox::SetValue(const wxString
& value
)
317 SetStringSelection( value
) ;
320 // Clipboard operations
321 void wxComboBox::Copy()
323 if ( m_text
!= NULL
)
329 void wxComboBox::Cut()
331 if ( m_text
!= NULL
)
337 void wxComboBox::Paste()
339 if ( m_text
!= NULL
)
345 void wxComboBox::SetEditable(bool editable
)
347 if ( ( m_text
== NULL
) && editable
)
349 m_text
= new wxComboBoxText( this );
351 else if ( ( m_text
!= NULL
) && !editable
)
357 int currentX
, currentY
;
358 GetPosition( ¤tX
, ¤tY
);
360 int currentW
, currentH
;
361 GetSize( ¤tW
, ¤tH
);
363 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
366 void wxComboBox::SetInsertionPoint(long pos
)
371 void wxComboBox::SetInsertionPointEnd()
376 long wxComboBox::GetInsertionPoint() const
382 long wxComboBox::GetLastPosition() const
388 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
393 void wxComboBox::Remove(long from
, long to
)
398 void wxComboBox::SetSelection(long from
, long to
)
403 void wxComboBox::Append(const wxString
& item
)
405 // I am not sure what other ports do,
406 // but wxMac chokes on empty entries.
409 m_choice
->DoAppend( item
);
412 void wxComboBox::Delete(int n
)
414 m_choice
->Delete( n
);
417 void wxComboBox::Clear()
422 int wxComboBox::GetSelection() const
424 return m_choice
->GetSelection();
427 void wxComboBox::SetSelection(int n
)
429 m_choice
->SetSelection( n
);
431 if ( m_text
!= NULL
)
433 m_text
->SetValue( GetString( n
) );
437 int wxComboBox::FindString(const wxString
& s
) const
439 return m_choice
->FindString( s
);
442 wxString
wxComboBox::GetString(int n
) const
444 return m_choice
->GetString( n
);
447 wxString
wxComboBox::GetStringSelection() const
449 int sel
= GetSelection ();
451 return wxString(this->GetString (sel
));
456 bool wxComboBox::SetStringSelection(const wxString
& sel
)
458 int s
= FindString (sel
);
468 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
470 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
471 event
.SetInt(GetSelection());
472 event
.SetEventObject(this);
473 event
.SetString(GetStringSelection());
474 ProcessCommand(event
);