]> git.saurik.com Git - wxWidgets.git/blob - src/generic/vlbox.cpp
better guarding when no printing architecture exists (patch from Joel Low)
[wxWidgets.git] / src / generic / vlbox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vlbox.cpp
3 // Purpose: implementation of wxVListBox
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTBOX
28
29 #include "wx/vlbox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/settings.h"
33 #include "wx/dcclient.h"
34 #include "wx/listbox.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/dcbuffer.h"
38 #include "wx/selstore.h"
39 #include "wx/renderer.h"
40
41 // ----------------------------------------------------------------------------
42 // event tables
43 // ----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxVListBox, wxVScrolledWindow)
46 EVT_PAINT(wxVListBox::OnPaint)
47
48 EVT_KEY_DOWN(wxVListBox::OnKeyDown)
49 EVT_LEFT_DOWN(wxVListBox::OnLeftDown)
50 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick)
51
52 EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus)
53 EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus)
54 END_EVENT_TABLE()
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 IMPLEMENT_ABSTRACT_CLASS(wxVListBox, wxVScrolledWindow)
61
62 // ----------------------------------------------------------------------------
63 // wxVListBox creation
64 // ----------------------------------------------------------------------------
65
66 void wxVListBox::Init()
67 {
68 m_current =
69 m_anchor = wxNOT_FOUND;
70 m_selStore = NULL;
71 }
72
73 bool wxVListBox::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79 {
80 #ifdef __WXMSW__
81 if ( (style & wxBORDER_MASK) == wxDEFAULT )
82 style |= wxBORDER_THEME;
83 #endif
84
85 style |= wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE;
86 if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) )
87 return false;
88
89 if ( style & wxLB_MULTIPLE )
90 m_selStore = new wxSelectionStore;
91
92 // make sure the native widget has the right colour since we do
93 // transparent drawing by default
94 SetBackgroundColour(GetBackgroundColour());
95
96 // leave m_colBgSel in an invalid state: it means for OnDrawBackground()
97 // to use wxRendererNative instead of painting selection bg ourselves
98 m_colBgSel = wxNullColour;
99
100 // flicker-free drawing requires this
101 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
102
103 return true;
104 }
105
106 wxVListBox::~wxVListBox()
107 {
108 delete m_selStore;
109 }
110
111 void wxVListBox::SetItemCount(size_t count)
112 {
113 // don't leave the current index invalid
114 if ( m_current != wxNOT_FOUND && (size_t)m_current >= count )
115 m_current = count - 1; // also ok when count == 0 as wxNOT_FOUND == -1
116
117 if ( m_selStore )
118 {
119 // tell the selection store that our number of items has changed
120 m_selStore->SetItemCount(count);
121 }
122
123 SetRowCount(count);
124 }
125
126 // ----------------------------------------------------------------------------
127 // selection handling
128 // ----------------------------------------------------------------------------
129
130 bool wxVListBox::IsSelected(size_t line) const
131 {
132 return m_selStore ? m_selStore->IsSelected(line) : (int)line == m_current;
133 }
134
135 bool wxVListBox::Select(size_t item, bool select)
136 {
137 wxCHECK_MSG( m_selStore, false,
138 _T("Select() may only be used with multiselection listbox") );
139
140 wxCHECK_MSG( item < GetItemCount(), false,
141 _T("Select(): invalid item index") );
142
143 bool changed = m_selStore->SelectItem(item, select);
144 if ( changed )
145 {
146 // selection really changed
147 RefreshRow(item);
148 }
149
150 DoSetCurrent(item);
151
152 return changed;
153 }
154
155 bool wxVListBox::SelectRange(size_t from, size_t to)
156 {
157 wxCHECK_MSG( m_selStore, false,
158 _T("SelectRange() may only be used with multiselection listbox") );
159
160 // make sure items are in correct order
161 if ( from > to )
162 {
163 size_t tmp = from;
164 from = to;
165 to = tmp;
166 }
167
168 wxCHECK_MSG( to < GetItemCount(), false,
169 _T("SelectRange(): invalid item index") );
170
171 wxArrayInt changed;
172 if ( !m_selStore->SelectRange(from, to, true, &changed) )
173 {
174 // too many items have changed, we didn't record them in changed array
175 // so we have no choice but to refresh everything between from and to
176 RefreshRows(from, to);
177 }
178 else // we've got the indices of the changed items
179 {
180 const size_t count = changed.GetCount();
181 if ( !count )
182 {
183 // nothing changed
184 return false;
185 }
186
187 // refresh just the lines which have really changed
188 for ( size_t n = 0; n < count; n++ )
189 {
190 RefreshRow(changed[n]);
191 }
192 }
193
194 // something changed
195 return true;
196 }
197
198 bool wxVListBox::DoSelectAll(bool select)
199 {
200 wxCHECK_MSG( m_selStore, false,
201 _T("SelectAll may only be used with multiselection listbox") );
202
203 size_t count = GetItemCount();
204 if ( count )
205 {
206 wxArrayInt changed;
207 if ( !m_selStore->SelectRange(0, count - 1, select) ||
208 !changed.IsEmpty() )
209 {
210 Refresh();
211
212 // something changed
213 return true;
214 }
215 }
216
217 return false;
218 }
219
220 bool wxVListBox::DoSetCurrent(int current)
221 {
222 wxASSERT_MSG( current == wxNOT_FOUND ||
223 (current >= 0 && (size_t)current < GetItemCount()),
224 _T("wxVListBox::DoSetCurrent(): invalid item index") );
225
226 if ( current == m_current )
227 {
228 // nothing to do
229 return false;
230 }
231
232 if ( m_current != wxNOT_FOUND )
233 RefreshRow(m_current);
234
235 m_current = current;
236
237 if ( m_current != wxNOT_FOUND )
238 {
239 // if the line is not visible at all, we scroll it into view but we
240 // don't need to refresh it -- it will be redrawn anyhow
241 if ( !IsVisible(m_current) )
242 {
243 ScrollToRow(m_current);
244 }
245 else // line is at least partly visible
246 {
247 // it is, indeed, only partly visible, so scroll it into view to
248 // make it entirely visible
249 while ( (size_t)m_current + 1 == GetVisibleRowsEnd() &&
250 ScrollToRow(GetVisibleBegin() + 1) ) ;
251
252 // but in any case refresh it as even if it was only partly visible
253 // before we need to redraw it entirely as its background changed
254 RefreshRow(m_current);
255 }
256 }
257
258 return true;
259 }
260
261 void wxVListBox::SendSelectedEvent()
262 {
263 wxASSERT_MSG( m_current != wxNOT_FOUND,
264 _T("SendSelectedEvent() shouldn't be called") );
265
266 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId());
267 event.SetEventObject(this);
268 event.SetInt(m_current);
269
270 (void)GetEventHandler()->ProcessEvent(event);
271 }
272
273 void wxVListBox::SetSelection(int selection)
274 {
275 wxCHECK_RET( selection == wxNOT_FOUND ||
276 (selection >= 0 && (size_t)selection < GetItemCount()),
277 _T("wxVListBox::SetSelection(): invalid item index") );
278
279 if ( HasMultipleSelection() )
280 {
281 if (selection != wxNOT_FOUND)
282 Select(selection);
283 else
284 DeselectAll();
285 m_anchor = selection;
286 }
287
288 DoSetCurrent(selection);
289 }
290
291 size_t wxVListBox::GetSelectedCount() const
292 {
293 return m_selStore ? m_selStore->GetSelectedCount()
294 : m_current == wxNOT_FOUND ? 0 : 1;
295 }
296
297 int wxVListBox::GetFirstSelected(unsigned long& cookie) const
298 {
299 cookie = 0;
300
301 return GetNextSelected(cookie);
302 }
303
304 int wxVListBox::GetNextSelected(unsigned long& cookie) const
305 {
306 wxCHECK_MSG( m_selStore, wxNOT_FOUND,
307 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
308
309 while ( cookie < GetItemCount() )
310 {
311 if ( IsSelected(cookie++) )
312 return cookie - 1;
313 }
314
315 return wxNOT_FOUND;
316 }
317
318 void wxVListBox::RefreshSelected()
319 {
320 // only refresh those items which are currently visible and selected:
321 for ( size_t n = GetVisibleBegin(), end = GetVisibleEnd(); n < end; n++ )
322 {
323 if ( IsSelected(n) )
324 RefreshRow(n);
325 }
326 }
327
328 wxRect wxVListBox::GetItemRect(size_t n) const
329 {
330 wxRect itemrect;
331
332 // check that this item is visible
333 const size_t lineMax = GetVisibleEnd();
334 if ( n >= lineMax )
335 return itemrect;
336 size_t line = GetVisibleBegin();
337 if ( n < line )
338 return itemrect;
339
340 while ( line <= n )
341 {
342 itemrect.y += itemrect.height;
343 itemrect.height = OnGetRowHeight(line);
344
345 line++;
346 }
347
348 itemrect.width = GetClientSize().x;
349
350 return itemrect;
351 }
352
353 // ----------------------------------------------------------------------------
354 // wxVListBox appearance parameters
355 // ----------------------------------------------------------------------------
356
357 void wxVListBox::SetMargins(const wxPoint& pt)
358 {
359 if ( pt != m_ptMargins )
360 {
361 m_ptMargins = pt;
362
363 Refresh();
364 }
365 }
366
367 void wxVListBox::SetSelectionBackground(const wxColour& col)
368 {
369 m_colBgSel = col;
370 }
371
372 // ----------------------------------------------------------------------------
373 // wxVListBox painting
374 // ----------------------------------------------------------------------------
375
376 wxCoord wxVListBox::OnGetRowHeight(size_t line) const
377 {
378 return OnMeasureItem(line) + 2*m_ptMargins.y;
379 }
380
381 void wxVListBox::OnDrawSeparator(wxDC& WXUNUSED(dc),
382 wxRect& WXUNUSED(rect),
383 size_t WXUNUSED(n)) const
384 {
385 }
386
387 bool
388 wxVListBox::DoDrawSolidBackground(const wxColour& col,
389 wxDC& dc,
390 const wxRect& rect,
391 size_t n) const
392 {
393 if ( !col.IsOk() )
394 return false;
395
396 // we need to render selected and current items differently
397 const bool isSelected = IsSelected(n),
398 isCurrent = IsCurrent(n);
399 if ( isSelected || isCurrent )
400 {
401 if ( isSelected )
402 {
403 dc.SetBrush(wxBrush(col, wxBRUSHSTYLE_SOLID));
404 }
405 else // !selected
406 {
407 dc.SetBrush(*wxTRANSPARENT_BRUSH);
408 }
409 dc.SetPen(*(isCurrent ? wxBLACK_PEN : wxTRANSPARENT_PEN));
410 dc.DrawRectangle(rect);
411 }
412 //else: do nothing for the normal items
413
414 return true;
415 }
416
417 void wxVListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
418 {
419 // use wxRendererNative for more native look unless we use custom bg colour
420 if ( !DoDrawSolidBackground(m_colBgSel, dc, rect, n) )
421 {
422 int flags = 0;
423 if ( IsSelected(n) )
424 flags |= wxCONTROL_SELECTED;
425 if ( IsCurrent(n) )
426 flags |= wxCONTROL_CURRENT;
427 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox*, this) )
428 flags |= wxCONTROL_FOCUSED;
429
430 wxRendererNative::Get().DrawItemSelectionRect(
431 wx_const_cast(wxVListBox *, this), dc, rect, flags);
432 }
433 }
434
435 void wxVListBox::OnPaint(wxPaintEvent& WXUNUSED(event))
436 {
437 wxSize clientSize = GetClientSize();
438
439 wxAutoBufferedPaintDC dc(this);
440
441 // the update rectangle
442 wxRect rectUpdate = GetUpdateClientRect();
443
444 // fill it with background colour
445 dc.SetBackground(GetBackgroundColour());
446 dc.Clear();
447
448 // the bounding rectangle of the current line
449 wxRect rectRow;
450 rectRow.width = clientSize.x;
451
452 // iterate over all visible lines
453 const size_t lineMax = GetVisibleEnd();
454 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
455 {
456 const wxCoord hRow = OnGetRowHeight(line);
457
458 rectRow.height = hRow;
459
460 // and draw the ones which intersect the update rect
461 if ( rectRow.Intersects(rectUpdate) )
462 {
463 // don't allow drawing outside of the lines rectangle
464 wxDCClipper clip(dc, rectRow);
465
466 wxRect rect = rectRow;
467 OnDrawBackground(dc, rect, line);
468
469 OnDrawSeparator(dc, rect, line);
470
471 rect.Deflate(m_ptMargins.x, m_ptMargins.y);
472 OnDrawItem(dc, rect, line);
473 }
474 else // no intersection
475 {
476 if ( rectRow.GetTop() > rectUpdate.GetBottom() )
477 {
478 // we are already below the update rect, no need to continue
479 // further
480 break;
481 }
482 //else: the next line may intersect the update rect
483 }
484
485 rectRow.y += hRow;
486 }
487 }
488
489 void wxVListBox::OnSetOrKillFocus(wxFocusEvent& WXUNUSED(event))
490 {
491 // we need to repaint the selection when we get the focus since
492 // wxRendererNative in general draws the focused selection differently
493 // from the unfocused selection (see OnDrawItem):
494 RefreshSelected();
495 }
496
497
498 // ============================================================================
499 // wxVListBox keyboard/mouse handling
500 // ============================================================================
501
502 void wxVListBox::DoHandleItemClick(int item, int flags)
503 {
504 // has anything worth telling the client code about happened?
505 bool notify = false;
506
507 if ( HasMultipleSelection() )
508 {
509 // select the iteem clicked?
510 bool select = true;
511
512 // NB: the keyboard interface we implement here corresponds to
513 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
514 // sense IMHO
515 if ( flags & ItemClick_Shift )
516 {
517 if ( m_current != wxNOT_FOUND )
518 {
519 if ( m_anchor == wxNOT_FOUND )
520 m_anchor = m_current;
521
522 select = false;
523
524 // only the range from the selection anchor to new m_current
525 // must be selected
526 if ( DeselectAll() )
527 notify = true;
528
529 if ( SelectRange(m_anchor, item) )
530 notify = true;
531 }
532 //else: treat it as ordinary click/keypress
533 }
534 else // Shift not pressed
535 {
536 m_anchor = item;
537
538 if ( flags & ItemClick_Ctrl )
539 {
540 select = false;
541
542 if ( !(flags & ItemClick_Kbd) )
543 {
544 Toggle(item);
545
546 // the status of the item has definitely changed
547 notify = true;
548 }
549 //else: Ctrl-arrow pressed, don't change selection
550 }
551 //else: behave as in single selection case
552 }
553
554 if ( select )
555 {
556 // make the clicked item the only selection
557 if ( DeselectAll() )
558 notify = true;
559
560 if ( Select(item) )
561 notify = true;
562 }
563 }
564
565 // in any case the item should become the current one
566 if ( DoSetCurrent(item) )
567 {
568 if ( !HasMultipleSelection() )
569 {
570 // this has also changed the selection for single selection case
571 notify = true;
572 }
573 }
574
575 if ( notify )
576 {
577 // notify the user about the selection change
578 SendSelectedEvent();
579 }
580 //else: nothing changed at all
581 }
582
583 // ----------------------------------------------------------------------------
584 // keyboard handling
585 // ----------------------------------------------------------------------------
586
587 void wxVListBox::OnKeyDown(wxKeyEvent& event)
588 {
589 // flags for DoHandleItemClick()
590 int flags = ItemClick_Kbd;
591
592 int current;
593 switch ( event.GetKeyCode() )
594 {
595 case WXK_HOME:
596 current = 0;
597 break;
598
599 case WXK_END:
600 current = GetRowCount() - 1;
601 break;
602
603 case WXK_DOWN:
604 if ( m_current == (int)GetRowCount() - 1 )
605 return;
606
607 current = m_current + 1;
608 break;
609
610 case WXK_UP:
611 if ( m_current == wxNOT_FOUND )
612 current = GetRowCount() - 1;
613 else if ( m_current != 0 )
614 current = m_current - 1;
615 else // m_current == 0
616 return;
617 break;
618
619 case WXK_PAGEDOWN:
620 PageDown();
621 current = GetVisibleBegin();
622 break;
623
624 case WXK_PAGEUP:
625 if ( m_current == (int)GetVisibleBegin() )
626 {
627 PageUp();
628 }
629
630 current = GetVisibleBegin();
631 break;
632
633 case WXK_SPACE:
634 // hack: pressing space should work like a mouse click rather than
635 // like a keyboard arrow press, so trick DoHandleItemClick() in
636 // thinking we were clicked
637 flags &= ~ItemClick_Kbd;
638 current = m_current;
639 break;
640
641 #ifdef __WXMSW__
642 case WXK_TAB:
643 // Since we are using wxWANTS_CHARS we need to send navigation
644 // events for the tabs on MSW
645 HandleAsNavigationKey(event);
646 // fall through to default
647 #endif
648 default:
649 event.Skip();
650 current = 0; // just to silent the stupid compiler warnings
651 wxUnusedVar(current);
652 return;
653 }
654
655 if ( event.ShiftDown() )
656 flags |= ItemClick_Shift;
657 if ( event.ControlDown() )
658 flags |= ItemClick_Ctrl;
659
660 DoHandleItemClick(current, flags);
661 }
662
663 // ----------------------------------------------------------------------------
664 // wxVListBox mouse handling
665 // ----------------------------------------------------------------------------
666
667 void wxVListBox::OnLeftDown(wxMouseEvent& event)
668 {
669 SetFocus();
670
671 int item = VirtualHitTest(event.GetPosition().y);
672
673 if ( item != wxNOT_FOUND )
674 {
675 int flags = 0;
676 if ( event.ShiftDown() )
677 flags |= ItemClick_Shift;
678
679 // under Mac Apple-click is used in the same way as Ctrl-click
680 // elsewhere
681 #ifdef __WXMAC__
682 if ( event.MetaDown() )
683 #else
684 if ( event.ControlDown() )
685 #endif
686 flags |= ItemClick_Ctrl;
687
688 DoHandleItemClick(item, flags);
689 }
690 }
691
692 void wxVListBox::OnLeftDClick(wxMouseEvent& eventMouse)
693 {
694 int item = VirtualHitTest(eventMouse.GetPosition().y);
695 if ( item != wxNOT_FOUND )
696 {
697
698 // if item double-clicked was not yet selected, then treat
699 // this event as a left-click instead
700 if ( item == m_current )
701 {
702 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId());
703 event.SetEventObject(this);
704 event.SetInt(item);
705
706 (void)GetEventHandler()->ProcessEvent(event);
707 }
708 else
709 {
710 OnLeftDown(eventMouse);
711 }
712
713 }
714 }
715
716
717 // ----------------------------------------------------------------------------
718 // use the same default attributes as wxListBox
719 // ----------------------------------------------------------------------------
720
721 //static
722 wxVisualAttributes
723 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant)
724 {
725 return wxListBox::GetClassDefaultAttributes(variant);
726 }
727
728 #endif