]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
support for Mac Help Button added (wxID_HELP) is automatically translated
[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 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/combobox.h"
17 #include "wx/button.h"
18 #include "wx/menu.h"
19 #include "wx/mac/uma.h"
20
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
23 #endif
24
25 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26
27
28 static int nextPopUpMenuId = 1000 ;
29 MenuHandle NewUniqueMenu()
30 {
31 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
32 nextPopUpMenuId++ ;
33 return handle ;
34 }
35
36
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 // the margin between the text control and the choice
42 #if TARGET_API_MAC_OSX
43 // margin should be bigger on OS X due to blue highlight
44 // around text control.
45 static const wxCoord MARGIN = 4;
46 // this is the border a focus rect on OSX is needing
47 static const int TEXTFOCUSBORDER = 3 ;
48 #else
49 static const wxCoord MARGIN = 2;
50 static const int TEXTFOCUSBORDER = 0 ;
51 #endif
52 static const int POPUPHEIGHT = 23;
53
54
55 // ----------------------------------------------------------------------------
56 // wxComboBoxText: text control forwards events to combobox
57 // ----------------------------------------------------------------------------
58
59 class wxComboBoxText : public wxTextCtrl
60 {
61 public:
62 wxComboBoxText( wxComboBox * cb )
63 : wxTextCtrl( cb , 1 )
64 {
65 m_cb = cb;
66 }
67
68 protected:
69 void OnChar( wxKeyEvent& event )
70 {
71 if ( event.GetKeyCode() == WXK_RETURN )
72 {
73 wxString value = GetValue();
74
75 if ( m_cb->GetCount() == 0 )
76 {
77 // make Enter generate "selected" event if there is only one item
78 // in the combobox - without it, it's impossible to select it at
79 // all!
80 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
81 event.SetInt( 0 );
82 event.SetString( value );
83 event.SetEventObject( m_cb );
84 m_cb->GetEventHandler()->ProcessEvent( event );
85 }
86 else
87 {
88 // add the item to the list if it's not there yet
89 if ( m_cb->FindString(value) == wxNOT_FOUND )
90 {
91 m_cb->Append(value);
92 m_cb->SetStringSelection(value);
93
94 // and generate the selected event for it
95 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
96 event.SetInt( m_cb->GetCount() - 1 );
97 event.SetString( value );
98 event.SetEventObject( m_cb );
99 m_cb->GetEventHandler()->ProcessEvent( event );
100 }
101
102 // This will invoke the dialog default action, such
103 // as the clicking the default button.
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 return ;
119 }
120 }
121
122 return;
123 }
124 }
125
126 event.Skip();
127 }
128
129 private:
130 wxComboBox *m_cb;
131
132 DECLARE_EVENT_TABLE()
133 };
134
135 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
136 EVT_CHAR( wxComboBoxText::OnChar)
137 END_EVENT_TABLE()
138
139 class wxComboBoxChoice : public wxChoice
140 {
141 public:
142 wxComboBoxChoice(wxComboBox *cb, int style)
143 : wxChoice( cb , 1 )
144 {
145 m_cb = cb;
146 }
147 int GetPopupWidth() const
148 {
149 switch ( GetWindowVariant() )
150 {
151 case wxWINDOW_VARIANT_NORMAL :
152 case wxWINDOW_VARIANT_LARGE :
153 return 24 ;
154 default :
155 return 21 ;
156 }
157 }
158
159 protected:
160 void OnChoice( wxCommandEvent& e )
161 {
162 wxString s = e.GetString();
163
164 m_cb->DelegateChoice( s );
165 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
166 event2.SetInt(m_cb->GetSelection());
167 event2.SetEventObject(m_cb);
168 event2.SetString(m_cb->GetStringSelection());
169 m_cb->ProcessCommand(event2);
170 }
171 virtual wxSize DoGetBestSize() const
172 {
173 wxSize sz = wxChoice::DoGetBestSize() ;
174 if (! m_cb->HasFlag(wxCB_READONLY) )
175 sz.x = GetPopupWidth() ;
176 return sz ;
177 }
178
179 private:
180 wxComboBox *m_cb;
181
182 DECLARE_EVENT_TABLE()
183 };
184
185 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
186 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
187 END_EVENT_TABLE()
188
189 wxComboBox::~wxComboBox()
190 {
191 // delete client objects
192 FreeData();
193
194 // delete the controls now, don't leave them alive even though they would
195 // still be eventually deleted by our parent - but it will be too late, the
196 // user code expects them to be gone now
197 if (m_text != NULL) {
198 delete m_text;
199 m_text = NULL;
200 }
201 if (m_choice != NULL) {
202 delete m_choice;
203 m_choice = NULL;
204 }
205 }
206
207
208 // ----------------------------------------------------------------------------
209 // geometry
210 // ----------------------------------------------------------------------------
211
212 wxSize wxComboBox::DoGetBestSize() const
213 {
214 if (!m_choice && !m_text)
215 return GetSize();
216 wxSize size = m_choice->GetBestSize();
217
218 if ( m_text != NULL )
219 {
220 wxSize sizeText = m_text->GetBestSize();
221 if (sizeText.y > size.y)
222 size.y = sizeText.y;
223 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
224 size.x += TEXTFOCUSBORDER ;
225 size.y += 2 * TEXTFOCUSBORDER ;
226 }
227 else
228 {
229 // clipping is too tight
230 size.y += 1 ;
231 }
232 return size;
233 }
234
235 void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
236 {
237 wxControl::DoMoveWindow(x, y, width , height );
238
239 if ( m_text == NULL )
240 {
241 // we might not be fully constructed yet, therefore watch out...
242 if ( m_choice )
243 m_choice->SetSize(0, 0 , width, -1);
244 }
245 else
246 {
247 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
248 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1 );
249 // put it at an inset of 1 to have outer area shadows drawn as well
250 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1);
251 }
252 }
253
254
255
256 // ----------------------------------------------------------------------------
257 // operations forwarded to the subcontrols
258 // ----------------------------------------------------------------------------
259
260 bool wxComboBox::Enable(bool enable)
261 {
262 if ( !wxControl::Enable(enable) )
263 return FALSE;
264
265 return TRUE;
266 }
267
268 bool wxComboBox::Show(bool show)
269 {
270 if ( !wxControl::Show(show) )
271 return FALSE;
272
273 return TRUE;
274 }
275
276 void wxComboBox::SetFocus()
277 {
278 if ( m_text != NULL) {
279 m_text->SetFocus();
280 }
281 }
282
283
284 void wxComboBox::DelegateTextChanged( const wxString& value )
285 {
286 SetStringSelection( value );
287 }
288
289
290 void wxComboBox::DelegateChoice( const wxString& value )
291 {
292 SetStringSelection( value );
293 }
294
295
296 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
297 const wxString& value,
298 const wxPoint& pos,
299 const wxSize& size,
300 const wxArrayString& choices,
301 long style,
302 const wxValidator& validator,
303 const wxString& name)
304 {
305 wxCArrayString chs( choices );
306
307 return Create( parent, id, value, pos, size, chs.GetCount(),
308 chs.GetStrings(), style, validator, name );
309 }
310
311
312 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
313 const wxString& value,
314 const wxPoint& pos,
315 const wxSize& size,
316 int n, const wxString choices[],
317 long style,
318 const wxValidator& validator,
319 const wxString& name)
320 {
321 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
322 wxDefaultValidator, name) )
323 {
324 return FALSE;
325 }
326
327 m_choice = new wxComboBoxChoice(this, style );
328 wxSize csize = size;
329 if ( style & wxCB_READONLY )
330 {
331 m_text = NULL;
332 }
333 else
334 {
335 m_text = new wxComboBoxText(this);
336 if ( size.y == -1 )
337 {
338 csize.y = m_text->GetSize().y ;
339 csize.y += 2 * TEXTFOCUSBORDER ;
340 }
341 }
342
343 DoSetSize(pos.x, pos.y, csize.x, csize.y);
344
345 for ( int i = 0 ; i < n ; i++ )
346 {
347 m_choice->DoAppend( choices[ i ] );
348 }
349
350 SetBestSize(size); // Needed because it is a wxControlWithItems
351
352 return TRUE;
353 }
354
355 wxString wxComboBox::GetValue() const
356 {
357 wxString result;
358
359 if ( m_text == NULL )
360 {
361 result = m_choice->GetString( m_choice->GetSelection() );
362 }
363 else
364 {
365 result = m_text->GetValue();
366 }
367
368 return result;
369 }
370
371 int wxComboBox::GetCount() const
372 {
373 return m_choice->GetCount() ;
374 }
375
376 void wxComboBox::SetValue(const wxString& value)
377 {
378 int s = FindString (value);
379 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
380 {
381 m_choice->Append(value) ;
382 }
383 SetStringSelection( value ) ;
384 }
385
386 // Clipboard operations
387 void wxComboBox::Copy()
388 {
389 if ( m_text != NULL )
390 {
391 m_text->Copy();
392 }
393 }
394
395 void wxComboBox::Cut()
396 {
397 if ( m_text != NULL )
398 {
399 m_text->Cut();
400 }
401 }
402
403 void wxComboBox::Paste()
404 {
405 if ( m_text != NULL )
406 {
407 m_text->Paste();
408 }
409 }
410
411 void wxComboBox::SetEditable(bool editable)
412 {
413 if ( ( m_text == NULL ) && editable )
414 {
415 m_text = new wxComboBoxText( this );
416 }
417 else if ( ( m_text != NULL ) && !editable )
418 {
419 delete m_text;
420 m_text = NULL;
421 }
422
423 int currentX, currentY;
424 GetPosition( &currentX, &currentY );
425
426 int currentW, currentH;
427 GetSize( &currentW, &currentH );
428
429 DoMoveWindow( currentX, currentY, currentW, currentH );
430 }
431
432 void wxComboBox::SetInsertionPoint(long pos)
433 {
434 // TODO
435 }
436
437 void wxComboBox::SetInsertionPointEnd()
438 {
439 // TODO
440 }
441
442 long wxComboBox::GetInsertionPoint() const
443 {
444 // TODO
445 return 0;
446 }
447
448 long wxComboBox::GetLastPosition() const
449 {
450 // TODO
451 return 0;
452 }
453
454 void wxComboBox::Replace(long from, long to, const wxString& value)
455 {
456 // TODO
457 }
458
459 void wxComboBox::Remove(long from, long to)
460 {
461 // TODO
462 }
463
464 void wxComboBox::SetSelection(long from, long to)
465 {
466 // TODO
467 }
468
469 int wxComboBox::DoAppend(const wxString& item)
470 {
471 return m_choice->DoAppend( item ) ;
472 }
473
474 int wxComboBox::DoInsert(const wxString& item, int pos)
475 {
476 return m_choice->DoInsert( item , pos ) ;
477 }
478
479 void wxComboBox::DoSetItemClientData(int n, void* clientData)
480 {
481 return m_choice->DoSetItemClientData( n , clientData ) ;
482 }
483
484 void* wxComboBox::DoGetItemClientData(int n) const
485 {
486 return m_choice->DoGetItemClientData( n ) ;
487 }
488
489 void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
490 {
491 return m_choice->DoSetItemClientObject( n , clientData ) ;
492 }
493
494 wxClientData* wxComboBox::DoGetItemClientObject(int n) const
495 {
496 return m_choice->DoGetItemClientObject( n ) ;
497 }
498
499 void wxComboBox::FreeData()
500 {
501 if ( HasClientObjectData() )
502 {
503 size_t count = GetCount();
504 for ( size_t n = 0; n < count; n++ )
505 {
506 SetClientObject( n, NULL );
507 }
508 }
509 }
510
511 void wxComboBox::Delete(int n)
512 {
513 // force client object deletion
514 if( HasClientObjectData() )
515 SetClientObject( n, NULL );
516 m_choice->Delete( n );
517 }
518
519 void wxComboBox::Clear()
520 {
521 FreeData();
522 m_choice->Clear();
523 }
524
525 int wxComboBox::GetSelection() const
526 {
527 return m_choice->GetSelection();
528 }
529
530 void wxComboBox::SetSelection(int n)
531 {
532 m_choice->SetSelection( n );
533
534 if ( m_text != NULL )
535 {
536 m_text->SetValue( GetString( n ) );
537 }
538 }
539
540 int wxComboBox::FindString(const wxString& s) const
541 {
542 return m_choice->FindString( s );
543 }
544
545 wxString wxComboBox::GetString(int n) const
546 {
547 return m_choice->GetString( n );
548 }
549
550 wxString wxComboBox::GetStringSelection() const
551 {
552 int sel = GetSelection ();
553 if (sel > -1)
554 return wxString(this->GetString (sel));
555 else
556 return wxEmptyString;
557 }
558
559 bool wxComboBox::SetStringSelection(const wxString& sel)
560 {
561 int s = FindString (sel);
562 if (s > -1)
563 {
564 SetSelection (s);
565 return TRUE;
566 }
567 else
568 return FALSE;
569 }
570
571 void wxComboBox::SetString(int n, const wxString& s)
572 {
573 m_choice->SetString( n , s ) ;
574 }
575
576
577 wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
578 {
579 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
580 event.SetInt(GetSelection());
581 event.SetEventObject(this);
582 event.SetString(GetStringSelection());
583 ProcessCommand(event);
584 return noErr ;
585 }
586