]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Use font encoding
[wxWidgets.git] / src / msw / checklst.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
8228b893 2// Name: src/msw/checklst.cpp
2bda0e17
KB
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>
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
dd3c394a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
e6e83933 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
eecdb000 27#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
1c089c47 28
e6e83933
WS
29#include "wx/checklst.h"
30
d90879fa 31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapwin.h"
d90879fa
VZ
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/dcmemory.h"
d90879fa 40 #include "wx/settings.h"
d90879fa
VZ
41 #include "wx/log.h"
42#endif
43
2bda0e17 44#include "wx/ownerdrw.h"
2bda0e17 45
dd3c394a
VZ
46#include <windowsx.h>
47
4676948b
JS
48#include "wx/msw/private.h"
49
dd3c394a
VZ
50// ----------------------------------------------------------------------------
51// private functions
52// ----------------------------------------------------------------------------
53
54// get item (converted to right type)
55#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
3d05544e 56
2bda0e17 57// ============================================================================
6463b9f5 58// implementation
2bda0e17
KB
59// ============================================================================
60
a2825c34
SC
61
62#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
63WX_DEFINE_FLAGS( wxCheckListBoxStyle )
64
3ff066a4 65wxBEGIN_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
66 // new style border flags, we put them first to
67 // use them for streaming out
3ff066a4
SC
68 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
69 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
70 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
71 wxFLAGS_MEMBER(wxBORDER_RAISED)
72 wxFLAGS_MEMBER(wxBORDER_STATIC)
73 wxFLAGS_MEMBER(wxBORDER_NONE)
02b7b6b0 74
bc9fb572 75 // old style border flags
3ff066a4
SC
76 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
77 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
78 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
79 wxFLAGS_MEMBER(wxRAISED_BORDER)
80 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 81 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
82
83 // standard window styles
3ff066a4
SC
84 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
85 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
86 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
87 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 88 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
89 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
90 wxFLAGS_MEMBER(wxVSCROLL)
91 wxFLAGS_MEMBER(wxHSCROLL)
92
93 wxFLAGS_MEMBER(wxLB_SINGLE)
94 wxFLAGS_MEMBER(wxLB_MULTIPLE)
95 wxFLAGS_MEMBER(wxLB_EXTENDED)
96 wxFLAGS_MEMBER(wxLB_HSCROLL)
97 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
98 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
99 wxFLAGS_MEMBER(wxLB_SORT)
a2825c34 100 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
3ff066a4
SC
101
102wxEND_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
103
104IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
105
3ff066a4 106wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
02b7b6b0 107 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
af498247 108 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 109wxEND_PROPERTIES_TABLE()
bc9fb572 110
a2825c34
SC
111wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
112wxEND_HANDLERS_TABLE()
113
02b7b6b0 114wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
a2825c34 115
bc9fb572 116#else
fd7ab28c 117IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
bc9fb572 118#endif
2bda0e17
KB
119
120// ----------------------------------------------------------------------------
121// declaration and implementation of wxCheckListBoxItem class
122// ----------------------------------------------------------------------------
123
124class wxCheckListBoxItem : public wxOwnerDrawn
125{
2b5f62a0 126friend class WXDLLEXPORT wxCheckListBox;
2bda0e17 127public:
aa61d352
VZ
128 // ctor
129 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17 130
aa61d352
VZ
131 // drawing functions
132 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
2bda0e17 133
aa61d352
VZ
134 // simple accessors and operations
135 bool IsChecked() const { return m_bChecked; }
e2ca995f 136
aa61d352
VZ
137 void Check(bool bCheck);
138 void Toggle() { Check(!IsChecked()); }
2bda0e17 139
aa61d352 140 void SendEvent();
e2ca995f 141
2bda0e17 142private:
aa61d352
VZ
143 bool m_bChecked;
144 wxCheckListBox *m_pParent;
145 size_t m_nIndex;
0d098130 146
aa61d352 147 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
2bda0e17
KB
148};
149
c86f1403 150wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
02b7b6b0 151 : wxOwnerDrawn(wxEmptyString, true) // checkable
2bda0e17 152{
aa61d352
VZ
153 m_bChecked = false;
154 m_pParent = pParent;
155 m_nIndex = nIndex;
156
157 // we don't initialize m_nCheckHeight/Width vars because it's
158 // done in OnMeasure while they are used only in OnDraw and we
159 // know that there will always be OnMeasure before OnDraw
160
161 // fix appearance for check list boxes: they don't look quite the same as
162 // menu icons
163 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) -
164 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X) + 1);
165 SetBackgroundColour(pParent->GetBackgroundColour());
2bda0e17
KB
166}
167
45187197 168bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
169 wxODAction act, wxODStatus stat)
170{
0d098130
VZ
171 // first draw the label
172 if ( IsChecked() )
173 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
174
175 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
176 return false;
2bda0e17 177
0d098130
VZ
178
179 // now draw the check mark part
c86f1403 180 size_t nCheckWidth = GetDefaultMarginWidth(),
0d098130 181 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
182
183 int x = rc.GetX(),
2bda0e17
KB
184 y = rc.GetY();
185
186 HDC hdc = (HDC)dc.GetHDC();
187
0d098130
VZ
188 // create pens, brushes &c
189 COLORREF colBg = ::GetSysColor(COLOR_WINDOW);
190 AutoHPEN hpenBack(colBg),
191 hpenGray(RGB(0xc0, 0xc0, 0xc0));
2bda0e17 192
0d098130
VZ
193 SelectInHDC selPen(hdc, (HGDIOBJ)hpenBack);
194 AutoHBRUSH hbrBack(colBg);
195 SelectInHDC selBrush(hdc, hbrBack);
2bda0e17 196
0d098130
VZ
197 // erase the background: it could have been filled with the selected colour
198 Rectangle(hdc, x, y, x + nCheckWidth + 1, rc.GetBottom() + 1);
2bda0e17 199
0d098130
VZ
200 // shift check mark 1 pixel to the right, looks better like this
201 x++;
ee6e1b1d 202
0d098130
VZ
203 if ( IsChecked() )
204 {
205 // first create a monochrome bitmap in a memory DC
206 MemoryHDC hdcMem(hdc);
207 MonoBitmap hbmpCheck(nCheckWidth, nCheckHeight);
208 SelectInHDC selBmp(hdcMem, hbmpCheck);
209
210 // then draw a check mark into it
211 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
212 ::DrawFrameControl(hdcMem, &rect,
4676948b 213#ifdef __WXWINCE__
0d098130 214 DFC_BUTTON, DFCS_BUTTONCHECK
4676948b 215#else
0d098130 216 DFC_MENU, DFCS_MENUCHECK
4676948b 217#endif
0d098130 218 );
2bda0e17 219
0d098130
VZ
220 // finally copy it to screen DC
221 ::BitBlt(hdc, x, y, nCheckWidth, nCheckHeight, hdcMem, 0, 0, SRCCOPY);
2bda0e17
KB
222 }
223
224 // now we draw the smaller rectangle
225 y++;
226 nCheckWidth -= 2;
227 nCheckHeight -= 2;
228
229 // draw hollow gray rectangle
0d098130 230 (void)::SelectObject(hdc, (HGDIOBJ)hpenGray);
2bda0e17 231
0d098130
VZ
232 SelectInHDC selBrush2(hdc, ::GetStockObject(NULL_BRUSH));
233 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
2bda0e17 234
02b7b6b0 235 return true;
2bda0e17
KB
236}
237
238// change the state of the item and redraw it
dd3c394a 239void wxCheckListBoxItem::Check(bool check)
45187197 240{
dd3c394a 241 m_bChecked = check;
2bda0e17 242
e2ca995f 243 // index may be changed because new items were added/deleted
dd3c394a
VZ
244 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
245 {
246 // update it
247 int index = m_pParent->GetItemIndex(this);
248
223d09f6 249 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
250
251 m_nIndex = (size_t)index;
252 }
2bda0e17 253
c4dbfe14
VZ
254 HWND hwndListbox = (HWND)m_pParent->GetHWND();
255
02b7b6b0 256 RECT rcUpdate;
0c35333e 257
02b7b6b0
WS
258 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
259 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
260 {
261 wxLogDebug(wxT("LB_GETITEMRECT failed"));
262 }
c4dbfe14 263
02b7b6b0 264 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 265}
0c35333e 266
e2ca995f
VZ
267// send an "item checked" event
268void wxCheckListBoxItem::SendEvent()
269{
dd3c394a
VZ
270 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
271 event.SetInt(m_nIndex);
272 event.SetEventObject(m_pParent);
273 m_pParent->ProcessCommand(event);
2bda0e17
KB
274}
275
276// ----------------------------------------------------------------------------
277// implementation of wxCheckListBox class
278// ----------------------------------------------------------------------------
279
280// define event table
281// ------------------
282BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 283 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
284 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
285END_EVENT_TABLE()
286
287// control creation
288// ----------------
289
290// def ctor: use Create() to really create the control
d90879fa 291wxCheckListBox::wxCheckListBox()
2bda0e17
KB
292{
293}
294
295// ctor which creates the associated control
debe6624 296wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 297 const wxPoint& pos, const wxSize& size,
debe6624
JS
298 int nStrings, const wxString choices[],
299 long style, const wxValidator& val,
dd3c394a 300 const wxString& name)
2bda0e17 301{
799cea00 302 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
303}
304
584ad2a3
MB
305wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
306 const wxPoint& pos, const wxSize& size,
307 const wxArrayString& choices,
308 long style, const wxValidator& val,
309 const wxString& name)
310{
311 Create(parent, id, pos, size, choices, style, val, name);
312}
313
799cea00
VS
314bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
315 const wxPoint& pos, const wxSize& size,
316 int n, const wxString choices[],
317 long style,
318 const wxValidator& validator, const wxString& name)
319{
a374cdc4
JS
320 return wxListBox::Create(parent, id, pos, size, n, choices,
321 style | wxLB_OWNERDRAW, validator, name);
799cea00 322}
799cea00 323
584ad2a3
MB
324bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
325 const wxPoint& pos, const wxSize& size,
326 const wxArrayString& choices,
327 long style,
328 const wxValidator& validator, const wxString& name)
329{
a374cdc4
JS
330 return wxListBox::Create(parent, id, pos, size, choices,
331 style | wxLB_OWNERDRAW, validator, name);
584ad2a3
MB
332}
333
e2ca995f
VZ
334// misc overloaded methods
335// -----------------------
799cea00 336
aa61d352 337void wxCheckListBox::Delete(unsigned int n)
dd3c394a 338{
aa61d352 339 wxCHECK_RET( IsValid(n),
223d09f6 340 wxT("invalid index in wxListBox::Delete") );
dd3c394a 341
aa61d352 342 wxListBox::Delete(n);
dd3c394a
VZ
343
344 // free memory
aa61d352 345 delete m_aItems[n];
dd3c394a 346
aa61d352 347 m_aItems.RemoveAt(n);
dd3c394a
VZ
348}
349
0c35333e
RD
350bool wxCheckListBox::SetFont( const wxFont &font )
351{
aa61d352 352 unsigned int i;
07cf98cb 353 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 354 m_aItems[i]->SetFont(font);
07cf98cb 355
0c35333e 356 wxListBox::SetFont(font);
07cf98cb 357
02b7b6b0 358 return true;
0c35333e
RD
359}
360
2bda0e17
KB
361// create/retrieve item
362// --------------------
363
364// create a check list box item
2b5f62a0 365wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
366{
367 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
368 return pItem;
369}
370
2bda0e17
KB
371// return item size
372// ----------------
373bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
374{
375 if ( wxListBox::MSWOnMeasure(item) ) {
376 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
377
378 // save item height
379 m_nItemHeight = pStruct->itemHeight;
380
381 // add place for the check mark
382 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
383
02b7b6b0 384 return true;
2bda0e17
KB
385 }
386
02b7b6b0 387 return false;
2bda0e17
KB
388}
389
390// check items
391// -----------
392
aa61d352 393bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
2bda0e17 394{
aa61d352 395 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
d90879fa
VZ
396
397 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
398}
399
aa61d352 400void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
2bda0e17 401{
aa61d352 402 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
d90879fa
VZ
403
404 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
405}
406
407// process events
408// --------------
409
d90879fa 410void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 411{
d90879fa
VZ
412 // what do we do?
413 enum
414 {
415 None,
416 Toggle,
417 Set,
418 Clear
419 } oper;
420
77e00fe9 421 switch ( event.GetKeyCode() )
d90879fa
VZ
422 {
423 case WXK_SPACE:
424 oper = Toggle;
425 break;
426
427 case WXK_NUMPAD_ADD:
428 case '+':
429 oper = Set;
430 break;
431
432 case WXK_NUMPAD_SUBTRACT:
433 case '-':
434 oper = Clear;
435 break;
436
437 default:
438 oper = None;
439 }
440
441 if ( oper != None )
442 {
443 wxArrayInt selections;
a8f473ac 444 int count = 0;
d90879fa
VZ
445 if ( HasMultipleSelection() )
446 {
447 count = GetSelections(selections);
448 }
449 else
450 {
a8f473ac
RD
451 int sel = GetSelection();
452 if (sel != -1)
453 {
454 count = 1;
455 selections.Add(sel);
456 }
d90879fa
VZ
457 }
458
459 for ( int i = 0; i < count; i++ )
460 {
461 wxCheckListBoxItem *item = GetItem(selections[i]);
462 if ( !item )
463 {
464 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
465 continue;
466 }
467
468 switch ( oper )
469 {
470 case Toggle:
471 item->Toggle();
472 break;
473
474 case Set:
475 case Clear:
476 item->Check( oper == Set );
477 break;
478
479 default:
480 wxFAIL_MSG( _T("what should this key do?") );
481 }
e2ca995f
VZ
482
483 // we should send an event as this has been done by the user and
484 // not by the program
485 item->SendEvent();
d90879fa
VZ
486 }
487 }
488 else // nothing to do
489 {
490 event.Skip();
491 }
2bda0e17
KB
492}
493
494void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
495{
aa61d352
VZ
496 // clicking on the item selects it, clicking on the checkmark toggles
497 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
498 {
499 int nItem = HitTest(event.GetX(), event.GetY());
500
501 if ( nItem != wxNOT_FOUND )
502 {
503 wxCheckListBoxItem *item = GetItem(nItem);
504 item->Toggle();
505 item->SendEvent();
506 }
507 //else: it's not an error, just click outside of client zone
508 }
509 else
510 {
511 // implement default behaviour: clicking on the item selects it
512 event.Skip();
e2ca995f 513 }
2bda0e17 514}
1c089c47 515
15c3723c
VZ
516int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
517{
15c3723c
VZ
518 int nItem = (int)::SendMessage
519 (
520 (HWND)GetHWND(),
521 LB_ITEMFROMPOINT,
522 0,
523 MAKELPARAM(x, y)
524 );
15c3723c 525
aa61d352 526 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
15c3723c
VZ
527}
528
6d50c6a7
RD
529
530wxSize wxCheckListBox::DoGetBestSize() const
531{
532 wxSize best = wxListBox::DoGetBestSize();
533 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 534 CacheBestSize(best);
6d50c6a7
RD
535 return best;
536}
537
1c089c47 538#endif