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