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