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