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