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