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