]> git.saurik.com Git - wxWidgets.git/blame - src/mac/combobox.cpp
GTK_TOOLBAR_BOTH_HORIZ is GTK 2 only (it would have been nice if the patch mentioned...
[wxWidgets.git] / src / mac / 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{
f5bb2251 165 // delete the controls now, don't leave them alive even though they would
12f31626
SC
166 // still be eventually deleted by our parent - but it will be too late, the
167 // user code expects them to be gone now
f5bb2251
GD
168 if (m_text != NULL) {
169 delete m_text;
170 m_text = NULL;
171 }
172 if (m_choice != NULL) {
173 delete m_choice;
174 m_choice = NULL;
175 }
12f31626
SC
176}
177
178
179// ----------------------------------------------------------------------------
180// geometry
181// ----------------------------------------------------------------------------
182
183wxSize wxComboBox::DoGetBestSize() const
184{
185 wxSize size = m_choice->GetBestSize();
186
d99937cd 187 if ( m_text != NULL )
12f31626
SC
188 {
189 wxSize sizeText = m_text->GetBestSize();
190
191 size.x = POPUPWIDTH + sizeText.x + MARGIN;
192 }
193
194 return size;
195}
196
197void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
198 height = POPUPHEIGHT;
199
200 wxControl::DoMoveWindow(x, y, width, height);
201
d99937cd 202 if ( m_text == NULL )
12f31626 203 {
327788ac 204 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
205 }
206 else
207 {
7854746e 208 wxCoord wText = width - POPUPWIDTH - MARGIN;
327788ac
SC
209 m_text->SetSize(0, 0, wText, height);
210 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
12f31626
SC
211 }
212}
213
214
215
216// ----------------------------------------------------------------------------
217// operations forwarded to the subcontrols
218// ----------------------------------------------------------------------------
219
220bool wxComboBox::Enable(bool enable)
221{
222 if ( !wxControl::Enable(enable) )
223 return FALSE;
224
12f31626
SC
225 return TRUE;
226}
227
228bool wxComboBox::Show(bool show)
229{
230 if ( !wxControl::Show(show) )
231 return FALSE;
232
12f31626
SC
233 return TRUE;
234}
235
d99937cd
GD
236void wxComboBox::SetFocus()
237{
238 if ( m_text != NULL) {
239 m_text->SetFocus();
240 }
241}
465605e0 242
12f31626 243
d99937cd
GD
244void wxComboBox::DelegateTextChanged( const wxString& value )
245{
8095ef23 246 SetStringSelection( value );
12f31626
SC
247}
248
249
250void wxComboBox::DelegateChoice( const wxString& value )
251{
252 SetStringSelection( value );
253}
254
255
e9576ca5
SC
256bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
257 const wxString& value,
258 const wxPoint& pos,
259 const wxSize& size,
465605e0
RR
260 int n, const wxString choices[],
261 long style,
e9576ca5
SC
262 const wxValidator& validator,
263 const wxString& name)
264{
327788ac 265 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
266 wxDefaultValidator, name) )
267 {
268 return FALSE;
269 }
270
327788ac 271 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
272
273 wxSize csize = size;
274 if ( style & wxCB_READONLY )
275 {
d99937cd 276 m_text = NULL;
12f31626
SC
277 }
278 else
279 {
280 m_text = new wxComboBoxText(this);
281 if ( size.y == -1 ) {
282 csize.y = m_text->GetSize().y ;
283 }
284 }
285
286 DoSetSize(pos.x, pos.y, csize.x, csize.y);
327788ac 287
12f31626
SC
288 for ( int i = 0 ; i < n ; i++ )
289 {
465605e0 290 m_choice->DoAppend( choices[ i ] );
12f31626
SC
291 }
292
12f31626 293 return TRUE;
e9576ca5
SC
294}
295
296wxString wxComboBox::GetValue() const
297{
12f31626
SC
298 wxString result;
299
d99937cd 300 if ( m_text == NULL )
12f31626
SC
301 {
302 result = m_choice->GetString( m_choice->GetSelection() );
303 }
304 else
305 {
306 result = m_text->GetValue();
307 }
308
309 return result;
e9576ca5
SC
310}
311
312void wxComboBox::SetValue(const wxString& value)
313{
d60de516
SC
314 int s = FindString (value);
315 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
316 {
e40298d5 317 m_choice->Append(value) ;
d60de516 318 }
e40298d5 319 SetStringSelection( value ) ;
e9576ca5
SC
320}
321
322// Clipboard operations
323void wxComboBox::Copy()
324{
d99937cd 325 if ( m_text != NULL )
12f31626
SC
326 {
327 m_text->Copy();
328 }
e9576ca5
SC
329}
330
331void wxComboBox::Cut()
332{
d99937cd 333 if ( m_text != NULL )
12f31626
SC
334 {
335 m_text->Cut();
336 }
e9576ca5
SC
337}
338
339void wxComboBox::Paste()
340{
d99937cd 341 if ( m_text != NULL )
12f31626
SC
342 {
343 m_text->Paste();
344 }
e9576ca5
SC
345}
346
347void wxComboBox::SetEditable(bool editable)
348{
d99937cd 349 if ( ( m_text == NULL ) && editable )
12f31626
SC
350 {
351 m_text = new wxComboBoxText( this );
352 }
d99937cd 353 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
354 {
355 delete m_text;
d99937cd 356 m_text = NULL;
12f31626
SC
357 }
358
359 int currentX, currentY;
360 GetPosition( &currentX, &currentY );
361
362 int currentW, currentH;
363 GetSize( &currentW, &currentH );
364
365 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
366}
367
368void wxComboBox::SetInsertionPoint(long pos)
369{
370 // TODO
371}
372
373void wxComboBox::SetInsertionPointEnd()
374{
375 // TODO
376}
377
378long wxComboBox::GetInsertionPoint() const
379{
380 // TODO
381 return 0;
382}
383
384long wxComboBox::GetLastPosition() const
385{
386 // TODO
387 return 0;
388}
389
390void wxComboBox::Replace(long from, long to, const wxString& value)
391{
392 // TODO
393}
394
395void wxComboBox::Remove(long from, long to)
396{
397 // TODO
398}
399
400void wxComboBox::SetSelection(long from, long to)
401{
402 // TODO
403}
404
e71a0aa9 405int wxComboBox::DoAppend(const wxString& item)
e9576ca5 406{
e71a0aa9
SC
407 return m_choice->DoAppend( item ) ;
408}
409
410int wxComboBox::DoInsert(const wxString& item, int pos)
411{
412 return m_choice->DoInsert( item , pos ) ;
413}
414
415void wxComboBox::DoSetItemClientData(int n, void* clientData)
416{
417 return m_choice->SetClientData( n , clientData ) ;
418}
419
420void* wxComboBox::DoGetItemClientData(int n) const
421{
422 return m_choice->GetClientData( n ) ;
423}
22a70443 424
e71a0aa9
SC
425void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
426{
427 return m_choice->SetClientObject( n , clientData ) ;
428}
429
430wxClientData* wxComboBox::DoGetItemClientObject(int n) const
431{
432 return m_choice->GetClientObject( n ) ;
e9576ca5
SC
433}
434
435void wxComboBox::Delete(int n)
436{
12f31626 437 m_choice->Delete( n );
e9576ca5
SC
438}
439
440void wxComboBox::Clear()
441{
12f31626 442 m_choice->Clear();
e9576ca5
SC
443}
444
445int wxComboBox::GetSelection() const
446{
12f31626 447 return m_choice->GetSelection();
e9576ca5
SC
448}
449
450void wxComboBox::SetSelection(int n)
451{
12f31626
SC
452 m_choice->SetSelection( n );
453
d99937cd 454 if ( m_text != NULL )
12f31626
SC
455 {
456 m_text->SetValue( GetString( n ) );
457 }
e9576ca5
SC
458}
459
460int wxComboBox::FindString(const wxString& s) const
461{
12f31626 462 return m_choice->FindString( s );
e9576ca5
SC
463}
464
465wxString wxComboBox::GetString(int n) const
466{
12f31626 467 return m_choice->GetString( n );
e9576ca5
SC
468}
469
470wxString wxComboBox::GetStringSelection() const
471{
519cb848
SC
472 int sel = GetSelection ();
473 if (sel > -1)
474 return wxString(this->GetString (sel));
475 else
427ff662 476 return wxEmptyString;
e9576ca5
SC
477}
478
479bool wxComboBox::SetStringSelection(const wxString& sel)
480{
519cb848
SC
481 int s = FindString (sel);
482 if (s > -1)
483 {
484 SetSelection (s);
485 return TRUE;
486 }
487 else
488 return FALSE;
489}
490
e71a0aa9
SC
491void wxComboBox::SetString(int n, const wxString& s)
492{
493 m_choice->SetString( n , s ) ;
494}
495
496
497void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
519cb848
SC
498{
499 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 500 event.SetInt(GetSelection());
519cb848 501 event.SetEventObject(this);
0a67a93b 502 event.SetString(GetStringSelection());
519cb848 503 ProcessCommand(event);
e9576ca5 504}
519cb848 505