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