]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: Vadim Zeitlin | |
45187197 | 5 | // Modified by: |
2bda0e17 KB |
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 | ||
dd3c394a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
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 | ||
47d67540 | 31 | #if wxUSE_OWNER_DRAWN |
1c089c47 | 32 | |
2432b92d JS |
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" | |
2bda0e17 | 39 | #include "wx/ownerdrw.h" |
2432b92d JS |
40 | #include "wx/settings.h" |
41 | #include "wx/dcmemory.h" | |
2bda0e17 | 42 | #include "wx/msw/checklst.h" |
6776a0b2 | 43 | #include "wx/log.h" |
2bda0e17 | 44 | |
3d05544e | 45 | #include <windows.h> |
dd3c394a VZ |
46 | #include <windowsx.h> |
47 | ||
65fd5cb0 | 48 | #if defined(__GNUWIN32__) && !defined(wxUSE_NORLANDER_HEADERS) |
9a05fd8d JS |
49 | #include "wx/msw/gnuwin32/extra.h" |
50 | #endif | |
51 | ||
dd3c394a VZ |
52 | // ---------------------------------------------------------------------------- |
53 | // private functions | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // get item (converted to right type) | |
57 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
3d05544e | 58 | |
2bda0e17 KB |
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 | { | |
dd3c394a | 73 | friend class wxCheckListBox; |
2bda0e17 KB |
74 | public: |
75 | // ctor | |
c86f1403 | 76 | wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); |
2bda0e17 KB |
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; } | |
dd3c394a VZ |
83 | void Check(bool bCheck); |
84 | void Toggle() { Check(!IsChecked()); } | |
2bda0e17 KB |
85 | |
86 | private: | |
87 | bool m_bChecked; | |
88 | wxCheckListBox *m_pParent; | |
c86f1403 | 89 | size_t m_nIndex; |
2bda0e17 KB |
90 | }; |
91 | ||
c86f1403 | 92 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) |
2bda0e17 KB |
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 | |
2bda0e17 KB |
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 | ||
45187197 | 115 | bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc, |
2bda0e17 KB |
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 | |
c86f1403 | 123 | size_t nCheckWidth = GetDefaultMarginWidth(), |
2bda0e17 | 124 | nCheckHeight = m_pParent->GetItemHeight(); |
45187197 RD |
125 | |
126 | int x = rc.GetX(), | |
2bda0e17 KB |
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)), | |
45187197 | 134 | hpenPrev = (HPEN)SelectObject(hdc, hpenBack); |
2bda0e17 KB |
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); | |
45187197 | 146 | HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck); |
2bda0e17 KB |
147 | |
148 | // then draw a check mark into it | |
197dd9af PA |
149 | RECT rect ; |
150 | rect.left = 0 ; | |
151 | rect.top = 0 ; | |
152 | rect.right = nCheckWidth ; | |
153 | rect.bottom = nCheckHeight ; | |
2bda0e17 KB |
154 | |
155 | #ifdef __WIN32__ | |
2432b92d | 156 | #ifndef __SC__ |
2bda0e17 | 157 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
2432b92d | 158 | #endif |
2bda0e17 KB |
159 | #else |
160 | // In WIN16, draw a cross | |
161 | HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); | |
27a9bd48 PA |
162 | HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH); |
163 | HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen); | |
164 | HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush); | |
2bda0e17 KB |
165 | ::SetROP2(hdcMem, R2_COPYPEN); |
166 | Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight); | |
167 | MoveTo(hdcMem, 0, 0); | |
168 | LineTo(hdcMem, nCheckWidth, nCheckHeight); | |
169 | MoveTo(hdcMem, nCheckWidth, 0); | |
170 | LineTo(hdcMem, 0, nCheckHeight); | |
171 | ::SelectObject(hdcMem, hPenOld); | |
172 | ::SelectObject(hdcMem, hBrushOld); | |
173 | ::DeleteObject(blackPen); | |
174 | #endif | |
175 | ||
176 | // finally copy it to screen DC and clean up | |
45187197 | 177 | BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight, |
2bda0e17 KB |
178 | hdcMem, 0, 0, SRCCOPY); |
179 | ||
180 | SelectObject(hdcMem, hbmpOld); | |
181 | DeleteObject(hbmpCheck); | |
182 | DeleteDC(hdcMem); | |
183 | } | |
184 | ||
185 | // now we draw the smaller rectangle | |
186 | y++; | |
187 | nCheckWidth -= 2; | |
188 | nCheckHeight -= 2; | |
189 | ||
190 | // draw hollow gray rectangle | |
191 | (void)SelectObject(hdc, hpenGray); | |
45187197 | 192 | HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH)); |
2bda0e17 KB |
193 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); |
194 | ||
195 | // clean up | |
196 | (void)SelectObject(hdc, hpenPrev); | |
197 | (void)SelectObject(hdc, hbrPrev); | |
198 | ||
199 | DeleteObject(hpenBack); | |
200 | DeleteObject(hpenGray); | |
201 | ||
202 | /* | |
203 | dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight); | |
204 | ||
205 | if ( IsChecked() ) { | |
206 | dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight); | |
207 | dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y); | |
208 | } | |
209 | */ | |
210 | ||
211 | return TRUE; | |
212 | } | |
213 | ||
214 | return FALSE; | |
215 | } | |
216 | ||
217 | // change the state of the item and redraw it | |
dd3c394a | 218 | void wxCheckListBoxItem::Check(bool check) |
45187197 | 219 | { |
dd3c394a | 220 | m_bChecked = check; |
2bda0e17 | 221 | |
dd3c394a VZ |
222 | // index may be chanegd because new items were added/deleted |
223 | if ( m_pParent->GetItemIndex(this) != (int)m_nIndex ) | |
224 | { | |
225 | // update it | |
226 | int index = m_pParent->GetItemIndex(this); | |
227 | ||
223d09f6 | 228 | wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") ); |
dd3c394a VZ |
229 | |
230 | m_nIndex = (size_t)index; | |
231 | } | |
2bda0e17 | 232 | |
c4dbfe14 VZ |
233 | HWND hwndListbox = (HWND)m_pParent->GetHWND(); |
234 | ||
235 | #ifdef __WIN32__ | |
236 | RECT rcUpdate; | |
0c35333e | 237 | |
c4dbfe14 VZ |
238 | if ( ::SendMessage(hwndListbox, LB_GETITEMRECT, |
239 | m_nIndex, (LPARAM)&rcUpdate) == LB_ERR ) | |
240 | { | |
223d09f6 | 241 | wxLogDebug(wxT("LB_GETITEMRECT failed")); |
c4dbfe14 VZ |
242 | } |
243 | #else // Win16 | |
244 | // FIXME this doesn't work if the listbox is scrolled! | |
245 | size_t nHeight = m_pParent->GetItemHeight(); | |
246 | size_t y = m_nIndex * nHeight; | |
197dd9af PA |
247 | RECT rcUpdate ; |
248 | rcUpdate.left = 0 ; | |
249 | rcUpdate.top = y ; | |
250 | rcUpdate.right = GetDefaultMarginWidth() ; | |
251 | rcUpdate.bottom = y + nHeight ; | |
c4dbfe14 VZ |
252 | #endif // Win32/16 |
253 | ||
254 | InvalidateRect(hwndListbox, &rcUpdate, FALSE); | |
0c35333e | 255 | |
dd3c394a VZ |
256 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); |
257 | event.SetInt(m_nIndex); | |
258 | event.SetEventObject(m_pParent); | |
259 | m_pParent->ProcessCommand(event); | |
2bda0e17 KB |
260 | } |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // implementation of wxCheckListBox class | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | // define event table | |
267 | // ------------------ | |
268 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
269 | EVT_CHAR(wxCheckListBox::OnChar) | |
270 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
271 | END_EVENT_TABLE() | |
272 | ||
273 | // control creation | |
274 | // ---------------- | |
275 | ||
276 | // def ctor: use Create() to really create the control | |
277 | wxCheckListBox::wxCheckListBox() : wxListBox() | |
278 | { | |
279 | } | |
280 | ||
281 | // ctor which creates the associated control | |
debe6624 | 282 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, |
2bda0e17 | 283 | const wxPoint& pos, const wxSize& size, |
debe6624 JS |
284 | int nStrings, const wxString choices[], |
285 | long style, const wxValidator& val, | |
dd3c394a | 286 | const wxString& name) |
2bda0e17 | 287 | : wxListBox() |
2bda0e17 | 288 | { |
dd3c394a VZ |
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, | |
223d09f6 | 296 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a VZ |
297 | |
298 | wxListBox::Delete(N); | |
299 | ||
300 | // free memory | |
301 | delete m_aItems[N]; | |
302 | ||
303 | m_aItems.Remove(N); | |
304 | } | |
305 | ||
306 | void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos) | |
307 | { | |
308 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
223d09f6 | 309 | wxT("invalid index in wxCheckListBox::InsertItems") ); |
dd3c394a VZ |
310 | |
311 | wxListBox::InsertItems(nItems, items, pos); | |
312 | ||
313 | int i; | |
314 | for ( i = 0; i < nItems; i++ ) { | |
315 | wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i)); | |
316 | pNewItem->SetName(items[i]); | |
07cf98cb VZ |
317 | pNewItem->SetFont(GetFont()); |
318 | ||
dd3c394a | 319 | m_aItems.Insert(pNewItem, (size_t)(pos + i)); |
07cf98cb | 320 | |
dd3c394a VZ |
321 | ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem); |
322 | } | |
2bda0e17 KB |
323 | } |
324 | ||
0c35333e RD |
325 | |
326 | bool wxCheckListBox::SetFont( const wxFont &font ) | |
327 | { | |
328 | size_t i; | |
07cf98cb | 329 | for ( i = 0; i < m_aItems.GetCount(); i++ ) |
0c35333e | 330 | m_aItems[i]->SetFont(font); |
07cf98cb | 331 | |
0c35333e | 332 | wxListBox::SetFont(font); |
07cf98cb | 333 | |
0c35333e RD |
334 | return TRUE; |
335 | } | |
336 | ||
2bda0e17 KB |
337 | // create/retrieve item |
338 | // -------------------- | |
339 | ||
340 | // create a check list box item | |
c86f1403 | 341 | wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex) |
2bda0e17 KB |
342 | { |
343 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); | |
2bda0e17 KB |
344 | return pItem; |
345 | } | |
346 | ||
2bda0e17 KB |
347 | // return item size |
348 | // ---------------- | |
349 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
350 | { | |
351 | if ( wxListBox::MSWOnMeasure(item) ) { | |
352 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
353 | ||
354 | // save item height | |
355 | m_nItemHeight = pStruct->itemHeight; | |
356 | ||
357 | // add place for the check mark | |
358 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); | |
359 | ||
360 | return TRUE; | |
361 | } | |
362 | ||
363 | return FALSE; | |
364 | } | |
365 | ||
366 | // check items | |
367 | // ----------- | |
368 | ||
c86f1403 | 369 | bool wxCheckListBox::IsChecked(size_t uiIndex) const |
2bda0e17 KB |
370 | { |
371 | return GetItem(uiIndex)->IsChecked(); | |
372 | } | |
373 | ||
c86f1403 | 374 | void wxCheckListBox::Check(size_t uiIndex, bool bCheck) |
2bda0e17 KB |
375 | { |
376 | GetItem(uiIndex)->Check(bCheck); | |
377 | } | |
378 | ||
379 | // process events | |
380 | // -------------- | |
381 | ||
382 | void wxCheckListBox::OnChar(wxKeyEvent& event) | |
383 | { | |
384 | if ( event.KeyCode() == WXK_SPACE ) | |
385 | GetItem(GetSelection())->Toggle(); | |
386 | else | |
a23fd0e1 | 387 | event.Skip(); |
2bda0e17 KB |
388 | } |
389 | ||
390 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) | |
391 | { | |
392 | // clicking on the item selects it, clicking on the checkmark toggles | |
c1888b05 | 393 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) { |
c4dbfe14 VZ |
394 | #ifdef __WIN32__ |
395 | size_t nItem = (size_t)::SendMessage | |
396 | ( | |
397 | (HWND)GetHWND(), | |
398 | LB_ITEMFROMPOINT, | |
399 | 0, | |
400 | MAKELPARAM(event.GetX(), event.GetY()) | |
401 | ); | |
402 | #else // Win16 | |
403 | // FIXME this doesn't work when the listbox is scrolled! | |
404 | size_t nItem = ((size_t)event.GetY()) / m_nItemHeight; | |
405 | #endif // Win32/16 | |
406 | ||
c86f1403 | 407 | if ( nItem < (size_t)m_noItems ) |
2bda0e17 KB |
408 | GetItem(nItem)->Toggle(); |
409 | //else: it's not an error, just click outside of client zone | |
410 | } | |
411 | else { | |
412 | // implement default behaviour: clicking on the item selects it | |
413 | event.Skip(); | |
414 | } | |
415 | } | |
1c089c47 JS |
416 | |
417 | #endif | |
418 |