]>
Commit | Line | Data |
---|---|---|
1e6feb95 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: univ/combobox.cpp | |
3 | // Purpose: wxComboControl and wxComboBox implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 15.12.00 | |
7 | // RCS-ID: $Id$ | |
442b35b5 | 8 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) |
1e6feb95 VZ |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /* | |
13 | TODO: | |
14 | ||
15 | +1. typing in the text should select the string in listbox | |
16 | +2. scrollbars in listbox are unusable | |
17 | +3. the initially selected item is not selected | |
18 | ?4. kbd interface (what does GTK do?) | |
19 | 5. there is still autoscrolling without scrollbars - but is it bad? | |
20 | */ | |
21 | ||
22 | // ============================================================================ | |
23 | // declarations | |
24 | // ============================================================================ | |
25 | ||
26 | // ---------------------------------------------------------------------------- | |
27 | // headers | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | #ifdef __GNUG__ | |
a3870b2f | 31 | #pragma implementation "univcombobox.h" |
1e6feb95 VZ |
32 | #endif |
33 | ||
34 | #include "wx/wxprec.h" | |
35 | ||
36 | #ifdef __BORLANDC__ | |
37 | #pragma hdrstop | |
38 | #endif | |
39 | ||
40 | #if wxUSE_COMBOBOX | |
41 | ||
42 | #ifndef WX_PRECOMP | |
43 | #include "wx/log.h" | |
44 | ||
45 | #include "wx/button.h" | |
46 | #include "wx/combobox.h" | |
47 | #include "wx/listbox.h" | |
48 | #include "wx/textctrl.h" | |
8cb172b4 | 49 | #include "wx/bmpbuttn.h" |
1e6feb95 VZ |
50 | |
51 | #include "wx/validate.h" | |
52 | #endif | |
53 | ||
54 | #include "wx/popupwin.h" | |
55 | ||
56 | #include "wx/univ/renderer.h" | |
57 | #include "wx/univ/inphand.h" | |
58 | #include "wx/univ/theme.h" | |
59 | ||
60 | /* | |
61 | The keyboard event flow: | |
62 | ||
63 | 1. they always come to the text ctrl | |
64 | 2. it forwards the ones it doesn't process to the wxComboControl | |
65 | 3. which passes them to the popup window if it is popped up | |
66 | */ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // wxComboButton is just a normal button except that it sends commands to the | |
70 | // combobox and not its parent | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | class wxComboButton : public wxBitmapButton | |
74 | { | |
75 | public: | |
76 | wxComboButton(wxComboControl *combo) | |
77 | : wxBitmapButton(combo->GetParent(), -1, wxNullBitmap, | |
78 | wxDefaultPosition, wxDefaultSize, | |
e4606ed9 | 79 | wxBORDER_NONE | wxBU_EXACTFIT) |
1e6feb95 VZ |
80 | { |
81 | m_combo = combo; | |
82 | ||
e4606ed9 VZ |
83 | wxBitmap bmpNormal, bmpFocus, bmpPressed, bmpDisabled; |
84 | ||
85 | GetRenderer()->GetComboBitmaps(&bmpNormal, | |
86 | &bmpFocus, | |
87 | &bmpPressed, | |
88 | &bmpDisabled); | |
1e6feb95 | 89 | |
1e6feb95 | 90 | SetBitmapLabel(bmpNormal); |
e4606ed9 VZ |
91 | SetBitmapFocus(bmpFocus.Ok() ? bmpFocus : bmpNormal); |
92 | SetBitmapSelected(bmpPressed.Ok() ? bmpPressed : bmpNormal); | |
93 | SetBitmapDisabled(bmpDisabled.Ok() ? bmpDisabled : bmpNormal); | |
1e6feb95 | 94 | |
e4606ed9 | 95 | SetBestSize(wxDefaultSize); |
1e6feb95 VZ |
96 | } |
97 | ||
98 | protected: | |
99 | void OnButton(wxCommandEvent& event) { m_combo->ShowPopup(); } | |
100 | ||
e4606ed9 VZ |
101 | virtual wxSize DoGetBestClientSize() const |
102 | { | |
103 | const wxBitmap& bmp = GetBitmapLabel(); | |
104 | ||
105 | return wxSize(bmp.GetWidth(), bmp.GetHeight()); | |
106 | ||
107 | } | |
1e6feb95 VZ |
108 | |
109 | private: | |
110 | wxComboControl *m_combo; | |
111 | ||
112 | DECLARE_EVENT_TABLE() | |
113 | }; | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // wxComboListBox is a listbox modified to be used as a popup window in a | |
117 | // combobox | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | class wxComboListBox : public wxListBox, public wxComboPopup | |
121 | { | |
122 | public: | |
123 | // ctor and dtor | |
124 | wxComboListBox(wxComboControl *combo, int style = 0); | |
125 | virtual ~wxComboListBox(); | |
126 | ||
127 | // implement wxComboPopup methods | |
128 | virtual bool SetSelection(const wxString& value); | |
129 | virtual wxControl *GetControl() { return this; } | |
130 | virtual void OnShow(); | |
131 | ||
132 | protected: | |
133 | // we shouldn't return height too big from here | |
134 | virtual wxSize DoGetBestClientSize() const; | |
135 | ||
136 | // filter mouse move events happening outside the list box | |
137 | void OnMouseMove(wxMouseEvent& event); | |
138 | ||
139 | // called whenever the user selects or activates a listbox item | |
140 | void OnSelect(wxCommandEvent& event); | |
141 | ||
142 | // used to process wxUniv actions | |
143 | bool PerformAction(const wxControlAction& action, | |
144 | long numArg, | |
145 | const wxString& strArg); | |
146 | ||
147 | private: | |
148 | DECLARE_EVENT_TABLE() | |
149 | }; | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // wxComboTextCtrl is a simple text ctrl which forwards | |
153 | // wxEVT_COMMAND_TEXT_UPDATED events and all key events to the combobox | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
156 | class wxComboTextCtrl : public wxTextCtrl | |
157 | { | |
158 | public: | |
159 | wxComboTextCtrl(wxComboControl *combo, | |
160 | const wxString& value, | |
161 | long style, | |
162 | const wxValidator& validator); | |
163 | ||
164 | protected: | |
165 | void OnKey(wxKeyEvent& event); | |
166 | void OnText(wxCommandEvent& event); | |
167 | ||
168 | private: | |
169 | wxComboControl *m_combo; | |
170 | ||
171 | DECLARE_EVENT_TABLE() | |
172 | }; | |
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // event tables and such | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | BEGIN_EVENT_TABLE(wxComboButton, wxButton) | |
179 | EVT_BUTTON(-1, wxComboButton::OnButton) | |
180 | END_EVENT_TABLE() | |
181 | ||
182 | BEGIN_EVENT_TABLE(wxComboListBox, wxListBox) | |
183 | EVT_LISTBOX(-1, wxComboListBox::OnSelect) | |
184 | EVT_LISTBOX_DCLICK(-1, wxComboListBox::OnSelect) | |
185 | EVT_MOTION(wxComboListBox::OnMouseMove) | |
186 | END_EVENT_TABLE() | |
187 | ||
188 | BEGIN_EVENT_TABLE(wxComboControl, wxControl) | |
189 | EVT_KEY_DOWN(wxComboControl::OnKey) | |
190 | EVT_KEY_UP(wxComboControl::OnKey) | |
191 | END_EVENT_TABLE() | |
192 | ||
193 | BEGIN_EVENT_TABLE(wxComboTextCtrl, wxTextCtrl) | |
194 | EVT_KEY_DOWN(wxComboTextCtrl::OnKey) | |
195 | EVT_KEY_UP(wxComboTextCtrl::OnKey) | |
196 | EVT_TEXT(-1, wxComboTextCtrl::OnText) | |
197 | END_EVENT_TABLE() | |
198 | ||
199 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl); | |
200 | ||
201 | // ============================================================================ | |
202 | // implementation | |
203 | // ============================================================================ | |
204 | ||
205 | // ---------------------------------------------------------------------------- | |
206 | // wxComboControl creation | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
209 | void wxComboControl::Init() | |
210 | { | |
211 | m_popup = (wxComboPopup *)NULL; | |
212 | m_winPopup = (wxPopupComboWindow *)NULL; | |
213 | m_isPopupShown = FALSE; | |
9f41d601 RR |
214 | m_btn = NULL; |
215 | m_text = NULL; | |
1e6feb95 VZ |
216 | } |
217 | ||
218 | bool wxComboControl::Create(wxWindow *parent, | |
219 | wxWindowID id, | |
220 | const wxString& value, | |
221 | const wxPoint& pos, | |
222 | const wxSize& size, | |
223 | long style, | |
224 | const wxValidator& validator, | |
225 | const wxString& name) | |
226 | { | |
227 | // first create our own window, i.e. the one which will contain all | |
228 | // subcontrols | |
229 | style &= ~wxBORDER_NONE; | |
230 | style |= wxBORDER_SUNKEN; | |
231 | if ( !wxControl::Create(parent, id, pos, size, style, validator, name) ) | |
232 | return FALSE; | |
233 | ||
234 | // create the text control and the button as our siblings (*not* children), | |
235 | // don't care about size/position here - they will be set in DoMoveWindow() | |
236 | m_btn = new wxComboButton(this); | |
237 | m_text = new wxComboTextCtrl(this, | |
238 | value, | |
239 | style & wxCB_READONLY ? wxTE_READONLY : 0, | |
240 | validator); | |
241 | ||
242 | // for compatibility with the other ports, the height specified is the | |
243 | // combined height of the combobox itself and the popup | |
244 | if ( size.y == -1 ) | |
245 | { | |
246 | // ok, use default height for popup too | |
247 | m_heightPopup = -1; | |
248 | } | |
249 | else | |
250 | { | |
251 | m_heightPopup = size.y - DoGetBestSize().y; | |
252 | } | |
253 | ||
e4606ed9 VZ |
254 | SetBestSize(size); |
255 | Move(pos); | |
1e6feb95 VZ |
256 | |
257 | // create the popup window immediately here to allow creating the controls | |
258 | // with parent == GetPopupWindow() from the derived class ctor | |
259 | m_winPopup = new wxPopupComboWindow(this); | |
260 | ||
261 | // have to disable this window to avoid interfering it with message | |
262 | // processing to the text and the button... but pretend it is enabled to | |
263 | // make IsEnabled() return TRUE | |
264 | wxControl::Enable(FALSE); // don't use non virtual Disable() here! | |
265 | m_isEnabled = TRUE; | |
266 | ||
267 | CreateInputHandler(wxINP_HANDLER_COMBOBOX); | |
268 | ||
269 | return TRUE; | |
270 | } | |
271 | ||
272 | wxComboControl::~wxComboControl() | |
273 | { | |
274 | // as the button and the text control are the parent's children and not | |
275 | // ours, we have to delete them manually - they are not deleted | |
276 | // automatically by wxWindows when we're deleted | |
277 | delete m_btn; | |
278 | delete m_text; | |
279 | ||
280 | delete m_winPopup; | |
281 | } | |
282 | ||
283 | // ---------------------------------------------------------------------------- | |
284 | // geometry stuff | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
287 | void wxComboControl::DoSetSize(int x, int y, | |
288 | int width, int height, | |
289 | int sizeFlags) | |
290 | { | |
291 | // combo height is always fixed | |
292 | wxControl::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
293 | } | |
294 | ||
295 | wxSize wxComboControl::DoGetBestClientSize() const | |
296 | { | |
297 | wxSize sizeBtn = m_btn->GetBestSize(), | |
298 | sizeText = m_text->GetBestSize(); | |
299 | ||
300 | return wxSize(sizeBtn.x + sizeText.x, wxMax(sizeBtn.y, sizeText.y)); | |
301 | } | |
302 | ||
303 | void wxComboControl::DoMoveWindow(int x, int y, int width, int height) | |
304 | { | |
305 | wxControl::DoMoveWindow(x, y, width, height); | |
306 | ||
307 | // position the subcontrols inside the client area | |
308 | wxRect rectBorders = GetRenderer()->GetBorderDimensions(GetBorder()); | |
309 | x += rectBorders.x; | |
310 | y += rectBorders.y; | |
311 | width -= rectBorders.x + rectBorders.width; | |
312 | height -= rectBorders.y + rectBorders.height; | |
313 | ||
e4606ed9 | 314 | wxSize sizeBtn = m_btn->GetBestSize(); |
1e6feb95 VZ |
315 | |
316 | wxCoord wText = width - sizeBtn.x; | |
c2ef70ec VS |
317 | wxPoint p = GetParent() ? GetParent()->GetClientAreaOrigin() : wxPoint(0,0); |
318 | m_text->SetSize(x - p.x, y - p.y, wText, height); | |
319 | m_btn->SetSize(x - p.x + wText, y - p.y, sizeBtn.x, height); | |
1e6feb95 VZ |
320 | } |
321 | ||
322 | // ---------------------------------------------------------------------------- | |
323 | // operations | |
324 | // ---------------------------------------------------------------------------- | |
325 | ||
326 | bool wxComboControl::Enable(bool enable) | |
327 | { | |
328 | if ( !wxControl::Enable(enable) ) | |
329 | return FALSE; | |
330 | ||
331 | m_btn->Enable(enable); | |
332 | m_text->Enable(enable); | |
333 | ||
334 | return TRUE; | |
335 | } | |
336 | ||
337 | bool wxComboControl::Show(bool show) | |
338 | { | |
339 | if ( !wxControl::Show(show) ) | |
340 | return FALSE; | |
341 | ||
9f41d601 RR |
342 | if (m_btn) |
343 | m_btn->Show(show); | |
344 | ||
345 | if (m_text) | |
346 | m_text->Show(show); | |
1e6feb95 VZ |
347 | |
348 | return TRUE; | |
349 | } | |
350 | ||
351 | // ---------------------------------------------------------------------------- | |
352 | // popup window handling | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | void wxComboControl::SetPopupControl(wxComboPopup *popup) | |
356 | { | |
357 | m_popup = popup; | |
358 | } | |
359 | ||
360 | void wxComboControl::ShowPopup() | |
361 | { | |
362 | wxCHECK_RET( m_popup, _T("no popup to show in wxComboControl") ); | |
363 | wxCHECK_RET( !IsPopupShown(), _T("popup window already shown") ); | |
364 | ||
365 | wxControl *control = m_popup->GetControl(); | |
366 | ||
367 | // size and position the popup window correctly | |
368 | m_winPopup->SetSize(GetSize().x, | |
369 | m_heightPopup == -1 ? control->GetBestSize().y | |
370 | : m_heightPopup); | |
371 | wxSize sizePopup = m_winPopup->GetClientSize(); | |
372 | control->SetSize(0, 0, sizePopup.x, sizePopup.y); | |
373 | ||
374 | // some controls don't accept the size we give then: e.g. a listbox may | |
375 | // require more space to show its last row | |
376 | wxSize sizeReal = control->GetSize(); | |
377 | if ( sizeReal != sizePopup ) | |
378 | { | |
379 | m_winPopup->SetClientSize(sizeReal); | |
380 | } | |
381 | ||
382 | m_winPopup->PositionNearCombo(); | |
383 | ||
384 | // show it | |
385 | m_winPopup->Popup(m_text); | |
386 | m_text->SelectAll(); | |
387 | m_popup->SetSelection(m_text->GetValue()); | |
388 | ||
389 | m_isPopupShown = TRUE; | |
390 | } | |
391 | ||
392 | void wxComboControl::HidePopup() | |
393 | { | |
394 | wxCHECK_RET( m_popup, _T("no popup to hide in wxComboControl") ); | |
395 | wxCHECK_RET( IsPopupShown(), _T("popup window not shown") ); | |
396 | ||
397 | m_winPopup->Dismiss(); | |
398 | ||
399 | m_isPopupShown = FALSE; | |
400 | } | |
401 | ||
402 | void wxComboControl::OnSelect(const wxString& value) | |
403 | { | |
404 | m_text->SetValue(value); | |
405 | m_text->SelectAll(); | |
406 | ||
407 | OnDismiss(); | |
408 | } | |
409 | ||
410 | void wxComboControl::OnDismiss() | |
411 | { | |
412 | HidePopup(); | |
413 | m_text->SetFocus(); | |
414 | } | |
415 | ||
416 | // ---------------------------------------------------------------------------- | |
417 | // wxComboTextCtrl | |
418 | // ---------------------------------------------------------------------------- | |
419 | ||
420 | wxComboTextCtrl::wxComboTextCtrl(wxComboControl *combo, | |
421 | const wxString& value, | |
422 | long style, | |
423 | const wxValidator& validator) | |
424 | : wxTextCtrl(combo->GetParent(), -1, value, | |
425 | wxDefaultPosition, wxDefaultSize, | |
426 | wxBORDER_NONE | style, | |
427 | validator) | |
428 | { | |
429 | m_combo = combo; | |
430 | } | |
431 | ||
432 | void wxComboTextCtrl::OnText(wxCommandEvent& event) | |
433 | { | |
434 | if ( m_combo->IsPopupShown() ) | |
435 | { | |
436 | m_combo->GetPopupControl()->SetSelection(GetValue()); | |
437 | } | |
438 | ||
439 | // we need to make a copy of the event to have the correct originating | |
440 | // object and id | |
441 | wxCommandEvent event2 = event; | |
442 | event2.SetEventObject(m_combo); | |
443 | event2.SetId(m_combo->GetId()); | |
444 | ||
445 | // there is a small incompatibility with wxMSW here: the combobox gets the | |
446 | // event before the text control in our case which corresponds to SMW | |
447 | // CBN_EDITUPDATE notification and not CBN_EDITCHANGE one wxMSW currently | |
448 | // uses | |
449 | // | |
450 | // if this is really a problem, we can play games with the event handlers | |
451 | // to circumvent this | |
452 | (void)m_combo->ProcessEvent(event2); | |
453 | ||
454 | event.Skip(); | |
455 | } | |
456 | ||
457 | // pass the keys we don't process to the combo first | |
458 | void wxComboTextCtrl::OnKey(wxKeyEvent& event) | |
459 | { | |
460 | switch ( event.GetKeyCode() ) | |
461 | { | |
462 | case WXK_RETURN: | |
463 | // the popup control gets it first but only if it is shown | |
464 | if ( !m_combo->IsPopupShown() ) | |
465 | break; | |
466 | //else: fall through | |
467 | ||
468 | case WXK_UP: | |
469 | case WXK_DOWN: | |
470 | case WXK_ESCAPE: | |
471 | case WXK_PAGEDOWN: | |
472 | case WXK_PAGEUP: | |
473 | case WXK_PRIOR: | |
474 | case WXK_NEXT: | |
475 | (void)m_combo->ProcessEvent(event); | |
476 | return; | |
477 | } | |
478 | ||
479 | event.Skip(); | |
480 | } | |
481 | ||
482 | // ---------------------------------------------------------------------------- | |
483 | // wxComboListBox | |
484 | // ---------------------------------------------------------------------------- | |
485 | ||
486 | wxComboListBox::wxComboListBox(wxComboControl *combo, int style) | |
487 | : wxListBox(combo->GetPopupWindow(), -1, | |
488 | wxDefaultPosition, wxDefaultSize, | |
489 | 0, NULL, | |
490 | wxBORDER_SIMPLE | wxLB_INT_HEIGHT | style), | |
491 | wxComboPopup(combo) | |
492 | { | |
493 | // we don't react to the mouse events outside the window at all | |
494 | StopAutoScrolling(); | |
495 | } | |
496 | ||
497 | wxComboListBox::~wxComboListBox() | |
498 | { | |
499 | } | |
500 | ||
501 | bool wxComboListBox::SetSelection(const wxString& value) | |
502 | { | |
503 | // FindItem() would just find the current item for an empty string (it | |
504 | // always matches), but we want to show the first one in such case | |
505 | if ( value.empty() ) | |
506 | { | |
507 | if ( GetCount() ) | |
508 | { | |
509 | wxListBox::SetSelection(0); | |
510 | } | |
511 | //else: empty listbox - nothing to do | |
512 | } | |
513 | else if ( !FindItem(value) ) | |
514 | { | |
515 | // no match att all | |
516 | return FALSE; | |
517 | } | |
518 | ||
519 | return TRUE; | |
520 | } | |
521 | ||
522 | void wxComboListBox::OnSelect(wxCommandEvent& event) | |
523 | { | |
e4315700 VS |
524 | // first update the combo and close the listbox |
525 | m_combo->OnSelect(event.GetString()); | |
526 | ||
527 | // next let the user code have the event | |
1e6feb95 VZ |
528 | |
529 | // all fields are already filled by the listbox, just change the event | |
530 | // type and send it to the combo | |
531 | wxCommandEvent event2 = event; | |
532 | event2.SetEventType(wxEVT_COMMAND_COMBOBOX_SELECTED); | |
533 | event2.SetEventObject(m_combo); | |
534 | event2.SetId(m_combo->GetId()); | |
535 | m_combo->ProcessEvent(event2); | |
1e6feb95 VZ |
536 | } |
537 | ||
538 | void wxComboListBox::OnShow() | |
539 | { | |
540 | } | |
541 | ||
542 | bool wxComboListBox::PerformAction(const wxControlAction& action, | |
543 | long numArg, | |
544 | const wxString& strArg) | |
545 | ||
546 | { | |
547 | if ( action == wxACTION_LISTBOX_FIND ) | |
548 | { | |
549 | // we don't let the listbox handle this as instead of just using the | |
550 | // single key presses, as usual, we use the text ctrl value as prefix | |
551 | // and this is done by wxComboControl itself | |
552 | return TRUE; | |
553 | } | |
554 | ||
555 | return wxListBox::PerformAction(action, numArg, strArg); | |
556 | } | |
557 | ||
558 | void wxComboListBox::OnMouseMove(wxMouseEvent& event) | |
559 | { | |
560 | // while a wxComboListBox is shown, it always has capture, so if it doesn't | |
561 | // we're about to go away anyhow (normally this shouldn't happen at all, | |
562 | // but I don't put assert here as it just might do on other platforms and | |
563 | // it doesn't break anythign anyhow) | |
564 | if ( this == wxWindow::GetCapture() ) | |
565 | { | |
566 | if ( HitTest(event.GetPosition()) == wxHT_WINDOW_INSIDE ) | |
567 | { | |
568 | event.Skip(); | |
569 | } | |
570 | //else: popup shouldn't react to the mouse motions outside it, it only | |
571 | // captures the mouse to be able to detect when it must be | |
572 | // dismissed, so don't call Skip() | |
573 | } | |
574 | } | |
575 | ||
576 | wxSize wxComboListBox::DoGetBestClientSize() const | |
577 | { | |
578 | // don't return size too big or we risk to not fit on the screen | |
579 | wxSize size = wxListBox::DoGetBestClientSize(); | |
580 | wxCoord hChar = GetCharHeight(); | |
581 | ||
582 | int nLines = size.y / hChar; | |
583 | ||
584 | // 10 is the same limit as used by wxMSW | |
585 | if ( nLines > 10 ) | |
586 | { | |
587 | size.y = 10*hChar; | |
588 | } | |
589 | ||
590 | return size; | |
591 | } | |
592 | ||
593 | // ---------------------------------------------------------------------------- | |
594 | // wxComboBox | |
595 | // ---------------------------------------------------------------------------- | |
596 | ||
597 | void wxComboBox::Init() | |
598 | { | |
599 | m_lbox = (wxListBox *)NULL; | |
600 | } | |
601 | ||
602 | bool wxComboBox::Create(wxWindow *parent, | |
603 | wxWindowID id, | |
604 | const wxString& value, | |
605 | const wxPoint& pos, | |
606 | const wxSize& size, | |
607 | int n, | |
608 | const wxString *choices, | |
609 | long style, | |
610 | const wxValidator& validator, | |
611 | const wxString& name) | |
612 | { | |
613 | if ( !wxComboControl::Create(parent, id, value, pos, size, style, | |
614 | validator, name) ) | |
615 | { | |
616 | return FALSE; | |
617 | } | |
618 | ||
619 | wxComboListBox *combolbox = | |
620 | new wxComboListBox(this, style & wxCB_SORT ? wxLB_SORT : 0); | |
621 | m_lbox = combolbox; | |
622 | m_lbox->Set(n, choices); | |
623 | ||
624 | SetPopupControl(combolbox); | |
625 | ||
626 | return TRUE; | |
627 | } | |
628 | ||
629 | wxComboBox::~wxComboBox() | |
630 | { | |
631 | } | |
632 | ||
633 | // ---------------------------------------------------------------------------- | |
634 | // wxComboBox methods forwarded to wxTextCtrl | |
635 | // ---------------------------------------------------------------------------- | |
636 | ||
637 | wxString wxComboBox::GetValue() const | |
638 | { | |
639 | return GetText()->GetValue(); | |
640 | } | |
641 | ||
642 | void wxComboBox::SetValue(const wxString& value) | |
643 | { | |
644 | GetText()->SetValue(value); | |
645 | } | |
646 | ||
647 | void wxComboBox::Copy() | |
648 | { | |
649 | GetText()->Copy(); | |
650 | } | |
651 | ||
652 | void wxComboBox::Cut() | |
653 | { | |
654 | GetText()->Cut(); | |
655 | } | |
656 | ||
657 | void wxComboBox::Paste() | |
658 | { | |
659 | GetText()->Paste(); | |
660 | } | |
661 | ||
662 | void wxComboBox::SetInsertionPoint(long pos) | |
663 | { | |
664 | GetText()->SetInsertionPoint(pos); | |
665 | } | |
666 | ||
667 | void wxComboBox::SetInsertionPointEnd() | |
668 | { | |
669 | GetText()->SetInsertionPointEnd(); | |
670 | } | |
671 | ||
672 | long wxComboBox::GetInsertionPoint() const | |
673 | { | |
674 | return GetText()->GetInsertionPoint(); | |
675 | } | |
676 | ||
677 | long wxComboBox::GetLastPosition() const | |
678 | { | |
679 | return GetText()->GetLastPosition(); | |
680 | } | |
681 | ||
682 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
683 | { | |
684 | GetText()->Replace(from, to, value); | |
685 | } | |
686 | ||
687 | void wxComboBox::Remove(long from, long to) | |
688 | { | |
689 | GetText()->Remove(from, to); | |
690 | } | |
691 | ||
692 | void wxComboBox::SetSelection(long from, long to) | |
693 | { | |
694 | GetText()->SetSelection(from, to); | |
695 | } | |
696 | ||
697 | void wxComboBox::SetEditable(bool editable) | |
698 | { | |
699 | GetText()->SetEditable(editable); | |
700 | } | |
701 | ||
702 | // ---------------------------------------------------------------------------- | |
703 | // wxComboBox methods forwarded to wxListBox | |
704 | // ---------------------------------------------------------------------------- | |
705 | ||
706 | void wxComboBox::Clear() | |
707 | { | |
708 | GetLBox()->Clear(); | |
709 | } | |
710 | ||
711 | void wxComboBox::Delete(int n) | |
712 | { | |
713 | GetLBox()->Delete(n); | |
714 | } | |
715 | ||
716 | int wxComboBox::GetCount() const | |
717 | { | |
718 | return GetLBox()->GetCount(); | |
719 | } | |
720 | ||
721 | wxString wxComboBox::GetString(int n) const | |
722 | { | |
723 | return GetLBox()->GetString(n); | |
724 | } | |
725 | ||
726 | void wxComboBox::SetString(int n, const wxString& s) | |
727 | { | |
728 | GetLBox()->SetString(n, s); | |
729 | } | |
730 | ||
731 | int wxComboBox::FindString(const wxString& s) const | |
732 | { | |
733 | return GetLBox()->FindString(s); | |
734 | } | |
735 | ||
736 | void wxComboBox::Select(int n) | |
737 | { | |
738 | wxCHECK_RET( (n >= 0) && (n < GetCount()), _T("invalid combobox index") ); | |
739 | ||
740 | GetLBox()->SetSelection(n); | |
741 | GetText()->SetValue(GetLBox()->GetString(n)); | |
742 | } | |
743 | ||
744 | int wxComboBox::GetSelection() const | |
745 | { | |
746 | // if the current value isn't one of the listbox strings, return -1 | |
747 | return FindString(GetText()->GetValue()); | |
748 | } | |
749 | ||
750 | int wxComboBox::DoAppend(const wxString& item) | |
751 | { | |
752 | return GetLBox()->Append(item); | |
753 | } | |
754 | ||
755 | void wxComboBox::DoSetItemClientData(int n, void* clientData) | |
756 | { | |
757 | GetLBox()->SetClientData(n, clientData); | |
758 | } | |
759 | ||
760 | void *wxComboBox::DoGetItemClientData(int n) const | |
761 | { | |
762 | return GetLBox()->GetClientData(n); | |
763 | } | |
764 | ||
765 | void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
766 | { | |
767 | GetLBox()->SetClientObject(n, clientData); | |
768 | } | |
769 | ||
770 | wxClientData* wxComboBox::DoGetItemClientObject(int n) const | |
771 | { | |
772 | return GetLBox()->GetClientObject(n); | |
773 | } | |
774 | ||
775 | // ---------------------------------------------------------------------------- | |
776 | // input handling | |
777 | // ---------------------------------------------------------------------------- | |
778 | ||
2e9f62da | 779 | void wxComboControl::OnKey(wxKeyEvent& event) |
1e6feb95 VZ |
780 | { |
781 | if ( m_isPopupShown ) | |
782 | { | |
783 | // pass it to the popped up control | |
784 | (void)m_popup->GetControl()->ProcessEvent(event); | |
785 | } | |
786 | else // no popup | |
787 | { | |
788 | event.Skip(); | |
789 | } | |
790 | } | |
791 | ||
792 | bool wxComboControl::PerformAction(const wxControlAction& action, | |
793 | long numArg, | |
794 | const wxString& strArg) | |
795 | { | |
796 | bool processed = FALSE; | |
797 | if ( action == wxACTION_COMBOBOX_POPUP ) | |
798 | { | |
799 | if ( !m_isPopupShown ) | |
800 | { | |
801 | ShowPopup(); | |
802 | ||
803 | processed = TRUE; | |
804 | } | |
805 | } | |
806 | else if ( action == wxACTION_COMBOBOX_DISMISS ) | |
807 | { | |
808 | if ( m_isPopupShown ) | |
809 | { | |
810 | HidePopup(); | |
811 | ||
812 | processed = TRUE; | |
813 | } | |
814 | } | |
815 | ||
816 | if ( !processed ) | |
817 | { | |
818 | // pass along | |
819 | return wxControl::PerformAction(action, numArg, strArg); | |
820 | } | |
821 | ||
822 | return TRUE; | |
823 | } | |
824 | ||
825 | // ---------------------------------------------------------------------------- | |
826 | // wxStdComboBoxInputHandler | |
827 | // ---------------------------------------------------------------------------- | |
828 | ||
829 | wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler *inphand) | |
830 | : wxStdInputHandler(inphand) | |
831 | { | |
832 | } | |
833 | ||
23645bfa | 834 | bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer *consumer, |
1e6feb95 VZ |
835 | const wxKeyEvent& event, |
836 | bool pressed) | |
837 | { | |
838 | if ( pressed ) | |
839 | { | |
840 | wxControlAction action; | |
841 | switch ( event.GetKeyCode() ) | |
842 | { | |
843 | case WXK_DOWN: | |
844 | action = wxACTION_COMBOBOX_POPUP; | |
845 | break; | |
846 | ||
847 | case WXK_ESCAPE: | |
848 | action = wxACTION_COMBOBOX_DISMISS; | |
849 | break; | |
850 | } | |
851 | ||
852 | if ( !!action ) | |
853 | { | |
23645bfa | 854 | consumer->PerformAction(action); |
1e6feb95 VZ |
855 | |
856 | return TRUE; | |
857 | } | |
858 | } | |
859 | ||
23645bfa | 860 | return wxStdInputHandler::HandleKey(consumer, event, pressed); |
1e6feb95 VZ |
861 | } |
862 | ||
863 | #endif // wxUSE_COMBOBOX |