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
->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
222 if (m_text
!= NULL
) {
226 if (m_choice
!= NULL
) {
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 wxSize
wxComboBox::DoGetBestSize() const
240 return wxControl::DoGetBestSize();
242 wxSize size
= m_choice
->GetBestSize();
244 if ( m_text
!= NULL
)
246 wxSize sizeText
= m_text
->GetBestSize();
248 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
255 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
257 wxControl::DoMoveWindow(x
, y
, width
, height
);
259 height
= POPUPHEIGHT
;
261 wxControl::DoMoveWindow(x
, y
, width
, height
);
263 if ( m_text
== NULL
)
265 // we might not be fully constructed yet, therefore watch out...
267 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
271 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
272 m_text
->SetSize(0, 0, wText
, height
);
273 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
280 // ----------------------------------------------------------------------------
281 // operations forwarded to the subcontrols
282 // ----------------------------------------------------------------------------
284 bool wxComboBox::Enable(bool enable
)
286 if ( !wxControl::Enable(enable
) )
292 bool wxComboBox::Show(bool show
)
294 if ( !wxControl::Show(show
) )
300 void wxComboBox::SetFocus()
303 wxControl::SetFocus();
305 if ( m_text
!= NULL
) {
312 void wxComboBox::DelegateTextChanged( const wxString
& value
)
314 SetStringSelection( value
);
318 void wxComboBox::DelegateChoice( const wxString
& value
)
320 SetStringSelection( value
);
324 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
325 const wxString
& value
,
328 const wxArrayString
& choices
,
330 const wxValidator
& validator
,
331 const wxString
& name
)
333 wxCArrayString
chs( choices
);
335 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
336 chs
.GetStrings(), style
, validator
, name
);
340 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
341 const wxString
& value
,
344 int n
, const wxString choices
[],
346 const wxValidator
& validator
,
347 const wxString
& name
)
352 m_macIsUserPane
= false;
354 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
355 wxDefaultValidator
, name
) )
360 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
363 hiRect
.origin
.x
= 20; //bounds.left;
364 hiRect
.origin
.y
= 25; //bounds.top;
365 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
366 hiRect
.size
.height
= 24;
368 //For some reason, this code causes the combo box not to be displayed at all.
369 //hiRect.origin.x = bounds.left;
370 //hiRect.origin.y = bounds.top;
371 //hiRect.size.width = bounds.right - bounds.left;
372 //hiRect.size.height = bounds.bottom - bounds.top;
373 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
374 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
375 m_peer
= new wxMacControl(this);
376 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, m_peer
->GetControlRefAddr() ) );
379 m_peer
->SetMinimum( 0 );
380 m_peer
->SetMaximum( 100);
382 m_peer
->SetValue( 1 );
384 MacPostControlCreate(pos
,size
);
386 Append( choices
[ i
] );
388 HIViewSetVisible( m_peer
->GetControlRef(), true );
390 EventHandlerRef comboEventHandler
;
391 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
392 GetEventTypeCount(eventList
), eventList
, this,
393 (EventHandlerRef
*)&comboEventHandler
);
395 m_choice
= new wxComboBoxChoice(this, style
);
396 m_choice
->SetMinSize( wxSize( POPUPWIDTH
, POPUPHEIGHT
) );
399 if ( style
& wxCB_READONLY
)
405 m_text
= new wxComboBoxText(this);
406 if ( size
.y
== wxDefaultCoord
) {
407 csize
.y
= m_text
->GetSize().y
;
411 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
413 m_choice
->Append( n
, choices
);
414 SetInitialSize(csize
); // Needed because it is a wxControlWithItems
420 wxString
wxComboBox::GetValue() const
423 CFStringRef myString
;
424 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)GetSelection(), &myString
);
425 return wxMacCFStringHolder( myString
, GetFont().GetEncoding() ).AsString();
429 if ( m_text
== NULL
)
431 result
= m_choice
->GetString( m_choice
->GetSelection() );
435 result
= m_text
->GetValue();
442 void wxComboBox::SetValue(const wxString
& value
)
447 int s
= FindString (value
);
448 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
450 m_choice
->Append(value
);
452 SetStringSelection( value
);
456 // Clipboard operations
457 void wxComboBox::Copy()
459 if ( m_text
!= NULL
)
465 void wxComboBox::Cut()
467 if ( m_text
!= NULL
)
473 void wxComboBox::Paste()
475 if ( m_text
!= NULL
)
481 void wxComboBox::SetEditable(bool editable
)
483 if ( ( m_text
== NULL
) && editable
)
485 m_text
= new wxComboBoxText( this );
487 else if ( ( m_text
!= NULL
) && !editable
)
493 int currentX
, currentY
;
494 GetPosition( ¤tX
, ¤tY
);
496 int currentW
, currentH
;
497 GetSize( ¤tW
, ¤tH
);
499 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
502 void wxComboBox::SetInsertionPoint(long pos
)
507 void wxComboBox::SetInsertionPointEnd()
512 long wxComboBox::GetInsertionPoint() const
518 wxTextPos
wxComboBox::GetLastPosition() const
524 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
529 void wxComboBox::Remove(long from
, long to
)
534 void wxComboBox::SetSelection(long from
, long to
)
539 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
541 void **clientData
, wxClientDataType type
)
544 const unsigned int count
= items
.GetCount();
545 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
547 HIComboBoxInsertTextItemAtIndex(m_peer
->GetControlRef(),
549 wxMacCFStringHolder(items
[i
],
550 GetFont().GetEncoding()));
551 AssignNewItemClientData(pos
, clientData
, i
, type
);
554 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
558 return m_choice
->DoInsertItems( items
, pos
, clientData
, type
);
562 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
567 return m_choice
->DoSetItemClientData( n
, clientData
);
571 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
576 return m_choice
->DoGetItemClientData( n
);
580 unsigned int wxComboBox::GetCount() const {
582 return (unsigned int) HIComboBoxGetItemCount( m_peer
->GetControlRef() );
584 return m_choice
->GetCount();
588 void wxComboBox::DoDeleteOneItem(unsigned int n
)
591 HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
);
593 m_choice
->Delete( n
);
597 void wxComboBox::DoClear()
600 for ( CFIndex i
= GetCount() - 1; i
>= 0; ++ i
)
601 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), i
) );
602 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
608 int wxComboBox::GetSelection() const
611 return FindString( GetStringSelection() );
613 return m_choice
->GetSelection();
617 void wxComboBox::SetSelection(int n
)
620 SetControl32BitValue( m_peer
->GetControlRef() , n
+ 1 );
622 m_choice
->SetSelection( n
);
624 if ( m_text
!= NULL
)
626 m_text
->SetValue(GetString(n
));
631 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
634 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
636 if (GetString(i
).IsSameAs(s
, bCase
) )
641 return m_choice
->FindString( s
, bCase
);
645 wxString
wxComboBox::GetString(unsigned int n
) const
648 CFStringRef itemText
;
649 HIComboBoxCopyTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
)n
, &itemText
);
650 return wxMacCFStringHolder(itemText
).AsString();
652 return m_choice
->GetString( n
);
656 wxString
wxComboBox::GetStringSelection() const
659 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString();
661 int sel
= GetSelection ();
662 if (sel
!= wxNOT_FOUND
)
663 return wxString(this->GetString((unsigned int)sel
));
665 return wxEmptyString
;
669 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
672 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
,
673 wxMacCFStringHolder(s
, GetFont().GetEncoding()) ) );
674 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer
->GetControlRef(), (CFIndex
) n
+ 1 ) );
676 m_choice
->SetString( n
, s
);
680 bool wxComboBox::IsEditable() const
684 return !HasFlag(wxCB_READONLY
);
686 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
690 void wxComboBox::Undo()
700 void wxComboBox::Redo()
710 void wxComboBox::SelectAll()
720 bool wxComboBox::CanCopy() const
727 return m_text
->CanCopy();
733 bool wxComboBox::CanCut() const
740 return m_text
->CanCut();
746 bool wxComboBox::CanPaste() const
753 return m_text
->CanPaste();
759 bool wxComboBox::CanUndo() const
766 return m_text
->CanUndo();
772 bool wxComboBox::CanRedo() const
779 return m_text
->CanRedo();
785 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
787 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
788 event
.SetInt(GetSelection());
789 event
.SetEventObject(this);
790 event
.SetString(GetStringSelection());
791 ProcessCommand(event
);