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