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
505 wxListMainWindow
*m_owner
;
506 wxString m_startValue
;
511 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
512 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
513 const wxString
&value
= "",
514 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
516 const wxValidator
& validator
= wxDefaultValidator
,
517 const wxString
&name
= "listctrltextctrl" );
518 void OnChar( wxKeyEvent
&event
);
519 void OnKeyUp( wxKeyEvent
&event
);
520 void OnKillFocus( wxFocusEvent
&event
);
523 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
)
524 DECLARE_EVENT_TABLE()
527 //-----------------------------------------------------------------------------
528 // wxListMainWindow (internal)
529 //-----------------------------------------------------------------------------
531 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
532 #include "wx/listimpl.cpp"
533 WX_DEFINE_LIST(wxListHeaderDataList
);
535 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
539 wxListMainWindow( wxWindow
*parent
,
541 const wxPoint
& pos
= wxDefaultPosition
,
542 const wxSize
& size
= wxDefaultSize
,
544 const wxString
&name
= _T("listctrlmainwindow") );
546 virtual ~wxListMainWindow();
548 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
550 // return true if this is a virtual list control
551 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
553 // return true if the control is in report mode
554 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
556 // return true if we are in single selection mode, false if multi sel
557 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
559 // do we have a header window?
560 bool HasHeader() const
561 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
563 void HighlightAll( bool on
);
565 // all these functions only do something if the line is currently visible
567 // change the line "selected" state, return TRUE if it really changed
568 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
570 // as HighlightLine() but do it for the range of lines: this is incredibly
571 // more efficient for virtual list controls!
573 // NB: unlike HighlightLine() this one does refresh the lines on screen
574 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
576 // toggle the line state and refresh it
577 void ReverseHighlight( size_t line
)
578 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
580 // return true if the line is highlighted
581 bool IsHighlighted(size_t line
) const;
583 // refresh one or several lines at once
584 void RefreshLine( size_t line
);
585 void RefreshLines( size_t lineFrom
, size_t lineTo
);
587 // refresh all selected items
588 void RefreshSelected();
590 // refresh all lines below the given one: the difference with
591 // RefreshLines() is that the index here might not be a valid one (happens
592 // when the last line is deleted)
593 void RefreshAfter( size_t lineFrom
);
595 // the methods which are forwarded to wxListLineData itself in list/icon
596 // modes but are here because the lines don't store their positions in the
599 // get the bound rect for the entire line
600 wxRect
GetLineRect(size_t line
) const;
602 // get the bound rect of the label
603 wxRect
GetLineLabelRect(size_t line
) const;
605 // get the bound rect of the items icon (only may be called if we do have
607 wxRect
GetLineIconRect(size_t line
) const;
609 // get the rect to be highlighted when the item has focus
610 wxRect
GetLineHighlightRect(size_t line
) const;
612 // get the size of the total line rect
613 wxSize
GetLineSize(size_t line
) const
614 { return GetLineRect(line
).GetSize(); }
616 // return the hit code for the corresponding position (in this line)
617 long HitTestLine(size_t line
, int x
, int y
) const;
619 // bring the selected item into view, scrolling to it if necessary
620 void MoveToItem(size_t item
);
622 // bring the current item into view
623 void MoveToFocus() { MoveToItem(m_current
); }
625 // start editing the label of the given item
626 void EditLabel( long item
);
628 // suspend/resume redrawing the control
634 void OnRenameTimer();
635 void OnRenameAccept();
637 void OnMouse( wxMouseEvent
&event
);
639 // called to switch the selection from the current item to newCurrent,
640 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
642 void OnChar( wxKeyEvent
&event
);
643 void OnKeyDown( wxKeyEvent
&event
);
644 void OnSetFocus( wxFocusEvent
&event
);
645 void OnKillFocus( wxFocusEvent
&event
);
646 void OnScroll(wxScrollWinEvent
& event
) ;
648 void OnPaint( wxPaintEvent
&event
);
650 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
651 void GetImageSize( int index
, int &width
, int &height
) const;
652 int GetTextLength( const wxString
&s
) const;
654 void SetImageList( wxImageList
*imageList
, int which
);
655 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
656 int GetItemSpacing( bool isSmall
= FALSE
);
658 void SetColumn( int col
, wxListItem
&item
);
659 void SetColumnWidth( int col
, int width
);
660 void GetColumn( int col
, wxListItem
&item
) const;
661 int GetColumnWidth( int col
) const;
662 int GetColumnCount() const { return m_columns
.GetCount(); }
664 // returns the sum of the heights of all columns
665 int GetHeaderWidth() const;
667 int GetCountPerPage() const;
669 void SetItem( wxListItem
&item
);
670 void GetItem( wxListItem
&item
);
671 void SetItemState( long item
, long state
, long stateMask
);
672 int GetItemState( long item
, long stateMask
);
673 void GetItemRect( long index
, wxRect
&rect
);
674 bool GetItemPosition( long item
, wxPoint
& pos
);
675 int GetSelectedItemCount();
677 // set the scrollbars and update the positions of the items
678 void RecalculatePositions(bool noRefresh
= FALSE
);
680 // refresh the window and the header
683 long GetNextItem( long item
, int geometry
, int state
);
684 void DeleteItem( long index
);
685 void DeleteAllItems();
686 void DeleteColumn( int col
);
687 void DeleteEverything();
688 void EnsureVisible( long index
);
689 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
690 long FindItem( long start
, long data
);
691 long HitTest( int x
, int y
, int &flags
);
692 void InsertItem( wxListItem
&item
);
693 void InsertColumn( long col
, wxListItem
&item
);
694 void SortItems( wxListCtrlCompare fn
, long data
);
696 size_t GetItemCount() const;
697 bool IsEmpty() const { return GetItemCount() == 0; }
698 void SetItemCount(long count
);
700 // change the current (== focused) item, send a notification event
701 void ChangeCurrent(size_t current
);
702 void ResetCurrent() { ChangeCurrent((size_t)-1); }
703 bool HasCurrent() const { return m_current
!= (size_t)-1; }
705 // send out a wxListEvent
706 void SendNotify( size_t line
,
708 wxPoint point
= wxDefaultPosition
);
710 // override base class virtual to reset m_lineHeight when the font changes
711 virtual bool SetFont(const wxFont
& font
)
713 if ( !wxScrolledWindow::SetFont(font
) )
721 // these are for wxListLineData usage only
723 // get the backpointer to the list ctrl
724 wxListCtrl
*GetListCtrl() const
726 return wxStaticCast(GetParent(), wxListCtrl
);
729 // get the height of all lines (assuming they all do have the same height)
730 wxCoord
GetLineHeight() const;
732 // get the y position of the given line (only for report view)
733 wxCoord
GetLineY(size_t line
) const;
735 // get the brush to use for the item highlighting
736 wxBrush
*GetHighlightBrush() const
738 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
742 // the array of all line objects for a non virtual list control
743 wxListLineDataArray m_lines
;
745 // the list of column objects
746 wxListHeaderDataList m_columns
;
748 // currently focused item or -1
751 // the item currently being edited or -1
752 size_t m_currentEdit
;
754 // the number of lines per page
757 // this flag is set when something which should result in the window
758 // redrawing happens (i.e. an item was added or deleted, or its appearance
759 // changed) and OnPaint() doesn't redraw the window while it is set which
760 // allows to minimize the number of repaintings when a lot of items are
761 // being added. The real repainting occurs only after the next OnIdle()
765 wxColour
*m_highlightColour
;
768 wxImageList
*m_small_image_list
;
769 wxImageList
*m_normal_image_list
;
771 int m_normal_spacing
;
775 wxTimer
*m_renameTimer
;
777 wxString m_renameRes
;
782 // for double click logic
783 size_t m_lineLastClicked
,
784 m_lineBeforeLastClicked
;
787 // the total count of items in a virtual list control
790 // the object maintaining the items selection state, only used in virtual
792 wxSelectionStore m_selStore
;
794 // common part of all ctors
797 // intiialize m_[xy]Scroll
798 void InitScrolling();
800 // get the line data for the given index
801 wxListLineData
*GetLine(size_t n
) const
803 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
807 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
815 // get a dummy line which can be used for geometry calculations and such:
816 // you must use GetLine() if you want to really draw the line
817 wxListLineData
*GetDummyLine() const;
819 // cache the line data of the n-th line in m_lines[0]
820 void CacheLineData(size_t line
);
822 // get the range of visible lines
823 void GetVisibleLinesRange(size_t *from
, size_t *to
);
825 // force us to recalculate the range of visible lines
826 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
828 // get the colour to be used for drawing the rules
829 wxColour
GetRuleColour() const
834 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
839 // initialize the current item if needed
840 void UpdateCurrent();
842 // delete all items but don't refresh: called from dtor
843 void DoDeleteAllItems();
845 // the height of one line using the current font
846 wxCoord m_lineHeight
;
848 // the total header width or 0 if not calculated yet
849 wxCoord m_headerWidth
;
851 // the first and last lines being shown on screen right now (inclusive),
852 // both may be -1 if they must be calculated so never access them directly:
853 // use GetVisibleLinesRange() above instead
857 // the brushes to use for item highlighting when we do/don't have focus
858 wxBrush
*m_highlightBrush
,
859 *m_highlightUnfocusedBrush
;
861 // if this is > 0, the control is frozen and doesn't redraw itself
862 size_t m_freezeCount
;
864 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
865 DECLARE_EVENT_TABLE()
868 // ============================================================================
870 // ============================================================================
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 bool wxSelectionStore::IsSelected(size_t item
) const
878 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
880 // if the default state is to be selected, being in m_itemsSel means that
881 // the item is not selected, so we have to inverse the logic
882 return m_defaultState
? !isSel
: isSel
;
885 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
887 // search for the item ourselves as like this we get the index where to
888 // insert it later if needed, so we do only one search in the array instead
889 // of two (adding item to a sorted array requires a search)
890 size_t index
= m_itemsSel
.IndexForInsert(item
);
891 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
893 if ( select
!= m_defaultState
)
897 m_itemsSel
.AddAt(item
, index
);
902 else // reset to default state
906 m_itemsSel
.RemoveAt(index
);
914 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
916 wxArrayInt
*itemsChanged
)
918 // 100 is hardcoded but it shouldn't matter much: the important thing is
919 // that we don't refresh everything when really few (e.g. 1 or 2) items
921 static const size_t MANY_ITEMS
= 100;
923 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
925 // are we going to have more [un]selected items than the other ones?
926 if ( itemTo
- itemFrom
> m_count
/2 )
928 if ( select
!= m_defaultState
)
930 // the default state now becomes the same as 'select'
931 m_defaultState
= select
;
933 // so all the old selections (which had state select) shouldn't be
934 // selected any more, but all the other ones should
935 wxIndexArray selOld
= m_itemsSel
;
938 // TODO: it should be possible to optimize the searches a bit
939 // knowing the possible range
942 for ( item
= 0; item
< itemFrom
; item
++ )
944 if ( selOld
.Index(item
) == wxNOT_FOUND
)
945 m_itemsSel
.Add(item
);
948 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
950 if ( selOld
.Index(item
) == wxNOT_FOUND
)
951 m_itemsSel
.Add(item
);
954 // many items (> half) changed state
957 else // select == m_defaultState
959 // get the inclusive range of items between itemFrom and itemTo
960 size_t count
= m_itemsSel
.GetCount(),
961 start
= m_itemsSel
.IndexForInsert(itemFrom
),
962 end
= m_itemsSel
.IndexForInsert(itemTo
);
964 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
969 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
976 // delete all of them (from end to avoid changing indices)
977 for ( int i
= end
; i
>= (int)start
; i
-- )
981 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
983 // stop counting (see comment below)
988 itemsChanged
->Add(m_itemsSel
[i
]);
992 m_itemsSel
.RemoveAt(i
);
997 else // "few" items change state
1001 itemsChanged
->Empty();
1004 // just add the items to the selection
1005 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1007 if ( SelectItem(item
, select
) && itemsChanged
)
1009 itemsChanged
->Add(item
);
1011 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1013 // stop counting them, we'll just eat gobs of memory
1014 // for nothing at all - faster to refresh everything in
1016 itemsChanged
= NULL
;
1022 // we set it to NULL if there are many items changing state
1023 return itemsChanged
!= NULL
;
1026 void wxSelectionStore::OnItemDelete(size_t item
)
1028 size_t count
= m_itemsSel
.GetCount(),
1029 i
= m_itemsSel
.IndexForInsert(item
);
1031 if ( i
< count
&& m_itemsSel
[i
] == item
)
1033 // this item itself was in m_itemsSel, remove it from there
1034 m_itemsSel
.RemoveAt(i
);
1039 // and adjust the index of all which follow it
1042 // all following elements must be greater than the one we deleted
1043 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1049 //-----------------------------------------------------------------------------
1051 //-----------------------------------------------------------------------------
1053 wxListItemData::~wxListItemData()
1055 // in the virtual list control the attributes are managed by the main
1056 // program, so don't delete them
1057 if ( !m_owner
->IsVirtual() )
1065 void wxListItemData::Init()
1073 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1079 if ( owner
->InReportView() )
1085 m_rect
= new wxRect
;
1089 void wxListItemData::SetItem( const wxListItem
&info
)
1091 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1092 SetText(info
.m_text
);
1093 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1094 m_image
= info
.m_image
;
1095 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1096 m_data
= info
.m_data
;
1098 if ( info
.HasAttributes() )
1101 *m_attr
= *info
.GetAttributes();
1103 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1111 m_rect
->width
= info
.m_width
;
1115 void wxListItemData::SetPosition( int x
, int y
)
1117 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1123 void wxListItemData::SetSize( int width
, int height
)
1125 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1128 m_rect
->width
= width
;
1130 m_rect
->height
= height
;
1133 bool wxListItemData::IsHit( int x
, int y
) const
1135 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1137 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1140 int wxListItemData::GetX() const
1142 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1147 int wxListItemData::GetY() const
1149 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1154 int wxListItemData::GetWidth() const
1156 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1158 return m_rect
->width
;
1161 int wxListItemData::GetHeight() const
1163 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1165 return m_rect
->height
;
1168 void wxListItemData::GetItem( wxListItem
&info
) const
1170 info
.m_text
= m_text
;
1171 info
.m_image
= m_image
;
1172 info
.m_data
= m_data
;
1176 if ( m_attr
->HasTextColour() )
1177 info
.SetTextColour(m_attr
->GetTextColour());
1178 if ( m_attr
->HasBackgroundColour() )
1179 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1180 if ( m_attr
->HasFont() )
1181 info
.SetFont(m_attr
->GetFont());
1185 //-----------------------------------------------------------------------------
1187 //-----------------------------------------------------------------------------
1189 void wxListHeaderData::Init()
1200 wxListHeaderData::wxListHeaderData()
1205 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1212 void wxListHeaderData::SetItem( const wxListItem
&item
)
1214 m_mask
= item
.m_mask
;
1216 if ( m_mask
& wxLIST_MASK_TEXT
)
1217 m_text
= item
.m_text
;
1219 if ( m_mask
& wxLIST_MASK_IMAGE
)
1220 m_image
= item
.m_image
;
1222 if ( m_mask
& wxLIST_MASK_FORMAT
)
1223 m_format
= item
.m_format
;
1225 if ( m_mask
& wxLIST_MASK_WIDTH
)
1226 SetWidth(item
.m_width
);
1229 void wxListHeaderData::SetPosition( int x
, int y
)
1235 void wxListHeaderData::SetHeight( int h
)
1240 void wxListHeaderData::SetWidth( int w
)
1244 m_width
= WIDTH_COL_DEFAULT
;
1245 else if (m_width
< WIDTH_COL_MIN
)
1246 m_width
= WIDTH_COL_MIN
;
1249 void wxListHeaderData::SetFormat( int format
)
1254 bool wxListHeaderData::HasImage() const
1256 return m_image
!= -1;
1259 bool wxListHeaderData::IsHit( int x
, int y
) const
1261 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1264 void wxListHeaderData::GetItem( wxListItem
& item
)
1266 item
.m_mask
= m_mask
;
1267 item
.m_text
= m_text
;
1268 item
.m_image
= m_image
;
1269 item
.m_format
= m_format
;
1270 item
.m_width
= m_width
;
1273 int wxListHeaderData::GetImage() const
1278 int wxListHeaderData::GetWidth() const
1283 int wxListHeaderData::GetFormat() const
1288 //-----------------------------------------------------------------------------
1290 //-----------------------------------------------------------------------------
1292 inline int wxListLineData::GetMode() const
1294 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1297 inline bool wxListLineData::InReportView() const
1299 return m_owner
->HasFlag(wxLC_REPORT
);
1302 inline bool wxListLineData::IsVirtual() const
1304 return m_owner
->IsVirtual();
1307 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1310 m_items
.DeleteContents( TRUE
);
1312 if ( InReportView() )
1318 m_gi
= new GeometryInfo
;
1321 m_highlighted
= FALSE
;
1323 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1326 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1328 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1329 wxCHECK_RET( node
, _T("no subitems at all??") );
1331 wxListItemData
*item
= node
->GetData();
1333 switch ( GetMode() )
1336 case wxLC_SMALL_ICON
:
1338 m_gi
->m_rectAll
.width
= spacing
;
1340 wxString s
= item
->GetText();
1346 m_gi
->m_rectLabel
.width
=
1347 m_gi
->m_rectLabel
.height
= 0;
1351 dc
->GetTextExtent( s
, &lw
, &lh
);
1352 if (lh
< SCROLL_UNIT_Y
)
1357 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1359 m_gi
->m_rectAll
.width
= lw
;
1361 m_gi
->m_rectLabel
.width
= lw
;
1362 m_gi
->m_rectLabel
.height
= lh
;
1365 if (item
->HasImage())
1368 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1369 m_gi
->m_rectIcon
.width
= w
+ 8;
1370 m_gi
->m_rectIcon
.height
= h
+ 8;
1372 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1373 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1374 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1375 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1378 if ( item
->HasText() )
1380 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1381 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1383 else // no text, highlight the icon
1385 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1386 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1393 wxString s
= item
->GetTextForMeasuring();
1396 dc
->GetTextExtent( s
, &lw
, &lh
);
1397 if (lh
< SCROLL_UNIT_Y
)
1402 m_gi
->m_rectLabel
.width
= lw
;
1403 m_gi
->m_rectLabel
.height
= lh
;
1405 m_gi
->m_rectAll
.width
= lw
;
1406 m_gi
->m_rectAll
.height
= lh
;
1408 if (item
->HasImage())
1411 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1412 m_gi
->m_rectIcon
.width
= w
;
1413 m_gi
->m_rectIcon
.height
= h
;
1415 m_gi
->m_rectAll
.width
+= 4 + w
;
1416 if (h
> m_gi
->m_rectAll
.height
)
1417 m_gi
->m_rectAll
.height
= h
;
1420 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1421 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1426 wxFAIL_MSG( _T("unexpected call to SetSize") );
1430 wxFAIL_MSG( _T("unknown mode") );
1434 void wxListLineData::SetPosition( int x
, int y
,
1438 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1439 wxCHECK_RET( node
, _T("no subitems at all??") );
1441 wxListItemData
*item
= node
->GetData();
1443 switch ( GetMode() )
1446 case wxLC_SMALL_ICON
:
1447 m_gi
->m_rectAll
.x
= x
;
1448 m_gi
->m_rectAll
.y
= y
;
1450 if ( item
->HasImage() )
1452 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1453 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1454 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1457 if ( item
->HasText() )
1459 if (m_gi
->m_rectAll
.width
> spacing
)
1460 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1462 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1463 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1464 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1465 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1467 else // no text, highlight the icon
1469 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1470 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1475 m_gi
->m_rectAll
.x
= x
;
1476 m_gi
->m_rectAll
.y
= y
;
1478 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1479 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1480 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1482 if (item
->HasImage())
1484 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1485 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1486 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1490 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1495 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1499 wxFAIL_MSG( _T("unknown mode") );
1503 void wxListLineData::InitItems( int num
)
1505 for (int i
= 0; i
< num
; i
++)
1506 m_items
.Append( new wxListItemData(m_owner
) );
1509 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1511 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1512 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1514 wxListItemData
*item
= node
->GetData();
1515 item
->SetItem( info
);
1518 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1520 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1523 wxListItemData
*item
= node
->GetData();
1524 item
->GetItem( info
);
1528 wxString
wxListLineData::GetText(int index
) const
1532 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1535 wxListItemData
*item
= node
->GetData();
1536 s
= item
->GetText();
1542 void wxListLineData::SetText( int index
, const wxString s
)
1544 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1547 wxListItemData
*item
= node
->GetData();
1552 void wxListLineData::SetImage( int index
, int image
)
1554 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1555 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1557 wxListItemData
*item
= node
->GetData();
1558 item
->SetImage(image
);
1561 int wxListLineData::GetImage( int index
) const
1563 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1564 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1566 wxListItemData
*item
= node
->GetData();
1567 return item
->GetImage();
1570 wxListItemAttr
*wxListLineData::GetAttr() const
1572 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1573 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1575 wxListItemData
*item
= node
->GetData();
1576 return item
->GetAttr();
1579 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1581 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1582 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1584 wxListItemData
*item
= node
->GetData();
1585 item
->SetAttr(attr
);
1588 bool wxListLineData::SetAttributes(wxDC
*dc
,
1589 const wxListItemAttr
*attr
,
1592 wxWindow
*listctrl
= m_owner
->GetParent();
1596 // don't use foreground colour for drawing highlighted items - this might
1597 // make them completely invisible (and there is no way to do bit
1598 // arithmetics on wxColour, unfortunately)
1602 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1606 if ( attr
&& attr
->HasTextColour() )
1608 colText
= attr
->GetTextColour();
1612 colText
= listctrl
->GetForegroundColour();
1616 dc
->SetTextForeground(colText
);
1620 if ( attr
&& attr
->HasFont() )
1622 font
= attr
->GetFont();
1626 font
= listctrl
->GetFont();
1632 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1633 if ( highlighted
|| hasBgCol
)
1637 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1641 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1644 dc
->SetPen( *wxTRANSPARENT_PEN
);
1652 void wxListLineData::Draw( wxDC
*dc
)
1654 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1655 wxCHECK_RET( node
, _T("no subitems at all??") );
1657 bool highlighted
= IsHighlighted();
1659 wxListItemAttr
*attr
= GetAttr();
1661 if ( SetAttributes(dc
, attr
, highlighted
) )
1663 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1666 wxListItemData
*item
= node
->GetData();
1667 if (item
->HasImage())
1669 wxRect rectIcon
= m_gi
->m_rectIcon
;
1670 m_owner
->DrawImage( item
->GetImage(), dc
,
1671 rectIcon
.x
, rectIcon
.y
);
1674 if (item
->HasText())
1676 wxRect rectLabel
= m_gi
->m_rectLabel
;
1678 wxDCClipper
clipper(*dc
, rectLabel
);
1679 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1683 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1685 const wxRect
& rectHL
,
1688 // TODO: later we should support setting different attributes for
1689 // different columns - to do it, just add "col" argument to
1690 // GetAttr() and move these lines into the loop below
1691 wxListItemAttr
*attr
= GetAttr();
1692 if ( SetAttributes(dc
, attr
, highlighted
) )
1694 dc
->DrawRectangle( rectHL
);
1697 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1698 wxCHECK_RET( node
, _T("no subitems at all??") );
1701 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1702 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1706 wxListItemData
*item
= node
->GetData();
1708 int width
= m_owner
->GetColumnWidth(col
++);
1712 if ( item
->HasImage() )
1715 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1716 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1718 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1724 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1726 if ( item
->HasText() )
1728 dc
->DrawText( item
->GetText(), xOld
, y
);
1731 node
= node
->GetNext();
1735 bool wxListLineData::Highlight( bool on
)
1737 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1739 if ( on
== m_highlighted
)
1747 void wxListLineData::ReverseHighlight( void )
1749 Highlight(!IsHighlighted());
1752 //-----------------------------------------------------------------------------
1753 // wxListHeaderWindow
1754 //-----------------------------------------------------------------------------
1756 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1758 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1759 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1760 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1761 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1764 void wxListHeaderWindow::Init()
1766 m_currentCursor
= (wxCursor
*) NULL
;
1767 m_isDragging
= FALSE
;
1771 wxListHeaderWindow::wxListHeaderWindow()
1775 m_owner
= (wxListMainWindow
*) NULL
;
1776 m_resizeCursor
= (wxCursor
*) NULL
;
1779 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1781 wxListMainWindow
*owner
,
1785 const wxString
&name
)
1786 : wxWindow( win
, id
, pos
, size
, style
, name
)
1791 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1793 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1796 wxListHeaderWindow::~wxListHeaderWindow()
1798 delete m_resizeCursor
;
1801 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1803 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1804 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1805 : GTK_STATE_INSENSITIVE
;
1807 x
= dc
->XLOG2DEV( x
);
1809 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1810 state
, GTK_SHADOW_OUT
,
1811 (GdkRectangle
*) NULL
, m_wxwindow
,
1812 (char *)"button", // const_cast
1813 x
-1, y
-1, w
+2, h
+2);
1814 #elif defined( __WXMAC__ )
1815 const int m_corner
= 1;
1817 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1819 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1820 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1821 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1823 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1826 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1827 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1829 dc
->SetPen( *wxWHITE_PEN
);
1830 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1831 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1832 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1833 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1835 const int m_corner
= 1;
1837 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1839 dc
->SetPen( *wxBLACK_PEN
);
1840 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1841 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1843 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1846 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1847 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1849 dc
->SetPen( *wxWHITE_PEN
);
1850 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1851 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1852 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1853 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1857 // shift the DC origin to match the position of the main window horz
1858 // scrollbar: this allows us to always use logical coords
1859 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1862 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1865 m_owner
->GetViewStart( &x
, NULL
);
1867 // account for the horz scrollbar offset
1868 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1871 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1873 #if defined(__WXGTK__)
1874 wxClientDC
dc( this );
1876 wxPaintDC
dc( this );
1884 dc
.SetFont( GetFont() );
1886 // width and height of the entire header window
1888 GetClientSize( &w
, &h
);
1889 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1891 dc
.SetBackgroundMode(wxTRANSPARENT
);
1893 // do *not* use the listctrl colour for headers - one day we will have a
1894 // function to set it separately
1895 //dc.SetTextForeground( *wxBLACK );
1896 dc
.SetTextForeground(wxSystemSettings::
1897 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1899 int x
= HEADER_OFFSET_X
;
1901 int numColumns
= m_owner
->GetColumnCount();
1903 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1905 m_owner
->GetColumn( i
, item
);
1906 int wCol
= item
.m_width
;
1908 // the width of the rect to draw: make it smaller to fit entirely
1909 // inside the column rect
1912 dc
.SetPen( *wxWHITE_PEN
);
1914 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1916 // if we have an image, draw it on the right of the label
1917 int image
= item
.m_image
;
1920 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1924 imageList
->GetSize(image
, ix
, iy
);
1931 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1932 wxIMAGELIST_DRAW_TRANSPARENT
1937 //else: ignore the column image
1940 // draw the text clipping it so that it doesn't overwrite the column
1942 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1944 dc
.DrawText( item
.GetText(),
1945 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1953 void wxListHeaderWindow::DrawCurrent()
1955 int x1
= m_currentX
;
1957 m_owner
->ClientToScreen( &x1
, &y1
);
1959 int x2
= m_currentX
;
1961 m_owner
->GetClientSize( NULL
, &y2
);
1962 m_owner
->ClientToScreen( &x2
, &y2
);
1965 dc
.SetLogicalFunction( wxINVERT
);
1966 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1967 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1971 dc
.DrawLine( x1
, y1
, x2
, y2
);
1973 dc
.SetLogicalFunction( wxCOPY
);
1975 dc
.SetPen( wxNullPen
);
1976 dc
.SetBrush( wxNullBrush
);
1979 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1981 // we want to work with logical coords
1983 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1984 int y
= event
.GetY();
1988 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
,
1989 event
.GetPosition());
1991 // we don't draw the line beyond our window, but we allow dragging it
1994 GetClientSize( &w
, NULL
);
1995 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1998 // erase the line if it was drawn
1999 if ( m_currentX
< w
)
2002 if (event
.ButtonUp())
2005 m_isDragging
= FALSE
;
2007 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2008 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
,
2009 event
.GetPosition());
2016 m_currentX
= m_minX
+ 7;
2018 // draw in the new location
2019 if ( m_currentX
< w
)
2023 else // not dragging
2026 bool hit_border
= FALSE
;
2028 // end of the current column
2031 // find the column where this event occured
2033 countCol
= m_owner
->GetColumnCount();
2034 for (col
= 0; col
< countCol
; col
++)
2036 xpos
+= m_owner
->GetColumnWidth( col
);
2039 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2041 // near the column border
2048 // inside the column
2055 if ( col
== countCol
)
2058 if (event
.LeftDown() || event
.RightUp())
2060 if (hit_border
&& event
.LeftDown())
2062 m_isDragging
= TRUE
;
2066 SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2067 event
.GetPosition());
2069 else // click on a column
2071 SendListEvent( event
.LeftDown()
2072 ? wxEVT_COMMAND_LIST_COL_CLICK
2073 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2074 event
.GetPosition());
2077 else if (event
.Moving())
2082 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2083 m_currentCursor
= m_resizeCursor
;
2087 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2088 m_currentCursor
= wxSTANDARD_CURSOR
;
2092 SetCursor(*m_currentCursor
);
2097 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2099 m_owner
->SetFocus();
2102 void wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2104 wxWindow
*parent
= GetParent();
2105 wxListEvent
le( type
, parent
->GetId() );
2106 le
.SetEventObject( parent
);
2107 le
.m_pointDrag
= pos
;
2109 // the position should be relative to the parent window, not
2110 // this one for compatibility with MSW and common sense: the
2111 // user code doesn't know anything at all about this header
2112 // window, so why should it get positions relative to it?
2113 le
.m_pointDrag
.y
-= GetSize().y
;
2115 le
.m_col
= m_column
;
2116 parent
->GetEventHandler()->ProcessEvent( le
);
2119 //-----------------------------------------------------------------------------
2120 // wxListRenameTimer (internal)
2121 //-----------------------------------------------------------------------------
2123 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2128 void wxListRenameTimer::Notify()
2130 m_owner
->OnRenameTimer();
2133 //-----------------------------------------------------------------------------
2134 // wxListTextCtrl (internal)
2135 //-----------------------------------------------------------------------------
2137 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
)
2139 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2140 EVT_CHAR (wxListTextCtrl::OnChar
)
2141 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2142 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2145 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
2146 const wxWindowID id
,
2149 wxListMainWindow
*owner
,
2150 const wxString
&value
,
2154 const wxValidator
& validator
,
2155 const wxString
&name
)
2156 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
2161 (*m_accept
) = FALSE
;
2163 m_startValue
= value
;
2167 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2169 if (event
.m_keyCode
== WXK_RETURN
)
2172 (*m_res
) = GetValue();
2174 if (!wxPendingDelete
.Member(this))
2175 wxPendingDelete
.Append(this);
2177 if ((*m_res
) != m_startValue
)
2178 m_owner
->OnRenameAccept();
2181 m_owner
->SetFocus();
2185 if (event
.m_keyCode
== WXK_ESCAPE
)
2187 (*m_accept
) = FALSE
;
2190 if (!wxPendingDelete
.Member(this))
2191 wxPendingDelete
.Append(this);
2194 m_owner
->SetFocus();
2202 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2210 // auto-grow the textctrl:
2211 wxSize parentSize
= m_owner
->GetSize();
2212 wxPoint myPos
= GetPosition();
2213 wxSize mySize
= GetSize();
2215 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2216 if (myPos
.x
+ sx
> parentSize
.x
)
2217 sx
= parentSize
.x
- myPos
.x
;
2225 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2233 if (!wxPendingDelete
.Member(this))
2234 wxPendingDelete
.Append(this);
2237 (*m_res
) = GetValue();
2239 if ((*m_res
) != m_startValue
)
2240 m_owner
->OnRenameAccept();
2243 //-----------------------------------------------------------------------------
2245 //-----------------------------------------------------------------------------
2247 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2249 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2250 EVT_PAINT (wxListMainWindow::OnPaint
)
2251 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2252 EVT_CHAR (wxListMainWindow::OnChar
)
2253 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2254 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2255 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2256 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2259 void wxListMainWindow::Init()
2261 m_columns
.DeleteContents( TRUE
);
2265 m_lineTo
= (size_t)-1;
2271 m_small_image_list
= (wxImageList
*) NULL
;
2272 m_normal_image_list
= (wxImageList
*) NULL
;
2274 m_small_spacing
= 30;
2275 m_normal_spacing
= 40;
2279 m_isCreated
= FALSE
;
2281 m_lastOnSame
= FALSE
;
2282 m_renameTimer
= new wxListRenameTimer( this );
2283 m_renameAccept
= FALSE
;
2288 m_lineBeforeLastClicked
= (size_t)-1;
2293 void wxListMainWindow::InitScrolling()
2295 if ( HasFlag(wxLC_REPORT
) )
2297 m_xScroll
= SCROLL_UNIT_X
;
2298 m_yScroll
= SCROLL_UNIT_Y
;
2302 m_xScroll
= SCROLL_UNIT_Y
;
2307 wxListMainWindow::wxListMainWindow()
2312 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2318 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2323 const wxString
&name
)
2324 : wxScrolledWindow( parent
, id
, pos
, size
,
2325 style
| wxHSCROLL
| wxVSCROLL
, name
)
2329 m_highlightBrush
= new wxBrush
2331 wxSystemSettings::GetColour
2333 wxSYS_COLOUR_HIGHLIGHT
2338 m_highlightUnfocusedBrush
= new wxBrush
2340 wxSystemSettings::GetColour
2342 wxSYS_COLOUR_BTNSHADOW
2351 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2353 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2356 wxListMainWindow::~wxListMainWindow()
2360 delete m_highlightBrush
;
2361 delete m_highlightUnfocusedBrush
;
2363 delete m_renameTimer
;
2366 void wxListMainWindow::CacheLineData(size_t line
)
2368 wxListCtrl
*listctrl
= GetListCtrl();
2370 wxListLineData
*ld
= GetDummyLine();
2372 size_t countCol
= GetColumnCount();
2373 for ( size_t col
= 0; col
< countCol
; col
++ )
2375 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2378 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2379 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2382 wxListLineData
*wxListMainWindow::GetDummyLine() const
2384 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2386 if ( m_lines
.IsEmpty() )
2388 // normal controls are supposed to have something in m_lines
2389 // already if it's not empty
2390 wxASSERT_MSG( IsVirtual(), _T("logic error") );
2392 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2393 wxListLineData
*line
= new wxListLineData(self
);
2394 self
->m_lines
.Add(line
);
2400 // ----------------------------------------------------------------------------
2401 // line geometry (report mode only)
2402 // ----------------------------------------------------------------------------
2404 wxCoord
wxListMainWindow::GetLineHeight() const
2406 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2408 // we cache the line height as calling GetTextExtent() is slow
2409 if ( !m_lineHeight
)
2411 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2413 wxClientDC
dc( self
);
2414 dc
.SetFont( GetFont() );
2417 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2419 if ( y
< SCROLL_UNIT_Y
)
2423 self
->m_lineHeight
= y
+ LINE_SPACING
;
2426 return m_lineHeight
;
2429 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2431 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2433 return LINE_SPACING
+ line
*GetLineHeight();
2436 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2438 if ( !InReportView() )
2439 return GetLine(line
)->m_gi
->m_rectAll
;
2442 rect
.x
= HEADER_OFFSET_X
;
2443 rect
.y
= GetLineY(line
);
2444 rect
.width
= GetHeaderWidth();
2445 rect
.height
= GetLineHeight();
2450 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2452 if ( !InReportView() )
2453 return GetLine(line
)->m_gi
->m_rectLabel
;
2456 rect
.x
= HEADER_OFFSET_X
;
2457 rect
.y
= GetLineY(line
);
2458 rect
.width
= GetColumnWidth(0);
2459 rect
.height
= GetLineHeight();
2464 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2466 if ( !InReportView() )
2467 return GetLine(line
)->m_gi
->m_rectIcon
;
2469 wxListLineData
*ld
= GetLine(line
);
2470 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2473 rect
.x
= HEADER_OFFSET_X
;
2474 rect
.y
= GetLineY(line
);
2475 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2480 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2482 return InReportView() ? GetLineRect(line
)
2483 : GetLine(line
)->m_gi
->m_rectHighlight
;
2486 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2488 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2490 wxListLineData
*ld
= GetLine(line
);
2492 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2493 return wxLIST_HITTEST_ONITEMICON
;
2495 // VS: Testing for "ld->HasText() || InReportView()" instead of
2496 // "ld->HasText()" is needed to make empty lines in report view
2498 if ( ld
->HasText() || InReportView() )
2500 wxRect rect
= InReportView() ? GetLineRect(line
)
2501 : GetLineLabelRect(line
);
2503 if ( rect
.Inside(x
, y
) )
2504 return wxLIST_HITTEST_ONITEMLABEL
;
2510 // ----------------------------------------------------------------------------
2511 // highlight (selection) handling
2512 // ----------------------------------------------------------------------------
2514 bool wxListMainWindow::IsHighlighted(size_t line
) const
2518 return m_selStore
.IsSelected(line
);
2522 wxListLineData
*ld
= GetLine(line
);
2523 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2525 return ld
->IsHighlighted();
2529 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2535 wxArrayInt linesChanged
;
2536 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2539 // meny items changed state, refresh everything
2540 RefreshLines(lineFrom
, lineTo
);
2542 else // only a few items changed state, refresh only them
2544 size_t count
= linesChanged
.GetCount();
2545 for ( size_t n
= 0; n
< count
; n
++ )
2547 RefreshLine(linesChanged
[n
]);
2551 else // iterate over all items in non report view
2553 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2555 if ( HighlightLine(line
, highlight
) )
2563 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2569 changed
= m_selStore
.SelectItem(line
, highlight
);
2573 wxListLineData
*ld
= GetLine(line
);
2574 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2576 changed
= ld
->Highlight(highlight
);
2581 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2582 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2588 void wxListMainWindow::RefreshLine( size_t line
)
2590 if ( HasFlag(wxLC_REPORT
) )
2592 size_t visibleFrom
, visibleTo
;
2593 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2595 if ( line
< visibleFrom
|| line
> visibleTo
)
2599 wxRect rect
= GetLineRect(line
);
2601 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2602 RefreshRect( rect
);
2605 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2607 // we suppose that they are ordered by caller
2608 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2610 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2612 if ( HasFlag(wxLC_REPORT
) )
2614 size_t visibleFrom
, visibleTo
;
2615 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2617 if ( lineFrom
< visibleFrom
)
2618 lineFrom
= visibleFrom
;
2619 if ( lineTo
> visibleTo
)
2624 rect
.y
= GetLineY(lineFrom
);
2625 rect
.width
= GetClientSize().x
;
2626 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2628 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2629 RefreshRect( rect
);
2633 // TODO: this should be optimized...
2634 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2641 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2643 if ( HasFlag(wxLC_REPORT
) )
2646 GetVisibleLinesRange(&visibleFrom
, NULL
);
2648 if ( lineFrom
< visibleFrom
)
2649 lineFrom
= visibleFrom
;
2653 rect
.y
= GetLineY(lineFrom
);
2655 wxSize size
= GetClientSize();
2656 rect
.width
= size
.x
;
2657 // refresh till the bottom of the window
2658 rect
.height
= size
.y
- rect
.y
;
2660 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2661 RefreshRect( rect
);
2665 // TODO: how to do it more efficiently?
2670 void wxListMainWindow::RefreshSelected()
2676 if ( InReportView() )
2678 GetVisibleLinesRange(&from
, &to
);
2683 to
= GetItemCount() - 1;
2686 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2688 RefreshLine(m_current
);
2691 for ( size_t line
= from
; line
<= to
; line
++ )
2693 // NB: the test works as expected even if m_current == -1
2694 if ( line
!= m_current
&& IsHighlighted(line
) )
2701 void wxListMainWindow::Freeze()
2706 void wxListMainWindow::Thaw()
2708 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2710 if ( !--m_freezeCount
)
2716 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2718 // Note: a wxPaintDC must be constructed even if no drawing is
2719 // done (a Windows requirement).
2720 wxPaintDC
dc( this );
2722 if ( IsEmpty() || m_freezeCount
)
2724 // nothing to draw or not the moment to draw it
2730 // delay the repainting until we calculate all the items positions
2737 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2741 dc
.SetFont( GetFont() );
2743 if ( HasFlag(wxLC_REPORT
) )
2745 int lineHeight
= GetLineHeight();
2747 size_t visibleFrom
, visibleTo
;
2748 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2751 wxCoord xOrig
, yOrig
;
2752 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2754 // tell the caller cache to cache the data
2757 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2758 GetParent()->GetId());
2759 evCache
.SetEventObject( GetParent() );
2760 evCache
.m_oldItemIndex
= visibleFrom
;
2761 evCache
.m_itemIndex
= visibleTo
;
2762 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2765 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2767 rectLine
= GetLineRect(line
);
2769 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2770 rectLine
.width
, rectLine
.height
) )
2772 // don't redraw unaffected lines to avoid flicker
2776 GetLine(line
)->DrawInReportMode( &dc
,
2778 GetLineHighlightRect(line
),
2779 IsHighlighted(line
) );
2782 if ( HasFlag(wxLC_HRULES
) )
2784 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2785 wxSize clientSize
= GetClientSize();
2787 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2790 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2791 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2792 clientSize
.x
- dev_x
, i
*lineHeight
);
2795 // Draw last horizontal rule
2796 if ( visibleTo
> visibleFrom
)
2799 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2800 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2801 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2805 // Draw vertical rules if required
2806 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2808 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2811 wxRect firstItemRect
;
2812 wxRect lastItemRect
;
2813 GetItemRect(0, firstItemRect
);
2814 GetItemRect(GetItemCount() - 1, lastItemRect
);
2815 int x
= firstItemRect
.GetX();
2817 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2818 for (col
= 0; col
< GetColumnCount(); col
++)
2820 int colWidth
= GetColumnWidth(col
);
2822 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2823 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2829 size_t count
= GetItemCount();
2830 for ( size_t i
= 0; i
< count
; i
++ )
2832 GetLine(i
)->Draw( &dc
);
2838 // don't draw rect outline under Max if we already have the background
2839 // color but under other platforms only draw it if we do: it is a bit
2840 // silly to draw "focus rect" if we don't have focus!
2845 #endif // __WXMAC__/!__WXMAC__
2847 dc
.SetPen( *wxBLACK_PEN
);
2848 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2849 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2856 void wxListMainWindow::HighlightAll( bool on
)
2858 if ( IsSingleSel() )
2860 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2862 // we just have one item to turn off
2863 if ( HasCurrent() && IsHighlighted(m_current
) )
2865 HighlightLine(m_current
, FALSE
);
2866 RefreshLine(m_current
);
2871 HighlightLines(0, GetItemCount() - 1, on
);
2875 void wxListMainWindow::SendNotify( size_t line
,
2876 wxEventType command
,
2879 wxListEvent
le( command
, GetParent()->GetId() );
2880 le
.SetEventObject( GetParent() );
2881 le
.m_itemIndex
= line
;
2883 // set only for events which have position
2884 if ( point
!= wxDefaultPosition
)
2885 le
.m_pointDrag
= point
;
2887 // don't try to get the line info for virtual list controls: the main
2888 // program has it anyhow and if we did it would result in accessing all
2889 // the lines, even those which are not visible now and this is precisely
2890 // what we're trying to avoid
2891 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2893 if ( line
!= (size_t)-1 )
2895 GetLine(line
)->GetItem( 0, le
.m_item
);
2897 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2899 //else: there may be no more such item
2901 GetParent()->GetEventHandler()->ProcessEvent( le
);
2904 void wxListMainWindow::ChangeCurrent(size_t current
)
2906 m_current
= current
;
2908 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2911 void wxListMainWindow::EditLabel( long item
)
2913 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2914 wxT("wrong index in wxListCtrl::EditLabel()") );
2916 m_currentEdit
= (size_t)item
;
2918 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2919 le
.SetEventObject( GetParent() );
2920 le
.m_itemIndex
= item
;
2921 wxListLineData
*data
= GetLine(m_currentEdit
);
2922 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2923 data
->GetItem( 0, le
.m_item
);
2924 GetParent()->GetEventHandler()->ProcessEvent( le
);
2926 if (!le
.IsAllowed())
2929 // We have to call this here because the label in question might just have
2930 // been added and no screen update taken place.
2934 wxString s
= data
->GetText(0);
2935 wxRect rectLabel
= GetLineLabelRect(m_currentEdit
);
2937 CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
, &rectLabel
.x
, &rectLabel
.y
);
2939 wxListTextCtrl
*text
= new wxListTextCtrl
2946 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2947 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2952 void wxListMainWindow::OnRenameTimer()
2954 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2956 EditLabel( m_current
);
2959 void wxListMainWindow::OnRenameAccept()
2961 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2962 le
.SetEventObject( GetParent() );
2963 le
.m_itemIndex
= m_currentEdit
;
2965 wxListLineData
*data
= GetLine(m_currentEdit
);
2966 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2968 data
->GetItem( 0, le
.m_item
);
2969 le
.m_item
.m_text
= m_renameRes
;
2970 GetParent()->GetEventHandler()->ProcessEvent( le
);
2972 if (!le
.IsAllowed()) return;
2975 info
.m_mask
= wxLIST_MASK_TEXT
;
2976 info
.m_itemId
= le
.m_itemIndex
;
2977 info
.m_text
= m_renameRes
;
2978 info
.SetTextColour(le
.m_item
.GetTextColour());
2982 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2984 event
.SetEventObject( GetParent() );
2985 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2988 if ( !HasCurrent() || IsEmpty() )
2994 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2995 event
.ButtonDClick()) )
2998 int x
= event
.GetX();
2999 int y
= event
.GetY();
3000 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3002 // where did we hit it (if we did)?
3005 size_t count
= GetItemCount(),
3008 if ( HasFlag(wxLC_REPORT
) )
3010 current
= y
/ GetLineHeight();
3011 if ( current
< count
)
3012 hitResult
= HitTestLine(current
, x
, y
);
3016 // TODO: optimize it too! this is less simple than for report view but
3017 // enumerating all items is still not a way to do it!!
3018 for ( current
= 0; current
< count
; current
++ )
3020 hitResult
= HitTestLine(current
, x
, y
);
3026 if (event
.Dragging())
3028 if (m_dragCount
== 0)
3030 // we have to report the raw, physical coords as we want to be
3031 // able to call HitTest(event.m_pointDrag) from the user code to
3032 // get the item being dragged
3033 m_dragStart
= event
.GetPosition();
3038 if (m_dragCount
!= 3)
3041 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3042 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3044 wxListEvent
le( command
, GetParent()->GetId() );
3045 le
.SetEventObject( GetParent() );
3046 le
.m_pointDrag
= m_dragStart
;
3047 GetParent()->GetEventHandler()->ProcessEvent( le
);
3058 // outside of any item
3062 bool forceClick
= FALSE
;
3063 if (event
.ButtonDClick())
3065 m_renameTimer
->Stop();
3066 m_lastOnSame
= FALSE
;
3069 // FIXME: wxGTK generates bad sequence of events prior to doubleclick
3070 // ("down, up, down, double, up" while other ports
3071 // do "down, up, double, up"). We have to have this hack
3072 // in place till somebody fixes wxGTK...
3073 if ( current
== m_lineBeforeLastClicked
)
3075 if ( current
== m_lineLastClicked
)
3078 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3084 // the first click was on another item, so don't interpret this as
3085 // a double click, but as a simple click instead
3090 if (event
.LeftUp() && m_lastOnSame
)
3092 if ((current
== m_current
) &&
3093 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3094 HasFlag(wxLC_EDIT_LABELS
) )
3096 m_renameTimer
->Start( 100, TRUE
);
3098 m_lastOnSame
= FALSE
;
3100 else if (event
.RightDown())
3102 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3103 event
.GetPosition() );
3105 else if (event
.MiddleDown())
3107 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3109 else if ( event
.LeftDown() || forceClick
)
3111 m_lineBeforeLastClicked
= m_lineLastClicked
;
3112 m_lineLastClicked
= current
;
3114 size_t oldCurrent
= m_current
;
3116 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3118 HighlightAll( FALSE
);
3120 ChangeCurrent(current
);
3122 ReverseHighlight(m_current
);
3124 else // multi sel & either ctrl or shift is down
3126 if (event
.ControlDown())
3128 ChangeCurrent(current
);
3130 ReverseHighlight(m_current
);
3132 else if (event
.ShiftDown())
3134 ChangeCurrent(current
);
3136 size_t lineFrom
= oldCurrent
,
3139 if ( lineTo
< lineFrom
)
3142 lineFrom
= m_current
;
3145 HighlightLines(lineFrom
, lineTo
);
3147 else // !ctrl, !shift
3149 // test in the enclosing if should make it impossible
3150 wxFAIL_MSG( _T("how did we get here?") );
3154 if (m_current
!= oldCurrent
)
3156 RefreshLine( oldCurrent
);
3159 // forceClick is only set if the previous click was on another item
3160 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3164 void wxListMainWindow::MoveToItem(size_t item
)
3166 if ( item
== (size_t)-1 )
3169 wxRect rect
= GetLineRect(item
);
3171 int client_w
, client_h
;
3172 GetClientSize( &client_w
, &client_h
);
3174 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3175 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3177 if ( HasFlag(wxLC_REPORT
) )
3179 // the next we need the range of lines shown it might be different, so
3181 ResetVisibleLinesRange();
3183 if (rect
.y
< view_y
)
3184 Scroll( -1, rect
.y
/m_yScroll
);
3185 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3186 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3190 if (rect
.x
-view_x
< 5)
3191 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3192 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3193 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3197 // ----------------------------------------------------------------------------
3198 // keyboard handling
3199 // ----------------------------------------------------------------------------
3201 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3203 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3204 _T("invalid item index in OnArrowChar()") );
3206 size_t oldCurrent
= m_current
;
3208 // in single selection we just ignore Shift as we can't select several
3210 if ( event
.ShiftDown() && !IsSingleSel() )
3212 ChangeCurrent(newCurrent
);
3214 // select all the items between the old and the new one
3215 if ( oldCurrent
> newCurrent
)
3217 newCurrent
= oldCurrent
;
3218 oldCurrent
= m_current
;
3221 HighlightLines(oldCurrent
, newCurrent
);
3225 // all previously selected items are unselected unless ctrl is held
3226 if ( !event
.ControlDown() )
3227 HighlightAll(FALSE
);
3229 ChangeCurrent(newCurrent
);
3231 HighlightLine( oldCurrent
, FALSE
);
3232 RefreshLine( oldCurrent
);
3234 if ( !event
.ControlDown() )
3236 HighlightLine( m_current
, TRUE
);
3240 RefreshLine( m_current
);
3245 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3247 wxWindow
*parent
= GetParent();
3249 /* we propagate the key event up */
3250 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3251 ke
.m_shiftDown
= event
.m_shiftDown
;
3252 ke
.m_controlDown
= event
.m_controlDown
;
3253 ke
.m_altDown
= event
.m_altDown
;
3254 ke
.m_metaDown
= event
.m_metaDown
;
3255 ke
.m_keyCode
= event
.m_keyCode
;
3258 ke
.SetEventObject( parent
);
3259 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3264 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3266 wxWindow
*parent
= GetParent();
3268 /* we send a list_key event up */
3271 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3272 le
.m_itemIndex
= m_current
;
3273 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3274 le
.m_code
= (int)event
.KeyCode();
3275 le
.SetEventObject( parent
);
3276 parent
->GetEventHandler()->ProcessEvent( le
);
3279 /* we propagate the char event up */
3280 wxKeyEvent
ke( wxEVT_CHAR
);
3281 ke
.m_shiftDown
= event
.m_shiftDown
;
3282 ke
.m_controlDown
= event
.m_controlDown
;
3283 ke
.m_altDown
= event
.m_altDown
;
3284 ke
.m_metaDown
= event
.m_metaDown
;
3285 ke
.m_keyCode
= event
.m_keyCode
;
3288 ke
.SetEventObject( parent
);
3289 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3291 if (event
.KeyCode() == WXK_TAB
)
3293 wxNavigationKeyEvent nevent
;
3294 nevent
.SetWindowChange( event
.ControlDown() );
3295 nevent
.SetDirection( !event
.ShiftDown() );
3296 nevent
.SetEventObject( GetParent()->GetParent() );
3297 nevent
.SetCurrentFocus( m_parent
);
3298 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3302 /* no item -> nothing to do */
3309 switch (event
.KeyCode())
3312 if ( m_current
> 0 )
3313 OnArrowChar( m_current
- 1, event
);
3317 if ( m_current
< (size_t)GetItemCount() - 1 )
3318 OnArrowChar( m_current
+ 1, event
);
3323 OnArrowChar( GetItemCount() - 1, event
);
3328 OnArrowChar( 0, event
);
3334 if ( HasFlag(wxLC_REPORT
) )
3336 steps
= m_linesPerPage
- 1;
3340 steps
= m_current
% m_linesPerPage
;
3343 int index
= m_current
- steps
;
3347 OnArrowChar( index
, event
);
3354 if ( HasFlag(wxLC_REPORT
) )
3356 steps
= m_linesPerPage
- 1;
3360 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3363 size_t index
= m_current
+ steps
;
3364 size_t count
= GetItemCount();
3365 if ( index
>= count
)
3368 OnArrowChar( index
, event
);
3373 if ( !HasFlag(wxLC_REPORT
) )
3375 int index
= m_current
- m_linesPerPage
;
3379 OnArrowChar( index
, event
);
3384 if ( !HasFlag(wxLC_REPORT
) )
3386 size_t index
= m_current
+ m_linesPerPage
;
3388 size_t count
= GetItemCount();
3389 if ( index
>= count
)
3392 OnArrowChar( index
, event
);
3397 if ( IsSingleSel() )
3399 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3401 if ( IsHighlighted(m_current
) )
3403 // don't unselect the item in single selection mode
3406 //else: select it in ReverseHighlight() below if unselected
3409 ReverseHighlight(m_current
);
3414 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3422 // ----------------------------------------------------------------------------
3424 // ----------------------------------------------------------------------------
3426 void wxListMainWindow::SetFocus()
3428 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3429 // overrides SetFocus in such way that it does never change focus from
3430 // panel's child to the panel itself. Unfortunately, we must be able to change
3431 // focus to the panel from wxListTextCtrl because the text control should
3432 // disappear when the user clicks outside it.
3434 wxWindow
*oldFocus
= FindFocus();
3436 if ( oldFocus
&& oldFocus
->GetParent() == this )
3438 wxWindow::SetFocus();
3442 wxScrolledWindow::SetFocus();
3446 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3448 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3449 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3450 // which are already drawn correctly resulting in horrible flicker - avoid
3462 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3463 event
.SetEventObject( GetParent() );
3464 GetParent()->GetEventHandler()->ProcessEvent( event
);
3467 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3474 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3476 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3478 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3480 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3482 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3484 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3486 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3488 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3490 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3494 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3496 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3498 m_normal_image_list
->GetSize( index
, width
, height
);
3500 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3502 m_small_image_list
->GetSize( index
, width
, height
);
3504 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3506 m_small_image_list
->GetSize( index
, width
, height
);
3508 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3510 m_small_image_list
->GetSize( index
, width
, height
);
3519 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3521 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3522 dc
.SetFont( GetFont() );
3525 dc
.GetTextExtent( s
, &lw
, NULL
);
3527 return lw
+ AUTOSIZE_COL_MARGIN
;
3530 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3534 // calc the spacing from the icon size
3537 if ((imageList
) && (imageList
->GetImageCount()) )
3539 imageList
->GetSize(0, width
, height
);
3542 if (which
== wxIMAGE_LIST_NORMAL
)
3544 m_normal_image_list
= imageList
;
3545 m_normal_spacing
= width
+ 8;
3548 if (which
== wxIMAGE_LIST_SMALL
)
3550 m_small_image_list
= imageList
;
3551 m_small_spacing
= width
+ 14;
3555 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3560 m_small_spacing
= spacing
;
3564 m_normal_spacing
= spacing
;
3568 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3570 return isSmall
? m_small_spacing
: m_normal_spacing
;
3573 // ----------------------------------------------------------------------------
3575 // ----------------------------------------------------------------------------
3577 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3579 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3581 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3583 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3584 item
.m_width
= GetTextLength( item
.m_text
);
3586 wxListHeaderData
*column
= node
->GetData();
3587 column
->SetItem( item
);
3589 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3591 headerWin
->m_dirty
= TRUE
;
3595 // invalidate it as it has to be recalculated
3599 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3601 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3602 _T("invalid column index") );
3604 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3605 _T("SetColumnWidth() can only be called in report mode.") );
3608 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3610 headerWin
->m_dirty
= TRUE
;
3612 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3613 wxCHECK_RET( node
, _T("no column?") );
3615 wxListHeaderData
*column
= node
->GetData();
3617 size_t count
= GetItemCount();
3619 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3621 width
= GetTextLength(column
->GetText());
3623 else if ( width
== wxLIST_AUTOSIZE
)
3627 // TODO: determine the max width somehow...
3628 width
= WIDTH_COL_DEFAULT
;
3632 wxClientDC
dc(this);
3633 dc
.SetFont( GetFont() );
3635 int max
= AUTOSIZE_COL_MARGIN
;
3637 for ( size_t i
= 0; i
< count
; i
++ )
3639 wxListLineData
*line
= GetLine(i
);
3640 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3642 wxCHECK_RET( n
, _T("no subitem?") );
3644 wxListItemData
*item
= n
->GetData();
3647 if (item
->HasImage())
3650 GetImageSize( item
->GetImage(), ix
, iy
);
3654 if (item
->HasText())
3657 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3665 width
= max
+ AUTOSIZE_COL_MARGIN
;
3669 column
->SetWidth( width
);
3671 // invalidate it as it has to be recalculated
3675 int wxListMainWindow::GetHeaderWidth() const
3677 if ( !m_headerWidth
)
3679 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3681 size_t count
= GetColumnCount();
3682 for ( size_t col
= 0; col
< count
; col
++ )
3684 self
->m_headerWidth
+= GetColumnWidth(col
);
3688 return m_headerWidth
;
3691 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3693 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3694 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3696 wxListHeaderData
*column
= node
->GetData();
3697 column
->GetItem( item
);
3700 int wxListMainWindow::GetColumnWidth( int col
) const
3702 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3703 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3705 wxListHeaderData
*column
= node
->GetData();
3706 return column
->GetWidth();
3709 // ----------------------------------------------------------------------------
3711 // ----------------------------------------------------------------------------
3713 void wxListMainWindow::SetItem( wxListItem
&item
)
3715 long id
= item
.m_itemId
;
3716 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3717 _T("invalid item index in SetItem") );
3721 wxListLineData
*line
= GetLine((size_t)id
);
3722 line
->SetItem( item
.m_col
, item
);
3725 if ( InReportView() )
3727 // just refresh the line to show the new value of the text/image
3728 RefreshLine((size_t)id
);
3732 // refresh everything (resulting in horrible flicker - FIXME!)
3737 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3739 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3740 _T("invalid list ctrl item index in SetItem") );
3742 size_t oldCurrent
= m_current
;
3743 size_t item
= (size_t)litem
; // safe because of the check above
3745 // do we need to change the focus?
3746 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3748 if ( state
& wxLIST_STATE_FOCUSED
)
3750 // don't do anything if this item is already focused
3751 if ( item
!= m_current
)
3753 ChangeCurrent(item
);
3755 if ( oldCurrent
!= (size_t)-1 )
3757 if ( IsSingleSel() )
3759 HighlightLine(oldCurrent
, FALSE
);
3762 RefreshLine(oldCurrent
);
3765 RefreshLine( m_current
);
3770 // don't do anything if this item is not focused
3771 if ( item
== m_current
)
3775 if ( IsSingleSel() )
3777 // we must unselect the old current item as well or we
3778 // might end up with more than one selected item in a
3779 // single selection control
3780 HighlightLine(oldCurrent
, FALSE
);
3783 RefreshLine( oldCurrent
);
3788 // do we need to change the selection state?
3789 if ( stateMask
& wxLIST_STATE_SELECTED
)
3791 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3793 if ( IsSingleSel() )
3797 // selecting the item also makes it the focused one in the
3799 if ( m_current
!= item
)
3801 ChangeCurrent(item
);
3803 if ( oldCurrent
!= (size_t)-1 )
3805 HighlightLine( oldCurrent
, FALSE
);
3806 RefreshLine( oldCurrent
);
3812 // only the current item may be selected anyhow
3813 if ( item
!= m_current
)
3818 if ( HighlightLine(item
, on
) )
3825 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3827 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3828 _T("invalid list ctrl item index in GetItemState()") );
3830 int ret
= wxLIST_STATE_DONTCARE
;
3832 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3834 if ( (size_t)item
== m_current
)
3835 ret
|= wxLIST_STATE_FOCUSED
;
3838 if ( stateMask
& wxLIST_STATE_SELECTED
)
3840 if ( IsHighlighted(item
) )
3841 ret
|= wxLIST_STATE_SELECTED
;
3847 void wxListMainWindow::GetItem( wxListItem
&item
)
3849 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3850 _T("invalid item index in GetItem") );
3852 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3853 line
->GetItem( item
.m_col
, item
);
3856 // ----------------------------------------------------------------------------
3858 // ----------------------------------------------------------------------------
3860 size_t wxListMainWindow::GetItemCount() const
3862 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3865 void wxListMainWindow::SetItemCount(long count
)
3867 m_selStore
.SetItemCount(count
);
3868 m_countVirt
= count
;
3870 ResetVisibleLinesRange();
3872 // scrollbars must be reset
3876 int wxListMainWindow::GetSelectedItemCount()
3878 // deal with the quick case first
3879 if ( IsSingleSel() )
3881 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3884 // virtual controls remmebers all its selections itself
3886 return m_selStore
.GetSelectedCount();
3888 // TODO: we probably should maintain the number of items selected even for
3889 // non virtual controls as enumerating all lines is really slow...
3890 size_t countSel
= 0;
3891 size_t count
= GetItemCount();
3892 for ( size_t line
= 0; line
< count
; line
++ )
3894 if ( GetLine(line
)->IsHighlighted() )
3901 // ----------------------------------------------------------------------------
3902 // item position/size
3903 // ----------------------------------------------------------------------------
3905 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3907 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3908 _T("invalid index in GetItemRect") );
3910 rect
= GetLineRect((size_t)index
);
3912 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3915 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3918 GetItemRect(item
, rect
);
3926 // ----------------------------------------------------------------------------
3927 // geometry calculation
3928 // ----------------------------------------------------------------------------
3930 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3932 wxClientDC
dc( this );
3933 dc
.SetFont( GetFont() );
3936 if ( HasFlag(wxLC_ICON
) )
3937 iconSpacing
= m_normal_spacing
;
3938 else if ( HasFlag(wxLC_SMALL_ICON
) )
3939 iconSpacing
= m_small_spacing
;
3943 // Note that we do not call GetClientSize() here but
3944 // GetSize() and substract the border size for sunken
3945 // borders manually. This is technically incorrect,
3946 // but we need to know the client area's size WITHOUT
3947 // scrollbars here. Since we don't know if there are
3948 // any scrollbars, we use GetSize() instead. Another
3949 // solution would be to call SetScrollbars() here to
3950 // remove the scrollbars and call GetClientSize() then,
3951 // but this might result in flicker and - worse - will
3952 // reset the scrollbars to 0 which is not good at all
3953 // if you resize a dialog/window, but don't want to
3954 // reset the window scrolling. RR.
3955 // Furthermore, we actually do NOT subtract the border
3956 // width as 2 pixels is just the extra space which we
3957 // need around the actual content in the window. Other-
3958 // wise the text would e.g. touch the upper border. RR.
3961 GetSize( &clientWidth
, &clientHeight
);
3963 if ( HasFlag(wxLC_REPORT
) )
3965 // all lines have the same height
3966 int lineHeight
= GetLineHeight();
3968 // scroll one line per step
3969 m_yScroll
= lineHeight
;
3971 size_t lineCount
= GetItemCount();
3972 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3974 m_linesPerPage
= clientHeight
/ lineHeight
;
3976 ResetVisibleLinesRange();
3978 SetScrollbars( m_xScroll
, m_yScroll
,
3979 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3980 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3981 GetScrollPos(wxHORIZONTAL
),
3982 GetScrollPos(wxVERTICAL
),
3987 // at first we try without any scrollbar. if the items don't
3988 // fit into the window, we recalculate after subtracting an
3989 // approximated 15 pt for the horizontal scrollbar
3991 int entireWidth
= 0;
3993 for (int tries
= 0; tries
< 2; tries
++)
3995 // We start with 4 for the border around all items
4000 // Now we have decided that the items do not fit into the
4001 // client area. Unfortunately, wxWindows sometimes thinks
4002 // that it does fit and therefore NO horizontal scrollbar
4003 // is inserted. This looks ugly, so we fudge here and make
4004 // the calculated width bigger than was actually has been
4005 // calculated. This ensures that wxScrolledWindows puts
4006 // a scrollbar at the bottom of its client area.
4007 entireWidth
+= SCROLL_UNIT_X
;
4010 // Start at 2,2 so the text does not touch the border
4015 int currentlyVisibleLines
= 0;
4017 size_t count
= GetItemCount();
4018 for (size_t i
= 0; i
< count
; i
++)
4020 currentlyVisibleLines
++;
4021 wxListLineData
*line
= GetLine(i
);
4022 line
->CalculateSize( &dc
, iconSpacing
);
4023 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4025 wxSize sizeLine
= GetLineSize(i
);
4027 if ( maxWidth
< sizeLine
.x
)
4028 maxWidth
= sizeLine
.x
;
4031 if (currentlyVisibleLines
> m_linesPerPage
)
4032 m_linesPerPage
= currentlyVisibleLines
;
4034 // Assume that the size of the next one is the same... (FIXME)
4035 if ( y
+ sizeLine
.y
>= clientHeight
)
4037 currentlyVisibleLines
= 0;
4040 entireWidth
+= maxWidth
+6;
4044 // We have reached the last item.
4045 if ( i
== count
- 1 )
4046 entireWidth
+= maxWidth
;
4048 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4050 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4052 currentlyVisibleLines
= 0;
4056 if ( i
== count
- 1 )
4057 tries
= 1; // Everything fits, no second try required.
4061 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4062 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4067 // FIXME: why should we call it from here?
4074 void wxListMainWindow::RefreshAll()
4079 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4080 if ( headerWin
&& headerWin
->m_dirty
)
4082 headerWin
->m_dirty
= FALSE
;
4083 headerWin
->Refresh();
4087 void wxListMainWindow::UpdateCurrent()
4089 if ( !HasCurrent() && !IsEmpty() )
4095 long wxListMainWindow::GetNextItem( long item
,
4096 int WXUNUSED(geometry
),
4100 max
= GetItemCount();
4101 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4102 _T("invalid listctrl index in GetNextItem()") );
4104 // notice that we start with the next item (or the first one if item == -1)
4105 // and this is intentional to allow writing a simple loop to iterate over
4106 // all selected items
4110 // this is not an error because the index was ok initially, just no
4121 size_t count
= GetItemCount();
4122 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4124 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4127 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4134 // ----------------------------------------------------------------------------
4136 // ----------------------------------------------------------------------------
4138 void wxListMainWindow::DeleteItem( long lindex
)
4140 size_t count
= GetItemCount();
4142 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4143 _T("invalid item index in DeleteItem") );
4145 size_t index
= (size_t)lindex
;
4147 // we don't need to adjust the index for the previous items
4148 if ( HasCurrent() && m_current
>= index
)
4150 // if the current item is being deleted, we want the next one to
4151 // become selected - unless there is no next one - so don't adjust
4152 // m_current in this case
4153 if ( m_current
!= index
|| m_current
== count
- 1 )
4159 if ( InReportView() )
4161 ResetVisibleLinesRange();
4168 m_selStore
.OnItemDelete(index
);
4172 m_lines
.RemoveAt( index
);
4175 // we need to refresh the (vert) scrollbar as the number of items changed
4178 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4180 RefreshAfter(index
);
4183 void wxListMainWindow::DeleteColumn( int col
)
4185 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4187 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4190 m_columns
.DeleteNode( node
);
4192 // invalidate it as it has to be recalculated
4196 void wxListMainWindow::DoDeleteAllItems()
4200 // nothing to do - in particular, don't send the event
4206 // to make the deletion of all items faster, we don't send the
4207 // notifications for each item deletion in this case but only one event
4208 // for all of them: this is compatible with wxMSW and documented in
4209 // DeleteAllItems() description
4211 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4212 event
.SetEventObject( GetParent() );
4213 GetParent()->GetEventHandler()->ProcessEvent( event
);
4222 if ( InReportView() )
4224 ResetVisibleLinesRange();
4230 void wxListMainWindow::DeleteAllItems()
4234 RecalculatePositions();
4237 void wxListMainWindow::DeleteEverything()
4244 // ----------------------------------------------------------------------------
4245 // scanning for an item
4246 // ----------------------------------------------------------------------------
4248 void wxListMainWindow::EnsureVisible( long index
)
4250 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4251 _T("invalid index in EnsureVisible") );
4253 // We have to call this here because the label in question might just have
4254 // been added and its position is not known yet
4257 RecalculatePositions(TRUE
/* no refresh */);
4260 MoveToItem((size_t)index
);
4263 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4270 size_t count
= GetItemCount();
4271 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4273 wxListLineData
*line
= GetLine(i
);
4274 if ( line
->GetText(0) == tmp
)
4281 long wxListMainWindow::FindItem(long start
, long data
)
4287 size_t count
= GetItemCount();
4288 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4290 wxListLineData
*line
= GetLine(i
);
4292 line
->GetItem( 0, item
);
4293 if (item
.m_data
== data
)
4300 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4302 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4304 size_t count
= GetItemCount();
4306 if ( HasFlag(wxLC_REPORT
) )
4308 size_t current
= y
/ GetLineHeight();
4309 if ( current
< count
)
4311 flags
= HitTestLine(current
, x
, y
);
4318 // TODO: optimize it too! this is less simple than for report view but
4319 // enumerating all items is still not a way to do it!!
4320 for ( size_t current
= 0; current
< count
; current
++ )
4322 flags
= HitTestLine(current
, x
, y
);
4331 // ----------------------------------------------------------------------------
4333 // ----------------------------------------------------------------------------
4335 void wxListMainWindow::InsertItem( wxListItem
&item
)
4337 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4339 size_t count
= GetItemCount();
4340 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4341 _T("invalid item index") );
4343 size_t id
= item
.m_itemId
;
4348 if ( HasFlag(wxLC_REPORT
) )
4350 else if ( HasFlag(wxLC_LIST
) )
4352 else if ( HasFlag(wxLC_ICON
) )
4354 else if ( HasFlag(wxLC_SMALL_ICON
) )
4355 mode
= wxLC_ICON
; // no typo
4358 wxFAIL_MSG( _T("unknown mode") );
4361 wxListLineData
*line
= new wxListLineData(this);
4363 line
->SetItem( 0, item
);
4365 m_lines
.Insert( line
, id
);
4368 RefreshLines(id
, GetItemCount() - 1);
4371 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4374 if ( HasFlag(wxLC_REPORT
) )
4376 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4377 item
.m_width
= GetTextLength( item
.m_text
);
4378 wxListHeaderData
*column
= new wxListHeaderData( item
);
4379 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4381 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4382 m_columns
.Insert( node
, column
);
4386 m_columns
.Append( column
);
4389 // invalidate it as it has to be recalculated
4394 // ----------------------------------------------------------------------------
4396 // ----------------------------------------------------------------------------
4398 wxListCtrlCompare list_ctrl_compare_func_2
;
4399 long list_ctrl_compare_data
;
4401 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4403 wxListLineData
*line1
= *arg1
;
4404 wxListLineData
*line2
= *arg2
;
4406 line1
->GetItem( 0, item
);
4407 long data1
= item
.m_data
;
4408 line2
->GetItem( 0, item
);
4409 long data2
= item
.m_data
;
4410 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4413 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4415 list_ctrl_compare_func_2
= fn
;
4416 list_ctrl_compare_data
= data
;
4417 m_lines
.Sort( list_ctrl_compare_func_1
);
4421 // ----------------------------------------------------------------------------
4423 // ----------------------------------------------------------------------------
4425 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4427 // update our idea of which lines are shown when we redraw the window the
4429 ResetVisibleLinesRange();
4432 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4433 wxScrolledWindow::OnScroll(event
);
4435 HandleOnScroll( event
);
4438 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4440 wxListCtrl
* lc
= GetListCtrl();
4441 wxCHECK_RET( lc
, _T("no listctrl window?") );
4443 lc
->m_headerWin
->Refresh();
4444 lc
->m_headerWin
->Update();
4448 int wxListMainWindow::GetCountPerPage() const
4450 if ( !m_linesPerPage
)
4452 wxConstCast(this, wxListMainWindow
)->
4453 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4456 return m_linesPerPage
;
4459 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4461 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4463 if ( m_lineFrom
== (size_t)-1 )
4465 size_t count
= GetItemCount();
4468 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4470 // this may happen if SetScrollbars() hadn't been called yet
4471 if ( m_lineFrom
>= count
)
4472 m_lineFrom
= count
- 1;
4474 // we redraw one extra line but this is needed to make the redrawing
4475 // logic work when there is a fractional number of lines on screen
4476 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4477 if ( m_lineTo
>= count
)
4478 m_lineTo
= count
- 1;
4480 else // empty control
4483 m_lineTo
= (size_t)-1;
4487 wxASSERT_MSG( IsEmpty() ||
4488 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4489 _T("GetVisibleLinesRange() returns incorrect result") );
4497 // -------------------------------------------------------------------------------------
4499 // -------------------------------------------------------------------------------------
4501 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4503 // -------------------------------------------------------------------------------------
4505 // -------------------------------------------------------------------------------------
4507 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4508 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4510 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4512 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4513 EVT_SIZE(wxListCtrl::OnSize
)
4514 EVT_IDLE(wxListCtrl::OnIdle
)
4517 wxListCtrl::wxListCtrl()
4519 m_imageListNormal
= (wxImageList
*) NULL
;
4520 m_imageListSmall
= (wxImageList
*) NULL
;
4521 m_imageListState
= (wxImageList
*) NULL
;
4523 m_ownsImageListNormal
=
4524 m_ownsImageListSmall
=
4525 m_ownsImageListState
= FALSE
;
4527 m_mainWin
= (wxListMainWindow
*) NULL
;
4528 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4531 wxListCtrl::~wxListCtrl()
4533 if (m_ownsImageListNormal
)
4534 delete m_imageListNormal
;
4535 if (m_ownsImageListSmall
)
4536 delete m_imageListSmall
;
4537 if (m_ownsImageListState
)
4538 delete m_imageListState
;
4541 void wxListCtrl::CreateHeaderWindow()
4543 m_headerWin
= new wxListHeaderWindow
4545 this, -1, m_mainWin
,
4547 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4552 bool wxListCtrl::Create(wxWindow
*parent
,
4557 const wxValidator
&validator
,
4558 const wxString
&name
)
4562 m_imageListState
= (wxImageList
*) NULL
;
4563 m_ownsImageListNormal
=
4564 m_ownsImageListSmall
=
4565 m_ownsImageListState
= FALSE
;
4567 m_mainWin
= (wxListMainWindow
*) NULL
;
4568 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4570 if ( !(style
& wxLC_MASK_TYPE
) )
4572 style
= style
| wxLC_LIST
;
4575 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4578 // don't create the inner window with the border
4579 style
&= ~wxSUNKEN_BORDER
;
4581 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4583 if ( HasFlag(wxLC_REPORT
) )
4585 CreateHeaderWindow();
4587 if ( HasFlag(wxLC_NO_HEADER
) )
4589 // VZ: why do we create it at all then?
4590 m_headerWin
->Show( FALSE
);
4597 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4599 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4600 _T("wxLC_VIRTUAL can't be [un]set") );
4602 long flag
= GetWindowStyle();
4606 if (style
& wxLC_MASK_TYPE
)
4607 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4608 if (style
& wxLC_MASK_ALIGN
)
4609 flag
&= ~wxLC_MASK_ALIGN
;
4610 if (style
& wxLC_MASK_SORT
)
4611 flag
&= ~wxLC_MASK_SORT
;
4623 SetWindowStyleFlag( flag
);
4626 void wxListCtrl::SetWindowStyleFlag( long flag
)
4630 m_mainWin
->DeleteEverything();
4632 // has the header visibility changed?
4633 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4634 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4636 if ( hasHeader
!= willHaveHeader
)
4643 // don't delete, just hide, as we can reuse it later
4644 m_headerWin
->Show(FALSE
);
4646 //else: nothing to do
4648 else // must show header
4652 CreateHeaderWindow();
4654 else // already have it, just show
4656 m_headerWin
->Show( TRUE
);
4660 ResizeReportView(willHaveHeader
);
4664 wxWindow::SetWindowStyleFlag( flag
);
4667 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4669 m_mainWin
->GetColumn( col
, item
);
4673 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4675 m_mainWin
->SetColumn( col
, item
);
4679 int wxListCtrl::GetColumnWidth( int col
) const
4681 return m_mainWin
->GetColumnWidth( col
);
4684 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4686 m_mainWin
->SetColumnWidth( col
, width
);
4690 int wxListCtrl::GetCountPerPage() const
4692 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4695 bool wxListCtrl::GetItem( wxListItem
&info
) const
4697 m_mainWin
->GetItem( info
);
4701 bool wxListCtrl::SetItem( wxListItem
&info
)
4703 m_mainWin
->SetItem( info
);
4707 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4710 info
.m_text
= label
;
4711 info
.m_mask
= wxLIST_MASK_TEXT
;
4712 info
.m_itemId
= index
;
4716 info
.m_image
= imageId
;
4717 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4719 m_mainWin
->SetItem(info
);
4723 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4725 return m_mainWin
->GetItemState( item
, stateMask
);
4728 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4730 m_mainWin
->SetItemState( item
, state
, stateMask
);
4734 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4737 info
.m_image
= image
;
4738 info
.m_mask
= wxLIST_MASK_IMAGE
;
4739 info
.m_itemId
= item
;
4740 m_mainWin
->SetItem( info
);
4744 wxString
wxListCtrl::GetItemText( long item
) const
4747 info
.m_itemId
= item
;
4748 m_mainWin
->GetItem( info
);
4752 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4755 info
.m_mask
= wxLIST_MASK_TEXT
;
4756 info
.m_itemId
= item
;
4758 m_mainWin
->SetItem( info
);
4761 long wxListCtrl::GetItemData( long item
) const
4764 info
.m_itemId
= item
;
4765 m_mainWin
->GetItem( info
);
4769 bool wxListCtrl::SetItemData( long item
, long data
)
4772 info
.m_mask
= wxLIST_MASK_DATA
;
4773 info
.m_itemId
= item
;
4775 m_mainWin
->SetItem( info
);
4779 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4781 m_mainWin
->GetItemRect( item
, rect
);
4785 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4787 m_mainWin
->GetItemPosition( item
, pos
);
4791 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4796 int wxListCtrl::GetItemCount() const
4798 return m_mainWin
->GetItemCount();
4801 int wxListCtrl::GetColumnCount() const
4803 return m_mainWin
->GetColumnCount();
4806 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4808 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4811 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4813 return m_mainWin
->GetItemSpacing( isSmall
);
4816 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4819 info
.m_itemId
= item
;
4820 info
.SetTextColour( col
);
4821 m_mainWin
->SetItem( info
);
4824 wxColour
wxListCtrl::GetItemTextColour( long item
) const
4827 info
.m_itemId
= item
;
4828 m_mainWin
->GetItem( info
);
4829 return info
.GetTextColour();
4832 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4835 info
.m_itemId
= item
;
4836 info
.SetBackgroundColour( col
);
4837 m_mainWin
->SetItem( info
);
4840 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
4843 info
.m_itemId
= item
;
4844 m_mainWin
->GetItem( info
);
4845 return info
.GetBackgroundColour();
4848 int wxListCtrl::GetSelectedItemCount() const
4850 return m_mainWin
->GetSelectedItemCount();
4853 wxColour
wxListCtrl::GetTextColour() const
4855 return GetForegroundColour();
4858 void wxListCtrl::SetTextColour(const wxColour
& col
)
4860 SetForegroundColour(col
);
4863 long wxListCtrl::GetTopItem() const
4868 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4870 return m_mainWin
->GetNextItem( item
, geom
, state
);
4873 wxImageList
*wxListCtrl::GetImageList(int which
) const
4875 if (which
== wxIMAGE_LIST_NORMAL
)
4877 return m_imageListNormal
;
4879 else if (which
== wxIMAGE_LIST_SMALL
)
4881 return m_imageListSmall
;
4883 else if (which
== wxIMAGE_LIST_STATE
)
4885 return m_imageListState
;
4887 return (wxImageList
*) NULL
;
4890 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4892 if ( which
== wxIMAGE_LIST_NORMAL
)
4894 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4895 m_imageListNormal
= imageList
;
4896 m_ownsImageListNormal
= FALSE
;
4898 else if ( which
== wxIMAGE_LIST_SMALL
)
4900 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4901 m_imageListSmall
= imageList
;
4902 m_ownsImageListSmall
= FALSE
;
4904 else if ( which
== wxIMAGE_LIST_STATE
)
4906 if (m_ownsImageListState
) delete m_imageListState
;
4907 m_imageListState
= imageList
;
4908 m_ownsImageListState
= FALSE
;
4911 m_mainWin
->SetImageList( imageList
, which
);
4914 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4916 SetImageList(imageList
, which
);
4917 if ( which
== wxIMAGE_LIST_NORMAL
)
4918 m_ownsImageListNormal
= TRUE
;
4919 else if ( which
== wxIMAGE_LIST_SMALL
)
4920 m_ownsImageListSmall
= TRUE
;
4921 else if ( which
== wxIMAGE_LIST_STATE
)
4922 m_ownsImageListState
= TRUE
;
4925 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4930 bool wxListCtrl::DeleteItem( long item
)
4932 m_mainWin
->DeleteItem( item
);
4936 bool wxListCtrl::DeleteAllItems()
4938 m_mainWin
->DeleteAllItems();
4942 bool wxListCtrl::DeleteAllColumns()
4944 size_t count
= m_mainWin
->m_columns
.GetCount();
4945 for ( size_t n
= 0; n
< count
; n
++ )
4951 void wxListCtrl::ClearAll()
4953 m_mainWin
->DeleteEverything();
4956 bool wxListCtrl::DeleteColumn( int col
)
4958 m_mainWin
->DeleteColumn( col
);
4960 // if we don't have the header any longer, we need to relayout the window
4961 if ( !GetColumnCount() )
4963 ResizeReportView(FALSE
/* no header */);
4969 void wxListCtrl::Edit( long item
)
4971 m_mainWin
->EditLabel( item
);
4974 bool wxListCtrl::EnsureVisible( long item
)
4976 m_mainWin
->EnsureVisible( item
);
4980 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4982 return m_mainWin
->FindItem( start
, str
, partial
);
4985 long wxListCtrl::FindItem( long start
, long data
)
4987 return m_mainWin
->FindItem( start
, data
);
4990 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4991 int WXUNUSED(direction
))
4996 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4998 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5001 long wxListCtrl::InsertItem( wxListItem
& info
)
5003 m_mainWin
->InsertItem( info
);
5004 return info
.m_itemId
;
5007 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
5010 info
.m_text
= label
;
5011 info
.m_mask
= wxLIST_MASK_TEXT
;
5012 info
.m_itemId
= index
;
5013 return InsertItem( info
);
5016 long wxListCtrl::InsertItem( long index
, int imageIndex
)
5019 info
.m_mask
= wxLIST_MASK_IMAGE
;
5020 info
.m_image
= imageIndex
;
5021 info
.m_itemId
= index
;
5022 return InsertItem( info
);
5025 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5028 info
.m_text
= label
;
5029 info
.m_image
= imageIndex
;
5030 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5031 info
.m_itemId
= index
;
5032 return InsertItem( info
);
5035 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
5037 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5039 m_mainWin
->InsertColumn( col
, item
);
5041 // if we hadn't had header before and have it now we need to relayout the
5043 if ( GetColumnCount() == 1 )
5045 ResizeReportView(TRUE
/* have header */);
5048 m_headerWin
->Refresh();
5053 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
5054 int format
, int width
)
5057 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5058 item
.m_text
= heading
;
5061 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5062 item
.m_width
= width
;
5064 item
.m_format
= format
;
5066 return InsertColumn( col
, item
);
5069 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5075 // fn is a function which takes 3 long arguments: item1, item2, data.
5076 // item1 is the long data associated with a first item (NOT the index).
5077 // item2 is the long data associated with a second item (NOT the index).
5078 // data is the same value as passed to SortItems.
5079 // The return value is a negative number if the first item should precede the second
5080 // item, a positive number of the second item should precede the first,
5081 // or zero if the two items are equivalent.
5082 // data is arbitrary data to be passed to the sort function.
5084 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5086 m_mainWin
->SortItems( fn
, data
);
5090 // ----------------------------------------------------------------------------
5092 // ----------------------------------------------------------------------------
5094 void wxListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5099 ResizeReportView(m_mainWin
->HasHeader());
5101 m_mainWin
->RecalculatePositions();
5104 void wxListCtrl::ResizeReportView(bool showHeader
)
5107 GetClientSize( &cw
, &ch
);
5111 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5112 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5114 else // no header window
5116 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5120 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5124 // do it only if needed
5125 if ( !m_mainWin
->m_dirty
)
5128 m_mainWin
->RecalculatePositions();
5131 // ----------------------------------------------------------------------------
5133 // ----------------------------------------------------------------------------
5135 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5139 m_mainWin
->SetBackgroundColour( colour
);
5140 m_mainWin
->m_dirty
= TRUE
;
5146 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5148 if ( !wxWindow::SetForegroundColour( colour
) )
5153 m_mainWin
->SetForegroundColour( colour
);
5154 m_mainWin
->m_dirty
= TRUE
;
5159 m_headerWin
->SetForegroundColour( colour
);
5165 bool wxListCtrl::SetFont( const wxFont
&font
)
5167 if ( !wxWindow::SetFont( font
) )
5172 m_mainWin
->SetFont( font
);
5173 m_mainWin
->m_dirty
= TRUE
;
5178 m_headerWin
->SetFont( font
);
5184 // ----------------------------------------------------------------------------
5185 // methods forwarded to m_mainWin
5186 // ----------------------------------------------------------------------------
5188 #if wxUSE_DRAG_AND_DROP
5190 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5192 m_mainWin
->SetDropTarget( dropTarget
);
5195 wxDropTarget
*wxListCtrl::GetDropTarget() const
5197 return m_mainWin
->GetDropTarget();
5200 #endif // wxUSE_DRAG_AND_DROP
5202 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5204 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5207 wxColour
wxListCtrl::GetBackgroundColour() const
5209 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5212 wxColour
wxListCtrl::GetForegroundColour() const
5214 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5217 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5220 return m_mainWin
->PopupMenu( menu
, x
, y
);
5223 #endif // wxUSE_MENUS
5226 void wxListCtrl::SetFocus()
5228 /* The test in window.cpp fails as we are a composite
5229 window, so it checks against "this", but not m_mainWin. */
5230 if ( FindFocus() != this )
5231 m_mainWin
->SetFocus();
5234 // ----------------------------------------------------------------------------
5235 // virtual list control support
5236 // ----------------------------------------------------------------------------
5238 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5240 // this is a pure virtual function, in fact - which is not really pure
5241 // because the controls which are not virtual don't need to implement it
5242 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
5244 return wxEmptyString
;
5247 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5250 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
5255 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5257 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5258 _T("invalid item index in OnGetItemAttr()") );
5260 // no attributes by default
5264 void wxListCtrl::SetItemCount(long count
)
5266 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5268 m_mainWin
->SetItemCount(count
);
5271 void wxListCtrl::RefreshItem(long item
)
5273 m_mainWin
->RefreshLine(item
);
5276 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5278 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5281 void wxListCtrl::Freeze()
5283 m_mainWin
->Freeze();
5286 void wxListCtrl::Thaw()
5291 #endif // wxUSE_LISTCTRL