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