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