1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/uma.h"
22 #if TARGET_API_MAC_OSX
24 #include <HIToolbox/HIView.h>
28 #if TARGET_API_MAC_OSX
29 #define USE_HICOMBOBOX 1 //use hi combobox define
31 #define USE_HICOMBOBOX 0
34 static int nextPopUpMenuId
= 1000;
35 MenuHandle
NewUniqueMenu()
37 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" );
43 static const EventTypeSpec eventList
[] =
45 { kEventClassTextField
, kEventTextAccepted
} ,
48 static pascal OSStatus
wxMacComboBoxEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
50 OSStatus result
= eventNotHandledErr
;
51 wxComboBox
* cb
= (wxComboBox
*) data
;
53 wxMacCarbonEvent
cEvent( event
);
55 switch( cEvent
.GetClass() )
57 case kEventClassTextField
:
58 switch( cEvent
.GetKind() )
60 case kEventTextAccepted
:
62 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, cb
->GetId() );
63 event
.SetInt( cb
->GetSelection() );
64 event
.SetString( cb
->GetStringSelection() );
65 event
.SetEventObject( cb
);
66 cb
->HandleWindowEvent( event
);
81 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the margin between the text control and the choice
90 static const wxCoord MARGIN
= 2;
91 #if TARGET_API_MAC_OSX
92 static const int POPUPWIDTH
= 24;
94 static const int POPUPWIDTH
= 18;
96 static const int POPUPHEIGHT
= 23;
98 // ----------------------------------------------------------------------------
99 // wxComboBoxText: text control forwards events to combobox
100 // ----------------------------------------------------------------------------
102 class wxComboBoxText
: public wxTextCtrl
105 wxComboBoxText( wxComboBox
* cb
)
106 : wxTextCtrl( cb
, 1 )
112 void OnChar( wxKeyEvent
& event
)
114 if ( event
.GetKeyCode() == WXK_RETURN
)
116 wxString value
= GetValue();
118 if ( m_cb
->GetCount() == 0 )
120 // make Enter generate "selected" event if there is only one item
121 // in the combobox - without it, it's impossible to select it at
123 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
125 event
.SetString( value
);
126 event
.SetEventObject( m_cb
);
127 m_cb
->HandleWindowEvent( event
);
131 // add the item to the list if it's not there yet
132 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
135 m_cb
->SetStringSelection(value
);
137 // and generate the selected event for it
138 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
139 event
.SetInt( m_cb
->GetCount() - 1 );
140 event
.SetString( value
);
141 event
.SetEventObject( m_cb
);
142 m_cb
->HandleWindowEvent( event
);
145 // This will invoke the dialog default action, such
146 // as the clicking the default button.
148 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
149 if ( tlw
&& tlw
->GetDefaultItem() )
151 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
152 if ( def
&& def
->IsEnabled() )
154 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
155 event
.SetEventObject(def
);
170 DECLARE_EVENT_TABLE()
173 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
174 EVT_CHAR( wxComboBoxText::OnChar
)
177 class wxComboBoxChoice
: public wxChoice
180 wxComboBoxChoice(wxComboBox
*cb
, int style
)
187 void OnChoice( wxCommandEvent
& e
)
189 wxString s
= e
.GetString();
191 m_cb
->DelegateChoice( s
);
192 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
193 event2
.SetInt(m_cb
->GetSelection());
194 event2
.SetEventObject(m_cb
);
195 event2
.SetString(m_cb
->GetStringSelection());
196 m_cb
->ProcessCommand(event2
);
198 virtual wxSize
DoGetBestSize() const
200 wxSize sz
= wxChoice::DoGetBestSize();
208 DECLARE_EVENT_TABLE()
211 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
212 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
215 wxComboBox::~wxComboBox()
217 // delete the controls now, don't leave them alive even though they would
218 // still be eventually deleted by our parent - but it will be too late, the
219 // user code expects them to be gone now
221 wxDELETE( m_choice
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 wxSize
wxComboBox::DoGetBestSize() const
232 return wxControl::DoGetBestSize();
234 wxSize size
= m_choice
->GetBestSize();
236 if ( m_text
!= NULL
)
238 wxSize sizeText
= m_text
->GetBestSize();
240 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
247 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
249 wxControl::DoMoveWindow(x
, y
, width
, height
);
251 height
= POPUPHEIGHT
;
253 wxControl::DoMoveWindow(x
, y
, width
, height
);
255 if ( m_text
== NULL
)
257 // we might not be fully constructed yet, therefore watch out...
259 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
263 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
264 m_text
->SetSize(0, 0, wText
, height
);
265 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
272 // ----------------------------------------------------------------------------
273 // operations forwarded to the subcontrols
274 // ----------------------------------------------------------------------------
276 bool wxComboBox::Enable(bool enable
)
278 if ( !wxControl::Enable(enable
) )
284 bool wxComboBox::Show(bool show
)
286 if ( !wxControl::Show(show
) )
292 void wxComboBox::SetFocus()
295 wxControl::SetFocus();
297 if ( m_text
!= NULL
) {
304 void wxComboBox::DelegateTextChanged( const wxString
& value
)
306 SetStringSelection( value
);
310 void wxComboBox::DelegateChoice( const wxString
& value
)
312 SetStringSelection( value
);
316 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
317 const wxString
& value
,
320 const wxArrayString
& choices
,
322 const wxValidator
& validator
,
323 const wxString
& name
)
325 wxCArrayString
chs( choices
);
327 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
328 chs
.GetStrings(), style
, validator
, name
);
332 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
333 const wxString
& value
,
336 int n
, const wxString choices
[],
338 const wxValidator
& validator
,
339 const wxString
& name
)
346 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
347 wxDefaultValidator
, name
) )
352 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
355 hiRect
.origin
.x
= 20; //bounds.left;
356 hiRect
.origin
.y
= 25; //bounds.top;
357 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
358 hiRect
.size
.height
= 24;
360 //For some reason, this code causes the combo box not to be displayed at all.
361 //hiRect.origin.x = bounds.left;
362 //hiRect.origin.y = bounds.top;
363 //hiRect.size.width = bounds.right - bounds.left;
364 //hiRect.size.height = bounds.bottom - bounds.top;
365 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
366 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
367 m_peer
= new wxMacControl(this);
368 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
371 m_peer
->SetMinimum( 0 );
372 m_peer
->SetMaximum( 100);
374 m_peer
->SetValue( 1 );
376 MacPostControlCreate(pos
,size
);
378 Append( choices
[ i
] );
380 HIViewSetVisible( m_peer
->GetControlRef(), true );
382 EventHandlerRef comboEventHandler
;
383 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
384 GetEventTypeCount(eventList
), eventList
, this,
385 (EventHandlerRef
*)&comboEventHandler
);
387 m_choice
= new wxComboBoxChoice(this, style
);
388 m_choice
->SetMinSize( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
391 if ( style
& wxCB_READONLY
)
397 m_text
= new wxComboBoxText(this);
398 if ( size
.y
== wxDefaultCoord
) {
399 csize
.y
= m_text
->GetSize().y
;
403 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
405 m_choice
->Append( n
, choices
);
406 SetInitialSize(csize
); // Needed because it is a wxControlWithItems
412 wxString
wxComboBox::GetValue() const
415 CFStringRef myString
;
416 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
417 return wxMacCFStringHolder( myString
, GetFont().GetEncoding() ).AsString();
421 if ( m_text
== NULL
)
423 result
= m_choice
->GetString( m_choice
->GetSelection() );
427 result
= m_text
->GetValue();
434 void wxComboBox::SetValue(const wxString
& value
)
439 int s
= FindString (value
);
440 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
442 m_choice
->Append(value
);
444 SetStringSelection( value
);
448 // Clipboard operations
449 void wxComboBox::Copy()
451 if ( m_text
!= NULL
)
457 void wxComboBox::Cut()
459 if ( m_text
!= NULL
)
465 void wxComboBox::Paste()
467 if ( m_text
!= NULL
)
473 void wxComboBox::SetEditable(bool editable
)
475 if ( ( m_text
== NULL
) && editable
)
477 m_text
= new wxComboBoxText( this );
479 else if ( !editable
)
484 int currentX
, currentY
;
485 GetPosition( ¤tX
, ¤tY
);
487 int currentW
, currentH
;
488 GetSize( ¤tW
, ¤tH
);
490 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
493 void wxComboBox::SetInsertionPoint(long pos
)
498 void wxComboBox::SetInsertionPointEnd()
503 long wxComboBox::GetInsertionPoint() const
509 wxTextPos
wxComboBox::GetLastPosition() const
515 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
520 void wxComboBox::Remove(long from
, long to
)
525 void wxComboBox::SetSelection(long from
, long to
)
530 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
532 void **clientData
, wxClientDataType type
)
535 const unsigned int count
= items
.GetCount();
536 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
538 HIComboBoxInsertTextItemAtIndex(m_peer
->GetControlRef(),
540 wxMacCFStringHolder(items
[i
],
541 GetFont().GetEncoding()));
542 AssignNewItemClientData(pos
, clientData
, i
, type
);
545 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
549 return m_choice
->DoInsertItems( items
, pos
, clientData
, type
);
553 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
558 return m_choice
->DoSetItemClientData( n
, clientData
);
562 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
567 return m_choice
->DoGetItemClientData( n
);
571 unsigned int wxComboBox::GetCount() const {
573 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
575 return m_choice
->GetCount();
579 void wxComboBox::DoDeleteOneItem(unsigned int n
)
582 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
584 m_choice
->Delete( n
);
588 void wxComboBox::DoClear()
591 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
592 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
593 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
599 int wxComboBox::GetSelection() const
602 return FindString( GetStringSelection() );
604 return m_choice
->GetSelection();
608 void wxComboBox::SetSelection(int n
)
611 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
613 m_choice
->SetSelection( n
);
615 if ( m_text
!= NULL
)
617 m_text
->SetValue(GetString(n
));
622 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
625 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
627 if (GetString(i
).IsSameAs(s
, bCase
) )
632 return m_choice
->FindString( s
, bCase
);
636 wxString
wxComboBox::GetString(unsigned int n
) const
639 CFStringRef itemText
;
640 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
641 return wxMacCFStringHolder(itemText
).AsString();
643 return m_choice
->GetString( n
);
647 wxString
wxComboBox::GetStringSelection() const
650 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
652 int sel
= GetSelection ();
653 if (sel
!= wxNOT_FOUND
)
654 return wxString(this->GetString((unsigned int)sel
));
656 return wxEmptyString
;
660 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
663 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
664 wxMacCFStringHolder(s
, GetFont().GetEncoding()) ) );
665 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
667 m_choice
->SetString( n
, s
);
671 bool wxComboBox::IsEditable() const
675 return !HasFlag(wxCB_READONLY
);
677 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
681 void wxComboBox::Undo()
691 void wxComboBox::Redo()
701 void wxComboBox::SelectAll()
711 bool wxComboBox::CanCopy() const
718 return m_text
->CanCopy();
724 bool wxComboBox::CanCut() const
731 return m_text
->CanCut();
737 bool wxComboBox::CanPaste() const
744 return m_text
->CanPaste();
750 bool wxComboBox::CanUndo() const
757 return m_text
->CanUndo();
763 bool wxComboBox::CanRedo() const
770 return m_text
->CanRedo();
776 bool wxComboBox::OSXHandleClicked( double timestampsec
)
778 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
779 event
.SetInt(GetSelection());
780 event
.SetEventObject(this);
781 event
.SetString(GetStringSelection());
782 ProcessCommand(event
);