]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/combobox.cpp
centralized key handling
[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/button.h"
18 #include "wx/menu.h"
19 #include "wx/mac/uma.h"
20
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
23 #endif
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.KeyCode() == 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 wxWindow *parent = GetParent();
98 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
99 parent = parent->GetParent() ;
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 }
117
118 event.Skip();
119 }
120
121 private:
122 wxComboBox *m_cb;
123
124 DECLARE_EVENT_TABLE()
125 };
126
127 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
128 EVT_CHAR( wxComboBoxText::OnChar)
129 END_EVENT_TABLE()
130
131 class wxComboBoxChoice : public wxChoice
132 {
133 public:
134 wxComboBoxChoice(wxComboBox *cb, int style)
135 : wxChoice( cb , 1 )
136 {
137 m_cb = cb;
138 }
139
140 protected:
141 void OnChoice( wxCommandEvent& e )
142 {
143 wxString s = e.GetString();
144
145 m_cb->DelegateChoice( s );
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);
151 }
152
153 private:
154 wxComboBox *m_cb;
155
156 DECLARE_EVENT_TABLE()
157 };
158
159 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
160 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
161 END_EVENT_TABLE()
162
163 wxComboBox::~wxComboBox()
164 {
165 // delete the controls now, don't leave them alive even though they would
166 // still be eventually deleted by our parent - but it will be too late, the
167 // user code expects them to be gone now
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 }
176 }
177
178
179 // ----------------------------------------------------------------------------
180 // geometry
181 // ----------------------------------------------------------------------------
182
183 wxSize wxComboBox::DoGetBestSize() const
184 {
185 wxSize size = m_choice->GetBestSize();
186
187 if ( m_text != NULL )
188 {
189 wxSize sizeText = m_text->GetBestSize();
190
191 size.x = POPUPWIDTH + sizeText.x + MARGIN;
192 }
193
194 return size;
195 }
196
197 void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
198 height = POPUPHEIGHT;
199
200 wxControl::DoMoveWindow(x, y, width, height);
201
202 if ( m_text == NULL )
203 {
204 m_choice->SetSize(0, 0 , width, -1);
205 }
206 else
207 {
208 wxCoord wText = width - POPUPWIDTH;
209 m_text->SetSize(0, 0, wText, height);
210 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
211 }
212 }
213
214
215
216 // ----------------------------------------------------------------------------
217 // operations forwarded to the subcontrols
218 // ----------------------------------------------------------------------------
219
220 bool wxComboBox::Enable(bool enable)
221 {
222 if ( !wxControl::Enable(enable) )
223 return FALSE;
224
225 return TRUE;
226 }
227
228 bool wxComboBox::Show(bool show)
229 {
230 if ( !wxControl::Show(show) )
231 return FALSE;
232
233 return TRUE;
234 }
235
236 void wxComboBox::SetFocus()
237 {
238 if ( m_text != NULL) {
239 m_text->SetFocus();
240 }
241 }
242
243
244 void wxComboBox::DelegateTextChanged( const wxString& value )
245 {
246 SetStringSelection( value );
247 }
248
249
250 void wxComboBox::DelegateChoice( const wxString& value )
251 {
252 SetStringSelection( value );
253 }
254
255
256 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
257 const wxString& value,
258 const wxPoint& pos,
259 const wxSize& size,
260 int n, const wxString choices[],
261 long style,
262 const wxValidator& validator,
263 const wxString& name)
264 {
265
266 Rect bounds ;
267 Str255 title ;
268
269 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
270 wxDefaultValidator, name) )
271 {
272 return FALSE;
273 }
274
275 m_choice = new wxComboBoxChoice(this, style );
276
277 wxSize csize = size;
278 if ( style & wxCB_READONLY )
279 {
280 m_text = NULL;
281 }
282 else
283 {
284 m_text = new wxComboBoxText(this);
285 if ( size.y == -1 ) {
286 csize.y = m_text->GetSize().y ;
287 }
288 }
289
290 DoSetSize(pos.x, pos.y, csize.x, csize.y);
291
292 for ( int i = 0 ; i < n ; i++ )
293 {
294 m_choice->DoAppend( choices[ i ] );
295 }
296
297 return TRUE;
298 }
299
300 wxString wxComboBox::GetValue() const
301 {
302 wxString result;
303
304 if ( m_text == NULL )
305 {
306 result = m_choice->GetString( m_choice->GetSelection() );
307 }
308 else
309 {
310 result = m_text->GetValue();
311 }
312
313 return result;
314 }
315
316 void wxComboBox::SetValue(const wxString& value)
317 {
318 SetStringSelection( value ) ;
319 }
320
321 // Clipboard operations
322 void wxComboBox::Copy()
323 {
324 if ( m_text != NULL )
325 {
326 m_text->Copy();
327 }
328 }
329
330 void wxComboBox::Cut()
331 {
332 if ( m_text != NULL )
333 {
334 m_text->Cut();
335 }
336 }
337
338 void wxComboBox::Paste()
339 {
340 if ( m_text != NULL )
341 {
342 m_text->Paste();
343 }
344 }
345
346 void wxComboBox::SetEditable(bool editable)
347 {
348 if ( ( m_text == NULL ) && editable )
349 {
350 m_text = new wxComboBoxText( this );
351 }
352 else if ( ( m_text != NULL ) && !editable )
353 {
354 delete m_text;
355 m_text = NULL;
356 }
357
358 int currentX, currentY;
359 GetPosition( &currentX, &currentY );
360
361 int currentW, currentH;
362 GetSize( &currentW, &currentH );
363
364 DoMoveWindow( currentX, currentY, currentW, currentH );
365 }
366
367 void wxComboBox::SetInsertionPoint(long pos)
368 {
369 // TODO
370 }
371
372 void wxComboBox::SetInsertionPointEnd()
373 {
374 // TODO
375 }
376
377 long wxComboBox::GetInsertionPoint() const
378 {
379 // TODO
380 return 0;
381 }
382
383 long wxComboBox::GetLastPosition() const
384 {
385 // TODO
386 return 0;
387 }
388
389 void wxComboBox::Replace(long from, long to, const wxString& value)
390 {
391 // TODO
392 }
393
394 void wxComboBox::Remove(long from, long to)
395 {
396 // TODO
397 }
398
399 void wxComboBox::SetSelection(long from, long to)
400 {
401 // TODO
402 }
403
404 void wxComboBox::Append(const wxString& item)
405 {
406 // I am not sure what other ports do,
407 // but wxMac chokes on empty entries.
408
409 if (!item.IsEmpty())
410 m_choice->DoAppend( item );
411 }
412
413 void wxComboBox::Delete(int n)
414 {
415 m_choice->Delete( n );
416 }
417
418 void wxComboBox::Clear()
419 {
420 m_choice->Clear();
421 }
422
423 int wxComboBox::GetSelection() const
424 {
425 return m_choice->GetSelection();
426 }
427
428 void wxComboBox::SetSelection(int n)
429 {
430 m_choice->SetSelection( n );
431
432 if ( m_text != NULL )
433 {
434 m_text->SetValue( GetString( n ) );
435 }
436 }
437
438 int wxComboBox::FindString(const wxString& s) const
439 {
440 return m_choice->FindString( s );
441 }
442
443 wxString wxComboBox::GetString(int n) const
444 {
445 return m_choice->GetString( n );
446 }
447
448 wxString wxComboBox::GetStringSelection() const
449 {
450 int sel = GetSelection ();
451 if (sel > -1)
452 return wxString(this->GetString (sel));
453 else
454 return wxString("");
455 }
456
457 bool wxComboBox::SetStringSelection(const wxString& sel)
458 {
459 int s = FindString (sel);
460 if (s > -1)
461 {
462 SetSelection (s);
463 return TRUE;
464 }
465 else
466 return FALSE;
467 }
468
469 void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
470 {
471 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
472 event.SetInt(GetSelection());
473 event.SetEventObject(this);
474 event.SetString(GetStringSelection());
475 ProcessCommand(event);
476 }
477