]>
Commit | Line | Data |
---|---|---|
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 | #if wxUSE_LISTBOX | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/settings.h" | |
31 | #include "wx/dcclient.h" | |
32 | #endif //WX_PRECOMP | |
33 | ||
34 | #include "wx/vlbox.h" | |
35 | #include "wx/dcbuffer.h" | |
36 | #include "wx/selstore.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // event tables | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | BEGIN_EVENT_TABLE(wxVListBox, wxVScrolledWindow) | |
43 | EVT_PAINT(wxVListBox::OnPaint) | |
44 | ||
45 | EVT_KEY_DOWN(wxVListBox::OnKeyDown) | |
46 | EVT_LEFT_DOWN(wxVListBox::OnLeftDown) | |
47 | EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick) | |
48 | END_EVENT_TABLE() | |
49 | ||
50 | // ============================================================================ | |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | IMPLEMENT_ABSTRACT_CLASS(wxVListBox, wxVScrolledWindow) | |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // wxVListBox creation | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | void wxVListBox::Init() | |
61 | { | |
62 | m_current = | |
63 | m_anchor = wxNOT_FOUND; | |
64 | m_selStore = NULL; | |
65 | m_doubleBuffer = NULL; | |
66 | } | |
67 | ||
68 | bool wxVListBox::Create(wxWindow *parent, | |
69 | wxWindowID id, | |
70 | const wxPoint& pos, | |
71 | const wxSize& size, | |
72 | long style, | |
73 | const wxString& name) | |
74 | { | |
75 | style |= wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE; | |
76 | if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) ) | |
77 | return false; | |
78 | ||
79 | if ( style & wxLB_MULTIPLE ) | |
80 | m_selStore = new wxSelectionStore; | |
81 | ||
82 | // make sure the native widget has the right colour since we do | |
83 | // transparent drawing by default | |
84 | SetBackgroundColour(GetBackgroundColour()); | |
85 | m_colBgSel = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
86 | ||
87 | // flicker-free drawing requires this | |
88 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
89 | ||
90 | return true; | |
91 | } | |
92 | ||
93 | wxVListBox::~wxVListBox() | |
94 | { | |
95 | delete m_doubleBuffer; | |
96 | delete m_selStore; | |
97 | } | |
98 | ||
99 | void 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 | ||
110 | // ---------------------------------------------------------------------------- | |
111 | // selection handling | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | bool wxVListBox::IsSelected(size_t line) const | |
115 | { | |
116 | return m_selStore ? m_selStore->IsSelected(line) : (int)line == m_current; | |
117 | } | |
118 | ||
119 | bool 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 | ||
139 | bool wxVListBox::SelectRange(size_t from, size_t to) | |
140 | { | |
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 | ||
182 | bool 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 | ||
204 | bool wxVListBox::DoSetCurrent(int current) | |
205 | { | |
206 | wxASSERT_MSG( current == wxNOT_FOUND || | |
207 | (current >= 0 && (size_t)current < GetItemCount()), | |
208 | _T("wxVListBox::DoSetCurrent(): invalid item index") ); | |
209 | ||
210 | if ( current == m_current ) | |
211 | { | |
212 | // nothing to do | |
213 | return false; | |
214 | } | |
215 | ||
216 | if ( m_current != wxNOT_FOUND ) | |
217 | RefreshLine(m_current); | |
218 | ||
219 | m_current = current; | |
220 | ||
221 | if ( m_current != wxNOT_FOUND ) | |
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 | |
225 | if ( !IsVisible(m_current) ) | |
226 | { | |
227 | ScrollToLine(m_current); | |
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 | |
233 | while ( (size_t)m_current == GetLastVisibleLine() && | |
234 | ScrollToLine(GetVisibleBegin()+1) ); | |
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 | |
238 | RefreshLine(m_current); | |
239 | } | |
240 | } | |
241 | ||
242 | return true; | |
243 | } | |
244 | ||
245 | void 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); | |
252 | event.SetInt(m_current); | |
253 | ||
254 | (void)GetEventHandler()->ProcessEvent(event); | |
255 | } | |
256 | ||
257 | void wxVListBox::SetSelection(int selection) | |
258 | { | |
259 | wxCHECK_RET( selection == wxNOT_FOUND || | |
260 | (selection >= 0 && (size_t)selection < GetItemCount()), | |
261 | _T("wxVListBox::SetSelection(): invalid item index") ); | |
262 | ||
263 | if ( HasMultipleSelection() ) | |
264 | { | |
265 | Select(selection); | |
266 | m_anchor = selection; | |
267 | } | |
268 | ||
269 | DoSetCurrent(selection); | |
270 | } | |
271 | ||
272 | size_t wxVListBox::GetSelectedCount() const | |
273 | { | |
274 | return m_selStore ? m_selStore->GetSelectedCount() | |
275 | : m_current == wxNOT_FOUND ? 0 : 1; | |
276 | } | |
277 | ||
278 | int wxVListBox::GetFirstSelected(unsigned long& cookie) const | |
279 | { | |
280 | cookie = 0; | |
281 | ||
282 | return GetNextSelected(cookie); | |
283 | } | |
284 | ||
285 | int wxVListBox::GetNextSelected(unsigned long& cookie) const | |
286 | { | |
287 | wxCHECK_MSG( m_selStore, wxNOT_FOUND, | |
288 | _T("GetFirst/NextSelected() may only be used with multiselection listboxes") ); | |
289 | ||
290 | while ( cookie < GetItemCount() ) | |
291 | { | |
292 | if ( IsSelected(cookie++) ) | |
293 | return cookie - 1; | |
294 | } | |
295 | ||
296 | return wxNOT_FOUND; | |
297 | } | |
298 | ||
299 | // ---------------------------------------------------------------------------- | |
300 | // wxVListBox appearance parameters | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
303 | void wxVListBox::SetMargins(const wxPoint& pt) | |
304 | { | |
305 | if ( pt != m_ptMargins ) | |
306 | { | |
307 | m_ptMargins = pt; | |
308 | ||
309 | Refresh(); | |
310 | } | |
311 | } | |
312 | ||
313 | void wxVListBox::SetSelectionBackground(const wxColour& col) | |
314 | { | |
315 | m_colBgSel = col; | |
316 | } | |
317 | ||
318 | // ---------------------------------------------------------------------------- | |
319 | // wxVListBox painting | |
320 | // ---------------------------------------------------------------------------- | |
321 | ||
322 | wxCoord wxVListBox::OnGetLineHeight(size_t line) const | |
323 | { | |
324 | return OnMeasureItem(line) + 2*m_ptMargins.y; | |
325 | } | |
326 | ||
327 | void wxVListBox::OnDrawSeparator(wxDC& WXUNUSED(dc), | |
328 | wxRect& WXUNUSED(rect), | |
329 | size_t WXUNUSED(n)) const | |
330 | { | |
331 | } | |
332 | ||
333 | void wxVListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const | |
334 | { | |
335 | // we need to render selected and current items differently | |
336 | const bool isSelected = IsSelected(n), | |
337 | isCurrent = IsCurrent(n); | |
338 | if ( isSelected || isCurrent ) | |
339 | { | |
340 | if ( isSelected ) | |
341 | { | |
342 | dc.SetBrush(wxBrush(m_colBgSel, wxSOLID)); | |
343 | } | |
344 | else // !selected | |
345 | { | |
346 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
347 | } | |
348 | ||
349 | dc.SetPen(*(isCurrent ? wxBLACK_PEN : wxTRANSPARENT_PEN)); | |
350 | ||
351 | dc.DrawRectangle(rect); | |
352 | } | |
353 | //else: do nothing for the normal items | |
354 | } | |
355 | ||
356 | void wxVListBox::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
357 | { | |
358 | // If size is larger, recalculate double buffer bitmap | |
359 | wxSize clientSize = GetClientSize(); | |
360 | ||
361 | if ( !m_doubleBuffer || | |
362 | clientSize.x > m_doubleBuffer->GetWidth() || | |
363 | clientSize.y > m_doubleBuffer->GetHeight() ) | |
364 | { | |
365 | delete m_doubleBuffer; | |
366 | m_doubleBuffer = new wxBitmap(clientSize.x+25,clientSize.y+25); | |
367 | } | |
368 | ||
369 | wxBufferedPaintDC dc(this,*m_doubleBuffer); | |
370 | ||
371 | // the update rectangle | |
372 | wxRect rectUpdate = GetUpdateClientRect(); | |
373 | ||
374 | // Fill it with background colour | |
375 | dc.SetBrush(GetBackgroundColour()); | |
376 | dc.Clear(); | |
377 | ||
378 | // the bounding rectangle of the current line | |
379 | wxRect rectLine; | |
380 | rectLine.width = clientSize.x; | |
381 | ||
382 | // iterate over all visible lines | |
383 | const size_t lineMax = GetLastVisibleLine(); | |
384 | for ( size_t line = GetFirstVisibleLine(); line <= lineMax; line++ ) | |
385 | { | |
386 | const wxCoord hLine = OnGetLineHeight(line); | |
387 | ||
388 | rectLine.height = hLine; | |
389 | ||
390 | // and draw the ones which intersect the update rect | |
391 | if ( rectLine.Intersects(rectUpdate) ) | |
392 | { | |
393 | // don't allow drawing outside of the lines rectangle | |
394 | wxDCClipper clip(dc, rectLine); | |
395 | ||
396 | wxRect rect = rectLine; | |
397 | OnDrawBackground(dc, rect, line); | |
398 | ||
399 | OnDrawSeparator(dc, rect, line); | |
400 | ||
401 | rect.Deflate(m_ptMargins.x, m_ptMargins.y); | |
402 | OnDrawItem(dc, rect, line); | |
403 | } | |
404 | else // no intersection | |
405 | { | |
406 | if ( rectLine.GetTop() > rectUpdate.GetBottom() ) | |
407 | { | |
408 | // we are already below the update rect, no need to continue | |
409 | // further | |
410 | break; | |
411 | } | |
412 | //else: the next line may intersect the update rect | |
413 | } | |
414 | ||
415 | rectLine.y += hLine; | |
416 | } | |
417 | } | |
418 | ||
419 | // ============================================================================ | |
420 | // wxVListBox keyboard/mouse handling | |
421 | // ============================================================================ | |
422 | ||
423 | void wxVListBox::DoHandleItemClick(int item, int flags) | |
424 | { | |
425 | // has anything worth telling the client code about happened? | |
426 | bool notify = false; | |
427 | ||
428 | if ( HasMultipleSelection() ) | |
429 | { | |
430 | // select the iteem clicked? | |
431 | bool select = true; | |
432 | ||
433 | // NB: the keyboard interface we implement here corresponds to | |
434 | // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more | |
435 | // sense IMHO | |
436 | if ( flags & ItemClick_Shift ) | |
437 | { | |
438 | if ( m_current != wxNOT_FOUND ) | |
439 | { | |
440 | if ( m_anchor == wxNOT_FOUND ) | |
441 | m_anchor = m_current; | |
442 | ||
443 | select = false; | |
444 | ||
445 | // only the range from the selection anchor to new m_current | |
446 | // must be selected | |
447 | if ( DeselectAll() ) | |
448 | notify = true; | |
449 | ||
450 | if ( SelectRange(m_anchor, item) ) | |
451 | notify = true; | |
452 | } | |
453 | //else: treat it as ordinary click/keypress | |
454 | } | |
455 | else // Shift not pressed | |
456 | { | |
457 | m_anchor = item; | |
458 | ||
459 | if ( flags & ItemClick_Ctrl ) | |
460 | { | |
461 | select = false; | |
462 | ||
463 | if ( !(flags & ItemClick_Kbd) ) | |
464 | { | |
465 | Toggle(item); | |
466 | ||
467 | // the status of the item has definitely changed | |
468 | notify = true; | |
469 | } | |
470 | //else: Ctrl-arrow pressed, don't change selection | |
471 | } | |
472 | //else: behave as in single selection case | |
473 | } | |
474 | ||
475 | if ( select ) | |
476 | { | |
477 | // make the clicked item the only selection | |
478 | if ( DeselectAll() ) | |
479 | notify = true; | |
480 | ||
481 | if ( Select(item) ) | |
482 | notify = true; | |
483 | } | |
484 | } | |
485 | ||
486 | // in any case the item should become the current one | |
487 | if ( DoSetCurrent(item) ) | |
488 | { | |
489 | if ( !HasMultipleSelection() ) | |
490 | { | |
491 | // this has also changed the selection for single selection case | |
492 | notify = true; | |
493 | } | |
494 | } | |
495 | ||
496 | if ( notify ) | |
497 | { | |
498 | // notify the user about the selection change | |
499 | SendSelectedEvent(); | |
500 | } | |
501 | //else: nothing changed at all | |
502 | } | |
503 | ||
504 | // ---------------------------------------------------------------------------- | |
505 | // keyboard handling | |
506 | // ---------------------------------------------------------------------------- | |
507 | ||
508 | void wxVListBox::OnKeyDown(wxKeyEvent& event) | |
509 | { | |
510 | // flags for DoHandleItemClick() | |
511 | int flags = ItemClick_Kbd; | |
512 | ||
513 | int current; | |
514 | switch ( event.GetKeyCode() ) | |
515 | { | |
516 | case WXK_HOME: | |
517 | current = 0; | |
518 | break; | |
519 | ||
520 | case WXK_END: | |
521 | current = GetLineCount() - 1; | |
522 | break; | |
523 | ||
524 | case WXK_DOWN: | |
525 | if ( m_current == (int)GetLineCount() - 1 ) | |
526 | return; | |
527 | ||
528 | current = m_current + 1; | |
529 | break; | |
530 | ||
531 | case WXK_UP: | |
532 | if ( m_current == wxNOT_FOUND ) | |
533 | current = GetLineCount() - 1; | |
534 | else if ( m_current != 0 ) | |
535 | current = m_current - 1; | |
536 | else // m_current == 0 | |
537 | return; | |
538 | break; | |
539 | ||
540 | case WXK_PAGEDOWN: | |
541 | case WXK_NEXT: | |
542 | PageDown(); | |
543 | current = GetFirstVisibleLine(); | |
544 | break; | |
545 | ||
546 | case WXK_PAGEUP: | |
547 | case WXK_PRIOR: | |
548 | if ( m_current == (int)GetFirstVisibleLine() ) | |
549 | { | |
550 | PageUp(); | |
551 | } | |
552 | ||
553 | current = GetFirstVisibleLine(); | |
554 | break; | |
555 | ||
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 | ||
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 | |
577 | default: | |
578 | event.Skip(); | |
579 | current = 0; // just to silent the stupid compiler warnings | |
580 | wxUnusedVar(current); | |
581 | return; | |
582 | } | |
583 | ||
584 | if ( event.ShiftDown() ) | |
585 | flags |= ItemClick_Shift; | |
586 | if ( event.ControlDown() ) | |
587 | flags |= ItemClick_Ctrl; | |
588 | ||
589 | DoHandleItemClick(current, flags); | |
590 | } | |
591 | ||
592 | // ---------------------------------------------------------------------------- | |
593 | // wxVListBox mouse handling | |
594 | // ---------------------------------------------------------------------------- | |
595 | ||
596 | void wxVListBox::OnLeftDown(wxMouseEvent& event) | |
597 | { | |
598 | SetFocus(); | |
599 | ||
600 | int item = HitTest(event.GetPosition()); | |
601 | ||
602 | if ( item != wxNOT_FOUND ) | |
603 | { | |
604 | int flags = 0; | |
605 | if ( event.ShiftDown() ) | |
606 | flags |= ItemClick_Shift; | |
607 | ||
608 | // under Mac Apple-click is used in the same way as Ctrl-click | |
609 | // elsewhere | |
610 | #ifdef __WXMAC__ | |
611 | if ( event.MetaDown() ) | |
612 | #else | |
613 | if ( event.ControlDown() ) | |
614 | #endif | |
615 | flags |= ItemClick_Ctrl; | |
616 | ||
617 | DoHandleItemClick(item, flags); | |
618 | } | |
619 | } | |
620 | ||
621 | void wxVListBox::OnLeftDClick(wxMouseEvent& eventMouse) | |
622 | { | |
623 | int item = HitTest(eventMouse.GetPosition()); | |
624 | if ( item != wxNOT_FOUND ) | |
625 | { | |
626 | ||
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 | } | |
641 | ||
642 | } | |
643 | } | |
644 | ||
645 | ||
646 | // ---------------------------------------------------------------------------- | |
647 | // use the same default attributes as wxListBox | |
648 | // ---------------------------------------------------------------------------- | |
649 | ||
650 | #include "wx/listbox.h" | |
651 | ||
652 | //static | |
653 | wxVisualAttributes | |
654 | wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant) | |
655 | { | |
656 | return wxListBox::GetClassDefaultAttributes(variant); | |
657 | } | |
658 | ||
659 | #endif |