X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/743b426605478eeb31232c30e804e70ff493b1f2..9914bfbb77f48baf869b63aff58feb2b145ff4d3:/src/msw/listbox.cpp diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 84634d3088..78d76fdfab 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -77,6 +77,7 @@ wxBEGIN_FLAGS( wxListBoxStyle ) wxFLAGS_MEMBER(wxLB_HSCROLL) wxFLAGS_MEMBER(wxLB_ALWAYS_SB) wxFLAGS_MEMBER(wxLB_NEEDED_SB) + wxFLAGS_MEMBER(wxLB_NO_SB) wxFLAGS_MEMBER(wxLB_SORT) wxEND_FLAGS( wxListBoxStyle ) @@ -117,18 +118,25 @@ TODO PROPERTIES class wxListBoxItem : public wxOwnerDrawn { public: - wxListBoxItem(const wxString& str = wxEmptyString); -}; + wxListBoxItem(wxListBox *parent) + { m_parent = parent; } -wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false) -{ - // no bitmaps/checkmarks - SetMarginWidth(0); -} + wxListBox *GetParent() const + { return m_parent; } + + int GetIndex() const + { return m_parent->GetItemIndex(const_cast(this)); } + + wxString GetName() const + { return m_parent->GetString(GetIndex()); } + +private: + wxListBox *m_parent; +}; wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) { - return new wxListBoxItem(); + return new wxListBoxItem(this); } #endif //USE_OWNER_DRAWN @@ -165,7 +173,7 @@ bool wxListBox::Create(wxWindow *parent, return false; // create the native control - if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) ) + if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString, pos, size) ) { // control creation failed return false; @@ -207,10 +215,6 @@ WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const { WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); - // always show the vertical scrollbar if necessary -- otherwise it is - // impossible to use the control with the mouse - msStyle |= WS_VSCROLL; - // we always want to get the notifications msStyle |= LBS_NOTIFY; @@ -219,15 +223,23 @@ WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const msStyle |= LBS_NOINTEGRALHEIGHT; wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), - _T("only one of listbox selection modes can be specified") ); + wxT("only one of listbox selection modes can be specified") ); if ( style & wxLB_MULTIPLE ) msStyle |= LBS_MULTIPLESEL; else if ( style & wxLB_EXTENDED ) msStyle |= LBS_EXTENDEDSEL; - if ( m_windowStyle & wxLB_ALWAYS_SB ) - msStyle |= LBS_DISABLENOSCROLL; + wxASSERT_MSG( !(style & wxLB_ALWAYS_SB) || !(style & wxLB_NO_SB), + wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) ); + + if ( !(style & wxLB_NO_SB) ) + { + msStyle |= WS_VSCROLL; + if ( style & wxLB_ALWAYS_SB ) + msStyle |= LBS_DISABLENOSCROLL; + } + if ( m_windowStyle & wxLB_HSCROLL ) msStyle |= WS_HSCROLL; if ( m_windowStyle & wxLB_SORT ) @@ -249,7 +261,7 @@ WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const void wxListBox::OnInternalIdle() { wxWindow::OnInternalIdle(); - + if (m_updateHorizontalExtent) { SetHorizontalExtent(wxEmptyString); @@ -274,6 +286,14 @@ void wxListBox::DoDeleteOneItem(unsigned int n) wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") ); +#if wxUSE_OWNER_DRAWN + if ( HasFlag(wxLB_OWNERDRAW) ) + { + delete m_aItems[n]; + m_aItems.RemoveAt(n); + } +#endif // wxUSE_OWNER_DRAWN + SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); m_noItems--; @@ -298,7 +318,12 @@ int wxListBox::FindString(const wxString& s, bool bCase) const void wxListBox::DoClear() { - Free(); +#if wxUSE_OWNER_DRAWN + if ( HasFlag(wxLB_OWNERDRAW) ) + { + WX_CLEAR_ARRAY(m_aItems); + } +#endif // wxUSE_OWNER_DRAWN ListBox_ResetContent(GetHwnd()); @@ -308,16 +333,6 @@ void wxListBox::DoClear() UpdateOldSelections(); } -void wxListBox::Free() -{ -#if wxUSE_OWNER_DRAWN - if ( m_windowStyle & wxLB_OWNERDRAW ) - { - WX_CLEAR_ARRAY(m_aItems); - } -#endif // wxUSE_OWNER_DRAWN -} - void wxListBox::DoSetSelection(int N, bool select) { wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), @@ -357,7 +372,9 @@ void wxListBox::DoSetItemClientData(unsigned int n, void *clientData) wxT("invalid index in wxListBox::SetClientData") ); if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) + { wxLogDebug(wxT("LB_SETITEMDATA failed")); + } } // Return number of selections and an array of selected integers @@ -370,7 +387,7 @@ int wxListBox::GetSelections(wxArrayInt& aSelections) const int countSel = ListBox_GetSelCount(GetHwnd()); if ( countSel == LB_ERR ) { - wxLogDebug(_T("ListBox_GetSelCount failed")); + wxLogDebug(wxT("ListBox_GetSelCount failed")); } else if ( countSel != 0 ) { @@ -462,7 +479,6 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, if ( HasFlag(wxLB_OWNERDRAW) ) { wxOwnerDrawn *pNewItem = CreateLboxItem(n); - pNewItem->SetName(items[i]); pNewItem->SetFont(GetFont()); m_aItems.Insert(pNewItem, n); } @@ -477,7 +493,7 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, return n; } -int wxListBox::DoListHitTest(const wxPoint& point) const +int wxListBox::DoHitTestList(const wxPoint& point) const { LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT, 0, MAKELPARAM(point.x, point.y)); @@ -517,14 +533,6 @@ void wxListBox::SetString(unsigned int n, const wxString& s) else if ( oldObjData ) SetClientObject(n, oldObjData); -#if wxUSE_OWNER_DRAWN - if ( m_windowStyle & wxLB_OWNERDRAW ) - { - // update item's text - m_aItems[n]->SetName(s); - } -#endif //USE_OWNER_DRAWN - // we may have lost the selection if ( wasSelected ) Select(n); @@ -673,17 +681,69 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) } // ---------------------------------------------------------------------------- -// wxCheckListBox support +// owner-drawn list boxes support // ---------------------------------------------------------------------------- #if wxUSE_OWNER_DRAWN +// misc overloaded methods +// ----------------------- + +bool wxListBox::SetFont(const wxFont &font) +{ + if ( HasFlag(wxLB_OWNERDRAW) ) + { + const unsigned count = m_aItems.GetCount(); + for ( unsigned i = 0; i < count; i++ ) + m_aItems[i]->SetFont(font); + } + + wxListBoxBase::SetFont(font); + + return true; +} + +bool wxListBox::GetItemRect(size_t n, wxRect& rect) const +{ + wxCHECK_MSG( IsValid(n), false, + wxT("invalid index in wxListBox::GetItemRect") ); + + RECT rc; + + if ( ListBox_GetItemRect(GetHwnd(), n, &rc) != LB_ERR ) + { + rect = wxRectFromRECT(rc); + return true; + } + else + { + // couldn't retrieve rect: for example, item isn't visible + return false; + } +} + +bool wxListBox::RefreshItem(size_t n) +{ + wxRect rect; + if ( !GetItemRect(n, rect) ) + return false; + + RECT rc; + wxCopyRectToRECT(rect, rc); + + return ::InvalidateRect((HWND)GetHWND(), &rc, FALSE) == TRUE; +} + + // drawing // ------- -// space beneath/above each row in pixels -// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. -#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) +namespace +{ + // space beneath/above each row in pixels + static const int LISTBOX_EXTRA_SPACE = 1; + +} // anonymous namespace // the height is the same for all items // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes @@ -693,7 +753,7 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) { // only owner-drawn control should receive this message - wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); + wxCHECK( HasFlag(wxLB_OWNERDRAW), false ); MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; @@ -707,7 +767,7 @@ bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) wxDCTemp dc((WXHDC)hdc); dc.SetFont(GetFont()); - pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; + pStruct->itemHeight = dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE; pStruct->itemWidth = dc.GetCharWidth(); } @@ -724,7 +784,7 @@ bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) { // only owner-drawn control should receive this message - wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); + wxCHECK( HasFlag(wxLB_OWNERDRAW), false ); DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; @@ -738,7 +798,7 @@ bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) return pItem->OnDrawItem(dc, wxRectFromRECT(pStruct->rcItem), (wxOwnerDrawn::wxODAction)pStruct->itemAction, - (wxOwnerDrawn::wxODStatus)pStruct->itemState); + (wxOwnerDrawn::wxODStatus)(pStruct->itemState | wxOwnerDrawn::wxODHidePrefix)); } #endif // wxUSE_OWNER_DRAWN