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