]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/listbox.cpp | |
3 | // Purpose: wxListBox | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin (owner drawn stuff) | |
6 | // Created: | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_LISTBOX | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/listbox.h" | |
27 | #include "wx/settings.h" | |
28 | #include "wx/brush.h" | |
29 | #include "wx/font.h" | |
30 | #include "wx/dc.h" | |
31 | #include "wx/utils.h" | |
32 | #endif | |
33 | ||
34 | #include "wx/window.h" | |
35 | #include "wx/msw/private.h" | |
36 | ||
37 | #include <windowsx.h> | |
38 | ||
39 | #include "wx/dynarray.h" | |
40 | #include "wx/log.h" | |
41 | ||
42 | #if wxUSE_OWNER_DRAWN | |
43 | #include "wx/ownerdrw.h" | |
44 | #endif | |
45 | ||
46 | #ifdef __GNUWIN32_OLD__ | |
47 | #include "wx/msw/gnuwin32/extra.h" | |
48 | #endif | |
49 | ||
50 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) | |
51 | ||
52 | /* | |
53 | TODO PROPERTIES | |
54 | selection | |
55 | content | |
56 | item | |
57 | */ | |
58 | ||
59 | // ============================================================================ | |
60 | // list box item declaration and implementation | |
61 | // ============================================================================ | |
62 | ||
63 | #if wxUSE_OWNER_DRAWN | |
64 | ||
65 | class wxListBoxItem : public wxOwnerDrawn | |
66 | { | |
67 | public: | |
68 | wxListBoxItem(const wxString& str = wxEmptyString); | |
69 | }; | |
70 | ||
71 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
72 | { | |
73 | // no bitmaps/checkmarks | |
74 | SetMarginWidth(0); | |
75 | } | |
76 | ||
77 | wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) | |
78 | { | |
79 | return new wxListBoxItem(); | |
80 | } | |
81 | ||
82 | #endif //USE_OWNER_DRAWN | |
83 | ||
84 | // ============================================================================ | |
85 | // list box control implementation | |
86 | // ============================================================================ | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // creation | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | // Listbox item | |
93 | wxListBox::wxListBox() | |
94 | { | |
95 | m_noItems = 0; | |
96 | m_selected = 0; | |
97 | } | |
98 | ||
99 | bool wxListBox::Create(wxWindow *parent, | |
100 | wxWindowID id, | |
101 | const wxPoint& pos, | |
102 | const wxSize& size, | |
103 | int n, const wxString choices[], | |
104 | long style, | |
105 | const wxValidator& validator, | |
106 | const wxString& name) | |
107 | { | |
108 | m_noItems = 0; | |
109 | m_hWnd = 0; | |
110 | m_selected = 0; | |
111 | ||
112 | SetName(name); | |
113 | #if wxUSE_VALIDATORS | |
114 | SetValidator(validator); | |
115 | #endif // wxUSE_VALIDATORS | |
116 | ||
117 | if (parent) | |
118 | parent->AddChild(this); | |
119 | ||
120 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
121 | SetForegroundColour(parent->GetForegroundColour()); | |
122 | ||
123 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; | |
124 | ||
125 | int x = pos.x; | |
126 | int y = pos.y; | |
127 | int width = size.x; | |
128 | int height = size.y; | |
129 | m_windowStyle = style; | |
130 | ||
131 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_TABSTOP | | |
132 | LBS_NOTIFY | LBS_HASSTRINGS ; | |
133 | ||
134 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
135 | _T("only one of listbox selection modes can be specified") ); | |
136 | ||
137 | if ( (m_windowStyle & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
138 | m_windowStyle |= wxBORDER_SUNKEN; | |
139 | ||
140 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
141 | wstyle |= WS_CLIPSIBLINGS; | |
142 | ||
143 | if (m_windowStyle & wxLB_MULTIPLE) | |
144 | wstyle |= LBS_MULTIPLESEL; | |
145 | else if (m_windowStyle & wxLB_EXTENDED) | |
146 | wstyle |= LBS_EXTENDEDSEL; | |
147 | ||
148 | if (m_windowStyle & wxLB_ALWAYS_SB) | |
149 | wstyle |= LBS_DISABLENOSCROLL; | |
150 | if (m_windowStyle & wxLB_HSCROLL) | |
151 | wstyle |= WS_HSCROLL; | |
152 | if (m_windowStyle & wxLB_SORT) | |
153 | wstyle |= LBS_SORT; | |
154 | ||
155 | #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__) | |
156 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
157 | // we don't support LBS_OWNERDRAWVARIABLE yet | |
158 | wstyle |= LBS_OWNERDRAWFIXED; | |
159 | } | |
160 | #endif | |
161 | ||
162 | // Without this style, you get unexpected heights, so e.g. constraint layout | |
163 | // doesn't work properly | |
164 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
165 | ||
166 | WXDWORD exStyle = 0; | |
167 | (void) MSWGetStyle(m_windowStyle, & exStyle) ; | |
168 | ||
169 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL, | |
170 | wstyle , | |
171 | 0, 0, 0, 0, | |
172 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
173 | wxGetInstance(), NULL); | |
174 | ||
175 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") ); | |
176 | ||
177 | // Subclass again to catch messages | |
178 | SubclassWin(m_hWnd); | |
179 | ||
180 | size_t ui; | |
181 | for (ui = 0; ui < (size_t)n; ui++) { | |
182 | Append(choices[ui]); | |
183 | } | |
184 | ||
185 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) | |
186 | SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0); | |
187 | ||
188 | SetFont(parent->GetFont()); | |
189 | ||
190 | SetSize(x, y, width, height); | |
191 | ||
192 | return TRUE; | |
193 | } | |
194 | ||
195 | wxListBox::~wxListBox() | |
196 | { | |
197 | Free(); | |
198 | } | |
199 | ||
200 | void wxListBox::SetupColours() | |
201 | { | |
202 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
203 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
204 | } | |
205 | ||
206 | // ---------------------------------------------------------------------------- | |
207 | // implementation of wxListBoxBase methods | |
208 | // ---------------------------------------------------------------------------- | |
209 | ||
210 | void wxListBox::DoSetFirstItem(int N) | |
211 | { | |
212 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
213 | wxT("invalid index in wxListBox::SetFirstItem") ); | |
214 | ||
215 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); | |
216 | } | |
217 | ||
218 | void wxListBox::Delete(int N) | |
219 | { | |
220 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
221 | wxT("invalid index in wxListBox::Delete") ); | |
222 | ||
223 | // for owner drawn objects, the data is used for storing wxOwnerDrawn | |
224 | // pointers and we shouldn't touch it | |
225 | #if !wxUSE_OWNER_DRAWN | |
226 | if ( !(m_windowStyle & wxLB_OWNERDRAW) ) | |
227 | #endif // !wxUSE_OWNER_DRAWN | |
228 | if ( HasClientObjectData() ) | |
229 | { | |
230 | delete GetClientObject(N); | |
231 | } | |
232 | ||
233 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
234 | m_noItems--; | |
235 | ||
236 | SetHorizontalExtent(wxEmptyString); | |
237 | } | |
238 | ||
239 | int wxListBox::DoAppend(const wxString& item) | |
240 | { | |
241 | int index = ListBox_AddString(GetHwnd(), item); | |
242 | m_noItems++; | |
243 | ||
244 | #if wxUSE_OWNER_DRAWN | |
245 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
246 | wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument | |
247 | pNewItem->SetName(item); | |
248 | m_aItems.Insert(pNewItem, index); | |
249 | ListBox_SetItemData(GetHwnd(), index, pNewItem); | |
250 | pNewItem->SetFont(GetFont()); | |
251 | } | |
252 | #endif // wxUSE_OWNER_DRAWN | |
253 | ||
254 | SetHorizontalExtent(item); | |
255 | ||
256 | return index; | |
257 | } | |
258 | ||
259 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) | |
260 | { | |
261 | // avoid flicker - but don't need to do this for a hidden listbox | |
262 | bool hideAndShow = IsShown(); | |
263 | if ( hideAndShow ) | |
264 | { | |
265 | ShowWindow(GetHwnd(), SW_HIDE); | |
266 | } | |
267 | ||
268 | ListBox_ResetContent(GetHwnd()); | |
269 | ||
270 | m_noItems = choices.GetCount(); | |
271 | int i; | |
272 | for (i = 0; i < m_noItems; i++) | |
273 | { | |
274 | ListBox_AddString(GetHwnd(), choices[i]); | |
275 | if ( clientData ) | |
276 | { | |
277 | SetClientData(i, clientData[i]); | |
278 | } | |
279 | } | |
280 | ||
281 | #if wxUSE_OWNER_DRAWN | |
282 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
283 | // first delete old items | |
284 | WX_CLEAR_ARRAY(m_aItems); | |
285 | ||
286 | // then create new ones | |
287 | for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
288 | wxOwnerDrawn *pNewItem = CreateLboxItem(ui); | |
289 | pNewItem->SetName(choices[ui]); | |
290 | m_aItems.Add(pNewItem); | |
291 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); | |
292 | } | |
293 | } | |
294 | #endif // wxUSE_OWNER_DRAWN | |
295 | ||
296 | SetHorizontalExtent(); | |
297 | ||
298 | if ( hideAndShow ) | |
299 | { | |
300 | // show the listbox back if we hid it | |
301 | ShowWindow(GetHwnd(), SW_SHOW); | |
302 | } | |
303 | } | |
304 | ||
305 | int wxListBox::FindString(const wxString& s) const | |
306 | { | |
307 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); | |
308 | if (pos == LB_ERR) | |
309 | return wxNOT_FOUND; | |
310 | else | |
311 | return pos; | |
312 | } | |
313 | ||
314 | void wxListBox::Clear() | |
315 | { | |
316 | Free(); | |
317 | ||
318 | ListBox_ResetContent(GetHwnd()); | |
319 | ||
320 | m_noItems = 0; | |
321 | SetHorizontalExtent(); | |
322 | } | |
323 | ||
324 | void wxListBox::Free() | |
325 | { | |
326 | #if wxUSE_OWNER_DRAWN | |
327 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
328 | { | |
329 | WX_CLEAR_ARRAY(m_aItems); | |
330 | } | |
331 | else | |
332 | #endif // wxUSE_OWNER_DRAWN | |
333 | if ( HasClientObjectData() ) | |
334 | { | |
335 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
336 | { | |
337 | delete GetClientObject(n); | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | void wxListBox::SetSelection(int N, bool select) | |
343 | { | |
344 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
345 | wxT("invalid index in wxListBox::SetSelection") ); | |
346 | ||
347 | if ( HasMultipleSelection() ) | |
348 | { | |
349 | SendMessage(GetHwnd(), LB_SETSEL, select, N); | |
350 | } | |
351 | else | |
352 | { | |
353 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); | |
354 | } | |
355 | } | |
356 | ||
357 | bool wxListBox::IsSelected(int N) const | |
358 | { | |
359 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
360 | wxT("invalid index in wxListBox::Selected") ); | |
361 | ||
362 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; | |
363 | } | |
364 | ||
365 | wxClientData* wxListBox::DoGetItemClientObject(int n) const | |
366 | { | |
367 | return (wxClientData *)DoGetItemClientData(n); | |
368 | } | |
369 | ||
370 | void *wxListBox::DoGetItemClientData(int n) const | |
371 | { | |
372 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, | |
373 | wxT("invalid index in wxListBox::GetClientData") ); | |
374 | ||
375 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); | |
376 | } | |
377 | ||
378 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
379 | { | |
380 | DoSetItemClientData(n, clientData); | |
381 | } | |
382 | ||
383 | void wxListBox::DoSetItemClientData(int n, void *clientData) | |
384 | { | |
385 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
386 | wxT("invalid index in wxListBox::SetClientData") ); | |
387 | ||
388 | #if wxUSE_OWNER_DRAWN | |
389 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
390 | { | |
391 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
392 | // in OnMeasure/OnDraw. | |
393 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
394 | } | |
395 | #endif // wxUSE_OWNER_DRAWN | |
396 | ||
397 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
398 | wxLogDebug(wxT("LB_SETITEMDATA failed")); | |
399 | } | |
400 | ||
401 | // Return number of selections and an array of selected integers | |
402 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
403 | { | |
404 | aSelections.Empty(); | |
405 | ||
406 | if ( HasMultipleSelection() ) | |
407 | { | |
408 | int countSel = ListBox_GetSelCount(GetHwnd()); | |
409 | if ( countSel == LB_ERR ) | |
410 | { | |
411 | wxLogDebug(_T("ListBox_GetSelCount failed")); | |
412 | } | |
413 | else if ( countSel != 0 ) | |
414 | { | |
415 | int *selections = new int[countSel]; | |
416 | ||
417 | if ( ListBox_GetSelItems(GetHwnd(), | |
418 | countSel, selections) == LB_ERR ) | |
419 | { | |
420 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
421 | countSel = -1; | |
422 | } | |
423 | else | |
424 | { | |
425 | aSelections.Alloc(countSel); | |
426 | for ( int n = 0; n < countSel; n++ ) | |
427 | aSelections.Add(selections[n]); | |
428 | } | |
429 | ||
430 | delete [] selections; | |
431 | } | |
432 | ||
433 | return countSel; | |
434 | } | |
435 | else // single-selection listbox | |
436 | { | |
437 | if (ListBox_GetCurSel(GetHwnd()) > -1) | |
438 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
439 | ||
440 | return aSelections.Count(); | |
441 | } | |
442 | } | |
443 | ||
444 | // Get single selection, for single choice list items | |
445 | int wxListBox::GetSelection() const | |
446 | { | |
447 | wxCHECK_MSG( !HasMultipleSelection(), | |
448 | -1, | |
449 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); | |
450 | ||
451 | return ListBox_GetCurSel(GetHwnd()); | |
452 | } | |
453 | ||
454 | // Find string for position | |
455 | wxString wxListBox::GetString(int N) const | |
456 | { | |
457 | wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString, | |
458 | wxT("invalid index in wxListBox::GetClientData") ); | |
459 | ||
460 | int len = ListBox_GetTextLen(GetHwnd(), N); | |
461 | ||
462 | // +1 for terminating NUL | |
463 | wxString result; | |
464 | ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1)); | |
465 | ||
466 | return result; | |
467 | } | |
468 | ||
469 | void | |
470 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
471 | { | |
472 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
473 | wxT("invalid index in wxListBox::InsertItems") ); | |
474 | ||
475 | int nItems = items.GetCount(); | |
476 | for ( int i = 0; i < nItems; i++ ) | |
477 | { | |
478 | int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
479 | ||
480 | #if wxUSE_OWNER_DRAWN | |
481 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
482 | { | |
483 | wxOwnerDrawn *pNewItem = CreateLboxItem(idx); | |
484 | pNewItem->SetName(items[i]); | |
485 | pNewItem->SetFont(GetFont()); | |
486 | m_aItems.Insert(pNewItem, idx); | |
487 | ||
488 | ListBox_SetItemData(GetHwnd(), idx, pNewItem); | |
489 | } | |
490 | #endif // wxUSE_OWNER_DRAWN | |
491 | } | |
492 | ||
493 | m_noItems += nItems; | |
494 | ||
495 | SetHorizontalExtent(); | |
496 | } | |
497 | ||
498 | void wxListBox::SetString(int N, const wxString& s) | |
499 | { | |
500 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
501 | wxT("invalid index in wxListBox::SetString") ); | |
502 | ||
503 | // remember the state of the item | |
504 | bool wasSelected = IsSelected(N); | |
505 | ||
506 | void *oldData = NULL; | |
507 | wxClientData *oldObjData = NULL; | |
508 | if ( m_clientDataItemsType == wxClientData_Void ) | |
509 | oldData = GetClientData(N); | |
510 | else if ( m_clientDataItemsType == wxClientData_Object ) | |
511 | oldObjData = GetClientObject(N); | |
512 | ||
513 | // delete and recreate it | |
514 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
515 | ||
516 | int newN = N; | |
517 | if ( N == m_noItems - 1 ) | |
518 | newN = -1; | |
519 | ||
520 | ListBox_InsertString(GetHwnd(), newN, s); | |
521 | ||
522 | // restore the client data | |
523 | if ( oldData ) | |
524 | SetClientData(N, oldData); | |
525 | else if ( oldObjData ) | |
526 | SetClientObject(N, oldObjData); | |
527 | ||
528 | // we may have lost the selection | |
529 | if ( wasSelected ) | |
530 | Select(N); | |
531 | ||
532 | #if wxUSE_OWNER_DRAWN | |
533 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
534 | { | |
535 | // update item's text | |
536 | m_aItems[N]->SetName(s); | |
537 | ||
538 | // reassign the item's data | |
539 | ListBox_SetItemData(GetHwnd(), N, m_aItems[N]); | |
540 | } | |
541 | #endif //USE_OWNER_DRAWN | |
542 | } | |
543 | ||
544 | int wxListBox::GetCount() const | |
545 | { | |
546 | return m_noItems; | |
547 | } | |
548 | ||
549 | // ---------------------------------------------------------------------------- | |
550 | // helpers | |
551 | // ---------------------------------------------------------------------------- | |
552 | ||
553 | // Windows-specific code to set the horizontal extent of the listbox, if | |
554 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. | |
555 | // Otherwise, all strings are used. | |
556 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
557 | { | |
558 | // Only necessary if we want a horizontal scrollbar | |
559 | if (!(m_windowStyle & wxHSCROLL)) | |
560 | return; | |
561 | TEXTMETRIC lpTextMetric; | |
562 | ||
563 | if ( !s.IsEmpty() ) | |
564 | { | |
565 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); | |
566 | HDC dc = GetWindowDC(GetHwnd()); | |
567 | HFONT oldFont = 0; | |
568 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
569 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
570 | ||
571 | GetTextMetrics(dc, &lpTextMetric); | |
572 | SIZE extentXY; | |
573 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); | |
574 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
575 | ||
576 | if (oldFont) | |
577 | ::SelectObject(dc, oldFont); | |
578 | ||
579 | ReleaseDC(GetHwnd(), dc); | |
580 | if (extentX > existingExtent) | |
581 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); | |
582 | } | |
583 | else | |
584 | { | |
585 | int largestExtent = 0; | |
586 | HDC dc = GetWindowDC(GetHwnd()); | |
587 | HFONT oldFont = 0; | |
588 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
589 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
590 | ||
591 | GetTextMetrics(dc, &lpTextMetric); | |
592 | ||
593 | // FIXME: buffer overflow!! | |
594 | wxChar buf[1024]; | |
595 | for (int i = 0; i < m_noItems; i++) | |
596 | { | |
597 | int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf); | |
598 | buf[len] = 0; | |
599 | SIZE extentXY; | |
600 | ::GetTextExtentPoint(dc, buf, len, &extentXY); | |
601 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
602 | if (extentX > largestExtent) | |
603 | largestExtent = extentX; | |
604 | } | |
605 | if (oldFont) | |
606 | ::SelectObject(dc, oldFont); | |
607 | ||
608 | ReleaseDC(GetHwnd(), dc); | |
609 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
610 | } | |
611 | } | |
612 | ||
613 | wxSize wxListBox::DoGetBestSize() const | |
614 | { | |
615 | // find the widest string | |
616 | int wLine; | |
617 | int wListbox = 0; | |
618 | for ( int i = 0; i < m_noItems; i++ ) | |
619 | { | |
620 | wxString str(GetString(i)); | |
621 | GetTextExtent(str, &wLine, NULL); | |
622 | if ( wLine > wListbox ) | |
623 | wListbox = wLine; | |
624 | } | |
625 | ||
626 | // give it some reasonable default value if there are no strings in the | |
627 | // list | |
628 | if ( wListbox == 0 ) | |
629 | wListbox = 100; | |
630 | ||
631 | // the listbox should be slightly larger than the widest string | |
632 | int cx, cy; | |
633 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
634 | ||
635 | wListbox += 3*cx; | |
636 | ||
637 | // don't make the listbox too tall (limit height to 10 items) but don't | |
638 | // make it too small neither | |
639 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* | |
640 | wxMin(wxMax(m_noItems, 3), 10); | |
641 | ||
642 | return wxSize(wListbox, hListbox); | |
643 | } | |
644 | ||
645 | // ---------------------------------------------------------------------------- | |
646 | // callbacks | |
647 | // ---------------------------------------------------------------------------- | |
648 | ||
649 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
650 | { | |
651 | wxEventType evtType; | |
652 | if ( param == LBN_SELCHANGE ) | |
653 | { | |
654 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
655 | } | |
656 | else if ( param == LBN_DBLCLK ) | |
657 | { | |
658 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; | |
659 | } | |
660 | else | |
661 | { | |
662 | // some event we're not interested in | |
663 | return FALSE; | |
664 | } | |
665 | ||
666 | wxCommandEvent event(evtType, m_windowId); | |
667 | event.SetEventObject( this ); | |
668 | ||
669 | // retrieve the affected item | |
670 | int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
671 | if ( n != LB_ERR ) | |
672 | { | |
673 | if ( HasClientObjectData() ) | |
674 | event.SetClientObject( GetClientObject(n) ); | |
675 | else if ( HasClientUntypedData() ) | |
676 | event.SetClientData( GetClientData(n) ); | |
677 | ||
678 | event.SetString( GetString(n) ); | |
679 | event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE ); | |
680 | } | |
681 | ||
682 | event.m_commandInt = n; | |
683 | ||
684 | return GetEventHandler()->ProcessEvent(event); | |
685 | } | |
686 | ||
687 | // ---------------------------------------------------------------------------- | |
688 | // wxCheckListBox support | |
689 | // ---------------------------------------------------------------------------- | |
690 | ||
691 | #if wxUSE_OWNER_DRAWN | |
692 | ||
693 | // drawing | |
694 | // ------- | |
695 | ||
696 | // space beneath/above each row in pixels | |
697 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
698 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
699 | ||
700 | // the height is the same for all items | |
701 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes | |
702 | ||
703 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
704 | // message is not yet sent when we get here! | |
705 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
706 | { | |
707 | // only owner-drawn control should receive this message | |
708 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
709 | ||
710 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
711 | ||
712 | #ifdef __WXWINCE__ | |
713 | HDC hdc = GetDC(NULL); | |
714 | #else | |
715 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); | |
716 | #endif | |
717 | ||
718 | wxDC dc; | |
719 | dc.SetHDC((WXHDC)hdc); | |
720 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT)); | |
721 | ||
722 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; | |
723 | pStruct->itemWidth = dc.GetCharWidth(); | |
724 | ||
725 | dc.SetHDC(0); | |
726 | ||
727 | DeleteDC(hdc); | |
728 | ||
729 | return TRUE; | |
730 | } | |
731 | ||
732 | // forward the message to the appropriate item | |
733 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
734 | { | |
735 | // only owner-drawn control should receive this message | |
736 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
737 | ||
738 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
739 | UINT itemID = pStruct->itemID; | |
740 | ||
741 | // the item may be -1 for an empty listbox | |
742 | if ( itemID == (UINT)-1 ) | |
743 | return FALSE; | |
744 | ||
745 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); | |
746 | ||
747 | wxCHECK( data && (data != LB_ERR), FALSE ); | |
748 | ||
749 | wxListBoxItem *pItem = (wxListBoxItem *)data; | |
750 | ||
751 | wxDCTemp dc((WXHDC)pStruct->hDC); | |
752 | wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), | |
753 | wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); | |
754 | ||
755 | return pItem->OnDrawItem(dc, rect, | |
756 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
757 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
758 | } | |
759 | ||
760 | #endif // wxUSE_OWNER_DRAWN | |
761 | ||
762 | #endif // wxUSE_LISTBOX |