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