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