]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/univ/combobox.cpp | |
3 | // Purpose: wxComboBox implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 15.12.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_COMBOBOX | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/log.h" | |
30 | ||
31 | #include "wx/button.h" | |
32 | #include "wx/combobox.h" | |
33 | #include "wx/listbox.h" | |
34 | #include "wx/textctrl.h" | |
35 | #include "wx/bmpbuttn.h" | |
36 | ||
37 | #include "wx/validate.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/tooltip.h" | |
41 | #include "wx/combo.h" | |
42 | ||
43 | #include "wx/univ/renderer.h" | |
44 | #include "wx/univ/inphand.h" | |
45 | #include "wx/univ/theme.h" | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxStdComboBoxInputHandler: allows the user to open/close the combo from kbd | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | class WXDLLEXPORT wxStdComboBoxInputHandler : public wxStdInputHandler | |
52 | { | |
53 | public: | |
54 | wxStdComboBoxInputHandler(wxInputHandler *inphand); | |
55 | ||
56 | virtual bool HandleKey(wxInputConsumer *consumer, | |
57 | const wxKeyEvent& event, | |
58 | bool pressed); | |
59 | }; | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // wxComboListBox is a listbox modified to be used as a popup window in a | |
63 | // combobox | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | class wxComboListBox : public wxListBox, public wxComboPopup | |
67 | { | |
68 | public: | |
69 | // ctor and dtor | |
70 | wxComboListBox(); | |
71 | virtual ~wxComboListBox(); | |
72 | ||
73 | // implement wxComboPopup methods | |
74 | virtual bool Create(wxWindow* parent); | |
75 | virtual void SetStringValue(const wxString& s); | |
76 | virtual wxString GetStringValue() const; | |
77 | virtual wxWindow *GetControl() { return this; } | |
78 | virtual void OnPopup(); | |
79 | virtual wxSize GetAdjustedSize(int minWidth, int prefHeight, int maxHeight); | |
80 | ||
81 | // fix virtual function hiding | |
82 | virtual void SetSelection(int n) { DoSetSelection(n, true); } | |
83 | void SetSelection(int n, bool select) { DoSetSelection(n, select); } | |
84 | ||
85 | // used to process wxUniv actions | |
86 | bool PerformAction(const wxControlAction& action, | |
87 | long numArg, | |
88 | const wxString& strArg); | |
89 | ||
90 | protected: | |
91 | // set m_clicked value from here | |
92 | void OnLeftUp(wxMouseEvent& event); | |
93 | ||
94 | private: | |
95 | friend class wxComboBox; // it accesses our DoGetItemClientData() | |
96 | ||
97 | DECLARE_EVENT_TABLE() | |
98 | }; | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
101 | // event tables and such | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
104 | BEGIN_EVENT_TABLE(wxComboListBox, wxListBox) | |
105 | EVT_LEFT_UP(wxComboListBox::OnLeftUp) | |
106 | END_EVENT_TABLE() | |
107 | ||
108 | // ============================================================================ | |
109 | // implementation | |
110 | // ============================================================================ | |
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // wxComboListBox | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | wxComboListBox::wxComboListBox() : wxListBox(), wxComboPopup() | |
117 | { | |
118 | } | |
119 | ||
120 | bool wxComboListBox::Create(wxWindow* parent) | |
121 | { | |
122 | if ( !wxListBox::Create(parent, wxID_ANY, | |
123 | wxDefaultPosition, wxDefaultSize, | |
124 | 0, NULL, | |
125 | wxBORDER_SIMPLE | | |
126 | ( m_combo->GetWindowStyle() & wxCB_SORT ? wxLB_SORT : 0 ) ) ) | |
127 | return false; | |
128 | ||
129 | // we don't react to the mouse events outside the window at all | |
130 | StopAutoScrolling(); | |
131 | ||
132 | return true; | |
133 | } | |
134 | ||
135 | wxComboListBox::~wxComboListBox() | |
136 | { | |
137 | } | |
138 | ||
139 | wxString wxComboListBox::GetStringValue() const | |
140 | { | |
141 | return wxListBox::GetStringSelection(); | |
142 | } | |
143 | ||
144 | void wxComboListBox::SetStringValue(const wxString& value) | |
145 | { | |
146 | if ( !value.empty() ) | |
147 | { | |
148 | if (FindString(value) != wxNOT_FOUND) | |
149 | wxListBox::SetStringSelection(value); | |
150 | } | |
151 | else | |
152 | wxListBox::SetSelection(-1); | |
153 | } | |
154 | ||
155 | void wxComboListBox::OnPopup() | |
156 | { | |
157 | } | |
158 | ||
159 | bool wxComboListBox::PerformAction(const wxControlAction& action, | |
160 | long numArg, | |
161 | const wxString& strArg) | |
162 | ||
163 | { | |
164 | if ( action == wxACTION_LISTBOX_FIND ) | |
165 | { | |
166 | // we don't let the listbox handle this as instead of just using the | |
167 | // single key presses, as usual, we use the text ctrl value as prefix | |
168 | // and this is done by wxComboCtrl itself | |
169 | return true; | |
170 | } | |
171 | ||
172 | return wxListBox::PerformAction(action, numArg, strArg); | |
173 | } | |
174 | ||
175 | void wxComboListBox::OnLeftUp(wxMouseEvent& event) | |
176 | { | |
177 | // we should dismiss the combo now | |
178 | // first update the combo and close the listbox | |
179 | Dismiss(); | |
180 | m_combo->SetValue(wxListBox::GetStringSelection()); | |
181 | ||
182 | // next let the user code have the event | |
183 | wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId()); | |
184 | evt.SetInt(wxListBox::GetSelection()); | |
185 | evt.SetEventObject(m_combo); | |
186 | m_combo->ProcessWindowEvent(evt); | |
187 | ||
188 | event.Skip(); | |
189 | } | |
190 | ||
191 | wxSize wxComboListBox::GetAdjustedSize(int minWidth, | |
192 | int WXUNUSED(prefHeight), | |
193 | int maxHeight) | |
194 | { | |
195 | wxSize bestSize = wxListBox::GetBestSize(); | |
196 | return wxSize(wxMax(bestSize.x,minWidth), | |
197 | wxMin(bestSize.y,maxHeight)); | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // wxComboBox | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | void wxComboBox::Init() | |
205 | { | |
206 | m_lbox = NULL; | |
207 | } | |
208 | ||
209 | wxComboBox::wxComboBox(wxWindow *parent, | |
210 | wxWindowID id, | |
211 | const wxString& value, | |
212 | const wxPoint& pos, | |
213 | const wxSize& size, | |
214 | const wxArrayString& choices, | |
215 | long style, | |
216 | const wxValidator& validator, | |
217 | const wxString& name) | |
218 | { | |
219 | Init(); | |
220 | ||
221 | Create(parent, id, value, pos, size, choices, style, validator, name); | |
222 | } | |
223 | ||
224 | bool wxComboBox::Create(wxWindow *parent, | |
225 | wxWindowID id, | |
226 | const wxString& value, | |
227 | const wxPoint& pos, | |
228 | const wxSize& size, | |
229 | const wxArrayString& choices, | |
230 | long style, | |
231 | const wxValidator& validator, | |
232 | const wxString& name) | |
233 | { | |
234 | wxCArrayString chs(choices); | |
235 | ||
236 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
237 | chs.GetStrings(), style, validator, name); | |
238 | } | |
239 | ||
240 | bool wxComboBox::Create(wxWindow *parent, | |
241 | wxWindowID id, | |
242 | const wxString& value, | |
243 | const wxPoint& pos, | |
244 | const wxSize& size, | |
245 | int n, | |
246 | const wxString choices[], | |
247 | long style, | |
248 | const wxValidator& validator, | |
249 | const wxString& name) | |
250 | { | |
251 | if ( !wxComboCtrl::Create(parent, id, value, pos, size, style, | |
252 | validator, name) ) | |
253 | { | |
254 | return false; | |
255 | } | |
256 | ||
257 | wxComboListBox *combolbox = new wxComboListBox(); | |
258 | SetPopupControl(combolbox); | |
259 | ||
260 | m_lbox = combolbox; | |
261 | m_lbox->Set(n, choices); | |
262 | ||
263 | return true; | |
264 | } | |
265 | ||
266 | wxComboBox::~wxComboBox() | |
267 | { | |
268 | } | |
269 | ||
270 | // ---------------------------------------------------------------------------- | |
271 | // wxComboBox methods forwarded to wxTextCtrl | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | wxString wxComboBox::DoGetValue() const | |
275 | { | |
276 | return wxComboCtrl::GetValue(); | |
277 | } | |
278 | ||
279 | void wxComboBox::SetValue(const wxString& value) | |
280 | { | |
281 | wxComboCtrl::SetValue(value); | |
282 | } | |
283 | ||
284 | void wxComboBox::WriteText(const wxString& value) | |
285 | { | |
286 | if ( GetTextCtrl() ) GetTextCtrl()->WriteText(value); | |
287 | } | |
288 | ||
289 | void wxComboBox::Copy() | |
290 | { | |
291 | if ( GetTextCtrl() ) GetTextCtrl()->Copy(); | |
292 | } | |
293 | ||
294 | void wxComboBox::Cut() | |
295 | { | |
296 | if ( GetTextCtrl() ) GetTextCtrl()->Cut(); | |
297 | } | |
298 | ||
299 | void wxComboBox::Paste() | |
300 | { | |
301 | if ( GetTextCtrl() ) GetTextCtrl()->Paste(); | |
302 | } | |
303 | ||
304 | void wxComboBox::SetInsertionPoint(long pos) | |
305 | { | |
306 | if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos); | |
307 | } | |
308 | ||
309 | void wxComboBox::SetInsertionPointEnd() | |
310 | { | |
311 | if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd(); | |
312 | } | |
313 | ||
314 | long wxComboBox::GetInsertionPoint() const | |
315 | { | |
316 | if ( GetTextCtrl() ) | |
317 | return GetTextCtrl()->GetInsertionPoint(); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | wxTextPos wxComboBox::GetLastPosition() const | |
322 | { | |
323 | if ( GetTextCtrl() ) | |
324 | return GetTextCtrl()->GetLastPosition(); | |
325 | return -1; | |
326 | } | |
327 | ||
328 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
329 | { | |
330 | if ( GetTextCtrl() ) GetTextCtrl()->Replace(from, to, value); | |
331 | } | |
332 | ||
333 | void wxComboBox::Remove(long from, long to) | |
334 | { | |
335 | if ( GetTextCtrl() ) GetTextCtrl()->Remove(from, to); | |
336 | } | |
337 | ||
338 | void wxComboBox::SetSelection(long from, long to) | |
339 | { | |
340 | if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from, to); | |
341 | } | |
342 | ||
343 | void wxComboBox::GetSelection(long *from, long *to) const | |
344 | { | |
345 | if ( GetTextCtrl() ) GetTextCtrl()->GetSelection(from, to); | |
346 | } | |
347 | ||
348 | void wxComboBox::SetEditable(bool editable) | |
349 | { | |
350 | if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable); | |
351 | } | |
352 | ||
353 | // ---------------------------------------------------------------------------- | |
354 | // wxComboBox methods forwarded to wxListBox | |
355 | // ---------------------------------------------------------------------------- | |
356 | ||
357 | void wxComboBox::DoClear() | |
358 | { | |
359 | GetLBox()->Clear(); | |
360 | if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString); | |
361 | } | |
362 | ||
363 | void wxComboBox::DoDeleteOneItem(unsigned int n) | |
364 | { | |
365 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxComboBox::Delete") ); | |
366 | ||
367 | if (GetSelection() == (int)n) | |
368 | if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString); | |
369 | ||
370 | GetLBox()->Delete(n); | |
371 | } | |
372 | ||
373 | unsigned int wxComboBox::GetCount() const | |
374 | { | |
375 | return GetLBox()->GetCount(); | |
376 | } | |
377 | ||
378 | wxString wxComboBox::GetString(unsigned int n) const | |
379 | { | |
380 | wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("invalid index in wxComboBox::GetString") ); | |
381 | ||
382 | return GetLBox()->GetString(n); | |
383 | } | |
384 | ||
385 | void wxComboBox::SetString(unsigned int n, const wxString& s) | |
386 | { | |
387 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxComboBox::SetString") ); | |
388 | ||
389 | GetLBox()->SetString(n, s); | |
390 | } | |
391 | ||
392 | int wxComboBox::FindString(const wxString& s, bool bCase) const | |
393 | { | |
394 | return GetLBox()->FindString(s, bCase); | |
395 | } | |
396 | ||
397 | void wxComboBox::SetSelection(int n) | |
398 | { | |
399 | wxCHECK_RET( (n == wxNOT_FOUND || IsValid(n)), wxT("invalid index in wxComboBox::Select") ); | |
400 | ||
401 | GetLBox()->SetSelection(n); | |
402 | ||
403 | wxString str; | |
404 | if ( n != wxNOT_FOUND ) | |
405 | str = GetLBox()->GetString(n); | |
406 | ||
407 | SetText(str); | |
408 | } | |
409 | ||
410 | int wxComboBox::GetSelection() const | |
411 | { | |
412 | #if 1 // FIXME:: What is the correct behaviour? | |
413 | // if the current value isn't one of the listbox strings, return -1 | |
414 | return GetLBox()->GetSelection(); | |
415 | #else | |
416 | // Why oh why is this done this way? | |
417 | // It is not because the value displayed in the text can be found | |
418 | // in the list that it is the item that is selected! | |
419 | return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue()); | |
420 | #endif | |
421 | } | |
422 | ||
423 | wxString wxComboBox::GetStringSelection() const | |
424 | { | |
425 | return GetLBox()->GetStringSelection(); | |
426 | } | |
427 | ||
428 | wxClientDataType wxComboBox::GetClientDataType() const | |
429 | { | |
430 | return GetLBox()->GetClientDataType(); | |
431 | } | |
432 | ||
433 | void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType) | |
434 | { | |
435 | GetLBox()->SetClientDataType(clientDataItemsType); | |
436 | } | |
437 | ||
438 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
439 | unsigned int pos, | |
440 | void **clientData, wxClientDataType type) | |
441 | { | |
442 | return GetLBox()->DoInsertItems(items, pos, clientData, type); | |
443 | } | |
444 | ||
445 | void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData) | |
446 | { | |
447 | GetLBox()->DoSetItemClientData(n, clientData); | |
448 | } | |
449 | ||
450 | void *wxComboBox::DoGetItemClientData(unsigned int n) const | |
451 | { | |
452 | return GetLBox()->DoGetItemClientData(n); | |
453 | } | |
454 | ||
455 | bool wxComboBox::IsEditable() const | |
456 | { | |
457 | return GetTextCtrl() != NULL && (!HasFlag(wxCB_READONLY) || GetTextCtrl()->IsEditable() ); | |
458 | } | |
459 | ||
460 | void wxComboBox::Undo() | |
461 | { | |
462 | if (IsEditable()) | |
463 | if ( GetTextCtrl() ) GetTextCtrl()->Undo(); | |
464 | } | |
465 | ||
466 | void wxComboBox::Redo() | |
467 | { | |
468 | if (IsEditable()) | |
469 | if ( GetTextCtrl() ) GetTextCtrl()->Redo(); | |
470 | } | |
471 | ||
472 | void wxComboBox::SelectAll() | |
473 | { | |
474 | if ( GetTextCtrl() ) GetTextCtrl()->SelectAll(); | |
475 | } | |
476 | ||
477 | bool wxComboBox::CanCopy() const | |
478 | { | |
479 | if (GetTextCtrl() != NULL) | |
480 | return GetTextCtrl()->CanCopy(); | |
481 | else | |
482 | return false; | |
483 | } | |
484 | ||
485 | bool wxComboBox::CanCut() const | |
486 | { | |
487 | if (GetTextCtrl() != NULL) | |
488 | return GetTextCtrl()->CanCut(); | |
489 | else | |
490 | return false; | |
491 | } | |
492 | ||
493 | bool wxComboBox::CanPaste() const | |
494 | { | |
495 | if (IsEditable()) | |
496 | return GetTextCtrl()->CanPaste(); | |
497 | else | |
498 | return false; | |
499 | } | |
500 | ||
501 | bool wxComboBox::CanUndo() const | |
502 | { | |
503 | if (IsEditable()) | |
504 | return GetTextCtrl()->CanUndo(); | |
505 | else | |
506 | return false; | |
507 | } | |
508 | ||
509 | bool wxComboBox::CanRedo() const | |
510 | { | |
511 | if (IsEditable()) | |
512 | return GetTextCtrl()->CanRedo(); | |
513 | else | |
514 | return false; | |
515 | } | |
516 | ||
517 | ||
518 | // ---------------------------------------------------------------------------- | |
519 | // wxStdComboBoxInputHandler | |
520 | // ---------------------------------------------------------------------------- | |
521 | ||
522 | wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler *inphand) | |
523 | : wxStdInputHandler(inphand) | |
524 | { | |
525 | } | |
526 | ||
527 | bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer *consumer, | |
528 | const wxKeyEvent& event, | |
529 | bool pressed) | |
530 | { | |
531 | if ( pressed ) | |
532 | { | |
533 | wxControlAction action; | |
534 | switch ( event.GetKeyCode() ) | |
535 | { | |
536 | case WXK_DOWN: | |
537 | action = wxACTION_COMBOBOX_POPUP; | |
538 | break; | |
539 | ||
540 | case WXK_ESCAPE: | |
541 | action = wxACTION_COMBOBOX_DISMISS; | |
542 | break; | |
543 | } | |
544 | ||
545 | if ( !action.IsEmpty() ) | |
546 | { | |
547 | consumer->PerformAction(action); | |
548 | ||
549 | return true; | |
550 | } | |
551 | } | |
552 | ||
553 | return wxStdInputHandler::HandleKey(consumer, event, pressed); | |
554 | } | |
555 | ||
556 | /* static */ | |
557 | wxInputHandler *wxComboBox::GetStdInputHandler(wxInputHandler *handlerDef) | |
558 | { | |
559 | static wxStdComboBoxInputHandler s_handler(handlerDef); | |
560 | ||
561 | return &s_handler; | |
562 | } | |
563 | ||
564 | #endif // wxUSE_COMBOBOX |