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