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