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