1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/combobxc.cpp
3 // Purpose: wxComboBox class using HIView ComboBox
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/combobox.h"
17 #include "wx/button.h"
21 #include "wx/mac/uma.h"
22 #if TARGET_API_MAC_OSX
24 #include <HIToolbox/HIView.h>
28 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
30 #if TARGET_API_MAC_OSX
31 #define USE_HICOMBOBOX 1 //use hi combobox define
33 #define USE_HICOMBOBOX 0
36 static int nextPopUpMenuId
= 1000;
37 MenuHandle
NewUniqueMenu()
39 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" );
45 static const EventTypeSpec eventList
[] =
47 { kEventClassTextField
, kEventTextAccepted
} ,
50 static pascal OSStatus
wxMacComboBoxEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
52 OSStatus result
= eventNotHandledErr
;
53 wxComboBox
* cb
= (wxComboBox
*) data
;
55 wxMacCarbonEvent
cEvent( event
);
57 switch( cEvent
.GetClass() )
59 case kEventClassTextField
:
60 switch( cEvent
.GetKind() )
62 case kEventTextAccepted
:
64 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, cb
->GetId() );
65 event
.SetInt( cb
->GetSelection() );
66 event
.SetString( cb
->GetStringSelection() );
67 event
.SetEventObject( cb
);
68 cb
->GetEventHandler()->ProcessEvent( event
);
83 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler
)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // the margin between the text control and the choice
92 static const wxCoord MARGIN
= 2;
93 #if TARGET_API_MAC_OSX
94 static const int POPUPWIDTH
= 24;
96 static const int POPUPWIDTH
= 18;
98 static const int POPUPHEIGHT
= 23;
100 // ----------------------------------------------------------------------------
101 // wxComboBoxText: text control forwards events to combobox
102 // ----------------------------------------------------------------------------
104 class wxComboBoxText
: public wxTextCtrl
107 wxComboBoxText( wxComboBox
* cb
)
108 : wxTextCtrl( cb
, 1 )
114 void OnChar( wxKeyEvent
& event
)
116 if ( event
.GetKeyCode() == WXK_RETURN
)
118 wxString value
= GetValue();
120 if ( m_cb
->GetCount() == 0 )
122 // make Enter generate "selected" event if there is only one item
123 // in the combobox - without it, it's impossible to select it at
125 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
127 event
.SetString( value
);
128 event
.SetEventObject( m_cb
);
129 m_cb
->GetEventHandler()->ProcessEvent( event
);
133 // add the item to the list if it's not there yet
134 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
137 m_cb
->SetStringSelection(value
);
139 // and generate the selected event for it
140 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
141 event
.SetInt( m_cb
->GetCount() - 1 );
142 event
.SetString( value
);
143 event
.SetEventObject( m_cb
);
144 m_cb
->GetEventHandler()->ProcessEvent( event
);
147 // This will invoke the dialog default action, such
148 // as the clicking the default button.
150 wxWindow
*parent
= GetParent();
151 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
152 parent
= parent
->GetParent();
154 if ( parent
&& parent
->GetDefaultItem() )
156 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
158 if ( def
&& def
->IsEnabled() )
160 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
161 event
.SetEventObject(def
);
176 DECLARE_EVENT_TABLE()
179 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
180 EVT_CHAR( wxComboBoxText::OnChar
)
183 class wxComboBoxChoice
: public wxChoice
186 wxComboBoxChoice(wxComboBox
*cb
, int style
)
193 void OnChoice( wxCommandEvent
& e
)
195 wxString s
= e
.GetString();
197 m_cb
->DelegateChoice( s
);
198 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
199 event2
.SetInt(m_cb
->GetSelection());
200 event2
.SetEventObject(m_cb
);
201 event2
.SetString(m_cb
->GetStringSelection());
202 m_cb
->ProcessCommand(event2
);
204 virtual wxSize
DoGetBestSize() const
206 wxSize sz
= wxChoice::DoGetBestSize();
214 DECLARE_EVENT_TABLE()
217 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
218 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
221 wxComboBox::~wxComboBox()
223 // delete client objects
226 // delete the controls now, don't leave them alive even though they would
227 // still be eventually deleted by our parent - but it will be too late, the
228 // user code expects them to be gone now
229 if (m_text
!= NULL
) {
233 if (m_choice
!= NULL
) {
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 wxSize
wxComboBox::DoGetBestSize() const
247 return wxControl::DoGetBestSize();
249 wxSize size
= m_choice
->GetBestSize();
251 if ( m_text
!= NULL
)
253 wxSize sizeText
= m_text
->GetBestSize();
255 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
262 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
264 wxControl::DoMoveWindow(x
, y
, width
, height
);
266 height
= POPUPHEIGHT
;
268 wxControl::DoMoveWindow(x
, y
, width
, height
);
270 if ( m_text
== NULL
)
272 // we might not be fully constructed yet, therefore watch out...
274 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
278 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
279 m_text
->SetSize(0, 0, wText
, height
);
280 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
287 // ----------------------------------------------------------------------------
288 // operations forwarded to the subcontrols
289 // ----------------------------------------------------------------------------
291 bool wxComboBox::Enable(bool enable
)
293 if ( !wxControl::Enable(enable
) )
299 bool wxComboBox::Show(bool show
)
301 if ( !wxControl::Show(show
) )
307 void wxComboBox::SetFocus()
310 wxControl::SetFocus();
312 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
)
359 m_macIsUserPane
= false;
361 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
362 wxDefaultValidator
, name
) )
367 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
370 hiRect
.origin
.x
= 20; //bounds.left;
371 hiRect
.origin
.y
= 25; //bounds.top;
372 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
373 hiRect
.size
.height
= 24;
375 //For some reason, this code causes the combo box not to be displayed at all.
376 //hiRect.origin.x = bounds.left;
377 //hiRect.origin.y = bounds.top;
378 //hiRect.size.width = bounds.right - bounds.left;
379 //hiRect.size.height = bounds.bottom - bounds.top;
380 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
381 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
382 m_peer
= new wxMacControl(this);
383 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
386 m_peer
->SetMinimum( 0 );
387 m_peer
->SetMaximum( 100);
389 m_peer
->SetValue( 1 );
391 MacPostControlCreate(pos
,size
);
393 for ( int i
= 0; i
< n
; i
++ )
395 DoAppend( choices
[ i
] );
398 HIViewSetVisible( m_peer
->GetControlRef(), true );
400 EventHandlerRef comboEventHandler
;
401 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
402 GetEventTypeCount(eventList
), eventList
, this,
403 (EventHandlerRef
*)&comboEventHandler
);
405 m_choice
= new wxComboBoxChoice(this, style
);
407 m_choice
= new wxComboBoxChoice(this, style
);
408 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
411 if ( style
& wxCB_READONLY
)
417 m_text
= new wxComboBoxText(this);
418 if ( size
.y
== wxDefaultCoord
) {
419 csize
.y
= m_text
->GetSize().y
;
423 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
425 for ( int i
= 0; i
< n
; i
++ )
427 m_choice
->DoAppend( choices
[ i
] );
429 SetBestSize(csize
); // Needed because it is a wxControlWithItems
435 wxString
wxComboBox::GetValue() const
438 CFStringRef myString
;
439 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
440 return wxMacCFStringHolder( myString
, m_font
.GetEncoding() ).AsString();
444 if ( m_text
== NULL
)
446 result
= m_choice
->GetString( m_choice
->GetSelection() );
450 result
= m_text
->GetValue();
457 void wxComboBox::SetValue(const wxString
& value
)
462 int s
= FindString (value
);
463 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
465 m_choice
->Append(value
);
467 SetStringSelection( value
);
471 // Clipboard operations
472 void wxComboBox::Copy()
474 if ( m_text
!= NULL
)
480 void wxComboBox::Cut()
482 if ( m_text
!= NULL
)
488 void wxComboBox::Paste()
490 if ( m_text
!= NULL
)
496 void wxComboBox::SetEditable(bool editable
)
498 if ( ( m_text
== NULL
) && editable
)
500 m_text
= new wxComboBoxText( this );
502 else if ( ( m_text
!= NULL
) && !editable
)
508 int currentX
, currentY
;
509 GetPosition( ¤tX
, ¤tY
);
511 int currentW
, currentH
;
512 GetSize( ¤tW
, ¤tH
);
514 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
517 void wxComboBox::SetInsertionPoint(long pos
)
522 void wxComboBox::SetInsertionPointEnd()
527 long wxComboBox::GetInsertionPoint() const
533 wxTextPos
wxComboBox::GetLastPosition() const
539 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
544 void wxComboBox::Remove(long from
, long to
)
549 void wxComboBox::SetSelection(long from
, long to
)
554 int wxComboBox::DoAppend(const wxString
& item
)
558 HIComboBoxAppendTextItem( m_peer
->GetControlRef(), wxMacCFStringHolder( item
, m_font
.GetEncoding() ), &outIndex
);
559 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
560 return (int) outIndex
;
562 return m_choice
->DoAppend( item
);
566 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
569 HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)pos
, wxMacCFStringHolder(item
, m_font
.GetEncoding()) );
571 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
575 return m_choice
->DoInsert( item
, pos
);
579 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
584 return m_choice
->DoSetItemClientData( n
, clientData
);
588 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
593 return m_choice
->DoGetItemClientData( n
);
597 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
602 return m_choice
->DoSetItemClientObject( n
, clientData
);
606 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
611 return m_choice
->DoGetItemClientObject( n
);
615 void wxComboBox::FreeData()
617 if (HasClientObjectData())
619 unsigned int count
= GetCount();
620 for ( unsigned int n
= 0; n
< count
; n
++ )
622 SetClientObject( n
, NULL
);
627 unsigned int wxComboBox::GetCount() const {
629 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
631 return m_choice
->GetCount();
635 void wxComboBox::Delete(unsigned int n
)
638 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
640 // force client object deletion
641 if( HasClientObjectData() )
642 SetClientObject( n
, NULL
);
643 m_choice
->Delete( n
);
647 void wxComboBox::Clear()
651 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
652 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
653 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
659 int wxComboBox::GetSelection() const
662 return FindString( GetStringSelection() );
664 return m_choice
->GetSelection();
668 void wxComboBox::SetSelection(int n
)
671 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
673 m_choice
->SetSelection( n
);
675 if ( m_text
!= NULL
)
677 m_text
->SetValue(GetString(n
));
682 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
685 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
687 if (GetString(i
).IsSameAs(s
, bCase
) )
692 return m_choice
->FindString( s
, bCase
);
696 wxString
wxComboBox::GetString(unsigned int n
) const
699 CFStringRef itemText
;
700 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
701 return wxMacCFStringHolder(itemText
).AsString();
703 return m_choice
->GetString( n
);
707 wxString
wxComboBox::GetStringSelection() const
710 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
712 int sel
= GetSelection ();
713 if (sel
!= wxNOT_FOUND
)
714 return wxString(this->GetString((unsigned int)sel
));
716 return wxEmptyString
;
720 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
723 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
724 wxMacCFStringHolder(s
, m_font
.GetEncoding()) ) );
725 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
727 m_choice
->SetString( n
, s
);
731 bool wxComboBox::IsEditable() const
735 return !HasFlag(wxCB_READONLY
);
737 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
741 void wxComboBox::Undo()
751 void wxComboBox::Redo()
761 void wxComboBox::SelectAll()
771 bool wxComboBox::CanCopy() const
778 return m_text
->CanCopy();
784 bool wxComboBox::CanCut() const
791 return m_text
->CanCut();
797 bool wxComboBox::CanPaste() const
804 return m_text
->CanPaste();
810 bool wxComboBox::CanUndo() const
817 return m_text
->CanUndo();
823 bool wxComboBox::CanRedo() const
830 return m_text
->CanRedo();
836 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
838 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
839 event
.SetInt(GetSelection());
840 event
.SetEventObject(this);
841 event
.SetString(GetStringSelection());
842 ProcessCommand(event
);