]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
535a468371d3c0d1e40a4b0a6a05f841c47bd320
[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 #include "wx/combobox.h"
19 #include "wx/button.h"
20 #include "wx/menu.h"
21 #include "wx/mac/uma.h"
22
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
25 #endif
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 if ( event.GetKeyCode() == WXK_RETURN )
86 {
87 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_cb->GetId());
88 event.SetString( GetValue() );
89 event.SetInt( m_cb->GetSelection() );
90 event.SetEventObject( m_cb );
91
92 // This will invoke the dialog default action, such
93 // as the clicking the default button.
94
95 if (!m_cb->GetEventHandler()->ProcessEvent( event ))
96 {
97 wxWindow *parent = GetParent();
98 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
99 parent = parent->GetParent() ;
100 }
101 if ( parent && parent->GetDefaultItem() )
102 {
103 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
104 wxButton);
105 if ( def && def->IsEnabled() )
106 {
107 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
108 event.SetEventObject(def);
109 def->Command(event);
110 }
111 }
112
113 return;
114 }
115 }
116
117 event.Skip();
118 }
119
120 // Use the KeyUp as a naive approximation for TEXT_UPDATED, even though it is somewhat delayed
121 // but this is less complicated than dealing with idle-ness, and is much better than nothing
122 void OnKeyUp( wxKeyEvent& event )
123 {
124 if ( event.GetKeyCode() != WXK_RETURN && event.GetKeyCode() != WXK_TAB )
125 {
126 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId());
127 event.SetString( GetValue() );
128 event.SetEventObject( m_cb );
129 m_cb->GetEventHandler()->ProcessEvent(event);
130 }
131 }
132 private:
133 wxComboBox *m_cb;
134
135 DECLARE_EVENT_TABLE()
136 };
137
138 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
139 EVT_CHAR( wxComboBoxText::OnChar)
140 EVT_KEY_UP( wxComboBoxText::OnKeyUp)
141 END_EVENT_TABLE()
142
143 class wxComboBoxChoice : public wxChoice
144 {
145 public:
146 wxComboBoxChoice(wxComboBox *cb, int style)
147 : wxChoice( cb , 1 )
148 {
149 m_cb = cb;
150 }
151 int GetPopupWidth() const
152 {
153 switch ( GetWindowVariant() )
154 {
155 case wxWINDOW_VARIANT_NORMAL :
156 case wxWINDOW_VARIANT_LARGE :
157 return 24 ;
158 default :
159 return 21 ;
160 }
161 }
162
163 protected:
164 void OnChoice( wxCommandEvent& e )
165 {
166 wxString s = e.GetString();
167
168 m_cb->DelegateChoice( s );
169 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
170 event2.SetInt(m_cb->GetSelection());
171 event2.SetEventObject(m_cb);
172 event2.SetString(m_cb->GetStringSelection());
173 m_cb->ProcessCommand(event2);
174
175 // For consistency with MSW and GTK, also send a text updated event
176 // After all, the text is updated when a selection is made
177 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId() );
178 TextEvent.SetString( m_cb->GetStringSelection() );
179 TextEvent.SetEventObject( m_cb );
180 m_cb->ProcessCommand( TextEvent );
181 }
182 virtual wxSize DoGetBestSize() const
183 {
184 wxSize sz = wxChoice::DoGetBestSize() ;
185 if (! m_cb->HasFlag(wxCB_READONLY) )
186 sz.x = GetPopupWidth() ;
187 return sz ;
188 }
189
190 private:
191 wxComboBox *m_cb;
192
193 DECLARE_EVENT_TABLE()
194 };
195
196 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
197 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
198 END_EVENT_TABLE()
199
200 wxComboBox::~wxComboBox()
201 {
202 // delete client objects
203 FreeData();
204
205 // delete the controls now, don't leave them alive even though they would
206 // still be eventually deleted by our parent - but it will be too late, the
207 // user code expects them to be gone now
208 if (m_text != NULL) {
209 delete m_text;
210 m_text = NULL;
211 }
212 if (m_choice != NULL) {
213 delete m_choice;
214 m_choice = NULL;
215 }
216 }
217
218
219 // ----------------------------------------------------------------------------
220 // geometry
221 // ----------------------------------------------------------------------------
222
223 wxSize wxComboBox::DoGetBestSize() const
224 {
225 if (!m_choice && !m_text)
226 return GetSize();
227 wxSize size = m_choice->GetBestSize();
228
229 if ( m_text != NULL )
230 {
231 wxSize sizeText = m_text->GetBestSize();
232 if (sizeText.y > size.y)
233 size.y = sizeText.y;
234 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
235 size.x += TEXTFOCUSBORDER ;
236 size.y += 2 * TEXTFOCUSBORDER ;
237 }
238 else
239 {
240 // clipping is too tight
241 size.y += 1 ;
242 }
243 return size;
244 }
245
246 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
247 {
248 wxControl::DoMoveWindow(x, y, width , height );
249
250 if ( m_text == NULL )
251 {
252 // we might not be fully constructed yet, therefore watch out...
253 if ( m_choice )
254 m_choice->SetSize(0, 0 , width, -1);
255 }
256 else
257 {
258 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
259 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1 );
260 // put it at an inset of 1 to have outer area shadows drawn as well
261 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1);
262 }
263 }
264
265
266
267 // ----------------------------------------------------------------------------
268 // operations forwarded to the subcontrols
269 // ----------------------------------------------------------------------------
270
271 bool wxComboBox::Enable(bool enable)
272 {
273 if ( !wxControl::Enable(enable) )
274 return false;
275
276 return true;
277 }
278
279 bool wxComboBox::Show(bool show)
280 {
281 if ( !wxControl::Show(show) )
282 return false;
283
284 return true;
285 }
286
287 void wxComboBox::SetFocus()
288 {
289 if ( m_text != NULL) {
290 m_text->SetFocus();
291 }
292 }
293
294
295 void wxComboBox::DelegateTextChanged( const wxString& value )
296 {
297 SetStringSelection( value );
298 }
299
300
301 void wxComboBox::DelegateChoice( const wxString& value )
302 {
303 SetStringSelection( value );
304 }
305
306
307 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
308 const wxString& value,
309 const wxPoint& pos,
310 const wxSize& size,
311 const wxArrayString& choices,
312 long style,
313 const wxValidator& validator,
314 const wxString& name)
315 {
316 wxCArrayString chs( choices );
317
318 return Create( parent, id, value, pos, size, chs.GetCount(),
319 chs.GetStrings(), style, validator, name );
320 }
321
322
323 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
324 const wxString& value,
325 const wxPoint& pos,
326 const wxSize& size,
327 int n, const wxString choices[],
328 long style,
329 const wxValidator& validator,
330 const wxString& name)
331 {
332 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
333 wxDefaultValidator, name) )
334 {
335 return false;
336 }
337
338 m_choice = new wxComboBoxChoice(this, style );
339 wxSize csize = size;
340 if ( style & wxCB_READONLY )
341 {
342 m_text = NULL;
343 }
344 else
345 {
346 m_text = new wxComboBoxText(this);
347 if ( size.y == -1 )
348 {
349 csize.y = m_text->GetSize().y ;
350 csize.y += 2 * TEXTFOCUSBORDER ;
351 }
352 }
353
354 DoSetSize(pos.x, pos.y, csize.x, csize.y);
355
356 for ( int i = 0 ; i < n ; i++ )
357 {
358 m_choice->DoAppend( choices[ i ] );
359 }
360
361 SetBestSize(size); // Needed because it is a wxControlWithItems
362 SetStringSelection(value);
363
364 return true;
365 }
366
367 wxString wxComboBox::GetValue() const
368 {
369 wxString result;
370
371 if ( m_text == NULL )
372 {
373 result = m_choice->GetString( m_choice->GetSelection() );
374 }
375 else
376 {
377 result = m_text->GetValue();
378 }
379
380 return result;
381 }
382
383 int wxComboBox::GetCount() const
384 {
385 return m_choice->GetCount() ;
386 }
387
388 void wxComboBox::SetValue(const wxString& value)
389 {
390 if ( HasFlag(wxCB_READONLY) )
391 SetStringSelection( value ) ;
392 else
393 m_text->SetValue( value );
394 }
395
396 // Clipboard operations
397 void wxComboBox::Copy()
398 {
399 if ( m_text != NULL )
400 {
401 m_text->Copy();
402 }
403 }
404
405 void wxComboBox::Cut()
406 {
407 if ( m_text != NULL )
408 {
409 m_text->Cut();
410 }
411 }
412
413 void wxComboBox::Paste()
414 {
415 if ( m_text != NULL )
416 {
417 m_text->Paste();
418 }
419 }
420
421 void wxComboBox::SetEditable(bool editable)
422 {
423 if ( ( m_text == NULL ) && editable )
424 {
425 m_text = new wxComboBoxText( this );
426 }
427 else if ( ( m_text != NULL ) && !editable )
428 {
429 delete m_text;
430 m_text = NULL;
431 }
432
433 int currentX, currentY;
434 GetPosition( &currentX, &currentY );
435
436 int currentW, currentH;
437 GetSize( &currentW, &currentH );
438
439 DoMoveWindow( currentX, currentY, currentW, currentH );
440 }
441
442 void wxComboBox::SetInsertionPoint(long pos)
443 {
444 // TODO
445 }
446
447 void wxComboBox::SetInsertionPointEnd()
448 {
449 // TODO
450 }
451
452 long wxComboBox::GetInsertionPoint() const
453 {
454 // TODO
455 return 0;
456 }
457
458 wxTextPos wxComboBox::GetLastPosition() const
459 {
460 // TODO
461 return 0;
462 }
463
464 void wxComboBox::Replace(long from, long to, const wxString& value)
465 {
466 // TODO
467 }
468
469 void wxComboBox::Remove(long from, long to)
470 {
471 // TODO
472 }
473
474 void wxComboBox::SetSelection(long from, long to)
475 {
476 // TODO
477 }
478
479 int wxComboBox::DoAppend(const wxString& item)
480 {
481 return m_choice->DoAppend( item ) ;
482 }
483
484 int wxComboBox::DoInsert(const wxString& item, int pos)
485 {
486 return m_choice->DoInsert( item , pos ) ;
487 }
488
489 void wxComboBox::DoSetItemClientData(int n, void* clientData)
490 {
491 return m_choice->DoSetItemClientData( n , clientData ) ;
492 }
493
494 void* wxComboBox::DoGetItemClientData(int n) const
495 {
496 return m_choice->DoGetItemClientData( n ) ;
497 }
498
499 void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
500 {
501 return m_choice->DoSetItemClientObject( n , clientData ) ;
502 }
503
504 wxClientData* wxComboBox::DoGetItemClientObject(int n) const
505 {
506 return m_choice->DoGetItemClientObject( n ) ;
507 }
508
509 void wxComboBox::FreeData()
510 {
511 if ( HasClientObjectData() )
512 {
513 size_t count = GetCount();
514 for ( size_t n = 0; n < count; n++ )
515 {
516 SetClientObject( n, NULL );
517 }
518 }
519 }
520
521 void wxComboBox::Delete(int n)
522 {
523 // force client object deletion
524 if( HasClientObjectData() )
525 SetClientObject( n, NULL );
526 m_choice->Delete( n );
527 }
528
529 void wxComboBox::Clear()
530 {
531 FreeData();
532 m_choice->Clear();
533 }
534
535 int wxComboBox::GetSelection() const
536 {
537 return m_choice->GetSelection();
538 }
539
540 void wxComboBox::SetSelection(int n)
541 {
542 m_choice->SetSelection( n );
543
544 if ( m_text != NULL )
545 {
546 m_text->SetValue( GetString( n ) );
547 }
548 }
549
550 int wxComboBox::FindString(const wxString& s) const
551 {
552 return m_choice->FindString( s );
553 }
554
555 wxString wxComboBox::GetString(int n) const
556 {
557 return m_choice->GetString( n );
558 }
559
560 wxString wxComboBox::GetStringSelection() const
561 {
562 int sel = GetSelection ();
563 if (sel > -1)
564 return wxString(this->GetString (sel));
565 else
566 return wxEmptyString;
567 }
568
569 void wxComboBox::SetString(int n, const wxString& s)
570 {
571 m_choice->SetString( n , s ) ;
572 }
573
574 bool wxComboBox::IsEditable() const
575 {
576 return m_text != NULL && !HasFlag(wxCB_READONLY);
577 }
578
579 void wxComboBox::Undo()
580 {
581 if (m_text != NULL)
582 m_text->Undo();
583 }
584
585 void wxComboBox::Redo()
586 {
587 if (m_text != NULL)
588 m_text->Redo();
589 }
590
591 void wxComboBox::SelectAll()
592 {
593 if (m_text != NULL)
594 m_text->SelectAll();
595 }
596
597 bool wxComboBox::CanCopy() const
598 {
599 if (m_text != NULL)
600 return m_text->CanCopy();
601 else
602 return false;
603 }
604
605 bool wxComboBox::CanCut() const
606 {
607 if (m_text != NULL)
608 return m_text->CanCut();
609 else
610 return false;
611 }
612
613 bool wxComboBox::CanPaste() const
614 {
615 if (m_text != NULL)
616 return m_text->CanPaste();
617 else
618 return false;
619 }
620
621 bool wxComboBox::CanUndo() const
622 {
623 if (m_text != NULL)
624 return m_text->CanUndo();
625 else
626 return false;
627 }
628
629 bool wxComboBox::CanRedo() const
630 {
631 if (m_text != NULL)
632 return m_text->CanRedo();
633 else
634 return false;
635 }
636
637 wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
638 {
639 /* For consistency with other platforms, clicking in the text area does not constitute a selection
640 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
641 event.SetInt(GetSelection());
642 event.SetEventObject(this);
643 event.SetString(GetStringSelection());
644 ProcessCommand(event);*/
645 return noErr ;
646 }
647