1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/combobox.h"
17 #include "wx/button.h"
21 #include "wx/mac/uma.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
25 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
28 static int nextPopUpMenuId
= 1000 ;
29 MenuHandle
NewUniqueMenu()
31 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the margin between the text control and the choice
42 static const wxCoord MARGIN
= 2;
43 static const int POPUPWIDTH
= 18;
44 static const int POPUPHEIGHT
= 23;
47 // ----------------------------------------------------------------------------
48 // wxComboBoxText: text control forwards events to combobox
49 // ----------------------------------------------------------------------------
51 class wxComboBoxText
: public wxTextCtrl
54 wxComboBoxText( wxComboBox
* cb
)
55 : wxTextCtrl( cb
, 1 )
61 void OnChar( wxKeyEvent
& event
)
63 if ( event
.GetKeyCode() == WXK_RETURN
)
65 wxString value
= GetValue();
67 if ( m_cb
->GetCount() == 0 )
69 // make Enter generate "selected" event if there is only one item
70 // in the combobox - without it, it's impossible to select it at
72 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
74 event
.SetString( value
);
75 event
.SetEventObject( m_cb
);
76 m_cb
->GetEventHandler()->ProcessEvent( event
);
80 // add the item to the list if it's not there yet
81 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
84 m_cb
->SetStringSelection(value
);
86 // and generate the selected event for it
87 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
88 event
.SetInt( m_cb
->GetCount() - 1 );
89 event
.SetString( value
);
90 event
.SetEventObject( m_cb
);
91 m_cb
->GetEventHandler()->ProcessEvent( event
);
94 // This will invoke the dialog default action, such
95 // as the clicking the default button.
97 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
98 if ( tlw
&& tlw
->GetDefaultItem() )
100 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
101 if ( def
&& def
->IsEnabled() )
103 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
104 event
.SetEventObject(def
);
120 DECLARE_EVENT_TABLE()
123 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
124 EVT_CHAR( wxComboBoxText::OnChar
)
127 class wxComboBoxChoice
: public wxChoice
130 wxComboBoxChoice(wxComboBox
*cb
, int style
)
137 void OnChoice( wxCommandEvent
& e
)
139 wxString s
= e
.GetString();
141 m_cb
->DelegateChoice( s
);
142 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
143 event2
.SetInt(m_cb
->GetSelection());
144 event2
.SetEventObject(m_cb
);
145 event2
.SetString(m_cb
->GetStringSelection());
146 m_cb
->ProcessCommand(event2
);
152 DECLARE_EVENT_TABLE()
155 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
156 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
159 wxComboBox::~wxComboBox()
161 // delete client objects
164 // delete the controls now, don't leave them alive even though they would
165 // still be eventually deleted by our parent - but it will be too late, the
166 // user code expects them to be gone now
167 if (m_text
!= NULL
) {
171 if (m_choice
!= NULL
) {
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 wxSize
wxComboBox::DoGetBestSize() const
184 wxSize size
= m_choice
->GetBestSize();
186 if ( m_text
!= NULL
)
188 wxSize sizeText
= m_text
->GetBestSize();
190 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
196 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
197 height
= POPUPHEIGHT
;
199 wxControl::DoMoveWindow(x
, y
, width
, height
);
201 if ( m_text
== NULL
)
203 m_choice
->SetSize(0, 0 , width
, -1);
207 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
208 m_text
->SetSize(0, 0, wText
, height
);
209 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
215 // ----------------------------------------------------------------------------
216 // operations forwarded to the subcontrols
217 // ----------------------------------------------------------------------------
219 bool wxComboBox::Enable(bool enable
)
221 if ( !wxControl::Enable(enable
) )
227 bool wxComboBox::Show(bool show
)
229 if ( !wxControl::Show(show
) )
235 void wxComboBox::SetFocus()
237 if ( m_text
!= NULL
) {
243 void wxComboBox::DelegateTextChanged( const wxString
& value
)
245 SetStringSelection( value
);
249 void wxComboBox::DelegateChoice( const wxString
& value
)
251 SetStringSelection( value
);
255 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
256 const wxString
& value
,
259 const wxArrayString
& choices
,
261 const wxValidator
& validator
,
262 const wxString
& name
)
264 wxCArrayString
chs( choices
);
266 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
267 chs
.GetStrings(), style
, validator
, name
);
271 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
272 const wxString
& value
,
275 int n
, const wxString choices
[],
277 const wxValidator
& validator
,
278 const wxString
& name
)
280 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
281 wxDefaultValidator
, name
) )
286 m_choice
= new wxComboBoxChoice(this, style
);
289 if ( style
& wxCB_READONLY
)
295 m_text
= new wxComboBoxText(this);
296 if ( size
.y
== -1 ) {
297 csize
.y
= m_text
->GetSize().y
;
301 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
303 for ( int i
= 0 ; i
< n
; i
++ )
305 m_choice
->DoAppend( choices
[ i
] );
311 wxString
wxComboBox::GetValue() const
315 if ( m_text
== NULL
)
317 result
= m_choice
->GetString( m_choice
->GetSelection() );
321 result
= m_text
->GetValue();
327 void wxComboBox::SetValue(const wxString
& value
)
329 int s
= FindString (value
);
330 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
332 m_choice
->Append(value
) ;
334 SetStringSelection( value
) ;
337 // Clipboard operations
338 void wxComboBox::Copy()
340 if ( m_text
!= NULL
)
346 void wxComboBox::Cut()
348 if ( m_text
!= NULL
)
354 void wxComboBox::Paste()
356 if ( m_text
!= NULL
)
362 void wxComboBox::SetEditable(bool editable
)
364 if ( ( m_text
== NULL
) && editable
)
366 m_text
= new wxComboBoxText( this );
368 else if ( ( m_text
!= NULL
) && !editable
)
374 int currentX
, currentY
;
375 GetPosition( ¤tX
, ¤tY
);
377 int currentW
, currentH
;
378 GetSize( ¤tW
, ¤tH
);
380 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
383 void wxComboBox::SetInsertionPoint(long pos
)
388 void wxComboBox::SetInsertionPointEnd()
393 long wxComboBox::GetInsertionPoint() const
399 wxTextPos
wxComboBox::GetLastPosition() const
405 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
410 void wxComboBox::Remove(long from
, long to
)
415 void wxComboBox::SetSelection(long from
, long to
)
420 int wxComboBox::DoAppend(const wxString
& item
)
422 return m_choice
->DoAppend( item
) ;
425 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
427 return m_choice
->DoInsert( item
, pos
) ;
430 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
432 return m_choice
->DoSetItemClientData( n
, clientData
) ;
435 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
437 return m_choice
->DoGetItemClientData( n
) ;
440 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
442 return m_choice
->DoSetItemClientObject(n
, clientData
);
445 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
447 return m_choice
->DoGetItemClientObject( n
) ;
450 void wxComboBox::FreeData()
452 if ( HasClientObjectData() )
454 unsigned int count
= GetCount();
455 for ( unsigned int n
= 0; n
< count
; n
++ )
457 SetClientObject( n
, NULL
);
462 void wxComboBox::Delete(unsigned int n
)
464 // force client object deletion
465 if( HasClientObjectData() )
466 SetClientObject( n
, NULL
);
467 m_choice
->Delete( n
);
470 void wxComboBox::Clear()
476 int wxComboBox::GetSelection() const
478 return m_choice
->GetSelection();
481 void wxComboBox::SetSelection(int n
)
483 m_choice
->SetSelection( n
);
485 if ( m_text
!= NULL
)
487 m_text
->SetValue(GetString(n
));
491 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
493 return m_choice
->FindString( s
, bCase
);
496 wxString
wxComboBox::GetString(unsigned int n
) const
498 return m_choice
->GetString( n
);
501 wxString
wxComboBox::GetStringSelection() const
503 int sel
= GetSelection ();
505 if (sel
!= wxNOT_FOUND
)
506 return wxString(this->GetString((unsigned int)sel
));
508 return wxEmptyString
;
511 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
513 m_choice
->SetString( n
, s
);
516 bool wxComboBox::IsEditable() const
518 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
521 void wxComboBox::Undo()
527 void wxComboBox::Redo()
533 void wxComboBox::SelectAll()
539 bool wxComboBox::CanCopy() const
542 return m_text
->CanCopy();
547 bool wxComboBox::CanCut() const
550 return m_text
->CanCut();
555 bool wxComboBox::CanPaste() const
558 return m_text
->CanPaste();
563 bool wxComboBox::CanUndo() const
566 return m_text
->CanUndo();
571 bool wxComboBox::CanRedo() const
574 return m_text
->CanRedo();
579 void wxComboBox::MacHandleControlClick( WXWidget
WXUNUSED(control
) , wxInt16
WXUNUSED(controlpart
) , bool WXUNUSED(mouseStillDown
))
581 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
582 event
.SetInt(GetSelection());
583 event
.SetEventObject(this);
584 event
.SetString(GetStringSelection());
585 ProcessCommand(event
);