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 license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/listbox.h"
27 #include "wx/settings.h"
34 #include "wx/window.h"
35 #include "wx/msw/private.h"
40 #if defined(GetWindowStyle)
45 #include "wx/dynarray.h"
49 #include "wx/ownerdrw.h"
53 #ifdef __GNUWIN32_OLD__
54 #include "wx/msw/gnuwin32/extra.h"
59 #ifndef ListBox_SetItemData
60 #define ListBox_SetItemData(hwndCtl, index, data) \
61 ((int)(DWORD)SendMessage((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data)))
63 #ifndef ListBox_GetHorizontalExtent
64 #define ListBox_GetHorizontalExtent(hwndCtl) \
65 ((int)(DWORD)SendMessage((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L))
67 #ifndef ListBox_GetSelCount
68 #define ListBox_GetSelCount(hwndCtl) \
69 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELCOUNT, 0L, 0L))
71 #ifndef ListBox_GetSelItems
72 #define ListBox_GetSelItems(hwndCtl, cItems, lpItems) \
73 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems)))
75 #ifndef ListBox_GetTextLen
76 #define ListBox_GetTextLen(hwndCtl, index) \
77 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L))
79 #ifndef ListBox_GetText
80 #define ListBox_GetText(hwndCtl, index, lpszBuffer) \
81 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer)))
85 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
87 // ============================================================================
88 // list box item declaration and implementation
89 // ============================================================================
93 class wxListBoxItem
: public wxOwnerDrawn
96 wxListBoxItem(const wxString
& str
= "");
99 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
101 // no bitmaps/checkmarks
105 wxOwnerDrawn
*wxListBox::CreateItem(size_t WXUNUSED(n
))
107 return new wxListBoxItem();
110 #endif //USE_OWNER_DRAWN
112 // ============================================================================
113 // list box control implementation
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
121 wxListBox::wxListBox()
127 bool wxListBox::Create(wxWindow
*parent
,
131 int n
, const wxString choices
[],
133 const wxValidator
& validator
,
134 const wxString
& name
)
142 SetValidator(validator
);
143 #endif // wxUSE_VALIDATORS
146 parent
->AddChild(this);
148 wxSystemSettings settings
;
149 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
150 SetForegroundColour(parent
->GetForegroundColour());
152 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
158 m_windowStyle
= style
;
160 DWORD wstyle
= WS_VISIBLE
| WS_VSCROLL
| WS_TABSTOP
|
161 LBS_NOTIFY
| LBS_HASSTRINGS
/* | WS_CLIPSIBLINGS */;
163 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
164 _T("only one of listbox selection modes can be specified") );
165 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
166 wstyle
|= WS_CLIPSIBLINGS
;
168 if (m_windowStyle
& wxLB_MULTIPLE
)
169 wstyle
|= LBS_MULTIPLESEL
;
170 else if (m_windowStyle
& wxLB_EXTENDED
)
171 wstyle
|= LBS_EXTENDEDSEL
;
173 if (m_windowStyle
& wxLB_ALWAYS_SB
)
174 wstyle
|= LBS_DISABLENOSCROLL
;
175 if (m_windowStyle
& wxLB_HSCROLL
)
176 wstyle
|= WS_HSCROLL
;
177 if (m_windowStyle
& wxLB_SORT
)
180 #if wxUSE_OWNER_DRAWN
181 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
182 // we don't support LBS_OWNERDRAWVARIABLE yet
183 wstyle
|= LBS_OWNERDRAWFIXED
;
187 // Without this style, you get unexpected heights, so e.g. constraint layout
188 // doesn't work properly
189 wstyle
|= LBS_NOINTEGRALHEIGHT
;
192 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
194 // Even with extended styles, need to combine with WS_BORDER for them to
196 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
201 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("LISTBOX"), NULL
,
204 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
205 wxGetInstance(), NULL
);
207 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create listbox") );
212 Ctl3dSubclassCtl(GetHwnd());
217 // Subclass again to catch messages
221 for (ui
= 0; ui
< (size_t)n
; ui
++) {
225 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
226 SendMessage(GetHwnd(), LB_SETCURSEL
, 0, 0);
228 SetFont(parent
->GetFont());
230 SetSize(x
, y
, width
, height
);
235 wxListBox::~wxListBox()
240 void wxListBox::SetupColours()
242 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
243 SetForegroundColour(GetParent()->GetForegroundColour());
246 // ----------------------------------------------------------------------------
247 // implementation of wxListBoxBase methods
248 // ----------------------------------------------------------------------------
250 void wxListBox::DoSetFirstItem(int N
)
252 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
253 wxT("invalid index in wxListBox::SetFirstItem") );
255 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
258 void wxListBox::Delete(int N
)
260 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
261 wxT("invalid index in wxListBox::Delete") );
263 // for owner drawn objects, the data is used for storing wxOwnerDrawn
264 // pointers and we shouldn't touch it
265 #if !wxUSE_OWNER_DRAWN
266 if ( !(m_windowStyle
& wxLB_OWNERDRAW
) )
267 #endif // !wxUSE_OWNER_DRAWN
268 if ( HasClientObjectData() )
270 delete GetClientObject(N
);
273 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
276 SetHorizontalExtent("");
279 int wxListBox::DoAppend(const wxString
& item
)
281 int index
= ListBox_AddString(GetHwnd(), item
);
284 #if wxUSE_OWNER_DRAWN
285 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
286 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
287 pNewItem
->SetName(item
);
288 m_aItems
.Add(pNewItem
);
289 ListBox_SetItemData(GetHwnd(), index
, pNewItem
);
290 pNewItem
->SetFont(GetFont());
294 SetHorizontalExtent(item
);
299 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
301 // avoid flicker - but don't need to do this for a hidden listbox
302 bool hideAndShow
= IsShown();
305 ShowWindow(GetHwnd(), SW_HIDE
);
308 ListBox_ResetContent(GetHwnd());
310 m_noItems
= choices
.GetCount();
312 for (i
= 0; i
< m_noItems
; i
++)
314 ListBox_AddString(GetHwnd(), choices
[i
]);
317 #if wxUSE_OWNER_DRAWN
318 if ( m_windowStyle
& wxLB_OWNERDRAW
)
320 wxASSERT_MSG(clientData
[i
] == NULL
,
321 wxT("Can't use client data with owner-drawn listboxes"));
323 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
324 #else // !wxUSE_OWNER_DRAWN
325 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
326 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
330 #if wxUSE_OWNER_DRAWN
331 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
332 // first delete old items
333 size_t ui
= m_aItems
.Count();
334 while ( ui
-- != 0 ) {
339 // then create new ones
340 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
341 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
342 pNewItem
->SetName(choices
[ui
]);
343 m_aItems
.Add(pNewItem
);
344 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
347 #endif // wxUSE_OWNER_DRAWN
349 SetHorizontalExtent();
353 // show the listbox back if we hid it
354 ShowWindow(GetHwnd(), SW_SHOW
);
358 int wxListBox::FindString(const wxString
& s
) const
360 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
367 void wxListBox::Clear()
371 ListBox_ResetContent(GetHwnd());
374 SetHorizontalExtent();
377 void wxListBox::Free()
379 #if wxUSE_OWNER_DRAWN
380 if ( m_windowStyle
& wxLB_OWNERDRAW
)
382 size_t uiCount
= m_aItems
.Count();
383 while ( uiCount
-- != 0 ) {
384 delete m_aItems
[uiCount
];
390 #endif // wxUSE_OWNER_DRAWN
391 if ( HasClientObjectData() )
393 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
395 delete GetClientObject(n
);
400 void wxListBox::SetSelection(int N
, bool select
)
402 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
403 wxT("invalid index in wxListBox::SetSelection") );
405 if ( HasMultipleSelection() )
407 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
411 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
415 bool wxListBox::IsSelected(int N
) const
417 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
418 wxT("invalid index in wxListBox::Selected") );
420 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
423 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
425 return (wxClientData
*)DoGetItemClientData(n
);
428 void *wxListBox::DoGetItemClientData(int n
) const
430 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
431 wxT("invalid index in wxListBox::GetClientData") );
433 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
436 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
438 DoSetItemClientData(n
, clientData
);
441 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
443 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
444 wxT("invalid index in wxListBox::SetClientData") );
446 #if wxUSE_OWNER_DRAWN
447 if ( m_windowStyle
& wxLB_OWNERDRAW
)
449 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
450 // in OnMeasure/OnDraw.
451 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
453 #endif // wxUSE_OWNER_DRAWN
455 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
456 wxLogDebug(wxT("LB_SETITEMDATA failed"));
459 // Return number of selections and an array of selected integers
460 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
464 if ( HasMultipleSelection() )
466 int no_sel
= ListBox_GetSelCount(GetHwnd());
468 int *selections
= new int[no_sel
];
469 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
471 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
473 aSelections
.Alloc(no_sel
);
474 for ( int n
= 0; n
< no_sel
; n
++ )
475 aSelections
.Add(selections
[n
]);
477 delete [] selections
;
482 else // single-selection listbox
484 if (ListBox_GetCurSel(GetHwnd()) > -1)
485 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
487 return aSelections
.Count();
491 // Get single selection, for single choice list items
492 int wxListBox::GetSelection() const
494 wxCHECK_MSG( !HasMultipleSelection(),
496 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
498 return ListBox_GetCurSel(GetHwnd());
501 // Find string for position
502 wxString
wxListBox::GetString(int N
) const
504 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
505 wxT("invalid index in wxListBox::GetClientData") );
507 int len
= ListBox_GetTextLen(GetHwnd(), N
);
509 // +1 for terminating NUL
511 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
512 result
.UngetWriteBuf();
518 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
520 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
521 wxT("invalid index in wxListBox::InsertItems") );
523 int nItems
= items
.GetCount();
524 for ( int i
= 0; i
< nItems
; i
++ )
525 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
528 SetHorizontalExtent();
531 void wxListBox::SetString(int N
, const wxString
& s
)
533 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
534 wxT("invalid index in wxListBox::SetString") );
536 // remember the state of the item
537 bool wasSelected
= IsSelected(N
);
539 void *oldData
= NULL
;
540 wxClientData
*oldObjData
= NULL
;
541 if ( m_clientDataItemsType
== wxClientData_Void
)
542 oldData
= GetClientData(N
);
543 else if ( m_clientDataItemsType
== wxClientData_Object
)
544 oldObjData
= GetClientObject(N
);
546 // delete and recreate it
547 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
550 if ( N
== m_noItems
- 1 )
553 ListBox_InsertString(GetHwnd(), newN
, s
);
555 // restore the client data
557 SetClientData(N
, oldData
);
558 else if ( oldObjData
)
559 SetClientObject(N
, oldObjData
);
561 // we may have lost the selection
565 #if wxUSE_OWNER_DRAWN
566 if ( m_windowStyle
& wxLB_OWNERDRAW
)
567 // update item's text
568 m_aItems
[N
]->SetName(s
);
569 #endif //USE_OWNER_DRAWN
572 int wxListBox::GetCount() const
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 // Windows-specific code to set the horizontal extent of the listbox, if
582 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
583 // Otherwise, all strings are used.
584 void wxListBox::SetHorizontalExtent(const wxString
& s
)
586 // Only necessary if we want a horizontal scrollbar
587 if (!(m_windowStyle
& wxHSCROLL
))
589 TEXTMETRIC lpTextMetric
;
593 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
594 HDC dc
= GetWindowDC(GetHwnd());
596 if (GetFont().Ok() && GetFont().GetResourceHandle())
597 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
599 GetTextMetrics(dc
, &lpTextMetric
);
601 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
602 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
605 ::SelectObject(dc
, oldFont
);
607 ReleaseDC(GetHwnd(), dc
);
608 if (extentX
> existingExtent
)
609 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
613 int largestExtent
= 0;
614 HDC dc
= GetWindowDC(GetHwnd());
616 if (GetFont().Ok() && GetFont().GetResourceHandle())
617 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
619 GetTextMetrics(dc
, &lpTextMetric
);
621 for (i
= 0; i
< m_noItems
; i
++)
623 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
626 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
627 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
628 if (extentX
> largestExtent
)
629 largestExtent
= extentX
;
632 ::SelectObject(dc
, oldFont
);
634 ReleaseDC(GetHwnd(), dc
);
635 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
639 wxSize
wxListBox::DoGetBestSize() const
641 // find the widest string
644 for ( int i
= 0; i
< m_noItems
; i
++ )
646 wxString
str(GetString(i
));
647 GetTextExtent(str
, &wLine
, NULL
);
648 if ( wLine
> wListbox
)
652 // give it some reasonable default value if there are no strings in the
657 // the listbox should be slightly larger than the widest string
659 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
663 // don't make the listbox too tall (limit height to 10 items) but don't
664 // make it too small neither
665 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*
666 wxMin(wxMax(m_noItems
, 3), 10);
668 return wxSize(wListbox
, hListbox
);
671 // ----------------------------------------------------------------------------
673 // ----------------------------------------------------------------------------
675 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
678 if ( param
== LBN_SELCHANGE
)
680 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
682 else if ( param
== LBN_DBLCLK
)
684 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
688 // some event we're not interested in
692 wxCommandEvent
event(evtType
, m_windowId
);
693 event
.SetEventObject( this );
695 wxArrayInt aSelections
;
696 int n
, count
= GetSelections(aSelections
);
700 if ( HasClientObjectData() )
701 event
.SetClientObject( GetClientObject(n
) );
702 else if ( HasClientUntypedData() )
703 event
.SetClientData( GetClientData(n
) );
704 event
.SetString( GetString(n
) );
711 event
.m_commandInt
= n
;
713 return GetEventHandler()->ProcessEvent(event
);
716 // ----------------------------------------------------------------------------
717 // wxCheckListBox support
718 // ----------------------------------------------------------------------------
720 #if wxUSE_OWNER_DRAWN
725 // space beneath/above each row in pixels
726 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
727 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
729 // the height is the same for all items
730 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
732 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
733 // message is not yet sent when we get here!
734 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
736 // only owner-drawn control should receive this message
737 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
739 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
741 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
744 dc
.SetHDC((WXHDC
)hdc
);
745 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
747 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
748 pStruct
->itemWidth
= dc
.GetCharWidth();
757 // forward the message to the appropriate item
758 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
760 // only owner-drawn control should receive this message
761 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
763 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
764 UINT itemID
= pStruct
->itemID
;
766 // the item may be -1 for an empty listbox
767 if ( itemID
== (UINT
)-1 )
770 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
772 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
774 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
776 wxDCTemp
dc((WXHDC
)pStruct
->hDC
);
777 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
778 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
780 return pItem
->OnDrawItem(dc
, rect
,
781 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
782 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);
785 #endif // wxUSE_OWNER_DRAWN
787 #endif // wxUSE_LISTBOX