1. wxDropTarget::OnData() returns wxDragResult now, not bool
[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 Free();
234 }
235
236 void wxListBox::SetupColours()
237 {
238 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
239 SetForegroundColour(GetParent()->GetForegroundColour());
240 }
241
242 // ----------------------------------------------------------------------------
243 // implementation of wxListBoxBase methods
244 // ----------------------------------------------------------------------------
245
246 void wxListBox::DoSetFirstItem(int N)
247 {
248 wxCHECK_RET( N >= 0 && N < m_noItems,
249 wxT("invalid index in wxListBox::SetFirstItem") );
250
251 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
252 }
253
254 void wxListBox::Delete(int N)
255 {
256 wxCHECK_RET( N >= 0 && N < m_noItems,
257 wxT("invalid index in wxListBox::Delete") );
258
259 #if wxUSE_OWNER_DRAWN
260 delete m_aItems[N];
261 m_aItems.Remove(N);
262 #else // !wxUSE_OWNER_DRAWN
263 if ( HasClientObjectData() )
264 {
265 delete GetClientObject(N);
266 }
267 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
268
269 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
270 m_noItems--;
271
272 SetHorizontalExtent("");
273 }
274
275 int wxListBox::DoAppend(const wxString& item)
276 {
277 int index = ListBox_AddString(GetHwnd(), item);
278 m_noItems++;
279
280 #if wxUSE_OWNER_DRAWN
281 if ( m_windowStyle & wxLB_OWNERDRAW ) {
282 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
283 pNewItem->SetName(item);
284 m_aItems.Add(pNewItem);
285 ListBox_SetItemData(GetHwnd(), index, pNewItem);
286 }
287 #endif
288
289 SetHorizontalExtent(item);
290
291 return index;
292 }
293
294 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
295 {
296 ShowWindow(GetHwnd(), SW_HIDE);
297
298 ListBox_ResetContent(GetHwnd());
299
300 m_noItems = choices.GetCount();
301 int i;
302 for (i = 0; i < m_noItems; i++)
303 {
304 ListBox_AddString(GetHwnd(), choices[i]);
305 if ( clientData )
306 {
307 #if wxUSE_OWNER_DRAWN
308 wxASSERT_MSG(clientData[i] == NULL,
309 wxT("Can't use client data with owner-drawn listboxes"));
310 #else // !wxUSE_OWNER_DRAWN
311 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
312 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
313 }
314 }
315
316 #if wxUSE_OWNER_DRAWN
317 if ( m_windowStyle & wxLB_OWNERDRAW ) {
318 // first delete old items
319 size_t ui = m_aItems.Count();
320 while ( ui-- != 0 ) {
321 delete m_aItems[ui];
322 }
323 m_aItems.Empty();
324
325 // then create new ones
326 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
327 wxOwnerDrawn *pNewItem = CreateItem(ui);
328 pNewItem->SetName(choices[ui]);
329 m_aItems.Add(pNewItem);
330 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
331 }
332 }
333 #endif // wxUSE_OWNER_DRAWN
334
335 SetHorizontalExtent();
336
337 ShowWindow(GetHwnd(), SW_SHOW);
338 }
339
340 int wxListBox::FindString(const wxString& s) const
341 {
342 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
343 if (pos == LB_ERR)
344 return wxNOT_FOUND;
345 else
346 return pos;
347 }
348
349 void wxListBox::Clear()
350 {
351 Free();
352
353 ListBox_ResetContent(GetHwnd());
354
355 m_noItems = 0;
356 SetHorizontalExtent();
357 }
358
359 void wxListBox::Free()
360 #if wxUSE_OWNER_DRAWN
361 size_t uiCount = m_aItems.Count();
362 while ( uiCount-- != 0 ) {
363 delete m_aItems[uiCount];
364 }
365
366 m_aItems.Clear();
367 #else // !wxUSE_OWNER_DRAWN
368 if ( HasClientObjectData() )
369 {
370 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
371 {
372 delete GetClientObject(n);
373 }
374 }
375 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
376 }
377
378 void wxListBox::SetSelection(int N, bool select)
379 {
380 wxCHECK_RET( N >= 0 && N < m_noItems,
381 wxT("invalid index in wxListBox::SetSelection") );
382
383 if ( HasMultipleSelection() )
384 {
385 SendMessage(GetHwnd(), LB_SETSEL, select, N);
386 }
387 else
388 {
389 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
390 }
391 }
392
393 bool wxListBox::IsSelected(int N) const
394 {
395 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
396 wxT("invalid index in wxListBox::Selected") );
397
398 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
399 }
400
401 wxClientData* wxListBox::DoGetItemClientObject(int n) const
402 {
403 return (wxClientData *)DoGetItemClientData(n);
404 }
405
406 void *wxListBox::DoGetItemClientData(int n) const
407 {
408 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
409 wxT("invalid index in wxListBox::GetClientData") );
410
411 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
412 }
413
414 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
415 {
416 DoSetItemClientData(n, clientData);
417 }
418
419 void wxListBox::DoSetItemClientData(int n, void *clientData)
420 {
421 wxCHECK_RET( n >= 0 && n < m_noItems,
422 wxT("invalid index in wxListBox::SetClientData") );
423
424 #if wxUSE_OWNER_DRAWN
425 if ( m_windowStyle & wxLB_OWNERDRAW )
426 {
427 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
428 // in OnMeasure/OnDraw.
429 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
430 }
431 #endif // wxUSE_OWNER_DRAWN
432
433 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
434 wxLogDebug(wxT("LB_SETITEMDATA failed"));
435 }
436
437 bool wxListBox::HasMultipleSelection() const
438 {
439 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
440 }
441
442 // Return number of selections and an array of selected integers
443 int wxListBox::GetSelections(wxArrayInt& aSelections) const
444 {
445 aSelections.Empty();
446
447 if ( HasMultipleSelection() )
448 {
449 int no_sel = ListBox_GetSelCount(GetHwnd());
450 if (no_sel != 0) {
451 int *selections = new int[no_sel];
452 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
453
454 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
455
456 aSelections.Alloc(no_sel);
457 for ( int n = 0; n < no_sel; n++ )
458 aSelections.Add(selections[n]);
459
460 delete [] selections;
461 }
462
463 return no_sel;
464 }
465 else // single-selection listbox
466 {
467 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
468
469 return 1;
470 }
471 }
472
473 // Get single selection, for single choice list items
474 int wxListBox::GetSelection() const
475 {
476 wxCHECK_MSG( !HasMultipleSelection(),
477 -1,
478 wxT("GetSelection() can't be used with multiple-selection "
479 "listboxes, use GetSelections() instead.") );
480
481 return ListBox_GetCurSel(GetHwnd());
482 }
483
484 // Find string for position
485 wxString wxListBox::GetString(int N) const
486 {
487 wxCHECK_MSG( N >= 0 && N < m_noItems, "",
488 wxT("invalid index in wxListBox::GetClientData") );
489
490 int len = ListBox_GetTextLen(GetHwnd(), N);
491
492 // +1 for terminating NUL
493 wxString result;
494 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
495 result.UngetWriteBuf();
496
497 return result;
498 }
499
500 void
501 wxListBox::DoInsertItems(const wxArrayString& items, int pos)
502 {
503 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
504 wxT("invalid index in wxListBox::InsertItems") );
505
506 int nItems = items.GetCount();
507 for ( int i = 0; i < nItems; i++ )
508 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
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 == ClientData_Void )
525 oldData = GetClientData(N);
526 else if ( m_clientDataItemsType == ClientData_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 // update item's text
551 m_aItems[N]->SetName(s);
552 #endif //USE_OWNER_DRAWN
553 }
554
555 int wxListBox::GetCount() const
556 {
557 return m_noItems;
558 }
559
560 // ----------------------------------------------------------------------------
561 // helpers
562 // ----------------------------------------------------------------------------
563
564 // Windows-specific code to set the horizontal extent of the listbox, if
565 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
566 // Otherwise, all strings are used.
567 void wxListBox::SetHorizontalExtent(const wxString& s)
568 {
569 // Only necessary if we want a horizontal scrollbar
570 if (!(m_windowStyle & wxHSCROLL))
571 return;
572 TEXTMETRIC lpTextMetric;
573
574 if ( !s.IsEmpty() )
575 {
576 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
577 HDC dc = GetWindowDC(GetHwnd());
578 HFONT oldFont = 0;
579 if (GetFont().Ok() && GetFont().GetResourceHandle())
580 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
581
582 GetTextMetrics(dc, &lpTextMetric);
583 SIZE extentXY;
584 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
585 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
586
587 if (oldFont)
588 ::SelectObject(dc, oldFont);
589
590 ReleaseDC(GetHwnd(), dc);
591 if (extentX > existingExtent)
592 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
593 }
594 else
595 {
596 int largestExtent = 0;
597 HDC dc = GetWindowDC(GetHwnd());
598 HFONT oldFont = 0;
599 if (GetFont().Ok() && GetFont().GetResourceHandle())
600 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
601
602 GetTextMetrics(dc, &lpTextMetric);
603 int i;
604 for (i = 0; i < m_noItems; i++)
605 {
606 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
607 wxBuffer[len] = 0;
608 SIZE extentXY;
609 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
610 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
611 if (extentX > largestExtent)
612 largestExtent = extentX;
613 }
614 if (oldFont)
615 ::SelectObject(dc, oldFont);
616
617 ReleaseDC(GetHwnd(), dc);
618 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
619 }
620 }
621
622 wxSize wxListBox::DoGetBestSize()
623 {
624 // find the widest string
625 int wLine;
626 int wListbox = 0;
627 for ( int i = 0; i < m_noItems; i++ )
628 {
629 wxString str(GetString(i));
630 GetTextExtent(str, &wLine, NULL);
631 if ( wLine > wListbox )
632 wListbox = wLine;
633 }
634
635 // give it some reasonable default value if there are no strings in the
636 // list
637 if ( wListbox == 0 )
638 wListbox = 100;
639
640 // the listbox should be slightly larger than the widest string
641 int cx, cy;
642 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
643
644 wListbox += 3*cx;
645
646 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7));
647
648 return wxSize(wListbox, hListbox);
649 }
650
651 // ----------------------------------------------------------------------------
652 // callbacks
653 // ----------------------------------------------------------------------------
654
655 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
656 {
657 if ( param == LBN_SELCHANGE )
658 {
659 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
660 event.SetEventObject( this );
661
662 wxArrayInt aSelections;
663 int n, count = GetSelections(aSelections);
664 if ( count > 0 )
665 {
666 n = aSelections[0];
667 if ( HasClientObjectData() )
668 event.SetClientObject( GetClientObject(n) );
669 else if ( HasClientUntypedData() )
670 event.SetClientData( GetClientData(n) );
671 event.SetString( GetString(n) );
672 }
673 else
674 {
675 n = -1;
676 }
677
678 event.m_commandInt = n;
679
680 return GetEventHandler()->ProcessEvent(event);
681 }
682 else if ( param == LBN_DBLCLK )
683 {
684 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
685 event.SetEventObject( this );
686
687 return GetEventHandler()->ProcessEvent(event);
688 }
689 //else:
690
691 return FALSE;
692 }
693
694 WXHBRUSH wxListBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
695 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
696 {
697 #if wxUSE_CTL3D
698 if ( m_useCtl3D )
699 {
700 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
701 return (WXHBRUSH) hbrush;
702 }
703 #endif
704
705 if (GetParent()->GetTransparentBackground())
706 SetBkMode((HDC) pDC, TRANSPARENT);
707 else
708 SetBkMode((HDC) pDC, OPAQUE);
709
710 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
711 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
712
713 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
714
715 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
716 // has a zero usage count.
717 backgroundBrush->RealizeResource();
718 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
719 }
720
721 // ----------------------------------------------------------------------------
722 // wxCheckListBox support
723 // ----------------------------------------------------------------------------
724
725 #if wxUSE_OWNER_DRAWN
726
727 // drawing
728 // -------
729
730 // space beneath/above each row in pixels
731 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
732 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
733
734 // the height is the same for all items
735 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
736
737 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
738 // message is not yet sent when we get here!
739 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
740 {
741 // only owner-drawn control should receive this message
742 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
743
744 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
745
746 wxDC dc;
747 dc.SetHDC((WXHDC)CreateIC(wxT("DISPLAY"), NULL, NULL, 0));
748 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
749
750 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
751 pStruct->itemWidth = dc.GetCharWidth();
752
753 return TRUE;
754 }
755
756 // forward the message to the appropriate item
757 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
758 {
759 // only owner-drawn control should receive this message
760 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
761
762 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
763
764 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
765
766 wxCHECK( data && (data != LB_ERR), FALSE );
767
768 wxListBoxItem *pItem = (wxListBoxItem *)data;
769
770 wxDC dc;
771 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
772 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
773 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
774
775 return pItem->OnDrawItem(dc, rect,
776 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
777 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
778 }
779
780 #endif
781 // wxUSE_OWNER_DRAWN