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