Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / src / msw / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/choice.cpp
3 // Purpose: wxChoice
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to derive from wxChoiceBase
6 // Created: 04/01/98
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_CHOICE && !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
27
28 #include "wx/choice.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/utils.h"
32 #include "wx/app.h"
33 #include "wx/log.h"
34 #include "wx/brush.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/dynlib.h"
39
40 #include "wx/msw/private.h"
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // creation
48 // ----------------------------------------------------------------------------
49
50 bool wxChoice::Create(wxWindow *parent,
51 wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 int n, const wxString choices[],
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58 {
59 // Experience shows that wxChoice vs. wxComboBox distinction confuses
60 // quite a few people - try to help them
61 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
62 !(style & wxCB_READONLY) &&
63 !(style & wxCB_SIMPLE),
64 wxT("this style flag is ignored by wxChoice, you ")
65 wxT("probably want to use a wxComboBox") );
66
67 return CreateAndInit(parent, id, pos, size, n, choices, style,
68 validator, name);
69 }
70
71 bool wxChoice::CreateAndInit(wxWindow *parent,
72 wxWindowID id,
73 const wxPoint& pos,
74 const wxSize& size,
75 int n, const wxString choices[],
76 long style,
77 const wxValidator& validator,
78 const wxString& name)
79 {
80 // initialize wxControl
81 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
82 return false;
83
84 // now create the real HWND
85 if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString, pos, size) )
86 return false;
87
88
89 // initialize the controls contents
90 Append(n, choices);
91
92 // and now we may finally size the control properly (if needed)
93 SetInitialSize(size);
94
95 return true;
96 }
97
98 void wxChoice::SetLabel(const wxString& label)
99 {
100 if ( FindString(label) == wxNOT_FOUND )
101 {
102 // unless we explicitly do this here, CB_GETCURSEL will continue to
103 // return the index of the previously selected item which will result
104 // in wrongly replacing the value being set now with the previously
105 // value if the user simply opens and closes (without selecting
106 // anything) the combobox popup
107 SetSelection(-1);
108 }
109
110 wxChoiceBase::SetLabel(label);
111 }
112
113 bool wxChoice::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 const wxArrayString& choices,
118 long style,
119 const wxValidator& validator,
120 const wxString& name)
121 {
122 wxCArrayString chs(choices);
123 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
124 style, validator, name);
125 }
126
127 bool wxChoice::MSWShouldPreProcessMessage(WXMSG *pMsg)
128 {
129 MSG *msg = (MSG *) pMsg;
130
131 // if the dropdown list is visible, don't preprocess certain keys
132 if ( msg->message == WM_KEYDOWN
133 && (msg->wParam == VK_ESCAPE || msg->wParam == VK_RETURN) )
134 {
135 if (::SendMessage(GetHwndOf(this), CB_GETDROPPEDSTATE, 0, 0))
136 {
137 return false;
138 }
139 }
140
141 return wxControl::MSWShouldPreProcessMessage(pMsg);
142 }
143
144 WXDWORD wxChoice::MSWGetStyle(long style, WXDWORD *exstyle) const
145 {
146 // we never have an external border
147 WXDWORD msStyle = wxControl::MSWGetStyle
148 (
149 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
150 );
151
152 // WS_CLIPSIBLINGS is useful with wxChoice and doesn't seem to result in
153 // any problems
154 msStyle |= WS_CLIPSIBLINGS;
155
156 // wxChoice-specific styles
157 msStyle |= CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL;
158 if ( style & wxCB_SORT )
159 msStyle |= CBS_SORT;
160
161 return msStyle;
162 }
163
164 #ifndef EP_EDITTEXT
165 #define EP_EDITTEXT 1
166 #define ETS_NORMAL 1
167 #endif
168
169 wxVisualAttributes
170 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
171 {
172 // it is important to return valid values for all attributes from here,
173 // GetXXX() below rely on this
174 wxVisualAttributes attrs;
175
176 // FIXME: Use better dummy window?
177 wxWindow* wnd = wxTheApp->GetTopWindow();
178 if (!wnd)
179 return attrs;
180
181 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
182
183 // there doesn't seem to be any way to get the text colour using themes
184 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
185 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
186
187 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
188 attrs.colBg = wnd->MSWGetThemeColour(L"EDIT",
189 EP_EDITTEXT,
190 ETS_NORMAL,
191 ThemeColourBackground,
192 wxSYS_COLOUR_WINDOW);
193
194 return attrs;
195 }
196
197 wxChoice::~wxChoice()
198 {
199 Clear();
200 }
201
202 bool wxChoice::MSWGetComboBoxInfo(tagCOMBOBOXINFO* info) const
203 {
204 // TODO-Win9x: Get rid of this once we officially drop support for Win9x
205 // and just call the function directly.
206 #if wxUSE_DYNLIB_CLASS
207 typedef BOOL (WINAPI *GetComboBoxInfo_t)(HWND, tagCOMBOBOXINFO*);
208 static GetComboBoxInfo_t s_pfnGetComboBoxInfo = NULL;
209 static bool s_triedToLoad = false;
210 if ( !s_triedToLoad )
211 {
212 s_triedToLoad = true;
213 wxLoadedDLL dllUser32("user32.dll");
214 wxDL_INIT_FUNC(s_pfn, GetComboBoxInfo, dllUser32);
215 }
216
217 if ( s_pfnGetComboBoxInfo )
218 return (*s_pfnGetComboBoxInfo)(GetHwnd(), info) != 0;
219 #endif // wxUSE_DYNLIB_CLASS
220
221 return false;
222 }
223
224 // ----------------------------------------------------------------------------
225 // adding/deleting items to/from the list
226 // ----------------------------------------------------------------------------
227
228 int wxChoice::DoInsertItems(const wxArrayStringsAdapter& items,
229 unsigned int pos,
230 void **clientData, wxClientDataType type)
231 {
232 MSWAllocStorage(items, CB_INITSTORAGE);
233
234 const bool append = pos == GetCount();
235
236 // use CB_ADDSTRING when appending at the end to make sure the control is
237 // resorted if it has wxCB_SORT style
238 const unsigned msg = append ? CB_ADDSTRING : CB_INSERTSTRING;
239
240 if ( append )
241 pos = 0;
242
243 int n = wxNOT_FOUND;
244 const unsigned numItems = items.GetCount();
245 for ( unsigned i = 0; i < numItems; ++i )
246 {
247 n = MSWInsertOrAppendItem(pos, items[i], msg);
248 if ( n == wxNOT_FOUND )
249 return n;
250
251 if ( !append )
252 pos++;
253
254 AssignNewItemClientData(n, clientData, i, type);
255 }
256
257 // we need to refresh our size in order to have enough space for the
258 // newly added items
259 if ( !IsFrozen() )
260 MSWUpdateDropDownHeight();
261
262 InvalidateBestSize();
263
264 return n;
265 }
266
267 void wxChoice::DoDeleteOneItem(unsigned int n)
268 {
269 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
270
271 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
272
273 if ( !IsFrozen() )
274 MSWUpdateDropDownHeight();
275
276 InvalidateBestSize();
277 }
278
279 void wxChoice::DoClear()
280 {
281 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
282
283 if ( !IsFrozen() )
284 MSWUpdateDropDownHeight();
285
286 InvalidateBestSize();
287 }
288
289 // ----------------------------------------------------------------------------
290 // selection
291 // ----------------------------------------------------------------------------
292
293 int wxChoice::GetSelection() const
294 {
295 // if m_lastAcceptedSelection is set, it means that the dropdown is
296 // currently shown and that we want to use the last "permanent" selection
297 // instead of whatever is under the mouse pointer currently
298 //
299 // otherwise, get the selection from the control
300 return m_lastAcceptedSelection == wxID_NONE ? GetCurrentSelection()
301 : m_lastAcceptedSelection;
302 }
303
304 int wxChoice::GetCurrentSelection() const
305 {
306 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
307 }
308
309 void wxChoice::SetSelection(int n)
310 {
311 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
312 }
313
314 // ----------------------------------------------------------------------------
315 // string list functions
316 // ----------------------------------------------------------------------------
317
318 unsigned int wxChoice::GetCount() const
319 {
320 return (unsigned int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
321 }
322
323 int wxChoice::FindString(const wxString& s, bool bCase) const
324 {
325 #if defined(__WATCOMC__) && defined(__WIN386__)
326 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
327 // wxChoice::Do it the long way instead.
328 unsigned int count = GetCount();
329 for ( unsigned int i = 0; i < count; i++ )
330 {
331 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
332 if (GetString(i).IsSameAs(s, bCase))
333 return i;
334 }
335
336 return wxNOT_FOUND;
337 #else // !Watcom
338 //TODO: Evidently some MSW versions (all?) don't like empty strings
339 //passed to SendMessage, so we have to do it ourselves in that case
340 if ( s.empty() )
341 {
342 unsigned int count = GetCount();
343 for ( unsigned int i = 0; i < count; i++ )
344 {
345 if (GetString(i).empty())
346 return i;
347 }
348
349 return wxNOT_FOUND;
350 }
351 else if (bCase)
352 {
353 // back to base class search for not native search type
354 return wxItemContainerImmutable::FindString( s, bCase );
355 }
356 else
357 {
358 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
359 (WPARAM)-1, wxMSW_CONV_LPARAM(s));
360
361 return pos == LB_ERR ? wxNOT_FOUND : pos;
362 }
363 #endif // Watcom/!Watcom
364 }
365
366 void wxChoice::SetString(unsigned int n, const wxString& s)
367 {
368 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::SetString") );
369
370 // we have to delete and add back the string as there is no way to change a
371 // string in place
372
373 // we need to preserve the client data manually
374 void *oldData = NULL;
375 wxClientData *oldObjData = NULL;
376 if ( HasClientUntypedData() )
377 oldData = GetClientData(n);
378 else if ( HasClientObjectData() )
379 oldObjData = GetClientObject(n);
380
381 // and also the selection if we're going to delete the item that was
382 // selected
383 const bool wasSelected = static_cast<int>(n) == GetSelection();
384
385 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
386 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, wxMSW_CONV_LPARAM(s) );
387
388 // restore the client data
389 if ( oldData )
390 SetClientData(n, oldData);
391 else if ( oldObjData )
392 SetClientObject(n, oldObjData);
393
394 // and the selection
395 if ( wasSelected )
396 SetSelection(n);
397
398 // the width could have changed so the best size needs to be recomputed
399 InvalidateBestSize();
400 }
401
402 wxString wxChoice::GetString(unsigned int n) const
403 {
404 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
405
406 wxString str;
407 if ( len != CB_ERR && len > 0 )
408 {
409 if ( ::SendMessage
410 (
411 GetHwnd(),
412 CB_GETLBTEXT,
413 n,
414 (LPARAM)(wxChar *)wxStringBuffer(str, len)
415 ) == CB_ERR )
416 {
417 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
418 }
419 }
420
421 return str;
422 }
423
424 // ----------------------------------------------------------------------------
425 // client data
426 // ----------------------------------------------------------------------------
427
428 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
429 {
430 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
431 n, (LPARAM)clientData) == CB_ERR )
432 {
433 wxLogLastError(wxT("CB_SETITEMDATA"));
434 }
435 }
436
437 void* wxChoice::DoGetItemClientData(unsigned int n) const
438 {
439 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
440 if ( rc == CB_ERR && GetLastError() != ERROR_SUCCESS )
441 {
442 wxLogLastError(wxT("CB_GETITEMDATA"));
443
444 // unfortunately, there is no way to return an error code to the user
445 rc = (LPARAM) NULL;
446 }
447
448 return (void *)rc;
449 }
450
451 // ----------------------------------------------------------------------------
452 // wxMSW-specific geometry management
453 // ----------------------------------------------------------------------------
454
455 namespace
456 {
457
458 // there is a difference between the height passed to CB_SETITEMHEIGHT and the
459 // real height of the combobox; it is probably not constant for all Windows
460 // versions/settings but right now I don't know how to find what it is so it is
461 // temporarily hardcoded to its value under XP systems with normal fonts sizes
462 const int COMBO_HEIGHT_ADJ = 6;
463
464 } // anonymous namespace
465
466 void wxChoice::MSWUpdateVisibleHeight()
467 {
468 if ( m_heightOwn != wxDefaultCoord )
469 {
470 ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT,
471 (WPARAM)-1, m_heightOwn - COMBO_HEIGHT_ADJ);
472 }
473 }
474
475 #if wxUSE_DEFERRED_SIZING
476 void wxChoice::MSWEndDeferWindowPos()
477 {
478 // we can only set the height of the choice itself now as it is reset to
479 // default every time the control is resized
480 MSWUpdateVisibleHeight();
481
482 wxChoiceBase::MSWEndDeferWindowPos();
483 }
484 #endif // wxUSE_DEFERRED_SIZING
485
486 void wxChoice::MSWUpdateDropDownHeight()
487 {
488 // be careful to not change the width here
489 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y,
490 wxSIZE_USE_EXISTING);
491 }
492
493 void wxChoice::DoMoveWindow(int x, int y, int width, int height)
494 {
495 // here is why this is necessary: if the width is negative, the combobox
496 // window proc makes the window of the size width*height instead of
497 // interpreting height in the usual manner (meaning the height of the drop
498 // down list - usually the height specified in the call to MoveWindow()
499 // will not change the height of combo box per se)
500 //
501 // this behaviour is not documented anywhere, but this is just how it is
502 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
503 // the check, constraints/sizers using combos may break the height
504 // constraint will have not at all the same value as expected
505 if ( width < 0 )
506 return;
507
508 wxControl::DoMoveWindow(x, y, width, height);
509 }
510
511 void wxChoice::DoGetSize(int *w, int *h) const
512 {
513 wxControl::DoGetSize(w, h);
514
515 // this is weird: sometimes, the height returned by Windows is clearly the
516 // total height of the control including the drop down list -- but only
517 // sometimes, and sometimes it isn't so work around this here by using our
518 // own stored value if we have it
519 if ( h && m_heightOwn != wxDefaultCoord )
520 *h = m_heightOwn;
521 }
522
523 void wxChoice::DoSetSize(int x, int y,
524 int width, int height,
525 int sizeFlags)
526 {
527 const int heightBest = GetBestSize().y;
528
529 // we need the real height below so get the current one if it's not given
530 if ( height == wxDefaultCoord )
531 {
532 // height not specified, use the same as before
533 DoGetSize(NULL, &height);
534 }
535 else if ( height == heightBest )
536 {
537 // we don't need to manually manage our height, let the system use the
538 // default one
539 m_heightOwn = wxDefaultCoord;
540 }
541 else // non-default height specified
542 {
543 // set our new own height but be careful not to make it too big: the
544 // native control apparently stores it as a single byte and so setting
545 // own height to 256 pixels results in default height being used (255
546 // is still ok)
547 m_heightOwn = height;
548
549 if ( m_heightOwn > UCHAR_MAX )
550 m_heightOwn = UCHAR_MAX;
551 // nor too small: see MSWUpdateVisibleHeight()
552 else if ( m_heightOwn < COMBO_HEIGHT_ADJ )
553 m_heightOwn = COMBO_HEIGHT_ADJ;
554 }
555
556
557 // the height which we must pass to Windows should be the total height of
558 // the control including the drop down list while the height given to us
559 // is, of course, just the height of the permanently visible part of it so
560 // add the drop down height to it
561
562 // don't make the drop down list too tall, arbitrarily limit it to 30
563 // items max and also don't make it too small if it's currently empty
564 size_t nItems = GetCount();
565 if (!HasFlag(wxCB_SIMPLE))
566 {
567 if ( !nItems )
568 nItems = 9;
569 else if ( nItems > 30 )
570 nItems = 30;
571 }
572
573 const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
574 int heightWithItems = 0;
575 if (!HasFlag(wxCB_SIMPLE))
576 // The extra item (" + 1") is required to prevent a vertical
577 // scrollbar from appearing with comctl32.dll versions earlier
578 // than 6.0 (such as found in Win2k).
579 heightWithItems = height + hItem*(nItems + 1);
580 else
581 heightWithItems = SetHeightSimpleComboBox(nItems);
582
583
584 // do resize the native control
585 wxControl::DoSetSize(x, y, width, heightWithItems, sizeFlags);
586
587
588 // make the control itself of the requested height: notice that this
589 // must be done after changing its size or it has no effect (apparently
590 // the height is reset to default during the control layout) and that it's
591 // useless to do it when using the deferred sizing -- in this case it
592 // will be done from MSWEndDeferWindowPos()
593 #if wxUSE_DEFERRED_SIZING
594 if ( m_pendingSize == wxDefaultSize )
595 {
596 // not using deferred sizing, update it immediately
597 MSWUpdateVisibleHeight();
598 }
599 else // in the middle of deferred sizing
600 {
601 // we need to report the size of the visible part of the control back
602 // in GetSize() and not height stored by DoSetSize() in m_pendingSize
603 m_pendingSize = wxSize(width, height);
604 }
605 #else // !wxUSE_DEFERRED_SIZING
606 // always update the visible height immediately
607 MSWUpdateVisibleHeight();
608 #endif // wxUSE_DEFERRED_SIZING
609 }
610
611 wxSize wxChoice::DoGetBestSize() const
612 {
613 // The base version returns the size of the largest string
614 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x);
615 }
616
617 int wxChoice::SetHeightSimpleComboBox(int nItems) const
618 {
619 int cx, cy;
620 wxGetCharSize( GetHWND(), &cx, &cy, GetFont() );
621 int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0);
622 return EDIT_HEIGHT_FROM_CHAR_HEIGHT( cy ) * wxMin( wxMax( nItems, 3 ), 6 ) + hItem - 1;
623 }
624
625 wxSize wxChoice::DoGetSizeFromTextSize(int xlen, int ylen) const
626 {
627 int cHeight = GetCharHeight();
628
629 // We are interested in the difference of sizes between the whole control
630 // and its child part. I.e. arrow, separators, etc.
631 wxSize tsize(xlen, 0);
632
633 // FIXME-VC6: Only VC6 needs this guard, see WINVER definition in
634 // include/wx/msw/wrapwin.h
635 #if defined(WINVER) && WINVER >= 0x0500
636 WinStruct<COMBOBOXINFO> info;
637 if ( MSWGetComboBoxInfo(&info) )
638 {
639 tsize.x += info.rcItem.left + info.rcButton.right - info.rcItem.right
640 + info.rcItem.left + 3; // right and extra margins
641 }
642 else // Just use some rough approximation.
643 #endif // WINVER >= 0x0500
644 {
645 tsize.x += 4*cHeight;
646 }
647
648 // set height on our own
649 if( HasFlag( wxCB_SIMPLE ) )
650 tsize.y = SetHeightSimpleComboBox(GetCount());
651 else
652 tsize.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cHeight);
653
654 // Perhaps the user wants something different from CharHeight
655 if ( ylen > 0 )
656 tsize.IncBy(0, ylen - cHeight);
657
658 return tsize;
659 }
660
661 // ----------------------------------------------------------------------------
662 // Popup operations
663 // ----------------------------------------------------------------------------
664
665 void wxChoice::MSWDoPopupOrDismiss(bool show)
666 {
667 wxASSERT_MSG( !HasFlag(wxCB_SIMPLE),
668 wxT("can't popup/dismiss the list for simple combo box") );
669
670 // we *must* set focus to the combobox before showing or hiding the drop
671 // down as without this we get WM_LBUTTONDOWN messages with invalid HWND
672 // when hiding it (whether programmatically or manually) resulting in a
673 // crash when we pass them to IsDialogMessage()
674 //
675 // this can be seen in the combo page of the widgets sample under Windows 7
676 SetFocus();
677
678 ::SendMessage(GetHwnd(), CB_SHOWDROPDOWN, show, 0);
679 }
680
681 bool wxChoice::Show(bool show)
682 {
683 if ( !wxChoiceBase::Show(show) )
684 return false;
685
686 // When hiding the combobox, we also need to hide its popup part as it
687 // doesn't happen automatically.
688 if ( !show && ::SendMessage(GetHwnd(), CB_GETDROPPEDSTATE, 0, 0) )
689 MSWDoPopupOrDismiss(false);
690
691 return true;
692 }
693
694 // ----------------------------------------------------------------------------
695 // MSW message handlers
696 // ----------------------------------------------------------------------------
697
698 WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
699 {
700 switch ( nMsg )
701 {
702 case WM_LBUTTONUP:
703 {
704 int x = (int)LOWORD(lParam);
705 int y = (int)HIWORD(lParam);
706
707 // Ok, this is truly weird, but if a panel with a wxChoice
708 // loses the focus, then you get a *fake* WM_LBUTTONUP message
709 // with x = 65535 and y = 65535. Filter out this nonsense.
710 //
711 // VZ: I'd like to know how to reproduce this please...
712 if ( x == 65535 && y == 65535 )
713 return 0;
714 }
715 break;
716
717 // we have to handle both: one for the normal case and the other
718 // for readonly
719 case WM_CTLCOLOREDIT:
720 case WM_CTLCOLORLISTBOX:
721 case WM_CTLCOLORSTATIC:
722 {
723 WXHDC hdc;
724 WXHWND hwnd;
725 UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
726
727 WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd);
728 if ( hbr )
729 return (WXLRESULT)hbr;
730 //else: fall through to default window proc
731 }
732 }
733
734 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
735 }
736
737 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
738 {
739 /*
740 The native control provides a great variety in the events it sends in
741 the different selection scenarios (undoubtedly for greater amusement of
742 the programmers using it). Here are the different cases:
743
744 A. Selecting with just the arrows without opening the dropdown:
745 1. CBN_SELENDOK
746 2. CBN_SELCHANGE
747
748 B. Opening dropdown with F4 and selecting with arrows:
749 1. CBN_DROPDOWN
750 2. many CBN_SELCHANGE while changing selection in the list
751 3. CBN_SELENDOK
752 4. CBN_CLOSEUP
753
754 C. Selecting with the mouse:
755 1. CBN_DROPDOWN
756 -- no intermediate CBN_SELCHANGEs --
757 2. CBN_SELENDOK
758 3. CBN_CLOSEUP
759 4. CBN_SELCHANGE
760
761 Admire the different order of messages in all of those cases, it must
762 surely have taken a lot of effort to Microsoft developers to achieve
763 such originality.
764
765 Additionally, notice that CBN_SELENDCANCEL doesn't seem to actually
766 cancel anything, if we get CBN_SELCHANGE before it, as it happens in
767 the case (B), the selection is still accepted. This doesn't make much
768 sense and directly contradicts MSDN documentation but is how the native
769 comboboxes behave and so we do the same thing.
770 */
771 switch ( param )
772 {
773 case CBN_DROPDOWN:
774 // we use this value both because we don't want to track selection
775 // using CB_GETCURSEL while the dropdown is opened and because we
776 // need to reset the selection back to it if it's eventually
777 // cancelled by user
778 m_lastAcceptedSelection = GetCurrentSelection();
779 break;
780
781 case CBN_CLOSEUP:
782 if ( m_pendingSelection != wxID_NONE )
783 {
784 // This can only happen in the case (B), so set the item
785 // selected in the drop down as our real selection.
786 SendSelectionChangedEvent(wxEVT_CHOICE);
787 m_pendingSelection = wxID_NONE;
788 }
789 break;
790
791 case CBN_SELENDOK:
792 // Reset the variables to prevent CBN_CLOSEUP from doing anything,
793 // it's not needed if we do get CBN_SELENDOK.
794 m_lastAcceptedSelection =
795 m_pendingSelection = wxID_NONE;
796
797 SendSelectionChangedEvent(wxEVT_CHOICE);
798 break;
799
800 case CBN_SELCHANGE:
801 // If we get this event after CBN_SELENDOK, i.e. cases (A) or (C)
802 // above, we don't have anything to do. But in the case (B) we need
803 // to remember that the selection should really change once the
804 // drop down is closed.
805 if ( m_lastAcceptedSelection != wxID_NONE )
806 m_pendingSelection = GetCurrentSelection();
807 break;
808
809 case CBN_SELENDCANCEL:
810 // Do not reset m_pendingSelection here -- it would make sense but,
811 // as described above, native controls keep the selection even when
812 // closing the drop down by pressing Escape or TAB, so conform to
813 // their behaviour.
814 m_lastAcceptedSelection = wxID_NONE;
815 break;
816
817 default:
818 return false;
819 }
820
821 return true;
822 }
823
824 WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)
825 {
826 if ( !IsThisEnabled() )
827 return MSWControlColorDisabled(hDC);
828
829 return wxChoiceBase::MSWControlColor(hDC, hWnd);
830 }
831
832 #endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__)