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 OnTextChange( wxCommandEvent
& event
)
62 wxString s
= GetValue();
65 m_cb
->DelegateTextChanged( s
);
76 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
77 EVT_TEXT(-1, wxComboBoxText::OnTextChange
)
80 class wxComboBoxChoice
: public wxChoice
83 wxComboBoxChoice(wxComboBox
*cb
, int style
)
90 void OnChoice( wxCommandEvent
& e
)
92 wxString s
= e
.GetString();
94 m_cb
->DelegateChoice( s
);
100 DECLARE_EVENT_TABLE()
103 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
104 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
107 wxComboBox::~wxComboBox()
109 // delete the controls now, don't leave them alive even though they would
110 // still be eventually deleted by our parent - but it will be too late, the
111 // user code expects them to be gone now
112 if (m_text
!= NULL
) {
116 if (m_choice
!= NULL
) {
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxSize
wxComboBox::DoGetBestSize() const
129 wxSize size
= m_choice
->GetBestSize();
131 if ( m_text
!= NULL
)
133 wxSize sizeText
= m_text
->GetBestSize();
135 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
141 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
142 height
= POPUPHEIGHT
;
144 wxControl::DoMoveWindow(x
, y
, width
, height
);
146 if ( m_text
== NULL
)
148 m_choice
->SetSize(0, 0 , width
, -1);
152 wxCoord wText
= width
- POPUPWIDTH
;
153 m_text
->SetSize(0, 0, wText
, height
);
154 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
160 // ----------------------------------------------------------------------------
161 // operations forwarded to the subcontrols
162 // ----------------------------------------------------------------------------
164 bool wxComboBox::Enable(bool enable
)
166 if ( !wxControl::Enable(enable
) )
172 bool wxComboBox::Show(bool show
)
174 if ( !wxControl::Show(show
) )
180 void wxComboBox::SetFocus()
182 if ( m_text
!= NULL
) {
188 void wxComboBox::DelegateTextChanged( const wxString
& value
)
193 void wxComboBox::DelegateChoice( const wxString
& value
)
195 SetStringSelection( value
);
199 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
200 const wxString
& value
,
203 int n
, const wxString choices
[],
205 const wxValidator
& validator
,
206 const wxString
& name
)
212 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
213 wxDefaultValidator
, name
) )
218 m_choice
= new wxComboBoxChoice(this, style
);
221 if ( style
& wxCB_READONLY
)
227 m_text
= new wxComboBoxText(this);
228 if ( size
.y
== -1 ) {
229 csize
.y
= m_text
->GetSize().y
;
233 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
235 for ( int i
= 0 ; i
< n
; i
++ )
237 m_choice
->DoAppend( choices
[ i
] );
243 wxString
wxComboBox::GetValue() const
247 if ( m_text
== NULL
)
249 result
= m_choice
->GetString( m_choice
->GetSelection() );
253 result
= m_text
->GetValue();
259 void wxComboBox::SetValue(const wxString
& value
)
261 SetStringSelection( value
) ;
264 // Clipboard operations
265 void wxComboBox::Copy()
267 if ( m_text
!= NULL
)
273 void wxComboBox::Cut()
275 if ( m_text
!= NULL
)
281 void wxComboBox::Paste()
283 if ( m_text
!= NULL
)
289 void wxComboBox::SetEditable(bool editable
)
291 if ( ( m_text
== NULL
) && editable
)
293 m_text
= new wxComboBoxText( this );
295 else if ( ( m_text
!= NULL
) && !editable
)
301 int currentX
, currentY
;
302 GetPosition( ¤tX
, ¤tY
);
304 int currentW
, currentH
;
305 GetSize( ¤tW
, ¤tH
);
307 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
310 void wxComboBox::SetInsertionPoint(long pos
)
315 void wxComboBox::SetInsertionPointEnd()
320 long wxComboBox::GetInsertionPoint() const
326 long wxComboBox::GetLastPosition() const
332 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
337 void wxComboBox::Remove(long from
, long to
)
342 void wxComboBox::SetSelection(long from
, long to
)
347 void wxComboBox::Append(const wxString
& item
)
349 // I am not sure what other ports do,
350 // but wxMac chokes on empty entries.
353 m_choice
->DoAppend( item
);
356 void wxComboBox::Delete(int n
)
358 m_choice
->Delete( n
);
361 void wxComboBox::Clear()
366 int wxComboBox::GetSelection() const
368 return m_choice
->GetSelection();
371 void wxComboBox::SetSelection(int n
)
373 m_choice
->SetSelection( n
);
375 if ( m_text
!= NULL
)
377 m_text
->SetValue( GetString( n
) );
381 int wxComboBox::FindString(const wxString
& s
) const
383 return m_choice
->FindString( s
);
386 wxString
wxComboBox::GetString(int n
) const
388 return m_choice
->GetString( n
);
391 wxString
wxComboBox::GetStringSelection() const
393 int sel
= GetSelection ();
395 return wxString(this->GetString (sel
));
400 bool wxComboBox::SetStringSelection(const wxString
& sel
)
402 int s
= FindString (sel
);
412 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
414 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
415 event
.SetInt(GetSelection());
416 event
.SetEventObject(this);
417 event
.SetString(GetStringSelection());
418 ProcessCommand(event
);