1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/combobox.h"
15 #include "wx/button.h"
17 #include "wx/mac/uma.h"
18 #if TARGET_API_MAC_OSX
20 #include <HIToolbox/HIView.h>
24 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
26 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
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
->GetEventHandler()->ProcessEvent( 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
->GetEventHandler()->ProcessEvent( 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
->GetEventHandler()->ProcessEvent( event
);
145 // This will invoke the dialog default action, such
146 // as the clicking the default button.
148 wxWindow
*parent
= GetParent();
149 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
150 parent
= parent
->GetParent() ;
152 if ( parent
&& parent
->GetDefaultItem() )
154 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
156 if ( def
&& def
->IsEnabled() )
158 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
159 event
.SetEventObject(def
);
174 DECLARE_EVENT_TABLE()
177 BEGIN_EVENT_TABLE(wxComboBoxText
, wxTextCtrl
)
178 EVT_CHAR( wxComboBoxText::OnChar
)
181 class wxComboBoxChoice
: public wxChoice
184 wxComboBoxChoice(wxComboBox
*cb
, int style
)
191 void OnChoice( wxCommandEvent
& e
)
193 wxString s
= e
.GetString();
195 m_cb
->DelegateChoice( s
);
196 wxCommandEvent
event2(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_cb
->GetId() );
197 event2
.SetInt(m_cb
->GetSelection());
198 event2
.SetEventObject(m_cb
);
199 event2
.SetString(m_cb
->GetStringSelection());
200 m_cb
->ProcessCommand(event2
);
202 virtual wxSize
DoGetBestSize() const
204 wxSize sz
= wxChoice::DoGetBestSize() ;
212 DECLARE_EVENT_TABLE()
215 BEGIN_EVENT_TABLE(wxComboBoxChoice
, wxChoice
)
216 EVT_CHOICE(wxID_ANY
, wxComboBoxChoice::OnChoice
)
219 wxComboBox::~wxComboBox()
221 // delete client objects
224 // delete the controls now, don't leave them alive even though they would
225 // still be eventually deleted by our parent - but it will be too late, the
226 // user code expects them to be gone now
227 if (m_text
!= NULL
) {
231 if (m_choice
!= NULL
) {
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 wxSize
wxComboBox::DoGetBestSize() const
245 return wxControl::DoGetBestSize();
247 wxSize size
= m_choice
->GetBestSize();
249 if ( m_text
!= NULL
)
251 wxSize sizeText
= m_text
->GetBestSize();
253 size
.x
= POPUPWIDTH
+ sizeText
.x
+ MARGIN
;
260 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
) {
262 wxControl::DoMoveWindow(x
, y
, width
, height
);
264 height
= POPUPHEIGHT
;
266 wxControl::DoMoveWindow(x
, y
, width
, height
);
268 if ( m_text
== NULL
)
270 // we might not be fully constructed yet, therefore watch out...
272 m_choice
->SetSize(0, 0 , width
, wxDefaultCoord
);
276 wxCoord wText
= width
- POPUPWIDTH
- MARGIN
;
277 m_text
->SetSize(0, 0, wText
, height
);
278 m_choice
->SetSize(0 + wText
+ MARGIN
, 0, POPUPWIDTH
, wxDefaultCoord
);
285 // ----------------------------------------------------------------------------
286 // operations forwarded to the subcontrols
287 // ----------------------------------------------------------------------------
289 bool wxComboBox::Enable(bool enable
)
291 if ( !wxControl::Enable(enable
) )
297 bool wxComboBox::Show(bool show
)
299 if ( !wxControl::Show(show
) )
305 void wxComboBox::SetFocus()
308 wxControl::SetFocus();
310 if ( m_text
!= NULL
) {
317 void wxComboBox::DelegateTextChanged( const wxString
& value
)
319 SetStringSelection( value
);
323 void wxComboBox::DelegateChoice( const wxString
& value
)
325 SetStringSelection( value
);
329 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
330 const wxString
& value
,
333 const wxArrayString
& choices
,
335 const wxValidator
& validator
,
336 const wxString
& name
)
338 wxCArrayString
chs( choices
);
340 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
341 chs
.GetStrings(), style
, validator
, name
);
345 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
346 const wxString
& value
,
349 int n
, const wxString choices
[],
351 const wxValidator
& validator
,
352 const wxString
& name
)
357 m_macIsUserPane
= false ;
359 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
360 wxDefaultValidator
, name
) )
365 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
368 hiRect
.origin
.x
= 20; //bounds.left;
369 hiRect
.origin
.y
= 25; //bounds.top;
370 hiRect
.size
.width
= 120;// bounds.right - bounds.left;
371 hiRect
.size
.height
= 24;
373 //For some reason, this code causes the combo box not to be displayed at all.
374 //hiRect.origin.x = bounds.left;
375 //hiRect.origin.y = bounds.top;
376 //hiRect.size.width = bounds.right - bounds.left;
377 //hiRect.size.height = bounds.bottom - bounds.top;
378 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
379 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
380 m_peer
= new wxMacControl(this) ;
381 verify_noerr( HIComboBoxCreate( &hiRect
, CFSTR(""), NULL
, NULL
, kHIComboBoxStandardAttributes
, *m_peer
) );
384 SetControl32BitMinimum( *m_peer
, 0 ) ;
385 SetControl32BitMaximum( *m_peer
, 100) ;
387 SetControl32BitValue( *m_peer
, 1 ) ;
389 MacPostControlCreate(pos
,size
) ;
391 for ( int i
= 0 ; i
< n
; i
++ )
393 DoAppend( choices
[ i
] );
396 HIViewSetVisible( *m_peer
, true );
398 EventHandlerRef comboEventHandler
;
399 InstallControlEventHandler( *m_peer
, GetwxMacComboBoxEventHandlerUPP(),
400 GetEventTypeCount(eventList
), eventList
, this,
401 (EventHandlerRef
*)&comboEventHandler
);
403 m_choice
= new wxComboBoxChoice(this, style
);
405 m_choice
= new wxComboBoxChoice(this, style
);
406 m_choice
->SetSizeHints( wxSize( POPUPWIDTH
, POPUPHEIGHT
) ) ;
409 if ( style
& wxCB_READONLY
)
415 m_text
= new wxComboBoxText(this);
416 if ( size
.y
== wxDefaultCoord
) {
417 csize
.y
= m_text
->GetSize().y
;
421 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
423 for ( int i
= 0 ; i
< n
; i
++ )
425 m_choice
->DoAppend( choices
[ i
] );
427 SetBestSize(csize
); // Needed because it is a wxControlWithItems
433 wxString
wxComboBox::GetValue() const
436 CFStringRef myString
;
437 HIComboBoxCopyTextItemAtIndex( *m_peer
, (CFIndex
)GetSelection(), &myString
);
438 return wxMacCFStringHolder( myString
, m_font
.GetEncoding() ).AsString();
442 if ( m_text
== NULL
)
444 result
= m_choice
->GetString( m_choice
->GetSelection() );
448 result
= m_text
->GetValue();
455 void wxComboBox::SetValue(const wxString
& value
)
460 int s
= FindString (value
);
461 if (s
== wxNOT_FOUND
&& !HasFlag(wxCB_READONLY
) )
463 m_choice
->Append(value
) ;
465 SetStringSelection( value
) ;
469 // Clipboard operations
470 void wxComboBox::Copy()
472 if ( m_text
!= NULL
)
478 void wxComboBox::Cut()
480 if ( m_text
!= NULL
)
486 void wxComboBox::Paste()
488 if ( m_text
!= NULL
)
494 void wxComboBox::SetEditable(bool editable
)
496 if ( ( m_text
== NULL
) && editable
)
498 m_text
= new wxComboBoxText( this );
500 else if ( ( m_text
!= NULL
) && !editable
)
506 int currentX
, currentY
;
507 GetPosition( ¤tX
, ¤tY
);
509 int currentW
, currentH
;
510 GetSize( ¤tW
, ¤tH
);
512 DoMoveWindow( currentX
, currentY
, currentW
, currentH
);
515 void wxComboBox::SetInsertionPoint(long pos
)
520 void wxComboBox::SetInsertionPointEnd()
525 long wxComboBox::GetInsertionPoint() const
531 wxTextPos
wxComboBox::GetLastPosition() const
537 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
542 void wxComboBox::Remove(long from
, long to
)
547 void wxComboBox::SetSelection(long from
, long to
)
552 int wxComboBox::DoAppend(const wxString
& item
)
556 HIComboBoxAppendTextItem( *m_peer
, wxMacCFStringHolder( item
, m_font
.GetEncoding() ), &outIndex
);
557 //SetControl32BitMaximum( *m_peer, GetCount() );
558 return (int) outIndex
;
560 return m_choice
->DoAppend( item
) ;
564 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
567 HIComboBoxInsertTextItemAtIndex( *m_peer
, (CFIndex
)pos
, wxMacCFStringHolder(item
, m_font
.GetEncoding()) );
569 //SetControl32BitMaximum( *m_peer, GetCount() );
573 return m_choice
->DoInsert( item
, pos
) ;
577 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
582 return m_choice
->DoSetItemClientData( n
, clientData
) ;
586 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
591 return m_choice
->DoGetItemClientData( n
) ;
595 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
600 return m_choice
->DoSetItemClientObject( n
, clientData
) ;
604 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
609 return m_choice
->DoGetItemClientObject( n
) ;
613 void wxComboBox::FreeData()
615 if (HasClientObjectData())
617 unsigned int count
= GetCount();
618 for ( unsigned int n
= 0; n
< count
; n
++ )
620 SetClientObject( n
, NULL
);
625 unsigned int wxComboBox::GetCount() const {
627 return (unsigned int) HIComboBoxGetItemCount( *m_peer
);
629 return m_choice
->GetCount() ;
633 void wxComboBox::Delete(unsigned int n
)
636 HIComboBoxRemoveItemAtIndex( *m_peer
, (CFIndex
)n
);
638 // force client object deletion
639 if( HasClientObjectData() )
640 SetClientObject( n
, NULL
);
641 m_choice
->Delete( n
);
645 void wxComboBox::Clear()
649 for ( CFIndex i
= GetCount() - 1 ; i
>= 0 ; ++ i
)
650 verify_noerr( HIComboBoxRemoveItemAtIndex( *m_peer
, i
) );
651 m_peer
->SetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
,CFSTR(""));
657 int wxComboBox::GetSelection() const
660 return FindString( GetStringSelection() ) ;
662 return m_choice
->GetSelection();
666 void wxComboBox::SetSelection(int n
)
669 SetControl32BitValue( *m_peer
, n
+ 1 ) ;
671 m_choice
->SetSelection( n
);
673 if ( m_text
!= NULL
)
675 m_text
->SetValue(GetString(n
));
680 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
683 for( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
685 if (GetString(i
).IsSameAs(s
, bCase
) )
690 return m_choice
->FindString( s
, bCase
);
694 wxString
wxComboBox::GetString(unsigned int n
) const
697 CFStringRef itemText
;
698 HIComboBoxCopyTextItemAtIndex( *m_peer
, (CFIndex
)n
, &itemText
);
699 return wxMacCFStringHolder(itemText
).AsString();
701 return m_choice
->GetString( n
);
705 wxString
wxComboBox::GetStringSelection() const
708 return wxMacCFStringHolder(m_peer
->GetData
<CFStringRef
>(kHIComboBoxEditTextPart
,kControlEditTextCFStringTag
)).AsString() ;
710 int sel
= GetSelection ();
711 if (sel
!= wxNOT_FOUND
)
712 return wxString(this->GetString((unsigned int)sel
));
714 return wxEmptyString
;
718 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
721 verify_noerr ( HIComboBoxInsertTextItemAtIndex( *m_peer
, (CFIndex
) n
,
722 wxMacCFStringHolder(s
, m_font
.GetEncoding()) ) );
723 verify_noerr ( HIComboBoxRemoveItemAtIndex( *m_peer
, (CFIndex
) n
+ 1 ) );
725 m_choice
->SetString( n
, s
) ;
729 bool wxComboBox::IsEditable() const
733 return !HasFlag(wxCB_READONLY
);
735 return m_text
!= NULL
&& !HasFlag(wxCB_READONLY
);
739 void wxComboBox::Undo()
749 void wxComboBox::Redo()
759 void wxComboBox::SelectAll()
769 bool wxComboBox::CanCopy() const
776 return m_text
->CanCopy();
782 bool wxComboBox::CanCut() const
789 return m_text
->CanCut();
795 bool wxComboBox::CanPaste() const
802 return m_text
->CanPaste();
808 bool wxComboBox::CanUndo() const
815 return m_text
->CanUndo();
821 bool wxComboBox::CanRedo() const
828 return m_text
->CanRedo();
834 wxInt32
wxComboBox::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
836 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
837 event
.SetInt(GetSelection());
838 event
.SetEventObject(this);
839 event
.SetString(GetStringSelection());
840 ProcessCommand(event
);