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