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