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