Fix focus handling logic in generic wxListCtrl.
[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 long style,
1587 const wxString &name )
1588 : wxWindow( parent, id, pos, size, style, name )
1589 {
1590 Init();
1591
1592 m_highlightBrush = new wxBrush
1593 (
1594 wxSystemSettings::GetColour
1595 (
1596 wxSYS_COLOUR_HIGHLIGHT
1597 ),
1598 wxBRUSHSTYLE_SOLID
1599 );
1600
1601 m_highlightUnfocusedBrush = new wxBrush
1602 (
1603 wxSystemSettings::GetColour
1604 (
1605 wxSYS_COLOUR_BTNSHADOW
1606 ),
1607 wxBRUSHSTYLE_SOLID
1608 );
1609
1610 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
1611 SetOwnForegroundColour( attr.colFg );
1612 SetOwnBackgroundColour( attr.colBg );
1613 if (!m_hasFont)
1614 SetOwnFont( attr.font );
1615 }
1616
1617 wxListMainWindow::~wxListMainWindow()
1618 {
1619 if ( m_textctrlWrapper )
1620 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
1621
1622 DoDeleteAllItems();
1623 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
1624 WX_CLEAR_ARRAY(m_aColWidths);
1625
1626 delete m_highlightBrush;
1627 delete m_highlightUnfocusedBrush;
1628 delete m_renameTimer;
1629 }
1630
1631 void wxListMainWindow::SetReportView(bool inReportView)
1632 {
1633 const size_t count = m_lines.size();
1634 for ( size_t n = 0; n < count; n++ )
1635 {
1636 m_lines[n].SetReportView(inReportView);
1637 }
1638 }
1639
1640 void wxListMainWindow::CacheLineData(size_t line)
1641 {
1642 wxGenericListCtrl *listctrl = GetListCtrl();
1643
1644 wxListLineData *ld = GetDummyLine();
1645
1646 size_t countCol = GetColumnCount();
1647 for ( size_t col = 0; col < countCol; col++ )
1648 {
1649 ld->SetText(col, listctrl->OnGetItemText(line, col));
1650 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
1651 }
1652
1653 ld->SetAttr(listctrl->OnGetItemAttr(line));
1654 }
1655
1656 wxListLineData *wxListMainWindow::GetDummyLine() const
1657 {
1658 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1659 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1660
1661 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1662
1663 // we need to recreate the dummy line if the number of columns in the
1664 // control changed as it would have the incorrect number of fields
1665 // otherwise
1666 if ( !m_lines.IsEmpty() &&
1667 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
1668 {
1669 self->m_lines.Clear();
1670 }
1671
1672 if ( m_lines.IsEmpty() )
1673 {
1674 wxListLineData *line = new wxListLineData(self);
1675 self->m_lines.Add(line);
1676
1677 // don't waste extra memory -- there never going to be anything
1678 // else/more in this array
1679 self->m_lines.Shrink();
1680 }
1681
1682 return &m_lines[0];
1683 }
1684
1685 // ----------------------------------------------------------------------------
1686 // line geometry (report mode only)
1687 // ----------------------------------------------------------------------------
1688
1689 wxCoord wxListMainWindow::GetLineHeight() const
1690 {
1691 // we cache the line height as calling GetTextExtent() is slow
1692 if ( !m_lineHeight )
1693 {
1694 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1695
1696 wxClientDC dc( self );
1697 dc.SetFont( GetFont() );
1698
1699 wxCoord y;
1700 dc.GetTextExtent(wxT("H"), NULL, &y);
1701
1702 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1703 {
1704 int iw = 0, ih = 0;
1705 m_small_image_list->GetSize(0, iw, ih);
1706 y = wxMax(y, ih);
1707 }
1708
1709 y += EXTRA_HEIGHT;
1710 self->m_lineHeight = y + LINE_SPACING;
1711 }
1712
1713 return m_lineHeight;
1714 }
1715
1716 wxCoord wxListMainWindow::GetLineY(size_t line) const
1717 {
1718 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1719
1720 return LINE_SPACING + line * GetLineHeight();
1721 }
1722
1723 wxRect wxListMainWindow::GetLineRect(size_t line) const
1724 {
1725 if ( !InReportView() )
1726 return GetLine(line)->m_gi->m_rectAll;
1727
1728 wxRect rect;
1729 rect.x = HEADER_OFFSET_X;
1730 rect.y = GetLineY(line);
1731 rect.width = GetHeaderWidth();
1732 rect.height = GetLineHeight();
1733
1734 return rect;
1735 }
1736
1737 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1738 {
1739 if ( !InReportView() )
1740 return GetLine(line)->m_gi->m_rectLabel;
1741
1742 int image_x = 0;
1743 wxListLineData *data = GetLine(line);
1744 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1745 if (node)
1746 {
1747 wxListItemData *item = node->GetData();
1748 if ( item->HasImage() )
1749 {
1750 int ix, iy;
1751 GetImageSize( item->GetImage(), ix, iy );
1752 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1753 }
1754 }
1755
1756 wxRect rect;
1757 rect.x = image_x + HEADER_OFFSET_X;
1758 rect.y = GetLineY(line);
1759 rect.width = GetColumnWidth(0) - image_x;
1760 rect.height = GetLineHeight();
1761
1762 return rect;
1763 }
1764
1765 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1766 {
1767 if ( !InReportView() )
1768 return GetLine(line)->m_gi->m_rectIcon;
1769
1770 wxListLineData *ld = GetLine(line);
1771 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
1772
1773 wxRect rect;
1774 rect.x = HEADER_OFFSET_X;
1775 rect.y = GetLineY(line);
1776 GetImageSize(ld->GetImage(), rect.width, rect.height);
1777
1778 return rect;
1779 }
1780
1781 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1782 {
1783 return InReportView() ? GetLineRect(line)
1784 : GetLine(line)->m_gi->m_rectHighlight;
1785 }
1786
1787 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1788 {
1789 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
1790
1791 wxListLineData *ld = GetLine(line);
1792
1793 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
1794 return wxLIST_HITTEST_ONITEMICON;
1795
1796 // VS: Testing for "ld->HasText() || InReportView()" instead of
1797 // "ld->HasText()" is needed to make empty lines in report view
1798 // possible
1799 if ( ld->HasText() || InReportView() )
1800 {
1801 wxRect rect = InReportView() ? GetLineRect(line)
1802 : GetLineLabelRect(line);
1803
1804 if ( rect.Contains(x, y) )
1805 return wxLIST_HITTEST_ONITEMLABEL;
1806 }
1807
1808 return 0;
1809 }
1810
1811 // ----------------------------------------------------------------------------
1812 // highlight (selection) handling
1813 // ----------------------------------------------------------------------------
1814
1815 bool wxListMainWindow::IsHighlighted(size_t line) const
1816 {
1817 if ( IsVirtual() )
1818 {
1819 return m_selStore.IsSelected(line);
1820 }
1821 else // !virtual
1822 {
1823 wxListLineData *ld = GetLine(line);
1824 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
1825
1826 return ld->IsHighlighted();
1827 }
1828 }
1829
1830 void wxListMainWindow::HighlightLines( size_t lineFrom,
1831 size_t lineTo,
1832 bool highlight )
1833 {
1834 if ( IsVirtual() )
1835 {
1836 wxArrayInt linesChanged;
1837 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1838 &linesChanged) )
1839 {
1840 // meny items changed state, refresh everything
1841 RefreshLines(lineFrom, lineTo);
1842 }
1843 else // only a few items changed state, refresh only them
1844 {
1845 size_t count = linesChanged.GetCount();
1846 for ( size_t n = 0; n < count; n++ )
1847 {
1848 RefreshLine(linesChanged[n]);
1849 }
1850 }
1851 }
1852 else // iterate over all items in non report view
1853 {
1854 for ( size_t line = lineFrom; line <= lineTo; line++ )
1855 {
1856 if ( HighlightLine(line, highlight) )
1857 RefreshLine(line);
1858 }
1859 }
1860 }
1861
1862 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1863 {
1864 bool changed;
1865
1866 if ( IsVirtual() )
1867 {
1868 changed = m_selStore.SelectItem(line, highlight);
1869 }
1870 else // !virtual
1871 {
1872 wxListLineData *ld = GetLine(line);
1873 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
1874
1875 changed = ld->Highlight(highlight);
1876 }
1877
1878 if ( changed )
1879 {
1880 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1881 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1882 }
1883
1884 return changed;
1885 }
1886
1887 void wxListMainWindow::RefreshLine( size_t line )
1888 {
1889 if ( InReportView() )
1890 {
1891 size_t visibleFrom, visibleTo;
1892 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1893
1894 if ( line < visibleFrom || line > visibleTo )
1895 return;
1896 }
1897
1898 wxRect rect = GetLineRect(line);
1899
1900 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1901 RefreshRect( rect );
1902 }
1903
1904 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1905 {
1906 // we suppose that they are ordered by caller
1907 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
1908
1909 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
1910
1911 if ( InReportView() )
1912 {
1913 size_t visibleFrom, visibleTo;
1914 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1915
1916 if ( lineFrom < visibleFrom )
1917 lineFrom = visibleFrom;
1918 if ( lineTo > visibleTo )
1919 lineTo = visibleTo;
1920
1921 wxRect rect;
1922 rect.x = 0;
1923 rect.y = GetLineY(lineFrom);
1924 rect.width = GetClientSize().x;
1925 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
1926
1927 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1928 RefreshRect( rect );
1929 }
1930 else // !report
1931 {
1932 // TODO: this should be optimized...
1933 for ( size_t line = lineFrom; line <= lineTo; line++ )
1934 {
1935 RefreshLine(line);
1936 }
1937 }
1938 }
1939
1940 void wxListMainWindow::RefreshAfter( size_t lineFrom )
1941 {
1942 if ( InReportView() )
1943 {
1944 size_t visibleFrom, visibleTo;
1945 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1946
1947 if ( lineFrom < visibleFrom )
1948 lineFrom = visibleFrom;
1949 else if ( lineFrom > visibleTo )
1950 return;
1951
1952 wxRect rect;
1953 rect.x = 0;
1954 rect.y = GetLineY(lineFrom);
1955 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1956
1957 wxSize size = GetClientSize();
1958 rect.width = size.x;
1959
1960 // refresh till the bottom of the window
1961 rect.height = size.y - rect.y;
1962
1963 RefreshRect( rect );
1964 }
1965 else // !report
1966 {
1967 // TODO: how to do it more efficiently?
1968 m_dirty = true;
1969 }
1970 }
1971
1972 void wxListMainWindow::RefreshSelected()
1973 {
1974 if ( IsEmpty() )
1975 return;
1976
1977 size_t from, to;
1978 if ( InReportView() )
1979 {
1980 GetVisibleLinesRange(&from, &to);
1981 }
1982 else // !virtual
1983 {
1984 from = 0;
1985 to = GetItemCount() - 1;
1986 }
1987
1988 if ( HasCurrent() && m_current >= from && m_current <= to )
1989 RefreshLine(m_current);
1990
1991 for ( size_t line = from; line <= to; line++ )
1992 {
1993 // NB: the test works as expected even if m_current == -1
1994 if ( line != m_current && IsHighlighted(line) )
1995 RefreshLine(line);
1996 }
1997 }
1998
1999 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2000 {
2001 // Note: a wxPaintDC must be constructed even if no drawing is
2002 // done (a Windows requirement).
2003 wxPaintDC dc( this );
2004
2005 if ( IsEmpty() )
2006 {
2007 // nothing to draw or not the moment to draw it
2008 return;
2009 }
2010
2011 if ( m_dirty )
2012 RecalculatePositions( false );
2013
2014 GetListCtrl()->PrepareDC( dc );
2015
2016 int dev_x, dev_y;
2017 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2018
2019 dc.SetFont( GetFont() );
2020
2021 if ( InReportView() )
2022 {
2023 int lineHeight = GetLineHeight();
2024
2025 size_t visibleFrom, visibleTo;
2026 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2027
2028 wxRect rectLine;
2029 int xOrig = dc.LogicalToDeviceX( 0 );
2030 int yOrig = dc.LogicalToDeviceY( 0 );
2031
2032 // tell the caller cache to cache the data
2033 if ( IsVirtual() )
2034 {
2035 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2036 GetParent()->GetId());
2037 evCache.SetEventObject( GetParent() );
2038 evCache.m_oldItemIndex = visibleFrom;
2039 evCache.m_item.m_itemId =
2040 evCache.m_itemIndex = visibleTo;
2041 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2042 }
2043
2044 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2045 {
2046 rectLine = GetLineRect(line);
2047
2048
2049 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2050 rectLine.width, rectLine.height) )
2051 {
2052 // don't redraw unaffected lines to avoid flicker
2053 continue;
2054 }
2055
2056 GetLine(line)->DrawInReportMode( &dc,
2057 rectLine,
2058 GetLineHighlightRect(line),
2059 IsHighlighted(line),
2060 line == m_current );
2061 }
2062
2063 if ( HasFlag(wxLC_HRULES) )
2064 {
2065 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2066 wxSize clientSize = GetClientSize();
2067
2068 size_t i = visibleFrom;
2069 if (i == 0) i = 1; // Don't draw the first one
2070 for ( ; i <= visibleTo; i++ )
2071 {
2072 dc.SetPen(pen);
2073 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2074 dc.DrawLine(0 - dev_x, i * lineHeight,
2075 clientSize.x - dev_x, i * lineHeight);
2076 }
2077
2078 // Draw last horizontal rule
2079 if ( visibleTo == GetItemCount() - 1 )
2080 {
2081 dc.SetPen( pen );
2082 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2083 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2084 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2085 }
2086 }
2087
2088 // Draw vertical rules if required
2089 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2090 {
2091 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2092 wxRect firstItemRect, lastItemRect;
2093
2094 GetItemRect(visibleFrom, firstItemRect);
2095 GetItemRect(visibleTo, lastItemRect);
2096 int x = firstItemRect.GetX();
2097 dc.SetPen(pen);
2098 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2099
2100 for (int col = 0; col < GetColumnCount(); col++)
2101 {
2102 int colWidth = GetColumnWidth(col);
2103 x += colWidth;
2104 int x_pos = x - dev_x;
2105 if (col < GetColumnCount()-1) x_pos -= 2;
2106 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2107 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2108 }
2109 }
2110 }
2111 else // !report
2112 {
2113 size_t count = GetItemCount();
2114 for ( size_t i = 0; i < count; i++ )
2115 {
2116 GetLine(i)->Draw( &dc, i == m_current );
2117 }
2118 }
2119
2120 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2121 // rectangle somehow and so leaves traces when the item is not selected any
2122 // more, see #12229.
2123 #ifndef __WXMAC__
2124 if ( HasCurrent() )
2125 {
2126 int flags = 0;
2127 if ( IsHighlighted(m_current) )
2128 flags |= wxCONTROL_SELECTED;
2129
2130 wxRendererNative::Get().
2131 DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
2132 }
2133 #endif // !__WXMAC__
2134 }
2135
2136 void wxListMainWindow::HighlightAll( bool on )
2137 {
2138 if ( IsSingleSel() )
2139 {
2140 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
2141
2142 // we just have one item to turn off
2143 if ( HasCurrent() && IsHighlighted(m_current) )
2144 {
2145 HighlightLine(m_current, false);
2146 RefreshLine(m_current);
2147 }
2148 }
2149 else // multi selection
2150 {
2151 if ( !IsEmpty() )
2152 HighlightLines(0, GetItemCount() - 1, on);
2153 }
2154 }
2155
2156 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2157 {
2158 // Do nothing here. This prevents the default handler in wxScrolledWindow
2159 // from needlessly scrolling the window when the edit control is
2160 // dismissed. See ticket #9563.
2161 }
2162
2163 void wxListMainWindow::SendNotify( size_t line,
2164 wxEventType command,
2165 const wxPoint& point )
2166 {
2167 wxListEvent le( command, GetParent()->GetId() );
2168 le.SetEventObject( GetParent() );
2169
2170 le.m_item.m_itemId =
2171 le.m_itemIndex = line;
2172
2173 // set only for events which have position
2174 if ( point != wxDefaultPosition )
2175 le.m_pointDrag = point;
2176
2177 // don't try to get the line info for virtual list controls: the main
2178 // program has it anyhow and if we did it would result in accessing all
2179 // the lines, even those which are not visible now and this is precisely
2180 // what we're trying to avoid
2181 if ( !IsVirtual() )
2182 {
2183 if ( line != (size_t)-1 )
2184 {
2185 GetLine(line)->GetItem( 0, le.m_item );
2186 }
2187 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2188 }
2189 //else: there may be no more such item
2190
2191 GetParent()->GetEventHandler()->ProcessEvent( le );
2192 }
2193
2194 void wxListMainWindow::ChangeCurrent(size_t current)
2195 {
2196 m_current = current;
2197
2198 // as the current item changed, we shouldn't start editing it when the
2199 // "slow click" timer expires as the click happened on another item
2200 if ( m_renameTimer->IsRunning() )
2201 m_renameTimer->Stop();
2202
2203 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2204 }
2205
2206 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2207 {
2208 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2209 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2210
2211 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2212 wxT("EditLabel() needs a text control") );
2213
2214 size_t itemEdit = (size_t)item;
2215
2216 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2217 le.SetEventObject( GetParent() );
2218 le.m_item.m_itemId =
2219 le.m_itemIndex = item;
2220 wxListLineData *data = GetLine(itemEdit);
2221 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
2222 data->GetItem( 0, le.m_item );
2223
2224 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2225 {
2226 // vetoed by user code
2227 return NULL;
2228 }
2229
2230 // We have to call this here because the label in question might just have
2231 // been added and no screen update taken place.
2232 if ( m_dirty )
2233 {
2234 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2235 // so that no pending events may change the item count (see below)
2236 // IMPORTANT: needs to be tested!
2237 wxSafeYield();
2238
2239 // Pending events dispatched by wxSafeYield might have changed the item
2240 // count
2241 if ( (size_t)item >= GetItemCount() )
2242 return NULL;
2243 }
2244
2245 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2246 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2247 return m_textctrlWrapper->GetText();
2248 }
2249
2250 void wxListMainWindow::OnRenameTimer()
2251 {
2252 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2253
2254 EditLabel( m_current );
2255 }
2256
2257 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2258 {
2259 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2260 le.SetEventObject( GetParent() );
2261 le.m_item.m_itemId =
2262 le.m_itemIndex = itemEdit;
2263
2264 wxListLineData *data = GetLine(itemEdit);
2265
2266 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
2267
2268 data->GetItem( 0, le.m_item );
2269 le.m_item.m_text = value;
2270 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2271 le.IsAllowed();
2272 }
2273
2274 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2275 {
2276 // let owner know that the edit was cancelled
2277 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2278
2279 le.SetEditCanceled(true);
2280
2281 le.SetEventObject( GetParent() );
2282 le.m_item.m_itemId =
2283 le.m_itemIndex = itemEdit;
2284
2285 wxListLineData *data = GetLine(itemEdit);
2286 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
2287
2288 data->GetItem( 0, le.m_item );
2289 GetEventHandler()->ProcessEvent( le );
2290 }
2291
2292 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2293 {
2294 #ifdef __WXMAC__
2295 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2296 // shutdown the edit control when the mouse is clicked elsewhere on the
2297 // listctrl because the order of events is different (or something like
2298 // that), so explicitly end the edit if it is active.
2299 if ( event.LeftDown() && m_textctrlWrapper )
2300 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
2301 #endif // __WXMAC__
2302
2303 if ( event.LeftDown() )
2304 SetFocus();
2305
2306 event.SetEventObject( GetParent() );
2307 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2308 return;
2309
2310 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2311 {
2312 // let the base class handle mouse wheel events.
2313 event.Skip();
2314 return;
2315 }
2316
2317 if ( !HasCurrent() || IsEmpty() )
2318 {
2319 if (event.RightDown())
2320 {
2321 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2322
2323 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2324 GetParent()->GetId(),
2325 ClientToScreen(event.GetPosition()));
2326 evtCtx.SetEventObject(GetParent());
2327 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2328 }
2329 return;
2330 }
2331
2332 if (m_dirty)
2333 return;
2334
2335 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2336 event.ButtonDClick()) )
2337 return;
2338
2339 int x = event.GetX();
2340 int y = event.GetY();
2341 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
2342
2343 // where did we hit it (if we did)?
2344 long hitResult = 0;
2345
2346 size_t count = GetItemCount(),
2347 current;
2348
2349 if ( InReportView() )
2350 {
2351 current = y / GetLineHeight();
2352 if ( current < count )
2353 hitResult = HitTestLine(current, x, y);
2354 }
2355 else // !report
2356 {
2357 // TODO: optimize it too! this is less simple than for report view but
2358 // enumerating all items is still not a way to do it!!
2359 for ( current = 0; current < count; current++ )
2360 {
2361 hitResult = HitTestLine(current, x, y);
2362 if ( hitResult )
2363 break;
2364 }
2365 }
2366
2367 // Update drag events counter first as we must do it even if the mouse is
2368 // not on any item right now as we must keep count in case we started
2369 // dragging from the empty control area but continued to do it over a valid
2370 // item -- in this situation we must not start dragging this item.
2371 if (event.Dragging())
2372 m_dragCount++;
2373 else
2374 m_dragCount = 0;
2375
2376 // The only mouse event that can be generated without any valid item is
2377 // wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
2378 // popup menu for the list control itself which should be shown even when
2379 // the user clicks outside of any item.
2380 if ( !hitResult )
2381 {
2382 // outside of any item
2383 if (event.RightDown())
2384 {
2385 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2386
2387 wxContextMenuEvent evtCtx(
2388 wxEVT_CONTEXT_MENU,
2389 GetParent()->GetId(),
2390 ClientToScreen(event.GetPosition()));
2391 evtCtx.SetEventObject(GetParent());
2392 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2393 }
2394 else
2395 {
2396 // reset the selection and bail out
2397 HighlightAll(false);
2398 }
2399
2400 return;
2401 }
2402
2403 if ( event.Dragging() )
2404 {
2405 if (m_dragCount == 1)
2406 {
2407 // we have to report the raw, physical coords as we want to be
2408 // able to call HitTest(event.m_pointDrag) from the user code to
2409 // get the item being dragged
2410 m_dragStart = event.GetPosition();
2411 }
2412
2413 if (m_dragCount != 3)
2414 return;
2415
2416 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2417 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
2418
2419 SendNotify( m_lineLastClicked, command, m_dragStart );
2420
2421 return;
2422 }
2423
2424 bool forceClick = false;
2425 if (event.ButtonDClick())
2426 {
2427 if ( m_renameTimer->IsRunning() )
2428 m_renameTimer->Stop();
2429
2430 m_lastOnSame = false;
2431
2432 if ( current == m_lineLastClicked )
2433 {
2434 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2435
2436 return;
2437 }
2438 else
2439 {
2440 // The first click was on another item, so don't interpret this as
2441 // a double click, but as a simple click instead
2442 forceClick = true;
2443 }
2444 }
2445
2446 if (event.LeftUp())
2447 {
2448 if (m_lineSelectSingleOnUp != (size_t)-1)
2449 {
2450 // select single line
2451 HighlightAll( false );
2452 ReverseHighlight(m_lineSelectSingleOnUp);
2453 }
2454
2455 if (m_lastOnSame)
2456 {
2457 if ((current == m_current) &&
2458 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2459 HasFlag(wxLC_EDIT_LABELS) )
2460 {
2461 if ( !InReportView() ||
2462 GetLineLabelRect(current).Contains(x, y) )
2463 {
2464 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2465 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
2466 }
2467 }
2468 }
2469
2470 m_lastOnSame = false;
2471 m_lineSelectSingleOnUp = (size_t)-1;
2472 }
2473 else
2474 {
2475 // This is necessary, because after a DnD operation in
2476 // from and to ourself, the up event is swallowed by the
2477 // DnD code. So on next non-up event (which means here and
2478 // now) m_lineSelectSingleOnUp should be reset.
2479 m_lineSelectSingleOnUp = (size_t)-1;
2480 }
2481 if (event.RightDown())
2482 {
2483 m_lineBeforeLastClicked = m_lineLastClicked;
2484 m_lineLastClicked = current;
2485
2486 // If the item is already selected, do not update the selection.
2487 // Multi-selections should not be cleared if a selected item is clicked.
2488 if (!IsHighlighted(current))
2489 {
2490 HighlightAll(false);
2491 ChangeCurrent(current);
2492 ReverseHighlight(m_current);
2493 }
2494
2495 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2496
2497 // Allow generation of context menu event
2498 event.Skip();
2499 }
2500 else if (event.MiddleDown())
2501 {
2502 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
2503 }
2504 else if ( event.LeftDown() || forceClick )
2505 {
2506 m_lineBeforeLastClicked = m_lineLastClicked;
2507 m_lineLastClicked = current;
2508
2509 size_t oldCurrent = m_current;
2510 bool oldWasSelected = IsHighlighted(m_current);
2511
2512 bool cmdModifierDown = event.CmdDown();
2513 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2514 {
2515 if ( IsSingleSel() || !IsHighlighted(current) )
2516 {
2517 HighlightAll( false );
2518
2519 ChangeCurrent(current);
2520
2521 ReverseHighlight(m_current);
2522 }
2523 else // multi sel & current is highlighted & no mod keys
2524 {
2525 m_lineSelectSingleOnUp = current;
2526 ChangeCurrent(current); // change focus
2527 }
2528 }
2529 else // multi sel & either ctrl or shift is down
2530 {
2531 if (cmdModifierDown)
2532 {
2533 ChangeCurrent(current);
2534
2535 ReverseHighlight(m_current);
2536 }
2537 else if (event.ShiftDown())
2538 {
2539 ChangeCurrent(current);
2540
2541 size_t lineFrom = oldCurrent,
2542 lineTo = current;
2543
2544 if ( lineTo < lineFrom )
2545 {
2546 lineTo = lineFrom;
2547 lineFrom = m_current;
2548 }
2549
2550 HighlightLines(lineFrom, lineTo);
2551 }
2552 else // !ctrl, !shift
2553 {
2554 // test in the enclosing if should make it impossible
2555 wxFAIL_MSG( wxT("how did we get here?") );
2556 }
2557 }
2558
2559 if (m_current != oldCurrent)
2560 RefreshLine( oldCurrent );
2561
2562 // forceClick is only set if the previous click was on another item
2563 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
2564 }
2565 }
2566
2567 void wxListMainWindow::MoveToItem(size_t item)
2568 {
2569 if ( item == (size_t)-1 )
2570 return;
2571
2572 wxRect rect = GetLineRect(item);
2573
2574 int client_w, client_h;
2575 GetClientSize( &client_w, &client_h );
2576
2577 const int hLine = GetLineHeight();
2578
2579 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2580 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
2581
2582 if ( InReportView() )
2583 {
2584 // the next we need the range of lines shown it might be different,
2585 // so recalculate it
2586 ResetVisibleLinesRange();
2587
2588 if (rect.y < view_y)
2589 GetListCtrl()->Scroll( -1, rect.y / hLine );
2590 if (rect.y + rect.height + 5 > view_y + client_h)
2591 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
2592
2593 #ifdef __WXMAC__
2594 // At least on Mac the visible lines value will get reset inside of
2595 // Scroll *before* it actually scrolls the window because of the
2596 // Update() that happens there, so it will still have the wrong value.
2597 // So let's reset it again and wait for it to be recalculated in the
2598 // next paint event. I would expect this problem to show up in wxGTK
2599 // too but couldn't duplicate it there. Perhaps the order of events
2600 // is different... --Robin
2601 ResetVisibleLinesRange();
2602 #endif
2603 }
2604 else // !report
2605 {
2606 int sx = -1,
2607 sy = -1;
2608
2609 if (rect.x-view_x < 5)
2610 sx = (rect.x - 5) / SCROLL_UNIT_X;
2611 if (rect.x + rect.width - 5 > view_x + client_w)
2612 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2613
2614 if (rect.y-view_y < 5)
2615 sy = (rect.y - 5) / hLine;
2616 if (rect.y + rect.height - 5 > view_y + client_h)
2617 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2618
2619 GetListCtrl()->Scroll(sx, sy);
2620 }
2621 }
2622
2623 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2624 {
2625 if ( !InReportView() )
2626 {
2627 // TODO: this should work in all views but is not implemented now
2628 return false;
2629 }
2630
2631 size_t top, bottom;
2632 GetVisibleLinesRange(&top, &bottom);
2633
2634 if ( bottom == (size_t)-1 )
2635 return 0;
2636
2637 ResetVisibleLinesRange();
2638
2639 int hLine = GetLineHeight();
2640
2641 GetListCtrl()->Scroll(-1, top + dy / hLine);
2642
2643 #ifdef __WXMAC__
2644 // see comment in MoveToItem() for why we do this
2645 ResetVisibleLinesRange();
2646 #endif
2647
2648 return true;
2649 }
2650
2651 // ----------------------------------------------------------------------------
2652 // keyboard handling
2653 // ----------------------------------------------------------------------------
2654
2655 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
2656 {
2657 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
2658 wxT("invalid item index in OnArrowChar()") );
2659
2660 size_t oldCurrent = m_current;
2661
2662 // in single selection we just ignore Shift as we can't select several
2663 // items anyhow
2664 if ( event.ShiftDown() && !IsSingleSel() )
2665 {
2666 ChangeCurrent(newCurrent);
2667
2668 // refresh the old focus to remove it
2669 RefreshLine( oldCurrent );
2670
2671 // select all the items between the old and the new one
2672 if ( oldCurrent > newCurrent )
2673 {
2674 newCurrent = oldCurrent;
2675 oldCurrent = m_current;
2676 }
2677
2678 HighlightLines(oldCurrent, newCurrent);
2679 }
2680 else // !shift
2681 {
2682 // all previously selected items are unselected unless ctrl is held
2683 // in a multiselection control
2684 if ( !event.ControlDown() || IsSingleSel() )
2685 HighlightAll(false);
2686
2687 ChangeCurrent(newCurrent);
2688
2689 // refresh the old focus to remove it
2690 RefreshLine( oldCurrent );
2691
2692 // in single selection mode we must always have a selected item
2693 if ( !event.ControlDown() || IsSingleSel() )
2694 HighlightLine( m_current, true );
2695 }
2696
2697 RefreshLine( m_current );
2698
2699 MoveToFocus();
2700 }
2701
2702 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2703 {
2704 wxWindow *parent = GetParent();
2705
2706 // propagate the key event upwards
2707 wxKeyEvent ke(event);
2708 ke.SetEventObject( parent );
2709 if (parent->GetEventHandler()->ProcessEvent( ke ))
2710 return;
2711
2712 // send a list event
2713 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, parent->GetId() );
2714 le.m_item.m_itemId =
2715 le.m_itemIndex = m_current;
2716 if (HasCurrent())
2717 GetLine(m_current)->GetItem( 0, le.m_item );
2718 le.m_code = event.GetKeyCode();
2719 le.SetEventObject( parent );
2720 if (parent->GetEventHandler()->ProcessEvent( le ))
2721 return;
2722
2723 event.Skip();
2724 }
2725
2726 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2727 {
2728 wxWindow *parent = GetParent();
2729
2730 // propagate the key event upwards
2731 wxKeyEvent ke(event);
2732 if (parent->GetEventHandler()->ProcessEvent( ke ))
2733 return;
2734
2735 event.Skip();
2736 }
2737
2738 void wxListMainWindow::OnCharHook( wxKeyEvent &event )
2739 {
2740 if ( m_textctrlWrapper )
2741 {
2742 // When an in-place editor is active we should ensure that it always
2743 // gets the key events that are special to it.
2744 if ( m_textctrlWrapper->CheckForEndEditKey(event) )
2745 {
2746 // Skip the call to wxEvent::Skip() below.
2747 return;
2748 }
2749 }
2750
2751 event.Skip();
2752 }
2753
2754 void wxListMainWindow::OnChar( wxKeyEvent &event )
2755 {
2756 wxWindow *parent = GetParent();
2757
2758 // propagate the char event upwards
2759 wxKeyEvent ke(event);
2760 ke.SetEventObject( parent );
2761 if (parent->GetEventHandler()->ProcessEvent( ke ))
2762 return;
2763
2764 if ( HandleAsNavigationKey(event) )
2765 return;
2766
2767 // no item -> nothing to do
2768 if (!HasCurrent())
2769 {
2770 event.Skip();
2771 return;
2772 }
2773
2774 // don't use m_linesPerPage directly as it might not be computed yet
2775 const int pageSize = GetCountPerPage();
2776 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
2777
2778 if (GetLayoutDirection() == wxLayout_RightToLeft)
2779 {
2780 if (event.GetKeyCode() == WXK_RIGHT)
2781 event.m_keyCode = WXK_LEFT;
2782 else if (event.GetKeyCode() == WXK_LEFT)
2783 event.m_keyCode = WXK_RIGHT;
2784 }
2785
2786 switch ( event.GetKeyCode() )
2787 {
2788 case WXK_UP:
2789 if ( m_current > 0 )
2790 OnArrowChar( m_current - 1, event );
2791 break;
2792
2793 case WXK_DOWN:
2794 if ( m_current < (size_t)GetItemCount() - 1 )
2795 OnArrowChar( m_current + 1, event );
2796 break;
2797
2798 case WXK_END:
2799 if (!IsEmpty())
2800 OnArrowChar( GetItemCount() - 1, event );
2801 break;
2802
2803 case WXK_HOME:
2804 if (!IsEmpty())
2805 OnArrowChar( 0, event );
2806 break;
2807
2808 case WXK_PAGEUP:
2809 {
2810 int steps = InReportView() ? pageSize - 1
2811 : m_current % pageSize;
2812
2813 int index = m_current - steps;
2814 if (index < 0)
2815 index = 0;
2816
2817 OnArrowChar( index, event );
2818 }
2819 break;
2820
2821 case WXK_PAGEDOWN:
2822 {
2823 int steps = InReportView()
2824 ? pageSize - 1
2825 : pageSize - (m_current % pageSize) - 1;
2826
2827 size_t index = m_current + steps;
2828 size_t count = GetItemCount();
2829 if ( index >= count )
2830 index = count - 1;
2831
2832 OnArrowChar( index, event );
2833 }
2834 break;
2835
2836 case WXK_LEFT:
2837 if ( !InReportView() )
2838 {
2839 int index = m_current - pageSize;
2840 if (index < 0)
2841 index = 0;
2842
2843 OnArrowChar( index, event );
2844 }
2845 break;
2846
2847 case WXK_RIGHT:
2848 if ( !InReportView() )
2849 {
2850 size_t index = m_current + pageSize;
2851
2852 size_t count = GetItemCount();
2853 if ( index >= count )
2854 index = count - 1;
2855
2856 OnArrowChar( index, event );
2857 }
2858 break;
2859
2860 case WXK_SPACE:
2861 if ( IsSingleSel() )
2862 {
2863 if ( event.ControlDown() )
2864 {
2865 ReverseHighlight(m_current);
2866 }
2867 else // normal space press
2868 {
2869 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2870 }
2871 }
2872 else // multiple selection
2873 {
2874 ReverseHighlight(m_current);
2875 }
2876 break;
2877
2878 case WXK_RETURN:
2879 case WXK_EXECUTE:
2880 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2881 break;
2882
2883 default:
2884 event.Skip();
2885 }
2886 }
2887
2888 // ----------------------------------------------------------------------------
2889 // focus handling
2890 // ----------------------------------------------------------------------------
2891
2892 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2893 {
2894 if ( GetParent() )
2895 {
2896 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
2897 event.SetEventObject( GetParent() );
2898 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2899 return;
2900 }
2901
2902 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2903 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2904 // which are already drawn correctly resulting in horrible flicker - avoid
2905 // it
2906 if ( !m_hasFocus )
2907 {
2908 m_hasFocus = true;
2909
2910 RefreshSelected();
2911 }
2912 }
2913
2914 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
2915 {
2916 if ( GetParent() )
2917 {
2918 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
2919 event.SetEventObject( GetParent() );
2920 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2921 return;
2922 }
2923
2924 m_hasFocus = false;
2925 RefreshSelected();
2926 }
2927
2928 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
2929 {
2930 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
2931 {
2932 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2933 }
2934 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
2935 {
2936 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2937 }
2938 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
2939 {
2940 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2941 }
2942 else if ( InReportView() && (m_small_image_list))
2943 {
2944 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2945 }
2946 }
2947
2948 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
2949 {
2950 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
2951 {
2952 m_normal_image_list->GetSize( index, width, height );
2953 }
2954 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
2955 {
2956 m_small_image_list->GetSize( index, width, height );
2957 }
2958 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
2959 {
2960 m_small_image_list->GetSize( index, width, height );
2961 }
2962 else if ( InReportView() && m_small_image_list )
2963 {
2964 m_small_image_list->GetSize( index, width, height );
2965 }
2966 else
2967 {
2968 width =
2969 height = 0;
2970 }
2971 }
2972
2973 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
2974 {
2975 m_dirty = true;
2976
2977 // calc the spacing from the icon size
2978 int width = 0, height = 0;
2979
2980 if ((imageList) && (imageList->GetImageCount()) )
2981 imageList->GetSize(0, width, height);
2982
2983 if (which == wxIMAGE_LIST_NORMAL)
2984 {
2985 m_normal_image_list = imageList;
2986 m_normal_spacing = width + 8;
2987 }
2988
2989 if (which == wxIMAGE_LIST_SMALL)
2990 {
2991 m_small_image_list = imageList;
2992 m_small_spacing = width + 14;
2993 m_lineHeight = 0; // ensure that the line height will be recalc'd
2994 }
2995 }
2996
2997 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
2998 {
2999 m_dirty = true;
3000 if (isSmall)
3001 m_small_spacing = spacing;
3002 else
3003 m_normal_spacing = spacing;
3004 }
3005
3006 int wxListMainWindow::GetItemSpacing( bool isSmall )
3007 {
3008 return isSmall ? m_small_spacing : m_normal_spacing;
3009 }
3010
3011 // ----------------------------------------------------------------------------
3012 // columns
3013 // ----------------------------------------------------------------------------
3014
3015 int
3016 wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
3017 {
3018 wxClientDC dc(const_cast<wxListMainWindow*>(this));
3019
3020 int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
3021
3022 width += 2*EXTRA_WIDTH;
3023
3024 // check for column header's image availability
3025 const int image = column->GetImage();
3026 if ( image != -1 )
3027 {
3028 if ( m_small_image_list )
3029 {
3030 int ix = 0, iy = 0;
3031 m_small_image_list->GetSize(image, ix, iy);
3032 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3033 }
3034 }
3035
3036 return width;
3037 }
3038
3039 void wxListMainWindow::SetColumn( int col, const wxListItem &item )
3040 {
3041 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3042
3043 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
3044
3045 wxListHeaderData *column = node->GetData();
3046 column->SetItem( item );
3047
3048 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3049 column->SetWidth(ComputeMinHeaderWidth(column));
3050
3051 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3052 if ( headerWin )
3053 headerWin->m_dirty = true;
3054
3055 m_dirty = true;
3056
3057 // invalidate it as it has to be recalculated
3058 m_headerWidth = 0;
3059 }
3060
3061 void wxListMainWindow::SetColumnWidth( int col, int width )
3062 {
3063 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3064 wxT("invalid column index") );
3065
3066 wxCHECK_RET( InReportView(),
3067 wxT("SetColumnWidth() can only be called in report mode.") );
3068
3069 m_dirty = true;
3070
3071 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3072 if ( headerWin )
3073 headerWin->m_dirty = true;
3074
3075 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3076 wxCHECK_RET( node, wxT("no column?") );
3077
3078 wxListHeaderData *column = node->GetData();
3079
3080 size_t count = GetItemCount();
3081
3082 if (width == wxLIST_AUTOSIZE_USEHEADER)
3083 {
3084 width = ComputeMinHeaderWidth(column);
3085 }
3086 else if ( width == wxLIST_AUTOSIZE )
3087 {
3088 width = ComputeMinHeaderWidth(column);
3089
3090 if ( !IsVirtual() )
3091 {
3092 wxClientDC dc(this);
3093 dc.SetFont( GetFont() );
3094
3095 int max = AUTOSIZE_COL_MARGIN;
3096
3097 // if the cached column width isn't valid then recalculate it
3098 if (m_aColWidths.Item(col)->bNeedsUpdate)
3099 {
3100 for (size_t i = 0; i < count; i++)
3101 {
3102 wxListLineData *line = GetLine( i );
3103 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3104
3105 wxCHECK_RET( n, wxT("no subitem?") );
3106
3107 wxListItemData *itemData = n->GetData();
3108 wxListItem item;
3109
3110 itemData->GetItem(item);
3111 int itemWidth = GetItemWidthWithImage(&item);
3112 if (itemWidth > max)
3113 max = itemWidth;
3114 }
3115
3116 m_aColWidths.Item(col)->bNeedsUpdate = false;
3117 m_aColWidths.Item(col)->nMaxWidth = max;
3118 }
3119
3120 max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
3121 if ( width < max )
3122 width = max;
3123 }
3124 }
3125
3126 column->SetWidth( width );
3127
3128 // invalidate it as it has to be recalculated
3129 m_headerWidth = 0;
3130 }
3131
3132 int wxListMainWindow::GetHeaderWidth() const
3133 {
3134 if ( !m_headerWidth )
3135 {
3136 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3137
3138 size_t count = GetColumnCount();
3139 for ( size_t col = 0; col < count; col++ )
3140 {
3141 self->m_headerWidth += GetColumnWidth(col);
3142 }
3143 }
3144
3145 return m_headerWidth;
3146 }
3147
3148 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3149 {
3150 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3151 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
3152
3153 wxListHeaderData *column = node->GetData();
3154 column->GetItem( item );
3155 }
3156
3157 int wxListMainWindow::GetColumnWidth( int col ) const
3158 {
3159 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3160 wxCHECK_MSG( node, 0, wxT("invalid column index") );
3161
3162 wxListHeaderData *column = node->GetData();
3163 return column->GetWidth();
3164 }
3165
3166 // ----------------------------------------------------------------------------
3167 // item state
3168 // ----------------------------------------------------------------------------
3169
3170 void wxListMainWindow::SetItem( wxListItem &item )
3171 {
3172 long id = item.m_itemId;
3173 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3174 wxT("invalid item index in SetItem") );
3175
3176 if ( !IsVirtual() )
3177 {
3178 wxListLineData *line = GetLine((size_t)id);
3179 line->SetItem( item.m_col, item );
3180
3181 // Set item state if user wants
3182 if ( item.m_mask & wxLIST_MASK_STATE )
3183 SetItemState( item.m_itemId, item.m_state, item.m_state );
3184
3185 if (InReportView())
3186 {
3187 // update the Max Width Cache if needed
3188 int width = GetItemWidthWithImage(&item);
3189
3190 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3191 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3192 }
3193 }
3194
3195 // update the item on screen unless we're going to update everything soon
3196 // anyhow
3197 if ( !m_dirty )
3198 {
3199 wxRect rectItem;
3200 GetItemRect(id, rectItem);
3201 RefreshRect(rectItem);
3202 }
3203 }
3204
3205 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3206 {
3207 if ( IsEmpty() )
3208 return;
3209
3210 // first deal with selection
3211 if ( stateMask & wxLIST_STATE_SELECTED )
3212 {
3213 // set/clear select state
3214 if ( IsVirtual() )
3215 {
3216 // optimized version for virtual listctrl.
3217 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3218 Refresh();
3219 }
3220 else if ( state & wxLIST_STATE_SELECTED )
3221 {
3222 const long count = GetItemCount();
3223 for( long i = 0; i < count; i++ )
3224 {
3225 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3226 }
3227
3228 }
3229 else
3230 {
3231 // clear for non virtual (somewhat optimized by using GetNextItem())
3232 long i = -1;
3233 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3234 {
3235 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3236 }
3237 }
3238 }
3239
3240 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3241 {
3242 // unfocus all: only one item can be focussed, so clearing focus for
3243 // all items is simply clearing focus of the focussed item.
3244 SetItemState(m_current, state, stateMask);
3245 }
3246 //(setting focus to all items makes no sense, so it is not handled here.)
3247 }
3248
3249 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3250 {
3251 if ( litem == -1 )
3252 {
3253 SetItemStateAll(state, stateMask);
3254 return;
3255 }
3256
3257 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3258 wxT("invalid list ctrl item index in SetItem") );
3259
3260 size_t oldCurrent = m_current;
3261 size_t item = (size_t)litem; // safe because of the check above
3262
3263 // do we need to change the focus?
3264 if ( stateMask & wxLIST_STATE_FOCUSED )
3265 {
3266 if ( state & wxLIST_STATE_FOCUSED )
3267 {
3268 // don't do anything if this item is already focused
3269 if ( item != m_current )
3270 {
3271 ChangeCurrent(item);
3272
3273 if ( oldCurrent != (size_t)-1 )
3274 {
3275 if ( IsSingleSel() )
3276 {
3277 HighlightLine(oldCurrent, false);
3278 }
3279
3280 RefreshLine(oldCurrent);
3281 }
3282
3283 RefreshLine( m_current );
3284 }
3285 }
3286 else // unfocus
3287 {
3288 // don't do anything if this item is not focused
3289 if ( item == m_current )
3290 {
3291 ResetCurrent();
3292
3293 if ( IsSingleSel() )
3294 {
3295 // we must unselect the old current item as well or we
3296 // might end up with more than one selected item in a
3297 // single selection control
3298 HighlightLine(oldCurrent, false);
3299 }
3300
3301 RefreshLine( oldCurrent );
3302 }
3303 }
3304 }
3305
3306 // do we need to change the selection state?
3307 if ( stateMask & wxLIST_STATE_SELECTED )
3308 {
3309 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3310
3311 if ( IsSingleSel() )
3312 {
3313 if ( on )
3314 {
3315 // selecting the item also makes it the focused one in the
3316 // single sel mode
3317 if ( m_current != item )
3318 {
3319 ChangeCurrent(item);
3320
3321 if ( oldCurrent != (size_t)-1 )
3322 {
3323 HighlightLine( oldCurrent, false );
3324 RefreshLine( oldCurrent );
3325 }
3326 }
3327 }
3328 else // off
3329 {
3330 // only the current item may be selected anyhow
3331 if ( item != m_current )
3332 return;
3333 }
3334 }
3335
3336 if ( HighlightLine(item, on) )
3337 {
3338 RefreshLine(item);
3339 }
3340 }
3341 }
3342
3343 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3344 {
3345 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3346 wxT("invalid list ctrl item index in GetItemState()") );
3347
3348 int ret = wxLIST_STATE_DONTCARE;
3349
3350 if ( stateMask & wxLIST_STATE_FOCUSED )
3351 {
3352 if ( (size_t)item == m_current )
3353 ret |= wxLIST_STATE_FOCUSED;
3354 }
3355
3356 if ( stateMask & wxLIST_STATE_SELECTED )
3357 {
3358 if ( IsHighlighted(item) )
3359 ret |= wxLIST_STATE_SELECTED;
3360 }
3361
3362 return ret;
3363 }
3364
3365 void wxListMainWindow::GetItem( wxListItem &item ) const
3366 {
3367 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3368 wxT("invalid item index in GetItem") );
3369
3370 wxListLineData *line = GetLine((size_t)item.m_itemId);
3371 line->GetItem( item.m_col, item );
3372
3373 // Get item state if user wants it
3374 if ( item.m_mask & wxLIST_MASK_STATE )
3375 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3376 wxLIST_STATE_FOCUSED );
3377 }
3378
3379 // ----------------------------------------------------------------------------
3380 // item count
3381 // ----------------------------------------------------------------------------
3382
3383 size_t wxListMainWindow::GetItemCount() const
3384 {
3385 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3386 }
3387
3388 void wxListMainWindow::SetItemCount(long count)
3389 {
3390 m_selStore.SetItemCount(count);
3391 m_countVirt = count;
3392
3393 ResetVisibleLinesRange();
3394
3395 // scrollbars must be reset
3396 m_dirty = true;
3397 }
3398
3399 int wxListMainWindow::GetSelectedItemCount() const
3400 {
3401 // deal with the quick case first
3402 if ( IsSingleSel() )
3403 return HasCurrent() ? IsHighlighted(m_current) : false;
3404
3405 // virtual controls remmebers all its selections itself
3406 if ( IsVirtual() )
3407 return m_selStore.GetSelectedCount();
3408
3409 // TODO: we probably should maintain the number of items selected even for
3410 // non virtual controls as enumerating all lines is really slow...
3411 size_t countSel = 0;
3412 size_t count = GetItemCount();
3413 for ( size_t line = 0; line < count; line++ )
3414 {
3415 if ( GetLine(line)->IsHighlighted() )
3416 countSel++;
3417 }
3418
3419 return countSel;
3420 }
3421
3422 // ----------------------------------------------------------------------------
3423 // item position/size
3424 // ----------------------------------------------------------------------------
3425
3426 wxRect wxListMainWindow::GetViewRect() const
3427 {
3428 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
3429
3430 // we need to find the longest/tallest label
3431 wxCoord xMax = 0, yMax = 0;
3432 const int count = GetItemCount();
3433 if ( count )
3434 {
3435 for ( int i = 0; i < count; i++ )
3436 {
3437 // we need logical, not physical, coordinates here, so use
3438 // GetLineRect() instead of GetItemRect()
3439 wxRect r = GetLineRect(i);
3440
3441 wxCoord x = r.GetRight(),
3442 y = r.GetBottom();
3443
3444 if ( x > xMax )
3445 xMax = x;
3446 if ( y > yMax )
3447 yMax = y;
3448 }
3449 }
3450
3451 // some fudge needed to make it look prettier
3452 xMax += 2 * EXTRA_BORDER_X;
3453 yMax += 2 * EXTRA_BORDER_Y;
3454
3455 // account for the scrollbars if necessary
3456 const wxSize sizeAll = GetClientSize();
3457 if ( xMax > sizeAll.x )
3458 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3459 if ( yMax > sizeAll.y )
3460 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3461
3462 return wxRect(0, 0, xMax, yMax);
3463 }
3464
3465 bool
3466 wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
3467 {
3468 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3469 false,
3470 wxT("GetSubItemRect only meaningful in report view") );
3471 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
3472 wxT("invalid item in GetSubItemRect") );
3473
3474 // ensure that we're laid out, otherwise we could return nonsense
3475 if ( m_dirty )
3476 {
3477 wxConstCast(this, wxListMainWindow)->
3478 RecalculatePositions(true /* no refresh */);
3479 }
3480
3481 rect = GetLineRect((size_t)item);
3482
3483 // Adjust rect to specified column
3484 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3485 {
3486 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
3487 wxT("invalid subItem in GetSubItemRect") );
3488
3489 for (int i = 0; i < subItem; i++)
3490 {
3491 rect.x += GetColumnWidth(i);
3492 }
3493 rect.width = GetColumnWidth(subItem);
3494 }
3495
3496 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
3497
3498 return true;
3499 }
3500
3501 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
3502 {
3503 wxRect rect;
3504 GetItemRect(item, rect);
3505
3506 pos.x = rect.x;
3507 pos.y = rect.y;
3508
3509 return true;
3510 }
3511
3512 // ----------------------------------------------------------------------------
3513 // geometry calculation
3514 // ----------------------------------------------------------------------------
3515
3516 void wxListMainWindow::RecalculatePositions(bool noRefresh)
3517 {
3518 const int lineHeight = GetLineHeight();
3519
3520 wxClientDC dc( this );
3521 dc.SetFont( GetFont() );
3522
3523 const size_t count = GetItemCount();
3524
3525 int iconSpacing;
3526 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3527 iconSpacing = m_normal_spacing;
3528 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3529 iconSpacing = m_small_spacing;
3530 else
3531 iconSpacing = 0;
3532
3533 // Note that we do not call GetClientSize() here but
3534 // GetSize() and subtract the border size for sunken
3535 // borders manually. This is technically incorrect,
3536 // but we need to know the client area's size WITHOUT
3537 // scrollbars here. Since we don't know if there are
3538 // any scrollbars, we use GetSize() instead. Another
3539 // solution would be to call SetScrollbars() here to
3540 // remove the scrollbars and call GetClientSize() then,
3541 // but this might result in flicker and - worse - will
3542 // reset the scrollbars to 0 which is not good at all
3543 // if you resize a dialog/window, but don't want to
3544 // reset the window scrolling. RR.
3545 // Furthermore, we actually do NOT subtract the border
3546 // width as 2 pixels is just the extra space which we
3547 // need around the actual content in the window. Other-
3548 // wise the text would e.g. touch the upper border. RR.
3549 int clientWidth,
3550 clientHeight;
3551 GetSize( &clientWidth, &clientHeight );
3552
3553 if ( InReportView() )
3554 {
3555 // all lines have the same height and we scroll one line per step
3556 int entireHeight = count * lineHeight + LINE_SPACING;
3557
3558 m_linesPerPage = clientHeight / lineHeight;
3559
3560 ResetVisibleLinesRange();
3561
3562 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
3563 GetHeaderWidth() / SCROLL_UNIT_X,
3564 (entireHeight + lineHeight - 1) / lineHeight,
3565 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3566 GetListCtrl()->GetScrollPos(wxVERTICAL),
3567 true );
3568 }
3569 else // !report
3570 {
3571 // we have 3 different layout strategies: either layout all items
3572 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3573 // to arrange them in top to bottom, left to right (don't ask me why
3574 // not the other way round...) order
3575 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
3576 {
3577 int x = EXTRA_BORDER_X;
3578 int y = EXTRA_BORDER_Y;
3579
3580 wxCoord widthMax = 0;
3581
3582 size_t i;
3583 for ( i = 0; i < count; i++ )
3584 {
3585 wxListLineData *line = GetLine(i);
3586 line->CalculateSize( &dc, iconSpacing );
3587 line->SetPosition( x, y, iconSpacing );
3588
3589 wxSize sizeLine = GetLineSize(i);
3590
3591 if ( HasFlag(wxLC_ALIGN_TOP) )
3592 {
3593 if ( sizeLine.x > widthMax )
3594 widthMax = sizeLine.x;
3595
3596 y += sizeLine.y;
3597 }
3598 else // wxLC_ALIGN_LEFT
3599 {
3600 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3601 }
3602 }
3603
3604 if ( HasFlag(wxLC_ALIGN_TOP) )
3605 {
3606 // traverse the items again and tweak their sizes so that they are
3607 // all the same in a row
3608 for ( i = 0; i < count; i++ )
3609 {
3610 wxListLineData *line = GetLine(i);
3611 line->m_gi->ExtendWidth(widthMax);
3612 }
3613 }
3614
3615 GetListCtrl()->SetScrollbars
3616 (
3617 SCROLL_UNIT_X,
3618 lineHeight,
3619 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3620 (y + lineHeight) / lineHeight,
3621 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3622 GetListCtrl()->GetScrollPos( wxVERTICAL ),
3623 true
3624 );
3625 }
3626 else // "flowed" arrangement, the most complicated case
3627 {
3628 // at first we try without any scrollbars, if the items don't fit into
3629 // the window, we recalculate after subtracting the space taken by the
3630 // scrollbar
3631
3632 int entireWidth = 0;
3633
3634 for (int tries = 0; tries < 2; tries++)
3635 {
3636 entireWidth = 2 * EXTRA_BORDER_X;
3637
3638 if (tries == 1)
3639 {
3640 // Now we have decided that the items do not fit into the
3641 // client area, so we need a scrollbar
3642 entireWidth += SCROLL_UNIT_X;
3643 }
3644
3645 int x = EXTRA_BORDER_X;
3646 int y = EXTRA_BORDER_Y;
3647
3648 // Note that "row" here is vertical, i.e. what is called
3649 // "column" in many other places in wxWidgets.
3650 int maxWidthInThisRow = 0;
3651
3652 m_linesPerPage = 0;
3653 int currentlyVisibleLines = 0;
3654
3655 for (size_t i = 0; i < count; i++)
3656 {
3657 currentlyVisibleLines++;
3658 wxListLineData *line = GetLine( i );
3659 line->CalculateSize( &dc, iconSpacing );
3660 line->SetPosition( x, y, iconSpacing );
3661
3662 wxSize sizeLine = GetLineSize( i );
3663
3664 if ( maxWidthInThisRow < sizeLine.x )
3665 maxWidthInThisRow = sizeLine.x;
3666
3667 y += sizeLine.y;
3668 if (currentlyVisibleLines > m_linesPerPage)
3669 m_linesPerPage = currentlyVisibleLines;
3670
3671 // Have we reached the end of the row either because no
3672 // more items would fit or because there are simply no more
3673 // items?
3674 if ( y + sizeLine.y >= clientHeight
3675 || i == count - 1)
3676 {
3677 // Adjust all items in this row to have the same
3678 // width to ensure that they all align horizontally in
3679 // icon view.
3680 if ( HasFlag(wxLC_ICON) || HasFlag(wxLC_SMALL_ICON) )
3681 {
3682 size_t firstRowLine = i - currentlyVisibleLines + 1;
3683 for (size_t j = firstRowLine; j <= i; j++)
3684 {
3685 GetLine(j)->m_gi->ExtendWidth(maxWidthInThisRow);
3686 }
3687 }
3688
3689 currentlyVisibleLines = 0;
3690 y = EXTRA_BORDER_Y;
3691 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3692 x += maxWidthInThisRow;
3693 entireWidth += maxWidthInThisRow;
3694 maxWidthInThisRow = 0;
3695 }
3696
3697 if ( (tries == 0) &&
3698 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3699 {
3700 clientHeight -= wxSystemSettings::
3701 GetMetric(wxSYS_HSCROLL_Y);
3702 m_linesPerPage = 0;
3703 break;
3704 }
3705
3706 if ( i == count - 1 )
3707 tries = 1; // Everything fits, no second try required.
3708 }
3709 }
3710
3711 GetListCtrl()->SetScrollbars
3712 (
3713 SCROLL_UNIT_X,
3714 lineHeight,
3715 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3716 0,
3717 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3718 0,
3719 true
3720 );
3721 }
3722 }
3723
3724 if ( !noRefresh )
3725 {
3726 // FIXME: why should we call it from here?
3727 UpdateCurrent();
3728
3729 RefreshAll();
3730 }
3731 }
3732
3733 void wxListMainWindow::RefreshAll()
3734 {
3735 m_dirty = false;
3736 Refresh();
3737
3738 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3739 if ( headerWin && headerWin->m_dirty )
3740 {
3741 headerWin->m_dirty = false;
3742 headerWin->Refresh();
3743 }
3744 }
3745
3746 void wxListMainWindow::UpdateCurrent()
3747 {
3748 if ( !HasCurrent() && !IsEmpty() )
3749 ChangeCurrent(0);
3750 }
3751
3752 long wxListMainWindow::GetNextItem( long item,
3753 int WXUNUSED(geometry),
3754 int state ) const
3755 {
3756 long ret = item,
3757 max = GetItemCount();
3758 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
3759 wxT("invalid listctrl index in GetNextItem()") );
3760
3761 // notice that we start with the next item (or the first one if item == -1)
3762 // and this is intentional to allow writing a simple loop to iterate over
3763 // all selected items
3764 ret++;
3765 if ( ret == max )
3766 // this is not an error because the index was OK initially,
3767 // just no such item
3768 return -1;
3769
3770 if ( !state )
3771 // any will do
3772 return (size_t)ret;
3773
3774 size_t count = GetItemCount();
3775 for ( size_t line = (size_t)ret; line < count; line++ )
3776 {
3777 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3778 return line;
3779
3780 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
3781 return line;
3782 }
3783
3784 return -1;
3785 }
3786
3787 // ----------------------------------------------------------------------------
3788 // deleting stuff
3789 // ----------------------------------------------------------------------------
3790
3791 void wxListMainWindow::DeleteItem( long lindex )
3792 {
3793 size_t count = GetItemCount();
3794
3795 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
3796 wxT("invalid item index in DeleteItem") );
3797
3798 size_t index = (size_t)lindex;
3799
3800 // we don't need to adjust the index for the previous items
3801 if ( HasCurrent() && m_current >= index )
3802 {
3803 // if the current item is being deleted, we want the next one to
3804 // become selected - unless there is no next one - so don't adjust
3805 // m_current in this case
3806 if ( m_current != index || m_current == count - 1 )
3807 m_current--;
3808 }
3809
3810 if ( InReportView() )
3811 {
3812 // mark the Column Max Width cache as dirty if the items in the line
3813 // we're deleting contain the Max Column Width
3814 wxListLineData * const line = GetLine(index);
3815 wxListItemDataList::compatibility_iterator n;
3816 wxListItemData *itemData;
3817 wxListItem item;
3818 int itemWidth;
3819
3820 for (size_t i = 0; i < m_columns.GetCount(); i++)
3821 {
3822 n = line->m_items.Item( i );
3823 itemData = n->GetData();
3824 itemData->GetItem(item);
3825
3826 itemWidth = GetItemWidthWithImage(&item);
3827
3828 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3829 m_aColWidths.Item(i)->bNeedsUpdate = true;
3830 }
3831
3832 ResetVisibleLinesRange();
3833 }
3834
3835 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
3836
3837 if ( IsVirtual() )
3838 {
3839 m_countVirt--;
3840 m_selStore.OnItemDelete(index);
3841 }
3842 else
3843 {
3844 m_lines.RemoveAt( index );
3845 }
3846
3847 // we need to refresh the (vert) scrollbar as the number of items changed
3848 m_dirty = true;
3849
3850 RefreshAfter(index);
3851 }
3852
3853 void wxListMainWindow::DeleteColumn( int col )
3854 {
3855 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3856
3857 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
3858
3859 m_dirty = true;
3860 delete node->GetData();
3861 m_columns.Erase( node );
3862
3863 if ( !IsVirtual() )
3864 {
3865 // update all the items
3866 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3867 {
3868 wxListLineData * const line = GetLine(i);
3869
3870 // In the following atypical but possible scenario it can be
3871 // legal to call DeleteColumn() but the items may not have any
3872 // values for it:
3873 // 1. In report view, insert a second column.
3874 // 2. Still in report view, add an item with 2 values.
3875 // 3. Switch to an icon (or list) view.
3876 // 4. Add an item -- necessarily with 1 value only.
3877 // 5. Switch back to report view.
3878 // 6. Call DeleteColumn().
3879 // So we need to check for this as otherwise we would simply crash
3880 // if this happens.
3881 if ( line->m_items.GetCount() <= static_cast<unsigned>(col) )
3882 continue;
3883
3884 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3885 delete n->GetData();
3886 line->m_items.Erase(n);
3887 }
3888 }
3889
3890 if ( InReportView() ) // we only cache max widths when in Report View
3891 {
3892 delete m_aColWidths.Item(col);
3893 m_aColWidths.RemoveAt(col);
3894 }
3895
3896 // invalidate it as it has to be recalculated
3897 m_headerWidth = 0;
3898 }
3899
3900 void wxListMainWindow::DoDeleteAllItems()
3901 {
3902 if ( IsEmpty() )
3903 // nothing to do - in particular, don't send the event
3904 return;
3905
3906 ResetCurrent();
3907
3908 // to make the deletion of all items faster, we don't send the
3909 // notifications for each item deletion in this case but only one event
3910 // for all of them: this is compatible with wxMSW and documented in
3911 // DeleteAllItems() description
3912
3913 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
3914 event.SetEventObject( GetParent() );
3915 GetParent()->GetEventHandler()->ProcessEvent( event );
3916
3917 if ( IsVirtual() )
3918 {
3919 m_countVirt = 0;
3920 m_selStore.Clear();
3921 }
3922
3923 if ( InReportView() )
3924 {
3925 ResetVisibleLinesRange();
3926 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
3927 {
3928 m_aColWidths.Item(i)->bNeedsUpdate = true;
3929 }
3930 }
3931
3932 m_lines.Clear();
3933 }
3934
3935 void wxListMainWindow::DeleteAllItems()
3936 {
3937 DoDeleteAllItems();
3938
3939 RecalculatePositions();
3940 }
3941
3942 void wxListMainWindow::DeleteEverything()
3943 {
3944 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
3945 WX_CLEAR_ARRAY(m_aColWidths);
3946
3947 DeleteAllItems();
3948 }
3949
3950 // ----------------------------------------------------------------------------
3951 // scanning for an item
3952 // ----------------------------------------------------------------------------
3953
3954 void wxListMainWindow::EnsureVisible( long index )
3955 {
3956 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
3957 wxT("invalid index in EnsureVisible") );
3958
3959 // We have to call this here because the label in question might just have
3960 // been added and its position is not known yet
3961 if ( m_dirty )
3962 RecalculatePositions(true /* no refresh */);
3963
3964 MoveToItem((size_t)index);
3965 }
3966
3967 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
3968 {
3969 if (str.empty())
3970 return wxNOT_FOUND;
3971
3972 long pos = start;
3973 wxString str_upper = str.Upper();
3974 if (pos < 0)
3975 pos = 0;
3976
3977 size_t count = GetItemCount();
3978 for ( size_t i = (size_t)pos; i < count; i++ )
3979 {
3980 wxListLineData *line = GetLine(i);
3981 wxString line_upper = line->GetText(0).Upper();
3982 if (!partial)
3983 {
3984 if (line_upper == str_upper )
3985 return i;
3986 }
3987 else
3988 {
3989 if (line_upper.find(str_upper) == 0)
3990 return i;
3991 }
3992 }
3993
3994 return wxNOT_FOUND;
3995 }
3996
3997 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
3998 {
3999 long pos = start;
4000 if (pos < 0)
4001 pos = 0;
4002
4003 size_t count = GetItemCount();
4004 for (size_t i = (size_t)pos; i < count; i++)
4005 {
4006 wxListLineData *line = GetLine(i);
4007 wxListItem item;
4008 line->GetItem( 0, item );
4009 if (item.m_data == data)
4010 return i;
4011 }
4012
4013 return wxNOT_FOUND;
4014 }
4015
4016 long wxListMainWindow::FindItem( const wxPoint& pt )
4017 {
4018 size_t topItem;
4019 GetVisibleLinesRange( &topItem, NULL );
4020
4021 wxPoint p;
4022 GetItemPosition( GetItemCount() - 1, p );
4023 if ( p.y == 0 )
4024 return topItem;
4025
4026 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4027 if ( id >= 0 && id < (long)GetItemCount() )
4028 return id;
4029
4030 return wxNOT_FOUND;
4031 }
4032
4033 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4034 {
4035 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
4036
4037 size_t count = GetItemCount();
4038
4039 if ( InReportView() )
4040 {
4041 size_t current = y / GetLineHeight();
4042 if ( current < count )
4043 {
4044 flags = HitTestLine(current, x, y);
4045 if ( flags )
4046 return current;
4047 }
4048 }
4049 else // !report
4050 {
4051 // TODO: optimize it too! this is less simple than for report view but
4052 // enumerating all items is still not a way to do it!!
4053 for ( size_t current = 0; current < count; current++ )
4054 {
4055 flags = HitTestLine(current, x, y);
4056 if ( flags )
4057 return current;
4058 }
4059 }
4060
4061 return wxNOT_FOUND;
4062 }
4063
4064 // ----------------------------------------------------------------------------
4065 // adding stuff
4066 // ----------------------------------------------------------------------------
4067
4068 void wxListMainWindow::InsertItem( wxListItem &item )
4069 {
4070 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4071
4072 int count = GetItemCount();
4073 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
4074
4075 if (item.m_itemId > count)
4076 item.m_itemId = count;
4077
4078 size_t id = item.m_itemId;
4079
4080 m_dirty = true;
4081
4082 if ( InReportView() )
4083 {
4084 ResetVisibleLinesRange();
4085
4086 const unsigned col = item.GetColumn();
4087 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4088
4089 // calculate the width of the item and adjust the max column width
4090 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
4091 int width = GetItemWidthWithImage(&item);
4092 item.SetWidth(width);
4093 if (width > pWidthInfo->nMaxWidth)
4094 pWidthInfo->nMaxWidth = width;
4095 }
4096
4097 wxListLineData *line = new wxListLineData(this);
4098
4099 line->SetItem( item.m_col, item );
4100 if ( item.m_mask & wxLIST_MASK_IMAGE )
4101 {
4102 // Reset the buffered height if it's not big enough for the new image.
4103 int image = item.GetImage();
4104 if ( m_small_image_list && image != -1 && InReportView() )
4105 {
4106 int imageWidth, imageHeight;
4107 m_small_image_list->GetSize(image, imageWidth, imageHeight);
4108
4109 if ( imageHeight > m_lineHeight )
4110 m_lineHeight = 0;
4111 }
4112 }
4113
4114 m_lines.Insert( line, id );
4115
4116 m_dirty = true;
4117
4118 // If an item is selected at or below the point of insertion, we need to
4119 // increment the member variables because the current row's index has gone
4120 // up by one
4121 if ( HasCurrent() && m_current >= id )
4122 m_current++;
4123
4124 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4125
4126 RefreshLines(id, GetItemCount() - 1);
4127 }
4128
4129 void wxListMainWindow::InsertColumn( long col, const wxListItem &item )
4130 {
4131 m_dirty = true;
4132 if ( InReportView() )
4133 {
4134 wxListHeaderData *column = new wxListHeaderData( item );
4135 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4136 column->SetWidth(ComputeMinHeaderWidth(column));
4137
4138 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4139
4140 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4141 if ( insert )
4142 {
4143 wxListHeaderDataList::compatibility_iterator
4144 node = m_columns.Item( col );
4145 m_columns.Insert( node, column );
4146 m_aColWidths.Insert( colWidthInfo, col );
4147 }
4148 else
4149 {
4150 m_columns.Append( column );
4151 m_aColWidths.Add( colWidthInfo );
4152 }
4153
4154 if ( !IsVirtual() )
4155 {
4156 // update all the items
4157 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4158 {
4159 wxListLineData * const line = GetLine(i);
4160 wxListItemData * const data = new wxListItemData(this);
4161 if ( insert )
4162 line->m_items.Insert(col, data);
4163 else
4164 line->m_items.Append(data);
4165 }
4166 }
4167
4168 // invalidate it as it has to be recalculated
4169 m_headerWidth = 0;
4170 }
4171 }
4172
4173 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4174 {
4175 int width = 0;
4176 wxClientDC dc(this);
4177
4178 dc.SetFont( GetFont() );
4179
4180 if (item->GetImage() != -1)
4181 {
4182 int ix, iy;
4183 GetImageSize( item->GetImage(), ix, iy );
4184 width += ix + 5;
4185 }
4186
4187 if (!item->GetText().empty())
4188 {
4189 wxCoord w;
4190 dc.GetTextExtent( item->GetText(), &w, NULL );
4191 width += w;
4192 }
4193
4194 return width;
4195 }
4196
4197 // ----------------------------------------------------------------------------
4198 // sorting
4199 // ----------------------------------------------------------------------------
4200
4201 static wxListCtrlCompare list_ctrl_compare_func_2;
4202 static wxIntPtr list_ctrl_compare_data;
4203
4204 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4205 {
4206 wxListLineData *line1 = *arg1;
4207 wxListLineData *line2 = *arg2;
4208 wxListItem item;
4209 line1->GetItem( 0, item );
4210 wxUIntPtr data1 = item.m_data;
4211 line2->GetItem( 0, item );
4212 wxUIntPtr data2 = item.m_data;
4213 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4214 }
4215
4216 void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4217 {
4218 // selections won't make sense any more after sorting the items so reset
4219 // them
4220 HighlightAll(false);
4221 ResetCurrent();
4222
4223 list_ctrl_compare_func_2 = fn;
4224 list_ctrl_compare_data = data;
4225 m_lines.Sort( list_ctrl_compare_func_1 );
4226 m_dirty = true;
4227 }
4228
4229 // ----------------------------------------------------------------------------
4230 // scrolling
4231 // ----------------------------------------------------------------------------
4232
4233 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4234 {
4235 // update our idea of which lines are shown when we redraw the window the
4236 // next time
4237 ResetVisibleLinesRange();
4238
4239 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4240 {
4241 wxGenericListCtrl* lc = GetListCtrl();
4242 wxCHECK_RET( lc, wxT("no listctrl window?") );
4243
4244 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4245 {
4246 lc->m_headerWin->Refresh();
4247 lc->m_headerWin->Update();
4248 }
4249 }
4250 }
4251
4252 int wxListMainWindow::GetCountPerPage() const
4253 {
4254 if ( !m_linesPerPage )
4255 {
4256 wxConstCast(this, wxListMainWindow)->
4257 m_linesPerPage = GetClientSize().y / GetLineHeight();
4258 }
4259
4260 return m_linesPerPage;
4261 }
4262
4263 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4264 {
4265 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4266
4267 if ( m_lineFrom == (size_t)-1 )
4268 {
4269 size_t count = GetItemCount();
4270 if ( count )
4271 {
4272 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
4273
4274 // this may happen if SetScrollbars() hadn't been called yet
4275 if ( m_lineFrom >= count )
4276 m_lineFrom = count - 1;
4277
4278 // we redraw one extra line but this is needed to make the redrawing
4279 // logic work when there is a fractional number of lines on screen
4280 m_lineTo = m_lineFrom + m_linesPerPage;
4281 if ( m_lineTo >= count )
4282 m_lineTo = count - 1;
4283 }
4284 else // empty control
4285 {
4286 m_lineFrom = 0;
4287 m_lineTo = (size_t)-1;
4288 }
4289 }
4290
4291 wxASSERT_MSG( IsEmpty() ||
4292 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4293 wxT("GetVisibleLinesRange() returns incorrect result") );
4294
4295 if ( from )
4296 *from = m_lineFrom;
4297 if ( to )
4298 *to = m_lineTo;
4299 }
4300
4301 // -------------------------------------------------------------------------------------
4302 // wxGenericListCtrl
4303 // -------------------------------------------------------------------------------------
4304
4305 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4306
4307 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxListCtrlBase)
4308 EVT_SIZE(wxGenericListCtrl::OnSize)
4309 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
4310 END_EVENT_TABLE()
4311
4312 void wxGenericListCtrl::Init()
4313 {
4314 m_imageListNormal = NULL;
4315 m_imageListSmall = NULL;
4316 m_imageListState = NULL;
4317
4318 m_ownsImageListNormal =
4319 m_ownsImageListSmall =
4320 m_ownsImageListState = false;
4321
4322 m_mainWin = NULL;
4323 m_headerWin = NULL;
4324 }
4325
4326 wxGenericListCtrl::~wxGenericListCtrl()
4327 {
4328 if (m_ownsImageListNormal)
4329 delete m_imageListNormal;
4330 if (m_ownsImageListSmall)
4331 delete m_imageListSmall;
4332 if (m_ownsImageListState)
4333 delete m_imageListState;
4334 }
4335
4336 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4337 {
4338 bool needs_header = HasHeader();
4339 bool has_header = (m_headerWin != NULL);
4340
4341 if (needs_header == has_header)
4342 return;
4343
4344 if (needs_header)
4345 {
4346 m_headerWin = new wxListHeaderWindow
4347 (
4348 this, wxID_ANY, m_mainWin,
4349 wxPoint(0,0),
4350 wxSize
4351 (
4352 GetClientSize().x,
4353 wxRendererNative::Get().GetHeaderButtonHeight(this)
4354 ),
4355 wxTAB_TRAVERSAL
4356 );
4357
4358 #if defined( __WXMAC__ )
4359 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
4360 m_headerWin->SetFont( font );
4361 #endif
4362
4363 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4364 }
4365 else
4366 {
4367 GetSizer()->Detach( m_headerWin );
4368
4369 wxDELETE(m_headerWin);
4370 }
4371 }
4372
4373 bool wxGenericListCtrl::Create(wxWindow *parent,
4374 wxWindowID id,
4375 const wxPoint &pos,
4376 const wxSize &size,
4377 long style,
4378 const wxValidator &validator,
4379 const wxString &name)
4380 {
4381 Init();
4382
4383 // just like in other ports, an assert will fail if the user doesn't give any type style:
4384 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
4385 wxT("wxListCtrl style should have exactly one mode bit set") );
4386
4387 if ( !wxListCtrlBase::Create( parent, id, pos, size,
4388 style | wxVSCROLL | wxHSCROLL,
4389 validator, name ) )
4390 return false;
4391
4392 #ifdef __WXGTK__
4393 style &= ~wxBORDER_MASK;
4394 style |= wxBORDER_THEME;
4395 #endif
4396
4397 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
4398
4399 SetTargetWindow( m_mainWin );
4400
4401 // We use the cursor keys for moving the selection, not scrolling, so call
4402 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4403 // keyboard events forwarded to us from wxListMainWindow.
4404 DisableKeyboardScrolling();
4405
4406 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4407 sizer->Add( m_mainWin, 1, wxGROW );
4408 SetSizer( sizer );
4409
4410 CreateOrDestroyHeaderWindowAsNeeded();
4411
4412 SetInitialSize(size);
4413
4414 return true;
4415 }
4416
4417 wxBorder wxGenericListCtrl::GetDefaultBorder() const
4418 {
4419 return wxBORDER_THEME;
4420 }
4421
4422 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4423 WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4424 WXWPARAM wParam,
4425 WXLPARAM lParam)
4426 {
4427 WXLRESULT rc = wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4428
4429 // we need to process arrows ourselves for scrolling
4430 if ( nMsg == WM_GETDLGCODE )
4431 {
4432 rc |= DLGC_WANTARROWS;
4433 }
4434
4435 return rc;
4436 }
4437 #endif // __WXMSW__
4438
4439 wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4440 {
4441 wxSize newsize = size;
4442 if (m_headerWin)
4443 newsize.y -= m_headerWin->GetSize().y;
4444
4445 return newsize;
4446 }
4447
4448 void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4449 {
4450 // update our idea of which lines are shown when we redraw
4451 // the window the next time
4452 m_mainWin->ResetVisibleLinesRange();
4453
4454 HandleOnScroll( event );
4455
4456 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4457 {
4458 m_headerWin->Refresh();
4459 m_headerWin->Update();
4460 }
4461 }
4462
4463 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4464 {
4465 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4466 wxT("wxLC_VIRTUAL can't be [un]set") );
4467
4468 long flag = GetWindowStyle();
4469
4470 if (add)
4471 {
4472 if (style & wxLC_MASK_TYPE)
4473 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4474 if (style & wxLC_MASK_ALIGN)
4475 flag &= ~wxLC_MASK_ALIGN;
4476 if (style & wxLC_MASK_SORT)
4477 flag &= ~wxLC_MASK_SORT;
4478 }
4479
4480 if (add)
4481 flag |= style;
4482 else
4483 flag &= ~style;
4484
4485 // some styles can be set without recreating everything (as happens in
4486 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4487 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4488 {
4489 Refresh();
4490 wxWindow::SetWindowStyleFlag(flag);
4491 }
4492 else
4493 {
4494 SetWindowStyleFlag( flag );
4495 }
4496 }
4497
4498 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4499 {
4500 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4501 // makes sense to remove them as we'll always add scrollbars anyhow when
4502 // needed
4503 flag |= wxHSCROLL | wxVSCROLL;
4504
4505 const bool wasInReportView = HasFlag(wxLC_REPORT);
4506
4507 // update the window style first so that the header is created or destroyed
4508 // corresponding to the new style
4509 wxWindow::SetWindowStyleFlag( flag );
4510
4511 if (m_mainWin)
4512 {
4513 const bool inReportView = (flag & wxLC_REPORT) != 0;
4514 if ( inReportView != wasInReportView )
4515 {
4516 // we need to notify the main window about this change as it must
4517 // update its data structures
4518 m_mainWin->SetReportView(inReportView);
4519 }
4520
4521 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4522
4523 CreateOrDestroyHeaderWindowAsNeeded();
4524
4525 GetSizer()->Layout();
4526 }
4527 }
4528
4529 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4530 {
4531 m_mainWin->GetColumn( col, item );
4532 return true;
4533 }
4534
4535 bool wxGenericListCtrl::SetColumn( int col, const wxListItem& item )
4536 {
4537 m_mainWin->SetColumn( col, item );
4538 return true;
4539 }
4540
4541 int wxGenericListCtrl::GetColumnWidth( int col ) const
4542 {
4543 return m_mainWin->GetColumnWidth( col );
4544 }
4545
4546 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
4547 {
4548 m_mainWin->SetColumnWidth( col, width );
4549 return true;
4550 }
4551
4552 int wxGenericListCtrl::GetCountPerPage() const
4553 {
4554 return m_mainWin->GetCountPerPage(); // different from Windows ?
4555 }
4556
4557 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
4558 {
4559 m_mainWin->GetItem( info );
4560 return true;
4561 }
4562
4563 bool wxGenericListCtrl::SetItem( wxListItem &info )
4564 {
4565 m_mainWin->SetItem( info );
4566 return true;
4567 }
4568
4569 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
4570 {
4571 wxListItem info;
4572 info.m_text = label;
4573 info.m_mask = wxLIST_MASK_TEXT;
4574 info.m_itemId = index;
4575 info.m_col = col;
4576 if ( imageId > -1 )
4577 {
4578 info.m_image = imageId;
4579 info.m_mask |= wxLIST_MASK_IMAGE;
4580 }
4581
4582 m_mainWin->SetItem(info);
4583 return true;
4584 }
4585
4586 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
4587 {
4588 return m_mainWin->GetItemState( item, stateMask );
4589 }
4590
4591 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
4592 {
4593 m_mainWin->SetItemState( item, state, stateMask );
4594 return true;
4595 }
4596
4597 bool
4598 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
4599 {
4600 return SetItemColumnImage(item, 0, image);
4601 }
4602
4603 bool
4604 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
4605 {
4606 wxListItem info;
4607 info.m_image = image;
4608 info.m_mask = wxLIST_MASK_IMAGE;
4609 info.m_itemId = item;
4610 info.m_col = column;
4611 m_mainWin->SetItem( info );
4612 return true;
4613 }
4614
4615 wxString wxGenericListCtrl::GetItemText( long item, int col ) const
4616 {
4617 return m_mainWin->GetItemText(item, col);
4618 }
4619
4620 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
4621 {
4622 m_mainWin->SetItemText(item, str);
4623 }
4624
4625 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
4626 {
4627 wxListItem info;
4628 info.m_mask = wxLIST_MASK_DATA;
4629 info.m_itemId = item;
4630 m_mainWin->GetItem( info );
4631 return info.m_data;
4632 }
4633
4634 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
4635 {
4636 wxListItem info;
4637 info.m_mask = wxLIST_MASK_DATA;
4638 info.m_itemId = item;
4639 info.m_data = data;
4640 m_mainWin->SetItem( info );
4641 return true;
4642 }
4643
4644 wxRect wxGenericListCtrl::GetViewRect() const
4645 {
4646 return m_mainWin->GetViewRect();
4647 }
4648
4649 bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4650 {
4651 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4652 }
4653
4654 bool wxGenericListCtrl::GetSubItemRect(long item,
4655 long subItem,
4656 wxRect& rect,
4657 int WXUNUSED(code)) const
4658 {
4659 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4660 return false;
4661
4662 if ( m_mainWin->HasHeader() )
4663 rect.y += m_headerWin->GetSize().y + 1;
4664
4665 return true;
4666 }
4667
4668 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
4669 {
4670 m_mainWin->GetItemPosition( item, pos );
4671 return true;
4672 }
4673
4674 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
4675 {
4676 return false;
4677 }
4678
4679 int wxGenericListCtrl::GetItemCount() const
4680 {
4681 return m_mainWin->GetItemCount();
4682 }
4683
4684 int wxGenericListCtrl::GetColumnCount() const
4685 {
4686 return m_mainWin->GetColumnCount();
4687 }
4688
4689 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
4690 {
4691 m_mainWin->SetItemSpacing( spacing, isSmall );
4692 }
4693
4694 wxSize wxGenericListCtrl::GetItemSpacing() const
4695 {
4696 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
4697
4698 return wxSize(spacing, spacing);
4699 }
4700
4701 #if WXWIN_COMPATIBILITY_2_6
4702 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
4703 {
4704 return m_mainWin->GetItemSpacing( isSmall );
4705 }
4706 #endif // WXWIN_COMPATIBILITY_2_6
4707
4708 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
4709 {
4710 wxListItem info;
4711 info.m_itemId = item;
4712 info.SetTextColour( col );
4713 m_mainWin->SetItem( info );
4714 }
4715
4716 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
4717 {
4718 wxListItem info;
4719 info.m_itemId = item;
4720 m_mainWin->GetItem( info );
4721 return info.GetTextColour();
4722 }
4723
4724 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
4725 {
4726 wxListItem info;
4727 info.m_itemId = item;
4728 info.SetBackgroundColour( col );
4729 m_mainWin->SetItem( info );
4730 }
4731
4732 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
4733 {
4734 wxListItem info;
4735 info.m_itemId = item;
4736 m_mainWin->GetItem( info );
4737 return info.GetBackgroundColour();
4738 }
4739
4740 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4741 {
4742 wxListItem info;
4743 info.m_itemId = item;
4744 info.SetFont( f );
4745 m_mainWin->SetItem( info );
4746 }
4747
4748 wxFont wxGenericListCtrl::GetItemFont( long item ) const
4749 {
4750 wxListItem info;
4751 info.m_itemId = item;
4752 m_mainWin->GetItem( info );
4753 return info.GetFont();
4754 }
4755
4756 int wxGenericListCtrl::GetSelectedItemCount() const
4757 {
4758 return m_mainWin->GetSelectedItemCount();
4759 }
4760
4761 wxColour wxGenericListCtrl::GetTextColour() const
4762 {
4763 return GetForegroundColour();
4764 }
4765
4766 void wxGenericListCtrl::SetTextColour(const wxColour& col)
4767 {
4768 SetForegroundColour(col);
4769 }
4770
4771 long wxGenericListCtrl::GetTopItem() const
4772 {
4773 size_t top;
4774 m_mainWin->GetVisibleLinesRange(&top, NULL);
4775 return (long)top;
4776 }
4777
4778 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
4779 {
4780 return m_mainWin->GetNextItem( item, geom, state );
4781 }
4782
4783 wxImageList *wxGenericListCtrl::GetImageList(int which) const
4784 {
4785 if (which == wxIMAGE_LIST_NORMAL)
4786 return m_imageListNormal;
4787 else if (which == wxIMAGE_LIST_SMALL)
4788 return m_imageListSmall;
4789 else if (which == wxIMAGE_LIST_STATE)
4790 return m_imageListState;
4791
4792 return NULL;
4793 }
4794
4795 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
4796 {
4797 if ( which == wxIMAGE_LIST_NORMAL )
4798 {
4799 if (m_ownsImageListNormal)
4800 delete m_imageListNormal;
4801 m_imageListNormal = imageList;
4802 m_ownsImageListNormal = false;
4803 }
4804 else if ( which == wxIMAGE_LIST_SMALL )
4805 {
4806 if (m_ownsImageListSmall)
4807 delete m_imageListSmall;
4808 m_imageListSmall = imageList;
4809 m_ownsImageListSmall = false;
4810 }
4811 else if ( which == wxIMAGE_LIST_STATE )
4812 {
4813 if (m_ownsImageListState)
4814 delete m_imageListState;
4815 m_imageListState = imageList;
4816 m_ownsImageListState = false;
4817 }
4818
4819 m_mainWin->SetImageList( imageList, which );
4820 }
4821
4822 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
4823 {
4824 SetImageList(imageList, which);
4825 if ( which == wxIMAGE_LIST_NORMAL )
4826 m_ownsImageListNormal = true;
4827 else if ( which == wxIMAGE_LIST_SMALL )
4828 m_ownsImageListSmall = true;
4829 else if ( which == wxIMAGE_LIST_STATE )
4830 m_ownsImageListState = true;
4831 }
4832
4833 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
4834 {
4835 return 0;
4836 }
4837
4838 bool wxGenericListCtrl::DeleteItem( long item )
4839 {
4840 m_mainWin->DeleteItem( item );
4841 return true;
4842 }
4843
4844 bool wxGenericListCtrl::DeleteAllItems()
4845 {
4846 m_mainWin->DeleteAllItems();
4847 return true;
4848 }
4849
4850 bool wxGenericListCtrl::DeleteAllColumns()
4851 {
4852 size_t count = m_mainWin->m_columns.GetCount();
4853 for ( size_t n = 0; n < count; n++ )
4854 DeleteColumn( 0 );
4855 return true;
4856 }
4857
4858 void wxGenericListCtrl::ClearAll()
4859 {
4860 m_mainWin->DeleteEverything();
4861 }
4862
4863 bool wxGenericListCtrl::DeleteColumn( int col )
4864 {
4865 m_mainWin->DeleteColumn( col );
4866
4867 // if we don't have the header any longer, we need to relayout the window
4868 // if ( !GetColumnCount() )
4869
4870
4871 // Ensure that the non-existent columns are really removed from display.
4872 Refresh();
4873
4874 return true;
4875 }
4876
4877 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
4878 wxClassInfo* textControlClass)
4879 {
4880 return m_mainWin->EditLabel( item, textControlClass );
4881 }
4882
4883 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
4884 {
4885 return m_mainWin->GetEditControl();
4886 }
4887
4888 bool wxGenericListCtrl::EnsureVisible( long item )
4889 {
4890 m_mainWin->EnsureVisible( item );
4891 return true;
4892 }
4893
4894 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
4895 {
4896 return m_mainWin->FindItem( start, str, partial );
4897 }
4898
4899 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
4900 {
4901 return m_mainWin->FindItem( start, data );
4902 }
4903
4904 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
4905 int WXUNUSED(direction))
4906 {
4907 return m_mainWin->FindItem( pt );
4908 }
4909
4910 // TODO: sub item hit testing
4911 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
4912 {
4913 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
4914 }
4915
4916 long wxGenericListCtrl::InsertItem( wxListItem& info )
4917 {
4918 m_mainWin->InsertItem( info );
4919 return info.m_itemId;
4920 }
4921
4922 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
4923 {
4924 wxListItem info;
4925 info.m_text = label;
4926 info.m_mask = wxLIST_MASK_TEXT;
4927 info.m_itemId = index;
4928 return InsertItem( info );
4929 }
4930
4931 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
4932 {
4933 wxListItem info;
4934 info.m_mask = wxLIST_MASK_IMAGE;
4935 info.m_image = imageIndex;
4936 info.m_itemId = index;
4937 return InsertItem( info );
4938 }
4939
4940 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
4941 {
4942 wxListItem info;
4943 info.m_text = label;
4944 info.m_image = imageIndex;
4945 info.m_mask = wxLIST_MASK_TEXT;
4946 if (imageIndex > -1)
4947 info.m_mask |= wxLIST_MASK_IMAGE;
4948 info.m_itemId = index;
4949 return InsertItem( info );
4950 }
4951
4952 long wxGenericListCtrl::DoInsertColumn( long col, const wxListItem &item )
4953 {
4954 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4955
4956 m_mainWin->InsertColumn( col, item );
4957
4958 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4959 // still have m_headerWin==NULL
4960 if (m_headerWin)
4961 m_headerWin->Refresh();
4962
4963 return 0;
4964 }
4965
4966 bool wxGenericListCtrl::ScrollList( int dx, int dy )
4967 {
4968 return m_mainWin->ScrollList(dx, dy);
4969 }
4970
4971 // Sort items.
4972 // fn is a function which takes 3 long arguments: item1, item2, data.
4973 // item1 is the long data associated with a first item (NOT the index).
4974 // item2 is the long data associated with a second item (NOT the index).
4975 // data is the same value as passed to SortItems.
4976 // The return value is a negative number if the first item should precede the second
4977 // item, a positive number of the second item should precede the first,
4978 // or zero if the two items are equivalent.
4979 // data is arbitrary data to be passed to the sort function.
4980
4981 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4982 {
4983 m_mainWin->SortItems( fn, data );
4984 return true;
4985 }
4986
4987 // ----------------------------------------------------------------------------
4988 // event handlers
4989 // ----------------------------------------------------------------------------
4990
4991 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
4992 {
4993 if (!m_mainWin) return;
4994
4995 // We need to override OnSize so that our scrolled
4996 // window a) does call Layout() to use sizers for
4997 // positioning the controls but b) does not query
4998 // the sizer for their size and use that for setting
4999 // the scrollable area as set that ourselves by
5000 // calling SetScrollbar() further down.
5001
5002 Layout();
5003
5004 m_mainWin->RecalculatePositions();
5005
5006 AdjustScrollbars();
5007 }
5008
5009 void wxGenericListCtrl::OnInternalIdle()
5010 {
5011 wxWindow::OnInternalIdle();
5012
5013 if (m_mainWin->m_dirty)
5014 m_mainWin->RecalculatePositions();
5015 }
5016
5017 // ----------------------------------------------------------------------------
5018 // font/colours
5019 // ----------------------------------------------------------------------------
5020
5021 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5022 {
5023 if (m_mainWin)
5024 {
5025 m_mainWin->SetBackgroundColour( colour );
5026 m_mainWin->m_dirty = true;
5027 }
5028
5029 return true;
5030 }
5031
5032 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5033 {
5034 if ( !wxWindow::SetForegroundColour( colour ) )
5035 return false;
5036
5037 if (m_mainWin)
5038 {
5039 m_mainWin->SetForegroundColour( colour );
5040 m_mainWin->m_dirty = true;
5041 }
5042
5043 if (m_headerWin)
5044 m_headerWin->SetForegroundColour( colour );
5045
5046 return true;
5047 }
5048
5049 bool wxGenericListCtrl::SetFont( const wxFont &font )
5050 {
5051 if ( !wxWindow::SetFont( font ) )
5052 return false;
5053
5054 if (m_mainWin)
5055 {
5056 m_mainWin->SetFont( font );
5057 m_mainWin->m_dirty = true;
5058 }
5059
5060 if (m_headerWin)
5061 {
5062 m_headerWin->SetFont( font );
5063 // CalculateAndSetHeaderHeight();
5064 }
5065
5066 Refresh();
5067
5068 return true;
5069 }
5070
5071 // static
5072 wxVisualAttributes
5073 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5074 {
5075 #if _USE_VISATTR
5076 // Use the same color scheme as wxListBox
5077 return wxListBox::GetClassDefaultAttributes(variant);
5078 #else
5079 wxUnusedVar(variant);
5080 wxVisualAttributes attr;
5081 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5082 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5083 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5084 return attr;
5085 #endif
5086 }
5087
5088 // ----------------------------------------------------------------------------
5089 // methods forwarded to m_mainWin
5090 // ----------------------------------------------------------------------------
5091
5092 #if wxUSE_DRAG_AND_DROP
5093
5094 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5095 {
5096 m_mainWin->SetDropTarget( dropTarget );
5097 }
5098
5099 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5100 {
5101 return m_mainWin->GetDropTarget();
5102 }
5103
5104 #endif
5105
5106 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5107 {
5108 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5109 }
5110
5111 wxColour wxGenericListCtrl::GetBackgroundColour() const
5112 {
5113 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5114 }
5115
5116 wxColour wxGenericListCtrl::GetForegroundColour() const
5117 {
5118 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5119 }
5120
5121 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5122 {
5123 #if wxUSE_MENUS
5124 return m_mainWin->PopupMenu( menu, x, y );
5125 #else
5126 return false;
5127 #endif
5128 }
5129
5130 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5131 {
5132 // It's not clear whether this can be called before m_mainWin is created
5133 // but it seems better to be on the safe side and check.
5134 if ( m_mainWin )
5135 m_mainWin->DoClientToScreen(x, y);
5136 else
5137 wxListCtrlBase::DoClientToScreen(x, y);
5138 }
5139
5140 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5141 {
5142 // At least in wxGTK/Univ build this method can be called before m_mainWin
5143 // is created so avoid crashes in this case.
5144 if ( m_mainWin )
5145 m_mainWin->DoScreenToClient(x, y);
5146 else
5147 wxListCtrlBase::DoScreenToClient(x, y);
5148 }
5149
5150 wxSize wxGenericListCtrl::DoGetBestClientSize() const
5151 {
5152 // Something is better than nothing even if this is completely arbitrary.
5153 wxSize sizeBest(100, 80);
5154
5155 if ( !InReportView() )
5156 {
5157 // Ensure that our minimal width is at least big enough to show all our
5158 // items. This is important for wxListbook to size itself correctly.
5159
5160 // Remember the offset of the first item: this corresponds to the
5161 // margins around the item so we will add it to the minimal size below
5162 // to ensure that we have equal margins on all sides.
5163 wxPoint ofs;
5164
5165 // We can iterate over all items as there shouldn't be too many of them
5166 // in non-report view. If it ever becomes a problem, we could examine
5167 // just the first few items probably, the determination of the best
5168 // size is less important if we will need scrollbars anyhow.
5169 for ( int n = 0; n < GetItemCount(); n++ )
5170 {
5171 const wxRect itemRect = m_mainWin->GetLineRect(n);
5172 if ( !n )
5173 {
5174 // Remember the position of the first item as all the rest are
5175 // offset by at least this number of pixels too.
5176 ofs = itemRect.GetPosition();
5177 }
5178
5179 sizeBest.IncTo(itemRect.GetSize());
5180 }
5181
5182 sizeBest.IncBy(2*ofs);
5183
5184
5185 // If we have the scrollbars we need to account for them too. And to
5186 // make sure the scrollbars status is up to date we need to call this
5187 // function to set them.
5188 m_mainWin->RecalculatePositions(true /* no refresh */);
5189
5190 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5191 // to use m_mainWin client/virtual size for determination of whether we
5192 // use scrollbars and not the size of this window itself. Maybe that
5193 // function should be extended to work correctly in the case when our
5194 // scrollbars manage a different window from this one but currently it
5195 // doesn't work.
5196 const wxSize sizeClient = m_mainWin->GetClientSize();
5197 const wxSize sizeVirt = m_mainWin->GetVirtualSize();
5198
5199 if ( sizeVirt.x > sizeClient.x /* HasScrollbar(wxHORIZONTAL) */ )
5200 sizeBest.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
5201
5202 if ( sizeVirt.y > sizeClient.y /* HasScrollbar(wxVERTICAL) */ )
5203 sizeBest.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
5204 }
5205
5206 return sizeBest;
5207 }
5208
5209 // ----------------------------------------------------------------------------
5210 // virtual list control support
5211 // ----------------------------------------------------------------------------
5212
5213 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5214 {
5215 // this is a pure virtual function, in fact - which is not really pure
5216 // because the controls which are not virtual don't need to implement it
5217 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5218
5219 return wxEmptyString;
5220 }
5221
5222 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5223 {
5224 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5225 -1,
5226 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5227 return -1;
5228 }
5229
5230 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5231 {
5232 if (!column)
5233 return OnGetItemImage(item);
5234
5235 return -1;
5236 }
5237
5238 wxListItemAttr *
5239 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5240 {
5241 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5242 wxT("invalid item index in OnGetItemAttr()") );
5243
5244 // no attributes by default
5245 return NULL;
5246 }
5247
5248 void wxGenericListCtrl::SetItemCount(long count)
5249 {
5250 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5251
5252 m_mainWin->SetItemCount(count);
5253 }
5254
5255 void wxGenericListCtrl::RefreshItem(long item)
5256 {
5257 m_mainWin->RefreshLine(item);
5258 }
5259
5260 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5261 {
5262 m_mainWin->RefreshLines(itemFrom, itemTo);
5263 }
5264
5265 // Generic wxListCtrl is more or less a container for two other
5266 // windows which drawings are done upon. These are namely
5267 // 'm_headerWin' and 'm_mainWin'.
5268 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5269 // behaviour wxListCtrl has under wxMSW.
5270 //
5271 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5272 {
5273 if (!rect)
5274 {
5275 // The easy case, no rectangle specified.
5276 if (m_headerWin)
5277 m_headerWin->Refresh(eraseBackground);
5278
5279 if (m_mainWin)
5280 m_mainWin->Refresh(eraseBackground);
5281 }
5282 else
5283 {
5284 // Refresh the header window
5285 if (m_headerWin)
5286 {
5287 wxRect rectHeader = m_headerWin->GetRect();
5288 rectHeader.Intersect(*rect);
5289 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5290 {
5291 int x, y;
5292 m_headerWin->GetPosition(&x, &y);
5293 rectHeader.Offset(-x, -y);
5294 m_headerWin->Refresh(eraseBackground, &rectHeader);
5295 }
5296 }
5297
5298 // Refresh the main window
5299 if (m_mainWin)
5300 {
5301 wxRect rectMain = m_mainWin->GetRect();
5302 rectMain.Intersect(*rect);
5303 if (rectMain.GetWidth() && rectMain.GetHeight())
5304 {
5305 int x, y;
5306 m_mainWin->GetPosition(&x, &y);
5307 rectMain.Offset(-x, -y);
5308 m_mainWin->Refresh(eraseBackground, &rectMain);
5309 }
5310 }
5311 }
5312 }
5313
5314 void wxGenericListCtrl::Update()
5315 {
5316 if ( m_mainWin )
5317 {
5318 if ( m_mainWin->m_dirty )
5319 m_mainWin->RecalculatePositions();
5320
5321 m_mainWin->Update();
5322 }
5323
5324 if ( m_headerWin )
5325 m_headerWin->Update();
5326 }
5327
5328 #endif // wxUSE_LISTCTRL