]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
fixing retval for SetFont
[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 if ( HasFlag(wxCB_READONLY) )
379 SetStringSelection( value ) ;
380 else
381 m_text->SetValue( value );
382 }
383
384 // Clipboard operations
385 void wxComboBox::Copy()
386 {
387 if ( m_text != NULL )
388 {
389 m_text->Copy();
390 }
391 }
392
393 void wxComboBox::Cut()
394 {
395 if ( m_text != NULL )
396 {
397 m_text->Cut();
398 }
399 }
400
401 void wxComboBox::Paste()
402 {
403 if ( m_text != NULL )
404 {
405 m_text->Paste();
406 }
407 }
408
409 void wxComboBox::SetEditable(bool editable)
410 {
411 if ( ( m_text == NULL ) && editable )
412 {
413 m_text = new wxComboBoxText( this );
414 }
415 else if ( ( m_text != NULL ) && !editable )
416 {
417 delete m_text;
418 m_text = NULL;
419 }
420
421 int currentX, currentY;
422 GetPosition( &currentX, &currentY );
423
424 int currentW, currentH;
425 GetSize( &currentW, &currentH );
426
427 DoMoveWindow( currentX, currentY, currentW, currentH );
428 }
429
430 void wxComboBox::SetInsertionPoint(long pos)
431 {
432 // TODO
433 }
434
435 void wxComboBox::SetInsertionPointEnd()
436 {
437 // TODO
438 }
439
440 long wxComboBox::GetInsertionPoint() const
441 {
442 // TODO
443 return 0;
444 }
445
446 long wxComboBox::GetLastPosition() const
447 {
448 // TODO
449 return 0;
450 }
451
452 void wxComboBox::Replace(long from, long to, const wxString& value)
453 {
454 // TODO
455 }
456
457 void wxComboBox::Remove(long from, long to)
458 {
459 // TODO
460 }
461
462 void wxComboBox::SetSelection(long from, long to)
463 {
464 // TODO
465 }
466
467 int wxComboBox::DoAppend(const wxString& item)
468 {
469 return m_choice->DoAppend( item ) ;
470 }
471
472 int wxComboBox::DoInsert(const wxString& item, int pos)
473 {
474 return m_choice->DoInsert( item , pos ) ;
475 }
476
477 void wxComboBox::DoSetItemClientData(int n, void* clientData)
478 {
479 return m_choice->DoSetItemClientData( n , clientData ) ;
480 }
481
482 void* wxComboBox::DoGetItemClientData(int n) const
483 {
484 return m_choice->DoGetItemClientData( n ) ;
485 }
486
487 void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
488 {
489 return m_choice->DoSetItemClientObject( n , clientData ) ;
490 }
491
492 wxClientData* wxComboBox::DoGetItemClientObject(int n) const
493 {
494 return m_choice->DoGetItemClientObject( n ) ;
495 }
496
497 void wxComboBox::FreeData()
498 {
499 if ( HasClientObjectData() )
500 {
501 size_t count = GetCount();
502 for ( size_t n = 0; n < count; n++ )
503 {
504 SetClientObject( n, NULL );
505 }
506 }
507 }
508
509 void wxComboBox::Delete(int n)
510 {
511 // force client object deletion
512 if( HasClientObjectData() )
513 SetClientObject( n, NULL );
514 m_choice->Delete( n );
515 }
516
517 void wxComboBox::Clear()
518 {
519 FreeData();
520 m_choice->Clear();
521 }
522
523 int wxComboBox::GetSelection() const
524 {
525 return m_choice->GetSelection();
526 }
527
528 void wxComboBox::SetSelection(int n)
529 {
530 m_choice->SetSelection( n );
531
532 if ( m_text != NULL )
533 {
534 m_text->SetValue( GetString( n ) );
535 }
536 }
537
538 int wxComboBox::FindString(const wxString& s) const
539 {
540 return m_choice->FindString( s );
541 }
542
543 wxString wxComboBox::GetString(int n) const
544 {
545 return m_choice->GetString( n );
546 }
547
548 wxString wxComboBox::GetStringSelection() const
549 {
550 int sel = GetSelection ();
551 if (sel > -1)
552 return wxString(this->GetString (sel));
553 else
554 return wxEmptyString;
555 }
556
557 bool wxComboBox::SetStringSelection(const wxString& sel)
558 {
559 int s = FindString (sel);
560 if (s > -1)
561 {
562 SetSelection (s);
563 return TRUE;
564 }
565 else
566 return FALSE;
567 }
568
569 void wxComboBox::SetString(int n, const wxString& s)
570 {
571 m_choice->SetString( n , s ) ;
572 }
573
574
575 wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
576 {
577 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
578 event.SetInt(GetSelection());
579 event.SetEventObject(this);
580 event.SetString(GetStringSelection());
581 ProcessCommand(event);
582 return noErr ;
583 }
584