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