1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "combobox.h"
16 #include "wx/wxprec.h"
18 #include "wx/combobox.h"
19 #include "wx/button.h"
21 #include "wx/mac/uma.h"
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
27 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
30 static int nextPopUpMenuId
= 1000 ;
31 MenuHandle
NewUniqueMenu()
33 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // the margin between the text control and the choice
44 #if TARGET_API_MAC_OSX
45 // margin should be bigger on OS X due to blue highlight
46 // around text control.
47 static const wxCoord MARGIN
= 4;
48 // this is the border a focus rect on OSX is needing
49 static const int TEXTFOCUSBORDER
= 3 ;
51 static const wxCoord MARGIN
= 2;
52 static const int TEXTFOCUSBORDER
= 0 ;
54 static const int POPUPHEIGHT
= 23;
57 // ----------------------------------------------------------------------------
58 // wxComboBoxText: text control forwards events to combobox
59 // ----------------------------------------------------------------------------
61 class wxComboBoxText
: public wxTextCtrl
64 wxComboBoxText( wxComboBox
* cb
)
65 : wxTextCtrl( cb
, 1 )
71 void OnChar( wxKeyEvent
& event
)
73 // Allows processing the tab key to go to the next control
74 if (event
.GetKeyCode() == WXK_TAB
)
76 wxNavigationKeyEvent NavEvent
;
77 NavEvent
.SetEventObject(this);
78 NavEvent
.SetDirection(true);
79 NavEvent
.SetWindowChange(false);
81 // Get the parent of the combo and have it process the navigation?
82 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
85 if ( event
.GetKeyCode() == WXK_RETURN
)
87 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
88 event
.SetString( GetValue() );
89 event
.SetInt( m_cb
->GetSelection() );
90 event
.SetEventObject( m_cb
);
92 // This will invoke the dialog default action, such
93 // as the clicking the default button.
95 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
97 wxWindow
*parent
= GetParent();
98 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
99 parent
= parent
->GetParent() ;
101 if ( parent
&& parent
->GetDefaultItem() )
103 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
105 if ( def
&& def
->IsEnabled() )
107 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
108 event
.SetEventObject(def
);
120 // Use the KeyUp as a naive approximation for TEXT_UPDATED, even though it is somewhat delayed
121 // but this is less complicated than dealing with idle-ness, and is much better than nothing
122 void OnKeyUp( wxKeyEvent
& event
)
124 if ( event
.GetKeyCode() != WXK_RETURN
&& event
.GetKeyCode() != WXK_TAB
)
126 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId());
127 event
.SetString( GetValue() );
128 event
.SetEventObject( m_cb
);
129 m_cb
->GetEventHandler()->ProcessEvent(event
);
135 DECLARE_EVENT_TABLE()
138 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
139 EVT_CHAR( wxComboBoxText::OnChar
)
140 EVT_KEY_UP( wxComboBoxText::OnKeyUp
)
143 class wxComboBoxChoice
: public wxChoice
146 wxComboBoxChoice(wxComboBox
*cb
, int style
)
151 int GetPopupWidth() const
153 switch ( GetWindowVariant() )
155 case wxWINDOW_VARIANT_NORMAL
:
156 case wxWINDOW_VARIANT_LARGE
:
164 void OnChoice( wxCommandEvent
& e
)
166 wxString s
= e
.GetString();
168 m_cb
->DelegateChoice( s
);
169 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
170 event2
.SetInt(m_cb
->GetSelection());
171 event2
.SetEventObject(m_cb
);
172 event2
.SetString(m_cb
->GetStringSelection());
173 m_cb
->ProcessCommand(event2
);
175 // For consistency with MSW and GTK, also send a text updated event
176 // After all, the text is updated when a selection is made
177 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
178 TextEvent
.SetString( m_cb
->GetStringSelection() );
179 TextEvent
.SetEventObject( m_cb
);
180 m_cb
->ProcessCommand( TextEvent
);
182 virtual wxSize
DoGetBestSize() const
184 wxSize sz
= wxChoice::DoGetBestSize() ;
185 if (! m_cb
->HasFlag(wxCB_READONLY
) )
186 sz
.x
= GetPopupWidth() ;
193 DECLARE_EVENT_TABLE()
196 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
197 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
200 wxComboBox::~wxComboBox()
202 // delete client objects
205 // delete the controls now, don't leave them alive even though they would
206 // still be eventually deleted by our parent - but it will be too late, the
207 // user code expects them to be gone now
208 if (m_text
!= NULL
) {
212 if (m_choice
!= NULL
) {
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 wxSize
wxComboBox::DoGetBestSize() const
225 if (!m_choice
&& !m_text
)
227 wxSize size
= m_choice
->GetBestSize();
229 if ( m_text
!= NULL
)
231 wxSize sizeText
= m_text
->GetBestSize();
232 if (sizeText
.y
> size
.y
)
234 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
235 size
.x
+= TEXTFOCUSBORDER
;
236 size
.y
+= 2 * TEXTFOCUSBORDER
;
240 // clipping is too tight
246 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
248 wxControl::DoMoveWindow(x
, y
, width
, height
);
250 if ( m_text
== NULL
)
252 // we might not be fully constructed yet, therefore watch out...
254 m_choice
->SetSize(0, 0 , width
, -1);
258 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
259 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
260 // put it at an inset of 1 to have outer area shadows drawn as well
261 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
267 // ----------------------------------------------------------------------------
268 // operations forwarded to the subcontrols
269 // ----------------------------------------------------------------------------
271 bool wxComboBox::Enable(bool enable
)
273 if ( !wxControl::Enable(enable
) )
279 bool wxComboBox::Show(bool show
)
281 if ( !wxControl::Show(show
) )
287 void wxComboBox::SetFocus()
289 if ( m_text
!= NULL
) {
295 void wxComboBox::DelegateTextChanged( const wxString
& value
)
297 SetStringSelection( value
);
301 void wxComboBox::DelegateChoice( const wxString
& value
)
303 SetStringSelection( value
);
307 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
308 const wxString
& value
,
311 const wxArrayString
& choices
,
313 const wxValidator
& validator
,
314 const wxString
& name
)
316 wxCArrayString
chs( choices
);
318 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
319 chs
.GetStrings(), style
, validator
, name
);
323 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
324 const wxString
& value
,
327 int n
, const wxString choices
[],
329 const wxValidator
& validator
,
330 const wxString
& name
)
332 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
333 wxDefaultValidator
, name
) )
338 m_choice
= new wxComboBoxChoice(this, style
);
340 if ( style
& wxCB_READONLY
)
346 m_text
= new wxComboBoxText(this);
349 csize
.y
= m_text
->GetSize().y
;
350 csize
.y
+= 2 * TEXTFOCUSBORDER
;
354 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
356 for ( int i
= 0 ; i
< n
; i
++ )
358 m_choice
->DoAppend( choices
[ i
] );
361 SetBestSize(size
); // Needed because it is a wxControlWithItems
366 wxString
wxComboBox::GetValue() const
370 if ( m_text
== NULL
)
372 result
= m_choice
->GetString( m_choice
->GetSelection() );
376 result
= m_text
->GetValue();
382 int wxComboBox::GetCount() const
384 return m_choice
->GetCount() ;
387 void wxComboBox::SetValue(const wxString
& value
)
389 if ( HasFlag(wxCB_READONLY
) )
390 SetStringSelection( value
) ;
392 m_text
->SetValue( value
);
395 // Clipboard operations
396 void wxComboBox::Copy()
398 if ( m_text
!= NULL
)
404 void wxComboBox::Cut()
406 if ( m_text
!= NULL
)
412 void wxComboBox::Paste()
414 if ( m_text
!= NULL
)
420 void wxComboBox::SetEditable(bool editable
)
422 if ( ( m_text
== NULL
) && editable
)
424 m_text
= new wxComboBoxText( this );
426 else if ( ( m_text
!= NULL
) && !editable
)
432 int currentX
, currentY
;
433 GetPosition( ¤tX
, ¤tY
);
435 int currentW
, currentH
;
436 GetSize( ¤tW
, ¤tH
);
438 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
441 void wxComboBox::SetInsertionPoint(long pos
)
446 void wxComboBox::SetInsertionPointEnd()
451 long wxComboBox::GetInsertionPoint() const
457 wxTextPos
wxComboBox::GetLastPosition() const
463 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
468 void wxComboBox::Remove(long from
, long to
)
473 void wxComboBox::SetSelection(long from
, long to
)
478 int wxComboBox::DoAppend(const wxString
& item
)
480 return m_choice
->DoAppend( item
) ;
483 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
485 return m_choice
->DoInsert( item
, pos
) ;
488 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
490 return m_choice
->DoSetItemClientData( n
, clientData
) ;
493 void* wxComboBox::DoGetItemClientData(int n
) const
495 return m_choice
->DoGetItemClientData( n
) ;
498 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
500 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
503 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
505 return m_choice
->DoGetItemClientObject( n
) ;
508 void wxComboBox::FreeData()
510 if ( HasClientObjectData() )
512 size_t count
= GetCount();
513 for ( size_t n
= 0; n
< count
; n
++ )
515 SetClientObject( n
, NULL
);
520 void wxComboBox::Delete(int n
)
522 // force client object deletion
523 if( HasClientObjectData() )
524 SetClientObject( n
, NULL
);
525 m_choice
->Delete( n
);
528 void wxComboBox::Clear()
534 int wxComboBox::GetSelection() const
536 return m_choice
->GetSelection();
539 void wxComboBox::SetSelection(int n
)
541 m_choice
->SetSelection( n
);
543 if ( m_text
!= NULL
)
545 m_text
->SetValue( GetString( n
) );
549 int wxComboBox::FindString(const wxString
& s
) const
551 return m_choice
->FindString( s
);
554 wxString
wxComboBox::GetString(int n
) const
556 return m_choice
->GetString( n
);
559 wxString
wxComboBox::GetStringSelection() const
561 int sel
= GetSelection ();
563 return wxString(this->GetString (sel
));
565 return wxEmptyString
;
568 void wxComboBox::SetString(int n
, const wxString
& s
)
570 m_choice
->SetString( n
, s
) ;
573 bool wxComboBox::IsEditable() const
575 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
578 void wxComboBox::Undo()
584 void wxComboBox::Redo()
590 void wxComboBox::SelectAll()
596 bool wxComboBox::CanCopy() const
599 return m_text
->CanCopy();
604 bool wxComboBox::CanCut() const
607 return m_text
->CanCut();
612 bool wxComboBox::CanPaste() const
615 return m_text
->CanPaste();
620 bool wxComboBox::CanUndo() const
623 return m_text
->CanUndo();
628 bool wxComboBox::CanRedo() const
631 return m_text
->CanRedo();
636 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
638 /* For consistency with other platforms, clicking in the text area does not constitute a selection
639 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
640 event.SetInt(GetSelection());
641 event.SetEventObject(this);
642 event.SetString(GetStringSelection());
643 ProcessCommand(event);*/