]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | /////////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/msw/listbox.cpp |
2bda0e17 KB |
3 | // Purpose: wxListBox |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin (owner drawn stuff) | |
dd3c394a | 6 | // Created: |
2bda0e17 KB |
7 | // RCS-ID: $Id$ |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
2bda0e17 KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
dd3c394a | 16 | #pragma hdrstop |
2bda0e17 KB |
17 | #endif |
18 | ||
1e6feb95 | 19 | #if wxUSE_LISTBOX |
0c589ad0 | 20 | |
2a673eb1 WS |
21 | #include "wx/listbox.h" |
22 | ||
2bda0e17 | 23 | #ifndef WX_PRECOMP |
ad9835c9 | 24 | #include "wx/dynarray.h" |
ad9835c9 WS |
25 | #include "wx/settings.h" |
26 | #include "wx/brush.h" | |
27 | #include "wx/font.h" | |
28 | #include "wx/dc.h" | |
29 | #include "wx/utils.h" | |
e4db172a | 30 | #include "wx/log.h" |
cdccdfab | 31 | #include "wx/window.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
1e6feb95 | 34 | #include "wx/msw/private.h" |
74052fe8 | 35 | #include "wx/msw/dc.h" |
1e6feb95 | 36 | |
5ea105e0 RR |
37 | #include <windowsx.h> |
38 | ||
0c589ad0 BM |
39 | #if wxUSE_OWNER_DRAWN |
40 | #include "wx/ownerdrw.h" | |
41 | #endif | |
2bda0e17 | 42 | |
2bda0e17 KB |
43 | // ============================================================================ |
44 | // list box item declaration and implementation | |
45 | // ============================================================================ | |
46 | ||
47d67540 | 47 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
48 | |
49 | class wxListBoxItem : public wxOwnerDrawn | |
50 | { | |
51 | public: | |
98fbab9e VZ |
52 | wxListBoxItem(wxListBox *parent) |
53 | { m_parent = parent; } | |
2bda0e17 | 54 | |
98fbab9e VZ |
55 | wxListBox *GetParent() const |
56 | { return m_parent; } | |
57 | ||
58 | int GetIndex() const | |
59 | { return m_parent->GetItemIndex(const_cast<wxListBoxItem*>(this)); } | |
60 | ||
61 | wxString GetName() const | |
62 | { return m_parent->GetString(GetIndex()); } | |
63 | ||
64 | private: | |
65 | wxListBox *m_parent; | |
66 | }; | |
2bda0e17 | 67 | |
2b5f62a0 | 68 | wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) |
2bda0e17 | 69 | { |
98fbab9e | 70 | return new wxListBoxItem(this); |
2bda0e17 KB |
71 | } |
72 | ||
73 | #endif //USE_OWNER_DRAWN | |
74 | ||
75 | // ============================================================================ | |
76 | // list box control implementation | |
77 | // ============================================================================ | |
78 | ||
2ee3ee1b VZ |
79 | // ---------------------------------------------------------------------------- |
80 | // creation | |
81 | // ---------------------------------------------------------------------------- | |
dd3c394a | 82 | |
096d5447 | 83 | void wxListBox::Init() |
2bda0e17 | 84 | { |
dd3c394a | 85 | m_noItems = 0; |
1ddb283a | 86 | m_updateHorizontalExtent = false; |
f2eb4ad2 | 87 | m_selectedByKeyboard = false; |
2bda0e17 KB |
88 | } |
89 | ||
dd3c394a VZ |
90 | bool wxListBox::Create(wxWindow *parent, |
91 | wxWindowID id, | |
2bda0e17 KB |
92 | const wxPoint& pos, |
93 | const wxSize& size, | |
debe6624 JS |
94 | int n, const wxString choices[], |
95 | long style, | |
56a44c64 | 96 | const wxValidator& validator, |
2bda0e17 KB |
97 | const wxString& name) |
98 | { | |
7a69cd96 VZ |
99 | // initialize base class fields |
100 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
101 | return false; | |
2bda0e17 | 102 | |
7a69cd96 | 103 | // create the native control |
9a83f860 | 104 | if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString, pos, size) ) |
7a69cd96 VZ |
105 | { |
106 | // control creation failed | |
107 | return false; | |
2bda0e17 | 108 | } |
1c089c47 | 109 | |
7a69cd96 VZ |
110 | // initialize the contents |
111 | for ( int i = 0; i < n; i++ ) | |
112 | { | |
113 | Append(choices[i]); | |
2bda0e17 | 114 | } |
2bda0e17 | 115 | |
f022dc23 | 116 | // now we can compute our best size correctly, so do it again |
170acdc9 | 117 | SetInitialSize(size); |
7ec5921d | 118 | |
7a69cd96 | 119 | return true; |
2bda0e17 KB |
120 | } |
121 | ||
584ad2a3 MB |
122 | bool wxListBox::Create(wxWindow *parent, |
123 | wxWindowID id, | |
124 | const wxPoint& pos, | |
125 | const wxSize& size, | |
126 | const wxArrayString& choices, | |
127 | long style, | |
76efbc2a | 128 | const wxValidator& validator, |
584ad2a3 MB |
129 | const wxString& name) |
130 | { | |
131 | wxCArrayString chs(choices); | |
132 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
133 | style, validator, name); | |
134 | } | |
135 | ||
bfc6fde4 | 136 | wxListBox::~wxListBox() |
2bda0e17 | 137 | { |
ad998d78 | 138 | Clear(); |
2bda0e17 KB |
139 | } |
140 | ||
7a69cd96 | 141 | WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const |
2bda0e17 | 142 | { |
7a69cd96 VZ |
143 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); |
144 | ||
145 | // we always want to get the notifications | |
146 | msStyle |= LBS_NOTIFY; | |
147 | ||
148 | // without this style, you get unexpected heights, so e.g. constraint | |
149 | // layout doesn't work properly | |
150 | msStyle |= LBS_NOINTEGRALHEIGHT; | |
151 | ||
152 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
9a83f860 | 153 | wxT("only one of listbox selection modes can be specified") ); |
7a69cd96 VZ |
154 | |
155 | if ( style & wxLB_MULTIPLE ) | |
156 | msStyle |= LBS_MULTIPLESEL; | |
157 | else if ( style & wxLB_EXTENDED ) | |
158 | msStyle |= LBS_EXTENDEDSEL; | |
159 | ||
58dcd1ae | 160 | wxASSERT_MSG( !(style & wxLB_ALWAYS_SB) || !(style & wxLB_NO_SB), |
9a83f860 | 161 | wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) ); |
58dcd1ae VZ |
162 | |
163 | if ( !(style & wxLB_NO_SB) ) | |
164 | { | |
165 | msStyle |= WS_VSCROLL; | |
166 | if ( style & wxLB_ALWAYS_SB ) | |
167 | msStyle |= LBS_DISABLENOSCROLL; | |
168 | } | |
169 | ||
7a69cd96 VZ |
170 | if ( m_windowStyle & wxLB_HSCROLL ) |
171 | msStyle |= WS_HSCROLL; | |
172 | if ( m_windowStyle & wxLB_SORT ) | |
173 | msStyle |= LBS_SORT; | |
174 | ||
175 | #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__) | |
176 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
177 | { | |
178 | // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put | |
179 | // the strings in the listbox for simplicity even though we could have | |
180 | // avoided it in this case | |
181 | msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS; | |
182 | } | |
183 | #endif // wxUSE_OWNER_DRAWN | |
184 | ||
185 | return msStyle; | |
2bda0e17 KB |
186 | } |
187 | ||
1ddb283a RR |
188 | void wxListBox::OnInternalIdle() |
189 | { | |
190 | wxWindow::OnInternalIdle(); | |
03647350 | 191 | |
1ddb283a RR |
192 | if (m_updateHorizontalExtent) |
193 | { | |
194 | SetHorizontalExtent(wxEmptyString); | |
195 | m_updateHorizontalExtent = false; | |
196 | } | |
197 | } | |
198 | ||
316bba0c VZ |
199 | void wxListBox::MSWOnItemsChanged() |
200 | { | |
201 | // we need to do two things when items change: update their max horizontal | |
202 | // extent so that horizontal scrollbar could be shown or hidden as | |
203 | // appropriate and also invlaidate the best size | |
204 | // | |
205 | // updating the max extent is slow (it's an O(N) operation) and so we defer | |
206 | // it until the idle time but the best size should be invalidated | |
207 | // immediately doing it in idle time is too late -- layout using incorrect | |
208 | // old best size will have been already done by then | |
209 | ||
210 | m_updateHorizontalExtent = true; | |
211 | ||
212 | InvalidateBestSize(); | |
213 | } | |
214 | ||
2ee3ee1b VZ |
215 | // ---------------------------------------------------------------------------- |
216 | // implementation of wxListBoxBase methods | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | void wxListBox::DoSetFirstItem(int N) | |
2bda0e17 | 220 | { |
8228b893 | 221 | wxCHECK_RET( IsValid(N), |
223d09f6 | 222 | wxT("invalid index in wxListBox::SetFirstItem") ); |
dd3c394a | 223 | |
2ee3ee1b | 224 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); |
2bda0e17 KB |
225 | } |
226 | ||
a236aa20 | 227 | void wxListBox::DoDeleteOneItem(unsigned int n) |
2bda0e17 | 228 | { |
aa61d352 | 229 | wxCHECK_RET( IsValid(n), |
223d09f6 | 230 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a | 231 | |
98fbab9e | 232 | #if wxUSE_OWNER_DRAWN |
e1009295 VZ |
233 | if ( HasFlag(wxLB_OWNERDRAW) ) |
234 | { | |
235 | delete m_aItems[n]; | |
236 | m_aItems.RemoveAt(n); | |
237 | } | |
98fbab9e VZ |
238 | #endif // wxUSE_OWNER_DRAWN |
239 | ||
aa61d352 | 240 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); |
dd3c394a VZ |
241 | m_noItems--; |
242 | ||
316bba0c | 243 | MSWOnItemsChanged(); |
84c5c49b RR |
244 | |
245 | UpdateOldSelections(); | |
2bda0e17 KB |
246 | } |
247 | ||
11e62fe6 | 248 | int wxListBox::FindString(const wxString& s, bool bCase) const |
2bda0e17 | 249 | { |
11e62fe6 WS |
250 | // back to base class search for not native search type |
251 | if (bCase) | |
252 | return wxItemContainerImmutable::FindString( s, bCase ); | |
253 | ||
e0a050e3 | 254 | int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str()); |
dd3c394a | 255 | if (pos == LB_ERR) |
2ee3ee1b | 256 | return wxNOT_FOUND; |
dd3c394a VZ |
257 | else |
258 | return pos; | |
2bda0e17 KB |
259 | } |
260 | ||
a236aa20 | 261 | void wxListBox::DoClear() |
2bda0e17 | 262 | { |
98fbab9e | 263 | #if wxUSE_OWNER_DRAWN |
e1009295 | 264 | if ( HasFlag(wxLB_OWNERDRAW) ) |
98fbab9e VZ |
265 | { |
266 | WX_CLEAR_ARRAY(m_aItems); | |
267 | } | |
268 | #endif // wxUSE_OWNER_DRAWN | |
8ee9d618 VZ |
269 | |
270 | ListBox_ResetContent(GetHwnd()); | |
271 | ||
272 | m_noItems = 0; | |
316bba0c | 273 | MSWOnItemsChanged(); |
84c5c49b RR |
274 | |
275 | UpdateOldSelections(); | |
8ee9d618 VZ |
276 | } |
277 | ||
c6179a84 | 278 | void wxListBox::DoSetSelection(int N, bool select) |
2bda0e17 | 279 | { |
8228b893 | 280 | wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), |
223d09f6 | 281 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 282 | |
2ee3ee1b VZ |
283 | if ( HasMultipleSelection() ) |
284 | { | |
bbb03ec9 VZ |
285 | // Setting selection to -1 should deselect everything. |
286 | const bool deselectAll = N == wxNOT_FOUND; | |
287 | SendMessage(GetHwnd(), LB_SETSEL, | |
288 | deselectAll ? FALSE : select, | |
289 | deselectAll ? -1 : N); | |
2ee3ee1b | 290 | } |
dd3c394a VZ |
291 | else |
292 | { | |
2ee3ee1b | 293 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 294 | } |
84c5c49b RR |
295 | |
296 | UpdateOldSelections(); | |
2bda0e17 KB |
297 | } |
298 | ||
2ee3ee1b | 299 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 300 | { |
8228b893 | 301 | wxCHECK_MSG( IsValid(N), false, |
223d09f6 | 302 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 303 | |
598ddd96 | 304 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true; |
2bda0e17 KB |
305 | } |
306 | ||
aa61d352 | 307 | void *wxListBox::DoGetItemClientData(unsigned int n) const |
2bda0e17 | 308 | { |
8228b893 | 309 | wxCHECK_MSG( IsValid(n), NULL, |
223d09f6 | 310 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 311 | |
2ee3ee1b | 312 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
313 | } |
314 | ||
aa61d352 | 315 | void wxListBox::DoSetItemClientData(unsigned int n, void *clientData) |
2ee3ee1b | 316 | { |
8228b893 | 317 | wxCHECK_RET( IsValid(n), |
223d09f6 | 318 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 319 | |
2ee3ee1b | 320 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) |
43b2d5e7 | 321 | { |
223d09f6 | 322 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
43b2d5e7 | 323 | } |
2bda0e17 KB |
324 | } |
325 | ||
326 | // Return number of selections and an array of selected integers | |
14483330 | 327 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 328 | { |
dd3c394a | 329 | aSelections.Empty(); |
14483330 | 330 | |
2ee3ee1b | 331 | if ( HasMultipleSelection() ) |
dd3c394a | 332 | { |
d90879fa VZ |
333 | int countSel = ListBox_GetSelCount(GetHwnd()); |
334 | if ( countSel == LB_ERR ) | |
335 | { | |
9a83f860 | 336 | wxLogDebug(wxT("ListBox_GetSelCount failed")); |
d90879fa VZ |
337 | } |
338 | else if ( countSel != 0 ) | |
339 | { | |
340 | int *selections = new int[countSel]; | |
14483330 | 341 | |
d90879fa VZ |
342 | if ( ListBox_GetSelItems(GetHwnd(), |
343 | countSel, selections) == LB_ERR ) | |
344 | { | |
345 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
346 | countSel = -1; | |
347 | } | |
348 | else | |
349 | { | |
350 | aSelections.Alloc(countSel); | |
351 | for ( int n = 0; n < countSel; n++ ) | |
352 | aSelections.Add(selections[n]); | |
353 | } | |
14483330 | 354 | |
dd3c394a VZ |
355 | delete [] selections; |
356 | } | |
14483330 | 357 | |
d90879fa | 358 | return countSel; |
dd3c394a VZ |
359 | } |
360 | else // single-selection listbox | |
361 | { | |
f6bcfd97 BP |
362 | if (ListBox_GetCurSel(GetHwnd()) > -1) |
363 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
14483330 | 364 | |
f6bcfd97 | 365 | return aSelections.Count(); |
dd3c394a | 366 | } |
2bda0e17 KB |
367 | } |
368 | ||
369 | // Get single selection, for single choice list items | |
14483330 | 370 | int wxListBox::GetSelection() const |
2bda0e17 | 371 | { |
2ee3ee1b | 372 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 373 | -1, |
f6bcfd97 | 374 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); |
14483330 | 375 | |
a23fd0e1 | 376 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
377 | } |
378 | ||
379 | // Find string for position | |
aa61d352 | 380 | wxString wxListBox::GetString(unsigned int n) const |
2bda0e17 | 381 | { |
aa61d352 | 382 | wxCHECK_MSG( IsValid(n), wxEmptyString, |
66cf41cb | 383 | wxT("invalid index in wxListBox::GetString") ); |
dd3c394a | 384 | |
aa61d352 | 385 | int len = ListBox_GetTextLen(GetHwnd(), n); |
dd3c394a VZ |
386 | |
387 | // +1 for terminating NUL | |
388 | wxString result; | |
aa61d352 | 389 | ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1)); |
dd3c394a VZ |
390 | |
391 | return result; | |
2bda0e17 KB |
392 | } |
393 | ||
a236aa20 VZ |
394 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, |
395 | unsigned int pos, | |
396 | void **clientData, | |
397 | wxClientDataType type) | |
2ee3ee1b | 398 | { |
a236aa20 VZ |
399 | MSWAllocStorage(items, LB_INITSTORAGE); |
400 | ||
401 | const bool append = pos == GetCount(); | |
402 | ||
403 | // we must use CB_ADDSTRING when appending as only it works correctly for | |
404 | // the sorted controls | |
405 | const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING; | |
2ee3ee1b | 406 | |
a236aa20 VZ |
407 | if ( append ) |
408 | pos = 0; | |
409 | ||
410 | int n = wxNOT_FOUND; | |
411 | ||
412 | const unsigned int numItems = items.GetCount(); | |
413 | for ( unsigned int i = 0; i < numItems; i++ ) | |
fd7ab28c | 414 | { |
a236aa20 VZ |
415 | n = MSWInsertOrAppendItem(pos, items[i], msg); |
416 | if ( n == wxNOT_FOUND ) | |
417 | return n; | |
418 | ||
419 | if ( !append ) | |
420 | pos++; | |
421 | ||
422 | ++m_noItems; | |
fd7ab28c VZ |
423 | |
424 | #if wxUSE_OWNER_DRAWN | |
a236aa20 | 425 | if ( HasFlag(wxLB_OWNERDRAW) ) |
9085d634 | 426 | { |
a236aa20 | 427 | wxOwnerDrawn *pNewItem = CreateLboxItem(n); |
9085d634 | 428 | pNewItem->SetFont(GetFont()); |
a236aa20 | 429 | m_aItems.Insert(pNewItem, n); |
9085d634 | 430 | } |
fd7ab28c | 431 | #endif // wxUSE_OWNER_DRAWN |
a236aa20 | 432 | AssignNewItemClientData(n, clientData, i, type); |
fd7ab28c VZ |
433 | } |
434 | ||
316bba0c | 435 | MSWOnItemsChanged(); |
a236aa20 | 436 | |
84c5c49b RR |
437 | UpdateOldSelections(); |
438 | ||
a236aa20 | 439 | return n; |
2ee3ee1b VZ |
440 | } |
441 | ||
85ee88cd | 442 | int wxListBox::DoHitTestList(const wxPoint& point) const |
c00fed0e | 443 | { |
44e7bad0 VZ |
444 | LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT, |
445 | 0, MAKELPARAM(point.x, point.y)); | |
c00fed0e VZ |
446 | |
447 | // non zero high-order word means that this item is outside of the client | |
448 | // area, IOW the point is outside of the listbox | |
44e7bad0 | 449 | return HIWORD(lRes) ? wxNOT_FOUND : LOWORD(lRes); |
c00fed0e VZ |
450 | } |
451 | ||
aa61d352 | 452 | void wxListBox::SetString(unsigned int n, const wxString& s) |
2ee3ee1b | 453 | { |
aa61d352 | 454 | wxCHECK_RET( IsValid(n), |
2ee3ee1b VZ |
455 | wxT("invalid index in wxListBox::SetString") ); |
456 | ||
457 | // remember the state of the item | |
aa61d352 | 458 | bool wasSelected = IsSelected(n); |
2ee3ee1b VZ |
459 | |
460 | void *oldData = NULL; | |
461 | wxClientData *oldObjData = NULL; | |
b4a11fe8 | 462 | if ( HasClientUntypedData() ) |
aa61d352 | 463 | oldData = GetClientData(n); |
b4a11fe8 | 464 | else if ( HasClientObjectData() ) |
aa61d352 | 465 | oldObjData = GetClientObject(n); |
2ee3ee1b VZ |
466 | |
467 | // delete and recreate it | |
aa61d352 | 468 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); |
2ee3ee1b | 469 | |
aa61d352 VZ |
470 | int newN = n; |
471 | if ( n == (m_noItems - 1) ) | |
2ee3ee1b VZ |
472 | newN = -1; |
473 | ||
e0a050e3 | 474 | ListBox_InsertString(GetHwnd(), newN, s.wx_str()); |
2ee3ee1b VZ |
475 | |
476 | // restore the client data | |
477 | if ( oldData ) | |
aa61d352 | 478 | SetClientData(n, oldData); |
2ee3ee1b | 479 | else if ( oldObjData ) |
aa61d352 | 480 | SetClientObject(n, oldObjData); |
2ee3ee1b | 481 | |
781bdbb4 JS |
482 | // we may have lost the selection |
483 | if ( wasSelected ) | |
aa61d352 | 484 | Select(n); |
31582e4e | 485 | |
316bba0c | 486 | MSWOnItemsChanged(); |
2ee3ee1b VZ |
487 | } |
488 | ||
aa61d352 | 489 | unsigned int wxListBox::GetCount() const |
2ee3ee1b VZ |
490 | { |
491 | return m_noItems; | |
492 | } | |
493 | ||
494 | // ---------------------------------------------------------------------------- | |
fca418ae | 495 | // size-related stuff |
2ee3ee1b VZ |
496 | // ---------------------------------------------------------------------------- |
497 | ||
2bda0e17 KB |
498 | void wxListBox::SetHorizontalExtent(const wxString& s) |
499 | { | |
fca418ae VZ |
500 | // the rest is only necessary if we want a horizontal scrollbar |
501 | if ( !HasFlag(wxHSCROLL) ) | |
dd3c394a | 502 | return; |
dd3c394a | 503 | |
dd3c394a | 504 | |
fca418ae VZ |
505 | WindowHDC dc(GetHwnd()); |
506 | SelectInHDC selFont(dc, GetHfontOf(GetFont())); | |
dd3c394a | 507 | |
fca418ae VZ |
508 | TEXTMETRIC lpTextMetric; |
509 | ::GetTextMetrics(dc, &lpTextMetric); | |
dd3c394a | 510 | |
fca418ae VZ |
511 | int largestExtent = 0; |
512 | SIZE extentXY; | |
3e3be693 | 513 | |
fca418ae VZ |
514 | if ( s.empty() ) |
515 | { | |
516 | // set extent to the max length of all strings | |
517 | for ( unsigned int i = 0; i < m_noItems; i++ ) | |
dd3c394a | 518 | { |
fca418ae | 519 | const wxString str = GetString(i); |
767b35a5 | 520 | ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY); |
fca418ae | 521 | |
dd3c394a | 522 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
fca418ae | 523 | if ( extentX > largestExtent ) |
dd3c394a VZ |
524 | largestExtent = extentX; |
525 | } | |
fca418ae VZ |
526 | } |
527 | else // just increase the extent to the length of this string | |
528 | { | |
529 | int existingExtent = (int)SendMessage(GetHwnd(), | |
530 | LB_GETHORIZONTALEXTENT, 0, 0L); | |
dd3c394a | 531 | |
fca418ae VZ |
532 | ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY); |
533 | ||
534 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
535 | if ( extentX > existingExtent ) | |
536 | largestExtent = extentX; | |
2bda0e17 | 537 | } |
fca418ae VZ |
538 | |
539 | if ( largestExtent ) | |
540 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
541 | //else: it shouldn't change | |
2bda0e17 KB |
542 | } |
543 | ||
743b4266 | 544 | wxSize wxListBox::DoGetBestClientSize() const |
b908d224 VZ |
545 | { |
546 | // find the widest string | |
547 | int wLine; | |
548 | int wListbox = 0; | |
aa61d352 | 549 | for (unsigned int i = 0; i < m_noItems; i++) |
b908d224 VZ |
550 | { |
551 | wxString str(GetString(i)); | |
552 | GetTextExtent(str, &wLine, NULL); | |
553 | if ( wLine > wListbox ) | |
554 | wListbox = wLine; | |
555 | } | |
556 | ||
557 | // give it some reasonable default value if there are no strings in the | |
558 | // list | |
559 | if ( wListbox == 0 ) | |
560 | wListbox = 100; | |
561 | ||
562 | // the listbox should be slightly larger than the widest string | |
743b4266 | 563 | wListbox += 3*GetCharWidth(); |
b908d224 | 564 | |
743b4266 | 565 | // add room for the scrollbar |
908f91bc RD |
566 | wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); |
567 | ||
5b6d6b70 VZ |
568 | // don't make the listbox too tall (limit height to 10 items) but don't |
569 | // make it too small neither | |
743b4266 | 570 | int hListbox = SendMessage(GetHwnd(), LB_GETITEMHEIGHT, 0, 0)* |
5b6d6b70 | 571 | wxMin(wxMax(m_noItems, 3), 10); |
b908d224 | 572 | |
743b4266 | 573 | return wxSize(wListbox, hListbox); |
b908d224 VZ |
574 | } |
575 | ||
2ee3ee1b VZ |
576 | // ---------------------------------------------------------------------------- |
577 | // callbacks | |
578 | // ---------------------------------------------------------------------------- | |
579 | ||
580 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 581 | { |
bada28f0 | 582 | wxEventType evtType; |
f2eb4ad2 | 583 | int n = wxNOT_FOUND; |
8ee9d618 | 584 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 585 | { |
53f60d4a VZ |
586 | if ( HasMultipleSelection() ) |
587 | return CalcAndSendEvent(); | |
588 | ||
bada28f0 | 589 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; |
44e7bad0 | 590 | |
f2eb4ad2 VZ |
591 | if ( m_selectedByKeyboard ) |
592 | { | |
593 | // We shouldn't use the mouse position to find the item as mouse | |
594 | // can be anywhere, ask the listbox itself. Notice that this can't | |
595 | // be used when the item is selected using the mouse however as | |
596 | // LB_GETCARETINDEX will always return a valid item, even if the | |
597 | // mouse is clicked below all the items, which is why we find the | |
598 | // item ourselves below in this case. | |
599 | n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
600 | } | |
f186bd4d | 601 | //else: n will be determined below from the mouse position |
2bda0e17 | 602 | } |
8ee9d618 | 603 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b | 604 | { |
bada28f0 VZ |
605 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
606 | } | |
607 | else | |
608 | { | |
609 | // some event we're not interested in | |
598ddd96 | 610 | return false; |
bada28f0 VZ |
611 | } |
612 | ||
f186bd4d VZ |
613 | // Find the item position if it was a mouse-generated selection event or a |
614 | // double click event (which is always generated using the mouse) | |
615 | if ( n == wxNOT_FOUND ) | |
616 | { | |
617 | const DWORD pos = ::GetMessagePos(); | |
618 | const wxPoint pt(GET_X_LPARAM(pos), GET_Y_LPARAM(pos)); | |
619 | n = HitTest(ScreenToClient(wxPoint(pt))); | |
620 | } | |
621 | ||
622 | // We get events even when mouse is clicked outside of any valid item from | |
623 | // Windows, just ignore them. | |
c07b0669 VZ |
624 | if ( n == wxNOT_FOUND ) |
625 | return false; | |
626 | ||
c07b0669 VZ |
627 | if ( param == LBN_SELCHANGE ) |
628 | { | |
24ee1bef | 629 | if ( !DoChangeSingleSelection(n) ) |
c07b0669 | 630 | return false; |
c07b0669 VZ |
631 | } |
632 | ||
633 | // Do generate an event otherwise. | |
634 | return SendEvent(evtType, n, true /* selection */); | |
2bda0e17 KB |
635 | } |
636 | ||
f2eb4ad2 VZ |
637 | WXLRESULT |
638 | wxListBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
639 | { | |
640 | // Remember whether there was a keyboard or mouse event before | |
641 | // LBN_SELCHANGE: this allows us to correctly determine the item affected | |
642 | // by it in MSWCommand() above in any case. | |
643 | if ( WM_KEYFIRST <= nMsg && nMsg <= WM_KEYLAST ) | |
644 | m_selectedByKeyboard = true; | |
645 | else if ( WM_MOUSEFIRST <= nMsg && nMsg <= WM_MOUSELAST ) | |
646 | m_selectedByKeyboard = false; | |
647 | ||
648 | return wxListBoxBase::MSWWindowProc(nMsg, wParam, lParam); | |
649 | } | |
650 | ||
2ee3ee1b | 651 | // ---------------------------------------------------------------------------- |
e1009295 | 652 | // owner-drawn list boxes support |
2ee3ee1b | 653 | // ---------------------------------------------------------------------------- |
2bda0e17 | 654 | |
47d67540 | 655 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 656 | |
85ee88cd VZ |
657 | // misc overloaded methods |
658 | // ----------------------- | |
659 | ||
85ee88cd VZ |
660 | bool wxListBox::SetFont(const wxFont &font) |
661 | { | |
e1009295 VZ |
662 | if ( HasFlag(wxLB_OWNERDRAW) ) |
663 | { | |
664 | const unsigned count = m_aItems.GetCount(); | |
665 | for ( unsigned i = 0; i < count; i++ ) | |
666 | m_aItems[i]->SetFont(font); | |
667 | } | |
85ee88cd VZ |
668 | |
669 | wxListBoxBase::SetFont(font); | |
670 | ||
671 | return true; | |
672 | } | |
673 | ||
674 | bool wxListBox::GetItemRect(size_t n, wxRect& rect) const | |
675 | { | |
676 | wxCHECK_MSG( IsValid(n), false, | |
677 | wxT("invalid index in wxListBox::GetItemRect") ); | |
678 | ||
679 | RECT rc; | |
680 | ||
681 | if ( ListBox_GetItemRect(GetHwnd(), n, &rc) != LB_ERR ) | |
682 | { | |
683 | rect = wxRectFromRECT(rc); | |
684 | return true; | |
685 | } | |
686 | else | |
687 | { | |
688 | // couldn't retrieve rect: for example, item isn't visible | |
689 | return false; | |
690 | } | |
691 | } | |
692 | ||
693 | bool wxListBox::RefreshItem(size_t n) | |
694 | { | |
695 | wxRect rect; | |
696 | if ( !GetItemRect(n, rect) ) | |
697 | return false; | |
698 | ||
699 | RECT rc; | |
700 | wxCopyRectToRECT(rect, rc); | |
701 | ||
702 | return ::InvalidateRect((HWND)GetHWND(), &rc, FALSE) == TRUE; | |
703 | } | |
704 | ||
705 | ||
2bda0e17 KB |
706 | // drawing |
707 | // ------- | |
708 | ||
85ee88cd VZ |
709 | namespace |
710 | { | |
711 | // space beneath/above each row in pixels | |
712 | static const int LISTBOX_EXTRA_SPACE = 1; | |
713 | ||
714 | } // anonymous namespace | |
2bda0e17 KB |
715 | |
716 | // the height is the same for all items | |
dd3c394a VZ |
717 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
718 | ||
719 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
720 | // message is not yet sent when we get here! | |
2bda0e17 KB |
721 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
722 | { | |
dd3c394a | 723 | // only owner-drawn control should receive this message |
e1009295 | 724 | wxCHECK( HasFlag(wxLB_OWNERDRAW), false ); |
2bda0e17 | 725 | |
dd3c394a | 726 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 727 | |
4676948b JS |
728 | #ifdef __WXWINCE__ |
729 | HDC hdc = GetDC(NULL); | |
730 | #else | |
07cf98cb | 731 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
4676948b | 732 | #endif |
07cf98cb | 733 | |
7d09b97f VZ |
734 | { |
735 | wxDCTemp dc((WXHDC)hdc); | |
736 | dc.SetFont(GetFont()); | |
2bda0e17 | 737 | |
85ee88cd | 738 | pStruct->itemHeight = dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE; |
7d09b97f VZ |
739 | pStruct->itemWidth = dc.GetCharWidth(); |
740 | } | |
07cf98cb | 741 | |
7d09b97f VZ |
742 | #ifdef __WXWINCE__ |
743 | ReleaseDC(NULL, hdc); | |
744 | #else | |
07cf98cb | 745 | DeleteDC(hdc); |
7d09b97f | 746 | #endif |
07cf98cb | 747 | |
598ddd96 | 748 | return true; |
2bda0e17 KB |
749 | } |
750 | ||
751 | // forward the message to the appropriate item | |
752 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
753 | { | |
dd3c394a | 754 | // only owner-drawn control should receive this message |
e1009295 | 755 | wxCHECK( HasFlag(wxLB_OWNERDRAW), false ); |
dd3c394a VZ |
756 | |
757 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
f6bcfd97 BP |
758 | |
759 | // the item may be -1 for an empty listbox | |
e4de7a77 | 760 | if ( pStruct->itemID == (UINT)-1 ) |
598ddd96 | 761 | return false; |
dd3c394a | 762 | |
e4de7a77 | 763 | wxListBoxItem *pItem = (wxListBoxItem *)m_aItems[pStruct->itemID]; |
2bda0e17 | 764 | |
7561aacd | 765 | wxDCTemp dc((WXHDC)pStruct->hDC); |
2bda0e17 | 766 | |
6efda5fd | 767 | return pItem->OnDrawItem(dc, wxRectFromRECT(pStruct->rcItem), |
7561aacd | 768 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, |
98fbab9e | 769 | (wxOwnerDrawn::wxODStatus)(pStruct->itemState | wxOwnerDrawn::wxODHidePrefix)); |
2bda0e17 KB |
770 | } |
771 | ||
1e6feb95 VZ |
772 | #endif // wxUSE_OWNER_DRAWN |
773 | ||
774 | #endif // wxUSE_LISTBOX |