]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
Explicitly unregister custom wxWebViewIE namespaces when we are done with them. Also...
[wxWidgets.git] / src / generic / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // TODO
12 //
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
15
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if wxUSE_LISTCTRL
25
26 #include "wx/listctrl.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/scrolwin.h"
30 #include "wx/timer.h"
31 #include "wx/settings.h"
32 #include "wx/dynarray.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/math.h"
36 #include "wx/settings.h"
37 #include "wx/sizer.h"
38 #endif
39
40 #include "wx/imaglist.h"
41 #include "wx/renderer.h"
42 #include "wx/generic/private/listctrl.h"
43
44 #ifdef __WXMAC__
45 #include "wx/osx/private.h"
46 #endif
47
48 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
49 #define "wx/msw/wrapwin.h"
50 #endif
51
52 // NOTE: If using the wxListBox visual attributes works everywhere then this can
53 // be removed, as well as the #else case below.
54 #define _USE_VISATTR 0
55
56
57 // ----------------------------------------------------------------------------
58 // constants
59 // ----------------------------------------------------------------------------
60
61 // // the height of the header window (FIXME: should depend on its font!)
62 // static const int HEADER_HEIGHT = 23;
63
64 static const int SCROLL_UNIT_X = 15;
65
66 // the spacing between the lines (in report mode)
67 static const int LINE_SPACING = 0;
68
69 // extra margins around the text label
70 #ifdef __WXGTK__
71 static const int EXTRA_WIDTH = 6;
72 #else
73 static const int EXTRA_WIDTH = 4;
74 #endif
75
76 #ifdef __WXGTK__
77 static const int EXTRA_HEIGHT = 6;
78 #else
79 static const int EXTRA_HEIGHT = 4;
80 #endif
81
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X = 2;
84 static const int EXTRA_BORDER_Y = 2;
85
86 // offset for the header window
87 static const int HEADER_OFFSET_X = 0;
88 static const int HEADER_OFFSET_Y = 0;
89
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS = 6;
92
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN = 10;
95
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT = 80;
98
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
101
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
104
105
106
107 // ----------------------------------------------------------------------------
108 // arrays/list implementations
109 // ----------------------------------------------------------------------------
110
111 #include "wx/listimpl.cpp"
112 WX_DEFINE_LIST(wxListItemDataList)
113
114 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxListLineDataArray)
116
117 #include "wx/listimpl.cpp"
118 WX_DEFINE_LIST(wxListHeaderDataList)
119
120
121 // ----------------------------------------------------------------------------
122 // wxListItemData
123 // ----------------------------------------------------------------------------
124
125 wxListItemData::~wxListItemData()
126 {
127 // in the virtual list control the attributes are managed by the main
128 // program, so don't delete them
129 if ( !m_owner->IsVirtual() )
130 delete m_attr;
131
132 delete m_rect;
133 }
134
135 void wxListItemData::Init()
136 {
137 m_image = -1;
138 m_data = 0;
139
140 m_attr = NULL;
141 }
142
143 wxListItemData::wxListItemData(wxListMainWindow *owner)
144 {
145 Init();
146
147 m_owner = owner;
148
149 if ( owner->InReportView() )
150 m_rect = NULL;
151 else
152 m_rect = new wxRect;
153 }
154
155 void wxListItemData::SetItem( const wxListItem &info )
156 {
157 if ( info.m_mask & wxLIST_MASK_TEXT )
158 SetText(info.m_text);
159 if ( info.m_mask & wxLIST_MASK_IMAGE )
160 m_image = info.m_image;
161 if ( info.m_mask & wxLIST_MASK_DATA )
162 m_data = info.m_data;
163
164 if ( info.HasAttributes() )
165 {
166 if ( m_attr )
167 m_attr->AssignFrom(*info.GetAttributes());
168 else
169 m_attr = new wxListItemAttr(*info.GetAttributes());
170 }
171
172 if ( m_rect )
173 {
174 m_rect->x =
175 m_rect->y =
176 m_rect->height = 0;
177 m_rect->width = info.m_width;
178 }
179 }
180
181 void wxListItemData::SetPosition( int x, int y )
182 {
183 wxCHECK_RET( m_rect, wxT("unexpected SetPosition() call") );
184
185 m_rect->x = x;
186 m_rect->y = y;
187 }
188
189 void wxListItemData::SetSize( int width, int height )
190 {
191 wxCHECK_RET( m_rect, wxT("unexpected SetSize() call") );
192
193 if ( width != -1 )
194 m_rect->width = width;
195 if ( height != -1 )
196 m_rect->height = height;
197 }
198
199 bool wxListItemData::IsHit( int x, int y ) const
200 {
201 wxCHECK_MSG( m_rect, false, wxT("can't be called in this mode") );
202
203 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
204 }
205
206 int wxListItemData::GetX() const
207 {
208 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
209
210 return m_rect->x;
211 }
212
213 int wxListItemData::GetY() const
214 {
215 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
216
217 return m_rect->y;
218 }
219
220 int wxListItemData::GetWidth() const
221 {
222 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
223
224 return m_rect->width;
225 }
226
227 int wxListItemData::GetHeight() const
228 {
229 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
230
231 return m_rect->height;
232 }
233
234 void wxListItemData::GetItem( wxListItem &info ) const
235 {
236 long mask = info.m_mask;
237 if ( !mask )
238 // by default, get everything for backwards compatibility
239 mask = -1;
240
241 if ( mask & wxLIST_MASK_TEXT )
242 info.m_text = m_text;
243 if ( mask & wxLIST_MASK_IMAGE )
244 info.m_image = m_image;
245 if ( mask & wxLIST_MASK_DATA )
246 info.m_data = m_data;
247
248 if ( m_attr )
249 {
250 if ( m_attr->HasTextColour() )
251 info.SetTextColour(m_attr->GetTextColour());
252 if ( m_attr->HasBackgroundColour() )
253 info.SetBackgroundColour(m_attr->GetBackgroundColour());
254 if ( m_attr->HasFont() )
255 info.SetFont(m_attr->GetFont());
256 }
257 }
258
259 //-----------------------------------------------------------------------------
260 // wxListHeaderData
261 //-----------------------------------------------------------------------------
262
263 void wxListHeaderData::Init()
264 {
265 m_mask = 0;
266 m_image = -1;
267 m_format = 0;
268 m_width = 0;
269 m_xpos = 0;
270 m_ypos = 0;
271 m_height = 0;
272 m_state = 0;
273 }
274
275 wxListHeaderData::wxListHeaderData()
276 {
277 Init();
278 }
279
280 wxListHeaderData::wxListHeaderData( const wxListItem &item )
281 {
282 Init();
283
284 SetItem( item );
285 }
286
287 void wxListHeaderData::SetItem( const wxListItem &item )
288 {
289 m_mask = item.m_mask;
290
291 if ( m_mask & wxLIST_MASK_TEXT )
292 m_text = item.m_text;
293
294 if ( m_mask & wxLIST_MASK_IMAGE )
295 m_image = item.m_image;
296
297 if ( m_mask & wxLIST_MASK_FORMAT )
298 m_format = item.m_format;
299
300 if ( m_mask & wxLIST_MASK_WIDTH )
301 SetWidth(item.m_width);
302
303 if ( m_mask & wxLIST_MASK_STATE )
304 SetState(item.m_state);
305 }
306
307 void wxListHeaderData::SetPosition( int x, int y )
308 {
309 m_xpos = x;
310 m_ypos = y;
311 }
312
313 void wxListHeaderData::SetHeight( int h )
314 {
315 m_height = h;
316 }
317
318 void wxListHeaderData::SetWidth( int w )
319 {
320 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
321 }
322
323 void wxListHeaderData::SetState( int flag )
324 {
325 m_state = flag;
326 }
327
328 void wxListHeaderData::SetFormat( int format )
329 {
330 m_format = format;
331 }
332
333 bool wxListHeaderData::HasImage() const
334 {
335 return m_image != -1;
336 }
337
338 bool wxListHeaderData::IsHit( int x, int y ) const
339 {
340 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
341 }
342
343 void wxListHeaderData::GetItem( wxListItem& item )
344 {
345 long mask = item.m_mask;
346 if ( !mask )
347 {
348 // by default, get everything for backwards compatibility
349 mask = -1;
350 }
351
352 if ( mask & wxLIST_MASK_STATE )
353 item.m_state = m_state;
354 if ( mask & wxLIST_MASK_TEXT )
355 item.m_text = m_text;
356 if ( mask & wxLIST_MASK_IMAGE )
357 item.m_image = m_image;
358 if ( mask & wxLIST_MASK_WIDTH )
359 item.m_width = m_width;
360 if ( mask & wxLIST_MASK_FORMAT )
361 item.m_format = m_format;
362 }
363
364 int wxListHeaderData::GetImage() const
365 {
366 return m_image;
367 }
368
369 int wxListHeaderData::GetWidth() const
370 {
371 return m_width;
372 }
373
374 int wxListHeaderData::GetFormat() const
375 {
376 return m_format;
377 }
378
379 int wxListHeaderData::GetState() const
380 {
381 return m_state;
382 }
383
384 //-----------------------------------------------------------------------------
385 // wxListLineData
386 //-----------------------------------------------------------------------------
387
388 inline int wxListLineData::GetMode() const
389 {
390 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
391 }
392
393 inline bool wxListLineData::InReportView() const
394 {
395 return m_owner->HasFlag(wxLC_REPORT);
396 }
397
398 inline bool wxListLineData::IsVirtual() const
399 {
400 return m_owner->IsVirtual();
401 }
402
403 wxListLineData::wxListLineData( wxListMainWindow *owner )
404 {
405 m_owner = owner;
406
407 if ( InReportView() )
408 m_gi = NULL;
409 else // !report
410 m_gi = new GeometryInfo;
411
412 m_highlighted = false;
413
414 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
415 }
416
417 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
418 {
419 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
420 wxCHECK_RET( node, wxT("no subitems at all??") );
421
422 wxListItemData *item = node->GetData();
423
424 wxString s;
425 wxCoord lw, lh;
426
427 switch ( GetMode() )
428 {
429 case wxLC_ICON:
430 case wxLC_SMALL_ICON:
431 m_gi->m_rectAll.width = spacing;
432
433 s = item->GetText();
434
435 if ( s.empty() )
436 {
437 lh =
438 m_gi->m_rectLabel.width =
439 m_gi->m_rectLabel.height = 0;
440 }
441 else // has label
442 {
443 dc->GetTextExtent( s, &lw, &lh );
444 lw += EXTRA_WIDTH;
445 lh += EXTRA_HEIGHT;
446
447 m_gi->m_rectAll.height = spacing + lh;
448 if (lw > spacing)
449 m_gi->m_rectAll.width = lw;
450
451 m_gi->m_rectLabel.width = lw;
452 m_gi->m_rectLabel.height = lh;
453 }
454
455 if (item->HasImage())
456 {
457 int w, h;
458 m_owner->GetImageSize( item->GetImage(), w, h );
459 m_gi->m_rectIcon.width = w + 8;
460 m_gi->m_rectIcon.height = h + 8;
461
462 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
463 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
464 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
465 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
466 }
467
468 if ( item->HasText() )
469 {
470 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
471 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
472 }
473 else // no text, highlight the icon
474 {
475 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
476 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
477 }
478 break;
479
480 case wxLC_LIST:
481 s = item->GetTextForMeasuring();
482
483 dc->GetTextExtent( s, &lw, &lh );
484 lw += EXTRA_WIDTH;
485 lh += EXTRA_HEIGHT;
486
487 m_gi->m_rectLabel.width = lw;
488 m_gi->m_rectLabel.height = lh;
489
490 m_gi->m_rectAll.width = lw;
491 m_gi->m_rectAll.height = lh;
492
493 if (item->HasImage())
494 {
495 int w, h;
496 m_owner->GetImageSize( item->GetImage(), w, h );
497 m_gi->m_rectIcon.width = w;
498 m_gi->m_rectIcon.height = h;
499
500 m_gi->m_rectAll.width += 4 + w;
501 if (h > m_gi->m_rectAll.height)
502 m_gi->m_rectAll.height = h;
503 }
504
505 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
506 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
507 break;
508
509 case wxLC_REPORT:
510 wxFAIL_MSG( wxT("unexpected call to SetSize") );
511 break;
512
513 default:
514 wxFAIL_MSG( wxT("unknown mode") );
515 break;
516 }
517 }
518
519 void wxListLineData::SetPosition( int x, int y, int spacing )
520 {
521 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
522 wxCHECK_RET( node, wxT("no subitems at all??") );
523
524 wxListItemData *item = node->GetData();
525
526 switch ( GetMode() )
527 {
528 case wxLC_ICON:
529 case wxLC_SMALL_ICON:
530 m_gi->m_rectAll.x = x;
531 m_gi->m_rectAll.y = y;
532
533 if ( item->HasImage() )
534 {
535 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
536 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
537 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
538 }
539
540 if ( item->HasText() )
541 {
542 if (m_gi->m_rectAll.width > spacing)
543 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
544 else
545 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
546 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
547 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
548 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
549 }
550 else // no text, highlight the icon
551 {
552 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
553 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
554 }
555 break;
556
557 case wxLC_LIST:
558 m_gi->m_rectAll.x = x;
559 m_gi->m_rectAll.y = y;
560
561 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
562 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
563 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
564
565 if (item->HasImage())
566 {
567 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
568 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
569 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
570 }
571 else
572 {
573 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
574 }
575 break;
576
577 case wxLC_REPORT:
578 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
579 break;
580
581 default:
582 wxFAIL_MSG( wxT("unknown mode") );
583 break;
584 }
585 }
586
587 void wxListLineData::InitItems( int num )
588 {
589 for (int i = 0; i < num; i++)
590 m_items.Append( new wxListItemData(m_owner) );
591 }
592
593 void wxListLineData::SetItem( int index, const wxListItem &info )
594 {
595 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
596 wxCHECK_RET( node, wxT("invalid column index in SetItem") );
597
598 wxListItemData *item = node->GetData();
599 item->SetItem( info );
600 }
601
602 void wxListLineData::GetItem( int index, wxListItem &info )
603 {
604 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
605 if (node)
606 {
607 wxListItemData *item = node->GetData();
608 item->GetItem( info );
609 }
610 }
611
612 wxString wxListLineData::GetText(int index) const
613 {
614 wxString s;
615
616 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
617 if (node)
618 {
619 wxListItemData *item = node->GetData();
620 s = item->GetText();
621 }
622
623 return s;
624 }
625
626 void wxListLineData::SetText( int index, const wxString& s )
627 {
628 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
629 if (node)
630 {
631 wxListItemData *item = node->GetData();
632 item->SetText( s );
633 }
634 }
635
636 void wxListLineData::SetImage( int index, int image )
637 {
638 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
639 wxCHECK_RET( node, wxT("invalid column index in SetImage()") );
640
641 wxListItemData *item = node->GetData();
642 item->SetImage(image);
643 }
644
645 int wxListLineData::GetImage( int index ) const
646 {
647 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
648 wxCHECK_MSG( node, -1, wxT("invalid column index in GetImage()") );
649
650 wxListItemData *item = node->GetData();
651 return item->GetImage();
652 }
653
654 wxListItemAttr *wxListLineData::GetAttr() const
655 {
656 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
657 wxCHECK_MSG( node, NULL, wxT("invalid column index in GetAttr()") );
658
659 wxListItemData *item = node->GetData();
660 return item->GetAttr();
661 }
662
663 void wxListLineData::SetAttr(wxListItemAttr *attr)
664 {
665 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
666 wxCHECK_RET( node, wxT("invalid column index in SetAttr()") );
667
668 wxListItemData *item = node->GetData();
669 item->SetAttr(attr);
670 }
671
672 void wxListLineData::ApplyAttributes(wxDC *dc,
673 const wxRect& rectHL,
674 bool highlighted,
675 bool current)
676 {
677 const wxListItemAttr * const attr = GetAttr();
678
679 wxWindow * const listctrl = m_owner->GetParent();
680
681 const bool hasFocus = listctrl->HasFocus()
682 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
683 && IsControlActive( (ControlRef)listctrl->GetHandle() )
684 #endif
685 ;
686
687 // fg colour
688
689 // don't use foreground colour for drawing highlighted items - this might
690 // make them completely invisible (and there is no way to do bit
691 // arithmetics on wxColour, unfortunately)
692 wxColour colText;
693 if ( highlighted )
694 {
695 #ifdef __WXMAC__
696 if ( hasFocus )
697 colText = *wxWHITE;
698 else
699 colText = *wxBLACK;
700 #else
701 if ( hasFocus )
702 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
703 else
704 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT);
705 #endif
706 }
707 else if ( attr && attr->HasTextColour() )
708 colText = attr->GetTextColour();
709 else
710 colText = listctrl->GetForegroundColour();
711
712 dc->SetTextForeground(colText);
713
714 // font
715 wxFont font;
716 if ( attr && attr->HasFont() )
717 font = attr->GetFont();
718 else
719 font = listctrl->GetFont();
720
721 dc->SetFont(font);
722
723 // background
724 if ( highlighted )
725 {
726 // Use the renderer method to ensure that the selected items use the
727 // native look.
728 int flags = wxCONTROL_SELECTED;
729 if ( hasFocus )
730 flags |= wxCONTROL_FOCUSED;
731 if (current)
732 flags |= wxCONTROL_CURRENT;
733 wxRendererNative::Get().
734 DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
735 }
736 else if ( attr && attr->HasBackgroundColour() )
737 {
738 // Draw the background using the items custom background colour.
739 dc->SetBrush(attr->GetBackgroundColour());
740 dc->SetPen(*wxTRANSPARENT_PEN);
741 dc->DrawRectangle(rectHL);
742 }
743
744 // just for debugging to better see where the items are
745 #if 0
746 dc->SetPen(*wxRED_PEN);
747 dc->SetBrush(*wxTRANSPARENT_BRUSH);
748 dc->DrawRectangle( m_gi->m_rectAll );
749 dc->SetPen(*wxGREEN_PEN);
750 dc->DrawRectangle( m_gi->m_rectIcon );
751 #endif
752 }
753
754 void wxListLineData::Draw(wxDC *dc, bool current)
755 {
756 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
757 wxCHECK_RET( node, wxT("no subitems at all??") );
758
759 ApplyAttributes(dc, m_gi->m_rectHighlight, IsHighlighted(), current);
760
761 wxListItemData *item = node->GetData();
762 if (item->HasImage())
763 {
764 // centre the image inside our rectangle, this looks nicer when items
765 // ae aligned in a row
766 const wxRect& rectIcon = m_gi->m_rectIcon;
767
768 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
769 }
770
771 if (item->HasText())
772 {
773 const wxRect& rectLabel = m_gi->m_rectLabel;
774
775 wxDCClipper clipper(*dc, rectLabel);
776 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
777 }
778 }
779
780 void wxListLineData::DrawInReportMode( wxDC *dc,
781 const wxRect& rect,
782 const wxRect& rectHL,
783 bool highlighted,
784 bool current )
785 {
786 // TODO: later we should support setting different attributes for
787 // different columns - to do it, just add "col" argument to
788 // GetAttr() and move these lines into the loop below
789
790 ApplyAttributes(dc, rectHL, highlighted, current);
791
792 wxCoord x = rect.x + HEADER_OFFSET_X,
793 yMid = rect.y + rect.height/2;
794 #ifdef __WXGTK__
795 // This probably needs to be done
796 // on all platforms as the icons
797 // otherwise nearly touch the border
798 x += 2;
799 #endif
800
801 size_t col = 0;
802 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
803 node;
804 node = node->GetNext(), col++ )
805 {
806 wxListItemData *item = node->GetData();
807
808 int width = m_owner->GetColumnWidth(col);
809 int xOld = x;
810 x += width;
811
812 width -= 8;
813 const int wText = width;
814 wxDCClipper clipper(*dc, xOld, rect.y, wText, rect.height);
815
816 if ( item->HasImage() )
817 {
818 int ix, iy;
819 m_owner->GetImageSize( item->GetImage(), ix, iy );
820 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
821
822 ix += IMAGE_MARGIN_IN_REPORT_MODE;
823
824 xOld += ix;
825 width -= ix;
826 }
827
828 if ( item->HasText() )
829 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width);
830 }
831 }
832
833 void wxListLineData::DrawTextFormatted(wxDC *dc,
834 const wxString& textOrig,
835 int col,
836 int x,
837 int yMid,
838 int width)
839 {
840 // we don't support displaying multiple lines currently (and neither does
841 // wxMSW FWIW) so just merge all the lines
842 wxString text(textOrig);
843 text.Replace(wxT("\n"), wxT(" "));
844
845 wxCoord w, h;
846 dc->GetTextExtent(text, &w, &h);
847
848 const wxCoord y = yMid - (h + 1)/2;
849
850 wxDCClipper clipper(*dc, x, y, width, h);
851
852 // determine if the string can fit inside the current width
853 if (w <= width)
854 {
855 // it can, draw it using the items alignment
856 wxListItem item;
857 m_owner->GetColumn(col, item);
858 switch ( item.GetAlign() )
859 {
860 case wxLIST_FORMAT_LEFT:
861 // nothing to do
862 break;
863
864 case wxLIST_FORMAT_RIGHT:
865 x += width - w;
866 break;
867
868 case wxLIST_FORMAT_CENTER:
869 x += (width - w) / 2;
870 break;
871
872 default:
873 wxFAIL_MSG( wxT("unknown list item format") );
874 break;
875 }
876
877 dc->DrawText(text, x, y);
878 }
879 else // otherwise, truncate and add an ellipsis if possible
880 {
881 // determine the base width
882 wxString ellipsis(wxT("..."));
883 wxCoord base_w;
884 dc->GetTextExtent(ellipsis, &base_w, &h);
885
886 // continue until we have enough space or only one character left
887 wxCoord w_c, h_c;
888 size_t len = text.length();
889 wxString drawntext = text.Left(len);
890 while (len > 1)
891 {
892 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
893 drawntext.RemoveLast();
894 len--;
895 w -= w_c;
896 if (w + base_w <= width)
897 break;
898 }
899
900 // if still not enough space, remove ellipsis characters
901 while (ellipsis.length() > 0 && w + base_w > width)
902 {
903 ellipsis = ellipsis.Left(ellipsis.length() - 1);
904 dc->GetTextExtent(ellipsis, &base_w, &h);
905 }
906
907 // now draw the text
908 dc->DrawText(drawntext, x, y);
909 dc->DrawText(ellipsis, x + w, y);
910 }
911 }
912
913 bool wxListLineData::Highlight( bool on )
914 {
915 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
916
917 if ( on == m_highlighted )
918 return false;
919
920 m_highlighted = on;
921
922 return true;
923 }
924
925 void wxListLineData::ReverseHighlight( void )
926 {
927 Highlight(!IsHighlighted());
928 }
929
930 //-----------------------------------------------------------------------------
931 // wxListHeaderWindow
932 //-----------------------------------------------------------------------------
933
934 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
935 EVT_PAINT (wxListHeaderWindow::OnPaint)
936 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
937 END_EVENT_TABLE()
938
939 void wxListHeaderWindow::Init()
940 {
941 m_currentCursor = NULL;
942 m_isDragging = false;
943 m_dirty = false;
944 m_sendSetColumnWidth = false;
945 }
946
947 wxListHeaderWindow::wxListHeaderWindow()
948 {
949 Init();
950
951 m_owner = NULL;
952 m_resizeCursor = NULL;
953 }
954
955 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
956 wxWindowID id,
957 wxListMainWindow *owner,
958 const wxPoint& pos,
959 const wxSize& size,
960 long style,
961 const wxString &name )
962 : wxWindow( win, id, pos, size, style, name )
963 {
964 Init();
965
966 m_owner = owner;
967 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
968
969 #if _USE_VISATTR
970 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
971 SetOwnForegroundColour( attr.colFg );
972 SetOwnBackgroundColour( attr.colBg );
973 if (!m_hasFont)
974 SetOwnFont( attr.font );
975 #else
976 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
977 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
978 if (!m_hasFont)
979 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
980 #endif
981 }
982
983 wxListHeaderWindow::~wxListHeaderWindow()
984 {
985 delete m_resizeCursor;
986 }
987
988 #ifdef __WXUNIVERSAL__
989 #include "wx/univ/renderer.h"
990 #include "wx/univ/theme.h"
991 #endif
992
993 // shift the DC origin to match the position of the main window horz
994 // scrollbar: this allows us to always use logical coords
995 void wxListHeaderWindow::AdjustDC(wxDC& dc)
996 {
997 wxGenericListCtrl *parent = m_owner->GetListCtrl();
998
999 int xpix;
1000 parent->GetScrollPixelsPerUnit( &xpix, NULL );
1001
1002 int view_start;
1003 parent->GetViewStart( &view_start, NULL );
1004
1005
1006 int org_x = 0;
1007 int org_y = 0;
1008 dc.GetDeviceOrigin( &org_x, &org_y );
1009
1010 // account for the horz scrollbar offset
1011 #ifdef __WXGTK__
1012 if (GetLayoutDirection() == wxLayout_RightToLeft)
1013 {
1014 // Maybe we just have to check for m_signX
1015 // in the DC, but I leave the #ifdef __WXGTK__
1016 // for now
1017 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1018 }
1019 else
1020 #endif
1021 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1022 }
1023
1024 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1025 {
1026 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1027
1028 wxPaintDC dc( this );
1029
1030 AdjustDC( dc );
1031
1032 dc.SetFont( GetFont() );
1033
1034 // width and height of the entire header window
1035 int w, h;
1036 GetClientSize( &w, &h );
1037 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1038
1039 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
1040 dc.SetTextForeground(GetForegroundColour());
1041
1042 int x = HEADER_OFFSET_X;
1043 int numColumns = m_owner->GetColumnCount();
1044 wxListItem item;
1045 for ( int i = 0; i < numColumns && x < w; i++ )
1046 {
1047 m_owner->GetColumn( i, item );
1048 int wCol = item.m_width;
1049
1050 int cw = wCol;
1051 int ch = h;
1052
1053 int flags = 0;
1054 if (!m_parent->IsEnabled())
1055 flags |= wxCONTROL_DISABLED;
1056
1057 // NB: The code below is not really Mac-specific, but since we are close
1058 // to 2.8 release and I don't have time to test on other platforms, I
1059 // defined this only for wxMac. If this behaviour is desired on
1060 // other platforms, please go ahead and revise or remove the #ifdef.
1061 #ifdef __WXMAC__
1062 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1063 (item.m_state & wxLIST_STATE_SELECTED) )
1064 flags |= wxCONTROL_SELECTED;
1065 #endif
1066
1067 if (i == 0)
1068 flags |= wxCONTROL_SPECIAL; // mark as first column
1069
1070 wxRendererNative::Get().DrawHeaderButton
1071 (
1072 this,
1073 dc,
1074 wxRect(x, HEADER_OFFSET_Y, cw, ch),
1075 flags
1076 );
1077
1078 // see if we have enough space for the column label
1079
1080 // for this we need the width of the text
1081 wxCoord wLabel;
1082 wxCoord hLabel;
1083 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1084 wLabel += 2 * EXTRA_WIDTH;
1085
1086 // and the width of the icon, if any
1087 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
1088 const int image = item.m_image;
1089 wxImageList *imageList;
1090 if ( image != -1 )
1091 {
1092 imageList = m_owner->GetSmallImageList();
1093 if ( imageList )
1094 {
1095 imageList->GetSize(image, ix, iy);
1096 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1097 }
1098 }
1099 else
1100 {
1101 imageList = NULL;
1102 }
1103
1104 // ignore alignment if there is not enough space anyhow
1105 int xAligned;
1106 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1107 {
1108 default:
1109 wxFAIL_MSG( wxT("unknown list item format") );
1110 // fall through
1111
1112 case wxLIST_FORMAT_LEFT:
1113 xAligned = x;
1114 break;
1115
1116 case wxLIST_FORMAT_RIGHT:
1117 xAligned = x + cw - wLabel;
1118 break;
1119
1120 case wxLIST_FORMAT_CENTER:
1121 xAligned = x + (cw - wLabel) / 2;
1122 break;
1123 }
1124
1125 // draw the text and image clipping them so that they
1126 // don't overwrite the column boundary
1127 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h);
1128
1129 // if we have an image, draw it on the right of the label
1130 if ( imageList )
1131 {
1132 imageList->Draw
1133 (
1134 image,
1135 dc,
1136 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1137 HEADER_OFFSET_Y + (h - iy)/2,
1138 wxIMAGELIST_DRAW_TRANSPARENT
1139 );
1140 }
1141
1142 dc.DrawText( item.GetText(),
1143 xAligned + EXTRA_WIDTH, (h - hLabel) / 2 );
1144
1145 x += wCol;
1146 }
1147
1148 // Fill in what's missing to the right of the columns, otherwise we will
1149 // leave an unpainted area when columns are removed (and it looks better)
1150 if ( x < w )
1151 {
1152 wxRendererNative::Get().DrawHeaderButton
1153 (
1154 this,
1155 dc,
1156 wxRect(x, HEADER_OFFSET_Y, w - x, h),
1157 wxCONTROL_DIRTY // mark as last column
1158 );
1159 }
1160 }
1161
1162 void wxListHeaderWindow::OnInternalIdle()
1163 {
1164 wxWindow::OnInternalIdle();
1165
1166 if (m_sendSetColumnWidth)
1167 {
1168 m_owner->SetColumnWidth( m_colToSend, m_widthToSend );
1169 m_sendSetColumnWidth = false;
1170 }
1171 }
1172
1173 void wxListHeaderWindow::DrawCurrent()
1174 {
1175 #if 1
1176 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1177 m_sendSetColumnWidth = true;
1178 m_colToSend = m_column;
1179 m_widthToSend = m_currentX - m_minX;
1180 #else
1181 int x1 = m_currentX;
1182 int y1 = 0;
1183 m_owner->ClientToScreen( &x1, &y1 );
1184
1185 int x2 = m_currentX;
1186 int y2 = 0;
1187 m_owner->GetClientSize( NULL, &y2 );
1188 m_owner->ClientToScreen( &x2, &y2 );
1189
1190 wxScreenDC dc;
1191 dc.SetLogicalFunction( wxINVERT );
1192 dc.SetPen( wxPen(*wxBLACK, 2) );
1193 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1194
1195 AdjustDC(dc);
1196
1197 dc.DrawLine( x1, y1, x2, y2 );
1198
1199 dc.SetLogicalFunction( wxCOPY );
1200
1201 dc.SetPen( wxNullPen );
1202 dc.SetBrush( wxNullBrush );
1203 #endif
1204 }
1205
1206 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1207 {
1208 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1209
1210 // we want to work with logical coords
1211 int x;
1212 parent->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1213 int y = event.GetY();
1214
1215 if (m_isDragging)
1216 {
1217 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
1218
1219 // we don't draw the line beyond our window, but we allow dragging it
1220 // there
1221 int w = 0;
1222 GetClientSize( &w, NULL );
1223 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1224 w -= 6;
1225
1226 // erase the line if it was drawn
1227 if ( m_currentX < w )
1228 DrawCurrent();
1229
1230 if (event.ButtonUp())
1231 {
1232 ReleaseMouse();
1233 m_isDragging = false;
1234 m_dirty = true;
1235 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1236 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
1237 }
1238 else
1239 {
1240 if (x > m_minX + 7)
1241 m_currentX = x;
1242 else
1243 m_currentX = m_minX + 7;
1244
1245 // draw in the new location
1246 if ( m_currentX < w )
1247 DrawCurrent();
1248 }
1249 }
1250 else // not dragging
1251 {
1252 m_minX = 0;
1253 bool hit_border = false;
1254
1255 // end of the current column
1256 int xpos = 0;
1257
1258 // find the column where this event occurred
1259 int col,
1260 countCol = m_owner->GetColumnCount();
1261 for (col = 0; col < countCol; col++)
1262 {
1263 xpos += m_owner->GetColumnWidth( col );
1264 m_column = col;
1265
1266 if ( (abs(x-xpos) < 3) && (y < 22) )
1267 {
1268 // near the column border
1269 hit_border = true;
1270 break;
1271 }
1272
1273 if ( x < xpos )
1274 {
1275 // inside the column
1276 break;
1277 }
1278
1279 m_minX = xpos;
1280 }
1281
1282 if ( col == countCol )
1283 m_column = -1;
1284
1285 if (event.LeftDown() || event.RightUp())
1286 {
1287 if (hit_border && event.LeftDown())
1288 {
1289 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
1290 event.GetPosition()) )
1291 {
1292 m_isDragging = true;
1293 m_currentX = x;
1294 CaptureMouse();
1295 DrawCurrent();
1296 }
1297 //else: column resizing was vetoed by the user code
1298 }
1299 else // click on a column
1300 {
1301 // record the selected state of the columns
1302 if (event.LeftDown())
1303 {
1304 for (int i=0; i < m_owner->GetColumnCount(); i++)
1305 {
1306 wxListItem colItem;
1307 m_owner->GetColumn(i, colItem);
1308 long state = colItem.GetState();
1309 if (i == m_column)
1310 colItem.SetState(state | wxLIST_STATE_SELECTED);
1311 else
1312 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
1313 m_owner->SetColumn(i, colItem);
1314 }
1315 }
1316
1317 SendListEvent( event.LeftDown()
1318 ? wxEVT_COMMAND_LIST_COL_CLICK
1319 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
1320 event.GetPosition());
1321 }
1322 }
1323 else if (event.Moving())
1324 {
1325 bool setCursor;
1326 if (hit_border)
1327 {
1328 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1329 m_currentCursor = m_resizeCursor;
1330 }
1331 else
1332 {
1333 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1334 m_currentCursor = wxSTANDARD_CURSOR;
1335 }
1336
1337 if ( setCursor )
1338 SetCursor(*m_currentCursor);
1339 }
1340 }
1341 }
1342
1343 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
1344 {
1345 wxWindow *parent = GetParent();
1346 wxListEvent le( type, parent->GetId() );
1347 le.SetEventObject( parent );
1348 le.m_pointDrag = pos;
1349
1350 // the position should be relative to the parent window, not
1351 // this one for compatibility with MSW and common sense: the
1352 // user code doesn't know anything at all about this header
1353 // window, so why should it get positions relative to it?
1354 le.m_pointDrag.y -= GetSize().y;
1355
1356 le.m_col = m_column;
1357 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
1358 }
1359
1360 //-----------------------------------------------------------------------------
1361 // wxListRenameTimer (internal)
1362 //-----------------------------------------------------------------------------
1363
1364 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1365 {
1366 m_owner = owner;
1367 }
1368
1369 void wxListRenameTimer::Notify()
1370 {
1371 m_owner->OnRenameTimer();
1372 }
1373
1374 //-----------------------------------------------------------------------------
1375 // wxListFindTimer (internal)
1376 //-----------------------------------------------------------------------------
1377
1378 void wxListFindTimer::Notify()
1379 {
1380 m_owner->OnFindTimer();
1381 }
1382
1383 //-----------------------------------------------------------------------------
1384 // wxListTextCtrlWrapper (internal)
1385 //-----------------------------------------------------------------------------
1386
1387 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
1388 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
1389 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
1390 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
1391 END_EVENT_TABLE()
1392
1393 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
1394 wxTextCtrl *text,
1395 size_t itemEdit)
1396 : m_startValue(owner->GetItemText(itemEdit)),
1397 m_itemEdited(itemEdit)
1398 {
1399 m_owner = owner;
1400 m_text = text;
1401 m_aboutToFinish = false;
1402
1403 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1404
1405 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
1406
1407 parent->CalcScrolledPosition(rectLabel.x, rectLabel.y,
1408 &rectLabel.x, &rectLabel.y);
1409
1410 m_text->Create(owner, wxID_ANY, m_startValue,
1411 wxPoint(rectLabel.x-4,rectLabel.y-4),
1412 wxSize(rectLabel.width+11,rectLabel.height+8));
1413 m_text->SetFocus();
1414
1415 m_text->PushEventHandler(this);
1416 }
1417
1418 void wxListTextCtrlWrapper::EndEdit(EndReason reason)
1419 {
1420 m_aboutToFinish = true;
1421
1422 switch ( reason )
1423 {
1424 case End_Accept:
1425 // Notify the owner about the changes
1426 AcceptChanges();
1427
1428 // Even if vetoed, close the control (consistent with MSW)
1429 Finish( true );
1430 break;
1431
1432 case End_Discard:
1433 m_owner->OnRenameCancelled(m_itemEdited);
1434
1435 Finish( true );
1436 break;
1437
1438 case End_Destroy:
1439 // Don't generate any notifications for the control being destroyed
1440 // and don't set focus to it neither.
1441 Finish(false);
1442 break;
1443 }
1444 }
1445
1446 void wxListTextCtrlWrapper::Finish( bool setfocus )
1447 {
1448 m_text->RemoveEventHandler(this);
1449 m_owner->ResetTextControl( m_text );
1450
1451 wxPendingDelete.Append( this );
1452
1453 if (setfocus)
1454 m_owner->SetFocus();
1455 }
1456
1457 bool wxListTextCtrlWrapper::AcceptChanges()
1458 {
1459 const wxString value = m_text->GetValue();
1460
1461 // notice that we should always call OnRenameAccept() to generate the "end
1462 // label editing" event, even if the user hasn't really changed anything
1463 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
1464 {
1465 // vetoed by the user
1466 return false;
1467 }
1468
1469 // accepted, do rename the item (unless nothing changed)
1470 if ( value != m_startValue )
1471 m_owner->SetItemText(m_itemEdited, value);
1472
1473 return true;
1474 }
1475
1476 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
1477 {
1478 if ( !CheckForEndEditKey(event) )
1479 event.Skip();
1480 }
1481
1482 bool wxListTextCtrlWrapper::CheckForEndEditKey(const wxKeyEvent& event)
1483 {
1484 switch ( event.m_keyCode )
1485 {
1486 case WXK_RETURN:
1487 EndEdit( End_Accept );
1488 break;
1489
1490 case WXK_ESCAPE:
1491 EndEdit( End_Discard );
1492 break;
1493
1494 default:
1495 return false;
1496 }
1497
1498 return true;
1499 }
1500
1501 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
1502 {
1503 if (m_aboutToFinish)
1504 {
1505 // auto-grow the textctrl:
1506 wxSize parentSize = m_owner->GetSize();
1507 wxPoint myPos = m_text->GetPosition();
1508 wxSize mySize = m_text->GetSize();
1509 int sx, sy;
1510 m_text->GetTextExtent(m_text->GetValue() + wxT("MM"), &sx, &sy);
1511 if (myPos.x + sx > parentSize.x)
1512 sx = parentSize.x - myPos.x;
1513 if (mySize.x > sx)
1514 sx = mySize.x;
1515 m_text->SetSize(sx, wxDefaultCoord);
1516 }
1517
1518 event.Skip();
1519 }
1520
1521 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
1522 {
1523 if ( !m_aboutToFinish )
1524 {
1525 if ( !AcceptChanges() )
1526 m_owner->OnRenameCancelled( m_itemEdited );
1527
1528 Finish( false );
1529 }
1530
1531 // We must let the native text control handle focus
1532 event.Skip();
1533 }
1534
1535 //-----------------------------------------------------------------------------
1536 // wxListMainWindow
1537 //-----------------------------------------------------------------------------
1538
1539 BEGIN_EVENT_TABLE(wxListMainWindow, wxWindow)
1540 EVT_PAINT (wxListMainWindow::OnPaint)
1541 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
1542 EVT_CHAR_HOOK (wxListMainWindow::OnCharHook)
1543 EVT_CHAR (wxListMainWindow::OnChar)
1544 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
1545 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
1546 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
1547 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
1548 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
1549 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
1550 END_EVENT_TABLE()
1551
1552 void wxListMainWindow::Init()
1553 {
1554 m_dirty = true;
1555 m_countVirt = 0;
1556 m_lineFrom =
1557 m_lineTo = (size_t)-1;
1558 m_linesPerPage = 0;
1559
1560 m_headerWidth =
1561 m_lineHeight = 0;
1562
1563 m_small_image_list = NULL;
1564 m_normal_image_list = NULL;
1565
1566 m_small_spacing = 30;
1567 m_normal_spacing = 40;
1568
1569 m_hasFocus = false;
1570 m_dragCount = 0;
1571 m_isCreated = false;
1572
1573 m_lastOnSame = false;
1574 m_renameTimer = new wxListRenameTimer( this );
1575 m_findTimer = NULL;
1576 m_findBell = 0; // default is to not ring bell at all
1577 m_textctrlWrapper = NULL;
1578
1579 m_current =
1580 m_lineLastClicked =
1581 m_lineSelectSingleOnUp =
1582 m_lineBeforeLastClicked = (size_t)-1;
1583 }
1584
1585 wxListMainWindow::wxListMainWindow()
1586 {
1587 Init();
1588
1589 m_highlightBrush =
1590 m_highlightUnfocusedBrush = NULL;
1591 }
1592
1593 wxListMainWindow::wxListMainWindow( wxWindow *parent,
1594 wxWindowID id,
1595 const wxPoint& pos,
1596 const wxSize& size )
1597 : wxWindow( parent, id, pos, size,
1598 wxWANTS_CHARS | wxBORDER_NONE )
1599 {
1600 Init();
1601
1602 m_highlightBrush = new wxBrush
1603 (
1604 wxSystemSettings::GetColour
1605 (
1606 wxSYS_COLOUR_HIGHLIGHT
1607 ),
1608 wxBRUSHSTYLE_SOLID
1609 );
1610
1611 m_highlightUnfocusedBrush = new wxBrush
1612 (
1613 wxSystemSettings::GetColour
1614 (
1615 wxSYS_COLOUR_BTNSHADOW
1616 ),
1617 wxBRUSHSTYLE_SOLID
1618 );
1619
1620 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
1621 SetOwnForegroundColour( attr.colFg );
1622 SetOwnBackgroundColour( attr.colBg );
1623 if (!m_hasFont)
1624 SetOwnFont( attr.font );
1625 }
1626
1627 wxListMainWindow::~wxListMainWindow()
1628 {
1629 if ( m_textctrlWrapper )
1630 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
1631
1632 DoDeleteAllItems();
1633 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
1634 WX_CLEAR_ARRAY(m_aColWidths);
1635
1636 delete m_highlightBrush;
1637 delete m_highlightUnfocusedBrush;
1638 delete m_renameTimer;
1639 delete m_findTimer;
1640 }
1641
1642 void wxListMainWindow::SetReportView(bool inReportView)
1643 {
1644 const size_t count = m_lines.size();
1645 for ( size_t n = 0; n < count; n++ )
1646 {
1647 m_lines[n].SetReportView(inReportView);
1648 }
1649 }
1650
1651 void wxListMainWindow::CacheLineData(size_t line)
1652 {
1653 wxGenericListCtrl *listctrl = GetListCtrl();
1654
1655 wxListLineData *ld = GetDummyLine();
1656
1657 size_t countCol = GetColumnCount();
1658 for ( size_t col = 0; col < countCol; col++ )
1659 {
1660 ld->SetText(col, listctrl->OnGetItemText(line, col));
1661 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
1662 }
1663
1664 ld->SetAttr(listctrl->OnGetItemAttr(line));
1665 }
1666
1667 wxListLineData *wxListMainWindow::GetDummyLine() const
1668 {
1669 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1670 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1671
1672 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1673
1674 // we need to recreate the dummy line if the number of columns in the
1675 // control changed as it would have the incorrect number of fields
1676 // otherwise
1677 if ( !m_lines.IsEmpty() &&
1678 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
1679 {
1680 self->m_lines.Clear();
1681 }
1682
1683 if ( m_lines.IsEmpty() )
1684 {
1685 wxListLineData *line = new wxListLineData(self);
1686 self->m_lines.Add(line);
1687
1688 // don't waste extra memory -- there never going to be anything
1689 // else/more in this array
1690 self->m_lines.Shrink();
1691 }
1692
1693 return &m_lines[0];
1694 }
1695
1696 // ----------------------------------------------------------------------------
1697 // line geometry (report mode only)
1698 // ----------------------------------------------------------------------------
1699
1700 wxCoord wxListMainWindow::GetLineHeight() const
1701 {
1702 // we cache the line height as calling GetTextExtent() is slow
1703 if ( !m_lineHeight )
1704 {
1705 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1706
1707 wxClientDC dc( self );
1708 dc.SetFont( GetFont() );
1709
1710 wxCoord y;
1711 dc.GetTextExtent(wxT("H"), NULL, &y);
1712
1713 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1714 {
1715 int iw = 0, ih = 0;
1716 m_small_image_list->GetSize(0, iw, ih);
1717 y = wxMax(y, ih);
1718 }
1719
1720 y += EXTRA_HEIGHT;
1721 self->m_lineHeight = y + LINE_SPACING;
1722 }
1723
1724 return m_lineHeight;
1725 }
1726
1727 wxCoord wxListMainWindow::GetLineY(size_t line) const
1728 {
1729 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1730
1731 return LINE_SPACING + line * GetLineHeight();
1732 }
1733
1734 wxRect wxListMainWindow::GetLineRect(size_t line) const
1735 {
1736 if ( !InReportView() )
1737 return GetLine(line)->m_gi->m_rectAll;
1738
1739 wxRect rect;
1740 rect.x = HEADER_OFFSET_X;
1741 rect.y = GetLineY(line);
1742 rect.width = GetHeaderWidth();
1743 rect.height = GetLineHeight();
1744
1745 return rect;
1746 }
1747
1748 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1749 {
1750 if ( !InReportView() )
1751 return GetLine(line)->m_gi->m_rectLabel;
1752
1753 int image_x = 0;
1754 wxListLineData *data = GetLine(line);
1755 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1756 if (node)
1757 {
1758 wxListItemData *item = node->GetData();
1759 if ( item->HasImage() )
1760 {
1761 int ix, iy;
1762 GetImageSize( item->GetImage(), ix, iy );
1763 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1764 }
1765 }
1766
1767 wxRect rect;
1768 rect.x = image_x + HEADER_OFFSET_X;
1769 rect.y = GetLineY(line);
1770 rect.width = GetColumnWidth(0) - image_x;
1771 rect.height = GetLineHeight();
1772
1773 return rect;
1774 }
1775
1776 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1777 {
1778 if ( !InReportView() )
1779 return GetLine(line)->m_gi->m_rectIcon;
1780
1781 wxListLineData *ld = GetLine(line);
1782 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
1783
1784 wxRect rect;
1785 rect.x = HEADER_OFFSET_X;
1786 rect.y = GetLineY(line);
1787 GetImageSize(ld->GetImage(), rect.width, rect.height);
1788
1789 return rect;
1790 }
1791
1792 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1793 {
1794 return InReportView() ? GetLineRect(line)
1795 : GetLine(line)->m_gi->m_rectHighlight;
1796 }
1797
1798 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1799 {
1800 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
1801
1802 wxListLineData *ld = GetLine(line);
1803
1804 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
1805 return wxLIST_HITTEST_ONITEMICON;
1806
1807 // VS: Testing for "ld->HasText() || InReportView()" instead of
1808 // "ld->HasText()" is needed to make empty lines in report view
1809 // possible
1810 if ( ld->HasText() || InReportView() )
1811 {
1812 wxRect rect = InReportView() ? GetLineRect(line)
1813 : GetLineLabelRect(line);
1814
1815 if ( rect.Contains(x, y) )
1816 return wxLIST_HITTEST_ONITEMLABEL;
1817 }
1818
1819 return 0;
1820 }
1821
1822 // ----------------------------------------------------------------------------
1823 // highlight (selection) handling
1824 // ----------------------------------------------------------------------------
1825
1826 bool wxListMainWindow::IsHighlighted(size_t line) const
1827 {
1828 if ( IsVirtual() )
1829 {
1830 return m_selStore.IsSelected(line);
1831 }
1832 else // !virtual
1833 {
1834 wxListLineData *ld = GetLine(line);
1835 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
1836
1837 return ld->IsHighlighted();
1838 }
1839 }
1840
1841 void wxListMainWindow::HighlightLines( size_t lineFrom,
1842 size_t lineTo,
1843 bool highlight )
1844 {
1845 if ( IsVirtual() )
1846 {
1847 wxArrayInt linesChanged;
1848 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1849 &linesChanged) )
1850 {
1851 // meny items changed state, refresh everything
1852 RefreshLines(lineFrom, lineTo);
1853 }
1854 else // only a few items changed state, refresh only them
1855 {
1856 size_t count = linesChanged.GetCount();
1857 for ( size_t n = 0; n < count; n++ )
1858 {
1859 RefreshLine(linesChanged[n]);
1860 }
1861 }
1862 }
1863 else // iterate over all items in non report view
1864 {
1865 for ( size_t line = lineFrom; line <= lineTo; line++ )
1866 {
1867 if ( HighlightLine(line, highlight) )
1868 RefreshLine(line);
1869 }
1870 }
1871 }
1872
1873 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1874 {
1875 bool changed;
1876
1877 if ( IsVirtual() )
1878 {
1879 changed = m_selStore.SelectItem(line, highlight);
1880 }
1881 else // !virtual
1882 {
1883 wxListLineData *ld = GetLine(line);
1884 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
1885
1886 changed = ld->Highlight(highlight);
1887 }
1888
1889 if ( changed )
1890 {
1891 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1892 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1893 }
1894
1895 return changed;
1896 }
1897
1898 void wxListMainWindow::RefreshLine( size_t line )
1899 {
1900 if ( InReportView() )
1901 {
1902 size_t visibleFrom, visibleTo;
1903 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1904
1905 if ( line < visibleFrom || line > visibleTo )
1906 return;
1907 }
1908
1909 wxRect rect = GetLineRect(line);
1910
1911 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1912 RefreshRect( rect );
1913 }
1914
1915 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1916 {
1917 // we suppose that they are ordered by caller
1918 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
1919
1920 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
1921
1922 if ( InReportView() )
1923 {
1924 size_t visibleFrom, visibleTo;
1925 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1926
1927 if ( lineFrom < visibleFrom )
1928 lineFrom = visibleFrom;
1929 if ( lineTo > visibleTo )
1930 lineTo = visibleTo;
1931
1932 wxRect rect;
1933 rect.x = 0;
1934 rect.y = GetLineY(lineFrom);
1935 rect.width = GetClientSize().x;
1936 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
1937
1938 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1939 RefreshRect( rect );
1940 }
1941 else // !report
1942 {
1943 // TODO: this should be optimized...
1944 for ( size_t line = lineFrom; line <= lineTo; line++ )
1945 {
1946 RefreshLine(line);
1947 }
1948 }
1949 }
1950
1951 void wxListMainWindow::RefreshAfter( size_t lineFrom )
1952 {
1953 if ( InReportView() )
1954 {
1955 size_t visibleFrom, visibleTo;
1956 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1957
1958 if ( lineFrom < visibleFrom )
1959 lineFrom = visibleFrom;
1960 else if ( lineFrom > visibleTo )
1961 return;
1962
1963 wxRect rect;
1964 rect.x = 0;
1965 rect.y = GetLineY(lineFrom);
1966 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1967
1968 wxSize size = GetClientSize();
1969 rect.width = size.x;
1970
1971 // refresh till the bottom of the window
1972 rect.height = size.y - rect.y;
1973
1974 RefreshRect( rect );
1975 }
1976 else // !report
1977 {
1978 // TODO: how to do it more efficiently?
1979 m_dirty = true;
1980 }
1981 }
1982
1983 void wxListMainWindow::RefreshSelected()
1984 {
1985 if ( IsEmpty() )
1986 return;
1987
1988 size_t from, to;
1989 if ( InReportView() )
1990 {
1991 GetVisibleLinesRange(&from, &to);
1992 }
1993 else // !virtual
1994 {
1995 from = 0;
1996 to = GetItemCount() - 1;
1997 }
1998
1999 if ( HasCurrent() && m_current >= from && m_current <= to )
2000 RefreshLine(m_current);
2001
2002 for ( size_t line = from; line <= to; line++ )
2003 {
2004 // NB: the test works as expected even if m_current == -1
2005 if ( line != m_current && IsHighlighted(line) )
2006 RefreshLine(line);
2007 }
2008 }
2009
2010 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2011 {
2012 // Note: a wxPaintDC must be constructed even if no drawing is
2013 // done (a Windows requirement).
2014 wxPaintDC dc( this );
2015
2016 if ( IsEmpty() )
2017 {
2018 // nothing to draw or not the moment to draw it
2019 return;
2020 }
2021
2022 if ( m_dirty )
2023 RecalculatePositions( false );
2024
2025 GetListCtrl()->PrepareDC( dc );
2026
2027 int dev_x, dev_y;
2028 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2029
2030 dc.SetFont( GetFont() );
2031
2032 if ( InReportView() )
2033 {
2034 int lineHeight = GetLineHeight();
2035
2036 size_t visibleFrom, visibleTo;
2037 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2038
2039 wxRect rectLine;
2040 int xOrig = dc.LogicalToDeviceX( 0 );
2041 int yOrig = dc.LogicalToDeviceY( 0 );
2042
2043 // tell the caller cache to cache the data
2044 if ( IsVirtual() )
2045 {
2046 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2047 GetParent()->GetId());
2048 evCache.SetEventObject( GetParent() );
2049 evCache.m_oldItemIndex = visibleFrom;
2050 evCache.m_item.m_itemId =
2051 evCache.m_itemIndex = visibleTo;
2052 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2053 }
2054
2055 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2056 {
2057 rectLine = GetLineRect(line);
2058
2059
2060 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2061 rectLine.width, rectLine.height) )
2062 {
2063 // don't redraw unaffected lines to avoid flicker
2064 continue;
2065 }
2066
2067 GetLine(line)->DrawInReportMode( &dc,
2068 rectLine,
2069 GetLineHighlightRect(line),
2070 IsHighlighted(line),
2071 line == m_current );
2072 }
2073
2074 if ( HasFlag(wxLC_HRULES) )
2075 {
2076 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2077 wxSize clientSize = GetClientSize();
2078
2079 size_t i = visibleFrom;
2080 if (i == 0) i = 1; // Don't draw the first one
2081 for ( ; i <= visibleTo; i++ )
2082 {
2083 dc.SetPen(pen);
2084 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2085 dc.DrawLine(0 - dev_x, i * lineHeight,
2086 clientSize.x - dev_x, i * lineHeight);
2087 }
2088
2089 // Draw last horizontal rule
2090 if ( visibleTo == GetItemCount() - 1 )
2091 {
2092 dc.SetPen( pen );
2093 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2094 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2095 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2096 }
2097 }
2098
2099 // Draw vertical rules if required
2100 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2101 {
2102 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2103 wxRect firstItemRect, lastItemRect;
2104
2105 GetItemRect(visibleFrom, firstItemRect);
2106 GetItemRect(visibleTo, lastItemRect);
2107 int x = firstItemRect.GetX();
2108 dc.SetPen(pen);
2109 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2110
2111 for (int col = 0; col < GetColumnCount(); col++)
2112 {
2113 int colWidth = GetColumnWidth(col);
2114 x += colWidth;
2115 int x_pos = x - dev_x;
2116 if (col < GetColumnCount()-1) x_pos -= 2;
2117 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2118 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2119 }
2120 }
2121 }
2122 else // !report
2123 {
2124 size_t count = GetItemCount();
2125 for ( size_t i = 0; i < count; i++ )
2126 {
2127 GetLine(i)->Draw( &dc, i == m_current );
2128 }
2129 }
2130
2131 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2132 // rectangle somehow and so leaves traces when the item is not selected any
2133 // more, see #12229.
2134 #ifndef __WXMAC__
2135 if ( HasCurrent() )
2136 {
2137 int flags = 0;
2138 if ( IsHighlighted(m_current) )
2139 flags |= wxCONTROL_SELECTED;
2140
2141 wxRendererNative::Get().
2142 DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
2143 }
2144 #endif // !__WXMAC__
2145 }
2146
2147 void wxListMainWindow::HighlightAll( bool on )
2148 {
2149 if ( IsSingleSel() )
2150 {
2151 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
2152
2153 // we just have one item to turn off
2154 if ( HasCurrent() && IsHighlighted(m_current) )
2155 {
2156 HighlightLine(m_current, false);
2157 RefreshLine(m_current);
2158 }
2159 }
2160 else // multi selection
2161 {
2162 if ( !IsEmpty() )
2163 HighlightLines(0, GetItemCount() - 1, on);
2164 }
2165 }
2166
2167 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2168 {
2169 // Do nothing here. This prevents the default handler in wxScrolledWindow
2170 // from needlessly scrolling the window when the edit control is
2171 // dismissed. See ticket #9563.
2172 }
2173
2174 void wxListMainWindow::SendNotify( size_t line,
2175 wxEventType command,
2176 const wxPoint& point )
2177 {
2178 wxListEvent le( command, GetParent()->GetId() );
2179 le.SetEventObject( GetParent() );
2180
2181 le.m_item.m_itemId =
2182 le.m_itemIndex = line;
2183
2184 // set only for events which have position
2185 if ( point != wxDefaultPosition )
2186 le.m_pointDrag = point;
2187
2188 // don't try to get the line info for virtual list controls: the main
2189 // program has it anyhow and if we did it would result in accessing all
2190 // the lines, even those which are not visible now and this is precisely
2191 // what we're trying to avoid
2192 if ( !IsVirtual() )
2193 {
2194 if ( line != (size_t)-1 )
2195 {
2196 GetLine(line)->GetItem( 0, le.m_item );
2197 }
2198 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2199 }
2200 //else: there may be no more such item
2201
2202 GetParent()->GetEventHandler()->ProcessEvent( le );
2203 }
2204
2205 void wxListMainWindow::ChangeCurrent(size_t current)
2206 {
2207 m_current = current;
2208
2209 // as the current item changed, we shouldn't start editing it when the
2210 // "slow click" timer expires as the click happened on another item
2211 if ( m_renameTimer->IsRunning() )
2212 m_renameTimer->Stop();
2213
2214 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2215 }
2216
2217 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2218 {
2219 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2220 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2221
2222 wxASSERT_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)),
2223 wxT("EditLabel() needs a text control") );
2224
2225 size_t itemEdit = (size_t)item;
2226
2227 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2228 le.SetEventObject( GetParent() );
2229 le.m_item.m_itemId =
2230 le.m_itemIndex = item;
2231 wxListLineData *data = GetLine(itemEdit);
2232 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
2233 data->GetItem( 0, le.m_item );
2234
2235 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2236 {
2237 // vetoed by user code
2238 return NULL;
2239 }
2240
2241 // We have to call this here because the label in question might just have
2242 // been added and no screen update taken place.
2243 if ( m_dirty )
2244 {
2245 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2246 // so that no pending events may change the item count (see below)
2247 // IMPORTANT: needs to be tested!
2248 wxSafeYield();
2249
2250 // Pending events dispatched by wxSafeYield might have changed the item
2251 // count
2252 if ( (size_t)item >= GetItemCount() )
2253 return NULL;
2254 }
2255
2256 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2257 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2258 return m_textctrlWrapper->GetText();
2259 }
2260
2261 void wxListMainWindow::OnRenameTimer()
2262 {
2263 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2264
2265 EditLabel( m_current );
2266 }
2267
2268 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2269 {
2270 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2271 le.SetEventObject( GetParent() );
2272 le.m_item.m_itemId =
2273 le.m_itemIndex = itemEdit;
2274
2275 wxListLineData *data = GetLine(itemEdit);
2276
2277 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
2278
2279 data->GetItem( 0, le.m_item );
2280 le.m_item.m_text = value;
2281 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2282 le.IsAllowed();
2283 }
2284
2285 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2286 {
2287 // let owner know that the edit was cancelled
2288 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2289
2290 le.SetEditCanceled(true);
2291
2292 le.SetEventObject( GetParent() );
2293 le.m_item.m_itemId =
2294 le.m_itemIndex = itemEdit;
2295
2296 wxListLineData *data = GetLine(itemEdit);
2297 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
2298
2299 data->GetItem( 0, le.m_item );
2300 GetEventHandler()->ProcessEvent( le );
2301 }
2302
2303 void wxListMainWindow::OnFindTimer()
2304 {
2305 m_findPrefix.clear();
2306 if ( m_findBell )
2307 m_findBell = 1;
2308 }
2309
2310 void wxListMainWindow::EnableBellOnNoMatch( bool on )
2311 {
2312 m_findBell = on;
2313 }
2314
2315 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2316 {
2317 #ifdef __WXMAC__
2318 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2319 // shutdown the edit control when the mouse is clicked elsewhere on the
2320 // listctrl because the order of events is different (or something like
2321 // that), so explicitly end the edit if it is active.
2322 if ( event.LeftDown() && m_textctrlWrapper )
2323 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
2324 #endif // __WXMAC__
2325
2326 if ( event.LeftDown() )
2327 {
2328 // Ensure we skip the event to let the system set focus to this window.
2329 event.Skip();
2330 }
2331
2332 // Pretend that the event happened in wxListCtrl itself.
2333 wxMouseEvent me(event);
2334 me.SetEventObject( GetParent() );
2335 me.SetId(GetParent()->GetId());
2336 if ( GetParent()->GetEventHandler()->ProcessEvent( me ))
2337 return;
2338
2339 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2340 {
2341 // let the base class handle mouse wheel events.
2342 event.Skip();
2343 return;
2344 }
2345
2346 if ( !HasCurrent() || IsEmpty() )
2347 {
2348 if (event.RightDown())
2349 {
2350 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2351
2352 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2353 GetParent()->GetId(),
2354 ClientToScreen(event.GetPosition()));
2355 evtCtx.SetEventObject(GetParent());
2356 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2357 }
2358 return;
2359 }
2360
2361 if (m_dirty)
2362 return;
2363
2364 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2365 event.ButtonDClick()) )
2366 return;
2367
2368 int x = event.GetX();
2369 int y = event.GetY();
2370 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
2371
2372 // where did we hit it (if we did)?
2373 long hitResult = 0;
2374
2375 size_t count = GetItemCount(),
2376 current;
2377
2378 if ( InReportView() )
2379 {
2380 current = y / GetLineHeight();
2381 if ( current < count )
2382 hitResult = HitTestLine(current, x, y);
2383 }
2384 else // !report
2385 {
2386 // TODO: optimize it too! this is less simple than for report view but
2387 // enumerating all items is still not a way to do it!!
2388 for ( current = 0; current < count; current++ )
2389 {
2390 hitResult = HitTestLine(current, x, y);
2391 if ( hitResult )
2392 break;
2393 }
2394 }
2395
2396 // Update drag events counter first as we must do it even if the mouse is
2397 // not on any item right now as we must keep count in case we started
2398 // dragging from the empty control area but continued to do it over a valid
2399 // item -- in this situation we must not start dragging this item.
2400 if (event.Dragging())
2401 m_dragCount++;
2402 else
2403 m_dragCount = 0;
2404
2405 // The only mouse event that can be generated without any valid item is
2406 // wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
2407 // popup menu for the list control itself which should be shown even when
2408 // the user clicks outside of any item.
2409 if ( !hitResult )
2410 {
2411 // outside of any item
2412 if (event.RightDown())
2413 {
2414 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2415
2416 wxContextMenuEvent evtCtx(
2417 wxEVT_CONTEXT_MENU,
2418 GetParent()->GetId(),
2419 ClientToScreen(event.GetPosition()));
2420 evtCtx.SetEventObject(GetParent());
2421 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2422 }
2423 else
2424 {
2425 // reset the selection and bail out
2426 HighlightAll(false);
2427 }
2428
2429 return;
2430 }
2431
2432 if ( event.Dragging() )
2433 {
2434 if (m_dragCount == 1)
2435 {
2436 // we have to report the raw, physical coords as we want to be
2437 // able to call HitTest(event.m_pointDrag) from the user code to
2438 // get the item being dragged
2439 m_dragStart = event.GetPosition();
2440 }
2441
2442 if (m_dragCount != 3)
2443 return;
2444
2445 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2446 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
2447
2448 SendNotify( m_lineLastClicked, command, m_dragStart );
2449
2450 return;
2451 }
2452
2453 bool forceClick = false;
2454 if (event.ButtonDClick())
2455 {
2456 if ( m_renameTimer->IsRunning() )
2457 m_renameTimer->Stop();
2458
2459 m_lastOnSame = false;
2460
2461 if ( current == m_lineLastClicked )
2462 {
2463 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2464
2465 return;
2466 }
2467 else
2468 {
2469 // The first click was on another item, so don't interpret this as
2470 // a double click, but as a simple click instead
2471 forceClick = true;
2472 }
2473 }
2474
2475 if (event.LeftUp())
2476 {
2477 if (m_lineSelectSingleOnUp != (size_t)-1)
2478 {
2479 // select single line
2480 HighlightAll( false );
2481 ReverseHighlight(m_lineSelectSingleOnUp);
2482 }
2483
2484 if (m_lastOnSame)
2485 {
2486 if ((current == m_current) &&
2487 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2488 HasFlag(wxLC_EDIT_LABELS) )
2489 {
2490 if ( !InReportView() ||
2491 GetLineLabelRect(current).Contains(x, y) )
2492 {
2493 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2494 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
2495 }
2496 }
2497
2498 m_lastOnSame = false;
2499 }
2500
2501 m_lineSelectSingleOnUp = (size_t)-1;
2502 }
2503 else
2504 {
2505 // This is necessary, because after a DnD operation in
2506 // from and to ourself, the up event is swallowed by the
2507 // DnD code. So on next non-up event (which means here and
2508 // now) m_lineSelectSingleOnUp should be reset.
2509 m_lineSelectSingleOnUp = (size_t)-1;
2510 }
2511 if (event.RightDown())
2512 {
2513 m_lineBeforeLastClicked = m_lineLastClicked;
2514 m_lineLastClicked = current;
2515
2516 // If the item is already selected, do not update the selection.
2517 // Multi-selections should not be cleared if a selected item is clicked.
2518 if (!IsHighlighted(current))
2519 {
2520 HighlightAll(false);
2521 ChangeCurrent(current);
2522 ReverseHighlight(m_current);
2523 }
2524
2525 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2526
2527 // Allow generation of context menu event
2528 event.Skip();
2529 }
2530 else if (event.MiddleDown())
2531 {
2532 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
2533 }
2534 else if ( event.LeftDown() || forceClick )
2535 {
2536 m_lineBeforeLastClicked = m_lineLastClicked;
2537 m_lineLastClicked = current;
2538
2539 size_t oldCurrent = m_current;
2540 bool oldWasSelected = IsHighlighted(m_current);
2541
2542 bool cmdModifierDown = event.CmdDown();
2543 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2544 {
2545 if ( IsSingleSel() || !IsHighlighted(current) )
2546 {
2547 HighlightAll( false );
2548
2549 ChangeCurrent(current);
2550
2551 ReverseHighlight(m_current);
2552 }
2553 else // multi sel & current is highlighted & no mod keys
2554 {
2555 m_lineSelectSingleOnUp = current;
2556 ChangeCurrent(current); // change focus
2557 }
2558 }
2559 else // multi sel & either ctrl or shift is down
2560 {
2561 if (cmdModifierDown)
2562 {
2563 ChangeCurrent(current);
2564
2565 ReverseHighlight(m_current);
2566 }
2567 else if (event.ShiftDown())
2568 {
2569 ChangeCurrent(current);
2570
2571 size_t lineFrom = oldCurrent,
2572 lineTo = current;
2573
2574 if ( lineTo < lineFrom )
2575 {
2576 lineTo = lineFrom;
2577 lineFrom = m_current;
2578 }
2579
2580 HighlightLines(lineFrom, lineTo);
2581 }
2582 else // !ctrl, !shift
2583 {
2584 // test in the enclosing if should make it impossible
2585 wxFAIL_MSG( wxT("how did we get here?") );
2586 }
2587 }
2588
2589 if (m_current != oldCurrent)
2590 RefreshLine( oldCurrent );
2591
2592 // Set the flag telling us whether the next click on this item should
2593 // start editing its label. This should happen if we clicked on the
2594 // current item and it was already selected, i.e. if this click was not
2595 // done to select it.
2596 //
2597 // It should not happen if this was a double click (forceClick is true)
2598 // nor if we hadn't had the focus before as then this click was used to
2599 // give focus to the control.
2600 m_lastOnSame = (m_current == oldCurrent) && oldWasSelected &&
2601 !forceClick && HasFocus();
2602 }
2603 }
2604
2605 void wxListMainWindow::MoveToItem(size_t item)
2606 {
2607 if ( item == (size_t)-1 )
2608 return;
2609
2610 wxRect rect = GetLineRect(item);
2611
2612 int client_w, client_h;
2613 GetClientSize( &client_w, &client_h );
2614
2615 const int hLine = GetLineHeight();
2616
2617 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2618 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
2619
2620 if ( InReportView() )
2621 {
2622 // the next we need the range of lines shown it might be different,
2623 // so recalculate it
2624 ResetVisibleLinesRange();
2625
2626 if (rect.y < view_y)
2627 GetListCtrl()->Scroll( -1, rect.y / hLine );
2628 if (rect.y + rect.height + 5 > view_y + client_h)
2629 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
2630
2631 #ifdef __WXMAC__
2632 // At least on Mac the visible lines value will get reset inside of
2633 // Scroll *before* it actually scrolls the window because of the
2634 // Update() that happens there, so it will still have the wrong value.
2635 // So let's reset it again and wait for it to be recalculated in the
2636 // next paint event. I would expect this problem to show up in wxGTK
2637 // too but couldn't duplicate it there. Perhaps the order of events
2638 // is different... --Robin
2639 ResetVisibleLinesRange();
2640 #endif
2641 }
2642 else // !report
2643 {
2644 int sx = -1,
2645 sy = -1;
2646
2647 if (rect.x-view_x < 5)
2648 sx = (rect.x - 5) / SCROLL_UNIT_X;
2649 if (rect.x + rect.width - 5 > view_x + client_w)
2650 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2651
2652 if (rect.y-view_y < 5)
2653 sy = (rect.y - 5) / hLine;
2654 if (rect.y + rect.height - 5 > view_y + client_h)
2655 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2656
2657 GetListCtrl()->Scroll(sx, sy);
2658 }
2659 }
2660
2661 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2662 {
2663 if ( !InReportView() )
2664 {
2665 // TODO: this should work in all views but is not implemented now
2666 return false;
2667 }
2668
2669 size_t top, bottom;
2670 GetVisibleLinesRange(&top, &bottom);
2671
2672 if ( bottom == (size_t)-1 )
2673 return 0;
2674
2675 ResetVisibleLinesRange();
2676
2677 int hLine = GetLineHeight();
2678
2679 GetListCtrl()->Scroll(-1, top + dy / hLine);
2680
2681 #ifdef __WXMAC__
2682 // see comment in MoveToItem() for why we do this
2683 ResetVisibleLinesRange();
2684 #endif
2685
2686 return true;
2687 }
2688
2689 // ----------------------------------------------------------------------------
2690 // keyboard handling
2691 // ----------------------------------------------------------------------------
2692
2693 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
2694 {
2695 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
2696 wxT("invalid item index in OnArrowChar()") );
2697
2698 size_t oldCurrent = m_current;
2699
2700 // in single selection we just ignore Shift as we can't select several
2701 // items anyhow
2702 if ( event.ShiftDown() && !IsSingleSel() )
2703 {
2704 ChangeCurrent(newCurrent);
2705
2706 // refresh the old focus to remove it
2707 RefreshLine( oldCurrent );
2708
2709 // select all the items between the old and the new one
2710 if ( oldCurrent > newCurrent )
2711 {
2712 newCurrent = oldCurrent;
2713 oldCurrent = m_current;
2714 }
2715
2716 HighlightLines(oldCurrent, newCurrent);
2717 }
2718 else // !shift
2719 {
2720 // all previously selected items are unselected unless ctrl is held
2721 // in a multiselection control
2722 if ( !event.ControlDown() || IsSingleSel() )
2723 HighlightAll(false);
2724
2725 ChangeCurrent(newCurrent);
2726
2727 // refresh the old focus to remove it
2728 RefreshLine( oldCurrent );
2729
2730 // in single selection mode we must always have a selected item
2731 if ( !event.ControlDown() || IsSingleSel() )
2732 HighlightLine( m_current, true );
2733 }
2734
2735 RefreshLine( m_current );
2736
2737 MoveToFocus();
2738 }
2739
2740 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2741 {
2742 wxWindow *parent = GetParent();
2743
2744 // propagate the key event upwards
2745 wxKeyEvent ke(event);
2746 ke.SetEventObject( parent );
2747 ke.SetId(GetParent()->GetId());
2748 if (parent->GetEventHandler()->ProcessEvent( ke ))
2749 return;
2750
2751 // send a list event
2752 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, parent->GetId() );
2753 le.m_item.m_itemId =
2754 le.m_itemIndex = m_current;
2755 if (HasCurrent())
2756 GetLine(m_current)->GetItem( 0, le.m_item );
2757 le.m_code = event.GetKeyCode();
2758 le.SetEventObject( parent );
2759 if (parent->GetEventHandler()->ProcessEvent( le ))
2760 return;
2761
2762 event.Skip();
2763 }
2764
2765 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2766 {
2767 wxWindow *parent = GetParent();
2768
2769 // propagate the key event upwards
2770 wxKeyEvent ke(event);
2771 ke.SetEventObject( parent );
2772 ke.SetId(GetParent()->GetId());
2773 if (parent->GetEventHandler()->ProcessEvent( ke ))
2774 return;
2775
2776 event.Skip();
2777 }
2778
2779 void wxListMainWindow::OnCharHook( wxKeyEvent &event )
2780 {
2781 if ( m_textctrlWrapper )
2782 {
2783 // When an in-place editor is active we should ensure that it always
2784 // gets the key events that are special to it.
2785 if ( m_textctrlWrapper->CheckForEndEditKey(event) )
2786 {
2787 // Skip the call to wxEvent::Skip() below.
2788 return;
2789 }
2790 }
2791
2792 event.Skip();
2793 }
2794
2795 void wxListMainWindow::OnChar( wxKeyEvent &event )
2796 {
2797 wxWindow *parent = GetParent();
2798
2799 // propagate the char event upwards
2800 wxKeyEvent ke(event);
2801 ke.SetEventObject( parent );
2802 ke.SetId(GetParent()->GetId());
2803 if (parent->GetEventHandler()->ProcessEvent( ke ))
2804 return;
2805
2806 if ( HandleAsNavigationKey(event) )
2807 return;
2808
2809 // no item -> nothing to do
2810 if (!HasCurrent())
2811 {
2812 event.Skip();
2813 return;
2814 }
2815
2816 // don't use m_linesPerPage directly as it might not be computed yet
2817 const int pageSize = GetCountPerPage();
2818 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
2819
2820 if (GetLayoutDirection() == wxLayout_RightToLeft)
2821 {
2822 if (event.GetKeyCode() == WXK_RIGHT)
2823 event.m_keyCode = WXK_LEFT;
2824 else if (event.GetKeyCode() == WXK_LEFT)
2825 event.m_keyCode = WXK_RIGHT;
2826 }
2827
2828 int keyCode = event.GetKeyCode();
2829 switch ( keyCode )
2830 {
2831 case WXK_UP:
2832 if ( m_current > 0 )
2833 OnArrowChar( m_current - 1, event );
2834 break;
2835
2836 case WXK_DOWN:
2837 if ( m_current < (size_t)GetItemCount() - 1 )
2838 OnArrowChar( m_current + 1, event );
2839 break;
2840
2841 case WXK_END:
2842 if (!IsEmpty())
2843 OnArrowChar( GetItemCount() - 1, event );
2844 break;
2845
2846 case WXK_HOME:
2847 if (!IsEmpty())
2848 OnArrowChar( 0, event );
2849 break;
2850
2851 case WXK_PAGEUP:
2852 {
2853 int steps = InReportView() ? pageSize - 1
2854 : m_current % pageSize;
2855
2856 int index = m_current - steps;
2857 if (index < 0)
2858 index = 0;
2859
2860 OnArrowChar( index, event );
2861 }
2862 break;
2863
2864 case WXK_PAGEDOWN:
2865 {
2866 int steps = InReportView()
2867 ? pageSize - 1
2868 : pageSize - (m_current % pageSize) - 1;
2869
2870 size_t index = m_current + steps;
2871 size_t count = GetItemCount();
2872 if ( index >= count )
2873 index = count - 1;
2874
2875 OnArrowChar( index, event );
2876 }
2877 break;
2878
2879 case WXK_LEFT:
2880 if ( !InReportView() )
2881 {
2882 int index = m_current - pageSize;
2883 if (index < 0)
2884 index = 0;
2885
2886 OnArrowChar( index, event );
2887 }
2888 break;
2889
2890 case WXK_RIGHT:
2891 if ( !InReportView() )
2892 {
2893 size_t index = m_current + pageSize;
2894
2895 size_t count = GetItemCount();
2896 if ( index >= count )
2897 index = count - 1;
2898
2899 OnArrowChar( index, event );
2900 }
2901 break;
2902
2903 case WXK_SPACE:
2904 if ( IsSingleSel() )
2905 {
2906 if ( event.ControlDown() )
2907 {
2908 ReverseHighlight(m_current);
2909 }
2910 else // normal space press
2911 {
2912 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2913 }
2914 }
2915 else // multiple selection
2916 {
2917 ReverseHighlight(m_current);
2918 }
2919 break;
2920
2921 case WXK_RETURN:
2922 case WXK_EXECUTE:
2923 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2924 break;
2925
2926 default:
2927 if ( !event.HasModifiers() &&
2928 ((keyCode >= '0' && keyCode <= '9') ||
2929 (keyCode >= 'a' && keyCode <= 'z') ||
2930 (keyCode >= 'A' && keyCode <= 'Z') ||
2931 (keyCode == '_') ||
2932 (keyCode == '+') ||
2933 (keyCode == '*') ||
2934 (keyCode == '-')))
2935 {
2936 // find the next item starting with the given prefix
2937 wxChar ch = (wxChar)keyCode;
2938 size_t item;
2939
2940 // if the same character is typed multiple times then go to the
2941 // next entry starting with that character instead of searching
2942 // for an item starting with multiple copies of this character,
2943 // this is more useful and is how it works under Windows.
2944 if ( m_findPrefix.length() == 1 && m_findPrefix[0] == ch )
2945 {
2946 item = PrefixFindItem(m_current, ch);
2947 }
2948 else
2949 {
2950 const wxString newPrefix(m_findPrefix + ch);
2951 item = PrefixFindItem(m_current, newPrefix);
2952 if ( item != (size_t)-1 )
2953 m_findPrefix = newPrefix;
2954 }
2955
2956 // also start the timer to reset the current prefix if the user
2957 // doesn't press any more alnum keys soon -- we wouldn't want
2958 // to use this prefix for a new item search
2959 if ( !m_findTimer )
2960 {
2961 m_findTimer = new wxListFindTimer( this );
2962 }
2963
2964 // Notice that we should start the timer even if we didn't find
2965 // anything to make sure we reset the search state later.
2966 m_findTimer->Start(wxListFindTimer::DELAY, wxTIMER_ONE_SHOT);
2967
2968 // restart timer even when there's no match so bell get's reset
2969 if ( item != (size_t)-1 )
2970 {
2971 // Select the found item and go to it.
2972 HighlightAll(false);
2973 SetItemState(item,
2974 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
2975 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
2976
2977 // Reset the bell flag if it had been temporarily disabled
2978 // before.
2979 if ( m_findBell )
2980 m_findBell = 1;
2981 }
2982 else // No such item
2983 {
2984 // Signal it with a bell if enabled.
2985 if ( m_findBell == 1 )
2986 {
2987 ::wxBell();
2988
2989 // Disable it for the next unsuccessful match, we only
2990 // beep once, this is usually enough and continuing to
2991 // do it would be annoying.
2992 m_findBell = -1;
2993 }
2994 }
2995 }
2996 else
2997 {
2998 event.Skip();
2999 }
3000 }
3001 }
3002
3003 // ----------------------------------------------------------------------------
3004 // focus handling
3005 // ----------------------------------------------------------------------------
3006
3007 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3008 {
3009 if ( GetParent() )
3010 {
3011 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3012 event.SetEventObject( GetParent() );
3013 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3014 return;
3015 }
3016
3017 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3018 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3019 // which are already drawn correctly resulting in horrible flicker - avoid
3020 // it
3021 if ( !m_hasFocus )
3022 {
3023 m_hasFocus = true;
3024
3025 RefreshSelected();
3026 }
3027 }
3028
3029 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3030 {
3031 if ( GetParent() )
3032 {
3033 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3034 event.SetEventObject( GetParent() );
3035 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3036 return;
3037 }
3038
3039 m_hasFocus = false;
3040 RefreshSelected();
3041 }
3042
3043 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3044 {
3045 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3046 {
3047 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3048 }
3049 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3050 {
3051 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3052 }
3053 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3054 {
3055 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3056 }
3057 else if ( InReportView() && (m_small_image_list))
3058 {
3059 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3060 }
3061 }
3062
3063 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3064 {
3065 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3066 {
3067 m_normal_image_list->GetSize( index, width, height );
3068 }
3069 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3070 {
3071 m_small_image_list->GetSize( index, width, height );
3072 }
3073 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3074 {
3075 m_small_image_list->GetSize( index, width, height );
3076 }
3077 else if ( InReportView() && m_small_image_list )
3078 {
3079 m_small_image_list->GetSize( index, width, height );
3080 }
3081 else
3082 {
3083 width =
3084 height = 0;
3085 }
3086 }
3087
3088 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3089 {
3090 m_dirty = true;
3091
3092 // calc the spacing from the icon size
3093 int width = 0, height = 0;
3094
3095 if ((imageList) && (imageList->GetImageCount()) )
3096 imageList->GetSize(0, width, height);
3097
3098 if (which == wxIMAGE_LIST_NORMAL)
3099 {
3100 m_normal_image_list = imageList;
3101 m_normal_spacing = width + 8;
3102 }
3103
3104 if (which == wxIMAGE_LIST_SMALL)
3105 {
3106 m_small_image_list = imageList;
3107 m_small_spacing = width + 14;
3108 m_lineHeight = 0; // ensure that the line height will be recalc'd
3109 }
3110 }
3111
3112 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3113 {
3114 m_dirty = true;
3115 if (isSmall)
3116 m_small_spacing = spacing;
3117 else
3118 m_normal_spacing = spacing;
3119 }
3120
3121 int wxListMainWindow::GetItemSpacing( bool isSmall )
3122 {
3123 return isSmall ? m_small_spacing : m_normal_spacing;
3124 }
3125
3126 // ----------------------------------------------------------------------------
3127 // columns
3128 // ----------------------------------------------------------------------------
3129
3130 int
3131 wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
3132 {
3133 wxClientDC dc(const_cast<wxListMainWindow*>(this));
3134
3135 int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
3136
3137 width += 2*EXTRA_WIDTH;
3138
3139 // check for column header's image availability
3140 const int image = column->GetImage();
3141 if ( image != -1 )
3142 {
3143 if ( m_small_image_list )
3144 {
3145 int ix = 0, iy = 0;
3146 m_small_image_list->GetSize(image, ix, iy);
3147 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3148 }
3149 }
3150
3151 return width;
3152 }
3153
3154 void wxListMainWindow::SetColumn( int col, const wxListItem &item )
3155 {
3156 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3157
3158 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
3159
3160 wxListHeaderData *column = node->GetData();
3161 column->SetItem( item );
3162
3163 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3164 column->SetWidth(ComputeMinHeaderWidth(column));
3165
3166 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3167 if ( headerWin )
3168 headerWin->m_dirty = true;
3169
3170 m_dirty = true;
3171
3172 // invalidate it as it has to be recalculated
3173 m_headerWidth = 0;
3174 }
3175
3176 void wxListMainWindow::SetColumnWidth( int col, int width )
3177 {
3178 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3179 wxT("invalid column index") );
3180
3181 wxCHECK_RET( InReportView(),
3182 wxT("SetColumnWidth() can only be called in report mode.") );
3183
3184 m_dirty = true;
3185
3186 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3187 if ( headerWin )
3188 headerWin->m_dirty = true;
3189
3190 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3191 wxCHECK_RET( node, wxT("no column?") );
3192
3193 wxListHeaderData *column = node->GetData();
3194
3195 size_t count = GetItemCount();
3196
3197 if (width == wxLIST_AUTOSIZE_USEHEADER)
3198 {
3199 width = ComputeMinHeaderWidth(column);
3200 }
3201 else if ( width == wxLIST_AUTOSIZE )
3202 {
3203 width = ComputeMinHeaderWidth(column);
3204
3205 if ( !IsVirtual() )
3206 {
3207 wxClientDC dc(this);
3208 dc.SetFont( GetFont() );
3209
3210 int max = AUTOSIZE_COL_MARGIN;
3211
3212 // if the cached column width isn't valid then recalculate it
3213 if (m_aColWidths.Item(col)->bNeedsUpdate)
3214 {
3215 for (size_t i = 0; i < count; i++)
3216 {
3217 wxListLineData *line = GetLine( i );
3218 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3219
3220 wxCHECK_RET( n, wxT("no subitem?") );
3221
3222 wxListItemData *itemData = n->GetData();
3223 wxListItem item;
3224
3225 itemData->GetItem(item);
3226 int itemWidth = GetItemWidthWithImage(&item);
3227 if (itemWidth > max)
3228 max = itemWidth;
3229 }
3230
3231 m_aColWidths.Item(col)->bNeedsUpdate = false;
3232 m_aColWidths.Item(col)->nMaxWidth = max;
3233 }
3234
3235 max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
3236 if ( width < max )
3237 width = max;
3238 }
3239 }
3240
3241 column->SetWidth( width );
3242
3243 // invalidate it as it has to be recalculated
3244 m_headerWidth = 0;
3245 }
3246
3247 int wxListMainWindow::GetHeaderWidth() const
3248 {
3249 if ( !m_headerWidth )
3250 {
3251 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3252
3253 size_t count = GetColumnCount();
3254 for ( size_t col = 0; col < count; col++ )
3255 {
3256 self->m_headerWidth += GetColumnWidth(col);
3257 }
3258 }
3259
3260 return m_headerWidth;
3261 }
3262
3263 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3264 {
3265 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3266 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
3267
3268 wxListHeaderData *column = node->GetData();
3269 column->GetItem( item );
3270 }
3271
3272 int wxListMainWindow::GetColumnWidth( int col ) const
3273 {
3274 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3275 wxCHECK_MSG( node, 0, wxT("invalid column index") );
3276
3277 wxListHeaderData *column = node->GetData();
3278 return column->GetWidth();
3279 }
3280
3281 // ----------------------------------------------------------------------------
3282 // item state
3283 // ----------------------------------------------------------------------------
3284
3285 void wxListMainWindow::SetItem( wxListItem &item )
3286 {
3287 long id = item.m_itemId;
3288 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3289 wxT("invalid item index in SetItem") );
3290
3291 if ( !IsVirtual() )
3292 {
3293 wxListLineData *line = GetLine((size_t)id);
3294 line->SetItem( item.m_col, item );
3295
3296 // Set item state if user wants
3297 if ( item.m_mask & wxLIST_MASK_STATE )
3298 SetItemState( item.m_itemId, item.m_state, item.m_state );
3299
3300 if (InReportView())
3301 {
3302 // update the Max Width Cache if needed
3303 int width = GetItemWidthWithImage(&item);
3304
3305 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3306 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3307 }
3308 }
3309
3310 // update the item on screen unless we're going to update everything soon
3311 // anyhow
3312 if ( !m_dirty )
3313 {
3314 wxRect rectItem;
3315 GetItemRect(id, rectItem);
3316 RefreshRect(rectItem);
3317 }
3318 }
3319
3320 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3321 {
3322 if ( IsEmpty() )
3323 return;
3324
3325 // first deal with selection
3326 if ( stateMask & wxLIST_STATE_SELECTED )
3327 {
3328 // set/clear select state
3329 if ( IsVirtual() )
3330 {
3331 // optimized version for virtual listctrl.
3332 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3333 Refresh();
3334 }
3335 else if ( state & wxLIST_STATE_SELECTED )
3336 {
3337 const long count = GetItemCount();
3338 for( long i = 0; i < count; i++ )
3339 {
3340 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3341 }
3342
3343 }
3344 else
3345 {
3346 // clear for non virtual (somewhat optimized by using GetNextItem())
3347 long i = -1;
3348 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3349 {
3350 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3351 }
3352 }
3353 }
3354
3355 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3356 {
3357 // unfocus all: only one item can be focussed, so clearing focus for
3358 // all items is simply clearing focus of the focussed item.
3359 SetItemState(m_current, state, stateMask);
3360 }
3361 //(setting focus to all items makes no sense, so it is not handled here.)
3362 }
3363
3364 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3365 {
3366 if ( litem == -1 )
3367 {
3368 SetItemStateAll(state, stateMask);
3369 return;
3370 }
3371
3372 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3373 wxT("invalid list ctrl item index in SetItem") );
3374
3375 size_t oldCurrent = m_current;
3376 size_t item = (size_t)litem; // safe because of the check above
3377
3378 // do we need to change the focus?
3379 if ( stateMask & wxLIST_STATE_FOCUSED )
3380 {
3381 if ( state & wxLIST_STATE_FOCUSED )
3382 {
3383 // don't do anything if this item is already focused
3384 if ( item != m_current )
3385 {
3386 ChangeCurrent(item);
3387
3388 if ( oldCurrent != (size_t)-1 )
3389 {
3390 if ( IsSingleSel() )
3391 {
3392 HighlightLine(oldCurrent, false);
3393 }
3394
3395 RefreshLine(oldCurrent);
3396 }
3397
3398 RefreshLine( m_current );
3399 }
3400 }
3401 else // unfocus
3402 {
3403 // don't do anything if this item is not focused
3404 if ( item == m_current )
3405 {
3406 ResetCurrent();
3407
3408 if ( IsSingleSel() )
3409 {
3410 // we must unselect the old current item as well or we
3411 // might end up with more than one selected item in a
3412 // single selection control
3413 HighlightLine(oldCurrent, false);
3414 }
3415
3416 RefreshLine( oldCurrent );
3417 }
3418 }
3419 }
3420
3421 // do we need to change the selection state?
3422 if ( stateMask & wxLIST_STATE_SELECTED )
3423 {
3424 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3425
3426 if ( IsSingleSel() )
3427 {
3428 if ( on )
3429 {
3430 // selecting the item also makes it the focused one in the
3431 // single sel mode
3432 if ( m_current != item )
3433 {
3434 ChangeCurrent(item);
3435
3436 if ( oldCurrent != (size_t)-1 )
3437 {
3438 HighlightLine( oldCurrent, false );
3439 RefreshLine( oldCurrent );
3440 }
3441 }
3442 }
3443 else // off
3444 {
3445 // only the current item may be selected anyhow
3446 if ( item != m_current )
3447 return;
3448 }
3449 }
3450
3451 if ( HighlightLine(item, on) )
3452 {
3453 RefreshLine(item);
3454 }
3455 }
3456 }
3457
3458 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3459 {
3460 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3461 wxT("invalid list ctrl item index in GetItemState()") );
3462
3463 int ret = wxLIST_STATE_DONTCARE;
3464
3465 if ( stateMask & wxLIST_STATE_FOCUSED )
3466 {
3467 if ( (size_t)item == m_current )
3468 ret |= wxLIST_STATE_FOCUSED;
3469 }
3470
3471 if ( stateMask & wxLIST_STATE_SELECTED )
3472 {
3473 if ( IsHighlighted(item) )
3474 ret |= wxLIST_STATE_SELECTED;
3475 }
3476
3477 return ret;
3478 }
3479
3480 void wxListMainWindow::GetItem( wxListItem &item ) const
3481 {
3482 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3483 wxT("invalid item index in GetItem") );
3484
3485 wxListLineData *line = GetLine((size_t)item.m_itemId);
3486 line->GetItem( item.m_col, item );
3487
3488 // Get item state if user wants it
3489 if ( item.m_mask & wxLIST_MASK_STATE )
3490 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3491 wxLIST_STATE_FOCUSED );
3492 }
3493
3494 // ----------------------------------------------------------------------------
3495 // item count
3496 // ----------------------------------------------------------------------------
3497
3498 size_t wxListMainWindow::GetItemCount() const
3499 {
3500 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3501 }
3502
3503 void wxListMainWindow::SetItemCount(long count)
3504 {
3505 m_selStore.SetItemCount(count);
3506 m_countVirt = count;
3507
3508 ResetVisibleLinesRange();
3509
3510 // scrollbars must be reset
3511 m_dirty = true;
3512 }
3513
3514 int wxListMainWindow::GetSelectedItemCount() const
3515 {
3516 // deal with the quick case first
3517 if ( IsSingleSel() )
3518 return HasCurrent() ? IsHighlighted(m_current) : false;
3519
3520 // virtual controls remmebers all its selections itself
3521 if ( IsVirtual() )
3522 return m_selStore.GetSelectedCount();
3523
3524 // TODO: we probably should maintain the number of items selected even for
3525 // non virtual controls as enumerating all lines is really slow...
3526 size_t countSel = 0;
3527 size_t count = GetItemCount();
3528 for ( size_t line = 0; line < count; line++ )
3529 {
3530 if ( GetLine(line)->IsHighlighted() )
3531 countSel++;
3532 }
3533
3534 return countSel;
3535 }
3536
3537 // ----------------------------------------------------------------------------
3538 // item position/size
3539 // ----------------------------------------------------------------------------
3540
3541 wxRect wxListMainWindow::GetViewRect() const
3542 {
3543 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
3544
3545 // we need to find the longest/tallest label
3546 wxCoord xMax = 0, yMax = 0;
3547 const int count = GetItemCount();
3548 if ( count )
3549 {
3550 for ( int i = 0; i < count; i++ )
3551 {
3552 // we need logical, not physical, coordinates here, so use
3553 // GetLineRect() instead of GetItemRect()
3554 wxRect r = GetLineRect(i);
3555
3556 wxCoord x = r.GetRight(),
3557 y = r.GetBottom();
3558
3559 if ( x > xMax )
3560 xMax = x;
3561 if ( y > yMax )
3562 yMax = y;
3563 }
3564 }
3565
3566 // some fudge needed to make it look prettier
3567 xMax += 2 * EXTRA_BORDER_X;
3568 yMax += 2 * EXTRA_BORDER_Y;
3569
3570 // account for the scrollbars if necessary
3571 const wxSize sizeAll = GetClientSize();
3572 if ( xMax > sizeAll.x )
3573 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3574 if ( yMax > sizeAll.y )
3575 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3576
3577 return wxRect(0, 0, xMax, yMax);
3578 }
3579
3580 bool
3581 wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
3582 {
3583 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3584 false,
3585 wxT("GetSubItemRect only meaningful in report view") );
3586 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
3587 wxT("invalid item in GetSubItemRect") );
3588
3589 // ensure that we're laid out, otherwise we could return nonsense
3590 if ( m_dirty )
3591 {
3592 wxConstCast(this, wxListMainWindow)->
3593 RecalculatePositions(true /* no refresh */);
3594 }
3595
3596 rect = GetLineRect((size_t)item);
3597
3598 // Adjust rect to specified column
3599 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3600 {
3601 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
3602 wxT("invalid subItem in GetSubItemRect") );
3603
3604 for (int i = 0; i < subItem; i++)
3605 {
3606 rect.x += GetColumnWidth(i);
3607 }
3608 rect.width = GetColumnWidth(subItem);
3609 }
3610
3611 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
3612
3613 return true;
3614 }
3615
3616 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
3617 {
3618 wxRect rect;
3619 GetItemRect(item, rect);
3620
3621 pos.x = rect.x;
3622 pos.y = rect.y;
3623
3624 return true;
3625 }
3626
3627 // ----------------------------------------------------------------------------
3628 // geometry calculation
3629 // ----------------------------------------------------------------------------
3630
3631 void wxListMainWindow::RecalculatePositions(bool noRefresh)
3632 {
3633 const int lineHeight = GetLineHeight();
3634
3635 wxClientDC dc( this );
3636 dc.SetFont( GetFont() );
3637
3638 const size_t count = GetItemCount();
3639
3640 int iconSpacing;
3641 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3642 iconSpacing = m_normal_spacing;
3643 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3644 iconSpacing = m_small_spacing;
3645 else
3646 iconSpacing = 0;
3647
3648 // Note that we do not call GetClientSize() here but
3649 // GetSize() and subtract the border size for sunken
3650 // borders manually. This is technically incorrect,
3651 // but we need to know the client area's size WITHOUT
3652 // scrollbars here. Since we don't know if there are
3653 // any scrollbars, we use GetSize() instead. Another
3654 // solution would be to call SetScrollbars() here to
3655 // remove the scrollbars and call GetClientSize() then,
3656 // but this might result in flicker and - worse - will
3657 // reset the scrollbars to 0 which is not good at all
3658 // if you resize a dialog/window, but don't want to
3659 // reset the window scrolling. RR.
3660 // Furthermore, we actually do NOT subtract the border
3661 // width as 2 pixels is just the extra space which we
3662 // need around the actual content in the window. Other-
3663 // wise the text would e.g. touch the upper border. RR.
3664 int clientWidth,
3665 clientHeight;
3666 GetSize( &clientWidth, &clientHeight );
3667
3668 if ( InReportView() )
3669 {
3670 // all lines have the same height and we scroll one line per step
3671 int entireHeight = count * lineHeight + LINE_SPACING;
3672
3673 m_linesPerPage = clientHeight / lineHeight;
3674
3675 ResetVisibleLinesRange();
3676
3677 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
3678 GetHeaderWidth() / SCROLL_UNIT_X,
3679 (entireHeight + lineHeight - 1) / lineHeight,
3680 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3681 GetListCtrl()->GetScrollPos(wxVERTICAL),
3682 true );
3683 }
3684 else // !report
3685 {
3686 // we have 3 different layout strategies: either layout all items
3687 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3688 // to arrange them in top to bottom, left to right (don't ask me why
3689 // not the other way round...) order
3690 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
3691 {
3692 int x = EXTRA_BORDER_X;
3693 int y = EXTRA_BORDER_Y;
3694
3695 wxCoord widthMax = 0;
3696
3697 size_t i;
3698 for ( i = 0; i < count; i++ )
3699 {
3700 wxListLineData *line = GetLine(i);
3701 line->CalculateSize( &dc, iconSpacing );
3702 line->SetPosition( x, y, iconSpacing );
3703
3704 wxSize sizeLine = GetLineSize(i);
3705
3706 if ( HasFlag(wxLC_ALIGN_TOP) )
3707 {
3708 if ( sizeLine.x > widthMax )
3709 widthMax = sizeLine.x;
3710
3711 y += sizeLine.y;
3712 }
3713 else // wxLC_ALIGN_LEFT
3714 {
3715 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3716 }
3717 }
3718
3719 if ( HasFlag(wxLC_ALIGN_TOP) )
3720 {
3721 // traverse the items again and tweak their sizes so that they are
3722 // all the same in a row
3723 for ( i = 0; i < count; i++ )
3724 {
3725 wxListLineData *line = GetLine(i);
3726 line->m_gi->ExtendWidth(widthMax);
3727 }
3728 }
3729
3730 GetListCtrl()->SetScrollbars
3731 (
3732 SCROLL_UNIT_X,
3733 lineHeight,
3734 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3735 (y + lineHeight) / lineHeight,
3736 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3737 GetListCtrl()->GetScrollPos( wxVERTICAL ),
3738 true
3739 );
3740 }
3741 else // "flowed" arrangement, the most complicated case
3742 {
3743 // at first we try without any scrollbars, if the items don't fit into
3744 // the window, we recalculate after subtracting the space taken by the
3745 // scrollbar
3746
3747 int entireWidth = 0;
3748
3749 for (int tries = 0; tries < 2; tries++)
3750 {
3751 entireWidth = 2 * EXTRA_BORDER_X;
3752
3753 if (tries == 1)
3754 {
3755 // Now we have decided that the items do not fit into the
3756 // client area, so we need a scrollbar
3757 entireWidth += SCROLL_UNIT_X;
3758 }
3759
3760 int x = EXTRA_BORDER_X;
3761 int y = EXTRA_BORDER_Y;
3762
3763 // Note that "row" here is vertical, i.e. what is called
3764 // "column" in many other places in wxWidgets.
3765 int maxWidthInThisRow = 0;
3766
3767 m_linesPerPage = 0;
3768 int currentlyVisibleLines = 0;
3769
3770 for (size_t i = 0; i < count; i++)
3771 {
3772 currentlyVisibleLines++;
3773 wxListLineData *line = GetLine( i );
3774 line->CalculateSize( &dc, iconSpacing );
3775 line->SetPosition( x, y, iconSpacing );
3776
3777 wxSize sizeLine = GetLineSize( i );
3778
3779 if ( maxWidthInThisRow < sizeLine.x )
3780 maxWidthInThisRow = sizeLine.x;
3781
3782 y += sizeLine.y;
3783 if (currentlyVisibleLines > m_linesPerPage)
3784 m_linesPerPage = currentlyVisibleLines;
3785
3786 // Have we reached the end of the row either because no
3787 // more items would fit or because there are simply no more
3788 // items?
3789 if ( y + sizeLine.y >= clientHeight
3790 || i == count - 1)
3791 {
3792 // Adjust all items in this row to have the same
3793 // width to ensure that they all align horizontally in
3794 // icon view.
3795 if ( HasFlag(wxLC_ICON) || HasFlag(wxLC_SMALL_ICON) )
3796 {
3797 size_t firstRowLine = i - currentlyVisibleLines + 1;
3798 for (size_t j = firstRowLine; j <= i; j++)
3799 {
3800 GetLine(j)->m_gi->ExtendWidth(maxWidthInThisRow);
3801 }
3802 }
3803
3804 currentlyVisibleLines = 0;
3805 y = EXTRA_BORDER_Y;
3806 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3807 x += maxWidthInThisRow;
3808 entireWidth += maxWidthInThisRow;
3809 maxWidthInThisRow = 0;
3810 }
3811
3812 if ( (tries == 0) &&
3813 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3814 {
3815 clientHeight -= wxSystemSettings::
3816 GetMetric(wxSYS_HSCROLL_Y);
3817 m_linesPerPage = 0;
3818 break;
3819 }
3820
3821 if ( i == count - 1 )
3822 tries = 1; // Everything fits, no second try required.
3823 }
3824 }
3825
3826 GetListCtrl()->SetScrollbars
3827 (
3828 SCROLL_UNIT_X,
3829 lineHeight,
3830 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3831 0,
3832 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3833 0,
3834 true
3835 );
3836 }
3837 }
3838
3839 if ( !noRefresh )
3840 {
3841 // FIXME: why should we call it from here?
3842 UpdateCurrent();
3843
3844 RefreshAll();
3845 }
3846 }
3847
3848 void wxListMainWindow::RefreshAll()
3849 {
3850 m_dirty = false;
3851 Refresh();
3852
3853 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3854 if ( headerWin && headerWin->m_dirty )
3855 {
3856 headerWin->m_dirty = false;
3857 headerWin->Refresh();
3858 }
3859 }
3860
3861 void wxListMainWindow::UpdateCurrent()
3862 {
3863 if ( !HasCurrent() && !IsEmpty() )
3864 ChangeCurrent(0);
3865 }
3866
3867 long wxListMainWindow::GetNextItem( long item,
3868 int WXUNUSED(geometry),
3869 int state ) const
3870 {
3871 long ret = item,
3872 max = GetItemCount();
3873 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
3874 wxT("invalid listctrl index in GetNextItem()") );
3875
3876 // notice that we start with the next item (or the first one if item == -1)
3877 // and this is intentional to allow writing a simple loop to iterate over
3878 // all selected items
3879 ret++;
3880 if ( ret == max )
3881 // this is not an error because the index was OK initially,
3882 // just no such item
3883 return -1;
3884
3885 if ( !state )
3886 // any will do
3887 return (size_t)ret;
3888
3889 size_t count = GetItemCount();
3890 for ( size_t line = (size_t)ret; line < count; line++ )
3891 {
3892 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3893 return line;
3894
3895 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
3896 return line;
3897 }
3898
3899 return -1;
3900 }
3901
3902 // ----------------------------------------------------------------------------
3903 // deleting stuff
3904 // ----------------------------------------------------------------------------
3905
3906 void wxListMainWindow::DeleteItem( long lindex )
3907 {
3908 size_t count = GetItemCount();
3909
3910 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
3911 wxT("invalid item index in DeleteItem") );
3912
3913 size_t index = (size_t)lindex;
3914
3915 // we don't need to adjust the index for the previous items
3916 if ( HasCurrent() && m_current >= index )
3917 {
3918 // if the current item is being deleted, we want the next one to
3919 // become selected - unless there is no next one - so don't adjust
3920 // m_current in this case
3921 if ( m_current != index || m_current == count - 1 )
3922 m_current--;
3923 }
3924
3925 if ( InReportView() )
3926 {
3927 // mark the Column Max Width cache as dirty if the items in the line
3928 // we're deleting contain the Max Column Width
3929 wxListLineData * const line = GetLine(index);
3930 wxListItemDataList::compatibility_iterator n;
3931 wxListItemData *itemData;
3932 wxListItem item;
3933 int itemWidth;
3934
3935 for (size_t i = 0; i < m_columns.GetCount(); i++)
3936 {
3937 n = line->m_items.Item( i );
3938 itemData = n->GetData();
3939 itemData->GetItem(item);
3940
3941 itemWidth = GetItemWidthWithImage(&item);
3942
3943 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3944 m_aColWidths.Item(i)->bNeedsUpdate = true;
3945 }
3946
3947 ResetVisibleLinesRange();
3948 }
3949
3950 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
3951
3952 if ( IsVirtual() )
3953 {
3954 m_countVirt--;
3955 m_selStore.OnItemDelete(index);
3956 }
3957 else
3958 {
3959 m_lines.RemoveAt( index );
3960 }
3961
3962 // we need to refresh the (vert) scrollbar as the number of items changed
3963 m_dirty = true;
3964
3965 RefreshAfter(index);
3966 }
3967
3968 void wxListMainWindow::DeleteColumn( int col )
3969 {
3970 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3971
3972 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
3973
3974 m_dirty = true;
3975 delete node->GetData();
3976 m_columns.Erase( node );
3977
3978 if ( !IsVirtual() )
3979 {
3980 // update all the items
3981 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3982 {
3983 wxListLineData * const line = GetLine(i);
3984
3985 // In the following atypical but possible scenario it can be
3986 // legal to call DeleteColumn() but the items may not have any
3987 // values for it:
3988 // 1. In report view, insert a second column.
3989 // 2. Still in report view, add an item with 2 values.
3990 // 3. Switch to an icon (or list) view.
3991 // 4. Add an item -- necessarily with 1 value only.
3992 // 5. Switch back to report view.
3993 // 6. Call DeleteColumn().
3994 // So we need to check for this as otherwise we would simply crash
3995 // if this happens.
3996 if ( line->m_items.GetCount() <= static_cast<unsigned>(col) )
3997 continue;
3998
3999 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4000 delete n->GetData();
4001 line->m_items.Erase(n);
4002 }
4003 }
4004
4005 if ( InReportView() ) // we only cache max widths when in Report View
4006 {
4007 delete m_aColWidths.Item(col);
4008 m_aColWidths.RemoveAt(col);
4009 }
4010
4011 // invalidate it as it has to be recalculated
4012 m_headerWidth = 0;
4013 }
4014
4015 void wxListMainWindow::DoDeleteAllItems()
4016 {
4017 if ( IsEmpty() )
4018 // nothing to do - in particular, don't send the event
4019 return;
4020
4021 ResetCurrent();
4022
4023 // to make the deletion of all items faster, we don't send the
4024 // notifications for each item deletion in this case but only one event
4025 // for all of them: this is compatible with wxMSW and documented in
4026 // DeleteAllItems() description
4027
4028 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4029 event.SetEventObject( GetParent() );
4030 GetParent()->GetEventHandler()->ProcessEvent( event );
4031
4032 if ( IsVirtual() )
4033 {
4034 m_countVirt = 0;
4035 m_selStore.Clear();
4036 }
4037
4038 if ( InReportView() )
4039 {
4040 ResetVisibleLinesRange();
4041 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4042 {
4043 m_aColWidths.Item(i)->bNeedsUpdate = true;
4044 }
4045 }
4046
4047 m_lines.Clear();
4048 }
4049
4050 void wxListMainWindow::DeleteAllItems()
4051 {
4052 DoDeleteAllItems();
4053
4054 RecalculatePositions();
4055 }
4056
4057 void wxListMainWindow::DeleteEverything()
4058 {
4059 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4060 WX_CLEAR_ARRAY(m_aColWidths);
4061
4062 DeleteAllItems();
4063 }
4064
4065 // ----------------------------------------------------------------------------
4066 // scanning for an item
4067 // ----------------------------------------------------------------------------
4068
4069 void wxListMainWindow::EnsureVisible( long index )
4070 {
4071 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4072 wxT("invalid index in EnsureVisible") );
4073
4074 // We have to call this here because the label in question might just have
4075 // been added and its position is not known yet
4076 if ( m_dirty )
4077 RecalculatePositions(true /* no refresh */);
4078
4079 MoveToItem((size_t)index);
4080 }
4081
4082 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4083 {
4084 if (str.empty())
4085 return wxNOT_FOUND;
4086
4087 long pos = start;
4088 wxString str_upper = str.Upper();
4089 if (pos < 0)
4090 pos = 0;
4091
4092 size_t count = GetItemCount();
4093 for ( size_t i = (size_t)pos; i < count; i++ )
4094 {
4095 wxListLineData *line = GetLine(i);
4096 wxString line_upper = line->GetText(0).Upper();
4097 if (!partial)
4098 {
4099 if (line_upper == str_upper )
4100 return i;
4101 }
4102 else
4103 {
4104 if (line_upper.find(str_upper) == 0)
4105 return i;
4106 }
4107 }
4108
4109 return wxNOT_FOUND;
4110 }
4111
4112 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4113 {
4114 long pos = start;
4115 if (pos < 0)
4116 pos = 0;
4117
4118 size_t count = GetItemCount();
4119 for (size_t i = (size_t)pos; i < count; i++)
4120 {
4121 wxListLineData *line = GetLine(i);
4122 wxListItem item;
4123 line->GetItem( 0, item );
4124 if (item.m_data == data)
4125 return i;
4126 }
4127
4128 return wxNOT_FOUND;
4129 }
4130
4131 long wxListMainWindow::FindItem( const wxPoint& pt )
4132 {
4133 size_t topItem;
4134 GetVisibleLinesRange( &topItem, NULL );
4135
4136 wxPoint p;
4137 GetItemPosition( GetItemCount() - 1, p );
4138 if ( p.y == 0 )
4139 return topItem;
4140
4141 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4142 if ( id >= 0 && id < (long)GetItemCount() )
4143 return id;
4144
4145 return wxNOT_FOUND;
4146 }
4147
4148 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4149 {
4150 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
4151
4152 size_t count = GetItemCount();
4153
4154 if ( InReportView() )
4155 {
4156 size_t current = y / GetLineHeight();
4157 if ( current < count )
4158 {
4159 flags = HitTestLine(current, x, y);
4160 if ( flags )
4161 return current;
4162 }
4163 }
4164 else // !report
4165 {
4166 // TODO: optimize it too! this is less simple than for report view but
4167 // enumerating all items is still not a way to do it!!
4168 for ( size_t current = 0; current < count; current++ )
4169 {
4170 flags = HitTestLine(current, x, y);
4171 if ( flags )
4172 return current;
4173 }
4174 }
4175
4176 return wxNOT_FOUND;
4177 }
4178
4179 // ----------------------------------------------------------------------------
4180 // adding stuff
4181 // ----------------------------------------------------------------------------
4182
4183 void wxListMainWindow::InsertItem( wxListItem &item )
4184 {
4185 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4186
4187 int count = GetItemCount();
4188 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
4189
4190 if (item.m_itemId > count)
4191 item.m_itemId = count;
4192
4193 size_t id = item.m_itemId;
4194
4195 m_dirty = true;
4196
4197 if ( InReportView() )
4198 {
4199 ResetVisibleLinesRange();
4200
4201 const unsigned col = item.GetColumn();
4202 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4203
4204 // calculate the width of the item and adjust the max column width
4205 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
4206 int width = GetItemWidthWithImage(&item);
4207 item.SetWidth(width);
4208 if (width > pWidthInfo->nMaxWidth)
4209 pWidthInfo->nMaxWidth = width;
4210 }
4211
4212 wxListLineData *line = new wxListLineData(this);
4213
4214 line->SetItem( item.m_col, item );
4215 if ( item.m_mask & wxLIST_MASK_IMAGE )
4216 {
4217 // Reset the buffered height if it's not big enough for the new image.
4218 int image = item.GetImage();
4219 if ( m_small_image_list && image != -1 && InReportView() )
4220 {
4221 int imageWidth, imageHeight;
4222 m_small_image_list->GetSize(image, imageWidth, imageHeight);
4223
4224 if ( imageHeight > m_lineHeight )
4225 m_lineHeight = 0;
4226 }
4227 }
4228
4229 m_lines.Insert( line, id );
4230
4231 m_dirty = true;
4232
4233 // If an item is selected at or below the point of insertion, we need to
4234 // increment the member variables because the current row's index has gone
4235 // up by one
4236 if ( HasCurrent() && m_current >= id )
4237 m_current++;
4238
4239 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4240
4241 RefreshLines(id, GetItemCount() - 1);
4242 }
4243
4244 long wxListMainWindow::InsertColumn( long col, const wxListItem &item )
4245 {
4246 long idx = -1;
4247
4248 m_dirty = true;
4249 if ( InReportView() )
4250 {
4251 wxListHeaderData *column = new wxListHeaderData( item );
4252 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4253 column->SetWidth(ComputeMinHeaderWidth(column));
4254
4255 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4256
4257 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4258 if ( insert )
4259 {
4260 wxListHeaderDataList::compatibility_iterator
4261 node = m_columns.Item( col );
4262 m_columns.Insert( node, column );
4263 m_aColWidths.Insert( colWidthInfo, col );
4264 idx = col;
4265 }
4266 else
4267 {
4268 idx = m_aColWidths.GetCount();
4269 m_columns.Append( column );
4270 m_aColWidths.Add( colWidthInfo );
4271 }
4272
4273 if ( !IsVirtual() )
4274 {
4275 // update all the items
4276 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4277 {
4278 wxListLineData * const line = GetLine(i);
4279 wxListItemData * const data = new wxListItemData(this);
4280 if ( insert )
4281 line->m_items.Insert(col, data);
4282 else
4283 line->m_items.Append(data);
4284 }
4285 }
4286
4287 // invalidate it as it has to be recalculated
4288 m_headerWidth = 0;
4289 }
4290 return idx;
4291 }
4292
4293 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4294 {
4295 int width = 0;
4296 wxClientDC dc(this);
4297
4298 dc.SetFont( GetFont() );
4299
4300 if (item->GetImage() != -1)
4301 {
4302 int ix, iy;
4303 GetImageSize( item->GetImage(), ix, iy );
4304 width += ix + 5;
4305 }
4306
4307 if (!item->GetText().empty())
4308 {
4309 wxCoord w;
4310 dc.GetTextExtent( item->GetText(), &w, NULL );
4311 width += w;
4312 }
4313
4314 return width;
4315 }
4316
4317 // ----------------------------------------------------------------------------
4318 // sorting
4319 // ----------------------------------------------------------------------------
4320
4321 static wxListCtrlCompare list_ctrl_compare_func_2;
4322 static wxIntPtr list_ctrl_compare_data;
4323
4324 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4325 {
4326 wxListLineData *line1 = *arg1;
4327 wxListLineData *line2 = *arg2;
4328 wxListItem item;
4329 line1->GetItem( 0, item );
4330 wxUIntPtr data1 = item.m_data;
4331 line2->GetItem( 0, item );
4332 wxUIntPtr data2 = item.m_data;
4333 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4334 }
4335
4336 void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4337 {
4338 // selections won't make sense any more after sorting the items so reset
4339 // them
4340 HighlightAll(false);
4341 ResetCurrent();
4342
4343 list_ctrl_compare_func_2 = fn;
4344 list_ctrl_compare_data = data;
4345 m_lines.Sort( list_ctrl_compare_func_1 );
4346 m_dirty = true;
4347 }
4348
4349 // ----------------------------------------------------------------------------
4350 // scrolling
4351 // ----------------------------------------------------------------------------
4352
4353 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4354 {
4355 // update our idea of which lines are shown when we redraw the window the
4356 // next time
4357 ResetVisibleLinesRange();
4358
4359 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4360 {
4361 wxGenericListCtrl* lc = GetListCtrl();
4362 wxCHECK_RET( lc, wxT("no listctrl window?") );
4363
4364 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4365 {
4366 lc->m_headerWin->Refresh();
4367 lc->m_headerWin->Update();
4368 }
4369 }
4370 }
4371
4372 int wxListMainWindow::GetCountPerPage() const
4373 {
4374 if ( !m_linesPerPage )
4375 {
4376 wxConstCast(this, wxListMainWindow)->
4377 m_linesPerPage = GetClientSize().y / GetLineHeight();
4378 }
4379
4380 return m_linesPerPage;
4381 }
4382
4383 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4384 {
4385 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4386
4387 if ( m_lineFrom == (size_t)-1 )
4388 {
4389 size_t count = GetItemCount();
4390 if ( count )
4391 {
4392 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
4393
4394 // this may happen if SetScrollbars() hadn't been called yet
4395 if ( m_lineFrom >= count )
4396 m_lineFrom = count - 1;
4397
4398 // we redraw one extra line but this is needed to make the redrawing
4399 // logic work when there is a fractional number of lines on screen
4400 m_lineTo = m_lineFrom + m_linesPerPage;
4401 if ( m_lineTo >= count )
4402 m_lineTo = count - 1;
4403 }
4404 else // empty control
4405 {
4406 m_lineFrom = 0;
4407 m_lineTo = (size_t)-1;
4408 }
4409 }
4410
4411 wxASSERT_MSG( IsEmpty() ||
4412 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4413 wxT("GetVisibleLinesRange() returns incorrect result") );
4414
4415 if ( from )
4416 *from = m_lineFrom;
4417 if ( to )
4418 *to = m_lineTo;
4419 }
4420
4421 size_t
4422 wxListMainWindow::PrefixFindItem(size_t idParent,
4423 const wxString& prefixOrig) const
4424 {
4425 // if no items then just return
4426 if ( idParent == (size_t)-1 )
4427 return idParent;
4428
4429 // match is case insensitive as this is more convenient to the user: having
4430 // to press Shift-letter to go to the item starting with a capital letter
4431 // would be too bothersome
4432 wxString prefix = prefixOrig.Lower();
4433
4434 // determine the starting point: we shouldn't take the current item (this
4435 // allows to switch between two items starting with the same letter just by
4436 // pressing it) but we shouldn't jump to the next one if the user is
4437 // continuing to type as otherwise he might easily skip the item he wanted
4438 size_t itemid = idParent;
4439 if ( prefix.length() == 1 )
4440 {
4441 itemid += 1;
4442 }
4443
4444 // look for the item starting with the given prefix after it
4445 while ( ( itemid < (size_t)GetItemCount() ) &&
4446 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4447 {
4448 itemid += 1;
4449 }
4450
4451 // if we haven't found anything...
4452 if ( !( itemid < (size_t)GetItemCount() ) )
4453 {
4454 // ... wrap to the beginning
4455 itemid = 0;
4456
4457 // and try all the items (stop when we get to the one we started from)
4458 while ( ( itemid < (size_t)GetItemCount() ) && itemid != idParent &&
4459 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4460 {
4461 itemid += 1;
4462 }
4463 // If we haven't found the item, id will be (size_t)-1, as per
4464 // documentation
4465 if ( !( itemid < (size_t)GetItemCount() ) ||
4466 ( ( itemid == idParent ) &&
4467 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) ) )
4468 {
4469 itemid = (size_t)-1;
4470 }
4471 }
4472
4473 return itemid;
4474 }
4475
4476 // -------------------------------------------------------------------------------------
4477 // wxGenericListCtrl
4478 // -------------------------------------------------------------------------------------
4479
4480 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4481
4482 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxListCtrlBase)
4483 EVT_SIZE(wxGenericListCtrl::OnSize)
4484 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
4485 END_EVENT_TABLE()
4486
4487 void wxGenericListCtrl::Init()
4488 {
4489 m_imageListNormal = NULL;
4490 m_imageListSmall = NULL;
4491 m_imageListState = NULL;
4492
4493 m_ownsImageListNormal =
4494 m_ownsImageListSmall =
4495 m_ownsImageListState = false;
4496
4497 m_mainWin = NULL;
4498 m_headerWin = NULL;
4499 }
4500
4501 wxGenericListCtrl::~wxGenericListCtrl()
4502 {
4503 if (m_ownsImageListNormal)
4504 delete m_imageListNormal;
4505 if (m_ownsImageListSmall)
4506 delete m_imageListSmall;
4507 if (m_ownsImageListState)
4508 delete m_imageListState;
4509 }
4510
4511 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4512 {
4513 bool needs_header = HasHeader();
4514 bool has_header = (m_headerWin != NULL);
4515
4516 if (needs_header == has_header)
4517 return;
4518
4519 if (needs_header)
4520 {
4521 m_headerWin = new wxListHeaderWindow
4522 (
4523 this, wxID_ANY, m_mainWin,
4524 wxPoint(0,0),
4525 wxSize
4526 (
4527 GetClientSize().x,
4528 wxRendererNative::Get().GetHeaderButtonHeight(this)
4529 ),
4530 wxTAB_TRAVERSAL
4531 );
4532
4533 #if defined( __WXMAC__ )
4534 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
4535 m_headerWin->SetFont( font );
4536 #endif
4537
4538 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4539 }
4540 else
4541 {
4542 GetSizer()->Detach( m_headerWin );
4543
4544 wxDELETE(m_headerWin);
4545 }
4546 }
4547
4548 bool wxGenericListCtrl::Create(wxWindow *parent,
4549 wxWindowID id,
4550 const wxPoint &pos,
4551 const wxSize &size,
4552 long style,
4553 const wxValidator &validator,
4554 const wxString &name)
4555 {
4556 Init();
4557
4558 // just like in other ports, an assert will fail if the user doesn't give any type style:
4559 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
4560 wxT("wxListCtrl style should have exactly one mode bit set") );
4561
4562 if ( !wxListCtrlBase::Create( parent, id, pos, size,
4563 style | wxVSCROLL | wxHSCROLL,
4564 validator, name ) )
4565 return false;
4566
4567 m_mainWin = new wxListMainWindow(this, wxID_ANY, wxPoint(0, 0), size);
4568
4569 SetTargetWindow( m_mainWin );
4570
4571 // We use the cursor keys for moving the selection, not scrolling, so call
4572 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4573 // keyboard events forwarded to us from wxListMainWindow.
4574 DisableKeyboardScrolling();
4575
4576 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4577 sizer->Add( m_mainWin, 1, wxGROW );
4578 SetSizer( sizer );
4579
4580 CreateOrDestroyHeaderWindowAsNeeded();
4581
4582 SetInitialSize(size);
4583
4584 return true;
4585 }
4586
4587 wxBorder wxGenericListCtrl::GetDefaultBorder() const
4588 {
4589 return wxBORDER_THEME;
4590 }
4591
4592 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4593 WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4594 WXWPARAM wParam,
4595 WXLPARAM lParam)
4596 {
4597 WXLRESULT rc = wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4598
4599 // we need to process arrows ourselves for scrolling
4600 if ( nMsg == WM_GETDLGCODE )
4601 {
4602 rc |= DLGC_WANTARROWS;
4603 }
4604
4605 return rc;
4606 }
4607 #endif // __WXMSW__
4608
4609 wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4610 {
4611 wxSize newsize = size;
4612 if (m_headerWin)
4613 newsize.y -= m_headerWin->GetSize().y;
4614
4615 return newsize;
4616 }
4617
4618 void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4619 {
4620 // update our idea of which lines are shown when we redraw
4621 // the window the next time
4622 m_mainWin->ResetVisibleLinesRange();
4623
4624 HandleOnScroll( event );
4625
4626 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4627 {
4628 m_headerWin->Refresh();
4629 m_headerWin->Update();
4630 }
4631 }
4632
4633 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4634 {
4635 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4636 wxT("wxLC_VIRTUAL can't be [un]set") );
4637
4638 long flag = GetWindowStyle();
4639
4640 if (add)
4641 {
4642 if (style & wxLC_MASK_TYPE)
4643 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4644 if (style & wxLC_MASK_ALIGN)
4645 flag &= ~wxLC_MASK_ALIGN;
4646 if (style & wxLC_MASK_SORT)
4647 flag &= ~wxLC_MASK_SORT;
4648 }
4649
4650 if (add)
4651 flag |= style;
4652 else
4653 flag &= ~style;
4654
4655 // some styles can be set without recreating everything (as happens in
4656 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4657 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4658 {
4659 Refresh();
4660 wxWindow::SetWindowStyleFlag(flag);
4661 }
4662 else
4663 {
4664 SetWindowStyleFlag( flag );
4665 }
4666 }
4667
4668 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4669 {
4670 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4671 // makes sense to remove them as we'll always add scrollbars anyhow when
4672 // needed
4673 flag |= wxHSCROLL | wxVSCROLL;
4674
4675 const bool wasInReportView = HasFlag(wxLC_REPORT);
4676
4677 // update the window style first so that the header is created or destroyed
4678 // corresponding to the new style
4679 wxWindow::SetWindowStyleFlag( flag );
4680
4681 if (m_mainWin)
4682 {
4683 const bool inReportView = (flag & wxLC_REPORT) != 0;
4684 if ( inReportView != wasInReportView )
4685 {
4686 // we need to notify the main window about this change as it must
4687 // update its data structures
4688 m_mainWin->SetReportView(inReportView);
4689 }
4690
4691 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4692
4693 CreateOrDestroyHeaderWindowAsNeeded();
4694
4695 GetSizer()->Layout();
4696 }
4697 }
4698
4699 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4700 {
4701 m_mainWin->GetColumn( col, item );
4702 return true;
4703 }
4704
4705 bool wxGenericListCtrl::SetColumn( int col, const wxListItem& item )
4706 {
4707 m_mainWin->SetColumn( col, item );
4708 return true;
4709 }
4710
4711 int wxGenericListCtrl::GetColumnWidth( int col ) const
4712 {
4713 return m_mainWin->GetColumnWidth( col );
4714 }
4715
4716 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
4717 {
4718 m_mainWin->SetColumnWidth( col, width );
4719 return true;
4720 }
4721
4722 int wxGenericListCtrl::GetCountPerPage() const
4723 {
4724 return m_mainWin->GetCountPerPage(); // different from Windows ?
4725 }
4726
4727 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
4728 {
4729 m_mainWin->GetItem( info );
4730 return true;
4731 }
4732
4733 bool wxGenericListCtrl::SetItem( wxListItem &info )
4734 {
4735 m_mainWin->SetItem( info );
4736 return true;
4737 }
4738
4739 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
4740 {
4741 wxListItem info;
4742 info.m_text = label;
4743 info.m_mask = wxLIST_MASK_TEXT;
4744 info.m_itemId = index;
4745 info.m_col = col;
4746 if ( imageId > -1 )
4747 {
4748 info.m_image = imageId;
4749 info.m_mask |= wxLIST_MASK_IMAGE;
4750 }
4751
4752 m_mainWin->SetItem(info);
4753 return true;
4754 }
4755
4756 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
4757 {
4758 return m_mainWin->GetItemState( item, stateMask );
4759 }
4760
4761 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
4762 {
4763 m_mainWin->SetItemState( item, state, stateMask );
4764 return true;
4765 }
4766
4767 bool
4768 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
4769 {
4770 return SetItemColumnImage(item, 0, image);
4771 }
4772
4773 bool
4774 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
4775 {
4776 wxListItem info;
4777 info.m_image = image;
4778 info.m_mask = wxLIST_MASK_IMAGE;
4779 info.m_itemId = item;
4780 info.m_col = column;
4781 m_mainWin->SetItem( info );
4782 return true;
4783 }
4784
4785 wxString wxGenericListCtrl::GetItemText( long item, int col ) const
4786 {
4787 return m_mainWin->GetItemText(item, col);
4788 }
4789
4790 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
4791 {
4792 m_mainWin->SetItemText(item, str);
4793 }
4794
4795 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
4796 {
4797 wxListItem info;
4798 info.m_mask = wxLIST_MASK_DATA;
4799 info.m_itemId = item;
4800 m_mainWin->GetItem( info );
4801 return info.m_data;
4802 }
4803
4804 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
4805 {
4806 wxListItem info;
4807 info.m_mask = wxLIST_MASK_DATA;
4808 info.m_itemId = item;
4809 info.m_data = data;
4810 m_mainWin->SetItem( info );
4811 return true;
4812 }
4813
4814 wxRect wxGenericListCtrl::GetViewRect() const
4815 {
4816 return m_mainWin->GetViewRect();
4817 }
4818
4819 bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4820 {
4821 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4822 }
4823
4824 bool wxGenericListCtrl::GetSubItemRect(long item,
4825 long subItem,
4826 wxRect& rect,
4827 int WXUNUSED(code)) const
4828 {
4829 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4830 return false;
4831
4832 if ( m_mainWin->HasHeader() )
4833 rect.y += m_headerWin->GetSize().y + 1;
4834
4835 return true;
4836 }
4837
4838 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
4839 {
4840 m_mainWin->GetItemPosition( item, pos );
4841 return true;
4842 }
4843
4844 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
4845 {
4846 return false;
4847 }
4848
4849 int wxGenericListCtrl::GetItemCount() const
4850 {
4851 return m_mainWin->GetItemCount();
4852 }
4853
4854 int wxGenericListCtrl::GetColumnCount() const
4855 {
4856 return m_mainWin->GetColumnCount();
4857 }
4858
4859 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
4860 {
4861 m_mainWin->SetItemSpacing( spacing, isSmall );
4862 }
4863
4864 wxSize wxGenericListCtrl::GetItemSpacing() const
4865 {
4866 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
4867
4868 return wxSize(spacing, spacing);
4869 }
4870
4871 #if WXWIN_COMPATIBILITY_2_6
4872 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
4873 {
4874 return m_mainWin->GetItemSpacing( isSmall );
4875 }
4876 #endif // WXWIN_COMPATIBILITY_2_6
4877
4878 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
4879 {
4880 wxListItem info;
4881 info.m_itemId = item;
4882 info.SetTextColour( col );
4883 m_mainWin->SetItem( info );
4884 }
4885
4886 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
4887 {
4888 wxListItem info;
4889 info.m_itemId = item;
4890 m_mainWin->GetItem( info );
4891 return info.GetTextColour();
4892 }
4893
4894 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
4895 {
4896 wxListItem info;
4897 info.m_itemId = item;
4898 info.SetBackgroundColour( col );
4899 m_mainWin->SetItem( info );
4900 }
4901
4902 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
4903 {
4904 wxListItem info;
4905 info.m_itemId = item;
4906 m_mainWin->GetItem( info );
4907 return info.GetBackgroundColour();
4908 }
4909
4910 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4911 {
4912 wxListItem info;
4913 info.m_itemId = item;
4914 info.SetFont( f );
4915 m_mainWin->SetItem( info );
4916 }
4917
4918 wxFont wxGenericListCtrl::GetItemFont( long item ) const
4919 {
4920 wxListItem info;
4921 info.m_itemId = item;
4922 m_mainWin->GetItem( info );
4923 return info.GetFont();
4924 }
4925
4926 int wxGenericListCtrl::GetSelectedItemCount() const
4927 {
4928 return m_mainWin->GetSelectedItemCount();
4929 }
4930
4931 wxColour wxGenericListCtrl::GetTextColour() const
4932 {
4933 return GetForegroundColour();
4934 }
4935
4936 void wxGenericListCtrl::SetTextColour(const wxColour& col)
4937 {
4938 SetForegroundColour(col);
4939 }
4940
4941 long wxGenericListCtrl::GetTopItem() const
4942 {
4943 size_t top;
4944 m_mainWin->GetVisibleLinesRange(&top, NULL);
4945 return (long)top;
4946 }
4947
4948 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
4949 {
4950 return m_mainWin->GetNextItem( item, geom, state );
4951 }
4952
4953 wxImageList *wxGenericListCtrl::GetImageList(int which) const
4954 {
4955 if (which == wxIMAGE_LIST_NORMAL)
4956 return m_imageListNormal;
4957 else if (which == wxIMAGE_LIST_SMALL)
4958 return m_imageListSmall;
4959 else if (which == wxIMAGE_LIST_STATE)
4960 return m_imageListState;
4961
4962 return NULL;
4963 }
4964
4965 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
4966 {
4967 if ( which == wxIMAGE_LIST_NORMAL )
4968 {
4969 if (m_ownsImageListNormal)
4970 delete m_imageListNormal;
4971 m_imageListNormal = imageList;
4972 m_ownsImageListNormal = false;
4973 }
4974 else if ( which == wxIMAGE_LIST_SMALL )
4975 {
4976 if (m_ownsImageListSmall)
4977 delete m_imageListSmall;
4978 m_imageListSmall = imageList;
4979 m_ownsImageListSmall = false;
4980 }
4981 else if ( which == wxIMAGE_LIST_STATE )
4982 {
4983 if (m_ownsImageListState)
4984 delete m_imageListState;
4985 m_imageListState = imageList;
4986 m_ownsImageListState = false;
4987 }
4988
4989 m_mainWin->SetImageList( imageList, which );
4990 }
4991
4992 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
4993 {
4994 SetImageList(imageList, which);
4995 if ( which == wxIMAGE_LIST_NORMAL )
4996 m_ownsImageListNormal = true;
4997 else if ( which == wxIMAGE_LIST_SMALL )
4998 m_ownsImageListSmall = true;
4999 else if ( which == wxIMAGE_LIST_STATE )
5000 m_ownsImageListState = true;
5001 }
5002
5003 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5004 {
5005 return 0;
5006 }
5007
5008 bool wxGenericListCtrl::DeleteItem( long item )
5009 {
5010 m_mainWin->DeleteItem( item );
5011 return true;
5012 }
5013
5014 bool wxGenericListCtrl::DeleteAllItems()
5015 {
5016 m_mainWin->DeleteAllItems();
5017 return true;
5018 }
5019
5020 bool wxGenericListCtrl::DeleteAllColumns()
5021 {
5022 size_t count = m_mainWin->m_columns.GetCount();
5023 for ( size_t n = 0; n < count; n++ )
5024 DeleteColumn( 0 );
5025 return true;
5026 }
5027
5028 void wxGenericListCtrl::ClearAll()
5029 {
5030 m_mainWin->DeleteEverything();
5031 }
5032
5033 bool wxGenericListCtrl::DeleteColumn( int col )
5034 {
5035 m_mainWin->DeleteColumn( col );
5036
5037 // if we don't have the header any longer, we need to relayout the window
5038 // if ( !GetColumnCount() )
5039
5040
5041 // Ensure that the non-existent columns are really removed from display.
5042 Refresh();
5043
5044 return true;
5045 }
5046
5047 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5048 wxClassInfo* textControlClass)
5049 {
5050 return m_mainWin->EditLabel( item, textControlClass );
5051 }
5052
5053 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5054 {
5055 return m_mainWin->GetEditControl();
5056 }
5057
5058 bool wxGenericListCtrl::EnsureVisible( long item )
5059 {
5060 m_mainWin->EnsureVisible( item );
5061 return true;
5062 }
5063
5064 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5065 {
5066 return m_mainWin->FindItem( start, str, partial );
5067 }
5068
5069 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5070 {
5071 return m_mainWin->FindItem( start, data );
5072 }
5073
5074 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5075 int WXUNUSED(direction))
5076 {
5077 return m_mainWin->FindItem( pt );
5078 }
5079
5080 // TODO: sub item hit testing
5081 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5082 {
5083 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5084 }
5085
5086 long wxGenericListCtrl::InsertItem( wxListItem& info )
5087 {
5088 m_mainWin->InsertItem( info );
5089 return info.m_itemId;
5090 }
5091
5092 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5093 {
5094 wxListItem info;
5095 info.m_text = label;
5096 info.m_mask = wxLIST_MASK_TEXT;
5097 info.m_itemId = index;
5098 return InsertItem( info );
5099 }
5100
5101 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5102 {
5103 wxListItem info;
5104 info.m_mask = wxLIST_MASK_IMAGE;
5105 info.m_image = imageIndex;
5106 info.m_itemId = index;
5107 return InsertItem( info );
5108 }
5109
5110 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5111 {
5112 wxListItem info;
5113 info.m_text = label;
5114 info.m_image = imageIndex;
5115 info.m_mask = wxLIST_MASK_TEXT;
5116 if (imageIndex > -1)
5117 info.m_mask |= wxLIST_MASK_IMAGE;
5118 info.m_itemId = index;
5119 return InsertItem( info );
5120 }
5121
5122 long wxGenericListCtrl::DoInsertColumn( long col, const wxListItem &item )
5123 {
5124 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
5125
5126 long idx = m_mainWin->InsertColumn( col, item );
5127
5128 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
5129 // still have m_headerWin==NULL
5130 if (m_headerWin)
5131 m_headerWin->Refresh();
5132
5133 return idx;
5134 }
5135
5136 bool wxGenericListCtrl::ScrollList( int dx, int dy )
5137 {
5138 return m_mainWin->ScrollList(dx, dy);
5139 }
5140
5141 // Sort items.
5142 // fn is a function which takes 3 long arguments: item1, item2, data.
5143 // item1 is the long data associated with a first item (NOT the index).
5144 // item2 is the long data associated with a second item (NOT the index).
5145 // data is the same value as passed to SortItems.
5146 // The return value is a negative number if the first item should precede the second
5147 // item, a positive number of the second item should precede the first,
5148 // or zero if the two items are equivalent.
5149 // data is arbitrary data to be passed to the sort function.
5150
5151 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
5152 {
5153 m_mainWin->SortItems( fn, data );
5154 return true;
5155 }
5156
5157 // ----------------------------------------------------------------------------
5158 // event handlers
5159 // ----------------------------------------------------------------------------
5160
5161 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5162 {
5163 if (!m_mainWin) return;
5164
5165 // We need to override OnSize so that our scrolled
5166 // window a) does call Layout() to use sizers for
5167 // positioning the controls but b) does not query
5168 // the sizer for their size and use that for setting
5169 // the scrollable area as set that ourselves by
5170 // calling SetScrollbar() further down.
5171
5172 Layout();
5173
5174 m_mainWin->RecalculatePositions();
5175
5176 AdjustScrollbars();
5177 }
5178
5179 void wxGenericListCtrl::OnInternalIdle()
5180 {
5181 wxWindow::OnInternalIdle();
5182
5183 if (m_mainWin->m_dirty)
5184 m_mainWin->RecalculatePositions();
5185 }
5186
5187 // ----------------------------------------------------------------------------
5188 // font/colours
5189 // ----------------------------------------------------------------------------
5190
5191 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5192 {
5193 if (m_mainWin)
5194 {
5195 m_mainWin->SetBackgroundColour( colour );
5196 m_mainWin->m_dirty = true;
5197 }
5198
5199 return true;
5200 }
5201
5202 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5203 {
5204 if ( !wxWindow::SetForegroundColour( colour ) )
5205 return false;
5206
5207 if (m_mainWin)
5208 {
5209 m_mainWin->SetForegroundColour( colour );
5210 m_mainWin->m_dirty = true;
5211 }
5212
5213 if (m_headerWin)
5214 m_headerWin->SetForegroundColour( colour );
5215
5216 return true;
5217 }
5218
5219 bool wxGenericListCtrl::SetFont( const wxFont &font )
5220 {
5221 if ( !wxWindow::SetFont( font ) )
5222 return false;
5223
5224 if (m_mainWin)
5225 {
5226 m_mainWin->SetFont( font );
5227 m_mainWin->m_dirty = true;
5228 }
5229
5230 if (m_headerWin)
5231 {
5232 m_headerWin->SetFont( font );
5233 // CalculateAndSetHeaderHeight();
5234 }
5235
5236 Refresh();
5237
5238 return true;
5239 }
5240
5241 // static
5242 wxVisualAttributes
5243 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5244 {
5245 #if _USE_VISATTR
5246 // Use the same color scheme as wxListBox
5247 return wxListBox::GetClassDefaultAttributes(variant);
5248 #else
5249 wxUnusedVar(variant);
5250 wxVisualAttributes attr;
5251 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5252 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5253 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5254 return attr;
5255 #endif
5256 }
5257
5258 // ----------------------------------------------------------------------------
5259 // methods forwarded to m_mainWin
5260 // ----------------------------------------------------------------------------
5261
5262 #if wxUSE_DRAG_AND_DROP
5263
5264 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5265 {
5266 m_mainWin->SetDropTarget( dropTarget );
5267 }
5268
5269 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5270 {
5271 return m_mainWin->GetDropTarget();
5272 }
5273
5274 #endif
5275
5276 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5277 {
5278 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5279 }
5280
5281 wxColour wxGenericListCtrl::GetBackgroundColour() const
5282 {
5283 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5284 }
5285
5286 wxColour wxGenericListCtrl::GetForegroundColour() const
5287 {
5288 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5289 }
5290
5291 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5292 {
5293 #if wxUSE_MENUS
5294 return m_mainWin->PopupMenu( menu, x, y );
5295 #else
5296 return false;
5297 #endif
5298 }
5299
5300 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5301 {
5302 // It's not clear whether this can be called before m_mainWin is created
5303 // but it seems better to be on the safe side and check.
5304 if ( m_mainWin )
5305 m_mainWin->DoClientToScreen(x, y);
5306 else
5307 wxListCtrlBase::DoClientToScreen(x, y);
5308 }
5309
5310 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5311 {
5312 // At least in wxGTK/Univ build this method can be called before m_mainWin
5313 // is created so avoid crashes in this case.
5314 if ( m_mainWin )
5315 m_mainWin->DoScreenToClient(x, y);
5316 else
5317 wxListCtrlBase::DoScreenToClient(x, y);
5318 }
5319
5320 wxSize wxGenericListCtrl::DoGetBestClientSize() const
5321 {
5322 // The base class version can compute the best size in report view only.
5323 wxSize sizeBest = wxListCtrlBase::DoGetBestClientSize();
5324
5325 if ( !InReportView() )
5326 {
5327 // Ensure that our minimal width is at least big enough to show all our
5328 // items. This is important for wxListbook to size itself correctly.
5329
5330 // Remember the offset of the first item: this corresponds to the
5331 // margins around the item so we will add it to the minimal size below
5332 // to ensure that we have equal margins on all sides.
5333 wxPoint ofs;
5334
5335 // We can iterate over all items as there shouldn't be too many of them
5336 // in non-report view. If it ever becomes a problem, we could examine
5337 // just the first few items probably, the determination of the best
5338 // size is less important if we will need scrollbars anyhow.
5339 for ( int n = 0; n < GetItemCount(); n++ )
5340 {
5341 const wxRect itemRect = m_mainWin->GetLineRect(n);
5342 if ( !n )
5343 {
5344 // Remember the position of the first item as all the rest are
5345 // offset by at least this number of pixels too.
5346 ofs = itemRect.GetPosition();
5347 }
5348
5349 sizeBest.IncTo(itemRect.GetSize());
5350 }
5351
5352 sizeBest.IncBy(2*ofs);
5353
5354
5355 // If we have the scrollbars we need to account for them too. And to
5356 // make sure the scrollbars status is up to date we need to call this
5357 // function to set them.
5358 m_mainWin->RecalculatePositions(true /* no refresh */);
5359
5360 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5361 // to use m_mainWin client/virtual size for determination of whether we
5362 // use scrollbars and not the size of this window itself. Maybe that
5363 // function should be extended to work correctly in the case when our
5364 // scrollbars manage a different window from this one but currently it
5365 // doesn't work.
5366 const wxSize sizeClient = m_mainWin->GetClientSize();
5367 const wxSize sizeVirt = m_mainWin->GetVirtualSize();
5368
5369 if ( sizeVirt.x > sizeClient.x /* HasScrollbar(wxHORIZONTAL) */ )
5370 sizeBest.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
5371
5372 if ( sizeVirt.y > sizeClient.y /* HasScrollbar(wxVERTICAL) */ )
5373 sizeBest.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
5374 }
5375
5376 return sizeBest;
5377 }
5378
5379 // ----------------------------------------------------------------------------
5380 // virtual list control support
5381 // ----------------------------------------------------------------------------
5382
5383 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5384 {
5385 // this is a pure virtual function, in fact - which is not really pure
5386 // because the controls which are not virtual don't need to implement it
5387 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5388
5389 return wxEmptyString;
5390 }
5391
5392 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5393 {
5394 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5395 -1,
5396 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5397 return -1;
5398 }
5399
5400 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5401 {
5402 if (!column)
5403 return OnGetItemImage(item);
5404
5405 return -1;
5406 }
5407
5408 wxListItemAttr *
5409 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5410 {
5411 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5412 wxT("invalid item index in OnGetItemAttr()") );
5413
5414 // no attributes by default
5415 return NULL;
5416 }
5417
5418 void wxGenericListCtrl::SetItemCount(long count)
5419 {
5420 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5421
5422 m_mainWin->SetItemCount(count);
5423 }
5424
5425 void wxGenericListCtrl::RefreshItem(long item)
5426 {
5427 m_mainWin->RefreshLine(item);
5428 }
5429
5430 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5431 {
5432 m_mainWin->RefreshLines(itemFrom, itemTo);
5433 }
5434
5435 void wxGenericListCtrl::EnableBellOnNoMatch( bool on )
5436 {
5437 m_mainWin->EnableBellOnNoMatch(on);
5438 }
5439
5440 // Generic wxListCtrl is more or less a container for two other
5441 // windows which drawings are done upon. These are namely
5442 // 'm_headerWin' and 'm_mainWin'.
5443 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5444 // behaviour wxListCtrl has under wxMSW.
5445 //
5446 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5447 {
5448 if (!rect)
5449 {
5450 // The easy case, no rectangle specified.
5451 if (m_headerWin)
5452 m_headerWin->Refresh(eraseBackground);
5453
5454 if (m_mainWin)
5455 m_mainWin->Refresh(eraseBackground);
5456 }
5457 else
5458 {
5459 // Refresh the header window
5460 if (m_headerWin)
5461 {
5462 wxRect rectHeader = m_headerWin->GetRect();
5463 rectHeader.Intersect(*rect);
5464 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5465 {
5466 int x, y;
5467 m_headerWin->GetPosition(&x, &y);
5468 rectHeader.Offset(-x, -y);
5469 m_headerWin->Refresh(eraseBackground, &rectHeader);
5470 }
5471 }
5472
5473 // Refresh the main window
5474 if (m_mainWin)
5475 {
5476 wxRect rectMain = m_mainWin->GetRect();
5477 rectMain.Intersect(*rect);
5478 if (rectMain.GetWidth() && rectMain.GetHeight())
5479 {
5480 int x, y;
5481 m_mainWin->GetPosition(&x, &y);
5482 rectMain.Offset(-x, -y);
5483 m_mainWin->Refresh(eraseBackground, &rectMain);
5484 }
5485 }
5486 }
5487 }
5488
5489 void wxGenericListCtrl::Update()
5490 {
5491 if ( m_mainWin )
5492 {
5493 if ( m_mainWin->m_dirty )
5494 m_mainWin->RecalculatePositions();
5495
5496 m_mainWin->Update();
5497 }
5498
5499 if ( m_headerWin )
5500 m_headerWin->Update();
5501 }
5502
5503 #endif // wxUSE_LISTCTRL