]>
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 KB |
42 | #include "wx/msw/checklst.h" |
43 | ||
3d05544e | 44 | #include <windows.h> |
dd3c394a VZ |
45 | #include <windowsx.h> |
46 | ||
9a05fd8d JS |
47 | #ifdef __GNUWIN32__ |
48 | #include "wx/msw/gnuwin32/extra.h" | |
49 | #endif | |
50 | ||
dd3c394a VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // private functions | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // get item (converted to right type) | |
56 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
3d05544e | 57 | |
2bda0e17 KB |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | #if !USE_SHARED_LIBRARY | |
63 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) | |
64 | #endif | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // declaration and implementation of wxCheckListBoxItem class | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | class wxCheckListBoxItem : public wxOwnerDrawn | |
71 | { | |
dd3c394a | 72 | friend class wxCheckListBox; |
2bda0e17 KB |
73 | public: |
74 | // ctor | |
c86f1403 | 75 | wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); |
2bda0e17 KB |
76 | |
77 | // drawing functions | |
78 | virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); | |
79 | ||
80 | // simple accessors | |
81 | bool IsChecked() const { return m_bChecked; } | |
dd3c394a VZ |
82 | void Check(bool bCheck); |
83 | void Toggle() { Check(!IsChecked()); } | |
2bda0e17 KB |
84 | |
85 | private: | |
86 | bool m_bChecked; | |
87 | wxCheckListBox *m_pParent; | |
c86f1403 | 88 | size_t m_nIndex; |
2bda0e17 KB |
89 | }; |
90 | ||
c86f1403 | 91 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) |
2bda0e17 KB |
92 | : wxOwnerDrawn("", TRUE) // checkable |
93 | { | |
94 | m_bChecked = FALSE; | |
95 | m_pParent = pParent; | |
96 | m_nIndex = nIndex; | |
97 | ||
98 | // we don't initialize m_nCheckHeight/Width vars because it's | |
99 | // done in OnMeasure while they are used only in OnDraw and we | |
100 | // know that there will always be OnMeasure before OnDraw | |
101 | ||
102 | // fix appearance | |
103 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); | |
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 | |
149 | RECT rect = { 0, 0, nCheckWidth, nCheckHeight }; | |
150 | ||
151 | #ifdef __WIN32__ | |
2432b92d | 152 | #ifndef __SC__ |
2bda0e17 | 153 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
2432b92d | 154 | #endif |
2bda0e17 KB |
155 | #else |
156 | // In WIN16, draw a cross | |
157 | HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); | |
158 | HPEN whiteBrush = GetStockObject(WHITE_BRUSH); | |
159 | HPEN hPenOld = ::SelectObject(hdcMem, blackPen); | |
160 | HPEN hBrushOld = ::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 | |
45187197 | 173 | BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight, |
2bda0e17 KB |
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); | |
45187197 | 188 | HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH)); |
2bda0e17 KB |
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 | |
dd3c394a | 214 | void wxCheckListBoxItem::Check(bool check) |
45187197 | 215 | { |
dd3c394a | 216 | m_bChecked = check; |
2bda0e17 | 217 | |
dd3c394a VZ |
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, "what does this item do here?" ); | |
225 | ||
226 | m_nIndex = (size_t)index; | |
227 | } | |
2bda0e17 | 228 | |
dd3c394a VZ |
229 | size_t nHeight = m_pParent->GetItemHeight(); |
230 | size_t y = m_nIndex * nHeight; | |
231 | RECT rcUpdate = { 0, y, GetDefaultMarginWidth(), y + nHeight}; | |
232 | InvalidateRect((HWND)m_pParent->GetHWND(), &rcUpdate, FALSE); | |
233 | ||
234 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); | |
235 | event.SetInt(m_nIndex); | |
236 | event.SetEventObject(m_pParent); | |
237 | m_pParent->ProcessCommand(event); | |
2bda0e17 KB |
238 | } |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // implementation of wxCheckListBox class | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | // define event table | |
245 | // ------------------ | |
246 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
247 | EVT_CHAR(wxCheckListBox::OnChar) | |
248 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
249 | END_EVENT_TABLE() | |
250 | ||
251 | // control creation | |
252 | // ---------------- | |
253 | ||
254 | // def ctor: use Create() to really create the control | |
255 | wxCheckListBox::wxCheckListBox() : wxListBox() | |
256 | { | |
257 | } | |
258 | ||
259 | // ctor which creates the associated control | |
debe6624 | 260 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, |
2bda0e17 | 261 | const wxPoint& pos, const wxSize& size, |
debe6624 JS |
262 | int nStrings, const wxString choices[], |
263 | long style, const wxValidator& val, | |
dd3c394a | 264 | const wxString& name) |
2bda0e17 | 265 | : wxListBox() |
2bda0e17 | 266 | { |
dd3c394a VZ |
267 | Create(parent, id, pos, size, nStrings, choices, |
268 | style | wxLB_OWNERDRAW, val, name); | |
269 | } | |
270 | ||
271 | void wxCheckListBox::Delete(int N) | |
272 | { | |
273 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
274 | "invalid index in wxListBox::Delete" ); | |
275 | ||
276 | wxListBox::Delete(N); | |
277 | ||
278 | // free memory | |
279 | delete m_aItems[N]; | |
280 | ||
281 | m_aItems.Remove(N); | |
282 | } | |
283 | ||
284 | void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos) | |
285 | { | |
286 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
287 | "invalid index in wxCheckListBox::InsertItems" ); | |
288 | ||
289 | wxListBox::InsertItems(nItems, items, pos); | |
290 | ||
291 | int i; | |
292 | for ( i = 0; i < nItems; i++ ) { | |
293 | wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i)); | |
294 | pNewItem->SetName(items[i]); | |
295 | m_aItems.Insert(pNewItem, (size_t)(pos + i)); | |
296 | ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem); | |
297 | } | |
2bda0e17 KB |
298 | } |
299 | ||
300 | // create/retrieve item | |
301 | // -------------------- | |
302 | ||
303 | // create a check list box item | |
c86f1403 | 304 | wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex) |
2bda0e17 KB |
305 | { |
306 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); | |
307 | if ( m_windowFont.Ok() ) | |
308 | pItem->SetFont(m_windowFont); | |
309 | ||
310 | return pItem; | |
311 | } | |
312 | ||
2bda0e17 KB |
313 | // return item size |
314 | // ---------------- | |
315 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
316 | { | |
317 | if ( wxListBox::MSWOnMeasure(item) ) { | |
318 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
319 | ||
320 | // save item height | |
321 | m_nItemHeight = pStruct->itemHeight; | |
322 | ||
323 | // add place for the check mark | |
324 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); | |
325 | ||
326 | return TRUE; | |
327 | } | |
328 | ||
329 | return FALSE; | |
330 | } | |
331 | ||
332 | // check items | |
333 | // ----------- | |
334 | ||
c86f1403 | 335 | bool wxCheckListBox::IsChecked(size_t uiIndex) const |
2bda0e17 KB |
336 | { |
337 | return GetItem(uiIndex)->IsChecked(); | |
338 | } | |
339 | ||
c86f1403 | 340 | void wxCheckListBox::Check(size_t uiIndex, bool bCheck) |
2bda0e17 KB |
341 | { |
342 | GetItem(uiIndex)->Check(bCheck); | |
343 | } | |
344 | ||
345 | // process events | |
346 | // -------------- | |
347 | ||
348 | void wxCheckListBox::OnChar(wxKeyEvent& event) | |
349 | { | |
350 | if ( event.KeyCode() == WXK_SPACE ) | |
351 | GetItem(GetSelection())->Toggle(); | |
352 | else | |
353 | wxListBox::OnChar(event); | |
354 | } | |
355 | ||
356 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) | |
357 | { | |
358 | // clicking on the item selects it, clicking on the checkmark toggles | |
c1888b05 | 359 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) { |
dd3c394a | 360 | // FIXME better use LB_ITEMFROMPOINT perhaps? |
c86f1403 VZ |
361 | size_t nItem = ((size_t)event.GetY()) / m_nItemHeight; |
362 | if ( nItem < (size_t)m_noItems ) | |
2bda0e17 KB |
363 | GetItem(nItem)->Toggle(); |
364 | //else: it's not an error, just click outside of client zone | |
365 | } | |
366 | else { | |
367 | // implement default behaviour: clicking on the item selects it | |
368 | event.Skip(); | |
369 | } | |
370 | } | |
1c089c47 JS |
371 | |
372 | #endif | |
373 |