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