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"
20 #include "wx/combobox.h"
21 #include "wx/button.h"
23 #include "wx/mac/uma.h"
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
29 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
32 static int nextPopUpMenuId
= 1000 ;
33 MenuHandle
NewUniqueMenu()
35 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the margin between the text control and the choice
46 #if TARGET_API_MAC_OSX
47 // margin should be bigger on OS X due to blue highlight
48 // around text control.
49 static const wxCoord MARGIN
= 4;
50 // this is the border a focus rect on OSX is needing
51 static const int TEXTFOCUSBORDER
= 3 ;
53 static const wxCoord MARGIN
= 2;
54 static const int TEXTFOCUSBORDER
= 0 ;
56 static const int POPUPHEIGHT
= 23;
59 // ----------------------------------------------------------------------------
60 // wxComboBoxText: text control forwards events to combobox
61 // ----------------------------------------------------------------------------
63 class wxComboBoxText
: public wxTextCtrl
66 wxComboBoxText( wxComboBox
* cb
)
67 : wxTextCtrl( cb
, 1 )
73 void OnChar( wxKeyEvent
& event
)
75 // Allows processing the tab key to go to the next control
76 if (event
.GetKeyCode() == WXK_TAB
)
78 wxNavigationKeyEvent NavEvent
;
79 NavEvent
.SetEventObject(this);
80 NavEvent
.SetDirection(true);
81 NavEvent
.SetWindowChange(false);
83 // Get the parent of the combo and have it process the navigation?
84 if (m_cb
->GetParent()->GetEventHandler()->ProcessEvent(NavEvent
))
88 // send the event to the combobox class in case the user has bound EVT_CHAR
89 wxKeyEvent
kevt(event
);
90 kevt
.SetEventObject(m_cb
);
91 if (m_cb
->GetEventHandler()->ProcessEvent(kevt
))
92 // If the event was handled and not skipped then we're done
95 if ( event
.GetKeyCode() == WXK_RETURN
)
97 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_cb
->GetId());
98 event
.SetString( GetValue() );
99 event
.SetInt( m_cb
->GetSelection() );
100 event
.SetEventObject( m_cb
);
102 // This will invoke the dialog default action, such
103 // as the clicking the default button.
105 if (!m_cb
->GetEventHandler()->ProcessEvent( event
))
107 wxWindow
*parent
= GetParent();
108 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
109 parent
= parent
->GetParent() ;
111 if ( parent
&& parent
->GetDefaultItem() )
113 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
115 if ( def
&& def
->IsEnabled() )
117 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
118 event
.SetEventObject(def
);
130 void OnKeyUp( wxKeyEvent
& event
)
132 event
.SetEventObject(m_cb
);
133 event
.SetId(m_cb
->GetId());
134 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
138 void OnKeyDown( wxKeyEvent
& event
)
140 event
.SetEventObject(m_cb
);
141 event
.SetId(m_cb
->GetId());
142 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
146 void OnText( wxCommandEvent
& event
)
148 event
.SetEventObject(m_cb
);
149 event
.SetId(m_cb
->GetId());
150 if (! m_cb
->GetEventHandler()->ProcessEvent(event
))
157 DECLARE_EVENT_TABLE()
160 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
161 EVT_KEY_DOWN( wxComboBoxText::OnKeyDown
)
162 EVT_CHAR( wxComboBoxText::OnChar
)
163 EVT_KEY_UP( wxComboBoxText::OnKeyUp
)
164 EVT_TEXT( -1, wxComboBoxText::OnText
)
167 class wxComboBoxChoice
: public wxChoice
170 wxComboBoxChoice(wxComboBox
*cb
, int style
)
171 : wxChoice( cb
, 1 , wxDefaultPosition
, wxDefaultSize
, 0 , NULL
, style
& (wxCB_SORT
) )
175 int GetPopupWidth() const
177 switch ( GetWindowVariant() )
179 case wxWINDOW_VARIANT_NORMAL
:
180 case wxWINDOW_VARIANT_LARGE
:
188 void OnChoice( wxCommandEvent
& e
)
190 wxString s
= e
.GetString();
192 m_cb
->DelegateChoice( s
);
193 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
194 event2
.SetInt(m_cb
->GetSelection());
195 event2
.SetEventObject(m_cb
);
196 event2
.SetString(m_cb
->GetStringSelection());
197 m_cb
->ProcessCommand(event2
);
199 // For consistency with MSW and GTK, also send a text updated event
200 // After all, the text is updated when a selection is made
201 wxCommandEvent
TextEvent( wxEVT_COMMAND_TEXT_UPDATED
, m_cb
->GetId() );
202 TextEvent
.SetString( m_cb
->GetStringSelection() );
203 TextEvent
.SetEventObject( m_cb
);
204 m_cb
->ProcessCommand( TextEvent
);
206 virtual wxSize
DoGetBestSize() const
208 wxSize sz
= wxChoice::DoGetBestSize() ;
209 if (! m_cb
->HasFlag(wxCB_READONLY
) )
210 sz
.x
= GetPopupWidth() ;
217 DECLARE_EVENT_TABLE()
220 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
221 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
224 wxComboBox::~wxComboBox()
226 // delete client objects
229 // delete the controls now, don't leave them alive even though they would
230 // still be eventually deleted by our parent - but it will be too late, the
231 // user code expects them to be gone now
232 if (m_text
!= NULL
) {
236 if (m_choice
!= NULL
) {
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxSize
wxComboBox::DoGetBestSize() const
249 if (!m_choice
&& !m_text
)
251 wxSize size
= m_choice
->GetBestSize();
253 if ( m_text
!= NULL
)
255 wxSize sizeText
= m_text
->GetBestSize();
256 if (sizeText
.y
> size
.y
)
258 size
.x
= m_choice
->GetPopupWidth() + sizeText
.x
+ MARGIN
;
259 size
.x
+= TEXTFOCUSBORDER
;
260 size
.y
+= 2 * TEXTFOCUSBORDER
;
264 // clipping is too tight
270 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
272 wxControl::DoMoveWindow(x
, y
, width
, height
);
274 if ( m_text
== NULL
)
276 // we might not be fully constructed yet, therefore watch out...
278 m_choice
->SetSize(0, 0 , width
, -1);
282 wxCoord wText
= width
- m_choice
->GetPopupWidth() - MARGIN
;
283 m_text
->SetSize(TEXTFOCUSBORDER
, TEXTFOCUSBORDER
, wText
, -1 );
284 // put it at an inset of 1 to have outer area shadows drawn as well
285 m_choice
->SetSize(TEXTFOCUSBORDER
+ wText
+ MARGIN
- 1 , TEXTFOCUSBORDER
, m_choice
->GetPopupWidth() , -1);
291 // ----------------------------------------------------------------------------
292 // operations forwarded to the subcontrols
293 // ----------------------------------------------------------------------------
295 bool wxComboBox::Enable(bool enable
)
297 if ( !wxControl::Enable(enable
) )
303 bool wxComboBox::Show(bool show
)
305 if ( !wxControl::Show(show
) )
311 void wxComboBox::SetFocus()
313 if ( m_text
!= NULL
) {
319 void wxComboBox::DelegateTextChanged( const wxString
& value
)
321 SetStringSelection( value
);
325 void wxComboBox::DelegateChoice( const wxString
& value
)
327 SetStringSelection( value
);
331 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
332 const wxString
& value
,
335 const wxArrayString
& choices
,
337 const wxValidator
& validator
,
338 const wxString
& name
)
340 wxCArrayString
chs( choices
);
342 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
343 chs
.GetStrings(), style
, validator
, name
);
347 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
348 const wxString
& value
,
351 int n
, const wxString choices
[],
353 const wxValidator
& validator
,
354 const wxString
& name
)
356 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
362 m_choice
= new wxComboBoxChoice(this, style
);
364 if ( style
& wxCB_READONLY
)
370 m_text
= new wxComboBoxText(this);
373 csize
.y
= m_text
->GetSize().y
;
374 csize
.y
+= 2 * TEXTFOCUSBORDER
;
378 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
380 for ( int i
= 0 ; i
< n
; i
++ )
382 m_choice
->DoAppend( choices
[ i
] );
385 SetBestSize(size
); // Needed because it is a wxControlWithItems
386 SetStringSelection(value
);
391 wxString
wxComboBox::GetValue() const
395 if ( m_text
== NULL
)
397 result
= m_choice
->GetString( m_choice
->GetSelection() );
401 result
= m_text
->GetValue();
407 int wxComboBox::GetCount() const
409 return m_choice
->GetCount() ;
412 void wxComboBox::SetValue(const wxString
& value
)
414 if ( HasFlag(wxCB_READONLY
) )
415 SetStringSelection( value
) ;
417 m_text
->SetValue( value
);
420 // Clipboard operations
421 void wxComboBox::Copy()
423 if ( m_text
!= NULL
)
429 void wxComboBox::Cut()
431 if ( m_text
!= NULL
)
437 void wxComboBox::Paste()
439 if ( m_text
!= NULL
)
445 void wxComboBox::SetEditable(bool editable
)
447 if ( ( m_text
== NULL
) && editable
)
449 m_text
= new wxComboBoxText( this );
451 else if ( ( m_text
!= NULL
) && !editable
)
457 int currentX
, currentY
;
458 GetPosition( ¤tX
, ¤tY
);
460 int currentW
, currentH
;
461 GetSize( ¤tW
, ¤tH
);
463 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
466 void wxComboBox::SetInsertionPoint(long pos
)
471 void wxComboBox::SetInsertionPointEnd()
476 long wxComboBox::GetInsertionPoint() const
482 wxTextPos
wxComboBox::GetLastPosition() const
488 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
493 void wxComboBox::Remove(long from
, long to
)
498 void wxComboBox::SetSelection(long from
, long to
)
503 int wxComboBox::DoAppend(const wxString
& item
)
505 return m_choice
->DoAppend( item
) ;
508 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
510 return m_choice
->DoInsert( item
, pos
) ;
513 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
515 return m_choice
->DoSetItemClientData( n
, clientData
) ;
518 void* wxComboBox::DoGetItemClientData(int n
) const
520 return m_choice
->DoGetItemClientData( n
) ;
523 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
525 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
528 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
530 return m_choice
->DoGetItemClientObject( n
) ;
533 void wxComboBox::FreeData()
535 if ( HasClientObjectData() )
537 size_t count
= GetCount();
538 for ( size_t n
= 0; n
< count
; n
++ )
540 SetClientObject( n
, NULL
);
545 void wxComboBox::Delete(int n
)
547 // force client object deletion
548 if( HasClientObjectData() )
549 SetClientObject( n
, NULL
);
550 m_choice
->Delete( n
);
553 void wxComboBox::Clear()
559 int wxComboBox::GetSelection() const
561 return m_choice
->GetSelection();
564 void wxComboBox::SetSelection(int n
)
566 m_choice
->SetSelection( n
);
568 if ( m_text
!= NULL
)
570 m_text
->SetValue( GetString( n
) );
574 int wxComboBox::FindString(const wxString
& s
) const
576 return m_choice
->FindString( s
);
579 wxString
wxComboBox::GetString(int n
) const
581 return m_choice
->GetString( n
);
584 wxString
wxComboBox::GetStringSelection() const
586 int sel
= GetSelection ();
588 return wxString(this->GetString (sel
));
590 return wxEmptyString
;
593 void wxComboBox::SetString(int n
, const wxString
& s
)
595 m_choice
->SetString( n
, s
) ;
598 bool wxComboBox::IsEditable() const
600 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
603 void wxComboBox::Undo()
609 void wxComboBox::Redo()
615 void wxComboBox::SelectAll()
621 bool wxComboBox::CanCopy() const
624 return m_text
->CanCopy();
629 bool wxComboBox::CanCut() const
632 return m_text
->CanCut();
637 bool wxComboBox::CanPaste() const
640 return m_text
->CanPaste();
645 bool wxComboBox::CanUndo() const
648 return m_text
->CanUndo();
653 bool wxComboBox::CanRedo() const
656 return m_text
->CanRedo();
661 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
663 /* For consistency with other platforms, clicking in the text area does not constitute a selection
664 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
665 event.SetInt(GetSelection());
666 event.SetEventObject(this);
667 event.SetString(GetStringSelection());
668 ProcessCommand(event);*/