]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
added test for wxScopeGuard
[wxWidgets.git] / src / mac / carbon / combobox.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose: wxComboBox class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "combobox.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
179e085f
RN
18#if wxUSE_COMBOBOX
19
e9576ca5 20#include "wx/combobox.h"
b5a8b32f 21#include "wx/button.h"
03e11df5 22#include "wx/menu.h"
519cb848 23#include "wx/mac/uma.h"
e9576ca5 24
e9576ca5 25IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
e9576ca5 26
12f31626
SC
27// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
28
519cb848 29
0e03d1fa 30static int nextPopUpMenuId = 1000 ;
150e31d2 31MenuHandle NewUniqueMenu()
892e461e
SC
32{
33 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
34 nextPopUpMenuId++ ;
35 return handle ;
36}
519cb848 37
12f31626
SC
38
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// the margin between the text control and the choice
6097743a 44#if TARGET_API_MAC_OSX
f26ca7f8
KO
45// margin should be bigger on OS X due to blue highlight
46// around text control.
b5267123 47static const wxCoord MARGIN = 4;
788e118f
SC
48// this is the border a focus rect on OSX is needing
49static const int TEXTFOCUSBORDER = 3 ;
6097743a 50#else
f26ca7f8 51static const wxCoord MARGIN = 2;
788e118f 52static const int TEXTFOCUSBORDER = 0 ;
6097743a 53#endif
12f31626
SC
54static const int POPUPHEIGHT = 23;
55
56
57// ----------------------------------------------------------------------------
58// wxComboBoxText: text control forwards events to combobox
59// ----------------------------------------------------------------------------
60
61class wxComboBoxText : public wxTextCtrl
62{
63public:
64 wxComboBoxText( wxComboBox * cb )
327788ac 65 : wxTextCtrl( cb , 1 )
12f31626
SC
66 {
67 m_cb = cb;
68 }
69
70protected:
8095ef23 71 void OnChar( wxKeyEvent& event )
12f31626 72 {
7d8268a1
WS
73 // Allows processing the tab key to go to the next control
74 if (event.GetKeyCode() == WXK_TAB)
75 {
76 wxNavigationKeyEvent NavEvent;
77 NavEvent.SetEventObject(this);
78 NavEvent.SetDirection(true);
79 NavEvent.SetWindowChange(false);
80
81 // Get the parent of the combo and have it process the navigation?
82 if (m_cb->GetParent()->GetEventHandler()->ProcessEvent(NavEvent))
645b5bd6 83 return;
7d8268a1 84 }
4a5d352f
RD
85
86 // send the event to the combobox class in case the user has bound EVT_CHAR
87 wxKeyEvent kevt(event);
88 kevt.SetEventObject(m_cb);
89 if (m_cb->GetEventHandler()->ProcessEvent(kevt))
90 // If the event was handled and not skipped then we're done
91 return;
92
eb22f2a6 93 if ( event.GetKeyCode() == WXK_RETURN )
8095ef23 94 {
645b5bd6
JS
95 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_cb->GetId());
96 event.SetString( GetValue() );
97 event.SetInt( m_cb->GetSelection() );
98 event.SetEventObject( m_cb );
8095ef23 99
645b5bd6
JS
100 // This will invoke the dialog default action, such
101 // as the clicking the default button.
8095ef23 102
645b5bd6
JS
103 if (!m_cb->GetEventHandler()->ProcessEvent( event ))
104 {
8095ef23
SC
105 wxWindow *parent = GetParent();
106 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
e40298d5 107 parent = parent->GetParent() ;
8095ef23
SC
108 }
109 if ( parent && parent->GetDefaultItem() )
110 {
111 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
112 wxButton);
113 if ( def && def->IsEnabled() )
114 {
115 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
116 event.SetEventObject(def);
117 def->Command(event);
8095ef23
SC
118 }
119 }
120
121 return;
122 }
123 }
150e31d2 124
12f31626
SC
125 event.Skip();
126 }
127
645b5bd6
JS
128 void OnKeyUp( wxKeyEvent& event )
129 {
4a5d352f
RD
130 event.SetEventObject(m_cb);
131 event.SetId(m_cb->GetId());
132 if (! m_cb->GetEventHandler()->ProcessEvent(event))
133 event.Skip();
645b5bd6 134 }
4a5d352f
RD
135
136 void OnKeyDown( wxKeyEvent& event )
137 {
138 event.SetEventObject(m_cb);
139 event.SetId(m_cb->GetId());
140 if (! m_cb->GetEventHandler()->ProcessEvent(event))
141 event.Skip();
142 }
143
144 void OnText( wxCommandEvent& event )
145 {
146 event.SetEventObject(m_cb);
147 event.SetId(m_cb->GetId());
148 if (! m_cb->GetEventHandler()->ProcessEvent(event))
149 event.Skip();
150 }
151
12f31626
SC
152private:
153 wxComboBox *m_cb;
154
155 DECLARE_EVENT_TABLE()
156};
157
158BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
4a5d352f
RD
159 EVT_KEY_DOWN( wxComboBoxText::OnKeyDown)
160 EVT_CHAR( wxComboBoxText::OnChar)
161 EVT_KEY_UP( wxComboBoxText::OnKeyUp)
162 EVT_TEXT( -1, wxComboBoxText::OnText)
12f31626
SC
163END_EVENT_TABLE()
164
165class wxComboBoxChoice : public wxChoice
166{
167public:
168 wxComboBoxChoice(wxComboBox *cb, int style)
768a4995 169 : wxChoice( cb , 1 , wxDefaultPosition , wxDefaultSize , 0 , NULL , style & (wxCB_SORT) )
12f31626
SC
170 {
171 m_cb = cb;
172 }
ff6871ef
SC
173 int GetPopupWidth() const
174 {
175 switch ( GetWindowVariant() )
176 {
177 case wxWINDOW_VARIANT_NORMAL :
178 case wxWINDOW_VARIANT_LARGE :
179 return 24 ;
180 default :
181 return 21 ;
182 }
183 }
12f31626
SC
184
185protected:
186 void OnChoice( wxCommandEvent& e )
187 {
188 wxString s = e.GetString();
189
190 m_cb->DelegateChoice( s );
8095ef23
SC
191 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
192 event2.SetInt(m_cb->GetSelection());
193 event2.SetEventObject(m_cb);
194 event2.SetString(m_cb->GetStringSelection());
195 m_cb->ProcessCommand(event2);
645b5bd6
JS
196
197 // For consistency with MSW and GTK, also send a text updated event
198 // After all, the text is updated when a selection is made
199 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId() );
200 TextEvent.SetString( m_cb->GetStringSelection() );
201 TextEvent.SetEventObject( m_cb );
202 m_cb->ProcessCommand( TextEvent );
12f31626 203 }
6097743a
SC
204 virtual wxSize DoGetBestSize() const
205 {
206 wxSize sz = wxChoice::DoGetBestSize() ;
d0770e4a 207 if (! m_cb->HasFlag(wxCB_READONLY) )
ff6871ef 208 sz.x = GetPopupWidth() ;
6097743a 209 return sz ;
150e31d2 210 }
12f31626
SC
211
212private:
213 wxComboBox *m_cb;
214
215 DECLARE_EVENT_TABLE()
216};
217
218BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
219 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
220END_EVENT_TABLE()
221
12f31626
SC
222wxComboBox::~wxComboBox()
223{
e94e2e95
MB
224 // delete client objects
225 FreeData();
226
227 // delete the controls now, don't leave them alive even though they would
12f31626
SC
228 // still be eventually deleted by our parent - but it will be too late, the
229 // user code expects them to be gone now
f5bb2251
GD
230 if (m_text != NULL) {
231 delete m_text;
232 m_text = NULL;
233 }
234 if (m_choice != NULL) {
235 delete m_choice;
236 m_choice = NULL;
237 }
12f31626
SC
238}
239
240
241// ----------------------------------------------------------------------------
242// geometry
243// ----------------------------------------------------------------------------
244
245wxSize wxComboBox::DoGetBestSize() const
246{
88db1d64 247 if (!m_choice && !m_text)
1deb64c0 248 return GetSize();
12f31626 249 wxSize size = m_choice->GetBestSize();
150e31d2 250
d99937cd 251 if ( m_text != NULL )
12f31626
SC
252 {
253 wxSize sizeText = m_text->GetBestSize();
f26ca7f8
KO
254 if (sizeText.y > size.y)
255 size.y = sizeText.y;
ff6871ef 256 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
788e118f
SC
257 size.x += TEXTFOCUSBORDER ;
258 size.y += 2 * TEXTFOCUSBORDER ;
12f31626 259 }
ff6871ef
SC
260 else
261 {
262 // clipping is too tight
263 size.y += 1 ;
264 }
12f31626
SC
265 return size;
266}
267
150e31d2 268void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
ff6871ef 269{
788e118f 270 wxControl::DoMoveWindow(x, y, width , height );
150e31d2 271
d99937cd 272 if ( m_text == NULL )
12f31626 273 {
facd6764
SC
274 // we might not be fully constructed yet, therefore watch out...
275 if ( m_choice )
276 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
277 }
278 else
279 {
ff6871ef 280 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
788e118f 281 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1 );
ff6871ef
SC
282 // put it at an inset of 1 to have outer area shadows drawn as well
283 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1);
150e31d2 284 }
12f31626
SC
285}
286
287
288
289// ----------------------------------------------------------------------------
290// operations forwarded to the subcontrols
291// ----------------------------------------------------------------------------
292
293bool wxComboBox::Enable(bool enable)
294{
295 if ( !wxControl::Enable(enable) )
7d8268a1 296 return false;
12f31626 297
7d8268a1 298 return true;
12f31626
SC
299}
300
301bool wxComboBox::Show(bool show)
302{
303 if ( !wxControl::Show(show) )
7d8268a1 304 return false;
12f31626 305
7d8268a1 306 return true;
12f31626
SC
307}
308
d99937cd
GD
309void wxComboBox::SetFocus()
310{
311 if ( m_text != NULL) {
312 m_text->SetFocus();
313 }
314}
465605e0 315
12f31626 316
d99937cd
GD
317void wxComboBox::DelegateTextChanged( const wxString& value )
318{
8095ef23 319 SetStringSelection( value );
12f31626
SC
320}
321
322
323void wxComboBox::DelegateChoice( const wxString& value )
324{
325 SetStringSelection( value );
326}
327
328
584ad2a3
MB
329bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
330 const wxString& value,
331 const wxPoint& pos,
332 const wxSize& size,
333 const wxArrayString& choices,
334 long style,
335 const wxValidator& validator,
336 const wxString& name)
337{
338 wxCArrayString chs( choices );
339
340 return Create( parent, id, value, pos, size, chs.GetCount(),
341 chs.GetStrings(), style, validator, name );
342}
343
344
e9576ca5
SC
345bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
346 const wxString& value,
347 const wxPoint& pos,
348 const wxSize& size,
465605e0
RR
349 int n, const wxString choices[],
350 long style,
e9576ca5
SC
351 const wxValidator& validator,
352 const wxString& name)
353{
327788ac 354 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
29998406 355 validator, name) )
12f31626 356 {
7d8268a1 357 return false;
12f31626
SC
358 }
359
327788ac 360 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
361 wxSize csize = size;
362 if ( style & wxCB_READONLY )
363 {
d99937cd 364 m_text = NULL;
12f31626
SC
365 }
366 else
367 {
368 m_text = new wxComboBoxText(this);
150e31d2 369 if ( size.y == -1 )
788e118f
SC
370 {
371 csize.y = m_text->GetSize().y ;
372 csize.y += 2 * TEXTFOCUSBORDER ;
12f31626
SC
373 }
374 }
150e31d2 375
12f31626 376 DoSetSize(pos.x, pos.y, csize.x, csize.y);
150e31d2 377
12f31626
SC
378 for ( int i = 0 ; i < n ; i++ )
379 {
465605e0 380 m_choice->DoAppend( choices[ i ] );
12f31626
SC
381 }
382
88db1d64 383 SetBestSize(size); // Needed because it is a wxControlWithItems
eba2f031
RD
384 SetStringSelection(value);
385
7d8268a1 386 return true;
e9576ca5
SC
387}
388
389wxString wxComboBox::GetValue() const
390{
12f31626 391 wxString result;
150e31d2 392
d99937cd 393 if ( m_text == NULL )
12f31626
SC
394 {
395 result = m_choice->GetString( m_choice->GetSelection() );
396 }
397 else
398 {
399 result = m_text->GetValue();
400 }
401
402 return result;
e9576ca5
SC
403}
404
46cc7c4e 405int wxComboBox::GetCount() const
150e31d2
JS
406{
407 return m_choice->GetCount() ;
46cc7c4e
SC
408}
409
e9576ca5
SC
410void wxComboBox::SetValue(const wxString& value)
411{
30a936ee
SC
412 if ( HasFlag(wxCB_READONLY) )
413 SetStringSelection( value ) ;
414 else
415 m_text->SetValue( value );
e9576ca5
SC
416}
417
418// Clipboard operations
419void wxComboBox::Copy()
420{
d99937cd 421 if ( m_text != NULL )
12f31626
SC
422 {
423 m_text->Copy();
424 }
e9576ca5
SC
425}
426
427void wxComboBox::Cut()
428{
d99937cd 429 if ( m_text != NULL )
12f31626
SC
430 {
431 m_text->Cut();
432 }
e9576ca5
SC
433}
434
435void wxComboBox::Paste()
436{
d99937cd 437 if ( m_text != NULL )
12f31626
SC
438 {
439 m_text->Paste();
440 }
e9576ca5
SC
441}
442
443void wxComboBox::SetEditable(bool editable)
444{
d99937cd 445 if ( ( m_text == NULL ) && editable )
12f31626
SC
446 {
447 m_text = new wxComboBoxText( this );
448 }
d99937cd 449 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
450 {
451 delete m_text;
d99937cd 452 m_text = NULL;
12f31626
SC
453 }
454
455 int currentX, currentY;
456 GetPosition( &currentX, &currentY );
150e31d2 457
12f31626
SC
458 int currentW, currentH;
459 GetSize( &currentW, &currentH );
460
461 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
462}
463
464void wxComboBox::SetInsertionPoint(long pos)
465{
466 // TODO
467}
468
469void wxComboBox::SetInsertionPointEnd()
470{
471 // TODO
472}
473
474long wxComboBox::GetInsertionPoint() const
475{
476 // TODO
477 return 0;
478}
479
7d8268a1 480wxTextPos wxComboBox::GetLastPosition() const
e9576ca5
SC
481{
482 // TODO
483 return 0;
484}
485
486void wxComboBox::Replace(long from, long to, const wxString& value)
487{
488 // TODO
489}
490
491void wxComboBox::Remove(long from, long to)
492{
493 // TODO
494}
495
496void wxComboBox::SetSelection(long from, long to)
497{
498 // TODO
499}
500
150e31d2 501int wxComboBox::DoAppend(const wxString& item)
e9576ca5 502{
e71a0aa9
SC
503 return m_choice->DoAppend( item ) ;
504}
505
150e31d2 506int wxComboBox::DoInsert(const wxString& item, int pos)
e71a0aa9
SC
507{
508 return m_choice->DoInsert( item , pos ) ;
509}
510
150e31d2 511void wxComboBox::DoSetItemClientData(int n, void* clientData)
e71a0aa9 512{
f148f2ba 513 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
514}
515
516void* wxComboBox::DoGetItemClientData(int n) const
517{
f148f2ba 518 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 519}
22a70443 520
e71a0aa9
SC
521void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
522{
f148f2ba 523 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
524}
525
150e31d2 526wxClientData* wxComboBox::DoGetItemClientObject(int n) const
e71a0aa9 527{
f148f2ba
MB
528 return m_choice->DoGetItemClientObject( n ) ;
529}
530
531void wxComboBox::FreeData()
532{
533 if ( HasClientObjectData() )
534 {
535 size_t count = GetCount();
536 for ( size_t n = 0; n < count; n++ )
537 {
538 SetClientObject( n, NULL );
539 }
540 }
e9576ca5
SC
541}
542
543void wxComboBox::Delete(int n)
544{
f148f2ba
MB
545 // force client object deletion
546 if( HasClientObjectData() )
547 SetClientObject( n, NULL );
12f31626 548 m_choice->Delete( n );
e9576ca5
SC
549}
550
551void wxComboBox::Clear()
552{
f148f2ba 553 FreeData();
12f31626 554 m_choice->Clear();
e9576ca5
SC
555}
556
557int wxComboBox::GetSelection() const
558{
12f31626 559 return m_choice->GetSelection();
e9576ca5
SC
560}
561
562void wxComboBox::SetSelection(int n)
563{
12f31626 564 m_choice->SetSelection( n );
150e31d2 565
d99937cd 566 if ( m_text != NULL )
12f31626
SC
567 {
568 m_text->SetValue( GetString( n ) );
569 }
e9576ca5
SC
570}
571
572int wxComboBox::FindString(const wxString& s) const
573{
12f31626 574 return m_choice->FindString( s );
e9576ca5
SC
575}
576
577wxString wxComboBox::GetString(int n) const
578{
12f31626 579 return m_choice->GetString( n );
e9576ca5
SC
580}
581
582wxString wxComboBox::GetStringSelection() const
583{
519cb848
SC
584 int sel = GetSelection ();
585 if (sel > -1)
586 return wxString(this->GetString (sel));
587 else
427ff662 588 return wxEmptyString;
e9576ca5
SC
589}
590
150e31d2 591void wxComboBox::SetString(int n, const wxString& s)
e71a0aa9
SC
592{
593 m_choice->SetString( n , s ) ;
594}
595
150e31d2
JS
596bool wxComboBox::IsEditable() const
597{
7d8268a1 598 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
599}
600
601void wxComboBox::Undo()
602{
603 if (m_text != NULL)
604 m_text->Undo();
605}
606
607void wxComboBox::Redo()
608{
609 if (m_text != NULL)
610 m_text->Redo();
611}
612
613void wxComboBox::SelectAll()
614{
615 if (m_text != NULL)
616 m_text->SelectAll();
617}
618
619bool wxComboBox::CanCopy() const
620{
621 if (m_text != NULL)
622 return m_text->CanCopy();
623 else
624 return false;
625}
626
627bool wxComboBox::CanCut() const
628{
629 if (m_text != NULL)
630 return m_text->CanCut();
631 else
632 return false;
633}
634
635bool wxComboBox::CanPaste() const
636{
637 if (m_text != NULL)
638 return m_text->CanPaste();
639 else
640 return false;
641}
642
643bool wxComboBox::CanUndo() const
644{
645 if (m_text != NULL)
646 return m_text->CanUndo();
647 else
648 return false;
649}
650
651bool wxComboBox::CanRedo() const
652{
653 if (m_text != NULL)
654 return m_text->CanRedo();
655 else
656 return false;
657}
e71a0aa9 658
150e31d2 659wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848 660{
645b5bd6 661 /* For consistency with other platforms, clicking in the text area does not constitute a selection
519cb848 662 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 663 event.SetInt(GetSelection());
519cb848 664 event.SetEventObject(this);
0a67a93b 665 event.SetString(GetStringSelection());
12fce8fb
RN
666 ProcessCommand(event);*/
667 return noErr ;
e9576ca5 668}
519cb848 669
179e085f 670#endif