]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/combobox.cpp
avoid overdrawing, fixes #10865
[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;
489468fe
SC
60 }
61
d9d551f6
SC
62 void ForwardEnableTextChangedEvents(bool enable)
63 {
64 EnableTextChangedEvents(enable);
65 }
489468fe
SC
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
9044b125
SC
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 }
d9d551f6 153
489468fe
SC
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)
9044b125
SC
164 EVT_SET_FOCUS(wxComboBoxText::OnFocus)
165 EVT_KILL_FOCUS(wxComboBoxText::OnFocus)
489468fe
SC
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 > size.y)
265 size.y = sizeText.y;
266
267 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
268 size.x += TEXTFOCUSBORDER ;
269 size.y += 2 * TEXTFOCUSBORDER ;
270 }
271 else
272 {
273 // clipping is too tight
274 size.y += 1 ;
275 }
276
277 return size;
278}
279
280void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
281{
282 wxControl::DoMoveWindow( x, y, width , height );
283
284 if ( m_text == NULL )
285 {
286 // we might not be fully constructed yet, therefore watch out...
287 if ( m_choice )
288 m_choice->SetSize(0, 0 , width, -1);
289 }
290 else
291 {
292 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
293 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1);
294
295 // put it at an inset of 1 to have outer area shadows drawn as well
296 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1);
297 }
298}
299
300// ----------------------------------------------------------------------------
301// operations forwarded to the subcontrols
302// ----------------------------------------------------------------------------
303
304bool wxComboBox::Enable(bool enable)
305{
306 if ( !wxControl::Enable(enable) )
307 return false;
308
309 if (m_text)
310 m_text->Enable(enable);
311
312 return true;
313}
314
315bool wxComboBox::Show(bool show)
316{
317 if ( !wxControl::Show(show) )
318 return false;
319
320 return true;
321}
322
323void wxComboBox::DelegateTextChanged( const wxString& value )
324{
325 SetStringSelection( value );
326}
327
328void wxComboBox::DelegateChoice( const wxString& value )
329{
330 SetStringSelection( value );
331}
332
333void wxComboBox::Init()
334{
335 WX_INIT_CONTROL_CONTAINER();
336}
337
338bool wxComboBox::Create(wxWindow *parent,
339 wxWindowID id,
340 const wxString& value,
341 const wxPoint& pos,
342 const wxSize& size,
343 const wxArrayString& choices,
344 long style,
345 const wxValidator& validator,
346 const wxString& name)
347{
348 if ( !Create( parent, id, value, pos, size, 0, NULL,
349 style, validator, name ) )
350 return false;
351
352 Append(choices);
353
354 return true;
355}
356
357bool wxComboBox::Create(wxWindow *parent,
358 wxWindowID id,
359 const wxString& value,
360 const wxPoint& pos,
361 const wxSize& size,
362 int n,
363 const wxString choices[],
364 long style,
365 const wxValidator& validator,
366 const wxString& name)
367{
368 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
369 validator, name) )
370 {
371 return false;
372 }
373
374 wxSize csize = size;
375 if ( style & wxCB_READONLY )
376 {
377 m_text = NULL;
378 }
379 else
380 {
381 m_text = new wxComboBoxText(this);
382 if ( size.y == -1 )
383 {
384 csize.y = m_text->GetSize().y ;
385 csize.y += 2 * TEXTFOCUSBORDER ;
386 }
387 }
388 m_choice = new wxComboBoxChoice(this, style );
389
390 DoSetSize(pos.x, pos.y, csize.x, csize.y);
391
392 Append( n, choices );
393
394 // Needed because it is a wxControlWithItems
395 SetInitialSize(size);
396 SetStringSelection(value);
397
398 return true;
399}
400
d9d551f6
SC
401void wxComboBox::EnableTextChangedEvents(bool enable)
402{
403 if ( m_text )
404 m_text->ForwardEnableTextChangedEvents(enable);
405}
406
00c89b22
VZ
407wxString wxComboBox::DoGetValue() const
408{
409 wxCHECK_MSG( m_text, wxString(), "can't be called for read-only combobox" );
410
411 return m_text->GetValue();
412}
413
489468fe
SC
414wxString wxComboBox::GetValue() const
415{
416 wxString result;
417
418 if ( m_text == NULL )
419 result = m_choice->GetString( m_choice->GetSelection() );
420 else
421 result = m_text->GetValue();
422
423 return result;
424}
425
426unsigned int wxComboBox::GetCount() const
427{
428 return m_choice->GetCount() ;
429}
430
431void wxComboBox::SetValue(const wxString& value)
432{
433 if ( HasFlag(wxCB_READONLY) )
434 SetStringSelection( value ) ;
435 else
436 m_text->SetValue( value );
437}
438
439void wxComboBox::WriteText(const wxString& text)
440{
441 m_text->WriteText(text);
442}
443
444void wxComboBox::GetSelection(long *from, long *to) const
445{
446 m_text->GetSelection(from, to);
447}
448
449// Clipboard operations
450
451void wxComboBox::Copy()
452{
453 if ( m_text != NULL )
454 m_text->Copy();
455}
456
457void wxComboBox::Cut()
458{
459 if ( m_text != NULL )
460 m_text->Cut();
461}
462
463void wxComboBox::Paste()
464{
465 if ( m_text != NULL )
466 m_text->Paste();
467}
468
469void wxComboBox::SetEditable(bool editable)
470{
471 if ( ( m_text == NULL ) && editable )
472 {
473 m_text = new wxComboBoxText( this );
474 }
475 else if ( ( m_text != NULL ) && !editable )
476 {
477 delete m_text;
478 m_text = NULL;
479 }
480
481 int currentX, currentY;
482 GetPosition( &currentX, &currentY );
483
484 int currentW, currentH;
485 GetSize( &currentW, &currentH );
486
487 DoMoveWindow( currentX, currentY, currentW, currentH );
488}
489
490void wxComboBox::SetInsertionPoint(long pos)
491{
492 if ( m_text )
493 m_text->SetInsertionPoint(pos);
494}
495
496void wxComboBox::SetInsertionPointEnd()
497{
498 if ( m_text )
499 m_text->SetInsertionPointEnd();
500}
501
502long wxComboBox::GetInsertionPoint() const
503{
504 if ( m_text )
505 return m_text->GetInsertionPoint();
506 return 0;
507}
508
509wxTextPos wxComboBox::GetLastPosition() const
510{
511 if ( m_text )
512 return m_text->GetLastPosition();
513 return 0;
514}
515
516void wxComboBox::Replace(long from, long to, const wxString& value)
517{
518 if ( m_text )
519 m_text->Replace(from,to,value);
520}
521
522void wxComboBox::Remove(long from, long to)
523{
524 if ( m_text )
525 m_text->Remove(from,to);
526}
527
528void wxComboBox::SetSelection(long from, long to)
529{
530 if ( m_text )
531 m_text->SetSelection(from,to);
532}
533
534int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
535 unsigned int pos,
536 void **clientData,
537 wxClientDataType type)
538{
539 return m_choice->DoInsertItems(items, pos, clientData, type);
540}
541
542void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
543{
544 return m_choice->DoSetItemClientData( n , clientData ) ;
545}
546
547void* wxComboBox::DoGetItemClientData(unsigned int n) const
548{
549 return m_choice->DoGetItemClientData( n ) ;
550}
551
552wxClientDataType wxComboBox::GetClientDataType() const
553{
554 return m_choice->GetClientDataType();
555}
556
557void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType)
558{
559 m_choice->SetClientDataType(clientDataItemsType);
560}
561
562void wxComboBox::DoDeleteOneItem(unsigned int n)
563{
564 m_choice->DoDeleteOneItem( n );
565}
566
567void wxComboBox::DoClear()
568{
569 m_choice->DoClear();
570}
571
572int wxComboBox::GetSelection() const
573{
574 return m_choice->GetSelection();
575}
576
577void wxComboBox::SetSelection(int n)
578{
579 m_choice->SetSelection( n );
580
581 if ( m_text != NULL )
582 m_text->SetValue(n != wxNOT_FOUND ? GetString(n) : wxString(wxEmptyString));
583}
584
585int wxComboBox::FindString(const wxString& s, bool bCase) const
586{
587 return m_choice->FindString( s, bCase );
588}
589
590wxString wxComboBox::GetString(unsigned int n) const
591{
592 return m_choice->GetString( n );
593}
594
595wxString wxComboBox::GetStringSelection() const
596{
597 int sel = GetSelection();
598 if (sel != wxNOT_FOUND)
599 return wxString(this->GetString((unsigned int)sel));
600 else
601 return wxEmptyString;
602}
603
604void wxComboBox::SetString(unsigned int n, const wxString& s)
605{
606 m_choice->SetString( n , s );
607}
608
609bool wxComboBox::IsEditable() const
610{
611 return m_text != NULL && !HasFlag(wxCB_READONLY);
612}
613
614void wxComboBox::Undo()
615{
616 if (m_text != NULL)
617 m_text->Undo();
618}
619
620void wxComboBox::Redo()
621{
622 if (m_text != NULL)
623 m_text->Redo();
624}
625
626void wxComboBox::SelectAll()
627{
628 if (m_text != NULL)
629 m_text->SelectAll();
630}
631
632bool wxComboBox::CanCopy() const
633{
634 if (m_text != NULL)
635 return m_text->CanCopy();
636 else
637 return false;
638}
639
640bool wxComboBox::CanCut() const
641{
642 if (m_text != NULL)
643 return m_text->CanCut();
644 else
645 return false;
646}
647
648bool wxComboBox::CanPaste() const
649{
650 if (m_text != NULL)
651 return m_text->CanPaste();
652 else
653 return false;
654}
655
656bool wxComboBox::CanUndo() const
657{
658 if (m_text != NULL)
659 return m_text->CanUndo();
660 else
661 return false;
662}
663
664bool wxComboBox::CanRedo() const
665{
666 if (m_text != NULL)
667 return m_text->CanRedo();
668 else
669 return false;
670}
671
de0d2095 672bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec) )
489468fe
SC
673{
674/*
675 For consistency with other platforms, clicking in the text area does not constitute a selection
676 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
677 event.SetInt(GetSelection());
678 event.SetEventObject(this);
679 event.SetString(GetStringSelection());
680 ProcessCommand(event);
681*/
682
524c47aa 683 return true ;
489468fe
SC
684}
685
686#endif // wxUSE_COMBOBOX