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