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