1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/combobxc.cpp
3 // Purpose: wxComboBox class using HIView ComboBox
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/combobox.h"
16 #include "wx/button.h"
20 #include "wx/osx/uma.h"
21 #if TARGET_API_MAC_OSX
23 #include <HIToolbox/HIView.h>
27 #if TARGET_API_MAC_OSX
28 #define USE_HICOMBOBOX 1 //use hi combobox define
30 #define USE_HICOMBOBOX 0
33 static int nextPopUpMenuId
= 1000;
34 MenuHandle
NewUniqueMenu()
36 MenuHandle handle
= NewMenu( nextPopUpMenuId
, "\pMenu" );
42 static const EventTypeSpec eventList
[] =
44 { kEventClassTextField
, kEventTextAccepted
} ,
47 static pascal OSStatus
wxMacComboBoxEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
49 OSStatus result
= eventNotHandledErr
;
50 wxComboBox
* cb
= (wxComboBox
*) data
;
52 wxMacCarbonEvent
cEvent( event
);
54 switch( cEvent
.GetClass() )
56 case kEventClassTextField
:
57 switch( cEvent
.GetKind() )
59 case kEventTextAccepted
:
61 wxCommandEvent
event( wxEVT_COMBOBOX
, cb
->GetId() );
62 event
.SetInt( cb
->GetSelection() );
63 event
.SetString( cb
->GetStringSelection() );
64 event
.SetEventObject( cb
);
65 cb
->HandleWindowEvent( event
);
80 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // the margin between the text control and the choice
89 static const wxCoord MARGIN
= 2;
90 #if TARGET_API_MAC_OSX
91 static const int POPUPWIDTH
= 24;
93 static const int POPUPWIDTH
= 18;
95 static const int POPUPHEIGHT
= 23;
97 // ----------------------------------------------------------------------------
98 // wxComboBoxText: text control forwards events to combobox
99 // ----------------------------------------------------------------------------
101 class wxComboBoxText
: public wxTextCtrl
104 wxComboBoxText( wxComboBox
* cb
)
105 : wxTextCtrl( cb
, 1 )
111 void OnChar( wxKeyEvent
& event
)
113 if ( event
.GetKeyCode() == WXK_RETURN
)
115 wxString value
= GetValue();
117 if ( m_cb
->GetCount() == 0 )
119 // make Enter generate "selected" event if there is only one item
120 // in the combobox - without it, it's impossible to select it at
122 wxCommandEvent
event( wxEVT_COMBOBOX
, m_cb
->GetId() );
124 event
.SetString( value
);
125 event
.SetEventObject( m_cb
);
126 m_cb
->HandleWindowEvent( event
);
130 // add the item to the list if it's not there yet
131 if ( m_cb
->FindString(value
) == wxNOT_FOUND
)
134 m_cb
->SetStringSelection(value
);
136 // and generate the selected event for it
137 wxCommandEvent
event( wxEVT_COMBOBOX
, m_cb
->GetId() );
138 event
.SetInt( m_cb
->GetCount() - 1 );
139 event
.SetString( value
);
140 event
.SetEventObject( m_cb
);
141 m_cb
->HandleWindowEvent( event
);
144 // This will invoke the dialog default action, such
145 // as the clicking the default button.
147 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
148 if ( tlw
&& tlw
->GetDefaultItem() )
150 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
151 if ( def
&& def
->IsEnabled() )
153 wxCommandEvent
event(wxEVT_BUTTON
, def
->GetId() );
154 event
.SetEventObject(def
);
169 DECLARE_EVENT_TABLE()
172 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
173 EVT_CHAR( wxComboBoxText::OnChar
)
176 class wxComboBoxChoice
: public wxChoice
179 wxComboBoxChoice(wxComboBox
*cb
, int style
)
186 void OnChoice( wxCommandEvent
& e
)
188 wxString s
= e
.GetString();
190 m_cb
->DelegateChoice( s
);
191 wxCommandEvent
event2(wxEVT_COMBOBOX
, m_cb
->GetId() );
192 event2
.SetInt(m_cb
->GetSelection());
193 event2
.SetEventObject(m_cb
);
194 event2
.SetString(m_cb
->GetStringSelection());
195 m_cb
->ProcessCommand(event2
);
197 virtual wxSize
DoGetBestSize() const
199 wxSize sz
= wxChoice::DoGetBestSize();
207 DECLARE_EVENT_TABLE()
210 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
211 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
214 wxComboBox::~wxComboBox()
216 // delete the controls now, don't leave them alive even though they would
217 // still be eventually deleted by our parent - but it will be too late, the
218 // user code expects them to be gone now
220 wxDELETE( m_choice
);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 wxSize
wxComboBox::DoGetBestSize() const
231 return wxControl::DoGetBestSize();
233 wxSize size
= m_choice
->GetBestSize();
235 if ( m_text
!= NULL
)
237 wxSize sizeText
= m_text
->GetBestSize();
239 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
246 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
248 wxControl::DoMoveWindow(x
, y
, width
, height
);
250 height
= POPUPHEIGHT
;
252 wxControl::DoMoveWindow(x
, y
, width
, height
);
254 if ( m_text
== NULL
)
256 // we might not be fully constructed yet, therefore watch out...
258 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
262 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
263 m_text
->SetSize(0, 0, wText
, height
);
264 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
271 // ----------------------------------------------------------------------------
272 // operations forwarded to the subcontrols
273 // ----------------------------------------------------------------------------
275 bool wxComboBox::Enable(bool enable
)
277 if ( !wxControl::Enable(enable
) )
283 bool wxComboBox::Show(bool show
)
285 if ( !wxControl::Show(show
) )
291 void wxComboBox::SetFocus()
294 wxControl::SetFocus();
296 if ( m_text
!= NULL
) {
303 void wxComboBox::DelegateTextChanged( const wxString
& value
)
305 SetStringSelection( value
);
309 void wxComboBox::DelegateChoice( const wxString
& value
)
311 SetStringSelection( value
);
315 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
316 const wxString
& value
,
319 const wxArrayString
& choices
,
321 const wxValidator
& validator
,
322 const wxString
& name
)
324 wxCArrayString
chs( choices
);
326 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
327 chs
.GetStrings(), style
, validator
, name
);
331 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
332 const wxString
& value
,
335 int n
, const wxString choices
[],
337 const wxValidator
& validator
,
338 const wxString
& name
)
345 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
346 wxDefaultValidator
, name
) )
351 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
354 hiRect
.origin
.x
= 20; //bounds.left;
355 hiRect
.origin
.y
= 25; //bounds.top;
356 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
357 hiRect
.size
.height
= 24;
359 //For some reason, this code causes the combo box not to be displayed at all.
360 //hiRect.origin.x = bounds.left;
361 //hiRect.origin.y = bounds.top;
362 //hiRect.size.width = bounds.right - bounds.left;
363 //hiRect.size.height = bounds.bottom - bounds.top;
364 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
365 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
366 m_peer
= new wxMacControl(this);
367 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
370 m_peer
->SetMinimum( 0 );
371 m_peer
->SetMaximum( 100);
373 m_peer
->SetValue( 1 );
375 MacPostControlCreate(pos
,size
);
377 Append( choices
[ i
] );
379 HIViewSetVisible( m_peer
->GetControlRef(), true );
381 EventHandlerRef comboEventHandler
;
382 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
383 GetEventTypeCount(eventList
), eventList
, this,
384 (EventHandlerRef
*)&comboEventHandler
);
386 m_choice
= new wxComboBoxChoice(this, style
);
387 m_choice
->SetMinSize( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
390 if ( style
& wxCB_READONLY
)
396 m_text
= new wxComboBoxText(this);
397 if ( size
.y
== wxDefaultCoord
) {
398 csize
.y
= m_text
->GetSize().y
;
402 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
404 m_choice
->Append( n
, choices
);
405 SetInitialSize(csize
); // Needed because it is a wxControlWithItems
411 wxString
wxComboBox::GetValue() const
414 CFStringRef myString
;
415 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
416 return wxMacCFStringHolder( myString
, GetFont().GetEncoding() ).AsString();
420 if ( m_text
== NULL
)
422 result
= m_choice
->GetString( m_choice
->GetSelection() );
426 result
= m_text
->GetValue();
433 void wxComboBox::SetValue(const wxString
& value
)
438 int s
= FindString (value
);
439 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
441 m_choice
->Append(value
);
443 SetStringSelection( value
);
447 // Clipboard operations
448 void wxComboBox::Copy()
450 if ( m_text
!= NULL
)
456 void wxComboBox::Cut()
458 if ( m_text
!= NULL
)
464 void wxComboBox::Paste()
466 if ( m_text
!= NULL
)
472 void wxComboBox::SetEditable(bool editable
)
474 if ( ( m_text
== NULL
) && editable
)
476 m_text
= new wxComboBoxText( this );
478 else if ( !editable
)
483 int currentX
, currentY
;
484 GetPosition( ¤tX
, ¤tY
);
486 int currentW
, currentH
;
487 GetSize( ¤tW
, ¤tH
);
489 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
492 void wxComboBox::SetInsertionPoint(long pos
)
497 void wxComboBox::SetInsertionPointEnd()
502 long wxComboBox::GetInsertionPoint() const
508 wxTextPos
wxComboBox::GetLastPosition() const
514 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
519 void wxComboBox::Remove(long from
, long to
)
524 void wxComboBox::SetSelection(long from
, long to
)
529 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
531 void **clientData
, wxClientDataType type
)
534 const unsigned int count
= items
.GetCount();
535 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
537 HIComboBoxInsertTextItemAtIndex(m_peer
->GetControlRef(),
539 wxMacCFStringHolder(items
[i
],
540 GetFont().GetEncoding()));
541 AssignNewItemClientData(pos
, clientData
, i
, type
);
544 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
548 return m_choice
->DoInsertItems( items
, pos
, clientData
, type
);
552 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
557 return m_choice
->DoSetItemClientData( n
, clientData
);
561 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
566 return m_choice
->DoGetItemClientData( n
);
570 unsigned int wxComboBox::GetCount() const {
572 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
574 return m_choice
->GetCount();
578 void wxComboBox::DoDeleteOneItem(unsigned int n
)
581 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
583 m_choice
->Delete( n
);
587 void wxComboBox::DoClear()
590 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
591 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
592 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
598 int wxComboBox::GetSelection() const
601 return FindString( GetStringSelection() );
603 return m_choice
->GetSelection();
607 void wxComboBox::SetSelection(int n
)
610 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
612 m_choice
->SetSelection( n
);
614 if ( m_text
!= NULL
)
616 m_text
->SetValue(GetString(n
));
621 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
624 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
626 if (GetString(i
).IsSameAs(s
, bCase
) )
631 return m_choice
->FindString( s
, bCase
);
635 wxString
wxComboBox::GetString(unsigned int n
) const
638 CFStringRef itemText
;
639 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
640 return wxMacCFStringHolder(itemText
).AsString();
642 return m_choice
->GetString( n
);
646 wxString
wxComboBox::GetStringSelection() const
649 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
651 int sel
= GetSelection ();
652 if (sel
!= wxNOT_FOUND
)
653 return wxString(this->GetString((unsigned int)sel
));
655 return wxEmptyString
;
659 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
662 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
663 wxMacCFStringHolder(s
, GetFont().GetEncoding()) ) );
664 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
666 m_choice
->SetString( n
, s
);
670 bool wxComboBox::IsEditable() const
674 return !HasFlag(wxCB_READONLY
);
676 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
680 void wxComboBox::Undo()
690 void wxComboBox::Redo()
700 void wxComboBox::SelectAll()
710 bool wxComboBox::CanCopy() const
717 return m_text
->CanCopy();
723 bool wxComboBox::CanCut() const
730 return m_text
->CanCut();
736 bool wxComboBox::CanPaste() const
743 return m_text
->CanPaste();
749 bool wxComboBox::CanUndo() const
756 return m_text
->CanUndo();
762 bool wxComboBox::CanRedo() const
769 return m_text
->CanRedo();
775 bool wxComboBox::OSXHandleClicked( double timestampsec
)
777 wxCommandEvent
event(wxEVT_COMBOBOX
, m_windowId
);
778 event
.SetInt(GetSelection());
779 event
.SetEventObject(this);
780 event
.SetString(GetStringSelection());
781 ProcessCommand(event
);