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