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