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 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
39 // ============================================================================
40 // list box item declaration and implementation
41 // ============================================================================
45 class wxListBoxItem
: public wxOwnerDrawn
48 wxListBoxItem(const wxString
& str
= "");
51 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
53 // no bitmaps/checkmarks
57 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
59 return new wxListBoxItem();
62 #endif //USE_OWNER_DRAWN
64 // ============================================================================
65 // list box control implementation
66 // ============================================================================
69 wxListBox::wxListBox()
75 bool wxListBox::Create(wxWindow
*parent
,
79 int n
, const wxString choices
[],
82 const wxValidator
& validator
,
92 SetValidator(validator
);
96 parent
->AddChild(this);
98 wxSystemSettings settings
;
99 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
100 SetForegroundColour(parent
->GetForegroundColour());
102 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
108 m_windowStyle
= style
;
112 DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |
113 LBS_NOTIFY | LBS_HASSTRINGS;
114 if (m_windowStyle & wxLB_MULTIPLE)
115 wstyle |= LBS_MULTIPLESEL;
116 else if (m_windowStyle & wxLB_EXTENDED)
117 wstyle |= LBS_EXTENDEDSEL;
119 if (m_windowStyle & wxLB_ALWAYS_SB)
120 wstyle |= LBS_DISABLENOSCROLL;
121 if (m_windowStyle & wxLB_HSCROLL)
122 wstyle |= WS_HSCROLL;
123 if (m_windowStyle & wxLB_SORT)
126 #if wxUSE_OWNER_DRAWN
127 if ( m_windowStyle & wxLB_OWNERDRAW ) {
128 // we don't support LBS_OWNERDRAWVARIABLE yet
129 wstyle |= LBS_OWNERDRAWFIXED;
133 // Without this style, you get unexpected heights, so e.g. constraint layout
134 // doesn't work properly
135 wstyle |= LBS_NOINTEGRALHEIGHT;
138 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
140 // Even with extended styles, need to combine with WS_BORDER for them to
142 if ( want3D || wxStyleHasBorder(m_windowStyle) )
147 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
150 (HWND)parent->GetHWND(), (HMENU)m_windowId,
151 wxGetInstance(), NULL);
153 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
155 // Subclass again to catch messages
159 for (ui = 0; ui < (size_t)n; ui++) {
163 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
164 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
166 SetFont(parent
->GetFont());
168 SetSize(x
, y
, width
, height
);
175 wxListBox::~wxListBox()
177 #if wxUSE_OWNER_DRAWN
178 size_t uiCount
= m_aItems
.Count();
179 while ( uiCount
-- != 0 ) {
180 delete m_aItems
[uiCount
];
182 #endif // wxUSE_OWNER_DRAWN
185 void wxListBox::SetupColours()
187 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
188 SetForegroundColour(GetParent()->GetForegroundColour());
191 // ----------------------------------------------------------------------------
192 // implementation of wxListBoxBase methods
193 // ----------------------------------------------------------------------------
195 void wxListBox::DoSetFirstItem(int N
)
197 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
198 wxT("invalid index in wxListBox::SetFirstItem") );
200 // SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
203 void wxListBox::Delete(int N
)
205 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
206 wxT("invalid index in wxListBox::Delete") );
208 #if wxUSE_OWNER_DRAWN
211 #else // !wxUSE_OWNER_DRAWN
212 if ( HasClientObjectData() )
214 delete GetClientObject(N
);
216 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
218 // SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
221 SetHorizontalExtent("");
224 int wxListBox::DoAppend(const wxString
& item
)
228 int index = ListBox_AddString(GetHwnd(), item);
231 #if wxUSE_OWNER_DRAWN
232 if ( m_windowStyle & wxLB_OWNERDRAW ) {
233 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
234 pNewItem->SetName(item);
235 m_aItems.Add(pNewItem);
236 ListBox_SetItemData(GetHwnd(), index, pNewItem);
240 SetHorizontalExtent(item);
247 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
251 ShowWindow(GetHwnd(), SW_HIDE);
253 ListBox_ResetContent(GetHwnd());
255 m_noItems = choices.GetCount();
257 for (i = 0; i < m_noItems; i++)
259 ListBox_AddString(GetHwnd(), choices[i]);
262 #if wxUSE_OWNER_DRAWN
263 wxASSERT_MSG(clientData[ui] == NULL,
264 wxT("Can't use client data with owner-drawn listboxes"));
265 #else // !wxUSE_OWNER_DRAWN
266 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
267 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
271 #if wxUSE_OWNER_DRAWN
272 if ( m_windowStyle & wxLB_OWNERDRAW ) {
273 // first delete old items
274 size_t ui = m_aItems.Count();
275 while ( ui-- != 0 ) {
280 // then create new ones
281 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
282 wxOwnerDrawn *pNewItem = CreateItem(ui);
283 pNewItem->SetName(choices[ui]);
284 m_aItems.Add(pNewItem);
285 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
288 #endif // wxUSE_OWNER_DRAWN
290 SetHorizontalExtent();
292 ShowWindow(GetHwnd(), SW_SHOW);
296 int wxListBox::FindString(const wxString
& s
) const
300 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
309 void wxListBox::Clear()
311 #if wxUSE_OWNER_DRAWN
312 size_t uiCount
= m_aItems
.Count();
313 while ( uiCount
-- != 0 ) {
314 delete m_aItems
[uiCount
];
318 #else // !wxUSE_OWNER_DRAWN
319 if ( HasClientObjectData() )
321 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
323 delete GetClientObject(n
);
326 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
330 ListBox_ResetContent(GetHwnd());
333 SetHorizontalExtent();
337 void wxListBox::SetSelection(int N
, bool select
)
339 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
340 wxT("invalid index in wxListBox::SetSelection") );
344 if ( HasMultipleSelection() )
346 SendMessage(GetHwnd(), LB_SETSEL, select, N);
350 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
355 bool wxListBox::IsSelected(int N
) const
357 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
358 wxT("invalid index in wxListBox::Selected") );
360 // return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
364 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
366 return (wxClientData
*)DoGetItemClientData(n
);
369 void *wxListBox::DoGetItemClientData(int n
) const
371 wxCHECK_MSG( n
>= 0 && n
< m_noItems
, NULL
,
372 wxT("invalid index in wxListBox::GetClientData") );
374 // return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
378 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
380 DoSetItemClientData(n
, clientData
);
383 void wxListBox::DoSetItemClientData(int n
, void *clientData
)
385 wxCHECK_RET( n
>= 0 && n
< m_noItems
,
386 wxT("invalid index in wxListBox::SetClientData") );
388 #if wxUSE_OWNER_DRAWN
389 if ( m_windowStyle
& wxLB_OWNERDRAW
)
391 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
392 // in OnMeasure/OnDraw.
393 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
395 #endif // wxUSE_OWNER_DRAWN
399 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
400 wxLogDebug(wxT("LB_SETITEMDATA failed"));
404 bool wxListBox::HasMultipleSelection() const
406 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
409 // Return number of selections and an array of selected integers
410 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
416 if ( HasMultipleSelection() )
418 int no_sel = ListBox_GetSelCount(GetHwnd());
420 int *selections = new int[no_sel];
421 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
423 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
425 aSelections.Alloc(no_sel);
426 for ( int n = 0; n < no_sel; n++ )
427 aSelections.Add(selections[n]);
429 delete [] selections;
434 else // single-selection listbox
436 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
444 // Get single selection, for single choice list items
445 int wxListBox::GetSelection() const
447 wxCHECK_MSG( !HasMultipleSelection(),
449 wxT("GetSelection() can't be used with multiple-selection "
450 "listboxes, use GetSelections() instead.") );
452 // return ListBox_GetCurSel(GetHwnd());
456 // Find string for position
457 wxString
wxListBox::GetString(int N
) const
459 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, "",
460 wxT("invalid index in wxListBox::GetClientData") );
464 int len = ListBox_GetTextLen(GetHwnd(), N);
466 // +1 for terminating NUL
468 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
469 result.UngetWriteBuf();
473 return((wxString
)"");
477 wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
479 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
480 wxT("invalid index in wxListBox::InsertItems") );
484 int nItems = items.GetCount();
485 for ( int i = 0; i < nItems; i++ )
486 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
489 SetHorizontalExtent();
493 void wxListBox::SetString(int N
, const wxString
& s
)
495 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
496 wxT("invalid index in wxListBox::SetString") );
498 // remember the state of the item
499 bool wasSelected
= IsSelected(N
);
501 void *oldData
= NULL
;
502 wxClientData
*oldObjData
= NULL
;
503 if ( m_clientDataItemsType
== ClientData_Void
)
504 oldData
= GetClientData(N
);
505 else if ( m_clientDataItemsType
== ClientData_Object
)
506 oldObjData
= GetClientObject(N
);
510 // delete and recreate it
511 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
514 if ( N == m_noItems - 1 )
517 ListBox_InsertString(GetHwnd(), newN, s);
519 // restore the client data
521 SetClientData(N, oldData);
522 else if ( oldObjData )
523 SetClientObject(N, oldObjData);
525 // we may have lost the selection
529 #if wxUSE_OWNER_DRAWN
530 if ( m_windowStyle & wxLB_OWNERDRAW )
531 // update item's text
532 m_aItems[N]->SetName(s);
533 #endif //USE_OWNER_DRAWN
537 int wxListBox::GetCount() const
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 // Windows-specific code to set the horizontal extent of the listbox, if
547 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
548 // Otherwise, all strings are used.
549 void wxListBox::SetHorizontalExtent(const wxString
& s
)
553 // Only necessary if we want a horizontal scrollbar
554 if (!(m_windowStyle & wxHSCROLL))
556 TEXTMETRIC lpTextMetric;
560 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
561 HDC dc = GetWindowDC(GetHwnd());
563 if (GetFont().Ok() && GetFont().GetResourceHandle())
564 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
566 GetTextMetrics(dc, &lpTextMetric);
568 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
569 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
572 ::SelectObject(dc, oldFont);
574 ReleaseDC(GetHwnd(), dc);
575 if (extentX > existingExtent)
576 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
580 int largestExtent = 0;
581 HDC dc = GetWindowDC(GetHwnd());
583 if (GetFont().Ok() && GetFont().GetResourceHandle())
584 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
586 GetTextMetrics(dc, &lpTextMetric);
588 for (i = 0; i < m_noItems; i++)
590 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
593 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
594 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
595 if (extentX > largestExtent)
596 largestExtent = extentX;
599 ::SelectObject(dc, oldFont);
601 ReleaseDC(GetHwnd(), dc);
602 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
607 wxSize
wxListBox::DoGetBestSize() const
609 // find the widest string
612 for ( int i
= 0; i
< m_noItems
; i
++ )
614 wxString
str(GetString(i
));
615 GetTextExtent(str
, &wLine
, NULL
);
616 if ( wLine
> wListbox
)
620 // give it some reasonable default value if there are no strings in the
625 // the listbox should be slightly larger than the widest string
627 wxGetCharSize(GetHWND(), &cx
, &cy
, (wxFont
*)&GetFont());
631 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMax(m_noItems
, 7));
633 return wxSize(wListbox
, hListbox
);
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 bool wxListBox::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
643 if (param == LBN_SELCANCEL)
645 event.extraLong = FALSE;
650 if (param == LBN_SELCHANGE)
652 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
653 wxArrayInt aSelections;
654 int count = GetSelections(aSelections);
657 event.m_commandInt = aSelections[0];
658 event.m_clientData = GetClientData(event.m_commandInt);
659 wxString str(GetString(event.m_commandInt));
662 event.m_commandString = str;
667 event.m_commandInt = -1;
668 event.m_commandString.Empty();
671 event.SetEventObject( this );
672 ProcessCommand(event);
675 else if (param == LBN_DBLCLK)
677 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
678 event.SetEventObject( this );
679 GetEventHandler()->ProcessEvent(event);
686 WXHBRUSH
wxListBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
687 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
691 if (GetParent()->GetTransparentBackground())
692 SetBkMode((HDC) pDC, TRANSPARENT);
694 SetBkMode((HDC) pDC, OPAQUE);
696 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
697 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
699 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
701 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
702 // has a zero usage count.
703 backgroundBrush->RealizeResource();
704 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
709 // ----------------------------------------------------------------------------
710 // wxCheckListBox support
711 // ----------------------------------------------------------------------------
713 #if wxUSE_OWNER_DRAWN
718 // space beneath/above each row in pixels
719 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
720 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
722 // the height is the same for all items
723 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
725 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
726 // message is not yet sent when we get here!
727 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
731 // only owner-drawn control should receive this message
732 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
734 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
737 dc.SetHDC((WXHDC)CreateIC(wxT("DISPLAY"), NULL, NULL, 0));
738 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
740 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
741 pStruct->itemWidth = dc.GetCharWidth();
748 // forward the message to the appropriate item
749 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
753 // only owner-drawn control should receive this message
754 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
756 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
758 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
760 wxCHECK( data && (data != LB_ERR), FALSE );
762 wxListBoxItem *pItem = (wxListBoxItem *)data;
765 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
766 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
767 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
769 return pItem->OnDrawItem(dc, rect,
770 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
771 (wxOwnerDrawn::wxODStatus)pStruct->itemState);