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