No changes, just refactor wxListBox initialization in wxMSW.
[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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_LISTBOX
20
21 #include "wx/listbox.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #include "wx/settings.h"
26 #include "wx/brush.h"
27 #include "wx/font.h"
28 #include "wx/dc.h"
29 #include "wx/utils.h"
30 #include "wx/log.h"
31 #include "wx/window.h"
32 #endif
33
34 #include "wx/msw/private.h"
35 #include "wx/msw/dc.h"
36
37 #include <windowsx.h>
38
39 #if wxUSE_OWNER_DRAWN
40 #include "wx/ownerdrw.h"
41 #endif
42
43 #if wxUSE_EXTENDED_RTTI
44 WX_DEFINE_FLAGS( wxListBoxStyle )
45
46 wxBEGIN_FLAGS( wxListBoxStyle )
47 // new style border flags, we put them first to
48 // use them for streaming out
49 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
50 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
51 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
52 wxFLAGS_MEMBER(wxBORDER_RAISED)
53 wxFLAGS_MEMBER(wxBORDER_STATIC)
54 wxFLAGS_MEMBER(wxBORDER_NONE)
55
56 // old style border flags
57 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
58 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
59 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
60 wxFLAGS_MEMBER(wxRAISED_BORDER)
61 wxFLAGS_MEMBER(wxSTATIC_BORDER)
62 wxFLAGS_MEMBER(wxBORDER)
63
64 // standard window styles
65 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
66 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
67 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
68 wxFLAGS_MEMBER(wxWANTS_CHARS)
69 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
70 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
71 wxFLAGS_MEMBER(wxVSCROLL)
72 wxFLAGS_MEMBER(wxHSCROLL)
73
74 wxFLAGS_MEMBER(wxLB_SINGLE)
75 wxFLAGS_MEMBER(wxLB_MULTIPLE)
76 wxFLAGS_MEMBER(wxLB_EXTENDED)
77 wxFLAGS_MEMBER(wxLB_HSCROLL)
78 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
79 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
80 wxFLAGS_MEMBER(wxLB_NO_SB)
81 wxFLAGS_MEMBER(wxLB_SORT)
82
83 wxEND_FLAGS( wxListBoxStyle )
84
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems,"wx/listbox.h")
86
87 wxBEGIN_PROPERTIES_TABLE(wxListBox)
88 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
89 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
90
91 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
92 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
93 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
94 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
95 wxEND_PROPERTIES_TABLE()
96
97 wxBEGIN_HANDLERS_TABLE(wxListBox)
98 wxEND_HANDLERS_TABLE()
99
100 wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
101 #else
102 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
103 #endif
104
105 /*
106 TODO PROPERTIES
107 selection
108 content
109 item
110 */
111
112 // ============================================================================
113 // list box item declaration and implementation
114 // ============================================================================
115
116 #if wxUSE_OWNER_DRAWN
117
118 class wxListBoxItem : public wxOwnerDrawn
119 {
120 public:
121 wxListBoxItem(wxListBox *parent)
122 { m_parent = parent; }
123
124 wxListBox *GetParent() const
125 { return m_parent; }
126
127 int GetIndex() const
128 { return m_parent->GetItemIndex(const_cast<wxListBoxItem*>(this)); }
129
130 wxString GetName() const
131 { return m_parent->GetString(GetIndex()); }
132
133 private:
134 wxListBox *m_parent;
135 };
136
137 wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
138 {
139 return new wxListBoxItem(this);
140 }
141
142 #endif //USE_OWNER_DRAWN
143
144 // ============================================================================
145 // list box control implementation
146 // ============================================================================
147
148 // ----------------------------------------------------------------------------
149 // creation
150 // ----------------------------------------------------------------------------
151
152 void wxListBox::Init()
153 {
154 m_noItems = 0;
155 m_updateHorizontalExtent = false;
156 }
157
158 bool wxListBox::Create(wxWindow *parent,
159 wxWindowID id,
160 const wxPoint& pos,
161 const wxSize& size,
162 int n, const wxString choices[],
163 long style,
164 const wxValidator& validator,
165 const wxString& name)
166 {
167 // initialize base class fields
168 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
169 return false;
170
171 // create the native control
172 if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString, pos, size) )
173 {
174 // control creation failed
175 return false;
176 }
177
178 // initialize the contents
179 for ( int i = 0; i < n; i++ )
180 {
181 Append(choices[i]);
182 }
183
184 // now we can compute our best size correctly, so do it again
185 SetInitialSize(size);
186
187 return true;
188 }
189
190 bool wxListBox::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint& pos,
193 const wxSize& size,
194 const wxArrayString& choices,
195 long style,
196 const wxValidator& validator,
197 const wxString& name)
198 {
199 wxCArrayString chs(choices);
200 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
201 style, validator, name);
202 }
203
204 wxListBox::~wxListBox()
205 {
206 Clear();
207 }
208
209 WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
210 {
211 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
212
213 // we always want to get the notifications
214 msStyle |= LBS_NOTIFY;
215
216 // without this style, you get unexpected heights, so e.g. constraint
217 // layout doesn't work properly
218 msStyle |= LBS_NOINTEGRALHEIGHT;
219
220 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
221 wxT("only one of listbox selection modes can be specified") );
222
223 if ( style & wxLB_MULTIPLE )
224 msStyle |= LBS_MULTIPLESEL;
225 else if ( style & wxLB_EXTENDED )
226 msStyle |= LBS_EXTENDEDSEL;
227
228 wxASSERT_MSG( !(style & wxLB_ALWAYS_SB) || !(style & wxLB_NO_SB),
229 wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) );
230
231 if ( !(style & wxLB_NO_SB) )
232 {
233 msStyle |= WS_VSCROLL;
234 if ( style & wxLB_ALWAYS_SB )
235 msStyle |= LBS_DISABLENOSCROLL;
236 }
237
238 if ( m_windowStyle & wxLB_HSCROLL )
239 msStyle |= WS_HSCROLL;
240 if ( m_windowStyle & wxLB_SORT )
241 msStyle |= LBS_SORT;
242
243 #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
244 if ( m_windowStyle & wxLB_OWNERDRAW )
245 {
246 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
247 // the strings in the listbox for simplicity even though we could have
248 // avoided it in this case
249 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
250 }
251 #endif // wxUSE_OWNER_DRAWN
252
253 return msStyle;
254 }
255
256 void wxListBox::OnInternalIdle()
257 {
258 wxWindow::OnInternalIdle();
259
260 if (m_updateHorizontalExtent)
261 {
262 SetHorizontalExtent(wxEmptyString);
263 m_updateHorizontalExtent = false;
264 }
265 }
266
267 void wxListBox::MSWOnItemsChanged()
268 {
269 // we need to do two things when items change: update their max horizontal
270 // extent so that horizontal scrollbar could be shown or hidden as
271 // appropriate and also invlaidate the best size
272 //
273 // updating the max extent is slow (it's an O(N) operation) and so we defer
274 // it until the idle time but the best size should be invalidated
275 // immediately doing it in idle time is too late -- layout using incorrect
276 // old best size will have been already done by then
277
278 m_updateHorizontalExtent = true;
279
280 InvalidateBestSize();
281 }
282
283 // ----------------------------------------------------------------------------
284 // implementation of wxListBoxBase methods
285 // ----------------------------------------------------------------------------
286
287 void wxListBox::DoSetFirstItem(int N)
288 {
289 wxCHECK_RET( IsValid(N),
290 wxT("invalid index in wxListBox::SetFirstItem") );
291
292 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
293 }
294
295 void wxListBox::DoDeleteOneItem(unsigned int n)
296 {
297 wxCHECK_RET( IsValid(n),
298 wxT("invalid index in wxListBox::Delete") );
299
300 #if wxUSE_OWNER_DRAWN
301 if ( HasFlag(wxLB_OWNERDRAW) )
302 {
303 delete m_aItems[n];
304 m_aItems.RemoveAt(n);
305 }
306 #endif // wxUSE_OWNER_DRAWN
307
308 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
309 m_noItems--;
310
311 MSWOnItemsChanged();
312
313 UpdateOldSelections();
314 }
315
316 int wxListBox::FindString(const wxString& s, bool bCase) const
317 {
318 // back to base class search for not native search type
319 if (bCase)
320 return wxItemContainerImmutable::FindString( s, bCase );
321
322 int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str());
323 if (pos == LB_ERR)
324 return wxNOT_FOUND;
325 else
326 return pos;
327 }
328
329 void wxListBox::DoClear()
330 {
331 #if wxUSE_OWNER_DRAWN
332 if ( HasFlag(wxLB_OWNERDRAW) )
333 {
334 WX_CLEAR_ARRAY(m_aItems);
335 }
336 #endif // wxUSE_OWNER_DRAWN
337
338 ListBox_ResetContent(GetHwnd());
339
340 m_noItems = 0;
341 MSWOnItemsChanged();
342
343 UpdateOldSelections();
344 }
345
346 void wxListBox::DoSetSelection(int N, bool select)
347 {
348 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
349 wxT("invalid index in wxListBox::SetSelection") );
350
351 if ( HasMultipleSelection() )
352 {
353 SendMessage(GetHwnd(), LB_SETSEL, select, N);
354 }
355 else
356 {
357 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
358 }
359
360 UpdateOldSelections();
361 }
362
363 bool wxListBox::IsSelected(int N) const
364 {
365 wxCHECK_MSG( IsValid(N), false,
366 wxT("invalid index in wxListBox::Selected") );
367
368 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
369 }
370
371 void *wxListBox::DoGetItemClientData(unsigned int n) const
372 {
373 wxCHECK_MSG( IsValid(n), NULL,
374 wxT("invalid index in wxListBox::GetClientData") );
375
376 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
377 }
378
379 void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
380 {
381 wxCHECK_RET( IsValid(n),
382 wxT("invalid index in wxListBox::SetClientData") );
383
384 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
385 {
386 wxLogDebug(wxT("LB_SETITEMDATA failed"));
387 }
388 }
389
390 // Return number of selections and an array of selected integers
391 int wxListBox::GetSelections(wxArrayInt& aSelections) const
392 {
393 aSelections.Empty();
394
395 if ( HasMultipleSelection() )
396 {
397 int countSel = ListBox_GetSelCount(GetHwnd());
398 if ( countSel == LB_ERR )
399 {
400 wxLogDebug(wxT("ListBox_GetSelCount failed"));
401 }
402 else if ( countSel != 0 )
403 {
404 int *selections = new int[countSel];
405
406 if ( ListBox_GetSelItems(GetHwnd(),
407 countSel, selections) == LB_ERR )
408 {
409 wxLogDebug(wxT("ListBox_GetSelItems failed"));
410 countSel = -1;
411 }
412 else
413 {
414 aSelections.Alloc(countSel);
415 for ( int n = 0; n < countSel; n++ )
416 aSelections.Add(selections[n]);
417 }
418
419 delete [] selections;
420 }
421
422 return countSel;
423 }
424 else // single-selection listbox
425 {
426 if (ListBox_GetCurSel(GetHwnd()) > -1)
427 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
428
429 return aSelections.Count();
430 }
431 }
432
433 // Get single selection, for single choice list items
434 int wxListBox::GetSelection() const
435 {
436 wxCHECK_MSG( !HasMultipleSelection(),
437 -1,
438 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
439
440 return ListBox_GetCurSel(GetHwnd());
441 }
442
443 // Find string for position
444 wxString wxListBox::GetString(unsigned int n) const
445 {
446 wxCHECK_MSG( IsValid(n), wxEmptyString,
447 wxT("invalid index in wxListBox::GetString") );
448
449 int len = ListBox_GetTextLen(GetHwnd(), n);
450
451 // +1 for terminating NUL
452 wxString result;
453 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
454
455 return result;
456 }
457
458 int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
459 unsigned int pos,
460 void **clientData,
461 wxClientDataType type)
462 {
463 MSWAllocStorage(items, LB_INITSTORAGE);
464
465 const bool append = pos == GetCount();
466
467 // we must use CB_ADDSTRING when appending as only it works correctly for
468 // the sorted controls
469 const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING;
470
471 if ( append )
472 pos = 0;
473
474 int n = wxNOT_FOUND;
475
476 const unsigned int numItems = items.GetCount();
477 for ( unsigned int i = 0; i < numItems; i++ )
478 {
479 n = MSWInsertOrAppendItem(pos, items[i], msg);
480 if ( n == wxNOT_FOUND )
481 return n;
482
483 if ( !append )
484 pos++;
485
486 ++m_noItems;
487
488 #if wxUSE_OWNER_DRAWN
489 if ( HasFlag(wxLB_OWNERDRAW) )
490 {
491 wxOwnerDrawn *pNewItem = CreateLboxItem(n);
492 pNewItem->SetFont(GetFont());
493 m_aItems.Insert(pNewItem, n);
494 }
495 #endif // wxUSE_OWNER_DRAWN
496 AssignNewItemClientData(n, clientData, i, type);
497 }
498
499 MSWOnItemsChanged();
500
501 UpdateOldSelections();
502
503 return n;
504 }
505
506 int wxListBox::DoHitTestList(const wxPoint& point) const
507 {
508 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
509 0, MAKELPARAM(point.x, point.y));
510
511 // non zero high-order word means that this item is outside of the client
512 // area, IOW the point is outside of the listbox
513 return HIWORD(lRes) ? wxNOT_FOUND : LOWORD(lRes);
514 }
515
516 void wxListBox::SetString(unsigned int n, const wxString& s)
517 {
518 wxCHECK_RET( IsValid(n),
519 wxT("invalid index in wxListBox::SetString") );
520
521 // remember the state of the item
522 bool wasSelected = IsSelected(n);
523
524 void *oldData = NULL;
525 wxClientData *oldObjData = NULL;
526 if ( HasClientUntypedData() )
527 oldData = GetClientData(n);
528 else if ( HasClientObjectData() )
529 oldObjData = GetClientObject(n);
530
531 // delete and recreate it
532 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
533
534 int newN = n;
535 if ( n == (m_noItems - 1) )
536 newN = -1;
537
538 ListBox_InsertString(GetHwnd(), newN, s.wx_str());
539
540 // restore the client data
541 if ( oldData )
542 SetClientData(n, oldData);
543 else if ( oldObjData )
544 SetClientObject(n, oldObjData);
545
546 // we may have lost the selection
547 if ( wasSelected )
548 Select(n);
549
550 MSWOnItemsChanged();
551 }
552
553 unsigned int wxListBox::GetCount() const
554 {
555 return m_noItems;
556 }
557
558 // ----------------------------------------------------------------------------
559 // size-related stuff
560 // ----------------------------------------------------------------------------
561
562 void wxListBox::SetHorizontalExtent(const wxString& s)
563 {
564 // the rest is only necessary if we want a horizontal scrollbar
565 if ( !HasFlag(wxHSCROLL) )
566 return;
567
568
569 WindowHDC dc(GetHwnd());
570 SelectInHDC selFont(dc, GetHfontOf(GetFont()));
571
572 TEXTMETRIC lpTextMetric;
573 ::GetTextMetrics(dc, &lpTextMetric);
574
575 int largestExtent = 0;
576 SIZE extentXY;
577
578 if ( s.empty() )
579 {
580 // set extent to the max length of all strings
581 for ( unsigned int i = 0; i < m_noItems; i++ )
582 {
583 const wxString str = GetString(i);
584 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
585
586 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
587 if ( extentX > largestExtent )
588 largestExtent = extentX;
589 }
590 }
591 else // just increase the extent to the length of this string
592 {
593 int existingExtent = (int)SendMessage(GetHwnd(),
594 LB_GETHORIZONTALEXTENT, 0, 0L);
595
596 ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY);
597
598 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
599 if ( extentX > existingExtent )
600 largestExtent = extentX;
601 }
602
603 if ( largestExtent )
604 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
605 //else: it shouldn't change
606 }
607
608 wxSize wxListBox::DoGetBestClientSize() const
609 {
610 // find the widest string
611 int wLine;
612 int wListbox = 0;
613 for (unsigned int i = 0; i < m_noItems; i++)
614 {
615 wxString str(GetString(i));
616 GetTextExtent(str, &wLine, NULL);
617 if ( wLine > wListbox )
618 wListbox = wLine;
619 }
620
621 // give it some reasonable default value if there are no strings in the
622 // list
623 if ( wListbox == 0 )
624 wListbox = 100;
625
626 // the listbox should be slightly larger than the widest string
627 wListbox += 3*GetCharWidth();
628
629 // add room for the scrollbar
630 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
631
632 // don't make the listbox too tall (limit height to 10 items) but don't
633 // make it too small neither
634 int hListbox = SendMessage(GetHwnd(), LB_GETITEMHEIGHT, 0, 0)*
635 wxMin(wxMax(m_noItems, 3), 10);
636
637 return wxSize(wListbox, hListbox);
638 }
639
640 // ----------------------------------------------------------------------------
641 // callbacks
642 // ----------------------------------------------------------------------------
643
644 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
645 {
646 wxEventType evtType;
647 int n;
648 if ( param == LBN_SELCHANGE )
649 {
650 if ( HasMultipleSelection() )
651 return CalcAndSendEvent();
652
653 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
654 n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
655
656 // NB: conveniently enough, LB_ERR is the same as wxNOT_FOUND
657 }
658 else if ( param == LBN_DBLCLK )
659 {
660 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
661 n = HitTest(ScreenToClient(wxGetMousePosition()));
662 }
663 else
664 {
665 // some event we're not interested in
666 return false;
667 }
668
669 // only send an event if we have a valid item
670 return n != wxNOT_FOUND && SendEvent(evtType, n, true /* selection */);
671 }
672
673 // ----------------------------------------------------------------------------
674 // owner-drawn list boxes support
675 // ----------------------------------------------------------------------------
676
677 #if wxUSE_OWNER_DRAWN
678
679 // misc overloaded methods
680 // -----------------------
681
682 bool wxListBox::SetFont(const wxFont &font)
683 {
684 if ( HasFlag(wxLB_OWNERDRAW) )
685 {
686 const unsigned count = m_aItems.GetCount();
687 for ( unsigned i = 0; i < count; i++ )
688 m_aItems[i]->SetFont(font);
689 }
690
691 wxListBoxBase::SetFont(font);
692
693 return true;
694 }
695
696 bool wxListBox::GetItemRect(size_t n, wxRect& rect) const
697 {
698 wxCHECK_MSG( IsValid(n), false,
699 wxT("invalid index in wxListBox::GetItemRect") );
700
701 RECT rc;
702
703 if ( ListBox_GetItemRect(GetHwnd(), n, &rc) != LB_ERR )
704 {
705 rect = wxRectFromRECT(rc);
706 return true;
707 }
708 else
709 {
710 // couldn't retrieve rect: for example, item isn't visible
711 return false;
712 }
713 }
714
715 bool wxListBox::RefreshItem(size_t n)
716 {
717 wxRect rect;
718 if ( !GetItemRect(n, rect) )
719 return false;
720
721 RECT rc;
722 wxCopyRectToRECT(rect, rc);
723
724 return ::InvalidateRect((HWND)GetHWND(), &rc, FALSE) == TRUE;
725 }
726
727
728 // drawing
729 // -------
730
731 namespace
732 {
733 // space beneath/above each row in pixels
734 static const int LISTBOX_EXTRA_SPACE = 1;
735
736 } // anonymous namespace
737
738 // the height is the same for all items
739 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
740
741 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
742 // message is not yet sent when we get here!
743 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
744 {
745 // only owner-drawn control should receive this message
746 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
747
748 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
749
750 #ifdef __WXWINCE__
751 HDC hdc = GetDC(NULL);
752 #else
753 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
754 #endif
755
756 {
757 wxDCTemp dc((WXHDC)hdc);
758 dc.SetFont(GetFont());
759
760 pStruct->itemHeight = dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE;
761 pStruct->itemWidth = dc.GetCharWidth();
762 }
763
764 #ifdef __WXWINCE__
765 ReleaseDC(NULL, hdc);
766 #else
767 DeleteDC(hdc);
768 #endif
769
770 return true;
771 }
772
773 // forward the message to the appropriate item
774 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
775 {
776 // only owner-drawn control should receive this message
777 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
778
779 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
780
781 // the item may be -1 for an empty listbox
782 if ( pStruct->itemID == (UINT)-1 )
783 return false;
784
785 wxListBoxItem *pItem = (wxListBoxItem *)m_aItems[pStruct->itemID];
786
787 wxDCTemp dc((WXHDC)pStruct->hDC);
788
789 return pItem->OnDrawItem(dc, wxRectFromRECT(pStruct->rcItem),
790 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
791 (wxOwnerDrawn::wxODStatus)(pStruct->itemState | wxOwnerDrawn::wxODHidePrefix));
792 }
793
794 #endif // wxUSE_OWNER_DRAWN
795
796 #endif // wxUSE_LISTBOX