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"
20 #if TARGET_API_MAC_OSX
22 #include <HIToolbox/HIView.h>
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
30 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
32 #if TARGET_API_MAC_OSX
33 #define USE_HICOMBOBOX 1 //use hi combobox define
35 #define USE_HICOMBOBOX 0
38 static int nextPopUpMenuId
= 1000 ;
39 MenuHandle
NewUniqueMenu()
41 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" ) ;
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // the margin between the text control and the choice
52 static const wxCoord MARGIN
= 2;
53 #if TARGET_API_MAC_OSX
54 static const int POPUPWIDTH
= 24;
56 static const int POPUPWIDTH
= 18;
58 static const int POPUPHEIGHT
= 23;
60 // ----------------------------------------------------------------------------
61 // wxComboBoxText: text control forwards events to combobox
62 // ----------------------------------------------------------------------------
64 class wxComboBoxText
: public wxTextCtrl
67 wxComboBoxText( wxComboBox
* cb
)
68 : wxTextCtrl( cb
, 1 )
74 void OnChar( wxKeyEvent
& event
)
76 if ( event
.GetKeyCode() == WXK_RETURN
)
78 wxString value
= GetValue();
80 if ( m_cb
->GetCount() == 0 )
82 // make Enter generate "selected" event if there is only one item
83 // in the combobox - without it, it's impossible to select it at
85 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
87 event
.SetString( value
);
88 event
.SetEventObject( m_cb
);
89 m_cb
->GetEventHandler()->ProcessEvent( event
);
93 // add the item to the list if it's not there yet
94 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
97 m_cb
->SetStringSelection(value
);
99 // and generate the selected event for it
100 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
101 event
.SetInt( m_cb
->GetCount() - 1 );
102 event
.SetString( value
);
103 event
.SetEventObject( m_cb
);
104 m_cb
->GetEventHandler()->ProcessEvent( event
);
107 // This will invoke the dialog default action, such
108 // as the clicking the default button.
110 wxWindow
*parent
= GetParent();
111 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
112 parent
= parent
->GetParent() ;
114 if ( parent
&& parent
->GetDefaultItem() )
116 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
118 if ( def
&& def
->IsEnabled() )
120 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
121 event
.SetEventObject(def
);
136 DECLARE_EVENT_TABLE()
139 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
140 EVT_CHAR( wxComboBoxText::OnChar
)
143 class wxComboBoxChoice
: public wxChoice
146 wxComboBoxChoice(wxComboBox
*cb
, int style
)
153 void OnChoice( wxCommandEvent
& e
)
155 wxString s
= e
.GetString();
157 m_cb
->DelegateChoice( s
);
158 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
159 event2
.SetInt(m_cb
->GetSelection());
160 event2
.SetEventObject(m_cb
);
161 event2
.SetString(m_cb
->GetStringSelection());
162 m_cb
->ProcessCommand(event2
);
164 virtual wxSize
DoGetBestSize() const
166 wxSize sz
= wxChoice::DoGetBestSize() ;
174 DECLARE_EVENT_TABLE()
177 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
178 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
181 wxComboBox::~wxComboBox()
183 // delete client objects
186 // delete the controls now, don't leave them alive even though they would
187 // still be eventually deleted by our parent - but it will be too late, the
188 // user code expects them to be gone now
189 if (m_text
!= NULL
) {
193 if (m_choice
!= NULL
) {
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 wxSize
wxComboBox::DoGetBestSize() const
207 return wxControl::DoGetBestSize();
209 wxSize size
= m_choice
->GetBestSize();
211 if ( m_text
!= NULL
)
213 wxSize sizeText
= m_text
->GetBestSize();
215 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
222 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
224 wxControl::DoMoveWindow(x
, y
, width
, height
);
226 height
= POPUPHEIGHT
;
228 wxControl::DoMoveWindow(x
, y
, width
, height
);
230 if ( m_text
== NULL
)
232 // we might not be fully constructed yet, therefore watch out...
234 m_choice
->SetSize(0, 0 , width
, -1);
238 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
239 m_text
->SetSize(0, 0, wText
, height
);
240 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
247 // ----------------------------------------------------------------------------
248 // operations forwarded to the subcontrols
249 // ----------------------------------------------------------------------------
251 bool wxComboBox::Enable(bool enable
)
253 if ( !wxControl::Enable(enable
) )
259 bool wxComboBox::Show(bool show
)
261 if ( !wxControl::Show(show
) )
267 void wxComboBox::SetFocus()
270 wxControl::SetFocus();
272 if ( m_text
!= NULL
) {
279 void wxComboBox::DelegateTextChanged( const wxString
& value
)
281 SetStringSelection( value
);
285 void wxComboBox::DelegateChoice( const wxString
& value
)
287 SetStringSelection( value
);
291 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
292 const wxString
& value
,
295 const wxArrayString
& choices
,
297 const wxValidator
& validator
,
298 const wxString
& name
)
300 wxCArrayString
chs( choices
);
302 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
303 chs
.GetStrings(), style
, validator
, name
);
307 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
308 const wxString
& value
,
311 int n
, const wxString choices
[],
313 const wxValidator
& validator
,
314 const wxString
& name
)
319 m_macIsUserPane
= FALSE
;
321 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
322 wxDefaultValidator
, name
) )
327 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
330 hiRect
.origin
.x
= 20; //bounds.left;
331 hiRect
.origin
.y
= 25; //bounds.top;
332 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
333 hiRect
.size
.height
= 24;
335 //For some reason, this code causes the combo box not to be displayed at all.
336 //hiRect.origin.x = bounds.left;
337 //hiRect.origin.y = bounds.top;
338 //hiRect.size.width = bounds.right - bounds.left;
339 //hiRect.size.height = bounds.bottom - bounds.top;
340 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
341 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
342 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, (HIViewRef
*) &m_macControl
) );
344 SetControl32BitMinimum( (ControlRef
) m_macControl
, 0 ) ;
345 SetControl32BitMaximum( (ControlRef
) m_macControl
, 100) ;
347 SetControl32BitValue( (ControlRef
) m_macControl
, 1 ) ;
349 MacPostControlCreate(pos
,size
) ;
351 for ( int i
= 0 ; i
< n
; i
++ )
353 DoAppend( choices
[ i
] );
356 HIViewSetVisible( (HIViewRef
) m_macControl
, true );
359 m_choice
= new wxComboBoxChoice(this, style
);
361 m_choice
= new wxComboBoxChoice(this, style
);
362 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
365 if ( style
& wxCB_READONLY
)
371 m_text
= new wxComboBoxText(this);
372 if ( size
.y
== -1 ) {
373 csize
.y
= m_text
->GetSize().y
;
377 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
379 for ( int i
= 0 ; i
< n
; i
++ )
381 m_choice
->DoAppend( choices
[ i
] );
383 SetBestSize(csize
); // Needed because it is a wxControlWithItems
389 wxString
wxComboBox::GetValue() const
392 CFStringRef myString
;
393 HIComboBoxCopyTextItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
)GetSelection(), &myString
);
394 return wxMacCFStringHolder( myString
, m_font
.GetEncoding() ).AsString();
398 if ( m_text
== NULL
)
400 result
= m_choice
->GetString( m_choice
->GetSelection() );
404 result
= m_text
->GetValue();
411 void wxComboBox::SetValue(const wxString
& value
)
416 int s
= FindString (value
);
417 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
419 m_choice
->Append(value
) ;
421 SetStringSelection( value
) ;
425 // Clipboard operations
426 void wxComboBox::Copy()
428 if ( m_text
!= NULL
)
434 void wxComboBox::Cut()
436 if ( m_text
!= NULL
)
442 void wxComboBox::Paste()
444 if ( m_text
!= NULL
)
450 void wxComboBox::SetEditable(bool editable
)
452 if ( ( m_text
== NULL
) && editable
)
454 m_text
= new wxComboBoxText( this );
456 else if ( ( m_text
!= NULL
) && !editable
)
462 int currentX
, currentY
;
463 GetPosition( ¤tX
, ¤tY
);
465 int currentW
, currentH
;
466 GetSize( ¤tW
, ¤tH
);
468 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
471 void wxComboBox::SetInsertionPoint(long pos
)
476 void wxComboBox::SetInsertionPointEnd()
481 long wxComboBox::GetInsertionPoint() const
487 long wxComboBox::GetLastPosition() const
493 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
498 void wxComboBox::Remove(long from
, long to
)
503 void wxComboBox::SetSelection(long from
, long to
)
508 int wxComboBox::DoAppend(const wxString
& item
)
512 HIComboBoxAppendTextItem( (HIViewRef
) m_macControl
, wxMacCFStringHolder( item
, m_font
.GetEncoding() ), &outIndex
);
513 //SetControl32BitMaximum( (HIViewRef) m_macControl, GetCount() );
514 return (int) outIndex
;
516 return m_choice
->DoAppend( item
) ;
520 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
523 HIComboBoxInsertTextItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
)pos
, wxMacCFStringHolder(item
, m_font
.GetEncoding()) );
525 //SetControl32BitMaximum( (HIViewRef) m_macControl, GetCount() );
529 return m_choice
->DoInsert( item
, pos
) ;
533 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
538 return m_choice
->DoSetItemClientData( n
, clientData
) ;
542 void* wxComboBox::DoGetItemClientData(int n
) const
547 return m_choice
->DoGetItemClientData( n
) ;
551 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
556 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
560 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
565 return m_choice
->DoGetItemClientObject( n
) ;
569 void wxComboBox::FreeData()
571 if ( HasClientObjectData() )
573 size_t count
= GetCount();
574 for ( size_t n
= 0; n
< count
; n
++ )
576 SetClientObject( n
, NULL
);
581 int wxComboBox::GetCount() const {
583 return (int) HIComboBoxGetItemCount( (HIViewRef
) m_macControl
);
585 return m_choice
->GetCount() ;
589 void wxComboBox::Delete(int n
)
592 HIComboBoxRemoveItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
)n
);
594 // force client object deletion
595 if( HasClientObjectData() )
596 SetClientObject( n
, NULL
);
597 m_choice
->Delete( n
);
601 void wxComboBox::Clear()
605 for ( CFIndex i
= GetCount() - 1 ; i
>= 0 ; ++ i
)
606 verify_noerr( HIComboBoxRemoveItemAtIndex( (HIViewRef
) m_macControl
, i
) );
607 wxMacCFStringHolder
cf(wxEmptyString
,m_font
.GetEncoding()) ;
608 CFStringRef cfr
= cf
;
609 SetControlData((ControlRef
) m_macControl
,kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,
610 sizeof(CFStringRef
),(Ptr
) &cfr
);
616 int wxComboBox::GetSelection() const
619 return FindString( GetStringSelection() ) ;
621 return m_choice
->GetSelection();
625 void wxComboBox::SetSelection(int n
)
628 SetControl32BitValue( (ControlRef
) m_macControl
, n
+ 1 ) ;
630 m_choice
->SetSelection( n
);
632 if ( m_text
!= NULL
)
634 m_text
->SetValue( GetString( n
) );
639 int wxComboBox::FindString(const wxString
& s
) const
642 for( int i
= 0 ; i
< GetCount() ; i
++ )
644 if ( GetString( i
).IsSameAs(s
, FALSE
) )
649 return m_choice
->FindString( s
);
653 wxString
wxComboBox::GetString(int n
) const
656 CFStringRef itemText
;
657 HIComboBoxCopyTextItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
)n
, &itemText
);
658 return wxMacCFStringHolder(itemText
).AsString();
660 return m_choice
->GetString( n
);
664 wxString
wxComboBox::GetStringSelection() const
668 verify_noerr(GetControlData((ControlRef
) m_macControl
,kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,
669 sizeof(CFStringRef
),(Ptr
) &cfr
,NULL
));
670 // takes of release responsibility
671 wxMacCFStringHolder
cf( cfr
) ;
672 return cf
.AsString() ;
674 int sel
= GetSelection ();
676 return wxString(this->GetString (sel
));
678 return wxEmptyString
;
682 bool wxComboBox::SetStringSelection(const wxString
& sel
)
684 int s
= FindString (sel
);
694 void wxComboBox::SetString(int n
, const wxString
& s
)
697 verify_noerr ( HIComboBoxInsertTextItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
) n
,
698 wxMacCFStringHolder(s
, m_font
.GetEncoding()) ) );
699 verify_noerr ( HIComboBoxRemoveItemAtIndex( (HIViewRef
) m_macControl
, (CFIndex
) n
+ 1 ) );
701 m_choice
->SetString( n
, s
) ;
706 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
708 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
709 event
.SetInt(GetSelection());
710 event
.SetEventObject(this);
711 event
.SetString(GetStringSelection());
712 ProcessCommand(event
);