1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listbox.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (owner drawn stuff)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/listbox.h"
24 #include "wx/dynarray.h"
25 #include "wx/settings.h"
31 #include "wx/window.h"
34 #include "wx/msw/private.h"
35 #include "wx/msw/dc.h"
40 #include "wx/ownerdrw.h"
43 #if wxUSE_EXTENDED_RTTI
44 WX_DEFINE_FLAGS( wxListBoxStyle
)
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
)
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
)
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
)
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
)
83 wxEND_FLAGS( wxListBoxStyle
)
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox
, wxControlWithItems
,"wx/listbox.h")
87 wxBEGIN_PROPERTIES_TABLE(wxListBox
)
88 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_LISTBOX_SELECTED
, wxCommandEvent
)
89 wxEVENT_PROPERTY( DoubleClick
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, wxCommandEvent
)
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()
97 wxBEGIN_HANDLERS_TABLE(wxListBox
)
98 wxEND_HANDLERS_TABLE()
100 wxCONSTRUCTOR_4( wxListBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
102 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
112 // ============================================================================
113 // list box item declaration and implementation
114 // ============================================================================
116 #if wxUSE_OWNER_DRAWN
118 class wxListBoxItem
: public wxOwnerDrawn
121 wxListBoxItem(const wxString
& str
= wxEmptyString
);
124 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, false)
126 // no bitmaps/checkmarks
130 wxOwnerDrawn
*wxListBox::CreateLboxItem(size_t WXUNUSED(n
))
132 return new wxListBoxItem();
135 #endif //USE_OWNER_DRAWN
137 // ============================================================================
138 // list box control implementation
139 // ============================================================================
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
146 wxListBox::wxListBox()
149 m_updateHorizontalExtent
= false;
152 bool wxListBox::Create(wxWindow
*parent
,
156 int n
, const wxString choices
[],
158 const wxValidator
& validator
,
159 const wxString
& name
)
162 m_updateHorizontalExtent
= false;
164 // initialize base class fields
165 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
168 // create the native control
169 if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString
, pos
, size
) )
171 // control creation failed
175 // initialize the contents
176 for ( int i
= 0; i
< n
; i
++ )
181 // now we can compute our best size correctly, so do it again
182 InvalidateBestSize();
183 SetInitialSize(size
);
188 bool wxListBox::Create(wxWindow
*parent
,
192 const wxArrayString
& choices
,
194 const wxValidator
& validator
,
195 const wxString
& name
)
197 wxCArrayString
chs(choices
);
198 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
199 style
, validator
, name
);
202 wxListBox::~wxListBox()
207 WXDWORD
wxListBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
209 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
211 // we always want to get the notifications
212 msStyle
|= LBS_NOTIFY
;
214 // without this style, you get unexpected heights, so e.g. constraint
215 // layout doesn't work properly
216 msStyle
|= LBS_NOINTEGRALHEIGHT
;
218 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
219 wxT("only one of listbox selection modes can be specified") );
221 if ( style
& wxLB_MULTIPLE
)
222 msStyle
|= LBS_MULTIPLESEL
;
223 else if ( style
& wxLB_EXTENDED
)
224 msStyle
|= LBS_EXTENDEDSEL
;
226 wxASSERT_MSG( !(style
& wxLB_ALWAYS_SB
) || !(style
& wxLB_NO_SB
),
227 wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) );
229 if ( !(style
& wxLB_NO_SB
) )
231 msStyle
|= WS_VSCROLL
;
232 if ( style
& wxLB_ALWAYS_SB
)
233 msStyle
|= LBS_DISABLENOSCROLL
;
236 if ( m_windowStyle
& wxLB_HSCROLL
)
237 msStyle
|= WS_HSCROLL
;
238 if ( m_windowStyle
& wxLB_SORT
)
241 #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
242 if ( m_windowStyle
& wxLB_OWNERDRAW
)
244 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
245 // the strings in the listbox for simplicity even though we could have
246 // avoided it in this case
247 msStyle
|= LBS_OWNERDRAWFIXED
| LBS_HASSTRINGS
;
249 #endif // wxUSE_OWNER_DRAWN
254 void wxListBox::OnInternalIdle()
256 wxWindow::OnInternalIdle();
258 if (m_updateHorizontalExtent
)
260 SetHorizontalExtent(wxEmptyString
);
261 m_updateHorizontalExtent
= false;
265 // ----------------------------------------------------------------------------
266 // implementation of wxListBoxBase methods
267 // ----------------------------------------------------------------------------
269 void wxListBox::DoSetFirstItem(int N
)
271 wxCHECK_RET( IsValid(N
),
272 wxT("invalid index in wxListBox::SetFirstItem") );
274 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
277 void wxListBox::DoDeleteOneItem(unsigned int n
)
279 wxCHECK_RET( IsValid(n
),
280 wxT("invalid index in wxListBox::Delete") );
282 SendMessage(GetHwnd(), LB_DELETESTRING
, n
, 0);
285 // SetHorizontalExtent(wxEmptyString); can be slow
286 m_updateHorizontalExtent
= true;
288 UpdateOldSelections();
291 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
293 // back to base class search for not native search type
295 return wxItemContainerImmutable::FindString( s
, bCase
);
297 int pos
= ListBox_FindStringExact(GetHwnd(), -1, s
.wx_str());
304 void wxListBox::DoClear()
308 ListBox_ResetContent(GetHwnd());
311 m_updateHorizontalExtent
= true;
313 UpdateOldSelections();
316 void wxListBox::Free()
318 #if wxUSE_OWNER_DRAWN
319 if ( m_windowStyle
& wxLB_OWNERDRAW
)
321 WX_CLEAR_ARRAY(m_aItems
);
323 #endif // wxUSE_OWNER_DRAWN
326 void wxListBox::DoSetSelection(int N
, bool select
)
328 wxCHECK_RET( N
== wxNOT_FOUND
|| IsValid(N
),
329 wxT("invalid index in wxListBox::SetSelection") );
331 if ( HasMultipleSelection() )
333 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
337 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
340 UpdateOldSelections();
343 bool wxListBox::IsSelected(int N
) const
345 wxCHECK_MSG( IsValid(N
), false,
346 wxT("invalid index in wxListBox::Selected") );
348 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? false : true;
351 void *wxListBox::DoGetItemClientData(unsigned int n
) const
353 wxCHECK_MSG( IsValid(n
), NULL
,
354 wxT("invalid index in wxListBox::GetClientData") );
356 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
359 void wxListBox::DoSetItemClientData(unsigned int n
, void *clientData
)
361 wxCHECK_RET( IsValid(n
),
362 wxT("invalid index in wxListBox::SetClientData") );
364 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
366 wxLogDebug(wxT("LB_SETITEMDATA failed"));
370 // Return number of selections and an array of selected integers
371 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
375 if ( HasMultipleSelection() )
377 int countSel
= ListBox_GetSelCount(GetHwnd());
378 if ( countSel
== LB_ERR
)
380 wxLogDebug(wxT("ListBox_GetSelCount failed"));
382 else if ( countSel
!= 0 )
384 int *selections
= new int[countSel
];
386 if ( ListBox_GetSelItems(GetHwnd(),
387 countSel
, selections
) == LB_ERR
)
389 wxLogDebug(wxT("ListBox_GetSelItems failed"));
394 aSelections
.Alloc(countSel
);
395 for ( int n
= 0; n
< countSel
; n
++ )
396 aSelections
.Add(selections
[n
]);
399 delete [] selections
;
404 else // single-selection listbox
406 if (ListBox_GetCurSel(GetHwnd()) > -1)
407 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
409 return aSelections
.Count();
413 // Get single selection, for single choice list items
414 int wxListBox::GetSelection() const
416 wxCHECK_MSG( !HasMultipleSelection(),
418 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
420 return ListBox_GetCurSel(GetHwnd());
423 // Find string for position
424 wxString
wxListBox::GetString(unsigned int n
) const
426 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
427 wxT("invalid index in wxListBox::GetString") );
429 int len
= ListBox_GetTextLen(GetHwnd(), n
);
431 // +1 for terminating NUL
433 ListBox_GetText(GetHwnd(), n
, (wxChar
*)wxStringBuffer(result
, len
+ 1));
438 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
441 wxClientDataType type
)
443 MSWAllocStorage(items
, LB_INITSTORAGE
);
445 const bool append
= pos
== GetCount();
447 // we must use CB_ADDSTRING when appending as only it works correctly for
448 // the sorted controls
449 const unsigned msg
= append
? LB_ADDSTRING
: LB_INSERTSTRING
;
456 const unsigned int numItems
= items
.GetCount();
457 for ( unsigned int i
= 0; i
< numItems
; i
++ )
459 n
= MSWInsertOrAppendItem(pos
, items
[i
], msg
);
460 if ( n
== wxNOT_FOUND
)
468 #if wxUSE_OWNER_DRAWN
469 if ( HasFlag(wxLB_OWNERDRAW
) )
471 wxOwnerDrawn
*pNewItem
= CreateLboxItem(n
);
472 pNewItem
->SetName(items
[i
]);
473 pNewItem
->SetFont(GetFont());
474 m_aItems
.Insert(pNewItem
, n
);
476 #endif // wxUSE_OWNER_DRAWN
477 AssignNewItemClientData(n
, clientData
, i
, type
);
480 m_updateHorizontalExtent
= true;
482 UpdateOldSelections();
487 int wxListBox::DoListHitTest(const wxPoint
& point
) const
489 LRESULT lRes
= ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT
,
490 0, MAKELPARAM(point
.x
, point
.y
));
492 // non zero high-order word means that this item is outside of the client
493 // area, IOW the point is outside of the listbox
494 return HIWORD(lRes
) ? wxNOT_FOUND
: LOWORD(lRes
);
497 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
499 wxCHECK_RET( IsValid(n
),
500 wxT("invalid index in wxListBox::SetString") );
502 // remember the state of the item
503 bool wasSelected
= IsSelected(n
);
505 void *oldData
= NULL
;
506 wxClientData
*oldObjData
= NULL
;
507 if ( HasClientUntypedData() )
508 oldData
= GetClientData(n
);
509 else if ( HasClientObjectData() )
510 oldObjData
= GetClientObject(n
);
512 // delete and recreate it
513 SendMessage(GetHwnd(), LB_DELETESTRING
, n
, 0);
516 if ( n
== (m_noItems
- 1) )
519 ListBox_InsertString(GetHwnd(), newN
, s
.wx_str());
521 // restore the client data
523 SetClientData(n
, oldData
);
524 else if ( oldObjData
)
525 SetClientObject(n
, oldObjData
);
527 #if wxUSE_OWNER_DRAWN
528 if ( m_windowStyle
& wxLB_OWNERDRAW
)
530 // update item's text
531 m_aItems
[n
]->SetName(s
);
533 #endif //USE_OWNER_DRAWN
535 // we may have lost the selection
539 m_updateHorizontalExtent
= true;
542 unsigned int wxListBox::GetCount() const
547 // ----------------------------------------------------------------------------
548 // size-related stuff
549 // ----------------------------------------------------------------------------
551 void wxListBox::SetHorizontalExtent(const wxString
& s
)
553 // in any case, our best size could have changed
554 InvalidateBestSize();
556 // the rest is only necessary if we want a horizontal scrollbar
557 if ( !HasFlag(wxHSCROLL
) )
561 WindowHDC
dc(GetHwnd());
562 SelectInHDC
selFont(dc
, GetHfontOf(GetFont()));
564 TEXTMETRIC lpTextMetric
;
565 ::GetTextMetrics(dc
, &lpTextMetric
);
567 int largestExtent
= 0;
572 // set extent to the max length of all strings
573 for ( unsigned int i
= 0; i
< m_noItems
; i
++ )
575 const wxString str
= GetString(i
);
576 ::GetTextExtentPoint32(dc
, str
.c_str(), str
.length(), &extentXY
);
578 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
579 if ( extentX
> largestExtent
)
580 largestExtent
= extentX
;
583 else // just increase the extent to the length of this string
585 int existingExtent
= (int)SendMessage(GetHwnd(),
586 LB_GETHORIZONTALEXTENT
, 0, 0L);
588 ::GetTextExtentPoint32(dc
, s
.c_str(), s
.length(), &extentXY
);
590 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
591 if ( extentX
> existingExtent
)
592 largestExtent
= extentX
;
596 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
597 //else: it shouldn't change
600 wxSize
wxListBox::DoGetBestClientSize() const
602 // find the widest string
605 for (unsigned int i
= 0; i
< m_noItems
; i
++)
607 wxString
str(GetString(i
));
608 GetTextExtent(str
, &wLine
, NULL
);
609 if ( wLine
> wListbox
)
613 // give it some reasonable default value if there are no strings in the
618 // the listbox should be slightly larger than the widest string
619 wListbox
+= 3*GetCharWidth();
621 // add room for the scrollbar
622 wListbox
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
624 // don't make the listbox too tall (limit height to 10 items) but don't
625 // make it too small neither
626 int hListbox
= SendMessage(GetHwnd(), LB_GETITEMHEIGHT
, 0, 0)*
627 wxMin(wxMax(m_noItems
, 3), 10);
629 return wxSize(wListbox
, hListbox
);
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
638 if ((param
== LBN_SELCHANGE
) && HasMultipleSelection())
646 if ( param
== LBN_SELCHANGE
)
648 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
649 n
= SendMessage(GetHwnd(), LB_GETCARETINDEX
, 0, 0);
651 // NB: conveniently enough, LB_ERR is the same as wxNOT_FOUND
653 else if ( param
== LBN_DBLCLK
)
655 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
656 n
= HitTest(ScreenToClient(wxGetMousePosition()));
660 // some event we're not interested in
664 // retrieve the affected item
665 if ( n
== wxNOT_FOUND
)
668 wxCommandEvent
event(evtType
, m_windowId
);
669 event
.SetEventObject(this);
671 if ( HasClientObjectData() )
672 event
.SetClientObject( GetClientObject(n
) );
673 else if ( HasClientUntypedData() )
674 event
.SetClientData( GetClientData(n
) );
676 event
.SetString(GetString(n
));
679 return HandleWindowEvent(event
);
682 // ----------------------------------------------------------------------------
683 // wxCheckListBox support
684 // ----------------------------------------------------------------------------
686 #if wxUSE_OWNER_DRAWN
691 // space beneath/above each row in pixels
692 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
693 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
695 // the height is the same for all items
696 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
698 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
699 // message is not yet sent when we get here!
700 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
702 // only owner-drawn control should receive this message
703 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false );
705 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
708 HDC hdc
= GetDC(NULL
);
710 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
714 wxDCTemp
dc((WXHDC
)hdc
);
715 dc
.SetFont(GetFont());
717 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
718 pStruct
->itemWidth
= dc
.GetCharWidth();
722 ReleaseDC(NULL
, hdc
);
730 // forward the message to the appropriate item
731 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
733 // only owner-drawn control should receive this message
734 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false );
736 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
738 // the item may be -1 for an empty listbox
739 if ( pStruct
->itemID
== (UINT
)-1 )
742 wxListBoxItem
*pItem
= (wxListBoxItem
*)m_aItems
[pStruct
->itemID
];
744 wxDCTemp
dc((WXHDC
)pStruct
->hDC
);
746 return pItem
->OnDrawItem(dc
, wxRectFromRECT(pStruct
->rcItem
),
747 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
748 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);
751 #endif // wxUSE_OWNER_DRAWN
753 #endif // wxUSE_LISTBOX