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