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