]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
fixed (harmless) unused variable warning in release build
[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
228146b0
JS
298 if (m_text)
299 m_text->Enable(enable);
300
7d8268a1 301 return true;
12f31626
SC
302}
303
304bool wxComboBox::Show(bool show)
305{
306 if ( !wxControl::Show(show) )
7d8268a1 307 return false;
12f31626 308
7d8268a1 309 return true;
12f31626
SC
310}
311
d99937cd
GD
312void wxComboBox::SetFocus()
313{
314 if ( m_text != NULL) {
315 m_text->SetFocus();
316 }
317}
465605e0 318
12f31626 319
d99937cd
GD
320void wxComboBox::DelegateTextChanged( const wxString& value )
321{
8095ef23 322 SetStringSelection( value );
12f31626
SC
323}
324
325
326void wxComboBox::DelegateChoice( const wxString& value )
327{
328 SetStringSelection( value );
329}
330
331
584ad2a3
MB
332bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
333 const wxString& value,
334 const wxPoint& pos,
335 const wxSize& size,
336 const wxArrayString& choices,
337 long style,
338 const wxValidator& validator,
339 const wxString& name)
340{
341 wxCArrayString chs( choices );
342
343 return Create( parent, id, value, pos, size, chs.GetCount(),
344 chs.GetStrings(), style, validator, name );
345}
346
347
e9576ca5
SC
348bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
349 const wxString& value,
350 const wxPoint& pos,
351 const wxSize& size,
465605e0
RR
352 int n, const wxString choices[],
353 long style,
e9576ca5
SC
354 const wxValidator& validator,
355 const wxString& name)
356{
327788ac 357 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
29998406 358 validator, name) )
12f31626 359 {
7d8268a1 360 return false;
12f31626
SC
361 }
362
327788ac 363 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
364 wxSize csize = size;
365 if ( style & wxCB_READONLY )
366 {
d99937cd 367 m_text = NULL;
12f31626
SC
368 }
369 else
370 {
371 m_text = new wxComboBoxText(this);
150e31d2 372 if ( size.y == -1 )
788e118f
SC
373 {
374 csize.y = m_text->GetSize().y ;
375 csize.y += 2 * TEXTFOCUSBORDER ;
12f31626
SC
376 }
377 }
150e31d2 378
12f31626 379 DoSetSize(pos.x, pos.y, csize.x, csize.y);
150e31d2 380
12f31626
SC
381 for ( int i = 0 ; i < n ; i++ )
382 {
465605e0 383 m_choice->DoAppend( choices[ i ] );
12f31626
SC
384 }
385
88db1d64 386 SetBestSize(size); // Needed because it is a wxControlWithItems
eba2f031
RD
387 SetStringSelection(value);
388
7d8268a1 389 return true;
e9576ca5
SC
390}
391
392wxString wxComboBox::GetValue() const
393{
12f31626 394 wxString result;
150e31d2 395
d99937cd 396 if ( m_text == NULL )
12f31626
SC
397 {
398 result = m_choice->GetString( m_choice->GetSelection() );
399 }
400 else
401 {
402 result = m_text->GetValue();
403 }
404
405 return result;
e9576ca5
SC
406}
407
46cc7c4e 408int wxComboBox::GetCount() const
150e31d2
JS
409{
410 return m_choice->GetCount() ;
46cc7c4e
SC
411}
412
e9576ca5
SC
413void wxComboBox::SetValue(const wxString& value)
414{
30a936ee
SC
415 if ( HasFlag(wxCB_READONLY) )
416 SetStringSelection( value ) ;
417 else
418 m_text->SetValue( value );
e9576ca5
SC
419}
420
421// Clipboard operations
422void wxComboBox::Copy()
423{
d99937cd 424 if ( m_text != NULL )
12f31626
SC
425 {
426 m_text->Copy();
427 }
e9576ca5
SC
428}
429
430void wxComboBox::Cut()
431{
d99937cd 432 if ( m_text != NULL )
12f31626
SC
433 {
434 m_text->Cut();
435 }
e9576ca5
SC
436}
437
438void wxComboBox::Paste()
439{
d99937cd 440 if ( m_text != NULL )
12f31626
SC
441 {
442 m_text->Paste();
443 }
e9576ca5
SC
444}
445
446void wxComboBox::SetEditable(bool editable)
447{
d99937cd 448 if ( ( m_text == NULL ) && editable )
12f31626
SC
449 {
450 m_text = new wxComboBoxText( this );
451 }
d99937cd 452 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
453 {
454 delete m_text;
d99937cd 455 m_text = NULL;
12f31626
SC
456 }
457
458 int currentX, currentY;
459 GetPosition( &currentX, &currentY );
150e31d2 460
12f31626
SC
461 int currentW, currentH;
462 GetSize( &currentW, &currentH );
463
464 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
465}
466
467void wxComboBox::SetInsertionPoint(long pos)
468{
469 // TODO
470}
471
472void wxComboBox::SetInsertionPointEnd()
473{
474 // TODO
475}
476
477long wxComboBox::GetInsertionPoint() const
478{
479 // TODO
480 return 0;
481}
482
7d8268a1 483wxTextPos wxComboBox::GetLastPosition() const
e9576ca5
SC
484{
485 // TODO
486 return 0;
487}
488
489void wxComboBox::Replace(long from, long to, const wxString& value)
490{
491 // TODO
492}
493
494void wxComboBox::Remove(long from, long to)
495{
496 // TODO
497}
498
499void wxComboBox::SetSelection(long from, long to)
500{
501 // TODO
502}
503
150e31d2 504int wxComboBox::DoAppend(const wxString& item)
e9576ca5 505{
e71a0aa9
SC
506 return m_choice->DoAppend( item ) ;
507}
508
150e31d2 509int wxComboBox::DoInsert(const wxString& item, int pos)
e71a0aa9
SC
510{
511 return m_choice->DoInsert( item , pos ) ;
512}
513
150e31d2 514void wxComboBox::DoSetItemClientData(int n, void* clientData)
e71a0aa9 515{
f148f2ba 516 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
517}
518
519void* wxComboBox::DoGetItemClientData(int n) const
520{
f148f2ba 521 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 522}
22a70443 523
e71a0aa9
SC
524void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
525{
f148f2ba 526 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
527}
528
150e31d2 529wxClientData* wxComboBox::DoGetItemClientObject(int n) const
e71a0aa9 530{
f148f2ba
MB
531 return m_choice->DoGetItemClientObject( n ) ;
532}
533
534void wxComboBox::FreeData()
535{
536 if ( HasClientObjectData() )
537 {
538 size_t count = GetCount();
539 for ( size_t n = 0; n < count; n++ )
540 {
541 SetClientObject( n, NULL );
542 }
543 }
e9576ca5
SC
544}
545
546void wxComboBox::Delete(int n)
547{
f148f2ba
MB
548 // force client object deletion
549 if( HasClientObjectData() )
550 SetClientObject( n, NULL );
12f31626 551 m_choice->Delete( n );
e9576ca5
SC
552}
553
554void wxComboBox::Clear()
555{
f148f2ba 556 FreeData();
12f31626 557 m_choice->Clear();
e9576ca5
SC
558}
559
560int wxComboBox::GetSelection() const
561{
12f31626 562 return m_choice->GetSelection();
e9576ca5
SC
563}
564
565void wxComboBox::SetSelection(int n)
566{
12f31626 567 m_choice->SetSelection( n );
150e31d2 568
d99937cd 569 if ( m_text != NULL )
12f31626
SC
570 {
571 m_text->SetValue( GetString( n ) );
572 }
e9576ca5
SC
573}
574
575int wxComboBox::FindString(const wxString& s) const
576{
12f31626 577 return m_choice->FindString( s );
e9576ca5
SC
578}
579
580wxString wxComboBox::GetString(int n) const
581{
12f31626 582 return m_choice->GetString( n );
e9576ca5
SC
583}
584
585wxString wxComboBox::GetStringSelection() const
586{
519cb848
SC
587 int sel = GetSelection ();
588 if (sel > -1)
589 return wxString(this->GetString (sel));
590 else
427ff662 591 return wxEmptyString;
e9576ca5
SC
592}
593
150e31d2 594void wxComboBox::SetString(int n, const wxString& s)
e71a0aa9
SC
595{
596 m_choice->SetString( n , s ) ;
597}
598
150e31d2
JS
599bool wxComboBox::IsEditable() const
600{
7d8268a1 601 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
602}
603
604void wxComboBox::Undo()
605{
606 if (m_text != NULL)
607 m_text->Undo();
608}
609
610void wxComboBox::Redo()
611{
612 if (m_text != NULL)
613 m_text->Redo();
614}
615
616void wxComboBox::SelectAll()
617{
618 if (m_text != NULL)
619 m_text->SelectAll();
620}
621
622bool wxComboBox::CanCopy() const
623{
624 if (m_text != NULL)
625 return m_text->CanCopy();
626 else
627 return false;
628}
629
630bool wxComboBox::CanCut() const
631{
632 if (m_text != NULL)
633 return m_text->CanCut();
634 else
635 return false;
636}
637
638bool wxComboBox::CanPaste() const
639{
640 if (m_text != NULL)
641 return m_text->CanPaste();
642 else
643 return false;
644}
645
646bool wxComboBox::CanUndo() const
647{
648 if (m_text != NULL)
649 return m_text->CanUndo();
650 else
651 return false;
652}
653
654bool wxComboBox::CanRedo() const
655{
656 if (m_text != NULL)
657 return m_text->CanRedo();
658 else
659 return false;
660}
e71a0aa9 661
150e31d2 662wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848 663{
645b5bd6 664 /* For consistency with other platforms, clicking in the text area does not constitute a selection
519cb848 665 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 666 event.SetInt(GetSelection());
519cb848 667 event.SetEventObject(this);
0a67a93b 668 event.SetString(GetStringSelection());
12fce8fb
RN
669 ProcessCommand(event);*/
670 return noErr ;
e9576ca5 671}
519cb848 672
179e085f 673#endif