]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
tentative fix for BestSize problems (non-native toolbar)
[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 if (m_text)
299 m_text->Enable(enable);
300
301 return true;
302 }
303
304 bool wxComboBox::Show(bool show)
305 {
306 if ( !wxControl::Show(show) )
307 return false;
308
309 return true;
310 }
311
312 void wxComboBox::SetFocus()
313 {
314 if ( m_text != NULL) {
315 m_text->SetFocus();
316 }
317 }
318
319
320 void wxComboBox::DelegateTextChanged( const wxString& value )
321 {
322 SetStringSelection( value );
323 }
324
325
326 void wxComboBox::DelegateChoice( const wxString& value )
327 {
328 SetStringSelection( value );
329 }
330
331
332 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
333 const wxString& value,
334 const wxPoint& pos,
335 const wxSize& size,
336 const wxArrayString& choices,
337 long style,
338 const wxValidator& validator,
339 const wxString& name)
340 {
341 wxCArrayString chs( choices );
342
343 return Create( parent, id, value, pos, size, chs.GetCount(),
344 chs.GetStrings(), style, validator, name );
345 }
346
347
348 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
349 const wxString& value,
350 const wxPoint& pos,
351 const wxSize& size,
352 int n, const wxString choices[],
353 long style,
354 const wxValidator& validator,
355 const wxString& name)
356 {
357 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
358 validator, name) )
359 {
360 return false;
361 }
362
363 m_choice = new wxComboBoxChoice(this, style );
364 wxSize csize = size;
365 if ( style & wxCB_READONLY )
366 {
367 m_text = NULL;
368 }
369 else
370 {
371 m_text = new wxComboBoxText(this);
372 if ( size.y == -1 )
373 {
374 csize.y = m_text->GetSize().y ;
375 csize.y += 2 * TEXTFOCUSBORDER ;
376 }
377 }
378
379 DoSetSize(pos.x, pos.y, csize.x, csize.y);
380
381 for ( int i = 0 ; i < n ; i++ )
382 {
383 m_choice->DoAppend( choices[ i ] );
384 }
385
386 SetBestSize(size); // Needed because it is a wxControlWithItems
387 SetStringSelection(value);
388
389 return true;
390 }
391
392 wxString wxComboBox::GetValue() const
393 {
394 wxString result;
395
396 if ( m_text == NULL )
397 {
398 result = m_choice->GetString( m_choice->GetSelection() );
399 }
400 else
401 {
402 result = m_text->GetValue();
403 }
404
405 return result;
406 }
407
408 int wxComboBox::GetCount() const
409 {
410 return m_choice->GetCount() ;
411 }
412
413 void wxComboBox::SetValue(const wxString& value)
414 {
415 if ( HasFlag(wxCB_READONLY) )
416 SetStringSelection( value ) ;
417 else
418 m_text->SetValue( value );
419 }
420
421 // Clipboard operations
422 void wxComboBox::Copy()
423 {
424 if ( m_text != NULL )
425 {
426 m_text->Copy();
427 }
428 }
429
430 void wxComboBox::Cut()
431 {
432 if ( m_text != NULL )
433 {
434 m_text->Cut();
435 }
436 }
437
438 void wxComboBox::Paste()
439 {
440 if ( m_text != NULL )
441 {
442 m_text->Paste();
443 }
444 }
445
446 void wxComboBox::SetEditable(bool editable)
447 {
448 if ( ( m_text == NULL ) && editable )
449 {
450 m_text = new wxComboBoxText( this );
451 }
452 else if ( ( m_text != NULL ) && !editable )
453 {
454 delete m_text;
455 m_text = NULL;
456 }
457
458 int currentX, currentY;
459 GetPosition( &currentX, &currentY );
460
461 int currentW, currentH;
462 GetSize( &currentW, &currentH );
463
464 DoMoveWindow( currentX, currentY, currentW, currentH );
465 }
466
467 void wxComboBox::SetInsertionPoint(long pos)
468 {
469 // TODO
470 }
471
472 void wxComboBox::SetInsertionPointEnd()
473 {
474 // TODO
475 }
476
477 long wxComboBox::GetInsertionPoint() const
478 {
479 // TODO
480 return 0;
481 }
482
483 wxTextPos wxComboBox::GetLastPosition() const
484 {
485 // TODO
486 return 0;
487 }
488
489 void wxComboBox::Replace(long from, long to, const wxString& value)
490 {
491 // TODO
492 }
493
494 void wxComboBox::Remove(long from, long to)
495 {
496 // TODO
497 }
498
499 void wxComboBox::SetSelection(long from, long to)
500 {
501 // TODO
502 }
503
504 int wxComboBox::DoAppend(const wxString& item)
505 {
506 return m_choice->DoAppend( item ) ;
507 }
508
509 int wxComboBox::DoInsert(const wxString& item, int pos)
510 {
511 return m_choice->DoInsert( item , pos ) ;
512 }
513
514 void wxComboBox::DoSetItemClientData(int n, void* clientData)
515 {
516 return m_choice->DoSetItemClientData( n , clientData ) ;
517 }
518
519 void* wxComboBox::DoGetItemClientData(int n) const
520 {
521 return m_choice->DoGetItemClientData( n ) ;
522 }
523
524 void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
525 {
526 return m_choice->DoSetItemClientObject( n , clientData ) ;
527 }
528
529 wxClientData* wxComboBox::DoGetItemClientObject(int n) const
530 {
531 return m_choice->DoGetItemClientObject( n ) ;
532 }
533
534 void wxComboBox::FreeData()
535 {
536 if ( HasClientObjectData() )
537 {
538 size_t count = GetCount();
539 for ( size_t n = 0; n < count; n++ )
540 {
541 SetClientObject( n, NULL );
542 }
543 }
544 }
545
546 void wxComboBox::Delete(int n)
547 {
548 // force client object deletion
549 if( HasClientObjectData() )
550 SetClientObject( n, NULL );
551 m_choice->Delete( n );
552 }
553
554 void wxComboBox::Clear()
555 {
556 FreeData();
557 m_choice->Clear();
558 }
559
560 int wxComboBox::GetSelection() const
561 {
562 return m_choice->GetSelection();
563 }
564
565 void wxComboBox::SetSelection(int n)
566 {
567 m_choice->SetSelection( n );
568
569 if ( m_text != NULL )
570 {
571 m_text->SetValue( GetString( n ) );
572 }
573 }
574
575 int wxComboBox::FindString(const wxString& s) const
576 {
577 return m_choice->FindString( s );
578 }
579
580 wxString wxComboBox::GetString(int n) const
581 {
582 return m_choice->GetString( n );
583 }
584
585 wxString wxComboBox::GetStringSelection() const
586 {
587 int sel = GetSelection ();
588 if (sel > -1)
589 return wxString(this->GetString (sel));
590 else
591 return wxEmptyString;
592 }
593
594 void wxComboBox::SetString(int n, const wxString& s)
595 {
596 m_choice->SetString( n , s ) ;
597 }
598
599 bool wxComboBox::IsEditable() const
600 {
601 return m_text != NULL && !HasFlag(wxCB_READONLY);
602 }
603
604 void wxComboBox::Undo()
605 {
606 if (m_text != NULL)
607 m_text->Undo();
608 }
609
610 void wxComboBox::Redo()
611 {
612 if (m_text != NULL)
613 m_text->Redo();
614 }
615
616 void wxComboBox::SelectAll()
617 {
618 if (m_text != NULL)
619 m_text->SelectAll();
620 }
621
622 bool wxComboBox::CanCopy() const
623 {
624 if (m_text != NULL)
625 return m_text->CanCopy();
626 else
627 return false;
628 }
629
630 bool wxComboBox::CanCut() const
631 {
632 if (m_text != NULL)
633 return m_text->CanCut();
634 else
635 return false;
636 }
637
638 bool wxComboBox::CanPaste() const
639 {
640 if (m_text != NULL)
641 return m_text->CanPaste();
642 else
643 return false;
644 }
645
646 bool wxComboBox::CanUndo() const
647 {
648 if (m_text != NULL)
649 return m_text->CanUndo();
650 else
651 return false;
652 }
653
654 bool wxComboBox::CanRedo() const
655 {
656 if (m_text != NULL)
657 return m_text->CanRedo();
658 else
659 return false;
660 }
661
662 wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
663 {
664 /* For consistency with other platforms, clicking in the text area does not constitute a selection
665 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
666 event.SetInt(GetSelection());
667 event.SetEventObject(this);
668 event.SetString(GetStringSelection());
669 ProcessCommand(event);*/
670 return noErr ;
671 }
672
673 #endif