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 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
151 if ( tlw
&& tlw
->GetDefaultItem() )
153 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
154 if ( def
&& def
->IsEnabled() )
156 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
157 event
.SetEventObject(def
);
172 DECLARE_EVENT_TABLE()
175 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
176 EVT_CHAR( wxComboBoxText::OnChar
)
179 class wxComboBoxChoice
: public wxChoice
182 wxComboBoxChoice(wxComboBox
*cb
, int style
)
189 void OnChoice( wxCommandEvent
& e
)
191 wxString s
= e
.GetString();
193 m_cb
->DelegateChoice( s
);
194 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
195 event2
.SetInt(m_cb
->GetSelection());
196 event2
.SetEventObject(m_cb
);
197 event2
.SetString(m_cb
->GetStringSelection());
198 m_cb
->ProcessCommand(event2
);
200 virtual wxSize
DoGetBestSize() const
202 wxSize sz
= wxChoice::DoGetBestSize();
210 DECLARE_EVENT_TABLE()
213 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
214 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
217 wxComboBox::~wxComboBox()
219 // delete client objects
222 // delete the controls now, don't leave them alive even though they would
223 // still be eventually deleted by our parent - but it will be too late, the
224 // user code expects them to be gone now
225 if (m_text
!= NULL
) {
229 if (m_choice
!= NULL
) {
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 wxSize
wxComboBox::DoGetBestSize() const
243 return wxControl::DoGetBestSize();
245 wxSize size
= m_choice
->GetBestSize();
247 if ( m_text
!= NULL
)
249 wxSize sizeText
= m_text
->GetBestSize();
251 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
258 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
260 wxControl::DoMoveWindow(x
, y
, width
, height
);
262 height
= POPUPHEIGHT
;
264 wxControl::DoMoveWindow(x
, y
, width
, height
);
266 if ( m_text
== NULL
)
268 // we might not be fully constructed yet, therefore watch out...
270 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
274 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
275 m_text
->SetSize(0, 0, wText
, height
);
276 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
283 // ----------------------------------------------------------------------------
284 // operations forwarded to the subcontrols
285 // ----------------------------------------------------------------------------
287 bool wxComboBox::Enable(bool enable
)
289 if ( !wxControl::Enable(enable
) )
295 bool wxComboBox::Show(bool show
)
297 if ( !wxControl::Show(show
) )
303 void wxComboBox::SetFocus()
306 wxControl::SetFocus();
308 if ( m_text
!= NULL
) {
315 void wxComboBox::DelegateTextChanged( const wxString
& value
)
317 SetStringSelection( value
);
321 void wxComboBox::DelegateChoice( const wxString
& value
)
323 SetStringSelection( value
);
327 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
328 const wxString
& value
,
331 const wxArrayString
& choices
,
333 const wxValidator
& validator
,
334 const wxString
& name
)
336 wxCArrayString
chs( choices
);
338 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
339 chs
.GetStrings(), style
, validator
, name
);
343 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
344 const wxString
& value
,
347 int n
, const wxString choices
[],
349 const wxValidator
& validator
,
350 const wxString
& name
)
355 m_macIsUserPane
= false;
357 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
358 wxDefaultValidator
, name
) )
363 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
366 hiRect
.origin
.x
= 20; //bounds.left;
367 hiRect
.origin
.y
= 25; //bounds.top;
368 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
369 hiRect
.size
.height
= 24;
371 //For some reason, this code causes the combo box not to be displayed at all.
372 //hiRect.origin.x = bounds.left;
373 //hiRect.origin.y = bounds.top;
374 //hiRect.size.width = bounds.right - bounds.left;
375 //hiRect.size.height = bounds.bottom - bounds.top;
376 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
377 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
378 m_peer
= new wxMacControl(this);
379 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
382 m_peer
->SetMinimum( 0 );
383 m_peer
->SetMaximum( 100);
385 m_peer
->SetValue( 1 );
387 MacPostControlCreate(pos
,size
);
389 for ( int i
= 0; i
< n
; i
++ )
391 DoAppend( choices
[ i
] );
394 HIViewSetVisible( m_peer
->GetControlRef(), true );
396 EventHandlerRef comboEventHandler
;
397 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
398 GetEventTypeCount(eventList
), eventList
, this,
399 (EventHandlerRef
*)&comboEventHandler
);
401 m_choice
= new wxComboBoxChoice(this, style
);
403 m_choice
= new wxComboBoxChoice(this, style
);
404 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
407 if ( style
& wxCB_READONLY
)
413 m_text
= new wxComboBoxText(this);
414 if ( size
.y
== wxDefaultCoord
) {
415 csize
.y
= m_text
->GetSize().y
;
419 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
421 for ( int i
= 0; i
< n
; i
++ )
423 m_choice
->DoAppend( choices
[ i
] );
425 SetBestSize(csize
); // Needed because it is a wxControlWithItems
431 wxString
wxComboBox::GetValue() const
434 CFStringRef myString
;
435 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
436 return wxMacCFStringHolder( myString
, m_font
.GetEncoding() ).AsString();
440 if ( m_text
== NULL
)
442 result
= m_choice
->GetString( m_choice
->GetSelection() );
446 result
= m_text
->GetValue();
453 void wxComboBox::SetValue(const wxString
& value
)
458 int s
= FindString (value
);
459 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
461 m_choice
->Append(value
);
463 SetStringSelection( value
);
467 // Clipboard operations
468 void wxComboBox::Copy()
470 if ( m_text
!= NULL
)
476 void wxComboBox::Cut()
478 if ( m_text
!= NULL
)
484 void wxComboBox::Paste()
486 if ( m_text
!= NULL
)
492 void wxComboBox::SetEditable(bool editable
)
494 if ( ( m_text
== NULL
) && editable
)
496 m_text
= new wxComboBoxText( this );
498 else if ( ( m_text
!= NULL
) && !editable
)
504 int currentX
, currentY
;
505 GetPosition( ¤tX
, ¤tY
);
507 int currentW
, currentH
;
508 GetSize( ¤tW
, ¤tH
);
510 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
513 void wxComboBox::SetInsertionPoint(long pos
)
518 void wxComboBox::SetInsertionPointEnd()
523 long wxComboBox::GetInsertionPoint() const
529 wxTextPos
wxComboBox::GetLastPosition() const
535 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
540 void wxComboBox::Remove(long from
, long to
)
545 void wxComboBox::SetSelection(long from
, long to
)
550 int wxComboBox::DoAppend(const wxString
& item
)
554 HIComboBoxAppendTextItem( m_peer
->GetControlRef(), wxMacCFStringHolder( item
, m_font
.GetEncoding() ), &outIndex
);
555 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
556 return (int) outIndex
;
558 return m_choice
->DoAppend( item
);
562 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
565 HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)pos
, wxMacCFStringHolder(item
, m_font
.GetEncoding()) );
567 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
571 return m_choice
->DoInsert( item
, pos
);
575 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
580 return m_choice
->DoSetItemClientData( n
, clientData
);
584 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
589 return m_choice
->DoGetItemClientData( n
);
593 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
598 return m_choice
->DoSetItemClientObject( n
, clientData
);
602 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
607 return m_choice
->DoGetItemClientObject( n
);
611 void wxComboBox::FreeData()
613 if (HasClientObjectData())
615 unsigned int count
= GetCount();
616 for ( unsigned int n
= 0; n
< count
; n
++ )
618 SetClientObject( n
, NULL
);
623 unsigned int wxComboBox::GetCount() const {
625 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
627 return m_choice
->GetCount();
631 void wxComboBox::Delete(unsigned int n
)
634 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
636 // force client object deletion
637 if( HasClientObjectData() )
638 SetClientObject( n
, NULL
);
639 m_choice
->Delete( n
);
643 void wxComboBox::Clear()
647 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
648 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
649 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
655 int wxComboBox::GetSelection() const
658 return FindString( GetStringSelection() );
660 return m_choice
->GetSelection();
664 void wxComboBox::SetSelection(int n
)
667 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
669 m_choice
->SetSelection( n
);
671 if ( m_text
!= NULL
)
673 m_text
->SetValue(GetString(n
));
678 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
681 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
683 if (GetString(i
).IsSameAs(s
, bCase
) )
688 return m_choice
->FindString( s
, bCase
);
692 wxString
wxComboBox::GetString(unsigned int n
) const
695 CFStringRef itemText
;
696 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
697 return wxMacCFStringHolder(itemText
).AsString();
699 return m_choice
->GetString( n
);
703 wxString
wxComboBox::GetStringSelection() const
706 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
708 int sel
= GetSelection ();
709 if (sel
!= wxNOT_FOUND
)
710 return wxString(this->GetString((unsigned int)sel
));
712 return wxEmptyString
;
716 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
719 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
720 wxMacCFStringHolder(s
, m_font
.GetEncoding()) ) );
721 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
723 m_choice
->SetString( n
, s
);
727 bool wxComboBox::IsEditable() const
731 return !HasFlag(wxCB_READONLY
);
733 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
737 void wxComboBox::Undo()
747 void wxComboBox::Redo()
757 void wxComboBox::SelectAll()
767 bool wxComboBox::CanCopy() const
774 return m_text
->CanCopy();
780 bool wxComboBox::CanCut() const
787 return m_text
->CanCut();
793 bool wxComboBox::CanPaste() const
800 return m_text
->CanPaste();
806 bool wxComboBox::CanUndo() const
813 return m_text
->CanUndo();
819 bool wxComboBox::CanRedo() const
826 return m_text
->CanRedo();
832 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
834 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
835 event
.SetInt(GetSelection());
836 event
.SetEventObject(this);
837 event
.SetString(GetStringSelection());
838 ProcessCommand(event
);