]> git.saurik.com Git - wxWidgets.git/blob - src/mac/combobox.cpp
added new implementation for editable and brought selection behaviour in synch
[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 != NULL )
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 == NULL )
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 if ( m_text != NULL) {
183 m_text->SetFocus();
184 }
185 }
186
187
188 void wxComboBox::DelegateTextChanged( const wxString& value )
189 {
190 }
191
192
193 void wxComboBox::DelegateChoice( const wxString& value )
194 {
195 SetStringSelection( value );
196 }
197
198
199 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
200 const wxString& value,
201 const wxPoint& pos,
202 const wxSize& size,
203 int n, const wxString choices[],
204 long style,
205 const wxValidator& validator,
206 const wxString& name)
207 {
208
209 Rect bounds ;
210 Str255 title ;
211
212 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
213 wxDefaultValidator, name) )
214 {
215 return FALSE;
216 }
217
218 m_choice = new wxComboBoxChoice(this, style );
219
220 wxSize csize = size;
221 if ( style & wxCB_READONLY )
222 {
223 m_text = NULL;
224 }
225 else
226 {
227 m_text = new wxComboBoxText(this);
228 if ( size.y == -1 ) {
229 csize.y = m_text->GetSize().y ;
230 }
231 }
232
233 DoSetSize(pos.x, pos.y, csize.x, csize.y);
234
235 for ( int i = 0 ; i < n ; i++ )
236 {
237 m_choice->DoAppend( choices[ i ] );
238 }
239
240 return TRUE;
241 }
242
243 wxString wxComboBox::GetValue() const
244 {
245 wxString result;
246
247 if ( m_text == NULL )
248 {
249 result = m_choice->GetString( m_choice->GetSelection() );
250 }
251 else
252 {
253 result = m_text->GetValue();
254 }
255
256 return result;
257 }
258
259 void wxComboBox::SetValue(const wxString& value)
260 {
261 SetStringSelection( value ) ;
262 }
263
264 // Clipboard operations
265 void wxComboBox::Copy()
266 {
267 if ( m_text != NULL )
268 {
269 m_text->Copy();
270 }
271 }
272
273 void wxComboBox::Cut()
274 {
275 if ( m_text != NULL )
276 {
277 m_text->Cut();
278 }
279 }
280
281 void wxComboBox::Paste()
282 {
283 if ( m_text != NULL )
284 {
285 m_text->Paste();
286 }
287 }
288
289 void wxComboBox::SetEditable(bool editable)
290 {
291 if ( ( m_text == NULL ) && editable )
292 {
293 m_text = new wxComboBoxText( this );
294 }
295 else if ( ( m_text != NULL ) && !editable )
296 {
297 delete m_text;
298 m_text = NULL;
299 }
300
301 int currentX, currentY;
302 GetPosition( &currentX, &currentY );
303
304 int currentW, currentH;
305 GetSize( &currentW, &currentH );
306
307 DoMoveWindow( currentX, currentY, currentW, currentH );
308 }
309
310 void wxComboBox::SetInsertionPoint(long pos)
311 {
312 // TODO
313 }
314
315 void wxComboBox::SetInsertionPointEnd()
316 {
317 // TODO
318 }
319
320 long wxComboBox::GetInsertionPoint() const
321 {
322 // TODO
323 return 0;
324 }
325
326 long wxComboBox::GetLastPosition() const
327 {
328 // TODO
329 return 0;
330 }
331
332 void wxComboBox::Replace(long from, long to, const wxString& value)
333 {
334 // TODO
335 }
336
337 void wxComboBox::Remove(long from, long to)
338 {
339 // TODO
340 }
341
342 void wxComboBox::SetSelection(long from, long to)
343 {
344 // TODO
345 }
346
347 void wxComboBox::Append(const wxString& item)
348 {
349 // I am not sure what other ports do,
350 // but wxMac chokes on empty entries.
351
352 if (!item.IsEmpty())
353 m_choice->DoAppend( item );
354 }
355
356 void wxComboBox::Delete(int n)
357 {
358 m_choice->Delete( n );
359 }
360
361 void wxComboBox::Clear()
362 {
363 m_choice->Clear();
364 }
365
366 int wxComboBox::GetSelection() const
367 {
368 return m_choice->GetSelection();
369 }
370
371 void wxComboBox::SetSelection(int n)
372 {
373 m_choice->SetSelection( n );
374
375 if ( m_text != NULL )
376 {
377 m_text->SetValue( GetString( n ) );
378 }
379 }
380
381 int wxComboBox::FindString(const wxString& s) const
382 {
383 return m_choice->FindString( s );
384 }
385
386 wxString wxComboBox::GetString(int n) const
387 {
388 return m_choice->GetString( n );
389 }
390
391 wxString wxComboBox::GetStringSelection() const
392 {
393 int sel = GetSelection ();
394 if (sel > -1)
395 return wxString(this->GetString (sel));
396 else
397 return wxString("");
398 }
399
400 bool wxComboBox::SetStringSelection(const wxString& sel)
401 {
402 int s = FindString (sel);
403 if (s > -1)
404 {
405 SetSelection (s);
406 return TRUE;
407 }
408 else
409 return FALSE;
410 }
411
412 void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
413 {
414 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
415 event.SetInt(GetSelection());
416 event.SetEventObject(this);
417 event.SetString(GetStringSelection());
418 ProcessCommand(event);
419 }
420