]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/combobxc.cpp
default button on older systems was not shown with OK, but empty, resulting in wxID_C...
[wxWidgets.git] / src / mac / carbon / combobxc.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/combobxc.cpp
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#include "wx/button.h"
16#include "wx/menu.h"
17#include "wx/mac/uma.h"
18#if TARGET_API_MAC_OSX
19#ifndef __HIVIEW__
20 #include <HIToolbox/HIView.h>
21#endif
22#endif
23
24IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
25
26#if TARGET_API_MAC_OSX
27#define USE_HICOMBOBOX 1 //use hi combobox define
28#else
29#define USE_HICOMBOBOX 0
30#endif
31
32static int nextPopUpMenuId = 1000;
33MenuHandle NewUniqueMenu()
34{
35 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" );
36 nextPopUpMenuId++;
37 return handle;
38}
39
40#if USE_HICOMBOBOX
41static const EventTypeSpec eventList[] =
42{
43 { kEventClassTextField , kEventTextAccepted } ,
44};
45
46static pascal OSStatus wxMacComboBoxEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
47{
48 OSStatus result = eventNotHandledErr;
49 wxComboBox* cb = (wxComboBox*) data;
50
51 wxMacCarbonEvent cEvent( event );
52
53 switch( cEvent.GetClass() )
54 {
55 case kEventClassTextField :
56 switch( cEvent.GetKind() )
57 {
58 case kEventTextAccepted :
59 {
60 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, cb->GetId() );
61 event.SetInt( cb->GetSelection() );
62 event.SetString( cb->GetStringSelection() );
63 event.SetEventObject( cb );
64 cb->GetEventHandler()->ProcessEvent( event );
65 }
66 break;
67 default :
68 break;
69 }
70 break;
71 default :
72 break;
73 }
74
75
76 return result;
77}
78
79DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler )
80
81#endif
82
83// ----------------------------------------------------------------------------
84// constants
85// ----------------------------------------------------------------------------
86
87// the margin between the text control and the choice
88static const wxCoord MARGIN = 2;
89#if TARGET_API_MAC_OSX
90static const int POPUPWIDTH = 24;
91#else
92static const int POPUPWIDTH = 18;
93#endif
94static const int POPUPHEIGHT = 23;
95
96// ----------------------------------------------------------------------------
97// wxComboBoxText: text control forwards events to combobox
98// ----------------------------------------------------------------------------
99
100class wxComboBoxText : public wxTextCtrl
101{
102public:
103 wxComboBoxText( wxComboBox * cb )
104 : wxTextCtrl( cb , 1 )
105 {
106 m_cb = cb;
107 }
108
109protected:
110 void OnChar( wxKeyEvent& event )
111 {
112 if ( event.GetKeyCode() == WXK_RETURN )
113 {
114 wxString value = GetValue();
115
116 if ( m_cb->GetCount() == 0 )
117 {
118 // make Enter generate "selected" event if there is only one item
119 // in the combobox - without it, it's impossible to select it at
120 // all!
121 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
122 event.SetInt( 0 );
123 event.SetString( value );
124 event.SetEventObject( m_cb );
125 m_cb->GetEventHandler()->ProcessEvent( event );
126 }
127 else
128 {
129 // add the item to the list if it's not there yet
130 if ( m_cb->FindString(value) == wxNOT_FOUND )
131 {
132 m_cb->Append(value);
133 m_cb->SetStringSelection(value);
134
135 // and generate the selected event for it
136 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
137 event.SetInt( m_cb->GetCount() - 1 );
138 event.SetString( value );
139 event.SetEventObject( m_cb );
140 m_cb->GetEventHandler()->ProcessEvent( event );
141 }
142
143 // This will invoke the dialog default action, such
144 // as the clicking the default button.
145
146 wxWindow *parent = GetParent();
147 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
148 parent = parent->GetParent();
149 }
150 if ( parent && parent->GetDefaultItem() )
151 {
152 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
153 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 client objects
220 FreeData();
221
222 // delete the controls now, don't leave them alive even though they would
223 // still be eventually deleted by our parent - but it will be too late, the
224 // user code expects them to be gone now
225 if (m_text != NULL) {
226 delete m_text;
227 m_text = NULL;
228 }
229 if (m_choice != NULL) {
230 delete m_choice;
231 m_choice = NULL;
232 }
233}
234
235
236// ----------------------------------------------------------------------------
237// geometry
238// ----------------------------------------------------------------------------
239
240wxSize wxComboBox::DoGetBestSize() const
241{
242#if USE_HICOMBOBOX
243 return wxControl::DoGetBestSize();
244#else
245 wxSize size = m_choice->GetBestSize();
246
247 if ( m_text != NULL )
248 {
249 wxSize sizeText = m_text->GetBestSize();
250
251 size.x = POPUPWIDTH + sizeText.x + MARGIN;
252 }
253
254 return size;
255#endif
256}
257
258void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
259#if USE_HICOMBOBOX
260 wxControl::DoMoveWindow(x, y, width, height);
261#else
262 height = POPUPHEIGHT;
263
264 wxControl::DoMoveWindow(x, y, width, height);
265
266 if ( m_text == NULL )
267 {
268 // we might not be fully constructed yet, therefore watch out...
269 if ( m_choice )
270 m_choice->SetSize(0, 0 , width, wxDefaultCoord);
271 }
272 else
273 {
274 wxCoord wText = width - POPUPWIDTH - MARGIN;
275 m_text->SetSize(0, 0, wText, height);
276 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, wxDefaultCoord);
277 }
278#endif
279}
280
281
282
283// ----------------------------------------------------------------------------
284// operations forwarded to the subcontrols
285// ----------------------------------------------------------------------------
286
287bool wxComboBox::Enable(bool enable)
288{
289 if ( !wxControl::Enable(enable) )
290 return false;
291
292 return true;
293}
294
295bool wxComboBox::Show(bool show)
296{
297 if ( !wxControl::Show(show) )
298 return false;
299
300 return true;
301}
302
303void wxComboBox::SetFocus()
304{
305#if USE_HICOMBOBOX
306 wxControl::SetFocus();
307#else
308 if ( m_text != NULL) {
309 m_text->SetFocus();
310 }
311#endif
312}
313
314
315void wxComboBox::DelegateTextChanged( const wxString& value )
316{
317 SetStringSelection( value );
318}
319
320
321void wxComboBox::DelegateChoice( const wxString& value )
322{
323 SetStringSelection( value );
324}
325
326
327bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
328 const wxString& value,
329 const wxPoint& pos,
330 const wxSize& size,
331 const wxArrayString& choices,
332 long style,
333 const wxValidator& validator,
334 const wxString& name)
335{
336 wxCArrayString chs( choices );
337
338 return Create( parent, id, value, pos, size, chs.GetCount(),
339 chs.GetStrings(), style, validator, name );
340}
341
342
343bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
344 const wxString& value,
345 const wxPoint& pos,
346 const wxSize& size,
347 int n, const wxString choices[],
348 long style,
349 const wxValidator& validator,
350 const wxString& name)
351{
352 m_text = NULL;
353 m_choice = NULL;
354#if USE_HICOMBOBOX
355 m_macIsUserPane = false;
356#endif
357 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
358 wxDefaultValidator, name) )
359 {
360 return false;
361 }
362#if USE_HICOMBOBOX
363 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
364 HIRect hiRect;
365
366 hiRect.origin.x = 20; //bounds.left;
367 hiRect.origin.y = 25; //bounds.top;
368 hiRect.size.width = 120;// bounds.right - bounds.left;
369 hiRect.size.height = 24;
370
371 //For some reason, this code causes the combo box not to be displayed at all.
372 //hiRect.origin.x = bounds.left;
373 //hiRect.origin.y = bounds.top;
374 //hiRect.size.width = bounds.right - bounds.left;
375 //hiRect.size.height = bounds.bottom - bounds.top;
376 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
377 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
378 m_peer = new wxMacControl(this);
379 verify_noerr( HIComboBoxCreate( &hiRect, CFSTR(""), NULL, NULL, kHIComboBoxStandardAttributes, m_peer->GetControlRefAddr() ) );
380
381
382 m_peer->SetMinimum( 0 );
383 m_peer->SetMaximum( 100);
384 if ( n > 0 )
385 m_peer->SetValue( 1 );
386
387 MacPostControlCreate(pos,size);
388
389 for ( int i = 0; i < n; i++ )
390 {
391 DoAppend( choices[ i ] );
392 }
393
394 HIViewSetVisible( m_peer->GetControlRef(), true );
395 SetSelection(0);
396 EventHandlerRef comboEventHandler;
397 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
398 GetEventTypeCount(eventList), eventList, this,
399 (EventHandlerRef *)&comboEventHandler);
400#else
401 m_choice = new wxComboBoxChoice(this, style );
402
403 m_choice = new wxComboBoxChoice(this, style );
404 m_choice->SetSizeHints( wxSize( POPUPWIDTH , POPUPHEIGHT ) );
405
406 wxSize csize = size;
407 if ( style & wxCB_READONLY )
408 {
409 m_text = NULL;
410 }
411 else
412 {
413 m_text = new wxComboBoxText(this);
414 if ( size.y == wxDefaultCoord ) {
415 csize.y = m_text->GetSize().y;
416 }
417 }
418
419 DoSetSize(pos.x, pos.y, csize.x, csize.y);
420
421 for ( int i = 0; i < n; i++ )
422 {
423 m_choice->DoAppend( choices[ i ] );
424 }
425 SetBestSize(csize); // Needed because it is a wxControlWithItems
426#endif
427
428 return true;
429}
430
431wxString wxComboBox::GetValue() const
432{
433#if USE_HICOMBOBOX
434 CFStringRef myString;
435 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)GetSelection(), &myString );
436 return wxMacCFStringHolder( myString, m_font.GetEncoding() ).AsString();
437#else
438 wxString result;
439
440 if ( m_text == NULL )
441 {
442 result = m_choice->GetString( m_choice->GetSelection() );
443 }
444 else
445 {
446 result = m_text->GetValue();
447 }
448
449 return result;
450#endif
451}
452
453void wxComboBox::SetValue(const wxString& value)
454{
455#if USE_HICOMBOBOX
456
457#else
458 int s = FindString (value);
459 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
460 {
461 m_choice->Append(value);
462 }
463 SetStringSelection( value );
464#endif
465}
466
467// Clipboard operations
468void wxComboBox::Copy()
469{
470 if ( m_text != NULL )
471 {
472 m_text->Copy();
473 }
474}
475
476void wxComboBox::Cut()
477{
478 if ( m_text != NULL )
479 {
480 m_text->Cut();
481 }
482}
483
484void wxComboBox::Paste()
485{
486 if ( m_text != NULL )
487 {
488 m_text->Paste();
489 }
490}
491
492void wxComboBox::SetEditable(bool editable)
493{
494 if ( ( m_text == NULL ) && editable )
495 {
496 m_text = new wxComboBoxText( this );
497 }
498 else if ( ( m_text != NULL ) && !editable )
499 {
500 delete m_text;
501 m_text = NULL;
502 }
503
504 int currentX, currentY;
505 GetPosition( &currentX, &currentY );
506
507 int currentW, currentH;
508 GetSize( &currentW, &currentH );
509
510 DoMoveWindow( currentX, currentY, currentW, currentH );
511}
512
513void wxComboBox::SetInsertionPoint(long pos)
514{
515 // TODO
516}
517
518void wxComboBox::SetInsertionPointEnd()
519{
520 // TODO
521}
522
523long wxComboBox::GetInsertionPoint() const
524{
525 // TODO
526 return 0;
527}
528
529wxTextPos wxComboBox::GetLastPosition() const
530{
531 // TODO
532 return 0;
533}
534
535void wxComboBox::Replace(long from, long to, const wxString& value)
536{
537 // TODO
538}
539
540void wxComboBox::Remove(long from, long to)
541{
542 // TODO
543}
544
545void wxComboBox::SetSelection(long from, long to)
546{
547 // TODO
548}
549
550int wxComboBox::DoAppend(const wxString& item)
551{
552#if USE_HICOMBOBOX
553 CFIndex outIndex;
554 HIComboBoxAppendTextItem( m_peer->GetControlRef(), wxMacCFStringHolder( item, m_font.GetEncoding() ), &outIndex );
555 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
556 return (int) outIndex;
557#else
558 return m_choice->DoAppend( item );
559#endif
560}
561
562int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
563{
564#if USE_HICOMBOBOX
565 HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)pos, wxMacCFStringHolder(item, m_font.GetEncoding()) );
566
567 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
568
569 return pos;
570#else
571 return m_choice->DoInsert( item , pos );
572#endif
573}
574
575void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
576{
577#if USE_HICOMBOBOX
578 return; //TODO
579#else
580 return m_choice->DoSetItemClientData( n , clientData );
581#endif
582}
583
584void* wxComboBox::DoGetItemClientData(unsigned int n) const
585{
586#if USE_HICOMBOBOX
587 return NULL; //TODO
588#else
589 return m_choice->DoGetItemClientData( n );
590#endif
591}
592
593void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
594{
595#if USE_HICOMBOBOX
596 return; //TODO
597#else
598 return m_choice->DoSetItemClientObject( n , clientData );
599#endif
600}
601
602wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
603{
604#if USE_HICOMBOBOX
605 return NULL;
606#else
607 return m_choice->DoGetItemClientObject( n );
608#endif
609}
610
611void wxComboBox::FreeData()
612{
613 if (HasClientObjectData())
614 {
615 unsigned int count = GetCount();
616 for ( unsigned int n = 0; n < count; n++ )
617 {
618 SetClientObject( n, NULL );
619 }
620 }
621}
622
623unsigned int wxComboBox::GetCount() const {
624#if USE_HICOMBOBOX
625 return (unsigned int) HIComboBoxGetItemCount( m_peer->GetControlRef() );
626#else
627 return m_choice->GetCount();
628#endif
629}
630
631void wxComboBox::Delete(unsigned int n)
632{
633#if USE_HICOMBOBOX
634 HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex)n );
635#else
636 // force client object deletion
637 if( HasClientObjectData() )
638 SetClientObject( n, NULL );
639 m_choice->Delete( n );
640#endif
641}
642
643void wxComboBox::Clear()
644{
645 FreeData();
646#if USE_HICOMBOBOX
647 for ( CFIndex i = GetCount() - 1; i >= 0; ++ i )
648 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), i ) );
649 m_peer->SetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag,CFSTR(""));
650#else
651 m_choice->Clear();
652#endif
653}
654
655int wxComboBox::GetSelection() const
656{
657#if USE_HICOMBOBOX
658 return FindString( GetStringSelection() );
659#else
660 return m_choice->GetSelection();
661#endif
662}
663
664void wxComboBox::SetSelection(int n)
665{
666#if USE_HICOMBOBOX
667 SetControl32BitValue( m_peer->GetControlRef() , n + 1 );
668#else
669 m_choice->SetSelection( n );
670
671 if ( m_text != NULL )
672 {
673 m_text->SetValue(GetString(n));
674 }
675#endif
676}
677
678int wxComboBox::FindString(const wxString& s, bool bCase) const
679{
680#if USE_HICOMBOBOX
681 for( unsigned int i = 0 ; i < GetCount() ; i++ )
682 {
683 if (GetString(i).IsSameAs(s, bCase) )
684 return i ;
685 }
686 return wxNOT_FOUND;
687#else
688 return m_choice->FindString( s, bCase );
689#endif
690}
691
692wxString wxComboBox::GetString(unsigned int n) const
693{
694#if USE_HICOMBOBOX
695 CFStringRef itemText;
696 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)n, &itemText );
697 return wxMacCFStringHolder(itemText).AsString();
698#else
699 return m_choice->GetString( n );
700#endif
701}
702
703wxString wxComboBox::GetStringSelection() const
704{
705#if USE_HICOMBOBOX
706 return wxMacCFStringHolder(m_peer->GetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString();
707#else
708 int sel = GetSelection ();
709 if (sel != wxNOT_FOUND)
710 return wxString(this->GetString((unsigned int)sel));
711 else
712 return wxEmptyString;
713#endif
714}
715
716void wxComboBox::SetString(unsigned int n, const wxString& s)
717{
718#if USE_HICOMBOBOX
719 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex) n,
720 wxMacCFStringHolder(s, m_font.GetEncoding()) ) );
721 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex) n + 1 ) );
722#else
723 m_choice->SetString( n , s );
724#endif
725}
726
727bool wxComboBox::IsEditable() const
728{
729#if USE_HICOMBOBOX
730 // TODO
731 return !HasFlag(wxCB_READONLY);
732#else
733 return m_text != NULL && !HasFlag(wxCB_READONLY);
734#endif
735}
736
737void wxComboBox::Undo()
738{
739#if USE_HICOMBOBOX
740 // TODO
741#else
742 if (m_text != NULL)
743 m_text->Undo();
744#endif
745}
746
747void wxComboBox::Redo()
748{
749#if USE_HICOMBOBOX
750 // TODO
751#else
752 if (m_text != NULL)
753 m_text->Redo();
754#endif
755}
756
757void wxComboBox::SelectAll()
758{
759#if USE_HICOMBOBOX
760 // TODO
761#else
762 if (m_text != NULL)
763 m_text->SelectAll();
764#endif
765}
766
767bool wxComboBox::CanCopy() const
768{
769#if USE_HICOMBOBOX
770 // TODO
771 return false;
772#else
773 if (m_text != NULL)
774 return m_text->CanCopy();
775 else
776 return false;
777#endif
778}
779
780bool wxComboBox::CanCut() const
781{
782#if USE_HICOMBOBOX
783 // TODO
784 return false;
785#else
786 if (m_text != NULL)
787 return m_text->CanCut();
788 else
789 return false;
790#endif
791}
792
793bool wxComboBox::CanPaste() const
794{
795#if USE_HICOMBOBOX
796 // TODO
797 return false;
798#else
799 if (m_text != NULL)
800 return m_text->CanPaste();
801 else
802 return false;
803#endif
804}
805
806bool wxComboBox::CanUndo() const
807{
808#if USE_HICOMBOBOX
809 // TODO
810 return false;
811#else
812 if (m_text != NULL)
813 return m_text->CanUndo();
814 else
815 return false;
816#endif
817}
818
819bool wxComboBox::CanRedo() const
820{
821#if USE_HICOMBOBOX
822 // TODO
823 return false;
824#else
825 if (m_text != NULL)
826 return m_text->CanRedo();
827 else
828 return false;
829#endif
830}
831
832wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
833{
834 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
835 event.SetInt(GetSelection());
836 event.SetEventObject(this);
837 event.SetString(GetStringSelection());
838 ProcessCommand(event);
839 return noErr;
840}