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