Include wx/choice.h according to precompiled headers of wx/wx.h (with other minor...
[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, wxControl,"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, wxControl)
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 _T("this style flag is ignored by wxChoice, you ")
121 _T("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 // choice/combobox normally has "white" (depends on colour scheme, of
146 // course) background rather than inheriting the parent's background
147 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
148
149 // initialize the controls contents
150 for ( int i = 0; i < n; i++ )
151 {
152 Append(choices[i]);
153 }
154
155 // and now we may finally size the control properly (if needed)
156 SetBestSize(size);
157
158 return true;
159 }
160
161 bool wxChoice::Create(wxWindow *parent,
162 wxWindowID id,
163 const wxPoint& pos,
164 const wxSize& size,
165 const wxArrayString& choices,
166 long style,
167 const wxValidator& validator,
168 const wxString& name)
169 {
170 wxCArrayString chs(choices);
171 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
172 style, validator, name);
173 }
174
175 bool wxChoice::MSWShouldPreProcessMessage(WXMSG *pMsg)
176 {
177 MSG *msg = (MSG *) pMsg;
178
179 // if the dropdown list is visible, don't preprocess certain keys
180 if ( msg->message == WM_KEYDOWN
181 && (msg->wParam == VK_ESCAPE || msg->wParam == VK_RETURN) )
182 {
183 if (::SendMessage(GetHwndOf(this), CB_GETDROPPEDSTATE, 0, 0))
184 {
185 return false;
186 }
187 }
188
189 return wxControl::MSWShouldPreProcessMessage(pMsg);
190 }
191
192 WXDWORD wxChoice::MSWGetStyle(long style, WXDWORD *exstyle) const
193 {
194 // we never have an external border
195 WXDWORD msStyle = wxControl::MSWGetStyle
196 (
197 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
198 );
199
200 // WS_CLIPSIBLINGS is useful with wxChoice and doesn't seem to result in
201 // any problems
202 msStyle |= WS_CLIPSIBLINGS;
203
204 // wxChoice-specific styles
205 msStyle |= CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL;
206 if ( style & wxCB_SORT )
207 msStyle |= CBS_SORT;
208
209 return msStyle;
210 }
211
212 wxChoice::~wxChoice()
213 {
214 Free();
215 }
216
217 // ----------------------------------------------------------------------------
218 // adding/deleting items to/from the list
219 // ----------------------------------------------------------------------------
220
221 int wxChoice::DoAppend(const wxString& item)
222 {
223 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LPARAM)item.c_str());
224 if ( n == CB_ERR )
225 {
226 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
227 }
228 else // ok
229 {
230 // we need to refresh our size in order to have enough space for the
231 // newly added items
232 if ( !IsFrozen() )
233 UpdateVisibleHeight();
234 }
235
236 InvalidateBestSize();
237 return n;
238 }
239
240 int wxChoice::DoInsert(const wxString& item, unsigned int pos)
241 {
242 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
243 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
244
245 int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LPARAM)item.c_str());
246 if ( n == CB_ERR )
247 {
248 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
249 }
250 else // ok
251 {
252 if ( !IsFrozen() )
253 UpdateVisibleHeight();
254 }
255
256 InvalidateBestSize();
257 return n;
258 }
259
260 void wxChoice::Delete(unsigned int n)
261 {
262 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
263
264 if ( HasClientObjectData() )
265 {
266 delete GetClientObject(n);
267 }
268
269 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
270
271 if ( !IsFrozen() )
272 UpdateVisibleHeight();
273
274 InvalidateBestSize();
275 }
276
277 void wxChoice::Clear()
278 {
279 Free();
280
281 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
282
283 if ( !IsFrozen() )
284 UpdateVisibleHeight();
285
286 InvalidateBestSize();
287 }
288
289 void wxChoice::Free()
290 {
291 if ( HasClientObjectData() )
292 {
293 unsigned int count = GetCount();
294 for ( unsigned int n = 0; n < count; n++ )
295 {
296 delete GetClientObject(n);
297 }
298 }
299 }
300
301 // ----------------------------------------------------------------------------
302 // selection
303 // ----------------------------------------------------------------------------
304
305 int wxChoice::GetSelection() const
306 {
307 // if m_lastAcceptedSelection is set, it means that the dropdown is
308 // currently shown and that we want to use the last "permanent" selection
309 // instead of whatever is under the mouse pointer currently
310 //
311 // otherwise, get the selection from the control
312 return m_lastAcceptedSelection == wxID_NONE ? GetCurrentSelection()
313 : m_lastAcceptedSelection;
314 }
315
316 int wxChoice::GetCurrentSelection() const
317 {
318 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
319 }
320
321 void wxChoice::SetSelection(int n)
322 {
323 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
324 }
325
326 // ----------------------------------------------------------------------------
327 // string list functions
328 // ----------------------------------------------------------------------------
329
330 unsigned int wxChoice::GetCount() const
331 {
332 return (unsigned int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
333 }
334
335 int wxChoice::FindString(const wxString& s, bool bCase) const
336 {
337 #if defined(__WATCOMC__) && defined(__WIN386__)
338 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
339 // wxChoice::Do it the long way instead.
340 unsigned int count = GetCount();
341 for ( unsigned int i = 0; i < count; i++ )
342 {
343 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
344 if (GetString(i).IsSameAs(s, bCase))
345 return i;
346 }
347
348 return wxNOT_FOUND;
349 #else // !Watcom
350 //TODO: Evidently some MSW versions (all?) don't like empty strings
351 //passed to SendMessage, so we have to do it ourselves in that case
352 if ( s.empty() )
353 {
354 unsigned int count = GetCount();
355 for ( unsigned int i = 0; i < count; i++ )
356 {
357 if (GetString(i).empty())
358 return i;
359 }
360
361 return wxNOT_FOUND;
362 }
363 else if (bCase)
364 {
365 // back to base class search for not native search type
366 return wxItemContainerImmutable::FindString( s, bCase );
367 }
368 else
369 {
370 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
371 (WPARAM)-1, (LPARAM)s.c_str());
372
373 return pos == LB_ERR ? wxNOT_FOUND : pos;
374 }
375 #endif // Watcom/!Watcom
376 }
377
378 void wxChoice::SetString(unsigned int n, const wxString& s)
379 {
380 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::SetString") );
381
382 // we have to delete and add back the string as there is no way to change a
383 // string in place
384
385 // we need to preserve the client data
386 void *data;
387 if ( m_clientDataItemsType != wxClientData_None )
388 {
389 data = DoGetItemClientData(n);
390 }
391 else // no client data
392 {
393 data = NULL;
394 }
395
396 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
397 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() );
398
399 if ( data )
400 {
401 DoSetItemClientData(n, data);
402 }
403 //else: it's already NULL by default
404
405 InvalidateBestSize();
406 }
407
408 wxString wxChoice::GetString(unsigned int n) const
409 {
410 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
411
412 wxString str;
413 if ( len != CB_ERR && len > 0 )
414 {
415 if ( ::SendMessage
416 (
417 GetHwnd(),
418 CB_GETLBTEXT,
419 n,
420 (LPARAM)(wxChar *)wxStringBuffer(str, len)
421 ) == CB_ERR )
422 {
423 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
424 }
425 }
426
427 return str;
428 }
429
430 // ----------------------------------------------------------------------------
431 // client data
432 // ----------------------------------------------------------------------------
433
434 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
435 {
436 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
437 n, (LPARAM)clientData) == CB_ERR )
438 {
439 wxLogLastError(wxT("CB_SETITEMDATA"));
440 }
441 }
442
443 void* wxChoice::DoGetItemClientData(unsigned int n) const
444 {
445 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
446 if ( rc == CB_ERR )
447 {
448 wxLogLastError(wxT("CB_GETITEMDATA"));
449
450 // unfortunately, there is no way to return an error code to the user
451 rc = (LPARAM) NULL;
452 }
453
454 return (void *)rc;
455 }
456
457 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
458 {
459 DoSetItemClientData(n, clientData);
460 }
461
462 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
463 {
464 return (wxClientData *)DoGetItemClientData(n);
465 }
466
467 // ----------------------------------------------------------------------------
468 // wxMSW specific helpers
469 // ----------------------------------------------------------------------------
470
471 void wxChoice::UpdateVisibleHeight()
472 {
473 // be careful to not change the width here
474 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y, wxSIZE_USE_EXISTING);
475 }
476
477 void wxChoice::DoMoveWindow(int x, int y, int width, int height)
478 {
479 // here is why this is necessary: if the width is negative, the combobox
480 // window proc makes the window of the size width*height instead of
481 // interpreting height in the usual manner (meaning the height of the drop
482 // down list - usually the height specified in the call to MoveWindow()
483 // will not change the height of combo box per se)
484 //
485 // this behaviour is not documented anywhere, but this is just how it is
486 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
487 // the check, constraints/sizers using combos may break the height
488 // constraint will have not at all the same value as expected
489 if ( width < 0 )
490 return;
491
492 wxControl::DoMoveWindow(x, y, width, height);
493 }
494
495 void wxChoice::DoGetSize(int *w, int *h) const
496 {
497 // this is weird: sometimes, the height returned by Windows is clearly the
498 // total height of the control including the drop down list -- but only
499 // sometimes, and normally it isn't... I have no idea about what to do with
500 // this
501 wxControl::DoGetSize(w, h);
502 }
503
504 void wxChoice::DoSetSize(int x, int y,
505 int width, int height,
506 int sizeFlags)
507 {
508 // the height which we must pass to Windows should be the total height of
509 // the control including the drop down list while the height given to us
510 // is, of course, just the height of the permanently visible part of it
511 if ( height != wxDefaultCoord )
512 {
513 // don't make the drop down list too tall, arbitrarily limit it to 40
514 // items max and also don't leave it empty
515 size_t nItems = GetCount();
516 if ( !nItems )
517 nItems = 9;
518 else if ( nItems > 24 )
519 nItems = 24;
520
521 // add space for the drop down list
522 const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
523 height += hItem*(nItems + 1);
524 }
525 else
526 {
527 // We cannot pass wxDefaultCoord as height to wxControl. wxControl uses
528 // wxGetWindowRect() to determine the current height of the combobox,
529 // and then again sets the combobox's height to that value. Unfortunately,
530 // wxGetWindowRect doesn't include the dropdown list's height (at least
531 // on Win2K), so this would result in a combobox with dropdown height of
532 // 1 pixel. We have to determine the default height ourselves and call
533 // wxControl with that value instead.
534 int w, h;
535 RECT r;
536 DoGetSize(&w, &h);
537 if (::SendMessage(GetHwnd(), CB_GETDROPPEDCONTROLRECT, 0, (LPARAM) &r) != 0)
538 {
539 height = h + r.bottom - r.top;
540 }
541 }
542
543 wxControl::DoSetSize(x, y, width, height, sizeFlags);
544
545 // This solution works on XP, but causes choice/combobox lists to be
546 // too short on W2K and earlier.
547 #if 0
548 int widthCurrent, heightCurrent;
549 DoGetSize(&widthCurrent, &heightCurrent);
550
551 // the height which we must pass to Windows should be the total height of
552 // the control including the drop down list while the height given to us
553 // is, of course, just the height of the permanently visible part of it
554 if ( height != wxDefaultCoord && height != heightCurrent )
555 {
556 // don't make the drop down list too tall, arbitrarily limit it to 40
557 // items max and also don't leave it empty
558 unsigned int nItems = GetCount();
559 if ( !nItems )
560 nItems = 9;
561 else if ( nItems > 24 )
562 nItems = 24;
563
564 // add space for the drop down list
565 const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
566 height += hItem*(nItems + 1);
567 }
568 else // keep the same height as now
569 {
570 // normally wxWindow::DoSetSize() checks if we set the same size as the
571 // window already has and does nothing in this case, but for us the
572 // check fails as the size we pass to it includes the dropdown while
573 // the size returned by our GetSize() does not, so test if the size
574 // didn't really change ourselves here
575 if ( width == wxDefaultCoord || width == widthCurrent )
576 {
577 // size doesn't change, what about position?
578 int xCurrent, yCurrent;
579 DoGetPosition(&xCurrent, &yCurrent);
580 const bool defMeansUnchanged = !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE);
581 if ( ((x == wxDefaultCoord && defMeansUnchanged) || x == xCurrent)
582 &&
583 ((y == wxDefaultCoord && defMeansUnchanged) || y == yCurrent) )
584 {
585 // nothing changes, nothing to do
586 return;
587 }
588 }
589
590 // We cannot pass wxDefaultCoord as height to wxControl. wxControl uses
591 // wxGetWindowRect() to determine the current height of the combobox,
592 // and then again sets the combobox's height to that value. Unfortunately,
593 // wxGetWindowRect doesn't include the dropdown list's height (at least
594 // on Win2K), so this would result in a combobox with dropdown height of
595 // 1 pixel. We have to determine the default height ourselves and call
596 // wxControl with that value instead.
597 //
598 // Also notice that sometimes CB_GETDROPPEDCONTROLRECT seems to return
599 // wildly incorrect values (~32000) which looks like a bug in it, just
600 // ignore them in this case
601 RECT r;
602 if ( ::SendMessage(GetHwnd(), CB_GETDROPPEDCONTROLRECT, 0, (LPARAM) &r)
603 && r.bottom < 30000 )
604 {
605 height = heightCurrent + r.bottom - r.top;
606 }
607 }
608
609 wxControl::DoSetSize(x, y, width, height, sizeFlags);
610 #endif
611 }
612
613 wxSize wxChoice::DoGetBestSize() const
614 {
615 // find the widest string
616 int wChoice = 0;
617 const unsigned int nItems = GetCount();
618 for ( unsigned int i = 0; i < nItems; i++ )
619 {
620 int wLine;
621 GetTextExtent(GetString(i), &wLine, NULL);
622 if ( wLine > wChoice )
623 wChoice = wLine;
624 }
625
626 // give it some reasonable default value if there are no strings in the
627 // list
628 if ( wChoice == 0 )
629 wChoice = 100;
630
631 // the combobox should be slightly larger than the widest string
632 wChoice += 5*GetCharWidth();
633
634 wxSize best(wChoice, EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight()));
635 CacheBestSize(best);
636 return best;
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__)