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
;
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
);
229 wxListBox::~wxListBox()
234 void wxListBox::SetupColours()
236 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
237 SetForegroundColour(GetParent()->GetForegroundColour());
240 // ----------------------------------------------------------------------------
241 // implementation of wxListBoxBase methods
242 // ----------------------------------------------------------------------------
244 void wxListBox::DoSetFirstItem(int N
)
246 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
247 wxT("invalid index in wxListBox::SetFirstItem") );
249 SendMessage(GetHwnd(), LB_SETTOPINDEX
, (WPARAM
)N
, (LPARAM
)0);
252 void wxListBox::Delete(int N
)
254 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
255 wxT("invalid index in wxListBox::Delete") );
257 // for owner drawn objects, the data is used for storing wxOwnerDrawn
258 // pointers and we shouldn't touch it
259 #if !wxUSE_OWNER_DRAWN
260 if ( !(m_windowStyle
& wxLB_OWNERDRAW
) )
261 #endif // !wxUSE_OWNER_DRAWN
262 if ( HasClientObjectData() )
264 delete GetClientObject(N
);
267 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
270 SetHorizontalExtent("");
273 int wxListBox::DoAppend(const wxString
& item
)
275 int index
= ListBox_AddString(GetHwnd(), item
);
278 #if wxUSE_OWNER_DRAWN
279 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
280 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
281 pNewItem
->SetName(item
);
282 m_aItems
.Add(pNewItem
);
283 ListBox_SetItemData(GetHwnd(), index
, pNewItem
);
284 pNewItem
->SetFont(GetFont());
288 SetHorizontalExtent(item
);
293 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
295 ShowWindow(GetHwnd(), SW_HIDE
);
297 ListBox_ResetContent(GetHwnd());
299 m_noItems
= choices
.GetCount();
301 for (i
= 0; i
< m_noItems
; i
++)
303 ListBox_AddString(GetHwnd(), choices
[i
]);
306 #if wxUSE_OWNER_DRAWN
307 wxASSERT_MSG(clientData
[i
] == NULL
,
308 wxT("Can't use client data with owner-drawn listboxes"));
309 #else // !wxUSE_OWNER_DRAWN
310 ListBox_SetItemData(GetHwnd(), i
, clientData
[i
]);
311 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
315 #if wxUSE_OWNER_DRAWN
316 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
317 // first delete old items
318 size_t ui
= m_aItems
.Count();
319 while ( ui
-- != 0 ) {
324 // then create new ones
325 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
326 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
327 pNewItem
->SetName(choices
[ui
]);
328 m_aItems
.Add(pNewItem
);
329 ListBox_SetItemData(GetHwnd(), ui
, pNewItem
);
332 #endif // wxUSE_OWNER_DRAWN
334 SetHorizontalExtent();
336 ShowWindow(GetHwnd(), SW_SHOW
);
339 int wxListBox::FindString(const wxString
& s
) const
341 int pos
= ListBox_FindStringExact(GetHwnd(), (WPARAM
)-1, s
);
348 void wxListBox::Clear()
352 ListBox_ResetContent(GetHwnd());
355 SetHorizontalExtent();
358 void wxListBox::Free()
360 #if wxUSE_OWNER_DRAWN
361 if ( m_windowStyle
& wxLB_OWNERDRAW
)
363 size_t uiCount
= m_aItems
.Count();
364 while ( uiCount
-- != 0 ) {
365 delete m_aItems
[uiCount
];
371 #endif // wxUSE_OWNER_DRAWN
372 if ( HasClientObjectData() )
374 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
376 delete GetClientObject(n
);
381 void wxListBox::SetSelection(int N
, bool select
)
383 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
384 wxT("invalid index in wxListBox::SetSelection") );
386 if ( HasMultipleSelection() )
388 SendMessage(GetHwnd(), LB_SETSEL
, select
, N
);
392 SendMessage(GetHwnd(), LB_SETCURSEL
, select
? N
: -1, 0);
396 bool wxListBox::IsSelected(int N
) const
398 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
399 wxT("invalid index in wxListBox::Selected") );
401 return SendMessage(GetHwnd(), LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
404 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
406 return (wxClientData
*)DoGetItemClientData(n
);
409 void *wxListBox::DoGetItemClientData(int n
) const
411 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
412 wxT("invalid index in wxListBox::GetClientData") );
414 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA
, n
, 0);
417 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
419 DoSetItemClientData(n
, clientData
);
422 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
424 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
425 wxT("invalid index in wxListBox::SetClientData") );
427 #if wxUSE_OWNER_DRAWN
428 if ( m_windowStyle
& wxLB_OWNERDRAW
)
430 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
431 // in OnMeasure/OnDraw.
432 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
434 #endif // wxUSE_OWNER_DRAWN
436 if ( ListBox_SetItemData(GetHwnd(), n
, clientData
) == LB_ERR
)
437 wxLogDebug(wxT("LB_SETITEMDATA failed"));
440 bool wxListBox::HasMultipleSelection() const
442 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
445 // Return number of selections and an array of selected integers
446 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
450 if ( HasMultipleSelection() )
452 int no_sel
= ListBox_GetSelCount(GetHwnd());
454 int *selections
= new int[no_sel
];
455 int rc
= ListBox_GetSelItems(GetHwnd(), no_sel
, selections
);
457 wxCHECK_MSG(rc
!= LB_ERR
, -1, wxT("ListBox_GetSelItems failed"));
459 aSelections
.Alloc(no_sel
);
460 for ( int n
= 0; n
< no_sel
; n
++ )
461 aSelections
.Add(selections
[n
]);
463 delete [] selections
;
468 else // single-selection listbox
470 aSelections
.Add(ListBox_GetCurSel(GetHwnd()));
476 // Get single selection, for single choice list items
477 int wxListBox::GetSelection() const
479 wxCHECK_MSG( !HasMultipleSelection(),
481 wxT("GetSelection() can't be used with multiple-selection "
482 "listboxes, use GetSelections() instead.") );
484 return ListBox_GetCurSel(GetHwnd());
487 // Find string for position
488 wxString
wxListBox::GetString(int N
) const
490 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
491 wxT("invalid index in wxListBox::GetClientData") );
493 int len
= ListBox_GetTextLen(GetHwnd(), N
);
495 // +1 for terminating NUL
497 ListBox_GetText(GetHwnd(), N
, result
.GetWriteBuf(len
+ 1));
498 result
.UngetWriteBuf();
504 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
506 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
507 wxT("invalid index in wxListBox::InsertItems") );
509 int nItems
= items
.GetCount();
510 for ( int i
= 0; i
< nItems
; i
++ )
511 ListBox_InsertString(GetHwnd(), i
+ pos
, items
[i
]);
514 SetHorizontalExtent();
517 void wxListBox::SetString(int N
, const wxString
& s
)
519 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
520 wxT("invalid index in wxListBox::SetString") );
522 // remember the state of the item
523 bool wasSelected
= IsSelected(N
);
525 void *oldData
= NULL
;
526 wxClientData
*oldObjData
= NULL
;
527 if ( m_clientDataItemsType
== ClientData_Void
)
528 oldData
= GetClientData(N
);
529 else if ( m_clientDataItemsType
== ClientData_Object
)
530 oldObjData
= GetClientObject(N
);
532 // delete and recreate it
533 SendMessage(GetHwnd(), LB_DELETESTRING
, N
, 0);
536 if ( N
== m_noItems
- 1 )
539 ListBox_InsertString(GetHwnd(), newN
, s
);
541 // restore the client data
543 SetClientData(N
, oldData
);
544 else if ( oldObjData
)
545 SetClientObject(N
, oldObjData
);
547 // we may have lost the selection
551 #if wxUSE_OWNER_DRAWN
552 if ( m_windowStyle
& wxLB_OWNERDRAW
)
553 // update item's text
554 m_aItems
[N
]->SetName(s
);
555 #endif //USE_OWNER_DRAWN
558 int wxListBox::GetCount() const
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 // Windows-specific code to set the horizontal extent of the listbox, if
568 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
569 // Otherwise, all strings are used.
570 void wxListBox::SetHorizontalExtent(const wxString
& s
)
572 // Only necessary if we want a horizontal scrollbar
573 if (!(m_windowStyle
& wxHSCROLL
))
575 TEXTMETRIC lpTextMetric
;
579 int existingExtent
= (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT
, 0, 0L);
580 HDC dc
= GetWindowDC(GetHwnd());
582 if (GetFont().Ok() && GetFont().GetResourceHandle())
583 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
585 GetTextMetrics(dc
, &lpTextMetric
);
587 ::GetTextExtentPoint(dc
, (LPTSTR
) (const wxChar
*)s
, s
.Length(), &extentXY
);
588 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
591 ::SelectObject(dc
, oldFont
);
593 ReleaseDC(GetHwnd(), dc
);
594 if (extentX
> existingExtent
)
595 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
599 int largestExtent
= 0;
600 HDC dc
= GetWindowDC(GetHwnd());
602 if (GetFont().Ok() && GetFont().GetResourceHandle())
603 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
605 GetTextMetrics(dc
, &lpTextMetric
);
607 for (i
= 0; i
< m_noItems
; i
++)
609 int len
= (int)SendMessage(GetHwnd(), LB_GETTEXT
, i
, (LONG
)wxBuffer
);
612 ::GetTextExtentPoint(dc
, (LPTSTR
)wxBuffer
, len
, &extentXY
);
613 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
614 if (extentX
> largestExtent
)
615 largestExtent
= extentX
;
618 ::SelectObject(dc
, oldFont
);
620 ReleaseDC(GetHwnd(), dc
);
621 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
625 wxSize
wxListBox::DoGetBestSize() const
627 // find the widest string
630 for ( int i
= 0; i
< m_noItems
; i
++ )
632 wxString
str(GetString(i
));
633 GetTextExtent(str
, &wLine
, NULL
);
634 if ( wLine
> wListbox
)
638 // give it some reasonable default value if there are no strings in the
643 // the listbox should be slightly larger than the widest string
645 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
649 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
651 return wxSize(wListbox
, hListbox
);
654 // ----------------------------------------------------------------------------
656 // ----------------------------------------------------------------------------
658 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
660 if ( param
== LBN_SELCHANGE
)
662 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
663 event
.SetEventObject( this );
665 wxArrayInt aSelections
;
666 int n
, count
= GetSelections(aSelections
);
670 if ( HasClientObjectData() )
671 event
.SetClientObject( GetClientObject(n
) );
672 else if ( HasClientUntypedData() )
673 event
.SetClientData( GetClientData(n
) );
674 event
.SetString( GetString(n
) );
681 event
.m_commandInt
= n
;
683 return GetEventHandler()->ProcessEvent(event
);
685 else if ( param
== LBN_DBLCLK
)
687 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
688 event
.SetEventObject( this );
690 return GetEventHandler()->ProcessEvent(event
);
697 // ----------------------------------------------------------------------------
698 // wxCheckListBox support
699 // ----------------------------------------------------------------------------
701 #if wxUSE_OWNER_DRAWN
706 // space beneath/above each row in pixels
707 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
708 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
710 // the height is the same for all items
711 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
713 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
714 // message is not yet sent when we get here!
715 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
717 // only owner-drawn control should receive this message
718 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
720 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
722 HDC hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0);
725 dc
.SetHDC((WXHDC
)hdc
);
726 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
728 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
729 pStruct
->itemWidth
= dc
.GetCharWidth();
738 // forward the message to the appropriate item
739 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
741 // only owner-drawn control should receive this message
742 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
744 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
746 long data
= ListBox_GetItemData(GetHwnd(), pStruct
->itemID
);
748 wxCHECK( data
&& (data
!= LB_ERR
), FALSE
);
750 wxListBoxItem
*pItem
= (wxListBoxItem
*)data
;
753 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
754 wxRect
rect(wxPoint(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
),
755 wxPoint(pStruct
->rcItem
.right
, pStruct
->rcItem
.bottom
));
757 return pItem
->OnDrawItem(dc
, rect
,
758 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
759 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);