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 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
->HandleWindowEvent( 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
->HandleWindowEvent( 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
->HandleWindowEvent( 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 the controls now, don't leave them alive even though they would
220 // still be eventually deleted by our parent - but it will be too late, the
221 // user code expects them to be gone now
223 wxDELETE( m_choice
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 wxSize
wxComboBox::DoGetBestSize() const
234 return wxControl::DoGetBestSize();
236 wxSize size
= m_choice
->GetBestSize();
238 if ( m_text
!= NULL
)
240 wxSize sizeText
= m_text
->GetBestSize();
242 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
249 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
251 wxControl::DoMoveWindow(x
, y
, width
, height
);
253 height
= POPUPHEIGHT
;
255 wxControl::DoMoveWindow(x
, y
, width
, height
);
257 if ( m_text
== NULL
)
259 // we might not be fully constructed yet, therefore watch out...
261 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
265 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
266 m_text
->SetSize(0, 0, wText
, height
);
267 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
274 // ----------------------------------------------------------------------------
275 // operations forwarded to the subcontrols
276 // ----------------------------------------------------------------------------
278 bool wxComboBox::Enable(bool enable
)
280 if ( !wxControl::Enable(enable
) )
286 bool wxComboBox::Show(bool show
)
288 if ( !wxControl::Show(show
) )
294 void wxComboBox::SetFocus()
297 wxControl::SetFocus();
299 if ( m_text
!= NULL
) {
306 void wxComboBox::DelegateTextChanged( const wxString
& value
)
308 SetStringSelection( value
);
312 void wxComboBox::DelegateChoice( const wxString
& value
)
314 SetStringSelection( value
);
318 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
319 const wxString
& value
,
322 const wxArrayString
& choices
,
324 const wxValidator
& validator
,
325 const wxString
& name
)
327 wxCArrayString
chs( choices
);
329 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
330 chs
.GetStrings(), style
, validator
, name
);
334 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
335 const wxString
& value
,
338 int n
, const wxString choices
[],
340 const wxValidator
& validator
,
341 const wxString
& name
)
346 m_macIsUserPane
= false;
348 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
349 wxDefaultValidator
, name
) )
354 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
357 hiRect
.origin
.x
= 20; //bounds.left;
358 hiRect
.origin
.y
= 25; //bounds.top;
359 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
360 hiRect
.size
.height
= 24;
362 //For some reason, this code causes the combo box not to be displayed at all.
363 //hiRect.origin.x = bounds.left;
364 //hiRect.origin.y = bounds.top;
365 //hiRect.size.width = bounds.right - bounds.left;
366 //hiRect.size.height = bounds.bottom - bounds.top;
367 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
368 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
369 m_peer
= new wxMacControl(this);
370 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
373 m_peer
->SetMinimum( 0 );
374 m_peer
->SetMaximum( 100);
376 m_peer
->SetValue( 1 );
378 MacPostControlCreate(pos
,size
);
380 Append( choices
[ i
] );
382 HIViewSetVisible( m_peer
->GetControlRef(), true );
384 EventHandlerRef comboEventHandler
;
385 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
386 GetEventTypeCount(eventList
), eventList
, this,
387 (EventHandlerRef
*)&comboEventHandler
);
389 m_choice
= new wxComboBoxChoice(this, style
);
390 m_choice
->SetMinSize( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
393 if ( style
& wxCB_READONLY
)
399 m_text
= new wxComboBoxText(this);
400 if ( size
.y
== wxDefaultCoord
) {
401 csize
.y
= m_text
->GetSize().y
;
405 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
407 m_choice
->Append( n
, choices
);
408 SetInitialSize(csize
); // Needed because it is a wxControlWithItems
414 wxString
wxComboBox::GetValue() const
417 CFStringRef myString
;
418 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
419 return wxMacCFStringHolder( myString
, GetFont().GetEncoding() ).AsString();
423 if ( m_text
== NULL
)
425 result
= m_choice
->GetString( m_choice
->GetSelection() );
429 result
= m_text
->GetValue();
436 void wxComboBox::SetValue(const wxString
& value
)
441 int s
= FindString (value
);
442 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
444 m_choice
->Append(value
);
446 SetStringSelection( value
);
450 // Clipboard operations
451 void wxComboBox::Copy()
453 if ( m_text
!= NULL
)
459 void wxComboBox::Cut()
461 if ( m_text
!= NULL
)
467 void wxComboBox::Paste()
469 if ( m_text
!= NULL
)
475 void wxComboBox::SetEditable(bool editable
)
477 if ( ( m_text
== NULL
) && editable
)
479 m_text
= new wxComboBoxText( this );
481 else if ( !editable
)
486 int currentX
, currentY
;
487 GetPosition( ¤tX
, ¤tY
);
489 int currentW
, currentH
;
490 GetSize( ¤tW
, ¤tH
);
492 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
495 void wxComboBox::SetInsertionPoint(long pos
)
500 void wxComboBox::SetInsertionPointEnd()
505 long wxComboBox::GetInsertionPoint() const
511 wxTextPos
wxComboBox::GetLastPosition() const
517 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
522 void wxComboBox::Remove(long from
, long to
)
527 void wxComboBox::SetSelection(long from
, long to
)
532 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
534 void **clientData
, wxClientDataType type
)
537 const unsigned int count
= items
.GetCount();
538 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
540 HIComboBoxInsertTextItemAtIndex(m_peer
->GetControlRef(),
542 wxMacCFStringHolder(items
[i
],
543 GetFont().GetEncoding()));
544 AssignNewItemClientData(pos
, clientData
, i
, type
);
547 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
551 return m_choice
->DoInsertItems( items
, pos
, clientData
, type
);
555 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
560 return m_choice
->DoSetItemClientData( n
, clientData
);
564 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
569 return m_choice
->DoGetItemClientData( n
);
573 unsigned int wxComboBox::GetCount() const {
575 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
577 return m_choice
->GetCount();
581 void wxComboBox::DoDeleteOneItem(unsigned int n
)
584 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
586 m_choice
->Delete( n
);
590 void wxComboBox::DoClear()
593 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
594 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
595 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
601 int wxComboBox::GetSelection() const
604 return FindString( GetStringSelection() );
606 return m_choice
->GetSelection();
610 void wxComboBox::SetSelection(int n
)
613 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
615 m_choice
->SetSelection( n
);
617 if ( m_text
!= NULL
)
619 m_text
->SetValue(GetString(n
));
624 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
627 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
629 if (GetString(i
).IsSameAs(s
, bCase
) )
634 return m_choice
->FindString( s
, bCase
);
638 wxString
wxComboBox::GetString(unsigned int n
) const
641 CFStringRef itemText
;
642 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
643 return wxMacCFStringHolder(itemText
).AsString();
645 return m_choice
->GetString( n
);
649 wxString
wxComboBox::GetStringSelection() const
652 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
654 int sel
= GetSelection ();
655 if (sel
!= wxNOT_FOUND
)
656 return wxString(this->GetString((unsigned int)sel
));
658 return wxEmptyString
;
662 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
665 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
666 wxMacCFStringHolder(s
, GetFont().GetEncoding()) ) );
667 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
669 m_choice
->SetString( n
, s
);
673 bool wxComboBox::IsEditable() const
677 return !HasFlag(wxCB_READONLY
);
679 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
683 void wxComboBox::Undo()
693 void wxComboBox::Redo()
703 void wxComboBox::SelectAll()
713 bool wxComboBox::CanCopy() const
720 return m_text
->CanCopy();
726 bool wxComboBox::CanCut() const
733 return m_text
->CanCut();
739 bool wxComboBox::CanPaste() const
746 return m_text
->CanPaste();
752 bool wxComboBox::CanUndo() const
759 return m_text
->CanUndo();
765 bool wxComboBox::CanRedo() const
772 return m_text
->CanRedo();
778 bool wxComboBox::OSXHandleClicked( double timestampsec
)
780 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
781 event
.SetInt(GetSelection());
782 event
.SetEventObject(this);
783 event
.SetString(GetStringSelection());
784 ProcessCommand(event
);