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