]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/combobox.cpp
Back to previous call with -1 replacement.
[wxWidgets.git] / src / mac / classic / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/combobox.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/button.h"
18 #include "wx/menu.h"
19 #endif
20
21 #include "wx/mac/uma.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
24
25 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26
27
28 static int nextPopUpMenuId = 1000 ;
29 MenuHandle NewUniqueMenu()
30 {
31 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
32 nextPopUpMenuId++ ;
33 return handle ;
34 }
35
36
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 // the margin between the text control and the choice
42 static const wxCoord MARGIN = 2;
43 static const int POPUPWIDTH = 18;
44 static const int POPUPHEIGHT = 23;
45
46
47 // ----------------------------------------------------------------------------
48 // wxComboBoxText: text control forwards events to combobox
49 // ----------------------------------------------------------------------------
50
51 class wxComboBoxText : public wxTextCtrl
52 {
53 public:
54 wxComboBoxText( wxComboBox * cb )
55 : wxTextCtrl( cb , 1 )
56 {
57 m_cb = cb;
58 }
59
60 protected:
61 void OnChar( wxKeyEvent& event )
62 {
63 if ( event.GetKeyCode() == WXK_RETURN )
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 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
98 if ( tlw && tlw->GetDefaultItem() )
99 {
100 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
101 if ( def && def->IsEnabled() )
102 {
103 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
104 event.SetEventObject(def);
105 def->Command(event);
106 return ;
107 }
108 }
109
110 return;
111 }
112 }
113
114 event.Skip();
115 }
116
117 private:
118 wxComboBox *m_cb;
119
120 DECLARE_EVENT_TABLE()
121 };
122
123 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
124 EVT_CHAR( wxComboBoxText::OnChar)
125 END_EVENT_TABLE()
126
127 class wxComboBoxChoice : public wxChoice
128 {
129 public:
130 wxComboBoxChoice(wxComboBox *cb, int style)
131 : wxChoice( cb , 1 )
132 {
133 m_cb = cb;
134 }
135
136 protected:
137 void OnChoice( wxCommandEvent& e )
138 {
139 wxString s = e.GetString();
140
141 m_cb->DelegateChoice( s );
142 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
143 event2.SetInt(m_cb->GetSelection());
144 event2.SetEventObject(m_cb);
145 event2.SetString(m_cb->GetStringSelection());
146 m_cb->ProcessCommand(event2);
147 }
148
149 private:
150 wxComboBox *m_cb;
151
152 DECLARE_EVENT_TABLE()
153 };
154
155 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
156 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
157 END_EVENT_TABLE()
158
159 wxComboBox::~wxComboBox()
160 {
161 // delete client objects
162 FreeData();
163
164 // delete the controls now, don't leave them alive even though they would
165 // still be eventually deleted by our parent - but it will be too late, the
166 // user code expects them to be gone now
167 if (m_text != NULL) {
168 delete m_text;
169 m_text = NULL;
170 }
171 if (m_choice != NULL) {
172 delete m_choice;
173 m_choice = NULL;
174 }
175 }
176
177
178 // ----------------------------------------------------------------------------
179 // geometry
180 // ----------------------------------------------------------------------------
181
182 wxSize wxComboBox::DoGetBestSize() const
183 {
184 wxSize size = m_choice->GetBestSize();
185
186 if ( m_text != NULL )
187 {
188 wxSize sizeText = m_text->GetBestSize();
189
190 size.x = POPUPWIDTH + sizeText.x + MARGIN;
191 }
192
193 return size;
194 }
195
196 void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
197 height = POPUPHEIGHT;
198
199 wxControl::DoMoveWindow(x, y, width, height);
200
201 if ( m_text == NULL )
202 {
203 m_choice->SetSize(0, 0 , width, -1);
204 }
205 else
206 {
207 wxCoord wText = width - POPUPWIDTH - MARGIN;
208 m_text->SetSize(0, 0, wText, height);
209 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
210 }
211 }
212
213
214
215 // ----------------------------------------------------------------------------
216 // operations forwarded to the subcontrols
217 // ----------------------------------------------------------------------------
218
219 bool wxComboBox::Enable(bool enable)
220 {
221 if ( !wxControl::Enable(enable) )
222 return false;
223
224 return true;
225 }
226
227 bool wxComboBox::Show(bool show)
228 {
229 if ( !wxControl::Show(show) )
230 return false;
231
232 return true;
233 }
234
235 void wxComboBox::SetFocus()
236 {
237 if ( m_text != NULL) {
238 m_text->SetFocus();
239 }
240 }
241
242
243 void wxComboBox::DelegateTextChanged( const wxString& value )
244 {
245 SetStringSelection( value );
246 }
247
248
249 void wxComboBox::DelegateChoice( const wxString& value )
250 {
251 SetStringSelection( value );
252 }
253
254
255 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
256 const wxString& value,
257 const wxPoint& pos,
258 const wxSize& size,
259 const wxArrayString& choices,
260 long style,
261 const wxValidator& validator,
262 const wxString& name)
263 {
264 wxCArrayString chs( choices );
265
266 return Create( parent, id, value, pos, size, chs.GetCount(),
267 chs.GetStrings(), style, validator, name );
268 }
269
270
271 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
272 const wxString& value,
273 const wxPoint& pos,
274 const wxSize& size,
275 int n, const wxString choices[],
276 long style,
277 const wxValidator& validator,
278 const wxString& name)
279 {
280 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
281 wxDefaultValidator, name) )
282 {
283 return false;
284 }
285
286 m_choice = new wxComboBoxChoice(this, style );
287
288 wxSize csize = size;
289 if ( style & wxCB_READONLY )
290 {
291 m_text = NULL;
292 }
293 else
294 {
295 m_text = new wxComboBoxText(this);
296 if ( size.y == -1 ) {
297 csize.y = m_text->GetSize().y ;
298 }
299 }
300
301 DoSetSize(pos.x, pos.y, csize.x, csize.y);
302
303 for ( int i = 0 ; i < n ; i++ )
304 {
305 m_choice->DoAppend( choices[ i ] );
306 }
307
308 return true;
309 }
310
311 wxString wxComboBox::GetValue() const
312 {
313 wxString result;
314
315 if ( m_text == NULL )
316 {
317 result = m_choice->GetString( m_choice->GetSelection() );
318 }
319 else
320 {
321 result = m_text->GetValue();
322 }
323
324 return result;
325 }
326
327 void wxComboBox::SetValue(const wxString& value)
328 {
329 int s = FindString (value);
330 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
331 {
332 m_choice->Append(value) ;
333 }
334 SetStringSelection( value ) ;
335 }
336
337 // Clipboard operations
338 void wxComboBox::Copy()
339 {
340 if ( m_text != NULL )
341 {
342 m_text->Copy();
343 }
344 }
345
346 void wxComboBox::Cut()
347 {
348 if ( m_text != NULL )
349 {
350 m_text->Cut();
351 }
352 }
353
354 void wxComboBox::Paste()
355 {
356 if ( m_text != NULL )
357 {
358 m_text->Paste();
359 }
360 }
361
362 void wxComboBox::SetEditable(bool editable)
363 {
364 if ( ( m_text == NULL ) && editable )
365 {
366 m_text = new wxComboBoxText( this );
367 }
368 else if ( ( m_text != NULL ) && !editable )
369 {
370 delete m_text;
371 m_text = NULL;
372 }
373
374 int currentX, currentY;
375 GetPosition( &currentX, &currentY );
376
377 int currentW, currentH;
378 GetSize( &currentW, &currentH );
379
380 DoMoveWindow( currentX, currentY, currentW, currentH );
381 }
382
383 void wxComboBox::SetInsertionPoint(long pos)
384 {
385 // TODO
386 }
387
388 void wxComboBox::SetInsertionPointEnd()
389 {
390 // TODO
391 }
392
393 long wxComboBox::GetInsertionPoint() const
394 {
395 // TODO
396 return 0;
397 }
398
399 wxTextPos wxComboBox::GetLastPosition() const
400 {
401 // TODO
402 return 0;
403 }
404
405 void wxComboBox::Replace(long from, long to, const wxString& value)
406 {
407 // TODO
408 }
409
410 void wxComboBox::Remove(long from, long to)
411 {
412 // TODO
413 }
414
415 void wxComboBox::SetSelection(long from, long to)
416 {
417 // TODO
418 }
419
420 int wxComboBox::DoAppend(const wxString& item)
421 {
422 return m_choice->DoAppend( item ) ;
423 }
424
425 int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
426 {
427 return m_choice->DoInsert( item , pos ) ;
428 }
429
430 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
431 {
432 return m_choice->DoSetItemClientData( n , clientData ) ;
433 }
434
435 void* wxComboBox::DoGetItemClientData(unsigned int n) const
436 {
437 return m_choice->DoGetItemClientData( n ) ;
438 }
439
440 void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
441 {
442 return m_choice->DoSetItemClientObject(n , clientData);
443 }
444
445 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
446 {
447 return m_choice->DoGetItemClientObject( n ) ;
448 }
449
450 void wxComboBox::FreeData()
451 {
452 if ( HasClientObjectData() )
453 {
454 unsigned int count = GetCount();
455 for ( unsigned int n = 0; n < count; n++ )
456 {
457 SetClientObject( n, NULL );
458 }
459 }
460 }
461
462 void wxComboBox::Delete(unsigned int n)
463 {
464 // force client object deletion
465 if( HasClientObjectData() )
466 SetClientObject( n, NULL );
467 m_choice->Delete( n );
468 }
469
470 void wxComboBox::Clear()
471 {
472 FreeData();
473 m_choice->Clear();
474 }
475
476 int wxComboBox::GetSelection() const
477 {
478 return m_choice->GetSelection();
479 }
480
481 void wxComboBox::SetSelection(int n)
482 {
483 m_choice->SetSelection( n );
484
485 if ( m_text != NULL )
486 {
487 m_text->SetValue(GetString(n));
488 }
489 }
490
491 int wxComboBox::FindString(const wxString& s, bool bCase ) const
492 {
493 return m_choice->FindString( s , bCase );
494 }
495
496 wxString wxComboBox::GetString(unsigned int n) const
497 {
498 return m_choice->GetString( n );
499 }
500
501 wxString wxComboBox::GetStringSelection() const
502 {
503 int sel = GetSelection ();
504
505 if (sel != wxNOT_FOUND)
506 return wxString(this->GetString((unsigned int)sel));
507 else
508 return wxEmptyString;
509 }
510
511 void wxComboBox::SetString(unsigned int n, const wxString& s)
512 {
513 m_choice->SetString( n , s );
514 }
515
516 bool wxComboBox::IsEditable() const
517 {
518 return m_text != NULL && !HasFlag(wxCB_READONLY);
519 }
520
521 void wxComboBox::Undo()
522 {
523 if (m_text != NULL)
524 m_text->Undo();
525 }
526
527 void wxComboBox::Redo()
528 {
529 if (m_text != NULL)
530 m_text->Redo();
531 }
532
533 void wxComboBox::SelectAll()
534 {
535 if (m_text != NULL)
536 m_text->SelectAll();
537 }
538
539 bool wxComboBox::CanCopy() const
540 {
541 if (m_text != NULL)
542 return m_text->CanCopy();
543 else
544 return false;
545 }
546
547 bool wxComboBox::CanCut() const
548 {
549 if (m_text != NULL)
550 return m_text->CanCut();
551 else
552 return false;
553 }
554
555 bool wxComboBox::CanPaste() const
556 {
557 if (m_text != NULL)
558 return m_text->CanPaste();
559 else
560 return false;
561 }
562
563 bool wxComboBox::CanUndo() const
564 {
565 if (m_text != NULL)
566 return m_text->CanUndo();
567 else
568 return false;
569 }
570
571 bool wxComboBox::CanRedo() const
572 {
573 if (m_text != NULL)
574 return m_text->CanRedo();
575 else
576 return false;
577 }
578
579 void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
580 {
581 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
582 event.SetInt(GetSelection());
583 event.SetEventObject(this);
584 event.SetString(GetStringSelection());
585 ProcessCommand(event);
586 }