]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combobox.cpp
Fix wxGrid editors background painting.
[wxWidgets.git] / src / msw / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_COMBOBOX
28
29 #include "wx/combobox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 // for wxEVT_COMMAND_TEXT_ENTER
36 #include "wx/textctrl.h"
37 #include "wx/app.h"
38 #include "wx/brush.h"
39 #endif
40
41 #include "wx/clipbrd.h"
42 #include "wx/dynlib.h"
43 #include "wx/wupdlock.h"
44 #include "wx/msw/private.h"
45
46 #if wxUSE_UXTHEME
47 #include "wx/msw/uxtheme.h"
48 #endif
49
50 #if wxUSE_TOOLTIPS
51 #include "wx/tooltip.h"
52 #endif // wxUSE_TOOLTIPS
53
54 // ----------------------------------------------------------------------------
55 // wxWin macros
56 // ----------------------------------------------------------------------------
57
58 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
59 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
60 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
61 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
62 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
63 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
64 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
65 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
66
67 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
68 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
69 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
70 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
71 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
72 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
73 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
74 END_EVENT_TABLE()
75
76 // ----------------------------------------------------------------------------
77 // function prototypes
78 // ----------------------------------------------------------------------------
79
80 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
81 UINT message,
82 WPARAM wParam,
83 LPARAM lParam);
84
85 // ---------------------------------------------------------------------------
86 // global vars
87 // ---------------------------------------------------------------------------
88
89 // the pointer to standard radio button wnd proc
90 static WNDPROC gs_wndprocEdit = (WNDPROC)NULL;
91
92 // ============================================================================
93 // implementation
94 // ============================================================================
95
96 namespace
97 {
98
99 // Check if the given message should be forwarded from the edit control which
100 // is part of the combobox to wxComboBox itself. All messages generating the
101 // events that the code using wxComboBox could be interested in must be
102 // forwarded.
103 bool ShouldForwardFromEditToCombo(UINT message)
104 {
105 switch ( message )
106 {
107 case WM_KEYUP:
108 case WM_KEYDOWN:
109 case WM_CHAR:
110 case WM_SYSCHAR:
111 case WM_SYSKEYDOWN:
112 case WM_SYSKEYUP:
113 case WM_SETFOCUS:
114 case WM_KILLFOCUS:
115 case WM_CUT:
116 case WM_COPY:
117 case WM_PASTE:
118 return true;
119 }
120
121 return false;
122 }
123
124 } // anonymous namespace
125
126 // ----------------------------------------------------------------------------
127 // wnd proc for subclassed edit control
128 // ----------------------------------------------------------------------------
129
130 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
131 UINT message,
132 WPARAM wParam,
133 LPARAM lParam)
134 {
135 HWND hwndCombo = ::GetParent(hWnd);
136 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
137
138 if ( ShouldForwardFromEditToCombo(message) )
139 {
140 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
141 if ( !combo )
142 {
143 // we can get WM_KILLFOCUS while our parent is already half
144 // destroyed and hence doesn't look like a combobx any
145 // longer, check for it to avoid bogus assert failures
146 if ( !win->IsBeingDeleted() )
147 {
148 wxFAIL_MSG( wxT("should have combo as parent") );
149 }
150 }
151 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
152 {
153 // handled by parent
154 return 0;
155 }
156 }
157 else if ( message == WM_GETDLGCODE )
158 {
159 wxCHECK_MSG( win, 0, wxT("should have a parent") );
160
161 if ( win->GetWindowStyle() & wxTE_PROCESS_ENTER )
162 {
163 // need to return a custom dlg code or we'll never get it
164 return DLGC_WANTMESSAGE;
165 }
166 }
167
168 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxComboBox callbacks
173 // ----------------------------------------------------------------------------
174
175 WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
176 {
177 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
178 // set its colour correctly (to be the same as our own one)
179
180 switch ( nMsg )
181 {
182 case WM_SIZE:
183 // wxStaticBox can generate this message, when modifying the control's style.
184 // This causes the content of the combobox to be selected, for some reason.
185 case WM_STYLECHANGED:
186 {
187 // combobox selection sometimes spontaneously changes when its
188 // size changes, restore it to the old value if necessary
189 if ( !GetEditHWNDIfAvailable() )
190 break;
191
192 long fromOld, toOld;
193 GetSelection(&fromOld, &toOld);
194
195 // if an editable combobox has a not empty text not from the
196 // list, it tries to autocomplete it from the list when it is
197 // resized, but we don't want this to happen as it doesn't seem
198 // to make any sense, so we forcefully restore the old text
199 wxString textOld;
200 if ( !HasFlag(wxCB_READONLY) && GetCurrentSelection() == -1 )
201 textOld = GetValue();
202
203 // eliminate flickering during following hacks
204 wxWindowUpdateLocker lock(this);
205
206 WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam);
207
208 if ( !textOld.empty() && GetValue() != textOld )
209 SetLabel(textOld);
210
211 long fromNew, toNew;
212 GetSelection(&fromNew, &toNew);
213
214 if ( fromOld != fromNew || toOld != toNew )
215 {
216 SetSelection(fromOld, toOld);
217 }
218
219 return result;
220 }
221 }
222
223 return wxChoice::MSWWindowProc(nMsg, wParam, lParam);
224 }
225
226 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
227 {
228 switch ( msg )
229 {
230 case WM_CHAR:
231 // for compatibility with wxTextCtrl, generate a special message
232 // when Enter is pressed
233 if ( wParam == VK_RETURN )
234 {
235 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE, 0, 0))
236 return false;
237
238 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
239
240 const int sel = GetSelection();
241 event.SetInt(sel);
242 event.SetString(GetValue());
243 InitCommandEventWithItems(event, sel);
244
245 if ( ProcessCommand(event) )
246 {
247 // don't let the event through to the native control
248 // because it doesn't need it and may generate an annoying
249 // beep if it gets it
250 return true;
251 }
252 }
253 // fall through, WM_CHAR is one of the message we should forward.
254
255 default:
256 if ( ShouldForwardFromEditToCombo(msg) )
257 {
258 // For all the messages forward from the edit control the
259 // result is not used.
260 WXLRESULT result;
261 return MSWHandleMessage(&result, msg, wParam, lParam);
262 }
263 }
264
265 return false;
266 }
267
268 bool wxComboBox::MSWCommand(WXUINT param, WXWORD id)
269 {
270 int sel = -1;
271 wxString value;
272
273 switch ( param )
274 {
275 case CBN_DROPDOWN:
276 // remember the last selection, just as wxChoice does
277 m_lastAcceptedSelection = GetCurrentSelection();
278 if ( m_lastAcceptedSelection == -1 )
279 {
280 // but unlike with wxChoice we may have no selection but still
281 // have some text and we should avoid erasing it if the drop
282 // down is cancelled (see #8474)
283 m_lastAcceptedSelection = wxID_NONE;
284 }
285 {
286 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId());
287 event.SetEventObject(this);
288 ProcessCommand(event);
289 }
290 break;
291 case CBN_CLOSEUP:
292 {
293 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId());
294 event.SetEventObject(this);
295 ProcessCommand(event);
296 }
297 break;
298 case CBN_SELENDOK:
299 #ifndef __SMARTPHONE__
300 // we need to reset this to prevent the selection from being undone
301 // by wxChoice, see wxChoice::MSWCommand() and comments there
302 m_lastAcceptedSelection = wxID_NONE;
303 #endif
304
305 // set these variables so that they could be also fixed in
306 // CBN_EDITCHANGE below
307 sel = GetSelection();
308 value = GetStringSelection();
309
310 // this string is going to become the new combobox value soon but
311 // we need it to be done right now, otherwise the event handler
312 // could get a wrong value when it calls our GetValue()
313 ::SetWindowText(GetHwnd(), value.t_str());
314
315 {
316 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
317 event.SetInt(sel);
318 event.SetString(value);
319 InitCommandEventWithItems(event, sel);
320
321 ProcessCommand(event);
322 }
323
324 // fall through: for compability with wxGTK, also send the text
325 // update event when the selection changes (this also seems more
326 // logical as the text does change)
327
328 case CBN_EDITCHANGE:
329 if ( m_allowTextEvents )
330 {
331 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
332
333 // if sel != -1, value was already initialized above
334 if ( sel == -1 )
335 {
336 value = wxGetWindowText(GetHwnd());
337 }
338
339 event.SetString(value);
340 InitCommandEventWithItems(event, sel);
341
342 ProcessCommand(event);
343 }
344 break;
345
346 default:
347 return wxChoice::MSWCommand(param, id);
348 }
349
350 // skip wxChoice version as it would generate its own events for
351 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
352 return true;
353 }
354
355 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG *pMsg)
356 {
357 // prevent command accelerators from stealing editing
358 // hotkeys when we have the focus
359 if (wxIsCtrlDown())
360 {
361 WPARAM vkey = pMsg->wParam;
362
363 switch (vkey)
364 {
365 case 'C':
366 case 'V':
367 case 'X':
368 case VK_INSERT:
369 case VK_DELETE:
370 case VK_HOME:
371 case VK_END:
372 return false;
373 }
374 }
375
376 return wxChoice::MSWShouldPreProcessMessage(pMsg);
377 }
378
379 WXHWND wxComboBox::GetEditHWNDIfAvailable() const
380 {
381 #if wxUSE_DYNLIB_CLASS
382 #if defined(WINVER) && WINVER >= 0x0500
383 typedef BOOL (WINAPI *GetComboBoxInfo_t)(HWND, COMBOBOXINFO*);
384 static GetComboBoxInfo_t s_pfnGetComboBoxInfo = NULL;
385 static bool s_triedToLoad = false;
386 if ( !s_triedToLoad )
387 {
388 s_triedToLoad = true;
389 wxLoadedDLL dllUser32("user32.dll");
390 wxDL_INIT_FUNC(s_pfn, GetComboBoxInfo, dllUser32);
391 }
392
393 if ( s_pfnGetComboBoxInfo )
394 {
395 WinStruct<COMBOBOXINFO> info;
396 (*s_pfnGetComboBoxInfo)(GetHwnd(), &info);
397 return info.hwndItem;
398 }
399 #endif // WINVER >= 0x0500
400 #endif // wxUSE_DYNLIB_CLASS
401
402 if (HasFlag(wxCB_SIMPLE))
403 {
404 POINT pt;
405 pt.x = pt.y = 4;
406 return (WXHWND) ::ChildWindowFromPoint(GetHwnd(), pt);
407 }
408
409 // notice that a slightly safer alternative could be to use FindWindowEx()
410 // but it's not available under WinCE so just take the first child for now
411 // to keep one version of the code for all platforms and fix it later if
412 // problems are discovered
413
414 // we assume that the only child of the combobox is the edit window
415 return (WXHWND)::GetWindow(GetHwnd(), GW_CHILD);
416 }
417
418 WXHWND wxComboBox::GetEditHWND() const
419 {
420 // this function should not be called for wxCB_READONLY controls, it is
421 // the callers responsibility to check this
422 wxASSERT_MSG( !HasFlag(wxCB_READONLY),
423 wxT("read-only combobox doesn't have any edit control") );
424
425 WXHWND hWndEdit = GetEditHWNDIfAvailable();
426 wxASSERT_MSG( hWndEdit, wxT("combobox without edit control?") );
427
428 return hWndEdit;
429 }
430
431 wxWindow *wxComboBox::GetEditableWindow()
432 {
433 wxASSERT_MSG( !HasFlag(wxCB_READONLY),
434 wxT("read-only combobox doesn't have any edit control") );
435
436 return this;
437 }
438
439 // ----------------------------------------------------------------------------
440 // wxComboBox creation
441 // ----------------------------------------------------------------------------
442
443 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
444 const wxString& value,
445 const wxPoint& pos,
446 const wxSize& size,
447 int n, const wxString choices[],
448 long style,
449 const wxValidator& validator,
450 const wxString& name)
451 {
452 // pretend that wxComboBox is hidden while it is positioned and resized and
453 // show it only right before leaving this method because otherwise there is
454 // some noticeable flicker while the control rearranges itself
455 m_isShown = false;
456
457 if ( !CreateAndInit(parent, id, pos, size, n, choices, style,
458 validator, name) )
459 return false;
460
461 // we shouldn't call SetValue() for an empty string because this would
462 // (correctly) result in an assert with a read only combobox and is useless
463 // for the other ones anyhow
464 if ( !value.empty() )
465 SetValue(value);
466
467 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
468 // and an edit control inside it and if we want to catch events from this
469 // edit control, we must subclass it as well
470 if ( !(style & wxCB_READONLY) )
471 {
472 gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), wxComboEditWndProc);
473 }
474
475 // and finally, show the control
476 Show(true);
477
478 return true;
479 }
480
481 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
482 const wxString& value,
483 const wxPoint& pos,
484 const wxSize& size,
485 const wxArrayString& choices,
486 long style,
487 const wxValidator& validator,
488 const wxString& name)
489 {
490 wxCArrayString chs(choices);
491 return Create(parent, id, value, pos, size, chs.GetCount(),
492 chs.GetStrings(), style, validator, name);
493 }
494
495 WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
496 {
497 // we never have an external border
498 WXDWORD msStyle = wxChoice::MSWGetStyle
499 (
500 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
501 );
502
503 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
504 // created hidden (see Create() above), it is not done for us but we still
505 // want to have this style
506 msStyle |= WS_TABSTOP;
507
508 // remove the style always added by wxChoice
509 msStyle &= ~CBS_DROPDOWNLIST;
510
511 if ( style & wxCB_READONLY )
512 msStyle |= CBS_DROPDOWNLIST;
513 #ifndef __WXWINCE__
514 else if ( style & wxCB_SIMPLE )
515 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
516 #endif
517 else
518 msStyle |= CBS_DROPDOWN;
519
520 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
521 msStyle |= CBS_AUTOHSCROLL;
522
523 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
524
525 return msStyle;
526 }
527
528 // ----------------------------------------------------------------------------
529 // wxComboBox text control-like methods
530 // ----------------------------------------------------------------------------
531
532 wxString wxComboBox::GetValue() const
533 {
534 return HasFlag(wxCB_READONLY) ? GetStringSelection()
535 : wxTextEntry::GetValue();
536 }
537
538 void wxComboBox::SetValue(const wxString& value)
539 {
540 if ( HasFlag(wxCB_READONLY) )
541 SetStringSelection(value);
542 else
543 wxTextEntry::SetValue(value);
544 }
545
546 void wxComboBox::Clear()
547 {
548 wxChoice::Clear();
549 if ( !HasFlag(wxCB_READONLY) )
550 wxTextEntry::Clear();
551 }
552
553 bool wxComboBox::ContainsHWND(WXHWND hWnd) const
554 {
555 return hWnd == GetEditHWNDIfAvailable();
556 }
557
558 void wxComboBox::GetSelection(long *from, long *to) const
559 {
560 if ( !HasFlag(wxCB_READONLY) )
561 {
562 wxTextEntry::GetSelection(from, to);
563 }
564 else // text selection doesn't make sense for read only comboboxes
565 {
566 if ( from )
567 *from = -1;
568 if ( to )
569 *to = -1;
570 }
571 }
572
573 bool wxComboBox::IsEditable() const
574 {
575 return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable();
576 }
577
578 // ----------------------------------------------------------------------------
579 // standard event handling
580 // ----------------------------------------------------------------------------
581
582 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
583 {
584 Cut();
585 }
586
587 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
588 {
589 Copy();
590 }
591
592 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
593 {
594 Paste();
595 }
596
597 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
598 {
599 Undo();
600 }
601
602 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
603 {
604 Redo();
605 }
606
607 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
608 {
609 RemoveSelection();
610 }
611
612 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
613 {
614 SelectAll();
615 }
616
617 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
618 {
619 event.Enable( CanCut() );
620 }
621
622 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
623 {
624 event.Enable( CanCopy() );
625 }
626
627 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
628 {
629 event.Enable( CanPaste() );
630 }
631
632 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
633 {
634 event.Enable( IsEditable() && CanUndo() );
635 }
636
637 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
638 {
639 event.Enable( IsEditable() && CanRedo() );
640 }
641
642 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
643 {
644 event.Enable(IsEditable() && HasSelection());
645 }
646
647 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
648 {
649 event.Enable(IsEditable() && !wxTextEntry::IsEmpty());
650 }
651
652 #if wxUSE_TOOLTIPS
653
654 void wxComboBox::DoSetToolTip(wxToolTip *tip)
655 {
656 wxChoice::DoSetToolTip(tip);
657
658 if ( tip && !HasFlag(wxCB_READONLY) )
659 tip->AddOtherWindow(GetEditHWND());
660 }
661
662 #endif // wxUSE_TOOLTIPS
663
664 #if wxUSE_UXTHEME
665
666 bool wxComboBox::SetHint(const wxString& hintOrig)
667 {
668 wxString hint(hintOrig);
669 if ( wxUxThemeEngine::GetIfActive() )
670 {
671 // under XP (but not Vista) there is a bug in cue banners
672 // implementation for combobox edit control: the first character is
673 // partially chopped off, so prepend a space to make it fully visible
674 hint.insert(0, " ");
675 }
676
677 return wxTextEntry::SetHint(hint);
678 }
679
680 #endif // wxUSE_UXTHEME
681
682 #endif // wxUSE_COMBOBOX