]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
make sure we are getting the gui mutex before shutting down
[wxWidgets.git] / src / mac / carbon / combobox.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose: wxComboBox class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.h"
14#endif
15
16#include "wx/combobox.h"
b5a8b32f 17#include "wx/button.h"
03e11df5 18#include "wx/menu.h"
519cb848 19#include "wx/mac/uma.h"
e9576ca5 20
2f1ae414 21#if !USE_SHARED_LIBRARY
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
2f1ae414 23#endif
e9576ca5 24
12f31626
SC
25// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26
519cb848 27
0e03d1fa 28static int nextPopUpMenuId = 1000 ;
892e461e
SC
29MenuHandle NewUniqueMenu()
30{
31 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
32 nextPopUpMenuId++ ;
33 return handle ;
34}
519cb848 35
12f31626
SC
36
37// ----------------------------------------------------------------------------
38// constants
39// ----------------------------------------------------------------------------
40
41// the margin between the text control and the choice
42static const wxCoord MARGIN = 2;
6097743a
SC
43#if TARGET_API_MAC_OSX
44static const int POPUPWIDTH = 24;
45#else
12f31626 46static const int POPUPWIDTH = 18;
6097743a 47#endif
12f31626
SC
48static const int POPUPHEIGHT = 23;
49
50
51// ----------------------------------------------------------------------------
52// wxComboBoxText: text control forwards events to combobox
53// ----------------------------------------------------------------------------
54
55class wxComboBoxText : public wxTextCtrl
56{
57public:
58 wxComboBoxText( wxComboBox * cb )
327788ac 59 : wxTextCtrl( cb , 1 )
12f31626
SC
60 {
61 m_cb = cb;
d0770e4a
RD
62
63 // remove the default minsize, the combobox will have one instead
64 SetSizeHints(-1,-1);
12f31626
SC
65 }
66
67protected:
8095ef23 68 void OnChar( wxKeyEvent& event )
12f31626 69 {
eb22f2a6 70 if ( event.GetKeyCode() == WXK_RETURN )
8095ef23
SC
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 ) {
e40298d5 106 parent = parent->GetParent() ;
8095ef23
SC
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 }
22a70443 124
12f31626
SC
125 event.Skip();
126 }
127
128private:
129 wxComboBox *m_cb;
130
131 DECLARE_EVENT_TABLE()
132};
133
134BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
8095ef23 135 EVT_CHAR( wxComboBoxText::OnChar)
12f31626
SC
136END_EVENT_TABLE()
137
138class wxComboBoxChoice : public wxChoice
139{
140public:
141 wxComboBoxChoice(wxComboBox *cb, int style)
327788ac 142 : wxChoice( cb , 1 )
12f31626
SC
143 {
144 m_cb = cb;
d0770e4a
RD
145
146 // remove the default minsize, the combobox will have one instead
147 SetSizeHints(-1,-1);
12f31626
SC
148 }
149
150protected:
151 void OnChoice( wxCommandEvent& e )
152 {
153 wxString s = e.GetString();
154
155 m_cb->DelegateChoice( s );
8095ef23
SC
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);
12f31626 161 }
6097743a
SC
162 virtual wxSize DoGetBestSize() const
163 {
164 wxSize sz = wxChoice::DoGetBestSize() ;
d0770e4a
RD
165 if (! m_cb->HasFlag(wxCB_READONLY) )
166 sz.x = POPUPWIDTH;
6097743a
SC
167 return sz ;
168 }
12f31626
SC
169
170private:
171 wxComboBox *m_cb;
172
173 DECLARE_EVENT_TABLE()
174};
175
176BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
177 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
178END_EVENT_TABLE()
179
12f31626
SC
180wxComboBox::~wxComboBox()
181{
e94e2e95
MB
182 // delete client objects
183 FreeData();
184
185 // delete the controls now, don't leave them alive even though they would
12f31626
SC
186 // still be eventually deleted by our parent - but it will be too late, the
187 // user code expects them to be gone now
f5bb2251
GD
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 }
12f31626
SC
196}
197
198
199// ----------------------------------------------------------------------------
200// geometry
201// ----------------------------------------------------------------------------
202
203wxSize wxComboBox::DoGetBestSize() const
204{
205 wxSize size = m_choice->GetBestSize();
206
d99937cd 207 if ( m_text != NULL )
12f31626
SC
208 {
209 wxSize sizeText = m_text->GetBestSize();
210
211 size.x = POPUPWIDTH + sizeText.x + MARGIN;
212 }
213
214 return size;
215}
216
217void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
218 height = POPUPHEIGHT;
219
220 wxControl::DoMoveWindow(x, y, width, height);
221
d99937cd 222 if ( m_text == NULL )
12f31626 223 {
facd6764
SC
224 // we might not be fully constructed yet, therefore watch out...
225 if ( m_choice )
226 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
227 }
228 else
229 {
7854746e 230 wxCoord wText = width - POPUPWIDTH - MARGIN;
327788ac
SC
231 m_text->SetSize(0, 0, wText, height);
232 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
12f31626
SC
233 }
234}
235
236
237
238// ----------------------------------------------------------------------------
239// operations forwarded to the subcontrols
240// ----------------------------------------------------------------------------
241
242bool wxComboBox::Enable(bool enable)
243{
244 if ( !wxControl::Enable(enable) )
245 return FALSE;
246
12f31626
SC
247 return TRUE;
248}
249
250bool wxComboBox::Show(bool show)
251{
252 if ( !wxControl::Show(show) )
253 return FALSE;
254
12f31626
SC
255 return TRUE;
256}
257
d99937cd
GD
258void wxComboBox::SetFocus()
259{
260 if ( m_text != NULL) {
261 m_text->SetFocus();
262 }
263}
465605e0 264
12f31626 265
d99937cd
GD
266void wxComboBox::DelegateTextChanged( const wxString& value )
267{
8095ef23 268 SetStringSelection( value );
12f31626
SC
269}
270
271
272void wxComboBox::DelegateChoice( const wxString& value )
273{
274 SetStringSelection( value );
275}
276
277
584ad2a3
MB
278bool 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
e9576ca5
SC
294bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
295 const wxString& value,
296 const wxPoint& pos,
297 const wxSize& size,
465605e0
RR
298 int n, const wxString choices[],
299 long style,
e9576ca5
SC
300 const wxValidator& validator,
301 const wxString& name)
302{
327788ac 303 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
304 wxDefaultValidator, name) )
305 {
306 return FALSE;
307 }
308
327788ac 309 m_choice = new wxComboBoxChoice(this, style );
6097743a 310 m_choice->SetSizeHints( wxSize( POPUPWIDTH , POPUPHEIGHT ) ) ;
12f31626
SC
311 wxSize csize = size;
312 if ( style & wxCB_READONLY )
313 {
d99937cd 314 m_text = NULL;
12f31626
SC
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);
327788ac 325
12f31626
SC
326 for ( int i = 0 ; i < n ; i++ )
327 {
465605e0 328 m_choice->DoAppend( choices[ i ] );
12f31626
SC
329 }
330
d3b5db4b
RD
331 SetBestSize(csize); // Needed because it is a wxControlWithItems
332
12f31626 333 return TRUE;
e9576ca5
SC
334}
335
336wxString wxComboBox::GetValue() const
337{
12f31626
SC
338 wxString result;
339
d99937cd 340 if ( m_text == NULL )
12f31626
SC
341 {
342 result = m_choice->GetString( m_choice->GetSelection() );
343 }
344 else
345 {
346 result = m_text->GetValue();
347 }
348
349 return result;
e9576ca5
SC
350}
351
46cc7c4e
SC
352int wxComboBox::GetCount() const
353{
354 return m_choice->GetCount() ;
355}
356
e9576ca5
SC
357void wxComboBox::SetValue(const wxString& value)
358{
d60de516
SC
359 int s = FindString (value);
360 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
361 {
e40298d5 362 m_choice->Append(value) ;
d60de516 363 }
e40298d5 364 SetStringSelection( value ) ;
e9576ca5
SC
365}
366
367// Clipboard operations
368void wxComboBox::Copy()
369{
d99937cd 370 if ( m_text != NULL )
12f31626
SC
371 {
372 m_text->Copy();
373 }
e9576ca5
SC
374}
375
376void wxComboBox::Cut()
377{
d99937cd 378 if ( m_text != NULL )
12f31626
SC
379 {
380 m_text->Cut();
381 }
e9576ca5
SC
382}
383
384void wxComboBox::Paste()
385{
d99937cd 386 if ( m_text != NULL )
12f31626
SC
387 {
388 m_text->Paste();
389 }
e9576ca5
SC
390}
391
392void wxComboBox::SetEditable(bool editable)
393{
d99937cd 394 if ( ( m_text == NULL ) && editable )
12f31626
SC
395 {
396 m_text = new wxComboBoxText( this );
397 }
d99937cd 398 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
399 {
400 delete m_text;
d99937cd 401 m_text = NULL;
12f31626
SC
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 );
e9576ca5
SC
411}
412
413void wxComboBox::SetInsertionPoint(long pos)
414{
415 // TODO
416}
417
418void wxComboBox::SetInsertionPointEnd()
419{
420 // TODO
421}
422
423long wxComboBox::GetInsertionPoint() const
424{
425 // TODO
426 return 0;
427}
428
429long wxComboBox::GetLastPosition() const
430{
431 // TODO
432 return 0;
433}
434
435void wxComboBox::Replace(long from, long to, const wxString& value)
436{
437 // TODO
438}
439
440void wxComboBox::Remove(long from, long to)
441{
442 // TODO
443}
444
445void wxComboBox::SetSelection(long from, long to)
446{
447 // TODO
448}
449
e71a0aa9 450int wxComboBox::DoAppend(const wxString& item)
e9576ca5 451{
e71a0aa9
SC
452 return m_choice->DoAppend( item ) ;
453}
454
455int wxComboBox::DoInsert(const wxString& item, int pos)
456{
457 return m_choice->DoInsert( item , pos ) ;
458}
459
460void wxComboBox::DoSetItemClientData(int n, void* clientData)
461{
f148f2ba 462 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
463}
464
465void* wxComboBox::DoGetItemClientData(int n) const
466{
f148f2ba 467 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 468}
22a70443 469
e71a0aa9
SC
470void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
471{
f148f2ba 472 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
473}
474
475wxClientData* wxComboBox::DoGetItemClientObject(int n) const
476{
f148f2ba
MB
477 return m_choice->DoGetItemClientObject( n ) ;
478}
479
480void 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 }
e9576ca5
SC
490}
491
492void wxComboBox::Delete(int n)
493{
f148f2ba
MB
494 // force client object deletion
495 if( HasClientObjectData() )
496 SetClientObject( n, NULL );
12f31626 497 m_choice->Delete( n );
e9576ca5
SC
498}
499
500void wxComboBox::Clear()
501{
f148f2ba 502 FreeData();
12f31626 503 m_choice->Clear();
e9576ca5
SC
504}
505
506int wxComboBox::GetSelection() const
507{
12f31626 508 return m_choice->GetSelection();
e9576ca5
SC
509}
510
511void wxComboBox::SetSelection(int n)
512{
12f31626
SC
513 m_choice->SetSelection( n );
514
d99937cd 515 if ( m_text != NULL )
12f31626
SC
516 {
517 m_text->SetValue( GetString( n ) );
518 }
e9576ca5
SC
519}
520
521int wxComboBox::FindString(const wxString& s) const
522{
12f31626 523 return m_choice->FindString( s );
e9576ca5
SC
524}
525
526wxString wxComboBox::GetString(int n) const
527{
12f31626 528 return m_choice->GetString( n );
e9576ca5
SC
529}
530
531wxString wxComboBox::GetStringSelection() const
532{
519cb848
SC
533 int sel = GetSelection ();
534 if (sel > -1)
535 return wxString(this->GetString (sel));
536 else
427ff662 537 return wxEmptyString;
e9576ca5
SC
538}
539
540bool wxComboBox::SetStringSelection(const wxString& sel)
541{
519cb848
SC
542 int s = FindString (sel);
543 if (s > -1)
544 {
545 SetSelection (s);
546 return TRUE;
547 }
548 else
549 return FALSE;
550}
551
e71a0aa9
SC
552void wxComboBox::SetString(int n, const wxString& s)
553{
554 m_choice->SetString( n , s ) ;
555}
556
557
4c37f124 558wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848
SC
559{
560 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 561 event.SetInt(GetSelection());
519cb848 562 event.SetEventObject(this);
0a67a93b 563 event.SetString(GetStringSelection());
519cb848 564 ProcessCommand(event);
4c37f124 565 return noErr ;
e9576ca5 566}
519cb848 567