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