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