]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Resubmitted binary files that may been corrupted
[wxWidgets.git] / src / msw / checklst.cpp
CommitLineData
2bda0e17
KB
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// headers & declarations
14// ============================================================================
15
16#ifdef __GNUG__
17#pragma implementation "checklst.h"
18#endif
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#include <windows.h>
28
29#include "wx/ownerdrw.h"
30#include "wx/msw/checklst.h"
31
32// ============================================================================
33// implementation
34// ============================================================================
35
36#if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
38#endif
39
40// ----------------------------------------------------------------------------
41// declaration and implementation of wxCheckListBoxItem class
42// ----------------------------------------------------------------------------
43
44class wxCheckListBoxItem : public wxOwnerDrawn
45{
46public:
47 // ctor
48 wxCheckListBoxItem(wxCheckListBox *pParent, uint nIndex);
49
50 // drawing functions
51 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
52
53 // simple accessors
54 bool IsChecked() const { return m_bChecked; }
55 void Check(bool bCheck) { m_bChecked = bCheck; }
56 void Toggle();
57
58private:
59 bool m_bChecked;
60 wxCheckListBox *m_pParent;
61 uint m_nIndex;
62};
63
64wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, uint nIndex)
65 : wxOwnerDrawn("", TRUE) // checkable
66{
67 m_bChecked = FALSE;
68 m_pParent = pParent;
69 m_nIndex = nIndex;
70
71 // we don't initialize m_nCheckHeight/Width vars because it's
72 // done in OnMeasure while they are used only in OnDraw and we
73 // know that there will always be OnMeasure before OnDraw
74
75 // fix appearance
76 SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
77 SetMarginWidth(GetDefaultMarginWidth());
78}
79
80/*
81 * JACS - I've got the owner-draw stuff partially working with WIN16,
82 * with a really horrible-looking cross for wxCheckListBox instead of a
83 * check - could use a bitmap check-mark instead, defined in wx.rc.
84 * Also there's a refresh problem whereby it doesn't always draw the
85 * check until you click to the right of it, which is OK for WIN32.
86 */
87
88bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
89 wxODAction act, wxODStatus stat)
90{
91 if ( IsChecked() )
92 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
93
94 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
95 // ## using native API for performance and precision
96 uint nCheckWidth = GetDefaultMarginWidth(),
97 nCheckHeight = m_pParent->GetItemHeight();
98
99 int x = rc.GetX(),
100 y = rc.GetY();
101
102 HDC hdc = (HDC)dc.GetHDC();
103
104 // create pens
105 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
106 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
107 hpenPrev = SelectObject(hdc, hpenBack);
108
109 // we erase the 1-pixel border
110 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
111
112 // shift check mark 1 pixel to the right (it looks better like this)
113 x++;
114
115 if ( IsChecked() ) {
116 // first create a monochrome bitmap in a memory DC
117 HDC hdcMem = CreateCompatibleDC(hdc);
118 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
119 HBITMAP hbmpOld = SelectObject(hdcMem, hbmpCheck);
120
121 // then draw a check mark into it
122 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
123
124#ifdef __WIN32__
125 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
126#else
127 // In WIN16, draw a cross
128 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
129 HPEN whiteBrush = GetStockObject(WHITE_BRUSH);
130 HPEN hPenOld = ::SelectObject(hdcMem, blackPen);
131 HPEN hBrushOld = ::SelectObject(hdcMem, whiteBrush);
132 ::SetROP2(hdcMem, R2_COPYPEN);
133 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
134 MoveTo(hdcMem, 0, 0);
135 LineTo(hdcMem, nCheckWidth, nCheckHeight);
136 MoveTo(hdcMem, nCheckWidth, 0);
137 LineTo(hdcMem, 0, nCheckHeight);
138 ::SelectObject(hdcMem, hPenOld);
139 ::SelectObject(hdcMem, hBrushOld);
140 ::DeleteObject(blackPen);
141#endif
142
143 // finally copy it to screen DC and clean up
144 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
145 hdcMem, 0, 0, SRCCOPY);
146
147 SelectObject(hdcMem, hbmpOld);
148 DeleteObject(hbmpCheck);
149 DeleteDC(hdcMem);
150 }
151
152 // now we draw the smaller rectangle
153 y++;
154 nCheckWidth -= 2;
155 nCheckHeight -= 2;
156
157 // draw hollow gray rectangle
158 (void)SelectObject(hdc, hpenGray);
159 HBRUSH hbrPrev = SelectObject(hdc, GetStockObject(NULL_BRUSH));
160 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
161
162 // clean up
163 (void)SelectObject(hdc, hpenPrev);
164 (void)SelectObject(hdc, hbrPrev);
165
166 DeleteObject(hpenBack);
167 DeleteObject(hpenGray);
168
169 /*
170 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
171
172 if ( IsChecked() ) {
173 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
174 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
175 }
176 */
177
178 return TRUE;
179 }
180
181 return FALSE;
182}
183
184// change the state of the item and redraw it
185void wxCheckListBoxItem::Toggle()
186{
187 m_bChecked = !m_bChecked;
188
189 uint nHeight = m_pParent->GetItemHeight();
190 uint y = m_nIndex * nHeight;
191 RECT rcUpdate = { 0, y, GetDefaultMarginWidth(), y + nHeight};
192 InvalidateRect((HWND)m_pParent->GetHWND(), &rcUpdate, FALSE);
193
194 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
195 event.SetInt(m_nIndex);
196 event.SetEventObject(m_pParent);
197 m_pParent->ProcessCommand(event);
198}
199
200// ----------------------------------------------------------------------------
201// implementation of wxCheckListBox class
202// ----------------------------------------------------------------------------
203
204// define event table
205// ------------------
206BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
207 EVT_CHAR(wxCheckListBox::OnChar)
208 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
209END_EVENT_TABLE()
210
211// control creation
212// ----------------
213
214// def ctor: use Create() to really create the control
215wxCheckListBox::wxCheckListBox() : wxListBox()
216{
217}
218
219// ctor which creates the associated control
220wxCheckListBox::wxCheckListBox(wxWindow *parent, const wxWindowID id,
221 const wxPoint& pos, const wxSize& size,
222 const int nStrings, const wxString choices[],
223 const long style, const wxValidator& val,
224 const wxString& name) // , const wxFont& font)
225 // don't use ctor with arguments! we must call Create()
226 // ourselves from here.
227 : wxListBox()
228// , m_font(font)
229{
230 Create(parent, id, pos, size, nStrings, choices, style|wxLB_OWNERDRAW, val, name);
231}
232
233// create/retrieve item
234// --------------------
235
236// create a check list box item
237wxOwnerDrawn *wxCheckListBox::CreateItem(uint nIndex)
238{
239 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
240 if ( m_windowFont.Ok() )
241 pItem->SetFont(m_windowFont);
242
243 return pItem;
244}
245
246// get item (converted to right type)
247#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
248
249// return item size
250// ----------------
251bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
252{
253 if ( wxListBox::MSWOnMeasure(item) ) {
254 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
255
256 // save item height
257 m_nItemHeight = pStruct->itemHeight;
258
259 // add place for the check mark
260 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
261
262 return TRUE;
263 }
264
265 return FALSE;
266}
267
268// check items
269// -----------
270
271bool wxCheckListBox::IsChecked(uint uiIndex) const
272{
273 return GetItem(uiIndex)->IsChecked();
274}
275
276void wxCheckListBox::Check(uint uiIndex, bool bCheck)
277{
278 GetItem(uiIndex)->Check(bCheck);
279}
280
281// process events
282// --------------
283
284void wxCheckListBox::OnChar(wxKeyEvent& event)
285{
286 if ( event.KeyCode() == WXK_SPACE )
287 GetItem(GetSelection())->Toggle();
288 else
289 wxListBox::OnChar(event);
290}
291
292void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
293{
294 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 295 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
2bda0e17
KB
296 // # better use LB_ITEMFROMPOINT perhaps?
297 uint nItem = ((uint)event.GetY()) / m_nItemHeight;
298 if ( nItem < (uint)m_noItems )
299 GetItem(nItem)->Toggle();
300 //else: it's not an error, just click outside of client zone
301 }
302 else {
303 // implement default behaviour: clicking on the item selects it
304 event.Skip();
305 }
306}