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