]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
adapting for dual implementation native and composited
[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;
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;
62 }
63
64protected:
8095ef23 65 void OnChar( wxKeyEvent& event )
12f31626 66 {
eb22f2a6 67 if ( event.GetKeyCode() == WXK_RETURN )
8095ef23
SC
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 ) {
e40298d5 103 parent = parent->GetParent() ;
8095ef23
SC
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 }
22a70443 121
12f31626
SC
122 event.Skip();
123 }
124
125private:
126 wxComboBox *m_cb;
127
128 DECLARE_EVENT_TABLE()
129};
130
131BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
8095ef23 132 EVT_CHAR( wxComboBoxText::OnChar)
12f31626
SC
133END_EVENT_TABLE()
134
135class wxComboBoxChoice : public wxChoice
136{
137public:
138 wxComboBoxChoice(wxComboBox *cb, int style)
327788ac 139 : wxChoice( cb , 1 )
12f31626
SC
140 {
141 m_cb = cb;
142 }
143
144protected:
145 void OnChoice( wxCommandEvent& e )
146 {
147 wxString s = e.GetString();
148
149 m_cb->DelegateChoice( s );
8095ef23
SC
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);
12f31626 155 }
6097743a
SC
156 virtual wxSize DoGetBestSize() const
157 {
158 wxSize sz = wxChoice::DoGetBestSize() ;
159 sz.x = POPUPWIDTH ;
160 return sz ;
161 }
12f31626
SC
162
163private:
164 wxComboBox *m_cb;
165
166 DECLARE_EVENT_TABLE()
167};
168
169BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
170 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
171END_EVENT_TABLE()
172
12f31626
SC
173wxComboBox::~wxComboBox()
174{
e94e2e95
MB
175 // delete client objects
176 FreeData();
177
178 // delete the controls now, don't leave them alive even though they would
12f31626
SC
179 // still be eventually deleted by our parent - but it will be too late, the
180 // user code expects them to be gone now
f5bb2251
GD
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 }
12f31626
SC
189}
190
191
192// ----------------------------------------------------------------------------
193// geometry
194// ----------------------------------------------------------------------------
195
196wxSize wxComboBox::DoGetBestSize() const
197{
198 wxSize size = m_choice->GetBestSize();
199
d99937cd 200 if ( m_text != NULL )
12f31626
SC
201 {
202 wxSize sizeText = m_text->GetBestSize();
203
204 size.x = POPUPWIDTH + sizeText.x + MARGIN;
205 }
206
207 return size;
208}
209
210void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
211 height = POPUPHEIGHT;
212
213 wxControl::DoMoveWindow(x, y, width, height);
214
d99937cd 215 if ( m_text == NULL )
12f31626 216 {
facd6764
SC
217 // we might not be fully constructed yet, therefore watch out...
218 if ( m_choice )
219 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
220 }
221 else
222 {
7854746e 223 wxCoord wText = width - POPUPWIDTH - MARGIN;
327788ac
SC
224 m_text->SetSize(0, 0, wText, height);
225 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
12f31626
SC
226 }
227}
228
229
230
231// ----------------------------------------------------------------------------
232// operations forwarded to the subcontrols
233// ----------------------------------------------------------------------------
234
235bool wxComboBox::Enable(bool enable)
236{
237 if ( !wxControl::Enable(enable) )
238 return FALSE;
239
12f31626
SC
240 return TRUE;
241}
242
243bool wxComboBox::Show(bool show)
244{
245 if ( !wxControl::Show(show) )
246 return FALSE;
247
12f31626
SC
248 return TRUE;
249}
250
d99937cd
GD
251void wxComboBox::SetFocus()
252{
253 if ( m_text != NULL) {
254 m_text->SetFocus();
255 }
256}
465605e0 257
12f31626 258
d99937cd
GD
259void wxComboBox::DelegateTextChanged( const wxString& value )
260{
8095ef23 261 SetStringSelection( value );
12f31626
SC
262}
263
264
265void wxComboBox::DelegateChoice( const wxString& value )
266{
267 SetStringSelection( value );
268}
269
270
584ad2a3
MB
271bool 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
e9576ca5
SC
287bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
288 const wxString& value,
289 const wxPoint& pos,
290 const wxSize& size,
465605e0
RR
291 int n, const wxString choices[],
292 long style,
e9576ca5
SC
293 const wxValidator& validator,
294 const wxString& name)
295{
327788ac 296 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
297 wxDefaultValidator, name) )
298 {
299 return FALSE;
300 }
301
327788ac 302 m_choice = new wxComboBoxChoice(this, style );
6097743a 303 m_choice->SetSizeHints( wxSize( POPUPWIDTH , POPUPHEIGHT ) ) ;
12f31626
SC
304 wxSize csize = size;
305 if ( style & wxCB_READONLY )
306 {
d99937cd 307 m_text = NULL;
12f31626
SC
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);
327788ac 318
12f31626
SC
319 for ( int i = 0 ; i < n ; i++ )
320 {
465605e0 321 m_choice->DoAppend( choices[ i ] );
12f31626
SC
322 }
323
d3b5db4b
RD
324 SetBestSize(csize); // Needed because it is a wxControlWithItems
325
12f31626 326 return TRUE;
e9576ca5
SC
327}
328
329wxString wxComboBox::GetValue() const
330{
12f31626
SC
331 wxString result;
332
d99937cd 333 if ( m_text == NULL )
12f31626
SC
334 {
335 result = m_choice->GetString( m_choice->GetSelection() );
336 }
337 else
338 {
339 result = m_text->GetValue();
340 }
341
342 return result;
e9576ca5
SC
343}
344
345void wxComboBox::SetValue(const wxString& value)
346{
d60de516
SC
347 int s = FindString (value);
348 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
349 {
e40298d5 350 m_choice->Append(value) ;
d60de516 351 }
e40298d5 352 SetStringSelection( value ) ;
e9576ca5
SC
353}
354
355// Clipboard operations
356void wxComboBox::Copy()
357{
d99937cd 358 if ( m_text != NULL )
12f31626
SC
359 {
360 m_text->Copy();
361 }
e9576ca5
SC
362}
363
364void wxComboBox::Cut()
365{
d99937cd 366 if ( m_text != NULL )
12f31626
SC
367 {
368 m_text->Cut();
369 }
e9576ca5
SC
370}
371
372void wxComboBox::Paste()
373{
d99937cd 374 if ( m_text != NULL )
12f31626
SC
375 {
376 m_text->Paste();
377 }
e9576ca5
SC
378}
379
380void wxComboBox::SetEditable(bool editable)
381{
d99937cd 382 if ( ( m_text == NULL ) && editable )
12f31626
SC
383 {
384 m_text = new wxComboBoxText( this );
385 }
d99937cd 386 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
387 {
388 delete m_text;
d99937cd 389 m_text = NULL;
12f31626
SC
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 );
e9576ca5
SC
399}
400
401void wxComboBox::SetInsertionPoint(long pos)
402{
403 // TODO
404}
405
406void wxComboBox::SetInsertionPointEnd()
407{
408 // TODO
409}
410
411long wxComboBox::GetInsertionPoint() const
412{
413 // TODO
414 return 0;
415}
416
417long wxComboBox::GetLastPosition() const
418{
419 // TODO
420 return 0;
421}
422
423void wxComboBox::Replace(long from, long to, const wxString& value)
424{
425 // TODO
426}
427
428void wxComboBox::Remove(long from, long to)
429{
430 // TODO
431}
432
433void wxComboBox::SetSelection(long from, long to)
434{
435 // TODO
436}
437
e71a0aa9 438int wxComboBox::DoAppend(const wxString& item)
e9576ca5 439{
e71a0aa9
SC
440 return m_choice->DoAppend( item ) ;
441}
442
443int wxComboBox::DoInsert(const wxString& item, int pos)
444{
445 return m_choice->DoInsert( item , pos ) ;
446}
447
448void wxComboBox::DoSetItemClientData(int n, void* clientData)
449{
f148f2ba 450 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
451}
452
453void* wxComboBox::DoGetItemClientData(int n) const
454{
f148f2ba 455 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 456}
22a70443 457
e71a0aa9
SC
458void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
459{
f148f2ba 460 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
461}
462
463wxClientData* wxComboBox::DoGetItemClientObject(int n) const
464{
f148f2ba
MB
465 return m_choice->DoGetItemClientObject( n ) ;
466}
467
468void 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 }
e9576ca5
SC
478}
479
480void wxComboBox::Delete(int n)
481{
f148f2ba
MB
482 // force client object deletion
483 if( HasClientObjectData() )
484 SetClientObject( n, NULL );
12f31626 485 m_choice->Delete( n );
e9576ca5
SC
486}
487
488void wxComboBox::Clear()
489{
f148f2ba 490 FreeData();
12f31626 491 m_choice->Clear();
e9576ca5
SC
492}
493
494int wxComboBox::GetSelection() const
495{
12f31626 496 return m_choice->GetSelection();
e9576ca5
SC
497}
498
499void wxComboBox::SetSelection(int n)
500{
12f31626
SC
501 m_choice->SetSelection( n );
502
d99937cd 503 if ( m_text != NULL )
12f31626
SC
504 {
505 m_text->SetValue( GetString( n ) );
506 }
e9576ca5
SC
507}
508
509int wxComboBox::FindString(const wxString& s) const
510{
12f31626 511 return m_choice->FindString( s );
e9576ca5
SC
512}
513
514wxString wxComboBox::GetString(int n) const
515{
12f31626 516 return m_choice->GetString( n );
e9576ca5
SC
517}
518
519wxString wxComboBox::GetStringSelection() const
520{
519cb848
SC
521 int sel = GetSelection ();
522 if (sel > -1)
523 return wxString(this->GetString (sel));
524 else
427ff662 525 return wxEmptyString;
e9576ca5
SC
526}
527
528bool wxComboBox::SetStringSelection(const wxString& sel)
529{
519cb848
SC
530 int s = FindString (sel);
531 if (s > -1)
532 {
533 SetSelection (s);
534 return TRUE;
535 }
536 else
537 return FALSE;
538}
539
e71a0aa9
SC
540void wxComboBox::SetString(int n, const wxString& s)
541{
542 m_choice->SetString( n , s ) ;
543}
544
545
4c37f124 546wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848
SC
547{
548 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 549 event.SetInt(GetSelection());
519cb848 550 event.SetEventObject(this);
0a67a93b 551 event.SetString(GetStringSelection());
519cb848 552 ProcessCommand(event);
4c37f124 553 return noErr ;
e9576ca5 554}
519cb848 555