]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/combobxc.cpp
implement flag for Unix-like behaviour in wxCmdLineParser::ConverStringToArgs()
[wxWidgets.git] / src / osx / carbon / combobxc.cpp
CommitLineData
489468fe
SC
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
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
222 if (m_text != NULL) {
223 delete m_text;
224 m_text = NULL;
225 }
226 if (m_choice != NULL) {
227 delete m_choice;
228 m_choice = NULL;
229 }
230}
231
232
233// ----------------------------------------------------------------------------
234// geometry
235// ----------------------------------------------------------------------------
236
237wxSize wxComboBox::DoGetBestSize() const
238{
239#if USE_HICOMBOBOX
240 return wxControl::DoGetBestSize();
241#else
242 wxSize size = m_choice->GetBestSize();
243
244 if ( m_text != NULL )
245 {
246 wxSize sizeText = m_text->GetBestSize();
247
248 size.x = POPUPWIDTH + sizeText.x + MARGIN;
249 }
250
251 return size;
252#endif
253}
254
255void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
256#if USE_HICOMBOBOX
257 wxControl::DoMoveWindow(x, y, width, height);
258#else
259 height = POPUPHEIGHT;
260
261 wxControl::DoMoveWindow(x, y, width, height);
262
263 if ( m_text == NULL )
264 {
265 // we might not be fully constructed yet, therefore watch out...
266 if ( m_choice )
267 m_choice->SetSize(0, 0 , width, wxDefaultCoord);
268 }
269 else
270 {
271 wxCoord wText = width - POPUPWIDTH - MARGIN;
272 m_text->SetSize(0, 0, wText, height);
273 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, wxDefaultCoord);
274 }
275#endif
276}
277
278
279
280// ----------------------------------------------------------------------------
281// operations forwarded to the subcontrols
282// ----------------------------------------------------------------------------
283
284bool wxComboBox::Enable(bool enable)
285{
286 if ( !wxControl::Enable(enable) )
287 return false;
288
289 return true;
290}
291
292bool wxComboBox::Show(bool show)
293{
294 if ( !wxControl::Show(show) )
295 return false;
296
297 return true;
298}
299
300void wxComboBox::SetFocus()
301{
302#if USE_HICOMBOBOX
303 wxControl::SetFocus();
304#else
305 if ( m_text != NULL) {
306 m_text->SetFocus();
307 }
308#endif
309}
310
311
312void wxComboBox::DelegateTextChanged( const wxString& value )
313{
314 SetStringSelection( value );
315}
316
317
318void wxComboBox::DelegateChoice( const wxString& value )
319{
320 SetStringSelection( value );
321}
322
323
324bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
325 const wxString& value,
326 const wxPoint& pos,
327 const wxSize& size,
328 const wxArrayString& choices,
329 long style,
330 const wxValidator& validator,
331 const wxString& name)
332{
333 wxCArrayString chs( choices );
334
335 return Create( parent, id, value, pos, size, chs.GetCount(),
336 chs.GetStrings(), style, validator, name );
337}
338
339
340bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
341 const wxString& value,
342 const wxPoint& pos,
343 const wxSize& size,
344 int n, const wxString choices[],
345 long style,
346 const wxValidator& validator,
347 const wxString& name)
348{
349 m_text = NULL;
350 m_choice = NULL;
351#if USE_HICOMBOBOX
352 m_macIsUserPane = false;
353#endif
354 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
355 wxDefaultValidator, name) )
356 {
357 return false;
358 }
359#if USE_HICOMBOBOX
360 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
361 HIRect hiRect;
362
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;
367
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() ) );
377
378
379 m_peer->SetMinimum( 0 );
380 m_peer->SetMaximum( 100);
381 if ( n > 0 )
382 m_peer->SetValue( 1 );
383
384 MacPostControlCreate(pos,size);
385
386 Append( choices[ i ] );
387
388 HIViewSetVisible( m_peer->GetControlRef(), true );
389 SetSelection(0);
390 EventHandlerRef comboEventHandler;
391 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
392 GetEventTypeCount(eventList), eventList, this,
393 (EventHandlerRef *)&comboEventHandler);
394#else
395 m_choice = new wxComboBoxChoice(this, style );
396 m_choice->SetMinSize( wxSize( POPUPWIDTH , POPUPHEIGHT ) );
397
398 wxSize csize = size;
399 if ( style & wxCB_READONLY )
400 {
401 m_text = NULL;
402 }
403 else
404 {
405 m_text = new wxComboBoxText(this);
406 if ( size.y == wxDefaultCoord ) {
407 csize.y = m_text->GetSize().y;
408 }
409 }
410
411 DoSetSize(pos.x, pos.y, csize.x, csize.y);
412
413 m_choice->Append( n, choices );
414 SetInitialSize(csize); // Needed because it is a wxControlWithItems
415#endif
416
417 return true;
418}
419
420wxString wxComboBox::GetValue() const
421{
422#if USE_HICOMBOBOX
423 CFStringRef myString;
424 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)GetSelection(), &myString );
425 return wxMacCFStringHolder( myString, GetFont().GetEncoding() ).AsString();
426#else
427 wxString result;
428
429 if ( m_text == NULL )
430 {
431 result = m_choice->GetString( m_choice->GetSelection() );
432 }
433 else
434 {
435 result = m_text->GetValue();
436 }
437
438 return result;
439#endif
440}
441
442void wxComboBox::SetValue(const wxString& value)
443{
444#if USE_HICOMBOBOX
445
446#else
447 int s = FindString (value);
448 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
449 {
450 m_choice->Append(value);
451 }
452 SetStringSelection( value );
453#endif
454}
455
456// Clipboard operations
457void wxComboBox::Copy()
458{
459 if ( m_text != NULL )
460 {
461 m_text->Copy();
462 }
463}
464
465void wxComboBox::Cut()
466{
467 if ( m_text != NULL )
468 {
469 m_text->Cut();
470 }
471}
472
473void wxComboBox::Paste()
474{
475 if ( m_text != NULL )
476 {
477 m_text->Paste();
478 }
479}
480
481void wxComboBox::SetEditable(bool editable)
482{
483 if ( ( m_text == NULL ) && editable )
484 {
485 m_text = new wxComboBoxText( this );
486 }
487 else if ( ( m_text != NULL ) && !editable )
488 {
489 delete m_text;
490 m_text = NULL;
491 }
492
493 int currentX, currentY;
494 GetPosition( &currentX, &currentY );
495
496 int currentW, currentH;
497 GetSize( &currentW, &currentH );
498
499 DoMoveWindow( currentX, currentY, currentW, currentH );
500}
501
502void wxComboBox::SetInsertionPoint(long pos)
503{
504 // TODO
505}
506
507void wxComboBox::SetInsertionPointEnd()
508{
509 // TODO
510}
511
512long wxComboBox::GetInsertionPoint() const
513{
514 // TODO
515 return 0;
516}
517
518wxTextPos wxComboBox::GetLastPosition() const
519{
520 // TODO
521 return 0;
522}
523
524void wxComboBox::Replace(long from, long to, const wxString& value)
525{
526 // TODO
527}
528
529void wxComboBox::Remove(long from, long to)
530{
531 // TODO
532}
533
534void wxComboBox::SetSelection(long from, long to)
535{
536 // TODO
537}
538
539int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
540 unsigned int pos,
541 void **clientData, wxClientDataType type)
542{
543#if USE_HICOMBOBOX
544 const unsigned int count = items.GetCount();
545 for ( unsigned int i = 0; i < count; ++i, ++pos )
546 {
547 HIComboBoxInsertTextItemAtIndex(m_peer->GetControlRef(),
548 (CFIndex)pos,
549 wxMacCFStringHolder(items[i],
550 GetFont().GetEncoding()));
551 AssignNewItemClientData(pos, clientData, i, type);
552 }
553
554 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
555
556 return pos - 1;
557#else
558 return m_choice->DoInsertItems( items, pos, clientData, type );
559#endif
560}
561
562void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
563{
564#if USE_HICOMBOBOX
565 return; //TODO
566#else
567 return m_choice->DoSetItemClientData( n , clientData );
568#endif
569}
570
571void* wxComboBox::DoGetItemClientData(unsigned int n) const
572{
573#if USE_HICOMBOBOX
574 return NULL; //TODO
575#else
576 return m_choice->DoGetItemClientData( n );
577#endif
578}
579
580unsigned int wxComboBox::GetCount() const {
581#if USE_HICOMBOBOX
582 return (unsigned int) HIComboBoxGetItemCount( m_peer->GetControlRef() );
583#else
584 return m_choice->GetCount();
585#endif
586}
587
588void wxComboBox::DoDeleteOneItem(unsigned int n)
589{
590#if USE_HICOMBOBOX
591 HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex)n );
592#else
593 m_choice->Delete( n );
594#endif
595}
596
597void wxComboBox::DoClear()
598{
599#if USE_HICOMBOBOX
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(""));
603#else
604 m_choice->Clear();
605#endif
606}
607
608int wxComboBox::GetSelection() const
609{
610#if USE_HICOMBOBOX
611 return FindString( GetStringSelection() );
612#else
613 return m_choice->GetSelection();
614#endif
615}
616
617void wxComboBox::SetSelection(int n)
618{
619#if USE_HICOMBOBOX
620 SetControl32BitValue( m_peer->GetControlRef() , n + 1 );
621#else
622 m_choice->SetSelection( n );
623
624 if ( m_text != NULL )
625 {
626 m_text->SetValue(GetString(n));
627 }
628#endif
629}
630
631int wxComboBox::FindString(const wxString& s, bool bCase) const
632{
633#if USE_HICOMBOBOX
634 for( unsigned int i = 0 ; i < GetCount() ; i++ )
635 {
636 if (GetString(i).IsSameAs(s, bCase) )
637 return i ;
638 }
639 return wxNOT_FOUND;
640#else
641 return m_choice->FindString( s, bCase );
642#endif
643}
644
645wxString wxComboBox::GetString(unsigned int n) const
646{
647#if USE_HICOMBOBOX
648 CFStringRef itemText;
649 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)n, &itemText );
650 return wxMacCFStringHolder(itemText).AsString();
651#else
652 return m_choice->GetString( n );
653#endif
654}
655
656wxString wxComboBox::GetStringSelection() const
657{
658#if USE_HICOMBOBOX
659 return wxMacCFStringHolder(m_peer->GetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString();
660#else
661 int sel = GetSelection ();
662 if (sel != wxNOT_FOUND)
663 return wxString(this->GetString((unsigned int)sel));
664 else
665 return wxEmptyString;
666#endif
667}
668
669void wxComboBox::SetString(unsigned int n, const wxString& s)
670{
671#if USE_HICOMBOBOX
672 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex) n,
673 wxMacCFStringHolder(s, GetFont().GetEncoding()) ) );
674 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex) n + 1 ) );
675#else
676 m_choice->SetString( n , s );
677#endif
678}
679
680bool wxComboBox::IsEditable() const
681{
682#if USE_HICOMBOBOX
683 // TODO
684 return !HasFlag(wxCB_READONLY);
685#else
686 return m_text != NULL && !HasFlag(wxCB_READONLY);
687#endif
688}
689
690void wxComboBox::Undo()
691{
692#if USE_HICOMBOBOX
693 // TODO
694#else
695 if (m_text != NULL)
696 m_text->Undo();
697#endif
698}
699
700void wxComboBox::Redo()
701{
702#if USE_HICOMBOBOX
703 // TODO
704#else
705 if (m_text != NULL)
706 m_text->Redo();
707#endif
708}
709
710void wxComboBox::SelectAll()
711{
712#if USE_HICOMBOBOX
713 // TODO
714#else
715 if (m_text != NULL)
716 m_text->SelectAll();
717#endif
718}
719
720bool wxComboBox::CanCopy() const
721{
722#if USE_HICOMBOBOX
723 // TODO
724 return false;
725#else
726 if (m_text != NULL)
727 return m_text->CanCopy();
728 else
729 return false;
730#endif
731}
732
733bool wxComboBox::CanCut() const
734{
735#if USE_HICOMBOBOX
736 // TODO
737 return false;
738#else
739 if (m_text != NULL)
740 return m_text->CanCut();
741 else
742 return false;
743#endif
744}
745
746bool wxComboBox::CanPaste() const
747{
748#if USE_HICOMBOBOX
749 // TODO
750 return false;
751#else
752 if (m_text != NULL)
753 return m_text->CanPaste();
754 else
755 return false;
756#endif
757}
758
759bool wxComboBox::CanUndo() const
760{
761#if USE_HICOMBOBOX
762 // TODO
763 return false;
764#else
765 if (m_text != NULL)
766 return m_text->CanUndo();
767 else
768 return false;
769#endif
770}
771
772bool wxComboBox::CanRedo() const
773{
774#if USE_HICOMBOBOX
775 // TODO
776 return false;
777#else
778 if (m_text != NULL)
779 return m_text->CanRedo();
780 else
781 return false;
782#endif
783}
784
785wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
786{
787 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
788 event.SetInt(GetSelection());
789 event.SetEventObject(this);
790 event.SetString(GetStringSelection());
791 ProcessCommand(event);
792 return noErr;
793}