]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/combobxc.cpp
Fixing SetColumnWidth and selection handling, attempt at fixing ClearAll(), and initi...
[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
16#ifndef WX_PRECOMP
17 #include "wx/button.h"
18 #include "wx/menu.h"
19#endif
20
21#include "wx/mac/uma.h"
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->GetEventHandler()->ProcessEvent( 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->GetEventHandler()->ProcessEvent( 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->GetEventHandler()->ProcessEvent( 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 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}