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