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