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
->GetParent(), 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
)
84 : wxChoice( cb
->GetParent(), 1 )
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
)
110 wxComboBox::~wxComboBox()
112 // delete the controls now, don't leave them alive even though they woudl
113 // still be eventually deleted by our parent - but it will be too late, the
114 // user code expects them to be gone now
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 wxSize
wxComboBox::DoGetBestSize() const
126 wxSize size
= m_choice
->GetBestSize();
130 wxSize sizeText
= m_text
->GetBestSize();
132 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
138 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
139 height
= POPUPHEIGHT
;
141 wxControl::DoMoveWindow(x
, y
, width
, height
);
145 m_choice
->SetSize(x
, y
, width
, -1);
149 wxCoord wText
= width
- POPUPWIDTH
;
150 m_text
->SetSize(x
, y
, wText
, height
);
151 m_choice
->SetSize(x
+ wText
+ MARGIN
, y
, POPUPWIDTH
, -1);
157 // ----------------------------------------------------------------------------
158 // operations forwarded to the subcontrols
159 // ----------------------------------------------------------------------------
161 bool wxComboBox::Enable(bool enable
)
163 if ( !wxControl::Enable(enable
) )
166 m_choice
->Enable(enable
);
170 m_text
->Enable(enable
);
176 bool wxComboBox::Show(bool show
)
178 if ( !wxControl::Show(show
) )
181 // under GTK Show() is called the first time before we are fully
185 m_choice
->Show(show
);
195 void wxComboBox::SetFocus()
201 void wxComboBox::DelegateTextChanged( const wxString
& value
) {
205 void wxComboBox::DelegateChoice( const wxString
& value
)
207 SetStringSelection( value
);
211 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
212 const wxString
& value
,
215 int n
, const wxString choices
[],
217 const wxValidator
& validator
,
218 const wxString
& name
)
224 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
225 wxDefaultValidator
, name
) )
230 m_choice
= new wxComboBoxChoice(this, style
);
233 if ( style
& wxCB_READONLY
)
239 m_text
= new wxComboBoxText(this);
240 if ( size
.y
== -1 ) {
241 csize
.y
= m_text
->GetSize().y
;
245 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
246 for ( int i
= 0 ; i
< n
; i
++ )
248 m_choice
->DoAppend( choices
[ i
] );
251 // have to disable this window to avoid interfering it with message
252 // processing to the text and the button... but pretend it is enabled to
253 // make IsEnabled() return TRUE
254 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
257 // we don't even need to show this window itself - and not doing it avoids
258 // that it overwrites the text control
259 wxControl::Show(FALSE
);
264 wxString
wxComboBox::GetValue() const
270 result
= m_choice
->GetString( m_choice
->GetSelection() );
274 result
= m_text
->GetValue();
280 void wxComboBox::SetValue(const wxString
& value
)
282 SetStringSelection( value
) ;
285 // Clipboard operations
286 void wxComboBox::Copy()
294 void wxComboBox::Cut()
302 void wxComboBox::Paste()
310 void wxComboBox::SetEditable(bool editable
)
312 if ( ( m_text
== 0 ) && editable
)
314 m_text
= new wxComboBoxText( this );
316 else if ( ( m_text
!= 0 ) && !editable
)
322 int currentX
, currentY
;
323 GetPosition( ¤tX
, ¤tY
);
325 int currentW
, currentH
;
326 GetSize( ¤tW
, ¤tH
);
328 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
331 void wxComboBox::SetInsertionPoint(long pos
)
336 void wxComboBox::SetInsertionPointEnd()
341 long wxComboBox::GetInsertionPoint() const
347 long wxComboBox::GetLastPosition() const
353 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
358 void wxComboBox::Remove(long from
, long to
)
363 void wxComboBox::SetSelection(long from
, long to
)
368 void wxComboBox::Append(const wxString
& item
)
370 // I am not sure what other ports do,
371 // but wxMac chokes on empty entries.
374 m_choice
->DoAppend( item
);
377 void wxComboBox::Delete(int n
)
379 m_choice
->Delete( n
);
382 void wxComboBox::Clear()
387 int wxComboBox::GetSelection() const
389 return m_choice
->GetSelection();
392 void wxComboBox::SetSelection(int n
)
394 m_choice
->SetSelection( n
);
398 m_text
->SetValue( GetString( n
) );
402 int wxComboBox::FindString(const wxString
& s
) const
404 return m_choice
->FindString( s
);
407 wxString
wxComboBox::GetString(int n
) const
409 return m_choice
->GetString( n
);
412 wxString
wxComboBox::GetStringSelection() const
414 int sel
= GetSelection ();
416 return wxString(this->GetString (sel
));
421 bool wxComboBox::SetStringSelection(const wxString
& sel
)
423 int s
= FindString (sel
);
433 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
435 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
436 event
.SetInt(GetSelection());
437 event
.SetEventObject(this);
438 event
.SetString(GetStringSelection());
439 ProcessCommand(event
);