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