]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Don't send a focus event if the window is a wxTextCtrl, since it sends
[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
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
ae090fdb 48#if defined(__GNUWIN32_OLD__)
c42404a5 49 #include "wx/msw/gnuwin32/extra.h"
9a05fd8d
JS
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
2bda0e17 63 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
2bda0e17
KB
64
65// ----------------------------------------------------------------------------
66// declaration and implementation of wxCheckListBoxItem class
67// ----------------------------------------------------------------------------
68
69class wxCheckListBoxItem : public wxOwnerDrawn
70{
dd3c394a 71friend class wxCheckListBox;
2bda0e17
KB
72public:
73 // ctor
c86f1403 74 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17
KB
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; }
dd3c394a
VZ
81 void Check(bool bCheck);
82 void Toggle() { Check(!IsChecked()); }
2bda0e17
KB
83
84private:
85 bool m_bChecked;
86 wxCheckListBox *m_pParent;
c86f1403 87 size_t m_nIndex;
2bda0e17
KB
88};
89
c86f1403 90wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
2bda0e17
KB
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
2bda0e17
KB
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
45187197 113bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
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
c86f1403 121 size_t nCheckWidth = GetDefaultMarginWidth(),
2bda0e17 122 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
123
124 int x = rc.GetX(),
2bda0e17
KB
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)),
45187197 132 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
2bda0e17
KB
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);
45187197 144 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
2bda0e17
KB
145
146 // then draw a check mark into it
ee6e1b1d
VZ
147#if defined(__WIN32__) && !defined(__SC__)
148 RECT rect;
149 rect.left = 0;
150 rect.top = 0;
151 rect.right = nCheckWidth;
152 rect.bottom = nCheckHeight;
153
2bda0e17
KB
154 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
155#else
156 // In WIN16, draw a cross
157 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
27a9bd48
PA
158 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
159 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
160 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
2bda0e17
KB
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 214void 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
223d09f6 224 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
225
226 m_nIndex = (size_t)index;
227 }
2bda0e17 228
c4dbfe14
VZ
229 HWND hwndListbox = (HWND)m_pParent->GetHWND();
230
231 #ifdef __WIN32__
232 RECT rcUpdate;
0c35333e 233
c4dbfe14
VZ
234 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
235 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
236 {
223d09f6 237 wxLogDebug(wxT("LB_GETITEMRECT failed"));
c4dbfe14
VZ
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;
197dd9af
PA
243 RECT rcUpdate ;
244 rcUpdate.left = 0 ;
245 rcUpdate.top = y ;
246 rcUpdate.right = GetDefaultMarginWidth() ;
247 rcUpdate.bottom = y + nHeight ;
c4dbfe14
VZ
248 #endif // Win32/16
249
250 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
0c35333e 251
dd3c394a
VZ
252 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
253 event.SetInt(m_nIndex);
254 event.SetEventObject(m_pParent);
255 m_pParent->ProcessCommand(event);
2bda0e17
KB
256}
257
258// ----------------------------------------------------------------------------
259// implementation of wxCheckListBox class
260// ----------------------------------------------------------------------------
261
262// define event table
263// ------------------
264BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
265 EVT_CHAR(wxCheckListBox::OnChar)
266 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
267END_EVENT_TABLE()
268
269// control creation
270// ----------------
271
272// def ctor: use Create() to really create the control
273wxCheckListBox::wxCheckListBox() : wxListBox()
274{
275}
276
277// ctor which creates the associated control
debe6624 278wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 279 const wxPoint& pos, const wxSize& size,
debe6624
JS
280 int nStrings, const wxString choices[],
281 long style, const wxValidator& val,
dd3c394a 282 const wxString& name)
2bda0e17 283 : wxListBox()
2bda0e17 284{
dd3c394a
VZ
285 Create(parent, id, pos, size, nStrings, choices,
286 style | wxLB_OWNERDRAW, val, name);
287}
288
289void wxCheckListBox::Delete(int N)
290{
291 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 292 wxT("invalid index in wxListBox::Delete") );
dd3c394a
VZ
293
294 wxListBox::Delete(N);
295
296 // free memory
297 delete m_aItems[N];
298
b54e41c5 299 m_aItems.RemoveAt(N);
dd3c394a
VZ
300}
301
302void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos)
303{
304 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
223d09f6 305 wxT("invalid index in wxCheckListBox::InsertItems") );
dd3c394a
VZ
306
307 wxListBox::InsertItems(nItems, items, pos);
308
309 int i;
310 for ( i = 0; i < nItems; i++ ) {
311 wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i));
312 pNewItem->SetName(items[i]);
07cf98cb
VZ
313 pNewItem->SetFont(GetFont());
314
dd3c394a 315 m_aItems.Insert(pNewItem, (size_t)(pos + i));
07cf98cb 316
dd3c394a
VZ
317 ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
318 }
2bda0e17
KB
319}
320
0c35333e
RD
321
322bool wxCheckListBox::SetFont( const wxFont &font )
323{
324 size_t i;
07cf98cb 325 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 326 m_aItems[i]->SetFont(font);
07cf98cb 327
0c35333e 328 wxListBox::SetFont(font);
07cf98cb 329
0c35333e
RD
330 return TRUE;
331}
332
2bda0e17
KB
333// create/retrieve item
334// --------------------
335
336// create a check list box item
c86f1403 337wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
2bda0e17
KB
338{
339 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
340 return pItem;
341}
342
2bda0e17
KB
343// return item size
344// ----------------
345bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
346{
347 if ( wxListBox::MSWOnMeasure(item) ) {
348 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
349
350 // save item height
351 m_nItemHeight = pStruct->itemHeight;
352
353 // add place for the check mark
354 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
355
356 return TRUE;
357 }
358
359 return FALSE;
360}
361
362// check items
363// -----------
364
c86f1403 365bool wxCheckListBox::IsChecked(size_t uiIndex) const
2bda0e17
KB
366{
367 return GetItem(uiIndex)->IsChecked();
368}
369
c86f1403 370void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
2bda0e17
KB
371{
372 GetItem(uiIndex)->Check(bCheck);
373}
374
375// process events
376// --------------
377
378void wxCheckListBox::OnChar(wxKeyEvent& event)
379{
380 if ( event.KeyCode() == WXK_SPACE )
381 GetItem(GetSelection())->Toggle();
382 else
a23fd0e1 383 event.Skip();
2bda0e17
KB
384}
385
386void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
387{
388 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 389 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
c4dbfe14
VZ
390 #ifdef __WIN32__
391 size_t nItem = (size_t)::SendMessage
392 (
393 (HWND)GetHWND(),
394 LB_ITEMFROMPOINT,
395 0,
396 MAKELPARAM(event.GetX(), event.GetY())
397 );
398 #else // Win16
399 // FIXME this doesn't work when the listbox is scrolled!
400 size_t nItem = ((size_t)event.GetY()) / m_nItemHeight;
401 #endif // Win32/16
402
c86f1403 403 if ( nItem < (size_t)m_noItems )
2bda0e17
KB
404 GetItem(nItem)->Toggle();
405 //else: it's not an error, just click outside of client zone
406 }
407 else {
408 // implement default behaviour: clicking on the item selects it
409 event.Skip();
410 }
411}
1c089c47
JS
412
413#endif
414