]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checklst.cpp
Fixed compile error
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "checklst.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_OWNER_DRAWN
32
33 #ifndef WX_PRECOMP
34 #include "wx/object.h"
35 #include "wx/colour.h"
36 #include "wx/font.h"
37 #include "wx/bitmap.h"
38 #include "wx/window.h"
39 #include "wx/listbox.h"
40 #include "wx/dcmemory.h"
41
42 #include "wx/settings.h"
43
44 #include "wx/log.h"
45 #endif
46
47 #include "wx/ownerdrw.h"
48 #include "wx/checklst.h"
49
50 #include <windows.h>
51 #include <windowsx.h>
52
53 #if defined(__GNUWIN32_OLD__)
54 #include "wx/msw/gnuwin32/extra.h"
55 #endif
56
57 // ----------------------------------------------------------------------------
58 // private functions
59 // ----------------------------------------------------------------------------
60
61 // get item (converted to right type)
62 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
69
70 // ----------------------------------------------------------------------------
71 // declaration and implementation of wxCheckListBoxItem class
72 // ----------------------------------------------------------------------------
73
74 class wxCheckListBoxItem : public wxOwnerDrawn
75 {
76 friend class wxCheckListBox;
77 public:
78 // ctor
79 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
80
81 // drawing functions
82 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
83
84 // simple accessors
85 bool IsChecked() const { return m_bChecked; }
86 void Check(bool bCheck);
87 void Toggle() { Check(!IsChecked()); }
88
89 private:
90 bool m_bChecked;
91 wxCheckListBox *m_pParent;
92 size_t m_nIndex;
93 };
94
95 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
96 : wxOwnerDrawn("", TRUE) // checkable
97 {
98 m_bChecked = FALSE;
99 m_pParent = pParent;
100 m_nIndex = nIndex;
101
102 // we don't initialize m_nCheckHeight/Width vars because it's
103 // done in OnMeasure while they are used only in OnDraw and we
104 // know that there will always be OnMeasure before OnDraw
105
106 // fix appearance
107 SetMarginWidth(GetDefaultMarginWidth());
108 }
109
110 /*
111 * JACS - I've got the owner-draw stuff partially working with WIN16,
112 * with a really horrible-looking cross for wxCheckListBox instead of a
113 * check - could use a bitmap check-mark instead, defined in wx.rc.
114 * Also there's a refresh problem whereby it doesn't always draw the
115 * check until you click to the right of it, which is OK for WIN32.
116 */
117
118 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
119 wxODAction act, wxODStatus stat)
120 {
121 if ( IsChecked() )
122 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
123
124 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
125 // ## using native API for performance and precision
126 size_t nCheckWidth = GetDefaultMarginWidth(),
127 nCheckHeight = m_pParent->GetItemHeight();
128
129 int x = rc.GetX(),
130 y = rc.GetY();
131
132 HDC hdc = (HDC)dc.GetHDC();
133
134 // create pens
135 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
136 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
137 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
138
139 // we erase the 1-pixel border
140 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
141
142 // shift check mark 1 pixel to the right (it looks better like this)
143 x++;
144
145 if ( IsChecked() ) {
146 // first create a monochrome bitmap in a memory DC
147 HDC hdcMem = CreateCompatibleDC(hdc);
148 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
149 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
150
151 // then draw a check mark into it
152 #if defined(__WIN32__) && !defined(__SC__)
153 RECT rect;
154 rect.left = 0;
155 rect.top = 0;
156 rect.right = nCheckWidth;
157 rect.bottom = nCheckHeight;
158
159 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
160 #else
161 // In WIN16, draw a cross
162 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
163 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
164 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
165 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
166 ::SetROP2(hdcMem, R2_COPYPEN);
167 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
168 MoveTo(hdcMem, 0, 0);
169 LineTo(hdcMem, nCheckWidth, nCheckHeight);
170 MoveTo(hdcMem, nCheckWidth, 0);
171 LineTo(hdcMem, 0, nCheckHeight);
172 ::SelectObject(hdcMem, hPenOld);
173 ::SelectObject(hdcMem, hBrushOld);
174 ::DeleteObject(blackPen);
175 #endif
176
177 // finally copy it to screen DC and clean up
178 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
179 hdcMem, 0, 0, SRCCOPY);
180
181 SelectObject(hdcMem, hbmpOld);
182 DeleteObject(hbmpCheck);
183 DeleteDC(hdcMem);
184 }
185
186 // now we draw the smaller rectangle
187 y++;
188 nCheckWidth -= 2;
189 nCheckHeight -= 2;
190
191 // draw hollow gray rectangle
192 (void)SelectObject(hdc, hpenGray);
193 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
194 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
195
196 // clean up
197 (void)SelectObject(hdc, hpenPrev);
198 (void)SelectObject(hdc, hbrPrev);
199
200 DeleteObject(hpenBack);
201 DeleteObject(hpenGray);
202
203 /*
204 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
205
206 if ( IsChecked() ) {
207 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
208 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
209 }
210 */
211
212 return TRUE;
213 }
214
215 return FALSE;
216 }
217
218 // change the state of the item and redraw it
219 void wxCheckListBoxItem::Check(bool check)
220 {
221 m_bChecked = check;
222
223 // index may be chanegd because new items were added/deleted
224 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
225 {
226 // update it
227 int index = m_pParent->GetItemIndex(this);
228
229 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
230
231 m_nIndex = (size_t)index;
232 }
233
234 HWND hwndListbox = (HWND)m_pParent->GetHWND();
235
236 #ifdef __WIN32__
237 RECT rcUpdate;
238
239 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
240 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
241 {
242 wxLogDebug(wxT("LB_GETITEMRECT failed"));
243 }
244 #else // Win16
245 // FIXME this doesn't work if the listbox is scrolled!
246 size_t nHeight = m_pParent->GetItemHeight();
247 size_t y = m_nIndex * nHeight;
248 RECT rcUpdate ;
249 rcUpdate.left = 0 ;
250 rcUpdate.top = y ;
251 rcUpdate.right = GetDefaultMarginWidth() ;
252 rcUpdate.bottom = y + nHeight ;
253 #endif // Win32/16
254
255 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
256
257 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
258 event.SetInt(m_nIndex);
259 event.SetEventObject(m_pParent);
260 m_pParent->ProcessCommand(event);
261 }
262
263 // ----------------------------------------------------------------------------
264 // implementation of wxCheckListBox class
265 // ----------------------------------------------------------------------------
266
267 // define event table
268 // ------------------
269 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
270 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
271 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
272 END_EVENT_TABLE()
273
274 // control creation
275 // ----------------
276
277 // def ctor: use Create() to really create the control
278 wxCheckListBox::wxCheckListBox()
279 {
280 }
281
282 // ctor which creates the associated control
283 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
284 const wxPoint& pos, const wxSize& size,
285 int nStrings, const wxString choices[],
286 long style, const wxValidator& val,
287 const wxString& name)
288 {
289 Create(parent, id, pos, size, nStrings, choices,
290 style | wxLB_OWNERDRAW, val, name);
291 }
292
293 void wxCheckListBox::Delete(int N)
294 {
295 wxCHECK_RET( N >= 0 && N < m_noItems,
296 wxT("invalid index in wxListBox::Delete") );
297
298 wxListBox::Delete(N);
299
300 // free memory
301 delete m_aItems[N];
302
303 m_aItems.RemoveAt(N);
304 }
305
306 bool wxCheckListBox::SetFont( const wxFont &font )
307 {
308 size_t i;
309 for ( i = 0; i < m_aItems.GetCount(); i++ )
310 m_aItems[i]->SetFont(font);
311
312 wxListBox::SetFont(font);
313
314 return TRUE;
315 }
316
317 // create/retrieve item
318 // --------------------
319
320 // create a check list box item
321 wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
322 {
323 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
324 return pItem;
325 }
326
327 // return item size
328 // ----------------
329 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
330 {
331 if ( wxListBox::MSWOnMeasure(item) ) {
332 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
333
334 // save item height
335 m_nItemHeight = pStruct->itemHeight;
336
337 // add place for the check mark
338 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
339
340 return TRUE;
341 }
342
343 return FALSE;
344 }
345
346 // check items
347 // -----------
348
349 bool wxCheckListBox::IsChecked(size_t uiIndex) const
350 {
351 wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") );
352
353 return GetItem(uiIndex)->IsChecked();
354 }
355
356 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
357 {
358 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
359
360 GetItem(uiIndex)->Check(bCheck);
361 }
362
363 // process events
364 // --------------
365
366 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
367 {
368 // what do we do?
369 enum
370 {
371 None,
372 Toggle,
373 Set,
374 Clear
375 } oper;
376
377 switch ( event.KeyCode() )
378 {
379 case WXK_SPACE:
380 oper = Toggle;
381 break;
382
383 case WXK_NUMPAD_ADD:
384 case '+':
385 oper = Set;
386 break;
387
388 case WXK_NUMPAD_SUBTRACT:
389 case '-':
390 oper = Clear;
391 break;
392
393 default:
394 oper = None;
395 }
396
397 if ( oper != None )
398 {
399 wxArrayInt selections;
400 int count;
401 if ( HasMultipleSelection() )
402 {
403 count = GetSelections(selections);
404 }
405 else
406 {
407 count = 1;
408 selections.Add(GetSelection());
409 }
410
411 for ( int i = 0; i < count; i++ )
412 {
413 wxCheckListBoxItem *item = GetItem(selections[i]);
414 if ( !item )
415 {
416 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
417 continue;
418 }
419
420 switch ( oper )
421 {
422 case Toggle:
423 item->Toggle();
424 break;
425
426 case Set:
427 case Clear:
428 item->Check( oper == Set );
429 break;
430
431 default:
432 wxFAIL_MSG( _T("what should this key do?") );
433 }
434 }
435 }
436 else // nothing to do
437 {
438 event.Skip();
439 }
440 }
441
442 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
443 {
444 // clicking on the item selects it, clicking on the checkmark toggles
445 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
446 #ifdef __WIN32__
447 size_t nItem = (size_t)::SendMessage
448 (
449 (HWND)GetHWND(),
450 LB_ITEMFROMPOINT,
451 0,
452 MAKELPARAM(event.GetX(), event.GetY())
453 );
454 #else // Win16
455 // FIXME this doesn't work when the listbox is scrolled!
456 size_t nItem = ((size_t)event.GetY()) / m_nItemHeight;
457 #endif // Win32/16
458
459 if ( nItem < (size_t)m_noItems )
460 GetItem(nItem)->Toggle();
461 //else: it's not an error, just click outside of client zone
462 }
463 else {
464 // implement default behaviour: clicking on the item selects it
465 event.Skip();
466 }
467 }
468
469 #endif
470