]>
Commit | Line | Data |
---|---|---|
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 | ||
239 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
240 | ||
241 | // there doesn't seem to be any way to get the text colour using themes | |
242 | // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX | |
243 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
244 | ||
245 | // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista) | |
246 | attrs.colBg = wnd->MSWGetThemeColour(L"EDIT", | |
247 | EP_EDITTEXT, | |
248 | ETS_NORMAL, | |
249 | ThemeColourBackground, | |
250 | wxSYS_COLOUR_WINDOW); | |
251 | ||
252 | return attrs; | |
253 | } | |
254 | ||
255 | wxChoice::~wxChoice() | |
256 | { | |
257 | Clear(); | |
258 | } | |
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // adding/deleting items to/from the list | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter& items, | |
265 | unsigned int pos, | |
266 | void **clientData, wxClientDataType type) | |
267 | { | |
268 | MSWAllocStorage(items, CB_INITSTORAGE); | |
269 | ||
270 | const bool append = pos == GetCount(); | |
271 | ||
272 | // use CB_ADDSTRING when appending at the end to make sure the control is | |
273 | // resorted if it has wxCB_SORT style | |
274 | const unsigned msg = append ? CB_ADDSTRING : CB_INSERTSTRING; | |
275 | ||
276 | if ( append ) | |
277 | pos = 0; | |
278 | ||
279 | int n = wxNOT_FOUND; | |
280 | const unsigned numItems = items.GetCount(); | |
281 | for ( unsigned i = 0; i < numItems; ++i ) | |
282 | { | |
283 | n = MSWInsertOrAppendItem(pos, items[i], msg); | |
284 | if ( n == wxNOT_FOUND ) | |
285 | return n; | |
286 | ||
287 | if ( !append ) | |
288 | pos++; | |
289 | ||
290 | AssignNewItemClientData(n, clientData, i, type); | |
291 | } | |
292 | ||
293 | // we need to refresh our size in order to have enough space for the | |
294 | // newly added items | |
295 | if ( !IsFrozen() ) | |
296 | MSWUpdateDropDownHeight(); | |
297 | ||
298 | InvalidateBestSize(); | |
299 | ||
300 | return n; | |
301 | } | |
302 | ||
303 | void wxChoice::DoDeleteOneItem(unsigned int n) | |
304 | { | |
305 | wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") ); | |
306 | ||
307 | SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); | |
308 | ||
309 | if ( !IsFrozen() ) | |
310 | MSWUpdateDropDownHeight(); | |
311 | ||
312 | InvalidateBestSize(); | |
313 | } | |
314 | ||
315 | void wxChoice::DoClear() | |
316 | { | |
317 | SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); | |
318 | ||
319 | if ( !IsFrozen() ) | |
320 | MSWUpdateDropDownHeight(); | |
321 | ||
322 | InvalidateBestSize(); | |
323 | } | |
324 | ||
325 | // ---------------------------------------------------------------------------- | |
326 | // selection | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
329 | int wxChoice::GetSelection() const | |
330 | { | |
331 | // if m_lastAcceptedSelection is set, it means that the dropdown is | |
332 | // currently shown and that we want to use the last "permanent" selection | |
333 | // instead of whatever is under the mouse pointer currently | |
334 | // | |
335 | // otherwise, get the selection from the control | |
336 | return m_lastAcceptedSelection == wxID_NONE ? GetCurrentSelection() | |
337 | : m_lastAcceptedSelection; | |
338 | } | |
339 | ||
340 | int wxChoice::GetCurrentSelection() const | |
341 | { | |
342 | return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); | |
343 | } | |
344 | ||
345 | void wxChoice::SetSelection(int n) | |
346 | { | |
347 | SendMessage(GetHwnd(), CB_SETCURSEL, n, 0); | |
348 | } | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // string list functions | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | unsigned int wxChoice::GetCount() const | |
355 | { | |
356 | return (unsigned int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0); | |
357 | } | |
358 | ||
359 | int wxChoice::FindString(const wxString& s, bool bCase) const | |
360 | { | |
361 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
362 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. | |
363 | // wxChoice::Do it the long way instead. | |
364 | unsigned int count = GetCount(); | |
365 | for ( unsigned int i = 0; i < count; i++ ) | |
366 | { | |
367 | // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too | |
368 | if (GetString(i).IsSameAs(s, bCase)) | |
369 | return i; | |
370 | } | |
371 | ||
372 | return wxNOT_FOUND; | |
373 | #else // !Watcom | |
374 | //TODO: Evidently some MSW versions (all?) don't like empty strings | |
375 | //passed to SendMessage, so we have to do it ourselves in that case | |
376 | if ( s.empty() ) | |
377 | { | |
378 | unsigned int count = GetCount(); | |
379 | for ( unsigned int i = 0; i < count; i++ ) | |
380 | { | |
381 | if (GetString(i).empty()) | |
382 | return i; | |
383 | } | |
384 | ||
385 | return wxNOT_FOUND; | |
386 | } | |
387 | else if (bCase) | |
388 | { | |
389 | // back to base class search for not native search type | |
390 | return wxItemContainerImmutable::FindString( s, bCase ); | |
391 | } | |
392 | else | |
393 | { | |
394 | int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, | |
395 | (WPARAM)-1, (LPARAM)s.wx_str()); | |
396 | ||
397 | return pos == LB_ERR ? wxNOT_FOUND : pos; | |
398 | } | |
399 | #endif // Watcom/!Watcom | |
400 | } | |
401 | ||
402 | void wxChoice::SetString(unsigned int n, const wxString& s) | |
403 | { | |
404 | wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::SetString") ); | |
405 | ||
406 | // we have to delete and add back the string as there is no way to change a | |
407 | // string in place | |
408 | ||
409 | // we need to preserve the client data manually | |
410 | void *oldData = NULL; | |
411 | wxClientData *oldObjData = NULL; | |
412 | if ( HasClientUntypedData() ) | |
413 | oldData = GetClientData(n); | |
414 | else if ( HasClientObjectData() ) | |
415 | oldObjData = GetClientObject(n); | |
416 | ||
417 | ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); | |
418 | ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.wx_str() ); | |
419 | ||
420 | // restore the client data | |
421 | if ( oldData ) | |
422 | SetClientData(n, oldData); | |
423 | else if ( oldObjData ) | |
424 | SetClientObject(n, oldObjData); | |
425 | ||
426 | InvalidateBestSize(); | |
427 | } | |
428 | ||
429 | wxString wxChoice::GetString(unsigned int n) const | |
430 | { | |
431 | int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0); | |
432 | ||
433 | wxString str; | |
434 | if ( len != CB_ERR && len > 0 ) | |
435 | { | |
436 | if ( ::SendMessage | |
437 | ( | |
438 | GetHwnd(), | |
439 | CB_GETLBTEXT, | |
440 | n, | |
441 | (LPARAM)(wxChar *)wxStringBuffer(str, len) | |
442 | ) == CB_ERR ) | |
443 | { | |
444 | wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)")); | |
445 | } | |
446 | } | |
447 | ||
448 | return str; | |
449 | } | |
450 | ||
451 | // ---------------------------------------------------------------------------- | |
452 | // client data | |
453 | // ---------------------------------------------------------------------------- | |
454 | ||
455 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) | |
456 | { | |
457 | if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA, | |
458 | n, (LPARAM)clientData) == CB_ERR ) | |
459 | { | |
460 | wxLogLastError(wxT("CB_SETITEMDATA")); | |
461 | } | |
462 | } | |
463 | ||
464 | void* wxChoice::DoGetItemClientData(unsigned int n) const | |
465 | { | |
466 | LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0); | |
467 | if ( rc == CB_ERR ) | |
468 | { | |
469 | wxLogLastError(wxT("CB_GETITEMDATA")); | |
470 | ||
471 | // unfortunately, there is no way to return an error code to the user | |
472 | rc = (LPARAM) NULL; | |
473 | } | |
474 | ||
475 | return (void *)rc; | |
476 | } | |
477 | ||
478 | // ---------------------------------------------------------------------------- | |
479 | // wxMSW-specific geometry management | |
480 | // ---------------------------------------------------------------------------- | |
481 | ||
482 | namespace | |
483 | { | |
484 | ||
485 | // there is a difference between the height passed to CB_SETITEMHEIGHT and the | |
486 | // real height of the combobox; it is probably not constant for all Windows | |
487 | // versions/settings but right now I don't know how to find what it is so it is | |
488 | // temporarily hardcoded to its value under XP systems with normal fonts sizes | |
489 | const int COMBO_HEIGHT_ADJ = 6; | |
490 | ||
491 | } // anonymous namespace | |
492 | ||
493 | void wxChoice::MSWUpdateVisibleHeight() | |
494 | { | |
495 | if ( m_heightOwn != wxDefaultCoord ) | |
496 | { | |
497 | ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT, | |
498 | (WPARAM)-1, m_heightOwn - COMBO_HEIGHT_ADJ); | |
499 | } | |
500 | } | |
501 | ||
502 | #if wxUSE_DEFERRED_SIZING | |
503 | void wxChoice::MSWEndDeferWindowPos() | |
504 | { | |
505 | // we can only set the height of the choice itself now as it is reset to | |
506 | // default every time the control is resized | |
507 | MSWUpdateVisibleHeight(); | |
508 | ||
509 | wxChoiceBase::MSWEndDeferWindowPos(); | |
510 | } | |
511 | #endif // wxUSE_DEFERRED_SIZING | |
512 | ||
513 | void wxChoice::MSWUpdateDropDownHeight() | |
514 | { | |
515 | // be careful to not change the width here | |
516 | DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, GetSize().y, | |
517 | wxSIZE_USE_EXISTING); | |
518 | } | |
519 | ||
520 | void wxChoice::DoMoveWindow(int x, int y, int width, int height) | |
521 | { | |
522 | // here is why this is necessary: if the width is negative, the combobox | |
523 | // window proc makes the window of the size width*height instead of | |
524 | // interpreting height in the usual manner (meaning the height of the drop | |
525 | // down list - usually the height specified in the call to MoveWindow() | |
526 | // will not change the height of combo box per se) | |
527 | // | |
528 | // this behaviour is not documented anywhere, but this is just how it is | |
529 | // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without | |
530 | // the check, constraints/sizers using combos may break the height | |
531 | // constraint will have not at all the same value as expected | |
532 | if ( width < 0 ) | |
533 | return; | |
534 | ||
535 | wxControl::DoMoveWindow(x, y, width, height); | |
536 | } | |
537 | ||
538 | void wxChoice::DoGetSize(int *w, int *h) const | |
539 | { | |
540 | wxControl::DoGetSize(w, h); | |
541 | ||
542 | // this is weird: sometimes, the height returned by Windows is clearly the | |
543 | // total height of the control including the drop down list -- but only | |
544 | // sometimes, and sometimes it isn't so work around this here by using our | |
545 | // own stored value if we have it | |
546 | if ( h && m_heightOwn != wxDefaultCoord ) | |
547 | *h = m_heightOwn; | |
548 | } | |
549 | ||
550 | void wxChoice::DoSetSize(int x, int y, | |
551 | int width, int height, | |
552 | int sizeFlags) | |
553 | { | |
554 | // we need the real height below so get the current one if it's not given | |
555 | if ( height != wxDefaultCoord && height != GetBestSize().y ) | |
556 | { | |
557 | // set our new own height but be careful not to make it too big: the | |
558 | // native control apparently stores it as a single byte and so setting | |
559 | // own height to 256 pixels results in default height being used (255 | |
560 | // is still ok) | |
561 | m_heightOwn = height; | |
562 | ||
563 | if ( m_heightOwn > UCHAR_MAX ) | |
564 | m_heightOwn = UCHAR_MAX; | |
565 | // nor too small: see MSWUpdateVisibleHeight() | |
566 | else if ( m_heightOwn < COMBO_HEIGHT_ADJ ) | |
567 | m_heightOwn = COMBO_HEIGHT_ADJ; | |
568 | } | |
569 | else // height not specified | |
570 | { | |
571 | DoGetSize(NULL, &height); | |
572 | } | |
573 | ||
574 | ||
575 | // the height which we must pass to Windows should be the total height of | |
576 | // the control including the drop down list while the height given to us | |
577 | // is, of course, just the height of the permanently visible part of it so | |
578 | // add the drop down height to it | |
579 | ||
580 | // don't make the drop down list too tall, arbitrarily limit it to 30 | |
581 | // items max and also don't make it too small if it's currently empty | |
582 | size_t nItems = GetCount(); | |
583 | if (!HasFlag(wxCB_SIMPLE)) | |
584 | { | |
585 | if ( !nItems ) | |
586 | nItems = 9; | |
587 | else if ( nItems > 30 ) | |
588 | nItems = 30; | |
589 | } | |
590 | ||
591 | const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0); | |
592 | int heightWithItems = 0; | |
593 | if (!HasFlag(wxCB_SIMPLE)) | |
594 | heightWithItems = height + hItem*nItems; | |
595 | else | |
596 | heightWithItems = SetHeightSimpleComboBox(nItems); | |
597 | ||
598 | ||
599 | // do resize the native control | |
600 | wxControl::DoSetSize(x, y, width, heightWithItems, sizeFlags); | |
601 | ||
602 | ||
603 | // make the control itself of the requested height: notice that this | |
604 | // must be done after changing its size or it has no effect (apparently | |
605 | // the height is reset to default during the control layout) and that it's | |
606 | // useless to to do it when using the deferred sizing -- in this case it | |
607 | // will be done from MSWEndDeferWindowPos() | |
608 | #if wxUSE_DEFERRED_SIZING | |
609 | if ( m_pendingSize == wxDefaultSize ) | |
610 | { | |
611 | // not using deferred sizing, update it immediately | |
612 | MSWUpdateVisibleHeight(); | |
613 | } | |
614 | else // in the middle of deferred sizing | |
615 | { | |
616 | // we need to report the size of the visible part of the control back | |
617 | // in GetSize() and not height stored by DoSetSize() in m_pendingSize | |
618 | m_pendingSize = wxSize(width, height); | |
619 | } | |
620 | #else // !wxUSE_DEFERRED_SIZING | |
621 | // always update the visible height immediately | |
622 | MSWUpdateVisibleHeight(); | |
623 | #endif // wxUSE_DEFERRED_SIZING | |
624 | } | |
625 | ||
626 | wxSize wxChoice::DoGetBestSize() const | |
627 | { | |
628 | // find the widest string | |
629 | int wChoice = 0; | |
630 | int hChoice; | |
631 | const unsigned int nItems = GetCount(); | |
632 | for ( unsigned int i = 0; i < nItems; i++ ) | |
633 | { | |
634 | int wLine; | |
635 | GetTextExtent(GetString(i), &wLine, NULL); | |
636 | if ( wLine > wChoice ) | |
637 | wChoice = wLine; | |
638 | } | |
639 | ||
640 | // give it some reasonable default value if there are no strings in the | |
641 | // list | |
642 | if ( wChoice == 0 ) | |
643 | wChoice = 100; | |
644 | ||
645 | // the combobox should be slightly larger than the widest string | |
646 | wChoice += 5*GetCharWidth(); | |
647 | if( HasFlag( wxCB_SIMPLE ) ) | |
648 | { | |
649 | hChoice = SetHeightSimpleComboBox( nItems ); | |
650 | } | |
651 | else | |
652 | hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight()); | |
653 | ||
654 | wxSize best(wChoice, hChoice); | |
655 | CacheBestSize(best); | |
656 | return best; | |
657 | } | |
658 | ||
659 | int wxChoice::SetHeightSimpleComboBox(int nItems) const | |
660 | { | |
661 | int cx, cy; | |
662 | wxGetCharSize( GetHWND(), &cx, &cy, GetFont() ); | |
663 | int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0); | |
664 | return EDIT_HEIGHT_FROM_CHAR_HEIGHT( cy ) * wxMin( wxMax( nItems, 3 ), 6 ) + hItem - 1; | |
665 | } | |
666 | ||
667 | // ---------------------------------------------------------------------------- | |
668 | // MSW message handlers | |
669 | // ---------------------------------------------------------------------------- | |
670 | ||
671 | WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
672 | { | |
673 | switch ( nMsg ) | |
674 | { | |
675 | case WM_LBUTTONUP: | |
676 | { | |
677 | int x = (int)LOWORD(lParam); | |
678 | int y = (int)HIWORD(lParam); | |
679 | ||
680 | // Ok, this is truly weird, but if a panel with a wxChoice | |
681 | // loses the focus, then you get a *fake* WM_LBUTTONUP message | |
682 | // with x = 65535 and y = 65535. Filter out this nonsense. | |
683 | // | |
684 | // VZ: I'd like to know how to reproduce this please... | |
685 | if ( x == 65535 && y == 65535 ) | |
686 | return 0; | |
687 | } | |
688 | break; | |
689 | ||
690 | // we have to handle both: one for the normal case and the other | |
691 | // for readonly | |
692 | case WM_CTLCOLOREDIT: | |
693 | case WM_CTLCOLORLISTBOX: | |
694 | case WM_CTLCOLORSTATIC: | |
695 | { | |
696 | WXHDC hdc; | |
697 | WXHWND hwnd; | |
698 | UnpackCtlColor(wParam, lParam, &hdc, &hwnd); | |
699 | ||
700 | WXHBRUSH hbr = MSWControlColor((WXHDC)hdc, hwnd); | |
701 | if ( hbr ) | |
702 | return (WXLRESULT)hbr; | |
703 | //else: fall through to default window proc | |
704 | } | |
705 | } | |
706 | ||
707 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
708 | } | |
709 | ||
710 | bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
711 | { | |
712 | /* | |
713 | The native control provides a great variety in the events it sends in | |
714 | the different selection scenarios (undoubtedly for greater amusement of | |
715 | the programmers using it). For the reference, here are the cases when | |
716 | the final selection is accepted (things are quite interesting when it | |
717 | is cancelled too): | |
718 | ||
719 | A. Selecting with just the arrows without opening the dropdown: | |
720 | 1. CBN_SELENDOK | |
721 | 2. CBN_SELCHANGE | |
722 | ||
723 | B. Opening dropdown with F4 and selecting with arrows: | |
724 | 1. CBN_DROPDOWN | |
725 | 2. many CBN_SELCHANGE while changing selection in the list | |
726 | 3. CBN_SELENDOK | |
727 | 4. CBN_CLOSEUP | |
728 | ||
729 | C. Selecting with the mouse: | |
730 | 1. CBN_DROPDOWN | |
731 | -- no intermediate CBN_SELCHANGEs -- | |
732 | 2. CBN_SELENDOK | |
733 | 3. CBN_CLOSEUP | |
734 | 4. CBN_SELCHANGE | |
735 | ||
736 | Admire the different order of messages in all of those cases, it must | |
737 | surely have taken a lot of effort to Microsoft developers to achieve | |
738 | such originality. | |
739 | */ | |
740 | switch ( param ) | |
741 | { | |
742 | case CBN_DROPDOWN: | |
743 | // we use this value both because we don't want to track selection | |
744 | // using CB_GETCURSEL while the dropdown is opened and because we | |
745 | // need to reset the selection back to it if it's eventually | |
746 | // cancelled by user | |
747 | m_lastAcceptedSelection = GetCurrentSelection(); | |
748 | break; | |
749 | ||
750 | case CBN_CLOSEUP: | |
751 | // if the selection was accepted by the user, it should have been | |
752 | // reset to wxID_NONE by CBN_SELENDOK, otherwise the selection was | |
753 | // cancelled and we must restore the old one | |
754 | if ( m_lastAcceptedSelection != wxID_NONE ) | |
755 | { | |
756 | SetSelection(m_lastAcceptedSelection); | |
757 | m_lastAcceptedSelection = wxID_NONE; | |
758 | } | |
759 | break; | |
760 | ||
761 | case CBN_SELENDOK: | |
762 | // reset it to prevent CBN_CLOSEUP from undoing the selection (it's | |
763 | // ok to reset it now as GetCurrentSelection() will now return the | |
764 | // same thing anyhow) | |
765 | m_lastAcceptedSelection = wxID_NONE; | |
766 | ||
767 | { | |
768 | const int n = GetSelection(); | |
769 | ||
770 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); | |
771 | event.SetInt(n); | |
772 | event.SetEventObject(this); | |
773 | ||
774 | if ( n > -1 ) | |
775 | { | |
776 | event.SetString(GetStringSelection()); | |
777 | InitCommandEventWithItems(event, n); | |
778 | } | |
779 | ||
780 | ProcessCommand(event); | |
781 | } | |
782 | break; | |
783 | ||
784 | // don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection | |
785 | // valid and the selection will be undone in CBN_CLOSEUP above | |
786 | ||
787 | // don't handle CBN_SELCHANGE neither, we don't want to generate events | |
788 | // while the dropdown is opened -- but do add it if we ever need this | |
789 | ||
790 | default: | |
791 | return false; | |
792 | } | |
793 | ||
794 | return true; | |
795 | } | |
796 | ||
797 | WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd) | |
798 | { | |
799 | if ( !IsEnabled() ) | |
800 | return MSWControlColorDisabled(hDC); | |
801 | ||
802 | return wxChoiceBase::MSWControlColor(hDC, hWnd); | |
803 | } | |
804 | ||
805 | #endif // wxUSE_CHOICE && !(__SMARTPHONE__ && __WXWINCE__) |