1 ///////////////////////////////////////////////////////////////////////////////
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"
23 #include "wx/window.h"
24 #include "wx/msw/private.h"
27 #include "wx/listbox.h"
28 #include "wx/settings.h"
38 #if defined(GetWindowStyle)
43 #include "wx/dynarray.h"
47 #include "wx/ownerdrw.h"
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
57 #ifndef ListBox_SetItemData
58 #define ListBox_SetItemData(hwndCtl, index, data) \
59 ((int)(DWORD)SendMessage((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data)))
61 #ifndef ListBox_GetHorizontalExtent
62 #define ListBox_GetHorizontalExtent(hwndCtl) \
63 ((int)(DWORD)SendMessage((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L))
65 #ifndef ListBox_GetSelCount
66 #define ListBox_GetSelCount(hwndCtl) \
67 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELCOUNT, 0L, 0L))
69 #ifndef ListBox_GetSelItems
70 #define ListBox_GetSelItems(hwndCtl, cItems, lpItems) \
71 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems)))
73 #ifndef ListBox_GetTextLen
74 #define ListBox_GetTextLen(hwndCtl, index) \
75 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L))
77 #ifndef ListBox_GetText
78 #define ListBox_GetText(hwndCtl, index, lpszBuffer) \
79 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer)))
83 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
85 // ============================================================================
86 // list box item declaration and implementation
87 // ============================================================================
91 class wxListBoxItem
: public wxOwnerDrawn
94 wxListBoxItem(const wxString
& str
= "");
97 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
99 // no bitmaps/checkmarks
103 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
105 return new wxListBoxItem();
108 #endif //USE_OWNER_DRAWN
110 // ============================================================================
111 // list box control implementation
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
119 wxListBox::wxListBox()
125 bool wxListBox::Create(wxWindow
*parent
,
129 int n
, const wxString choices
[],
131 const wxValidator
& validator
,
132 const wxString
& name
)
140 SetValidator(validator
);
141 #endif // wxUSE_VALIDATORS
144 parent
->AddChild(this);
146 wxSystemSettings settings
;
147 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
148 SetForegroundColour(parent
->GetForegroundColour());
150 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
156 m_windowStyle
= style
;
158 DWORD wstyle
= WS_VISIBLE
| WS_VSCROLL
| WS_TABSTOP
|
159 LBS_NOTIFY
| LBS_HASSTRINGS
/* | WS_CLIPSIBLINGS */;
160 if (m_windowStyle
& wxLB_MULTIPLE
)
161 wstyle
|= LBS_MULTIPLESEL
;
162 else if (m_windowStyle
& wxLB_EXTENDED
)
163 wstyle
|= LBS_EXTENDEDSEL
;
165 if (m_windowStyle
& wxLB_ALWAYS_SB
)
166 wstyle
|= LBS_DISABLENOSCROLL
;
167 if (m_windowStyle
& wxLB_HSCROLL
)
168 wstyle
|= WS_HSCROLL
;
169 if (m_windowStyle
& wxLB_SORT
)
172 #if wxUSE_OWNER_DRAWN
173 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
174 // we don't support LBS_OWNERDRAWVARIABLE yet
175 wstyle
|= LBS_OWNERDRAWFIXED
;
179 // Without this style, you get unexpected heights, so e.g. constraint layout
180 // doesn't work properly
181 wstyle
|= LBS_NOINTEGRALHEIGHT
;
184 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
186 // Even with extended styles, need to combine with WS_BORDER for them to
188 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
193 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("LISTBOX"), NULL
,
196 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
197 wxGetInstance(), NULL
);
199 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create listbox") );
204 Ctl3dSubclassCtl(GetHwnd());
209 // Subclass again to catch messages
213 for (ui
= 0; ui
< (size_t)n
; ui
++) {
217 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
218 SendMessage(GetHwnd(), LB_SETCURSEL
, 0, 0);
220 SetFont(parent
->GetFont());
222 SetSize(x
, y
, width
, height
);
227 wxListBox::~wxListBox()
232 void wxListBox::SetupColours()
234 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
235 SetForegroundColour(GetParent()->GetForegroundColour());
238 // ----------------------------------------------------------------------------
239 // implementation of wxListBoxBase methods
240 // ----------------------------------------------------------------------------
242 void wxListBox::DoSetFirstItem(int N
)
244 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
245 wxT("invalid index in wxListBox::SetFirstItem") );
247 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
250 void wxListBox::Delete(int N
)
252 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
253 wxT("invalid index in wxListBox::Delete") );
255 // for owner drawn objects, the data is used for storing wxOwnerDrawn
256 // pointers and we shouldn't touch it
257 #if !wxUSE_OWNER_DRAWN
258 if ( !(m_windowStyle
& wxLB_OWNERDRAW
) )
259 #endif // !wxUSE_OWNER_DRAWN
260 if ( HasClientObjectData() )
262 delete GetClientObject(N
);
265 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
268 SetHorizontalExtent("");
271 int wxListBox::DoAppend(const wxString
& item
)
273 int index
= ListBox_AddString(GetHwnd(), item
);
276 #if wxUSE_OWNER_DRAWN
277 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
278 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
279 pNewItem
->SetName(item
);
280 m_aItems
.Add(pNewItem
);
281 ListBox_SetItemData(GetHwnd(), index
, pNewItem
);
282 pNewItem
->SetFont(GetFont());
286 SetHorizontalExtent(item
);
291 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
293 // avoid flicker - but don't need to do this for a hidden listbox
294 bool hideAndShow
= IsShown();
297 ShowWindow(GetHwnd(), SW_HIDE
);
300 ListBox_ResetContent(GetHwnd());
302 m_noItems
= choices
.GetCount();
304 for (i
= 0; i
< m_noItems
; i
++)
306 ListBox_AddString(GetHwnd(), choices
[i
]);
309 #if wxUSE_OWNER_DRAWN
310 if ( m_windowStyle
& wxLB_OWNERDRAW
)
312 wxASSERT_MSG(clientData
[i
] == NULL
,
313 wxT("Can't use client data with owner-drawn listboxes"));
315 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
316 #else // !wxUSE_OWNER_DRAWN
317 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
318 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
322 #if wxUSE_OWNER_DRAWN
323 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
324 // first delete old items
325 size_t ui
= m_aItems
.Count();
326 while ( ui
-- != 0 ) {
331 // then create new ones
332 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
333 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
334 pNewItem
->SetName(choices
[ui
]);
335 m_aItems
.Add(pNewItem
);
336 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
339 #endif // wxUSE_OWNER_DRAWN
341 SetHorizontalExtent();
345 // show the listbox back if we hid it
346 ShowWindow(GetHwnd(), SW_SHOW
);
350 int wxListBox::FindString(const wxString
& s
) const
352 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
359 void wxListBox::Clear()
363 ListBox_ResetContent(GetHwnd());
366 SetHorizontalExtent();
369 void wxListBox::Free()
371 #if wxUSE_OWNER_DRAWN
372 if ( m_windowStyle
& wxLB_OWNERDRAW
)
374 size_t uiCount
= m_aItems
.Count();
375 while ( uiCount
-- != 0 ) {
376 delete m_aItems
[uiCount
];
382 #endif // wxUSE_OWNER_DRAWN
383 if ( HasClientObjectData() )
385 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
387 delete GetClientObject(n
);
392 void wxListBox::SetSelection(int N
, bool select
)
394 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
395 wxT("invalid index in wxListBox::SetSelection") );
397 if ( HasMultipleSelection() )
399 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
403 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
407 bool wxListBox::IsSelected(int N
) const
409 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
410 wxT("invalid index in wxListBox::Selected") );
412 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
415 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
417 return (wxClientData
*)DoGetItemClientData(n
);
420 void *wxListBox::DoGetItemClientData(int n
) const
422 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
423 wxT("invalid index in wxListBox::GetClientData") );
425 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
428 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
430 DoSetItemClientData(n
, clientData
);
433 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
435 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
436 wxT("invalid index in wxListBox::SetClientData") );
438 #if wxUSE_OWNER_DRAWN
439 if ( m_windowStyle
& wxLB_OWNERDRAW
)
441 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
442 // in OnMeasure/OnDraw.
443 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
445 #endif // wxUSE_OWNER_DRAWN
447 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
448 wxLogDebug(wxT("LB_SETITEMDATA failed"));
451 bool wxListBox::HasMultipleSelection() const
453 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
456 // Return number of selections and an array of selected integers
457 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
461 if ( HasMultipleSelection() )
463 int no_sel
= ListBox_GetSelCount(GetHwnd());
465 int *selections
= new int[no_sel
];
466 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
468 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
470 aSelections
.Alloc(no_sel
);
471 for ( int n
= 0; n
< no_sel
; n
++ )
472 aSelections
.Add(selections
[n
]);
474 delete [] selections
;
479 else // single-selection listbox
481 if (ListBox_GetCurSel(GetHwnd()) > -1)
482 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
484 return aSelections
.Count();
488 // Get single selection, for single choice list items
489 int wxListBox::GetSelection() const
491 wxCHECK_MSG( !HasMultipleSelection(),
493 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
495 return ListBox_GetCurSel(GetHwnd());
498 // Find string for position
499 wxString
wxListBox::GetString(int N
) const
501 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
502 wxT("invalid index in wxListBox::GetClientData") );
504 int len
= ListBox_GetTextLen(GetHwnd(), N
);
506 // +1 for terminating NUL
508 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
509 result
.UngetWriteBuf();
515 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
517 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
518 wxT("invalid index in wxListBox::InsertItems") );
520 int nItems
= items
.GetCount();
521 for ( int i
= 0; i
< nItems
; i
++ )
522 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
525 SetHorizontalExtent();
528 void wxListBox::SetString(int N
, const wxString
& s
)
530 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
531 wxT("invalid index in wxListBox::SetString") );
533 // remember the state of the item
534 bool wasSelected
= IsSelected(N
);
536 void *oldData
= NULL
;
537 wxClientData
*oldObjData
= NULL
;
538 if ( m_clientDataItemsType
== ClientData_Void
)
539 oldData
= GetClientData(N
);
540 else if ( m_clientDataItemsType
== ClientData_Object
)
541 oldObjData
= GetClientObject(N
);
543 // delete and recreate it
544 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
547 if ( N
== m_noItems
- 1 )
550 ListBox_InsertString(GetHwnd(), newN
, s
);
552 // restore the client data
554 SetClientData(N
, oldData
);
555 else if ( oldObjData
)
556 SetClientObject(N
, oldObjData
);
558 // we may have lost the selection
562 #if wxUSE_OWNER_DRAWN
563 if ( m_windowStyle
& wxLB_OWNERDRAW
)
564 // update item's text
565 m_aItems
[N
]->SetName(s
);
566 #endif //USE_OWNER_DRAWN
569 int wxListBox::GetCount() const
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 // Windows-specific code to set the horizontal extent of the listbox, if
579 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
580 // Otherwise, all strings are used.
581 void wxListBox::SetHorizontalExtent(const wxString
& s
)
583 // Only necessary if we want a horizontal scrollbar
584 if (!(m_windowStyle
& wxHSCROLL
))
586 TEXTMETRIC lpTextMetric
;
590 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
591 HDC dc
= GetWindowDC(GetHwnd());
593 if (GetFont().Ok() && GetFont().GetResourceHandle())
594 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
596 GetTextMetrics(dc
, &lpTextMetric
);
598 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
599 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
602 ::SelectObject(dc
, oldFont
);
604 ReleaseDC(GetHwnd(), dc
);
605 if (extentX
> existingExtent
)
606 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
610 int largestExtent
= 0;
611 HDC dc
= GetWindowDC(GetHwnd());
613 if (GetFont().Ok() && GetFont().GetResourceHandle())
614 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
616 GetTextMetrics(dc
, &lpTextMetric
);
618 for (i
= 0; i
< m_noItems
; i
++)
620 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
623 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
624 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
625 if (extentX
> largestExtent
)
626 largestExtent
= extentX
;
629 ::SelectObject(dc
, oldFont
);
631 ReleaseDC(GetHwnd(), dc
);
632 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
636 wxSize
wxListBox::DoGetBestSize() const
638 // find the widest string
641 for ( int i
= 0; i
< m_noItems
; i
++ )
643 wxString
str(GetString(i
));
644 GetTextExtent(str
, &wLine
, NULL
);
645 if ( wLine
> wListbox
)
649 // give it some reasonable default value if there are no strings in the
654 // the listbox should be slightly larger than the widest string
656 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
660 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
662 return wxSize(wListbox
, hListbox
);
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
671 if ( param
== LBN_SELCHANGE
)
673 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
674 event
.SetEventObject( this );
676 wxArrayInt aSelections
;
677 int n
, count
= GetSelections(aSelections
);
681 if ( HasClientObjectData() )
682 event
.SetClientObject( GetClientObject(n
) );
683 else if ( HasClientUntypedData() )
684 event
.SetClientData( GetClientData(n
) );
685 event
.SetString( GetString(n
) );
692 event
.m_commandInt
= n
;
694 return GetEventHandler()->ProcessEvent(event
);
696 else if ( param
== LBN_DBLCLK
)
698 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
699 event
.SetEventObject( this );
701 return GetEventHandler()->ProcessEvent(event
);
708 // ----------------------------------------------------------------------------
709 // wxCheckListBox support
710 // ----------------------------------------------------------------------------
712 #if wxUSE_OWNER_DRAWN
717 // space beneath/above each row in pixels
718 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
719 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
721 // the height is the same for all items
722 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
724 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
725 // message is not yet sent when we get here!
726 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
728 // only owner-drawn control should receive this message
729 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
731 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
733 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
736 dc
.SetHDC((WXHDC
)hdc
);
737 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
739 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
740 pStruct
->itemWidth
= dc
.GetCharWidth();
749 // forward the message to the appropriate item
750 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
752 // only owner-drawn control should receive this message
753 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
755 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
756 UINT itemID
= pStruct
->itemID
;
758 // the item may be -1 for an empty listbox
759 if ( itemID
== (UINT
)-1 )
762 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
764 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
766 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
769 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
770 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
771 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
773 return pItem
->OnDrawItem(dc
, rect
,
774 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
775 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);