]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checklst.cpp
"continue;" restored to prevent the app from going into infinite loop
[wxWidgets.git] / src / msw / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 16.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "checklst.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_OWNER_DRAWN
24
25 #include "wx/object.h"
26 #include "wx/colour.h"
27 #include "wx/font.h"
28 #include "wx/bitmap.h"
29 #include "wx/window.h"
30 #include "wx/listbox.h"
31 #include "wx/ownerdrw.h"
32 #include "wx/settings.h"
33 #include "wx/dcmemory.h"
34 #include "wx/msw/checklst.h"
35
36 #include <windows.h>
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // declaration and implementation of wxCheckListBoxItem class
48 // ----------------------------------------------------------------------------
49
50 class wxCheckListBoxItem : public wxOwnerDrawn
51 {
52 public:
53 // ctor
54 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
55
56 // drawing functions
57 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
58
59 // simple accessors
60 bool IsChecked() const { return m_bChecked; }
61 void Check(bool bCheck) { m_bChecked = bCheck; }
62 void Toggle();
63
64 private:
65 bool m_bChecked;
66 wxCheckListBox *m_pParent;
67 size_t m_nIndex;
68 };
69
70 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
71 : wxOwnerDrawn("", TRUE) // checkable
72 {
73 m_bChecked = FALSE;
74 m_pParent = pParent;
75 m_nIndex = nIndex;
76
77 // we don't initialize m_nCheckHeight/Width vars because it's
78 // done in OnMeasure while they are used only in OnDraw and we
79 // know that there will always be OnMeasure before OnDraw
80
81 // fix appearance
82 SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
83 SetMarginWidth(GetDefaultMarginWidth());
84 }
85
86 /*
87 * JACS - I've got the owner-draw stuff partially working with WIN16,
88 * with a really horrible-looking cross for wxCheckListBox instead of a
89 * check - could use a bitmap check-mark instead, defined in wx.rc.
90 * Also there's a refresh problem whereby it doesn't always draw the
91 * check until you click to the right of it, which is OK for WIN32.
92 */
93
94 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
95 wxODAction act, wxODStatus stat)
96 {
97 if ( IsChecked() )
98 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
99
100 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
101 // ## using native API for performance and precision
102 size_t nCheckWidth = GetDefaultMarginWidth(),
103 nCheckHeight = m_pParent->GetItemHeight();
104
105 int x = rc.GetX(),
106 y = rc.GetY();
107
108 HDC hdc = (HDC)dc.GetHDC();
109
110 // create pens
111 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
112 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
113 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
114
115 // we erase the 1-pixel border
116 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
117
118 // shift check mark 1 pixel to the right (it looks better like this)
119 x++;
120
121 if ( IsChecked() ) {
122 // first create a monochrome bitmap in a memory DC
123 HDC hdcMem = CreateCompatibleDC(hdc);
124 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
125 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
126
127 // then draw a check mark into it
128 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
129
130 #ifdef __WIN32__
131 #ifndef __SC__
132 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
133 #endif
134 #else
135 // In WIN16, draw a cross
136 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
137 HPEN whiteBrush = GetStockObject(WHITE_BRUSH);
138 HPEN hPenOld = ::SelectObject(hdcMem, blackPen);
139 HPEN hBrushOld = ::SelectObject(hdcMem, whiteBrush);
140 ::SetROP2(hdcMem, R2_COPYPEN);
141 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
142 MoveTo(hdcMem, 0, 0);
143 LineTo(hdcMem, nCheckWidth, nCheckHeight);
144 MoveTo(hdcMem, nCheckWidth, 0);
145 LineTo(hdcMem, 0, nCheckHeight);
146 ::SelectObject(hdcMem, hPenOld);
147 ::SelectObject(hdcMem, hBrushOld);
148 ::DeleteObject(blackPen);
149 #endif
150
151 // finally copy it to screen DC and clean up
152 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
153 hdcMem, 0, 0, SRCCOPY);
154
155 SelectObject(hdcMem, hbmpOld);
156 DeleteObject(hbmpCheck);
157 DeleteDC(hdcMem);
158 }
159
160 // now we draw the smaller rectangle
161 y++;
162 nCheckWidth -= 2;
163 nCheckHeight -= 2;
164
165 // draw hollow gray rectangle
166 (void)SelectObject(hdc, hpenGray);
167 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
168 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
169
170 // clean up
171 (void)SelectObject(hdc, hpenPrev);
172 (void)SelectObject(hdc, hbrPrev);
173
174 DeleteObject(hpenBack);
175 DeleteObject(hpenGray);
176
177 /*
178 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
179
180 if ( IsChecked() ) {
181 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
182 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
183 }
184 */
185
186 return TRUE;
187 }
188
189 return FALSE;
190 }
191
192 // change the state of the item and redraw it
193 void wxCheckListBoxItem::Toggle()
194 {
195 m_bChecked = !m_bChecked;
196
197 size_t nHeight = m_pParent->GetItemHeight();
198 size_t y = m_nIndex * nHeight;
199 RECT rcUpdate = { 0, y, GetDefaultMarginWidth(), y + nHeight};
200 InvalidateRect((HWND)m_pParent->GetHWND(), &rcUpdate, FALSE);
201
202 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
203 event.SetInt(m_nIndex);
204 event.SetEventObject(m_pParent);
205 m_pParent->ProcessCommand(event);
206 }
207
208 // ----------------------------------------------------------------------------
209 // implementation of wxCheckListBox class
210 // ----------------------------------------------------------------------------
211
212 // define event table
213 // ------------------
214 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
215 EVT_CHAR(wxCheckListBox::OnChar)
216 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
217 END_EVENT_TABLE()
218
219 // control creation
220 // ----------------
221
222 // def ctor: use Create() to really create the control
223 wxCheckListBox::wxCheckListBox() : wxListBox()
224 {
225 }
226
227 // ctor which creates the associated control
228 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
229 const wxPoint& pos, const wxSize& size,
230 int nStrings, const wxString choices[],
231 long style, const wxValidator& val,
232 const wxString& name) // , const wxFont& font)
233 // don't use ctor with arguments! we must call Create()
234 // ourselves from here.
235 : wxListBox()
236 // , m_font(font)
237 {
238 Create(parent, id, pos, size, nStrings, choices, style|wxLB_OWNERDRAW, val, name);
239 }
240
241 // create/retrieve item
242 // --------------------
243
244 // create a check list box item
245 wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
246 {
247 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
248 if ( m_windowFont.Ok() )
249 pItem->SetFont(m_windowFont);
250
251 return pItem;
252 }
253
254 // get item (converted to right type)
255 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
256
257 // return item size
258 // ----------------
259 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
260 {
261 if ( wxListBox::MSWOnMeasure(item) ) {
262 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
263
264 // save item height
265 m_nItemHeight = pStruct->itemHeight;
266
267 // add place for the check mark
268 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
269
270 return TRUE;
271 }
272
273 return FALSE;
274 }
275
276 // check items
277 // -----------
278
279 bool wxCheckListBox::IsChecked(size_t uiIndex) const
280 {
281 return GetItem(uiIndex)->IsChecked();
282 }
283
284 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
285 {
286 GetItem(uiIndex)->Check(bCheck);
287 }
288
289 // process events
290 // --------------
291
292 void wxCheckListBox::OnChar(wxKeyEvent& event)
293 {
294 if ( event.KeyCode() == WXK_SPACE )
295 GetItem(GetSelection())->Toggle();
296 else
297 wxListBox::OnChar(event);
298 }
299
300 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
301 {
302 // clicking on the item selects it, clicking on the checkmark toggles
303 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
304 // # better use LB_ITEMFROMPOINT perhaps?
305 size_t nItem = ((size_t)event.GetY()) / m_nItemHeight;
306 if ( nItem < (size_t)m_noItems )
307 GetItem(nItem)->Toggle();
308 //else: it's not an error, just click outside of client zone
309 }
310 else {
311 // implement default behaviour: clicking on the item selects it
312 event.Skip();
313 }
314 }
315
316 #endif
317