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