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 WXUNUSED(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 */;
161 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
162 _T("only one of listbox selection modes can be specified") );
164 if (m_windowStyle
& wxLB_MULTIPLE
)
165 wstyle
|= LBS_MULTIPLESEL
;
166 else if (m_windowStyle
& wxLB_EXTENDED
)
167 wstyle
|= LBS_EXTENDEDSEL
;
169 if (m_windowStyle
& wxLB_ALWAYS_SB
)
170 wstyle
|= LBS_DISABLENOSCROLL
;
171 if (m_windowStyle
& wxLB_HSCROLL
)
172 wstyle
|= WS_HSCROLL
;
173 if (m_windowStyle
& wxLB_SORT
)
176 #if wxUSE_OWNER_DRAWN
177 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
178 // we don't support LBS_OWNERDRAWVARIABLE yet
179 wstyle
|= LBS_OWNERDRAWFIXED
;
183 // Without this style, you get unexpected heights, so e.g. constraint layout
184 // doesn't work properly
185 wstyle
|= LBS_NOINTEGRALHEIGHT
;
188 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
190 // Even with extended styles, need to combine with WS_BORDER for them to
192 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
197 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("LISTBOX"), NULL
,
200 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
201 wxGetInstance(), NULL
);
203 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create listbox") );
208 Ctl3dSubclassCtl(GetHwnd());
213 // Subclass again to catch messages
217 for (ui
= 0; ui
< (size_t)n
; ui
++) {
221 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
222 SendMessage(GetHwnd(), LB_SETCURSEL
, 0, 0);
224 SetFont(parent
->GetFont());
226 SetSize(x
, y
, width
, height
);
231 wxListBox::~wxListBox()
236 void wxListBox::SetupColours()
238 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
239 SetForegroundColour(GetParent()->GetForegroundColour());
242 // ----------------------------------------------------------------------------
243 // implementation of wxListBoxBase methods
244 // ----------------------------------------------------------------------------
246 void wxListBox::DoSetFirstItem(int N
)
248 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
249 wxT("invalid index in wxListBox::SetFirstItem") );
251 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
254 void wxListBox::Delete(int N
)
256 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
257 wxT("invalid index in wxListBox::Delete") );
259 // for owner drawn objects, the data is used for storing wxOwnerDrawn
260 // pointers and we shouldn't touch it
261 #if !wxUSE_OWNER_DRAWN
262 if ( !(m_windowStyle
& wxLB_OWNERDRAW
) )
263 #endif // !wxUSE_OWNER_DRAWN
264 if ( HasClientObjectData() )
266 delete GetClientObject(N
);
269 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
272 SetHorizontalExtent("");
275 int wxListBox::DoAppend(const wxString
& item
)
277 int index
= ListBox_AddString(GetHwnd(), item
);
280 #if wxUSE_OWNER_DRAWN
281 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
282 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
283 pNewItem
->SetName(item
);
284 m_aItems
.Add(pNewItem
);
285 ListBox_SetItemData(GetHwnd(), index
, pNewItem
);
286 pNewItem
->SetFont(GetFont());
290 SetHorizontalExtent(item
);
295 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
297 // avoid flicker - but don't need to do this for a hidden listbox
298 bool hideAndShow
= IsShown();
301 ShowWindow(GetHwnd(), SW_HIDE
);
304 ListBox_ResetContent(GetHwnd());
306 m_noItems
= choices
.GetCount();
308 for (i
= 0; i
< m_noItems
; i
++)
310 ListBox_AddString(GetHwnd(), choices
[i
]);
313 #if wxUSE_OWNER_DRAWN
314 if ( m_windowStyle
& wxLB_OWNERDRAW
)
316 wxASSERT_MSG(clientData
[i
] == NULL
,
317 wxT("Can't use client data with owner-drawn listboxes"));
319 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
320 #else // !wxUSE_OWNER_DRAWN
321 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
322 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
326 #if wxUSE_OWNER_DRAWN
327 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
328 // first delete old items
329 size_t ui
= m_aItems
.Count();
330 while ( ui
-- != 0 ) {
335 // then create new ones
336 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
337 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
338 pNewItem
->SetName(choices
[ui
]);
339 m_aItems
.Add(pNewItem
);
340 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
343 #endif // wxUSE_OWNER_DRAWN
345 SetHorizontalExtent();
349 // show the listbox back if we hid it
350 ShowWindow(GetHwnd(), SW_SHOW
);
354 int wxListBox::FindString(const wxString
& s
) const
356 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
363 void wxListBox::Clear()
367 ListBox_ResetContent(GetHwnd());
370 SetHorizontalExtent();
373 void wxListBox::Free()
375 #if wxUSE_OWNER_DRAWN
376 if ( m_windowStyle
& wxLB_OWNERDRAW
)
378 size_t uiCount
= m_aItems
.Count();
379 while ( uiCount
-- != 0 ) {
380 delete m_aItems
[uiCount
];
386 #endif // wxUSE_OWNER_DRAWN
387 if ( HasClientObjectData() )
389 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
391 delete GetClientObject(n
);
396 void wxListBox::SetSelection(int N
, bool select
)
398 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
399 wxT("invalid index in wxListBox::SetSelection") );
401 if ( HasMultipleSelection() )
403 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
407 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
411 bool wxListBox::IsSelected(int N
) const
413 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
414 wxT("invalid index in wxListBox::Selected") );
416 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
419 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
421 return (wxClientData
*)DoGetItemClientData(n
);
424 void *wxListBox::DoGetItemClientData(int n
) const
426 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
427 wxT("invalid index in wxListBox::GetClientData") );
429 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
432 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
434 DoSetItemClientData(n
, clientData
);
437 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
439 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
440 wxT("invalid index in wxListBox::SetClientData") );
442 #if wxUSE_OWNER_DRAWN
443 if ( m_windowStyle
& wxLB_OWNERDRAW
)
445 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
446 // in OnMeasure/OnDraw.
447 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
449 #endif // wxUSE_OWNER_DRAWN
451 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
452 wxLogDebug(wxT("LB_SETITEMDATA failed"));
455 bool wxListBox::HasMultipleSelection() const
457 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
460 // Return number of selections and an array of selected integers
461 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
465 if ( HasMultipleSelection() )
467 int no_sel
= ListBox_GetSelCount(GetHwnd());
469 int *selections
= new int[no_sel
];
470 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
472 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
474 aSelections
.Alloc(no_sel
);
475 for ( int n
= 0; n
< no_sel
; n
++ )
476 aSelections
.Add(selections
[n
]);
478 delete [] selections
;
483 else // single-selection listbox
485 if (ListBox_GetCurSel(GetHwnd()) > -1)
486 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
488 return aSelections
.Count();
492 // Get single selection, for single choice list items
493 int wxListBox::GetSelection() const
495 wxCHECK_MSG( !HasMultipleSelection(),
497 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
499 return ListBox_GetCurSel(GetHwnd());
502 // Find string for position
503 wxString
wxListBox::GetString(int N
) const
505 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
506 wxT("invalid index in wxListBox::GetClientData") );
508 int len
= ListBox_GetTextLen(GetHwnd(), N
);
510 // +1 for terminating NUL
512 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
513 result
.UngetWriteBuf();
519 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
521 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
522 wxT("invalid index in wxListBox::InsertItems") );
524 int nItems
= items
.GetCount();
525 for ( int i
= 0; i
< nItems
; i
++ )
526 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
529 SetHorizontalExtent();
532 void wxListBox::SetString(int N
, const wxString
& s
)
534 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
535 wxT("invalid index in wxListBox::SetString") );
537 // remember the state of the item
538 bool wasSelected
= IsSelected(N
);
540 void *oldData
= NULL
;
541 wxClientData
*oldObjData
= NULL
;
542 if ( m_clientDataItemsType
== ClientData_Void
)
543 oldData
= GetClientData(N
);
544 else if ( m_clientDataItemsType
== ClientData_Object
)
545 oldObjData
= GetClientObject(N
);
547 // delete and recreate it
548 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
551 if ( N
== m_noItems
- 1 )
554 ListBox_InsertString(GetHwnd(), newN
, s
);
556 // restore the client data
558 SetClientData(N
, oldData
);
559 else if ( oldObjData
)
560 SetClientObject(N
, oldObjData
);
562 // we may have lost the selection
566 #if wxUSE_OWNER_DRAWN
567 if ( m_windowStyle
& wxLB_OWNERDRAW
)
568 // update item's text
569 m_aItems
[N
]->SetName(s
);
570 #endif //USE_OWNER_DRAWN
573 int wxListBox::GetCount() const
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 // Windows-specific code to set the horizontal extent of the listbox, if
583 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
584 // Otherwise, all strings are used.
585 void wxListBox::SetHorizontalExtent(const wxString
& s
)
587 // Only necessary if we want a horizontal scrollbar
588 if (!(m_windowStyle
& wxHSCROLL
))
590 TEXTMETRIC lpTextMetric
;
594 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
595 HDC dc
= GetWindowDC(GetHwnd());
597 if (GetFont().Ok() && GetFont().GetResourceHandle())
598 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
600 GetTextMetrics(dc
, &lpTextMetric
);
602 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
603 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
606 ::SelectObject(dc
, oldFont
);
608 ReleaseDC(GetHwnd(), dc
);
609 if (extentX
> existingExtent
)
610 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
614 int largestExtent
= 0;
615 HDC dc
= GetWindowDC(GetHwnd());
617 if (GetFont().Ok() && GetFont().GetResourceHandle())
618 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
620 GetTextMetrics(dc
, &lpTextMetric
);
622 for (i
= 0; i
< m_noItems
; i
++)
624 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
627 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
628 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
629 if (extentX
> largestExtent
)
630 largestExtent
= extentX
;
633 ::SelectObject(dc
, oldFont
);
635 ReleaseDC(GetHwnd(), dc
);
636 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
640 wxSize
wxListBox::DoGetBestSize() const
642 // find the widest string
645 for ( int i
= 0; i
< m_noItems
; i
++ )
647 wxString
str(GetString(i
));
648 GetTextExtent(str
, &wLine
, NULL
);
649 if ( wLine
> wListbox
)
653 // give it some reasonable default value if there are no strings in the
658 // the listbox should be slightly larger than the widest string
660 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
664 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
666 return wxSize(wListbox
, hListbox
);
669 // ----------------------------------------------------------------------------
671 // ----------------------------------------------------------------------------
673 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
676 if ( param
== LBN_SELCHANGE
)
678 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
680 else if ( param
== LBN_DBLCLK
)
682 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
686 // some event we're not interested in
690 wxCommandEvent
event(evtType
, m_windowId
);
691 event
.SetEventObject( this );
693 wxArrayInt aSelections
;
694 int n
, count
= GetSelections(aSelections
);
698 if ( HasClientObjectData() )
699 event
.SetClientObject( GetClientObject(n
) );
700 else if ( HasClientUntypedData() )
701 event
.SetClientData( GetClientData(n
) );
702 event
.SetString( GetString(n
) );
709 event
.m_commandInt
= n
;
711 return GetEventHandler()->ProcessEvent(event
);
714 // ----------------------------------------------------------------------------
715 // wxCheckListBox support
716 // ----------------------------------------------------------------------------
718 #if wxUSE_OWNER_DRAWN
723 // space beneath/above each row in pixels
724 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
725 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
727 // the height is the same for all items
728 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
730 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
731 // message is not yet sent when we get here!
732 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
734 // only owner-drawn control should receive this message
735 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
737 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
739 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
742 dc
.SetHDC((WXHDC
)hdc
);
743 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
745 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
746 pStruct
->itemWidth
= dc
.GetCharWidth();
755 // forward the message to the appropriate item
756 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
758 // only owner-drawn control should receive this message
759 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
761 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
762 UINT itemID
= pStruct
->itemID
;
764 // the item may be -1 for an empty listbox
765 if ( itemID
== (UINT
)-1 )
768 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
770 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
772 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
775 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
776 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
777 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
779 return pItem
->OnDrawItem(dc
, rect
,
780 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
781 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);