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();
64 m_cb
->DelegateTextChanged( s
);
75 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
76 EVT_TEXT(-1, wxComboBoxText::OnTextChange
)
79 class wxComboBoxChoice
: public wxChoice
82 wxComboBoxChoice(wxComboBox
*cb
, int style
)
83 : wxChoice( cb
->GetParent(), 1 )
89 void OnChoice( wxCommandEvent
& e
)
91 wxString s
= e
.GetString();
93 m_cb
->DelegateChoice( s
);
102 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
103 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
109 wxComboBox::~wxComboBox()
111 // delete the controls now, don't leave them alive even though they woudl
112 // still be eventually deleted by our parent - but it will be too late, the
113 // user code expects them to be gone now
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxSize
wxComboBox::DoGetBestSize() const
125 wxSize size
= m_choice
->GetBestSize();
129 wxSize sizeText
= m_text
->GetBestSize();
131 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
137 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
138 height
= POPUPHEIGHT
;
140 wxControl::DoMoveWindow(x
, y
, width
, height
);
144 m_choice
->SetSize(x
, y
, width
, -1);
148 wxCoord wText
= width
- POPUPWIDTH
;
149 m_text
->SetSize(x
, y
, wText
, height
);
150 m_choice
->SetSize(x
+ wText
+ MARGIN
, y
, POPUPWIDTH
, -1);
156 // ----------------------------------------------------------------------------
157 // operations forwarded to the subcontrols
158 // ----------------------------------------------------------------------------
160 bool wxComboBox::Enable(bool enable
)
162 if ( !wxControl::Enable(enable
) )
165 m_choice
->Enable(enable
);
169 m_text
->Enable(enable
);
175 bool wxComboBox::Show(bool show
)
177 if ( !wxControl::Show(show
) )
180 // under GTK Show() is called the first time before we are fully
184 m_choice
->Show(show
);
195 void wxComboBox::DelegateTextChanged( const wxString
& value
) {
199 void wxComboBox::DelegateChoice( const wxString
& value
)
201 SetStringSelection( value
);
205 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
206 const wxString
& value
,
209 int n
, const wxString choices
[],
211 const wxValidator
& validator
,
212 const wxString
& name
)
218 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
219 wxDefaultValidator
, name
) )
224 m_choice
= new wxComboBoxChoice(this, style
);
227 if ( style
& wxCB_READONLY
)
233 m_text
= new wxComboBoxText(this);
234 if ( size
.y
== -1 ) {
235 csize
.y
= m_text
->GetSize().y
;
239 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
240 for ( int i
= 0 ; i
< n
; i
++ )
242 m_choice
->DoAppend( choices
[ i
] );
245 // have to disable this window to avoid interfering it with message
246 // processing to the text and the button... but pretend it is enabled to
247 // make IsEnabled() return TRUE
248 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
251 // we don't even need to show this window itself - and not doing it avoids
252 // that it overwrites the text control
253 wxControl::Show(FALSE
);
258 wxString
wxComboBox::GetValue() const
264 result
= m_choice
->GetString( m_choice
->GetSelection() );
268 result
= m_text
->GetValue();
274 void wxComboBox::SetValue(const wxString
& value
)
276 SetStringSelection( value
) ;
279 // Clipboard operations
280 void wxComboBox::Copy()
288 void wxComboBox::Cut()
296 void wxComboBox::Paste()
304 void wxComboBox::SetEditable(bool editable
)
306 if ( ( m_text
== 0 ) && editable
)
308 m_text
= new wxComboBoxText( this );
310 else if ( ( m_text
!= 0 ) && !editable
)
316 int currentX
, currentY
;
317 GetPosition( ¤tX
, ¤tY
);
319 int currentW
, currentH
;
320 GetSize( ¤tW
, ¤tH
);
322 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
325 void wxComboBox::SetInsertionPoint(long pos
)
330 void wxComboBox::SetInsertionPointEnd()
335 long wxComboBox::GetInsertionPoint() const
341 long wxComboBox::GetLastPosition() const
347 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
352 void wxComboBox::Remove(long from
, long to
)
357 void wxComboBox::SetSelection(long from
, long to
)
362 void wxComboBox::Append(const wxString
& item
)
364 m_choice
->DoAppend( item
);
367 void wxComboBox::Delete(int n
)
369 m_choice
->Delete( n
);
372 void wxComboBox::Clear()
377 int wxComboBox::GetSelection() const
379 return m_choice
->GetSelection();
382 void wxComboBox::SetSelection(int n
)
384 m_choice
->SetSelection( n
);
388 m_text
->SetValue( GetString( n
) );
392 int wxComboBox::FindString(const wxString
& s
) const
394 return m_choice
->FindString( s
);
397 wxString
wxComboBox::GetString(int n
) const
399 return m_choice
->GetString( n
);
402 wxString
wxComboBox::GetStringSelection() const
404 int sel
= GetSelection ();
406 return wxString(this->GetString (sel
));
411 bool wxComboBox::SetStringSelection(const wxString
& sel
)
413 int s
= FindString (sel
);
423 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
425 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
426 event
.SetInt(GetSelection());
427 event
.SetEventObject(this);
428 event
.SetString(GetStringSelection());
429 ProcessCommand(event
);