1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 #include "wx/imaglist.h"
51 #include "wx/listctrl.h"
53 #if defined(__WXGTK__)
55 #include "wx/gtk/win_gtk.h"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
68 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
69 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
77 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the height of the header window (FIXME: should depend on its font!)
90 static const int HEADER_HEIGHT
= 23;
92 // the scrollbar units
93 static const int SCROLL_UNIT_X
= 15;
94 static const int SCROLL_UNIT_Y
= 15;
96 // the spacing between the lines (in report mode)
97 static const int LINE_SPACING
= 0;
99 // extra margins around the text label
100 static const int EXTRA_WIDTH
= 3;
101 static const int EXTRA_HEIGHT
= 4;
103 // offset for the header window
104 static const int HEADER_OFFSET_X
= 1;
105 static const int HEADER_OFFSET_Y
= 1;
107 // when autosizing the columns, add some slack
108 static const int AUTOSIZE_COL_MARGIN
= 10;
110 // default and minimal widths for the header columns
111 static const int WIDTH_COL_DEFAULT
= 80;
112 static const int WIDTH_COL_MIN
= 10;
114 // the space between the image and the text in the report mode
115 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
127 WX_DEFINE_SORTED_EXPORTED_ARRAY(size_t, wxIndexArray
);
129 // this class is used to store the selected items in the virtual list control
130 // (but it is not tied to list control and so can be used with other controls
131 // such as wxListBox in wxUniv)
133 // the idea is to make it really smart later (i.e. store the selections as an
134 // array of ranes + individual items) but, as I don't have time to do it now
135 // (this would require writing code to merge/break ranges and much more) keep
136 // it simple but define a clean interface to it which allows it to be made
138 class WXDLLEXPORT wxSelectionStore
141 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
143 // set the total number of items we handle
144 void SetItemCount(size_t count
) { m_count
= count
; }
146 // special case of SetItemCount(0)
147 void Clear() { m_itemsSel
.Clear(); m_count
= 0; }
149 // must be called when a new item is inserted/added
150 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
152 // must be called when an item is deleted
153 void OnItemDelete(size_t item
);
155 // select one item, use SelectRange() insted if possible!
157 // returns true if the items selection really changed
158 bool SelectItem(size_t item
, bool select
= TRUE
);
160 // select the range of items
162 // return true and fill the itemsChanged array with the indices of items
163 // which have changed state if "few" of them did, otherwise return false
164 // (meaning that too many items changed state to bother counting them
166 bool SelectRange(size_t itemFrom
, size_t itemTo
,
168 wxArrayInt
*itemsChanged
= NULL
);
170 // return true if the given item is selected
171 bool IsSelected(size_t item
) const;
173 // return the total number of selected items
174 size_t GetSelectedCount() const
176 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
177 : m_itemsSel
.GetCount();
182 void Init() { m_defaultState
= FALSE
; }
184 // the total number of items we handle
187 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
188 // there are more selected items than non selected ones - this allows to
189 // handle selection of all items efficiently
192 // the array of items whose selection state is different from default
193 wxIndexArray m_itemsSel
;
195 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
198 //-----------------------------------------------------------------------------
199 // wxListItemData (internal)
200 //-----------------------------------------------------------------------------
202 class WXDLLEXPORT wxListItemData
205 wxListItemData(wxListMainWindow
*owner
);
208 void SetItem( const wxListItem
&info
);
209 void SetImage( int image
) { m_image
= image
; }
210 void SetData( long data
) { m_data
= data
; }
211 void SetPosition( int x
, int y
);
212 void SetSize( int width
, int height
);
214 bool HasText() const { return !m_text
.empty(); }
215 const wxString
& GetText() const { return m_text
; }
216 void SetText(const wxString
& text
) { m_text
= text
; }
218 // we can't use empty string for measuring the string width/height, so
219 // always return something
220 wxString
GetTextForMeasuring() const
222 wxString s
= GetText();
229 bool IsHit( int x
, int y
) const;
233 int GetWidth() const;
234 int GetHeight() const;
236 int GetImage() const { return m_image
; }
237 bool HasImage() const { return GetImage() != -1; }
239 void GetItem( wxListItem
&info
) const;
241 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
242 wxListItemAttr
*GetAttr() const { return m_attr
; }
245 // the item image or -1
248 // user data associated with the item
251 // the item coordinates are not used in report mode, instead this pointer
252 // is NULL and the owner window is used to retrieve the item position and
256 // the list ctrl we are in
257 wxListMainWindow
*m_owner
;
259 // custom attributes or NULL
260 wxListItemAttr
*m_attr
;
263 // common part of all ctors
269 //-----------------------------------------------------------------------------
270 // wxListHeaderData (internal)
271 //-----------------------------------------------------------------------------
273 class WXDLLEXPORT wxListHeaderData
: public wxObject
277 wxListHeaderData( const wxListItem
&info
);
278 void SetItem( const wxListItem
&item
);
279 void SetPosition( int x
, int y
);
280 void SetWidth( int w
);
281 void SetFormat( int format
);
282 void SetHeight( int h
);
283 bool HasImage() const;
285 bool HasText() const { return !m_text
.empty(); }
286 const wxString
& GetText() const { return m_text
; }
287 void SetText(const wxString
& text
) { m_text
= text
; }
289 void GetItem( wxListItem
&item
);
291 bool IsHit( int x
, int y
) const;
292 int GetImage() const;
293 int GetWidth() const;
294 int GetFormat() const;
310 //-----------------------------------------------------------------------------
311 // wxListLineData (internal)
312 //-----------------------------------------------------------------------------
314 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
315 #include "wx/listimpl.cpp"
316 WX_DEFINE_LIST(wxListItemDataList
);
318 class WXDLLEXPORT wxListLineData
321 // the list of subitems: only may have more than one item in report mode
322 wxListItemDataList m_items
;
324 // this is not used in report view
336 // the part to be highlighted
337 wxRect m_rectHighlight
;
340 // is this item selected? [NB: not used in virtual mode]
343 // back pointer to the list ctrl
344 wxListMainWindow
*m_owner
;
347 wxListLineData(wxListMainWindow
*owner
);
349 ~wxListLineData() { delete m_gi
; }
351 // are we in report mode?
352 inline bool InReportView() const;
354 // are we in virtual report mode?
355 inline bool IsVirtual() const;
357 // these 2 methods shouldn't be called for report view controls, in that
358 // case we determine our position/size ourselves
360 // calculate the size of the line
361 void CalculateSize( wxDC
*dc
, int spacing
);
363 // remember the position this line appears at
364 void SetPosition( int x
, int y
, int window_width
, int spacing
);
368 void SetImage( int image
) { SetImage(0, image
); }
369 int GetImage() const { return GetImage(0); }
370 bool HasImage() const { return GetImage() != -1; }
371 bool HasText() const { return !GetText(0).empty(); }
373 void SetItem( int index
, const wxListItem
&info
);
374 void GetItem( int index
, wxListItem
&info
);
376 wxString
GetText(int index
) const;
377 void SetText( int index
, const wxString s
);
379 wxListItemAttr
*GetAttr() const;
380 void SetAttr(wxListItemAttr
*attr
);
382 // return true if the highlighting really changed
383 bool Highlight( bool on
);
385 void ReverseHighlight();
387 bool IsHighlighted() const
389 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
391 return m_highlighted
;
394 // draw the line on the given DC in icon/list mode
395 void Draw( wxDC
*dc
);
397 // the same in report mode
398 void DrawInReportMode( wxDC
*dc
,
400 const wxRect
& rectHL
,
404 // set the line to contain num items (only can be > 1 in report mode)
405 void InitItems( int num
);
407 // get the mode (i.e. style) of the list control
408 inline int GetMode() const;
410 // prepare the DC for drawing with these item's attributes, return true if
411 // we need to draw the items background to highlight it, false otherwise
412 bool SetAttributes(wxDC
*dc
,
413 const wxListItemAttr
*attr
,
416 // these are only used by GetImage/SetImage above, we don't support images
417 // with subitems at the public API level yet
418 void SetImage( int index
, int image
);
419 int GetImage( int index
) const;
422 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
423 #include "wx/arrimpl.cpp"
424 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
426 //-----------------------------------------------------------------------------
427 // wxListHeaderWindow (internal)
428 //-----------------------------------------------------------------------------
430 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
433 wxListMainWindow
*m_owner
;
434 wxCursor
*m_currentCursor
;
435 wxCursor
*m_resizeCursor
;
438 // column being resized
441 // divider line position in logical (unscrolled) coords
444 // minimal position beyond which the divider line can't be dragged in
449 wxListHeaderWindow();
451 wxListHeaderWindow( wxWindow
*win
,
453 wxListMainWindow
*owner
,
454 const wxPoint
&pos
= wxDefaultPosition
,
455 const wxSize
&size
= wxDefaultSize
,
457 const wxString
&name
= "wxlistctrlcolumntitles" );
459 virtual ~wxListHeaderWindow();
461 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
463 void AdjustDC(wxDC
& dc
);
465 void OnPaint( wxPaintEvent
&event
);
466 void OnMouse( wxMouseEvent
&event
);
467 void OnSetFocus( wxFocusEvent
&event
);
473 // common part of all ctors
476 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
477 DECLARE_EVENT_TABLE()
480 //-----------------------------------------------------------------------------
481 // wxListRenameTimer (internal)
482 //-----------------------------------------------------------------------------
484 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
487 wxListMainWindow
*m_owner
;
490 wxListRenameTimer( wxListMainWindow
*owner
);
494 //-----------------------------------------------------------------------------
495 // wxListTextCtrl (internal)
496 //-----------------------------------------------------------------------------
498 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
503 wxListMainWindow
*m_owner
;
504 wxString m_startValue
;
509 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
510 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
511 const wxString
&value
= "",
512 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
514 const wxValidator
& validator
= wxDefaultValidator
,
515 const wxString
&name
= "listctrltextctrl" );
516 void OnChar( wxKeyEvent
&event
);
517 void OnKeyUp( wxKeyEvent
&event
);
518 void OnKillFocus( wxFocusEvent
&event
);
521 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
);
522 DECLARE_EVENT_TABLE()
525 //-----------------------------------------------------------------------------
526 // wxListMainWindow (internal)
527 //-----------------------------------------------------------------------------
529 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
530 #include "wx/listimpl.cpp"
531 WX_DEFINE_LIST(wxListHeaderDataList
);
533 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
537 wxListMainWindow( wxWindow
*parent
,
539 const wxPoint
& pos
= wxDefaultPosition
,
540 const wxSize
& size
= wxDefaultSize
,
542 const wxString
&name
= _T("listctrlmainwindow") );
544 virtual ~wxListMainWindow();
546 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
548 // return true if this is a virtual list control
549 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
551 // return true if the control is in report mode
552 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
554 // return true if we are in single selection mode, false if multi sel
555 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
557 // do we have a header window?
558 bool HasHeader() const
559 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
561 void HighlightAll( bool on
);
563 // all these functions only do something if the line is currently visible
565 // change the line "selected" state, return TRUE if it really changed
566 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
568 // as HighlightLine() but do it for the range of lines: this is incredibly
569 // more efficient for virtual list controls!
571 // NB: unlike HighlightLine() this one does refresh the lines on screen
572 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
574 // toggle the line state and refresh it
575 void ReverseHighlight( size_t line
)
576 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
578 // return true if the line is highlighted
579 bool IsHighlighted(size_t line
) const;
581 // refresh one or several lines at once
582 void RefreshLine( size_t line
);
583 void RefreshLines( size_t lineFrom
, size_t lineTo
);
585 // refresh all selected items
586 void RefreshSelected();
588 // refresh all lines below the given one: the difference with
589 // RefreshLines() is that the index here might not be a valid one (happens
590 // when the last line is deleted)
591 void RefreshAfter( size_t lineFrom
);
593 // the methods which are forwarded to wxListLineData itself in list/icon
594 // modes but are here because the lines don't store their positions in the
597 // get the bound rect for the entire line
598 wxRect
GetLineRect(size_t line
) const;
600 // get the bound rect of the label
601 wxRect
GetLineLabelRect(size_t line
) const;
603 // get the bound rect of the items icon (only may be called if we do have
605 wxRect
GetLineIconRect(size_t line
) const;
607 // get the rect to be highlighted when the item has focus
608 wxRect
GetLineHighlightRect(size_t line
) const;
610 // get the size of the total line rect
611 wxSize
GetLineSize(size_t line
) const
612 { return GetLineRect(line
).GetSize(); }
614 // return the hit code for the corresponding position (in this line)
615 long HitTestLine(size_t line
, int x
, int y
) const;
617 // bring the selected item into view, scrolling to it if necessary
618 void MoveToItem(size_t item
);
620 // bring the current item into view
621 void MoveToFocus() { MoveToItem(m_current
); }
623 // start editing the label of the given item
624 void EditLabel( long item
);
626 // suspend/resume redrawing the control
632 void OnRenameTimer();
633 void OnRenameAccept();
635 void OnMouse( wxMouseEvent
&event
);
637 // called to switch the selection from the current item to newCurrent,
638 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
640 void OnChar( wxKeyEvent
&event
);
641 void OnKeyDown( wxKeyEvent
&event
);
642 void OnSetFocus( wxFocusEvent
&event
);
643 void OnKillFocus( wxFocusEvent
&event
);
644 void OnScroll(wxScrollWinEvent
& event
) ;
646 void OnPaint( wxPaintEvent
&event
);
648 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
649 void GetImageSize( int index
, int &width
, int &height
) const;
650 int GetTextLength( const wxString
&s
) const;
652 void SetImageList( wxImageList
*imageList
, int which
);
653 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
654 int GetItemSpacing( bool isSmall
= FALSE
);
656 void SetColumn( int col
, wxListItem
&item
);
657 void SetColumnWidth( int col
, int width
);
658 void GetColumn( int col
, wxListItem
&item
) const;
659 int GetColumnWidth( int col
) const;
660 int GetColumnCount() const { return m_columns
.GetCount(); }
662 // returns the sum of the heights of all columns
663 int GetHeaderWidth() const;
665 int GetCountPerPage() const;
667 void SetItem( wxListItem
&item
);
668 void GetItem( wxListItem
&item
);
669 void SetItemState( long item
, long state
, long stateMask
);
670 int GetItemState( long item
, long stateMask
);
671 void GetItemRect( long index
, wxRect
&rect
);
672 bool GetItemPosition( long item
, wxPoint
& pos
);
673 int GetSelectedItemCount();
675 // set the scrollbars and update the positions of the items
676 void RecalculatePositions(bool noRefresh
= FALSE
);
678 // refresh the window and the header
681 long GetNextItem( long item
, int geometry
, int state
);
682 void DeleteItem( long index
);
683 void DeleteAllItems();
684 void DeleteColumn( int col
);
685 void DeleteEverything();
686 void EnsureVisible( long index
);
687 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
688 long FindItem( long start
, long data
);
689 long HitTest( int x
, int y
, int &flags
);
690 void InsertItem( wxListItem
&item
);
691 void InsertColumn( long col
, wxListItem
&item
);
692 void SortItems( wxListCtrlCompare fn
, long data
);
694 size_t GetItemCount() const;
695 bool IsEmpty() const { return GetItemCount() == 0; }
696 void SetItemCount(long count
);
698 // change the current (== focused) item, send a notification event
699 void ChangeCurrent(size_t current
);
700 void ResetCurrent() { ChangeCurrent((size_t)-1); }
701 bool HasCurrent() const { return m_current
!= (size_t)-1; }
703 // send out a wxListEvent
704 void SendNotify( size_t line
,
706 wxPoint point
= wxDefaultPosition
);
708 // override base class virtual to reset m_lineHeight when the font changes
709 virtual bool SetFont(const wxFont
& font
)
711 if ( !wxScrolledWindow::SetFont(font
) )
719 // these are for wxListLineData usage only
721 // get the backpointer to the list ctrl
722 wxListCtrl
*GetListCtrl() const
724 return wxStaticCast(GetParent(), wxListCtrl
);
727 // get the height of all lines (assuming they all do have the same height)
728 wxCoord
GetLineHeight() const;
730 // get the y position of the given line (only for report view)
731 wxCoord
GetLineY(size_t line
) const;
733 // get the brush to use for the item highlighting
734 wxBrush
*GetHighlightBrush() const
736 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
740 // the array of all line objects for a non virtual list control
741 wxListLineDataArray m_lines
;
743 // the list of column objects
744 wxListHeaderDataList m_columns
;
746 // currently focused item or -1
749 // the item currently being edited or -1
750 size_t m_currentEdit
;
752 // the number of lines per page
755 // this flag is set when something which should result in the window
756 // redrawing happens (i.e. an item was added or deleted, or its appearance
757 // changed) and OnPaint() doesn't redraw the window while it is set which
758 // allows to minimize the number of repaintings when a lot of items are
759 // being added. The real repainting occurs only after the next OnIdle()
763 wxColour
*m_highlightColour
;
766 wxImageList
*m_small_image_list
;
767 wxImageList
*m_normal_image_list
;
769 int m_normal_spacing
;
773 wxTimer
*m_renameTimer
;
775 wxString m_renameRes
;
780 // for double click logic
781 size_t m_lineLastClicked
,
782 m_lineBeforeLastClicked
;
785 // the total count of items in a virtual list control
788 // the object maintaining the items selection state, only used in virtual
790 wxSelectionStore m_selStore
;
792 // common part of all ctors
795 // intiialize m_[xy]Scroll
796 void InitScrolling();
798 // get the line data for the given index
799 wxListLineData
*GetLine(size_t n
) const
801 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
805 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
813 // get a dummy line which can be used for geometry calculations and such:
814 // you must use GetLine() if you want to really draw the line
815 wxListLineData
*GetDummyLine() const;
817 // cache the line data of the n-th line in m_lines[0]
818 void CacheLineData(size_t line
);
820 // get the range of visible lines
821 void GetVisibleLinesRange(size_t *from
, size_t *to
);
823 // force us to recalculate the range of visible lines
824 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
826 // get the colour to be used for drawing the rules
827 wxColour
GetRuleColour() const
832 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
837 // initialize the current item if needed
838 void UpdateCurrent();
840 // delete all items but don't refresh: called from dtor
841 void DoDeleteAllItems();
843 // the height of one line using the current font
844 wxCoord m_lineHeight
;
846 // the total header width or 0 if not calculated yet
847 wxCoord m_headerWidth
;
849 // the first and last lines being shown on screen right now (inclusive),
850 // both may be -1 if they must be calculated so never access them directly:
851 // use GetVisibleLinesRange() above instead
855 // the brushes to use for item highlighting when we do/don't have focus
856 wxBrush
*m_highlightBrush
,
857 *m_highlightUnfocusedBrush
;
859 // if this is > 0, the control is frozen and doesn't redraw itself
860 size_t m_freezeCount
;
862 DECLARE_DYNAMIC_CLASS(wxListMainWindow
);
863 DECLARE_EVENT_TABLE()
866 // ============================================================================
868 // ============================================================================
870 // ----------------------------------------------------------------------------
872 // ----------------------------------------------------------------------------
874 bool wxSelectionStore::IsSelected(size_t item
) const
876 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
878 // if the default state is to be selected, being in m_itemsSel means that
879 // the item is not selected, so we have to inverse the logic
880 return m_defaultState
? !isSel
: isSel
;
883 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
885 // search for the item ourselves as like this we get the index where to
886 // insert it later if needed, so we do only one search in the array instead
887 // of two (adding item to a sorted array requires a search)
888 size_t index
= m_itemsSel
.IndexForInsert(item
);
889 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
891 if ( select
!= m_defaultState
)
895 m_itemsSel
.AddAt(item
, index
);
900 else // reset to default state
904 m_itemsSel
.RemoveAt(index
);
912 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
914 wxArrayInt
*itemsChanged
)
916 // 100 is hardcoded but it shouldn't matter much: the important thing is
917 // that we don't refresh everything when really few (e.g. 1 or 2) items
919 static const size_t MANY_ITEMS
= 100;
921 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
923 // are we going to have more [un]selected items than the other ones?
924 if ( itemTo
- itemFrom
> m_count
/2 )
926 if ( select
!= m_defaultState
)
928 // the default state now becomes the same as 'select'
929 m_defaultState
= select
;
931 // so all the old selections (which had state select) shouldn't be
932 // selected any more, but all the other ones should
933 wxIndexArray selOld
= m_itemsSel
;
936 // TODO: it should be possible to optimize the searches a bit
937 // knowing the possible range
940 for ( item
= 0; item
< itemFrom
; item
++ )
942 if ( selOld
.Index(item
) == wxNOT_FOUND
)
943 m_itemsSel
.Add(item
);
946 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
948 if ( selOld
.Index(item
) == wxNOT_FOUND
)
949 m_itemsSel
.Add(item
);
952 // many items (> half) changed state
955 else // select == m_defaultState
957 // get the inclusive range of items between itemFrom and itemTo
958 size_t count
= m_itemsSel
.GetCount(),
959 start
= m_itemsSel
.IndexForInsert(itemFrom
),
960 end
= m_itemsSel
.IndexForInsert(itemTo
);
962 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
967 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
974 // delete all of them (from end to avoid changing indices)
975 for ( int i
= end
; i
>= (int)start
; i
-- )
979 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
981 // stop counting (see comment below)
986 itemsChanged
->Add(m_itemsSel
[i
]);
990 m_itemsSel
.RemoveAt(i
);
995 else // "few" items change state
999 itemsChanged
->Empty();
1002 // just add the items to the selection
1003 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1005 if ( SelectItem(item
, select
) && itemsChanged
)
1007 itemsChanged
->Add(item
);
1009 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1011 // stop counting them, we'll just eat gobs of memory
1012 // for nothing at all - faster to refresh everything in
1014 itemsChanged
= NULL
;
1020 // we set it to NULL if there are many items changing state
1021 return itemsChanged
!= NULL
;
1024 void wxSelectionStore::OnItemDelete(size_t item
)
1026 size_t count
= m_itemsSel
.GetCount(),
1027 i
= m_itemsSel
.IndexForInsert(item
);
1029 if ( i
< count
&& m_itemsSel
[i
] == item
)
1031 // this item itself was in m_itemsSel, remove it from there
1032 m_itemsSel
.RemoveAt(i
);
1037 // and adjust the index of all which follow it
1040 // all following elements must be greater than the one we deleted
1041 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1047 //-----------------------------------------------------------------------------
1049 //-----------------------------------------------------------------------------
1051 wxListItemData::~wxListItemData()
1053 // in the virtual list control the attributes are managed by the main
1054 // program, so don't delete them
1055 if ( !m_owner
->IsVirtual() )
1063 void wxListItemData::Init()
1071 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1077 if ( owner
->InReportView() )
1083 m_rect
= new wxRect
;
1087 void wxListItemData::SetItem( const wxListItem
&info
)
1089 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1090 SetText(info
.m_text
);
1091 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1092 m_image
= info
.m_image
;
1093 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1094 m_data
= info
.m_data
;
1096 if ( info
.HasAttributes() )
1099 *m_attr
= *info
.GetAttributes();
1101 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1109 m_rect
->width
= info
.m_width
;
1113 void wxListItemData::SetPosition( int x
, int y
)
1115 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1121 void wxListItemData::SetSize( int width
, int height
)
1123 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1126 m_rect
->width
= width
;
1128 m_rect
->height
= height
;
1131 bool wxListItemData::IsHit( int x
, int y
) const
1133 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1135 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1138 int wxListItemData::GetX() const
1140 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1145 int wxListItemData::GetY() const
1147 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1152 int wxListItemData::GetWidth() const
1154 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1156 return m_rect
->width
;
1159 int wxListItemData::GetHeight() const
1161 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1163 return m_rect
->height
;
1166 void wxListItemData::GetItem( wxListItem
&info
) const
1168 info
.m_text
= m_text
;
1169 info
.m_image
= m_image
;
1170 info
.m_data
= m_data
;
1174 if ( m_attr
->HasTextColour() )
1175 info
.SetTextColour(m_attr
->GetTextColour());
1176 if ( m_attr
->HasBackgroundColour() )
1177 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1178 if ( m_attr
->HasFont() )
1179 info
.SetFont(m_attr
->GetFont());
1183 //-----------------------------------------------------------------------------
1185 //-----------------------------------------------------------------------------
1187 void wxListHeaderData::Init()
1198 wxListHeaderData::wxListHeaderData()
1203 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1210 void wxListHeaderData::SetItem( const wxListItem
&item
)
1212 m_mask
= item
.m_mask
;
1214 if ( m_mask
& wxLIST_MASK_TEXT
)
1215 m_text
= item
.m_text
;
1217 if ( m_mask
& wxLIST_MASK_IMAGE
)
1218 m_image
= item
.m_image
;
1220 if ( m_mask
& wxLIST_MASK_FORMAT
)
1221 m_format
= item
.m_format
;
1223 if ( m_mask
& wxLIST_MASK_WIDTH
)
1224 SetWidth(item
.m_width
);
1227 void wxListHeaderData::SetPosition( int x
, int y
)
1233 void wxListHeaderData::SetHeight( int h
)
1238 void wxListHeaderData::SetWidth( int w
)
1242 m_width
= WIDTH_COL_DEFAULT
;
1243 else if (m_width
< WIDTH_COL_MIN
)
1244 m_width
= WIDTH_COL_MIN
;
1247 void wxListHeaderData::SetFormat( int format
)
1252 bool wxListHeaderData::HasImage() const
1254 return m_image
!= -1;
1257 bool wxListHeaderData::IsHit( int x
, int y
) const
1259 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1262 void wxListHeaderData::GetItem( wxListItem
& item
)
1264 item
.m_mask
= m_mask
;
1265 item
.m_text
= m_text
;
1266 item
.m_image
= m_image
;
1267 item
.m_format
= m_format
;
1268 item
.m_width
= m_width
;
1271 int wxListHeaderData::GetImage() const
1276 int wxListHeaderData::GetWidth() const
1281 int wxListHeaderData::GetFormat() const
1286 //-----------------------------------------------------------------------------
1288 //-----------------------------------------------------------------------------
1290 inline int wxListLineData::GetMode() const
1292 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1295 inline bool wxListLineData::InReportView() const
1297 return m_owner
->HasFlag(wxLC_REPORT
);
1300 inline bool wxListLineData::IsVirtual() const
1302 return m_owner
->IsVirtual();
1305 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1308 m_items
.DeleteContents( TRUE
);
1310 if ( InReportView() )
1316 m_gi
= new GeometryInfo
;
1319 m_highlighted
= FALSE
;
1321 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1324 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1326 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1327 wxCHECK_RET( node
, _T("no subitems at all??") );
1329 wxListItemData
*item
= node
->GetData();
1331 switch ( GetMode() )
1334 case wxLC_SMALL_ICON
:
1336 m_gi
->m_rectAll
.width
= spacing
;
1338 wxString s
= item
->GetText();
1344 m_gi
->m_rectLabel
.width
=
1345 m_gi
->m_rectLabel
.height
= 0;
1349 dc
->GetTextExtent( s
, &lw
, &lh
);
1350 if (lh
< SCROLL_UNIT_Y
)
1355 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1357 m_gi
->m_rectAll
.width
= lw
;
1359 m_gi
->m_rectLabel
.width
= lw
;
1360 m_gi
->m_rectLabel
.height
= lh
;
1363 if (item
->HasImage())
1366 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1367 m_gi
->m_rectIcon
.width
= w
+ 8;
1368 m_gi
->m_rectIcon
.height
= h
+ 8;
1370 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1371 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1372 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1373 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1376 if ( item
->HasText() )
1378 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1379 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1381 else // no text, highlight the icon
1383 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1384 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1391 wxString s
= item
->GetTextForMeasuring();
1394 dc
->GetTextExtent( s
, &lw
, &lh
);
1395 if (lh
< SCROLL_UNIT_Y
)
1400 m_gi
->m_rectLabel
.width
= lw
;
1401 m_gi
->m_rectLabel
.height
= lh
;
1403 m_gi
->m_rectAll
.width
= lw
;
1404 m_gi
->m_rectAll
.height
= lh
;
1406 if (item
->HasImage())
1409 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1410 m_gi
->m_rectIcon
.width
= w
;
1411 m_gi
->m_rectIcon
.height
= h
;
1413 m_gi
->m_rectAll
.width
+= 4 + w
;
1414 if (h
> m_gi
->m_rectAll
.height
)
1415 m_gi
->m_rectAll
.height
= h
;
1418 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1419 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1424 wxFAIL_MSG( _T("unexpected call to SetSize") );
1428 wxFAIL_MSG( _T("unknown mode") );
1432 void wxListLineData::SetPosition( int x
, int y
,
1436 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1437 wxCHECK_RET( node
, _T("no subitems at all??") );
1439 wxListItemData
*item
= node
->GetData();
1441 switch ( GetMode() )
1444 case wxLC_SMALL_ICON
:
1445 m_gi
->m_rectAll
.x
= x
;
1446 m_gi
->m_rectAll
.y
= y
;
1448 if ( item
->HasImage() )
1450 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4
1451 + (spacing
- m_gi
->m_rectIcon
.width
)/2;
1452 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1455 if ( item
->HasText() )
1457 if (m_gi
->m_rectAll
.width
> spacing
)
1458 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1460 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1461 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1462 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1463 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1465 else // no text, highlight the icon
1467 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1468 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1473 m_gi
->m_rectAll
.x
= x
;
1474 m_gi
->m_rectAll
.y
= y
;
1476 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1477 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1478 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1480 if (item
->HasImage())
1482 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1483 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1484 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1488 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1493 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1497 wxFAIL_MSG( _T("unknown mode") );
1501 void wxListLineData::InitItems( int num
)
1503 for (int i
= 0; i
< num
; i
++)
1504 m_items
.Append( new wxListItemData(m_owner
) );
1507 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1509 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1510 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1512 wxListItemData
*item
= node
->GetData();
1513 item
->SetItem( info
);
1516 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1518 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1521 wxListItemData
*item
= node
->GetData();
1522 item
->GetItem( info
);
1526 wxString
wxListLineData::GetText(int index
) const
1530 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1533 wxListItemData
*item
= node
->GetData();
1534 s
= item
->GetText();
1540 void wxListLineData::SetText( int index
, const wxString s
)
1542 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1545 wxListItemData
*item
= node
->GetData();
1550 void wxListLineData::SetImage( int index
, int image
)
1552 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1553 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1555 wxListItemData
*item
= node
->GetData();
1556 item
->SetImage(image
);
1559 int wxListLineData::GetImage( int index
) const
1561 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1562 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1564 wxListItemData
*item
= node
->GetData();
1565 return item
->GetImage();
1568 wxListItemAttr
*wxListLineData::GetAttr() const
1570 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1571 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1573 wxListItemData
*item
= node
->GetData();
1574 return item
->GetAttr();
1577 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1579 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1580 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1582 wxListItemData
*item
= node
->GetData();
1583 item
->SetAttr(attr
);
1586 bool wxListLineData::SetAttributes(wxDC
*dc
,
1587 const wxListItemAttr
*attr
,
1590 wxWindow
*listctrl
= m_owner
->GetParent();
1594 // don't use foreground colour for drawing highlighted items - this might
1595 // make them completely invisible (and there is no way to do bit
1596 // arithmetics on wxColour, unfortunately)
1600 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1604 if ( attr
&& attr
->HasTextColour() )
1606 colText
= attr
->GetTextColour();
1610 colText
= listctrl
->GetForegroundColour();
1614 dc
->SetTextForeground(colText
);
1618 if ( attr
&& attr
->HasFont() )
1620 font
= attr
->GetFont();
1624 font
= listctrl
->GetFont();
1630 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1631 if ( highlighted
|| hasBgCol
)
1635 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1639 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1642 dc
->SetPen( *wxTRANSPARENT_PEN
);
1650 void wxListLineData::Draw( wxDC
*dc
)
1652 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1653 wxCHECK_RET( node
, _T("no subitems at all??") );
1655 bool highlighted
= IsHighlighted();
1657 wxListItemAttr
*attr
= GetAttr();
1659 if ( SetAttributes(dc
, attr
, highlighted
) )
1661 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1664 wxListItemData
*item
= node
->GetData();
1665 if (item
->HasImage())
1667 wxRect rectIcon
= m_gi
->m_rectIcon
;
1668 m_owner
->DrawImage( item
->GetImage(), dc
,
1669 rectIcon
.x
, rectIcon
.y
);
1672 if (item
->HasText())
1674 wxRect rectLabel
= m_gi
->m_rectLabel
;
1676 wxDCClipper
clipper(*dc
, rectLabel
);
1677 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1681 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1683 const wxRect
& rectHL
,
1686 // TODO: later we should support setting different attributes for
1687 // different columns - to do it, just add "col" argument to
1688 // GetAttr() and move these lines into the loop below
1689 wxListItemAttr
*attr
= GetAttr();
1690 if ( SetAttributes(dc
, attr
, highlighted
) )
1692 dc
->DrawRectangle( rectHL
);
1695 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1696 wxCHECK_RET( node
, _T("no subitems at all??") );
1699 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1700 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1704 wxListItemData
*item
= node
->GetData();
1706 int width
= m_owner
->GetColumnWidth(col
++);
1710 if ( item
->HasImage() )
1713 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1714 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1716 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1722 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1724 if ( item
->HasText() )
1726 dc
->DrawText( item
->GetText(), xOld
, y
);
1729 node
= node
->GetNext();
1733 bool wxListLineData::Highlight( bool on
)
1735 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1737 if ( on
== m_highlighted
)
1745 void wxListLineData::ReverseHighlight( void )
1747 Highlight(!IsHighlighted());
1750 //-----------------------------------------------------------------------------
1751 // wxListHeaderWindow
1752 //-----------------------------------------------------------------------------
1754 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
);
1756 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1757 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1758 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1759 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1762 void wxListHeaderWindow::Init()
1764 m_currentCursor
= (wxCursor
*) NULL
;
1765 m_isDragging
= FALSE
;
1769 wxListHeaderWindow::wxListHeaderWindow()
1773 m_owner
= (wxListMainWindow
*) NULL
;
1774 m_resizeCursor
= (wxCursor
*) NULL
;
1777 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1779 wxListMainWindow
*owner
,
1783 const wxString
&name
)
1784 : wxWindow( win
, id
, pos
, size
, style
, name
)
1789 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1791 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1794 wxListHeaderWindow::~wxListHeaderWindow()
1796 delete m_resizeCursor
;
1799 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1801 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1802 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1803 : GTK_STATE_INSENSITIVE
;
1805 x
= dc
->XLOG2DEV( x
);
1807 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1808 state
, GTK_SHADOW_OUT
,
1809 (GdkRectangle
*) NULL
, m_wxwindow
,
1810 (char *)"button", // const_cast
1811 x
-1, y
-1, w
+2, h
+2);
1812 #elif defined( __WXMAC__ )
1813 const int m_corner
= 1;
1815 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1817 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1818 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1819 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1821 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1824 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1825 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1827 dc
->SetPen( *wxWHITE_PEN
);
1828 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1829 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1830 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1831 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1833 const int m_corner
= 1;
1835 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1837 dc
->SetPen( *wxBLACK_PEN
);
1838 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1839 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1841 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1844 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1845 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1847 dc
->SetPen( *wxWHITE_PEN
);
1848 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1849 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1850 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1851 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1855 // shift the DC origin to match the position of the main window horz
1856 // scrollbar: this allows us to always use logical coords
1857 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1860 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1863 m_owner
->GetViewStart( &x
, NULL
);
1865 // account for the horz scrollbar offset
1866 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1869 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1871 #if defined(__WXGTK__)
1872 wxClientDC
dc( this );
1874 wxPaintDC
dc( this );
1882 dc
.SetFont( GetFont() );
1884 // width and height of the entire header window
1886 GetClientSize( &w
, &h
);
1887 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1889 dc
.SetBackgroundMode(wxTRANSPARENT
);
1891 // do *not* use the listctrl colour for headers - one day we will have a
1892 // function to set it separately
1893 //dc.SetTextForeground( *wxBLACK );
1894 dc
.SetTextForeground(wxSystemSettings::
1895 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1897 int x
= HEADER_OFFSET_X
;
1899 int numColumns
= m_owner
->GetColumnCount();
1901 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1903 m_owner
->GetColumn( i
, item
);
1904 int wCol
= item
.m_width
;
1906 // the width of the rect to draw: make it smaller to fit entirely
1907 // inside the column rect
1910 dc
.SetPen( *wxWHITE_PEN
);
1912 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1914 // if we have an image, draw it on the right of the label
1915 int image
= item
.m_image
;
1918 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1922 imageList
->GetSize(image
, ix
, iy
);
1929 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1930 wxIMAGELIST_DRAW_TRANSPARENT
1935 //else: ignore the column image
1938 // draw the text clipping it so that it doesn't overwrite the column
1940 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1942 dc
.DrawText( item
.GetText(),
1943 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1951 void wxListHeaderWindow::DrawCurrent()
1953 int x1
= m_currentX
;
1955 m_owner
->ClientToScreen( &x1
, &y1
);
1957 int x2
= m_currentX
;
1959 m_owner
->GetClientSize( NULL
, &y2
);
1960 m_owner
->ClientToScreen( &x2
, &y2
);
1963 dc
.SetLogicalFunction( wxINVERT
);
1964 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1965 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1969 dc
.DrawLine( x1
, y1
, x2
, y2
);
1971 dc
.SetLogicalFunction( wxCOPY
);
1973 dc
.SetPen( wxNullPen
);
1974 dc
.SetBrush( wxNullBrush
);
1977 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1979 // we want to work with logical coords
1981 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1982 int y
= event
.GetY();
1986 // we don't draw the line beyond our window, but we allow dragging it
1989 GetClientSize( &w
, NULL
);
1990 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1993 // erase the line if it was drawn
1994 if ( m_currentX
< w
)
1997 if (event
.ButtonUp())
2000 m_isDragging
= FALSE
;
2002 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2009 m_currentX
= m_minX
+ 7;
2011 // draw in the new location
2012 if ( m_currentX
< w
)
2016 else // not dragging
2019 bool hit_border
= FALSE
;
2021 // end of the current column
2024 // find the column where this event occured
2025 int countCol
= m_owner
->GetColumnCount();
2026 for (int col
= 0; col
< countCol
; col
++)
2028 xpos
+= m_owner
->GetColumnWidth( col
);
2031 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2033 // near the column border
2040 // inside the column
2047 if (event
.LeftDown() || event
.RightUp())
2049 if (hit_border
&& event
.LeftDown())
2051 m_isDragging
= TRUE
;
2056 else // click on a column
2058 wxWindow
*parent
= GetParent();
2059 wxListEvent
le( event
.LeftDown()
2060 ? wxEVT_COMMAND_LIST_COL_CLICK
2061 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2063 le
.SetEventObject( parent
);
2064 le
.m_pointDrag
= event
.GetPosition();
2066 // the position should be relative to the parent window, not
2067 // this one for compatibility with MSW and common sense: the
2068 // user code doesn't know anything at all about this header
2069 // window, so why should it get positions relative to it?
2070 le
.m_pointDrag
.y
-= GetSize().y
;
2072 le
.m_col
= m_column
;
2073 parent
->GetEventHandler()->ProcessEvent( le
);
2076 else if (event
.Moving())
2081 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2082 m_currentCursor
= m_resizeCursor
;
2086 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2087 m_currentCursor
= wxSTANDARD_CURSOR
;
2091 SetCursor(*m_currentCursor
);
2096 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2098 m_owner
->SetFocus();
2101 //-----------------------------------------------------------------------------
2102 // wxListRenameTimer (internal)
2103 //-----------------------------------------------------------------------------
2105 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2110 void wxListRenameTimer::Notify()
2112 m_owner
->OnRenameTimer();
2115 //-----------------------------------------------------------------------------
2116 // wxListTextCtrl (internal)
2117 //-----------------------------------------------------------------------------
2119 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
);
2121 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2122 EVT_CHAR (wxListTextCtrl::OnChar
)
2123 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2124 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2127 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
2128 const wxWindowID id
,
2131 wxListMainWindow
*owner
,
2132 const wxString
&value
,
2136 const wxValidator
& validator
,
2137 const wxString
&name
)
2138 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
2143 (*m_accept
) = FALSE
;
2145 m_startValue
= value
;
2149 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2151 if (event
.m_keyCode
== WXK_RETURN
)
2154 (*m_res
) = GetValue();
2156 if (!wxPendingDelete
.Member(this))
2157 wxPendingDelete
.Append(this);
2159 if ((*m_res
) != m_startValue
)
2160 m_owner
->OnRenameAccept();
2163 m_owner
->SetFocus();
2167 if (event
.m_keyCode
== WXK_ESCAPE
)
2169 (*m_accept
) = FALSE
;
2172 if (!wxPendingDelete
.Member(this))
2173 wxPendingDelete
.Append(this);
2176 m_owner
->SetFocus();
2184 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2192 // auto-grow the textctrl:
2193 wxSize parentSize
= m_owner
->GetSize();
2194 wxPoint myPos
= GetPosition();
2195 wxSize mySize
= GetSize();
2197 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2198 if (myPos
.x
+ sx
> parentSize
.x
)
2199 sx
= parentSize
.x
- myPos
.x
;
2207 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2215 if (!wxPendingDelete
.Member(this))
2216 wxPendingDelete
.Append(this);
2219 (*m_res
) = GetValue();
2221 if ((*m_res
) != m_startValue
)
2222 m_owner
->OnRenameAccept();
2225 //-----------------------------------------------------------------------------
2227 //-----------------------------------------------------------------------------
2229 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
);
2231 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2232 EVT_PAINT (wxListMainWindow::OnPaint
)
2233 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2234 EVT_CHAR (wxListMainWindow::OnChar
)
2235 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2236 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2237 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2238 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2241 void wxListMainWindow::Init()
2243 m_columns
.DeleteContents( TRUE
);
2247 m_lineTo
= (size_t)-1;
2253 m_small_image_list
= (wxImageList
*) NULL
;
2254 m_normal_image_list
= (wxImageList
*) NULL
;
2256 m_small_spacing
= 30;
2257 m_normal_spacing
= 40;
2261 m_isCreated
= FALSE
;
2263 m_lastOnSame
= FALSE
;
2264 m_renameTimer
= new wxListRenameTimer( this );
2265 m_renameAccept
= FALSE
;
2270 m_lineBeforeLastClicked
= (size_t)-1;
2275 void wxListMainWindow::InitScrolling()
2277 if ( HasFlag(wxLC_REPORT
) )
2279 m_xScroll
= SCROLL_UNIT_X
;
2280 m_yScroll
= SCROLL_UNIT_Y
;
2284 m_xScroll
= SCROLL_UNIT_Y
;
2289 wxListMainWindow::wxListMainWindow()
2294 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2300 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2305 const wxString
&name
)
2306 : wxScrolledWindow( parent
, id
, pos
, size
,
2307 style
| wxHSCROLL
| wxVSCROLL
, name
)
2311 m_highlightBrush
= new wxBrush
2313 wxSystemSettings::GetColour
2315 wxSYS_COLOUR_HIGHLIGHT
2320 m_highlightUnfocusedBrush
= new wxBrush
2322 wxSystemSettings::GetColour
2324 wxSYS_COLOUR_BTNSHADOW
2333 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2335 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2338 wxListMainWindow::~wxListMainWindow()
2342 delete m_highlightBrush
;
2343 delete m_highlightUnfocusedBrush
;
2345 delete m_renameTimer
;
2348 void wxListMainWindow::CacheLineData(size_t line
)
2350 wxListCtrl
*listctrl
= GetListCtrl();
2352 wxListLineData
*ld
= GetDummyLine();
2354 size_t countCol
= GetColumnCount();
2355 for ( size_t col
= 0; col
< countCol
; col
++ )
2357 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2360 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2361 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2364 wxListLineData
*wxListMainWindow::GetDummyLine() const
2366 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2368 if ( m_lines
.IsEmpty() )
2370 // normal controls are supposed to have something in m_lines
2371 // already if it's not empty
2372 wxASSERT_MSG( IsVirtual(), _T("logic error") );
2374 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2375 wxListLineData
*line
= new wxListLineData(self
);
2376 self
->m_lines
.Add(line
);
2382 // ----------------------------------------------------------------------------
2383 // line geometry (report mode only)
2384 // ----------------------------------------------------------------------------
2386 wxCoord
wxListMainWindow::GetLineHeight() const
2388 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2390 // we cache the line height as calling GetTextExtent() is slow
2391 if ( !m_lineHeight
)
2393 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2395 wxClientDC
dc( self
);
2396 dc
.SetFont( GetFont() );
2399 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2401 if ( y
< SCROLL_UNIT_Y
)
2405 self
->m_lineHeight
= y
+ LINE_SPACING
;
2408 return m_lineHeight
;
2411 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2413 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2415 return LINE_SPACING
+ line
*GetLineHeight();
2418 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2420 if ( !InReportView() )
2421 return GetLine(line
)->m_gi
->m_rectAll
;
2424 rect
.x
= HEADER_OFFSET_X
;
2425 rect
.y
= GetLineY(line
);
2426 rect
.width
= GetHeaderWidth();
2427 rect
.height
= GetLineHeight();
2432 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2434 if ( !InReportView() )
2435 return GetLine(line
)->m_gi
->m_rectLabel
;
2438 rect
.x
= HEADER_OFFSET_X
;
2439 rect
.y
= GetLineY(line
);
2440 rect
.width
= GetColumnWidth(0);
2441 rect
.height
= GetLineHeight();
2446 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2448 if ( !InReportView() )
2449 return GetLine(line
)->m_gi
->m_rectIcon
;
2451 wxListLineData
*ld
= GetLine(line
);
2452 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2455 rect
.x
= HEADER_OFFSET_X
;
2456 rect
.y
= GetLineY(line
);
2457 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2462 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2464 return InReportView() ? GetLineRect(line
)
2465 : GetLine(line
)->m_gi
->m_rectHighlight
;
2468 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2470 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2472 wxListLineData
*ld
= GetLine(line
);
2474 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2475 return wxLIST_HITTEST_ONITEMICON
;
2477 if ( ld
->HasText() )
2479 wxRect rect
= InReportView() ? GetLineRect(line
)
2480 : GetLineLabelRect(line
);
2482 if ( rect
.Inside(x
, y
) )
2483 return wxLIST_HITTEST_ONITEMLABEL
;
2489 // ----------------------------------------------------------------------------
2490 // highlight (selection) handling
2491 // ----------------------------------------------------------------------------
2493 bool wxListMainWindow::IsHighlighted(size_t line
) const
2497 return m_selStore
.IsSelected(line
);
2501 wxListLineData
*ld
= GetLine(line
);
2502 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2504 return ld
->IsHighlighted();
2508 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2514 wxArrayInt linesChanged
;
2515 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2518 // meny items changed state, refresh everything
2519 RefreshLines(lineFrom
, lineTo
);
2521 else // only a few items changed state, refresh only them
2523 size_t count
= linesChanged
.GetCount();
2524 for ( size_t n
= 0; n
< count
; n
++ )
2526 RefreshLine(linesChanged
[n
]);
2530 else // iterate over all items in non report view
2532 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2534 if ( HighlightLine(line
, highlight
) )
2542 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2548 changed
= m_selStore
.SelectItem(line
, highlight
);
2552 wxListLineData
*ld
= GetLine(line
);
2553 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2555 changed
= ld
->Highlight(highlight
);
2560 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2561 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2567 void wxListMainWindow::RefreshLine( size_t line
)
2569 if ( HasFlag(wxLC_REPORT
) )
2571 size_t visibleFrom
, visibleTo
;
2572 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2574 if ( line
< visibleFrom
|| line
> visibleTo
)
2578 wxRect rect
= GetLineRect(line
);
2580 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2581 RefreshRect( rect
);
2584 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2586 // we suppose that they are ordered by caller
2587 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2589 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2591 if ( HasFlag(wxLC_REPORT
) )
2593 size_t visibleFrom
, visibleTo
;
2594 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2596 if ( lineFrom
< visibleFrom
)
2597 lineFrom
= visibleFrom
;
2598 if ( lineTo
> visibleTo
)
2603 rect
.y
= GetLineY(lineFrom
);
2604 rect
.width
= GetClientSize().x
;
2605 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2607 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2608 RefreshRect( rect
);
2612 // TODO: this should be optimized...
2613 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2620 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2622 if ( HasFlag(wxLC_REPORT
) )
2625 GetVisibleLinesRange(&visibleFrom
, NULL
);
2627 if ( lineFrom
< visibleFrom
)
2628 lineFrom
= visibleFrom
;
2632 rect
.y
= GetLineY(lineFrom
);
2634 wxSize size
= GetClientSize();
2635 rect
.width
= size
.x
;
2636 // refresh till the bottom of the window
2637 rect
.height
= size
.y
- rect
.y
;
2639 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2640 RefreshRect( rect
);
2644 // TODO: how to do it more efficiently?
2649 void wxListMainWindow::RefreshSelected()
2655 if ( InReportView() )
2657 GetVisibleLinesRange(&from
, &to
);
2662 to
= GetItemCount() - 1;
2665 // VZ: this code would work fine if wxGTK wxWindow::Refresh() were
2666 // reasonable, i.e. if it only generated one expose event for
2667 // several calls to it - as it is, each Refresh() results in a
2668 // repaint which provokes flicker too horrible to be seen
2670 // when/if wxGTK is fixed, this code should be restored as normally it
2671 // should generate _less_ flicker than the version below
2673 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2675 RefreshLine(m_current
);
2678 for ( size_t line
= from
; line
<= to
; line
++ )
2680 // NB: the test works as expected even if m_current == -1
2681 if ( line
!= m_current
&& IsHighlighted(line
) )
2687 size_t selMin
= (size_t)-1,
2690 for ( size_t line
= from
; line
<= to
; line
++ )
2692 if ( IsHighlighted(line
) || (line
== m_current
) )
2694 if ( line
< selMin
)
2696 if ( line
> selMax
)
2701 if ( selMin
!= (size_t)-1 )
2703 RefreshLines(selMin
, selMax
);
2705 #endif // !__WXGTK__/__WXGTK__
2708 void wxListMainWindow::Freeze()
2713 void wxListMainWindow::Thaw()
2715 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2717 if ( !--m_freezeCount
)
2723 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2725 // Note: a wxPaintDC must be constructed even if no drawing is
2726 // done (a Windows requirement).
2727 wxPaintDC
dc( this );
2729 if ( IsEmpty() || m_freezeCount
)
2731 // nothing to draw or not the moment to draw it
2737 // delay the repainting until we calculate all the items positions
2744 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2748 dc
.SetFont( GetFont() );
2750 if ( HasFlag(wxLC_REPORT
) )
2752 int lineHeight
= GetLineHeight();
2754 size_t visibleFrom
, visibleTo
;
2755 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2758 wxCoord xOrig
, yOrig
;
2759 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2761 // tell the caller cache to cache the data
2764 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2765 GetParent()->GetId());
2766 evCache
.SetEventObject( GetParent() );
2767 evCache
.m_oldItemIndex
= visibleFrom
;
2768 evCache
.m_itemIndex
= visibleTo
;
2769 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2772 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2774 rectLine
= GetLineRect(line
);
2776 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2777 rectLine
.width
, rectLine
.height
) )
2779 // don't redraw unaffected lines to avoid flicker
2783 GetLine(line
)->DrawInReportMode( &dc
,
2785 GetLineHighlightRect(line
),
2786 IsHighlighted(line
) );
2789 if ( HasFlag(wxLC_HRULES
) )
2791 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2792 wxSize clientSize
= GetClientSize();
2794 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2797 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2798 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2799 clientSize
.x
- dev_x
, i
*lineHeight
);
2802 // Draw last horizontal rule
2803 if ( visibleTo
> visibleFrom
)
2806 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2807 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2808 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2812 // Draw vertical rules if required
2813 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2815 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2818 wxRect firstItemRect
;
2819 wxRect lastItemRect
;
2820 GetItemRect(0, firstItemRect
);
2821 GetItemRect(GetItemCount() - 1, lastItemRect
);
2822 int x
= firstItemRect
.GetX();
2824 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2825 for (col
= 0; col
< GetColumnCount(); col
++)
2827 int colWidth
= GetColumnWidth(col
);
2829 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2830 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2836 size_t count
= GetItemCount();
2837 for ( size_t i
= 0; i
< count
; i
++ )
2839 GetLine(i
)->Draw( &dc
);
2845 // don't draw rect outline under Max if we already have the background
2846 // color but under other platforms only draw it if we do: it is a bit
2847 // silly to draw "focus rect" if we don't have focus!
2852 #endif // __WXMAC__/!__WXMAC__
2854 dc
.SetPen( *wxBLACK_PEN
);
2855 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2856 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2863 void wxListMainWindow::HighlightAll( bool on
)
2865 if ( IsSingleSel() )
2867 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2869 // we just have one item to turn off
2870 if ( HasCurrent() && IsHighlighted(m_current
) )
2872 HighlightLine(m_current
, FALSE
);
2873 RefreshLine(m_current
);
2878 HighlightLines(0, GetItemCount() - 1, on
);
2882 void wxListMainWindow::SendNotify( size_t line
,
2883 wxEventType command
,
2886 wxListEvent
le( command
, GetParent()->GetId() );
2887 le
.SetEventObject( GetParent() );
2888 le
.m_itemIndex
= line
;
2890 // set only for events which have position
2891 if ( point
!= wxDefaultPosition
)
2892 le
.m_pointDrag
= point
;
2894 // don't try to get the line info for virtual list controls: the main
2895 // program has it anyhow and if we did it would result in accessing all
2896 // the lines, even those which are not visible now and this is precisely
2897 // what we're trying to avoid
2898 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2900 if ( line
!= (size_t)-1 )
2902 GetLine(line
)->GetItem( 0, le
.m_item
);
2904 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2906 //else: there may be no more such item
2908 GetParent()->GetEventHandler()->ProcessEvent( le
);
2911 void wxListMainWindow::ChangeCurrent(size_t current
)
2913 m_current
= current
;
2915 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2918 void wxListMainWindow::EditLabel( long item
)
2920 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2921 wxT("wrong index in wxListCtrl::EditLabel()") );
2923 m_currentEdit
= (size_t)item
;
2925 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2926 le
.SetEventObject( GetParent() );
2927 le
.m_itemIndex
= item
;
2928 wxListLineData
*data
= GetLine(m_currentEdit
);
2929 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2930 data
->GetItem( 0, le
.m_item
);
2931 GetParent()->GetEventHandler()->ProcessEvent( le
);
2933 if (!le
.IsAllowed())
2936 // We have to call this here because the label in question might just have
2937 // been added and no screen update taken place.
2941 wxClientDC
dc(this);
2944 wxString s
= data
->GetText(0);
2945 wxRect rectLabel
= GetLineLabelRect(m_currentEdit
);
2947 rectLabel
.x
= dc
.LogicalToDeviceX( rectLabel
.x
);
2948 rectLabel
.y
= dc
.LogicalToDeviceY( rectLabel
.y
);
2950 wxListTextCtrl
*text
= new wxListTextCtrl
2957 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2958 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2963 void wxListMainWindow::OnRenameTimer()
2965 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2967 EditLabel( m_current
);
2970 void wxListMainWindow::OnRenameAccept()
2972 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2973 le
.SetEventObject( GetParent() );
2974 le
.m_itemIndex
= m_currentEdit
;
2976 wxListLineData
*data
= GetLine(m_currentEdit
);
2977 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2979 data
->GetItem( 0, le
.m_item
);
2980 le
.m_item
.m_text
= m_renameRes
;
2981 GetParent()->GetEventHandler()->ProcessEvent( le
);
2983 if (!le
.IsAllowed()) return;
2986 info
.m_mask
= wxLIST_MASK_TEXT
;
2987 info
.m_itemId
= le
.m_itemIndex
;
2988 info
.m_text
= m_renameRes
;
2989 info
.SetTextColour(le
.m_item
.GetTextColour());
2993 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2995 event
.SetEventObject( GetParent() );
2996 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2999 if ( !HasCurrent() || IsEmpty() )
3005 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3006 event
.ButtonDClick()) )
3009 int x
= event
.GetX();
3010 int y
= event
.GetY();
3011 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3013 // where did we hit it (if we did)?
3016 size_t count
= GetItemCount(),
3019 if ( HasFlag(wxLC_REPORT
) )
3021 current
= y
/ GetLineHeight();
3022 if ( current
< count
)
3023 hitResult
= HitTestLine(current
, x
, y
);
3027 // TODO: optimize it too! this is less simple than for report view but
3028 // enumerating all items is still not a way to do it!!
3029 for ( current
= 0; current
< count
; current
++ )
3031 hitResult
= HitTestLine(current
, x
, y
);
3037 if (event
.Dragging())
3039 if (m_dragCount
== 0)
3041 // we have to report the raw, physical coords as we want to be
3042 // able to call HitTest(event.m_pointDrag) from the user code to
3043 // get the item being dragged
3044 m_dragStart
= event
.GetPosition();
3049 if (m_dragCount
!= 3)
3052 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3053 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3055 wxListEvent
le( command
, GetParent()->GetId() );
3056 le
.SetEventObject( GetParent() );
3057 le
.m_pointDrag
= m_dragStart
;
3058 GetParent()->GetEventHandler()->ProcessEvent( le
);
3069 // outside of any item
3073 bool forceClick
= FALSE
;
3074 if (event
.ButtonDClick())
3076 m_renameTimer
->Stop();
3077 m_lastOnSame
= FALSE
;
3079 if ( current
== m_lineBeforeLastClicked
)
3081 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3087 // the first click was on another item, so don't interpret this as
3088 // a double click, but as a simple click instead
3093 if (event
.LeftUp() && m_lastOnSame
)
3095 if ((current
== m_current
) &&
3096 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3097 HasFlag(wxLC_EDIT_LABELS
) )
3099 m_renameTimer
->Start( 100, TRUE
);
3101 m_lastOnSame
= FALSE
;
3103 else if (event
.RightDown())
3105 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3106 event
.GetPosition() );
3108 else if (event
.MiddleDown())
3110 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3112 else if ( event
.LeftDown() || forceClick
)
3114 m_lineBeforeLastClicked
= m_lineLastClicked
;
3115 m_lineLastClicked
= current
;
3117 size_t oldCurrent
= m_current
;
3119 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3121 HighlightAll( FALSE
);
3123 ChangeCurrent(current
);
3125 ReverseHighlight(m_current
);
3127 else // multi sel & either ctrl or shift is down
3129 if (event
.ControlDown())
3131 ChangeCurrent(current
);
3133 ReverseHighlight(m_current
);
3135 else if (event
.ShiftDown())
3137 ChangeCurrent(current
);
3139 size_t lineFrom
= oldCurrent
,
3142 if ( lineTo
< lineFrom
)
3145 lineFrom
= m_current
;
3148 HighlightLines(lineFrom
, lineTo
);
3150 else // !ctrl, !shift
3152 // test in the enclosing if should make it impossible
3153 wxFAIL_MSG( _T("how did we get here?") );
3157 if (m_current
!= oldCurrent
)
3159 RefreshLine( oldCurrent
);
3162 // forceClick is only set if the previous click was on another item
3163 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3167 void wxListMainWindow::MoveToItem(size_t item
)
3169 if ( item
== (size_t)-1 )
3172 wxRect rect
= GetLineRect(item
);
3174 int client_w
, client_h
;
3175 GetClientSize( &client_w
, &client_h
);
3177 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3178 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3180 if ( HasFlag(wxLC_REPORT
) )
3182 // the next we need the range of lines shown it might be different, so
3184 ResetVisibleLinesRange();
3186 if (rect
.y
< view_y
)
3187 Scroll( -1, rect
.y
/m_yScroll
);
3188 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3189 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3193 if (rect
.x
-view_x
< 5)
3194 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3195 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3196 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3200 // ----------------------------------------------------------------------------
3201 // keyboard handling
3202 // ----------------------------------------------------------------------------
3204 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3206 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3207 _T("invalid item index in OnArrowChar()") );
3209 size_t oldCurrent
= m_current
;
3211 // in single selection we just ignore Shift as we can't select several
3213 if ( event
.ShiftDown() && !IsSingleSel() )
3215 ChangeCurrent(newCurrent
);
3217 // select all the items between the old and the new one
3218 if ( oldCurrent
> newCurrent
)
3220 newCurrent
= oldCurrent
;
3221 oldCurrent
= m_current
;
3224 HighlightLines(oldCurrent
, newCurrent
);
3228 // all previously selected items are unselected unless ctrl is held
3229 if ( !event
.ControlDown() )
3230 HighlightAll(FALSE
);
3232 ChangeCurrent(newCurrent
);
3234 HighlightLine( oldCurrent
, FALSE
);
3235 RefreshLine( oldCurrent
);
3237 if ( !event
.ControlDown() )
3239 HighlightLine( m_current
, TRUE
);
3243 RefreshLine( m_current
);
3248 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3250 wxWindow
*parent
= GetParent();
3252 /* we propagate the key event up */
3253 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
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;
3267 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3269 wxWindow
*parent
= GetParent();
3271 /* we send a list_key event up */
3274 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3275 le
.m_itemIndex
= m_current
;
3276 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3277 le
.m_code
= (int)event
.KeyCode();
3278 le
.SetEventObject( parent
);
3279 parent
->GetEventHandler()->ProcessEvent( le
);
3282 /* we propagate the char event up */
3283 wxKeyEvent
ke( wxEVT_CHAR
);
3284 ke
.m_shiftDown
= event
.m_shiftDown
;
3285 ke
.m_controlDown
= event
.m_controlDown
;
3286 ke
.m_altDown
= event
.m_altDown
;
3287 ke
.m_metaDown
= event
.m_metaDown
;
3288 ke
.m_keyCode
= event
.m_keyCode
;
3291 ke
.SetEventObject( parent
);
3292 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3294 if (event
.KeyCode() == WXK_TAB
)
3296 wxNavigationKeyEvent nevent
;
3297 nevent
.SetWindowChange( event
.ControlDown() );
3298 nevent
.SetDirection( !event
.ShiftDown() );
3299 nevent
.SetEventObject( GetParent()->GetParent() );
3300 nevent
.SetCurrentFocus( m_parent
);
3301 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3305 /* no item -> nothing to do */
3312 switch (event
.KeyCode())
3315 if ( m_current
> 0 )
3316 OnArrowChar( m_current
- 1, event
);
3320 if ( m_current
< (size_t)GetItemCount() - 1 )
3321 OnArrowChar( m_current
+ 1, event
);
3326 OnArrowChar( GetItemCount() - 1, event
);
3331 OnArrowChar( 0, event
);
3337 if ( HasFlag(wxLC_REPORT
) )
3339 steps
= m_linesPerPage
- 1;
3343 steps
= m_current
% m_linesPerPage
;
3346 int index
= m_current
- steps
;
3350 OnArrowChar( index
, event
);
3357 if ( HasFlag(wxLC_REPORT
) )
3359 steps
= m_linesPerPage
- 1;
3363 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3366 size_t index
= m_current
+ steps
;
3367 size_t count
= GetItemCount();
3368 if ( index
>= count
)
3371 OnArrowChar( index
, event
);
3376 if ( !HasFlag(wxLC_REPORT
) )
3378 int index
= m_current
- m_linesPerPage
;
3382 OnArrowChar( index
, event
);
3387 if ( !HasFlag(wxLC_REPORT
) )
3389 size_t index
= m_current
+ m_linesPerPage
;
3391 size_t count
= GetItemCount();
3392 if ( index
>= count
)
3395 OnArrowChar( index
, event
);
3400 if ( IsSingleSel() )
3402 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3404 if ( IsHighlighted(m_current
) )
3406 // don't unselect the item in single selection mode
3409 //else: select it in ReverseHighlight() below if unselected
3412 ReverseHighlight(m_current
);
3417 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3425 // ----------------------------------------------------------------------------
3427 // ----------------------------------------------------------------------------
3430 extern wxWindow
*g_focusWindow
;
3433 void wxListMainWindow::SetFocus()
3435 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3436 // overrides SetFocus in such way that it does never change focus from
3437 // panel's child to the panel itself. Unfortunately, we must be able to change
3438 // focus to the panel from wxListTextCtrl because the text control should
3439 // disappear when the user clicks outside it.
3441 wxWindow
*oldFocus
= FindFocus();
3443 if ( oldFocus
->GetParent() == this )
3445 wxWindow::SetFocus();
3449 wxScrolledWindow::SetFocus();
3453 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3455 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3456 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3457 // which are already drawn correctly resulting in horrible flicker - avoid
3470 g_focusWindow
= GetParent();
3473 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3474 event
.SetEventObject( GetParent() );
3475 GetParent()->GetEventHandler()->ProcessEvent( event
);
3478 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3485 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3487 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3489 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3491 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3493 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3495 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3497 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3499 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3501 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3505 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3507 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3509 m_normal_image_list
->GetSize( index
, width
, height
);
3511 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3513 m_small_image_list
->GetSize( index
, width
, height
);
3515 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3517 m_small_image_list
->GetSize( index
, width
, height
);
3519 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3521 m_small_image_list
->GetSize( index
, width
, height
);
3530 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3532 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3533 dc
.SetFont( GetFont() );
3536 dc
.GetTextExtent( s
, &lw
, NULL
);
3538 return lw
+ AUTOSIZE_COL_MARGIN
;
3541 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3545 // calc the spacing from the icon size
3548 if ((imageList
) && (imageList
->GetImageCount()) )
3550 imageList
->GetSize(0, width
, height
);
3553 if (which
== wxIMAGE_LIST_NORMAL
)
3555 m_normal_image_list
= imageList
;
3556 m_normal_spacing
= width
+ 8;
3559 if (which
== wxIMAGE_LIST_SMALL
)
3561 m_small_image_list
= imageList
;
3562 m_small_spacing
= width
+ 14;
3566 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3571 m_small_spacing
= spacing
;
3575 m_normal_spacing
= spacing
;
3579 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3581 return isSmall
? m_small_spacing
: m_normal_spacing
;
3584 // ----------------------------------------------------------------------------
3586 // ----------------------------------------------------------------------------
3588 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3590 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3592 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3594 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3595 item
.m_width
= GetTextLength( item
.m_text
);
3597 wxListHeaderData
*column
= node
->GetData();
3598 column
->SetItem( item
);
3600 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3602 headerWin
->m_dirty
= TRUE
;
3606 // invalidate it as it has to be recalculated
3610 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3612 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3613 _T("invalid column index") );
3615 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3616 _T("SetColumnWidth() can only be called in report mode.") );
3619 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3621 headerWin
->m_dirty
= TRUE
;
3623 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3624 wxCHECK_RET( node
, _T("no column?") );
3626 wxListHeaderData
*column
= node
->GetData();
3628 size_t count
= GetItemCount();
3630 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3632 width
= GetTextLength(column
->GetText());
3634 else if ( width
== wxLIST_AUTOSIZE
)
3638 // TODO: determine the max width somehow...
3639 width
= WIDTH_COL_DEFAULT
;
3643 wxClientDC
dc(this);
3644 dc
.SetFont( GetFont() );
3646 int max
= AUTOSIZE_COL_MARGIN
;
3648 for ( size_t i
= 0; i
< count
; i
++ )
3650 wxListLineData
*line
= GetLine(i
);
3651 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3653 wxCHECK_RET( n
, _T("no subitem?") );
3655 wxListItemData
*item
= n
->GetData();
3658 if (item
->HasImage())
3661 GetImageSize( item
->GetImage(), ix
, iy
);
3665 if (item
->HasText())
3668 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3676 width
= max
+ AUTOSIZE_COL_MARGIN
;
3680 column
->SetWidth( width
);
3682 // invalidate it as it has to be recalculated
3686 int wxListMainWindow::GetHeaderWidth() const
3688 if ( !m_headerWidth
)
3690 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3692 size_t count
= GetColumnCount();
3693 for ( size_t col
= 0; col
< count
; col
++ )
3695 self
->m_headerWidth
+= GetColumnWidth(col
);
3699 return m_headerWidth
;
3702 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3704 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3705 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3707 wxListHeaderData
*column
= node
->GetData();
3708 column
->GetItem( item
);
3711 int wxListMainWindow::GetColumnWidth( int col
) const
3713 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3714 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3716 wxListHeaderData
*column
= node
->GetData();
3717 return column
->GetWidth();
3720 // ----------------------------------------------------------------------------
3722 // ----------------------------------------------------------------------------
3724 void wxListMainWindow::SetItem( wxListItem
&item
)
3726 long id
= item
.m_itemId
;
3727 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3728 _T("invalid item index in SetItem") );
3732 wxListLineData
*line
= GetLine((size_t)id
);
3733 line
->SetItem( item
.m_col
, item
);
3736 if ( InReportView() )
3738 // just refresh the line to show the new value of the text/image
3739 RefreshLine((size_t)id
);
3743 // refresh everything (resulting in horrible flicker - FIXME!)
3748 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3750 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3751 _T("invalid list ctrl item index in SetItem") );
3753 size_t oldCurrent
= m_current
;
3754 size_t item
= (size_t)litem
; // safe because of the check above
3756 // do we need to change the focus?
3757 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3759 if ( state
& wxLIST_STATE_FOCUSED
)
3761 // don't do anything if this item is already focused
3762 if ( item
!= m_current
)
3764 ChangeCurrent(item
);
3766 if ( oldCurrent
!= (size_t)-1 )
3768 if ( IsSingleSel() )
3770 HighlightLine(oldCurrent
, FALSE
);
3773 RefreshLine(oldCurrent
);
3776 RefreshLine( m_current
);
3781 // don't do anything if this item is not focused
3782 if ( item
== m_current
)
3786 RefreshLine( oldCurrent
);
3791 // do we need to change the selection state?
3792 if ( stateMask
& wxLIST_STATE_SELECTED
)
3794 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3796 if ( IsSingleSel() )
3800 // selecting the item also makes it the focused one in the
3802 if ( m_current
!= item
)
3804 ChangeCurrent(item
);
3806 if ( oldCurrent
!= (size_t)-1 )
3808 HighlightLine( oldCurrent
, FALSE
);
3809 RefreshLine( oldCurrent
);
3815 // only the current item may be selected anyhow
3816 if ( item
!= m_current
)
3821 if ( HighlightLine(item
, on
) )
3828 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3830 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3831 _T("invalid list ctrl item index in GetItemState()") );
3833 int ret
= wxLIST_STATE_DONTCARE
;
3835 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3837 if ( (size_t)item
== m_current
)
3838 ret
|= wxLIST_STATE_FOCUSED
;
3841 if ( stateMask
& wxLIST_STATE_SELECTED
)
3843 if ( IsHighlighted(item
) )
3844 ret
|= wxLIST_STATE_SELECTED
;
3850 void wxListMainWindow::GetItem( wxListItem
&item
)
3852 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3853 _T("invalid item index in GetItem") );
3855 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3856 line
->GetItem( item
.m_col
, item
);
3859 // ----------------------------------------------------------------------------
3861 // ----------------------------------------------------------------------------
3863 size_t wxListMainWindow::GetItemCount() const
3865 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3868 void wxListMainWindow::SetItemCount(long count
)
3870 m_selStore
.SetItemCount(count
);
3871 m_countVirt
= count
;
3873 ResetVisibleLinesRange();
3875 // scrollbars must be reset
3879 int wxListMainWindow::GetSelectedItemCount()
3881 // deal with the quick case first
3882 if ( IsSingleSel() )
3884 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3887 // virtual controls remmebers all its selections itself
3889 return m_selStore
.GetSelectedCount();
3891 // TODO: we probably should maintain the number of items selected even for
3892 // non virtual controls as enumerating all lines is really slow...
3893 size_t countSel
= 0;
3894 size_t count
= GetItemCount();
3895 for ( size_t line
= 0; line
< count
; line
++ )
3897 if ( GetLine(line
)->IsHighlighted() )
3904 // ----------------------------------------------------------------------------
3905 // item position/size
3906 // ----------------------------------------------------------------------------
3908 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3910 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3911 _T("invalid index in GetItemRect") );
3913 rect
= GetLineRect((size_t)index
);
3915 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3918 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3921 GetItemRect(item
, rect
);
3929 // ----------------------------------------------------------------------------
3930 // geometry calculation
3931 // ----------------------------------------------------------------------------
3933 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3935 wxClientDC
dc( this );
3936 dc
.SetFont( GetFont() );
3939 if ( HasFlag(wxLC_ICON
) )
3940 iconSpacing
= m_normal_spacing
;
3941 else if ( HasFlag(wxLC_SMALL_ICON
) )
3942 iconSpacing
= m_small_spacing
;
3946 // Note that we do not call GetClientSize() here but
3947 // GetSize() and substract the border size for sunken
3948 // borders manually. This is technically incorrect,
3949 // but we need to know the client area's size WITHOUT
3950 // scrollbars here. Since we don't know if there are
3951 // any scrollbars, we use GetSize() instead. Another
3952 // solution would be to call SetScrollbars() here to
3953 // remove the scrollbars and call GetClientSize() then,
3954 // but this might result in flicker and - worse - will
3955 // reset the scrollbars to 0 which is not good at all
3956 // if you resize a dialog/window, but don't want to
3957 // reset the window scrolling. RR.
3958 // Furthermore, we actually do NOT subtract the border
3959 // width as 2 pixels is just the extra space which we
3960 // need around the actual content in the window. Other-
3961 // wise the text would e.g. touch the upper border. RR.
3964 GetSize( &clientWidth
, &clientHeight
);
3966 if ( HasFlag(wxLC_REPORT
) )
3968 // all lines have the same height
3969 int lineHeight
= GetLineHeight();
3971 // scroll one line per step
3972 m_yScroll
= lineHeight
;
3974 size_t lineCount
= GetItemCount();
3975 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3977 m_linesPerPage
= clientHeight
/ lineHeight
;
3979 ResetVisibleLinesRange();
3981 SetScrollbars( m_xScroll
, m_yScroll
,
3982 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3983 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3984 GetScrollPos(wxHORIZONTAL
),
3985 GetScrollPos(wxVERTICAL
),
3990 // at first we try without any scrollbar. if the items don't
3991 // fit into the window, we recalculate after subtracting an
3992 // approximated 15 pt for the horizontal scrollbar
3994 int entireWidth
= 0;
3996 for (int tries
= 0; tries
< 2; tries
++)
3998 // We start with 4 for the border around all items
4003 // Now we have decided that the items do not fit into the
4004 // client area. Unfortunately, wxWindows sometimes thinks
4005 // that it does fit and therefore NO horizontal scrollbar
4006 // is inserted. This looks ugly, so we fudge here and make
4007 // the calculated width bigger than was actually has been
4008 // calculated. This ensures that wxScrolledWindows puts
4009 // a scrollbar at the bottom of its client area.
4010 entireWidth
+= SCROLL_UNIT_X
;
4013 // Start at 2,2 so the text does not touch the border
4018 int currentlyVisibleLines
= 0;
4020 size_t count
= GetItemCount();
4021 for (size_t i
= 0; i
< count
; i
++)
4023 currentlyVisibleLines
++;
4024 wxListLineData
*line
= GetLine(i
);
4025 line
->CalculateSize( &dc
, iconSpacing
);
4026 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4028 wxSize sizeLine
= GetLineSize(i
);
4030 if ( maxWidth
< sizeLine
.x
)
4031 maxWidth
= sizeLine
.x
;
4034 if (currentlyVisibleLines
> m_linesPerPage
)
4035 m_linesPerPage
= currentlyVisibleLines
;
4037 // Assume that the size of the next one is the same... (FIXME)
4038 if ( y
+ sizeLine
.y
>= clientHeight
)
4040 currentlyVisibleLines
= 0;
4043 entireWidth
+= maxWidth
+6;
4047 // We have reached the last item.
4048 if ( i
== count
- 1 )
4049 entireWidth
+= maxWidth
;
4051 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4053 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4055 currentlyVisibleLines
= 0;
4059 if ( i
== count
- 1 )
4060 tries
= 1; // Everything fits, no second try required.
4064 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4065 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4070 // FIXME: why should we call it from here?
4077 void wxListMainWindow::RefreshAll()
4082 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4083 if ( headerWin
&& headerWin
->m_dirty
)
4085 headerWin
->m_dirty
= FALSE
;
4086 headerWin
->Refresh();
4090 void wxListMainWindow::UpdateCurrent()
4092 if ( !HasCurrent() && !IsEmpty() )
4098 long wxListMainWindow::GetNextItem( long item
,
4099 int WXUNUSED(geometry
),
4103 max
= GetItemCount();
4104 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4105 _T("invalid listctrl index in GetNextItem()") );
4107 // notice that we start with the next item (or the first one if item == -1)
4108 // and this is intentional to allow writing a simple loop to iterate over
4109 // all selected items
4113 // this is not an error because the index was ok initially, just no
4124 size_t count
= GetItemCount();
4125 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4127 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4130 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4137 // ----------------------------------------------------------------------------
4139 // ----------------------------------------------------------------------------
4141 void wxListMainWindow::DeleteItem( long lindex
)
4143 size_t count
= GetItemCount();
4145 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4146 _T("invalid item index in DeleteItem") );
4148 size_t index
= (size_t)lindex
;
4150 // we don't need to adjust the index for the previous items
4151 if ( HasCurrent() && m_current
>= index
)
4153 // if the current item is being deleted, we want the next one to
4154 // become selected - unless there is no next one - so don't adjust
4155 // m_current in this case
4156 if ( m_current
!= index
|| m_current
== count
- 1 )
4162 if ( InReportView() )
4164 ResetVisibleLinesRange();
4171 m_selStore
.OnItemDelete(index
);
4175 m_lines
.RemoveAt( index
);
4178 // we need to refresh the (vert) scrollbar as the number of items changed
4181 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4183 RefreshAfter(index
);
4186 void wxListMainWindow::DeleteColumn( int col
)
4188 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4190 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4193 m_columns
.DeleteNode( node
);
4196 void wxListMainWindow::DoDeleteAllItems()
4200 // nothing to do - in particular, don't send the event
4206 // to make the deletion of all items faster, we don't send the
4207 // notifications for each item deletion in this case but only one event
4208 // for all of them: this is compatible with wxMSW and documented in
4209 // DeleteAllItems() description
4211 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4212 event
.SetEventObject( GetParent() );
4213 GetParent()->GetEventHandler()->ProcessEvent( event
);
4222 if ( InReportView() )
4224 ResetVisibleLinesRange();
4230 void wxListMainWindow::DeleteAllItems()
4234 RecalculatePositions();
4237 void wxListMainWindow::DeleteEverything()
4244 // ----------------------------------------------------------------------------
4245 // scanning for an item
4246 // ----------------------------------------------------------------------------
4248 void wxListMainWindow::EnsureVisible( long index
)
4250 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4251 _T("invalid index in EnsureVisible") );
4253 // We have to call this here because the label in question might just have
4254 // been added and its position is not known yet
4257 RecalculatePositions(TRUE
/* no refresh */);
4260 MoveToItem((size_t)index
);
4263 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4270 size_t count
= GetItemCount();
4271 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4273 wxListLineData
*line
= GetLine(i
);
4274 if ( line
->GetText(0) == tmp
)
4281 long wxListMainWindow::FindItem(long start
, long data
)
4287 size_t count
= GetItemCount();
4288 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4290 wxListLineData
*line
= GetLine(i
);
4292 line
->GetItem( 0, item
);
4293 if (item
.m_data
== data
)
4300 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4302 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4304 size_t count
= GetItemCount();
4306 if ( HasFlag(wxLC_REPORT
) )
4308 size_t current
= y
/ GetLineHeight();
4309 if ( current
< count
)
4311 flags
= HitTestLine(current
, x
, y
);
4318 // TODO: optimize it too! this is less simple than for report view but
4319 // enumerating all items is still not a way to do it!!
4320 for ( size_t current
= 0; current
< count
; current
++ )
4322 flags
= HitTestLine(current
, x
, y
);
4331 // ----------------------------------------------------------------------------
4333 // ----------------------------------------------------------------------------
4335 void wxListMainWindow::InsertItem( wxListItem
&item
)
4337 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4339 size_t count
= GetItemCount();
4340 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4341 _T("invalid item index") );
4343 size_t id
= item
.m_itemId
;
4348 if ( HasFlag(wxLC_REPORT
) )
4350 else if ( HasFlag(wxLC_LIST
) )
4352 else if ( HasFlag(wxLC_ICON
) )
4354 else if ( HasFlag(wxLC_SMALL_ICON
) )
4355 mode
= wxLC_ICON
; // no typo
4358 wxFAIL_MSG( _T("unknown mode") );
4361 wxListLineData
*line
= new wxListLineData(this);
4363 line
->SetItem( 0, item
);
4365 m_lines
.Insert( line
, id
);
4368 RefreshLines(id
, GetItemCount() - 1);
4371 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4374 if ( HasFlag(wxLC_REPORT
) )
4376 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4377 item
.m_width
= GetTextLength( item
.m_text
);
4378 wxListHeaderData
*column
= new wxListHeaderData( item
);
4379 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4381 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4382 m_columns
.Insert( node
, column
);
4386 m_columns
.Append( column
);
4391 // ----------------------------------------------------------------------------
4393 // ----------------------------------------------------------------------------
4395 wxListCtrlCompare list_ctrl_compare_func_2
;
4396 long list_ctrl_compare_data
;
4398 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4400 wxListLineData
*line1
= *arg1
;
4401 wxListLineData
*line2
= *arg2
;
4403 line1
->GetItem( 0, item
);
4404 long data1
= item
.m_data
;
4405 line2
->GetItem( 0, item
);
4406 long data2
= item
.m_data
;
4407 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4410 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4412 list_ctrl_compare_func_2
= fn
;
4413 list_ctrl_compare_data
= data
;
4414 m_lines
.Sort( list_ctrl_compare_func_1
);
4418 // ----------------------------------------------------------------------------
4420 // ----------------------------------------------------------------------------
4422 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4424 // update our idea of which lines are shown when we redraw the window the
4426 ResetVisibleLinesRange();
4429 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4430 wxScrolledWindow::OnScroll(event
);
4432 HandleOnScroll( event
);
4435 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4437 wxListCtrl
* lc
= GetListCtrl();
4438 wxCHECK_RET( lc
, _T("no listctrl window?") );
4440 lc
->m_headerWin
->Refresh() ;
4442 lc
->m_headerWin
->MacUpdateImmediately() ;
4447 int wxListMainWindow::GetCountPerPage() const
4449 if ( !m_linesPerPage
)
4451 wxConstCast(this, wxListMainWindow
)->
4452 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4455 return m_linesPerPage
;
4458 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4460 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4462 if ( m_lineFrom
== (size_t)-1 )
4464 size_t count
= GetItemCount();
4467 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4469 // this may happen if SetScrollbars() hadn't been called yet
4470 if ( m_lineFrom
>= count
)
4471 m_lineFrom
= count
- 1;
4473 // we redraw one extra line but this is needed to make the redrawing
4474 // logic work when there is a fractional number of lines on screen
4475 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4476 if ( m_lineTo
>= count
)
4477 m_lineTo
= count
- 1;
4479 else // empty control
4482 m_lineTo
= (size_t)-1;
4486 wxASSERT_MSG( IsEmpty() ||
4487 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4488 _T("GetVisibleLinesRange() returns incorrect result") );
4496 // -------------------------------------------------------------------------------------
4498 // -------------------------------------------------------------------------------------
4500 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4502 wxListItem::wxListItem()
4509 void wxListItem::Clear()
4518 m_format
= wxLIST_FORMAT_CENTRE
;
4525 void wxListItem::ClearAttributes()
4534 // -------------------------------------------------------------------------------------
4536 // -------------------------------------------------------------------------------------
4538 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4539 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4541 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4543 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4544 EVT_SIZE(wxListCtrl::OnSize
)
4545 EVT_IDLE(wxListCtrl::OnIdle
)
4548 wxListCtrl::wxListCtrl()
4550 m_imageListNormal
= (wxImageList
*) NULL
;
4551 m_imageListSmall
= (wxImageList
*) NULL
;
4552 m_imageListState
= (wxImageList
*) NULL
;
4554 m_ownsImageListNormal
=
4555 m_ownsImageListSmall
=
4556 m_ownsImageListState
= FALSE
;
4558 m_mainWin
= (wxListMainWindow
*) NULL
;
4559 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4562 wxListCtrl::~wxListCtrl()
4564 if (m_ownsImageListNormal
)
4565 delete m_imageListNormal
;
4566 if (m_ownsImageListSmall
)
4567 delete m_imageListSmall
;
4568 if (m_ownsImageListState
)
4569 delete m_imageListState
;
4572 void wxListCtrl::CreateHeaderWindow()
4574 m_headerWin
= new wxListHeaderWindow
4576 this, -1, m_mainWin
,
4578 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4583 bool wxListCtrl::Create(wxWindow
*parent
,
4588 const wxValidator
&validator
,
4589 const wxString
&name
)
4593 m_imageListState
= (wxImageList
*) NULL
;
4594 m_ownsImageListNormal
=
4595 m_ownsImageListSmall
=
4596 m_ownsImageListState
= FALSE
;
4598 m_mainWin
= (wxListMainWindow
*) NULL
;
4599 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4601 if ( !(style
& wxLC_MASK_TYPE
) )
4603 style
= style
| wxLC_LIST
;
4606 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4609 // don't create the inner window with the border
4610 style
&= ~wxSUNKEN_BORDER
;
4612 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4614 if ( HasFlag(wxLC_REPORT
) )
4616 CreateHeaderWindow();
4618 if ( HasFlag(wxLC_NO_HEADER
) )
4620 // VZ: why do we create it at all then?
4621 m_headerWin
->Show( FALSE
);
4628 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4630 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4631 _T("wxLC_VIRTUAL can't be [un]set") );
4633 long flag
= GetWindowStyle();
4637 if (style
& wxLC_MASK_TYPE
)
4638 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4639 if (style
& wxLC_MASK_ALIGN
)
4640 flag
&= ~wxLC_MASK_ALIGN
;
4641 if (style
& wxLC_MASK_SORT
)
4642 flag
&= ~wxLC_MASK_SORT
;
4654 SetWindowStyleFlag( flag
);
4657 void wxListCtrl::SetWindowStyleFlag( long flag
)
4661 m_mainWin
->DeleteEverything();
4663 // has the header visibility changed?
4664 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4665 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4667 if ( hasHeader
!= willHaveHeader
)
4674 // don't delete, just hide, as we can reuse it later
4675 m_headerWin
->Show(FALSE
);
4677 //else: nothing to do
4679 else // must show header
4683 CreateHeaderWindow();
4685 else // already have it, just show
4687 m_headerWin
->Show( TRUE
);
4691 ResizeReportView(willHaveHeader
);
4695 wxWindow::SetWindowStyleFlag( flag
);
4698 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4700 m_mainWin
->GetColumn( col
, item
);
4704 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4706 m_mainWin
->SetColumn( col
, item
);
4710 int wxListCtrl::GetColumnWidth( int col
) const
4712 return m_mainWin
->GetColumnWidth( col
);
4715 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4717 m_mainWin
->SetColumnWidth( col
, width
);
4721 int wxListCtrl::GetCountPerPage() const
4723 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4726 bool wxListCtrl::GetItem( wxListItem
&info
) const
4728 m_mainWin
->GetItem( info
);
4732 bool wxListCtrl::SetItem( wxListItem
&info
)
4734 m_mainWin
->SetItem( info
);
4738 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4741 info
.m_text
= label
;
4742 info
.m_mask
= wxLIST_MASK_TEXT
;
4743 info
.m_itemId
= index
;
4747 info
.m_image
= imageId
;
4748 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4750 m_mainWin
->SetItem(info
);
4754 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4756 return m_mainWin
->GetItemState( item
, stateMask
);
4759 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4761 m_mainWin
->SetItemState( item
, state
, stateMask
);
4765 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4768 info
.m_image
= image
;
4769 info
.m_mask
= wxLIST_MASK_IMAGE
;
4770 info
.m_itemId
= item
;
4771 m_mainWin
->SetItem( info
);
4775 wxString
wxListCtrl::GetItemText( long item
) const
4778 info
.m_itemId
= item
;
4779 m_mainWin
->GetItem( info
);
4783 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4786 info
.m_mask
= wxLIST_MASK_TEXT
;
4787 info
.m_itemId
= item
;
4789 m_mainWin
->SetItem( info
);
4792 long wxListCtrl::GetItemData( long item
) const
4795 info
.m_itemId
= item
;
4796 m_mainWin
->GetItem( info
);
4800 bool wxListCtrl::SetItemData( long item
, long data
)
4803 info
.m_mask
= wxLIST_MASK_DATA
;
4804 info
.m_itemId
= item
;
4806 m_mainWin
->SetItem( info
);
4810 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4812 m_mainWin
->GetItemRect( item
, rect
);
4816 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4818 m_mainWin
->GetItemPosition( item
, pos
);
4822 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4827 int wxListCtrl::GetItemCount() const
4829 return m_mainWin
->GetItemCount();
4832 int wxListCtrl::GetColumnCount() const
4834 return m_mainWin
->GetColumnCount();
4837 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4839 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4842 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4844 return m_mainWin
->GetItemSpacing( isSmall
);
4847 int wxListCtrl::GetSelectedItemCount() const
4849 return m_mainWin
->GetSelectedItemCount();
4852 wxColour
wxListCtrl::GetTextColour() const
4854 return GetForegroundColour();
4857 void wxListCtrl::SetTextColour(const wxColour
& col
)
4859 SetForegroundColour(col
);
4862 long wxListCtrl::GetTopItem() const
4867 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4869 return m_mainWin
->GetNextItem( item
, geom
, state
);
4872 wxImageList
*wxListCtrl::GetImageList(int which
) const
4874 if (which
== wxIMAGE_LIST_NORMAL
)
4876 return m_imageListNormal
;
4878 else if (which
== wxIMAGE_LIST_SMALL
)
4880 return m_imageListSmall
;
4882 else if (which
== wxIMAGE_LIST_STATE
)
4884 return m_imageListState
;
4886 return (wxImageList
*) NULL
;
4889 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4891 if ( which
== wxIMAGE_LIST_NORMAL
)
4893 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4894 m_imageListNormal
= imageList
;
4895 m_ownsImageListNormal
= FALSE
;
4897 else if ( which
== wxIMAGE_LIST_SMALL
)
4899 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4900 m_imageListSmall
= imageList
;
4901 m_ownsImageListSmall
= FALSE
;
4903 else if ( which
== wxIMAGE_LIST_STATE
)
4905 if (m_ownsImageListState
) delete m_imageListState
;
4906 m_imageListState
= imageList
;
4907 m_ownsImageListState
= FALSE
;
4910 m_mainWin
->SetImageList( imageList
, which
);
4913 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4915 SetImageList(imageList
, which
);
4916 if ( which
== wxIMAGE_LIST_NORMAL
)
4917 m_ownsImageListNormal
= TRUE
;
4918 else if ( which
== wxIMAGE_LIST_SMALL
)
4919 m_ownsImageListSmall
= TRUE
;
4920 else if ( which
== wxIMAGE_LIST_STATE
)
4921 m_ownsImageListState
= TRUE
;
4924 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4929 bool wxListCtrl::DeleteItem( long item
)
4931 m_mainWin
->DeleteItem( item
);
4935 bool wxListCtrl::DeleteAllItems()
4937 m_mainWin
->DeleteAllItems();
4941 bool wxListCtrl::DeleteAllColumns()
4943 size_t count
= m_mainWin
->m_columns
.GetCount();
4944 for ( size_t n
= 0; n
< count
; n
++ )
4950 void wxListCtrl::ClearAll()
4952 m_mainWin
->DeleteEverything();
4955 bool wxListCtrl::DeleteColumn( int col
)
4957 m_mainWin
->DeleteColumn( col
);
4961 void wxListCtrl::Edit( long item
)
4963 m_mainWin
->EditLabel( item
);
4966 bool wxListCtrl::EnsureVisible( long item
)
4968 m_mainWin
->EnsureVisible( item
);
4972 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4974 return m_mainWin
->FindItem( start
, str
, partial
);
4977 long wxListCtrl::FindItem( long start
, long data
)
4979 return m_mainWin
->FindItem( start
, data
);
4982 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4983 int WXUNUSED(direction
))
4988 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4990 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4993 long wxListCtrl::InsertItem( wxListItem
& info
)
4995 m_mainWin
->InsertItem( info
);
4996 return info
.m_itemId
;
4999 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
5002 info
.m_text
= label
;
5003 info
.m_mask
= wxLIST_MASK_TEXT
;
5004 info
.m_itemId
= index
;
5005 return InsertItem( info
);
5008 long wxListCtrl::InsertItem( long index
, int imageIndex
)
5011 info
.m_mask
= wxLIST_MASK_IMAGE
;
5012 info
.m_image
= imageIndex
;
5013 info
.m_itemId
= index
;
5014 return InsertItem( info
);
5017 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5020 info
.m_text
= label
;
5021 info
.m_image
= imageIndex
;
5022 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5023 info
.m_itemId
= index
;
5024 return InsertItem( info
);
5027 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
5029 wxASSERT( m_headerWin
);
5030 m_mainWin
->InsertColumn( col
, item
);
5031 m_headerWin
->Refresh();
5036 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
5037 int format
, int width
)
5040 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5041 item
.m_text
= heading
;
5044 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5045 item
.m_width
= width
;
5047 item
.m_format
= format
;
5049 return InsertColumn( col
, item
);
5052 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5058 // fn is a function which takes 3 long arguments: item1, item2, data.
5059 // item1 is the long data associated with a first item (NOT the index).
5060 // item2 is the long data associated with a second item (NOT the index).
5061 // data is the same value as passed to SortItems.
5062 // The return value is a negative number if the first item should precede the second
5063 // item, a positive number of the second item should precede the first,
5064 // or zero if the two items are equivalent.
5065 // data is arbitrary data to be passed to the sort function.
5067 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5069 m_mainWin
->SortItems( fn
, data
);
5073 // ----------------------------------------------------------------------------
5075 // ----------------------------------------------------------------------------
5077 void wxListCtrl::OnSize(wxSizeEvent
& event
)
5082 ResizeReportView(m_mainWin
->HasHeader());
5084 m_mainWin
->RecalculatePositions();
5087 void wxListCtrl::ResizeReportView(bool showHeader
)
5090 GetClientSize( &cw
, &ch
);
5094 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5095 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5097 else // no header window
5099 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5103 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5107 // do it only if needed
5108 if ( !m_mainWin
->m_dirty
)
5111 m_mainWin
->RecalculatePositions();
5114 // ----------------------------------------------------------------------------
5116 // ----------------------------------------------------------------------------
5118 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5122 m_mainWin
->SetBackgroundColour( colour
);
5123 m_mainWin
->m_dirty
= TRUE
;
5129 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5131 if ( !wxWindow::SetForegroundColour( colour
) )
5136 m_mainWin
->SetForegroundColour( colour
);
5137 m_mainWin
->m_dirty
= TRUE
;
5142 m_headerWin
->SetForegroundColour( colour
);
5148 bool wxListCtrl::SetFont( const wxFont
&font
)
5150 if ( !wxWindow::SetFont( font
) )
5155 m_mainWin
->SetFont( font
);
5156 m_mainWin
->m_dirty
= TRUE
;
5161 m_headerWin
->SetFont( font
);
5167 // ----------------------------------------------------------------------------
5168 // methods forwarded to m_mainWin
5169 // ----------------------------------------------------------------------------
5171 #if wxUSE_DRAG_AND_DROP
5173 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5175 m_mainWin
->SetDropTarget( dropTarget
);
5178 wxDropTarget
*wxListCtrl::GetDropTarget() const
5180 return m_mainWin
->GetDropTarget();
5183 #endif // wxUSE_DRAG_AND_DROP
5185 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5187 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5190 wxColour
wxListCtrl::GetBackgroundColour() const
5192 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5195 wxColour
wxListCtrl::GetForegroundColour() const
5197 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5200 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5203 return m_mainWin
->PopupMenu( menu
, x
, y
);
5206 #endif // wxUSE_MENUS
5209 void wxListCtrl::SetFocus()
5211 /* The test in window.cpp fails as we are a composite
5212 window, so it checks against "this", but not m_mainWin. */
5213 if ( FindFocus() != this )
5214 m_mainWin
->SetFocus();
5217 // ----------------------------------------------------------------------------
5218 // virtual list control support
5219 // ----------------------------------------------------------------------------
5221 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
5223 // this is a pure virtual function, in fact - which is not really pure
5224 // because the controls which are not virtual don't need to implement it
5225 wxFAIL_MSG( _T("not supposed to be called") );
5227 return wxEmptyString
;
5230 int wxListCtrl::OnGetItemImage(long item
) const
5233 wxFAIL_MSG( _T("not supposed to be called") );
5238 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5240 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5241 _T("invalid item index in OnGetItemAttr()") );
5243 // no attributes by default
5247 void wxListCtrl::SetItemCount(long count
)
5249 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5251 m_mainWin
->SetItemCount(count
);
5254 void wxListCtrl::RefreshItem(long item
)
5256 m_mainWin
->RefreshLine(item
);
5259 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5261 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5264 void wxListCtrl::Freeze()
5266 m_mainWin
->Freeze();
5269 void wxListCtrl::Thaw()
5274 #endif // wxUSE_LISTCTRL