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