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