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 wxASSERT_MSG(clientData
[i
] == NULL
,
311 wxT("Can't use client data with owner-drawn listboxes"));
312 #else // !wxUSE_OWNER_DRAWN
313 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
314 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
318 #if wxUSE_OWNER_DRAWN
319 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
320 // first delete old items
321 size_t ui
= m_aItems
.Count();
322 while ( ui
-- != 0 ) {
327 // then create new ones
328 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
329 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
330 pNewItem
->SetName(choices
[ui
]);
331 m_aItems
.Add(pNewItem
);
332 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
335 #endif // wxUSE_OWNER_DRAWN
337 SetHorizontalExtent();
341 // show the listbox back if we hid it
342 ShowWindow(GetHwnd(), SW_SHOW
);
346 int wxListBox::FindString(const wxString
& s
) const
348 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
355 void wxListBox::Clear()
359 ListBox_ResetContent(GetHwnd());
362 SetHorizontalExtent();
365 void wxListBox::Free()
367 #if wxUSE_OWNER_DRAWN
368 if ( m_windowStyle
& wxLB_OWNERDRAW
)
370 size_t uiCount
= m_aItems
.Count();
371 while ( uiCount
-- != 0 ) {
372 delete m_aItems
[uiCount
];
378 #endif // wxUSE_OWNER_DRAWN
379 if ( HasClientObjectData() )
381 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
383 delete GetClientObject(n
);
388 void wxListBox::SetSelection(int N
, bool select
)
390 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
391 wxT("invalid index in wxListBox::SetSelection") );
393 if ( HasMultipleSelection() )
395 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
399 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
403 bool wxListBox::IsSelected(int N
) const
405 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
406 wxT("invalid index in wxListBox::Selected") );
408 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
411 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
413 return (wxClientData
*)DoGetItemClientData(n
);
416 void *wxListBox::DoGetItemClientData(int n
) const
418 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
419 wxT("invalid index in wxListBox::GetClientData") );
421 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
424 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
426 DoSetItemClientData(n
, clientData
);
429 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
431 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
432 wxT("invalid index in wxListBox::SetClientData") );
434 #if wxUSE_OWNER_DRAWN
435 if ( m_windowStyle
& wxLB_OWNERDRAW
)
437 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
438 // in OnMeasure/OnDraw.
439 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
441 #endif // wxUSE_OWNER_DRAWN
443 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
444 wxLogDebug(wxT("LB_SETITEMDATA failed"));
447 bool wxListBox::HasMultipleSelection() const
449 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
452 // Return number of selections and an array of selected integers
453 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
457 if ( HasMultipleSelection() )
459 int no_sel
= ListBox_GetSelCount(GetHwnd());
461 int *selections
= new int[no_sel
];
462 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
464 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
466 aSelections
.Alloc(no_sel
);
467 for ( int n
= 0; n
< no_sel
; n
++ )
468 aSelections
.Add(selections
[n
]);
470 delete [] selections
;
475 else // single-selection listbox
477 if (ListBox_GetCurSel(GetHwnd()) > -1)
478 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
480 return aSelections
.Count();
484 // Get single selection, for single choice list items
485 int wxListBox::GetSelection() const
487 wxCHECK_MSG( !HasMultipleSelection(),
489 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
491 return ListBox_GetCurSel(GetHwnd());
494 // Find string for position
495 wxString
wxListBox::GetString(int N
) const
497 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
498 wxT("invalid index in wxListBox::GetClientData") );
500 int len
= ListBox_GetTextLen(GetHwnd(), N
);
502 // +1 for terminating NUL
504 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
505 result
.UngetWriteBuf();
511 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
513 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
514 wxT("invalid index in wxListBox::InsertItems") );
516 int nItems
= items
.GetCount();
517 for ( int i
= 0; i
< nItems
; i
++ )
518 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
521 SetHorizontalExtent();
524 void wxListBox::SetString(int N
, const wxString
& s
)
526 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
527 wxT("invalid index in wxListBox::SetString") );
529 // remember the state of the item
530 bool wasSelected
= IsSelected(N
);
532 void *oldData
= NULL
;
533 wxClientData
*oldObjData
= NULL
;
534 if ( m_clientDataItemsType
== ClientData_Void
)
535 oldData
= GetClientData(N
);
536 else if ( m_clientDataItemsType
== ClientData_Object
)
537 oldObjData
= GetClientObject(N
);
539 // delete and recreate it
540 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
543 if ( N
== m_noItems
- 1 )
546 ListBox_InsertString(GetHwnd(), newN
, s
);
548 // restore the client data
550 SetClientData(N
, oldData
);
551 else if ( oldObjData
)
552 SetClientObject(N
, oldObjData
);
554 // we may have lost the selection
558 #if wxUSE_OWNER_DRAWN
559 if ( m_windowStyle
& wxLB_OWNERDRAW
)
560 // update item's text
561 m_aItems
[N
]->SetName(s
);
562 #endif //USE_OWNER_DRAWN
565 int wxListBox::GetCount() const
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 // Windows-specific code to set the horizontal extent of the listbox, if
575 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
576 // Otherwise, all strings are used.
577 void wxListBox::SetHorizontalExtent(const wxString
& s
)
579 // Only necessary if we want a horizontal scrollbar
580 if (!(m_windowStyle
& wxHSCROLL
))
582 TEXTMETRIC lpTextMetric
;
586 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
587 HDC dc
= GetWindowDC(GetHwnd());
589 if (GetFont().Ok() && GetFont().GetResourceHandle())
590 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
592 GetTextMetrics(dc
, &lpTextMetric
);
594 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
595 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
598 ::SelectObject(dc
, oldFont
);
600 ReleaseDC(GetHwnd(), dc
);
601 if (extentX
> existingExtent
)
602 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
606 int largestExtent
= 0;
607 HDC dc
= GetWindowDC(GetHwnd());
609 if (GetFont().Ok() && GetFont().GetResourceHandle())
610 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
612 GetTextMetrics(dc
, &lpTextMetric
);
614 for (i
= 0; i
< m_noItems
; i
++)
616 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
619 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
620 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
621 if (extentX
> largestExtent
)
622 largestExtent
= extentX
;
625 ::SelectObject(dc
, oldFont
);
627 ReleaseDC(GetHwnd(), dc
);
628 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
632 wxSize
wxListBox::DoGetBestSize() const
634 // find the widest string
637 for ( int i
= 0; i
< m_noItems
; i
++ )
639 wxString
str(GetString(i
));
640 GetTextExtent(str
, &wLine
, NULL
);
641 if ( wLine
> wListbox
)
645 // give it some reasonable default value if there are no strings in the
650 // the listbox should be slightly larger than the widest string
652 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
656 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
658 return wxSize(wListbox
, hListbox
);
661 // ----------------------------------------------------------------------------
663 // ----------------------------------------------------------------------------
665 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
667 if ( param
== LBN_SELCHANGE
)
669 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
670 event
.SetEventObject( this );
672 wxArrayInt aSelections
;
673 int n
, count
= GetSelections(aSelections
);
677 if ( HasClientObjectData() )
678 event
.SetClientObject( GetClientObject(n
) );
679 else if ( HasClientUntypedData() )
680 event
.SetClientData( GetClientData(n
) );
681 event
.SetString( GetString(n
) );
688 event
.m_commandInt
= n
;
690 return GetEventHandler()->ProcessEvent(event
);
692 else if ( param
== LBN_DBLCLK
)
694 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
695 event
.SetEventObject( this );
697 return GetEventHandler()->ProcessEvent(event
);
704 // ----------------------------------------------------------------------------
705 // wxCheckListBox support
706 // ----------------------------------------------------------------------------
708 #if wxUSE_OWNER_DRAWN
713 // space beneath/above each row in pixels
714 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
715 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
717 // the height is the same for all items
718 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
720 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
721 // message is not yet sent when we get here!
722 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
724 // only owner-drawn control should receive this message
725 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
727 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
729 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
732 dc
.SetHDC((WXHDC
)hdc
);
733 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
735 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
736 pStruct
->itemWidth
= dc
.GetCharWidth();
745 // forward the message to the appropriate item
746 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
748 // only owner-drawn control should receive this message
749 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
751 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
752 UINT itemID
= pStruct
->itemID
;
754 // the item may be -1 for an empty listbox
755 if ( itemID
== (UINT
)-1 )
758 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
760 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
762 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
765 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
766 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
767 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
769 return pItem
->OnDrawItem(dc
, rect
,
770 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
771 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);