1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/window.h"
16 #include "wx/os2/private.h"
19 #include "wx/listbox.h"
20 #include "wx/settings.h"
30 #include "wx/dynarray.h"
34 #include "wx/ownerdrw.h"
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
41 // ============================================================================
42 // list box item declaration and implementation
43 // ============================================================================
47 class wxListBoxItem
: public wxOwnerDrawn
50 wxListBoxItem(const wxString
& str
= "");
53 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
55 // no bitmaps/checkmarks
59 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
61 return new wxListBoxItem();
64 #endif //USE_OWNER_DRAWN
66 // ============================================================================
67 // list box control implementation
68 // ============================================================================
71 wxListBox::wxListBox()
77 bool wxListBox::Create(wxWindow
*parent
,
81 int n
, const wxString choices
[],
83 const wxValidator
& validator
,
91 SetValidator(validator
);
94 parent
->AddChild(this);
96 wxSystemSettings settings
;
97 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
98 SetForegroundColour(parent
->GetForegroundColour());
100 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
106 m_windowStyle
= style
;
110 DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |
111 LBS_NOTIFY | LBS_HASSTRINGS;
112 if (m_windowStyle & wxLB_MULTIPLE)
113 wstyle |= LBS_MULTIPLESEL;
114 else if (m_windowStyle & wxLB_EXTENDED)
115 wstyle |= LBS_EXTENDEDSEL;
117 if (m_windowStyle & wxLB_ALWAYS_SB)
118 wstyle |= LBS_DISABLENOSCROLL;
119 if (m_windowStyle & wxLB_HSCROLL)
120 wstyle |= WS_HSCROLL;
121 if (m_windowStyle & wxLB_SORT)
124 #if wxUSE_OWNER_DRAWN
125 if ( m_windowStyle & wxLB_OWNERDRAW ) {
126 // we don't support LBS_OWNERDRAWVARIABLE yet
127 wstyle |= LBS_OWNERDRAWFIXED;
131 // Without this style, you get unexpected heights, so e.g. constraint layout
132 // doesn't work properly
133 wstyle |= LBS_NOINTEGRALHEIGHT;
136 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
138 // Even with extended styles, need to combine with WS_BORDER for them to
140 if ( want3D || wxStyleHasBorder(m_windowStyle) )
145 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
148 (HWND)parent->GetHWND(), (HMENU)m_windowId,
149 wxGetInstance(), NULL);
151 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
153 // Subclass again to catch messages
157 for (ui = 0; ui < (size_t)n; ui++) {
161 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
162 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
164 SetFont(parent
->GetFont());
166 SetSize(x
, y
, width
, height
);
173 wxListBox::~wxListBox()
175 #if wxUSE_OWNER_DRAWN
176 size_t uiCount
= m_aItems
.Count();
177 while ( uiCount
-- != 0 ) {
178 delete m_aItems
[uiCount
];
180 #endif // wxUSE_OWNER_DRAWN
183 void wxListBox::SetupColours()
185 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
186 SetForegroundColour(GetParent()->GetForegroundColour());
189 // ----------------------------------------------------------------------------
190 // implementation of wxListBoxBase methods
191 // ----------------------------------------------------------------------------
193 void wxListBox::DoSetFirstItem(int N
)
195 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
196 wxT("invalid index in wxListBox::SetFirstItem") );
198 // SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
201 void wxListBox::Delete(int N
)
203 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
204 wxT("invalid index in wxListBox::Delete") );
206 #if wxUSE_OWNER_DRAWN
209 #else // !wxUSE_OWNER_DRAWN
210 if ( HasClientObjectData() )
212 delete GetClientObject(N
);
214 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
216 // SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
219 SetHorizontalExtent("");
222 int wxListBox::DoAppend(const wxString
& item
)
226 int index = ListBox_AddString(GetHwnd(), item);
229 #if wxUSE_OWNER_DRAWN
230 if ( m_windowStyle & wxLB_OWNERDRAW ) {
231 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
232 pNewItem->SetName(item);
233 m_aItems.Add(pNewItem);
234 ListBox_SetItemData(GetHwnd(), index, pNewItem);
238 SetHorizontalExtent(item);
245 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
249 ShowWindow(GetHwnd(), SW_HIDE);
251 ListBox_ResetContent(GetHwnd());
253 m_noItems = choices.GetCount();
255 for (i = 0; i < m_noItems; i++)
257 ListBox_AddString(GetHwnd(), choices[i]);
260 #if wxUSE_OWNER_DRAWN
261 wxASSERT_MSG(clientData[ui] == NULL,
262 wxT("Can't use client data with owner-drawn listboxes"));
263 #else // !wxUSE_OWNER_DRAWN
264 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
265 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
269 #if wxUSE_OWNER_DRAWN
270 if ( m_windowStyle & wxLB_OWNERDRAW ) {
271 // first delete old items
272 size_t ui = m_aItems.Count();
273 while ( ui-- != 0 ) {
278 // then create new ones
279 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
280 wxOwnerDrawn *pNewItem = CreateItem(ui);
281 pNewItem->SetName(choices[ui]);
282 m_aItems.Add(pNewItem);
283 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
286 #endif // wxUSE_OWNER_DRAWN
288 SetHorizontalExtent();
290 ShowWindow(GetHwnd(), SW_SHOW);
294 int wxListBox::FindString(const wxString
& s
) const
298 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
307 void wxListBox::Clear()
309 #if wxUSE_OWNER_DRAWN
310 size_t uiCount
= m_aItems
.Count();
311 while ( uiCount
-- != 0 ) {
312 delete m_aItems
[uiCount
];
316 #else // !wxUSE_OWNER_DRAWN
317 if ( HasClientObjectData() )
319 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
321 delete GetClientObject(n
);
324 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
328 ListBox_ResetContent(GetHwnd());
331 SetHorizontalExtent();
335 void wxListBox::SetSelection(int N
, bool select
)
337 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
338 wxT("invalid index in wxListBox::SetSelection") );
342 if ( HasMultipleSelection() )
344 SendMessage(GetHwnd(), LB_SETSEL, select, N);
348 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
353 bool wxListBox::IsSelected(int N
) const
355 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
356 wxT("invalid index in wxListBox::Selected") );
358 // return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
362 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
364 return (wxClientData
*)DoGetItemClientData(n
);
367 void *wxListBox::DoGetItemClientData(int n
) const
369 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
370 wxT("invalid index in wxListBox::GetClientData") );
372 // return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
376 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
378 DoSetItemClientData(n
, clientData
);
381 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
383 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
384 wxT("invalid index in wxListBox::SetClientData") );
386 #if wxUSE_OWNER_DRAWN
387 if ( m_windowStyle
& wxLB_OWNERDRAW
)
389 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
390 // in OnMeasure/OnDraw.
391 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
393 #endif // wxUSE_OWNER_DRAWN
397 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
398 wxLogDebug(wxT("LB_SETITEMDATA failed"));
402 bool wxListBox::HasMultipleSelection() const
404 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
407 // Return number of selections and an array of selected integers
408 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
414 if ( HasMultipleSelection() )
416 int no_sel = ListBox_GetSelCount(GetHwnd());
418 int *selections = new int[no_sel];
419 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
421 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
423 aSelections.Alloc(no_sel);
424 for ( int n = 0; n < no_sel; n++ )
425 aSelections.Add(selections[n]);
427 delete [] selections;
432 else // single-selection listbox
434 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
442 // Get single selection, for single choice list items
443 int wxListBox::GetSelection() const
445 wxCHECK_MSG( !HasMultipleSelection(),
447 wxT("GetSelection() can't be used with multiple-selection "
448 "listboxes, use GetSelections() instead.") );
450 // return ListBox_GetCurSel(GetHwnd());
454 // Find string for position
455 wxString
wxListBox::GetString(int N
) const
457 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
458 wxT("invalid index in wxListBox::GetClientData") );
462 int len = ListBox_GetTextLen(GetHwnd(), N);
464 // +1 for terminating NUL
466 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
467 result.UngetWriteBuf();
471 return((wxString
)"");
475 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
477 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
478 wxT("invalid index in wxListBox::InsertItems") );
482 int nItems = items.GetCount();
483 for ( int i = 0; i < nItems; i++ )
484 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
487 SetHorizontalExtent();
491 void wxListBox::SetString(int N
, const wxString
& s
)
493 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
494 wxT("invalid index in wxListBox::SetString") );
496 // remember the state of the item
497 bool wasSelected
= IsSelected(N
);
499 void *oldData
= NULL
;
500 wxClientData
*oldObjData
= NULL
;
501 if ( m_clientDataItemsType
== ClientData_Void
)
502 oldData
= GetClientData(N
);
503 else if ( m_clientDataItemsType
== ClientData_Object
)
504 oldObjData
= GetClientObject(N
);
508 // delete and recreate it
509 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
512 if ( N == m_noItems - 1 )
515 ListBox_InsertString(GetHwnd(), newN, s);
517 // restore the client data
519 SetClientData(N, oldData);
520 else if ( oldObjData )
521 SetClientObject(N, oldObjData);
523 // we may have lost the selection
527 #if wxUSE_OWNER_DRAWN
528 if ( m_windowStyle & wxLB_OWNERDRAW )
529 // update item's text
530 m_aItems[N]->SetName(s);
531 #endif //USE_OWNER_DRAWN
535 int wxListBox::GetCount() const
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 // Windows-specific code to set the horizontal extent of the listbox, if
545 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
546 // Otherwise, all strings are used.
547 void wxListBox::SetHorizontalExtent(const wxString
& s
)
551 // Only necessary if we want a horizontal scrollbar
552 if (!(m_windowStyle & wxHSCROLL))
554 TEXTMETRIC lpTextMetric;
558 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
559 HDC dc = GetWindowDC(GetHwnd());
561 if (GetFont().Ok() && GetFont().GetResourceHandle())
562 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
564 GetTextMetrics(dc, &lpTextMetric);
566 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
567 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
570 ::SelectObject(dc, oldFont);
572 ReleaseDC(GetHwnd(), dc);
573 if (extentX > existingExtent)
574 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
578 int largestExtent = 0;
579 HDC dc = GetWindowDC(GetHwnd());
581 if (GetFont().Ok() && GetFont().GetResourceHandle())
582 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
584 GetTextMetrics(dc, &lpTextMetric);
586 for (i = 0; i < m_noItems; i++)
588 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
591 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
592 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
593 if (extentX > largestExtent)
594 largestExtent = extentX;
597 ::SelectObject(dc, oldFont);
599 ReleaseDC(GetHwnd(), dc);
600 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
605 wxSize
wxListBox::DoGetBestSize()
607 // find the widest string
610 for ( int i
= 0; i
< m_noItems
; i
++ )
612 wxString
str(GetString(i
));
613 GetTextExtent(str
, &wLine
, NULL
);
614 if ( wLine
> wListbox
)
618 // give it some reasonable default value if there are no strings in the
623 // the listbox should be slightly larger than the widest string
625 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
629 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
631 return wxSize(wListbox
, hListbox
);
634 // ----------------------------------------------------------------------------
636 // ----------------------------------------------------------------------------
638 bool wxListBox::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
641 if (param == LBN_SELCANCEL)
643 event.extraLong = FALSE;
648 if (param == LBN_SELCHANGE)
650 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
651 wxArrayInt aSelections;
652 int count = GetSelections(aSelections);
655 event.m_commandInt = aSelections[0];
656 event.m_clientData = GetClientData(event.m_commandInt);
657 wxString str(GetString(event.m_commandInt));
660 event.m_commandString = str;
665 event.m_commandInt = -1;
666 event.m_commandString.Empty();
669 event.SetEventObject( this );
670 ProcessCommand(event);
673 else if (param == LBN_DBLCLK)
675 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
676 event.SetEventObject( this );
677 GetEventHandler()->ProcessEvent(event);
684 WXHBRUSH
wxListBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
685 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
689 if (GetParent()->GetTransparentBackground())
690 SetBkMode((HDC) pDC, TRANSPARENT);
692 SetBkMode((HDC) pDC, OPAQUE);
694 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
695 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
697 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
699 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
700 // has a zero usage count.
701 backgroundBrush->RealizeResource();
702 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
707 // ----------------------------------------------------------------------------
708 // wxCheckListBox support
709 // ----------------------------------------------------------------------------
711 #if wxUSE_OWNER_DRAWN
716 // space beneath/above each row in pixels
717 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
718 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
720 // the height is the same for all items
721 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
723 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
724 // message is not yet sent when we get here!
725 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
729 // only owner-drawn control should receive this message
730 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
732 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
735 dc.SetHDC((WXHDC)CreateIC(wxT("DISPLAY"), NULL, NULL, 0));
736 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
738 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
739 pStruct->itemWidth = dc.GetCharWidth();
746 // forward the message to the appropriate item
747 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
751 // only owner-drawn control should receive this message
752 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
754 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
756 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
758 wxCHECK( data && (data != LB_ERR), FALSE );
760 wxListBoxItem *pItem = (wxListBoxItem *)data;
763 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
764 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
765 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
767 return pItem->OnDrawItem(dc, rect,
768 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
769 (wxOwnerDrawn::wxODStatus)pStruct->itemState);