X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ad81651f00edc6f489d9b6a0839d316a964fd521..64ea838d8f4d1853b7d850db93ee565e901d099a:/src/os2/checklst.cpp diff --git a/src/os2/checklst.cpp b/src/os2/checklst.cpp index cb94c109e2..6773983e9f 100644 --- a/src/os2/checklst.cpp +++ b/src/os2/checklst.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: checklst.cpp +// Name: src/os2/checklst.cpp // Purpose: implementation of wxCheckListBox class // Author: David Webster // Modified by: @@ -16,23 +16,35 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#if wxUSE_OWNER_DRAWN +#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN + +#include "wx/checklst.h" + +#ifndef WX_PRECOMP + #include "wx/object.h" + #include "wx/log.h" + #include "wx/window.h" + #include "wx/dcmemory.h" + #include "wx/dcscreen.h" + #include "wx/settings.h" + #include "wx/listbox.h" + #include "wx/bitmap.h" + #include "wx/colour.h" + #include "wx/font.h" +#endif -#include "wx/object.h" -#include "wx/colour.h" -#include "wx/font.h" -#include "wx/bitmap.h" -#include "wx/window.h" -#include "wx/listbox.h" +#include "wx/os2/dc.h" #include "wx/ownerdrw.h" -#include "wx/settings.h" -#include "wx/dcmemory.h" -#include "wx/os2/checklst.h" -#include "wx/log.h" #define INCL_PM #include +// ---------------------------------------------------------------------------- +// constants for base class +// ---------------------------------------------------------------------------- + +static const int CHECK_MARK_WIDTH = 15; + // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- @@ -44,195 +56,171 @@ // implementation // ============================================================================ - IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) - // ---------------------------------------------------------------------------- // declaration and implementation of wxCheckListBoxItem class // ---------------------------------------------------------------------------- class wxCheckListBoxItem : public wxOwnerDrawn { -friend class wxCheckListBox; + friend class wxCheckListBox; public: - // ctor - wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); - - // drawing functions - virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); - - // simple accessors - bool IsChecked() const { return m_bChecked; } - void Check(bool bCheck); - void Toggle() { Check(!IsChecked()); } + // + // ctor + // + wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex); + + // + // Drawing functions + // + virtual bool OnDrawItem( wxDC& rDc, + const wxRect& rRect, + wxODAction eAct, + wxODStatus eStat + ); + + // + // Simple accessors + // + bool IsChecked(void) const { return m_bChecked; } + void Check(bool bCheck); + void Toggle(void) { Check(!IsChecked()); } + + virtual wxString GetName() const { return m_pParent->GetString(m_nIndex); } private: - bool m_bChecked; - wxCheckListBox *m_pParent; - size_t m_nIndex; -}; + bool m_bChecked; + wxCheckListBox* m_pParent; + size_t m_nIndex; +}; // end of CLASS wxCheckListBoxItem -wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) - : wxOwnerDrawn("", TRUE) // checkable -{ - m_bChecked = FALSE; - m_pParent = pParent; - m_nIndex = nIndex; - - // we don't initialize m_nCheckHeight/Width vars because it's - // done in OnMeasure while they are used only in OnDraw and we - // know that there will always be OnMeasure before OnDraw - - // fix appearance - SetMarginWidth(GetDefaultMarginWidth()); -} - -/* - * JACS - I've got the owner-draw stuff partially working with WIN16, - * with a really horrible-looking cross for wxCheckListBox instead of a - * check - could use a bitmap check-mark instead, defined in wx.rc. - * Also there's a refresh problem whereby it doesn't always draw the - * check until you click to the right of it, which is OK for WIN32. - */ - -bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc, - wxODAction act, wxODStatus stat) -{ - if ( IsChecked() ) - stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked); -// TODO: -/* - if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) { - // ## using native API for performance and precision - size_t nCheckWidth = GetDefaultMarginWidth(), - nCheckHeight = m_pParent->GetItemHeight(); - - int x = rc.GetX(), - y = rc.GetY(); - - HDC hdc = (HDC)dc.GetHDC(); - - // create pens - HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)), - hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)), - hpenPrev = (HPEN)SelectObject(hdc, hpenBack); - - // we erase the 1-pixel border - Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); - - // shift check mark 1 pixel to the right (it looks better like this) - x++; - - if ( IsChecked() ) { - // first create a monochrome bitmap in a memory DC - HDC hdcMem = CreateCompatibleDC(hdc); - HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0); - HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck); +wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex) + :wxOwnerDrawn( wxEmptyString, true /* checkable */ ) +{ + m_bChecked = false; + m_pParent = pParent; + m_nIndex = nIndex; - // then draw a check mark into it - RECT rect ; - rect.left = 0 ; - rect.top = 0 ; - rect.right = nCheckWidth ; - rect.bottom = nCheckHeight ; + // + // We don't initialize m_nCheckHeight/Width vars because it's + // done in OnMeasure while they are used only in OnDraw and we + // know that there will always be OnMeasure before OnDraw + // + SetMarginWidth(CHECK_MARK_WIDTH); +} // end of wxCheckListBoxItem::wxCheckListBoxItem -#ifdef __WIN32__ -#ifndef __SC__ - DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); -#endif -#else - // In WIN16, draw a cross - HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); - HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH); - HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen); - HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush); - ::SetROP2(hdcMem, R2_COPYPEN); - Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight); - MoveTo(hdcMem, 0, 0); - LineTo(hdcMem, nCheckWidth, nCheckHeight); - MoveTo(hdcMem, nCheckWidth, 0); - LineTo(hdcMem, 0, nCheckHeight); - ::SelectObject(hdcMem, hPenOld); - ::SelectObject(hdcMem, hBrushOld); - ::DeleteObject(blackPen); -#endif - // finally copy it to screen DC and clean up - BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight, - hdcMem, 0, 0, SRCCOPY); - SelectObject(hdcMem, hbmpOld); - DeleteObject(hbmpCheck); - DeleteDC(hdcMem); +bool wxCheckListBoxItem::OnDrawItem ( wxDC& rDc, + const wxRect& rRect, + wxODAction eAct, + wxODStatus eStat ) +{ + wxRect vRect = rRect; + + + wxPMDCImpl *impl = (wxPMDCImpl*) rDc.GetImpl(); + ::WinQueryWindowRect( m_pParent->GetHWND(), &impl->m_vRclPaint ); + if (IsChecked()) + eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked); + + // + // Unfortunately PM doesn't quite get the text position exact. We need to alter + // it down and to the right, just a little bit. The coords in rRect are OS/2 + // coords not wxWidgets coords. + // + vRect.x += 5; + vRect.y -= 3; + if (wxOwnerDrawn::OnDrawItem( rDc, vRect, eAct, eStat)) + { + size_t nCheckWidth = CHECK_MARK_WIDTH; + size_t nCheckHeight = m_pParent->GetItemHeight(); + int nParentHeight; + int nX = rRect.GetX(); + int nY = rRect.GetY(); + int nOldY = nY; + wxColour vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); + wxPen vPenBack; + wxPen vPenPrev; + + m_pParent->GetSize( NULL, &nParentHeight); + + nY = nParentHeight - nY - nCheckHeight; + vPenBack = wxPen(vColour, 1, wxSOLID); + + // + // Erase the 1-pixel border + // + rDc.SetPen(vPenBack); + rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight ); + + // + // Now we draw the smaller rectangle + // + nY++; + nCheckWidth -= 2; + nCheckHeight -= 2; + + // + // Draw hollow gray rectangle + // + rDc.SetPen(*wxGREY_PEN); + rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight ); + + nX++; + if (IsChecked()) + { + // + // Draw the check by loading the sys standard bitmap and drawing it + // + HBITMAP hChkBmp = ::WinGetSysBitmap( HWND_DESKTOP, SBMP_MENUCHECK ); + POINTL vPoint = {nX, nOldY + 3}; + wxPMDCImpl *impl = (wxPMDCImpl*) rDc.GetImpl(); + ::WinDrawBitmap( impl->GetHPS(), + hChkBmp, + NULL, + &vPoint, + NULL, + NULL, + DBM_NORMAL + ); + } + return true; } + return false; +} // end of wxCheckListBoxItem::OnDrawItem - // now we draw the smaller rectangle - y++; - nCheckWidth -= 2; - nCheckHeight -= 2; - - // draw hollow gray rectangle - (void)SelectObject(hdc, hpenGray); - HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH)); - Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); - - // clean up - (void)SelectObject(hdc, hpenPrev); - (void)SelectObject(hdc, hbrPrev); - - DeleteObject(hpenBack); - DeleteObject(hpenGray); - -// -// dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight); // -// if ( IsChecked() ) { -// dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight); -// dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y); -// } +// Change the state of the item and redraw it // - - return TRUE; - } -*/ - return FALSE; -} - -// change the state of the item and redraw it -void wxCheckListBoxItem::Check(bool check) +void wxCheckListBoxItem::Check( bool bCheck ) { - m_bChecked = check; + m_bChecked = bCheck; - // index may be chanegd because new items were added/deleted - if ( m_pParent->GetItemIndex(this) != (int)m_nIndex ) + // + // Index may be chanegd because new items were added/deleted + // + if (m_pParent->GetItemIndex(this) != (int)m_nIndex) { - // update it - int index = m_pParent->GetItemIndex(this); + // + // Update it + // + int nIndex = m_pParent->GetItemIndex(this); - wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") ); + wxASSERT_MSG(nIndex != wxNOT_FOUND, wxT("what does this item do here?")); - m_nIndex = (size_t)index; + m_nIndex = (size_t)nIndex; } - HWND hwndListbox = (HWND)m_pParent->GetHWND(); -// TODO: -/* - RECT rcUpdate; - if ( ::SendMessage(hwndListbox, LB_GETITEMRECT, - m_nIndex, (LPARAM)&rcUpdate) == LB_ERR ) - { - wxLogDebug(wxT("LB_GETITEMRECT failed")); - } + wxCommandEvent vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,m_pParent->GetId()); + + vEvent.SetInt(m_nIndex); + vEvent.SetEventObject(m_pParent); + m_pParent->ProcessCommand(vEvent); +} // end of wxCheckListBoxItem::Check - InvalidateRect(hwndListbox, &rcUpdate, FALSE); -*/ - wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); - event.SetInt(m_nIndex); - event.SetEventObject(m_pParent); - m_pParent->ProcessCommand(event); -} // ---------------------------------------------------------------------------- // implementation of wxCheckListBox class @@ -241,151 +229,189 @@ void wxCheckListBoxItem::Check(bool check) // define event table // ------------------ BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) - EVT_CHAR(wxCheckListBox::OnChar) - EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) + EVT_CHAR(wxCheckListBox::OnChar) + EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) END_EVENT_TABLE() -// control creation + + +// +// Control creation // ---------------- +// -// def ctor: use Create() to really create the control -wxCheckListBox::wxCheckListBox() : wxListBox() +// +// Default ctor: use Create() to really create the control +// +wxCheckListBox::wxCheckListBox() + :wxCheckListBoxBase() { -} - -// ctor which creates the associated control -wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, - const wxPoint& pos, const wxSize& size, - int nStrings, const wxString choices[], -#if wxUSE_VALIDATORS - long style, const wxValidator& val, -#endif - const wxString& name) - : wxListBox() +} // end of wxCheckListBox::wxCheckListBox + +// +// Ctor which creates the associated control +// +wxCheckListBox::wxCheckListBox ( wxWindow* pParent, + wxWindowID vId, + const wxPoint& rPos, + const wxSize& rSize, + int nStrings, + const wxString asChoices[], + long lStyle, + const wxValidator& rVal, + const wxString& rsName) + :wxCheckListBoxBase() { - Create(parent, id, pos, size, nStrings, choices, - style | wxLB_OWNERDRAW, val, name); -} + Create( pParent, vId, rPos, rSize, nStrings, asChoices, lStyle | wxLB_OWNERDRAW, rVal, rsName ); +} // end of wxCheckListBox::wxCheckListBox + +wxCheckListBox::wxCheckListBox ( wxWindow* pParent, + wxWindowID vId, + const wxPoint& rPos, + const wxSize& rSize, + const wxArrayString& asChoices, + long lStyle, + const wxValidator& rVal, + const wxString& rsName ) + :wxCheckListBoxBase() +{ + wxCArrayString chs(asChoices); + Create( pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(), + lStyle | wxLB_OWNERDRAW, rVal, rsName ); +} // end of wxCheckListBox::wxCheckListBox -void wxCheckListBox::Delete(int N) +void wxCheckListBox::Delete(unsigned int n) +{ + wxCHECK_RET( IsValid(n), + wxT("invalid index in wxCheckListBox::Delete") ); + wxListBox::Delete(n); + + // + // Free memory + // + delete m_aItems[n]; + m_aItems.RemoveAt(n); +} // end of wxCheckListBox::Delete + +bool wxCheckListBox::SetFont ( const wxFont& rFont ) { - wxCHECK_RET( N >= 0 && N < m_noItems, - wxT("invalid index in wxListBox::Delete") ); + for (unsigned int i = 0; i < m_aItems.GetCount(); i++) + m_aItems[i]->SetFont(rFont); + wxListBox::SetFont(rFont); + return true; +} // end of wxCheckListBox::SetFont - wxListBox::Delete(N); - // free memory - delete m_aItems[N]; - m_aItems.Remove(N); -} +// +// Create/retrieve item +// -------------------- +// -void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos) +// +// Create a check list box item +// +wxOwnerDrawn* wxCheckListBox::CreateItem(size_t nIndex) { - wxCHECK_RET( pos >= 0 && pos <= m_noItems, - wxT("invalid index in wxCheckListBox::InsertItems") ); + wxCheckListBoxItem* pItem = new wxCheckListBoxItem( this, nIndex ); + return pItem; +} // end of wxCheckListBox::CreateItem - wxListBox::InsertItems(nItems, items, pos); - int i; - for ( i = 0; i < nItems; i++ ) { - wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i)); - pNewItem->SetName(items[i]); - m_aItems.Insert(pNewItem, (size_t)(pos + i)); -// ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem); + +// +// Return item size +// ---------------- +// +long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT* pItem ) +{ + if (!pItem) + pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM; + if (wxListBox::OS2OnMeasure(pItem)) + { + POWNERITEM pStruct = (POWNERITEM)pItem; + + // + // Save item height + // + m_nItemHeight = pStruct->rclItem.yTop - pStruct->rclItem.yBottom; + + // + // Add place for the check mark + // + pStruct->rclItem.xRight += CHECK_MARK_WIDTH; + return long(MRFROM2SHORT((USHORT)m_nItemHeight, (USHORT)(pStruct->rclItem.xRight - pStruct->rclItem.xLeft))); } -} + return 0L; +} // end of wxCheckListBox::CreateItem -bool wxCheckListBox::SetFont( const wxFont &font ) -{ - size_t i; - for (i=0; i < m_aItems.GetCount(); i++) - m_aItems[i]->SetFont(font); - wxListBox::SetFont(font); - return TRUE; -} - -// create/retrieve item -// -------------------- -// create a check list box item -wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex) +// +// Check items +// ----------- +// +bool wxCheckListBox::IsChecked(unsigned int uiIndex) const { - wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); - return pItem; -} + return GetItem(uiIndex)->IsChecked(); +} // end of wxCheckListBox::IsChecked -// return item size -// ---------------- -// TODO: -/* -bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) +void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) { - if ( wxListBox::MSWOnMeasure(item) ) { - MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; + GetItem(uiIndex)->Check(bCheck); +} // end of wxCheckListBox::Check - // save item height - m_nItemHeight = pStruct->itemHeight; - // add place for the check mark - pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); - return TRUE; - } - - return FALSE; -} -*/ -// check items -// ----------- - -bool wxCheckListBox::IsChecked(size_t uiIndex) const +// +// Process events +// -------------- +// +void wxCheckListBox::OnChar ( wxKeyEvent& rEvent ) { - return GetItem(uiIndex)->IsChecked(); -} + if (rEvent.GetKeyCode() == WXK_SPACE) + GetItem(GetSelection())->Toggle(); + else + rEvent.Skip(); +} // end of wxCheckListBox::OnChar -void wxCheckListBox::Check(size_t uiIndex, bool bCheck) +void wxCheckListBox::OnLeftClick ( wxMouseEvent& rEvent ) { - GetItem(uiIndex)->Check(bCheck); -} + // + // Clicking on the item selects it, clicking on the checkmark toggles + // + if (rEvent.GetX() <= CHECK_MARK_WIDTH) + { + int nParentHeight; + wxScreenDC vDc; + wxCoord vHeight; -// process events -// -------------- + GetSize( NULL, &nParentHeight ); + vDc.SetFont(GetFont()); + vHeight = (wxCoord)(vDc.GetCharHeight() * 2.5); -void wxCheckListBox::OnChar(wxKeyEvent& event) -{ - if ( event.KeyCode() == WXK_SPACE ) - GetItem(GetSelection())->Toggle(); - else - event.Skip(); -} + // + // This, of course, will not work if the LB is scrolled + // + int nY = rEvent.GetY(); -void wxCheckListBox::OnLeftClick(wxMouseEvent& event) -{ - // clicking on the item selects it, clicking on the checkmark toggles - if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) { -// TODO: -/* - size_t nItem = (size_t)::SendMessage - ( - (HWND)GetHWND(), - LB_ITEMFROMPOINT, - 0, - MAKELPARAM(event.GetX(), event.GetY()) - ); -*/ - size_t nItem = 0; - - if ( nItem < (size_t)m_noItems ) - GetItem(nItem)->Toggle(); - //else: it's not an error, just click outside of client zone - } - else { - // implement default behaviour: clicking on the item selects it - event.Skip(); - } -} + nY = nParentHeight - (nY + vHeight); -#endif + size_t nItem = (size_t)(nY / vHeight); + + if (nItem < m_nNumItems) + GetItem(nItem)->Toggle(); + // + // else: it's not an error, just click outside of client zone + // + } + else + { + // + // Implement default behaviour: clicking on the item selects it + // + rEvent.Skip(); + } +} // end of wxCheckListBox::OnLeftClick +#endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN