]> git.saurik.com Git - wxWidgets.git/blame - src/mac/combobox.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[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{
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 {
327788ac 207 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
208 }
209 else
210 {
7854746e 211 wxCoord wText = width - POPUPWIDTH - MARGIN;
327788ac
SC
212 m_text->SetSize(0, 0, wText, height);
213 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
12f31626
SC
214 }
215}
216
217
218
219// ----------------------------------------------------------------------------
220// operations forwarded to the subcontrols
221// ----------------------------------------------------------------------------
222
223bool wxComboBox::Enable(bool enable)
224{
225 if ( !wxControl::Enable(enable) )
226 return FALSE;
227
12f31626
SC
228 return TRUE;
229}
230
231bool wxComboBox::Show(bool show)
232{
233 if ( !wxControl::Show(show) )
234 return FALSE;
235
12f31626
SC
236 return TRUE;
237}
238
d99937cd
GD
239void wxComboBox::SetFocus()
240{
241 if ( m_text != NULL) {
242 m_text->SetFocus();
243 }
244}
465605e0 245
12f31626 246
d99937cd
GD
247void wxComboBox::DelegateTextChanged( const wxString& value )
248{
8095ef23 249 SetStringSelection( value );
12f31626
SC
250}
251
252
253void wxComboBox::DelegateChoice( const wxString& value )
254{
255 SetStringSelection( value );
256}
257
258
584ad2a3
MB
259bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
260 const wxString& value,
261 const wxPoint& pos,
262 const wxSize& size,
263 const wxArrayString& choices,
264 long style,
265 const wxValidator& validator,
266 const wxString& name)
267{
268 wxCArrayString chs( choices );
269
270 return Create( parent, id, value, pos, size, chs.GetCount(),
271 chs.GetStrings(), style, validator, name );
272}
273
274
e9576ca5
SC
275bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
276 const wxString& value,
277 const wxPoint& pos,
278 const wxSize& size,
465605e0
RR
279 int n, const wxString choices[],
280 long style,
e9576ca5
SC
281 const wxValidator& validator,
282 const wxString& name)
283{
327788ac 284 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
285 wxDefaultValidator, name) )
286 {
287 return FALSE;
288 }
289
327788ac 290 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
291
292 wxSize csize = size;
293 if ( style & wxCB_READONLY )
294 {
d99937cd 295 m_text = NULL;
12f31626
SC
296 }
297 else
298 {
299 m_text = new wxComboBoxText(this);
300 if ( size.y == -1 ) {
301 csize.y = m_text->GetSize().y ;
302 }
303 }
304
305 DoSetSize(pos.x, pos.y, csize.x, csize.y);
327788ac 306
12f31626
SC
307 for ( int i = 0 ; i < n ; i++ )
308 {
465605e0 309 m_choice->DoAppend( choices[ i ] );
12f31626
SC
310 }
311
12f31626 312 return TRUE;
e9576ca5
SC
313}
314
315wxString wxComboBox::GetValue() const
316{
12f31626
SC
317 wxString result;
318
d99937cd 319 if ( m_text == NULL )
12f31626
SC
320 {
321 result = m_choice->GetString( m_choice->GetSelection() );
322 }
323 else
324 {
325 result = m_text->GetValue();
326 }
327
328 return result;
e9576ca5
SC
329}
330
331void wxComboBox::SetValue(const wxString& value)
332{
d60de516
SC
333 int s = FindString (value);
334 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
335 {
e40298d5 336 m_choice->Append(value) ;
d60de516 337 }
e40298d5 338 SetStringSelection( value ) ;
e9576ca5
SC
339}
340
341// Clipboard operations
342void wxComboBox::Copy()
343{
d99937cd 344 if ( m_text != NULL )
12f31626
SC
345 {
346 m_text->Copy();
347 }
e9576ca5
SC
348}
349
350void wxComboBox::Cut()
351{
d99937cd 352 if ( m_text != NULL )
12f31626
SC
353 {
354 m_text->Cut();
355 }
e9576ca5
SC
356}
357
358void wxComboBox::Paste()
359{
d99937cd 360 if ( m_text != NULL )
12f31626
SC
361 {
362 m_text->Paste();
363 }
e9576ca5
SC
364}
365
366void wxComboBox::SetEditable(bool editable)
367{
d99937cd 368 if ( ( m_text == NULL ) && editable )
12f31626
SC
369 {
370 m_text = new wxComboBoxText( this );
371 }
d99937cd 372 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
373 {
374 delete m_text;
d99937cd 375 m_text = NULL;
12f31626
SC
376 }
377
378 int currentX, currentY;
379 GetPosition( &currentX, &currentY );
380
381 int currentW, currentH;
382 GetSize( &currentW, &currentH );
383
384 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
385}
386
387void wxComboBox::SetInsertionPoint(long pos)
388{
389 // TODO
390}
391
392void wxComboBox::SetInsertionPointEnd()
393{
394 // TODO
395}
396
397long wxComboBox::GetInsertionPoint() const
398{
399 // TODO
400 return 0;
401}
402
403long wxComboBox::GetLastPosition() const
404{
405 // TODO
406 return 0;
407}
408
409void wxComboBox::Replace(long from, long to, const wxString& value)
410{
411 // TODO
412}
413
414void wxComboBox::Remove(long from, long to)
415{
416 // TODO
417}
418
419void wxComboBox::SetSelection(long from, long to)
420{
421 // TODO
422}
423
e71a0aa9 424int wxComboBox::DoAppend(const wxString& item)
e9576ca5 425{
e71a0aa9
SC
426 return m_choice->DoAppend( item ) ;
427}
428
429int wxComboBox::DoInsert(const wxString& item, int pos)
430{
431 return m_choice->DoInsert( item , pos ) ;
432}
433
434void wxComboBox::DoSetItemClientData(int n, void* clientData)
435{
f148f2ba 436 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
437}
438
439void* wxComboBox::DoGetItemClientData(int n) const
440{
f148f2ba 441 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 442}
22a70443 443
e71a0aa9
SC
444void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
445{
f148f2ba 446 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
447}
448
449wxClientData* wxComboBox::DoGetItemClientObject(int n) const
450{
f148f2ba
MB
451 return m_choice->DoGetItemClientObject( n ) ;
452}
453
454void wxComboBox::FreeData()
455{
456 if ( HasClientObjectData() )
457 {
458 size_t count = GetCount();
459 for ( size_t n = 0; n < count; n++ )
460 {
461 SetClientObject( n, NULL );
462 }
463 }
e9576ca5
SC
464}
465
466void wxComboBox::Delete(int n)
467{
f148f2ba
MB
468 // force client object deletion
469 if( HasClientObjectData() )
470 SetClientObject( n, NULL );
12f31626 471 m_choice->Delete( n );
e9576ca5
SC
472}
473
474void wxComboBox::Clear()
475{
f148f2ba 476 FreeData();
12f31626 477 m_choice->Clear();
e9576ca5
SC
478}
479
480int wxComboBox::GetSelection() const
481{
12f31626 482 return m_choice->GetSelection();
e9576ca5
SC
483}
484
485void wxComboBox::SetSelection(int n)
486{
12f31626
SC
487 m_choice->SetSelection( n );
488
d99937cd 489 if ( m_text != NULL )
12f31626
SC
490 {
491 m_text->SetValue( GetString( n ) );
492 }
e9576ca5
SC
493}
494
495int wxComboBox::FindString(const wxString& s) const
496{
12f31626 497 return m_choice->FindString( s );
e9576ca5
SC
498}
499
500wxString wxComboBox::GetString(int n) const
501{
12f31626 502 return m_choice->GetString( n );
e9576ca5
SC
503}
504
505wxString wxComboBox::GetStringSelection() const
506{
519cb848
SC
507 int sel = GetSelection ();
508 if (sel > -1)
509 return wxString(this->GetString (sel));
510 else
427ff662 511 return wxEmptyString;
e9576ca5
SC
512}
513
514bool wxComboBox::SetStringSelection(const wxString& sel)
515{
519cb848
SC
516 int s = FindString (sel);
517 if (s > -1)
518 {
519 SetSelection (s);
520 return TRUE;
521 }
522 else
523 return FALSE;
524}
525
e71a0aa9
SC
526void wxComboBox::SetString(int n, const wxString& s)
527{
528 m_choice->SetString( n , s ) ;
529}
530
531
532void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
519cb848
SC
533{
534 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 535 event.SetInt(GetSelection());
519cb848 536 event.SetEventObject(this);
0a67a93b 537 event.SetString(GetStringSelection());
519cb848 538 ProcessCommand(event);
e9576ca5 539}
519cb848 540