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 #if defined(__GNUWIN32__)
52 #ifndef wxUSE_NORLANDER_HEADERS
53 #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 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
;
162 if (m_windowStyle
& wxLB_MULTIPLE
)
163 wstyle
|= LBS_MULTIPLESEL
;
164 else if (m_windowStyle
& wxLB_EXTENDED
)
165 wstyle
|= LBS_EXTENDEDSEL
;
167 if (m_windowStyle
& wxLB_ALWAYS_SB
)
168 wstyle
|= LBS_DISABLENOSCROLL
;
169 if (m_windowStyle
& wxLB_HSCROLL
)
170 wstyle
|= WS_HSCROLL
;
171 if (m_windowStyle
& wxLB_SORT
)
174 #if wxUSE_OWNER_DRAWN
175 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
176 // we don't support LBS_OWNERDRAWVARIABLE yet
177 wstyle
|= LBS_OWNERDRAWFIXED
;
181 // Without this style, you get unexpected heights, so e.g. constraint layout
182 // doesn't work properly
183 wstyle
|= LBS_NOINTEGRALHEIGHT
;
186 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
188 // Even with extended styles, need to combine with WS_BORDER for them to
190 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
195 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("LISTBOX"), NULL
,
198 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
199 wxGetInstance(), NULL
);
201 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create listbox") );
206 Ctl3dSubclassCtl(GetHwnd());
211 // Subclass again to catch messages
215 for (ui
= 0; ui
< (size_t)n
; ui
++) {
219 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
220 SendMessage(GetHwnd(), LB_SETCURSEL
, 0, 0);
222 SetFont(parent
->GetFont());
224 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 ShowWindow(GetHwnd(), SW_HIDE
);
299 ListBox_ResetContent(GetHwnd());
301 m_noItems
= choices
.GetCount();
303 for (i
= 0; i
< m_noItems
; i
++)
305 ListBox_AddString(GetHwnd(), choices
[i
]);
308 #if wxUSE_OWNER_DRAWN
309 wxASSERT_MSG(clientData
[i
] == NULL
,
310 wxT("Can't use client data with owner-drawn listboxes"));
311 #else // !wxUSE_OWNER_DRAWN
312 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
313 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
317 #if wxUSE_OWNER_DRAWN
318 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
319 // first delete old items
320 size_t ui
= m_aItems
.Count();
321 while ( ui
-- != 0 ) {
326 // then create new ones
327 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
328 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
329 pNewItem
->SetName(choices
[ui
]);
330 m_aItems
.Add(pNewItem
);
331 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
334 #endif // wxUSE_OWNER_DRAWN
336 SetHorizontalExtent();
338 ShowWindow(GetHwnd(), SW_SHOW
);
341 int wxListBox::FindString(const wxString
& s
) const
343 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
350 void wxListBox::Clear()
354 ListBox_ResetContent(GetHwnd());
357 SetHorizontalExtent();
360 void wxListBox::Free()
362 #if wxUSE_OWNER_DRAWN
363 if ( m_windowStyle
& wxLB_OWNERDRAW
)
365 size_t uiCount
= m_aItems
.Count();
366 while ( uiCount
-- != 0 ) {
367 delete m_aItems
[uiCount
];
373 #endif // wxUSE_OWNER_DRAWN
374 if ( HasClientObjectData() )
376 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
378 delete GetClientObject(n
);
383 void wxListBox::SetSelection(int N
, bool select
)
385 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
386 wxT("invalid index in wxListBox::SetSelection") );
388 if ( HasMultipleSelection() )
390 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
394 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
398 bool wxListBox::IsSelected(int N
) const
400 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
401 wxT("invalid index in wxListBox::Selected") );
403 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
406 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
408 return (wxClientData
*)DoGetItemClientData(n
);
411 void *wxListBox::DoGetItemClientData(int n
) const
413 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
414 wxT("invalid index in wxListBox::GetClientData") );
416 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
419 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
421 DoSetItemClientData(n
, clientData
);
424 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
426 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
427 wxT("invalid index in wxListBox::SetClientData") );
429 #if wxUSE_OWNER_DRAWN
430 if ( m_windowStyle
& wxLB_OWNERDRAW
)
432 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
433 // in OnMeasure/OnDraw.
434 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
436 #endif // wxUSE_OWNER_DRAWN
438 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
439 wxLogDebug(wxT("LB_SETITEMDATA failed"));
442 bool wxListBox::HasMultipleSelection() const
444 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
447 // Return number of selections and an array of selected integers
448 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
452 if ( HasMultipleSelection() )
454 int no_sel
= ListBox_GetSelCount(GetHwnd());
456 int *selections
= new int[no_sel
];
457 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
459 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
461 aSelections
.Alloc(no_sel
);
462 for ( int n
= 0; n
< no_sel
; n
++ )
463 aSelections
.Add(selections
[n
]);
465 delete [] selections
;
470 else // single-selection listbox
472 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
478 // Get single selection, for single choice list items
479 int wxListBox::GetSelection() const
481 wxCHECK_MSG( !HasMultipleSelection(),
483 wxT("GetSelection() can't be used with multiple-selection "
484 "listboxes, use GetSelections() instead.") );
486 return ListBox_GetCurSel(GetHwnd());
489 // Find string for position
490 wxString
wxListBox::GetString(int N
) const
492 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
493 wxT("invalid index in wxListBox::GetClientData") );
495 int len
= ListBox_GetTextLen(GetHwnd(), N
);
497 // +1 for terminating NUL
499 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
500 result
.UngetWriteBuf();
506 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
508 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
509 wxT("invalid index in wxListBox::InsertItems") );
511 int nItems
= items
.GetCount();
512 for ( int i
= 0; i
< nItems
; i
++ )
513 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
516 SetHorizontalExtent();
519 void wxListBox::SetString(int N
, const wxString
& s
)
521 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
522 wxT("invalid index in wxListBox::SetString") );
524 // remember the state of the item
525 bool wasSelected
= IsSelected(N
);
527 void *oldData
= NULL
;
528 wxClientData
*oldObjData
= NULL
;
529 if ( m_clientDataItemsType
== ClientData_Void
)
530 oldData
= GetClientData(N
);
531 else if ( m_clientDataItemsType
== ClientData_Object
)
532 oldObjData
= GetClientObject(N
);
534 // delete and recreate it
535 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
538 if ( N
== m_noItems
- 1 )
541 ListBox_InsertString(GetHwnd(), newN
, s
);
543 // restore the client data
545 SetClientData(N
, oldData
);
546 else if ( oldObjData
)
547 SetClientObject(N
, oldObjData
);
549 // we may have lost the selection
553 #if wxUSE_OWNER_DRAWN
554 if ( m_windowStyle
& wxLB_OWNERDRAW
)
555 // update item's text
556 m_aItems
[N
]->SetName(s
);
557 #endif //USE_OWNER_DRAWN
560 int wxListBox::GetCount() const
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 // Windows-specific code to set the horizontal extent of the listbox, if
570 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
571 // Otherwise, all strings are used.
572 void wxListBox::SetHorizontalExtent(const wxString
& s
)
574 // Only necessary if we want a horizontal scrollbar
575 if (!(m_windowStyle
& wxHSCROLL
))
577 TEXTMETRIC lpTextMetric
;
581 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
582 HDC dc
= GetWindowDC(GetHwnd());
584 if (GetFont().Ok() && GetFont().GetResourceHandle())
585 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
587 GetTextMetrics(dc
, &lpTextMetric
);
589 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
590 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
593 ::SelectObject(dc
, oldFont
);
595 ReleaseDC(GetHwnd(), dc
);
596 if (extentX
> existingExtent
)
597 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
601 int largestExtent
= 0;
602 HDC dc
= GetWindowDC(GetHwnd());
604 if (GetFont().Ok() && GetFont().GetResourceHandle())
605 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
607 GetTextMetrics(dc
, &lpTextMetric
);
609 for (i
= 0; i
< m_noItems
; i
++)
611 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
614 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
615 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
616 if (extentX
> largestExtent
)
617 largestExtent
= extentX
;
620 ::SelectObject(dc
, oldFont
);
622 ReleaseDC(GetHwnd(), dc
);
623 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
627 wxSize
wxListBox::DoGetBestSize() const
629 // find the widest string
632 for ( int i
= 0; i
< m_noItems
; i
++ )
634 wxString
str(GetString(i
));
635 GetTextExtent(str
, &wLine
, NULL
);
636 if ( wLine
> wListbox
)
640 // give it some reasonable default value if there are no strings in the
645 // the listbox should be slightly larger than the widest string
647 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
651 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
653 return wxSize(wListbox
, hListbox
);
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
662 if ( param
== LBN_SELCHANGE
)
664 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
665 event
.SetEventObject( this );
667 wxArrayInt aSelections
;
668 int n
, count
= GetSelections(aSelections
);
672 if ( HasClientObjectData() )
673 event
.SetClientObject( GetClientObject(n
) );
674 else if ( HasClientUntypedData() )
675 event
.SetClientData( GetClientData(n
) );
676 event
.SetString( GetString(n
) );
683 event
.m_commandInt
= n
;
685 return GetEventHandler()->ProcessEvent(event
);
687 else if ( param
== LBN_DBLCLK
)
689 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
690 event
.SetEventObject( this );
692 return GetEventHandler()->ProcessEvent(event
);
699 // ----------------------------------------------------------------------------
700 // wxCheckListBox support
701 // ----------------------------------------------------------------------------
703 #if wxUSE_OWNER_DRAWN
708 // space beneath/above each row in pixels
709 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
710 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
712 // the height is the same for all items
713 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
715 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
716 // message is not yet sent when we get here!
717 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
719 // only owner-drawn control should receive this message
720 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
722 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
724 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
727 dc
.SetHDC((WXHDC
)hdc
);
728 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
730 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
731 pStruct
->itemWidth
= dc
.GetCharWidth();
740 // forward the message to the appropriate item
741 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
743 // only owner-drawn control should receive this message
744 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
746 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
748 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
750 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
752 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
755 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
756 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
757 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
759 return pItem
->OnDrawItem(dc
, rect
,
760 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
761 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);