]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/combobxc.cpp
implementing rollover and pressed image for bitmapbutton on osx_cocoa
[wxWidgets.git] / src / osx / carbon / combobxc.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/combobxc.cpp
489468fe
SC
3// Purpose: wxComboBox class using HIView ComboBox
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/combobox.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/button.h"
18 #include "wx/menu.h"
19#endif
20
1f0c8f31 21#include "wx/osx/uma.h"
489468fe
SC
22#if TARGET_API_MAC_OSX
23#ifndef __HIVIEW__
24 #include <HIToolbox/HIView.h>
25#endif
26#endif
27
28IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
29
30#if TARGET_API_MAC_OSX
31#define USE_HICOMBOBOX 1 //use hi combobox define
32#else
33#define USE_HICOMBOBOX 0
34#endif
35
36static int nextPopUpMenuId = 1000;
37MenuHandle NewUniqueMenu()
38{
39 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" );
40 nextPopUpMenuId++;
41 return handle;
42}
43
44#if USE_HICOMBOBOX
45static const EventTypeSpec eventList[] =
46{
47 { kEventClassTextField , kEventTextAccepted } ,
48};
49
50static pascal OSStatus wxMacComboBoxEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
51{
52 OSStatus result = eventNotHandledErr;
53 wxComboBox* cb = (wxComboBox*) data;
54
55 wxMacCarbonEvent cEvent( event );
56
57 switch( cEvent.GetClass() )
58 {
59 case kEventClassTextField :
60 switch( cEvent.GetKind() )
61 {
62 case kEventTextAccepted :
63 {
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 );
69 }
70 break;
71 default :
72 break;
73 }
74 break;
75 default :
76 break;
77 }
78
79
80 return result;
81}
82
83DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler )
84
85#endif
86
87// ----------------------------------------------------------------------------
88// constants
89// ----------------------------------------------------------------------------
90
91// the margin between the text control and the choice
92static const wxCoord MARGIN = 2;
93#if TARGET_API_MAC_OSX
94static const int POPUPWIDTH = 24;
95#else
96static const int POPUPWIDTH = 18;
97#endif
98static const int POPUPHEIGHT = 23;
99
100// ----------------------------------------------------------------------------
101// wxComboBoxText: text control forwards events to combobox
102// ----------------------------------------------------------------------------
103
104class wxComboBoxText : public wxTextCtrl
105{
106public:
107 wxComboBoxText( wxComboBox * cb )
108 : wxTextCtrl( cb , 1 )
109 {
110 m_cb = cb;
111 }
112
113protected:
114 void OnChar( wxKeyEvent& event )
115 {
116 if ( event.GetKeyCode() == WXK_RETURN )
117 {
118 wxString value = GetValue();
119
120 if ( m_cb->GetCount() == 0 )
121 {
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
124 // all!
125 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
126 event.SetInt( 0 );
127 event.SetString( value );
128 event.SetEventObject( m_cb );
129 m_cb->HandleWindowEvent( event );
130 }
131 else
132 {
133 // add the item to the list if it's not there yet
134 if ( m_cb->FindString(value) == wxNOT_FOUND )
135 {
136 m_cb->Append(value);
137 m_cb->SetStringSelection(value);
138
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 );
145 }
146
147 // This will invoke the dialog default action, such
148 // as the clicking the default button.
149
150 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
151 if ( tlw && tlw->GetDefaultItem() )
152 {
153 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
154 if ( def && def->IsEnabled() )
155 {
156 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
157 event.SetEventObject(def);
158 def->Command(event);
159 return;
160 }
161 }
162
163 return;
164 }
165 }
166
167 event.Skip();
168 }
169private:
170 wxComboBox *m_cb;
171
172 DECLARE_EVENT_TABLE()
173};
174
175BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
176 EVT_CHAR( wxComboBoxText::OnChar)
177END_EVENT_TABLE()
178
179class wxComboBoxChoice : public wxChoice
180{
181public:
182 wxComboBoxChoice(wxComboBox *cb, int style)
183 : wxChoice( cb , 1 )
184 {
185 m_cb = cb;
186 }
187
188protected:
189 void OnChoice( wxCommandEvent& e )
190 {
191 wxString s = e.GetString();
192
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);
199 }
200 virtual wxSize DoGetBestSize() const
201 {
202 wxSize sz = wxChoice::DoGetBestSize();
203 sz.x = POPUPWIDTH;
204 return sz;
205 }
206
207private:
208 wxComboBox *m_cb;
209
210 DECLARE_EVENT_TABLE()
211};
212
213BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
214 EVT_CHOICE(wxID_ANY, wxComboBoxChoice::OnChoice)
215END_EVENT_TABLE()
216
217wxComboBox::~wxComboBox()
218{
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
524c47aa
SC
222 wxDELETE( m_text );
223 wxDELETE( m_choice );
489468fe
SC
224}
225
226
227// ----------------------------------------------------------------------------
228// geometry
229// ----------------------------------------------------------------------------
230
231wxSize wxComboBox::DoGetBestSize() const
232{
233#if USE_HICOMBOBOX
234 return wxControl::DoGetBestSize();
235#else
236 wxSize size = m_choice->GetBestSize();
237
238 if ( m_text != NULL )
239 {
240 wxSize sizeText = m_text->GetBestSize();
241
242 size.x = POPUPWIDTH + sizeText.x + MARGIN;
243 }
244
245 return size;
246#endif
247}
248
249void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
250#if USE_HICOMBOBOX
251 wxControl::DoMoveWindow(x, y, width, height);
252#else
253 height = POPUPHEIGHT;
254
255 wxControl::DoMoveWindow(x, y, width, height);
256
257 if ( m_text == NULL )
258 {
259 // we might not be fully constructed yet, therefore watch out...
260 if ( m_choice )
261 m_choice->SetSize(0, 0 , width, wxDefaultCoord);
262 }
263 else
264 {
265 wxCoord wText = width - POPUPWIDTH - MARGIN;
266 m_text->SetSize(0, 0, wText, height);
267 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, wxDefaultCoord);
268 }
269#endif
270}
271
272
273
274// ----------------------------------------------------------------------------
275// operations forwarded to the subcontrols
276// ----------------------------------------------------------------------------
277
278bool wxComboBox::Enable(bool enable)
279{
280 if ( !wxControl::Enable(enable) )
281 return false;
282
283 return true;
284}
285
286bool wxComboBox::Show(bool show)
287{
288 if ( !wxControl::Show(show) )
289 return false;
290
291 return true;
292}
293
294void wxComboBox::SetFocus()
295{
296#if USE_HICOMBOBOX
297 wxControl::SetFocus();
298#else
299 if ( m_text != NULL) {
300 m_text->SetFocus();
301 }
302#endif
303}
304
305
306void wxComboBox::DelegateTextChanged( const wxString& value )
307{
308 SetStringSelection( value );
309}
310
311
312void wxComboBox::DelegateChoice( const wxString& value )
313{
314 SetStringSelection( value );
315}
316
317
318bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
319 const wxString& value,
320 const wxPoint& pos,
321 const wxSize& size,
322 const wxArrayString& choices,
323 long style,
324 const wxValidator& validator,
325 const wxString& name)
326{
327 wxCArrayString chs( choices );
328
329 return Create( parent, id, value, pos, size, chs.GetCount(),
330 chs.GetStrings(), style, validator, name );
331}
332
333
334bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
335 const wxString& value,
336 const wxPoint& pos,
337 const wxSize& size,
338 int n, const wxString choices[],
339 long style,
340 const wxValidator& validator,
341 const wxString& name)
342{
343 m_text = NULL;
344 m_choice = NULL;
345#if USE_HICOMBOBOX
346 m_macIsUserPane = false;
347#endif
348 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
349 wxDefaultValidator, name) )
350 {
351 return false;
352 }
353#if USE_HICOMBOBOX
354 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
355 HIRect hiRect;
356
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;
361
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() ) );
371
372
373 m_peer->SetMinimum( 0 );
374 m_peer->SetMaximum( 100);
375 if ( n > 0 )
376 m_peer->SetValue( 1 );
377
378 MacPostControlCreate(pos,size);
379
380 Append( choices[ i ] );
381
382 HIViewSetVisible( m_peer->GetControlRef(), true );
383 SetSelection(0);
384 EventHandlerRef comboEventHandler;
385 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
386 GetEventTypeCount(eventList), eventList, this,
387 (EventHandlerRef *)&comboEventHandler);
388#else
389 m_choice = new wxComboBoxChoice(this, style );
390 m_choice->SetMinSize( wxSize( POPUPWIDTH , POPUPHEIGHT ) );
391
392 wxSize csize = size;
393 if ( style & wxCB_READONLY )
394 {
395 m_text = NULL;
396 }
397 else
398 {
399 m_text = new wxComboBoxText(this);
400 if ( size.y == wxDefaultCoord ) {
401 csize.y = m_text->GetSize().y;
402 }
403 }
404
405 DoSetSize(pos.x, pos.y, csize.x, csize.y);
406
407 m_choice->Append( n, choices );
408 SetInitialSize(csize); // Needed because it is a wxControlWithItems
409#endif
410
411 return true;
412}
413
414wxString wxComboBox::GetValue() const
415{
416#if USE_HICOMBOBOX
417 CFStringRef myString;
418 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)GetSelection(), &myString );
419 return wxMacCFStringHolder( myString, GetFont().GetEncoding() ).AsString();
420#else
421 wxString result;
422
423 if ( m_text == NULL )
424 {
425 result = m_choice->GetString( m_choice->GetSelection() );
426 }
427 else
428 {
429 result = m_text->GetValue();
430 }
431
432 return result;
433#endif
434}
435
436void wxComboBox::SetValue(const wxString& value)
437{
438#if USE_HICOMBOBOX
439
440#else
441 int s = FindString (value);
442 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
443 {
444 m_choice->Append(value);
445 }
446 SetStringSelection( value );
447#endif
448}
449
450// Clipboard operations
451void wxComboBox::Copy()
452{
453 if ( m_text != NULL )
454 {
455 m_text->Copy();
456 }
457}
458
459void wxComboBox::Cut()
460{
461 if ( m_text != NULL )
462 {
463 m_text->Cut();
464 }
465}
466
467void wxComboBox::Paste()
468{
469 if ( m_text != NULL )
470 {
471 m_text->Paste();
472 }
473}
474
475void wxComboBox::SetEditable(bool editable)
476{
477 if ( ( m_text == NULL ) && editable )
478 {
479 m_text = new wxComboBoxText( this );
480 }
481 else if ( ( m_text != NULL ) && !editable )
482 {
483 delete m_text;
484 m_text = NULL;
485 }
486
487 int currentX, currentY;
488 GetPosition( &currentX, &currentY );
489
490 int currentW, currentH;
491 GetSize( &currentW, &currentH );
492
493 DoMoveWindow( currentX, currentY, currentW, currentH );
494}
495
496void wxComboBox::SetInsertionPoint(long pos)
497{
498 // TODO
499}
500
501void wxComboBox::SetInsertionPointEnd()
502{
503 // TODO
504}
505
506long wxComboBox::GetInsertionPoint() const
507{
508 // TODO
509 return 0;
510}
511
512wxTextPos wxComboBox::GetLastPosition() const
513{
514 // TODO
515 return 0;
516}
517
518void wxComboBox::Replace(long from, long to, const wxString& value)
519{
520 // TODO
521}
522
523void wxComboBox::Remove(long from, long to)
524{
525 // TODO
526}
527
528void wxComboBox::SetSelection(long from, long to)
529{
530 // TODO
531}
532
533int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
534 unsigned int pos,
535 void **clientData, wxClientDataType type)
536{
537#if USE_HICOMBOBOX
538 const unsigned int count = items.GetCount();
539 for ( unsigned int i = 0; i < count; ++i, ++pos )
540 {
541 HIComboBoxInsertTextItemAtIndex(m_peer->GetControlRef(),
542 (CFIndex)pos,
543 wxMacCFStringHolder(items[i],
544 GetFont().GetEncoding()));
545 AssignNewItemClientData(pos, clientData, i, type);
546 }
547
548 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
549
550 return pos - 1;
551#else
552 return m_choice->DoInsertItems( items, pos, clientData, type );
553#endif
554}
555
556void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
557{
558#if USE_HICOMBOBOX
559 return; //TODO
560#else
561 return m_choice->DoSetItemClientData( n , clientData );
562#endif
563}
564
565void* wxComboBox::DoGetItemClientData(unsigned int n) const
566{
567#if USE_HICOMBOBOX
568 return NULL; //TODO
569#else
570 return m_choice->DoGetItemClientData( n );
571#endif
572}
573
574unsigned int wxComboBox::GetCount() const {
575#if USE_HICOMBOBOX
576 return (unsigned int) HIComboBoxGetItemCount( m_peer->GetControlRef() );
577#else
578 return m_choice->GetCount();
579#endif
580}
581
582void wxComboBox::DoDeleteOneItem(unsigned int n)
583{
584#if USE_HICOMBOBOX
585 HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex)n );
586#else
587 m_choice->Delete( n );
588#endif
589}
590
591void wxComboBox::DoClear()
592{
593#if USE_HICOMBOBOX
594 for ( CFIndex i = GetCount() - 1; i >= 0; ++ i )
595 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), i ) );
596 m_peer->SetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag,CFSTR(""));
597#else
598 m_choice->Clear();
599#endif
600}
601
602int wxComboBox::GetSelection() const
603{
604#if USE_HICOMBOBOX
605 return FindString( GetStringSelection() );
606#else
607 return m_choice->GetSelection();
608#endif
609}
610
611void wxComboBox::SetSelection(int n)
612{
613#if USE_HICOMBOBOX
614 SetControl32BitValue( m_peer->GetControlRef() , n + 1 );
615#else
616 m_choice->SetSelection( n );
617
618 if ( m_text != NULL )
619 {
620 m_text->SetValue(GetString(n));
621 }
622#endif
623}
624
625int wxComboBox::FindString(const wxString& s, bool bCase) const
626{
627#if USE_HICOMBOBOX
628 for( unsigned int i = 0 ; i < GetCount() ; i++ )
629 {
630 if (GetString(i).IsSameAs(s, bCase) )
631 return i ;
632 }
633 return wxNOT_FOUND;
634#else
635 return m_choice->FindString( s, bCase );
636#endif
637}
638
639wxString wxComboBox::GetString(unsigned int n) const
640{
641#if USE_HICOMBOBOX
642 CFStringRef itemText;
643 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)n, &itemText );
644 return wxMacCFStringHolder(itemText).AsString();
645#else
646 return m_choice->GetString( n );
647#endif
648}
649
650wxString wxComboBox::GetStringSelection() const
651{
652#if USE_HICOMBOBOX
653 return wxMacCFStringHolder(m_peer->GetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString();
654#else
655 int sel = GetSelection ();
656 if (sel != wxNOT_FOUND)
657 return wxString(this->GetString((unsigned int)sel));
658 else
659 return wxEmptyString;
660#endif
661}
662
663void wxComboBox::SetString(unsigned int n, const wxString& s)
664{
665#if USE_HICOMBOBOX
666 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex) n,
667 wxMacCFStringHolder(s, GetFont().GetEncoding()) ) );
668 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex) n + 1 ) );
669#else
670 m_choice->SetString( n , s );
671#endif
672}
673
674bool wxComboBox::IsEditable() const
675{
676#if USE_HICOMBOBOX
677 // TODO
678 return !HasFlag(wxCB_READONLY);
679#else
680 return m_text != NULL && !HasFlag(wxCB_READONLY);
681#endif
682}
683
684void wxComboBox::Undo()
685{
686#if USE_HICOMBOBOX
687 // TODO
688#else
689 if (m_text != NULL)
690 m_text->Undo();
691#endif
692}
693
694void wxComboBox::Redo()
695{
696#if USE_HICOMBOBOX
697 // TODO
698#else
699 if (m_text != NULL)
700 m_text->Redo();
701#endif
702}
703
704void wxComboBox::SelectAll()
705{
706#if USE_HICOMBOBOX
707 // TODO
708#else
709 if (m_text != NULL)
710 m_text->SelectAll();
711#endif
712}
713
714bool wxComboBox::CanCopy() const
715{
716#if USE_HICOMBOBOX
717 // TODO
718 return false;
719#else
720 if (m_text != NULL)
721 return m_text->CanCopy();
722 else
723 return false;
724#endif
725}
726
727bool wxComboBox::CanCut() const
728{
729#if USE_HICOMBOBOX
730 // TODO
731 return false;
732#else
733 if (m_text != NULL)
734 return m_text->CanCut();
735 else
736 return false;
737#endif
738}
739
740bool wxComboBox::CanPaste() const
741{
742#if USE_HICOMBOBOX
743 // TODO
744 return false;
745#else
746 if (m_text != NULL)
747 return m_text->CanPaste();
748 else
749 return false;
750#endif
751}
752
753bool wxComboBox::CanUndo() const
754{
755#if USE_HICOMBOBOX
756 // TODO
757 return false;
758#else
759 if (m_text != NULL)
760 return m_text->CanUndo();
761 else
762 return false;
763#endif
764}
765
766bool wxComboBox::CanRedo() const
767{
768#if USE_HICOMBOBOX
769 // TODO
770 return false;
771#else
772 if (m_text != NULL)
773 return m_text->CanRedo();
774 else
775 return false;
776#endif
777}
778
4eb5a0ec 779bool wxComboBox::OSXHandleClicked( double timestampsec )
489468fe
SC
780{
781 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
782 event.SetInt(GetSelection());
783 event.SetEventObject(this);
784 event.SetString(GetStringSelection());
785 ProcessCommand(event);
524c47aa 786 return true;
489468fe 787}