1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 #include "wx/combobox.h"
17 #include "wx/button.h"
19 #include "wx/mac/uma.h"
21 #if !USE_SHARED_LIBRARY
22 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 #if TARGET_API_MAC_OSX
43 // margin should be bigger on OS X due to blue highlight
44 // around text control.
45 static const wxCoord MARGIN
= 4;
46 // this is the border a focus rect on OSX is needing
47 static const int TEXTFOCUSBORDER
= 3 ;
49 static const wxCoord MARGIN
= 2;
50 static const int TEXTFOCUSBORDER
= 0 ;
52 static const int POPUPHEIGHT
= 23;
55 // ----------------------------------------------------------------------------
56 // wxComboBoxText: text control forwards events to combobox
57 // ----------------------------------------------------------------------------
59 class wxComboBoxText
: public wxTextCtrl
62 wxComboBoxText( wxComboBox
* cb
)
63 : wxTextCtrl( cb
, 1 )
69 void OnChar( wxKeyEvent
& event
)
71 // Allows processing the tab key to go to the next control
72 if (event
.GetKeyCode() == WXK_TAB
)
74 wxNavigationKeyEvent NavEvent
;
75 NavEvent
.SetEventObject(this);
76 NavEvent
.SetDirection(true);
77 NavEvent
.SetWindowChange(false);
79 // Get the parent of the combo and have it process the navigation?
80 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
83 if ( event
.GetKeyCode() == WXK_RETURN
)
85 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
86 event
.SetString( GetValue() );
87 event
.SetInt( m_cb
->GetSelection() );
88 event
.SetEventObject( m_cb
);
90 // This will invoke the dialog default action, such
91 // as the clicking the default button.
93 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
95 wxWindow
*parent
= GetParent();
96 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
97 parent
= parent
->GetParent() ;
99 if ( parent
&& parent
->GetDefaultItem() )
101 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
103 if ( def
&& def
->IsEnabled() )
105 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
106 event
.SetEventObject(def
);
118 // Use the KeyUp as a naive approximation for TEXT_UPDATED, even though it is somewhat delayed
119 // but this is less complicated than dealing with idle-ness, and is much better than nothing
120 void OnKeyUp( wxKeyEvent
& event
)
122 if ( event
.GetKeyCode() != WXK_RETURN
)
124 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId());
125 event
.SetString( GetValue() );
126 event
.SetEventObject( m_cb
);
127 m_cb
->GetEventHandler()->ProcessEvent(event
);
133 DECLARE_EVENT_TABLE()
136 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
137 EVT_CHAR( wxComboBoxText::OnChar
)
138 EVT_KEY_UP( wxComboBoxText::OnKeyUp
)
141 class wxComboBoxChoice
: public wxChoice
144 wxComboBoxChoice(wxComboBox
*cb
, int style
)
149 int GetPopupWidth() const
151 switch ( GetWindowVariant() )
153 case wxWINDOW_VARIANT_NORMAL
:
154 case wxWINDOW_VARIANT_LARGE
:
162 void OnChoice( wxCommandEvent
& e
)
164 wxString s
= e
.GetString();
166 m_cb
->DelegateChoice( s
);
167 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
168 event2
.SetInt(m_cb
->GetSelection());
169 event2
.SetEventObject(m_cb
);
170 event2
.SetString(m_cb
->GetStringSelection());
171 m_cb
->ProcessCommand(event2
);
173 // For consistency with MSW and GTK, also send a text updated event
174 // After all, the text is updated when a selection is made
175 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
176 TextEvent
.SetString( m_cb
->GetStringSelection() );
177 TextEvent
.SetEventObject( m_cb
);
178 m_cb
->ProcessCommand( TextEvent
);
180 virtual wxSize
DoGetBestSize() const
182 wxSize sz
= wxChoice::DoGetBestSize() ;
183 if (! m_cb
->HasFlag(wxCB_READONLY
) )
184 sz
.x
= GetPopupWidth() ;
191 DECLARE_EVENT_TABLE()
194 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
195 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
198 wxComboBox::~wxComboBox()
200 // delete client objects
203 // delete the controls now, don't leave them alive even though they would
204 // still be eventually deleted by our parent - but it will be too late, the
205 // user code expects them to be gone now
206 if (m_text
!= NULL
) {
210 if (m_choice
!= NULL
) {
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 wxSize
wxComboBox::DoGetBestSize() const
223 if (!m_choice
&& !m_text
)
225 wxSize size
= m_choice
->GetBestSize();
227 if ( m_text
!= NULL
)
229 wxSize sizeText
= m_text
->GetBestSize();
230 if (sizeText
.y
> size
.y
)
232 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
233 size
.x
+= TEXTFOCUSBORDER
;
234 size
.y
+= 2 * TEXTFOCUSBORDER
;
238 // clipping is too tight
244 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
246 wxControl::DoMoveWindow(x
, y
, width
, height
);
248 if ( m_text
== NULL
)
250 // we might not be fully constructed yet, therefore watch out...
252 m_choice
->SetSize(0, 0 , width
, -1);
256 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
257 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
258 // put it at an inset of 1 to have outer area shadows drawn as well
259 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
265 // ----------------------------------------------------------------------------
266 // operations forwarded to the subcontrols
267 // ----------------------------------------------------------------------------
269 bool wxComboBox::Enable(bool enable
)
271 if ( !wxControl::Enable(enable
) )
277 bool wxComboBox::Show(bool show
)
279 if ( !wxControl::Show(show
) )
285 void wxComboBox::SetFocus()
287 if ( m_text
!= NULL
) {
293 void wxComboBox::DelegateTextChanged( const wxString
& value
)
295 SetStringSelection( value
);
299 void wxComboBox::DelegateChoice( const wxString
& value
)
301 SetStringSelection( value
);
305 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
306 const wxString
& value
,
309 const wxArrayString
& choices
,
311 const wxValidator
& validator
,
312 const wxString
& name
)
314 wxCArrayString
chs( choices
);
316 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
317 chs
.GetStrings(), style
, validator
, name
);
321 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
322 const wxString
& value
,
325 int n
, const wxString choices
[],
327 const wxValidator
& validator
,
328 const wxString
& name
)
330 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
331 wxDefaultValidator
, name
) )
336 m_choice
= new wxComboBoxChoice(this, style
);
338 if ( style
& wxCB_READONLY
)
344 m_text
= new wxComboBoxText(this);
347 csize
.y
= m_text
->GetSize().y
;
348 csize
.y
+= 2 * TEXTFOCUSBORDER
;
352 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
354 for ( int i
= 0 ; i
< n
; i
++ )
356 m_choice
->DoAppend( choices
[ i
] );
359 SetBestSize(size
); // Needed because it is a wxControlWithItems
364 wxString
wxComboBox::GetValue() const
368 if ( m_text
== NULL
)
370 result
= m_choice
->GetString( m_choice
->GetSelection() );
374 result
= m_text
->GetValue();
380 int wxComboBox::GetCount() const
382 return m_choice
->GetCount() ;
385 void wxComboBox::SetValue(const wxString
& value
)
387 if ( HasFlag(wxCB_READONLY
) )
388 SetStringSelection( value
) ;
390 m_text
->SetValue( value
);
393 // Clipboard operations
394 void wxComboBox::Copy()
396 if ( m_text
!= NULL
)
402 void wxComboBox::Cut()
404 if ( m_text
!= NULL
)
410 void wxComboBox::Paste()
412 if ( m_text
!= NULL
)
418 void wxComboBox::SetEditable(bool editable
)
420 if ( ( m_text
== NULL
) && editable
)
422 m_text
= new wxComboBoxText( this );
424 else if ( ( m_text
!= NULL
) && !editable
)
430 int currentX
, currentY
;
431 GetPosition( ¤tX
, ¤tY
);
433 int currentW
, currentH
;
434 GetSize( ¤tW
, ¤tH
);
436 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
439 void wxComboBox::SetInsertionPoint(long pos
)
444 void wxComboBox::SetInsertionPointEnd()
449 long wxComboBox::GetInsertionPoint() const
455 wxTextPos
wxComboBox::GetLastPosition() const
461 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
466 void wxComboBox::Remove(long from
, long to
)
471 void wxComboBox::SetSelection(long from
, long to
)
476 int wxComboBox::DoAppend(const wxString
& item
)
478 return m_choice
->DoAppend( item
) ;
481 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
483 return m_choice
->DoInsert( item
, pos
) ;
486 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
488 return m_choice
->DoSetItemClientData( n
, clientData
) ;
491 void* wxComboBox::DoGetItemClientData(int n
) const
493 return m_choice
->DoGetItemClientData( n
) ;
496 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
498 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
501 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
503 return m_choice
->DoGetItemClientObject( n
) ;
506 void wxComboBox::FreeData()
508 if ( HasClientObjectData() )
510 size_t count
= GetCount();
511 for ( size_t n
= 0; n
< count
; n
++ )
513 SetClientObject( n
, NULL
);
518 void wxComboBox::Delete(int n
)
520 // force client object deletion
521 if( HasClientObjectData() )
522 SetClientObject( n
, NULL
);
523 m_choice
->Delete( n
);
526 void wxComboBox::Clear()
532 int wxComboBox::GetSelection() const
534 return m_choice
->GetSelection();
537 void wxComboBox::SetSelection(int n
)
539 m_choice
->SetSelection( n
);
541 if ( m_text
!= NULL
)
543 m_text
->SetValue( GetString( n
) );
547 int wxComboBox::FindString(const wxString
& s
) const
549 return m_choice
->FindString( s
);
552 wxString
wxComboBox::GetString(int n
) const
554 return m_choice
->GetString( n
);
557 wxString
wxComboBox::GetStringSelection() const
559 int sel
= GetSelection ();
561 return wxString(this->GetString (sel
));
563 return wxEmptyString
;
566 bool wxComboBox::SetStringSelection(const wxString
& sel
)
568 int s
= FindString (sel
);
578 void wxComboBox::SetString(int n
, const wxString
& s
)
580 m_choice
->SetString( n
, s
) ;
583 bool wxComboBox::IsEditable() const
585 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
588 void wxComboBox::Undo()
594 void wxComboBox::Redo()
600 void wxComboBox::SelectAll()
606 bool wxComboBox::CanCopy() const
609 return m_text
->CanCopy();
614 bool wxComboBox::CanCut() const
617 return m_text
->CanCut();
622 bool wxComboBox::CanPaste() const
625 return m_text
->CanPaste();
630 bool wxComboBox::CanUndo() const
633 return m_text
->CanUndo();
638 bool wxComboBox::CanRedo() const
641 return m_text
->CanRedo();
646 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
648 /* For consistency with other platforms, clicking in the text area does not constitute a selection
649 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
650 event.SetInt(GetSelection());
651 event.SetEventObject(this);
652 event.SetString(GetStringSelection());
653 ProcessCommand(event);*/