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
)
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();
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
);
148 m_choice
->SetSize(x
, y
, width
, -1);
152 wxCoord wText
= width
- POPUPWIDTH
;
153 m_text
->SetSize(x
, y
, wText
, height
);
154 m_choice
->SetSize(x
+ wText
+ MARGIN
, y
, POPUPWIDTH
, -1);
160 // ----------------------------------------------------------------------------
161 // operations forwarded to the subcontrols
162 // ----------------------------------------------------------------------------
164 bool wxComboBox::Enable(bool enable
)
166 if ( !wxControl::Enable(enable
) )
169 m_choice
->Enable(enable
);
173 m_text
->Enable(enable
);
179 bool wxComboBox::Show(bool show
)
181 if ( !wxControl::Show(show
) )
184 // under GTK Show() is called the first time before we are fully
188 m_choice
->Show(show
);
198 void wxComboBox::SetFocus()
204 void wxComboBox::DelegateTextChanged( const wxString
& value
) {
208 void wxComboBox::DelegateChoice( const wxString
& value
)
210 SetStringSelection( value
);
214 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
215 const wxString
& value
,
218 int n
, const wxString choices
[],
220 const wxValidator
& validator
,
221 const wxString
& name
)
227 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
228 wxDefaultValidator
, name
) )
233 m_choice
= new wxComboBoxChoice(this, style
);
236 if ( style
& wxCB_READONLY
)
242 m_text
= new wxComboBoxText(this);
243 if ( size
.y
== -1 ) {
244 csize
.y
= m_text
->GetSize().y
;
248 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
249 for ( int i
= 0 ; i
< n
; i
++ )
251 m_choice
->DoAppend( choices
[ i
] );
254 // have to disable this window to avoid interfering it with message
255 // processing to the text and the button... but pretend it is enabled to
256 // make IsEnabled() return TRUE
257 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
260 // we don't even need to show this window itself - and not doing it avoids
261 // that it overwrites the text control
262 wxControl::Show(FALSE
);
267 wxString
wxComboBox::GetValue() const
273 result
= m_choice
->GetString( m_choice
->GetSelection() );
277 result
= m_text
->GetValue();
283 void wxComboBox::SetValue(const wxString
& value
)
285 SetStringSelection( value
) ;
288 // Clipboard operations
289 void wxComboBox::Copy()
297 void wxComboBox::Cut()
305 void wxComboBox::Paste()
313 void wxComboBox::SetEditable(bool editable
)
315 if ( ( m_text
== 0 ) && editable
)
317 m_text
= new wxComboBoxText( this );
319 else if ( ( m_text
!= 0 ) && !editable
)
325 int currentX
, currentY
;
326 GetPosition( ¤tX
, ¤tY
);
328 int currentW
, currentH
;
329 GetSize( ¤tW
, ¤tH
);
331 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
334 void wxComboBox::SetInsertionPoint(long pos
)
339 void wxComboBox::SetInsertionPointEnd()
344 long wxComboBox::GetInsertionPoint() const
350 long wxComboBox::GetLastPosition() const
356 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
361 void wxComboBox::Remove(long from
, long to
)
366 void wxComboBox::SetSelection(long from
, long to
)
371 void wxComboBox::Append(const wxString
& item
)
373 // I am not sure what other ports do,
374 // but wxMac chokes on empty entries.
377 m_choice
->DoAppend( item
);
380 void wxComboBox::Delete(int n
)
382 m_choice
->Delete( n
);
385 void wxComboBox::Clear()
390 int wxComboBox::GetSelection() const
392 return m_choice
->GetSelection();
395 void wxComboBox::SetSelection(int n
)
397 m_choice
->SetSelection( n
);
401 m_text
->SetValue( GetString( n
) );
405 int wxComboBox::FindString(const wxString
& s
) const
407 return m_choice
->FindString( s
);
410 wxString
wxComboBox::GetString(int n
) const
412 return m_choice
->GetString( n
);
415 wxString
wxComboBox::GetStringSelection() const
417 int sel
= GetSelection ();
419 return wxString(this->GetString (sel
));
424 bool wxComboBox::SetStringSelection(const wxString
& sel
)
426 int s
= FindString (sel
);
436 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
438 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
439 event
.SetInt(GetSelection());
440 event
.SetEventObject(this);
441 event
.SetString(GetStringSelection());
442 ProcessCommand(event
);