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 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
149 SetForegroundColour(parent
->GetForegroundColour());
151 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
157 m_windowStyle
= style
;
159 DWORD wstyle
= WS_VISIBLE
| WS_VSCROLL
| WS_TABSTOP
|
160 LBS_NOTIFY
| LBS_HASSTRINGS
/* | WS_CLIPSIBLINGS */;
162 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
163 _T("only one of listbox selection modes can be specified") );
164 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
165 wstyle
|= WS_CLIPSIBLINGS
;
167 if (m_windowStyle
& wxLB_MULTIPLE
)
168 wstyle
|= LBS_MULTIPLESEL
;
169 else if (m_windowStyle
& wxLB_EXTENDED
)
170 wstyle
|= LBS_EXTENDEDSEL
;
172 if (m_windowStyle
& wxLB_ALWAYS_SB
)
173 wstyle
|= LBS_DISABLENOSCROLL
;
174 if (m_windowStyle
& wxLB_HSCROLL
)
175 wstyle
|= WS_HSCROLL
;
176 if (m_windowStyle
& wxLB_SORT
)
179 #if wxUSE_OWNER_DRAWN
180 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
181 // we don't support LBS_OWNERDRAWVARIABLE yet
182 wstyle
|= LBS_OWNERDRAWFIXED
;
186 // Without this style, you get unexpected heights, so e.g. constraint layout
187 // doesn't work properly
188 wstyle
|= LBS_NOINTEGRALHEIGHT
;
191 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
193 // Even with extended styles, need to combine with WS_BORDER for them to
195 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
200 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("LISTBOX"), NULL
,
203 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
204 wxGetInstance(), NULL
);
206 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create listbox") );
211 Ctl3dSubclassCtl(GetHwnd());
216 // Subclass again to catch messages
220 for (ui
= 0; ui
< (size_t)n
; ui
++) {
224 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
225 SendMessage(GetHwnd(), LB_SETCURSEL
, 0, 0);
227 SetFont(parent
->GetFont());
229 SetSize(x
, y
, width
, height
);
234 wxListBox::~wxListBox()
239 void wxListBox::SetupColours()
241 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
242 SetForegroundColour(GetParent()->GetForegroundColour());
245 // ----------------------------------------------------------------------------
246 // implementation of wxListBoxBase methods
247 // ----------------------------------------------------------------------------
249 void wxListBox::DoSetFirstItem(int N
)
251 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
252 wxT("invalid index in wxListBox::SetFirstItem") );
254 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
257 void wxListBox::Delete(int N
)
259 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
260 wxT("invalid index in wxListBox::Delete") );
262 // for owner drawn objects, the data is used for storing wxOwnerDrawn
263 // pointers and we shouldn't touch it
264 #if !wxUSE_OWNER_DRAWN
265 if ( !(m_windowStyle
& wxLB_OWNERDRAW
) )
266 #endif // !wxUSE_OWNER_DRAWN
267 if ( HasClientObjectData() )
269 delete GetClientObject(N
);
272 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
275 SetHorizontalExtent("");
278 int wxListBox::DoAppend(const wxString
& item
)
280 int index
= ListBox_AddString(GetHwnd(), item
);
283 #if wxUSE_OWNER_DRAWN
284 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
285 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
286 pNewItem
->SetName(item
);
287 m_aItems
.Insert(pNewItem
, index
);
288 ListBox_SetItemData(GetHwnd(), index
, pNewItem
);
289 pNewItem
->SetFont(GetFont());
291 #endif // wxUSE_OWNER_DRAWN
293 SetHorizontalExtent(item
);
298 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
300 // avoid flicker - but don't need to do this for a hidden listbox
301 bool hideAndShow
= IsShown();
304 ShowWindow(GetHwnd(), SW_HIDE
);
307 ListBox_ResetContent(GetHwnd());
309 m_noItems
= choices
.GetCount();
311 for (i
= 0; i
< m_noItems
; i
++)
313 ListBox_AddString(GetHwnd(), choices
[i
]);
316 #if wxUSE_OWNER_DRAWN
317 if ( m_windowStyle
& wxLB_OWNERDRAW
)
319 wxASSERT_MSG(clientData
[i
] == NULL
,
320 wxT("Can't use client data with owner-drawn listboxes"));
322 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
323 #else // !wxUSE_OWNER_DRAWN
324 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
325 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
329 #if wxUSE_OWNER_DRAWN
330 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
331 // first delete old items
332 WX_CLEAR_ARRAY(m_aItems
);
334 // then create new ones
335 for ( size_t ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
336 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
337 pNewItem
->SetName(choices
[ui
]);
338 m_aItems
.Add(pNewItem
);
339 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
342 #endif // wxUSE_OWNER_DRAWN
344 SetHorizontalExtent();
348 // show the listbox back if we hid it
349 ShowWindow(GetHwnd(), SW_SHOW
);
353 int wxListBox::FindString(const wxString
& s
) const
355 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
362 void wxListBox::Clear()
366 ListBox_ResetContent(GetHwnd());
369 SetHorizontalExtent();
372 void wxListBox::Free()
374 #if wxUSE_OWNER_DRAWN
375 if ( m_windowStyle
& wxLB_OWNERDRAW
)
377 WX_CLEAR_ARRAY(m_aItems
);
380 #endif // wxUSE_OWNER_DRAWN
381 if ( HasClientObjectData() )
383 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
385 delete GetClientObject(n
);
390 void wxListBox::SetSelection(int N
, bool select
)
392 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
393 wxT("invalid index in wxListBox::SetSelection") );
395 if ( HasMultipleSelection() )
397 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
401 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
405 bool wxListBox::IsSelected(int N
) const
407 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
408 wxT("invalid index in wxListBox::Selected") );
410 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
413 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
415 return (wxClientData
*)DoGetItemClientData(n
);
418 void *wxListBox::DoGetItemClientData(int n
) const
420 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
421 wxT("invalid index in wxListBox::GetClientData") );
423 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
426 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
428 DoSetItemClientData(n
, clientData
);
431 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
433 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
434 wxT("invalid index in wxListBox::SetClientData") );
436 #if wxUSE_OWNER_DRAWN
437 if ( m_windowStyle
& wxLB_OWNERDRAW
)
439 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
440 // in OnMeasure/OnDraw.
441 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
443 #endif // wxUSE_OWNER_DRAWN
445 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
446 wxLogDebug(wxT("LB_SETITEMDATA failed"));
449 // Return number of selections and an array of selected integers
450 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
454 if ( HasMultipleSelection() )
456 int countSel
= ListBox_GetSelCount(GetHwnd());
457 if ( countSel
== LB_ERR
)
459 wxLogDebug(_T("ListBox_GetSelCount failed"));
461 else if ( countSel
!= 0 )
463 int *selections
= new int[countSel
];
465 if ( ListBox_GetSelItems(GetHwnd(),
466 countSel
, selections
) == LB_ERR
)
468 wxLogDebug(wxT("ListBox_GetSelItems failed"));
473 aSelections
.Alloc(countSel
);
474 for ( int n
= 0; n
< countSel
; n
++ )
475 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
++ )
527 int idx
= ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
529 #if wxUSE_OWNER_DRAWN
530 if ( m_windowStyle
& wxLB_OWNERDRAW
)
532 wxOwnerDrawn
*pNewItem
= CreateItem(idx
);
533 pNewItem
->SetName(items
[i
]);
534 pNewItem
->SetFont(GetFont());
535 m_aItems
.Insert(pNewItem
, idx
);
537 ListBox_SetItemData(GetHwnd(), idx
, pNewItem
);
539 #endif // wxUSE_OWNER_DRAWN
544 SetHorizontalExtent();
547 void wxListBox::SetString(int N
, const wxString
& s
)
549 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
550 wxT("invalid index in wxListBox::SetString") );
552 // remember the state of the item
553 bool wasSelected
= IsSelected(N
);
555 void *oldData
= NULL
;
556 wxClientData
*oldObjData
= NULL
;
557 if ( m_clientDataItemsType
== wxClientData_Void
)
558 oldData
= GetClientData(N
);
559 else if ( m_clientDataItemsType
== wxClientData_Object
)
560 oldObjData
= GetClientObject(N
);
562 // delete and recreate it
563 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
566 if ( N
== m_noItems
- 1 )
569 ListBox_InsertString(GetHwnd(), newN
, s
);
571 // restore the client data
573 SetClientData(N
, oldData
);
574 else if ( oldObjData
)
575 SetClientObject(N
, oldObjData
);
577 // we may have lost the selection
581 #if wxUSE_OWNER_DRAWN
582 if ( m_windowStyle
& wxLB_OWNERDRAW
)
584 // update item's text
585 m_aItems
[N
]->SetName(s
);
587 // reassign the item's data
588 ListBox_SetItemData(GetHwnd(), N
, m_aItems
[N
]);
590 #endif //USE_OWNER_DRAWN
593 int wxListBox::GetCount() const
598 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
602 // Windows-specific code to set the horizontal extent of the listbox, if
603 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
604 // Otherwise, all strings are used.
605 void wxListBox::SetHorizontalExtent(const wxString
& s
)
607 // Only necessary if we want a horizontal scrollbar
608 if (!(m_windowStyle
& wxHSCROLL
))
610 TEXTMETRIC lpTextMetric
;
614 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
615 HDC dc
= GetWindowDC(GetHwnd());
617 if (GetFont().Ok() && GetFont().GetResourceHandle())
618 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
620 GetTextMetrics(dc
, &lpTextMetric
);
622 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
623 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
626 ::SelectObject(dc
, oldFont
);
628 ReleaseDC(GetHwnd(), dc
);
629 if (extentX
> existingExtent
)
630 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
634 int largestExtent
= 0;
635 HDC dc
= GetWindowDC(GetHwnd());
637 if (GetFont().Ok() && GetFont().GetResourceHandle())
638 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
640 GetTextMetrics(dc
, &lpTextMetric
);
642 for (i
= 0; i
< m_noItems
; i
++)
644 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
647 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
648 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
649 if (extentX
> largestExtent
)
650 largestExtent
= extentX
;
653 ::SelectObject(dc
, oldFont
);
655 ReleaseDC(GetHwnd(), dc
);
656 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
660 wxSize
wxListBox::DoGetBestSize() const
662 // find the widest string
665 for ( int i
= 0; i
< m_noItems
; i
++ )
667 wxString
str(GetString(i
));
668 GetTextExtent(str
, &wLine
, NULL
);
669 if ( wLine
> wListbox
)
673 // give it some reasonable default value if there are no strings in the
678 // the listbox should be slightly larger than the widest string
680 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
684 // don't make the listbox too tall (limit height to 10 items) but don't
685 // make it too small neither
686 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*
687 wxMin(wxMax(m_noItems
, 3), 10);
689 return wxSize(wListbox
, hListbox
);
692 // ----------------------------------------------------------------------------
694 // ----------------------------------------------------------------------------
696 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
699 if ( param
== LBN_SELCHANGE
)
701 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
703 else if ( param
== LBN_DBLCLK
)
705 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
709 // some event we're not interested in
713 wxCommandEvent
event(evtType
, m_windowId
);
714 event
.SetEventObject( this );
716 wxArrayInt aSelections
;
717 int n
, count
= GetSelections(aSelections
);
721 if ( HasClientObjectData() )
722 event
.SetClientObject( GetClientObject(n
) );
723 else if ( HasClientUntypedData() )
724 event
.SetClientData( GetClientData(n
) );
725 event
.SetString( GetString(n
) );
732 event
.m_commandInt
= n
;
734 return GetEventHandler()->ProcessEvent(event
);
737 // ----------------------------------------------------------------------------
738 // wxCheckListBox support
739 // ----------------------------------------------------------------------------
741 #if wxUSE_OWNER_DRAWN
746 // space beneath/above each row in pixels
747 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
748 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
750 // the height is the same for all items
751 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
753 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
754 // message is not yet sent when we get here!
755 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
757 // only owner-drawn control should receive this message
758 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
760 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
762 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
765 dc
.SetHDC((WXHDC
)hdc
);
766 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT
));
768 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
769 pStruct
->itemWidth
= dc
.GetCharWidth();
778 // forward the message to the appropriate item
779 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
781 // only owner-drawn control should receive this message
782 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
784 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
785 UINT itemID
= pStruct
->itemID
;
787 // the item may be -1 for an empty listbox
788 if ( itemID
== (UINT
)-1 )
791 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
793 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
795 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
797 wxDCTemp
dc((WXHDC
)pStruct
->hDC
);
798 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
799 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
801 return pItem
->OnDrawItem(dc
, rect
,
802 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
803 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);
806 #endif // wxUSE_OWNER_DRAWN
808 #endif // wxUSE_LISTBOX