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
[],
84 # if defined(__VISAGECPP__)
85 const wxValidator
* validator
,
87 const wxValidator
& validator
,
98 SetValidator(validator
);
102 parent
->AddChild(this);
104 wxSystemSettings settings
;
105 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
106 SetForegroundColour(parent
->GetForegroundColour());
108 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
114 m_windowStyle
= style
;
118 DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |
119 LBS_NOTIFY | LBS_HASSTRINGS;
120 if (m_windowStyle & wxLB_MULTIPLE)
121 wstyle |= LBS_MULTIPLESEL;
122 else if (m_windowStyle & wxLB_EXTENDED)
123 wstyle |= LBS_EXTENDEDSEL;
125 if (m_windowStyle & wxLB_ALWAYS_SB)
126 wstyle |= LBS_DISABLENOSCROLL;
127 if (m_windowStyle & wxLB_HSCROLL)
128 wstyle |= WS_HSCROLL;
129 if (m_windowStyle & wxLB_SORT)
132 #if wxUSE_OWNER_DRAWN
133 if ( m_windowStyle & wxLB_OWNERDRAW ) {
134 // we don't support LBS_OWNERDRAWVARIABLE yet
135 wstyle |= LBS_OWNERDRAWFIXED;
139 // Without this style, you get unexpected heights, so e.g. constraint layout
140 // doesn't work properly
141 wstyle |= LBS_NOINTEGRALHEIGHT;
144 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
146 // Even with extended styles, need to combine with WS_BORDER for them to
148 if ( want3D || wxStyleHasBorder(m_windowStyle) )
153 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
156 (HWND)parent->GetHWND(), (HMENU)m_windowId,
157 wxGetInstance(), NULL);
159 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
161 // Subclass again to catch messages
165 for (ui = 0; ui < (size_t)n; ui++) {
169 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
170 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
172 SetFont(parent
->GetFont());
174 SetSize(x
, y
, width
, height
);
181 wxListBox::~wxListBox()
183 #if wxUSE_OWNER_DRAWN
184 size_t uiCount
= m_aItems
.Count();
185 while ( uiCount
-- != 0 ) {
186 delete m_aItems
[uiCount
];
188 #endif // wxUSE_OWNER_DRAWN
191 void wxListBox::SetupColours()
193 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
194 SetForegroundColour(GetParent()->GetForegroundColour());
197 // ----------------------------------------------------------------------------
198 // implementation of wxListBoxBase methods
199 // ----------------------------------------------------------------------------
201 void wxListBox::DoSetFirstItem(int N
)
203 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
204 wxT("invalid index in wxListBox::SetFirstItem") );
206 // SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
209 void wxListBox::Delete(int N
)
211 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
212 wxT("invalid index in wxListBox::Delete") );
214 #if wxUSE_OWNER_DRAWN
217 #else // !wxUSE_OWNER_DRAWN
218 if ( HasClientObjectData() )
220 delete GetClientObject(N
);
222 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
224 // SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
227 SetHorizontalExtent("");
230 int wxListBox::DoAppend(const wxString
& item
)
234 int index = ListBox_AddString(GetHwnd(), item);
237 #if wxUSE_OWNER_DRAWN
238 if ( m_windowStyle & wxLB_OWNERDRAW ) {
239 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
240 pNewItem->SetName(item);
241 m_aItems.Add(pNewItem);
242 ListBox_SetItemData(GetHwnd(), index, pNewItem);
246 SetHorizontalExtent(item);
253 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
257 ShowWindow(GetHwnd(), SW_HIDE);
259 ListBox_ResetContent(GetHwnd());
261 m_noItems = choices.GetCount();
263 for (i = 0; i < m_noItems; i++)
265 ListBox_AddString(GetHwnd(), choices[i]);
268 #if wxUSE_OWNER_DRAWN
269 wxASSERT_MSG(clientData[ui] == NULL,
270 wxT("Can't use client data with owner-drawn listboxes"));
271 #else // !wxUSE_OWNER_DRAWN
272 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
273 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
277 #if wxUSE_OWNER_DRAWN
278 if ( m_windowStyle & wxLB_OWNERDRAW ) {
279 // first delete old items
280 size_t ui = m_aItems.Count();
281 while ( ui-- != 0 ) {
286 // then create new ones
287 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
288 wxOwnerDrawn *pNewItem = CreateItem(ui);
289 pNewItem->SetName(choices[ui]);
290 m_aItems.Add(pNewItem);
291 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
294 #endif // wxUSE_OWNER_DRAWN
296 SetHorizontalExtent();
298 ShowWindow(GetHwnd(), SW_SHOW);
302 int wxListBox::FindString(const wxString
& s
) const
306 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
315 void wxListBox::Clear()
317 #if wxUSE_OWNER_DRAWN
318 size_t uiCount
= m_aItems
.Count();
319 while ( uiCount
-- != 0 ) {
320 delete m_aItems
[uiCount
];
324 #else // !wxUSE_OWNER_DRAWN
325 if ( HasClientObjectData() )
327 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
329 delete GetClientObject(n
);
332 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
336 ListBox_ResetContent(GetHwnd());
339 SetHorizontalExtent();
343 void wxListBox::SetSelection(int N
, bool select
)
345 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
346 wxT("invalid index in wxListBox::SetSelection") );
350 if ( HasMultipleSelection() )
352 SendMessage(GetHwnd(), LB_SETSEL, select, N);
356 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
361 bool wxListBox::IsSelected(int N
) const
363 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
364 wxT("invalid index in wxListBox::Selected") );
366 // return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
370 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
372 return (wxClientData
*)DoGetItemClientData(n
);
375 void *wxListBox::DoGetItemClientData(int n
) const
377 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
378 wxT("invalid index in wxListBox::GetClientData") );
380 // return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
384 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
386 DoSetItemClientData(n
, clientData
);
389 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
391 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
392 wxT("invalid index in wxListBox::SetClientData") );
394 #if wxUSE_OWNER_DRAWN
395 if ( m_windowStyle
& wxLB_OWNERDRAW
)
397 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
398 // in OnMeasure/OnDraw.
399 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
401 #endif // wxUSE_OWNER_DRAWN
405 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
406 wxLogDebug(wxT("LB_SETITEMDATA failed"));
410 bool wxListBox::HasMultipleSelection() const
412 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
415 // Return number of selections and an array of selected integers
416 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
422 if ( HasMultipleSelection() )
424 int no_sel = ListBox_GetSelCount(GetHwnd());
426 int *selections = new int[no_sel];
427 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
429 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
431 aSelections.Alloc(no_sel);
432 for ( int n = 0; n < no_sel; n++ )
433 aSelections.Add(selections[n]);
435 delete [] selections;
440 else // single-selection listbox
442 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
450 // Get single selection, for single choice list items
451 int wxListBox::GetSelection() const
453 wxCHECK_MSG( !HasMultipleSelection(),
455 wxT("GetSelection() can't be used with multiple-selection "
456 "listboxes, use GetSelections() instead.") );
458 // return ListBox_GetCurSel(GetHwnd());
462 // Find string for position
463 wxString
wxListBox::GetString(int N
) const
465 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
466 wxT("invalid index in wxListBox::GetClientData") );
470 int len = ListBox_GetTextLen(GetHwnd(), N);
472 // +1 for terminating NUL
474 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
475 result.UngetWriteBuf();
479 return((wxString
)"");
483 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
485 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
486 wxT("invalid index in wxListBox::InsertItems") );
490 int nItems = items.GetCount();
491 for ( int i = 0; i < nItems; i++ )
492 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
495 SetHorizontalExtent();
499 void wxListBox::SetString(int N
, const wxString
& s
)
501 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
502 wxT("invalid index in wxListBox::SetString") );
504 // remember the state of the item
505 bool wasSelected
= IsSelected(N
);
507 void *oldData
= NULL
;
508 wxClientData
*oldObjData
= NULL
;
509 if ( m_clientDataItemsType
== ClientData_Void
)
510 oldData
= GetClientData(N
);
511 else if ( m_clientDataItemsType
== ClientData_Object
)
512 oldObjData
= GetClientObject(N
);
516 // delete and recreate it
517 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
520 if ( N == m_noItems - 1 )
523 ListBox_InsertString(GetHwnd(), newN, s);
525 // restore the client data
527 SetClientData(N, oldData);
528 else if ( oldObjData )
529 SetClientObject(N, oldObjData);
531 // we may have lost the selection
535 #if wxUSE_OWNER_DRAWN
536 if ( m_windowStyle & wxLB_OWNERDRAW )
537 // update item's text
538 m_aItems[N]->SetName(s);
539 #endif //USE_OWNER_DRAWN
543 int wxListBox::GetCount() const
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 // Windows-specific code to set the horizontal extent of the listbox, if
553 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
554 // Otherwise, all strings are used.
555 void wxListBox::SetHorizontalExtent(const wxString
& s
)
559 // Only necessary if we want a horizontal scrollbar
560 if (!(m_windowStyle & wxHSCROLL))
562 TEXTMETRIC lpTextMetric;
566 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
567 HDC dc = GetWindowDC(GetHwnd());
569 if (GetFont().Ok() && GetFont().GetResourceHandle())
570 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
572 GetTextMetrics(dc, &lpTextMetric);
574 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
575 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
578 ::SelectObject(dc, oldFont);
580 ReleaseDC(GetHwnd(), dc);
581 if (extentX > existingExtent)
582 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
586 int largestExtent = 0;
587 HDC dc = GetWindowDC(GetHwnd());
589 if (GetFont().Ok() && GetFont().GetResourceHandle())
590 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
592 GetTextMetrics(dc, &lpTextMetric);
594 for (i = 0; i < m_noItems; i++)
596 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
599 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
600 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
601 if (extentX > largestExtent)
602 largestExtent = extentX;
605 ::SelectObject(dc, oldFont);
607 ReleaseDC(GetHwnd(), dc);
608 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
613 wxSize
wxListBox::DoGetBestSize()
615 // find the widest string
618 for ( int i
= 0; i
< m_noItems
; i
++ )
620 wxString
str(GetString(i
));
621 GetTextExtent(str
, &wLine
, NULL
);
622 if ( wLine
> wListbox
)
626 // give it some reasonable default value if there are no strings in the
631 // the listbox should be slightly larger than the widest string
633 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
637 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
639 return wxSize(wListbox
, hListbox
);
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
646 bool wxListBox::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
649 if (param == LBN_SELCANCEL)
651 event.extraLong = FALSE;
656 if (param == LBN_SELCHANGE)
658 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
659 wxArrayInt aSelections;
660 int count = GetSelections(aSelections);
663 event.m_commandInt = aSelections[0];
664 event.m_clientData = GetClientData(event.m_commandInt);
665 wxString str(GetString(event.m_commandInt));
668 event.m_commandString = str;
673 event.m_commandInt = -1;
674 event.m_commandString.Empty();
677 event.SetEventObject( this );
678 ProcessCommand(event);
681 else if (param == LBN_DBLCLK)
683 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
684 event.SetEventObject( this );
685 GetEventHandler()->ProcessEvent(event);
692 WXHBRUSH
wxListBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
693 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
697 if (GetParent()->GetTransparentBackground())
698 SetBkMode((HDC) pDC, TRANSPARENT);
700 SetBkMode((HDC) pDC, OPAQUE);
702 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
703 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
705 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
707 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
708 // has a zero usage count.
709 backgroundBrush->RealizeResource();
710 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
715 // ----------------------------------------------------------------------------
716 // wxCheckListBox support
717 // ----------------------------------------------------------------------------
719 #if wxUSE_OWNER_DRAWN
724 // space beneath/above each row in pixels
725 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
726 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
728 // the height is the same for all items
729 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
731 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
732 // message is not yet sent when we get here!
733 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
737 // only owner-drawn control should receive this message
738 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
740 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
743 dc.SetHDC((WXHDC)CreateIC(wxT("DISPLAY"), NULL, NULL, 0));
744 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
746 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
747 pStruct->itemWidth = dc.GetCharWidth();
754 // forward the message to the appropriate item
755 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
759 // only owner-drawn control should receive this message
760 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
762 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
764 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
766 wxCHECK( data && (data != LB_ERR), FALSE );
768 wxListBoxItem *pItem = (wxListBoxItem *)data;
771 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
772 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
773 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
775 return pItem->OnDrawItem(dc, rect,
776 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
777 (wxOwnerDrawn::wxODStatus)pStruct->itemState);