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