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