]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobxc.cpp
SetValue is not adding a line if values does not exist
[wxWidgets.git] / src / mac / carbon / combobxc.cpp
CommitLineData
46cc7c4e
SC
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
65571936 9// Licence: wxWindows licence
46cc7c4e
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.h"
14#endif
15
16#include "wx/combobox.h"
17#include "wx/button.h"
18#include "wx/menu.h"
19#include "wx/mac/uma.h"
c829d62b 20#if TARGET_API_MAC_OSX
46cc7c4e
SC
21#ifndef __HIVIEW__
22 #include <HIToolbox/HIView.h>
23#endif
c829d62b 24#endif
46cc7c4e
SC
25
26#if !USE_SHARED_LIBRARY
27IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
28#endif
29
30// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
31
c829d62b 32#if TARGET_API_MAC_OSX
46cc7c4e 33#define USE_HICOMBOBOX 1 //use hi combobox define
c829d62b
SC
34#else
35#define USE_HICOMBOBOX 0
36#endif
46cc7c4e
SC
37
38static int nextPopUpMenuId = 1000 ;
39MenuHandle NewUniqueMenu()
40{
41 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
42 nextPopUpMenuId++ ;
43 return handle ;
44}
45
21fd5529
SC
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
46cc7c4e
SC
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(-1, 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, -1);
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, -1);
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);
21fd5529
SC
384 m_peer = new wxMacControl() ;
385 verify_noerr( HIComboBoxCreate( &hiRect, CFSTR(""), NULL, NULL, kHIComboBoxStandardAttributes, *m_peer ) );
386
46cc7c4e 387
21fd5529
SC
388 SetControl32BitMinimum( *m_peer , 0 ) ;
389 SetControl32BitMaximum( *m_peer , 100) ;
46cc7c4e 390 if ( n > 0 )
21fd5529 391 SetControl32BitValue( *m_peer , 1 ) ;
46cc7c4e
SC
392
393 MacPostControlCreate(pos,size) ;
394
395 for ( int i = 0 ; i < n ; i++ )
396 {
397 DoAppend( choices[ i ] );
398 }
399
21fd5529 400 HIViewSetVisible( *m_peer, true );
46cc7c4e 401 SetSelection(0);
21fd5529
SC
402 EventHandlerRef comboEventHandler ;
403 InstallControlEventHandler( *m_peer, GetwxMacComboBoxEventHandlerUPP(),
404 GetEventTypeCount(eventList), eventList, this,
405 (EventHandlerRef *)&comboEventHandler);
46cc7c4e
SC
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 == -1 ) {
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;
21fd5529 441 HIComboBoxCopyTextItemAtIndex( *m_peer, (CFIndex)GetSelection(), &myString );
46cc7c4e
SC
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
535long 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;
21fd5529
SC
560 HIComboBoxAppendTextItem( *m_peer, wxMacCFStringHolder( item, m_font.GetEncoding() ), &outIndex );
561 //SetControl32BitMaximum( *m_peer, GetCount() );
46cc7c4e
SC
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
21fd5529 571 HIComboBoxInsertTextItemAtIndex( *m_peer, (CFIndex)pos, wxMacCFStringHolder(item, m_font.GetEncoding()) );
46cc7c4e 572
21fd5529 573 //SetControl32BitMaximum( *m_peer, GetCount() );
46cc7c4e
SC
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
21fd5529 631 return (int) HIComboBoxGetItemCount( *m_peer );
46cc7c4e
SC
632#else
633 return m_choice->GetCount() ;
634#endif
635}
636
637void wxComboBox::Delete(int n)
638{
639#if USE_HICOMBOBOX
21fd5529 640 HIComboBoxRemoveItemAtIndex( *m_peer, (CFIndex)n );
46cc7c4e
SC
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{
b310cebd 651 FreeData();
46cc7c4e 652#if USE_HICOMBOBOX
b310cebd 653 for ( CFIndex i = GetCount() - 1 ; i >= 0 ; ++ i )
21fd5529
SC
654 verify_noerr( HIComboBoxRemoveItemAtIndex( *m_peer, i ) );
655 m_peer->SetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag,CFSTR(""));
46cc7c4e 656#else
46cc7c4e
SC
657 m_choice->Clear();
658#endif
659}
660
661int wxComboBox::GetSelection() const
662{
663#if USE_HICOMBOBOX
b310cebd 664 return FindString( GetStringSelection() ) ;
46cc7c4e
SC
665#else
666 return m_choice->GetSelection();
667#endif
668}
669
670void wxComboBox::SetSelection(int n)
671{
672#if USE_HICOMBOBOX
21fd5529 673 SetControl32BitValue( *m_peer , n + 1 ) ;
46cc7c4e
SC
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;
21fd5529 702 HIComboBoxCopyTextItemAtIndex( *m_peer, (CFIndex)n, &itemText );
46cc7c4e
SC
703 return wxMacCFStringHolder(itemText).AsString();
704#else
705 return m_choice->GetString( n );
706#endif
707}
708
709wxString wxComboBox::GetStringSelection() const
710{
b310cebd 711#if USE_HICOMBOBOX
21fd5529 712 return wxMacCFStringHolder(m_peer->GetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString() ;
b310cebd 713#else
46cc7c4e
SC
714 int sel = GetSelection ();
715 if (sel > -1)
716 return wxString(this->GetString (sel));
717 else
718 return wxEmptyString;
b310cebd 719#endif
46cc7c4e
SC
720}
721
722bool wxComboBox::SetStringSelection(const wxString& sel)
723{
724 int s = FindString (sel);
725 if (s > -1)
726 {
727 SetSelection (s);
728 return TRUE;
729 }
730 else
731 return FALSE;
732}
733
734void wxComboBox::SetString(int n, const wxString& s)
735{
736#if USE_HICOMBOBOX
21fd5529 737 verify_noerr ( HIComboBoxInsertTextItemAtIndex( *m_peer, (CFIndex) n,
b310cebd 738 wxMacCFStringHolder(s, m_font.GetEncoding()) ) );
21fd5529 739 verify_noerr ( HIComboBoxRemoveItemAtIndex( *m_peer, (CFIndex) n + 1 ) );
46cc7c4e
SC
740#else
741 m_choice->SetString( n , s ) ;
742#endif
743}
744
745
746wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
747{
46cc7c4e
SC
748 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
749 event.SetInt(GetSelection());
750 event.SetEventObject(this);
751 event.SetString(GetStringSelection());
752 ProcessCommand(event);
753 return noErr ;
46cc7c4e
SC
754}
755