]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/combobox.cpp
set m_isBeingDeleted to true (only) in SendDestroyEvent(); call it as early as possib...
[wxWidgets.git] / src / osx / carbon / combobox.cpp
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
28 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
29
30 WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox, wxControl)
31
32 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
33 WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox)
34 END_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.
44 static const wxCoord MARGIN = 4;
45 // this is the border a focus rect on OSX is needing
46 static const int TEXTFOCUSBORDER = 3 ;
47
48
49 // ----------------------------------------------------------------------------
50 // wxComboBoxText: text control forwards events to combobox
51 // ----------------------------------------------------------------------------
52
53 class wxComboBoxText : public wxTextCtrl
54 {
55 public:
56 wxComboBoxText( wxComboBox * cb )
57 : wxTextCtrl( cb , 1 )
58 {
59 m_cb = cb;
60 SetTriggerOnSetValue( false );
61 }
62
63 protected:
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
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 }
150 private:
151 wxComboBox *m_cb;
152
153 DECLARE_EVENT_TABLE()
154 };
155
156 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
157 EVT_KEY_DOWN(wxComboBoxText::OnKeyDown)
158 EVT_CHAR(wxComboBoxText::OnChar)
159 EVT_KEY_UP(wxComboBoxText::OnKeyUp)
160 EVT_SET_FOCUS(wxComboBoxText::OnFocus)
161 EVT_KILL_FOCUS(wxComboBoxText::OnFocus)
162 EVT_TEXT(wxID_ANY, wxComboBoxText::OnText)
163 END_EVENT_TABLE()
164
165 class wxComboBoxChoice : public wxChoice
166 {
167 public:
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
187 protected:
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
216 private:
217 wxComboBox *m_cb;
218
219 friend class wxComboBox;
220
221 DECLARE_EVENT_TABLE()
222 };
223
224 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
225 EVT_CHOICE(wxID_ANY, wxComboBoxChoice::OnChoice)
226 END_EVENT_TABLE()
227
228 wxComboBox::~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
250 wxSize 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
276 void 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
300 bool 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
311 bool wxComboBox::Show(bool show)
312 {
313 if ( !wxControl::Show(show) )
314 return false;
315
316 return true;
317 }
318
319 void wxComboBox::DelegateTextChanged( const wxString& value )
320 {
321 SetStringSelection( value );
322 }
323
324 void wxComboBox::DelegateChoice( const wxString& value )
325 {
326 SetStringSelection( value );
327 }
328
329 void wxComboBox::Init()
330 {
331 WX_INIT_CONTROL_CONTAINER();
332 }
333
334 bool 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
353 bool 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
397 wxString wxComboBox::GetValue() const
398 {
399 wxString result;
400
401 if ( m_text == NULL )
402 result = m_choice->GetString( m_choice->GetSelection() );
403 else
404 result = m_text->GetValue();
405
406 return result;
407 }
408
409 unsigned int wxComboBox::GetCount() const
410 {
411 return m_choice->GetCount() ;
412 }
413
414 void wxComboBox::SetValue(const wxString& value)
415 {
416 if ( HasFlag(wxCB_READONLY) )
417 SetStringSelection( value ) ;
418 else
419 m_text->SetValue( value );
420 }
421
422 void wxComboBox::WriteText(const wxString& text)
423 {
424 m_text->WriteText(text);
425 }
426
427 void wxComboBox::GetSelection(long *from, long *to) const
428 {
429 m_text->GetSelection(from, to);
430 }
431
432 // Clipboard operations
433
434 void wxComboBox::Copy()
435 {
436 if ( m_text != NULL )
437 m_text->Copy();
438 }
439
440 void wxComboBox::Cut()
441 {
442 if ( m_text != NULL )
443 m_text->Cut();
444 }
445
446 void wxComboBox::Paste()
447 {
448 if ( m_text != NULL )
449 m_text->Paste();
450 }
451
452 void wxComboBox::SetEditable(bool editable)
453 {
454 if ( ( m_text == NULL ) && editable )
455 {
456 m_text = new wxComboBoxText( this );
457 }
458 else if ( ( m_text != NULL ) && !editable )
459 {
460 delete m_text;
461 m_text = NULL;
462 }
463
464 int currentX, currentY;
465 GetPosition( &currentX, &currentY );
466
467 int currentW, currentH;
468 GetSize( &currentW, &currentH );
469
470 DoMoveWindow( currentX, currentY, currentW, currentH );
471 }
472
473 void wxComboBox::SetInsertionPoint(long pos)
474 {
475 if ( m_text )
476 m_text->SetInsertionPoint(pos);
477 }
478
479 void wxComboBox::SetInsertionPointEnd()
480 {
481 if ( m_text )
482 m_text->SetInsertionPointEnd();
483 }
484
485 long wxComboBox::GetInsertionPoint() const
486 {
487 if ( m_text )
488 return m_text->GetInsertionPoint();
489 return 0;
490 }
491
492 wxTextPos wxComboBox::GetLastPosition() const
493 {
494 if ( m_text )
495 return m_text->GetLastPosition();
496 return 0;
497 }
498
499 void wxComboBox::Replace(long from, long to, const wxString& value)
500 {
501 if ( m_text )
502 m_text->Replace(from,to,value);
503 }
504
505 void wxComboBox::Remove(long from, long to)
506 {
507 if ( m_text )
508 m_text->Remove(from,to);
509 }
510
511 void wxComboBox::SetSelection(long from, long to)
512 {
513 if ( m_text )
514 m_text->SetSelection(from,to);
515 }
516
517 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
518 unsigned int pos,
519 void **clientData,
520 wxClientDataType type)
521 {
522 return m_choice->DoInsertItems(items, pos, clientData, type);
523 }
524
525 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
526 {
527 return m_choice->DoSetItemClientData( n , clientData ) ;
528 }
529
530 void* wxComboBox::DoGetItemClientData(unsigned int n) const
531 {
532 return m_choice->DoGetItemClientData( n ) ;
533 }
534
535 wxClientDataType wxComboBox::GetClientDataType() const
536 {
537 return m_choice->GetClientDataType();
538 }
539
540 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType)
541 {
542 m_choice->SetClientDataType(clientDataItemsType);
543 }
544
545 void wxComboBox::DoDeleteOneItem(unsigned int n)
546 {
547 m_choice->DoDeleteOneItem( n );
548 }
549
550 void wxComboBox::DoClear()
551 {
552 m_choice->DoClear();
553 }
554
555 int wxComboBox::GetSelection() const
556 {
557 return m_choice->GetSelection();
558 }
559
560 void wxComboBox::SetSelection(int n)
561 {
562 m_choice->SetSelection( n );
563
564 if ( m_text != NULL )
565 m_text->SetValue(n != wxNOT_FOUND ? GetString(n) : wxString(wxEmptyString));
566 }
567
568 int wxComboBox::FindString(const wxString& s, bool bCase) const
569 {
570 return m_choice->FindString( s, bCase );
571 }
572
573 wxString wxComboBox::GetString(unsigned int n) const
574 {
575 return m_choice->GetString( n );
576 }
577
578 wxString wxComboBox::GetStringSelection() const
579 {
580 int sel = GetSelection();
581 if (sel != wxNOT_FOUND)
582 return wxString(this->GetString((unsigned int)sel));
583 else
584 return wxEmptyString;
585 }
586
587 void wxComboBox::SetString(unsigned int n, const wxString& s)
588 {
589 m_choice->SetString( n , s );
590 }
591
592 bool wxComboBox::IsEditable() const
593 {
594 return m_text != NULL && !HasFlag(wxCB_READONLY);
595 }
596
597 void wxComboBox::Undo()
598 {
599 if (m_text != NULL)
600 m_text->Undo();
601 }
602
603 void wxComboBox::Redo()
604 {
605 if (m_text != NULL)
606 m_text->Redo();
607 }
608
609 void wxComboBox::SelectAll()
610 {
611 if (m_text != NULL)
612 m_text->SelectAll();
613 }
614
615 bool wxComboBox::CanCopy() const
616 {
617 if (m_text != NULL)
618 return m_text->CanCopy();
619 else
620 return false;
621 }
622
623 bool wxComboBox::CanCut() const
624 {
625 if (m_text != NULL)
626 return m_text->CanCut();
627 else
628 return false;
629 }
630
631 bool wxComboBox::CanPaste() const
632 {
633 if (m_text != NULL)
634 return m_text->CanPaste();
635 else
636 return false;
637 }
638
639 bool wxComboBox::CanUndo() const
640 {
641 if (m_text != NULL)
642 return m_text->CanUndo();
643 else
644 return false;
645 }
646
647 bool wxComboBox::CanRedo() const
648 {
649 if (m_text != NULL)
650 return m_text->CanRedo();
651 else
652 return false;
653 }
654
655 bool wxComboBox::HandleClicked( double timestampsec )
656 {
657 /*
658 For consistency with other platforms, clicking in the text area does not constitute a selection
659 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
660 event.SetInt(GetSelection());
661 event.SetEventObject(this);
662 event.SetString(GetStringSelection());
663 ProcessCommand(event);
664 */
665
666 return true ;
667 }
668
669 #endif // wxUSE_COMBOBOX