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