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_LONG(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 or -1
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 void SendListEvent(wxEventType type
, wxPoint pos
);
478 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
479 DECLARE_EVENT_TABLE()
482 //-----------------------------------------------------------------------------
483 // wxListRenameTimer (internal)
484 //-----------------------------------------------------------------------------
486 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
489 wxListMainWindow
*m_owner
;
492 wxListRenameTimer( wxListMainWindow
*owner
);
496 //-----------------------------------------------------------------------------
497 // wxListTextCtrl (internal)
498 //-----------------------------------------------------------------------------
500 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
503 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
506 void OnChar( wxKeyEvent
&event
);
507 void OnKeyUp( wxKeyEvent
&event
);
508 void OnKillFocus( wxFocusEvent
&event
);
510 bool AcceptChanges();
514 wxListMainWindow
*m_owner
;
515 wxString m_startValue
;
519 DECLARE_EVENT_TABLE()
522 //-----------------------------------------------------------------------------
523 // wxListMainWindow (internal)
524 //-----------------------------------------------------------------------------
526 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
527 #include "wx/listimpl.cpp"
528 WX_DEFINE_LIST(wxListHeaderDataList
);
530 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
534 wxListMainWindow( wxWindow
*parent
,
536 const wxPoint
& pos
= wxDefaultPosition
,
537 const wxSize
& size
= wxDefaultSize
,
539 const wxString
&name
= _T("listctrlmainwindow") );
541 virtual ~wxListMainWindow();
543 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
545 // return true if this is a virtual list control
546 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
548 // return true if the control is in report mode
549 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
551 // return true if we are in single selection mode, false if multi sel
552 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
554 // do we have a header window?
555 bool HasHeader() const
556 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
558 void HighlightAll( bool on
);
560 // all these functions only do something if the line is currently visible
562 // change the line "selected" state, return TRUE if it really changed
563 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
565 // as HighlightLine() but do it for the range of lines: this is incredibly
566 // more efficient for virtual list controls!
568 // NB: unlike HighlightLine() this one does refresh the lines on screen
569 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
571 // toggle the line state and refresh it
572 void ReverseHighlight( size_t line
)
573 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
575 // return true if the line is highlighted
576 bool IsHighlighted(size_t line
) const;
578 // refresh one or several lines at once
579 void RefreshLine( size_t line
);
580 void RefreshLines( size_t lineFrom
, size_t lineTo
);
582 // refresh all selected items
583 void RefreshSelected();
585 // refresh all lines below the given one: the difference with
586 // RefreshLines() is that the index here might not be a valid one (happens
587 // when the last line is deleted)
588 void RefreshAfter( size_t lineFrom
);
590 // the methods which are forwarded to wxListLineData itself in list/icon
591 // modes but are here because the lines don't store their positions in the
594 // get the bound rect for the entire line
595 wxRect
GetLineRect(size_t line
) const;
597 // get the bound rect of the label
598 wxRect
GetLineLabelRect(size_t line
) const;
600 // get the bound rect of the items icon (only may be called if we do have
602 wxRect
GetLineIconRect(size_t line
) const;
604 // get the rect to be highlighted when the item has focus
605 wxRect
GetLineHighlightRect(size_t line
) const;
607 // get the size of the total line rect
608 wxSize
GetLineSize(size_t line
) const
609 { return GetLineRect(line
).GetSize(); }
611 // return the hit code for the corresponding position (in this line)
612 long HitTestLine(size_t line
, int x
, int y
) const;
614 // bring the selected item into view, scrolling to it if necessary
615 void MoveToItem(size_t item
);
617 // bring the current item into view
618 void MoveToFocus() { MoveToItem(m_current
); }
620 // start editing the label of the given item
621 void EditLabel( long item
);
623 // suspend/resume redrawing the control
629 void OnRenameTimer();
630 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
632 void OnMouse( wxMouseEvent
&event
);
634 // called to switch the selection from the current item to newCurrent,
635 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
637 void OnChar( wxKeyEvent
&event
);
638 void OnKeyDown( wxKeyEvent
&event
);
639 void OnSetFocus( wxFocusEvent
&event
);
640 void OnKillFocus( wxFocusEvent
&event
);
641 void OnScroll(wxScrollWinEvent
& event
) ;
643 void OnPaint( wxPaintEvent
&event
);
645 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
646 void GetImageSize( int index
, int &width
, int &height
) const;
647 int GetTextLength( const wxString
&s
) const;
649 void SetImageList( wxImageList
*imageList
, int which
);
650 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
651 int GetItemSpacing( bool isSmall
= FALSE
);
653 void SetColumn( int col
, wxListItem
&item
);
654 void SetColumnWidth( int col
, int width
);
655 void GetColumn( int col
, wxListItem
&item
) const;
656 int GetColumnWidth( int col
) const;
657 int GetColumnCount() const { return m_columns
.GetCount(); }
659 // returns the sum of the heights of all columns
660 int GetHeaderWidth() const;
662 int GetCountPerPage() const;
664 void SetItem( wxListItem
&item
);
665 void GetItem( wxListItem
&item
) const;
666 void SetItemState( long item
, long state
, long stateMask
);
667 int GetItemState( long item
, long stateMask
) const;
668 void GetItemRect( long index
, wxRect
&rect
) const;
669 bool GetItemPosition( long item
, wxPoint
& pos
) const;
670 int GetSelectedItemCount() const;
672 wxString
GetItemText(long item
) const
675 info
.m_itemId
= item
;
680 void SetItemText(long item
, const wxString
& value
)
683 info
.m_mask
= wxLIST_MASK_TEXT
;
684 info
.m_itemId
= item
;
689 // set the scrollbars and update the positions of the items
690 void RecalculatePositions(bool noRefresh
= FALSE
);
692 // refresh the window and the header
695 long GetNextItem( long item
, int geometry
, int state
) const;
696 void DeleteItem( long index
);
697 void DeleteAllItems();
698 void DeleteColumn( int col
);
699 void DeleteEverything();
700 void EnsureVisible( long index
);
701 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
702 long FindItem( long start
, long data
);
703 long HitTest( int x
, int y
, int &flags
);
704 void InsertItem( wxListItem
&item
);
705 void InsertColumn( long col
, wxListItem
&item
);
706 void SortItems( wxListCtrlCompare fn
, long data
);
708 size_t GetItemCount() const;
709 bool IsEmpty() const { return GetItemCount() == 0; }
710 void SetItemCount(long count
);
712 // change the current (== focused) item, send a notification event
713 void ChangeCurrent(size_t current
);
714 void ResetCurrent() { ChangeCurrent((size_t)-1); }
715 bool HasCurrent() const { return m_current
!= (size_t)-1; }
717 // send out a wxListEvent
718 void SendNotify( size_t line
,
720 wxPoint point
= wxDefaultPosition
);
722 // override base class virtual to reset m_lineHeight when the font changes
723 virtual bool SetFont(const wxFont
& font
)
725 if ( !wxScrolledWindow::SetFont(font
) )
733 // these are for wxListLineData usage only
735 // get the backpointer to the list ctrl
736 wxListCtrl
*GetListCtrl() const
738 return wxStaticCast(GetParent(), wxListCtrl
);
741 // get the height of all lines (assuming they all do have the same height)
742 wxCoord
GetLineHeight() const;
744 // get the y position of the given line (only for report view)
745 wxCoord
GetLineY(size_t line
) const;
747 // get the brush to use for the item highlighting
748 wxBrush
*GetHighlightBrush() const
750 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
754 // the array of all line objects for a non virtual list control (for the
755 // virtual list control we only ever use m_lines[0])
756 wxListLineDataArray m_lines
;
758 // the list of column objects
759 wxListHeaderDataList m_columns
;
761 // currently focused item or -1
764 // the number of lines per page
767 // this flag is set when something which should result in the window
768 // redrawing happens (i.e. an item was added or deleted, or its appearance
769 // changed) and OnPaint() doesn't redraw the window while it is set which
770 // allows to minimize the number of repaintings when a lot of items are
771 // being added. The real repainting occurs only after the next OnIdle()
775 wxColour
*m_highlightColour
;
778 wxImageList
*m_small_image_list
;
779 wxImageList
*m_normal_image_list
;
781 int m_normal_spacing
;
785 wxTimer
*m_renameTimer
;
790 // for double click logic
791 size_t m_lineLastClicked
,
792 m_lineBeforeLastClicked
;
795 // the total count of items in a virtual list control
798 // the object maintaining the items selection state, only used in virtual
800 wxSelectionStore m_selStore
;
802 // common part of all ctors
805 // intiialize m_[xy]Scroll
806 void InitScrolling();
808 // get the line data for the given index
809 wxListLineData
*GetLine(size_t n
) const
811 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
815 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
823 // get a dummy line which can be used for geometry calculations and such:
824 // you must use GetLine() if you want to really draw the line
825 wxListLineData
*GetDummyLine() const;
827 // cache the line data of the n-th line in m_lines[0]
828 void CacheLineData(size_t line
);
830 // get the range of visible lines
831 void GetVisibleLinesRange(size_t *from
, size_t *to
);
833 // force us to recalculate the range of visible lines
834 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
836 // get the colour to be used for drawing the rules
837 wxColour
GetRuleColour() const
842 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
847 // initialize the current item if needed
848 void UpdateCurrent();
850 // delete all items but don't refresh: called from dtor
851 void DoDeleteAllItems();
853 // the height of one line using the current font
854 wxCoord m_lineHeight
;
856 // the total header width or 0 if not calculated yet
857 wxCoord m_headerWidth
;
859 // the first and last lines being shown on screen right now (inclusive),
860 // both may be -1 if they must be calculated so never access them directly:
861 // use GetVisibleLinesRange() above instead
865 // the brushes to use for item highlighting when we do/don't have focus
866 wxBrush
*m_highlightBrush
,
867 *m_highlightUnfocusedBrush
;
869 // if this is > 0, the control is frozen and doesn't redraw itself
870 size_t m_freezeCount
;
872 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
873 DECLARE_EVENT_TABLE()
876 // ============================================================================
878 // ============================================================================
880 // ----------------------------------------------------------------------------
882 // ----------------------------------------------------------------------------
884 bool wxSelectionStore::IsSelected(size_t item
) const
886 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
888 // if the default state is to be selected, being in m_itemsSel means that
889 // the item is not selected, so we have to inverse the logic
890 return m_defaultState
? !isSel
: isSel
;
893 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
895 // search for the item ourselves as like this we get the index where to
896 // insert it later if needed, so we do only one search in the array instead
897 // of two (adding item to a sorted array requires a search)
898 size_t index
= m_itemsSel
.IndexForInsert(item
);
899 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
901 if ( select
!= m_defaultState
)
905 m_itemsSel
.AddAt(item
, index
);
910 else // reset to default state
914 m_itemsSel
.RemoveAt(index
);
922 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
924 wxArrayInt
*itemsChanged
)
926 // 100 is hardcoded but it shouldn't matter much: the important thing is
927 // that we don't refresh everything when really few (e.g. 1 or 2) items
929 static const size_t MANY_ITEMS
= 100;
931 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
933 // are we going to have more [un]selected items than the other ones?
934 if ( itemTo
- itemFrom
> m_count
/2 )
936 if ( select
!= m_defaultState
)
938 // the default state now becomes the same as 'select'
939 m_defaultState
= select
;
941 // so all the old selections (which had state select) shouldn't be
942 // selected any more, but all the other ones should
943 wxIndexArray selOld
= m_itemsSel
;
946 // TODO: it should be possible to optimize the searches a bit
947 // knowing the possible range
950 for ( item
= 0; item
< itemFrom
; item
++ )
952 if ( selOld
.Index(item
) == wxNOT_FOUND
)
953 m_itemsSel
.Add(item
);
956 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
958 if ( selOld
.Index(item
) == wxNOT_FOUND
)
959 m_itemsSel
.Add(item
);
962 // many items (> half) changed state
965 else // select == m_defaultState
967 // get the inclusive range of items between itemFrom and itemTo
968 size_t count
= m_itemsSel
.GetCount(),
969 start
= m_itemsSel
.IndexForInsert(itemFrom
),
970 end
= m_itemsSel
.IndexForInsert(itemTo
);
972 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
977 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
984 // delete all of them (from end to avoid changing indices)
985 for ( int i
= end
; i
>= (int)start
; i
-- )
989 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
991 // stop counting (see comment below)
996 itemsChanged
->Add(m_itemsSel
[i
]);
1000 m_itemsSel
.RemoveAt(i
);
1005 else // "few" items change state
1009 itemsChanged
->Empty();
1012 // just add the items to the selection
1013 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1015 if ( SelectItem(item
, select
) && itemsChanged
)
1017 itemsChanged
->Add(item
);
1019 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1021 // stop counting them, we'll just eat gobs of memory
1022 // for nothing at all - faster to refresh everything in
1024 itemsChanged
= NULL
;
1030 // we set it to NULL if there are many items changing state
1031 return itemsChanged
!= NULL
;
1034 void wxSelectionStore::OnItemDelete(size_t item
)
1036 size_t count
= m_itemsSel
.GetCount(),
1037 i
= m_itemsSel
.IndexForInsert(item
);
1039 if ( i
< count
&& m_itemsSel
[i
] == item
)
1041 // this item itself was in m_itemsSel, remove it from there
1042 m_itemsSel
.RemoveAt(i
);
1047 // and adjust the index of all which follow it
1050 // all following elements must be greater than the one we deleted
1051 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1057 //-----------------------------------------------------------------------------
1059 //-----------------------------------------------------------------------------
1061 wxListItemData::~wxListItemData()
1063 // in the virtual list control the attributes are managed by the main
1064 // program, so don't delete them
1065 if ( !m_owner
->IsVirtual() )
1073 void wxListItemData::Init()
1081 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1087 if ( owner
->InReportView() )
1093 m_rect
= new wxRect
;
1097 void wxListItemData::SetItem( const wxListItem
&info
)
1099 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1100 SetText(info
.m_text
);
1101 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1102 m_image
= info
.m_image
;
1103 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1104 m_data
= info
.m_data
;
1106 if ( info
.HasAttributes() )
1109 *m_attr
= *info
.GetAttributes();
1111 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1119 m_rect
->width
= info
.m_width
;
1123 void wxListItemData::SetPosition( int x
, int y
)
1125 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1131 void wxListItemData::SetSize( int width
, int height
)
1133 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1136 m_rect
->width
= width
;
1138 m_rect
->height
= height
;
1141 bool wxListItemData::IsHit( int x
, int y
) const
1143 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1145 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1148 int wxListItemData::GetX() const
1150 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1155 int wxListItemData::GetY() const
1157 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1162 int wxListItemData::GetWidth() const
1164 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1166 return m_rect
->width
;
1169 int wxListItemData::GetHeight() const
1171 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1173 return m_rect
->height
;
1176 void wxListItemData::GetItem( wxListItem
&info
) const
1178 info
.m_text
= m_text
;
1179 info
.m_image
= m_image
;
1180 info
.m_data
= m_data
;
1184 if ( m_attr
->HasTextColour() )
1185 info
.SetTextColour(m_attr
->GetTextColour());
1186 if ( m_attr
->HasBackgroundColour() )
1187 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1188 if ( m_attr
->HasFont() )
1189 info
.SetFont(m_attr
->GetFont());
1193 //-----------------------------------------------------------------------------
1195 //-----------------------------------------------------------------------------
1197 void wxListHeaderData::Init()
1208 wxListHeaderData::wxListHeaderData()
1213 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1220 void wxListHeaderData::SetItem( const wxListItem
&item
)
1222 m_mask
= item
.m_mask
;
1224 if ( m_mask
& wxLIST_MASK_TEXT
)
1225 m_text
= item
.m_text
;
1227 if ( m_mask
& wxLIST_MASK_IMAGE
)
1228 m_image
= item
.m_image
;
1230 if ( m_mask
& wxLIST_MASK_FORMAT
)
1231 m_format
= item
.m_format
;
1233 if ( m_mask
& wxLIST_MASK_WIDTH
)
1234 SetWidth(item
.m_width
);
1237 void wxListHeaderData::SetPosition( int x
, int y
)
1243 void wxListHeaderData::SetHeight( int h
)
1248 void wxListHeaderData::SetWidth( int w
)
1252 m_width
= WIDTH_COL_DEFAULT
;
1253 else if (m_width
< WIDTH_COL_MIN
)
1254 m_width
= WIDTH_COL_MIN
;
1257 void wxListHeaderData::SetFormat( int format
)
1262 bool wxListHeaderData::HasImage() const
1264 return m_image
!= -1;
1267 bool wxListHeaderData::IsHit( int x
, int y
) const
1269 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1272 void wxListHeaderData::GetItem( wxListItem
& item
)
1274 item
.m_mask
= m_mask
;
1275 item
.m_text
= m_text
;
1276 item
.m_image
= m_image
;
1277 item
.m_format
= m_format
;
1278 item
.m_width
= m_width
;
1281 int wxListHeaderData::GetImage() const
1286 int wxListHeaderData::GetWidth() const
1291 int wxListHeaderData::GetFormat() const
1296 //-----------------------------------------------------------------------------
1298 //-----------------------------------------------------------------------------
1300 inline int wxListLineData::GetMode() const
1302 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1305 inline bool wxListLineData::InReportView() const
1307 return m_owner
->HasFlag(wxLC_REPORT
);
1310 inline bool wxListLineData::IsVirtual() const
1312 return m_owner
->IsVirtual();
1315 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1318 m_items
.DeleteContents( TRUE
);
1320 if ( InReportView() )
1326 m_gi
= new GeometryInfo
;
1329 m_highlighted
= FALSE
;
1331 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1334 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1336 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1337 wxCHECK_RET( node
, _T("no subitems at all??") );
1339 wxListItemData
*item
= node
->GetData();
1341 switch ( GetMode() )
1344 case wxLC_SMALL_ICON
:
1346 m_gi
->m_rectAll
.width
= spacing
;
1348 wxString s
= item
->GetText();
1354 m_gi
->m_rectLabel
.width
=
1355 m_gi
->m_rectLabel
.height
= 0;
1359 dc
->GetTextExtent( s
, &lw
, &lh
);
1360 if (lh
< SCROLL_UNIT_Y
)
1365 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1367 m_gi
->m_rectAll
.width
= lw
;
1369 m_gi
->m_rectLabel
.width
= lw
;
1370 m_gi
->m_rectLabel
.height
= lh
;
1373 if (item
->HasImage())
1376 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1377 m_gi
->m_rectIcon
.width
= w
+ 8;
1378 m_gi
->m_rectIcon
.height
= h
+ 8;
1380 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1381 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1382 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1383 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1386 if ( item
->HasText() )
1388 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1389 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1391 else // no text, highlight the icon
1393 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1394 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1401 wxString s
= item
->GetTextForMeasuring();
1404 dc
->GetTextExtent( s
, &lw
, &lh
);
1405 if (lh
< SCROLL_UNIT_Y
)
1410 m_gi
->m_rectLabel
.width
= lw
;
1411 m_gi
->m_rectLabel
.height
= lh
;
1413 m_gi
->m_rectAll
.width
= lw
;
1414 m_gi
->m_rectAll
.height
= lh
;
1416 if (item
->HasImage())
1419 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1420 m_gi
->m_rectIcon
.width
= w
;
1421 m_gi
->m_rectIcon
.height
= h
;
1423 m_gi
->m_rectAll
.width
+= 4 + w
;
1424 if (h
> m_gi
->m_rectAll
.height
)
1425 m_gi
->m_rectAll
.height
= h
;
1428 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1429 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1434 wxFAIL_MSG( _T("unexpected call to SetSize") );
1438 wxFAIL_MSG( _T("unknown mode") );
1442 void wxListLineData::SetPosition( int x
, int y
,
1446 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1447 wxCHECK_RET( node
, _T("no subitems at all??") );
1449 wxListItemData
*item
= node
->GetData();
1451 switch ( GetMode() )
1454 case wxLC_SMALL_ICON
:
1455 m_gi
->m_rectAll
.x
= x
;
1456 m_gi
->m_rectAll
.y
= y
;
1458 if ( item
->HasImage() )
1460 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1461 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1462 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1465 if ( item
->HasText() )
1467 if (m_gi
->m_rectAll
.width
> spacing
)
1468 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1470 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1471 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1472 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1473 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1475 else // no text, highlight the icon
1477 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1478 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1483 m_gi
->m_rectAll
.x
= x
;
1484 m_gi
->m_rectAll
.y
= y
;
1486 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1487 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1488 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1490 if (item
->HasImage())
1492 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1493 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1494 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1498 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1503 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1507 wxFAIL_MSG( _T("unknown mode") );
1511 void wxListLineData::InitItems( int num
)
1513 for (int i
= 0; i
< num
; i
++)
1514 m_items
.Append( new wxListItemData(m_owner
) );
1517 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1519 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1520 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1522 wxListItemData
*item
= node
->GetData();
1523 item
->SetItem( info
);
1526 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1528 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1531 wxListItemData
*item
= node
->GetData();
1532 item
->GetItem( info
);
1536 wxString
wxListLineData::GetText(int index
) const
1540 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1543 wxListItemData
*item
= node
->GetData();
1544 s
= item
->GetText();
1550 void wxListLineData::SetText( int index
, const wxString s
)
1552 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1555 wxListItemData
*item
= node
->GetData();
1560 void wxListLineData::SetImage( int index
, int image
)
1562 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1563 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1565 wxListItemData
*item
= node
->GetData();
1566 item
->SetImage(image
);
1569 int wxListLineData::GetImage( int index
) const
1571 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1572 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1574 wxListItemData
*item
= node
->GetData();
1575 return item
->GetImage();
1578 wxListItemAttr
*wxListLineData::GetAttr() const
1580 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1581 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1583 wxListItemData
*item
= node
->GetData();
1584 return item
->GetAttr();
1587 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1589 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1590 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1592 wxListItemData
*item
= node
->GetData();
1593 item
->SetAttr(attr
);
1596 bool wxListLineData::SetAttributes(wxDC
*dc
,
1597 const wxListItemAttr
*attr
,
1600 wxWindow
*listctrl
= m_owner
->GetParent();
1604 // don't use foreground colour for drawing highlighted items - this might
1605 // make them completely invisible (and there is no way to do bit
1606 // arithmetics on wxColour, unfortunately)
1610 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1614 if ( attr
&& attr
->HasTextColour() )
1616 colText
= attr
->GetTextColour();
1620 colText
= listctrl
->GetForegroundColour();
1624 dc
->SetTextForeground(colText
);
1628 if ( attr
&& attr
->HasFont() )
1630 font
= attr
->GetFont();
1634 font
= listctrl
->GetFont();
1640 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1641 if ( highlighted
|| hasBgCol
)
1645 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1649 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1652 dc
->SetPen( *wxTRANSPARENT_PEN
);
1660 void wxListLineData::Draw( wxDC
*dc
)
1662 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1663 wxCHECK_RET( node
, _T("no subitems at all??") );
1665 bool highlighted
= IsHighlighted();
1667 wxListItemAttr
*attr
= GetAttr();
1669 if ( SetAttributes(dc
, attr
, highlighted
) )
1671 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1674 wxListItemData
*item
= node
->GetData();
1675 if (item
->HasImage())
1677 wxRect rectIcon
= m_gi
->m_rectIcon
;
1678 m_owner
->DrawImage( item
->GetImage(), dc
,
1679 rectIcon
.x
, rectIcon
.y
);
1682 if (item
->HasText())
1684 wxRect rectLabel
= m_gi
->m_rectLabel
;
1686 wxDCClipper
clipper(*dc
, rectLabel
);
1687 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1691 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1693 const wxRect
& rectHL
,
1696 // TODO: later we should support setting different attributes for
1697 // different columns - to do it, just add "col" argument to
1698 // GetAttr() and move these lines into the loop below
1699 wxListItemAttr
*attr
= GetAttr();
1700 if ( SetAttributes(dc
, attr
, highlighted
) )
1702 dc
->DrawRectangle( rectHL
);
1705 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1706 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1709 for ( wxListItemDataList::Node
*node
= m_items
.GetFirst();
1711 node
= node
->GetNext(), col
++ )
1713 wxListItemData
*item
= node
->GetData();
1715 int width
= m_owner
->GetColumnWidth(col
);
1719 if ( item
->HasImage() )
1722 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1723 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1725 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1731 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1733 if ( item
->HasText() )
1735 dc
->DrawText( item
->GetText(), xOld
, y
);
1740 bool wxListLineData::Highlight( bool on
)
1742 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1744 if ( on
== m_highlighted
)
1752 void wxListLineData::ReverseHighlight( void )
1754 Highlight(!IsHighlighted());
1757 //-----------------------------------------------------------------------------
1758 // wxListHeaderWindow
1759 //-----------------------------------------------------------------------------
1761 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1763 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1764 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1765 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1766 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1769 void wxListHeaderWindow::Init()
1771 m_currentCursor
= (wxCursor
*) NULL
;
1772 m_isDragging
= FALSE
;
1776 wxListHeaderWindow::wxListHeaderWindow()
1780 m_owner
= (wxListMainWindow
*) NULL
;
1781 m_resizeCursor
= (wxCursor
*) NULL
;
1784 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1786 wxListMainWindow
*owner
,
1790 const wxString
&name
)
1791 : wxWindow( win
, id
, pos
, size
, style
, name
)
1796 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1798 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1801 wxListHeaderWindow::~wxListHeaderWindow()
1803 delete m_resizeCursor
;
1806 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1808 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1809 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1810 : GTK_STATE_INSENSITIVE
;
1812 x
= dc
->XLOG2DEV( x
);
1814 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1815 state
, GTK_SHADOW_OUT
,
1816 (GdkRectangle
*) NULL
, m_wxwindow
,
1817 (char *)"button", // const_cast
1818 x
-1, y
-1, w
+2, h
+2);
1819 #elif defined( __WXMAC__ )
1820 const int m_corner
= 1;
1822 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1824 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1825 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1826 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1828 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1831 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1832 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1834 dc
->SetPen( *wxWHITE_PEN
);
1835 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1836 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1837 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1838 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1840 const int m_corner
= 1;
1842 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1844 dc
->SetPen( *wxBLACK_PEN
);
1845 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1846 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1848 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1851 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1852 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1854 dc
->SetPen( *wxWHITE_PEN
);
1855 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1856 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1857 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1858 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1862 // shift the DC origin to match the position of the main window horz
1863 // scrollbar: this allows us to always use logical coords
1864 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1867 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1870 m_owner
->GetViewStart( &x
, NULL
);
1872 // account for the horz scrollbar offset
1873 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1876 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1878 #if defined(__WXGTK__)
1879 wxClientDC
dc( this );
1881 wxPaintDC
dc( this );
1889 dc
.SetFont( GetFont() );
1891 // width and height of the entire header window
1893 GetClientSize( &w
, &h
);
1894 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1896 dc
.SetBackgroundMode(wxTRANSPARENT
);
1898 // do *not* use the listctrl colour for headers - one day we will have a
1899 // function to set it separately
1900 //dc.SetTextForeground( *wxBLACK );
1901 dc
.SetTextForeground(wxSystemSettings::
1902 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1904 int x
= HEADER_OFFSET_X
;
1906 int numColumns
= m_owner
->GetColumnCount();
1908 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1910 m_owner
->GetColumn( i
, item
);
1911 int wCol
= item
.m_width
;
1913 // the width of the rect to draw: make it smaller to fit entirely
1914 // inside the column rect
1917 dc
.SetPen( *wxWHITE_PEN
);
1919 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1921 // if we have an image, draw it on the right of the label
1922 int image
= item
.m_image
;
1925 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1929 imageList
->GetSize(image
, ix
, iy
);
1936 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1937 wxIMAGELIST_DRAW_TRANSPARENT
1942 //else: ignore the column image
1945 // draw the text clipping it so that it doesn't overwrite the column
1947 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1949 dc
.DrawText( item
.GetText(),
1950 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1958 void wxListHeaderWindow::DrawCurrent()
1960 int x1
= m_currentX
;
1962 m_owner
->ClientToScreen( &x1
, &y1
);
1964 int x2
= m_currentX
;
1966 m_owner
->GetClientSize( NULL
, &y2
);
1967 m_owner
->ClientToScreen( &x2
, &y2
);
1970 dc
.SetLogicalFunction( wxINVERT
);
1971 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1972 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1976 dc
.DrawLine( x1
, y1
, x2
, y2
);
1978 dc
.SetLogicalFunction( wxCOPY
);
1980 dc
.SetPen( wxNullPen
);
1981 dc
.SetBrush( wxNullBrush
);
1984 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1986 // we want to work with logical coords
1988 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1989 int y
= event
.GetY();
1993 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
,
1994 event
.GetPosition());
1996 // we don't draw the line beyond our window, but we allow dragging it
1999 GetClientSize( &w
, NULL
);
2000 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2003 // erase the line if it was drawn
2004 if ( m_currentX
< w
)
2007 if (event
.ButtonUp())
2010 m_isDragging
= FALSE
;
2012 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2013 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
,
2014 event
.GetPosition());
2021 m_currentX
= m_minX
+ 7;
2023 // draw in the new location
2024 if ( m_currentX
< w
)
2028 else // not dragging
2031 bool hit_border
= FALSE
;
2033 // end of the current column
2036 // find the column where this event occured
2038 countCol
= m_owner
->GetColumnCount();
2039 for (col
= 0; col
< countCol
; col
++)
2041 xpos
+= m_owner
->GetColumnWidth( col
);
2044 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2046 // near the column border
2053 // inside the column
2060 if ( col
== countCol
)
2063 if (event
.LeftDown() || event
.RightUp())
2065 if (hit_border
&& event
.LeftDown())
2067 m_isDragging
= TRUE
;
2071 SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2072 event
.GetPosition());
2074 else // click on a column
2076 SendListEvent( event
.LeftDown()
2077 ? wxEVT_COMMAND_LIST_COL_CLICK
2078 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2079 event
.GetPosition());
2082 else if (event
.Moving())
2087 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2088 m_currentCursor
= m_resizeCursor
;
2092 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2093 m_currentCursor
= wxSTANDARD_CURSOR
;
2097 SetCursor(*m_currentCursor
);
2102 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2104 m_owner
->SetFocus();
2107 void wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2109 wxWindow
*parent
= GetParent();
2110 wxListEvent
le( type
, parent
->GetId() );
2111 le
.SetEventObject( parent
);
2112 le
.m_pointDrag
= pos
;
2114 // the position should be relative to the parent window, not
2115 // this one for compatibility with MSW and common sense: the
2116 // user code doesn't know anything at all about this header
2117 // window, so why should it get positions relative to it?
2118 le
.m_pointDrag
.y
-= GetSize().y
;
2120 le
.m_col
= m_column
;
2121 parent
->GetEventHandler()->ProcessEvent( le
);
2124 //-----------------------------------------------------------------------------
2125 // wxListRenameTimer (internal)
2126 //-----------------------------------------------------------------------------
2128 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2133 void wxListRenameTimer::Notify()
2135 m_owner
->OnRenameTimer();
2138 //-----------------------------------------------------------------------------
2139 // wxListTextCtrl (internal)
2140 //-----------------------------------------------------------------------------
2142 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2143 EVT_CHAR (wxListTextCtrl::OnChar
)
2144 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2145 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2148 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2149 : m_startValue(owner
->GetItemText(itemEdit
)),
2150 m_itemEdited(itemEdit
)
2155 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2157 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2158 &rectLabel
.x
, &rectLabel
.y
);
2160 (void)Create(owner
, wxID_ANY
, m_startValue
,
2161 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2162 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2165 void wxListTextCtrl::Finish()
2169 wxPendingDelete
.Append(this);
2173 m_owner
->SetFocus();
2177 bool wxListTextCtrl::AcceptChanges()
2179 const wxString value
= GetValue();
2181 if ( value
== m_startValue
)
2183 // nothing changed, always accept
2187 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2189 // vetoed by the user
2193 // accepted, do rename the item
2194 m_owner
->SetItemText(m_itemEdited
, value
);
2199 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2201 switch ( event
.m_keyCode
)
2204 if ( !AcceptChanges() )
2206 // vetoed by the user code
2209 //else: fall through
2220 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2228 // auto-grow the textctrl:
2229 wxSize parentSize
= m_owner
->GetSize();
2230 wxPoint myPos
= GetPosition();
2231 wxSize mySize
= GetSize();
2233 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2234 if (myPos
.x
+ sx
> parentSize
.x
)
2235 sx
= parentSize
.x
- myPos
.x
;
2243 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2247 (void)AcceptChanges();
2255 //-----------------------------------------------------------------------------
2257 //-----------------------------------------------------------------------------
2259 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2261 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2262 EVT_PAINT (wxListMainWindow::OnPaint
)
2263 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2264 EVT_CHAR (wxListMainWindow::OnChar
)
2265 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2266 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2267 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2268 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2271 void wxListMainWindow::Init()
2273 m_columns
.DeleteContents( TRUE
);
2277 m_lineTo
= (size_t)-1;
2283 m_small_image_list
= (wxImageList
*) NULL
;
2284 m_normal_image_list
= (wxImageList
*) NULL
;
2286 m_small_spacing
= 30;
2287 m_normal_spacing
= 40;
2291 m_isCreated
= FALSE
;
2293 m_lastOnSame
= FALSE
;
2294 m_renameTimer
= new wxListRenameTimer( this );
2298 m_lineBeforeLastClicked
= (size_t)-1;
2303 void wxListMainWindow::InitScrolling()
2305 if ( HasFlag(wxLC_REPORT
) )
2307 m_xScroll
= SCROLL_UNIT_X
;
2308 m_yScroll
= SCROLL_UNIT_Y
;
2312 m_xScroll
= SCROLL_UNIT_Y
;
2317 wxListMainWindow::wxListMainWindow()
2322 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2328 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2333 const wxString
&name
)
2334 : wxScrolledWindow( parent
, id
, pos
, size
,
2335 style
| wxHSCROLL
| wxVSCROLL
, name
)
2339 m_highlightBrush
= new wxBrush
2341 wxSystemSettings::GetColour
2343 wxSYS_COLOUR_HIGHLIGHT
2348 m_highlightUnfocusedBrush
= new wxBrush
2350 wxSystemSettings::GetColour
2352 wxSYS_COLOUR_BTNSHADOW
2361 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2363 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2366 wxListMainWindow::~wxListMainWindow()
2370 delete m_highlightBrush
;
2371 delete m_highlightUnfocusedBrush
;
2373 delete m_renameTimer
;
2376 void wxListMainWindow::CacheLineData(size_t line
)
2378 wxListCtrl
*listctrl
= GetListCtrl();
2380 wxListLineData
*ld
= GetDummyLine();
2382 size_t countCol
= GetColumnCount();
2383 for ( size_t col
= 0; col
< countCol
; col
++ )
2385 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2388 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2389 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2392 wxListLineData
*wxListMainWindow::GetDummyLine() const
2394 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2396 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2398 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2400 // we need to recreate the dummy line if the number of columns in the
2401 // control changed as it would have the incorrect number of fields
2403 if ( !m_lines
.IsEmpty() &&
2404 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2406 self
->m_lines
.Clear();
2409 if ( m_lines
.IsEmpty() )
2411 wxListLineData
*line
= new wxListLineData(self
);
2412 self
->m_lines
.Add(line
);
2414 // don't waste extra memory -- there never going to be anything
2415 // else/more in this array
2416 self
->m_lines
.Shrink();
2422 // ----------------------------------------------------------------------------
2423 // line geometry (report mode only)
2424 // ----------------------------------------------------------------------------
2426 wxCoord
wxListMainWindow::GetLineHeight() const
2428 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2430 // we cache the line height as calling GetTextExtent() is slow
2431 if ( !m_lineHeight
)
2433 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2435 wxClientDC
dc( self
);
2436 dc
.SetFont( GetFont() );
2439 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2441 if ( y
< SCROLL_UNIT_Y
)
2445 self
->m_lineHeight
= y
+ LINE_SPACING
;
2448 return m_lineHeight
;
2451 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2453 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2455 return LINE_SPACING
+ line
*GetLineHeight();
2458 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2460 if ( !InReportView() )
2461 return GetLine(line
)->m_gi
->m_rectAll
;
2464 rect
.x
= HEADER_OFFSET_X
;
2465 rect
.y
= GetLineY(line
);
2466 rect
.width
= GetHeaderWidth();
2467 rect
.height
= GetLineHeight();
2472 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2474 if ( !InReportView() )
2475 return GetLine(line
)->m_gi
->m_rectLabel
;
2478 rect
.x
= HEADER_OFFSET_X
;
2479 rect
.y
= GetLineY(line
);
2480 rect
.width
= GetColumnWidth(0);
2481 rect
.height
= GetLineHeight();
2486 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2488 if ( !InReportView() )
2489 return GetLine(line
)->m_gi
->m_rectIcon
;
2491 wxListLineData
*ld
= GetLine(line
);
2492 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2495 rect
.x
= HEADER_OFFSET_X
;
2496 rect
.y
= GetLineY(line
);
2497 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2502 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2504 return InReportView() ? GetLineRect(line
)
2505 : GetLine(line
)->m_gi
->m_rectHighlight
;
2508 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2510 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2512 wxListLineData
*ld
= GetLine(line
);
2514 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2515 return wxLIST_HITTEST_ONITEMICON
;
2517 // VS: Testing for "ld->HasText() || InReportView()" instead of
2518 // "ld->HasText()" is needed to make empty lines in report view
2520 if ( ld
->HasText() || InReportView() )
2522 wxRect rect
= InReportView() ? GetLineRect(line
)
2523 : GetLineLabelRect(line
);
2525 if ( rect
.Inside(x
, y
) )
2526 return wxLIST_HITTEST_ONITEMLABEL
;
2532 // ----------------------------------------------------------------------------
2533 // highlight (selection) handling
2534 // ----------------------------------------------------------------------------
2536 bool wxListMainWindow::IsHighlighted(size_t line
) const
2540 return m_selStore
.IsSelected(line
);
2544 wxListLineData
*ld
= GetLine(line
);
2545 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2547 return ld
->IsHighlighted();
2551 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2557 wxArrayInt linesChanged
;
2558 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2561 // meny items changed state, refresh everything
2562 RefreshLines(lineFrom
, lineTo
);
2564 else // only a few items changed state, refresh only them
2566 size_t count
= linesChanged
.GetCount();
2567 for ( size_t n
= 0; n
< count
; n
++ )
2569 RefreshLine(linesChanged
[n
]);
2573 else // iterate over all items in non report view
2575 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2577 if ( HighlightLine(line
, highlight
) )
2585 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2591 changed
= m_selStore
.SelectItem(line
, highlight
);
2595 wxListLineData
*ld
= GetLine(line
);
2596 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2598 changed
= ld
->Highlight(highlight
);
2603 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2604 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2610 void wxListMainWindow::RefreshLine( size_t line
)
2612 if ( HasFlag(wxLC_REPORT
) )
2614 size_t visibleFrom
, visibleTo
;
2615 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2617 if ( line
< visibleFrom
|| line
> visibleTo
)
2621 wxRect rect
= GetLineRect(line
);
2623 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2624 RefreshRect( rect
);
2627 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2629 // we suppose that they are ordered by caller
2630 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2632 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2634 if ( HasFlag(wxLC_REPORT
) )
2636 size_t visibleFrom
, visibleTo
;
2637 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2639 if ( lineFrom
< visibleFrom
)
2640 lineFrom
= visibleFrom
;
2641 if ( lineTo
> visibleTo
)
2646 rect
.y
= GetLineY(lineFrom
);
2647 rect
.width
= GetClientSize().x
;
2648 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2650 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2651 RefreshRect( rect
);
2655 // TODO: this should be optimized...
2656 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2663 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2665 if ( HasFlag(wxLC_REPORT
) )
2668 GetVisibleLinesRange(&visibleFrom
, NULL
);
2670 if ( lineFrom
< visibleFrom
)
2671 lineFrom
= visibleFrom
;
2675 rect
.y
= GetLineY(lineFrom
);
2677 wxSize size
= GetClientSize();
2678 rect
.width
= size
.x
;
2679 // refresh till the bottom of the window
2680 rect
.height
= size
.y
- rect
.y
;
2682 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2683 RefreshRect( rect
);
2687 // TODO: how to do it more efficiently?
2692 void wxListMainWindow::RefreshSelected()
2698 if ( InReportView() )
2700 GetVisibleLinesRange(&from
, &to
);
2705 to
= GetItemCount() - 1;
2708 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2710 RefreshLine(m_current
);
2713 for ( size_t line
= from
; line
<= to
; line
++ )
2715 // NB: the test works as expected even if m_current == -1
2716 if ( line
!= m_current
&& IsHighlighted(line
) )
2723 void wxListMainWindow::Freeze()
2728 void wxListMainWindow::Thaw()
2730 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2732 if ( !--m_freezeCount
)
2738 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2740 // Note: a wxPaintDC must be constructed even if no drawing is
2741 // done (a Windows requirement).
2742 wxPaintDC
dc( this );
2744 if ( IsEmpty() || m_freezeCount
)
2746 // nothing to draw or not the moment to draw it
2752 // delay the repainting until we calculate all the items positions
2759 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2763 dc
.SetFont( GetFont() );
2765 if ( HasFlag(wxLC_REPORT
) )
2767 int lineHeight
= GetLineHeight();
2769 size_t visibleFrom
, visibleTo
;
2770 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2773 wxCoord xOrig
, yOrig
;
2774 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2776 // tell the caller cache to cache the data
2779 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2780 GetParent()->GetId());
2781 evCache
.SetEventObject( GetParent() );
2782 evCache
.m_oldItemIndex
= visibleFrom
;
2783 evCache
.m_itemIndex
= visibleTo
;
2784 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2787 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2789 rectLine
= GetLineRect(line
);
2791 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2792 rectLine
.width
, rectLine
.height
) )
2794 // don't redraw unaffected lines to avoid flicker
2798 GetLine(line
)->DrawInReportMode( &dc
,
2800 GetLineHighlightRect(line
),
2801 IsHighlighted(line
) );
2804 if ( HasFlag(wxLC_HRULES
) )
2806 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2807 wxSize clientSize
= GetClientSize();
2809 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2812 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2813 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2814 clientSize
.x
- dev_x
, i
*lineHeight
);
2817 // Draw last horizontal rule
2818 if ( visibleTo
> visibleFrom
)
2821 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2822 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2823 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2827 // Draw vertical rules if required
2828 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2830 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2833 wxRect firstItemRect
;
2834 wxRect lastItemRect
;
2835 GetItemRect(0, firstItemRect
);
2836 GetItemRect(GetItemCount() - 1, lastItemRect
);
2837 int x
= firstItemRect
.GetX();
2839 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2840 for (col
= 0; col
< GetColumnCount(); col
++)
2842 int colWidth
= GetColumnWidth(col
);
2844 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2845 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2851 size_t count
= GetItemCount();
2852 for ( size_t i
= 0; i
< count
; i
++ )
2854 GetLine(i
)->Draw( &dc
);
2860 // don't draw rect outline under Max if we already have the background
2861 // color but under other platforms only draw it if we do: it is a bit
2862 // silly to draw "focus rect" if we don't have focus!
2867 #endif // __WXMAC__/!__WXMAC__
2869 dc
.SetPen( *wxBLACK_PEN
);
2870 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2871 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2878 void wxListMainWindow::HighlightAll( bool on
)
2880 if ( IsSingleSel() )
2882 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2884 // we just have one item to turn off
2885 if ( HasCurrent() && IsHighlighted(m_current
) )
2887 HighlightLine(m_current
, FALSE
);
2888 RefreshLine(m_current
);
2893 HighlightLines(0, GetItemCount() - 1, on
);
2897 void wxListMainWindow::SendNotify( size_t line
,
2898 wxEventType command
,
2901 wxListEvent
le( command
, GetParent()->GetId() );
2902 le
.SetEventObject( GetParent() );
2903 le
.m_itemIndex
= line
;
2905 // set only for events which have position
2906 if ( point
!= wxDefaultPosition
)
2907 le
.m_pointDrag
= point
;
2909 // don't try to get the line info for virtual list controls: the main
2910 // program has it anyhow and if we did it would result in accessing all
2911 // the lines, even those which are not visible now and this is precisely
2912 // what we're trying to avoid
2913 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2915 if ( line
!= (size_t)-1 )
2917 GetLine(line
)->GetItem( 0, le
.m_item
);
2919 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2921 //else: there may be no more such item
2923 GetParent()->GetEventHandler()->ProcessEvent( le
);
2926 void wxListMainWindow::ChangeCurrent(size_t current
)
2928 m_current
= current
;
2930 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2933 void wxListMainWindow::EditLabel( long item
)
2935 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2936 wxT("wrong index in wxListCtrl::EditLabel()") );
2938 size_t itemEdit
= (size_t)item
;
2940 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2941 le
.SetEventObject( GetParent() );
2942 le
.m_itemIndex
= item
;
2943 wxListLineData
*data
= GetLine(itemEdit
);
2944 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2945 data
->GetItem( 0, le
.m_item
);
2946 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2948 // vetoed by user code
2952 // We have to call this here because the label in question might just have
2953 // been added and no screen update taken place.
2957 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2962 void wxListMainWindow::OnRenameTimer()
2964 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2966 EditLabel( m_current
);
2969 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2971 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2972 le
.SetEventObject( GetParent() );
2973 le
.m_itemIndex
= itemEdit
;
2975 wxListLineData
*data
= GetLine(itemEdit
);
2976 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2978 data
->GetItem( 0, le
.m_item
);
2979 le
.m_item
.m_text
= value
;
2980 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2984 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2986 event
.SetEventObject( GetParent() );
2987 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2990 if ( !HasCurrent() || IsEmpty() )
2996 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2997 event
.ButtonDClick()) )
3000 int x
= event
.GetX();
3001 int y
= event
.GetY();
3002 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3004 // where did we hit it (if we did)?
3007 size_t count
= GetItemCount(),
3010 if ( HasFlag(wxLC_REPORT
) )
3012 current
= y
/ GetLineHeight();
3013 if ( current
< count
)
3014 hitResult
= HitTestLine(current
, x
, y
);
3018 // TODO: optimize it too! this is less simple than for report view but
3019 // enumerating all items is still not a way to do it!!
3020 for ( current
= 0; current
< count
; current
++ )
3022 hitResult
= HitTestLine(current
, x
, y
);
3028 if (event
.Dragging())
3030 if (m_dragCount
== 0)
3032 // we have to report the raw, physical coords as we want to be
3033 // able to call HitTest(event.m_pointDrag) from the user code to
3034 // get the item being dragged
3035 m_dragStart
= event
.GetPosition();
3040 if (m_dragCount
!= 3)
3043 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3044 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3046 wxListEvent
le( command
, GetParent()->GetId() );
3047 le
.SetEventObject( GetParent() );
3048 le
.m_pointDrag
= m_dragStart
;
3049 GetParent()->GetEventHandler()->ProcessEvent( le
);
3060 // outside of any item
3064 bool forceClick
= FALSE
;
3065 if (event
.ButtonDClick())
3067 m_renameTimer
->Stop();
3068 m_lastOnSame
= FALSE
;
3071 // FIXME: wxGTK generates bad sequence of events prior to doubleclick
3072 // ("down, up, down, double, up" while other ports
3073 // do "down, up, double, up"). We have to have this hack
3074 // in place till somebody fixes wxGTK...
3075 if ( current
== m_lineBeforeLastClicked
)
3077 if ( current
== m_lineLastClicked
)
3080 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3086 // the first click was on another item, so don't interpret this as
3087 // a double click, but as a simple click instead
3092 if (event
.LeftUp() && m_lastOnSame
)
3094 if ((current
== m_current
) &&
3095 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3096 HasFlag(wxLC_EDIT_LABELS
) )
3098 m_renameTimer
->Start( 100, TRUE
);
3100 m_lastOnSame
= FALSE
;
3102 else if (event
.RightDown())
3104 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3105 event
.GetPosition() );
3107 else if (event
.MiddleDown())
3109 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3111 else if ( event
.LeftDown() || forceClick
)
3113 m_lineBeforeLastClicked
= m_lineLastClicked
;
3114 m_lineLastClicked
= current
;
3116 size_t oldCurrent
= m_current
;
3118 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3120 HighlightAll( FALSE
);
3122 ChangeCurrent(current
);
3124 ReverseHighlight(m_current
);
3126 else // multi sel & either ctrl or shift is down
3128 if (event
.ControlDown())
3130 ChangeCurrent(current
);
3132 ReverseHighlight(m_current
);
3134 else if (event
.ShiftDown())
3136 ChangeCurrent(current
);
3138 size_t lineFrom
= oldCurrent
,
3141 if ( lineTo
< lineFrom
)
3144 lineFrom
= m_current
;
3147 HighlightLines(lineFrom
, lineTo
);
3149 else // !ctrl, !shift
3151 // test in the enclosing if should make it impossible
3152 wxFAIL_MSG( _T("how did we get here?") );
3156 if (m_current
!= oldCurrent
)
3158 RefreshLine( oldCurrent
);
3161 // forceClick is only set if the previous click was on another item
3162 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3166 void wxListMainWindow::MoveToItem(size_t item
)
3168 if ( item
== (size_t)-1 )
3171 wxRect rect
= GetLineRect(item
);
3173 int client_w
, client_h
;
3174 GetClientSize( &client_w
, &client_h
);
3176 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3177 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3179 if ( HasFlag(wxLC_REPORT
) )
3181 // the next we need the range of lines shown it might be different, so
3183 ResetVisibleLinesRange();
3185 if (rect
.y
< view_y
)
3186 Scroll( -1, rect
.y
/m_yScroll
);
3187 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3188 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3192 if (rect
.x
-view_x
< 5)
3193 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3194 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3195 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3199 // ----------------------------------------------------------------------------
3200 // keyboard handling
3201 // ----------------------------------------------------------------------------
3203 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3205 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3206 _T("invalid item index in OnArrowChar()") );
3208 size_t oldCurrent
= m_current
;
3210 // in single selection we just ignore Shift as we can't select several
3212 if ( event
.ShiftDown() && !IsSingleSel() )
3214 ChangeCurrent(newCurrent
);
3216 // select all the items between the old and the new one
3217 if ( oldCurrent
> newCurrent
)
3219 newCurrent
= oldCurrent
;
3220 oldCurrent
= m_current
;
3223 HighlightLines(oldCurrent
, newCurrent
);
3227 // all previously selected items are unselected unless ctrl is held
3228 if ( !event
.ControlDown() )
3229 HighlightAll(FALSE
);
3231 ChangeCurrent(newCurrent
);
3233 // refresh the old focus to remove it
3234 RefreshLine( oldCurrent
);
3236 if ( !event
.ControlDown() )
3238 HighlightLine( m_current
, TRUE
);
3242 RefreshLine( m_current
);
3247 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3249 wxWindow
*parent
= GetParent();
3251 /* we propagate the key event up */
3252 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3253 ke
.m_shiftDown
= event
.m_shiftDown
;
3254 ke
.m_controlDown
= event
.m_controlDown
;
3255 ke
.m_altDown
= event
.m_altDown
;
3256 ke
.m_metaDown
= event
.m_metaDown
;
3257 ke
.m_keyCode
= event
.m_keyCode
;
3260 ke
.SetEventObject( parent
);
3261 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3266 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3268 wxWindow
*parent
= GetParent();
3270 /* we send a list_key event up */
3273 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3274 le
.m_itemIndex
= m_current
;
3275 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3276 le
.m_code
= (int)event
.KeyCode();
3277 le
.SetEventObject( parent
);
3278 parent
->GetEventHandler()->ProcessEvent( le
);
3281 /* we propagate the char event up */
3282 wxKeyEvent
ke( wxEVT_CHAR
);
3283 ke
.m_shiftDown
= event
.m_shiftDown
;
3284 ke
.m_controlDown
= event
.m_controlDown
;
3285 ke
.m_altDown
= event
.m_altDown
;
3286 ke
.m_metaDown
= event
.m_metaDown
;
3287 ke
.m_keyCode
= event
.m_keyCode
;
3290 ke
.SetEventObject( parent
);
3291 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3293 if (event
.KeyCode() == WXK_TAB
)
3295 wxNavigationKeyEvent nevent
;
3296 nevent
.SetWindowChange( event
.ControlDown() );
3297 nevent
.SetDirection( !event
.ShiftDown() );
3298 nevent
.SetEventObject( GetParent()->GetParent() );
3299 nevent
.SetCurrentFocus( m_parent
);
3300 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3304 /* no item -> nothing to do */
3311 switch (event
.KeyCode())
3314 if ( m_current
> 0 )
3315 OnArrowChar( m_current
- 1, event
);
3319 if ( m_current
< (size_t)GetItemCount() - 1 )
3320 OnArrowChar( m_current
+ 1, event
);
3325 OnArrowChar( GetItemCount() - 1, event
);
3330 OnArrowChar( 0, event
);
3336 if ( HasFlag(wxLC_REPORT
) )
3338 steps
= m_linesPerPage
- 1;
3342 steps
= m_current
% m_linesPerPage
;
3345 int index
= m_current
- steps
;
3349 OnArrowChar( index
, event
);
3356 if ( HasFlag(wxLC_REPORT
) )
3358 steps
= m_linesPerPage
- 1;
3362 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3365 size_t index
= m_current
+ steps
;
3366 size_t count
= GetItemCount();
3367 if ( index
>= count
)
3370 OnArrowChar( index
, event
);
3375 if ( !HasFlag(wxLC_REPORT
) )
3377 int index
= m_current
- m_linesPerPage
;
3381 OnArrowChar( index
, event
);
3386 if ( !HasFlag(wxLC_REPORT
) )
3388 size_t index
= m_current
+ m_linesPerPage
;
3390 size_t count
= GetItemCount();
3391 if ( index
>= count
)
3394 OnArrowChar( index
, event
);
3399 if ( IsSingleSel() )
3401 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3403 if ( IsHighlighted(m_current
) )
3405 // don't unselect the item in single selection mode
3408 //else: select it in ReverseHighlight() below if unselected
3411 ReverseHighlight(m_current
);
3416 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3424 // ----------------------------------------------------------------------------
3426 // ----------------------------------------------------------------------------
3428 void wxListMainWindow::SetFocus()
3430 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3431 // overrides SetFocus in such way that it does never change focus from
3432 // panel's child to the panel itself. Unfortunately, we must be able to change
3433 // focus to the panel from wxListTextCtrl because the text control should
3434 // disappear when the user clicks outside it.
3436 wxWindow
*oldFocus
= FindFocus();
3438 if ( oldFocus
&& oldFocus
->GetParent() == this )
3440 wxWindow::SetFocus();
3444 wxScrolledWindow::SetFocus();
3448 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3450 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3451 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3452 // which are already drawn correctly resulting in horrible flicker - avoid
3464 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3465 event
.SetEventObject( GetParent() );
3466 GetParent()->GetEventHandler()->ProcessEvent( event
);
3469 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3476 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3478 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3480 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3482 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3484 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3486 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3488 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3490 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3492 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3496 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3498 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3500 m_normal_image_list
->GetSize( index
, width
, height
);
3502 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3504 m_small_image_list
->GetSize( index
, width
, height
);
3506 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3508 m_small_image_list
->GetSize( index
, width
, height
);
3510 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3512 m_small_image_list
->GetSize( index
, width
, height
);
3521 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3523 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3524 dc
.SetFont( GetFont() );
3527 dc
.GetTextExtent( s
, &lw
, NULL
);
3529 return lw
+ AUTOSIZE_COL_MARGIN
;
3532 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3536 // calc the spacing from the icon size
3539 if ((imageList
) && (imageList
->GetImageCount()) )
3541 imageList
->GetSize(0, width
, height
);
3544 if (which
== wxIMAGE_LIST_NORMAL
)
3546 m_normal_image_list
= imageList
;
3547 m_normal_spacing
= width
+ 8;
3550 if (which
== wxIMAGE_LIST_SMALL
)
3552 m_small_image_list
= imageList
;
3553 m_small_spacing
= width
+ 14;
3557 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3562 m_small_spacing
= spacing
;
3566 m_normal_spacing
= spacing
;
3570 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3572 return isSmall
? m_small_spacing
: m_normal_spacing
;
3575 // ----------------------------------------------------------------------------
3577 // ----------------------------------------------------------------------------
3579 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3581 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3583 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3585 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3586 item
.m_width
= GetTextLength( item
.m_text
);
3588 wxListHeaderData
*column
= node
->GetData();
3589 column
->SetItem( item
);
3591 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3593 headerWin
->m_dirty
= TRUE
;
3597 // invalidate it as it has to be recalculated
3601 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3603 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3604 _T("invalid column index") );
3606 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3607 _T("SetColumnWidth() can only be called in report mode.") );
3610 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3612 headerWin
->m_dirty
= TRUE
;
3614 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3615 wxCHECK_RET( node
, _T("no column?") );
3617 wxListHeaderData
*column
= node
->GetData();
3619 size_t count
= GetItemCount();
3621 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3623 width
= GetTextLength(column
->GetText());
3625 else if ( width
== wxLIST_AUTOSIZE
)
3629 // TODO: determine the max width somehow...
3630 width
= WIDTH_COL_DEFAULT
;
3634 wxClientDC
dc(this);
3635 dc
.SetFont( GetFont() );
3637 int max
= AUTOSIZE_COL_MARGIN
;
3639 for ( size_t i
= 0; i
< count
; i
++ )
3641 wxListLineData
*line
= GetLine(i
);
3642 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3644 wxCHECK_RET( n
, _T("no subitem?") );
3646 wxListItemData
*item
= n
->GetData();
3649 if (item
->HasImage())
3652 GetImageSize( item
->GetImage(), ix
, iy
);
3656 if (item
->HasText())
3659 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3667 width
= max
+ AUTOSIZE_COL_MARGIN
;
3671 column
->SetWidth( width
);
3673 // invalidate it as it has to be recalculated
3677 int wxListMainWindow::GetHeaderWidth() const
3679 if ( !m_headerWidth
)
3681 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3683 size_t count
= GetColumnCount();
3684 for ( size_t col
= 0; col
< count
; col
++ )
3686 self
->m_headerWidth
+= GetColumnWidth(col
);
3690 return m_headerWidth
;
3693 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3695 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3696 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3698 wxListHeaderData
*column
= node
->GetData();
3699 column
->GetItem( item
);
3702 int wxListMainWindow::GetColumnWidth( int col
) const
3704 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3705 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3707 wxListHeaderData
*column
= node
->GetData();
3708 return column
->GetWidth();
3711 // ----------------------------------------------------------------------------
3713 // ----------------------------------------------------------------------------
3715 void wxListMainWindow::SetItem( wxListItem
&item
)
3717 long id
= item
.m_itemId
;
3718 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3719 _T("invalid item index in SetItem") );
3723 wxListLineData
*line
= GetLine((size_t)id
);
3724 line
->SetItem( item
.m_col
, item
);
3727 if ( InReportView() )
3729 // just refresh the line to show the new value of the text/image
3730 RefreshLine((size_t)id
);
3734 // refresh everything (resulting in horrible flicker - FIXME!)
3739 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3741 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3742 _T("invalid list ctrl item index in SetItem") );
3744 size_t oldCurrent
= m_current
;
3745 size_t item
= (size_t)litem
; // safe because of the check above
3747 // do we need to change the focus?
3748 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3750 if ( state
& wxLIST_STATE_FOCUSED
)
3752 // don't do anything if this item is already focused
3753 if ( item
!= m_current
)
3755 ChangeCurrent(item
);
3757 if ( oldCurrent
!= (size_t)-1 )
3759 if ( IsSingleSel() )
3761 HighlightLine(oldCurrent
, FALSE
);
3764 RefreshLine(oldCurrent
);
3767 RefreshLine( m_current
);
3772 // don't do anything if this item is not focused
3773 if ( item
== m_current
)
3777 if ( IsSingleSel() )
3779 // we must unselect the old current item as well or we
3780 // might end up with more than one selected item in a
3781 // single selection control
3782 HighlightLine(oldCurrent
, FALSE
);
3785 RefreshLine( oldCurrent
);
3790 // do we need to change the selection state?
3791 if ( stateMask
& wxLIST_STATE_SELECTED
)
3793 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3795 if ( IsSingleSel() )
3799 // selecting the item also makes it the focused one in the
3801 if ( m_current
!= item
)
3803 ChangeCurrent(item
);
3805 if ( oldCurrent
!= (size_t)-1 )
3807 HighlightLine( oldCurrent
, FALSE
);
3808 RefreshLine( oldCurrent
);
3814 // only the current item may be selected anyhow
3815 if ( item
!= m_current
)
3820 if ( HighlightLine(item
, on
) )
3827 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3829 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3830 _T("invalid list ctrl item index in GetItemState()") );
3832 int ret
= wxLIST_STATE_DONTCARE
;
3834 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3836 if ( (size_t)item
== m_current
)
3837 ret
|= wxLIST_STATE_FOCUSED
;
3840 if ( stateMask
& wxLIST_STATE_SELECTED
)
3842 if ( IsHighlighted(item
) )
3843 ret
|= wxLIST_STATE_SELECTED
;
3849 void wxListMainWindow::GetItem( wxListItem
&item
) const
3851 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3852 _T("invalid item index in GetItem") );
3854 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3855 line
->GetItem( item
.m_col
, item
);
3858 // ----------------------------------------------------------------------------
3860 // ----------------------------------------------------------------------------
3862 size_t wxListMainWindow::GetItemCount() const
3864 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3867 void wxListMainWindow::SetItemCount(long count
)
3869 m_selStore
.SetItemCount(count
);
3870 m_countVirt
= count
;
3872 ResetVisibleLinesRange();
3874 // scrollbars must be reset
3878 int wxListMainWindow::GetSelectedItemCount() const
3880 // deal with the quick case first
3881 if ( IsSingleSel() )
3883 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3886 // virtual controls remmebers all its selections itself
3888 return m_selStore
.GetSelectedCount();
3890 // TODO: we probably should maintain the number of items selected even for
3891 // non virtual controls as enumerating all lines is really slow...
3892 size_t countSel
= 0;
3893 size_t count
= GetItemCount();
3894 for ( size_t line
= 0; line
< count
; line
++ )
3896 if ( GetLine(line
)->IsHighlighted() )
3903 // ----------------------------------------------------------------------------
3904 // item position/size
3905 // ----------------------------------------------------------------------------
3907 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3909 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3910 _T("invalid index in GetItemRect") );
3912 rect
= GetLineRect((size_t)index
);
3914 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3917 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3920 GetItemRect(item
, rect
);
3928 // ----------------------------------------------------------------------------
3929 // geometry calculation
3930 // ----------------------------------------------------------------------------
3932 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3934 wxClientDC
dc( this );
3935 dc
.SetFont( GetFont() );
3938 if ( HasFlag(wxLC_ICON
) )
3939 iconSpacing
= m_normal_spacing
;
3940 else if ( HasFlag(wxLC_SMALL_ICON
) )
3941 iconSpacing
= m_small_spacing
;
3945 // Note that we do not call GetClientSize() here but
3946 // GetSize() and substract the border size for sunken
3947 // borders manually. This is technically incorrect,
3948 // but we need to know the client area's size WITHOUT
3949 // scrollbars here. Since we don't know if there are
3950 // any scrollbars, we use GetSize() instead. Another
3951 // solution would be to call SetScrollbars() here to
3952 // remove the scrollbars and call GetClientSize() then,
3953 // but this might result in flicker and - worse - will
3954 // reset the scrollbars to 0 which is not good at all
3955 // if you resize a dialog/window, but don't want to
3956 // reset the window scrolling. RR.
3957 // Furthermore, we actually do NOT subtract the border
3958 // width as 2 pixels is just the extra space which we
3959 // need around the actual content in the window. Other-
3960 // wise the text would e.g. touch the upper border. RR.
3963 GetSize( &clientWidth
, &clientHeight
);
3965 if ( HasFlag(wxLC_REPORT
) )
3967 // all lines have the same height
3968 int lineHeight
= GetLineHeight();
3970 // scroll one line per step
3971 m_yScroll
= lineHeight
;
3973 size_t lineCount
= GetItemCount();
3974 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3976 m_linesPerPage
= clientHeight
/ lineHeight
;
3978 ResetVisibleLinesRange();
3980 SetScrollbars( m_xScroll
, m_yScroll
,
3981 GetHeaderWidth() / m_xScroll
,
3982 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3983 GetScrollPos(wxHORIZONTAL
),
3984 GetScrollPos(wxVERTICAL
),
3989 // at first we try without any scrollbar. if the items don't
3990 // fit into the window, we recalculate after subtracting an
3991 // approximated 15 pt for the horizontal scrollbar
3993 int entireWidth
= 0;
3995 for (int tries
= 0; tries
< 2; tries
++)
3997 // We start with 4 for the border around all items
4002 // Now we have decided that the items do not fit into the
4003 // client area. Unfortunately, wxWindows sometimes thinks
4004 // that it does fit and therefore NO horizontal scrollbar
4005 // is inserted. This looks ugly, so we fudge here and make
4006 // the calculated width bigger than was actually has been
4007 // calculated. This ensures that wxScrolledWindows puts
4008 // a scrollbar at the bottom of its client area.
4009 entireWidth
+= SCROLL_UNIT_X
;
4012 // Start at 2,2 so the text does not touch the border
4017 int currentlyVisibleLines
= 0;
4019 size_t count
= GetItemCount();
4020 for (size_t i
= 0; i
< count
; i
++)
4022 currentlyVisibleLines
++;
4023 wxListLineData
*line
= GetLine(i
);
4024 line
->CalculateSize( &dc
, iconSpacing
);
4025 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4027 wxSize sizeLine
= GetLineSize(i
);
4029 if ( maxWidth
< sizeLine
.x
)
4030 maxWidth
= sizeLine
.x
;
4033 if (currentlyVisibleLines
> m_linesPerPage
)
4034 m_linesPerPage
= currentlyVisibleLines
;
4036 // Assume that the size of the next one is the same... (FIXME)
4037 if ( y
+ sizeLine
.y
>= clientHeight
)
4039 currentlyVisibleLines
= 0;
4042 entireWidth
+= maxWidth
+6;
4046 // We have reached the last item.
4047 if ( i
== count
- 1 )
4048 entireWidth
+= maxWidth
;
4050 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4052 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4054 currentlyVisibleLines
= 0;
4058 if ( i
== count
- 1 )
4059 tries
= 1; // Everything fits, no second try required.
4063 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4064 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4069 // FIXME: why should we call it from here?
4076 void wxListMainWindow::RefreshAll()
4081 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4082 if ( headerWin
&& headerWin
->m_dirty
)
4084 headerWin
->m_dirty
= FALSE
;
4085 headerWin
->Refresh();
4089 void wxListMainWindow::UpdateCurrent()
4091 if ( !HasCurrent() && !IsEmpty() )
4097 long wxListMainWindow::GetNextItem( long item
,
4098 int WXUNUSED(geometry
),
4102 max
= GetItemCount();
4103 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4104 _T("invalid listctrl index in GetNextItem()") );
4106 // notice that we start with the next item (or the first one if item == -1)
4107 // and this is intentional to allow writing a simple loop to iterate over
4108 // all selected items
4112 // this is not an error because the index was ok initially, just no
4123 size_t count
= GetItemCount();
4124 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4126 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4129 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4136 // ----------------------------------------------------------------------------
4138 // ----------------------------------------------------------------------------
4140 void wxListMainWindow::DeleteItem( long lindex
)
4142 size_t count
= GetItemCount();
4144 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4145 _T("invalid item index in DeleteItem") );
4147 size_t index
= (size_t)lindex
;
4149 // we don't need to adjust the index for the previous items
4150 if ( HasCurrent() && m_current
>= index
)
4152 // if the current item is being deleted, we want the next one to
4153 // become selected - unless there is no next one - so don't adjust
4154 // m_current in this case
4155 if ( m_current
!= index
|| m_current
== count
- 1 )
4161 if ( InReportView() )
4163 ResetVisibleLinesRange();
4170 m_selStore
.OnItemDelete(index
);
4174 m_lines
.RemoveAt( index
);
4177 // we need to refresh the (vert) scrollbar as the number of items changed
4180 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4182 RefreshAfter(index
);
4185 void wxListMainWindow::DeleteColumn( int col
)
4187 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4189 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4192 m_columns
.DeleteNode( node
);
4194 // invalidate it as it has to be recalculated
4198 void wxListMainWindow::DoDeleteAllItems()
4202 // nothing to do - in particular, don't send the event
4208 // to make the deletion of all items faster, we don't send the
4209 // notifications for each item deletion in this case but only one event
4210 // for all of them: this is compatible with wxMSW and documented in
4211 // DeleteAllItems() description
4213 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4214 event
.SetEventObject( GetParent() );
4215 GetParent()->GetEventHandler()->ProcessEvent( event
);
4224 if ( InReportView() )
4226 ResetVisibleLinesRange();
4232 void wxListMainWindow::DeleteAllItems()
4236 RecalculatePositions();
4239 void wxListMainWindow::DeleteEverything()
4246 // ----------------------------------------------------------------------------
4247 // scanning for an item
4248 // ----------------------------------------------------------------------------
4250 void wxListMainWindow::EnsureVisible( long index
)
4252 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4253 _T("invalid index in EnsureVisible") );
4255 // We have to call this here because the label in question might just have
4256 // been added and its position is not known yet
4259 RecalculatePositions(TRUE
/* no refresh */);
4262 MoveToItem((size_t)index
);
4265 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4272 size_t count
= GetItemCount();
4273 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4275 wxListLineData
*line
= GetLine(i
);
4276 if ( line
->GetText(0) == tmp
)
4283 long wxListMainWindow::FindItem(long start
, long data
)
4289 size_t count
= GetItemCount();
4290 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4292 wxListLineData
*line
= GetLine(i
);
4294 line
->GetItem( 0, item
);
4295 if (item
.m_data
== data
)
4302 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4304 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4306 size_t count
= GetItemCount();
4308 if ( HasFlag(wxLC_REPORT
) )
4310 size_t current
= y
/ GetLineHeight();
4311 if ( current
< count
)
4313 flags
= HitTestLine(current
, x
, y
);
4320 // TODO: optimize it too! this is less simple than for report view but
4321 // enumerating all items is still not a way to do it!!
4322 for ( size_t current
= 0; current
< count
; current
++ )
4324 flags
= HitTestLine(current
, x
, y
);
4333 // ----------------------------------------------------------------------------
4335 // ----------------------------------------------------------------------------
4337 void wxListMainWindow::InsertItem( wxListItem
&item
)
4339 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4341 size_t count
= GetItemCount();
4342 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4343 _T("invalid item index") );
4345 size_t id
= item
.m_itemId
;
4350 if ( HasFlag(wxLC_REPORT
) )
4352 else if ( HasFlag(wxLC_LIST
) )
4354 else if ( HasFlag(wxLC_ICON
) )
4356 else if ( HasFlag(wxLC_SMALL_ICON
) )
4357 mode
= wxLC_ICON
; // no typo
4360 wxFAIL_MSG( _T("unknown mode") );
4363 wxListLineData
*line
= new wxListLineData(this);
4365 line
->SetItem( 0, item
);
4367 m_lines
.Insert( line
, id
);
4370 RefreshLines(id
, GetItemCount() - 1);
4373 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4376 if ( HasFlag(wxLC_REPORT
) )
4378 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4379 item
.m_width
= GetTextLength( item
.m_text
);
4380 wxListHeaderData
*column
= new wxListHeaderData( item
);
4381 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4383 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4384 m_columns
.Insert( node
, column
);
4388 m_columns
.Append( column
);
4391 // invalidate it as it has to be recalculated
4396 // ----------------------------------------------------------------------------
4398 // ----------------------------------------------------------------------------
4400 wxListCtrlCompare list_ctrl_compare_func_2
;
4401 long list_ctrl_compare_data
;
4403 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4405 wxListLineData
*line1
= *arg1
;
4406 wxListLineData
*line2
= *arg2
;
4408 line1
->GetItem( 0, item
);
4409 long data1
= item
.m_data
;
4410 line2
->GetItem( 0, item
);
4411 long data2
= item
.m_data
;
4412 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4415 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4417 list_ctrl_compare_func_2
= fn
;
4418 list_ctrl_compare_data
= data
;
4419 m_lines
.Sort( list_ctrl_compare_func_1
);
4423 // ----------------------------------------------------------------------------
4425 // ----------------------------------------------------------------------------
4427 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4429 // update our idea of which lines are shown when we redraw the window the
4431 ResetVisibleLinesRange();
4434 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4435 wxScrolledWindow::OnScroll(event
);
4437 HandleOnScroll( event
);
4440 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4442 wxListCtrl
* lc
= GetListCtrl();
4443 wxCHECK_RET( lc
, _T("no listctrl window?") );
4445 lc
->m_headerWin
->Refresh();
4446 lc
->m_headerWin
->Update();
4450 int wxListMainWindow::GetCountPerPage() const
4452 if ( !m_linesPerPage
)
4454 wxConstCast(this, wxListMainWindow
)->
4455 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4458 return m_linesPerPage
;
4461 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4463 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4465 if ( m_lineFrom
== (size_t)-1 )
4467 size_t count
= GetItemCount();
4470 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4472 // this may happen if SetScrollbars() hadn't been called yet
4473 if ( m_lineFrom
>= count
)
4474 m_lineFrom
= count
- 1;
4476 // we redraw one extra line but this is needed to make the redrawing
4477 // logic work when there is a fractional number of lines on screen
4478 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4479 if ( m_lineTo
>= count
)
4480 m_lineTo
= count
- 1;
4482 else // empty control
4485 m_lineTo
= (size_t)-1;
4489 wxASSERT_MSG( IsEmpty() ||
4490 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4491 _T("GetVisibleLinesRange() returns incorrect result") );
4499 // -------------------------------------------------------------------------------------
4501 // -------------------------------------------------------------------------------------
4503 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4505 // -------------------------------------------------------------------------------------
4507 // -------------------------------------------------------------------------------------
4509 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4510 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4512 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4514 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4515 EVT_SIZE(wxListCtrl::OnSize
)
4516 EVT_IDLE(wxListCtrl::OnIdle
)
4519 wxListCtrl::wxListCtrl()
4521 m_imageListNormal
= (wxImageList
*) NULL
;
4522 m_imageListSmall
= (wxImageList
*) NULL
;
4523 m_imageListState
= (wxImageList
*) NULL
;
4525 m_ownsImageListNormal
=
4526 m_ownsImageListSmall
=
4527 m_ownsImageListState
= FALSE
;
4529 m_mainWin
= (wxListMainWindow
*) NULL
;
4530 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4533 wxListCtrl::~wxListCtrl()
4535 if (m_ownsImageListNormal
)
4536 delete m_imageListNormal
;
4537 if (m_ownsImageListSmall
)
4538 delete m_imageListSmall
;
4539 if (m_ownsImageListState
)
4540 delete m_imageListState
;
4543 void wxListCtrl::CreateHeaderWindow()
4545 m_headerWin
= new wxListHeaderWindow
4547 this, -1, m_mainWin
,
4549 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4554 bool wxListCtrl::Create(wxWindow
*parent
,
4559 const wxValidator
&validator
,
4560 const wxString
&name
)
4564 m_imageListState
= (wxImageList
*) NULL
;
4565 m_ownsImageListNormal
=
4566 m_ownsImageListSmall
=
4567 m_ownsImageListState
= FALSE
;
4569 m_mainWin
= (wxListMainWindow
*) NULL
;
4570 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4572 if ( !(style
& wxLC_MASK_TYPE
) )
4574 style
= style
| wxLC_LIST
;
4577 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4580 // don't create the inner window with the border
4581 style
&= ~wxSUNKEN_BORDER
;
4583 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4585 if ( HasFlag(wxLC_REPORT
) )
4587 CreateHeaderWindow();
4589 if ( HasFlag(wxLC_NO_HEADER
) )
4591 // VZ: why do we create it at all then?
4592 m_headerWin
->Show( FALSE
);
4599 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4601 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4602 _T("wxLC_VIRTUAL can't be [un]set") );
4604 long flag
= GetWindowStyle();
4608 if (style
& wxLC_MASK_TYPE
)
4609 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4610 if (style
& wxLC_MASK_ALIGN
)
4611 flag
&= ~wxLC_MASK_ALIGN
;
4612 if (style
& wxLC_MASK_SORT
)
4613 flag
&= ~wxLC_MASK_SORT
;
4625 SetWindowStyleFlag( flag
);
4628 void wxListCtrl::SetWindowStyleFlag( long flag
)
4632 m_mainWin
->DeleteEverything();
4634 // has the header visibility changed?
4635 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4636 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4638 if ( hasHeader
!= willHaveHeader
)
4645 // don't delete, just hide, as we can reuse it later
4646 m_headerWin
->Show(FALSE
);
4648 //else: nothing to do
4650 else // must show header
4654 CreateHeaderWindow();
4656 else // already have it, just show
4658 m_headerWin
->Show( TRUE
);
4662 ResizeReportView(willHaveHeader
);
4666 wxWindow::SetWindowStyleFlag( flag
);
4669 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4671 m_mainWin
->GetColumn( col
, item
);
4675 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4677 m_mainWin
->SetColumn( col
, item
);
4681 int wxListCtrl::GetColumnWidth( int col
) const
4683 return m_mainWin
->GetColumnWidth( col
);
4686 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4688 m_mainWin
->SetColumnWidth( col
, width
);
4692 int wxListCtrl::GetCountPerPage() const
4694 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4697 bool wxListCtrl::GetItem( wxListItem
&info
) const
4699 m_mainWin
->GetItem( info
);
4703 bool wxListCtrl::SetItem( wxListItem
&info
)
4705 m_mainWin
->SetItem( info
);
4709 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4712 info
.m_text
= label
;
4713 info
.m_mask
= wxLIST_MASK_TEXT
;
4714 info
.m_itemId
= index
;
4718 info
.m_image
= imageId
;
4719 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4721 m_mainWin
->SetItem(info
);
4725 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4727 return m_mainWin
->GetItemState( item
, stateMask
);
4730 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4732 m_mainWin
->SetItemState( item
, state
, stateMask
);
4736 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4739 info
.m_image
= image
;
4740 info
.m_mask
= wxLIST_MASK_IMAGE
;
4741 info
.m_itemId
= item
;
4742 m_mainWin
->SetItem( info
);
4746 wxString
wxListCtrl::GetItemText( long item
) const
4748 return m_mainWin
->GetItemText(item
);
4751 void wxListCtrl::SetItemText( long item
, const wxString
& str
)
4753 m_mainWin
->SetItemText(item
, str
);
4756 long wxListCtrl::GetItemData( long item
) const
4759 info
.m_itemId
= item
;
4760 m_mainWin
->GetItem( info
);
4764 bool wxListCtrl::SetItemData( long item
, long data
)
4767 info
.m_mask
= wxLIST_MASK_DATA
;
4768 info
.m_itemId
= item
;
4770 m_mainWin
->SetItem( info
);
4774 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4776 m_mainWin
->GetItemRect( item
, rect
);
4780 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4782 m_mainWin
->GetItemPosition( item
, pos
);
4786 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4791 int wxListCtrl::GetItemCount() const
4793 return m_mainWin
->GetItemCount();
4796 int wxListCtrl::GetColumnCount() const
4798 return m_mainWin
->GetColumnCount();
4801 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4803 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4806 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4808 return m_mainWin
->GetItemSpacing( isSmall
);
4811 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4814 info
.m_itemId
= item
;
4815 info
.SetTextColour( col
);
4816 m_mainWin
->SetItem( info
);
4819 wxColour
wxListCtrl::GetItemTextColour( long item
) const
4822 info
.m_itemId
= item
;
4823 m_mainWin
->GetItem( info
);
4824 return info
.GetTextColour();
4827 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4830 info
.m_itemId
= item
;
4831 info
.SetBackgroundColour( col
);
4832 m_mainWin
->SetItem( info
);
4835 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
4838 info
.m_itemId
= item
;
4839 m_mainWin
->GetItem( info
);
4840 return info
.GetBackgroundColour();
4843 int wxListCtrl::GetSelectedItemCount() const
4845 return m_mainWin
->GetSelectedItemCount();
4848 wxColour
wxListCtrl::GetTextColour() const
4850 return GetForegroundColour();
4853 void wxListCtrl::SetTextColour(const wxColour
& col
)
4855 SetForegroundColour(col
);
4858 long wxListCtrl::GetTopItem() const
4863 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4865 return m_mainWin
->GetNextItem( item
, geom
, state
);
4868 wxImageList
*wxListCtrl::GetImageList(int which
) const
4870 if (which
== wxIMAGE_LIST_NORMAL
)
4872 return m_imageListNormal
;
4874 else if (which
== wxIMAGE_LIST_SMALL
)
4876 return m_imageListSmall
;
4878 else if (which
== wxIMAGE_LIST_STATE
)
4880 return m_imageListState
;
4882 return (wxImageList
*) NULL
;
4885 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4887 if ( which
== wxIMAGE_LIST_NORMAL
)
4889 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4890 m_imageListNormal
= imageList
;
4891 m_ownsImageListNormal
= FALSE
;
4893 else if ( which
== wxIMAGE_LIST_SMALL
)
4895 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4896 m_imageListSmall
= imageList
;
4897 m_ownsImageListSmall
= FALSE
;
4899 else if ( which
== wxIMAGE_LIST_STATE
)
4901 if (m_ownsImageListState
) delete m_imageListState
;
4902 m_imageListState
= imageList
;
4903 m_ownsImageListState
= FALSE
;
4906 m_mainWin
->SetImageList( imageList
, which
);
4909 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4911 SetImageList(imageList
, which
);
4912 if ( which
== wxIMAGE_LIST_NORMAL
)
4913 m_ownsImageListNormal
= TRUE
;
4914 else if ( which
== wxIMAGE_LIST_SMALL
)
4915 m_ownsImageListSmall
= TRUE
;
4916 else if ( which
== wxIMAGE_LIST_STATE
)
4917 m_ownsImageListState
= TRUE
;
4920 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4925 bool wxListCtrl::DeleteItem( long item
)
4927 m_mainWin
->DeleteItem( item
);
4931 bool wxListCtrl::DeleteAllItems()
4933 m_mainWin
->DeleteAllItems();
4937 bool wxListCtrl::DeleteAllColumns()
4939 size_t count
= m_mainWin
->m_columns
.GetCount();
4940 for ( size_t n
= 0; n
< count
; n
++ )
4946 void wxListCtrl::ClearAll()
4948 m_mainWin
->DeleteEverything();
4951 bool wxListCtrl::DeleteColumn( int col
)
4953 m_mainWin
->DeleteColumn( col
);
4955 // if we don't have the header any longer, we need to relayout the window
4956 if ( !GetColumnCount() )
4958 ResizeReportView(FALSE
/* no header */);
4964 void wxListCtrl::Edit( long item
)
4966 m_mainWin
->EditLabel( item
);
4969 bool wxListCtrl::EnsureVisible( long item
)
4971 m_mainWin
->EnsureVisible( item
);
4975 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4977 return m_mainWin
->FindItem( start
, str
, partial
);
4980 long wxListCtrl::FindItem( long start
, long data
)
4982 return m_mainWin
->FindItem( start
, data
);
4985 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4986 int WXUNUSED(direction
))
4991 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4993 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4996 long wxListCtrl::InsertItem( wxListItem
& info
)
4998 m_mainWin
->InsertItem( info
);
4999 return info
.m_itemId
;
5002 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
5005 info
.m_text
= label
;
5006 info
.m_mask
= wxLIST_MASK_TEXT
;
5007 info
.m_itemId
= index
;
5008 return InsertItem( info
);
5011 long wxListCtrl::InsertItem( long index
, int imageIndex
)
5014 info
.m_mask
= wxLIST_MASK_IMAGE
;
5015 info
.m_image
= imageIndex
;
5016 info
.m_itemId
= index
;
5017 return InsertItem( info
);
5020 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5023 info
.m_text
= label
;
5024 info
.m_image
= imageIndex
;
5025 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5026 info
.m_itemId
= index
;
5027 return InsertItem( info
);
5030 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
5032 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5034 m_mainWin
->InsertColumn( col
, item
);
5036 // if we hadn't had header before and have it now we need to relayout the
5038 if ( GetColumnCount() == 1 )
5040 ResizeReportView(TRUE
/* have header */);
5043 m_headerWin
->Refresh();
5048 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
5049 int format
, int width
)
5052 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5053 item
.m_text
= heading
;
5056 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5057 item
.m_width
= width
;
5059 item
.m_format
= format
;
5061 return InsertColumn( col
, item
);
5064 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5070 // fn is a function which takes 3 long arguments: item1, item2, data.
5071 // item1 is the long data associated with a first item (NOT the index).
5072 // item2 is the long data associated with a second item (NOT the index).
5073 // data is the same value as passed to SortItems.
5074 // The return value is a negative number if the first item should precede the second
5075 // item, a positive number of the second item should precede the first,
5076 // or zero if the two items are equivalent.
5077 // data is arbitrary data to be passed to the sort function.
5079 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5081 m_mainWin
->SortItems( fn
, data
);
5085 // ----------------------------------------------------------------------------
5087 // ----------------------------------------------------------------------------
5089 void wxListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5094 ResizeReportView(m_mainWin
->HasHeader());
5096 m_mainWin
->RecalculatePositions();
5099 void wxListCtrl::ResizeReportView(bool showHeader
)
5102 GetClientSize( &cw
, &ch
);
5106 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5107 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5109 else // no header window
5111 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5115 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5119 // do it only if needed
5120 if ( !m_mainWin
->m_dirty
)
5123 m_mainWin
->RecalculatePositions();
5126 // ----------------------------------------------------------------------------
5128 // ----------------------------------------------------------------------------
5130 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5134 m_mainWin
->SetBackgroundColour( colour
);
5135 m_mainWin
->m_dirty
= TRUE
;
5141 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5143 if ( !wxWindow::SetForegroundColour( colour
) )
5148 m_mainWin
->SetForegroundColour( colour
);
5149 m_mainWin
->m_dirty
= TRUE
;
5154 m_headerWin
->SetForegroundColour( colour
);
5160 bool wxListCtrl::SetFont( const wxFont
&font
)
5162 if ( !wxWindow::SetFont( font
) )
5167 m_mainWin
->SetFont( font
);
5168 m_mainWin
->m_dirty
= TRUE
;
5173 m_headerWin
->SetFont( font
);
5179 // ----------------------------------------------------------------------------
5180 // methods forwarded to m_mainWin
5181 // ----------------------------------------------------------------------------
5183 #if wxUSE_DRAG_AND_DROP
5185 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5187 m_mainWin
->SetDropTarget( dropTarget
);
5190 wxDropTarget
*wxListCtrl::GetDropTarget() const
5192 return m_mainWin
->GetDropTarget();
5195 #endif // wxUSE_DRAG_AND_DROP
5197 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5199 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5202 wxColour
wxListCtrl::GetBackgroundColour() const
5204 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5207 wxColour
wxListCtrl::GetForegroundColour() const
5209 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5212 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5215 return m_mainWin
->PopupMenu( menu
, x
, y
);
5218 #endif // wxUSE_MENUS
5221 void wxListCtrl::SetFocus()
5223 /* The test in window.cpp fails as we are a composite
5224 window, so it checks against "this", but not m_mainWin. */
5225 if ( FindFocus() != this )
5226 m_mainWin
->SetFocus();
5229 // ----------------------------------------------------------------------------
5230 // virtual list control support
5231 // ----------------------------------------------------------------------------
5233 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5235 // this is a pure virtual function, in fact - which is not really pure
5236 // because the controls which are not virtual don't need to implement it
5237 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
5239 return wxEmptyString
;
5242 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5245 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
5250 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5252 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5253 _T("invalid item index in OnGetItemAttr()") );
5255 // no attributes by default
5259 void wxListCtrl::SetItemCount(long count
)
5261 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5263 m_mainWin
->SetItemCount(count
);
5266 void wxListCtrl::RefreshItem(long item
)
5268 m_mainWin
->RefreshLine(item
);
5271 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5273 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5276 void wxListCtrl::Freeze()
5278 m_mainWin
->Freeze();
5281 void wxListCtrl::Thaw()
5286 #endif // wxUSE_LISTCTRL