1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
21 #include "wx/checklst.h"
24 #include "wx/object.h"
26 #include "wx/window.h"
27 #include "wx/dcmemory.h"
28 #include "wx/dcscreen.h"
29 #include "wx/settings.h"
30 #include "wx/listbox.h"
31 #include "wx/bitmap.h"
32 #include "wx/colour.h"
36 #include "wx/os2/dc.h"
37 #include "wx/ownerdrw.h"
42 // ----------------------------------------------------------------------------
43 // constants for base class
44 // ----------------------------------------------------------------------------
46 static const int CHECK_MARK_WIDTH
= 15;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // get item (converted to right type)
53 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
55 // ============================================================================
57 // ============================================================================
59 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
61 // ----------------------------------------------------------------------------
62 // declaration and implementation of wxCheckListBoxItem class
63 // ----------------------------------------------------------------------------
65 class wxCheckListBoxItem
: public wxOwnerDrawn
67 friend class wxCheckListBox
;
72 wxCheckListBoxItem(wxCheckListBox
* pParent
, size_t nIndex
);
77 virtual bool OnDrawItem( wxDC
& rDc
,
86 bool IsChecked(void) const { return m_bChecked
; }
87 void Check(bool bCheck
);
88 void Toggle(void) { Check(!IsChecked()); }
90 virtual wxString
GetName() const { return m_pParent
->GetString(m_nIndex
); }
94 wxCheckListBox
* m_pParent
;
96 }; // end of CLASS wxCheckListBoxItem
100 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
* pParent
, size_t nIndex
)
101 :wxOwnerDrawn( wxEmptyString
, true /* checkable */ )
108 // We don't initialize m_nCheckHeight/Width vars because it's
109 // done in OnMeasure while they are used only in OnDraw and we
110 // know that there will always be OnMeasure before OnDraw
112 SetMarginWidth(CHECK_MARK_WIDTH
);
113 } // end of wxCheckListBoxItem::wxCheckListBoxItem
117 bool wxCheckListBoxItem::OnDrawItem ( wxDC
& rDc
,
122 wxRect vRect
= rRect
;
125 wxPMDCImpl
*impl
= (wxPMDCImpl
*) rDc
.GetImpl();
126 ::WinQueryWindowRect( m_pParent
->GetHWND(), &impl
->m_vRclPaint
);
128 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
131 // Unfortunately PM doesn't quite get the text position exact. We need to alter
132 // it down and to the right, just a little bit. The coords in rRect are OS/2
133 // coords not wxWidgets coords.
137 if (wxOwnerDrawn::OnDrawItem( rDc
, vRect
, eAct
, eStat
))
139 size_t nCheckWidth
= CHECK_MARK_WIDTH
;
140 size_t nCheckHeight
= m_pParent
->GetItemHeight();
142 int nX
= rRect
.GetX();
143 int nY
= rRect
.GetY();
145 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
149 m_pParent
->GetSize( NULL
, &nParentHeight
);
151 nY
= nParentHeight
- nY
- nCheckHeight
;
152 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
155 // Erase the 1-pixel border
157 rDc
.SetPen(vPenBack
);
158 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
161 // Now we draw the smaller rectangle
168 // Draw hollow gray rectangle
170 rDc
.SetPen(*wxGREY_PEN
);
171 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
177 // Draw the check by loading the sys standard bitmap and drawing it
179 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
, SBMP_MENUCHECK
);
180 POINTL vPoint
= {nX
, nOldY
+ 3};
181 wxPMDCImpl
*impl
= (wxPMDCImpl
*) rDc
.GetImpl();
182 ::WinDrawBitmap( impl
->GetHPS(),
194 } // end of wxCheckListBoxItem::OnDrawItem
197 // Change the state of the item and redraw it
199 void wxCheckListBoxItem::Check( bool bCheck
)
204 // Index may be chanegd because new items were added/deleted
206 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
211 int nIndex
= m_pParent
->GetItemIndex(this);
213 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
215 m_nIndex
= (size_t)nIndex
;
219 wxCommandEvent
vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
,m_pParent
->GetId());
221 vEvent
.SetInt(m_nIndex
);
222 vEvent
.SetEventObject(m_pParent
);
223 m_pParent
->ProcessCommand(vEvent
);
224 } // end of wxCheckListBoxItem::Check
227 // ----------------------------------------------------------------------------
228 // implementation of wxCheckListBox class
229 // ----------------------------------------------------------------------------
231 // define event table
232 // ------------------
233 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
234 EVT_CHAR(wxCheckListBox::OnChar
)
235 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
246 // Default ctor: use Create() to really create the control
248 wxCheckListBox::wxCheckListBox()
249 :wxCheckListBoxBase()
251 } // end of wxCheckListBox::wxCheckListBox
254 // Ctor which creates the associated control
256 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
261 const wxString asChoices
[],
263 const wxValidator
& rVal
,
264 const wxString
& rsName
)
265 :wxCheckListBoxBase()
267 Create( pParent
, vId
, rPos
, rSize
, nStrings
, asChoices
, lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
268 } // end of wxCheckListBox::wxCheckListBox
270 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
274 const wxArrayString
& asChoices
,
276 const wxValidator
& rVal
,
277 const wxString
& rsName
)
278 :wxCheckListBoxBase()
280 wxCArrayString
chs(asChoices
);
281 Create( pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
282 lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
283 } // end of wxCheckListBox::wxCheckListBox
285 void wxCheckListBox::Delete(unsigned int n
)
287 wxCHECK_RET( IsValid(n
),
288 wxT("invalid index in wxCheckListBox::Delete") );
289 wxListBox::Delete(n
);
295 m_aItems
.RemoveAt(n
);
296 } // end of wxCheckListBox::Delete
298 bool wxCheckListBox::SetFont ( const wxFont
& rFont
)
300 for (unsigned int i
= 0; i
< m_aItems
.GetCount(); i
++)
301 m_aItems
[i
]->SetFont(rFont
);
302 wxListBox::SetFont(rFont
);
304 } // end of wxCheckListBox::SetFont
309 // Create/retrieve item
310 // --------------------
314 // Create a check list box item
316 wxOwnerDrawn
* wxCheckListBox::CreateItem(size_t nIndex
)
318 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this, nIndex
);
320 } // end of wxCheckListBox::CreateItem
328 long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT
* pItem
)
331 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
332 if (wxListBox::OS2OnMeasure(pItem
))
334 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
339 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
342 // Add place for the check mark
344 pStruct
->rclItem
.xRight
+= CHECK_MARK_WIDTH
;
345 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
348 } // end of wxCheckListBox::CreateItem
356 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
358 return GetItem(uiIndex
)->IsChecked();
359 } // end of wxCheckListBox::IsChecked
361 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
363 GetItem(uiIndex
)->Check(bCheck
);
364 } // end of wxCheckListBox::Check
372 void wxCheckListBox::OnChar ( wxKeyEvent
& rEvent
)
374 if (rEvent
.GetKeyCode() == WXK_SPACE
)
375 GetItem(GetSelection())->Toggle();
378 } // end of wxCheckListBox::OnChar
380 void wxCheckListBox::OnLeftClick ( wxMouseEvent
& rEvent
)
383 // Clicking on the item selects it, clicking on the checkmark toggles
385 if (rEvent
.GetX() <= CHECK_MARK_WIDTH
)
391 GetSize( NULL
, &nParentHeight
);
392 vDc
.SetFont(GetFont());
393 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
396 // This, of course, will not work if the LB is scrolled
398 int nY
= rEvent
.GetY();
400 nY
= nParentHeight
- (nY
+ vHeight
);
402 size_t nItem
= (size_t)(nY
/ vHeight
);
404 if (nItem
< m_nNumItems
)
405 GetItem(nItem
)->Toggle();
407 // else: it's not an error, just click outside of client zone
413 // Implement default behaviour: clicking on the item selects it
417 } // end of wxCheckListBox::OnLeftClick
419 #endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN