]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
Enclose code using m_macToolbar in #if wxOSX_USE_NATIVE_TOOLBAR.
[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
6a89f9ee 41#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
42WX_DEFINE_FLAGS( wxChoiceStyle )
43
3ff066a4 44wxBEGIN_FLAGS( wxChoiceStyle )
bc9fb572
JS
45 // new style border flags, we put them first to
46 // use them for streaming out
3ff066a4
SC
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)
3dfb79a6 53
bc9fb572 54 // old style border flags
3ff066a4
SC
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)
cb0afb26 60 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
61
62 // standard window styles
3ff066a4
SC
63 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
64 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
65 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
66 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 67 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
68 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
69 wxFLAGS_MEMBER(wxVSCROLL)
70 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 71
3ff066a4 72wxEND_FLAGS( wxChoiceStyle )
bc9fb572 73
0ad2a19e 74IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControlWithItems,"wx/choice.h")
6a89f9ee 75
3ff066a4 76wxBEGIN_PROPERTIES_TABLE(wxChoice)
3dfb79a6 77 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_CHOICE_SELECTED , wxCommandEvent )
c5ca409b 78
af498247 79 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 80 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3dfb79a6 81 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 82 wxPROPERTY_FLAGS( WindowStyle , wxChoiceStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 83wxEND_PROPERTIES_TABLE()
6a89f9ee 84
3ff066a4
SC
85wxBEGIN_HANDLERS_TABLE(wxChoice)
86wxEND_HANDLERS_TABLE()
2bda0e17 87
3dfb79a6 88wxCONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 89#else
0ad2a19e 90IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
6a89f9ee 91#endif
066f1b7a 92/*
3dfb79a6
VZ
93 TODO PROPERTIES
94 selection (long)
95 content (list)
96 item
066f1b7a
SC
97*/
98
8d99be5f
VZ
99// ============================================================================
100// implementation
101// ============================================================================
102
103// ----------------------------------------------------------------------------
104// creation
105// ----------------------------------------------------------------------------
106
107bool 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)
2bda0e17 115{
f6bcfd97 116 // Experience shows that wxChoice vs. wxComboBox distinction confuses
8d99be5f
VZ
117 // quite a few people - try to help them
118 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
119 !(style & wxCB_READONLY) &&
120 !(style & wxCB_SIMPLE),
9a83f860
VZ
121 wxT("this style flag is ignored by wxChoice, you ")
122 wxT("probably want to use a wxComboBox") );
2bda0e17 123
71e57cd6
VZ
124 return CreateAndInit(parent, id, pos, size, n, choices, style,
125 validator, name);
126}
127
cc61d2eb
VZ
128bool wxChoice::CreateAndInit(wxWindow *parent,
129 wxWindowID id,
71e57cd6 130 const wxPoint& pos,
3dfb79a6 131 const wxSize& size,
71e57cd6
VZ
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) )
02b7b6b0 139 return false;
2bda0e17 140
71e57cd6 141 // now create the real HWND
f31a4098 142 if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString, pos, size) )
02b7b6b0 143 return false;
71e57cd6
VZ
144
145
cc61d2eb 146 // initialize the controls contents
8d99be5f
VZ
147 for ( int i = 0; i < n; i++ )
148 {
149 Append(choices[i]);
150 }
2bda0e17 151
cc61d2eb 152 // and now we may finally size the control properly (if needed)
170acdc9 153 SetInitialSize(size);
cc61d2eb 154
02b7b6b0 155 return true;
2bda0e17
KB
156}
157
5de69dd3
VZ
158void 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
584ad2a3
MB
173bool 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
4b17d2e3
DS
187bool wxChoice::MSWShouldPreProcessMessage(WXMSG *pMsg)
188{
189 MSG *msg = (MSG *) pMsg;
190
dc302518
DS
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) )
4b17d2e3
DS
194 {
195 if (::SendMessage(GetHwndOf(this), CB_GETDROPPEDSTATE, 0, 0))
196 {
197 return false;
198 }
199 }
200
201 return wxControl::MSWShouldPreProcessMessage(pMsg);
202}
203
71e57cd6
VZ
204WXDWORD 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
2ec335db
JS
224#ifndef EP_EDITTEXT
225 #define EP_EDITTEXT 1
226 #define ETS_NORMAL 1
227#endif
228
229wxVisualAttributes
230wxChoice::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();
8c0f0784
JS
238 if (!wnd)
239 return attrs;
2ec335db
JS
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
8ee9d618
VZ
257wxChoice::~wxChoice()
258{
a236aa20 259 Clear();
8ee9d618
VZ
260}
261
8d99be5f
VZ
262// ----------------------------------------------------------------------------
263// adding/deleting items to/from the list
264// ----------------------------------------------------------------------------
2bda0e17 265
a236aa20
VZ
266int wxChoice::DoInsertItems(const wxArrayStringsAdapter& items,
267 unsigned int pos,
268 void **clientData, wxClientDataType type)
8d99be5f 269{
a236aa20 270 MSWAllocStorage(items, CB_INITSTORAGE);
def6fb9b 271
a236aa20 272 const bool append = pos == GetCount();
2bda0e17 273
a236aa20
VZ
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;
243dbf1a 277
a236aa20
VZ
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 )
71e57cd6 284 {
a236aa20
VZ
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);
71e57cd6 293 }
243dbf1a 294
a236aa20
VZ
295 // we need to refresh our size in order to have enough space for the
296 // newly added items
297 if ( !IsFrozen() )
75004dfb 298 MSWUpdateDropDownHeight();
a236aa20 299
31582e4e 300 InvalidateBestSize();
a236aa20 301
243dbf1a
VZ
302 return n;
303}
304
a236aa20 305void wxChoice::DoDeleteOneItem(unsigned int n)
2bda0e17 306{
8228b893 307 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
8d99be5f
VZ
308
309 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
71e57cd6 310
e0e3a32d 311 if ( !IsFrozen() )
75004dfb 312 MSWUpdateDropDownHeight();
31582e4e
RD
313
314 InvalidateBestSize();
2bda0e17
KB
315}
316
a236aa20 317void wxChoice::DoClear()
8ee9d618 318{
8ee9d618 319 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
71e57cd6 320
e0e3a32d 321 if ( !IsFrozen() )
75004dfb 322 MSWUpdateDropDownHeight();
31582e4e
RD
323
324 InvalidateBestSize();
8ee9d618
VZ
325}
326
8d99be5f
VZ
327// ----------------------------------------------------------------------------
328// selection
329// ----------------------------------------------------------------------------
2bda0e17 330
8d99be5f 331int wxChoice::GetSelection() const
6ba93d23
VZ
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
342int wxChoice::GetCurrentSelection() const
2bda0e17 343{
8d99be5f 344 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
2bda0e17
KB
345}
346
debe6624 347void wxChoice::SetSelection(int n)
2bda0e17 348{
8d99be5f
VZ
349 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
350}
351
352// ----------------------------------------------------------------------------
353// string list functions
354// ----------------------------------------------------------------------------
355
aa61d352 356unsigned int wxChoice::GetCount() const
8d99be5f 357{
aa61d352 358 return (unsigned int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
2bda0e17
KB
359}
360
11e62fe6 361int wxChoice::FindString(const wxString& s, bool bCase) const
2bda0e17
KB
362{
363#if defined(__WATCOMC__) && defined(__WIN386__)
8d99be5f
VZ
364 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
365 // wxChoice::Do it the long way instead.
aa61d352
VZ
366 unsigned int count = GetCount();
367 for ( unsigned int i = 0; i < count; i++ )
8d99be5f
VZ
368 {
369 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
aa61d352 370 if (GetString(i).IsSameAs(s, bCase))
8d99be5f
VZ
371 return i;
372 }
373
374 return wxNOT_FOUND;
375#else // !Watcom
4cd1ed99
RN
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
beedefb9 378 if ( s.empty() )
4cd1ed99 379 {
aa61d352
VZ
380 unsigned int count = GetCount();
381 for ( unsigned int i = 0; i < count; i++ )
11e62fe6 382 {
aa61d352 383 if (GetString(i).empty())
11e62fe6
WS
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 );
4cd1ed99
RN
393 }
394 else
395 {
11e62fe6 396 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
c9f78968 397 (WPARAM)-1, (LPARAM)s.wx_str());
f31a4098 398
11e62fe6 399 return pos == LB_ERR ? wxNOT_FOUND : pos;
4cd1ed99 400 }
8d99be5f 401#endif // Watcom/!Watcom
2bda0e17
KB
402}
403
aa61d352 404void wxChoice::SetString(unsigned int n, const wxString& s)
6c8a980f 405{
8228b893 406 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::SetString") );
2b5f62a0
VZ
407
408 // we have to delete and add back the string as there is no way to change a
409 // string in place
410
b4a11fe8
VZ
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);
2b5f62a0
VZ
418
419 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
c9f78968 420 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.wx_str() );
2b5f62a0 421
b4a11fe8
VZ
422 // restore the client data
423 if ( oldData )
424 SetClientData(n, oldData);
425 else if ( oldObjData )
426 SetClientObject(n, oldObjData);
31582e4e
RD
427
428 InvalidateBestSize();
6c8a980f
VZ
429}
430
aa61d352 431wxString wxChoice::GetString(unsigned int n) const
2bda0e17 432{
478cabab
VZ
433 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
434
6c8a980f 435 wxString str;
478cabab
VZ
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 {
f6bcfd97 446 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
21d72d17 447 }
4438caf4 448 }
2bda0e17 449
4438caf4
VZ
450 return str;
451}
2bda0e17 452
8d99be5f
VZ
453// ----------------------------------------------------------------------------
454// client data
455// ----------------------------------------------------------------------------
456
aa61d352 457void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
8d99be5f 458{
2b5f62a0
VZ
459 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
460 n, (LPARAM)clientData) == CB_ERR )
8d99be5f 461 {
223d09f6 462 wxLogLastError(wxT("CB_SETITEMDATA"));
8d99be5f
VZ
463 }
464}
465
aa61d352 466void* wxChoice::DoGetItemClientData(unsigned int n) const
8d99be5f
VZ
467{
468 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
469 if ( rc == CB_ERR )
470 {
223d09f6 471 wxLogLastError(wxT("CB_GETITEMDATA"));
8d99be5f
VZ
472
473 // unfortunately, there is no way to return an error code to the user
8ee9d618 474 rc = (LPARAM) NULL;
8d99be5f
VZ
475 }
476
477 return (void *)rc;
478}
479
8d99be5f 480// ----------------------------------------------------------------------------
75004dfb 481// wxMSW-specific geometry management
8d99be5f
VZ
482// ----------------------------------------------------------------------------
483
5d4ee50b
VZ
484namespace
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
491const int COMBO_HEIGHT_ADJ = 6;
492
493} // anonymous namespace
494
75004dfb
VZ
495void wxChoice::MSWUpdateVisibleHeight()
496{
497 if ( m_heightOwn != wxDefaultCoord )
5d4ee50b
VZ
498 {
499 ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT,
500 (WPARAM)-1, m_heightOwn - COMBO_HEIGHT_ADJ);
501 }
75004dfb
VZ
502}
503
504#if wxUSE_DEFERRED_SIZING
505void 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
515void wxChoice::MSWUpdateDropDownHeight()
71e57cd6 516{
e6968367 517 // be careful to not change the width here
75004dfb
VZ
518 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y,
519 wxSIZE_USE_EXISTING);
71e57cd6
VZ
520}
521
18c50997
VZ
522void 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
d99957b6
VZ
540void wxChoice::DoGetSize(int *w, int *h) const
541{
5d4ee50b
VZ
542 wxControl::DoGetSize(w, h);
543
94a77ff7
VZ
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
5d4ee50b
VZ
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;
d99957b6
VZ
550}
551
4438caf4 552void wxChoice::DoSetSize(int x, int y,
d99957b6 553 int width, int height,
4438caf4
VZ
554 int sizeFlags)
555{
5d4ee50b
VZ
556 // we need the real height below so get the current one if it's not given
557 if ( height != wxDefaultCoord && height != GetBestSize().y )
d4445d24 558 {
75004dfb
VZ
559 // set our new own height but be careful not to make it too big: the
560 // native control apparently stores it as a single byte and so setting
561 // own height to 256 pixels results in default height being used (255
562 // is still ok)
75004dfb
VZ
563 m_heightOwn = height;
564
5d4ee50b
VZ
565 if ( m_heightOwn > UCHAR_MAX )
566 m_heightOwn = UCHAR_MAX;
567 // nor too small: see MSWUpdateVisibleHeight()
568 else if ( m_heightOwn < COMBO_HEIGHT_ADJ )
569 m_heightOwn = COMBO_HEIGHT_ADJ;
d4445d24 570 }
5d4ee50b 571 else // height not specified
d4445d24 572 {
5d4ee50b 573 DoGetSize(NULL, &height);
d4445d24
JS
574 }
575
5d4ee50b
VZ
576
577 // the height which we must pass to Windows should be the total height of
578 // the control including the drop down list while the height given to us
579 // is, of course, just the height of the permanently visible part of it so
580 // add the drop down height to it
581
582 // don't make the drop down list too tall, arbitrarily limit it to 30
583 // items max and also don't make it too small if it's currently empty
584 size_t nItems = GetCount();
da89830a
JS
585 if (!HasFlag(wxCB_SIMPLE))
586 {
587 if ( !nItems )
588 nItems = 9;
589 else if ( nItems > 30 )
590 nItems = 30;
591 }
5d4ee50b
VZ
592
593 const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
da89830a
JS
594 int heightWithItems = 0;
595 if (!HasFlag(wxCB_SIMPLE))
596 heightWithItems = height + hItem*nItems;
597 else
598 heightWithItems = SetHeightSimpleComboBox(nItems);
5d4ee50b
VZ
599
600
601 // do resize the native control
602 wxControl::DoSetSize(x, y, width, heightWithItems, sizeFlags);
603
d4445d24 604
75004dfb
VZ
605 // make the control itself of the requested height: notice that this
606 // must be done after changing its size or it has no effect (apparently
607 // the height is reset to default during the control layout) and that it's
608 // useless to to do it when using the deferred sizing -- in this case it
609 // will be done from MSWEndDeferWindowPos()
610#if wxUSE_DEFERRED_SIZING
611 if ( m_pendingSize == wxDefaultSize )
612 {
613 // not using deferred sizing, update it immediately
614 MSWUpdateVisibleHeight();
615 }
616 else // in the middle of deferred sizing
617 {
618 // we need to report the size of the visible part of the control back
619 // in GetSize() and not height stored by DoSetSize() in m_pendingSize
5d4ee50b 620 m_pendingSize = wxSize(width, height);
75004dfb
VZ
621 }
622#else // !wxUSE_DEFERRED_SIZING
623 // always update the visible height immediately
624 MSWUpdateVisibleHeight();
625#endif // wxUSE_DEFERRED_SIZING
4438caf4 626}
2bda0e17 627
882a8f40 628wxSize wxChoice::DoGetBestSize() const
4438caf4
VZ
629{
630 // find the widest string
4438caf4 631 int wChoice = 0;
da89830a 632 int hChoice;
aa61d352
VZ
633 const unsigned int nItems = GetCount();
634 for ( unsigned int i = 0; i < nItems; i++ )
2bda0e17 635 {
d99957b6
VZ
636 int wLine;
637 GetTextExtent(GetString(i), &wLine, NULL);
4438caf4
VZ
638 if ( wLine > wChoice )
639 wChoice = wLine;
2bda0e17 640 }
fd3f686c 641
4438caf4
VZ
642 // give it some reasonable default value if there are no strings in the
643 // list
644 if ( wChoice == 0 )
645 wChoice = 100;
2bda0e17 646
d99957b6
VZ
647 // the combobox should be slightly larger than the widest string
648 wChoice += 5*GetCharWidth();
da89830a
JS
649 if( HasFlag( wxCB_SIMPLE ) )
650 {
651 hChoice = SetHeightSimpleComboBox( nItems );
652 }
653 else
654 hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight());
2bda0e17 655
da89830a 656 wxSize best(wChoice, hChoice);
1d13cc5d
RD
657 CacheBestSize(best);
658 return best;
2bda0e17
KB
659}
660
da89830a
JS
661int wxChoice::SetHeightSimpleComboBox(int nItems) const
662{
663 int cx, cy;
664 wxGetCharSize( GetHWND(), &cx, &cy, GetFont() );
7fc1b0c7 665 int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0);
da89830a
JS
666 return EDIT_HEIGHT_FROM_CHAR_HEIGHT( cy ) * wxMin( wxMax( nItems, 3 ), 6 ) + hItem - 1;
667}
668
75004dfb
VZ
669// ----------------------------------------------------------------------------
670// MSW message handlers
671// ----------------------------------------------------------------------------
672
c140b7e7 673WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 674{
f499e614 675 switch ( nMsg )
2bda0e17 676 {
f499e614
VZ
677 case WM_LBUTTONUP:
678 {
679 int x = (int)LOWORD(lParam);
680 int y = (int)HIWORD(lParam);
681
682 // Ok, this is truly weird, but if a panel with a wxChoice
683 // loses the focus, then you get a *fake* WM_LBUTTONUP message
684 // with x = 65535 and y = 65535. Filter out this nonsense.
685 //
686 // VZ: I'd like to know how to reproduce this please...
687 if ( x == 65535 && y == 65535 )
688 return 0;
689 }
690 break;
691
692 // we have to handle both: one for the normal case and the other
693 // for readonly
694 case WM_CTLCOLOREDIT:
695 case WM_CTLCOLORLISTBOX:
696 case WM_CTLCOLORSTATIC:
697 {
f499e614
VZ
698 WXHDC hdc;
699 WXHWND hwnd;
9f368d0d 700 UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
f499e614 701
2bae4332 702 WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd);
48fa6bd3
VZ
703 if ( hbr )
704 return (WXLRESULT)hbr;
705 //else: fall through to default window proc
f499e614 706 }
2bda0e17
KB
707 }
708
8d99be5f 709 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17
KB
710}
711
8d99be5f 712bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 713{
c11f0412
VZ
714 /*
715 The native control provides a great variety in the events it sends in
716 the different selection scenarios (undoubtedly for greater amusement of
717 the programmers using it). For the reference, here are the cases when
718 the final selection is accepted (things are quite interesting when it
719 is cancelled too):
720
721 A. Selecting with just the arrows without opening the dropdown:
722 1. CBN_SELENDOK
723 2. CBN_SELCHANGE
724
725 B. Opening dropdown with F4 and selecting with arrows:
726 1. CBN_DROPDOWN
727 2. many CBN_SELCHANGE while changing selection in the list
728 3. CBN_SELENDOK
729 4. CBN_CLOSEUP
730
731 C. Selecting with the mouse:
732 1. CBN_DROPDOWN
733 -- no intermediate CBN_SELCHANGEs --
734 2. CBN_SELENDOK
735 3. CBN_CLOSEUP
736 4. CBN_SELCHANGE
737
738 Admire the different order of messages in all of those cases, it must
739 surely have taken a lot of effort to Microsoft developers to achieve
740 such originality.
741 */
6ba93d23 742 switch ( param )
2bda0e17 743 {
6ba93d23 744 case CBN_DROPDOWN:
c11f0412
VZ
745 // we use this value both because we don't want to track selection
746 // using CB_GETCURSEL while the dropdown is opened and because we
747 // need to reset the selection back to it if it's eventually
748 // cancelled by user
6ba93d23
VZ
749 m_lastAcceptedSelection = GetCurrentSelection();
750 break;
2bda0e17 751
6ba93d23 752 case CBN_CLOSEUP:
c11f0412
VZ
753 // if the selection was accepted by the user, it should have been
754 // reset to wxID_NONE by CBN_SELENDOK, otherwise the selection was
755 // cancelled and we must restore the old one
756 if ( m_lastAcceptedSelection != wxID_NONE )
757 {
758 SetSelection(m_lastAcceptedSelection);
759 m_lastAcceptedSelection = wxID_NONE;
760 }
6ba93d23
VZ
761 break;
762
c11f0412
VZ
763 case CBN_SELENDOK:
764 // reset it to prevent CBN_CLOSEUP from undoing the selection (it's
765 // ok to reset it now as GetCurrentSelection() will now return the
766 // same thing anyhow)
767 m_lastAcceptedSelection = wxID_NONE;
768
6ba93d23
VZ
769 {
770 const int n = GetSelection();
771
772 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
773 event.SetInt(n);
774 event.SetEventObject(this);
775
776 if ( n > -1 )
777 {
778 event.SetString(GetStringSelection());
593ac33e 779 InitCommandEventWithItems(event, n);
6ba93d23
VZ
780 }
781
782 ProcessCommand(event);
783 }
c11f0412
VZ
784 break;
785
786 // don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection
787 // valid and the selection will be undone in CBN_CLOSEUP above
788
789 // don't handle CBN_SELCHANGE neither, we don't want to generate events
790 // while the dropdown is opened -- but do add it if we ever need this
791
792 default:
793 return false;
8c1c5302 794 }
2bda0e17 795
c11f0412 796 return true;
8d99be5f 797}
2bda0e17 798
2bae4332 799WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)
f6bcfd97 800{
48fa6bd3
VZ
801 if ( !IsEnabled() )
802 return MSWControlColorDisabled(hDC);
f6bcfd97 803
2bae4332 804 return wxChoiceBase::MSWControlColor(hDC, hWnd);
f6bcfd97
BP
805}
806
3180bc0e 807#endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__)