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