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