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"
40 #include "wx/dcscreen.h"
42 #include "wx/listctrl.h"
43 #include "wx/imaglist.h"
44 #include "wx/dynarray.h"
48 #include "wx/gtk/win_gtk.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
68 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
69 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // the height of the header window (FIXME: should depend on its font!)
82 static const int HEADER_HEIGHT
= 23;
84 // the scrollbar units
85 static const int SCROLL_UNIT_X
= 15;
86 static const int SCROLL_UNIT_Y
= 15;
88 // the spacing between the lines (in report mode)
89 static const int LINE_SPACING
= 0;
91 // extra margins around the text label
92 static const int EXTRA_WIDTH
= 3;
93 static const int EXTRA_HEIGHT
= 4;
95 // offset for the header window
96 static const int HEADER_OFFSET_X
= 1;
97 static const int HEADER_OFFSET_Y
= 1;
99 // when autosizing the columns, add some slack
100 static const int AUTOSIZE_COL_MARGIN
= 10;
102 // default and minimal widths for the header columns
103 static const int WIDTH_COL_DEFAULT
= 80;
104 static const int WIDTH_COL_MIN
= 10;
106 // the space between the image and the text in the report mode
107 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
119 WX_DEFINE_SORTED_EXPORTED_ARRAY(size_t, wxIndexArray
);
121 // this class is used to store the selected items in the virtual list control
122 // (but it is not tied to list control and so can be used with other controls
123 // such as wxListBox in wxUniv)
125 // the idea is to make it really smart later (i.e. store the selections as an
126 // array of ranes + individual items) but, as I don't have time to do it now
127 // (this would require writing code to merge/break ranges and much more) keep
128 // it simple but define a clean interface to it which allows it to be made
130 class WXDLLEXPORT wxSelectionStore
133 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
135 // set the total number of items we handle
136 void SetItemCount(size_t count
) { m_count
= count
; }
138 // special case of SetItemCount(0)
139 void Clear() { m_itemsSel
.Clear(); m_count
= 0; }
141 // must be called when a new item is inserted/added
142 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
144 // must be called when an item is deleted
145 void OnItemDelete(size_t item
);
147 // select one item, use SelectRange() insted if possible!
149 // returns true if the items selection really changed
150 bool SelectItem(size_t item
, bool select
= TRUE
);
152 // select the range of items
154 // return true and fill the itemsChanged array with the indices of items
155 // which have changed state if "few" of them did, otherwise return false
156 // (meaning that too many items changed state to bother counting them
158 bool SelectRange(size_t itemFrom
, size_t itemTo
,
160 wxArrayInt
*itemsChanged
= NULL
);
162 // return true if the given item is selected
163 bool IsSelected(size_t item
) const;
165 // return the total number of selected items
166 size_t GetSelectedCount() const
168 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
169 : m_itemsSel
.GetCount();
174 void Init() { m_defaultState
= FALSE
; }
176 // the total number of items we handle
179 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
180 // there are more selected items than non selected ones - this allows to
181 // handle selection of all items efficiently
184 // the array of items whose selection state is different from default
185 wxIndexArray m_itemsSel
;
187 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
190 //-----------------------------------------------------------------------------
191 // wxListItemData (internal)
192 //-----------------------------------------------------------------------------
194 class WXDLLEXPORT wxListItemData
197 wxListItemData(wxListMainWindow
*owner
);
200 void SetItem( const wxListItem
&info
);
201 void SetImage( int image
) { m_image
= image
; }
202 void SetData( long data
) { m_data
= data
; }
203 void SetPosition( int x
, int y
);
204 void SetSize( int width
, int height
);
206 bool HasText() const { return !m_text
.empty(); }
207 const wxString
& GetText() const { return m_text
; }
208 void SetText(const wxString
& text
) { m_text
= text
; }
210 // we can't use empty string for measuring the string width/height, so
211 // always return something
212 wxString
GetTextForMeasuring() const
214 wxString s
= GetText();
221 bool IsHit( int x
, int y
) const;
225 int GetWidth() const;
226 int GetHeight() const;
228 int GetImage() const { return m_image
; }
229 bool HasImage() const { return GetImage() != -1; }
231 void GetItem( wxListItem
&info
) const;
233 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
234 wxListItemAttr
*GetAttr() const { return m_attr
; }
237 // the item image or -1
240 // user data associated with the item
243 // the item coordinates are not used in report mode, instead this pointer
244 // is NULL and the owner window is used to retrieve the item position and
248 // the list ctrl we are in
249 wxListMainWindow
*m_owner
;
251 // custom attributes or NULL
252 wxListItemAttr
*m_attr
;
255 // common part of all ctors
261 //-----------------------------------------------------------------------------
262 // wxListHeaderData (internal)
263 //-----------------------------------------------------------------------------
265 class WXDLLEXPORT wxListHeaderData
: public wxObject
269 wxListHeaderData( const wxListItem
&info
);
270 void SetItem( const wxListItem
&item
);
271 void SetPosition( int x
, int y
);
272 void SetWidth( int w
);
273 void SetFormat( int format
);
274 void SetHeight( int h
);
275 bool HasImage() const;
277 bool HasText() const { return !m_text
.empty(); }
278 const wxString
& GetText() const { return m_text
; }
279 void SetText(const wxString
& text
) { m_text
= text
; }
281 void GetItem( wxListItem
&item
);
283 bool IsHit( int x
, int y
) const;
284 int GetImage() const;
285 int GetWidth() const;
286 int GetFormat() const;
302 //-----------------------------------------------------------------------------
303 // wxListLineData (internal)
304 //-----------------------------------------------------------------------------
306 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
307 #include "wx/listimpl.cpp"
308 WX_DEFINE_LIST(wxListItemDataList
);
310 class WXDLLEXPORT wxListLineData
313 // the list of subitems: only may have more than one item in report mode
314 wxListItemDataList m_items
;
316 // this is not used in report view
328 // the part to be highlighted
329 wxRect m_rectHighlight
;
332 // is this item selected? [NB: not used in virtual mode]
335 // back pointer to the list ctrl
336 wxListMainWindow
*m_owner
;
339 wxListLineData(wxListMainWindow
*owner
);
341 ~wxListLineData() { delete m_gi
; }
343 // are we in report mode?
344 inline bool InReportView() const;
346 // are we in virtual report mode?
347 inline bool IsVirtual() const;
349 // these 2 methods shouldn't be called for report view controls, in that
350 // case we determine our position/size ourselves
352 // calculate the size of the line
353 void CalculateSize( wxDC
*dc
, int spacing
);
355 // remember the position this line appears at
356 void SetPosition( int x
, int y
, int window_width
, int spacing
);
360 void SetImage( int image
) { SetImage(0, image
); }
361 int GetImage() const { return GetImage(0); }
362 bool HasImage() const { return GetImage() != -1; }
363 bool HasText() const { return !GetText(0).empty(); }
365 void SetItem( int index
, const wxListItem
&info
);
366 void GetItem( int index
, wxListItem
&info
);
368 wxString
GetText(int index
) const;
369 void SetText( int index
, const wxString s
);
371 wxListItemAttr
*GetAttr() const;
372 void SetAttr(wxListItemAttr
*attr
);
374 // return true if the highlighting really changed
375 bool Highlight( bool on
);
377 void ReverseHighlight();
379 bool IsHighlighted() const
381 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
383 return m_highlighted
;
386 // draw the line on the given DC in icon/list mode
387 void Draw( wxDC
*dc
);
389 // the same in report mode
390 void DrawInReportMode( wxDC
*dc
,
392 const wxRect
& rectHL
,
396 // set the line to contain num items (only can be > 1 in report mode)
397 void InitItems( int num
);
399 // get the mode (i.e. style) of the list control
400 inline int GetMode() const;
402 // prepare the DC for drawing with these item's attributes, return true if
403 // we need to draw the items background to highlight it, false otherwise
404 bool SetAttributes(wxDC
*dc
,
405 const wxListItemAttr
*attr
,
408 // these are only used by GetImage/SetImage above, we don't support images
409 // with subitems at the public API level yet
410 void SetImage( int index
, int image
);
411 int GetImage( int index
) const;
414 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
415 #include "wx/arrimpl.cpp"
416 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
418 //-----------------------------------------------------------------------------
419 // wxListHeaderWindow (internal)
420 //-----------------------------------------------------------------------------
422 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
425 wxListMainWindow
*m_owner
;
426 wxCursor
*m_currentCursor
;
427 wxCursor
*m_resizeCursor
;
430 // column being resized
433 // divider line position in logical (unscrolled) coords
436 // minimal position beyond which the divider line can't be dragged in
441 wxListHeaderWindow();
443 wxListHeaderWindow( wxWindow
*win
,
445 wxListMainWindow
*owner
,
446 const wxPoint
&pos
= wxDefaultPosition
,
447 const wxSize
&size
= wxDefaultSize
,
449 const wxString
&name
= "wxlistctrlcolumntitles" );
451 virtual ~wxListHeaderWindow();
453 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
455 void AdjustDC(wxDC
& dc
);
457 void OnPaint( wxPaintEvent
&event
);
458 void OnMouse( wxMouseEvent
&event
);
459 void OnSetFocus( wxFocusEvent
&event
);
465 // common part of all ctors
468 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
469 DECLARE_EVENT_TABLE()
472 //-----------------------------------------------------------------------------
473 // wxListRenameTimer (internal)
474 //-----------------------------------------------------------------------------
476 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
479 wxListMainWindow
*m_owner
;
482 wxListRenameTimer( wxListMainWindow
*owner
);
486 //-----------------------------------------------------------------------------
487 // wxListTextCtrl (internal)
488 //-----------------------------------------------------------------------------
490 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
495 wxListMainWindow
*m_owner
;
496 wxString m_startValue
;
500 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
501 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
502 const wxString
&value
= "",
503 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
505 const wxValidator
& validator
= wxDefaultValidator
,
506 const wxString
&name
= "listctrltextctrl" );
507 void OnChar( wxKeyEvent
&event
);
508 void OnKeyUp( wxKeyEvent
&event
);
509 void OnKillFocus( wxFocusEvent
&event
);
512 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
);
513 DECLARE_EVENT_TABLE()
516 //-----------------------------------------------------------------------------
517 // wxListMainWindow (internal)
518 //-----------------------------------------------------------------------------
520 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
521 #include "wx/listimpl.cpp"
522 WX_DEFINE_LIST(wxListHeaderDataList
);
524 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
528 wxListMainWindow( wxWindow
*parent
,
530 const wxPoint
& pos
= wxDefaultPosition
,
531 const wxSize
& size
= wxDefaultSize
,
533 const wxString
&name
= _T("listctrlmainwindow") );
535 virtual ~wxListMainWindow();
537 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
539 // return true if this is a virtual list control
540 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
542 // return true if the control is in report mode
543 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
545 // return true if we are in single selection mode, false if multi sel
546 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
548 // do we have a header window?
549 bool HasHeader() const
550 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
552 void HighlightAll( bool on
);
554 // all these functions only do something if the line is currently visible
556 // change the line "selected" state, return TRUE if it really changed
557 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
559 // as HighlightLine() but do it for the range of lines: this is incredibly
560 // more efficient for virtual list controls!
562 // NB: unlike HighlightLine() this one does refresh the lines on screen
563 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
565 // toggle the line state and refresh it
566 void ReverseHighlight( size_t line
)
567 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
569 // return true if the line is highlighted
570 bool IsHighlighted(size_t line
) const;
572 // refresh one or several lines at once
573 void RefreshLine( size_t line
);
574 void RefreshLines( size_t lineFrom
, size_t lineTo
);
576 // refresh all selected items
577 void RefreshSelected();
579 // refresh all lines below the given one: the difference with
580 // RefreshLines() is that the index here might not be a valid one (happens
581 // when the last line is deleted)
582 void RefreshAfter( size_t lineFrom
);
584 // the methods which are forwarded to wxListLineData itself in list/icon
585 // modes but are here because the lines don't store their positions in the
588 // get the bound rect for the entire line
589 wxRect
GetLineRect(size_t line
) const;
591 // get the bound rect of the label
592 wxRect
GetLineLabelRect(size_t line
) const;
594 // get the bound rect of the items icon (only may be called if we do have
596 wxRect
GetLineIconRect(size_t line
) const;
598 // get the rect to be highlighted when the item has focus
599 wxRect
GetLineHighlightRect(size_t line
) const;
601 // get the size of the total line rect
602 wxSize
GetLineSize(size_t line
) const
603 { return GetLineRect(line
).GetSize(); }
605 // return the hit code for the corresponding position (in this line)
606 long HitTestLine(size_t line
, int x
, int y
) const;
608 // bring the selected item into view, scrolling to it if necessary
609 void MoveToItem(size_t item
);
611 // bring the current item into view
612 void MoveToFocus() { MoveToItem(m_current
); }
614 // start editing the label of the given item
615 void EditLabel( long item
);
617 // suspend/resume redrawing the control
621 void OnRenameTimer();
622 void OnRenameAccept();
624 void OnMouse( wxMouseEvent
&event
);
626 // called to switch the selection from the current item to newCurrent,
627 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
629 void OnChar( wxKeyEvent
&event
);
630 void OnKeyDown( wxKeyEvent
&event
);
631 void OnSetFocus( wxFocusEvent
&event
);
632 void OnKillFocus( wxFocusEvent
&event
);
633 void OnScroll(wxScrollWinEvent
& event
) ;
635 void OnPaint( wxPaintEvent
&event
);
637 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
638 void GetImageSize( int index
, int &width
, int &height
) const;
639 int GetTextLength( const wxString
&s
) const;
641 void SetImageList( wxImageList
*imageList
, int which
);
642 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
643 int GetItemSpacing( bool isSmall
= FALSE
);
645 void SetColumn( int col
, wxListItem
&item
);
646 void SetColumnWidth( int col
, int width
);
647 void GetColumn( int col
, wxListItem
&item
) const;
648 int GetColumnWidth( int col
) const;
649 int GetColumnCount() const { return m_columns
.GetCount(); }
651 // returns the sum of the heights of all columns
652 int GetHeaderWidth() const;
654 int GetCountPerPage() const;
656 void SetItem( wxListItem
&item
);
657 void GetItem( wxListItem
&item
);
658 void SetItemState( long item
, long state
, long stateMask
);
659 int GetItemState( long item
, long stateMask
);
660 void GetItemRect( long index
, wxRect
&rect
);
661 bool GetItemPosition( long item
, wxPoint
& pos
);
662 int GetSelectedItemCount();
664 // set the scrollbars and update the positions of the items
665 void RecalculatePositions(bool noRefresh
= FALSE
);
667 // refresh the window and the header
670 long GetNextItem( long item
, int geometry
, int state
);
671 void DeleteItem( long index
);
672 void DeleteAllItems();
673 void DeleteColumn( int col
);
674 void DeleteEverything();
675 void EnsureVisible( long index
);
676 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
677 long FindItem( long start
, long data
);
678 long HitTest( int x
, int y
, int &flags
);
679 void InsertItem( wxListItem
&item
);
680 void InsertColumn( long col
, wxListItem
&item
);
681 void SortItems( wxListCtrlCompare fn
, long data
);
683 size_t GetItemCount() const;
684 bool IsEmpty() const { return GetItemCount() == 0; }
685 void SetItemCount(long count
);
687 void ResetCurrent() { m_current
= (size_t)-1; }
688 bool HasCurrent() const { return m_current
!= (size_t)-1; }
690 // send out a wxListEvent
691 void SendNotify( size_t line
,
693 wxPoint point
= wxDefaultPosition
);
695 // override base class virtual to reset m_lineHeight when the font changes
696 virtual bool SetFont(const wxFont
& font
)
698 if ( !wxScrolledWindow::SetFont(font
) )
706 // these are for wxListLineData usage only
708 // get the backpointer to the list ctrl
709 wxListCtrl
*GetListCtrl() const
711 return wxStaticCast(GetParent(), wxListCtrl
);
714 // get the height of all lines (assuming they all do have the same height)
715 wxCoord
GetLineHeight() const;
717 // get the y position of the given line (only for report view)
718 wxCoord
GetLineY(size_t line
) const;
720 // get the brush to use for the item highlighting
721 wxBrush
*GetHighlightBrush() const
723 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
727 // the array of all line objects for a non virtual list control
728 wxListLineDataArray m_lines
;
730 // the list of column objects
731 wxListHeaderDataList m_columns
;
733 // currently focused item or -1
736 // the item currently being edited or -1
737 size_t m_currentEdit
;
739 // the number of lines per page
742 // this flag is set when something which should result in the window
743 // redrawing happens (i.e. an item was added or deleted, or its appearance
744 // changed) and OnPaint() doesn't redraw the window while it is set which
745 // allows to minimize the number of repaintings when a lot of items are
746 // being added. The real repainting occurs only after the next OnIdle()
750 wxColour
*m_highlightColour
;
753 wxImageList
*m_small_image_list
;
754 wxImageList
*m_normal_image_list
;
756 int m_normal_spacing
;
760 wxTimer
*m_renameTimer
;
762 wxString m_renameRes
;
767 // for double click logic
768 size_t m_lineLastClicked
,
769 m_lineBeforeLastClicked
;
772 // the total count of items in a virtual list control
775 // the object maintaining the items selection state, only used in virtual
777 wxSelectionStore m_selStore
;
779 // common part of all ctors
782 // intiialize m_[xy]Scroll
783 void InitScrolling();
785 // get the line data for the given index
786 wxListLineData
*GetLine(size_t n
) const
788 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
792 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
800 // get a dummy line which can be used for geometry calculations and such:
801 // you must use GetLine() if you want to really draw the line
802 wxListLineData
*GetDummyLine() const;
804 // cache the line data of the n-th line in m_lines[0]
805 void CacheLineData(size_t line
);
807 // get the range of visible lines
808 void GetVisibleLinesRange(size_t *from
, size_t *to
);
810 // force us to recalculate the range of visible lines
811 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
813 // get the colour to be used for drawing the rules
814 wxColour
GetRuleColour() const
819 return wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
824 // initialize the current item if needed
825 void UpdateCurrent();
827 // delete all items but don't refresh: called from dtor
828 void DoDeleteAllItems();
830 // called when an item is [un]focuded, i.e. becomes [not] current
833 void OnFocusLine( size_t line
);
834 void OnUnfocusLine( size_t line
);
836 // the height of one line using the current font
837 wxCoord m_lineHeight
;
839 // the total header width or 0 if not calculated yet
840 wxCoord m_headerWidth
;
842 // the first and last lines being shown on screen right now (inclusive),
843 // both may be -1 if they must be calculated so never access them directly:
844 // use GetVisibleLinesRange() above instead
848 // the brushes to use for item highlighting when we do/don't have focus
849 wxBrush
*m_highlightBrush
,
850 *m_highlightUnfocusedBrush
;
852 // if this is > 0, the control is frozen and doesn't redraw itself
853 size_t m_freezeCount
;
855 DECLARE_DYNAMIC_CLASS(wxListMainWindow
);
856 DECLARE_EVENT_TABLE()
859 // ============================================================================
861 // ============================================================================
863 // ----------------------------------------------------------------------------
865 // ----------------------------------------------------------------------------
867 bool wxSelectionStore::IsSelected(size_t item
) const
869 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
871 // if the default state is to be selected, being in m_itemsSel means that
872 // the item is not selected, so we have to inverse the logic
873 return m_defaultState
? !isSel
: isSel
;
876 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
878 // search for the item ourselves as like this we get the index where to
879 // insert it later if needed, so we do only one search in the array instead
880 // of two (adding item to a sorted array requires a search)
881 size_t index
= m_itemsSel
.IndexForInsert(item
);
882 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
884 if ( select
!= m_defaultState
)
888 m_itemsSel
.AddAt(item
, index
);
893 else // reset to default state
897 m_itemsSel
.RemoveAt(index
);
905 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
907 wxArrayInt
*itemsChanged
)
909 // 100 is hardcoded but it shouldn't matter much: the important thing is
910 // that we don't refresh everything when really few (e.g. 1 or 2) items
912 static const size_t MANY_ITEMS
= 100;
914 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
916 // are we going to have more [un]selected items than the other ones?
917 if ( itemTo
- itemFrom
> m_count
/2 )
919 if ( select
!= m_defaultState
)
921 // the default state now becomes the same as 'select'
922 m_defaultState
= select
;
924 // so all the old selections (which had state select) shouldn't be
925 // selected any more, but all the other ones should
926 wxIndexArray selOld
= m_itemsSel
;
929 // TODO: it should be possible to optimize the searches a bit
930 // knowing the possible range
933 for ( item
= 0; item
< itemFrom
; item
++ )
935 if ( selOld
.Index(item
) == wxNOT_FOUND
)
936 m_itemsSel
.Add(item
);
939 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
941 if ( selOld
.Index(item
) == wxNOT_FOUND
)
942 m_itemsSel
.Add(item
);
945 // many items (> half) changed state
948 else // select == m_defaultState
950 // get the inclusive range of items between itemFrom and itemTo
951 size_t count
= m_itemsSel
.GetCount(),
952 start
= m_itemsSel
.IndexForInsert(itemFrom
),
953 end
= m_itemsSel
.IndexForInsert(itemTo
);
955 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
960 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
967 // delete all of them (from end to avoid changing indices)
968 for ( int i
= end
; i
>= (int)start
; i
-- )
972 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
974 // stop counting (see comment below)
978 itemsChanged
->Add(m_itemsSel
[i
]);
981 m_itemsSel
.RemoveAt(i
);
986 else // "few" items change state
990 itemsChanged
->Empty();
993 // just add the items to the selection
994 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
996 if ( SelectItem(item
, select
) && itemsChanged
)
998 itemsChanged
->Add(item
);
1000 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1002 // stop counting them, we'll just eat gobs of memory
1003 // for nothing at all - faster to refresh everything in
1005 itemsChanged
= NULL
;
1011 // we set it to NULL if there are many items changing state
1012 return itemsChanged
!= NULL
;
1015 void wxSelectionStore::OnItemDelete(size_t item
)
1017 size_t count
= m_itemsSel
.GetCount(),
1018 i
= m_itemsSel
.IndexForInsert(item
);
1020 if ( i
< count
&& m_itemsSel
[i
] == item
)
1022 // this item itself was in m_itemsSel, remove it from there
1023 m_itemsSel
.RemoveAt(i
);
1028 // and adjust the index of all which follow it
1031 // all following elements must be greater than the one we deleted
1032 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1038 //-----------------------------------------------------------------------------
1040 //-----------------------------------------------------------------------------
1042 wxListItemData::~wxListItemData()
1044 // in the virtual list control the attributes are managed by the main
1045 // program, so don't delete them
1046 if ( !m_owner
->IsVirtual() )
1054 void wxListItemData::Init()
1062 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1068 if ( owner
->InReportView() )
1074 m_rect
= new wxRect
;
1078 void wxListItemData::SetItem( const wxListItem
&info
)
1080 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1081 SetText(info
.m_text
);
1082 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1083 m_image
= info
.m_image
;
1084 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1085 m_data
= info
.m_data
;
1087 if ( info
.HasAttributes() )
1090 *m_attr
= *info
.GetAttributes();
1092 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1100 m_rect
->width
= info
.m_width
;
1104 void wxListItemData::SetPosition( int x
, int y
)
1106 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1112 void wxListItemData::SetSize( int width
, int height
)
1114 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1117 m_rect
->width
= width
;
1119 m_rect
->height
= height
;
1122 bool wxListItemData::IsHit( int x
, int y
) const
1124 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1126 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1129 int wxListItemData::GetX() const
1131 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1136 int wxListItemData::GetY() const
1138 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1143 int wxListItemData::GetWidth() const
1145 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1147 return m_rect
->width
;
1150 int wxListItemData::GetHeight() const
1152 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1154 return m_rect
->height
;
1157 void wxListItemData::GetItem( wxListItem
&info
) const
1159 info
.m_text
= m_text
;
1160 info
.m_image
= m_image
;
1161 info
.m_data
= m_data
;
1165 if ( m_attr
->HasTextColour() )
1166 info
.SetTextColour(m_attr
->GetTextColour());
1167 if ( m_attr
->HasBackgroundColour() )
1168 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1169 if ( m_attr
->HasFont() )
1170 info
.SetFont(m_attr
->GetFont());
1174 //-----------------------------------------------------------------------------
1176 //-----------------------------------------------------------------------------
1178 void wxListHeaderData::Init()
1189 wxListHeaderData::wxListHeaderData()
1194 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1201 void wxListHeaderData::SetItem( const wxListItem
&item
)
1203 m_mask
= item
.m_mask
;
1205 if ( m_mask
& wxLIST_MASK_TEXT
)
1206 m_text
= item
.m_text
;
1208 if ( m_mask
& wxLIST_MASK_IMAGE
)
1209 m_image
= item
.m_image
;
1211 if ( m_mask
& wxLIST_MASK_FORMAT
)
1212 m_format
= item
.m_format
;
1214 if ( m_mask
& wxLIST_MASK_WIDTH
)
1215 SetWidth(item
.m_width
);
1218 void wxListHeaderData::SetPosition( int x
, int y
)
1224 void wxListHeaderData::SetHeight( int h
)
1229 void wxListHeaderData::SetWidth( int w
)
1233 m_width
= WIDTH_COL_DEFAULT
;
1234 else if (m_width
< WIDTH_COL_MIN
)
1235 m_width
= WIDTH_COL_MIN
;
1238 void wxListHeaderData::SetFormat( int format
)
1243 bool wxListHeaderData::HasImage() const
1245 return m_image
!= -1;
1248 bool wxListHeaderData::IsHit( int x
, int y
) const
1250 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1253 void wxListHeaderData::GetItem( wxListItem
& item
)
1255 item
.m_mask
= m_mask
;
1256 item
.m_text
= m_text
;
1257 item
.m_image
= m_image
;
1258 item
.m_format
= m_format
;
1259 item
.m_width
= m_width
;
1262 int wxListHeaderData::GetImage() const
1267 int wxListHeaderData::GetWidth() const
1272 int wxListHeaderData::GetFormat() const
1277 //-----------------------------------------------------------------------------
1279 //-----------------------------------------------------------------------------
1281 inline int wxListLineData::GetMode() const
1283 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1286 inline bool wxListLineData::InReportView() const
1288 return m_owner
->HasFlag(wxLC_REPORT
);
1291 inline bool wxListLineData::IsVirtual() const
1293 return m_owner
->IsVirtual();
1296 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1299 m_items
.DeleteContents( TRUE
);
1301 if ( InReportView() )
1307 m_gi
= new GeometryInfo
;
1310 m_highlighted
= FALSE
;
1312 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1315 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1317 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1318 wxCHECK_RET( node
, _T("no subitems at all??") );
1320 wxListItemData
*item
= node
->GetData();
1322 switch ( GetMode() )
1325 case wxLC_SMALL_ICON
:
1327 m_gi
->m_rectAll
.width
= spacing
;
1329 wxString s
= item
->GetText();
1335 m_gi
->m_rectLabel
.width
=
1336 m_gi
->m_rectLabel
.height
= 0;
1340 dc
->GetTextExtent( s
, &lw
, &lh
);
1341 if (lh
< SCROLL_UNIT_Y
)
1346 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1348 m_gi
->m_rectAll
.width
= lw
;
1350 m_gi
->m_rectLabel
.width
= lw
;
1351 m_gi
->m_rectLabel
.height
= lh
;
1354 if (item
->HasImage())
1357 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1358 m_gi
->m_rectIcon
.width
= w
+ 8;
1359 m_gi
->m_rectIcon
.height
= h
+ 8;
1361 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1362 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1363 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1364 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1367 if ( item
->HasText() )
1369 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1370 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1372 else // no text, highlight the icon
1374 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1375 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1382 wxString s
= item
->GetTextForMeasuring();
1385 dc
->GetTextExtent( s
, &lw
, &lh
);
1386 if (lh
< SCROLL_UNIT_Y
)
1391 m_gi
->m_rectLabel
.width
= lw
;
1392 m_gi
->m_rectLabel
.height
= lh
;
1394 m_gi
->m_rectAll
.width
= lw
;
1395 m_gi
->m_rectAll
.height
= lh
;
1397 if (item
->HasImage())
1400 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1401 m_gi
->m_rectIcon
.width
= w
;
1402 m_gi
->m_rectIcon
.height
= h
;
1404 m_gi
->m_rectAll
.width
+= 4 + w
;
1405 if (h
> m_gi
->m_rectAll
.height
)
1406 m_gi
->m_rectAll
.height
= h
;
1409 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1410 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1415 wxFAIL_MSG( _T("unexpected call to SetSize") );
1419 wxFAIL_MSG( _T("unknown mode") );
1423 void wxListLineData::SetPosition( int x
, int y
,
1427 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1428 wxCHECK_RET( node
, _T("no subitems at all??") );
1430 wxListItemData
*item
= node
->GetData();
1432 switch ( GetMode() )
1435 case wxLC_SMALL_ICON
:
1436 m_gi
->m_rectAll
.x
= x
;
1437 m_gi
->m_rectAll
.y
= y
;
1439 if ( item
->HasImage() )
1441 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4
1442 + (spacing
- m_gi
->m_rectIcon
.width
)/2;
1443 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1446 if ( item
->HasText() )
1448 if (m_gi
->m_rectAll
.width
> spacing
)
1449 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1451 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1452 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1453 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1454 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1456 else // no text, highlight the icon
1458 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1459 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1464 m_gi
->m_rectAll
.x
= x
;
1465 m_gi
->m_rectAll
.y
= y
;
1467 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1468 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1469 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1471 if (item
->HasImage())
1473 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1474 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1475 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1479 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1484 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1488 wxFAIL_MSG( _T("unknown mode") );
1492 void wxListLineData::InitItems( int num
)
1494 for (int i
= 0; i
< num
; i
++)
1495 m_items
.Append( new wxListItemData(m_owner
) );
1498 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1500 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1501 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1503 wxListItemData
*item
= node
->GetData();
1504 item
->SetItem( info
);
1507 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1509 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1512 wxListItemData
*item
= node
->GetData();
1513 item
->GetItem( info
);
1517 wxString
wxListLineData::GetText(int index
) const
1521 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1524 wxListItemData
*item
= node
->GetData();
1525 s
= item
->GetText();
1531 void wxListLineData::SetText( int index
, const wxString s
)
1533 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1536 wxListItemData
*item
= node
->GetData();
1541 void wxListLineData::SetImage( int index
, int image
)
1543 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1544 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1546 wxListItemData
*item
= node
->GetData();
1547 item
->SetImage(image
);
1550 int wxListLineData::GetImage( int index
) const
1552 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1553 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1555 wxListItemData
*item
= node
->GetData();
1556 return item
->GetImage();
1559 wxListItemAttr
*wxListLineData::GetAttr() const
1561 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1562 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1564 wxListItemData
*item
= node
->GetData();
1565 return item
->GetAttr();
1568 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1570 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1571 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1573 wxListItemData
*item
= node
->GetData();
1574 item
->SetAttr(attr
);
1577 bool wxListLineData::SetAttributes(wxDC
*dc
,
1578 const wxListItemAttr
*attr
,
1581 wxWindow
*listctrl
= m_owner
->GetParent();
1585 // don't use foreground colour for drawing highlighted items - this might
1586 // make them completely invisible (and there is no way to do bit
1587 // arithmetics on wxColour, unfortunately)
1591 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1595 if ( attr
&& attr
->HasTextColour() )
1597 colText
= attr
->GetTextColour();
1601 colText
= listctrl
->GetForegroundColour();
1605 dc
->SetTextForeground(colText
);
1609 if ( attr
&& attr
->HasFont() )
1611 font
= attr
->GetFont();
1615 font
= listctrl
->GetFont();
1621 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1622 if ( highlighted
|| hasBgCol
)
1626 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1630 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1633 dc
->SetPen( *wxTRANSPARENT_PEN
);
1641 void wxListLineData::Draw( wxDC
*dc
)
1643 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1644 wxCHECK_RET( node
, _T("no subitems at all??") );
1646 bool highlighted
= IsHighlighted();
1648 wxListItemAttr
*attr
= GetAttr();
1650 if ( SetAttributes(dc
, attr
, highlighted
) )
1652 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1655 wxListItemData
*item
= node
->GetData();
1656 if (item
->HasImage())
1658 wxRect rectIcon
= m_gi
->m_rectIcon
;
1659 m_owner
->DrawImage( item
->GetImage(), dc
,
1660 rectIcon
.x
, rectIcon
.y
);
1663 if (item
->HasText())
1665 wxRect rectLabel
= m_gi
->m_rectLabel
;
1667 wxDCClipper
clipper(*dc
, rectLabel
);
1668 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1672 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1674 const wxRect
& rectHL
,
1677 // TODO: later we should support setting different attributes for
1678 // different columns - to do it, just add "col" argument to
1679 // GetAttr() and move these lines into the loop below
1680 wxListItemAttr
*attr
= GetAttr();
1681 if ( SetAttributes(dc
, attr
, highlighted
) )
1683 dc
->DrawRectangle( rectHL
);
1686 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1687 wxCHECK_RET( node
, _T("no subitems at all??") );
1690 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1691 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1695 wxListItemData
*item
= node
->GetData();
1697 int width
= m_owner
->GetColumnWidth(col
++);
1701 if ( item
->HasImage() )
1704 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1705 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1707 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1713 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1715 if ( item
->HasText() )
1717 dc
->DrawText( item
->GetText(), xOld
, y
);
1720 node
= node
->GetNext();
1724 bool wxListLineData::Highlight( bool on
)
1726 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1728 if ( on
== m_highlighted
)
1736 void wxListLineData::ReverseHighlight( void )
1738 Highlight(!IsHighlighted());
1741 //-----------------------------------------------------------------------------
1742 // wxListHeaderWindow
1743 //-----------------------------------------------------------------------------
1745 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
);
1747 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1748 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1749 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1750 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1753 void wxListHeaderWindow::Init()
1755 m_currentCursor
= (wxCursor
*) NULL
;
1756 m_isDragging
= FALSE
;
1760 wxListHeaderWindow::wxListHeaderWindow()
1764 m_owner
= (wxListMainWindow
*) NULL
;
1765 m_resizeCursor
= (wxCursor
*) NULL
;
1768 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1770 wxListMainWindow
*owner
,
1774 const wxString
&name
)
1775 : wxWindow( win
, id
, pos
, size
, style
, name
)
1780 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1782 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
) );
1785 wxListHeaderWindow::~wxListHeaderWindow()
1787 delete m_resizeCursor
;
1790 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1793 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1794 : GTK_STATE_INSENSITIVE
;
1796 x
= dc
->XLOG2DEV( x
);
1798 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1799 state
, GTK_SHADOW_OUT
,
1800 (GdkRectangle
*) NULL
, m_wxwindow
, "button",
1801 x
-1, y
-1, w
+2, h
+2);
1802 #elif defined( __WXMAC__ )
1803 const int m_corner
= 1;
1805 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1807 dc
->SetPen( wxPen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1808 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1809 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1811 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1814 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1815 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1817 dc
->SetPen( *wxWHITE_PEN
);
1818 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1819 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1820 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1821 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1823 const int m_corner
= 1;
1825 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1827 dc
->SetPen( *wxBLACK_PEN
);
1828 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1829 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1831 wxPen
pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1834 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1835 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1837 dc
->SetPen( *wxWHITE_PEN
);
1838 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1839 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1840 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1841 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1845 // shift the DC origin to match the position of the main window horz
1846 // scrollbar: this allows us to always use logical coords
1847 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1850 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1853 m_owner
->GetViewStart( &x
, NULL
);
1855 // account for the horz scrollbar offset
1856 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1859 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1862 wxClientDC
dc( this );
1864 wxPaintDC
dc( this );
1872 dc
.SetFont( GetFont() );
1874 // width and height of the entire header window
1876 GetClientSize( &w
, &h
);
1877 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1879 dc
.SetBackgroundMode(wxTRANSPARENT
);
1881 // do *not* use the listctrl colour for headers - one day we will have a
1882 // function to set it separately
1883 //dc.SetTextForeground( *wxBLACK );
1884 dc
.SetTextForeground(wxSystemSettings::
1885 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1887 int x
= HEADER_OFFSET_X
;
1889 int numColumns
= m_owner
->GetColumnCount();
1891 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1893 m_owner
->GetColumn( i
, item
);
1894 int wCol
= item
.m_width
;
1896 // the width of the rect to draw: make it smaller to fit entirely
1897 // inside the column rect
1900 dc
.SetPen( *wxWHITE_PEN
);
1902 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1904 // if we have an image, draw it on the right of the label
1905 int image
= item
.m_image
;
1908 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1912 imageList
->GetSize(image
, ix
, iy
);
1919 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1920 wxIMAGELIST_DRAW_TRANSPARENT
1925 //else: ignore the column image
1928 // draw the text clipping it so that it doesn't overwrite the column
1930 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1932 dc
.DrawText( item
.GetText(),
1933 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1941 void wxListHeaderWindow::DrawCurrent()
1943 int x1
= m_currentX
;
1945 ClientToScreen( &x1
, &y1
);
1947 int x2
= m_currentX
-1;
1949 m_owner
->GetClientSize( NULL
, &y2
);
1950 m_owner
->ClientToScreen( &x2
, &y2
);
1953 dc
.SetLogicalFunction( wxINVERT
);
1954 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1955 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1959 dc
.DrawLine( x1
, y1
, x2
, y2
);
1961 dc
.SetLogicalFunction( wxCOPY
);
1963 dc
.SetPen( wxNullPen
);
1964 dc
.SetBrush( wxNullBrush
);
1967 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1969 // we want to work with logical coords
1971 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1972 int y
= event
.GetY();
1976 // we don't draw the line beyond our window, but we allow dragging it
1979 GetClientSize( &w
, NULL
);
1980 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1983 // erase the line if it was drawn
1984 if ( m_currentX
< w
)
1987 if (event
.ButtonUp())
1990 m_isDragging
= FALSE
;
1992 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1999 m_currentX
= m_minX
+ 7;
2001 // draw in the new location
2002 if ( m_currentX
< w
)
2006 else // not dragging
2009 bool hit_border
= FALSE
;
2011 // end of the current column
2014 // find the column where this event occured
2015 int countCol
= m_owner
->GetColumnCount();
2016 for (int col
= 0; col
< countCol
; col
++)
2018 xpos
+= m_owner
->GetColumnWidth( col
);
2021 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2023 // near the column border
2030 // inside the column
2037 if (event
.LeftDown() || event
.RightUp())
2039 if (hit_border
&& event
.LeftDown())
2041 m_isDragging
= TRUE
;
2046 else // click on a column
2048 wxWindow
*parent
= GetParent();
2049 wxListEvent
le( event
.LeftDown()
2050 ? wxEVT_COMMAND_LIST_COL_CLICK
2051 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2053 le
.SetEventObject( parent
);
2054 le
.m_pointDrag
= event
.GetPosition();
2056 // the position should be relative to the parent window, not
2057 // this one for compatibility with MSW and common sense: the
2058 // user code doesn't know anything at all about this header
2059 // window, so why should it get positions relative to it?
2060 le
.m_pointDrag
.y
-= GetSize().y
;
2062 le
.m_col
= m_column
;
2063 parent
->GetEventHandler()->ProcessEvent( le
);
2066 else if (event
.Moving())
2071 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2072 m_currentCursor
= m_resizeCursor
;
2076 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2077 m_currentCursor
= wxSTANDARD_CURSOR
;
2081 SetCursor(*m_currentCursor
);
2086 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2088 m_owner
->SetFocus();
2091 //-----------------------------------------------------------------------------
2092 // wxListRenameTimer (internal)
2093 //-----------------------------------------------------------------------------
2095 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2100 void wxListRenameTimer::Notify()
2102 m_owner
->OnRenameTimer();
2105 //-----------------------------------------------------------------------------
2106 // wxListTextCtrl (internal)
2107 //-----------------------------------------------------------------------------
2109 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
);
2111 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2112 EVT_CHAR (wxListTextCtrl::OnChar
)
2113 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2114 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2117 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
2118 const wxWindowID id
,
2121 wxListMainWindow
*owner
,
2122 const wxString
&value
,
2126 const wxValidator
& validator
,
2127 const wxString
&name
)
2128 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
2133 (*m_accept
) = FALSE
;
2135 m_startValue
= value
;
2138 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2140 if (event
.m_keyCode
== WXK_RETURN
)
2143 (*m_res
) = GetValue();
2145 if (!wxPendingDelete
.Member(this))
2146 wxPendingDelete
.Append(this);
2148 if ((*m_accept
) && ((*m_res
) != m_startValue
))
2149 m_owner
->OnRenameAccept();
2153 if (event
.m_keyCode
== WXK_ESCAPE
)
2155 (*m_accept
) = FALSE
;
2158 if (!wxPendingDelete
.Member(this))
2159 wxPendingDelete
.Append(this);
2167 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2169 // auto-grow the textctrl:
2170 wxSize parentSize
= m_owner
->GetSize();
2171 wxPoint myPos
= GetPosition();
2172 wxSize mySize
= GetSize();
2174 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
); // FIXME: MM??
2175 if (myPos
.x
+ sx
> parentSize
.x
)
2176 sx
= parentSize
.x
- myPos
.x
;
2184 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2186 if (!wxPendingDelete
.Member(this))
2187 wxPendingDelete
.Append(this);
2189 if ((*m_accept
) && ((*m_res
) != m_startValue
))
2190 m_owner
->OnRenameAccept();
2193 //-----------------------------------------------------------------------------
2195 //-----------------------------------------------------------------------------
2197 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
);
2199 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2200 EVT_PAINT (wxListMainWindow::OnPaint
)
2201 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2202 EVT_CHAR (wxListMainWindow::OnChar
)
2203 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2204 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2205 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2206 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2209 void wxListMainWindow::Init()
2211 m_columns
.DeleteContents( TRUE
);
2215 m_lineTo
= (size_t)-1;
2221 m_small_image_list
= (wxImageList
*) NULL
;
2222 m_normal_image_list
= (wxImageList
*) NULL
;
2224 m_small_spacing
= 30;
2225 m_normal_spacing
= 40;
2229 m_isCreated
= FALSE
;
2231 m_lastOnSame
= FALSE
;
2232 m_renameTimer
= new wxListRenameTimer( this );
2233 m_renameAccept
= FALSE
;
2238 m_lineBeforeLastClicked
= (size_t)-1;
2243 void wxListMainWindow::InitScrolling()
2245 if ( HasFlag(wxLC_REPORT
) )
2247 m_xScroll
= SCROLL_UNIT_X
;
2248 m_yScroll
= SCROLL_UNIT_Y
;
2252 m_xScroll
= SCROLL_UNIT_Y
;
2257 wxListMainWindow::wxListMainWindow()
2262 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2268 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2273 const wxString
&name
)
2274 : wxScrolledWindow( parent
, id
, pos
, size
,
2275 style
| wxHSCROLL
| wxVSCROLL
, name
)
2279 m_highlightBrush
= new wxBrush
2281 wxSystemSettings::GetSystemColour
2283 wxSYS_COLOUR_HIGHLIGHT
2288 m_highlightUnfocusedBrush
= new wxBrush
2290 wxSystemSettings::GetSystemColour
2292 wxSYS_COLOUR_BTNSHADOW
2301 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2303 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
2306 wxListMainWindow::~wxListMainWindow()
2310 delete m_highlightBrush
;
2311 delete m_highlightUnfocusedBrush
;
2313 delete m_renameTimer
;
2316 void wxListMainWindow::CacheLineData(size_t line
)
2318 wxListCtrl
*listctrl
= GetListCtrl();
2320 wxListLineData
*ld
= GetDummyLine();
2322 size_t countCol
= GetColumnCount();
2323 for ( size_t col
= 0; col
< countCol
; col
++ )
2325 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2328 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2329 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2332 wxListLineData
*wxListMainWindow::GetDummyLine() const
2334 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2336 if ( m_lines
.IsEmpty() )
2338 // normal controls are supposed to have something in m_lines
2339 // already if it's not empty
2340 wxASSERT_MSG( IsVirtual(), _T("logic error") );
2342 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2343 wxListLineData
*line
= new wxListLineData(self
);
2344 self
->m_lines
.Add(line
);
2350 // ----------------------------------------------------------------------------
2351 // line geometry (report mode only)
2352 // ----------------------------------------------------------------------------
2354 wxCoord
wxListMainWindow::GetLineHeight() const
2356 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2358 // we cache the line height as calling GetTextExtent() is slow
2359 if ( !m_lineHeight
)
2361 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2363 wxClientDC
dc( self
);
2364 dc
.SetFont( GetFont() );
2367 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2369 if ( y
< SCROLL_UNIT_Y
)
2373 self
->m_lineHeight
= y
+ LINE_SPACING
;
2376 return m_lineHeight
;
2379 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2381 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2383 return LINE_SPACING
+ line
*GetLineHeight();
2386 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2388 if ( !InReportView() )
2389 return GetLine(line
)->m_gi
->m_rectAll
;
2392 rect
.x
= HEADER_OFFSET_X
;
2393 rect
.y
= GetLineY(line
);
2394 rect
.width
= GetHeaderWidth();
2395 rect
.height
= GetLineHeight();
2400 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2402 if ( !InReportView() )
2403 return GetLine(line
)->m_gi
->m_rectLabel
;
2406 rect
.x
= HEADER_OFFSET_X
;
2407 rect
.y
= GetLineY(line
);
2408 rect
.width
= GetColumnWidth(0);
2409 rect
.height
= GetLineHeight();
2414 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2416 if ( !InReportView() )
2417 return GetLine(line
)->m_gi
->m_rectIcon
;
2419 wxListLineData
*ld
= GetLine(line
);
2420 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2423 rect
.x
= HEADER_OFFSET_X
;
2424 rect
.y
= GetLineY(line
);
2425 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2430 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2432 return InReportView() ? GetLineRect(line
)
2433 : GetLine(line
)->m_gi
->m_rectHighlight
;
2436 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2438 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2440 wxListLineData
*ld
= GetLine(line
);
2442 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2443 return wxLIST_HITTEST_ONITEMICON
;
2445 if ( ld
->HasText() )
2447 wxRect rect
= InReportView() ? GetLineRect(line
)
2448 : GetLineLabelRect(line
);
2450 if ( rect
.Inside(x
, y
) )
2451 return wxLIST_HITTEST_ONITEMLABEL
;
2457 // ----------------------------------------------------------------------------
2458 // highlight (selection) handling
2459 // ----------------------------------------------------------------------------
2461 bool wxListMainWindow::IsHighlighted(size_t line
) const
2465 return m_selStore
.IsSelected(line
);
2469 wxListLineData
*ld
= GetLine(line
);
2470 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2472 return ld
->IsHighlighted();
2476 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2482 wxArrayInt linesChanged
;
2483 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2486 // meny items changed state, refresh everything
2487 RefreshLines(lineFrom
, lineTo
);
2489 else // only a few items changed state, refresh only them
2491 size_t count
= linesChanged
.GetCount();
2492 for ( size_t n
= 0; n
< count
; n
++ )
2494 RefreshLine(linesChanged
[n
]);
2498 else // iterate over all items in non report view
2500 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2502 if ( HighlightLine(line
, highlight
) )
2510 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2516 changed
= m_selStore
.SelectItem(line
, highlight
);
2520 wxListLineData
*ld
= GetLine(line
);
2521 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2523 changed
= ld
->Highlight(highlight
);
2528 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2529 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2535 void wxListMainWindow::RefreshLine( size_t line
)
2537 if ( HasFlag(wxLC_REPORT
) )
2539 size_t visibleFrom
, visibleTo
;
2540 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2542 if ( line
< visibleFrom
|| line
> visibleTo
)
2546 wxRect rect
= GetLineRect(line
);
2548 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2549 RefreshRect( rect
);
2552 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2554 // we suppose that they are ordered by caller
2555 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2557 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2559 if ( HasFlag(wxLC_REPORT
) )
2561 size_t visibleFrom
, visibleTo
;
2562 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2564 if ( lineFrom
< visibleFrom
)
2565 lineFrom
= visibleFrom
;
2566 if ( lineTo
> visibleTo
)
2571 rect
.y
= GetLineY(lineFrom
);
2572 rect
.width
= GetClientSize().x
;
2573 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2575 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2576 RefreshRect( rect
);
2580 // TODO: this should be optimized...
2581 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2588 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2590 if ( HasFlag(wxLC_REPORT
) )
2593 GetVisibleLinesRange(&visibleFrom
, NULL
);
2595 if ( lineFrom
< visibleFrom
)
2596 lineFrom
= visibleFrom
;
2600 rect
.y
= GetLineY(lineFrom
);
2602 wxSize size
= GetClientSize();
2603 rect
.width
= size
.x
;
2604 // refresh till the bottom of the window
2605 rect
.height
= size
.y
- rect
.y
;
2607 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2608 RefreshRect( rect
);
2612 // TODO: how to do it more efficiently?
2617 void wxListMainWindow::RefreshSelected()
2623 if ( InReportView() )
2625 GetVisibleLinesRange(&from
, &to
);
2630 to
= GetItemCount() - 1;
2633 // VZ: this code would work fine if wxGTK wxWindow::Refresh() were
2634 // reasonable, i.e. if it only generated one expose event for
2635 // several calls to it - as it is, each Refresh() results in a
2636 // repaint which provokes flicker too horrible to be seen
2638 // when/if wxGTK is fixed, this code should be restored as normally it
2639 // should generate _less_ flicker than the version below
2641 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2643 RefreshLine(m_current
);
2646 for ( size_t line
= from
; line
<= to
; line
++ )
2648 // NB: the test works as expected even if m_current == -1
2649 if ( line
!= m_current
&& IsHighlighted(line
) )
2655 size_t selMin
= (size_t)-1,
2658 for ( size_t line
= from
; line
<= to
; line
++ )
2660 if ( IsHighlighted(line
) || (line
== m_current
) )
2662 if ( line
< selMin
)
2664 if ( line
> selMax
)
2669 if ( selMin
!= (size_t)-1 )
2671 RefreshLines(selMin
, selMax
);
2673 #endif // !__WXGTK__/__WXGTK__
2676 void wxListMainWindow::Freeze()
2681 void wxListMainWindow::Thaw()
2683 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2685 if ( !--m_freezeCount
)
2691 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2693 // Note: a wxPaintDC must be constructed even if no drawing is
2694 // done (a Windows requirement).
2695 wxPaintDC
dc( this );
2697 if ( IsEmpty() || m_freezeCount
)
2699 // nothing to draw or not the moment to draw it
2705 // delay the repainting until we calculate all the items positions
2712 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2716 dc
.SetFont( GetFont() );
2718 if ( HasFlag(wxLC_REPORT
) )
2720 int lineHeight
= GetLineHeight();
2722 size_t visibleFrom
, visibleTo
;
2723 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2726 wxCoord xOrig
, yOrig
;
2727 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2729 // tell the caller cache to cache the data
2732 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2733 GetParent()->GetId());
2734 evCache
.SetEventObject( GetParent() );
2735 evCache
.m_oldItemIndex
= visibleFrom
;
2736 evCache
.m_itemIndex
= visibleTo
;
2737 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2740 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2742 rectLine
= GetLineRect(line
);
2744 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2745 rectLine
.width
, rectLine
.height
) )
2747 // don't redraw unaffected lines to avoid flicker
2751 GetLine(line
)->DrawInReportMode( &dc
,
2753 GetLineHighlightRect(line
),
2754 IsHighlighted(line
) );
2757 if ( HasFlag(wxLC_HRULES
) )
2759 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2760 wxSize clientSize
= GetClientSize();
2762 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2765 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2766 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2767 clientSize
.x
- dev_x
, i
*lineHeight
);
2770 // Draw last horizontal rule
2771 if ( visibleTo
> visibleFrom
)
2774 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2775 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2776 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2780 // Draw vertical rules if required
2781 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2783 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2786 wxRect firstItemRect
;
2787 wxRect lastItemRect
;
2788 GetItemRect(0, firstItemRect
);
2789 GetItemRect(GetItemCount() - 1, lastItemRect
);
2790 int x
= firstItemRect
.GetX();
2792 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2793 for (col
= 0; col
< GetColumnCount(); col
++)
2795 int colWidth
= GetColumnWidth(col
);
2797 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2798 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2804 size_t count
= GetItemCount();
2805 for ( size_t i
= 0; i
< count
; i
++ )
2807 GetLine(i
)->Draw( &dc
);
2813 // don't draw rect outline under Max if we already have the background
2814 // color but under other platforms only draw it if we do: it is a bit
2815 // silly to draw "focus rect" if we don't have focus!
2820 #endif // __WXMAC__/!__WXMAC__
2822 dc
.SetPen( *wxBLACK_PEN
);
2823 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2824 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2831 void wxListMainWindow::HighlightAll( bool on
)
2833 if ( IsSingleSel() )
2835 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2837 // we just have one item to turn off
2838 if ( HasCurrent() && IsHighlighted(m_current
) )
2840 HighlightLine(m_current
, FALSE
);
2841 RefreshLine(m_current
);
2846 HighlightLines(0, GetItemCount() - 1, on
);
2850 void wxListMainWindow::SendNotify( size_t line
,
2851 wxEventType command
,
2854 wxListEvent
le( command
, GetParent()->GetId() );
2855 le
.SetEventObject( GetParent() );
2856 le
.m_itemIndex
= line
;
2858 // set only for events which have position
2859 if ( point
!= wxDefaultPosition
)
2860 le
.m_pointDrag
= point
;
2862 // don't try to get the line info for virtual list controls: the main
2863 // program has it anyhow and if we did it would result in accessing all
2864 // the lines, even those which are not visible now and this is precisely
2865 // what we're trying to avoid
2866 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2868 GetLine(line
)->GetItem( 0, le
.m_item
);
2870 //else: there may be no more such item
2872 GetParent()->GetEventHandler()->ProcessEvent( le
);
2875 void wxListMainWindow::OnFocusLine( size_t WXUNUSED(line
) )
2877 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
2880 void wxListMainWindow::OnUnfocusLine( size_t WXUNUSED(line
) )
2882 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
2885 void wxListMainWindow::EditLabel( long item
)
2887 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2888 wxT("wrong index in wxListCtrl::EditLabel()") );
2890 m_currentEdit
= (size_t)item
;
2892 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2893 le
.SetEventObject( GetParent() );
2894 le
.m_itemIndex
= item
;
2895 wxListLineData
*data
= GetLine(m_currentEdit
);
2896 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2897 data
->GetItem( 0, le
.m_item
);
2898 GetParent()->GetEventHandler()->ProcessEvent( le
);
2900 if (!le
.IsAllowed())
2903 // We have to call this here because the label in question might just have
2904 // been added and no screen update taken place.
2908 wxClientDC
dc(this);
2911 wxString s
= data
->GetText(0);
2912 wxRect rectLabel
= GetLineLabelRect(m_currentEdit
);
2914 rectLabel
.x
= dc
.LogicalToDeviceX( rectLabel
.x
);
2915 rectLabel
.y
= dc
.LogicalToDeviceY( rectLabel
.y
);
2917 wxListTextCtrl
*text
= new wxListTextCtrl
2924 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2925 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2930 void wxListMainWindow::OnRenameTimer()
2932 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2934 EditLabel( m_current
);
2937 void wxListMainWindow::OnRenameAccept()
2939 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2940 le
.SetEventObject( GetParent() );
2941 le
.m_itemIndex
= m_currentEdit
;
2943 wxListLineData
*data
= GetLine(m_currentEdit
);
2944 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2946 data
->GetItem( 0, le
.m_item
);
2947 le
.m_item
.m_text
= m_renameRes
;
2948 GetParent()->GetEventHandler()->ProcessEvent( le
);
2950 if (!le
.IsAllowed()) return;
2953 info
.m_mask
= wxLIST_MASK_TEXT
;
2954 info
.m_itemId
= le
.m_itemIndex
;
2955 info
.m_text
= m_renameRes
;
2956 info
.SetTextColour(le
.m_item
.GetTextColour());
2960 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2962 event
.SetEventObject( GetParent() );
2963 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2966 if ( !HasCurrent() || IsEmpty() )
2972 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2973 event
.ButtonDClick()) )
2976 int x
= event
.GetX();
2977 int y
= event
.GetY();
2978 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2980 // where did we hit it (if we did)?
2983 size_t count
= GetItemCount(),
2986 if ( HasFlag(wxLC_REPORT
) )
2988 current
= y
/ GetLineHeight();
2989 if ( current
< count
)
2990 hitResult
= HitTestLine(current
, x
, y
);
2994 // TODO: optimize it too! this is less simple than for report view but
2995 // enumerating all items is still not a way to do it!!
2996 for ( current
= 0; current
< count
; current
++ )
2998 hitResult
= HitTestLine(current
, x
, y
);
3004 if (event
.Dragging())
3006 if (m_dragCount
== 0)
3008 // we have to report the raw, physical coords as we want to be
3009 // able to call HitTest(event.m_pointDrag) from the user code to
3010 // get the item being dragged
3011 m_dragStart
= event
.GetPosition();
3016 if (m_dragCount
!= 3)
3019 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3020 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3022 wxListEvent
le( command
, GetParent()->GetId() );
3023 le
.SetEventObject( GetParent() );
3024 le
.m_pointDrag
= m_dragStart
;
3025 GetParent()->GetEventHandler()->ProcessEvent( le
);
3036 // outside of any item
3040 bool forceClick
= FALSE
;
3041 if (event
.ButtonDClick())
3043 m_renameTimer
->Stop();
3044 m_lastOnSame
= FALSE
;
3046 if ( current
== m_lineBeforeLastClicked
)
3048 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3054 // the first click was on another item, so don't interpret this as
3055 // a double click, but as a simple click instead
3060 if (event
.LeftUp() && m_lastOnSame
)
3062 if ((current
== m_current
) &&
3063 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3064 HasFlag(wxLC_EDIT_LABELS
) )
3066 m_renameTimer
->Start( 100, TRUE
);
3068 m_lastOnSame
= FALSE
;
3070 else if (event
.RightDown())
3072 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3073 event
.GetPosition() );
3075 else if (event
.MiddleDown())
3077 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3079 else if ( event
.LeftDown() || forceClick
)
3081 m_lineBeforeLastClicked
= m_lineLastClicked
;
3082 m_lineLastClicked
= current
;
3084 size_t oldCurrent
= m_current
;
3086 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3088 HighlightAll( FALSE
);
3089 m_current
= current
;
3091 ReverseHighlight(m_current
);
3093 else // multi sel & either ctrl or shift is down
3095 if (event
.ControlDown())
3097 m_current
= current
;
3099 ReverseHighlight(m_current
);
3101 else if (event
.ShiftDown())
3103 m_current
= current
;
3105 size_t lineFrom
= oldCurrent
,
3108 if ( lineTo
< lineFrom
)
3111 lineFrom
= m_current
;
3114 HighlightLines(lineFrom
, lineTo
);
3116 else // !ctrl, !shift
3118 // test in the enclosing if should make it impossible
3119 wxFAIL_MSG( _T("how did we get here?") );
3123 if (m_current
!= oldCurrent
)
3125 RefreshLine( oldCurrent
);
3126 OnUnfocusLine( oldCurrent
);
3127 OnFocusLine( m_current
);
3130 // forceClick is only set if the previous click was on another item
3131 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3135 void wxListMainWindow::MoveToItem(size_t item
)
3137 if ( item
== (size_t)-1 )
3140 wxRect rect
= GetLineRect(item
);
3142 int client_w
, client_h
;
3143 GetClientSize( &client_w
, &client_h
);
3145 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3146 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3148 if ( HasFlag(wxLC_REPORT
) )
3150 // the next we need the range of lines shown it might be different, so
3152 ResetVisibleLinesRange();
3154 if (rect
.y
< view_y
)
3155 Scroll( -1, rect
.y
/m_yScroll
);
3156 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3157 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3161 if (rect
.x
-view_x
< 5)
3162 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3163 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3164 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3168 // ----------------------------------------------------------------------------
3169 // keyboard handling
3170 // ----------------------------------------------------------------------------
3172 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3174 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3175 _T("invalid item index in OnArrowChar()") );
3177 size_t oldCurrent
= m_current
;
3179 // in single selection we just ignore Shift as we can't select several
3181 if ( event
.ShiftDown() && !IsSingleSel() )
3183 m_current
= newCurrent
;
3185 // select all the items between the old and the new one
3186 if ( oldCurrent
> newCurrent
)
3188 newCurrent
= oldCurrent
;
3189 oldCurrent
= m_current
;
3192 HighlightLines(oldCurrent
, newCurrent
);
3196 // all previously selected items are unselected unless ctrl is held
3197 if ( !event
.ControlDown() )
3198 HighlightAll(FALSE
);
3200 m_current
= newCurrent
;
3202 HighlightLine( oldCurrent
, FALSE
);
3203 RefreshLine( oldCurrent
);
3205 if ( !event
.ControlDown() )
3207 HighlightLine( m_current
, TRUE
);
3211 OnUnfocusLine( oldCurrent
);
3212 OnFocusLine( m_current
);
3213 RefreshLine( m_current
);
3218 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3220 wxWindow
*parent
= GetParent();
3222 /* we propagate the key event up */
3223 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3224 ke
.m_shiftDown
= event
.m_shiftDown
;
3225 ke
.m_controlDown
= event
.m_controlDown
;
3226 ke
.m_altDown
= event
.m_altDown
;
3227 ke
.m_metaDown
= event
.m_metaDown
;
3228 ke
.m_keyCode
= event
.m_keyCode
;
3231 ke
.SetEventObject( parent
);
3232 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3237 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3239 wxWindow
*parent
= GetParent();
3241 /* we send a list_key event up */
3244 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3245 le
.m_itemIndex
= m_current
;
3246 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3247 le
.m_code
= (int)event
.KeyCode();
3248 le
.SetEventObject( parent
);
3249 parent
->GetEventHandler()->ProcessEvent( le
);
3252 /* we propagate the char event up */
3253 wxKeyEvent
ke( wxEVT_CHAR
);
3254 ke
.m_shiftDown
= event
.m_shiftDown
;
3255 ke
.m_controlDown
= event
.m_controlDown
;
3256 ke
.m_altDown
= event
.m_altDown
;
3257 ke
.m_metaDown
= event
.m_metaDown
;
3258 ke
.m_keyCode
= event
.m_keyCode
;
3261 ke
.SetEventObject( parent
);
3262 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3264 if (event
.KeyCode() == WXK_TAB
)
3266 wxNavigationKeyEvent nevent
;
3267 nevent
.SetWindowChange( event
.ControlDown() );
3268 nevent
.SetDirection( !event
.ShiftDown() );
3269 nevent
.SetEventObject( GetParent()->GetParent() );
3270 nevent
.SetCurrentFocus( m_parent
);
3271 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3275 /* no item -> nothing to do */
3282 switch (event
.KeyCode())
3285 if ( m_current
> 0 )
3286 OnArrowChar( m_current
- 1, event
);
3290 if ( m_current
< (size_t)GetItemCount() - 1 )
3291 OnArrowChar( m_current
+ 1, event
);
3296 OnArrowChar( GetItemCount() - 1, event
);
3301 OnArrowChar( 0, event
);
3307 if ( HasFlag(wxLC_REPORT
) )
3309 steps
= m_linesPerPage
- 1;
3313 steps
= m_current
% m_linesPerPage
;
3316 int index
= m_current
- steps
;
3320 OnArrowChar( index
, event
);
3327 if ( HasFlag(wxLC_REPORT
) )
3329 steps
= m_linesPerPage
- 1;
3333 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3336 size_t index
= m_current
+ steps
;
3337 size_t count
= GetItemCount();
3338 if ( index
>= count
)
3341 OnArrowChar( index
, event
);
3346 if ( !HasFlag(wxLC_REPORT
) )
3348 int index
= m_current
- m_linesPerPage
;
3352 OnArrowChar( index
, event
);
3357 if ( !HasFlag(wxLC_REPORT
) )
3359 size_t index
= m_current
+ m_linesPerPage
;
3361 size_t count
= GetItemCount();
3362 if ( index
>= count
)
3365 OnArrowChar( index
, event
);
3370 if ( IsSingleSel() )
3372 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3374 if ( IsHighlighted(m_current
) )
3376 // don't unselect the item in single selection mode
3379 //else: select it in ReverseHighlight() below if unselected
3382 ReverseHighlight(m_current
);
3387 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3395 // ----------------------------------------------------------------------------
3397 // ----------------------------------------------------------------------------
3400 extern wxWindow
*g_focusWindow
;
3403 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3405 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3406 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3407 // which are already drawn correctly resulting in horrible flicker - avoid
3420 g_focusWindow
= GetParent();
3423 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3424 event
.SetEventObject( GetParent() );
3425 GetParent()->GetEventHandler()->ProcessEvent( event
);
3428 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3435 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3437 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3439 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3441 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3443 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3445 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3447 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3449 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3451 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3455 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3457 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3459 m_normal_image_list
->GetSize( index
, width
, height
);
3461 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3463 m_small_image_list
->GetSize( index
, width
, height
);
3465 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3467 m_small_image_list
->GetSize( index
, width
, height
);
3469 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3471 m_small_image_list
->GetSize( index
, width
, height
);
3480 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3482 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3483 dc
.SetFont( GetFont() );
3486 dc
.GetTextExtent( s
, &lw
, NULL
);
3488 return lw
+ AUTOSIZE_COL_MARGIN
;
3491 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3495 // calc the spacing from the icon size
3498 if ((imageList
) && (imageList
->GetImageCount()) )
3500 imageList
->GetSize(0, width
, height
);
3503 if (which
== wxIMAGE_LIST_NORMAL
)
3505 m_normal_image_list
= imageList
;
3506 m_normal_spacing
= width
+ 8;
3509 if (which
== wxIMAGE_LIST_SMALL
)
3511 m_small_image_list
= imageList
;
3512 m_small_spacing
= width
+ 14;
3516 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3521 m_small_spacing
= spacing
;
3525 m_normal_spacing
= spacing
;
3529 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3531 return isSmall
? m_small_spacing
: m_normal_spacing
;
3534 // ----------------------------------------------------------------------------
3536 // ----------------------------------------------------------------------------
3538 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3540 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3542 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3544 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3545 item
.m_width
= GetTextLength( item
.m_text
);
3547 wxListHeaderData
*column
= node
->GetData();
3548 column
->SetItem( item
);
3550 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3552 headerWin
->m_dirty
= TRUE
;
3556 // invalidate it as it has to be recalculated
3560 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3562 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3563 _T("invalid column index") );
3565 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3566 _T("SetColumnWidth() can only be called in report mode.") );
3569 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3571 headerWin
->m_dirty
= TRUE
;
3573 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3574 wxCHECK_RET( node
, _T("no column?") );
3576 wxListHeaderData
*column
= node
->GetData();
3578 size_t count
= GetItemCount();
3580 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3582 width
= GetTextLength(column
->GetText());
3584 else if ( width
== wxLIST_AUTOSIZE
)
3588 // TODO: determine the max width somehow...
3589 width
= WIDTH_COL_DEFAULT
;
3593 wxClientDC
dc(this);
3594 dc
.SetFont( GetFont() );
3596 int max
= AUTOSIZE_COL_MARGIN
;
3598 for ( size_t i
= 0; i
< count
; i
++ )
3600 wxListLineData
*line
= GetLine(i
);
3601 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3603 wxCHECK_RET( n
, _T("no subitem?") );
3605 wxListItemData
*item
= n
->GetData();
3608 if (item
->HasImage())
3611 GetImageSize( item
->GetImage(), ix
, iy
);
3615 if (item
->HasText())
3618 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3626 width
= max
+ AUTOSIZE_COL_MARGIN
;
3630 column
->SetWidth( width
);
3632 // invalidate it as it has to be recalculated
3636 int wxListMainWindow::GetHeaderWidth() const
3638 if ( !m_headerWidth
)
3640 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3642 size_t count
= GetColumnCount();
3643 for ( size_t col
= 0; col
< count
; col
++ )
3645 self
->m_headerWidth
+= GetColumnWidth(col
);
3649 return m_headerWidth
;
3652 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3654 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3655 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3657 wxListHeaderData
*column
= node
->GetData();
3658 column
->GetItem( item
);
3661 int wxListMainWindow::GetColumnWidth( int col
) const
3663 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3664 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3666 wxListHeaderData
*column
= node
->GetData();
3667 return column
->GetWidth();
3670 // ----------------------------------------------------------------------------
3672 // ----------------------------------------------------------------------------
3674 void wxListMainWindow::SetItem( wxListItem
&item
)
3676 long id
= item
.m_itemId
;
3677 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3678 _T("invalid item index in SetItem") );
3682 wxListLineData
*line
= GetLine((size_t)id
);
3683 line
->SetItem( item
.m_col
, item
);
3686 if ( InReportView() )
3688 // just refresh the line to show the new value of the text/image
3689 RefreshLine((size_t)id
);
3693 // refresh everything (resulting in horrible flicker - FIXME!)
3698 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3700 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3701 _T("invalid list ctrl item index in SetItem") );
3703 size_t oldCurrent
= m_current
;
3704 size_t item
= (size_t)litem
; // safe because of the check above
3706 // do we need to change the focus?
3707 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3709 if ( state
& wxLIST_STATE_FOCUSED
)
3711 // don't do anything if this item is already focused
3712 if ( item
!= m_current
)
3714 OnUnfocusLine( m_current
);
3716 OnFocusLine( m_current
);
3718 if ( oldCurrent
!= (size_t)-1 )
3720 if ( IsSingleSel() )
3722 HighlightLine(oldCurrent
, FALSE
);
3725 RefreshLine(oldCurrent
);
3728 RefreshLine( m_current
);
3733 // don't do anything if this item is not focused
3734 if ( item
== m_current
)
3736 OnUnfocusLine( m_current
);
3737 m_current
= (size_t)-1;
3739 RefreshLine( oldCurrent
);
3744 // do we need to change the selection state?
3745 if ( stateMask
& wxLIST_STATE_SELECTED
)
3747 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3749 if ( IsSingleSel() )
3753 // selecting the item also makes it the focused one in the
3755 if ( m_current
!= item
)
3757 OnUnfocusLine( m_current
);
3759 OnFocusLine( m_current
);
3761 if ( oldCurrent
!= (size_t)-1 )
3763 HighlightLine( oldCurrent
, FALSE
);
3764 RefreshLine( oldCurrent
);
3770 // only the current item may be selected anyhow
3771 if ( item
!= m_current
)
3776 if ( HighlightLine(item
, on
) )
3783 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3785 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3786 _T("invalid list ctrl item index in GetItemState()") );
3788 int ret
= wxLIST_STATE_DONTCARE
;
3790 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3792 if ( (size_t)item
== m_current
)
3793 ret
|= wxLIST_STATE_FOCUSED
;
3796 if ( stateMask
& wxLIST_STATE_SELECTED
)
3798 if ( IsHighlighted(item
) )
3799 ret
|= wxLIST_STATE_SELECTED
;
3805 void wxListMainWindow::GetItem( wxListItem
&item
)
3807 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3808 _T("invalid item index in GetItem") );
3810 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3811 line
->GetItem( item
.m_col
, item
);
3814 // ----------------------------------------------------------------------------
3816 // ----------------------------------------------------------------------------
3818 size_t wxListMainWindow::GetItemCount() const
3820 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3823 void wxListMainWindow::SetItemCount(long count
)
3825 m_selStore
.SetItemCount(count
);
3826 m_countVirt
= count
;
3828 ResetVisibleLinesRange();
3830 // scrollbars must be reset
3834 int wxListMainWindow::GetSelectedItemCount()
3836 // deal with the quick case first
3837 if ( IsSingleSel() )
3839 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3842 // virtual controls remmebers all its selections itself
3844 return m_selStore
.GetSelectedCount();
3846 // TODO: we probably should maintain the number of items selected even for
3847 // non virtual controls as enumerating all lines is really slow...
3848 size_t countSel
= 0;
3849 size_t count
= GetItemCount();
3850 for ( size_t line
= 0; line
< count
; line
++ )
3852 if ( GetLine(line
)->IsHighlighted() )
3859 // ----------------------------------------------------------------------------
3860 // item position/size
3861 // ----------------------------------------------------------------------------
3863 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3865 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3866 _T("invalid index in GetItemRect") );
3868 rect
= GetLineRect((size_t)index
);
3870 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3873 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3876 GetItemRect(item
, rect
);
3884 // ----------------------------------------------------------------------------
3885 // geometry calculation
3886 // ----------------------------------------------------------------------------
3888 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3890 wxClientDC
dc( this );
3891 dc
.SetFont( GetFont() );
3894 if ( HasFlag(wxLC_ICON
) )
3895 iconSpacing
= m_normal_spacing
;
3896 else if ( HasFlag(wxLC_SMALL_ICON
) )
3897 iconSpacing
= m_small_spacing
;
3903 GetClientSize( &clientWidth
, &clientHeight
);
3905 if ( HasFlag(wxLC_REPORT
) )
3907 // all lines have the same height
3908 int lineHeight
= GetLineHeight();
3910 // scroll one line per step
3911 m_yScroll
= lineHeight
;
3913 size_t lineCount
= GetItemCount();
3914 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3916 m_linesPerPage
= clientHeight
/ lineHeight
;
3918 ResetVisibleLinesRange();
3920 SetScrollbars( m_xScroll
, m_yScroll
,
3921 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3922 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3923 GetScrollPos(wxHORIZONTAL
),
3924 GetScrollPos(wxVERTICAL
),
3929 // at first we try without any scrollbar. if the items don't
3930 // fit into the window, we recalculate after subtracting an
3931 // approximated 15 pt for the horizontal scrollbar
3933 clientHeight
-= 4; // sunken frame
3935 int entireWidth
= 0;
3937 for (int tries
= 0; tries
< 2; tries
++)
3944 int currentlyVisibleLines
= 0;
3946 size_t count
= GetItemCount();
3947 for (size_t i
= 0; i
< count
; i
++)
3949 currentlyVisibleLines
++;
3950 wxListLineData
*line
= GetLine(i
);
3951 line
->CalculateSize( &dc
, iconSpacing
);
3952 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
);
3954 wxSize sizeLine
= GetLineSize(i
);
3956 if ( maxWidth
< sizeLine
.x
)
3957 maxWidth
= sizeLine
.x
;
3960 if (currentlyVisibleLines
> m_linesPerPage
)
3961 m_linesPerPage
= currentlyVisibleLines
;
3963 // assume that the size of the next one is the same... (FIXME)
3964 if ( y
+ sizeLine
.y
- 6 >= clientHeight
)
3966 currentlyVisibleLines
= 0;
3969 entireWidth
+= maxWidth
+6;
3972 if ( i
== count
- 1 )
3973 entireWidth
+= maxWidth
;
3974 if ((tries
== 0) && (entireWidth
> clientWidth
))
3976 clientHeight
-= 15; // scrollbar height
3978 currentlyVisibleLines
= 0;
3981 if ( i
== count
- 1 )
3982 tries
= 1; // everything fits, no second try required
3986 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3987 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3992 // FIXME: why should we call it from here?
3999 void wxListMainWindow::RefreshAll()
4004 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4005 if ( headerWin
&& headerWin
->m_dirty
)
4007 headerWin
->m_dirty
= FALSE
;
4008 headerWin
->Refresh();
4012 void wxListMainWindow::UpdateCurrent()
4014 if ( !HasCurrent() && !IsEmpty() )
4019 if ( m_current
!= (size_t)-1 )
4021 OnFocusLine( m_current
);
4025 long wxListMainWindow::GetNextItem( long item
,
4026 int WXUNUSED(geometry
),
4030 max
= GetItemCount();
4031 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4032 _T("invalid listctrl index in GetNextItem()") );
4034 // notice that we start with the next item (or the first one if item == -1)
4035 // and this is intentional to allow writing a simple loop to iterate over
4036 // all selected items
4040 // this is not an error because the index was ok initially, just no
4051 size_t count
= GetItemCount();
4052 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4054 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4057 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4064 // ----------------------------------------------------------------------------
4066 // ----------------------------------------------------------------------------
4068 void wxListMainWindow::DeleteItem( long lindex
)
4070 size_t count
= GetItemCount();
4072 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4073 _T("invalid item index in DeleteItem") );
4075 size_t index
= (size_t)lindex
;
4077 // we don't need to adjust the index for the previous items
4078 if ( HasCurrent() && m_current
>= index
)
4080 // if the current item is being deleted, we want the next one to
4081 // become selected - unless there is no next one - so don't adjust
4082 // m_current in this case
4083 if ( m_current
!= index
|| m_current
== count
- 1 )
4089 if ( InReportView() )
4091 ResetVisibleLinesRange();
4098 m_selStore
.OnItemDelete(index
);
4102 m_lines
.RemoveAt( index
);
4105 // we need to refresh the (vert) scrollbar as the number of items changed
4108 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4110 RefreshAfter(index
);
4113 void wxListMainWindow::DeleteColumn( int col
)
4115 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4117 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4120 m_columns
.DeleteNode( node
);
4123 void wxListMainWindow::DoDeleteAllItems()
4127 // nothing to do - in particular, don't send the event
4133 // to make the deletion of all items faster, we don't send the
4134 // notifications for each item deletion in this case but only one event
4135 // for all of them: this is compatible with wxMSW and documented in
4136 // DeleteAllItems() description
4138 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4139 event
.SetEventObject( GetParent() );
4140 GetParent()->GetEventHandler()->ProcessEvent( event
);
4149 if ( InReportView() )
4151 ResetVisibleLinesRange();
4157 void wxListMainWindow::DeleteAllItems()
4161 RecalculatePositions();
4164 void wxListMainWindow::DeleteEverything()
4171 // ----------------------------------------------------------------------------
4172 // scanning for an item
4173 // ----------------------------------------------------------------------------
4175 void wxListMainWindow::EnsureVisible( long index
)
4177 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4178 _T("invalid index in EnsureVisible") );
4180 // We have to call this here because the label in question might just have
4181 // been added and its position is not known yet
4184 RecalculatePositions(TRUE
/* no refresh */);
4187 MoveToItem((size_t)index
);
4190 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4197 size_t count
= GetItemCount();
4198 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4200 wxListLineData
*line
= GetLine(i
);
4201 if ( line
->GetText(0) == tmp
)
4208 long wxListMainWindow::FindItem(long start
, long data
)
4214 size_t count
= GetItemCount();
4215 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4217 wxListLineData
*line
= GetLine(i
);
4219 line
->GetItem( 0, item
);
4220 if (item
.m_data
== data
)
4227 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4229 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4231 size_t count
= GetItemCount();
4233 if ( HasFlag(wxLC_REPORT
) )
4235 size_t current
= y
/ GetLineHeight();
4236 if ( current
< count
)
4238 flags
= HitTestLine(current
, x
, y
);
4245 // TODO: optimize it too! this is less simple than for report view but
4246 // enumerating all items is still not a way to do it!!
4247 for ( size_t current
= 0; current
< count
; current
++ )
4249 flags
= HitTestLine(current
, x
, y
);
4258 // ----------------------------------------------------------------------------
4260 // ----------------------------------------------------------------------------
4262 void wxListMainWindow::InsertItem( wxListItem
&item
)
4264 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4266 size_t count
= GetItemCount();
4267 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4268 _T("invalid item index") );
4270 size_t id
= item
.m_itemId
;
4275 if ( HasFlag(wxLC_REPORT
) )
4277 else if ( HasFlag(wxLC_LIST
) )
4279 else if ( HasFlag(wxLC_ICON
) )
4281 else if ( HasFlag(wxLC_SMALL_ICON
) )
4282 mode
= wxLC_ICON
; // no typo
4285 wxFAIL_MSG( _T("unknown mode") );
4288 wxListLineData
*line
= new wxListLineData(this);
4290 line
->SetItem( 0, item
);
4292 m_lines
.Insert( line
, id
);
4295 RefreshLines(id
, GetItemCount() - 1);
4298 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4301 if ( HasFlag(wxLC_REPORT
) )
4303 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4304 item
.m_width
= GetTextLength( item
.m_text
);
4305 wxListHeaderData
*column
= new wxListHeaderData( item
);
4306 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4308 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4309 m_columns
.Insert( node
, column
);
4313 m_columns
.Append( column
);
4318 // ----------------------------------------------------------------------------
4320 // ----------------------------------------------------------------------------
4322 wxListCtrlCompare list_ctrl_compare_func_2
;
4323 long list_ctrl_compare_data
;
4325 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4327 wxListLineData
*line1
= *arg1
;
4328 wxListLineData
*line2
= *arg2
;
4330 line1
->GetItem( 0, item
);
4331 long data1
= item
.m_data
;
4332 line2
->GetItem( 0, item
);
4333 long data2
= item
.m_data
;
4334 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4337 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4339 list_ctrl_compare_func_2
= fn
;
4340 list_ctrl_compare_data
= data
;
4341 m_lines
.Sort( list_ctrl_compare_func_1
);
4345 // ----------------------------------------------------------------------------
4347 // ----------------------------------------------------------------------------
4349 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4351 // update our idea of which lines are shown when we redraw the window the
4353 ResetVisibleLinesRange();
4356 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4357 wxScrolledWindow::OnScroll(event
);
4359 HandleOnScroll( event
);
4362 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4364 wxListCtrl
* lc
= GetListCtrl();
4365 wxCHECK_RET( lc
, _T("no listctrl window?") );
4367 lc
->m_headerWin
->Refresh() ;
4369 lc
->m_headerWin
->MacUpdateImmediately() ;
4374 int wxListMainWindow::GetCountPerPage() const
4376 if ( !m_linesPerPage
)
4378 wxConstCast(this, wxListMainWindow
)->
4379 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4382 return m_linesPerPage
;
4385 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4387 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4389 if ( m_lineFrom
== (size_t)-1 )
4391 size_t count
= GetItemCount();
4394 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4396 // this may happen if SetScrollbars() hadn't been called yet
4397 if ( m_lineFrom
>= count
)
4398 m_lineFrom
= count
- 1;
4400 // we redraw one extra line but this is needed to make the redrawing
4401 // logic work when there is a fractional number of lines on screen
4402 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4403 if ( m_lineTo
>= count
)
4404 m_lineTo
= count
- 1;
4406 else // empty control
4409 m_lineTo
= (size_t)-1;
4413 wxASSERT_MSG( IsEmpty() ||
4414 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4415 _T("GetVisibleLinesRange() returns incorrect result") );
4423 // -------------------------------------------------------------------------------------
4425 // -------------------------------------------------------------------------------------
4427 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4429 wxListItem::wxListItem()
4436 void wxListItem::Clear()
4445 m_format
= wxLIST_FORMAT_CENTRE
;
4452 void wxListItem::ClearAttributes()
4461 // -------------------------------------------------------------------------------------
4463 // -------------------------------------------------------------------------------------
4465 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4467 wxListEvent::wxListEvent( wxEventType commandType
, int id
)
4468 : wxNotifyEvent( commandType
, id
)
4474 m_cancelled
= FALSE
;
4479 void wxListEvent::CopyObject(wxObject
& object_dest
) const
4481 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
4483 wxNotifyEvent::CopyObject(object_dest
);
4485 obj
->m_code
= m_code
;
4486 obj
->m_itemIndex
= m_itemIndex
;
4487 obj
->m_oldItemIndex
= m_oldItemIndex
;
4489 obj
->m_cancelled
= m_cancelled
;
4490 obj
->m_pointDrag
= m_pointDrag
;
4491 obj
->m_item
.m_mask
= m_item
.m_mask
;
4492 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
4493 obj
->m_item
.m_col
= m_item
.m_col
;
4494 obj
->m_item
.m_state
= m_item
.m_state
;
4495 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
4496 obj
->m_item
.m_text
= m_item
.m_text
;
4497 obj
->m_item
.m_image
= m_item
.m_image
;
4498 obj
->m_item
.m_data
= m_item
.m_data
;
4499 obj
->m_item
.m_format
= m_item
.m_format
;
4500 obj
->m_item
.m_width
= m_item
.m_width
;
4502 if ( m_item
.HasAttributes() )
4504 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
4508 // -------------------------------------------------------------------------------------
4510 // -------------------------------------------------------------------------------------
4512 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4513 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4515 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4516 EVT_SIZE(wxListCtrl::OnSize
)
4517 EVT_IDLE(wxListCtrl::OnIdle
)
4520 wxListCtrl::wxListCtrl()
4522 m_imageListNormal
= (wxImageList
*) NULL
;
4523 m_imageListSmall
= (wxImageList
*) NULL
;
4524 m_imageListState
= (wxImageList
*) NULL
;
4526 m_ownsImageListNormal
=
4527 m_ownsImageListSmall
=
4528 m_ownsImageListState
= FALSE
;
4530 m_mainWin
= (wxListMainWindow
*) NULL
;
4531 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4534 wxListCtrl::~wxListCtrl()
4537 m_mainWin
->ResetCurrent();
4539 if (m_ownsImageListNormal
)
4540 delete m_imageListNormal
;
4541 if (m_ownsImageListSmall
)
4542 delete m_imageListSmall
;
4543 if (m_ownsImageListState
)
4544 delete m_imageListState
;
4547 void wxListCtrl::CreateHeaderWindow()
4549 m_headerWin
= new wxListHeaderWindow
4551 this, -1, m_mainWin
,
4553 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4558 bool wxListCtrl::Create(wxWindow
*parent
,
4563 const wxValidator
&validator
,
4564 const wxString
&name
)
4568 m_imageListState
= (wxImageList
*) NULL
;
4569 m_ownsImageListNormal
=
4570 m_ownsImageListSmall
=
4571 m_ownsImageListState
= FALSE
;
4573 m_mainWin
= (wxListMainWindow
*) NULL
;
4574 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4576 if ( !(style
& wxLC_MASK_TYPE
) )
4578 style
= style
| wxLC_LIST
;
4581 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4584 // don't create the inner window with the border
4585 style
&= ~wxSUNKEN_BORDER
;
4587 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4589 if ( HasFlag(wxLC_REPORT
) )
4591 CreateHeaderWindow();
4593 if ( HasFlag(wxLC_NO_HEADER
) )
4595 // VZ: why do we create it at all then?
4596 m_headerWin
->Show( FALSE
);
4603 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4605 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4606 _T("wxLC_VIRTUAL can't be [un]set") );
4608 long flag
= GetWindowStyle();
4612 if (style
& wxLC_MASK_TYPE
)
4613 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4614 if (style
& wxLC_MASK_ALIGN
)
4615 flag
&= ~wxLC_MASK_ALIGN
;
4616 if (style
& wxLC_MASK_SORT
)
4617 flag
&= ~wxLC_MASK_SORT
;
4629 SetWindowStyleFlag( flag
);
4632 void wxListCtrl::SetWindowStyleFlag( long flag
)
4636 m_mainWin
->DeleteEverything();
4638 // has the header visibility changed?
4639 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4640 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4642 if ( hasHeader
!= willHaveHeader
)
4649 // don't delete, just hide, as we can reuse it later
4650 m_headerWin
->Show(FALSE
);
4652 //else: nothing to do
4654 else // must show header
4658 CreateHeaderWindow();
4660 else // already have it, just show
4662 m_headerWin
->Show( TRUE
);
4666 ResizeReportView(willHaveHeader
);
4670 wxWindow::SetWindowStyleFlag( flag
);
4673 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4675 m_mainWin
->GetColumn( col
, item
);
4679 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4681 m_mainWin
->SetColumn( col
, item
);
4685 int wxListCtrl::GetColumnWidth( int col
) const
4687 return m_mainWin
->GetColumnWidth( col
);
4690 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4692 m_mainWin
->SetColumnWidth( col
, width
);
4696 int wxListCtrl::GetCountPerPage() const
4698 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4701 bool wxListCtrl::GetItem( wxListItem
&info
) const
4703 m_mainWin
->GetItem( info
);
4707 bool wxListCtrl::SetItem( wxListItem
&info
)
4709 m_mainWin
->SetItem( info
);
4713 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4716 info
.m_text
= label
;
4717 info
.m_mask
= wxLIST_MASK_TEXT
;
4718 info
.m_itemId
= index
;
4722 info
.m_image
= imageId
;
4723 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4725 m_mainWin
->SetItem(info
);
4729 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4731 return m_mainWin
->GetItemState( item
, stateMask
);
4734 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4736 m_mainWin
->SetItemState( item
, state
, stateMask
);
4740 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4743 info
.m_image
= image
;
4744 info
.m_mask
= wxLIST_MASK_IMAGE
;
4745 info
.m_itemId
= item
;
4746 m_mainWin
->SetItem( info
);
4750 wxString
wxListCtrl::GetItemText( long item
) const
4753 info
.m_itemId
= item
;
4754 m_mainWin
->GetItem( info
);
4758 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4761 info
.m_mask
= wxLIST_MASK_TEXT
;
4762 info
.m_itemId
= item
;
4764 m_mainWin
->SetItem( info
);
4767 long wxListCtrl::GetItemData( long item
) const
4770 info
.m_itemId
= item
;
4771 m_mainWin
->GetItem( info
);
4775 bool wxListCtrl::SetItemData( long item
, long data
)
4778 info
.m_mask
= wxLIST_MASK_DATA
;
4779 info
.m_itemId
= item
;
4781 m_mainWin
->SetItem( info
);
4785 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4787 m_mainWin
->GetItemRect( item
, rect
);
4791 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4793 m_mainWin
->GetItemPosition( item
, pos
);
4797 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4802 int wxListCtrl::GetItemCount() const
4804 return m_mainWin
->GetItemCount();
4807 int wxListCtrl::GetColumnCount() const
4809 return m_mainWin
->GetColumnCount();
4812 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4814 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4817 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4819 return m_mainWin
->GetItemSpacing( isSmall
);
4822 int wxListCtrl::GetSelectedItemCount() const
4824 return m_mainWin
->GetSelectedItemCount();
4827 wxColour
wxListCtrl::GetTextColour() const
4829 return GetForegroundColour();
4832 void wxListCtrl::SetTextColour(const wxColour
& col
)
4834 SetForegroundColour(col
);
4837 long wxListCtrl::GetTopItem() const
4842 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4844 return m_mainWin
->GetNextItem( item
, geom
, state
);
4847 wxImageList
*wxListCtrl::GetImageList(int which
) const
4849 if (which
== wxIMAGE_LIST_NORMAL
)
4851 return m_imageListNormal
;
4853 else if (which
== wxIMAGE_LIST_SMALL
)
4855 return m_imageListSmall
;
4857 else if (which
== wxIMAGE_LIST_STATE
)
4859 return m_imageListState
;
4861 return (wxImageList
*) NULL
;
4864 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4866 if ( which
== wxIMAGE_LIST_NORMAL
)
4868 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4869 m_imageListNormal
= imageList
;
4870 m_ownsImageListNormal
= FALSE
;
4872 else if ( which
== wxIMAGE_LIST_SMALL
)
4874 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4875 m_imageListSmall
= imageList
;
4876 m_ownsImageListSmall
= FALSE
;
4878 else if ( which
== wxIMAGE_LIST_STATE
)
4880 if (m_ownsImageListState
) delete m_imageListState
;
4881 m_imageListState
= imageList
;
4882 m_ownsImageListState
= FALSE
;
4885 m_mainWin
->SetImageList( imageList
, which
);
4888 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4890 SetImageList(imageList
, which
);
4891 if ( which
== wxIMAGE_LIST_NORMAL
)
4892 m_ownsImageListNormal
= TRUE
;
4893 else if ( which
== wxIMAGE_LIST_SMALL
)
4894 m_ownsImageListSmall
= TRUE
;
4895 else if ( which
== wxIMAGE_LIST_STATE
)
4896 m_ownsImageListState
= TRUE
;
4899 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4904 bool wxListCtrl::DeleteItem( long item
)
4906 m_mainWin
->DeleteItem( item
);
4910 bool wxListCtrl::DeleteAllItems()
4912 m_mainWin
->DeleteAllItems();
4916 bool wxListCtrl::DeleteAllColumns()
4918 size_t count
= m_mainWin
->m_columns
.GetCount();
4919 for ( size_t n
= 0; n
< count
; n
++ )
4925 void wxListCtrl::ClearAll()
4927 m_mainWin
->DeleteEverything();
4930 bool wxListCtrl::DeleteColumn( int col
)
4932 m_mainWin
->DeleteColumn( col
);
4936 void wxListCtrl::Edit( long item
)
4938 m_mainWin
->EditLabel( item
);
4941 bool wxListCtrl::EnsureVisible( long item
)
4943 m_mainWin
->EnsureVisible( item
);
4947 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4949 return m_mainWin
->FindItem( start
, str
, partial
);
4952 long wxListCtrl::FindItem( long start
, long data
)
4954 return m_mainWin
->FindItem( start
, data
);
4957 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4958 int WXUNUSED(direction
))
4963 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4965 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4968 long wxListCtrl::InsertItem( wxListItem
& info
)
4970 m_mainWin
->InsertItem( info
);
4971 return info
.m_itemId
;
4974 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
4977 info
.m_text
= label
;
4978 info
.m_mask
= wxLIST_MASK_TEXT
;
4979 info
.m_itemId
= index
;
4980 return InsertItem( info
);
4983 long wxListCtrl::InsertItem( long index
, int imageIndex
)
4986 info
.m_mask
= wxLIST_MASK_IMAGE
;
4987 info
.m_image
= imageIndex
;
4988 info
.m_itemId
= index
;
4989 return InsertItem( info
);
4992 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4995 info
.m_text
= label
;
4996 info
.m_image
= imageIndex
;
4997 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4998 info
.m_itemId
= index
;
4999 return InsertItem( info
);
5002 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
5004 wxASSERT( m_headerWin
);
5005 m_mainWin
->InsertColumn( col
, item
);
5006 m_headerWin
->Refresh();
5011 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
5012 int format
, int width
)
5015 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5016 item
.m_text
= heading
;
5019 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5020 item
.m_width
= width
;
5022 item
.m_format
= format
;
5024 return InsertColumn( col
, item
);
5027 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5033 // fn is a function which takes 3 long arguments: item1, item2, data.
5034 // item1 is the long data associated with a first item (NOT the index).
5035 // item2 is the long data associated with a second item (NOT the index).
5036 // data is the same value as passed to SortItems.
5037 // The return value is a negative number if the first item should precede the second
5038 // item, a positive number of the second item should precede the first,
5039 // or zero if the two items are equivalent.
5040 // data is arbitrary data to be passed to the sort function.
5042 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5044 m_mainWin
->SortItems( fn
, data
);
5048 // ----------------------------------------------------------------------------
5050 // ----------------------------------------------------------------------------
5052 void wxListCtrl::OnSize(wxSizeEvent
& event
)
5057 ResizeReportView(m_mainWin
->HasHeader());
5059 m_mainWin
->RecalculatePositions();
5062 void wxListCtrl::ResizeReportView(bool showHeader
)
5065 GetClientSize( &cw
, &ch
);
5069 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5070 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5072 else // no header window
5074 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5078 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5082 // do it only if needed
5083 if ( !m_mainWin
->m_dirty
)
5086 m_mainWin
->RecalculatePositions();
5089 // ----------------------------------------------------------------------------
5091 // ----------------------------------------------------------------------------
5093 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5097 m_mainWin
->SetBackgroundColour( colour
);
5098 m_mainWin
->m_dirty
= TRUE
;
5104 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5106 if ( !wxWindow::SetForegroundColour( colour
) )
5111 m_mainWin
->SetForegroundColour( colour
);
5112 m_mainWin
->m_dirty
= TRUE
;
5117 m_headerWin
->SetForegroundColour( colour
);
5123 bool wxListCtrl::SetFont( const wxFont
&font
)
5125 if ( !wxWindow::SetFont( font
) )
5130 m_mainWin
->SetFont( font
);
5131 m_mainWin
->m_dirty
= TRUE
;
5136 m_headerWin
->SetFont( font
);
5142 // ----------------------------------------------------------------------------
5143 // methods forwarded to m_mainWin
5144 // ----------------------------------------------------------------------------
5146 #if wxUSE_DRAG_AND_DROP
5148 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5150 m_mainWin
->SetDropTarget( dropTarget
);
5153 wxDropTarget
*wxListCtrl::GetDropTarget() const
5155 return m_mainWin
->GetDropTarget();
5158 #endif // wxUSE_DRAG_AND_DROP
5160 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5162 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5165 wxColour
wxListCtrl::GetBackgroundColour() const
5167 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5170 wxColour
wxListCtrl::GetForegroundColour() const
5172 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5175 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5178 return m_mainWin
->PopupMenu( menu
, x
, y
);
5181 #endif // wxUSE_MENUS
5184 void wxListCtrl::SetFocus()
5186 /* The test in window.cpp fails as we are a composite
5187 window, so it checks against "this", but not m_mainWin. */
5188 if ( FindFocus() != this )
5189 m_mainWin
->SetFocus();
5192 // ----------------------------------------------------------------------------
5193 // virtual list control support
5194 // ----------------------------------------------------------------------------
5196 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
5198 // this is a pure virtual function, in fact - which is not really pure
5199 // because the controls which are not virtual don't need to implement it
5200 wxFAIL_MSG( _T("not supposed to be called") );
5202 return wxEmptyString
;
5205 int wxListCtrl::OnGetItemImage(long item
) const
5208 wxFAIL_MSG( _T("not supposed to be called") );
5213 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5215 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5216 _T("invalid item index in OnGetItemAttr()") );
5218 // no attributes by default
5222 void wxListCtrl::SetItemCount(long count
)
5224 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5226 m_mainWin
->SetItemCount(count
);
5229 void wxListCtrl::RefreshItem(long item
)
5231 m_mainWin
->RefreshLine(item
);
5234 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5236 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5239 void wxListCtrl::Freeze()
5241 m_mainWin
->Freeze();
5244 void wxListCtrl::Thaw()
5249 #endif // wxUSE_LISTCTRL