1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 #include "wx/imaglist.h"
51 #include "wx/listctrl.h"
53 #if defined(__WXGTK__)
55 #include "wx/gtk/win_gtk.h"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
68 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
69 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
77 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the height of the header window (FIXME: should depend on its font!)
90 static const int HEADER_HEIGHT
= 23;
92 // the scrollbar units
93 static const int SCROLL_UNIT_X
= 15;
94 static const int SCROLL_UNIT_Y
= 15;
96 // the spacing between the lines (in report mode)
97 static const int LINE_SPACING
= 0;
99 // extra margins around the text label
100 static const int EXTRA_WIDTH
= 3;
101 static const int EXTRA_HEIGHT
= 4;
103 // offset for the header window
104 static const int HEADER_OFFSET_X
= 1;
105 static const int HEADER_OFFSET_Y
= 1;
107 // when autosizing the columns, add some slack
108 static const int AUTOSIZE_COL_MARGIN
= 10;
110 // default and minimal widths for the header columns
111 static const int WIDTH_COL_DEFAULT
= 80;
112 static const int WIDTH_COL_MIN
= 10;
114 // the space between the image and the text in the report mode
115 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
127 WX_DEFINE_SORTED_EXPORTED_ARRAY(size_t, wxIndexArray
);
129 // this class is used to store the selected items in the virtual list control
130 // (but it is not tied to list control and so can be used with other controls
131 // such as wxListBox in wxUniv)
133 // the idea is to make it really smart later (i.e. store the selections as an
134 // array of ranes + individual items) but, as I don't have time to do it now
135 // (this would require writing code to merge/break ranges and much more) keep
136 // it simple but define a clean interface to it which allows it to be made
138 class WXDLLEXPORT wxSelectionStore
141 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
143 // set the total number of items we handle
144 void SetItemCount(size_t count
) { m_count
= count
; }
146 // special case of SetItemCount(0)
147 void Clear() { m_itemsSel
.Clear(); m_count
= 0; }
149 // must be called when a new item is inserted/added
150 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
152 // must be called when an item is deleted
153 void OnItemDelete(size_t item
);
155 // select one item, use SelectRange() insted if possible!
157 // returns true if the items selection really changed
158 bool SelectItem(size_t item
, bool select
= TRUE
);
160 // select the range of items
162 // return true and fill the itemsChanged array with the indices of items
163 // which have changed state if "few" of them did, otherwise return false
164 // (meaning that too many items changed state to bother counting them
166 bool SelectRange(size_t itemFrom
, size_t itemTo
,
168 wxArrayInt
*itemsChanged
= NULL
);
170 // return true if the given item is selected
171 bool IsSelected(size_t item
) const;
173 // return the total number of selected items
174 size_t GetSelectedCount() const
176 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
177 : m_itemsSel
.GetCount();
182 void Init() { m_defaultState
= FALSE
; }
184 // the total number of items we handle
187 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
188 // there are more selected items than non selected ones - this allows to
189 // handle selection of all items efficiently
192 // the array of items whose selection state is different from default
193 wxIndexArray m_itemsSel
;
195 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
198 //-----------------------------------------------------------------------------
199 // wxListItemData (internal)
200 //-----------------------------------------------------------------------------
202 class WXDLLEXPORT wxListItemData
205 wxListItemData(wxListMainWindow
*owner
);
208 void SetItem( const wxListItem
&info
);
209 void SetImage( int image
) { m_image
= image
; }
210 void SetData( long data
) { m_data
= data
; }
211 void SetPosition( int x
, int y
);
212 void SetSize( int width
, int height
);
214 bool HasText() const { return !m_text
.empty(); }
215 const wxString
& GetText() const { return m_text
; }
216 void SetText(const wxString
& text
) { m_text
= text
; }
218 // we can't use empty string for measuring the string width/height, so
219 // always return something
220 wxString
GetTextForMeasuring() const
222 wxString s
= GetText();
229 bool IsHit( int x
, int y
) const;
233 int GetWidth() const;
234 int GetHeight() const;
236 int GetImage() const { return m_image
; }
237 bool HasImage() const { return GetImage() != -1; }
239 void GetItem( wxListItem
&info
) const;
241 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
242 wxListItemAttr
*GetAttr() const { return m_attr
; }
245 // the item image or -1
248 // user data associated with the item
251 // the item coordinates are not used in report mode, instead this pointer
252 // is NULL and the owner window is used to retrieve the item position and
256 // the list ctrl we are in
257 wxListMainWindow
*m_owner
;
259 // custom attributes or NULL
260 wxListItemAttr
*m_attr
;
263 // common part of all ctors
269 //-----------------------------------------------------------------------------
270 // wxListHeaderData (internal)
271 //-----------------------------------------------------------------------------
273 class WXDLLEXPORT wxListHeaderData
: public wxObject
277 wxListHeaderData( const wxListItem
&info
);
278 void SetItem( const wxListItem
&item
);
279 void SetPosition( int x
, int y
);
280 void SetWidth( int w
);
281 void SetFormat( int format
);
282 void SetHeight( int h
);
283 bool HasImage() const;
285 bool HasText() const { return !m_text
.empty(); }
286 const wxString
& GetText() const { return m_text
; }
287 void SetText(const wxString
& text
) { m_text
= text
; }
289 void GetItem( wxListItem
&item
);
291 bool IsHit( int x
, int y
) const;
292 int GetImage() const;
293 int GetWidth() const;
294 int GetFormat() const;
310 //-----------------------------------------------------------------------------
311 // wxListLineData (internal)
312 //-----------------------------------------------------------------------------
314 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
315 #include "wx/listimpl.cpp"
316 WX_DEFINE_LIST(wxListItemDataList
);
318 class WXDLLEXPORT wxListLineData
321 // the list of subitems: only may have more than one item in report mode
322 wxListItemDataList m_items
;
324 // this is not used in report view
336 // the part to be highlighted
337 wxRect m_rectHighlight
;
340 // is this item selected? [NB: not used in virtual mode]
343 // back pointer to the list ctrl
344 wxListMainWindow
*m_owner
;
347 wxListLineData(wxListMainWindow
*owner
);
349 ~wxListLineData() { delete m_gi
; }
351 // are we in report mode?
352 inline bool InReportView() const;
354 // are we in virtual report mode?
355 inline bool IsVirtual() const;
357 // these 2 methods shouldn't be called for report view controls, in that
358 // case we determine our position/size ourselves
360 // calculate the size of the line
361 void CalculateSize( wxDC
*dc
, int spacing
);
363 // remember the position this line appears at
364 void SetPosition( int x
, int y
, int window_width
, int spacing
);
368 void SetImage( int image
) { SetImage(0, image
); }
369 int GetImage() const { return GetImage(0); }
370 bool HasImage() const { return GetImage() != -1; }
371 bool HasText() const { return !GetText(0).empty(); }
373 void SetItem( int index
, const wxListItem
&info
);
374 void GetItem( int index
, wxListItem
&info
);
376 wxString
GetText(int index
) const;
377 void SetText( int index
, const wxString s
);
379 wxListItemAttr
*GetAttr() const;
380 void SetAttr(wxListItemAttr
*attr
);
382 // return true if the highlighting really changed
383 bool Highlight( bool on
);
385 void ReverseHighlight();
387 bool IsHighlighted() const
389 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
391 return m_highlighted
;
394 // draw the line on the given DC in icon/list mode
395 void Draw( wxDC
*dc
);
397 // the same in report mode
398 void DrawInReportMode( wxDC
*dc
,
400 const wxRect
& rectHL
,
404 // set the line to contain num items (only can be > 1 in report mode)
405 void InitItems( int num
);
407 // get the mode (i.e. style) of the list control
408 inline int GetMode() const;
410 // prepare the DC for drawing with these item's attributes, return true if
411 // we need to draw the items background to highlight it, false otherwise
412 bool SetAttributes(wxDC
*dc
,
413 const wxListItemAttr
*attr
,
416 // these are only used by GetImage/SetImage above, we don't support images
417 // with subitems at the public API level yet
418 void SetImage( int index
, int image
);
419 int GetImage( int index
) const;
422 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
423 #include "wx/arrimpl.cpp"
424 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
426 //-----------------------------------------------------------------------------
427 // wxListHeaderWindow (internal)
428 //-----------------------------------------------------------------------------
430 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
433 wxListMainWindow
*m_owner
;
434 wxCursor
*m_currentCursor
;
435 wxCursor
*m_resizeCursor
;
438 // column being resized
441 // divider line position in logical (unscrolled) coords
444 // minimal position beyond which the divider line can't be dragged in
449 wxListHeaderWindow();
451 wxListHeaderWindow( wxWindow
*win
,
453 wxListMainWindow
*owner
,
454 const wxPoint
&pos
= wxDefaultPosition
,
455 const wxSize
&size
= wxDefaultSize
,
457 const wxString
&name
= "wxlistctrlcolumntitles" );
459 virtual ~wxListHeaderWindow();
461 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
463 void AdjustDC(wxDC
& dc
);
465 void OnPaint( wxPaintEvent
&event
);
466 void OnMouse( wxMouseEvent
&event
);
467 void OnSetFocus( wxFocusEvent
&event
);
473 // common part of all ctors
476 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
477 DECLARE_EVENT_TABLE()
480 //-----------------------------------------------------------------------------
481 // wxListRenameTimer (internal)
482 //-----------------------------------------------------------------------------
484 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
487 wxListMainWindow
*m_owner
;
490 wxListRenameTimer( wxListMainWindow
*owner
);
494 //-----------------------------------------------------------------------------
495 // wxListTextCtrl (internal)
496 //-----------------------------------------------------------------------------
498 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
503 wxListMainWindow
*m_owner
;
504 wxString m_startValue
;
509 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
510 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
511 const wxString
&value
= "",
512 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
514 const wxValidator
& validator
= wxDefaultValidator
,
515 const wxString
&name
= "listctrltextctrl" );
516 void OnChar( wxKeyEvent
&event
);
517 void OnKeyUp( wxKeyEvent
&event
);
518 void OnKillFocus( wxFocusEvent
&event
);
521 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
);
522 DECLARE_EVENT_TABLE()
525 //-----------------------------------------------------------------------------
526 // wxListMainWindow (internal)
527 //-----------------------------------------------------------------------------
529 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
530 #include "wx/listimpl.cpp"
531 WX_DEFINE_LIST(wxListHeaderDataList
);
533 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
537 wxListMainWindow( wxWindow
*parent
,
539 const wxPoint
& pos
= wxDefaultPosition
,
540 const wxSize
& size
= wxDefaultSize
,
542 const wxString
&name
= _T("listctrlmainwindow") );
544 virtual ~wxListMainWindow();
546 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
548 // return true if this is a virtual list control
549 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
551 // return true if the control is in report mode
552 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
554 // return true if we are in single selection mode, false if multi sel
555 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
557 // do we have a header window?
558 bool HasHeader() const
559 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
561 void HighlightAll( bool on
);
563 // all these functions only do something if the line is currently visible
565 // change the line "selected" state, return TRUE if it really changed
566 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
568 // as HighlightLine() but do it for the range of lines: this is incredibly
569 // more efficient for virtual list controls!
571 // NB: unlike HighlightLine() this one does refresh the lines on screen
572 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
574 // toggle the line state and refresh it
575 void ReverseHighlight( size_t line
)
576 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
578 // return true if the line is highlighted
579 bool IsHighlighted(size_t line
) const;
581 // refresh one or several lines at once
582 void RefreshLine( size_t line
);
583 void RefreshLines( size_t lineFrom
, size_t lineTo
);
585 // refresh all selected items
586 void RefreshSelected();
588 // refresh all lines below the given one: the difference with
589 // RefreshLines() is that the index here might not be a valid one (happens
590 // when the last line is deleted)
591 void RefreshAfter( size_t lineFrom
);
593 // the methods which are forwarded to wxListLineData itself in list/icon
594 // modes but are here because the lines don't store their positions in the
597 // get the bound rect for the entire line
598 wxRect
GetLineRect(size_t line
) const;
600 // get the bound rect of the label
601 wxRect
GetLineLabelRect(size_t line
) const;
603 // get the bound rect of the items icon (only may be called if we do have
605 wxRect
GetLineIconRect(size_t line
) const;
607 // get the rect to be highlighted when the item has focus
608 wxRect
GetLineHighlightRect(size_t line
) const;
610 // get the size of the total line rect
611 wxSize
GetLineSize(size_t line
) const
612 { return GetLineRect(line
).GetSize(); }
614 // return the hit code for the corresponding position (in this line)
615 long HitTestLine(size_t line
, int x
, int y
) const;
617 // bring the selected item into view, scrolling to it if necessary
618 void MoveToItem(size_t item
);
620 // bring the current item into view
621 void MoveToFocus() { MoveToItem(m_current
); }
623 // start editing the label of the given item
624 void EditLabel( long item
);
626 // suspend/resume redrawing the control
630 void OnRenameTimer();
631 void OnRenameAccept();
633 void OnMouse( wxMouseEvent
&event
);
635 // called to switch the selection from the current item to newCurrent,
636 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
638 void OnChar( wxKeyEvent
&event
);
639 void OnKeyDown( wxKeyEvent
&event
);
640 void OnSetFocus( wxFocusEvent
&event
);
641 void OnKillFocus( wxFocusEvent
&event
);
642 void OnScroll(wxScrollWinEvent
& event
) ;
644 void OnPaint( wxPaintEvent
&event
);
646 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
647 void GetImageSize( int index
, int &width
, int &height
) const;
648 int GetTextLength( const wxString
&s
) const;
650 void SetImageList( wxImageList
*imageList
, int which
);
651 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
652 int GetItemSpacing( bool isSmall
= FALSE
);
654 void SetColumn( int col
, wxListItem
&item
);
655 void SetColumnWidth( int col
, int width
);
656 void GetColumn( int col
, wxListItem
&item
) const;
657 int GetColumnWidth( int col
) const;
658 int GetColumnCount() const { return m_columns
.GetCount(); }
660 // returns the sum of the heights of all columns
661 int GetHeaderWidth() const;
663 int GetCountPerPage() const;
665 void SetItem( wxListItem
&item
);
666 void GetItem( wxListItem
&item
);
667 void SetItemState( long item
, long state
, long stateMask
);
668 int GetItemState( long item
, long stateMask
);
669 void GetItemRect( long index
, wxRect
&rect
);
670 bool GetItemPosition( long item
, wxPoint
& pos
);
671 int GetSelectedItemCount();
673 // set the scrollbars and update the positions of the items
674 void RecalculatePositions(bool noRefresh
= FALSE
);
676 // refresh the window and the header
679 long GetNextItem( long item
, int geometry
, int state
);
680 void DeleteItem( long index
);
681 void DeleteAllItems();
682 void DeleteColumn( int col
);
683 void DeleteEverything();
684 void EnsureVisible( long index
);
685 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
686 long FindItem( long start
, long data
);
687 long HitTest( int x
, int y
, int &flags
);
688 void InsertItem( wxListItem
&item
);
689 void InsertColumn( long col
, wxListItem
&item
);
690 void SortItems( wxListCtrlCompare fn
, long data
);
692 size_t GetItemCount() const;
693 bool IsEmpty() const { return GetItemCount() == 0; }
694 void SetItemCount(long count
);
696 // change the current (== focused) item, send a notification event
697 void ChangeCurrent(size_t current
);
698 void ResetCurrent() { ChangeCurrent((size_t)-1); }
699 bool HasCurrent() const { return m_current
!= (size_t)-1; }
701 // send out a wxListEvent
702 void SendNotify( size_t line
,
704 wxPoint point
= wxDefaultPosition
);
706 // override base class virtual to reset m_lineHeight when the font changes
707 virtual bool SetFont(const wxFont
& font
)
709 if ( !wxScrolledWindow::SetFont(font
) )
717 // these are for wxListLineData usage only
719 // get the backpointer to the list ctrl
720 wxListCtrl
*GetListCtrl() const
722 return wxStaticCast(GetParent(), wxListCtrl
);
725 // get the height of all lines (assuming they all do have the same height)
726 wxCoord
GetLineHeight() const;
728 // get the y position of the given line (only for report view)
729 wxCoord
GetLineY(size_t line
) const;
731 // get the brush to use for the item highlighting
732 wxBrush
*GetHighlightBrush() const
734 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
738 // the array of all line objects for a non virtual list control
739 wxListLineDataArray m_lines
;
741 // the list of column objects
742 wxListHeaderDataList m_columns
;
744 // currently focused item or -1
747 // the item currently being edited or -1
748 size_t m_currentEdit
;
750 // the number of lines per page
753 // this flag is set when something which should result in the window
754 // redrawing happens (i.e. an item was added or deleted, or its appearance
755 // changed) and OnPaint() doesn't redraw the window while it is set which
756 // allows to minimize the number of repaintings when a lot of items are
757 // being added. The real repainting occurs only after the next OnIdle()
761 wxColour
*m_highlightColour
;
764 wxImageList
*m_small_image_list
;
765 wxImageList
*m_normal_image_list
;
767 int m_normal_spacing
;
771 wxTimer
*m_renameTimer
;
773 wxString m_renameRes
;
778 // for double click logic
779 size_t m_lineLastClicked
,
780 m_lineBeforeLastClicked
;
783 // the total count of items in a virtual list control
786 // the object maintaining the items selection state, only used in virtual
788 wxSelectionStore m_selStore
;
790 // common part of all ctors
793 // intiialize m_[xy]Scroll
794 void InitScrolling();
796 // get the line data for the given index
797 wxListLineData
*GetLine(size_t n
) const
799 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
803 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
811 // get a dummy line which can be used for geometry calculations and such:
812 // you must use GetLine() if you want to really draw the line
813 wxListLineData
*GetDummyLine() const;
815 // cache the line data of the n-th line in m_lines[0]
816 void CacheLineData(size_t line
);
818 // get the range of visible lines
819 void GetVisibleLinesRange(size_t *from
, size_t *to
);
821 // force us to recalculate the range of visible lines
822 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
824 // get the colour to be used for drawing the rules
825 wxColour
GetRuleColour() const
830 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
835 // initialize the current item if needed
836 void UpdateCurrent();
838 // delete all items but don't refresh: called from dtor
839 void DoDeleteAllItems();
841 // the height of one line using the current font
842 wxCoord m_lineHeight
;
844 // the total header width or 0 if not calculated yet
845 wxCoord m_headerWidth
;
847 // the first and last lines being shown on screen right now (inclusive),
848 // both may be -1 if they must be calculated so never access them directly:
849 // use GetVisibleLinesRange() above instead
853 // the brushes to use for item highlighting when we do/don't have focus
854 wxBrush
*m_highlightBrush
,
855 *m_highlightUnfocusedBrush
;
857 // if this is > 0, the control is frozen and doesn't redraw itself
858 size_t m_freezeCount
;
860 DECLARE_DYNAMIC_CLASS(wxListMainWindow
);
861 DECLARE_EVENT_TABLE()
864 // ============================================================================
866 // ============================================================================
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 bool wxSelectionStore::IsSelected(size_t item
) const
874 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
876 // if the default state is to be selected, being in m_itemsSel means that
877 // the item is not selected, so we have to inverse the logic
878 return m_defaultState
? !isSel
: isSel
;
881 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
883 // search for the item ourselves as like this we get the index where to
884 // insert it later if needed, so we do only one search in the array instead
885 // of two (adding item to a sorted array requires a search)
886 size_t index
= m_itemsSel
.IndexForInsert(item
);
887 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
889 if ( select
!= m_defaultState
)
893 m_itemsSel
.AddAt(item
, index
);
898 else // reset to default state
902 m_itemsSel
.RemoveAt(index
);
910 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
912 wxArrayInt
*itemsChanged
)
914 // 100 is hardcoded but it shouldn't matter much: the important thing is
915 // that we don't refresh everything when really few (e.g. 1 or 2) items
917 static const size_t MANY_ITEMS
= 100;
919 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
921 // are we going to have more [un]selected items than the other ones?
922 if ( itemTo
- itemFrom
> m_count
/2 )
924 if ( select
!= m_defaultState
)
926 // the default state now becomes the same as 'select'
927 m_defaultState
= select
;
929 // so all the old selections (which had state select) shouldn't be
930 // selected any more, but all the other ones should
931 wxIndexArray selOld
= m_itemsSel
;
934 // TODO: it should be possible to optimize the searches a bit
935 // knowing the possible range
938 for ( item
= 0; item
< itemFrom
; item
++ )
940 if ( selOld
.Index(item
) == wxNOT_FOUND
)
941 m_itemsSel
.Add(item
);
944 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
946 if ( selOld
.Index(item
) == wxNOT_FOUND
)
947 m_itemsSel
.Add(item
);
950 // many items (> half) changed state
953 else // select == m_defaultState
955 // get the inclusive range of items between itemFrom and itemTo
956 size_t count
= m_itemsSel
.GetCount(),
957 start
= m_itemsSel
.IndexForInsert(itemFrom
),
958 end
= m_itemsSel
.IndexForInsert(itemTo
);
960 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
965 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
972 // delete all of them (from end to avoid changing indices)
973 for ( int i
= end
; i
>= (int)start
; i
-- )
977 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
979 // stop counting (see comment below)
984 itemsChanged
->Add(m_itemsSel
[i
]);
988 m_itemsSel
.RemoveAt(i
);
993 else // "few" items change state
997 itemsChanged
->Empty();
1000 // just add the items to the selection
1001 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1003 if ( SelectItem(item
, select
) && itemsChanged
)
1005 itemsChanged
->Add(item
);
1007 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1009 // stop counting them, we'll just eat gobs of memory
1010 // for nothing at all - faster to refresh everything in
1012 itemsChanged
= NULL
;
1018 // we set it to NULL if there are many items changing state
1019 return itemsChanged
!= NULL
;
1022 void wxSelectionStore::OnItemDelete(size_t item
)
1024 size_t count
= m_itemsSel
.GetCount(),
1025 i
= m_itemsSel
.IndexForInsert(item
);
1027 if ( i
< count
&& m_itemsSel
[i
] == item
)
1029 // this item itself was in m_itemsSel, remove it from there
1030 m_itemsSel
.RemoveAt(i
);
1035 // and adjust the index of all which follow it
1038 // all following elements must be greater than the one we deleted
1039 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1045 //-----------------------------------------------------------------------------
1047 //-----------------------------------------------------------------------------
1049 wxListItemData::~wxListItemData()
1051 // in the virtual list control the attributes are managed by the main
1052 // program, so don't delete them
1053 if ( !m_owner
->IsVirtual() )
1061 void wxListItemData::Init()
1069 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1075 if ( owner
->InReportView() )
1081 m_rect
= new wxRect
;
1085 void wxListItemData::SetItem( const wxListItem
&info
)
1087 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1088 SetText(info
.m_text
);
1089 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1090 m_image
= info
.m_image
;
1091 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1092 m_data
= info
.m_data
;
1094 if ( info
.HasAttributes() )
1097 *m_attr
= *info
.GetAttributes();
1099 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1107 m_rect
->width
= info
.m_width
;
1111 void wxListItemData::SetPosition( int x
, int y
)
1113 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1119 void wxListItemData::SetSize( int width
, int height
)
1121 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1124 m_rect
->width
= width
;
1126 m_rect
->height
= height
;
1129 bool wxListItemData::IsHit( int x
, int y
) const
1131 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1133 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1136 int wxListItemData::GetX() const
1138 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1143 int wxListItemData::GetY() const
1145 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1150 int wxListItemData::GetWidth() const
1152 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1154 return m_rect
->width
;
1157 int wxListItemData::GetHeight() const
1159 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1161 return m_rect
->height
;
1164 void wxListItemData::GetItem( wxListItem
&info
) const
1166 info
.m_text
= m_text
;
1167 info
.m_image
= m_image
;
1168 info
.m_data
= m_data
;
1172 if ( m_attr
->HasTextColour() )
1173 info
.SetTextColour(m_attr
->GetTextColour());
1174 if ( m_attr
->HasBackgroundColour() )
1175 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1176 if ( m_attr
->HasFont() )
1177 info
.SetFont(m_attr
->GetFont());
1181 //-----------------------------------------------------------------------------
1183 //-----------------------------------------------------------------------------
1185 void wxListHeaderData::Init()
1196 wxListHeaderData::wxListHeaderData()
1201 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1208 void wxListHeaderData::SetItem( const wxListItem
&item
)
1210 m_mask
= item
.m_mask
;
1212 if ( m_mask
& wxLIST_MASK_TEXT
)
1213 m_text
= item
.m_text
;
1215 if ( m_mask
& wxLIST_MASK_IMAGE
)
1216 m_image
= item
.m_image
;
1218 if ( m_mask
& wxLIST_MASK_FORMAT
)
1219 m_format
= item
.m_format
;
1221 if ( m_mask
& wxLIST_MASK_WIDTH
)
1222 SetWidth(item
.m_width
);
1225 void wxListHeaderData::SetPosition( int x
, int y
)
1231 void wxListHeaderData::SetHeight( int h
)
1236 void wxListHeaderData::SetWidth( int w
)
1240 m_width
= WIDTH_COL_DEFAULT
;
1241 else if (m_width
< WIDTH_COL_MIN
)
1242 m_width
= WIDTH_COL_MIN
;
1245 void wxListHeaderData::SetFormat( int format
)
1250 bool wxListHeaderData::HasImage() const
1252 return m_image
!= -1;
1255 bool wxListHeaderData::IsHit( int x
, int y
) const
1257 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1260 void wxListHeaderData::GetItem( wxListItem
& item
)
1262 item
.m_mask
= m_mask
;
1263 item
.m_text
= m_text
;
1264 item
.m_image
= m_image
;
1265 item
.m_format
= m_format
;
1266 item
.m_width
= m_width
;
1269 int wxListHeaderData::GetImage() const
1274 int wxListHeaderData::GetWidth() const
1279 int wxListHeaderData::GetFormat() const
1284 //-----------------------------------------------------------------------------
1286 //-----------------------------------------------------------------------------
1288 inline int wxListLineData::GetMode() const
1290 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1293 inline bool wxListLineData::InReportView() const
1295 return m_owner
->HasFlag(wxLC_REPORT
);
1298 inline bool wxListLineData::IsVirtual() const
1300 return m_owner
->IsVirtual();
1303 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1306 m_items
.DeleteContents( TRUE
);
1308 if ( InReportView() )
1314 m_gi
= new GeometryInfo
;
1317 m_highlighted
= FALSE
;
1319 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1322 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1324 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1325 wxCHECK_RET( node
, _T("no subitems at all??") );
1327 wxListItemData
*item
= node
->GetData();
1329 switch ( GetMode() )
1332 case wxLC_SMALL_ICON
:
1334 m_gi
->m_rectAll
.width
= spacing
;
1336 wxString s
= item
->GetText();
1342 m_gi
->m_rectLabel
.width
=
1343 m_gi
->m_rectLabel
.height
= 0;
1347 dc
->GetTextExtent( s
, &lw
, &lh
);
1348 if (lh
< SCROLL_UNIT_Y
)
1353 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1355 m_gi
->m_rectAll
.width
= lw
;
1357 m_gi
->m_rectLabel
.width
= lw
;
1358 m_gi
->m_rectLabel
.height
= lh
;
1361 if (item
->HasImage())
1364 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1365 m_gi
->m_rectIcon
.width
= w
+ 8;
1366 m_gi
->m_rectIcon
.height
= h
+ 8;
1368 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1369 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1370 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1371 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1374 if ( item
->HasText() )
1376 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1377 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1379 else // no text, highlight the icon
1381 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1382 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1389 wxString s
= item
->GetTextForMeasuring();
1392 dc
->GetTextExtent( s
, &lw
, &lh
);
1393 if (lh
< SCROLL_UNIT_Y
)
1398 m_gi
->m_rectLabel
.width
= lw
;
1399 m_gi
->m_rectLabel
.height
= lh
;
1401 m_gi
->m_rectAll
.width
= lw
;
1402 m_gi
->m_rectAll
.height
= lh
;
1404 if (item
->HasImage())
1407 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1408 m_gi
->m_rectIcon
.width
= w
;
1409 m_gi
->m_rectIcon
.height
= h
;
1411 m_gi
->m_rectAll
.width
+= 4 + w
;
1412 if (h
> m_gi
->m_rectAll
.height
)
1413 m_gi
->m_rectAll
.height
= h
;
1416 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1417 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1422 wxFAIL_MSG( _T("unexpected call to SetSize") );
1426 wxFAIL_MSG( _T("unknown mode") );
1430 void wxListLineData::SetPosition( int x
, int y
,
1434 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1435 wxCHECK_RET( node
, _T("no subitems at all??") );
1437 wxListItemData
*item
= node
->GetData();
1439 switch ( GetMode() )
1442 case wxLC_SMALL_ICON
:
1443 m_gi
->m_rectAll
.x
= x
;
1444 m_gi
->m_rectAll
.y
= y
;
1446 if ( item
->HasImage() )
1448 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4
1449 + (spacing
- m_gi
->m_rectIcon
.width
)/2;
1450 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1453 if ( item
->HasText() )
1455 if (m_gi
->m_rectAll
.width
> spacing
)
1456 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1458 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1459 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1460 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1461 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1463 else // no text, highlight the icon
1465 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1466 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1471 m_gi
->m_rectAll
.x
= x
;
1472 m_gi
->m_rectAll
.y
= y
;
1474 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1475 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1476 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1478 if (item
->HasImage())
1480 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1481 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1482 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1486 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1491 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1495 wxFAIL_MSG( _T("unknown mode") );
1499 void wxListLineData::InitItems( int num
)
1501 for (int i
= 0; i
< num
; i
++)
1502 m_items
.Append( new wxListItemData(m_owner
) );
1505 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1507 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1508 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1510 wxListItemData
*item
= node
->GetData();
1511 item
->SetItem( info
);
1514 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1516 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1519 wxListItemData
*item
= node
->GetData();
1520 item
->GetItem( info
);
1524 wxString
wxListLineData::GetText(int index
) const
1528 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1531 wxListItemData
*item
= node
->GetData();
1532 s
= item
->GetText();
1538 void wxListLineData::SetText( int index
, const wxString s
)
1540 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1543 wxListItemData
*item
= node
->GetData();
1548 void wxListLineData::SetImage( int index
, int image
)
1550 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1551 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1553 wxListItemData
*item
= node
->GetData();
1554 item
->SetImage(image
);
1557 int wxListLineData::GetImage( int index
) const
1559 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1560 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1562 wxListItemData
*item
= node
->GetData();
1563 return item
->GetImage();
1566 wxListItemAttr
*wxListLineData::GetAttr() const
1568 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1569 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1571 wxListItemData
*item
= node
->GetData();
1572 return item
->GetAttr();
1575 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1577 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1578 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1580 wxListItemData
*item
= node
->GetData();
1581 item
->SetAttr(attr
);
1584 bool wxListLineData::SetAttributes(wxDC
*dc
,
1585 const wxListItemAttr
*attr
,
1588 wxWindow
*listctrl
= m_owner
->GetParent();
1592 // don't use foreground colour for drawing highlighted items - this might
1593 // make them completely invisible (and there is no way to do bit
1594 // arithmetics on wxColour, unfortunately)
1598 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1602 if ( attr
&& attr
->HasTextColour() )
1604 colText
= attr
->GetTextColour();
1608 colText
= listctrl
->GetForegroundColour();
1612 dc
->SetTextForeground(colText
);
1616 if ( attr
&& attr
->HasFont() )
1618 font
= attr
->GetFont();
1622 font
= listctrl
->GetFont();
1628 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1629 if ( highlighted
|| hasBgCol
)
1633 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1637 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1640 dc
->SetPen( *wxTRANSPARENT_PEN
);
1648 void wxListLineData::Draw( wxDC
*dc
)
1650 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1651 wxCHECK_RET( node
, _T("no subitems at all??") );
1653 bool highlighted
= IsHighlighted();
1655 wxListItemAttr
*attr
= GetAttr();
1657 if ( SetAttributes(dc
, attr
, highlighted
) )
1659 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1662 wxListItemData
*item
= node
->GetData();
1663 if (item
->HasImage())
1665 wxRect rectIcon
= m_gi
->m_rectIcon
;
1666 m_owner
->DrawImage( item
->GetImage(), dc
,
1667 rectIcon
.x
, rectIcon
.y
);
1670 if (item
->HasText())
1672 wxRect rectLabel
= m_gi
->m_rectLabel
;
1674 wxDCClipper
clipper(*dc
, rectLabel
);
1675 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1679 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1681 const wxRect
& rectHL
,
1684 // TODO: later we should support setting different attributes for
1685 // different columns - to do it, just add "col" argument to
1686 // GetAttr() and move these lines into the loop below
1687 wxListItemAttr
*attr
= GetAttr();
1688 if ( SetAttributes(dc
, attr
, highlighted
) )
1690 dc
->DrawRectangle( rectHL
);
1693 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1694 wxCHECK_RET( node
, _T("no subitems at all??") );
1697 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1698 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1702 wxListItemData
*item
= node
->GetData();
1704 int width
= m_owner
->GetColumnWidth(col
++);
1708 if ( item
->HasImage() )
1711 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1712 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1714 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1720 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1722 if ( item
->HasText() )
1724 dc
->DrawText( item
->GetText(), xOld
, y
);
1727 node
= node
->GetNext();
1731 bool wxListLineData::Highlight( bool on
)
1733 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1735 if ( on
== m_highlighted
)
1743 void wxListLineData::ReverseHighlight( void )
1745 Highlight(!IsHighlighted());
1748 //-----------------------------------------------------------------------------
1749 // wxListHeaderWindow
1750 //-----------------------------------------------------------------------------
1752 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
);
1754 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1755 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1756 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1757 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1760 void wxListHeaderWindow::Init()
1762 m_currentCursor
= (wxCursor
*) NULL
;
1763 m_isDragging
= FALSE
;
1767 wxListHeaderWindow::wxListHeaderWindow()
1771 m_owner
= (wxListMainWindow
*) NULL
;
1772 m_resizeCursor
= (wxCursor
*) NULL
;
1775 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1777 wxListMainWindow
*owner
,
1781 const wxString
&name
)
1782 : wxWindow( win
, id
, pos
, size
, style
, name
)
1787 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1789 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1792 wxListHeaderWindow::~wxListHeaderWindow()
1794 delete m_resizeCursor
;
1797 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1799 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1800 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1801 : GTK_STATE_INSENSITIVE
;
1803 x
= dc
->XLOG2DEV( x
);
1805 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1806 state
, GTK_SHADOW_OUT
,
1807 (GdkRectangle
*) NULL
, m_wxwindow
,
1808 (char *)"button", // const_cast
1809 x
-1, y
-1, w
+2, h
+2);
1810 #elif defined( __WXMAC__ )
1811 const int m_corner
= 1;
1813 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1815 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1816 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1817 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1819 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1822 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1823 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1825 dc
->SetPen( *wxWHITE_PEN
);
1826 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1827 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1828 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1829 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1831 const int m_corner
= 1;
1833 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1835 dc
->SetPen( *wxBLACK_PEN
);
1836 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1837 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1839 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1842 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1843 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1845 dc
->SetPen( *wxWHITE_PEN
);
1846 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1847 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1848 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1849 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1853 // shift the DC origin to match the position of the main window horz
1854 // scrollbar: this allows us to always use logical coords
1855 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1858 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1861 m_owner
->GetViewStart( &x
, NULL
);
1863 // account for the horz scrollbar offset
1864 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1867 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1869 #if defined(__WXGTK__)
1870 wxClientDC
dc( this );
1872 wxPaintDC
dc( this );
1880 dc
.SetFont( GetFont() );
1882 // width and height of the entire header window
1884 GetClientSize( &w
, &h
);
1885 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1887 dc
.SetBackgroundMode(wxTRANSPARENT
);
1889 // do *not* use the listctrl colour for headers - one day we will have a
1890 // function to set it separately
1891 //dc.SetTextForeground( *wxBLACK );
1892 dc
.SetTextForeground(wxSystemSettings::
1893 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1895 int x
= HEADER_OFFSET_X
;
1897 int numColumns
= m_owner
->GetColumnCount();
1899 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1901 m_owner
->GetColumn( i
, item
);
1902 int wCol
= item
.m_width
;
1904 // the width of the rect to draw: make it smaller to fit entirely
1905 // inside the column rect
1908 dc
.SetPen( *wxWHITE_PEN
);
1910 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1912 // if we have an image, draw it on the right of the label
1913 int image
= item
.m_image
;
1916 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1920 imageList
->GetSize(image
, ix
, iy
);
1927 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1928 wxIMAGELIST_DRAW_TRANSPARENT
1933 //else: ignore the column image
1936 // draw the text clipping it so that it doesn't overwrite the column
1938 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1940 dc
.DrawText( item
.GetText(),
1941 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1949 void wxListHeaderWindow::DrawCurrent()
1951 int x1
= m_currentX
;
1953 m_owner
->ClientToScreen( &x1
, &y1
);
1955 int x2
= m_currentX
;
1957 m_owner
->GetClientSize( NULL
, &y2
);
1958 m_owner
->ClientToScreen( &x2
, &y2
);
1961 dc
.SetLogicalFunction( wxINVERT
);
1962 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1963 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1967 dc
.DrawLine( x1
, y1
, x2
, y2
);
1969 dc
.SetLogicalFunction( wxCOPY
);
1971 dc
.SetPen( wxNullPen
);
1972 dc
.SetBrush( wxNullBrush
);
1975 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1977 // we want to work with logical coords
1979 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1980 int y
= event
.GetY();
1984 // we don't draw the line beyond our window, but we allow dragging it
1987 GetClientSize( &w
, NULL
);
1988 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1991 // erase the line if it was drawn
1992 if ( m_currentX
< w
)
1995 if (event
.ButtonUp())
1998 m_isDragging
= FALSE
;
2000 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2007 m_currentX
= m_minX
+ 7;
2009 // draw in the new location
2010 if ( m_currentX
< w
)
2014 else // not dragging
2017 bool hit_border
= FALSE
;
2019 // end of the current column
2022 // find the column where this event occured
2023 int countCol
= m_owner
->GetColumnCount();
2024 for (int col
= 0; col
< countCol
; col
++)
2026 xpos
+= m_owner
->GetColumnWidth( col
);
2029 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2031 // near the column border
2038 // inside the column
2045 if (event
.LeftDown() || event
.RightUp())
2047 if (hit_border
&& event
.LeftDown())
2049 m_isDragging
= TRUE
;
2054 else // click on a column
2056 wxWindow
*parent
= GetParent();
2057 wxListEvent
le( event
.LeftDown()
2058 ? wxEVT_COMMAND_LIST_COL_CLICK
2059 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2061 le
.SetEventObject( parent
);
2062 le
.m_pointDrag
= event
.GetPosition();
2064 // the position should be relative to the parent window, not
2065 // this one for compatibility with MSW and common sense: the
2066 // user code doesn't know anything at all about this header
2067 // window, so why should it get positions relative to it?
2068 le
.m_pointDrag
.y
-= GetSize().y
;
2070 le
.m_col
= m_column
;
2071 parent
->GetEventHandler()->ProcessEvent( le
);
2074 else if (event
.Moving())
2079 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2080 m_currentCursor
= m_resizeCursor
;
2084 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2085 m_currentCursor
= wxSTANDARD_CURSOR
;
2089 SetCursor(*m_currentCursor
);
2094 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2096 m_owner
->SetFocus();
2099 //-----------------------------------------------------------------------------
2100 // wxListRenameTimer (internal)
2101 //-----------------------------------------------------------------------------
2103 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2108 void wxListRenameTimer::Notify()
2110 m_owner
->OnRenameTimer();
2113 //-----------------------------------------------------------------------------
2114 // wxListTextCtrl (internal)
2115 //-----------------------------------------------------------------------------
2117 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
);
2119 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2120 EVT_CHAR (wxListTextCtrl::OnChar
)
2121 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2122 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2125 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
2126 const wxWindowID id
,
2129 wxListMainWindow
*owner
,
2130 const wxString
&value
,
2134 const wxValidator
& validator
,
2135 const wxString
&name
)
2136 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
2141 (*m_accept
) = FALSE
;
2143 m_startValue
= value
;
2147 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2149 if (event
.m_keyCode
== WXK_RETURN
)
2152 (*m_res
) = GetValue();
2154 if (!wxPendingDelete
.Member(this))
2155 wxPendingDelete
.Append(this);
2157 if ((*m_res
) != m_startValue
)
2158 m_owner
->OnRenameAccept();
2161 m_owner
->SetFocus(); // This doesn't work. TODO.
2165 if (event
.m_keyCode
== WXK_ESCAPE
)
2167 (*m_accept
) = FALSE
;
2170 if (!wxPendingDelete
.Member(this))
2171 wxPendingDelete
.Append(this);
2174 m_owner
->SetFocus(); // This doesn't work. TODO.
2182 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2190 // auto-grow the textctrl:
2191 wxSize parentSize
= m_owner
->GetSize();
2192 wxPoint myPos
= GetPosition();
2193 wxSize mySize
= GetSize();
2195 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
); // FIXME: MM??
2196 if (myPos
.x
+ sx
> parentSize
.x
)
2197 sx
= parentSize
.x
- myPos
.x
;
2205 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2213 if (!wxPendingDelete
.Member(this))
2214 wxPendingDelete
.Append(this);
2217 (*m_res
) = GetValue();
2219 if ((*m_res
) != m_startValue
)
2220 m_owner
->OnRenameAccept();
2223 //-----------------------------------------------------------------------------
2225 //-----------------------------------------------------------------------------
2227 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
);
2229 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2230 EVT_PAINT (wxListMainWindow::OnPaint
)
2231 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2232 EVT_CHAR (wxListMainWindow::OnChar
)
2233 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2234 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2235 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2236 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2239 void wxListMainWindow::Init()
2241 m_columns
.DeleteContents( TRUE
);
2245 m_lineTo
= (size_t)-1;
2251 m_small_image_list
= (wxImageList
*) NULL
;
2252 m_normal_image_list
= (wxImageList
*) NULL
;
2254 m_small_spacing
= 30;
2255 m_normal_spacing
= 40;
2259 m_isCreated
= FALSE
;
2261 m_lastOnSame
= FALSE
;
2262 m_renameTimer
= new wxListRenameTimer( this );
2263 m_renameAccept
= FALSE
;
2268 m_lineBeforeLastClicked
= (size_t)-1;
2273 void wxListMainWindow::InitScrolling()
2275 if ( HasFlag(wxLC_REPORT
) )
2277 m_xScroll
= SCROLL_UNIT_X
;
2278 m_yScroll
= SCROLL_UNIT_Y
;
2282 m_xScroll
= SCROLL_UNIT_Y
;
2287 wxListMainWindow::wxListMainWindow()
2292 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2298 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2303 const wxString
&name
)
2304 : wxScrolledWindow( parent
, id
, pos
, size
,
2305 style
| wxHSCROLL
| wxVSCROLL
, name
)
2309 m_highlightBrush
= new wxBrush
2311 wxSystemSettings::GetColour
2313 wxSYS_COLOUR_HIGHLIGHT
2318 m_highlightUnfocusedBrush
= new wxBrush
2320 wxSystemSettings::GetColour
2322 wxSYS_COLOUR_BTNSHADOW
2331 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2333 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2336 wxListMainWindow::~wxListMainWindow()
2340 delete m_highlightBrush
;
2341 delete m_highlightUnfocusedBrush
;
2343 delete m_renameTimer
;
2346 void wxListMainWindow::CacheLineData(size_t line
)
2348 wxListCtrl
*listctrl
= GetListCtrl();
2350 wxListLineData
*ld
= GetDummyLine();
2352 size_t countCol
= GetColumnCount();
2353 for ( size_t col
= 0; col
< countCol
; col
++ )
2355 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2358 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2359 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2362 wxListLineData
*wxListMainWindow::GetDummyLine() const
2364 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2366 if ( m_lines
.IsEmpty() )
2368 // normal controls are supposed to have something in m_lines
2369 // already if it's not empty
2370 wxASSERT_MSG( IsVirtual(), _T("logic error") );
2372 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2373 wxListLineData
*line
= new wxListLineData(self
);
2374 self
->m_lines
.Add(line
);
2380 // ----------------------------------------------------------------------------
2381 // line geometry (report mode only)
2382 // ----------------------------------------------------------------------------
2384 wxCoord
wxListMainWindow::GetLineHeight() const
2386 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2388 // we cache the line height as calling GetTextExtent() is slow
2389 if ( !m_lineHeight
)
2391 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2393 wxClientDC
dc( self
);
2394 dc
.SetFont( GetFont() );
2397 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2399 if ( y
< SCROLL_UNIT_Y
)
2403 self
->m_lineHeight
= y
+ LINE_SPACING
;
2406 return m_lineHeight
;
2409 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2411 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2413 return LINE_SPACING
+ line
*GetLineHeight();
2416 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2418 if ( !InReportView() )
2419 return GetLine(line
)->m_gi
->m_rectAll
;
2422 rect
.x
= HEADER_OFFSET_X
;
2423 rect
.y
= GetLineY(line
);
2424 rect
.width
= GetHeaderWidth();
2425 rect
.height
= GetLineHeight();
2430 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2432 if ( !InReportView() )
2433 return GetLine(line
)->m_gi
->m_rectLabel
;
2436 rect
.x
= HEADER_OFFSET_X
;
2437 rect
.y
= GetLineY(line
);
2438 rect
.width
= GetColumnWidth(0);
2439 rect
.height
= GetLineHeight();
2444 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2446 if ( !InReportView() )
2447 return GetLine(line
)->m_gi
->m_rectIcon
;
2449 wxListLineData
*ld
= GetLine(line
);
2450 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2453 rect
.x
= HEADER_OFFSET_X
;
2454 rect
.y
= GetLineY(line
);
2455 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2460 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2462 return InReportView() ? GetLineRect(line
)
2463 : GetLine(line
)->m_gi
->m_rectHighlight
;
2466 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2468 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2470 wxListLineData
*ld
= GetLine(line
);
2472 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2473 return wxLIST_HITTEST_ONITEMICON
;
2475 if ( ld
->HasText() )
2477 wxRect rect
= InReportView() ? GetLineRect(line
)
2478 : GetLineLabelRect(line
);
2480 if ( rect
.Inside(x
, y
) )
2481 return wxLIST_HITTEST_ONITEMLABEL
;
2487 // ----------------------------------------------------------------------------
2488 // highlight (selection) handling
2489 // ----------------------------------------------------------------------------
2491 bool wxListMainWindow::IsHighlighted(size_t line
) const
2495 return m_selStore
.IsSelected(line
);
2499 wxListLineData
*ld
= GetLine(line
);
2500 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2502 return ld
->IsHighlighted();
2506 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2512 wxArrayInt linesChanged
;
2513 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2516 // meny items changed state, refresh everything
2517 RefreshLines(lineFrom
, lineTo
);
2519 else // only a few items changed state, refresh only them
2521 size_t count
= linesChanged
.GetCount();
2522 for ( size_t n
= 0; n
< count
; n
++ )
2524 RefreshLine(linesChanged
[n
]);
2528 else // iterate over all items in non report view
2530 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2532 if ( HighlightLine(line
, highlight
) )
2540 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2546 changed
= m_selStore
.SelectItem(line
, highlight
);
2550 wxListLineData
*ld
= GetLine(line
);
2551 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2553 changed
= ld
->Highlight(highlight
);
2558 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2559 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2565 void wxListMainWindow::RefreshLine( size_t line
)
2567 if ( HasFlag(wxLC_REPORT
) )
2569 size_t visibleFrom
, visibleTo
;
2570 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2572 if ( line
< visibleFrom
|| line
> visibleTo
)
2576 wxRect rect
= GetLineRect(line
);
2578 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2579 RefreshRect( rect
);
2582 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2584 // we suppose that they are ordered by caller
2585 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2587 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2589 if ( HasFlag(wxLC_REPORT
) )
2591 size_t visibleFrom
, visibleTo
;
2592 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2594 if ( lineFrom
< visibleFrom
)
2595 lineFrom
= visibleFrom
;
2596 if ( lineTo
> visibleTo
)
2601 rect
.y
= GetLineY(lineFrom
);
2602 rect
.width
= GetClientSize().x
;
2603 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2605 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2606 RefreshRect( rect
);
2610 // TODO: this should be optimized...
2611 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2618 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2620 if ( HasFlag(wxLC_REPORT
) )
2623 GetVisibleLinesRange(&visibleFrom
, NULL
);
2625 if ( lineFrom
< visibleFrom
)
2626 lineFrom
= visibleFrom
;
2630 rect
.y
= GetLineY(lineFrom
);
2632 wxSize size
= GetClientSize();
2633 rect
.width
= size
.x
;
2634 // refresh till the bottom of the window
2635 rect
.height
= size
.y
- rect
.y
;
2637 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2638 RefreshRect( rect
);
2642 // TODO: how to do it more efficiently?
2647 void wxListMainWindow::RefreshSelected()
2653 if ( InReportView() )
2655 GetVisibleLinesRange(&from
, &to
);
2660 to
= GetItemCount() - 1;
2663 // VZ: this code would work fine if wxGTK wxWindow::Refresh() were
2664 // reasonable, i.e. if it only generated one expose event for
2665 // several calls to it - as it is, each Refresh() results in a
2666 // repaint which provokes flicker too horrible to be seen
2668 // when/if wxGTK is fixed, this code should be restored as normally it
2669 // should generate _less_ flicker than the version below
2671 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2673 RefreshLine(m_current
);
2676 for ( size_t line
= from
; line
<= to
; line
++ )
2678 // NB: the test works as expected even if m_current == -1
2679 if ( line
!= m_current
&& IsHighlighted(line
) )
2685 size_t selMin
= (size_t)-1,
2688 for ( size_t line
= from
; line
<= to
; line
++ )
2690 if ( IsHighlighted(line
) || (line
== m_current
) )
2692 if ( line
< selMin
)
2694 if ( line
> selMax
)
2699 if ( selMin
!= (size_t)-1 )
2701 RefreshLines(selMin
, selMax
);
2703 #endif // !__WXGTK__/__WXGTK__
2706 void wxListMainWindow::Freeze()
2711 void wxListMainWindow::Thaw()
2713 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2715 if ( !--m_freezeCount
)
2721 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2723 // Note: a wxPaintDC must be constructed even if no drawing is
2724 // done (a Windows requirement).
2725 wxPaintDC
dc( this );
2727 if ( IsEmpty() || m_freezeCount
)
2729 // nothing to draw or not the moment to draw it
2735 // delay the repainting until we calculate all the items positions
2742 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2746 dc
.SetFont( GetFont() );
2748 if ( HasFlag(wxLC_REPORT
) )
2750 int lineHeight
= GetLineHeight();
2752 size_t visibleFrom
, visibleTo
;
2753 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2756 wxCoord xOrig
, yOrig
;
2757 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2759 // tell the caller cache to cache the data
2762 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2763 GetParent()->GetId());
2764 evCache
.SetEventObject( GetParent() );
2765 evCache
.m_oldItemIndex
= visibleFrom
;
2766 evCache
.m_itemIndex
= visibleTo
;
2767 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2770 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2772 rectLine
= GetLineRect(line
);
2774 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2775 rectLine
.width
, rectLine
.height
) )
2777 // don't redraw unaffected lines to avoid flicker
2781 GetLine(line
)->DrawInReportMode( &dc
,
2783 GetLineHighlightRect(line
),
2784 IsHighlighted(line
) );
2787 if ( HasFlag(wxLC_HRULES
) )
2789 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2790 wxSize clientSize
= GetClientSize();
2792 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2795 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2796 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2797 clientSize
.x
- dev_x
, i
*lineHeight
);
2800 // Draw last horizontal rule
2801 if ( visibleTo
> visibleFrom
)
2804 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2805 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2806 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2810 // Draw vertical rules if required
2811 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2813 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2816 wxRect firstItemRect
;
2817 wxRect lastItemRect
;
2818 GetItemRect(0, firstItemRect
);
2819 GetItemRect(GetItemCount() - 1, lastItemRect
);
2820 int x
= firstItemRect
.GetX();
2822 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2823 for (col
= 0; col
< GetColumnCount(); col
++)
2825 int colWidth
= GetColumnWidth(col
);
2827 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2828 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2834 size_t count
= GetItemCount();
2835 for ( size_t i
= 0; i
< count
; i
++ )
2837 GetLine(i
)->Draw( &dc
);
2843 // don't draw rect outline under Max if we already have the background
2844 // color but under other platforms only draw it if we do: it is a bit
2845 // silly to draw "focus rect" if we don't have focus!
2850 #endif // __WXMAC__/!__WXMAC__
2852 dc
.SetPen( *wxBLACK_PEN
);
2853 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2854 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2861 void wxListMainWindow::HighlightAll( bool on
)
2863 if ( IsSingleSel() )
2865 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2867 // we just have one item to turn off
2868 if ( HasCurrent() && IsHighlighted(m_current
) )
2870 HighlightLine(m_current
, FALSE
);
2871 RefreshLine(m_current
);
2876 HighlightLines(0, GetItemCount() - 1, on
);
2880 void wxListMainWindow::SendNotify( size_t line
,
2881 wxEventType command
,
2884 wxListEvent
le( command
, GetParent()->GetId() );
2885 le
.SetEventObject( GetParent() );
2886 le
.m_itemIndex
= line
;
2888 // set only for events which have position
2889 if ( point
!= wxDefaultPosition
)
2890 le
.m_pointDrag
= point
;
2892 // don't try to get the line info for virtual list controls: the main
2893 // program has it anyhow and if we did it would result in accessing all
2894 // the lines, even those which are not visible now and this is precisely
2895 // what we're trying to avoid
2896 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2898 if ( line
!= (size_t)-1 )
2900 GetLine(line
)->GetItem( 0, le
.m_item
);
2902 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2904 //else: there may be no more such item
2906 GetParent()->GetEventHandler()->ProcessEvent( le
);
2909 void wxListMainWindow::ChangeCurrent(size_t current
)
2911 m_current
= current
;
2913 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2916 void wxListMainWindow::EditLabel( long item
)
2918 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2919 wxT("wrong index in wxListCtrl::EditLabel()") );
2921 m_currentEdit
= (size_t)item
;
2923 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2924 le
.SetEventObject( GetParent() );
2925 le
.m_itemIndex
= item
;
2926 wxListLineData
*data
= GetLine(m_currentEdit
);
2927 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2928 data
->GetItem( 0, le
.m_item
);
2929 GetParent()->GetEventHandler()->ProcessEvent( le
);
2931 if (!le
.IsAllowed())
2934 // We have to call this here because the label in question might just have
2935 // been added and no screen update taken place.
2939 wxClientDC
dc(this);
2942 wxString s
= data
->GetText(0);
2943 wxRect rectLabel
= GetLineLabelRect(m_currentEdit
);
2945 rectLabel
.x
= dc
.LogicalToDeviceX( rectLabel
.x
);
2946 rectLabel
.y
= dc
.LogicalToDeviceY( rectLabel
.y
);
2948 wxListTextCtrl
*text
= new wxListTextCtrl
2955 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2956 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2961 void wxListMainWindow::OnRenameTimer()
2963 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2965 EditLabel( m_current
);
2968 void wxListMainWindow::OnRenameAccept()
2970 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2971 le
.SetEventObject( GetParent() );
2972 le
.m_itemIndex
= m_currentEdit
;
2974 wxListLineData
*data
= GetLine(m_currentEdit
);
2975 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2977 data
->GetItem( 0, le
.m_item
);
2978 le
.m_item
.m_text
= m_renameRes
;
2979 GetParent()->GetEventHandler()->ProcessEvent( le
);
2981 if (!le
.IsAllowed()) return;
2984 info
.m_mask
= wxLIST_MASK_TEXT
;
2985 info
.m_itemId
= le
.m_itemIndex
;
2986 info
.m_text
= m_renameRes
;
2987 info
.SetTextColour(le
.m_item
.GetTextColour());
2991 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2993 event
.SetEventObject( GetParent() );
2994 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2997 if ( !HasCurrent() || IsEmpty() )
3003 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3004 event
.ButtonDClick()) )
3007 int x
= event
.GetX();
3008 int y
= event
.GetY();
3009 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3011 // where did we hit it (if we did)?
3014 size_t count
= GetItemCount(),
3017 if ( HasFlag(wxLC_REPORT
) )
3019 current
= y
/ GetLineHeight();
3020 if ( current
< count
)
3021 hitResult
= HitTestLine(current
, x
, y
);
3025 // TODO: optimize it too! this is less simple than for report view but
3026 // enumerating all items is still not a way to do it!!
3027 for ( current
= 0; current
< count
; current
++ )
3029 hitResult
= HitTestLine(current
, x
, y
);
3035 if (event
.Dragging())
3037 if (m_dragCount
== 0)
3039 // we have to report the raw, physical coords as we want to be
3040 // able to call HitTest(event.m_pointDrag) from the user code to
3041 // get the item being dragged
3042 m_dragStart
= event
.GetPosition();
3047 if (m_dragCount
!= 3)
3050 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3051 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3053 wxListEvent
le( command
, GetParent()->GetId() );
3054 le
.SetEventObject( GetParent() );
3055 le
.m_pointDrag
= m_dragStart
;
3056 GetParent()->GetEventHandler()->ProcessEvent( le
);
3067 // outside of any item
3071 bool forceClick
= FALSE
;
3072 if (event
.ButtonDClick())
3074 m_renameTimer
->Stop();
3075 m_lastOnSame
= FALSE
;
3077 if ( current
== m_lineBeforeLastClicked
)
3079 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3085 // the first click was on another item, so don't interpret this as
3086 // a double click, but as a simple click instead
3091 if (event
.LeftUp() && m_lastOnSame
)
3093 if ((current
== m_current
) &&
3094 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3095 HasFlag(wxLC_EDIT_LABELS
) )
3097 m_renameTimer
->Start( 100, TRUE
);
3099 m_lastOnSame
= FALSE
;
3101 else if (event
.RightDown())
3103 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3104 event
.GetPosition() );
3106 else if (event
.MiddleDown())
3108 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3110 else if ( event
.LeftDown() || forceClick
)
3112 m_lineBeforeLastClicked
= m_lineLastClicked
;
3113 m_lineLastClicked
= current
;
3115 size_t oldCurrent
= m_current
;
3117 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3119 HighlightAll( FALSE
);
3121 ChangeCurrent(current
);
3123 ReverseHighlight(m_current
);
3125 else // multi sel & either ctrl or shift is down
3127 if (event
.ControlDown())
3129 ChangeCurrent(current
);
3131 ReverseHighlight(m_current
);
3133 else if (event
.ShiftDown())
3135 ChangeCurrent(current
);
3137 size_t lineFrom
= oldCurrent
,
3140 if ( lineTo
< lineFrom
)
3143 lineFrom
= m_current
;
3146 HighlightLines(lineFrom
, lineTo
);
3148 else // !ctrl, !shift
3150 // test in the enclosing if should make it impossible
3151 wxFAIL_MSG( _T("how did we get here?") );
3155 if (m_current
!= oldCurrent
)
3157 RefreshLine( oldCurrent
);
3160 // forceClick is only set if the previous click was on another item
3161 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3165 void wxListMainWindow::MoveToItem(size_t item
)
3167 if ( item
== (size_t)-1 )
3170 wxRect rect
= GetLineRect(item
);
3172 int client_w
, client_h
;
3173 GetClientSize( &client_w
, &client_h
);
3175 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3176 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3178 if ( HasFlag(wxLC_REPORT
) )
3180 // the next we need the range of lines shown it might be different, so
3182 ResetVisibleLinesRange();
3184 if (rect
.y
< view_y
)
3185 Scroll( -1, rect
.y
/m_yScroll
);
3186 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3187 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3191 if (rect
.x
-view_x
< 5)
3192 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3193 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3194 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3198 // ----------------------------------------------------------------------------
3199 // keyboard handling
3200 // ----------------------------------------------------------------------------
3202 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3204 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3205 _T("invalid item index in OnArrowChar()") );
3207 size_t oldCurrent
= m_current
;
3209 // in single selection we just ignore Shift as we can't select several
3211 if ( event
.ShiftDown() && !IsSingleSel() )
3213 ChangeCurrent(newCurrent
);
3215 // select all the items between the old and the new one
3216 if ( oldCurrent
> newCurrent
)
3218 newCurrent
= oldCurrent
;
3219 oldCurrent
= m_current
;
3222 HighlightLines(oldCurrent
, newCurrent
);
3226 // all previously selected items are unselected unless ctrl is held
3227 if ( !event
.ControlDown() )
3228 HighlightAll(FALSE
);
3230 ChangeCurrent(newCurrent
);
3232 HighlightLine( oldCurrent
, FALSE
);
3233 RefreshLine( oldCurrent
);
3235 if ( !event
.ControlDown() )
3237 HighlightLine( m_current
, TRUE
);
3241 RefreshLine( m_current
);
3246 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3248 wxWindow
*parent
= GetParent();
3250 /* we propagate the key event up */
3251 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3252 ke
.m_shiftDown
= event
.m_shiftDown
;
3253 ke
.m_controlDown
= event
.m_controlDown
;
3254 ke
.m_altDown
= event
.m_altDown
;
3255 ke
.m_metaDown
= event
.m_metaDown
;
3256 ke
.m_keyCode
= event
.m_keyCode
;
3259 ke
.SetEventObject( parent
);
3260 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3265 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3267 wxWindow
*parent
= GetParent();
3269 /* we send a list_key event up */
3272 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3273 le
.m_itemIndex
= m_current
;
3274 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3275 le
.m_code
= (int)event
.KeyCode();
3276 le
.SetEventObject( parent
);
3277 parent
->GetEventHandler()->ProcessEvent( le
);
3280 /* we propagate the char event up */
3281 wxKeyEvent
ke( wxEVT_CHAR
);
3282 ke
.m_shiftDown
= event
.m_shiftDown
;
3283 ke
.m_controlDown
= event
.m_controlDown
;
3284 ke
.m_altDown
= event
.m_altDown
;
3285 ke
.m_metaDown
= event
.m_metaDown
;
3286 ke
.m_keyCode
= event
.m_keyCode
;
3289 ke
.SetEventObject( parent
);
3290 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3292 if (event
.KeyCode() == WXK_TAB
)
3294 wxNavigationKeyEvent nevent
;
3295 nevent
.SetWindowChange( event
.ControlDown() );
3296 nevent
.SetDirection( !event
.ShiftDown() );
3297 nevent
.SetEventObject( GetParent()->GetParent() );
3298 nevent
.SetCurrentFocus( m_parent
);
3299 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3303 /* no item -> nothing to do */
3310 switch (event
.KeyCode())
3313 if ( m_current
> 0 )
3314 OnArrowChar( m_current
- 1, event
);
3318 if ( m_current
< (size_t)GetItemCount() - 1 )
3319 OnArrowChar( m_current
+ 1, event
);
3324 OnArrowChar( GetItemCount() - 1, event
);
3329 OnArrowChar( 0, event
);
3335 if ( HasFlag(wxLC_REPORT
) )
3337 steps
= m_linesPerPage
- 1;
3341 steps
= m_current
% m_linesPerPage
;
3344 int index
= m_current
- steps
;
3348 OnArrowChar( index
, event
);
3355 if ( HasFlag(wxLC_REPORT
) )
3357 steps
= m_linesPerPage
- 1;
3361 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3364 size_t index
= m_current
+ steps
;
3365 size_t count
= GetItemCount();
3366 if ( index
>= count
)
3369 OnArrowChar( index
, event
);
3374 if ( !HasFlag(wxLC_REPORT
) )
3376 int index
= m_current
- m_linesPerPage
;
3380 OnArrowChar( index
, event
);
3385 if ( !HasFlag(wxLC_REPORT
) )
3387 size_t index
= m_current
+ m_linesPerPage
;
3389 size_t count
= GetItemCount();
3390 if ( index
>= count
)
3393 OnArrowChar( index
, event
);
3398 if ( IsSingleSel() )
3400 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3402 if ( IsHighlighted(m_current
) )
3404 // don't unselect the item in single selection mode
3407 //else: select it in ReverseHighlight() below if unselected
3410 ReverseHighlight(m_current
);
3415 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3423 // ----------------------------------------------------------------------------
3425 // ----------------------------------------------------------------------------
3428 extern wxWindow
*g_focusWindow
;
3431 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3433 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3434 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3435 // which are already drawn correctly resulting in horrible flicker - avoid
3448 g_focusWindow
= GetParent();
3451 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3452 event
.SetEventObject( GetParent() );
3453 GetParent()->GetEventHandler()->ProcessEvent( event
);
3456 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3463 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3465 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3467 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3469 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3471 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3473 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3475 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3477 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3479 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3483 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3485 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3487 m_normal_image_list
->GetSize( index
, width
, height
);
3489 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3491 m_small_image_list
->GetSize( index
, width
, height
);
3493 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3495 m_small_image_list
->GetSize( index
, width
, height
);
3497 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3499 m_small_image_list
->GetSize( index
, width
, height
);
3508 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3510 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3511 dc
.SetFont( GetFont() );
3514 dc
.GetTextExtent( s
, &lw
, NULL
);
3516 return lw
+ AUTOSIZE_COL_MARGIN
;
3519 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3523 // calc the spacing from the icon size
3526 if ((imageList
) && (imageList
->GetImageCount()) )
3528 imageList
->GetSize(0, width
, height
);
3531 if (which
== wxIMAGE_LIST_NORMAL
)
3533 m_normal_image_list
= imageList
;
3534 m_normal_spacing
= width
+ 8;
3537 if (which
== wxIMAGE_LIST_SMALL
)
3539 m_small_image_list
= imageList
;
3540 m_small_spacing
= width
+ 14;
3544 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3549 m_small_spacing
= spacing
;
3553 m_normal_spacing
= spacing
;
3557 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3559 return isSmall
? m_small_spacing
: m_normal_spacing
;
3562 // ----------------------------------------------------------------------------
3564 // ----------------------------------------------------------------------------
3566 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3568 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3570 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3572 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3573 item
.m_width
= GetTextLength( item
.m_text
);
3575 wxListHeaderData
*column
= node
->GetData();
3576 column
->SetItem( item
);
3578 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3580 headerWin
->m_dirty
= TRUE
;
3584 // invalidate it as it has to be recalculated
3588 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3590 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3591 _T("invalid column index") );
3593 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3594 _T("SetColumnWidth() can only be called in report mode.") );
3597 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3599 headerWin
->m_dirty
= TRUE
;
3601 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3602 wxCHECK_RET( node
, _T("no column?") );
3604 wxListHeaderData
*column
= node
->GetData();
3606 size_t count
= GetItemCount();
3608 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3610 width
= GetTextLength(column
->GetText());
3612 else if ( width
== wxLIST_AUTOSIZE
)
3616 // TODO: determine the max width somehow...
3617 width
= WIDTH_COL_DEFAULT
;
3621 wxClientDC
dc(this);
3622 dc
.SetFont( GetFont() );
3624 int max
= AUTOSIZE_COL_MARGIN
;
3626 for ( size_t i
= 0; i
< count
; i
++ )
3628 wxListLineData
*line
= GetLine(i
);
3629 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3631 wxCHECK_RET( n
, _T("no subitem?") );
3633 wxListItemData
*item
= n
->GetData();
3636 if (item
->HasImage())
3639 GetImageSize( item
->GetImage(), ix
, iy
);
3643 if (item
->HasText())
3646 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3654 width
= max
+ AUTOSIZE_COL_MARGIN
;
3658 column
->SetWidth( width
);
3660 // invalidate it as it has to be recalculated
3664 int wxListMainWindow::GetHeaderWidth() const
3666 if ( !m_headerWidth
)
3668 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3670 size_t count
= GetColumnCount();
3671 for ( size_t col
= 0; col
< count
; col
++ )
3673 self
->m_headerWidth
+= GetColumnWidth(col
);
3677 return m_headerWidth
;
3680 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3682 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3683 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3685 wxListHeaderData
*column
= node
->GetData();
3686 column
->GetItem( item
);
3689 int wxListMainWindow::GetColumnWidth( int col
) const
3691 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3692 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3694 wxListHeaderData
*column
= node
->GetData();
3695 return column
->GetWidth();
3698 // ----------------------------------------------------------------------------
3700 // ----------------------------------------------------------------------------
3702 void wxListMainWindow::SetItem( wxListItem
&item
)
3704 long id
= item
.m_itemId
;
3705 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3706 _T("invalid item index in SetItem") );
3710 wxListLineData
*line
= GetLine((size_t)id
);
3711 line
->SetItem( item
.m_col
, item
);
3714 if ( InReportView() )
3716 // just refresh the line to show the new value of the text/image
3717 RefreshLine((size_t)id
);
3721 // refresh everything (resulting in horrible flicker - FIXME!)
3726 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3728 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3729 _T("invalid list ctrl item index in SetItem") );
3731 size_t oldCurrent
= m_current
;
3732 size_t item
= (size_t)litem
; // safe because of the check above
3734 // do we need to change the focus?
3735 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3737 if ( state
& wxLIST_STATE_FOCUSED
)
3739 // don't do anything if this item is already focused
3740 if ( item
!= m_current
)
3742 ChangeCurrent(item
);
3744 if ( oldCurrent
!= (size_t)-1 )
3746 if ( IsSingleSel() )
3748 HighlightLine(oldCurrent
, FALSE
);
3751 RefreshLine(oldCurrent
);
3754 RefreshLine( m_current
);
3759 // don't do anything if this item is not focused
3760 if ( item
== m_current
)
3764 RefreshLine( oldCurrent
);
3769 // do we need to change the selection state?
3770 if ( stateMask
& wxLIST_STATE_SELECTED
)
3772 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3774 if ( IsSingleSel() )
3778 // selecting the item also makes it the focused one in the
3780 if ( m_current
!= item
)
3782 ChangeCurrent(item
);
3784 if ( oldCurrent
!= (size_t)-1 )
3786 HighlightLine( oldCurrent
, FALSE
);
3787 RefreshLine( oldCurrent
);
3793 // only the current item may be selected anyhow
3794 if ( item
!= m_current
)
3799 if ( HighlightLine(item
, on
) )
3806 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3808 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3809 _T("invalid list ctrl item index in GetItemState()") );
3811 int ret
= wxLIST_STATE_DONTCARE
;
3813 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3815 if ( (size_t)item
== m_current
)
3816 ret
|= wxLIST_STATE_FOCUSED
;
3819 if ( stateMask
& wxLIST_STATE_SELECTED
)
3821 if ( IsHighlighted(item
) )
3822 ret
|= wxLIST_STATE_SELECTED
;
3828 void wxListMainWindow::GetItem( wxListItem
&item
)
3830 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3831 _T("invalid item index in GetItem") );
3833 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3834 line
->GetItem( item
.m_col
, item
);
3837 // ----------------------------------------------------------------------------
3839 // ----------------------------------------------------------------------------
3841 size_t wxListMainWindow::GetItemCount() const
3843 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3846 void wxListMainWindow::SetItemCount(long count
)
3848 m_selStore
.SetItemCount(count
);
3849 m_countVirt
= count
;
3851 ResetVisibleLinesRange();
3853 // scrollbars must be reset
3857 int wxListMainWindow::GetSelectedItemCount()
3859 // deal with the quick case first
3860 if ( IsSingleSel() )
3862 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3865 // virtual controls remmebers all its selections itself
3867 return m_selStore
.GetSelectedCount();
3869 // TODO: we probably should maintain the number of items selected even for
3870 // non virtual controls as enumerating all lines is really slow...
3871 size_t countSel
= 0;
3872 size_t count
= GetItemCount();
3873 for ( size_t line
= 0; line
< count
; line
++ )
3875 if ( GetLine(line
)->IsHighlighted() )
3882 // ----------------------------------------------------------------------------
3883 // item position/size
3884 // ----------------------------------------------------------------------------
3886 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3888 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3889 _T("invalid index in GetItemRect") );
3891 rect
= GetLineRect((size_t)index
);
3893 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3896 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3899 GetItemRect(item
, rect
);
3907 // ----------------------------------------------------------------------------
3908 // geometry calculation
3909 // ----------------------------------------------------------------------------
3911 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3913 wxClientDC
dc( this );
3914 dc
.SetFont( GetFont() );
3917 if ( HasFlag(wxLC_ICON
) )
3918 iconSpacing
= m_normal_spacing
;
3919 else if ( HasFlag(wxLC_SMALL_ICON
) )
3920 iconSpacing
= m_small_spacing
;
3924 // Note that we do not call GetClientSize() here but
3925 // GetSize() and substract the border size for sunken
3926 // borders manually. This is technically incorrect,
3927 // but we need to know the client area's size WITHOUT
3928 // scrollbars here. Since we don't know if there are
3929 // any scrollbars, we use GetSize() instead. Another
3930 // solution would be to call SetScrollbars() here to
3931 // remove the scrollbars and call GetClientSize() then,
3932 // but this might result in flicker and - worse - will
3933 // reset the scrollbars to 0 which is not good at all
3934 // if you resize a dialog/window, but don't want to
3935 // reset the window scrolling. RR.
3936 // Furthermore, we actually do NOT subtract the border
3937 // width as 2 pixels is just the extra space which we
3938 // need around the actual content in the window. Other-
3939 // wise the text would e.g. touch the upper border. RR.
3942 GetSize( &clientWidth
, &clientHeight
);
3944 if ( HasFlag(wxLC_REPORT
) )
3946 // all lines have the same height
3947 int lineHeight
= GetLineHeight();
3949 // scroll one line per step
3950 m_yScroll
= lineHeight
;
3952 size_t lineCount
= GetItemCount();
3953 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3955 m_linesPerPage
= clientHeight
/ lineHeight
;
3957 ResetVisibleLinesRange();
3959 SetScrollbars( m_xScroll
, m_yScroll
,
3960 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3961 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3962 GetScrollPos(wxHORIZONTAL
),
3963 GetScrollPos(wxVERTICAL
),
3968 // at first we try without any scrollbar. if the items don't
3969 // fit into the window, we recalculate after subtracting an
3970 // approximated 15 pt for the horizontal scrollbar
3972 int entireWidth
= 0;
3974 for (int tries
= 0; tries
< 2; tries
++)
3976 // We start with 4 for the border around all items
3981 // Now we have decided that the items do not fit into the
3982 // client area. Unfortunately, wxWindows sometimes thinks
3983 // that it does fit and therefore NO horizontal scrollbar
3984 // is inserted. This looks ugly, so we fudge here and make
3985 // the calculated width bigger than was actually has been
3986 // calculated. This ensures that wxScrolledWindows puts
3987 // a scrollbar at the bottom of its client area.
3988 entireWidth
+= SCROLL_UNIT_X
;
3991 // Start at 2,2 so the text does not touch the border
3996 int currentlyVisibleLines
= 0;
3998 size_t count
= GetItemCount();
3999 for (size_t i
= 0; i
< count
; i
++)
4001 currentlyVisibleLines
++;
4002 wxListLineData
*line
= GetLine(i
);
4003 line
->CalculateSize( &dc
, iconSpacing
);
4004 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4006 wxSize sizeLine
= GetLineSize(i
);
4008 if ( maxWidth
< sizeLine
.x
)
4009 maxWidth
= sizeLine
.x
;
4012 if (currentlyVisibleLines
> m_linesPerPage
)
4013 m_linesPerPage
= currentlyVisibleLines
;
4015 // Assume that the size of the next one is the same... (FIXME)
4016 if ( y
+ sizeLine
.y
>= clientHeight
)
4018 currentlyVisibleLines
= 0;
4021 entireWidth
+= maxWidth
+6;
4025 // We have reached the last item.
4026 if ( i
== count
- 1 )
4027 entireWidth
+= maxWidth
;
4029 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4031 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4033 currentlyVisibleLines
= 0;
4037 if ( i
== count
- 1 )
4038 tries
= 1; // Everything fits, no second try required.
4042 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4043 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4048 // FIXME: why should we call it from here?
4055 void wxListMainWindow::RefreshAll()
4060 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4061 if ( headerWin
&& headerWin
->m_dirty
)
4063 headerWin
->m_dirty
= FALSE
;
4064 headerWin
->Refresh();
4068 void wxListMainWindow::UpdateCurrent()
4070 if ( !HasCurrent() && !IsEmpty() )
4076 long wxListMainWindow::GetNextItem( long item
,
4077 int WXUNUSED(geometry
),
4081 max
= GetItemCount();
4082 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4083 _T("invalid listctrl index in GetNextItem()") );
4085 // notice that we start with the next item (or the first one if item == -1)
4086 // and this is intentional to allow writing a simple loop to iterate over
4087 // all selected items
4091 // this is not an error because the index was ok initially, just no
4102 size_t count
= GetItemCount();
4103 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4105 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4108 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4115 // ----------------------------------------------------------------------------
4117 // ----------------------------------------------------------------------------
4119 void wxListMainWindow::DeleteItem( long lindex
)
4121 size_t count
= GetItemCount();
4123 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4124 _T("invalid item index in DeleteItem") );
4126 size_t index
= (size_t)lindex
;
4128 // we don't need to adjust the index for the previous items
4129 if ( HasCurrent() && m_current
>= index
)
4131 // if the current item is being deleted, we want the next one to
4132 // become selected - unless there is no next one - so don't adjust
4133 // m_current in this case
4134 if ( m_current
!= index
|| m_current
== count
- 1 )
4140 if ( InReportView() )
4142 ResetVisibleLinesRange();
4149 m_selStore
.OnItemDelete(index
);
4153 m_lines
.RemoveAt( index
);
4156 // we need to refresh the (vert) scrollbar as the number of items changed
4159 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4161 RefreshAfter(index
);
4164 void wxListMainWindow::DeleteColumn( int col
)
4166 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4168 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4171 m_columns
.DeleteNode( node
);
4174 void wxListMainWindow::DoDeleteAllItems()
4178 // nothing to do - in particular, don't send the event
4184 // to make the deletion of all items faster, we don't send the
4185 // notifications for each item deletion in this case but only one event
4186 // for all of them: this is compatible with wxMSW and documented in
4187 // DeleteAllItems() description
4189 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4190 event
.SetEventObject( GetParent() );
4191 GetParent()->GetEventHandler()->ProcessEvent( event
);
4200 if ( InReportView() )
4202 ResetVisibleLinesRange();
4208 void wxListMainWindow::DeleteAllItems()
4212 RecalculatePositions();
4215 void wxListMainWindow::DeleteEverything()
4222 // ----------------------------------------------------------------------------
4223 // scanning for an item
4224 // ----------------------------------------------------------------------------
4226 void wxListMainWindow::EnsureVisible( long index
)
4228 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4229 _T("invalid index in EnsureVisible") );
4231 // We have to call this here because the label in question might just have
4232 // been added and its position is not known yet
4235 RecalculatePositions(TRUE
/* no refresh */);
4238 MoveToItem((size_t)index
);
4241 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4248 size_t count
= GetItemCount();
4249 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4251 wxListLineData
*line
= GetLine(i
);
4252 if ( line
->GetText(0) == tmp
)
4259 long wxListMainWindow::FindItem(long start
, long data
)
4265 size_t count
= GetItemCount();
4266 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4268 wxListLineData
*line
= GetLine(i
);
4270 line
->GetItem( 0, item
);
4271 if (item
.m_data
== data
)
4278 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4280 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4282 size_t count
= GetItemCount();
4284 if ( HasFlag(wxLC_REPORT
) )
4286 size_t current
= y
/ GetLineHeight();
4287 if ( current
< count
)
4289 flags
= HitTestLine(current
, x
, y
);
4296 // TODO: optimize it too! this is less simple than for report view but
4297 // enumerating all items is still not a way to do it!!
4298 for ( size_t current
= 0; current
< count
; current
++ )
4300 flags
= HitTestLine(current
, x
, y
);
4309 // ----------------------------------------------------------------------------
4311 // ----------------------------------------------------------------------------
4313 void wxListMainWindow::InsertItem( wxListItem
&item
)
4315 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4317 size_t count
= GetItemCount();
4318 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4319 _T("invalid item index") );
4321 size_t id
= item
.m_itemId
;
4326 if ( HasFlag(wxLC_REPORT
) )
4328 else if ( HasFlag(wxLC_LIST
) )
4330 else if ( HasFlag(wxLC_ICON
) )
4332 else if ( HasFlag(wxLC_SMALL_ICON
) )
4333 mode
= wxLC_ICON
; // no typo
4336 wxFAIL_MSG( _T("unknown mode") );
4339 wxListLineData
*line
= new wxListLineData(this);
4341 line
->SetItem( 0, item
);
4343 m_lines
.Insert( line
, id
);
4346 RefreshLines(id
, GetItemCount() - 1);
4349 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4352 if ( HasFlag(wxLC_REPORT
) )
4354 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4355 item
.m_width
= GetTextLength( item
.m_text
);
4356 wxListHeaderData
*column
= new wxListHeaderData( item
);
4357 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4359 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4360 m_columns
.Insert( node
, column
);
4364 m_columns
.Append( column
);
4369 // ----------------------------------------------------------------------------
4371 // ----------------------------------------------------------------------------
4373 wxListCtrlCompare list_ctrl_compare_func_2
;
4374 long list_ctrl_compare_data
;
4376 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4378 wxListLineData
*line1
= *arg1
;
4379 wxListLineData
*line2
= *arg2
;
4381 line1
->GetItem( 0, item
);
4382 long data1
= item
.m_data
;
4383 line2
->GetItem( 0, item
);
4384 long data2
= item
.m_data
;
4385 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4388 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4390 list_ctrl_compare_func_2
= fn
;
4391 list_ctrl_compare_data
= data
;
4392 m_lines
.Sort( list_ctrl_compare_func_1
);
4396 // ----------------------------------------------------------------------------
4398 // ----------------------------------------------------------------------------
4400 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4402 // update our idea of which lines are shown when we redraw the window the
4404 ResetVisibleLinesRange();
4407 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4408 wxScrolledWindow::OnScroll(event
);
4410 HandleOnScroll( event
);
4413 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4415 wxListCtrl
* lc
= GetListCtrl();
4416 wxCHECK_RET( lc
, _T("no listctrl window?") );
4418 lc
->m_headerWin
->Refresh() ;
4420 lc
->m_headerWin
->MacUpdateImmediately() ;
4425 int wxListMainWindow::GetCountPerPage() const
4427 if ( !m_linesPerPage
)
4429 wxConstCast(this, wxListMainWindow
)->
4430 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4433 return m_linesPerPage
;
4436 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4438 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4440 if ( m_lineFrom
== (size_t)-1 )
4442 size_t count
= GetItemCount();
4445 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4447 // this may happen if SetScrollbars() hadn't been called yet
4448 if ( m_lineFrom
>= count
)
4449 m_lineFrom
= count
- 1;
4451 // we redraw one extra line but this is needed to make the redrawing
4452 // logic work when there is a fractional number of lines on screen
4453 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4454 if ( m_lineTo
>= count
)
4455 m_lineTo
= count
- 1;
4457 else // empty control
4460 m_lineTo
= (size_t)-1;
4464 wxASSERT_MSG( IsEmpty() ||
4465 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4466 _T("GetVisibleLinesRange() returns incorrect result") );
4474 // -------------------------------------------------------------------------------------
4476 // -------------------------------------------------------------------------------------
4478 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4480 wxListItem::wxListItem()
4487 void wxListItem::Clear()
4496 m_format
= wxLIST_FORMAT_CENTRE
;
4503 void wxListItem::ClearAttributes()
4512 // -------------------------------------------------------------------------------------
4514 // -------------------------------------------------------------------------------------
4516 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4517 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4519 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4521 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4522 EVT_SIZE(wxListCtrl::OnSize
)
4523 EVT_IDLE(wxListCtrl::OnIdle
)
4526 wxListCtrl::wxListCtrl()
4528 m_imageListNormal
= (wxImageList
*) NULL
;
4529 m_imageListSmall
= (wxImageList
*) NULL
;
4530 m_imageListState
= (wxImageList
*) NULL
;
4532 m_ownsImageListNormal
=
4533 m_ownsImageListSmall
=
4534 m_ownsImageListState
= FALSE
;
4536 m_mainWin
= (wxListMainWindow
*) NULL
;
4537 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4540 wxListCtrl::~wxListCtrl()
4542 if (m_ownsImageListNormal
)
4543 delete m_imageListNormal
;
4544 if (m_ownsImageListSmall
)
4545 delete m_imageListSmall
;
4546 if (m_ownsImageListState
)
4547 delete m_imageListState
;
4550 void wxListCtrl::CreateHeaderWindow()
4552 m_headerWin
= new wxListHeaderWindow
4554 this, -1, m_mainWin
,
4556 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4561 bool wxListCtrl::Create(wxWindow
*parent
,
4566 const wxValidator
&validator
,
4567 const wxString
&name
)
4571 m_imageListState
= (wxImageList
*) NULL
;
4572 m_ownsImageListNormal
=
4573 m_ownsImageListSmall
=
4574 m_ownsImageListState
= FALSE
;
4576 m_mainWin
= (wxListMainWindow
*) NULL
;
4577 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4579 if ( !(style
& wxLC_MASK_TYPE
) )
4581 style
= style
| wxLC_LIST
;
4584 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4587 // don't create the inner window with the border
4588 style
&= ~wxSUNKEN_BORDER
;
4590 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4592 if ( HasFlag(wxLC_REPORT
) )
4594 CreateHeaderWindow();
4596 if ( HasFlag(wxLC_NO_HEADER
) )
4598 // VZ: why do we create it at all then?
4599 m_headerWin
->Show( FALSE
);
4606 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4608 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4609 _T("wxLC_VIRTUAL can't be [un]set") );
4611 long flag
= GetWindowStyle();
4615 if (style
& wxLC_MASK_TYPE
)
4616 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4617 if (style
& wxLC_MASK_ALIGN
)
4618 flag
&= ~wxLC_MASK_ALIGN
;
4619 if (style
& wxLC_MASK_SORT
)
4620 flag
&= ~wxLC_MASK_SORT
;
4632 SetWindowStyleFlag( flag
);
4635 void wxListCtrl::SetWindowStyleFlag( long flag
)
4639 m_mainWin
->DeleteEverything();
4641 // has the header visibility changed?
4642 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4643 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4645 if ( hasHeader
!= willHaveHeader
)
4652 // don't delete, just hide, as we can reuse it later
4653 m_headerWin
->Show(FALSE
);
4655 //else: nothing to do
4657 else // must show header
4661 CreateHeaderWindow();
4663 else // already have it, just show
4665 m_headerWin
->Show( TRUE
);
4669 ResizeReportView(willHaveHeader
);
4673 wxWindow::SetWindowStyleFlag( flag
);
4676 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4678 m_mainWin
->GetColumn( col
, item
);
4682 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4684 m_mainWin
->SetColumn( col
, item
);
4688 int wxListCtrl::GetColumnWidth( int col
) const
4690 return m_mainWin
->GetColumnWidth( col
);
4693 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4695 m_mainWin
->SetColumnWidth( col
, width
);
4699 int wxListCtrl::GetCountPerPage() const
4701 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4704 bool wxListCtrl::GetItem( wxListItem
&info
) const
4706 m_mainWin
->GetItem( info
);
4710 bool wxListCtrl::SetItem( wxListItem
&info
)
4712 m_mainWin
->SetItem( info
);
4716 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4719 info
.m_text
= label
;
4720 info
.m_mask
= wxLIST_MASK_TEXT
;
4721 info
.m_itemId
= index
;
4725 info
.m_image
= imageId
;
4726 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4728 m_mainWin
->SetItem(info
);
4732 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4734 return m_mainWin
->GetItemState( item
, stateMask
);
4737 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4739 m_mainWin
->SetItemState( item
, state
, stateMask
);
4743 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4746 info
.m_image
= image
;
4747 info
.m_mask
= wxLIST_MASK_IMAGE
;
4748 info
.m_itemId
= item
;
4749 m_mainWin
->SetItem( info
);
4753 wxString
wxListCtrl::GetItemText( long item
) const
4756 info
.m_itemId
= item
;
4757 m_mainWin
->GetItem( info
);
4761 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4764 info
.m_mask
= wxLIST_MASK_TEXT
;
4765 info
.m_itemId
= item
;
4767 m_mainWin
->SetItem( info
);
4770 long wxListCtrl::GetItemData( long item
) const
4773 info
.m_itemId
= item
;
4774 m_mainWin
->GetItem( info
);
4778 bool wxListCtrl::SetItemData( long item
, long data
)
4781 info
.m_mask
= wxLIST_MASK_DATA
;
4782 info
.m_itemId
= item
;
4784 m_mainWin
->SetItem( info
);
4788 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4790 m_mainWin
->GetItemRect( item
, rect
);
4794 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4796 m_mainWin
->GetItemPosition( item
, pos
);
4800 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4805 int wxListCtrl::GetItemCount() const
4807 return m_mainWin
->GetItemCount();
4810 int wxListCtrl::GetColumnCount() const
4812 return m_mainWin
->GetColumnCount();
4815 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4817 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4820 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4822 return m_mainWin
->GetItemSpacing( isSmall
);
4825 int wxListCtrl::GetSelectedItemCount() const
4827 return m_mainWin
->GetSelectedItemCount();
4830 wxColour
wxListCtrl::GetTextColour() const
4832 return GetForegroundColour();
4835 void wxListCtrl::SetTextColour(const wxColour
& col
)
4837 SetForegroundColour(col
);
4840 long wxListCtrl::GetTopItem() const
4845 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4847 return m_mainWin
->GetNextItem( item
, geom
, state
);
4850 wxImageList
*wxListCtrl::GetImageList(int which
) const
4852 if (which
== wxIMAGE_LIST_NORMAL
)
4854 return m_imageListNormal
;
4856 else if (which
== wxIMAGE_LIST_SMALL
)
4858 return m_imageListSmall
;
4860 else if (which
== wxIMAGE_LIST_STATE
)
4862 return m_imageListState
;
4864 return (wxImageList
*) NULL
;
4867 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4869 if ( which
== wxIMAGE_LIST_NORMAL
)
4871 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4872 m_imageListNormal
= imageList
;
4873 m_ownsImageListNormal
= FALSE
;
4875 else if ( which
== wxIMAGE_LIST_SMALL
)
4877 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4878 m_imageListSmall
= imageList
;
4879 m_ownsImageListSmall
= FALSE
;
4881 else if ( which
== wxIMAGE_LIST_STATE
)
4883 if (m_ownsImageListState
) delete m_imageListState
;
4884 m_imageListState
= imageList
;
4885 m_ownsImageListState
= FALSE
;
4888 m_mainWin
->SetImageList( imageList
, which
);
4891 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4893 SetImageList(imageList
, which
);
4894 if ( which
== wxIMAGE_LIST_NORMAL
)
4895 m_ownsImageListNormal
= TRUE
;
4896 else if ( which
== wxIMAGE_LIST_SMALL
)
4897 m_ownsImageListSmall
= TRUE
;
4898 else if ( which
== wxIMAGE_LIST_STATE
)
4899 m_ownsImageListState
= TRUE
;
4902 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4907 bool wxListCtrl::DeleteItem( long item
)
4909 m_mainWin
->DeleteItem( item
);
4913 bool wxListCtrl::DeleteAllItems()
4915 m_mainWin
->DeleteAllItems();
4919 bool wxListCtrl::DeleteAllColumns()
4921 size_t count
= m_mainWin
->m_columns
.GetCount();
4922 for ( size_t n
= 0; n
< count
; n
++ )
4928 void wxListCtrl::ClearAll()
4930 m_mainWin
->DeleteEverything();
4933 bool wxListCtrl::DeleteColumn( int col
)
4935 m_mainWin
->DeleteColumn( col
);
4939 void wxListCtrl::Edit( long item
)
4941 m_mainWin
->EditLabel( item
);
4944 bool wxListCtrl::EnsureVisible( long item
)
4946 m_mainWin
->EnsureVisible( item
);
4950 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4952 return m_mainWin
->FindItem( start
, str
, partial
);
4955 long wxListCtrl::FindItem( long start
, long data
)
4957 return m_mainWin
->FindItem( start
, data
);
4960 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4961 int WXUNUSED(direction
))
4966 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4968 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4971 long wxListCtrl::InsertItem( wxListItem
& info
)
4973 m_mainWin
->InsertItem( info
);
4974 return info
.m_itemId
;
4977 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
4980 info
.m_text
= label
;
4981 info
.m_mask
= wxLIST_MASK_TEXT
;
4982 info
.m_itemId
= index
;
4983 return InsertItem( info
);
4986 long wxListCtrl::InsertItem( long index
, int imageIndex
)
4989 info
.m_mask
= wxLIST_MASK_IMAGE
;
4990 info
.m_image
= imageIndex
;
4991 info
.m_itemId
= index
;
4992 return InsertItem( info
);
4995 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4998 info
.m_text
= label
;
4999 info
.m_image
= imageIndex
;
5000 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5001 info
.m_itemId
= index
;
5002 return InsertItem( info
);
5005 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
5007 wxASSERT( m_headerWin
);
5008 m_mainWin
->InsertColumn( col
, item
);
5009 m_headerWin
->Refresh();
5014 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
5015 int format
, int width
)
5018 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5019 item
.m_text
= heading
;
5022 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5023 item
.m_width
= width
;
5025 item
.m_format
= format
;
5027 return InsertColumn( col
, item
);
5030 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5036 // fn is a function which takes 3 long arguments: item1, item2, data.
5037 // item1 is the long data associated with a first item (NOT the index).
5038 // item2 is the long data associated with a second item (NOT the index).
5039 // data is the same value as passed to SortItems.
5040 // The return value is a negative number if the first item should precede the second
5041 // item, a positive number of the second item should precede the first,
5042 // or zero if the two items are equivalent.
5043 // data is arbitrary data to be passed to the sort function.
5045 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5047 m_mainWin
->SortItems( fn
, data
);
5051 // ----------------------------------------------------------------------------
5053 // ----------------------------------------------------------------------------
5055 void wxListCtrl::OnSize(wxSizeEvent
& event
)
5060 ResizeReportView(m_mainWin
->HasHeader());
5062 m_mainWin
->RecalculatePositions();
5065 void wxListCtrl::ResizeReportView(bool showHeader
)
5068 GetClientSize( &cw
, &ch
);
5072 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5073 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5075 else // no header window
5077 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5081 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5085 // do it only if needed
5086 if ( !m_mainWin
->m_dirty
)
5089 m_mainWin
->RecalculatePositions();
5092 // ----------------------------------------------------------------------------
5094 // ----------------------------------------------------------------------------
5096 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5100 m_mainWin
->SetBackgroundColour( colour
);
5101 m_mainWin
->m_dirty
= TRUE
;
5107 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5109 if ( !wxWindow::SetForegroundColour( colour
) )
5114 m_mainWin
->SetForegroundColour( colour
);
5115 m_mainWin
->m_dirty
= TRUE
;
5120 m_headerWin
->SetForegroundColour( colour
);
5126 bool wxListCtrl::SetFont( const wxFont
&font
)
5128 if ( !wxWindow::SetFont( font
) )
5133 m_mainWin
->SetFont( font
);
5134 m_mainWin
->m_dirty
= TRUE
;
5139 m_headerWin
->SetFont( font
);
5145 // ----------------------------------------------------------------------------
5146 // methods forwarded to m_mainWin
5147 // ----------------------------------------------------------------------------
5149 #if wxUSE_DRAG_AND_DROP
5151 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5153 m_mainWin
->SetDropTarget( dropTarget
);
5156 wxDropTarget
*wxListCtrl::GetDropTarget() const
5158 return m_mainWin
->GetDropTarget();
5161 #endif // wxUSE_DRAG_AND_DROP
5163 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5165 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5168 wxColour
wxListCtrl::GetBackgroundColour() const
5170 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5173 wxColour
wxListCtrl::GetForegroundColour() const
5175 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5178 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5181 return m_mainWin
->PopupMenu( menu
, x
, y
);
5184 #endif // wxUSE_MENUS
5187 void wxListCtrl::SetFocus()
5189 /* The test in window.cpp fails as we are a composite
5190 window, so it checks against "this", but not m_mainWin. */
5191 if ( FindFocus() != this )
5192 m_mainWin
->SetFocus();
5195 // ----------------------------------------------------------------------------
5196 // virtual list control support
5197 // ----------------------------------------------------------------------------
5199 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
5201 // this is a pure virtual function, in fact - which is not really pure
5202 // because the controls which are not virtual don't need to implement it
5203 wxFAIL_MSG( _T("not supposed to be called") );
5205 return wxEmptyString
;
5208 int wxListCtrl::OnGetItemImage(long item
) const
5211 wxFAIL_MSG( _T("not supposed to be called") );
5216 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5218 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5219 _T("invalid item index in OnGetItemAttr()") );
5221 // no attributes by default
5225 void wxListCtrl::SetItemCount(long count
)
5227 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5229 m_mainWin
->SetItemCount(count
);
5232 void wxListCtrl::RefreshItem(long item
)
5234 m_mainWin
->RefreshLine(item
);
5237 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5239 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5242 void wxListCtrl::Freeze()
5244 m_mainWin
->Freeze();
5247 void wxListCtrl::Thaw()
5252 #endif // wxUSE_LISTCTRL