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