]> git.saurik.com Git - wxWidgets.git/blob - src/mac/combobox.cpp
corrected scrolling problems for controls, switched to separate wxSpinCtrl implementa...
[wxWidgets.git] / src / mac / 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 OnTextChange( wxCommandEvent& event )
61 {
62 wxString s = GetValue();
63
64 if (!s.IsEmpty())
65 m_cb->DelegateTextChanged( s );
66
67 event.Skip();
68 }
69
70 private:
71 wxComboBox *m_cb;
72
73 DECLARE_EVENT_TABLE()
74 };
75
76 BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
77 EVT_TEXT(-1, wxComboBoxText::OnTextChange)
78 END_EVENT_TABLE()
79
80 class wxComboBoxChoice : public wxChoice
81 {
82 public:
83 wxComboBoxChoice(wxComboBox *cb, int style)
84 : wxChoice( cb , 1 )
85 {
86 m_cb = cb;
87 }
88
89 protected:
90 void OnChoice( wxCommandEvent& e )
91 {
92 wxString s = e.GetString();
93
94 m_cb->DelegateChoice( s );
95 }
96
97 private:
98 wxComboBox *m_cb;
99
100 DECLARE_EVENT_TABLE()
101 };
102
103 BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
104 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
105 END_EVENT_TABLE()
106
107 wxComboBox::~wxComboBox()
108 {
109 // delete the controls now, don't leave them alive even though they would
110 // still be eventually deleted by our parent - but it will be too late, the
111 // user code expects them to be gone now
112 if (m_text != NULL) {
113 delete m_text;
114 m_text = NULL;
115 }
116 if (m_choice != NULL) {
117 delete m_choice;
118 m_choice = NULL;
119 }
120 }
121
122
123 // ----------------------------------------------------------------------------
124 // geometry
125 // ----------------------------------------------------------------------------
126
127 wxSize wxComboBox::DoGetBestSize() const
128 {
129 wxSize size = m_choice->GetBestSize();
130
131 if ( m_text != 0 )
132 {
133 wxSize sizeText = m_text->GetBestSize();
134
135 size.x = POPUPWIDTH + sizeText.x + MARGIN;
136 }
137
138 return size;
139 }
140
141 void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
142 height = POPUPHEIGHT;
143
144 wxControl::DoMoveWindow(x, y, width, height);
145
146 if ( m_text == 0 )
147 {
148 m_choice->SetSize(0, 0 , width, -1);
149 }
150 else
151 {
152 wxCoord wText = width - POPUPWIDTH;
153 m_text->SetSize(0, 0, wText, height);
154 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
155 }
156 }
157
158
159
160 // ----------------------------------------------------------------------------
161 // operations forwarded to the subcontrols
162 // ----------------------------------------------------------------------------
163
164 bool wxComboBox::Enable(bool enable)
165 {
166 if ( !wxControl::Enable(enable) )
167 return FALSE;
168
169 return TRUE;
170 }
171
172 bool wxComboBox::Show(bool show)
173 {
174 if ( !wxControl::Show(show) )
175 return FALSE;
176
177 return TRUE;
178 }
179
180 void wxComboBox::SetFocus()
181 {
182 m_text->SetFocus();
183 }
184
185
186 void wxComboBox::DelegateTextChanged( const wxString& value ) {
187 }
188
189
190 void wxComboBox::DelegateChoice( const wxString& value )
191 {
192 SetStringSelection( value );
193 }
194
195
196 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
197 const wxString& value,
198 const wxPoint& pos,
199 const wxSize& size,
200 int n, const wxString choices[],
201 long style,
202 const wxValidator& validator,
203 const wxString& name)
204 {
205
206 Rect bounds ;
207 Str255 title ;
208
209 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
210 wxDefaultValidator, name) )
211 {
212 return FALSE;
213 }
214
215 m_choice = new wxComboBoxChoice(this, style );
216
217 wxSize csize = size;
218 if ( style & wxCB_READONLY )
219 {
220 m_text = 0;
221 }
222 else
223 {
224 m_text = new wxComboBoxText(this);
225 if ( size.y == -1 ) {
226 csize.y = m_text->GetSize().y ;
227 }
228 }
229
230 DoSetSize(pos.x, pos.y, csize.x, csize.y);
231
232 for ( int i = 0 ; i < n ; i++ )
233 {
234 m_choice->DoAppend( choices[ i ] );
235 }
236
237 return TRUE;
238 }
239
240 wxString wxComboBox::GetValue() const
241 {
242 wxString result;
243
244 if ( m_text == 0 )
245 {
246 result = m_choice->GetString( m_choice->GetSelection() );
247 }
248 else
249 {
250 result = m_text->GetValue();
251 }
252
253 return result;
254 }
255
256 void wxComboBox::SetValue(const wxString& value)
257 {
258 SetStringSelection( value ) ;
259 }
260
261 // Clipboard operations
262 void wxComboBox::Copy()
263 {
264 if ( m_text != 0 )
265 {
266 m_text->Copy();
267 }
268 }
269
270 void wxComboBox::Cut()
271 {
272 if ( m_text != 0 )
273 {
274 m_text->Cut();
275 }
276 }
277
278 void wxComboBox::Paste()
279 {
280 if ( m_text != 0 )
281 {
282 m_text->Paste();
283 }
284 }
285
286 void wxComboBox::SetEditable(bool editable)
287 {
288 if ( ( m_text == 0 ) && editable )
289 {
290 m_text = new wxComboBoxText( this );
291 }
292 else if ( ( m_text != 0 ) && !editable )
293 {
294 delete m_text;
295 m_text = 0;
296 }
297
298 int currentX, currentY;
299 GetPosition( &currentX, &currentY );
300
301 int currentW, currentH;
302 GetSize( &currentW, &currentH );
303
304 DoMoveWindow( currentX, currentY, currentW, currentH );
305 }
306
307 void wxComboBox::SetInsertionPoint(long pos)
308 {
309 // TODO
310 }
311
312 void wxComboBox::SetInsertionPointEnd()
313 {
314 // TODO
315 }
316
317 long wxComboBox::GetInsertionPoint() const
318 {
319 // TODO
320 return 0;
321 }
322
323 long wxComboBox::GetLastPosition() const
324 {
325 // TODO
326 return 0;
327 }
328
329 void wxComboBox::Replace(long from, long to, const wxString& value)
330 {
331 // TODO
332 }
333
334 void wxComboBox::Remove(long from, long to)
335 {
336 // TODO
337 }
338
339 void wxComboBox::SetSelection(long from, long to)
340 {
341 // TODO
342 }
343
344 void wxComboBox::Append(const wxString& item)
345 {
346 // I am not sure what other ports do,
347 // but wxMac chokes on empty entries.
348
349 if (!item.IsEmpty())
350 m_choice->DoAppend( item );
351 }
352
353 void wxComboBox::Delete(int n)
354 {
355 m_choice->Delete( n );
356 }
357
358 void wxComboBox::Clear()
359 {
360 m_choice->Clear();
361 }
362
363 int wxComboBox::GetSelection() const
364 {
365 return m_choice->GetSelection();
366 }
367
368 void wxComboBox::SetSelection(int n)
369 {
370 m_choice->SetSelection( n );
371
372 if ( m_text != 0 )
373 {
374 m_text->SetValue( GetString( n ) );
375 }
376 }
377
378 int wxComboBox::FindString(const wxString& s) const
379 {
380 return m_choice->FindString( s );
381 }
382
383 wxString wxComboBox::GetString(int n) const
384 {
385 return m_choice->GetString( n );
386 }
387
388 wxString wxComboBox::GetStringSelection() const
389 {
390 int sel = GetSelection ();
391 if (sel > -1)
392 return wxString(this->GetString (sel));
393 else
394 return wxString("");
395 }
396
397 bool wxComboBox::SetStringSelection(const wxString& sel)
398 {
399 int s = FindString (sel);
400 if (s > -1)
401 {
402 SetSelection (s);
403 return TRUE;
404 }
405 else
406 return FALSE;
407 }
408
409 void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
410 {
411 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
412 event.SetInt(GetSelection());
413 event.SetEventObject(this);
414 event.SetString(GetStringSelection());
415 ProcessCommand(event);
416 }
417