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();
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(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()
186 void wxComboBox::DelegateTextChanged( const wxString
& value
) {
190 void wxComboBox::DelegateChoice( const wxString
& value
)
192 SetStringSelection( value
);
196 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
197 const wxString
& value
,
200 int n
, const wxString choices
[],
202 const wxValidator
& validator
,
203 const wxString
& name
)
209 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
210 wxDefaultValidator
, name
) )
215 m_choice
= new wxComboBoxChoice(this, style
);
218 if ( style
& wxCB_READONLY
)
224 m_text
= new wxComboBoxText(this);
225 if ( size
.y
== -1 ) {
226 csize
.y
= m_text
->GetSize().y
;
230 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
232 for ( int i
= 0 ; i
< n
; i
++ )
234 m_choice
->DoAppend( choices
[ i
] );
240 wxString
wxComboBox::GetValue() const
246 result
= m_choice
->GetString( m_choice
->GetSelection() );
250 result
= m_text
->GetValue();
256 void wxComboBox::SetValue(const wxString
& value
)
258 SetStringSelection( value
) ;
261 // Clipboard operations
262 void wxComboBox::Copy()
270 void wxComboBox::Cut()
278 void wxComboBox::Paste()
286 void wxComboBox::SetEditable(bool editable
)
288 if ( ( m_text
== 0 ) && editable
)
290 m_text
= new wxComboBoxText( this );
292 else if ( ( m_text
!= 0 ) && !editable
)
298 int currentX
, currentY
;
299 GetPosition( ¤tX
, ¤tY
);
301 int currentW
, currentH
;
302 GetSize( ¤tW
, ¤tH
);
304 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
307 void wxComboBox::SetInsertionPoint(long pos
)
312 void wxComboBox::SetInsertionPointEnd()
317 long wxComboBox::GetInsertionPoint() const
323 long wxComboBox::GetLastPosition() const
329 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
334 void wxComboBox::Remove(long from
, long to
)
339 void wxComboBox::SetSelection(long from
, long to
)
344 void wxComboBox::Append(const wxString
& item
)
346 // I am not sure what other ports do,
347 // but wxMac chokes on empty entries.
350 m_choice
->DoAppend( item
);
353 void wxComboBox::Delete(int n
)
355 m_choice
->Delete( n
);
358 void wxComboBox::Clear()
363 int wxComboBox::GetSelection() const
365 return m_choice
->GetSelection();
368 void wxComboBox::SetSelection(int n
)
370 m_choice
->SetSelection( n
);
374 m_text
->SetValue( GetString( n
) );
378 int wxComboBox::FindString(const wxString
& s
) const
380 return m_choice
->FindString( s
);
383 wxString
wxComboBox::GetString(int n
) const
385 return m_choice
->GetString( n
);
388 wxString
wxComboBox::GetStringSelection() const
390 int sel
= GetSelection ();
392 return wxString(this->GetString (sel
));
397 bool wxComboBox::SetStringSelection(const wxString
& sel
)
399 int s
= FindString (sel
);
409 void wxComboBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
411 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
412 event
.SetInt(GetSelection());
413 event
.SetEventObject(this);
414 event
.SetString(GetStringSelection());
415 ProcessCommand(event
);