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