]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
attempt to bring event system in synch with MSW
[wxWidgets.git] / src / mac / carbon / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/combobox.h"
17 #include "wx/menu.h"
18 #include "wx/mac/uma.h"
19
20 #if !USE_SHARED_LIBRARY
21 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
22 #endif
23
24 // composite combobox implementation by Dan "Bud" Keith bud@otsys.com
25
26
27 static int nextPopUpMenuId = 1000 ;
28 MenuHandle NewUniqueMenu()
29 {
30 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
31 nextPopUpMenuId++ ;
32 return handle ;
33 }
34
35
36 // ----------------------------------------------------------------------------
37 // constants
38 // ----------------------------------------------------------------------------
39
40 // the margin between the text control and the choice
41 static const wxCoord MARGIN = 2;
42 static const int POPUPWIDTH = 18;
43 static const int POPUPHEIGHT = 23;
44
45
46 // ----------------------------------------------------------------------------
47 // wxComboBoxText: text control forwards events to combobox
48 // ----------------------------------------------------------------------------
49
50 class wxComboBoxText : public wxTextCtrl
51 {
52 public:
53 wxComboBoxText( wxComboBox * cb )
54 : wxTextCtrl( cb , 1 )
55 {
56 m_cb = cb;
57 }
58
59 protected:
60 void OnChar( wxKeyEvent& event )
61 {
62 if ( event.KeyCode() == WXK_RETURN )
63 {
64 wxString value = GetValue();
65
66 if ( m_cb->GetCount() == 0 )
67 {
68 // make Enter generate "selected" event if there is only one item
69 // in the combobox - without it, it's impossible to select it at
70 // all!
71 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
72 event.SetInt( 0 );
73 event.SetString( value );
74 event.SetEventObject( m_cb );
75 m_cb->GetEventHandler()->ProcessEvent( event );
76 }
77 else
78 {
79 // add the item to the list if it's not there yet
80 if ( m_cb->FindString(value) == wxNOT_FOUND )
81 {
82 m_cb->Append(value);
83 m_cb->SetStringSelection(value);
84
85 // and generate the selected event for it
86 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
87 event.SetInt( m_cb->GetCount() - 1 );
88 event.SetString( value );
89 event.SetEventObject( m_cb );
90 m_cb->GetEventHandler()->ProcessEvent( event );
91 }
92
93 // This will invoke the dialog default action, such
94 // as the clicking the default button.
95
96 wxWindow *parent = GetParent();
97 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
98 parent = parent->GetParent() ;
99 }
100 if ( parent && parent->GetDefaultItem() )
101 {
102 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
103 wxButton);
104 if ( def && def->IsEnabled() )
105 {
106 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
107 event.SetEventObject(def);
108 def->Command(event);
109 return ;
110 }
111 }
112
113 return;
114 }
115 }
116
117 event.Skip();
118 }
119
120 private:
121 wxComboBox *m_cb;
122
123 DECLARE_EVENT_TABLE()
124 };
125
126 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
127 EVT_CHAR( wxComboBoxText::OnChar)
128 END_EVENT_TABLE()
129
130 class wxComboBoxChoice : public wxChoice
131 {
132 public:
133 wxComboBoxChoice(wxComboBox *cb, int style)
134 : wxChoice( cb , 1 )
135 {
136 m_cb = cb;
137 }
138
139 protected:
140 void OnChoice( wxCommandEvent& e )
141 {
142 wxString s = e.GetString();
143
144 m_cb->DelegateChoice( s );
145 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
146 event2.SetInt(m_cb->GetSelection());
147 event2.SetEventObject(m_cb);
148 event2.SetString(m_cb->GetStringSelection());
149 m_cb->ProcessCommand(event2);
150 }
151
152 private:
153 wxComboBox *m_cb;
154
155 DECLARE_EVENT_TABLE()
156 };
157
158 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
159 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
160 END_EVENT_TABLE()
161
162 wxComboBox::~wxComboBox()
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;
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 int n, const wxString choices[],
260 long style,
261 const wxValidator& validator,
262 const wxString& name)
263 {
264
265 Rect bounds ;
266 Str255 title ;
267
268 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
269 wxDefaultValidator, name) )
270 {
271 return FALSE;
272 }
273
274 m_choice = new wxComboBoxChoice(this, style );
275
276 wxSize csize = size;
277 if ( style & wxCB_READONLY )
278 {
279 m_text = NULL;
280 }
281 else
282 {
283 m_text = new wxComboBoxText(this);
284 if ( size.y == -1 ) {
285 csize.y = m_text->GetSize().y ;
286 }
287 }
288
289 DoSetSize(pos.x, pos.y, csize.x, csize.y);
290
291 for ( int i = 0 ; i < n ; i++ )
292 {
293 m_choice->DoAppend( choices[ i ] );
294 }
295
296 return TRUE;
297 }
298
299 wxString wxComboBox::GetValue() const
300 {
301 wxString result;
302
303 if ( m_text == NULL )
304 {
305 result = m_choice->GetString( m_choice->GetSelection() );
306 }
307 else
308 {
309 result = m_text->GetValue();
310 }
311
312 return result;
313 }
314
315 void wxComboBox::SetValue(const wxString& value)
316 {
317 SetStringSelection( value ) ;
318 }
319
320 // Clipboard operations
321 void wxComboBox::Copy()
322 {
323 if ( m_text != NULL )
324 {
325 m_text->Copy();
326 }
327 }
328
329 void wxComboBox::Cut()
330 {
331 if ( m_text != NULL )
332 {
333 m_text->Cut();
334 }
335 }
336
337 void wxComboBox::Paste()
338 {
339 if ( m_text != NULL )
340 {
341 m_text->Paste();
342 }
343 }
344
345 void wxComboBox::SetEditable(bool editable)
346 {
347 if ( ( m_text == NULL ) && editable )
348 {
349 m_text = new wxComboBoxText( this );
350 }
351 else if ( ( m_text != NULL ) && !editable )
352 {
353 delete m_text;
354 m_text = NULL;
355 }
356
357 int currentX, currentY;
358 GetPosition( &currentX, &currentY );
359
360 int currentW, currentH;
361 GetSize( &currentW, &currentH );
362
363 DoMoveWindow( currentX, currentY, currentW, currentH );
364 }
365
366 void wxComboBox::SetInsertionPoint(long pos)
367 {
368 // TODO
369 }
370
371 void wxComboBox::SetInsertionPointEnd()
372 {
373 // TODO
374 }
375
376 long wxComboBox::GetInsertionPoint() const
377 {
378 // TODO
379 return 0;
380 }
381
382 long wxComboBox::GetLastPosition() const
383 {
384 // TODO
385 return 0;
386 }
387
388 void wxComboBox::Replace(long from, long to, const wxString& value)
389 {
390 // TODO
391 }
392
393 void wxComboBox::Remove(long from, long to)
394 {
395 // TODO
396 }
397
398 void wxComboBox::SetSelection(long from, long to)
399 {
400 // TODO
401 }
402
403 void wxComboBox::Append(const wxString& item)
404 {
405 // I am not sure what other ports do,
406 // but wxMac chokes on empty entries.
407
408 if (!item.IsEmpty())
409 m_choice->DoAppend( item );
410 }
411
412 void wxComboBox::Delete(int n)
413 {
414 m_choice->Delete( n );
415 }
416
417 void wxComboBox::Clear()
418 {
419 m_choice->Clear();
420 }
421
422 int wxComboBox::GetSelection() const
423 {
424 return m_choice->GetSelection();
425 }
426
427 void wxComboBox::SetSelection(int n)
428 {
429 m_choice->SetSelection( n );
430
431 if ( m_text != NULL )
432 {
433 m_text->SetValue( GetString( n ) );
434 }
435 }
436
437 int wxComboBox::FindString(const wxString& s) const
438 {
439 return m_choice->FindString( s );
440 }
441
442 wxString wxComboBox::GetString(int n) const
443 {
444 return m_choice->GetString( n );
445 }
446
447 wxString wxComboBox::GetStringSelection() const
448 {
449 int sel = GetSelection ();
450 if (sel > -1)
451 return wxString(this->GetString (sel));
452 else
453 return wxString("");
454 }
455
456 bool wxComboBox::SetStringSelection(const wxString& sel)
457 {
458 int s = FindString (sel);
459 if (s > -1)
460 {
461 SetSelection (s);
462 return TRUE;
463 }
464 else
465 return FALSE;
466 }
467
468 void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
469 {
470 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
471 event.SetInt(GetSelection());
472 event.SetEventObject(this);
473 event.SetString(GetStringSelection());
474 ProcessCommand(event);
475 }
476