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