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 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #if defined(__WXGTK__)
73 #include "wx/gtk/win_gtk.h"
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // the height of the header window (FIXME: should depend on its font!)
108 static const int HEADER_HEIGHT
= 23;
110 // the scrollbar units
111 static const int SCROLL_UNIT_X
= 15;
112 static const int SCROLL_UNIT_Y
= 15;
114 // the spacing between the lines (in report mode)
115 static const int LINE_SPACING
= 0;
117 // extra margins around the text label
118 static const int EXTRA_WIDTH
= 3;
119 static const int EXTRA_HEIGHT
= 4;
121 // offset for the header window
122 static const int HEADER_OFFSET_X
= 1;
123 static const int HEADER_OFFSET_Y
= 1;
125 // when autosizing the columns, add some slack
126 static const int AUTOSIZE_COL_MARGIN
= 10;
128 // default and minimal widths for the header columns
129 static const int WIDTH_COL_DEFAULT
= 80;
130 static const int WIDTH_COL_MIN
= 10;
132 // the space between the image and the text in the report mode
133 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
145 WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(size_t, wxIndexArray
);
147 // this class is used to store the selected items in the virtual list control
148 // (but it is not tied to list control and so can be used with other controls
149 // such as wxListBox in wxUniv)
151 // the idea is to make it really smart later (i.e. store the selections as an
152 // array of ranes + individual items) but, as I don't have time to do it now
153 // (this would require writing code to merge/break ranges and much more) keep
154 // it simple but define a clean interface to it which allows it to be made
156 class WXDLLEXPORT wxSelectionStore
159 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
161 // set the total number of items we handle
162 void SetItemCount(size_t count
) { m_count
= count
; }
164 // special case of SetItemCount(0)
165 void Clear() { m_itemsSel
.Clear(); m_count
= 0; m_defaultState
= FALSE
; }
167 // must be called when a new item is inserted/added
168 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
170 // must be called when an item is deleted
171 void OnItemDelete(size_t item
);
173 // select one item, use SelectRange() insted if possible!
175 // returns true if the items selection really changed
176 bool SelectItem(size_t item
, bool select
= TRUE
);
178 // select the range of items
180 // return true and fill the itemsChanged array with the indices of items
181 // which have changed state if "few" of them did, otherwise return false
182 // (meaning that too many items changed state to bother counting them
184 bool SelectRange(size_t itemFrom
, size_t itemTo
,
186 wxArrayInt
*itemsChanged
= NULL
);
188 // return true if the given item is selected
189 bool IsSelected(size_t item
) const;
191 // return the total number of selected items
192 size_t GetSelectedCount() const
194 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
195 : m_itemsSel
.GetCount();
200 void Init() { m_defaultState
= FALSE
; }
202 // the total number of items we handle
205 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
206 // there are more selected items than non selected ones - this allows to
207 // handle selection of all items efficiently
210 // the array of items whose selection state is different from default
211 wxIndexArray m_itemsSel
;
213 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
216 //-----------------------------------------------------------------------------
217 // wxListItemData (internal)
218 //-----------------------------------------------------------------------------
220 class WXDLLEXPORT wxListItemData
223 wxListItemData(wxListMainWindow
*owner
);
226 void SetItem( const wxListItem
&info
);
227 void SetImage( int image
) { m_image
= image
; }
228 void SetData( long data
) { m_data
= data
; }
229 void SetPosition( int x
, int y
);
230 void SetSize( int width
, int height
);
232 bool HasText() const { return !m_text
.empty(); }
233 const wxString
& GetText() const { return m_text
; }
234 void SetText(const wxString
& text
) { m_text
= text
; }
236 // we can't use empty string for measuring the string width/height, so
237 // always return something
238 wxString
GetTextForMeasuring() const
240 wxString s
= GetText();
247 bool IsHit( int x
, int y
) const;
251 int GetWidth() const;
252 int GetHeight() const;
254 int GetImage() const { return m_image
; }
255 bool HasImage() const { return GetImage() != -1; }
257 void GetItem( wxListItem
&info
) const;
259 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
260 wxListItemAttr
*GetAttr() const { return m_attr
; }
263 // the item image or -1
266 // user data associated with the item
269 // the item coordinates are not used in report mode, instead this pointer
270 // is NULL and the owner window is used to retrieve the item position and
274 // the list ctrl we are in
275 wxListMainWindow
*m_owner
;
277 // custom attributes or NULL
278 wxListItemAttr
*m_attr
;
281 // common part of all ctors
287 //-----------------------------------------------------------------------------
288 // wxListHeaderData (internal)
289 //-----------------------------------------------------------------------------
291 class WXDLLEXPORT wxListHeaderData
: public wxObject
295 wxListHeaderData( const wxListItem
&info
);
296 void SetItem( const wxListItem
&item
);
297 void SetPosition( int x
, int y
);
298 void SetWidth( int w
);
299 void SetFormat( int format
);
300 void SetHeight( int h
);
301 bool HasImage() const;
303 bool HasText() const { return !m_text
.empty(); }
304 const wxString
& GetText() const { return m_text
; }
305 void SetText(const wxString
& text
) { m_text
= text
; }
307 void GetItem( wxListItem
&item
);
309 bool IsHit( int x
, int y
) const;
310 int GetImage() const;
311 int GetWidth() const;
312 int GetFormat() const;
328 //-----------------------------------------------------------------------------
329 // wxListLineData (internal)
330 //-----------------------------------------------------------------------------
332 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
333 #include "wx/listimpl.cpp"
334 WX_DEFINE_LIST(wxListItemDataList
);
336 class WXDLLEXPORT wxListLineData
339 // the list of subitems: only may have more than one item in report mode
340 wxListItemDataList m_items
;
342 // this is not used in report view
354 // the part to be highlighted
355 wxRect m_rectHighlight
;
358 // is this item selected? [NB: not used in virtual mode]
361 // back pointer to the list ctrl
362 wxListMainWindow
*m_owner
;
365 wxListLineData(wxListMainWindow
*owner
);
367 ~wxListLineData() { delete m_gi
; }
369 // are we in report mode?
370 inline bool InReportView() const;
372 // are we in virtual report mode?
373 inline bool IsVirtual() const;
375 // these 2 methods shouldn't be called for report view controls, in that
376 // case we determine our position/size ourselves
378 // calculate the size of the line
379 void CalculateSize( wxDC
*dc
, int spacing
);
381 // remember the position this line appears at
382 void SetPosition( int x
, int y
, int window_width
, int spacing
);
386 void SetImage( int image
) { SetImage(0, image
); }
387 int GetImage() const { return GetImage(0); }
388 bool HasImage() const { return GetImage() != -1; }
389 bool HasText() const { return !GetText(0).empty(); }
391 void SetItem( int index
, const wxListItem
&info
);
392 void GetItem( int index
, wxListItem
&info
);
394 wxString
GetText(int index
) const;
395 void SetText( int index
, const wxString s
);
397 wxListItemAttr
*GetAttr() const;
398 void SetAttr(wxListItemAttr
*attr
);
400 // return true if the highlighting really changed
401 bool Highlight( bool on
);
403 void ReverseHighlight();
405 bool IsHighlighted() const
407 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
409 return m_highlighted
;
412 // draw the line on the given DC in icon/list mode
413 void Draw( wxDC
*dc
);
415 // the same in report mode
416 void DrawInReportMode( wxDC
*dc
,
418 const wxRect
& rectHL
,
422 // set the line to contain num items (only can be > 1 in report mode)
423 void InitItems( int num
);
425 // get the mode (i.e. style) of the list control
426 inline int GetMode() const;
428 // prepare the DC for drawing with these item's attributes, return true if
429 // we need to draw the items background to highlight it, false otherwise
430 bool SetAttributes(wxDC
*dc
,
431 const wxListItemAttr
*attr
,
434 // draw the text on the DC with the correct justification; also add an
435 // ellipsis if the text is too large to fit in the current width
436 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
438 // these are only used by GetImage/SetImage above, we don't support images
439 // with subitems at the public API level yet
440 void SetImage( int index
, int image
);
441 int GetImage( int index
) const;
444 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
445 #include "wx/arrimpl.cpp"
446 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
448 //-----------------------------------------------------------------------------
449 // wxListHeaderWindow (internal)
450 //-----------------------------------------------------------------------------
452 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
455 wxListMainWindow
*m_owner
;
456 wxCursor
*m_currentCursor
;
457 wxCursor
*m_resizeCursor
;
460 // column being resized or -1
463 // divider line position in logical (unscrolled) coords
466 // minimal position beyond which the divider line can't be dragged in
471 wxListHeaderWindow();
473 wxListHeaderWindow( wxWindow
*win
,
475 wxListMainWindow
*owner
,
476 const wxPoint
&pos
= wxDefaultPosition
,
477 const wxSize
&size
= wxDefaultSize
,
479 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
481 virtual ~wxListHeaderWindow();
483 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
485 void AdjustDC(wxDC
& dc
);
487 void OnPaint( wxPaintEvent
&event
);
488 void OnMouse( wxMouseEvent
&event
);
489 void OnSetFocus( wxFocusEvent
&event
);
495 // common part of all ctors
498 // generate and process the list event of the given type, return true if
499 // it wasn't vetoed, i.e. if we should proceed
500 bool SendListEvent(wxEventType type
, wxPoint pos
);
502 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
503 DECLARE_EVENT_TABLE()
506 //-----------------------------------------------------------------------------
507 // wxListRenameTimer (internal)
508 //-----------------------------------------------------------------------------
510 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
513 wxListMainWindow
*m_owner
;
516 wxListRenameTimer( wxListMainWindow
*owner
);
520 //-----------------------------------------------------------------------------
521 // wxListTextCtrl (internal)
522 //-----------------------------------------------------------------------------
524 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
527 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
530 void OnChar( wxKeyEvent
&event
);
531 void OnKeyUp( wxKeyEvent
&event
);
532 void OnKillFocus( wxFocusEvent
&event
);
534 bool AcceptChanges();
538 wxListMainWindow
*m_owner
;
539 wxString m_startValue
;
543 DECLARE_EVENT_TABLE()
546 //-----------------------------------------------------------------------------
547 // wxListMainWindow (internal)
548 //-----------------------------------------------------------------------------
550 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
551 #include "wx/listimpl.cpp"
552 WX_DEFINE_LIST(wxListHeaderDataList
);
554 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
558 wxListMainWindow( wxWindow
*parent
,
560 const wxPoint
& pos
= wxDefaultPosition
,
561 const wxSize
& size
= wxDefaultSize
,
563 const wxString
&name
= _T("listctrlmainwindow") );
565 virtual ~wxListMainWindow();
567 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
569 // return true if this is a virtual list control
570 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
572 // return true if the control is in report mode
573 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
575 // return true if we are in single selection mode, false if multi sel
576 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
578 // do we have a header window?
579 bool HasHeader() const
580 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
582 void HighlightAll( bool on
);
584 // all these functions only do something if the line is currently visible
586 // change the line "selected" state, return TRUE if it really changed
587 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
589 // as HighlightLine() but do it for the range of lines: this is incredibly
590 // more efficient for virtual list controls!
592 // NB: unlike HighlightLine() this one does refresh the lines on screen
593 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
595 // toggle the line state and refresh it
596 void ReverseHighlight( size_t line
)
597 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
599 // return true if the line is highlighted
600 bool IsHighlighted(size_t line
) const;
602 // refresh one or several lines at once
603 void RefreshLine( size_t line
);
604 void RefreshLines( size_t lineFrom
, size_t lineTo
);
606 // refresh all selected items
607 void RefreshSelected();
609 // refresh all lines below the given one: the difference with
610 // RefreshLines() is that the index here might not be a valid one (happens
611 // when the last line is deleted)
612 void RefreshAfter( size_t lineFrom
);
614 // the methods which are forwarded to wxListLineData itself in list/icon
615 // modes but are here because the lines don't store their positions in the
618 // get the bound rect for the entire line
619 wxRect
GetLineRect(size_t line
) const;
621 // get the bound rect of the label
622 wxRect
GetLineLabelRect(size_t line
) const;
624 // get the bound rect of the items icon (only may be called if we do have
626 wxRect
GetLineIconRect(size_t line
) const;
628 // get the rect to be highlighted when the item has focus
629 wxRect
GetLineHighlightRect(size_t line
) const;
631 // get the size of the total line rect
632 wxSize
GetLineSize(size_t line
) const
633 { return GetLineRect(line
).GetSize(); }
635 // return the hit code for the corresponding position (in this line)
636 long HitTestLine(size_t line
, int x
, int y
) const;
638 // bring the selected item into view, scrolling to it if necessary
639 void MoveToItem(size_t item
);
641 // bring the current item into view
642 void MoveToFocus() { MoveToItem(m_current
); }
644 // start editing the label of the given item
645 void EditLabel( long item
);
647 // suspend/resume redrawing the control
653 void OnRenameTimer();
654 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
656 void OnMouse( wxMouseEvent
&event
);
658 // called to switch the selection from the current item to newCurrent,
659 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
661 void OnChar( wxKeyEvent
&event
);
662 void OnKeyDown( wxKeyEvent
&event
);
663 void OnSetFocus( wxFocusEvent
&event
);
664 void OnKillFocus( wxFocusEvent
&event
);
665 void OnScroll(wxScrollWinEvent
& event
) ;
667 void OnPaint( wxPaintEvent
&event
);
669 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
670 void GetImageSize( int index
, int &width
, int &height
) const;
671 int GetTextLength( const wxString
&s
) const;
673 void SetImageList( wxImageListType
*imageList
, int which
);
674 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
675 int GetItemSpacing( bool isSmall
= FALSE
);
677 void SetColumn( int col
, wxListItem
&item
);
678 void SetColumnWidth( int col
, int width
);
679 void GetColumn( int col
, wxListItem
&item
) const;
680 int GetColumnWidth( int col
) const;
681 int GetColumnCount() const { return m_columns
.GetCount(); }
683 // returns the sum of the heights of all columns
684 int GetHeaderWidth() const;
686 int GetCountPerPage() const;
688 void SetItem( wxListItem
&item
);
689 void GetItem( wxListItem
&item
) const;
690 void SetItemState( long item
, long state
, long stateMask
);
691 int GetItemState( long item
, long stateMask
) const;
692 void GetItemRect( long index
, wxRect
&rect
) const;
693 bool GetItemPosition( long item
, wxPoint
& pos
) const;
694 int GetSelectedItemCount() const;
696 wxString
GetItemText(long item
) const
699 info
.m_itemId
= item
;
704 void SetItemText(long item
, const wxString
& value
)
707 info
.m_mask
= wxLIST_MASK_TEXT
;
708 info
.m_itemId
= item
;
713 // set the scrollbars and update the positions of the items
714 void RecalculatePositions(bool noRefresh
= FALSE
);
716 // refresh the window and the header
719 long GetNextItem( long item
, int geometry
, int state
) const;
720 void DeleteItem( long index
);
721 void DeleteAllItems();
722 void DeleteColumn( int col
);
723 void DeleteEverything();
724 void EnsureVisible( long index
);
725 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
726 long FindItem( long start
, long data
);
727 long HitTest( int x
, int y
, int &flags
);
728 void InsertItem( wxListItem
&item
);
729 void InsertColumn( long col
, wxListItem
&item
);
730 void SortItems( wxListCtrlCompare fn
, long data
);
732 size_t GetItemCount() const;
733 bool IsEmpty() const { return GetItemCount() == 0; }
734 void SetItemCount(long count
);
736 // change the current (== focused) item, send a notification event
737 void ChangeCurrent(size_t current
);
738 void ResetCurrent() { ChangeCurrent((size_t)-1); }
739 bool HasCurrent() const { return m_current
!= (size_t)-1; }
741 // send out a wxListEvent
742 void SendNotify( size_t line
,
744 wxPoint point
= wxDefaultPosition
);
746 // override base class virtual to reset m_lineHeight when the font changes
747 virtual bool SetFont(const wxFont
& font
)
749 if ( !wxScrolledWindow::SetFont(font
) )
757 // these are for wxListLineData usage only
759 // get the backpointer to the list ctrl
760 wxGenericListCtrl
*GetListCtrl() const
762 return wxStaticCast(GetParent(), wxGenericListCtrl
);
765 // get the height of all lines (assuming they all do have the same height)
766 wxCoord
GetLineHeight() const;
768 // get the y position of the given line (only for report view)
769 wxCoord
GetLineY(size_t line
) const;
771 // get the brush to use for the item highlighting
772 wxBrush
*GetHighlightBrush() const
774 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
778 // the array of all line objects for a non virtual list control (for the
779 // virtual list control we only ever use m_lines[0])
780 wxListLineDataArray m_lines
;
782 // the list of column objects
783 wxListHeaderDataList m_columns
;
785 // currently focused item or -1
788 // the number of lines per page
791 // this flag is set when something which should result in the window
792 // redrawing happens (i.e. an item was added or deleted, or its appearance
793 // changed) and OnPaint() doesn't redraw the window while it is set which
794 // allows to minimize the number of repaintings when a lot of items are
795 // being added. The real repainting occurs only after the next OnIdle()
799 wxColour
*m_highlightColour
;
802 wxImageListType
*m_small_image_list
;
803 wxImageListType
*m_normal_image_list
;
805 int m_normal_spacing
;
809 wxTimer
*m_renameTimer
;
814 // for double click logic
815 size_t m_lineLastClicked
,
816 m_lineBeforeLastClicked
;
819 // the total count of items in a virtual list control
822 // the object maintaining the items selection state, only used in virtual
824 wxSelectionStore m_selStore
;
826 // common part of all ctors
829 // intiialize m_[xy]Scroll
830 void InitScrolling();
832 // get the line data for the given index
833 wxListLineData
*GetLine(size_t n
) const
835 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
839 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
847 // get a dummy line which can be used for geometry calculations and such:
848 // you must use GetLine() if you want to really draw the line
849 wxListLineData
*GetDummyLine() const;
851 // cache the line data of the n-th line in m_lines[0]
852 void CacheLineData(size_t line
);
854 // get the range of visible lines
855 void GetVisibleLinesRange(size_t *from
, size_t *to
);
857 // force us to recalculate the range of visible lines
858 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
860 // get the colour to be used for drawing the rules
861 wxColour
GetRuleColour() const
866 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
871 // initialize the current item if needed
872 void UpdateCurrent();
874 // delete all items but don't refresh: called from dtor
875 void DoDeleteAllItems();
877 // the height of one line using the current font
878 wxCoord m_lineHeight
;
880 // the total header width or 0 if not calculated yet
881 wxCoord m_headerWidth
;
883 // the first and last lines being shown on screen right now (inclusive),
884 // both may be -1 if they must be calculated so never access them directly:
885 // use GetVisibleLinesRange() above instead
889 // the brushes to use for item highlighting when we do/don't have focus
890 wxBrush
*m_highlightBrush
,
891 *m_highlightUnfocusedBrush
;
893 // if this is > 0, the control is frozen and doesn't redraw itself
894 size_t m_freezeCount
;
896 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
897 DECLARE_EVENT_TABLE()
899 friend class wxGenericListCtrl
;
902 // ============================================================================
904 // ============================================================================
906 // ----------------------------------------------------------------------------
908 // ----------------------------------------------------------------------------
910 bool wxSelectionStore::IsSelected(size_t item
) const
912 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
914 // if the default state is to be selected, being in m_itemsSel means that
915 // the item is not selected, so we have to inverse the logic
916 return m_defaultState
? !isSel
: isSel
;
919 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
921 // search for the item ourselves as like this we get the index where to
922 // insert it later if needed, so we do only one search in the array instead
923 // of two (adding item to a sorted array requires a search)
924 size_t index
= m_itemsSel
.IndexForInsert(item
);
925 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
927 if ( select
!= m_defaultState
)
931 m_itemsSel
.AddAt(item
, index
);
936 else // reset to default state
940 m_itemsSel
.RemoveAt(index
);
948 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
950 wxArrayInt
*itemsChanged
)
952 // 100 is hardcoded but it shouldn't matter much: the important thing is
953 // that we don't refresh everything when really few (e.g. 1 or 2) items
955 static const size_t MANY_ITEMS
= 100;
957 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
959 // are we going to have more [un]selected items than the other ones?
960 if ( itemTo
- itemFrom
> m_count
/2 )
962 if ( select
!= m_defaultState
)
964 // the default state now becomes the same as 'select'
965 m_defaultState
= select
;
967 // so all the old selections (which had state select) shouldn't be
968 // selected any more, but all the other ones should
969 wxIndexArray selOld
= m_itemsSel
;
972 // TODO: it should be possible to optimize the searches a bit
973 // knowing the possible range
976 for ( item
= 0; item
< itemFrom
; item
++ )
978 if ( selOld
.Index(item
) == wxNOT_FOUND
)
979 m_itemsSel
.Add(item
);
982 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
984 if ( selOld
.Index(item
) == wxNOT_FOUND
)
985 m_itemsSel
.Add(item
);
988 // many items (> half) changed state
991 else // select == m_defaultState
993 // get the inclusive range of items between itemFrom and itemTo
994 size_t count
= m_itemsSel
.GetCount(),
995 start
= m_itemsSel
.IndexForInsert(itemFrom
),
996 end
= m_itemsSel
.IndexForInsert(itemTo
);
998 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
1003 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
1010 // delete all of them (from end to avoid changing indices)
1011 for ( int i
= end
; i
>= (int)start
; i
-- )
1015 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1017 // stop counting (see comment below)
1018 itemsChanged
= NULL
;
1022 itemsChanged
->Add(m_itemsSel
[i
]);
1026 m_itemsSel
.RemoveAt(i
);
1031 else // "few" items change state
1035 itemsChanged
->Empty();
1038 // just add the items to the selection
1039 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1041 if ( SelectItem(item
, select
) && itemsChanged
)
1043 itemsChanged
->Add(item
);
1045 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1047 // stop counting them, we'll just eat gobs of memory
1048 // for nothing at all - faster to refresh everything in
1050 itemsChanged
= NULL
;
1056 // we set it to NULL if there are many items changing state
1057 return itemsChanged
!= NULL
;
1060 void wxSelectionStore::OnItemDelete(size_t item
)
1062 size_t count
= m_itemsSel
.GetCount(),
1063 i
= m_itemsSel
.IndexForInsert(item
);
1065 if ( i
< count
&& m_itemsSel
[i
] == item
)
1067 // this item itself was in m_itemsSel, remove it from there
1068 m_itemsSel
.RemoveAt(i
);
1073 // and adjust the index of all which follow it
1076 // all following elements must be greater than the one we deleted
1077 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1083 //-----------------------------------------------------------------------------
1085 //-----------------------------------------------------------------------------
1087 wxListItemData::~wxListItemData()
1089 // in the virtual list control the attributes are managed by the main
1090 // program, so don't delete them
1091 if ( !m_owner
->IsVirtual() )
1099 void wxListItemData::Init()
1107 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1113 if ( owner
->InReportView() )
1119 m_rect
= new wxRect
;
1123 void wxListItemData::SetItem( const wxListItem
&info
)
1125 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1126 SetText(info
.m_text
);
1127 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1128 m_image
= info
.m_image
;
1129 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1130 m_data
= info
.m_data
;
1132 if ( info
.HasAttributes() )
1135 *m_attr
= *info
.GetAttributes();
1137 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1145 m_rect
->width
= info
.m_width
;
1149 void wxListItemData::SetPosition( int x
, int y
)
1151 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1157 void wxListItemData::SetSize( int width
, int height
)
1159 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1162 m_rect
->width
= width
;
1164 m_rect
->height
= height
;
1167 bool wxListItemData::IsHit( int x
, int y
) const
1169 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1171 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1174 int wxListItemData::GetX() const
1176 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1181 int wxListItemData::GetY() const
1183 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1188 int wxListItemData::GetWidth() const
1190 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1192 return m_rect
->width
;
1195 int wxListItemData::GetHeight() const
1197 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1199 return m_rect
->height
;
1202 void wxListItemData::GetItem( wxListItem
&info
) const
1204 info
.m_text
= m_text
;
1205 info
.m_image
= m_image
;
1206 info
.m_data
= m_data
;
1210 if ( m_attr
->HasTextColour() )
1211 info
.SetTextColour(m_attr
->GetTextColour());
1212 if ( m_attr
->HasBackgroundColour() )
1213 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1214 if ( m_attr
->HasFont() )
1215 info
.SetFont(m_attr
->GetFont());
1219 //-----------------------------------------------------------------------------
1221 //-----------------------------------------------------------------------------
1223 void wxListHeaderData::Init()
1234 wxListHeaderData::wxListHeaderData()
1239 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1246 void wxListHeaderData::SetItem( const wxListItem
&item
)
1248 m_mask
= item
.m_mask
;
1250 if ( m_mask
& wxLIST_MASK_TEXT
)
1251 m_text
= item
.m_text
;
1253 if ( m_mask
& wxLIST_MASK_IMAGE
)
1254 m_image
= item
.m_image
;
1256 if ( m_mask
& wxLIST_MASK_FORMAT
)
1257 m_format
= item
.m_format
;
1259 if ( m_mask
& wxLIST_MASK_WIDTH
)
1260 SetWidth(item
.m_width
);
1263 void wxListHeaderData::SetPosition( int x
, int y
)
1269 void wxListHeaderData::SetHeight( int h
)
1274 void wxListHeaderData::SetWidth( int w
)
1278 m_width
= WIDTH_COL_DEFAULT
;
1279 else if (m_width
< WIDTH_COL_MIN
)
1280 m_width
= WIDTH_COL_MIN
;
1283 void wxListHeaderData::SetFormat( int format
)
1288 bool wxListHeaderData::HasImage() const
1290 return m_image
!= -1;
1293 bool wxListHeaderData::IsHit( int x
, int y
) const
1295 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1298 void wxListHeaderData::GetItem( wxListItem
& item
)
1300 item
.m_mask
= m_mask
;
1301 item
.m_text
= m_text
;
1302 item
.m_image
= m_image
;
1303 item
.m_format
= m_format
;
1304 item
.m_width
= m_width
;
1307 int wxListHeaderData::GetImage() const
1312 int wxListHeaderData::GetWidth() const
1317 int wxListHeaderData::GetFormat() const
1322 //-----------------------------------------------------------------------------
1324 //-----------------------------------------------------------------------------
1326 inline int wxListLineData::GetMode() const
1328 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1331 inline bool wxListLineData::InReportView() const
1333 return m_owner
->HasFlag(wxLC_REPORT
);
1336 inline bool wxListLineData::IsVirtual() const
1338 return m_owner
->IsVirtual();
1341 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1344 m_items
.DeleteContents( TRUE
);
1346 if ( InReportView() )
1352 m_gi
= new GeometryInfo
;
1355 m_highlighted
= FALSE
;
1357 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1360 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1362 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1363 wxCHECK_RET( node
, _T("no subitems at all??") );
1365 wxListItemData
*item
= node
->GetData();
1367 switch ( GetMode() )
1370 case wxLC_SMALL_ICON
:
1372 m_gi
->m_rectAll
.width
= spacing
;
1374 wxString s
= item
->GetText();
1380 m_gi
->m_rectLabel
.width
=
1381 m_gi
->m_rectLabel
.height
= 0;
1385 dc
->GetTextExtent( s
, &lw
, &lh
);
1386 if (lh
< SCROLL_UNIT_Y
)
1391 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1393 m_gi
->m_rectAll
.width
= lw
;
1395 m_gi
->m_rectLabel
.width
= lw
;
1396 m_gi
->m_rectLabel
.height
= lh
;
1399 if (item
->HasImage())
1402 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1403 m_gi
->m_rectIcon
.width
= w
+ 8;
1404 m_gi
->m_rectIcon
.height
= h
+ 8;
1406 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1407 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1408 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1409 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1412 if ( item
->HasText() )
1414 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1415 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1417 else // no text, highlight the icon
1419 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1420 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1427 wxString s
= item
->GetTextForMeasuring();
1430 dc
->GetTextExtent( s
, &lw
, &lh
);
1431 if (lh
< SCROLL_UNIT_Y
)
1436 m_gi
->m_rectLabel
.width
= lw
;
1437 m_gi
->m_rectLabel
.height
= lh
;
1439 m_gi
->m_rectAll
.width
= lw
;
1440 m_gi
->m_rectAll
.height
= lh
;
1442 if (item
->HasImage())
1445 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1446 m_gi
->m_rectIcon
.width
= w
;
1447 m_gi
->m_rectIcon
.height
= h
;
1449 m_gi
->m_rectAll
.width
+= 4 + w
;
1450 if (h
> m_gi
->m_rectAll
.height
)
1451 m_gi
->m_rectAll
.height
= h
;
1454 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1455 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1460 wxFAIL_MSG( _T("unexpected call to SetSize") );
1464 wxFAIL_MSG( _T("unknown mode") );
1468 void wxListLineData::SetPosition( int x
, int y
,
1472 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1473 wxCHECK_RET( node
, _T("no subitems at all??") );
1475 wxListItemData
*item
= node
->GetData();
1477 switch ( GetMode() )
1480 case wxLC_SMALL_ICON
:
1481 m_gi
->m_rectAll
.x
= x
;
1482 m_gi
->m_rectAll
.y
= y
;
1484 if ( item
->HasImage() )
1486 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1487 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1488 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1491 if ( item
->HasText() )
1493 if (m_gi
->m_rectAll
.width
> spacing
)
1494 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1496 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1497 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1498 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1499 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1501 else // no text, highlight the icon
1503 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1504 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1509 m_gi
->m_rectAll
.x
= x
;
1510 m_gi
->m_rectAll
.y
= y
;
1512 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1513 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1514 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1516 if (item
->HasImage())
1518 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1519 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1520 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1524 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1529 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1533 wxFAIL_MSG( _T("unknown mode") );
1537 void wxListLineData::InitItems( int num
)
1539 for (int i
= 0; i
< num
; i
++)
1540 m_items
.Append( new wxListItemData(m_owner
) );
1543 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1545 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1546 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1548 wxListItemData
*item
= node
->GetData();
1549 item
->SetItem( info
);
1552 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1554 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1557 wxListItemData
*item
= node
->GetData();
1558 item
->GetItem( info
);
1562 wxString
wxListLineData::GetText(int index
) const
1566 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1569 wxListItemData
*item
= node
->GetData();
1570 s
= item
->GetText();
1576 void wxListLineData::SetText( int index
, const wxString s
)
1578 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1581 wxListItemData
*item
= node
->GetData();
1586 void wxListLineData::SetImage( int index
, int image
)
1588 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1589 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1591 wxListItemData
*item
= node
->GetData();
1592 item
->SetImage(image
);
1595 int wxListLineData::GetImage( int index
) const
1597 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1598 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1600 wxListItemData
*item
= node
->GetData();
1601 return item
->GetImage();
1604 wxListItemAttr
*wxListLineData::GetAttr() const
1606 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1607 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1609 wxListItemData
*item
= node
->GetData();
1610 return item
->GetAttr();
1613 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1615 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1616 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1618 wxListItemData
*item
= node
->GetData();
1619 item
->SetAttr(attr
);
1622 bool wxListLineData::SetAttributes(wxDC
*dc
,
1623 const wxListItemAttr
*attr
,
1626 wxWindow
*listctrl
= m_owner
->GetParent();
1630 // don't use foreground colour for drawing highlighted items - this might
1631 // make them completely invisible (and there is no way to do bit
1632 // arithmetics on wxColour, unfortunately)
1636 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1640 if ( attr
&& attr
->HasTextColour() )
1642 colText
= attr
->GetTextColour();
1646 colText
= listctrl
->GetForegroundColour();
1650 dc
->SetTextForeground(colText
);
1654 if ( attr
&& attr
->HasFont() )
1656 font
= attr
->GetFont();
1660 font
= listctrl
->GetFont();
1666 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1667 if ( highlighted
|| hasBgCol
)
1671 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1675 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1678 dc
->SetPen( *wxTRANSPARENT_PEN
);
1686 void wxListLineData::Draw( wxDC
*dc
)
1688 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1689 wxCHECK_RET( node
, _T("no subitems at all??") );
1691 bool highlighted
= IsHighlighted();
1693 wxListItemAttr
*attr
= GetAttr();
1695 if ( SetAttributes(dc
, attr
, highlighted
) )
1697 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1700 wxListItemData
*item
= node
->GetData();
1701 if (item
->HasImage())
1703 wxRect rectIcon
= m_gi
->m_rectIcon
;
1704 m_owner
->DrawImage( item
->GetImage(), dc
,
1705 rectIcon
.x
, rectIcon
.y
);
1708 if (item
->HasText())
1710 wxRect rectLabel
= m_gi
->m_rectLabel
;
1712 wxDCClipper
clipper(*dc
, rectLabel
);
1713 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1717 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1719 const wxRect
& rectHL
,
1722 // TODO: later we should support setting different attributes for
1723 // different columns - to do it, just add "col" argument to
1724 // GetAttr() and move these lines into the loop below
1725 wxListItemAttr
*attr
= GetAttr();
1726 if ( SetAttributes(dc
, attr
, highlighted
) )
1728 dc
->DrawRectangle( rectHL
);
1731 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1732 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1735 for ( wxListItemDataList::Node
*node
= m_items
.GetFirst();
1737 node
= node
->GetNext(), col
++ )
1739 wxListItemData
*item
= node
->GetData();
1741 int width
= m_owner
->GetColumnWidth(col
);
1745 if ( item
->HasImage() )
1748 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1749 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1751 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1757 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1759 if ( item
->HasText() )
1761 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1766 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1767 const wxString
&text
,
1773 wxString drawntext
, ellipsis
;
1774 wxCoord w
, h
, base_w
;
1777 // determine if the string can fit inside the current width
1778 dc
->GetTextExtent(text
, &w
, &h
);
1781 // it can, draw it using the items alignment
1782 m_owner
->GetColumn(col
, item
);
1783 switch ( item
.GetAlign() )
1786 wxFAIL_MSG( _T("unknown list item format") );
1789 case wxLIST_FORMAT_LEFT
:
1793 case wxLIST_FORMAT_RIGHT
:
1797 case wxLIST_FORMAT_CENTER
:
1798 x
+= (width
- w
) / 2;
1802 dc
->DrawText(text
, x
, y
);
1804 else // otherwise, truncate and add an ellipsis if possible
1806 // determine the base width
1807 ellipsis
= wxString(wxT("..."));
1808 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1810 // continue until we have enough space or only one character left
1812 size_t len
= text
.Length();
1813 drawntext
= text
.Left(len
);
1816 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1817 drawntext
.RemoveLast();
1820 if (w
+ base_w
<= width
)
1824 // if still not enough space, remove ellipsis characters
1825 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1827 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1828 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1831 // now draw the text
1832 dc
->DrawText(drawntext
, x
, y
);
1833 dc
->DrawText(ellipsis
, x
+ w
, y
);
1837 bool wxListLineData::Highlight( bool on
)
1839 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1841 if ( on
== m_highlighted
)
1849 void wxListLineData::ReverseHighlight( void )
1851 Highlight(!IsHighlighted());
1854 //-----------------------------------------------------------------------------
1855 // wxListHeaderWindow
1856 //-----------------------------------------------------------------------------
1858 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1860 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1861 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1862 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1863 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1866 void wxListHeaderWindow::Init()
1868 m_currentCursor
= (wxCursor
*) NULL
;
1869 m_isDragging
= FALSE
;
1873 wxListHeaderWindow::wxListHeaderWindow()
1877 m_owner
= (wxListMainWindow
*) NULL
;
1878 m_resizeCursor
= (wxCursor
*) NULL
;
1881 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1883 wxListMainWindow
*owner
,
1887 const wxString
&name
)
1888 : wxWindow( win
, id
, pos
, size
, style
, name
)
1893 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1895 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1898 wxListHeaderWindow::~wxListHeaderWindow()
1900 delete m_resizeCursor
;
1903 #ifdef __WXUNIVERSAL__
1904 #include "wx/univ/renderer.h"
1905 #include "wx/univ/theme.h"
1908 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1910 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1911 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1912 : GTK_STATE_INSENSITIVE
;
1914 x
= dc
->XLOG2DEV( x
);
1916 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1917 state
, GTK_SHADOW_OUT
,
1918 (GdkRectangle
*) NULL
, m_wxwindow
,
1919 (char *)"button", // const_cast
1920 x
-1, y
-1, w
+2, h
+2);
1921 #elif defined(__WXUNIVERSAL__)
1922 wxTheme
*theme
= wxTheme::Get();
1923 wxRenderer
*renderer
= theme
->GetRenderer();
1924 renderer
->DrawBorder( *dc
, wxBORDER_RAISED
, wxRect(x
,y
,w
,h
), 0 );
1925 #elif defined(__WXMAC__)
1926 const int m_corner
= 1;
1928 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1930 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1931 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1932 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1934 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1937 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1938 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1940 dc
->SetPen( *wxWHITE_PEN
);
1941 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1942 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1943 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1944 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1946 const int m_corner
= 1;
1948 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1950 dc
->SetPen( *wxBLACK_PEN
);
1951 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1952 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1954 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1957 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1958 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1960 dc
->SetPen( *wxWHITE_PEN
);
1961 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1962 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1963 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1964 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1968 // shift the DC origin to match the position of the main window horz
1969 // scrollbar: this allows us to always use logical coords
1970 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1973 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1976 m_owner
->GetViewStart( &x
, NULL
);
1978 // account for the horz scrollbar offset
1979 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1982 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1984 #if defined(__WXGTK__)
1985 wxClientDC
dc( this );
1987 wxPaintDC
dc( this );
1995 dc
.SetFont( GetFont() );
1997 // width and height of the entire header window
1999 GetClientSize( &w
, &h
);
2000 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2002 dc
.SetBackgroundMode(wxTRANSPARENT
);
2004 // do *not* use the listctrl colour for headers - one day we will have a
2005 // function to set it separately
2006 //dc.SetTextForeground( *wxBLACK );
2007 dc
.SetTextForeground(wxSystemSettings::
2008 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
2010 int x
= HEADER_OFFSET_X
;
2012 int numColumns
= m_owner
->GetColumnCount();
2014 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
2016 m_owner
->GetColumn( i
, item
);
2017 int wCol
= item
.m_width
;
2019 // the width of the rect to draw: make it smaller to fit entirely
2020 // inside the column rect
2023 dc
.SetPen( *wxWHITE_PEN
);
2025 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
2027 // see if we have enough space for the column label
2029 // for this we need the width of the text
2031 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
2032 wLabel
+= 2*EXTRA_WIDTH
;
2034 // and the width of the icon, if any
2035 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
2036 int ix
= 0, // init them just to suppress the compiler warnings
2038 const int image
= item
.m_image
;
2039 wxImageListType
*imageList
;
2042 imageList
= m_owner
->m_small_image_list
;
2045 imageList
->GetSize(image
, ix
, iy
);
2046 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
2054 // ignore alignment if there is not enough space anyhow
2056 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
2059 wxFAIL_MSG( _T("unknown list item format") );
2062 case wxLIST_FORMAT_LEFT
:
2066 case wxLIST_FORMAT_RIGHT
:
2067 xAligned
= x
+ cw
- wLabel
;
2070 case wxLIST_FORMAT_CENTER
:
2071 xAligned
= x
+ (cw
- wLabel
) / 2;
2076 // if we have an image, draw it on the right of the label
2083 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
2084 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
2085 wxIMAGELIST_DRAW_TRANSPARENT
2088 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
2091 // draw the text clipping it so that it doesn't overwrite the column
2093 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
2095 dc
.DrawText( item
.GetText(),
2096 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
2104 void wxListHeaderWindow::DrawCurrent()
2106 int x1
= m_currentX
;
2108 m_owner
->ClientToScreen( &x1
, &y1
);
2110 int x2
= m_currentX
;
2112 m_owner
->GetClientSize( NULL
, &y2
);
2113 m_owner
->ClientToScreen( &x2
, &y2
);
2116 dc
.SetLogicalFunction( wxINVERT
);
2117 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
2118 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2122 dc
.DrawLine( x1
, y1
, x2
, y2
);
2124 dc
.SetLogicalFunction( wxCOPY
);
2126 dc
.SetPen( wxNullPen
);
2127 dc
.SetBrush( wxNullBrush
);
2130 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
2132 // we want to work with logical coords
2134 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
2135 int y
= event
.GetY();
2139 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
2141 // we don't draw the line beyond our window, but we allow dragging it
2144 GetClientSize( &w
, NULL
);
2145 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2148 // erase the line if it was drawn
2149 if ( m_currentX
< w
)
2152 if (event
.ButtonUp())
2155 m_isDragging
= FALSE
;
2157 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2158 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
2165 m_currentX
= m_minX
+ 7;
2167 // draw in the new location
2168 if ( m_currentX
< w
)
2172 else // not dragging
2175 bool hit_border
= FALSE
;
2177 // end of the current column
2180 // find the column where this event occured
2182 countCol
= m_owner
->GetColumnCount();
2183 for (col
= 0; col
< countCol
; col
++)
2185 xpos
+= m_owner
->GetColumnWidth( col
);
2188 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2190 // near the column border
2197 // inside the column
2204 if ( col
== countCol
)
2207 if (event
.LeftDown() || event
.RightUp())
2209 if (hit_border
&& event
.LeftDown())
2211 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2212 event
.GetPosition()) )
2214 m_isDragging
= TRUE
;
2219 //else: column resizing was vetoed by the user code
2221 else // click on a column
2223 SendListEvent( event
.LeftDown()
2224 ? wxEVT_COMMAND_LIST_COL_CLICK
2225 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2226 event
.GetPosition());
2229 else if (event
.Moving())
2234 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2235 m_currentCursor
= m_resizeCursor
;
2239 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2240 m_currentCursor
= wxSTANDARD_CURSOR
;
2244 SetCursor(*m_currentCursor
);
2249 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2251 m_owner
->SetFocus();
2254 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2256 wxWindow
*parent
= GetParent();
2257 wxListEvent
le( type
, parent
->GetId() );
2258 le
.SetEventObject( parent
);
2259 le
.m_pointDrag
= pos
;
2261 // the position should be relative to the parent window, not
2262 // this one for compatibility with MSW and common sense: the
2263 // user code doesn't know anything at all about this header
2264 // window, so why should it get positions relative to it?
2265 le
.m_pointDrag
.y
-= GetSize().y
;
2267 le
.m_col
= m_column
;
2268 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2271 //-----------------------------------------------------------------------------
2272 // wxListRenameTimer (internal)
2273 //-----------------------------------------------------------------------------
2275 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2280 void wxListRenameTimer::Notify()
2282 m_owner
->OnRenameTimer();
2285 //-----------------------------------------------------------------------------
2286 // wxListTextCtrl (internal)
2287 //-----------------------------------------------------------------------------
2289 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2290 EVT_CHAR (wxListTextCtrl::OnChar
)
2291 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2292 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2295 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2296 : m_startValue(owner
->GetItemText(itemEdit
)),
2297 m_itemEdited(itemEdit
)
2302 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2304 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2305 &rectLabel
.x
, &rectLabel
.y
);
2307 (void)Create(owner
, wxID_ANY
, m_startValue
,
2308 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2309 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2312 void wxListTextCtrl::Finish()
2316 wxPendingDelete
.Append(this);
2320 m_owner
->SetFocus();
2324 bool wxListTextCtrl::AcceptChanges()
2326 const wxString value
= GetValue();
2328 if ( value
== m_startValue
)
2330 // nothing changed, always accept
2334 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2336 // vetoed by the user
2340 // accepted, do rename the item
2341 m_owner
->SetItemText(m_itemEdited
, value
);
2346 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2348 switch ( event
.m_keyCode
)
2351 if ( !AcceptChanges() )
2353 // vetoed by the user code
2356 //else: fall through
2367 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2375 // auto-grow the textctrl:
2376 wxSize parentSize
= m_owner
->GetSize();
2377 wxPoint myPos
= GetPosition();
2378 wxSize mySize
= GetSize();
2380 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2381 if (myPos
.x
+ sx
> parentSize
.x
)
2382 sx
= parentSize
.x
- myPos
.x
;
2390 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2394 (void)AcceptChanges();
2402 //-----------------------------------------------------------------------------
2404 //-----------------------------------------------------------------------------
2406 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2408 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2409 EVT_PAINT (wxListMainWindow::OnPaint
)
2410 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2411 EVT_CHAR (wxListMainWindow::OnChar
)
2412 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2413 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2414 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2415 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2418 void wxListMainWindow::Init()
2420 m_columns
.DeleteContents( TRUE
);
2424 m_lineTo
= (size_t)-1;
2430 m_small_image_list
= (wxImageListType
*) NULL
;
2431 m_normal_image_list
= (wxImageListType
*) NULL
;
2433 m_small_spacing
= 30;
2434 m_normal_spacing
= 40;
2438 m_isCreated
= FALSE
;
2440 m_lastOnSame
= FALSE
;
2441 m_renameTimer
= new wxListRenameTimer( this );
2445 m_lineBeforeLastClicked
= (size_t)-1;
2450 void wxListMainWindow::InitScrolling()
2452 if ( HasFlag(wxLC_REPORT
) )
2454 m_xScroll
= SCROLL_UNIT_X
;
2455 m_yScroll
= SCROLL_UNIT_Y
;
2459 m_xScroll
= SCROLL_UNIT_Y
;
2464 wxListMainWindow::wxListMainWindow()
2469 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2475 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2480 const wxString
&name
)
2481 : wxScrolledWindow( parent
, id
, pos
, size
,
2482 style
| wxHSCROLL
| wxVSCROLL
, name
)
2486 m_highlightBrush
= new wxBrush
2488 wxSystemSettings::GetColour
2490 wxSYS_COLOUR_HIGHLIGHT
2495 m_highlightUnfocusedBrush
= new wxBrush
2497 wxSystemSettings::GetColour
2499 wxSYS_COLOUR_BTNSHADOW
2508 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2510 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2513 wxListMainWindow::~wxListMainWindow()
2517 delete m_highlightBrush
;
2518 delete m_highlightUnfocusedBrush
;
2520 delete m_renameTimer
;
2523 void wxListMainWindow::CacheLineData(size_t line
)
2525 wxGenericListCtrl
*listctrl
= GetListCtrl();
2527 wxListLineData
*ld
= GetDummyLine();
2529 size_t countCol
= GetColumnCount();
2530 for ( size_t col
= 0; col
< countCol
; col
++ )
2532 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2535 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2536 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2539 wxListLineData
*wxListMainWindow::GetDummyLine() const
2541 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2543 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2545 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2547 // we need to recreate the dummy line if the number of columns in the
2548 // control changed as it would have the incorrect number of fields
2550 if ( !m_lines
.IsEmpty() &&
2551 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2553 self
->m_lines
.Clear();
2556 if ( m_lines
.IsEmpty() )
2558 wxListLineData
*line
= new wxListLineData(self
);
2559 self
->m_lines
.Add(line
);
2561 // don't waste extra memory -- there never going to be anything
2562 // else/more in this array
2563 self
->m_lines
.Shrink();
2569 // ----------------------------------------------------------------------------
2570 // line geometry (report mode only)
2571 // ----------------------------------------------------------------------------
2573 wxCoord
wxListMainWindow::GetLineHeight() const
2575 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2577 // we cache the line height as calling GetTextExtent() is slow
2578 if ( !m_lineHeight
)
2580 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2582 wxClientDC
dc( self
);
2583 dc
.SetFont( GetFont() );
2586 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2588 if ( y
< SCROLL_UNIT_Y
)
2591 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2595 m_small_image_list
->GetSize(0, iw
, ih
);
2600 self
->m_lineHeight
= y
+ LINE_SPACING
;
2603 return m_lineHeight
;
2606 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2608 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2610 return LINE_SPACING
+ line
*GetLineHeight();
2613 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2615 if ( !InReportView() )
2616 return GetLine(line
)->m_gi
->m_rectAll
;
2619 rect
.x
= HEADER_OFFSET_X
;
2620 rect
.y
= GetLineY(line
);
2621 rect
.width
= GetHeaderWidth();
2622 rect
.height
= GetLineHeight();
2627 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2629 if ( !InReportView() )
2630 return GetLine(line
)->m_gi
->m_rectLabel
;
2633 rect
.x
= HEADER_OFFSET_X
;
2634 rect
.y
= GetLineY(line
);
2635 rect
.width
= GetColumnWidth(0);
2636 rect
.height
= GetLineHeight();
2641 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2643 if ( !InReportView() )
2644 return GetLine(line
)->m_gi
->m_rectIcon
;
2646 wxListLineData
*ld
= GetLine(line
);
2647 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2650 rect
.x
= HEADER_OFFSET_X
;
2651 rect
.y
= GetLineY(line
);
2652 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2657 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2659 return InReportView() ? GetLineRect(line
)
2660 : GetLine(line
)->m_gi
->m_rectHighlight
;
2663 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2665 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2667 wxListLineData
*ld
= GetLine(line
);
2669 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2670 return wxLIST_HITTEST_ONITEMICON
;
2672 // VS: Testing for "ld->HasText() || InReportView()" instead of
2673 // "ld->HasText()" is needed to make empty lines in report view
2675 if ( ld
->HasText() || InReportView() )
2677 wxRect rect
= InReportView() ? GetLineRect(line
)
2678 : GetLineLabelRect(line
);
2680 if ( rect
.Inside(x
, y
) )
2681 return wxLIST_HITTEST_ONITEMLABEL
;
2687 // ----------------------------------------------------------------------------
2688 // highlight (selection) handling
2689 // ----------------------------------------------------------------------------
2691 bool wxListMainWindow::IsHighlighted(size_t line
) const
2695 return m_selStore
.IsSelected(line
);
2699 wxListLineData
*ld
= GetLine(line
);
2700 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2702 return ld
->IsHighlighted();
2706 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2712 wxArrayInt linesChanged
;
2713 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2716 // meny items changed state, refresh everything
2717 RefreshLines(lineFrom
, lineTo
);
2719 else // only a few items changed state, refresh only them
2721 size_t count
= linesChanged
.GetCount();
2722 for ( size_t n
= 0; n
< count
; n
++ )
2724 RefreshLine(linesChanged
[n
]);
2728 else // iterate over all items in non report view
2730 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2732 if ( HighlightLine(line
, highlight
) )
2740 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2746 changed
= m_selStore
.SelectItem(line
, highlight
);
2750 wxListLineData
*ld
= GetLine(line
);
2751 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2753 changed
= ld
->Highlight(highlight
);
2758 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2759 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2765 void wxListMainWindow::RefreshLine( size_t line
)
2767 if ( HasFlag(wxLC_REPORT
) )
2769 size_t visibleFrom
, visibleTo
;
2770 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2772 if ( line
< visibleFrom
|| line
> visibleTo
)
2776 wxRect rect
= GetLineRect(line
);
2778 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2779 RefreshRect( rect
);
2782 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2784 // we suppose that they are ordered by caller
2785 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2787 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2789 if ( HasFlag(wxLC_REPORT
) )
2791 size_t visibleFrom
, visibleTo
;
2792 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2794 if ( lineFrom
< visibleFrom
)
2795 lineFrom
= visibleFrom
;
2796 if ( lineTo
> visibleTo
)
2801 rect
.y
= GetLineY(lineFrom
);
2802 rect
.width
= GetClientSize().x
;
2803 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2805 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2806 RefreshRect( rect
);
2810 // TODO: this should be optimized...
2811 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2818 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2820 if ( HasFlag(wxLC_REPORT
) )
2822 size_t visibleFrom
, visibleTo
;
2823 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2825 if ( lineFrom
< visibleFrom
)
2826 lineFrom
= visibleFrom
;
2827 else if ( lineFrom
> visibleTo
)
2832 rect
.y
= GetLineY(lineFrom
);
2833 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2835 wxSize size
= GetClientSize();
2836 rect
.width
= size
.x
;
2837 // refresh till the bottom of the window
2838 rect
.height
= size
.y
- rect
.y
;
2840 RefreshRect( rect
);
2844 // TODO: how to do it more efficiently?
2849 void wxListMainWindow::RefreshSelected()
2855 if ( InReportView() )
2857 GetVisibleLinesRange(&from
, &to
);
2862 to
= GetItemCount() - 1;
2865 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2867 RefreshLine(m_current
);
2870 for ( size_t line
= from
; line
<= to
; line
++ )
2872 // NB: the test works as expected even if m_current == -1
2873 if ( line
!= m_current
&& IsHighlighted(line
) )
2880 void wxListMainWindow::Freeze()
2885 void wxListMainWindow::Thaw()
2887 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2889 if ( !--m_freezeCount
)
2895 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2897 // Note: a wxPaintDC must be constructed even if no drawing is
2898 // done (a Windows requirement).
2899 wxPaintDC
dc( this );
2901 if ( IsEmpty() || m_freezeCount
)
2903 // nothing to draw or not the moment to draw it
2909 // delay the repainting until we calculate all the items positions
2916 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2920 dc
.SetFont( GetFont() );
2922 if ( HasFlag(wxLC_REPORT
) )
2924 int lineHeight
= GetLineHeight();
2926 size_t visibleFrom
, visibleTo
;
2927 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2930 wxCoord xOrig
, yOrig
;
2931 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2933 // tell the caller cache to cache the data
2936 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2937 GetParent()->GetId());
2938 evCache
.SetEventObject( GetParent() );
2939 evCache
.m_oldItemIndex
= visibleFrom
;
2940 evCache
.m_itemIndex
= visibleTo
;
2941 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2944 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2946 rectLine
= GetLineRect(line
);
2948 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2949 rectLine
.width
, rectLine
.height
) )
2951 // don't redraw unaffected lines to avoid flicker
2955 GetLine(line
)->DrawInReportMode( &dc
,
2957 GetLineHighlightRect(line
),
2958 IsHighlighted(line
) );
2961 if ( HasFlag(wxLC_HRULES
) )
2963 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2964 wxSize clientSize
= GetClientSize();
2966 // Don't draw the first one
2967 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2970 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2971 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2972 clientSize
.x
- dev_x
, i
*lineHeight
);
2975 // Draw last horizontal rule
2976 if ( visibleTo
== GetItemCount() - 1 )
2979 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2980 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2981 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2985 // Draw vertical rules if required
2986 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2988 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2991 wxRect firstItemRect
;
2992 wxRect lastItemRect
;
2993 GetItemRect(visibleFrom
, firstItemRect
);
2994 GetItemRect(visibleTo
, lastItemRect
);
2995 int x
= firstItemRect
.GetX();
2997 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2998 for (col
= 0; col
< GetColumnCount(); col
++)
3000 int colWidth
= GetColumnWidth(col
);
3002 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
3003 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
3009 size_t count
= GetItemCount();
3010 for ( size_t i
= 0; i
< count
; i
++ )
3012 GetLine(i
)->Draw( &dc
);
3018 // don't draw rect outline under Max if we already have the background
3019 // color but under other platforms only draw it if we do: it is a bit
3020 // silly to draw "focus rect" if we don't have focus!
3025 #endif // __WXMAC__/!__WXMAC__
3027 dc
.SetPen( *wxBLACK_PEN
);
3028 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
3029 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
3036 void wxListMainWindow::HighlightAll( bool on
)
3038 if ( IsSingleSel() )
3040 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
3042 // we just have one item to turn off
3043 if ( HasCurrent() && IsHighlighted(m_current
) )
3045 HighlightLine(m_current
, FALSE
);
3046 RefreshLine(m_current
);
3051 HighlightLines(0, GetItemCount() - 1, on
);
3055 void wxListMainWindow::SendNotify( size_t line
,
3056 wxEventType command
,
3059 wxListEvent
le( command
, GetParent()->GetId() );
3060 le
.SetEventObject( GetParent() );
3061 le
.m_itemIndex
= line
;
3063 // set only for events which have position
3064 if ( point
!= wxDefaultPosition
)
3065 le
.m_pointDrag
= point
;
3067 // don't try to get the line info for virtual list controls: the main
3068 // program has it anyhow and if we did it would result in accessing all
3069 // the lines, even those which are not visible now and this is precisely
3070 // what we're trying to avoid
3071 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
3073 if ( line
!= (size_t)-1 )
3075 GetLine(line
)->GetItem( 0, le
.m_item
);
3077 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
3079 //else: there may be no more such item
3081 GetParent()->GetEventHandler()->ProcessEvent( le
);
3084 void wxListMainWindow::ChangeCurrent(size_t current
)
3086 m_current
= current
;
3088 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
3091 void wxListMainWindow::EditLabel( long item
)
3093 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
3094 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
3096 size_t itemEdit
= (size_t)item
;
3098 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
3099 le
.SetEventObject( GetParent() );
3100 le
.m_itemIndex
= item
;
3101 wxListLineData
*data
= GetLine(itemEdit
);
3102 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
3103 data
->GetItem( 0, le
.m_item
);
3104 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
3106 // vetoed by user code
3110 // We have to call this here because the label in question might just have
3111 // been added and no screen update taken place.
3115 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
3120 void wxListMainWindow::OnRenameTimer()
3122 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
3124 EditLabel( m_current
);
3127 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
3129 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3130 le
.SetEventObject( GetParent() );
3131 le
.m_itemIndex
= itemEdit
;
3133 wxListLineData
*data
= GetLine(itemEdit
);
3134 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
3136 data
->GetItem( 0, le
.m_item
);
3137 le
.m_item
.m_text
= value
;
3138 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
3142 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3144 event
.SetEventObject( GetParent() );
3145 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3148 if ( !HasCurrent() || IsEmpty() )
3154 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3155 event
.ButtonDClick()) )
3158 int x
= event
.GetX();
3159 int y
= event
.GetY();
3160 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3162 // where did we hit it (if we did)?
3165 size_t count
= GetItemCount(),
3168 if ( HasFlag(wxLC_REPORT
) )
3170 current
= y
/ GetLineHeight();
3171 if ( current
< count
)
3172 hitResult
= HitTestLine(current
, x
, y
);
3176 // TODO: optimize it too! this is less simple than for report view but
3177 // enumerating all items is still not a way to do it!!
3178 for ( current
= 0; current
< count
; current
++ )
3180 hitResult
= HitTestLine(current
, x
, y
);
3186 if (event
.Dragging())
3188 if (m_dragCount
== 0)
3190 // we have to report the raw, physical coords as we want to be
3191 // able to call HitTest(event.m_pointDrag) from the user code to
3192 // get the item being dragged
3193 m_dragStart
= event
.GetPosition();
3198 if (m_dragCount
!= 3)
3201 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3202 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3204 wxListEvent
le( command
, GetParent()->GetId() );
3205 le
.SetEventObject( GetParent() );
3206 le
.m_itemIndex
= current
;
3207 le
.m_pointDrag
= m_dragStart
;
3208 GetParent()->GetEventHandler()->ProcessEvent( le
);
3219 // outside of any item
3223 bool forceClick
= FALSE
;
3224 if (event
.ButtonDClick())
3226 m_renameTimer
->Stop();
3227 m_lastOnSame
= FALSE
;
3229 if ( current
== m_lineLastClicked
)
3231 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3237 // the first click was on another item, so don't interpret this as
3238 // a double click, but as a simple click instead
3243 if (event
.LeftUp() && m_lastOnSame
)
3245 if ((current
== m_current
) &&
3246 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3247 HasFlag(wxLC_EDIT_LABELS
) )
3249 m_renameTimer
->Start( 100, TRUE
);
3251 m_lastOnSame
= FALSE
;
3253 else if (event
.RightDown())
3255 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3256 event
.GetPosition() );
3258 else if (event
.MiddleDown())
3260 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3262 else if ( event
.LeftDown() || forceClick
)
3264 m_lineBeforeLastClicked
= m_lineLastClicked
;
3265 m_lineLastClicked
= current
;
3267 size_t oldCurrent
= m_current
;
3269 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3271 HighlightAll( FALSE
);
3273 ChangeCurrent(current
);
3275 ReverseHighlight(m_current
);
3277 else // multi sel & either ctrl or shift is down
3279 if (event
.ControlDown())
3281 ChangeCurrent(current
);
3283 ReverseHighlight(m_current
);
3285 else if (event
.ShiftDown())
3287 ChangeCurrent(current
);
3289 size_t lineFrom
= oldCurrent
,
3292 if ( lineTo
< lineFrom
)
3295 lineFrom
= m_current
;
3298 HighlightLines(lineFrom
, lineTo
);
3300 else // !ctrl, !shift
3302 // test in the enclosing if should make it impossible
3303 wxFAIL_MSG( _T("how did we get here?") );
3307 if (m_current
!= oldCurrent
)
3309 RefreshLine( oldCurrent
);
3312 // forceClick is only set if the previous click was on another item
3313 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3317 void wxListMainWindow::MoveToItem(size_t item
)
3319 if ( item
== (size_t)-1 )
3322 wxRect rect
= GetLineRect(item
);
3324 int client_w
, client_h
;
3325 GetClientSize( &client_w
, &client_h
);
3327 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3328 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3330 if ( HasFlag(wxLC_REPORT
) )
3332 // the next we need the range of lines shown it might be different, so
3334 ResetVisibleLinesRange();
3336 if (rect
.y
< view_y
)
3337 Scroll( -1, rect
.y
/m_yScroll
);
3338 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3339 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3343 if (rect
.x
-view_x
< 5)
3344 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3345 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3346 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3350 // ----------------------------------------------------------------------------
3351 // keyboard handling
3352 // ----------------------------------------------------------------------------
3354 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3356 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3357 _T("invalid item index in OnArrowChar()") );
3359 size_t oldCurrent
= m_current
;
3361 // in single selection we just ignore Shift as we can't select several
3363 if ( event
.ShiftDown() && !IsSingleSel() )
3365 ChangeCurrent(newCurrent
);
3367 // select all the items between the old and the new one
3368 if ( oldCurrent
> newCurrent
)
3370 newCurrent
= oldCurrent
;
3371 oldCurrent
= m_current
;
3374 HighlightLines(oldCurrent
, newCurrent
);
3378 // all previously selected items are unselected unless ctrl is held
3379 if ( !event
.ControlDown() )
3380 HighlightAll(FALSE
);
3382 ChangeCurrent(newCurrent
);
3384 // refresh the old focus to remove it
3385 RefreshLine( oldCurrent
);
3387 if ( !event
.ControlDown() )
3389 HighlightLine( m_current
, TRUE
);
3393 RefreshLine( m_current
);
3398 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3400 wxWindow
*parent
= GetParent();
3402 /* we propagate the key event up */
3403 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3404 ke
.m_shiftDown
= event
.m_shiftDown
;
3405 ke
.m_controlDown
= event
.m_controlDown
;
3406 ke
.m_altDown
= event
.m_altDown
;
3407 ke
.m_metaDown
= event
.m_metaDown
;
3408 ke
.m_keyCode
= event
.m_keyCode
;
3411 ke
.SetEventObject( parent
);
3412 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3417 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3419 wxWindow
*parent
= GetParent();
3421 /* we send a list_key event up */
3424 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3425 le
.m_itemIndex
= m_current
;
3426 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3427 le
.m_code
= event
.GetKeyCode();
3428 le
.SetEventObject( parent
);
3429 parent
->GetEventHandler()->ProcessEvent( le
);
3432 /* we propagate the char event up */
3433 wxKeyEvent
ke( wxEVT_CHAR
);
3434 ke
.m_shiftDown
= event
.m_shiftDown
;
3435 ke
.m_controlDown
= event
.m_controlDown
;
3436 ke
.m_altDown
= event
.m_altDown
;
3437 ke
.m_metaDown
= event
.m_metaDown
;
3438 ke
.m_keyCode
= event
.m_keyCode
;
3441 ke
.SetEventObject( parent
);
3442 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3444 if (event
.GetKeyCode() == WXK_TAB
)
3446 wxNavigationKeyEvent nevent
;
3447 nevent
.SetWindowChange( event
.ControlDown() );
3448 nevent
.SetDirection( !event
.ShiftDown() );
3449 nevent
.SetEventObject( GetParent()->GetParent() );
3450 nevent
.SetCurrentFocus( m_parent
);
3451 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3455 /* no item -> nothing to do */
3462 switch (event
.GetKeyCode())
3465 if ( m_current
> 0 )
3466 OnArrowChar( m_current
- 1, event
);
3470 if ( m_current
< (size_t)GetItemCount() - 1 )
3471 OnArrowChar( m_current
+ 1, event
);
3476 OnArrowChar( GetItemCount() - 1, event
);
3481 OnArrowChar( 0, event
);
3487 if ( HasFlag(wxLC_REPORT
) )
3489 steps
= m_linesPerPage
- 1;
3493 steps
= m_current
% m_linesPerPage
;
3496 int index
= m_current
- steps
;
3500 OnArrowChar( index
, event
);
3507 if ( HasFlag(wxLC_REPORT
) )
3509 steps
= m_linesPerPage
- 1;
3513 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3516 size_t index
= m_current
+ steps
;
3517 size_t count
= GetItemCount();
3518 if ( index
>= count
)
3521 OnArrowChar( index
, event
);
3526 if ( !HasFlag(wxLC_REPORT
) )
3528 int index
= m_current
- m_linesPerPage
;
3532 OnArrowChar( index
, event
);
3537 if ( !HasFlag(wxLC_REPORT
) )
3539 size_t index
= m_current
+ m_linesPerPage
;
3541 size_t count
= GetItemCount();
3542 if ( index
>= count
)
3545 OnArrowChar( index
, event
);
3550 if ( IsSingleSel() )
3552 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3554 if ( IsHighlighted(m_current
) )
3556 // don't unselect the item in single selection mode
3559 //else: select it in ReverseHighlight() below if unselected
3562 ReverseHighlight(m_current
);
3567 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3575 // ----------------------------------------------------------------------------
3577 // ----------------------------------------------------------------------------
3579 void wxListMainWindow::SetFocus()
3581 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3582 // overrides SetFocus in such way that it does never change focus from
3583 // panel's child to the panel itself. Unfortunately, we must be able to change
3584 // focus to the panel from wxListTextCtrl because the text control should
3585 // disappear when the user clicks outside it.
3587 wxWindow
*oldFocus
= FindFocus();
3589 if ( oldFocus
&& oldFocus
->GetParent() == this )
3591 wxWindow::SetFocus();
3595 wxScrolledWindow::SetFocus();
3599 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3601 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3602 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3603 // which are already drawn correctly resulting in horrible flicker - avoid
3615 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3616 event
.SetEventObject( GetParent() );
3617 GetParent()->GetEventHandler()->ProcessEvent( event
);
3620 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3627 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3629 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3631 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3633 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3635 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3637 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3639 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3641 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3643 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3647 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3649 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3651 m_normal_image_list
->GetSize( index
, width
, height
);
3653 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3655 m_small_image_list
->GetSize( index
, width
, height
);
3657 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3659 m_small_image_list
->GetSize( index
, width
, height
);
3661 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3663 m_small_image_list
->GetSize( index
, width
, height
);
3672 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3674 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3675 dc
.SetFont( GetFont() );
3678 dc
.GetTextExtent( s
, &lw
, NULL
);
3680 return lw
+ AUTOSIZE_COL_MARGIN
;
3683 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3687 // calc the spacing from the icon size
3690 if ((imageList
) && (imageList
->GetImageCount()) )
3692 imageList
->GetSize(0, width
, height
);
3695 if (which
== wxIMAGE_LIST_NORMAL
)
3697 m_normal_image_list
= imageList
;
3698 m_normal_spacing
= width
+ 8;
3701 if (which
== wxIMAGE_LIST_SMALL
)
3703 m_small_image_list
= imageList
;
3704 m_small_spacing
= width
+ 14;
3705 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3709 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3714 m_small_spacing
= spacing
;
3718 m_normal_spacing
= spacing
;
3722 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3724 return isSmall
? m_small_spacing
: m_normal_spacing
;
3727 // ----------------------------------------------------------------------------
3729 // ----------------------------------------------------------------------------
3731 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3733 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3735 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3737 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3738 item
.m_width
= GetTextLength( item
.m_text
);
3740 wxListHeaderData
*column
= node
->GetData();
3741 column
->SetItem( item
);
3743 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3745 headerWin
->m_dirty
= TRUE
;
3749 // invalidate it as it has to be recalculated
3753 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3755 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3756 _T("invalid column index") );
3758 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3759 _T("SetColumnWidth() can only be called in report mode.") );
3762 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3764 headerWin
->m_dirty
= TRUE
;
3766 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3767 wxCHECK_RET( node
, _T("no column?") );
3769 wxListHeaderData
*column
= node
->GetData();
3771 size_t count
= GetItemCount();
3773 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3775 width
= GetTextLength(column
->GetText());
3777 else if ( width
== wxLIST_AUTOSIZE
)
3781 // TODO: determine the max width somehow...
3782 width
= WIDTH_COL_DEFAULT
;
3786 wxClientDC
dc(this);
3787 dc
.SetFont( GetFont() );
3789 int max
= AUTOSIZE_COL_MARGIN
;
3791 for ( size_t i
= 0; i
< count
; i
++ )
3793 wxListLineData
*line
= GetLine(i
);
3794 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3796 wxCHECK_RET( n
, _T("no subitem?") );
3798 wxListItemData
*item
= n
->GetData();
3801 if (item
->HasImage())
3804 GetImageSize( item
->GetImage(), ix
, iy
);
3808 if (item
->HasText())
3811 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3819 width
= max
+ AUTOSIZE_COL_MARGIN
;
3823 column
->SetWidth( width
);
3825 // invalidate it as it has to be recalculated
3829 int wxListMainWindow::GetHeaderWidth() const
3831 if ( !m_headerWidth
)
3833 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3835 size_t count
= GetColumnCount();
3836 for ( size_t col
= 0; col
< count
; col
++ )
3838 self
->m_headerWidth
+= GetColumnWidth(col
);
3842 return m_headerWidth
;
3845 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3847 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3848 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3850 wxListHeaderData
*column
= node
->GetData();
3851 column
->GetItem( item
);
3854 int wxListMainWindow::GetColumnWidth( int col
) const
3856 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3857 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3859 wxListHeaderData
*column
= node
->GetData();
3860 return column
->GetWidth();
3863 // ----------------------------------------------------------------------------
3865 // ----------------------------------------------------------------------------
3867 void wxListMainWindow::SetItem( wxListItem
&item
)
3869 long id
= item
.m_itemId
;
3870 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3871 _T("invalid item index in SetItem") );
3875 wxListLineData
*line
= GetLine((size_t)id
);
3876 line
->SetItem( item
.m_col
, item
);
3879 // update the item on screen
3881 GetItemRect(id
, rectItem
);
3882 RefreshRect(rectItem
);
3885 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3887 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3888 _T("invalid list ctrl item index in SetItem") );
3890 size_t oldCurrent
= m_current
;
3891 size_t item
= (size_t)litem
; // safe because of the check above
3893 // do we need to change the focus?
3894 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3896 if ( state
& wxLIST_STATE_FOCUSED
)
3898 // don't do anything if this item is already focused
3899 if ( item
!= m_current
)
3901 ChangeCurrent(item
);
3903 if ( oldCurrent
!= (size_t)-1 )
3905 if ( IsSingleSel() )
3907 HighlightLine(oldCurrent
, FALSE
);
3910 RefreshLine(oldCurrent
);
3913 RefreshLine( m_current
);
3918 // don't do anything if this item is not focused
3919 if ( item
== m_current
)
3923 if ( IsSingleSel() )
3925 // we must unselect the old current item as well or we
3926 // might end up with more than one selected item in a
3927 // single selection control
3928 HighlightLine(oldCurrent
, FALSE
);
3931 RefreshLine( oldCurrent
);
3936 // do we need to change the selection state?
3937 if ( stateMask
& wxLIST_STATE_SELECTED
)
3939 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3941 if ( IsSingleSel() )
3945 // selecting the item also makes it the focused one in the
3947 if ( m_current
!= item
)
3949 ChangeCurrent(item
);
3951 if ( oldCurrent
!= (size_t)-1 )
3953 HighlightLine( oldCurrent
, FALSE
);
3954 RefreshLine( oldCurrent
);
3960 // only the current item may be selected anyhow
3961 if ( item
!= m_current
)
3966 if ( HighlightLine(item
, on
) )
3973 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3975 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3976 _T("invalid list ctrl item index in GetItemState()") );
3978 int ret
= wxLIST_STATE_DONTCARE
;
3980 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3982 if ( (size_t)item
== m_current
)
3983 ret
|= wxLIST_STATE_FOCUSED
;
3986 if ( stateMask
& wxLIST_STATE_SELECTED
)
3988 if ( IsHighlighted(item
) )
3989 ret
|= wxLIST_STATE_SELECTED
;
3995 void wxListMainWindow::GetItem( wxListItem
&item
) const
3997 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3998 _T("invalid item index in GetItem") );
4000 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4001 line
->GetItem( item
.m_col
, item
);
4004 // ----------------------------------------------------------------------------
4006 // ----------------------------------------------------------------------------
4008 size_t wxListMainWindow::GetItemCount() const
4010 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4013 void wxListMainWindow::SetItemCount(long count
)
4015 m_selStore
.SetItemCount(count
);
4016 m_countVirt
= count
;
4018 ResetVisibleLinesRange();
4020 // scrollbars must be reset
4024 int wxListMainWindow::GetSelectedItemCount() const
4026 // deal with the quick case first
4027 if ( IsSingleSel() )
4029 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
4032 // virtual controls remmebers all its selections itself
4034 return m_selStore
.GetSelectedCount();
4036 // TODO: we probably should maintain the number of items selected even for
4037 // non virtual controls as enumerating all lines is really slow...
4038 size_t countSel
= 0;
4039 size_t count
= GetItemCount();
4040 for ( size_t line
= 0; line
< count
; line
++ )
4042 if ( GetLine(line
)->IsHighlighted() )
4049 // ----------------------------------------------------------------------------
4050 // item position/size
4051 // ----------------------------------------------------------------------------
4053 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4055 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4056 _T("invalid index in GetItemRect") );
4058 rect
= GetLineRect((size_t)index
);
4060 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4063 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4066 GetItemRect(item
, rect
);
4074 // ----------------------------------------------------------------------------
4075 // geometry calculation
4076 // ----------------------------------------------------------------------------
4078 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4080 wxClientDC
dc( this );
4081 dc
.SetFont( GetFont() );
4084 if ( HasFlag(wxLC_ICON
) )
4085 iconSpacing
= m_normal_spacing
;
4086 else if ( HasFlag(wxLC_SMALL_ICON
) )
4087 iconSpacing
= m_small_spacing
;
4091 // Note that we do not call GetClientSize() here but
4092 // GetSize() and substract the border size for sunken
4093 // borders manually. This is technically incorrect,
4094 // but we need to know the client area's size WITHOUT
4095 // scrollbars here. Since we don't know if there are
4096 // any scrollbars, we use GetSize() instead. Another
4097 // solution would be to call SetScrollbars() here to
4098 // remove the scrollbars and call GetClientSize() then,
4099 // but this might result in flicker and - worse - will
4100 // reset the scrollbars to 0 which is not good at all
4101 // if you resize a dialog/window, but don't want to
4102 // reset the window scrolling. RR.
4103 // Furthermore, we actually do NOT subtract the border
4104 // width as 2 pixels is just the extra space which we
4105 // need around the actual content in the window. Other-
4106 // wise the text would e.g. touch the upper border. RR.
4109 GetSize( &clientWidth
, &clientHeight
);
4111 if ( HasFlag(wxLC_REPORT
) )
4113 // all lines have the same height
4114 int lineHeight
= GetLineHeight();
4116 // scroll one line per step
4117 m_yScroll
= lineHeight
;
4119 size_t lineCount
= GetItemCount();
4120 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
4122 m_linesPerPage
= clientHeight
/ lineHeight
;
4124 ResetVisibleLinesRange();
4126 SetScrollbars( m_xScroll
, m_yScroll
,
4127 GetHeaderWidth() / m_xScroll
,
4128 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
4129 GetScrollPos(wxHORIZONTAL
),
4130 GetScrollPos(wxVERTICAL
),
4135 // at first we try without any scrollbar. if the items don't
4136 // fit into the window, we recalculate after subtracting an
4137 // approximated 15 pt for the horizontal scrollbar
4139 int entireWidth
= 0;
4141 for (int tries
= 0; tries
< 2; tries
++)
4143 // We start with 4 for the border around all items
4148 // Now we have decided that the items do not fit into the
4149 // client area. Unfortunately, wxWindows sometimes thinks
4150 // that it does fit and therefore NO horizontal scrollbar
4151 // is inserted. This looks ugly, so we fudge here and make
4152 // the calculated width bigger than was actually has been
4153 // calculated. This ensures that wxScrolledWindows puts
4154 // a scrollbar at the bottom of its client area.
4155 entireWidth
+= SCROLL_UNIT_X
;
4158 // Start at 2,2 so the text does not touch the border
4163 int currentlyVisibleLines
= 0;
4165 size_t count
= GetItemCount();
4166 for (size_t i
= 0; i
< count
; i
++)
4168 currentlyVisibleLines
++;
4169 wxListLineData
*line
= GetLine(i
);
4170 line
->CalculateSize( &dc
, iconSpacing
);
4171 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4173 wxSize sizeLine
= GetLineSize(i
);
4175 if ( maxWidth
< sizeLine
.x
)
4176 maxWidth
= sizeLine
.x
;
4179 if (currentlyVisibleLines
> m_linesPerPage
)
4180 m_linesPerPage
= currentlyVisibleLines
;
4182 // Assume that the size of the next one is the same... (FIXME)
4183 if ( y
+ sizeLine
.y
>= clientHeight
)
4185 currentlyVisibleLines
= 0;
4188 entireWidth
+= maxWidth
+6;
4192 // We have reached the last item.
4193 if ( i
== count
- 1 )
4194 entireWidth
+= maxWidth
;
4196 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4198 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4200 currentlyVisibleLines
= 0;
4204 if ( i
== count
- 1 )
4205 tries
= 1; // Everything fits, no second try required.
4209 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4210 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4215 // FIXME: why should we call it from here?
4222 void wxListMainWindow::RefreshAll()
4227 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4228 if ( headerWin
&& headerWin
->m_dirty
)
4230 headerWin
->m_dirty
= FALSE
;
4231 headerWin
->Refresh();
4235 void wxListMainWindow::UpdateCurrent()
4237 if ( !HasCurrent() && !IsEmpty() )
4243 long wxListMainWindow::GetNextItem( long item
,
4244 int WXUNUSED(geometry
),
4248 max
= GetItemCount();
4249 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4250 _T("invalid listctrl index in GetNextItem()") );
4252 // notice that we start with the next item (or the first one if item == -1)
4253 // and this is intentional to allow writing a simple loop to iterate over
4254 // all selected items
4258 // this is not an error because the index was ok initially, just no
4269 size_t count
= GetItemCount();
4270 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4272 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4275 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4282 // ----------------------------------------------------------------------------
4284 // ----------------------------------------------------------------------------
4286 void wxListMainWindow::DeleteItem( long lindex
)
4288 size_t count
= GetItemCount();
4290 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4291 _T("invalid item index in DeleteItem") );
4293 size_t index
= (size_t)lindex
;
4295 // we don't need to adjust the index for the previous items
4296 if ( HasCurrent() && m_current
>= index
)
4298 // if the current item is being deleted, we want the next one to
4299 // become selected - unless there is no next one - so don't adjust
4300 // m_current in this case
4301 if ( m_current
!= index
|| m_current
== count
- 1 )
4307 if ( InReportView() )
4309 ResetVisibleLinesRange();
4316 m_selStore
.OnItemDelete(index
);
4320 m_lines
.RemoveAt( index
);
4323 // we need to refresh the (vert) scrollbar as the number of items changed
4326 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4328 RefreshAfter(index
);
4331 void wxListMainWindow::DeleteColumn( int col
)
4333 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4335 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4338 m_columns
.DeleteNode( node
);
4340 // invalidate it as it has to be recalculated
4344 void wxListMainWindow::DoDeleteAllItems()
4348 // nothing to do - in particular, don't send the event
4354 // to make the deletion of all items faster, we don't send the
4355 // notifications for each item deletion in this case but only one event
4356 // for all of them: this is compatible with wxMSW and documented in
4357 // DeleteAllItems() description
4359 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4360 event
.SetEventObject( GetParent() );
4361 GetParent()->GetEventHandler()->ProcessEvent( event
);
4370 if ( InReportView() )
4372 ResetVisibleLinesRange();
4378 void wxListMainWindow::DeleteAllItems()
4382 RecalculatePositions();
4385 void wxListMainWindow::DeleteEverything()
4392 // ----------------------------------------------------------------------------
4393 // scanning for an item
4394 // ----------------------------------------------------------------------------
4396 void wxListMainWindow::EnsureVisible( long index
)
4398 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4399 _T("invalid index in EnsureVisible") );
4401 // We have to call this here because the label in question might just have
4402 // been added and its position is not known yet
4405 RecalculatePositions(TRUE
/* no refresh */);
4408 MoveToItem((size_t)index
);
4411 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4418 size_t count
= GetItemCount();
4419 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4421 wxListLineData
*line
= GetLine(i
);
4422 if ( line
->GetText(0) == tmp
)
4429 long wxListMainWindow::FindItem(long start
, long data
)
4435 size_t count
= GetItemCount();
4436 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4438 wxListLineData
*line
= GetLine(i
);
4440 line
->GetItem( 0, item
);
4441 if (item
.m_data
== data
)
4448 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4450 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4452 size_t count
= GetItemCount();
4454 if ( HasFlag(wxLC_REPORT
) )
4456 size_t current
= y
/ GetLineHeight();
4457 if ( current
< count
)
4459 flags
= HitTestLine(current
, x
, y
);
4466 // TODO: optimize it too! this is less simple than for report view but
4467 // enumerating all items is still not a way to do it!!
4468 for ( size_t current
= 0; current
< count
; current
++ )
4470 flags
= HitTestLine(current
, x
, y
);
4479 // ----------------------------------------------------------------------------
4481 // ----------------------------------------------------------------------------
4483 void wxListMainWindow::InsertItem( wxListItem
&item
)
4485 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4487 size_t count
= GetItemCount();
4488 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4489 _T("invalid item index") );
4491 size_t id
= item
.m_itemId
;
4496 if ( HasFlag(wxLC_REPORT
) )
4499 ResetVisibleLinesRange();
4501 else if ( HasFlag(wxLC_LIST
) )
4503 else if ( HasFlag(wxLC_ICON
) )
4505 else if ( HasFlag(wxLC_SMALL_ICON
) )
4506 mode
= wxLC_ICON
; // no typo
4509 wxFAIL_MSG( _T("unknown mode") );
4512 wxListLineData
*line
= new wxListLineData(this);
4514 line
->SetItem( 0, item
);
4516 m_lines
.Insert( line
, id
);
4519 RefreshLines(id
, GetItemCount() - 1);
4522 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4525 if ( HasFlag(wxLC_REPORT
) )
4527 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4528 item
.m_width
= GetTextLength( item
.m_text
);
4529 wxListHeaderData
*column
= new wxListHeaderData( item
);
4530 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4532 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4533 m_columns
.Insert( node
, column
);
4537 m_columns
.Append( column
);
4540 // invalidate it as it has to be recalculated
4545 // ----------------------------------------------------------------------------
4547 // ----------------------------------------------------------------------------
4549 wxListCtrlCompare list_ctrl_compare_func_2
;
4550 long list_ctrl_compare_data
;
4552 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4554 wxListLineData
*line1
= *arg1
;
4555 wxListLineData
*line2
= *arg2
;
4557 line1
->GetItem( 0, item
);
4558 long data1
= item
.m_data
;
4559 line2
->GetItem( 0, item
);
4560 long data2
= item
.m_data
;
4561 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4564 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4566 list_ctrl_compare_func_2
= fn
;
4567 list_ctrl_compare_data
= data
;
4568 m_lines
.Sort( list_ctrl_compare_func_1
);
4572 // ----------------------------------------------------------------------------
4574 // ----------------------------------------------------------------------------
4576 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4578 // update our idea of which lines are shown when we redraw the window the
4580 ResetVisibleLinesRange();
4583 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4584 wxScrolledWindow::OnScroll(event
);
4586 HandleOnScroll( event
);
4589 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4591 wxGenericListCtrl
* lc
= GetListCtrl();
4592 wxCHECK_RET( lc
, _T("no listctrl window?") );
4594 lc
->m_headerWin
->Refresh();
4595 lc
->m_headerWin
->Update();
4599 int wxListMainWindow::GetCountPerPage() const
4601 if ( !m_linesPerPage
)
4603 wxConstCast(this, wxListMainWindow
)->
4604 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4607 return m_linesPerPage
;
4610 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4612 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4614 if ( m_lineFrom
== (size_t)-1 )
4616 size_t count
= GetItemCount();
4619 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4621 // this may happen if SetScrollbars() hadn't been called yet
4622 if ( m_lineFrom
>= count
)
4623 m_lineFrom
= count
- 1;
4625 // we redraw one extra line but this is needed to make the redrawing
4626 // logic work when there is a fractional number of lines on screen
4627 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4628 if ( m_lineTo
>= count
)
4629 m_lineTo
= count
- 1;
4631 else // empty control
4634 m_lineTo
= (size_t)-1;
4638 wxASSERT_MSG( IsEmpty() ||
4639 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4640 _T("GetVisibleLinesRange() returns incorrect result") );
4648 // -------------------------------------------------------------------------------------
4649 // wxGenericListCtrl
4650 // -------------------------------------------------------------------------------------
4652 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4654 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4655 EVT_SIZE(wxGenericListCtrl::OnSize
)
4656 EVT_IDLE(wxGenericListCtrl::OnIdle
)
4659 wxGenericListCtrl::wxGenericListCtrl()
4661 m_imageListNormal
= (wxImageListType
*) NULL
;
4662 m_imageListSmall
= (wxImageListType
*) NULL
;
4663 m_imageListState
= (wxImageListType
*) NULL
;
4665 m_ownsImageListNormal
=
4666 m_ownsImageListSmall
=
4667 m_ownsImageListState
= FALSE
;
4669 m_mainWin
= (wxListMainWindow
*) NULL
;
4670 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4673 wxGenericListCtrl::~wxGenericListCtrl()
4675 if (m_ownsImageListNormal
)
4676 delete m_imageListNormal
;
4677 if (m_ownsImageListSmall
)
4678 delete m_imageListSmall
;
4679 if (m_ownsImageListState
)
4680 delete m_imageListState
;
4683 void wxGenericListCtrl::CreateHeaderWindow()
4685 m_headerWin
= new wxListHeaderWindow
4687 this, -1, m_mainWin
,
4689 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4694 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4699 const wxValidator
&validator
,
4700 const wxString
&name
)
4704 m_imageListState
= (wxImageListType
*) NULL
;
4705 m_ownsImageListNormal
=
4706 m_ownsImageListSmall
=
4707 m_ownsImageListState
= FALSE
;
4709 m_mainWin
= (wxListMainWindow
*) NULL
;
4710 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4712 if ( !(style
& wxLC_MASK_TYPE
) )
4714 style
= style
| wxLC_LIST
;
4717 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4720 // don't create the inner window with the border
4721 style
&= ~wxBORDER_MASK
;
4723 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4725 if ( HasFlag(wxLC_REPORT
) )
4727 CreateHeaderWindow();
4729 if ( HasFlag(wxLC_NO_HEADER
) )
4731 // VZ: why do we create it at all then?
4732 m_headerWin
->Show( FALSE
);
4739 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4741 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4742 _T("wxLC_VIRTUAL can't be [un]set") );
4744 long flag
= GetWindowStyle();
4748 if (style
& wxLC_MASK_TYPE
)
4749 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4750 if (style
& wxLC_MASK_ALIGN
)
4751 flag
&= ~wxLC_MASK_ALIGN
;
4752 if (style
& wxLC_MASK_SORT
)
4753 flag
&= ~wxLC_MASK_SORT
;
4765 SetWindowStyleFlag( flag
);
4768 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4772 m_mainWin
->DeleteEverything();
4774 // has the header visibility changed?
4775 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4776 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4778 if ( hasHeader
!= willHaveHeader
)
4785 // don't delete, just hide, as we can reuse it later
4786 m_headerWin
->Show(FALSE
);
4788 //else: nothing to do
4790 else // must show header
4794 CreateHeaderWindow();
4796 else // already have it, just show
4798 m_headerWin
->Show( TRUE
);
4802 ResizeReportView(willHaveHeader
);
4806 wxWindow::SetWindowStyleFlag( flag
);
4809 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4811 m_mainWin
->GetColumn( col
, item
);
4815 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4817 m_mainWin
->SetColumn( col
, item
);
4821 int wxGenericListCtrl::GetColumnWidth( int col
) const
4823 return m_mainWin
->GetColumnWidth( col
);
4826 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4828 m_mainWin
->SetColumnWidth( col
, width
);
4832 int wxGenericListCtrl::GetCountPerPage() const
4834 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4837 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4839 m_mainWin
->GetItem( info
);
4843 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4845 m_mainWin
->SetItem( info
);
4849 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4852 info
.m_text
= label
;
4853 info
.m_mask
= wxLIST_MASK_TEXT
;
4854 info
.m_itemId
= index
;
4858 info
.m_image
= imageId
;
4859 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4861 m_mainWin
->SetItem(info
);
4865 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4867 return m_mainWin
->GetItemState( item
, stateMask
);
4870 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4872 m_mainWin
->SetItemState( item
, state
, stateMask
);
4876 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4879 info
.m_image
= image
;
4880 info
.m_mask
= wxLIST_MASK_IMAGE
;
4881 info
.m_itemId
= item
;
4882 m_mainWin
->SetItem( info
);
4886 wxString
wxGenericListCtrl::GetItemText( long item
) const
4888 return m_mainWin
->GetItemText(item
);
4891 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4893 m_mainWin
->SetItemText(item
, str
);
4896 long wxGenericListCtrl::GetItemData( long item
) const
4899 info
.m_itemId
= item
;
4900 m_mainWin
->GetItem( info
);
4904 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4907 info
.m_mask
= wxLIST_MASK_DATA
;
4908 info
.m_itemId
= item
;
4910 m_mainWin
->SetItem( info
);
4914 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4916 m_mainWin
->GetItemRect( item
, rect
);
4917 if ( m_mainWin
->HasHeader() )
4918 rect
.y
+= HEADER_HEIGHT
+ 1;
4922 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4924 m_mainWin
->GetItemPosition( item
, pos
);
4928 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4933 int wxGenericListCtrl::GetItemCount() const
4935 return m_mainWin
->GetItemCount();
4938 int wxGenericListCtrl::GetColumnCount() const
4940 return m_mainWin
->GetColumnCount();
4943 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4945 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4948 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4950 return m_mainWin
->GetItemSpacing( isSmall
);
4953 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4956 info
.m_itemId
= item
;
4957 info
.SetTextColour( col
);
4958 m_mainWin
->SetItem( info
);
4961 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4964 info
.m_itemId
= item
;
4965 m_mainWin
->GetItem( info
);
4966 return info
.GetTextColour();
4969 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4972 info
.m_itemId
= item
;
4973 info
.SetBackgroundColour( col
);
4974 m_mainWin
->SetItem( info
);
4977 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4980 info
.m_itemId
= item
;
4981 m_mainWin
->GetItem( info
);
4982 return info
.GetBackgroundColour();
4985 int wxGenericListCtrl::GetSelectedItemCount() const
4987 return m_mainWin
->GetSelectedItemCount();
4990 wxColour
wxGenericListCtrl::GetTextColour() const
4992 return GetForegroundColour();
4995 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4997 SetForegroundColour(col
);
5000 long wxGenericListCtrl::GetTopItem() const
5003 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5007 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5009 return m_mainWin
->GetNextItem( item
, geom
, state
);
5012 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
5014 if (which
== wxIMAGE_LIST_NORMAL
)
5016 return m_imageListNormal
;
5018 else if (which
== wxIMAGE_LIST_SMALL
)
5020 return m_imageListSmall
;
5022 else if (which
== wxIMAGE_LIST_STATE
)
5024 return m_imageListState
;
5026 return (wxImageListType
*) NULL
;
5029 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
5031 if ( which
== wxIMAGE_LIST_NORMAL
)
5033 if (m_ownsImageListNormal
) delete m_imageListNormal
;
5034 m_imageListNormal
= imageList
;
5035 m_ownsImageListNormal
= FALSE
;
5037 else if ( which
== wxIMAGE_LIST_SMALL
)
5039 if (m_ownsImageListSmall
) delete m_imageListSmall
;
5040 m_imageListSmall
= imageList
;
5041 m_ownsImageListSmall
= FALSE
;
5043 else if ( which
== wxIMAGE_LIST_STATE
)
5045 if (m_ownsImageListState
) delete m_imageListState
;
5046 m_imageListState
= imageList
;
5047 m_ownsImageListState
= FALSE
;
5050 m_mainWin
->SetImageList( imageList
, which
);
5053 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
5055 SetImageList(imageList
, which
);
5056 if ( which
== wxIMAGE_LIST_NORMAL
)
5057 m_ownsImageListNormal
= TRUE
;
5058 else if ( which
== wxIMAGE_LIST_SMALL
)
5059 m_ownsImageListSmall
= TRUE
;
5060 else if ( which
== wxIMAGE_LIST_STATE
)
5061 m_ownsImageListState
= TRUE
;
5064 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5069 bool wxGenericListCtrl::DeleteItem( long item
)
5071 m_mainWin
->DeleteItem( item
);
5075 bool wxGenericListCtrl::DeleteAllItems()
5077 m_mainWin
->DeleteAllItems();
5081 bool wxGenericListCtrl::DeleteAllColumns()
5083 size_t count
= m_mainWin
->m_columns
.GetCount();
5084 for ( size_t n
= 0; n
< count
; n
++ )
5090 void wxGenericListCtrl::ClearAll()
5092 m_mainWin
->DeleteEverything();
5095 bool wxGenericListCtrl::DeleteColumn( int col
)
5097 m_mainWin
->DeleteColumn( col
);
5099 // if we don't have the header any longer, we need to relayout the window
5100 if ( !GetColumnCount() )
5102 ResizeReportView(FALSE
/* no header */);
5108 void wxGenericListCtrl::Edit( long item
)
5110 m_mainWin
->EditLabel( item
);
5113 bool wxGenericListCtrl::EnsureVisible( long item
)
5115 m_mainWin
->EnsureVisible( item
);
5119 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5121 return m_mainWin
->FindItem( start
, str
, partial
);
5124 long wxGenericListCtrl::FindItem( long start
, long data
)
5126 return m_mainWin
->FindItem( start
, data
);
5129 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
5130 int WXUNUSED(direction
))
5135 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5137 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5140 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5142 m_mainWin
->InsertItem( info
);
5143 return info
.m_itemId
;
5146 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5149 info
.m_text
= label
;
5150 info
.m_mask
= wxLIST_MASK_TEXT
;
5151 info
.m_itemId
= index
;
5152 return InsertItem( info
);
5155 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5158 info
.m_mask
= wxLIST_MASK_IMAGE
;
5159 info
.m_image
= imageIndex
;
5160 info
.m_itemId
= index
;
5161 return InsertItem( info
);
5164 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5167 info
.m_text
= label
;
5168 info
.m_image
= imageIndex
;
5169 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5170 info
.m_itemId
= index
;
5171 return InsertItem( info
);
5174 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5176 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5178 m_mainWin
->InsertColumn( col
, item
);
5180 // if we hadn't had header before and have it now we need to relayout the
5182 if ( GetColumnCount() == 1 )
5184 ResizeReportView(TRUE
/* have header */);
5187 m_headerWin
->Refresh();
5192 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5193 int format
, int width
)
5196 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5197 item
.m_text
= heading
;
5200 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5201 item
.m_width
= width
;
5203 item
.m_format
= format
;
5205 return InsertColumn( col
, item
);
5208 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5214 // fn is a function which takes 3 long arguments: item1, item2, data.
5215 // item1 is the long data associated with a first item (NOT the index).
5216 // item2 is the long data associated with a second item (NOT the index).
5217 // data is the same value as passed to SortItems.
5218 // The return value is a negative number if the first item should precede the second
5219 // item, a positive number of the second item should precede the first,
5220 // or zero if the two items are equivalent.
5221 // data is arbitrary data to be passed to the sort function.
5223 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5225 m_mainWin
->SortItems( fn
, data
);
5229 // ----------------------------------------------------------------------------
5231 // ----------------------------------------------------------------------------
5233 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5238 ResizeReportView(m_mainWin
->HasHeader());
5240 m_mainWin
->RecalculatePositions();
5243 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5246 GetClientSize( &cw
, &ch
);
5250 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5251 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5253 else // no header window
5255 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5259 void wxGenericListCtrl::OnIdle( wxIdleEvent
& event
)
5263 // do it only if needed
5264 if ( !m_mainWin
->m_dirty
)
5267 m_mainWin
->RecalculatePositions();
5270 // ----------------------------------------------------------------------------
5272 // ----------------------------------------------------------------------------
5274 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5278 m_mainWin
->SetBackgroundColour( colour
);
5279 m_mainWin
->m_dirty
= TRUE
;
5285 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5287 if ( !wxWindow::SetForegroundColour( colour
) )
5292 m_mainWin
->SetForegroundColour( colour
);
5293 m_mainWin
->m_dirty
= TRUE
;
5298 m_headerWin
->SetForegroundColour( colour
);
5304 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5306 if ( !wxWindow::SetFont( font
) )
5311 m_mainWin
->SetFont( font
);
5312 m_mainWin
->m_dirty
= TRUE
;
5317 m_headerWin
->SetFont( font
);
5323 // ----------------------------------------------------------------------------
5324 // methods forwarded to m_mainWin
5325 // ----------------------------------------------------------------------------
5327 #if wxUSE_DRAG_AND_DROP
5329 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5331 m_mainWin
->SetDropTarget( dropTarget
);
5334 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5336 return m_mainWin
->GetDropTarget();
5339 #endif // wxUSE_DRAG_AND_DROP
5341 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5343 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5346 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5348 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5351 wxColour
wxGenericListCtrl::GetForegroundColour() const
5353 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5356 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5359 return m_mainWin
->PopupMenu( menu
, x
, y
);
5362 #endif // wxUSE_MENUS
5365 void wxGenericListCtrl::SetFocus()
5367 /* The test in window.cpp fails as we are a composite
5368 window, so it checks against "this", but not m_mainWin. */
5369 if ( FindFocus() != this )
5370 m_mainWin
->SetFocus();
5373 // ----------------------------------------------------------------------------
5374 // virtual list control support
5375 // ----------------------------------------------------------------------------
5377 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5379 // this is a pure virtual function, in fact - which is not really pure
5380 // because the controls which are not virtual don't need to implement it
5381 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5383 return wxEmptyString
;
5386 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5389 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5394 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5396 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5397 _T("invalid item index in OnGetItemAttr()") );
5399 // no attributes by default
5403 void wxGenericListCtrl::SetItemCount(long count
)
5405 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5407 m_mainWin
->SetItemCount(count
);
5410 void wxGenericListCtrl::RefreshItem(long item
)
5412 m_mainWin
->RefreshLine(item
);
5415 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5417 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5420 void wxGenericListCtrl::Freeze()
5422 m_mainWin
->Freeze();
5425 void wxGenericListCtrl::Thaw()
5430 #endif // wxUSE_LISTCTRL