1 ///////////////////////////////////////////////////////////////////////////////
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"
21 #include "wx/object.h"
22 #include "wx/colour.h"
24 #include "wx/bitmap.h"
25 #include "wx/window.h"
26 #include "wx/listbox.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/settings.h"
29 #include "wx/dcmemory.h"
30 #include "wx/os2/checklst.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // get item (converted to right type)
41 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
43 // ============================================================================
45 // ============================================================================
47 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
49 // ----------------------------------------------------------------------------
50 // declaration and implementation of wxCheckListBoxItem class
51 // ----------------------------------------------------------------------------
53 class wxCheckListBoxItem
: public wxOwnerDrawn
55 friend class wxCheckListBox
;
60 wxCheckListBoxItem( wxCheckListBox
* pParent
67 virtual bool OnDrawItem( wxDC
& rDc
76 bool IsChecked(void) const { return m_bChecked
; }
77 void Check(bool bCheck
);
78 void Toggle(void) { Check(!IsChecked()); }
82 wxCheckListBox
* m_pParent
;
84 }; // end of CLASS wxCheckListBoxItem
86 wxCheckListBoxItem::wxCheckListBoxItem (
87 wxCheckListBox
* pParent
99 // We don't initialize m_nCheckHeight/Width vars because it's
100 // done in OnMeasure while they are used only in OnDraw and we
101 // know that there will always be OnMeasure before OnDraw
103 SetMarginWidth(GetDefaultMarginWidth());
104 } // end of wxCheckListBoxItem::wxCheckListBoxItem
106 bool wxCheckListBoxItem::OnDrawItem (
108 , const wxRect
& rRect
113 wxRect vRect
= rRect
;
115 ::WinQueryWindowRect( m_pParent
->GetHWND()
119 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
122 // Unfortunately PM doesn't quite get the text position exact. We need to alter
123 // it down and to the right, just a little bit. The coords in rRect are OS/2
124 // coords not wxWindows coords.
128 if (wxOwnerDrawn::OnDrawItem( rDc
133 size_t nCheckWidth
= GetDefaultMarginWidth();
134 size_t nCheckHeight
= m_pParent
->GetItemHeight();
136 int nX
= rRect
.GetX();
137 int nY
= rRect
.GetY();
139 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
144 m_pParent
->GetSize( NULL
148 nY
= nParentHeight
- nY
- nCheckHeight
;
149 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
150 vPenGray
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
153 // Erase the 1-pixel border
155 rDc
.SetPen(vPenBack
);
156 rDc
.DrawRectangle( nX
163 // Now we draw the smaller rectangle
170 // Draw hollow gray rectangle
172 rDc
.SetPen(vPenGray
);
173 rDc
.DrawRectangle( nX
183 // Draw the check by loading the sys standard bitmap and drawing it
185 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
188 POINTL vPoint
= {nX
, nOldY
+ 3};
190 ::WinDrawBitmap( rDc
.GetHPS()
202 } // end of wxCheckListBoxItem::OnDrawItem
205 // Change the state of the item and redraw it
207 void wxCheckListBoxItem::Check (
214 // Index may be chanegd because new items were added/deleted
216 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
221 int nIndex
= m_pParent
->GetItemIndex(this);
223 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
225 m_nIndex
= (size_t)nIndex
;
228 HWND hWndListbox
= (HWND
)m_pParent
->GetHWND();
232 wxCommandEvent
vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
236 vEvent
.SetInt(m_nIndex
);
237 vEvent
.SetEventObject(m_pParent
);
238 m_pParent
->ProcessCommand(vEvent
);
239 } // end of wxCheckListBoxItem::Check
241 // ----------------------------------------------------------------------------
242 // implementation of wxCheckListBox class
243 // ----------------------------------------------------------------------------
245 // define event table
246 // ------------------
247 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
248 EVT_CHAR(wxCheckListBox::OnChar
)
249 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
258 // Default ctor: use Create() to really create the control
260 wxCheckListBox::wxCheckListBox()
263 } // end of wxCheckListBox::wxCheckListBox
266 // Ctor which creates the associated control
268 wxCheckListBox::wxCheckListBox (
271 , const wxPoint
& rPos
272 , const wxSize
& rSize
274 , const wxString asChoices
[]
277 , const wxValidator
& rVal
279 , const wxString
& rsName
289 ,lStyle
| wxLB_OWNERDRAW
295 } // end of wxCheckListBox::wxCheckListBox
297 void wxCheckListBox::Delete(
301 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
302 wxT("invalid index in wxListBox::Delete") );
303 wxListBox::Delete(N
);
309 m_aItems
.RemoveAt(N
);
310 } // end of wxCheckListBox::Delete
312 void wxCheckListBox::InsertItems (
314 , const wxString asItems
[]
320 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
321 wxT("invalid index in wxCheckListBox::InsertItems") );
323 wxListBox::InsertItems( nItems
327 for (i
= 0; i
< nItems
; i
++)
329 wxOwnerDrawn
* pNewItem
= CreateItem((size_t)(nPos
+ i
));
331 pNewItem
->SetName(asItems
[i
]);
332 m_aItems
.Insert(pNewItem
, (size_t)(nPos
+ i
));
333 ::WinSendMsg( (HWND
)GetHWND()
339 } // end of wxCheckListBox::InsertItems
341 bool wxCheckListBox::SetFont (
347 for (i
= 0; i
< m_aItems
.GetCount(); i
++)
348 m_aItems
[i
]->SetFont(rFont
);
349 wxListBox::SetFont(rFont
);
351 } // end of wxCheckListBox::SetFont
354 // Create/retrieve item
355 // --------------------
359 // Create a check list box item
361 wxOwnerDrawn
* wxCheckListBox::CreateItem (
365 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this
369 } // end of wxCheckListBox::CreateItem
375 long wxCheckListBox::OS2OnMeasure (
376 WXMEASUREITEMSTRUCT
* pItem
380 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
381 if (wxListBox::OS2OnMeasure(pItem
) )
383 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
388 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
391 // Add place for the check mark
393 pStruct
->rclItem
.xRight
+= wxOwnerDrawn::GetDefaultMarginWidth();
394 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
397 } // end of wxCheckListBox::CreateItem
403 bool wxCheckListBox::IsChecked (
407 return GetItem(uiIndex
)->IsChecked();
408 } // end of wxCheckListBox::IsChecked
410 void wxCheckListBox::Check (
415 GetItem(uiIndex
)->Check(bCheck
);
416 } // end of wxCheckListBox::Check
422 void wxCheckListBox::OnChar (
426 if (rEvent
.KeyCode() == WXK_SPACE
)
427 GetItem(GetSelection())->Toggle();
430 } // end of wxCheckListBox::OnChar
432 void wxCheckListBox::OnLeftClick (
437 // Clicking on the item selects it, clicking on the checkmark toggles
439 if (rEvent
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth())
448 vDc
.SetFont(GetFont());
449 vHeight
= vDc
.GetCharHeight() * 2.5;
452 // This, of course, will not work if the LB is scrolled
454 int nY
= rEvent
.GetY();
456 nY
= nParentHeight
- (nY
+ vHeight
);
458 size_t nItem
= (size_t)(nY
/ vHeight
);
460 if (nItem
< (size_t)m_nNumItems
)
461 GetItem(nItem
)->Toggle();
463 // else: it's not an error, just click outside of client zone
469 // Implement default behaviour: clicking on the item selects it
473 } // end of wxCheckListBox::OnLeftClick