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 static const EventTypeSpec eventList
[] =
49 { kEventClassTextField
, kEventTextAccepted
} ,
52 static pascal OSStatus
wxMacComboBoxEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
54 OSStatus result
= eventNotHandledErr
;
55 wxComboBox
* cb
= (wxComboBox
*) data
;
57 wxMacCarbonEvent
cEvent( event
) ;
59 switch( cEvent
.GetClass() )
61 case kEventClassTextField
:
62 switch( cEvent
.GetKind() )
64 case kEventTextAccepted
:
66 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, cb
->GetId() );
67 event
.SetInt( cb
->GetSelection() );
68 event
.SetString( cb
->GetStringSelection() );
69 event
.SetEventObject( cb
);
70 cb
->GetEventHandler()->ProcessEvent( event
);
85 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // the margin between the text control and the choice
94 static const wxCoord MARGIN
= 2;
95 #if TARGET_API_MAC_OSX
96 static const int POPUPWIDTH
= 24;
98 static const int POPUPWIDTH
= 18;
100 static const int POPUPHEIGHT
= 23;
102 // ----------------------------------------------------------------------------
103 // wxComboBoxText: text control forwards events to combobox
104 // ----------------------------------------------------------------------------
106 class wxComboBoxText
: public wxTextCtrl
109 wxComboBoxText( wxComboBox
* cb
)
110 : wxTextCtrl( cb
, 1 )
116 void OnChar( wxKeyEvent
& event
)
118 if ( event
.GetKeyCode() == WXK_RETURN
)
120 wxString value
= GetValue();
122 if ( m_cb
->GetCount() == 0 )
124 // make Enter generate "selected" event if there is only one item
125 // in the combobox - without it, it's impossible to select it at
127 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
129 event
.SetString( value
);
130 event
.SetEventObject( m_cb
);
131 m_cb
->GetEventHandler()->ProcessEvent( event
);
135 // add the item to the list if it's not there yet
136 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
139 m_cb
->SetStringSelection(value
);
141 // and generate the selected event for it
142 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
143 event
.SetInt( m_cb
->GetCount() - 1 );
144 event
.SetString( value
);
145 event
.SetEventObject( m_cb
);
146 m_cb
->GetEventHandler()->ProcessEvent( event
);
149 // This will invoke the dialog default action, such
150 // as the clicking the default button.
152 wxWindow
*parent
= GetParent();
153 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
154 parent
= parent
->GetParent() ;
156 if ( parent
&& parent
->GetDefaultItem() )
158 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
160 if ( def
&& def
->IsEnabled() )
162 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
163 event
.SetEventObject(def
);
178 DECLARE_EVENT_TABLE()
181 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
182 EVT_CHAR( wxComboBoxText::OnChar
)
185 class wxComboBoxChoice
: public wxChoice
188 wxComboBoxChoice(wxComboBox
*cb
, int style
)
195 void OnChoice( wxCommandEvent
& e
)
197 wxString s
= e
.GetString();
199 m_cb
->DelegateChoice( s
);
200 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
201 event2
.SetInt(m_cb
->GetSelection());
202 event2
.SetEventObject(m_cb
);
203 event2
.SetString(m_cb
->GetStringSelection());
204 m_cb
->ProcessCommand(event2
);
206 virtual wxSize
DoGetBestSize() const
208 wxSize sz
= wxChoice::DoGetBestSize() ;
216 DECLARE_EVENT_TABLE()
219 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
220 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice
)
223 wxComboBox::~wxComboBox()
225 // delete client objects
228 // delete the controls now, don't leave them alive even though they would
229 // still be eventually deleted by our parent - but it will be too late, the
230 // user code expects them to be gone now
231 if (m_text
!= NULL
) {
235 if (m_choice
!= NULL
) {
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 wxSize
wxComboBox::DoGetBestSize() const
249 return wxControl::DoGetBestSize();
251 wxSize size
= m_choice
->GetBestSize();
253 if ( m_text
!= NULL
)
255 wxSize sizeText
= m_text
->GetBestSize();
257 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
264 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
266 wxControl::DoMoveWindow(x
, y
, width
, height
);
268 height
= POPUPHEIGHT
;
270 wxControl::DoMoveWindow(x
, y
, width
, height
);
272 if ( m_text
== NULL
)
274 // we might not be fully constructed yet, therefore watch out...
276 m_choice
->SetSize(0, 0 , width
, -1);
280 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
281 m_text
->SetSize(0, 0, wText
, height
);
282 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, -1);
289 // ----------------------------------------------------------------------------
290 // operations forwarded to the subcontrols
291 // ----------------------------------------------------------------------------
293 bool wxComboBox::Enable(bool enable
)
295 if ( !wxControl::Enable(enable
) )
301 bool wxComboBox::Show(bool show
)
303 if ( !wxControl::Show(show
) )
309 void wxComboBox::SetFocus()
312 wxControl::SetFocus();
314 if ( m_text
!= NULL
) {
321 void wxComboBox::DelegateTextChanged( const wxString
& value
)
323 SetStringSelection( value
);
327 void wxComboBox::DelegateChoice( const wxString
& value
)
329 SetStringSelection( value
);
333 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
334 const wxString
& value
,
337 const wxArrayString
& choices
,
339 const wxValidator
& validator
,
340 const wxString
& name
)
342 wxCArrayString
chs( choices
);
344 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
345 chs
.GetStrings(), style
, validator
, name
);
349 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
350 const wxString
& value
,
353 int n
, const wxString choices
[],
355 const wxValidator
& validator
,
356 const wxString
& name
)
361 m_macIsUserPane
= FALSE
;
363 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
364 wxDefaultValidator
, name
) )
369 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
372 hiRect
.origin
.x
= 20; //bounds.left;
373 hiRect
.origin
.y
= 25; //bounds.top;
374 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
375 hiRect
.size
.height
= 24;
377 //For some reason, this code causes the combo box not to be displayed at all.
378 //hiRect.origin.x = bounds.left;
379 //hiRect.origin.y = bounds.top;
380 //hiRect.size.width = bounds.right - bounds.left;
381 //hiRect.size.height = bounds.bottom - bounds.top;
382 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
383 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
384 m_peer
= new wxMacControl() ;
385 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, *m_peer
) );
388 SetControl32BitMinimum( *m_peer
, 0 ) ;
389 SetControl32BitMaximum( *m_peer
, 100) ;
391 SetControl32BitValue( *m_peer
, 1 ) ;
393 MacPostControlCreate(pos
,size
) ;
395 for ( int i
= 0 ; i
< n
; i
++ )
397 DoAppend( choices
[ i
] );
400 HIViewSetVisible( *m_peer
, true );
402 EventHandlerRef comboEventHandler
;
403 InstallControlEventHandler( *m_peer
, GetwxMacComboBoxEventHandlerUPP(),
404 GetEventTypeCount(eventList
), eventList
, this,
405 (EventHandlerRef
*)&comboEventHandler
);
407 m_choice
= new wxComboBoxChoice(this, style
);
409 m_choice
= new wxComboBoxChoice(this, style
);
410 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
413 if ( style
& wxCB_READONLY
)
419 m_text
= new wxComboBoxText(this);
420 if ( size
.y
== -1 ) {
421 csize
.y
= m_text
->GetSize().y
;
425 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
427 for ( int i
= 0 ; i
< n
; i
++ )
429 m_choice
->DoAppend( choices
[ i
] );
431 SetBestSize(csize
); // Needed because it is a wxControlWithItems
437 wxString
wxComboBox::GetValue() const
440 CFStringRef myString
;
441 HIComboBoxCopyTextItemAtIndex( *m_peer
, (CFIndex
)GetSelection(), &myString
);
442 return wxMacCFStringHolder( myString
, m_font
.GetEncoding() ).AsString();
446 if ( m_text
== NULL
)
448 result
= m_choice
->GetString( m_choice
->GetSelection() );
452 result
= m_text
->GetValue();
459 void wxComboBox::SetValue(const wxString
& value
)
464 int s
= FindString (value
);
465 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
467 m_choice
->Append(value
) ;
469 SetStringSelection( value
) ;
473 // Clipboard operations
474 void wxComboBox::Copy()
476 if ( m_text
!= NULL
)
482 void wxComboBox::Cut()
484 if ( m_text
!= NULL
)
490 void wxComboBox::Paste()
492 if ( m_text
!= NULL
)
498 void wxComboBox::SetEditable(bool editable
)
500 if ( ( m_text
== NULL
) && editable
)
502 m_text
= new wxComboBoxText( this );
504 else if ( ( m_text
!= NULL
) && !editable
)
510 int currentX
, currentY
;
511 GetPosition( ¤tX
, ¤tY
);
513 int currentW
, currentH
;
514 GetSize( ¤tW
, ¤tH
);
516 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
519 void wxComboBox::SetInsertionPoint(long pos
)
524 void wxComboBox::SetInsertionPointEnd()
529 long wxComboBox::GetInsertionPoint() const
535 long wxComboBox::GetLastPosition() const
541 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
546 void wxComboBox::Remove(long from
, long to
)
551 void wxComboBox::SetSelection(long from
, long to
)
556 int wxComboBox::DoAppend(const wxString
& item
)
560 HIComboBoxAppendTextItem( *m_peer
, wxMacCFStringHolder( item
, m_font
.GetEncoding() ), &outIndex
);
561 //SetControl32BitMaximum( *m_peer, GetCount() );
562 return (int) outIndex
;
564 return m_choice
->DoAppend( item
) ;
568 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
571 HIComboBoxInsertTextItemAtIndex( *m_peer
, (CFIndex
)pos
, wxMacCFStringHolder(item
, m_font
.GetEncoding()) );
573 //SetControl32BitMaximum( *m_peer, GetCount() );
577 return m_choice
->DoInsert( item
, pos
) ;
581 void wxComboBox::DoSetItemClientData(int n
, void* clientData
)
586 return m_choice
->DoSetItemClientData( n
, clientData
) ;
590 void* wxComboBox::DoGetItemClientData(int n
) const
595 return m_choice
->DoGetItemClientData( n
) ;
599 void wxComboBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
604 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
608 wxClientData
* wxComboBox::DoGetItemClientObject(int n
) const
613 return m_choice
->DoGetItemClientObject( n
) ;
617 void wxComboBox::FreeData()
619 if ( HasClientObjectData() )
621 size_t count
= GetCount();
622 for ( size_t n
= 0; n
< count
; n
++ )
624 SetClientObject( n
, NULL
);
629 int wxComboBox::GetCount() const {
631 return (int) HIComboBoxGetItemCount( *m_peer
);
633 return m_choice
->GetCount() ;
637 void wxComboBox::Delete(int n
)
640 HIComboBoxRemoveItemAtIndex( *m_peer
, (CFIndex
)n
);
642 // force client object deletion
643 if( HasClientObjectData() )
644 SetClientObject( n
, NULL
);
645 m_choice
->Delete( n
);
649 void wxComboBox::Clear()
653 for ( CFIndex i
= GetCount() - 1 ; i
>= 0 ; ++ i
)
654 verify_noerr( HIComboBoxRemoveItemAtIndex( *m_peer
, i
) );
655 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
661 int wxComboBox::GetSelection() const
664 return FindString( GetStringSelection() ) ;
666 return m_choice
->GetSelection();
670 void wxComboBox::SetSelection(int n
)
673 SetControl32BitValue( *m_peer
, n
+ 1 ) ;
675 m_choice
->SetSelection( n
);
677 if ( m_text
!= NULL
)
679 m_text
->SetValue( GetString( n
) );
684 int wxComboBox::FindString(const wxString
& s
) const
687 for( int i
= 0 ; i
< GetCount() ; i
++ )
689 if ( GetString( i
).IsSameAs(s
, FALSE
) )
694 return m_choice
->FindString( s
);
698 wxString
wxComboBox::GetString(int n
) const
701 CFStringRef itemText
;
702 HIComboBoxCopyTextItemAtIndex( *m_peer
, (CFIndex
)n
, &itemText
);
703 return wxMacCFStringHolder(itemText
).AsString();
705 return m_choice
->GetString( n
);
709 wxString
wxComboBox::GetStringSelection() const
712 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString() ;
714 int sel
= GetSelection ();
716 return wxString(this->GetString (sel
));
718 return wxEmptyString
;
722 bool wxComboBox::SetStringSelection(const wxString
& sel
)
724 int s
= FindString (sel
);
734 void wxComboBox::SetString(int n
, const wxString
& s
)
737 verify_noerr ( HIComboBoxInsertTextItemAtIndex( *m_peer
, (CFIndex
) n
,
738 wxMacCFStringHolder(s
, m_font
.GetEncoding()) ) );
739 verify_noerr ( HIComboBoxRemoveItemAtIndex( *m_peer
, (CFIndex
) n
+ 1 ) );
741 m_choice
->SetString( n
, s
) ;
746 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
748 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
749 event
.SetInt(GetSelection());
750 event
.SetEventObject(this);
751 event
.SetString(GetStringSelection());
752 ProcessCommand(event
);