]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
12 // headers & declarations
13 // ============================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
20 #include "wx/checklst.h"
23 #include "wx/object.h"
25 #include "wx/window.h"
26 #include "wx/dcmemory.h"
27 #include "wx/dcscreen.h"
28 #include "wx/settings.h"
29 #include "wx/listbox.h"
30 #include "wx/bitmap.h"
31 #include "wx/colour.h"
35 #include "wx/os2/dc.h"
36 #include "wx/ownerdrw.h"
41 // ----------------------------------------------------------------------------
42 // constants for base class
43 // ----------------------------------------------------------------------------
45 static const int CHECK_MARK_WIDTH
= 15;
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // get item (converted to right type)
52 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // declaration and implementation of wxCheckListBoxItem class
60 // ----------------------------------------------------------------------------
62 class wxCheckListBoxItem
: public wxOwnerDrawn
64 friend class wxCheckListBox
;
69 wxCheckListBoxItem(wxCheckListBox
* pParent
, size_t nIndex
);
74 virtual bool OnDrawItem( wxDC
& rDc
,
83 bool IsChecked(void) const { return m_bChecked
; }
84 void Check(bool bCheck
);
85 void Toggle(void) { Check(!IsChecked()); }
87 virtual wxString
GetName() const { return m_pParent
->GetString(m_nIndex
); }
91 wxCheckListBox
* m_pParent
;
93 }; // end of CLASS wxCheckListBoxItem
97 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
* pParent
, size_t nIndex
)
98 :wxOwnerDrawn( wxEmptyString
, true /* checkable */ )
105 // We don't initialize m_nCheckHeight/Width vars because it's
106 // done in OnMeasure while they are used only in OnDraw and we
107 // know that there will always be OnMeasure before OnDraw
109 SetMarginWidth(CHECK_MARK_WIDTH
);
110 } // end of wxCheckListBoxItem::wxCheckListBoxItem
114 bool wxCheckListBoxItem::OnDrawItem ( wxDC
& rDc
,
119 wxRect vRect
= rRect
;
122 wxPMDCImpl
*impl
= (wxPMDCImpl
*) rDc
.GetImpl();
123 ::WinQueryWindowRect( m_pParent
->GetHWND(), &impl
->m_vRclPaint
);
125 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
128 // Unfortunately PM doesn't quite get the text position exact. We need to alter
129 // it down and to the right, just a little bit. The coords in rRect are OS/2
130 // coords not wxWidgets coords.
134 if (wxOwnerDrawn::OnDrawItem( rDc
, vRect
, eAct
, eStat
))
136 size_t nCheckWidth
= CHECK_MARK_WIDTH
;
137 size_t nCheckHeight
= m_pParent
->GetItemHeight();
139 int nX
= rRect
.GetX();
140 int nY
= rRect
.GetY();
142 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
146 m_pParent
->GetSize( NULL
, &nParentHeight
);
148 nY
= nParentHeight
- nY
- nCheckHeight
;
149 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
152 // Erase the 1-pixel border
154 rDc
.SetPen(vPenBack
);
155 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
158 // Now we draw the smaller rectangle
165 // Draw hollow gray rectangle
167 rDc
.SetPen(*wxGREY_PEN
);
168 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
174 // Draw the check by loading the sys standard bitmap and drawing it
176 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
, SBMP_MENUCHECK
);
177 POINTL vPoint
= {nX
, nOldY
+ 3};
178 wxPMDCImpl
*impl
= (wxPMDCImpl
*) rDc
.GetImpl();
179 ::WinDrawBitmap( impl
->GetHPS(),
191 } // end of wxCheckListBoxItem::OnDrawItem
194 // Change the state of the item and redraw it
196 void wxCheckListBoxItem::Check( bool bCheck
)
201 // Index may be chanegd because new items were added/deleted
203 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
208 int nIndex
= m_pParent
->GetItemIndex(this);
210 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
212 m_nIndex
= (size_t)nIndex
;
216 wxCommandEvent
vEvent( wxEVT_CHECKLISTBOX
,m_pParent
->GetId());
218 vEvent
.SetInt(m_nIndex
);
219 vEvent
.SetEventObject(m_pParent
);
220 m_pParent
->ProcessCommand(vEvent
);
221 } // end of wxCheckListBoxItem::Check
224 // ----------------------------------------------------------------------------
225 // implementation of wxCheckListBox class
226 // ----------------------------------------------------------------------------
228 // define event table
229 // ------------------
230 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
231 EVT_CHAR(wxCheckListBox::OnChar
)
232 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
243 // Default ctor: use Create() to really create the control
245 wxCheckListBox::wxCheckListBox()
246 :wxCheckListBoxBase()
248 } // end of wxCheckListBox::wxCheckListBox
251 // Ctor which creates the associated control
253 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
258 const wxString asChoices
[],
260 const wxValidator
& rVal
,
261 const wxString
& rsName
)
262 :wxCheckListBoxBase()
264 Create( pParent
, vId
, rPos
, rSize
, nStrings
, asChoices
, lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
265 } // end of wxCheckListBox::wxCheckListBox
267 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
271 const wxArrayString
& asChoices
,
273 const wxValidator
& rVal
,
274 const wxString
& rsName
)
275 :wxCheckListBoxBase()
277 wxCArrayString
chs(asChoices
);
278 Create( pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
279 lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
280 } // end of wxCheckListBox::wxCheckListBox
282 void wxCheckListBox::Delete(unsigned int n
)
284 wxCHECK_RET( IsValid(n
),
285 wxT("invalid index in wxCheckListBox::Delete") );
286 wxListBox::Delete(n
);
292 m_aItems
.RemoveAt(n
);
293 } // end of wxCheckListBox::Delete
295 bool wxCheckListBox::SetFont ( const wxFont
& rFont
)
297 for (unsigned int i
= 0; i
< m_aItems
.GetCount(); i
++)
298 m_aItems
[i
]->SetFont(rFont
);
299 wxListBox::SetFont(rFont
);
301 } // end of wxCheckListBox::SetFont
306 // Create/retrieve item
307 // --------------------
311 // Create a check list box item
313 wxOwnerDrawn
* wxCheckListBox::CreateItem(size_t nIndex
)
315 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this, nIndex
);
317 } // end of wxCheckListBox::CreateItem
325 long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT
* pItem
)
328 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
329 if (wxListBox::OS2OnMeasure(pItem
))
331 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
336 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
339 // Add place for the check mark
341 pStruct
->rclItem
.xRight
+= CHECK_MARK_WIDTH
;
342 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
345 } // end of wxCheckListBox::CreateItem
353 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
355 return GetItem(uiIndex
)->IsChecked();
356 } // end of wxCheckListBox::IsChecked
358 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
360 GetItem(uiIndex
)->Check(bCheck
);
361 } // end of wxCheckListBox::Check
369 void wxCheckListBox::OnChar ( wxKeyEvent
& rEvent
)
371 if (rEvent
.GetKeyCode() == WXK_SPACE
)
372 GetItem(GetSelection())->Toggle();
375 } // end of wxCheckListBox::OnChar
377 void wxCheckListBox::OnLeftClick ( wxMouseEvent
& rEvent
)
380 // Clicking on the item selects it, clicking on the checkmark toggles
382 if (rEvent
.GetX() <= CHECK_MARK_WIDTH
)
388 GetSize( NULL
, &nParentHeight
);
389 vDc
.SetFont(GetFont());
390 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
393 // This, of course, will not work if the LB is scrolled
395 int nY
= rEvent
.GetY();
397 nY
= nParentHeight
- (nY
+ vHeight
);
399 size_t nItem
= (size_t)(nY
/ vHeight
);
401 if (nItem
< m_nNumItems
)
402 GetItem(nItem
)->Toggle();
404 // else: it's not an error, just click outside of client zone
410 // Implement default behaviour: clicking on the item selects it
414 } // end of wxCheckListBox::OnLeftClick
416 #endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN