]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
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 | |
465605e0 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "combobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/combobox.h" | |
03e11df5 | 17 | #include "wx/menu.h" |
519cb848 | 18 | #include "wx/mac/uma.h" |
e9576ca5 | 19 | |
2f1ae414 | 20 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 21 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
2f1ae414 | 22 | #endif |
e9576ca5 | 23 | |
12f31626 SC |
24 | // composite combobox implementation by Dan "Bud" Keith bud@otsys.com |
25 | ||
519cb848 | 26 | |
0e03d1fa | 27 | static int nextPopUpMenuId = 1000 ; |
892e461e SC |
28 | MenuHandle NewUniqueMenu() |
29 | { | |
30 | MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; | |
31 | nextPopUpMenuId++ ; | |
32 | return handle ; | |
33 | } | |
519cb848 | 34 | |
12f31626 SC |
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->GetParent(), 1 ) | |
55 | { | |
56 | m_cb = cb; | |
57 | } | |
58 | ||
59 | protected: | |
60 | void OnTextChange( wxCommandEvent& event ) | |
61 | { | |
62 | wxString s = GetValue(); | |
22a70443 RR |
63 | |
64 | if (!s.IsEmpty()) | |
65 | m_cb->DelegateTextChanged( s ); | |
12f31626 SC |
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->GetParent(), 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 | ||
108 | ||
109 | ||
110 | wxComboBox::~wxComboBox() | |
111 | { | |
112 | // delete the controls now, don't leave them alive even though they woudl | |
113 | // still be eventually deleted by our parent - but it will be too late, the | |
114 | // user code expects them to be gone now | |
115 | delete m_text; | |
116 | delete m_choice; | |
117 | } | |
118 | ||
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // geometry | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | wxSize wxComboBox::DoGetBestSize() const | |
125 | { | |
126 | wxSize size = m_choice->GetBestSize(); | |
127 | ||
128 | if ( m_text != 0 ) | |
129 | { | |
130 | wxSize sizeText = m_text->GetBestSize(); | |
131 | ||
132 | size.x = POPUPWIDTH + sizeText.x + MARGIN; | |
133 | } | |
134 | ||
135 | return size; | |
136 | } | |
137 | ||
138 | void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { | |
139 | height = POPUPHEIGHT; | |
140 | ||
141 | wxControl::DoMoveWindow(x, y, width, height); | |
142 | ||
143 | if ( m_text == 0 ) | |
144 | { | |
145 | m_choice->SetSize(x, y, width, -1); | |
146 | } | |
147 | else | |
148 | { | |
149 | wxCoord wText = width - POPUPWIDTH; | |
150 | m_text->SetSize(x, y, wText, height); | |
151 | m_choice->SetSize(x + wText + MARGIN, y, POPUPWIDTH, -1); | |
152 | } | |
153 | } | |
154 | ||
155 | ||
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // operations forwarded to the subcontrols | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | bool wxComboBox::Enable(bool enable) | |
162 | { | |
163 | if ( !wxControl::Enable(enable) ) | |
164 | return FALSE; | |
165 | ||
166 | m_choice->Enable(enable); | |
167 | ||
168 | if ( m_text != 0 ) | |
169 | { | |
170 | m_text->Enable(enable); | |
171 | } | |
172 | ||
173 | return TRUE; | |
174 | } | |
175 | ||
176 | bool wxComboBox::Show(bool show) | |
177 | { | |
178 | if ( !wxControl::Show(show) ) | |
179 | return FALSE; | |
180 | ||
181 | // under GTK Show() is called the first time before we are fully | |
182 | // constructed | |
183 | if ( m_choice ) | |
184 | { | |
185 | m_choice->Show(show); | |
186 | if ( m_text != 0 ) | |
187 | { | |
188 | m_text->Show(show); | |
189 | } | |
190 | } | |
191 | ||
192 | return TRUE; | |
193 | } | |
194 | ||
465605e0 RR |
195 | void wxComboBox::SetFocus() |
196 | { | |
197 | m_text->SetFocus(); | |
198 | } | |
199 | ||
12f31626 SC |
200 | |
201 | void wxComboBox::DelegateTextChanged( const wxString& value ) { | |
202 | } | |
203 | ||
204 | ||
205 | void wxComboBox::DelegateChoice( const wxString& value ) | |
206 | { | |
207 | SetStringSelection( value ); | |
208 | } | |
209 | ||
210 | ||
e9576ca5 SC |
211 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
212 | const wxString& value, | |
213 | const wxPoint& pos, | |
214 | const wxSize& size, | |
465605e0 RR |
215 | int n, const wxString choices[], |
216 | long style, | |
e9576ca5 SC |
217 | const wxValidator& validator, |
218 | const wxString& name) | |
219 | { | |
12f31626 SC |
220 | |
221 | Rect bounds ; | |
222 | Str255 title ; | |
223 | ||
224 | if ( !wxControl::Create(parent, id, pos, size, style, | |
225 | wxDefaultValidator, name) ) | |
226 | { | |
227 | return FALSE; | |
228 | } | |
229 | ||
230 | m_choice = new wxComboBoxChoice(this, style); | |
231 | ||
232 | wxSize csize = size; | |
233 | if ( style & wxCB_READONLY ) | |
234 | { | |
235 | m_text = 0; | |
236 | } | |
237 | else | |
238 | { | |
239 | m_text = new wxComboBoxText(this); | |
240 | if ( size.y == -1 ) { | |
241 | csize.y = m_text->GetSize().y ; | |
242 | } | |
243 | } | |
244 | ||
245 | DoSetSize(pos.x, pos.y, csize.x, csize.y); | |
246 | for ( int i = 0 ; i < n ; i++ ) | |
247 | { | |
465605e0 | 248 | m_choice->DoAppend( choices[ i ] ); |
12f31626 SC |
249 | } |
250 | ||
251 | // have to disable this window to avoid interfering it with message | |
252 | // processing to the text and the button... but pretend it is enabled to | |
253 | // make IsEnabled() return TRUE | |
254 | wxControl::Enable(FALSE); // don't use non virtual Disable() here! | |
255 | m_isEnabled = TRUE; | |
256 | ||
257 | // we don't even need to show this window itself - and not doing it avoids | |
258 | // that it overwrites the text control | |
259 | wxControl::Show(FALSE); | |
260 | ||
261 | return TRUE; | |
e9576ca5 SC |
262 | } |
263 | ||
264 | wxString wxComboBox::GetValue() const | |
265 | { | |
12f31626 SC |
266 | wxString result; |
267 | ||
268 | if ( m_text == 0 ) | |
269 | { | |
270 | result = m_choice->GetString( m_choice->GetSelection() ); | |
271 | } | |
272 | else | |
273 | { | |
274 | result = m_text->GetValue(); | |
275 | } | |
276 | ||
277 | return result; | |
e9576ca5 SC |
278 | } |
279 | ||
280 | void wxComboBox::SetValue(const wxString& value) | |
281 | { | |
519cb848 | 282 | SetStringSelection( value ) ; |
e9576ca5 SC |
283 | } |
284 | ||
285 | // Clipboard operations | |
286 | void wxComboBox::Copy() | |
287 | { | |
12f31626 SC |
288 | if ( m_text != 0 ) |
289 | { | |
290 | m_text->Copy(); | |
291 | } | |
e9576ca5 SC |
292 | } |
293 | ||
294 | void wxComboBox::Cut() | |
295 | { | |
12f31626 SC |
296 | if ( m_text != 0 ) |
297 | { | |
298 | m_text->Cut(); | |
299 | } | |
e9576ca5 SC |
300 | } |
301 | ||
302 | void wxComboBox::Paste() | |
303 | { | |
12f31626 SC |
304 | if ( m_text != 0 ) |
305 | { | |
306 | m_text->Paste(); | |
307 | } | |
e9576ca5 SC |
308 | } |
309 | ||
310 | void wxComboBox::SetEditable(bool editable) | |
311 | { | |
12f31626 SC |
312 | if ( ( m_text == 0 ) && editable ) |
313 | { | |
314 | m_text = new wxComboBoxText( this ); | |
315 | } | |
316 | else if ( ( m_text != 0 ) && !editable ) | |
317 | { | |
318 | delete m_text; | |
319 | m_text = 0; | |
320 | } | |
321 | ||
322 | int currentX, currentY; | |
323 | GetPosition( ¤tX, ¤tY ); | |
324 | ||
325 | int currentW, currentH; | |
326 | GetSize( ¤tW, ¤tH ); | |
327 | ||
328 | DoMoveWindow( currentX, currentY, currentW, currentH ); | |
e9576ca5 SC |
329 | } |
330 | ||
331 | void wxComboBox::SetInsertionPoint(long pos) | |
332 | { | |
333 | // TODO | |
334 | } | |
335 | ||
336 | void wxComboBox::SetInsertionPointEnd() | |
337 | { | |
338 | // TODO | |
339 | } | |
340 | ||
341 | long wxComboBox::GetInsertionPoint() const | |
342 | { | |
343 | // TODO | |
344 | return 0; | |
345 | } | |
346 | ||
347 | long wxComboBox::GetLastPosition() const | |
348 | { | |
349 | // TODO | |
350 | return 0; | |
351 | } | |
352 | ||
353 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
354 | { | |
355 | // TODO | |
356 | } | |
357 | ||
358 | void wxComboBox::Remove(long from, long to) | |
359 | { | |
360 | // TODO | |
361 | } | |
362 | ||
363 | void wxComboBox::SetSelection(long from, long to) | |
364 | { | |
365 | // TODO | |
366 | } | |
367 | ||
368 | void wxComboBox::Append(const wxString& item) | |
369 | { | |
22a70443 RR |
370 | // I am not sure what other ports do, |
371 | // but wxMac chokes on empty entries. | |
372 | ||
373 | if (!item.IsEmpty()) | |
374 | m_choice->DoAppend( item ); | |
e9576ca5 SC |
375 | } |
376 | ||
377 | void wxComboBox::Delete(int n) | |
378 | { | |
12f31626 | 379 | m_choice->Delete( n ); |
e9576ca5 SC |
380 | } |
381 | ||
382 | void wxComboBox::Clear() | |
383 | { | |
12f31626 | 384 | m_choice->Clear(); |
e9576ca5 SC |
385 | } |
386 | ||
387 | int wxComboBox::GetSelection() const | |
388 | { | |
12f31626 | 389 | return m_choice->GetSelection(); |
e9576ca5 SC |
390 | } |
391 | ||
392 | void wxComboBox::SetSelection(int n) | |
393 | { | |
12f31626 SC |
394 | m_choice->SetSelection( n ); |
395 | ||
396 | if ( m_text != 0 ) | |
397 | { | |
398 | m_text->SetValue( GetString( n ) ); | |
399 | } | |
e9576ca5 SC |
400 | } |
401 | ||
402 | int wxComboBox::FindString(const wxString& s) const | |
403 | { | |
12f31626 | 404 | return m_choice->FindString( s ); |
e9576ca5 SC |
405 | } |
406 | ||
407 | wxString wxComboBox::GetString(int n) const | |
408 | { | |
12f31626 | 409 | return m_choice->GetString( n ); |
e9576ca5 SC |
410 | } |
411 | ||
412 | wxString wxComboBox::GetStringSelection() const | |
413 | { | |
519cb848 SC |
414 | int sel = GetSelection (); |
415 | if (sel > -1) | |
416 | return wxString(this->GetString (sel)); | |
417 | else | |
418 | return wxString(""); | |
e9576ca5 SC |
419 | } |
420 | ||
421 | bool wxComboBox::SetStringSelection(const wxString& sel) | |
422 | { | |
519cb848 SC |
423 | int s = FindString (sel); |
424 | if (s > -1) | |
425 | { | |
426 | SetSelection (s); | |
427 | return TRUE; | |
428 | } | |
429 | else | |
430 | return FALSE; | |
431 | } | |
432 | ||
76a5e5d2 | 433 | void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
519cb848 SC |
434 | { |
435 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
465605e0 | 436 | event.SetInt(GetSelection()); |
519cb848 | 437 | event.SetEventObject(this); |
0a67a93b | 438 | event.SetString(GetStringSelection()); |
519cb848 | 439 | ProcessCommand(event); |
e9576ca5 | 440 | } |
519cb848 | 441 |