1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 // under Win32 we always use the native version and also may use the generic
27 // one, however some things should be done only if we use only the generic
29 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
30 #define HAVE_NATIVE_LISTCTRL
33 // if we have the native control, wx/listctrl.h declares it and not this one
34 #ifdef HAVE_NATIVE_LISTCTRL
35 #include "wx/generic/listctrl.h"
36 #else // !HAVE_NATIVE_LISTCTRL
37 #include "wx/listctrl.h"
39 // if we have a native version, its implementation file does all this
40 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
41 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
42 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
44 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
45 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
48 #include "wx/dynarray.h"
50 #include "wx/dcscreen.h"
51 #include "wx/textctrl.h"
52 #include "wx/listbox.h"
56 #include "wx/selstore.h"
57 #include "wx/renderer.h"
60 #include "wx/mac/private.h"
64 // NOTE: If using the wxListBox visual attributes works everywhere then this can
65 // be removed, as well as the #else case below.
66 #define _USE_VISATTR 0
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
77 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
79 #if WXWIN_COMPATIBILITY_2_4
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // // the height of the header window (FIXME: should depend on its font!)
103 // static const int HEADER_HEIGHT = 23;
105 static const int SCROLL_UNIT_X
= 15;
107 // the spacing between the lines (in report mode)
108 static const int LINE_SPACING
= 0;
110 // extra margins around the text label
111 static const int EXTRA_WIDTH
= 4;
112 static const int EXTRA_HEIGHT
= 4;
114 // margin between the window and the items
115 static const int EXTRA_BORDER_X
= 2;
116 static const int EXTRA_BORDER_Y
= 2;
118 // offset for the header window
119 static const int HEADER_OFFSET_X
= 1;
120 static const int HEADER_OFFSET_Y
= 1;
122 // margin between rows of icons in [small] icon view
123 static const int MARGIN_BETWEEN_ROWS
= 6;
125 // when autosizing the columns, add some slack
126 static const int AUTOSIZE_COL_MARGIN
= 10;
128 // default width for the header columns
129 static const int WIDTH_COL_DEFAULT
= 80;
131 // the space between the image and the text in the report mode
132 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
134 // ============================================================================
136 // ============================================================================
138 //-----------------------------------------------------------------------------
139 // wxColWidthInfo (internal)
140 //-----------------------------------------------------------------------------
142 struct wxColWidthInfo
145 bool bNeedsUpdate
; // only set to true when an item whose
146 // width == nMaxWidth is removed
148 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
151 bNeedsUpdate
= needsUpdate
;
155 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
157 //-----------------------------------------------------------------------------
158 // wxListItemData (internal)
159 //-----------------------------------------------------------------------------
161 class WXDLLEXPORT wxListItemData
164 wxListItemData(wxListMainWindow
*owner
);
167 void SetItem( const wxListItem
&info
);
168 void SetImage( int image
) { m_image
= image
; }
169 void SetData( wxUIntPtr data
) { m_data
= data
; }
170 void SetPosition( int x
, int y
);
171 void SetSize( int width
, int height
);
173 bool HasText() const { return !m_text
.empty(); }
174 const wxString
& GetText() const { return m_text
; }
175 void SetText(const wxString
& text
) { m_text
= text
; }
177 // we can't use empty string for measuring the string width/height, so
178 // always return something
179 wxString
GetTextForMeasuring() const
181 wxString s
= GetText();
188 bool IsHit( int x
, int y
) const;
192 int GetWidth() const;
193 int GetHeight() const;
195 int GetImage() const { return m_image
; }
196 bool HasImage() const { return GetImage() != -1; }
198 void GetItem( wxListItem
&info
) const;
200 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
201 wxListItemAttr
*GetAttr() const { return m_attr
; }
204 // the item image or -1
207 // user data associated with the item
210 // the item coordinates are not used in report mode; instead this pointer is
211 // NULL and the owner window is used to retrieve the item position and size
214 // the list ctrl we are in
215 wxListMainWindow
*m_owner
;
217 // custom attributes or NULL
218 wxListItemAttr
*m_attr
;
221 // common part of all ctors
227 //-----------------------------------------------------------------------------
228 // wxListHeaderData (internal)
229 //-----------------------------------------------------------------------------
231 class WXDLLEXPORT wxListHeaderData
: public wxObject
235 wxListHeaderData( const wxListItem
&info
);
236 void SetItem( const wxListItem
&item
);
237 void SetPosition( int x
, int y
);
238 void SetWidth( int w
);
239 void SetFormat( int format
);
240 void SetHeight( int h
);
241 bool HasImage() const;
243 bool HasText() const { return !m_text
.empty(); }
244 const wxString
& GetText() const { return m_text
; }
245 void SetText(const wxString
& text
) { m_text
= text
; }
247 void GetItem( wxListItem
&item
);
249 bool IsHit( int x
, int y
) const;
250 int GetImage() const;
251 int GetWidth() const;
252 int GetFormat() const;
268 //-----------------------------------------------------------------------------
269 // wxListLineData (internal)
270 //-----------------------------------------------------------------------------
272 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
273 #include "wx/listimpl.cpp"
274 WX_DEFINE_LIST(wxListItemDataList
)
279 // the list of subitems: only may have more than one item in report mode
280 wxListItemDataList m_items
;
282 // this is not used in report view
294 // the part to be highlighted
295 wxRect m_rectHighlight
;
297 // extend all our rects to be centered inside the one of given width
298 void ExtendWidth(wxCoord w
)
300 wxASSERT_MSG( m_rectAll
.width
<= w
,
301 _T("width can only be increased") );
304 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
305 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
306 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
311 // is this item selected? [NB: not used in virtual mode]
314 // back pointer to the list ctrl
315 wxListMainWindow
*m_owner
;
318 wxListLineData(wxListMainWindow
*owner
);
322 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
326 // are we in report mode?
327 inline bool InReportView() const;
329 // are we in virtual report mode?
330 inline bool IsVirtual() const;
332 // these 2 methods shouldn't be called for report view controls, in that
333 // case we determine our position/size ourselves
335 // calculate the size of the line
336 void CalculateSize( wxDC
*dc
, int spacing
);
338 // remember the position this line appears at
339 void SetPosition( int x
, int y
, int spacing
);
343 void SetImage( int image
) { SetImage(0, image
); }
344 int GetImage() const { return GetImage(0); }
345 void SetImage( int index
, int image
);
346 int GetImage( int index
) const;
348 bool HasImage() const { return GetImage() != -1; }
349 bool HasText() const { return !GetText(0).empty(); }
351 void SetItem( int index
, const wxListItem
&info
);
352 void GetItem( int index
, wxListItem
&info
);
354 wxString
GetText(int index
) const;
355 void SetText( int index
, const wxString
& s
);
357 wxListItemAttr
*GetAttr() const;
358 void SetAttr(wxListItemAttr
*attr
);
360 // return true if the highlighting really changed
361 bool Highlight( bool on
);
363 void ReverseHighlight();
365 bool IsHighlighted() const
367 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
369 return m_highlighted
;
372 // draw the line on the given DC in icon/list mode
373 void Draw( wxDC
*dc
);
375 // the same in report mode
376 void DrawInReportMode( wxDC
*dc
,
378 const wxRect
& rectHL
,
382 // set the line to contain num items (only can be > 1 in report mode)
383 void InitItems( int num
);
385 // get the mode (i.e. style) of the list control
386 inline int GetMode() const;
388 // prepare the DC for drawing with these item's attributes, return true if
389 // we need to draw the items background to highlight it, false otherwise
390 bool SetAttributes(wxDC
*dc
,
391 const wxListItemAttr
*attr
,
394 // draw the text on the DC with the correct justification; also add an
395 // ellipsis if the text is too large to fit in the current width
396 void DrawTextFormatted(wxDC
*dc
,
397 const wxString
&text
,
400 int yMid
, // this is middle, not top, of the text
404 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
405 #include "wx/arrimpl.cpp"
406 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
408 //-----------------------------------------------------------------------------
409 // wxListHeaderWindow (internal)
410 //-----------------------------------------------------------------------------
412 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
415 wxListMainWindow
*m_owner
;
416 const wxCursor
*m_currentCursor
;
417 wxCursor
*m_resizeCursor
;
420 // column being resized or -1
423 // divider line position in logical (unscrolled) coords
426 // minimal position beyond which the divider line
427 // can't be dragged in logical coords
431 wxListHeaderWindow();
433 wxListHeaderWindow( wxWindow
*win
,
435 wxListMainWindow
*owner
,
436 const wxPoint
&pos
= wxDefaultPosition
,
437 const wxSize
&size
= wxDefaultSize
,
439 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
441 virtual ~wxListHeaderWindow();
444 void AdjustDC( wxDC
& dc
);
446 void OnPaint( wxPaintEvent
&event
);
447 void OnMouse( wxMouseEvent
&event
);
448 void OnSetFocus( wxFocusEvent
&event
);
454 // common part of all ctors
457 // generate and process the list event of the given type, return true if
458 // it wasn't vetoed, i.e. if we should proceed
459 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
461 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
462 DECLARE_EVENT_TABLE()
465 //-----------------------------------------------------------------------------
466 // wxListRenameTimer (internal)
467 //-----------------------------------------------------------------------------
469 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
472 wxListMainWindow
*m_owner
;
475 wxListRenameTimer( wxListMainWindow
*owner
);
479 //-----------------------------------------------------------------------------
480 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
481 //-----------------------------------------------------------------------------
483 class WXDLLEXPORT wxListTextCtrlWrapper
: public wxEvtHandler
486 // NB: text must be a valid object but not Create()d yet
487 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
491 wxTextCtrl
*GetText() const { return m_text
; }
493 void AcceptChangesAndFinish();
496 void OnChar( wxKeyEvent
&event
);
497 void OnKeyUp( wxKeyEvent
&event
);
498 void OnKillFocus( wxFocusEvent
&event
);
500 bool AcceptChanges();
504 wxListMainWindow
*m_owner
;
506 wxString m_startValue
;
509 bool m_aboutToFinish
;
511 DECLARE_EVENT_TABLE()
514 //-----------------------------------------------------------------------------
515 // wxListMainWindow (internal)
516 //-----------------------------------------------------------------------------
518 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
519 #include "wx/listimpl.cpp"
520 WX_DEFINE_LIST(wxListHeaderDataList
)
522 class wxListMainWindow
: public wxScrolledWindow
526 wxListMainWindow( wxWindow
*parent
,
528 const wxPoint
& pos
= wxDefaultPosition
,
529 const wxSize
& size
= wxDefaultSize
,
531 const wxString
&name
= _T("listctrlmainwindow") );
533 virtual ~wxListMainWindow();
535 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
537 // return true if this is a virtual list control
538 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
540 // return true if the control is in report mode
541 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
543 // return true if we are in single selection mode, false if multi sel
544 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
546 // do we have a header window?
547 bool HasHeader() const
548 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
550 void HighlightAll( bool on
);
552 // all these functions only do something if the line is currently visible
554 // change the line "selected" state, return true if it really changed
555 bool HighlightLine( size_t line
, bool highlight
= true);
557 // as HighlightLine() but do it for the range of lines: this is incredibly
558 // more efficient for virtual list controls!
560 // NB: unlike HighlightLine() this one does refresh the lines on screen
561 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
563 // toggle the line state and refresh it
564 void ReverseHighlight( size_t line
)
565 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
567 // return true if the line is highlighted
568 bool IsHighlighted(size_t line
) const;
570 // refresh one or several lines at once
571 void RefreshLine( size_t line
);
572 void RefreshLines( size_t lineFrom
, size_t lineTo
);
574 // refresh all selected items
575 void RefreshSelected();
577 // refresh all lines below the given one: the difference with
578 // RefreshLines() is that the index here might not be a valid one (happens
579 // when the last line is deleted)
580 void RefreshAfter( size_t lineFrom
);
582 // the methods which are forwarded to wxListLineData itself in list/icon
583 // modes but are here because the lines don't store their positions in the
586 // get the bound rect for the entire line
587 wxRect
GetLineRect(size_t line
) const;
589 // get the bound rect of the label
590 wxRect
GetLineLabelRect(size_t line
) const;
592 // get the bound rect of the items icon (only may be called if we do have
594 wxRect
GetLineIconRect(size_t line
) const;
596 // get the rect to be highlighted when the item has focus
597 wxRect
GetLineHighlightRect(size_t line
) const;
599 // get the size of the total line rect
600 wxSize
GetLineSize(size_t line
) const
601 { return GetLineRect(line
).GetSize(); }
603 // return the hit code for the corresponding position (in this line)
604 long HitTestLine(size_t line
, int x
, int y
) const;
606 // bring the selected item into view, scrolling to it if necessary
607 void MoveToItem(size_t item
);
609 // bring the current item into view
610 void MoveToFocus() { MoveToItem(m_current
); }
612 // start editing the label of the given item
613 wxTextCtrl
*EditLabel(long item
,
614 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
615 wxTextCtrl
*GetEditControl() const
617 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
620 void FinishEditing(wxTextCtrl
*text
)
623 m_textctrlWrapper
= NULL
;
624 SetFocusIgnoringChildren();
627 // suspend/resume redrawing the control
631 void OnRenameTimer();
632 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
633 void OnRenameCancelled(size_t itemEdit
);
635 void OnMouse( wxMouseEvent
&event
);
637 // called to switch the selection from the current item to newCurrent,
638 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
640 void OnChar( wxKeyEvent
&event
);
641 void OnKeyDown( wxKeyEvent
&event
);
642 void OnSetFocus( wxFocusEvent
&event
);
643 void OnKillFocus( wxFocusEvent
&event
);
644 void OnScroll( wxScrollWinEvent
& event
);
646 void OnPaint( wxPaintEvent
&event
);
648 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
649 void GetImageSize( int index
, int &width
, int &height
) const;
650 int GetTextLength( const wxString
&s
) const;
652 void SetImageList( wxImageListType
*imageList
, int which
);
653 void SetItemSpacing( int spacing
, bool isSmall
= false );
654 int GetItemSpacing( bool isSmall
= false );
656 void SetColumn( int col
, wxListItem
&item
);
657 void SetColumnWidth( int col
, int width
);
658 void GetColumn( int col
, wxListItem
&item
) const;
659 int GetColumnWidth( int col
) const;
660 int GetColumnCount() const { return m_columns
.GetCount(); }
662 // returns the sum of the heights of all columns
663 int GetHeaderWidth() const;
665 int GetCountPerPage() const;
667 void SetItem( wxListItem
&item
);
668 void GetItem( wxListItem
&item
) const;
669 void SetItemState( long item
, long state
, long stateMask
);
670 void SetItemStateAll( long state
, long stateMask
);
671 int GetItemState( long item
, long stateMask
) const;
672 void GetItemRect( long index
, wxRect
&rect
) const;
673 wxRect
GetViewRect() const;
674 bool GetItemPosition( long item
, wxPoint
& pos
) const;
675 int GetSelectedItemCount() const;
677 wxString
GetItemText(long item
) const
680 info
.m_mask
= wxLIST_MASK_TEXT
;
681 info
.m_itemId
= item
;
686 void SetItemText(long item
, const wxString
& value
)
689 info
.m_mask
= wxLIST_MASK_TEXT
;
690 info
.m_itemId
= item
;
695 // set the scrollbars and update the positions of the items
696 void RecalculatePositions(bool noRefresh
= false);
698 // refresh the window and the header
701 long GetNextItem( long item
, int geometry
, int state
) const;
702 void DeleteItem( long index
);
703 void DeleteAllItems();
704 void DeleteColumn( int col
);
705 void DeleteEverything();
706 void EnsureVisible( long index
);
707 long FindItem( long start
, const wxString
& str
, bool partial
= false );
708 long FindItem( long start
, wxUIntPtr data
);
709 long FindItem( const wxPoint
& pt
);
710 long HitTest( int x
, int y
, int &flags
) const;
711 void InsertItem( wxListItem
&item
);
712 void InsertColumn( long col
, wxListItem
&item
);
713 int GetItemWidthWithImage(wxListItem
* item
);
714 void SortItems( wxListCtrlCompare fn
, long data
);
716 size_t GetItemCount() const;
717 bool IsEmpty() const { return GetItemCount() == 0; }
718 void SetItemCount(long count
);
720 // change the current (== focused) item, send a notification event
721 void ChangeCurrent(size_t current
);
722 void ResetCurrent() { ChangeCurrent((size_t)-1); }
723 bool HasCurrent() const { return m_current
!= (size_t)-1; }
725 // send out a wxListEvent
726 void SendNotify( size_t line
,
728 const wxPoint
& point
= wxDefaultPosition
);
730 // override base class virtual to reset m_lineHeight when the font changes
731 virtual bool SetFont(const wxFont
& font
)
733 if ( !wxScrolledWindow::SetFont(font
) )
741 // these are for wxListLineData usage only
743 // get the backpointer to the list ctrl
744 wxGenericListCtrl
*GetListCtrl() const
746 return wxStaticCast(GetParent(), wxGenericListCtrl
);
749 // get the height of all lines (assuming they all do have the same height)
750 wxCoord
GetLineHeight() const;
752 // get the y position of the given line (only for report view)
753 wxCoord
GetLineY(size_t line
) const;
755 // get the brush to use for the item highlighting
756 wxBrush
*GetHighlightBrush() const
758 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
762 // the array of all line objects for a non virtual list control (for the
763 // virtual list control we only ever use m_lines[0])
764 wxListLineDataArray m_lines
;
766 // the list of column objects
767 wxListHeaderDataList m_columns
;
769 // currently focused item or -1
772 // the number of lines per page
775 // this flag is set when something which should result in the window
776 // redrawing happens (i.e. an item was added or deleted, or its appearance
777 // changed) and OnPaint() doesn't redraw the window while it is set which
778 // allows to minimize the number of repaintings when a lot of items are
779 // being added. The real repainting occurs only after the next OnIdle()
783 wxColour
*m_highlightColour
;
784 wxImageListType
*m_small_image_list
;
785 wxImageListType
*m_normal_image_list
;
787 int m_normal_spacing
;
791 wxTimer
*m_renameTimer
;
795 ColWidthArray m_aColWidths
;
797 // for double click logic
798 size_t m_lineLastClicked
,
799 m_lineBeforeLastClicked
,
800 m_lineSelectSingleOnUp
;
803 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
805 // the total count of items in a virtual list control
808 // the object maintaining the items selection state, only used in virtual
810 wxSelectionStore m_selStore
;
812 // common part of all ctors
815 // get the line data for the given index
816 wxListLineData
*GetLine(size_t n
) const
818 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
822 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
829 // get a dummy line which can be used for geometry calculations and such:
830 // you must use GetLine() if you want to really draw the line
831 wxListLineData
*GetDummyLine() const;
833 // cache the line data of the n-th line in m_lines[0]
834 void CacheLineData(size_t line
);
836 // get the range of visible lines
837 void GetVisibleLinesRange(size_t *from
, size_t *to
);
839 // force us to recalculate the range of visible lines
840 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
842 // get the colour to be used for drawing the rules
843 wxColour
GetRuleColour() const
845 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
849 // initialize the current item if needed
850 void UpdateCurrent();
852 // delete all items but don't refresh: called from dtor
853 void DoDeleteAllItems();
855 // the height of one line using the current font
856 wxCoord m_lineHeight
;
858 // the total header width or 0 if not calculated yet
859 wxCoord m_headerWidth
;
861 // the first and last lines being shown on screen right now (inclusive),
862 // both may be -1 if they must be calculated so never access them directly:
863 // use GetVisibleLinesRange() above instead
867 // the brushes to use for item highlighting when we do/don't have focus
868 wxBrush
*m_highlightBrush
,
869 *m_highlightUnfocusedBrush
;
871 // if this is > 0, the control is frozen and doesn't redraw itself
872 size_t m_freezeCount
;
874 // wrapper around the text control currently used for in place editing or
875 // NULL if no item is being edited
876 wxListTextCtrlWrapper
*m_textctrlWrapper
;
879 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
880 DECLARE_EVENT_TABLE()
882 friend class wxGenericListCtrl
;
886 wxListItemData::~wxListItemData()
888 // in the virtual list control the attributes are managed by the main
889 // program, so don't delete them
890 if ( !m_owner
->IsVirtual() )
896 void wxListItemData::Init()
904 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
910 if ( owner
->InReportView() )
916 void wxListItemData::SetItem( const wxListItem
&info
)
918 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
919 SetText(info
.m_text
);
920 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
921 m_image
= info
.m_image
;
922 if ( info
.m_mask
& wxLIST_MASK_DATA
)
923 m_data
= info
.m_data
;
925 if ( info
.HasAttributes() )
928 *m_attr
= *info
.GetAttributes();
930 m_attr
= new wxListItemAttr(*info
.GetAttributes());
938 m_rect
->width
= info
.m_width
;
942 void wxListItemData::SetPosition( int x
, int y
)
944 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
950 void wxListItemData::SetSize( int width
, int height
)
952 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
955 m_rect
->width
= width
;
957 m_rect
->height
= height
;
960 bool wxListItemData::IsHit( int x
, int y
) const
962 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
964 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
967 int wxListItemData::GetX() const
969 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
974 int wxListItemData::GetY() const
976 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
981 int wxListItemData::GetWidth() const
983 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
985 return m_rect
->width
;
988 int wxListItemData::GetHeight() const
990 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
992 return m_rect
->height
;
995 void wxListItemData::GetItem( wxListItem
&info
) const
997 long mask
= info
.m_mask
;
999 // by default, get everything for backwards compatibility
1002 if ( mask
& wxLIST_MASK_TEXT
)
1003 info
.m_text
= m_text
;
1004 if ( mask
& wxLIST_MASK_IMAGE
)
1005 info
.m_image
= m_image
;
1006 if ( mask
& wxLIST_MASK_DATA
)
1007 info
.m_data
= m_data
;
1011 if ( m_attr
->HasTextColour() )
1012 info
.SetTextColour(m_attr
->GetTextColour());
1013 if ( m_attr
->HasBackgroundColour() )
1014 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1015 if ( m_attr
->HasFont() )
1016 info
.SetFont(m_attr
->GetFont());
1020 //-----------------------------------------------------------------------------
1022 //-----------------------------------------------------------------------------
1024 void wxListHeaderData::Init()
1035 wxListHeaderData::wxListHeaderData()
1040 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1047 void wxListHeaderData::SetItem( const wxListItem
&item
)
1049 m_mask
= item
.m_mask
;
1051 if ( m_mask
& wxLIST_MASK_TEXT
)
1052 m_text
= item
.m_text
;
1054 if ( m_mask
& wxLIST_MASK_IMAGE
)
1055 m_image
= item
.m_image
;
1057 if ( m_mask
& wxLIST_MASK_FORMAT
)
1058 m_format
= item
.m_format
;
1060 if ( m_mask
& wxLIST_MASK_WIDTH
)
1061 SetWidth(item
.m_width
);
1064 void wxListHeaderData::SetPosition( int x
, int y
)
1070 void wxListHeaderData::SetHeight( int h
)
1075 void wxListHeaderData::SetWidth( int w
)
1077 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1080 void wxListHeaderData::SetFormat( int format
)
1085 bool wxListHeaderData::HasImage() const
1087 return m_image
!= -1;
1090 bool wxListHeaderData::IsHit( int x
, int y
) const
1092 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1095 void wxListHeaderData::GetItem( wxListItem
& item
)
1097 item
.m_mask
= m_mask
;
1098 item
.m_text
= m_text
;
1099 item
.m_image
= m_image
;
1100 item
.m_format
= m_format
;
1101 item
.m_width
= m_width
;
1104 int wxListHeaderData::GetImage() const
1109 int wxListHeaderData::GetWidth() const
1114 int wxListHeaderData::GetFormat() const
1119 //-----------------------------------------------------------------------------
1121 //-----------------------------------------------------------------------------
1123 inline int wxListLineData::GetMode() const
1125 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1128 inline bool wxListLineData::InReportView() const
1130 return m_owner
->HasFlag(wxLC_REPORT
);
1133 inline bool wxListLineData::IsVirtual() const
1135 return m_owner
->IsVirtual();
1138 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1142 if ( InReportView() )
1145 m_gi
= new GeometryInfo
;
1147 m_highlighted
= false;
1149 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1152 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1154 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1155 wxCHECK_RET( node
, _T("no subitems at all??") );
1157 wxListItemData
*item
= node
->GetData();
1162 switch ( GetMode() )
1165 case wxLC_SMALL_ICON
:
1166 m_gi
->m_rectAll
.width
= spacing
;
1168 s
= item
->GetText();
1173 m_gi
->m_rectLabel
.width
=
1174 m_gi
->m_rectLabel
.height
= 0;
1178 dc
->GetTextExtent( s
, &lw
, &lh
);
1182 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1184 m_gi
->m_rectAll
.width
= lw
;
1186 m_gi
->m_rectLabel
.width
= lw
;
1187 m_gi
->m_rectLabel
.height
= lh
;
1190 if (item
->HasImage())
1193 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1194 m_gi
->m_rectIcon
.width
= w
+ 8;
1195 m_gi
->m_rectIcon
.height
= h
+ 8;
1197 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1198 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1199 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1200 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1203 if ( item
->HasText() )
1205 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1206 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1208 else // no text, highlight the icon
1210 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1211 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1216 s
= item
->GetTextForMeasuring();
1218 dc
->GetTextExtent( s
, &lw
, &lh
);
1222 m_gi
->m_rectLabel
.width
= lw
;
1223 m_gi
->m_rectLabel
.height
= lh
;
1225 m_gi
->m_rectAll
.width
= lw
;
1226 m_gi
->m_rectAll
.height
= lh
;
1228 if (item
->HasImage())
1231 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1232 m_gi
->m_rectIcon
.width
= w
;
1233 m_gi
->m_rectIcon
.height
= h
;
1235 m_gi
->m_rectAll
.width
+= 4 + w
;
1236 if (h
> m_gi
->m_rectAll
.height
)
1237 m_gi
->m_rectAll
.height
= h
;
1240 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1241 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1245 wxFAIL_MSG( _T("unexpected call to SetSize") );
1249 wxFAIL_MSG( _T("unknown mode") );
1254 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1256 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1257 wxCHECK_RET( node
, _T("no subitems at all??") );
1259 wxListItemData
*item
= node
->GetData();
1261 switch ( GetMode() )
1264 case wxLC_SMALL_ICON
:
1265 m_gi
->m_rectAll
.x
= x
;
1266 m_gi
->m_rectAll
.y
= y
;
1268 if ( item
->HasImage() )
1270 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1271 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1272 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1275 if ( item
->HasText() )
1277 if (m_gi
->m_rectAll
.width
> spacing
)
1278 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1280 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1281 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1282 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1283 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1285 else // no text, highlight the icon
1287 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1288 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1293 m_gi
->m_rectAll
.x
= x
;
1294 m_gi
->m_rectAll
.y
= y
;
1296 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1297 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1298 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1300 if (item
->HasImage())
1302 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1303 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1304 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1308 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1313 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1317 wxFAIL_MSG( _T("unknown mode") );
1322 void wxListLineData::InitItems( int num
)
1324 for (int i
= 0; i
< num
; i
++)
1325 m_items
.Append( new wxListItemData(m_owner
) );
1328 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1330 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1331 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1333 wxListItemData
*item
= node
->GetData();
1334 item
->SetItem( info
);
1337 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1339 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1342 wxListItemData
*item
= node
->GetData();
1343 item
->GetItem( info
);
1347 wxString
wxListLineData::GetText(int index
) const
1351 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1354 wxListItemData
*item
= node
->GetData();
1355 s
= item
->GetText();
1361 void wxListLineData::SetText( int index
, const wxString
& s
)
1363 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1366 wxListItemData
*item
= node
->GetData();
1371 void wxListLineData::SetImage( int index
, int image
)
1373 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1374 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1376 wxListItemData
*item
= node
->GetData();
1377 item
->SetImage(image
);
1380 int wxListLineData::GetImage( int index
) const
1382 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1383 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1385 wxListItemData
*item
= node
->GetData();
1386 return item
->GetImage();
1389 wxListItemAttr
*wxListLineData::GetAttr() const
1391 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1392 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1394 wxListItemData
*item
= node
->GetData();
1395 return item
->GetAttr();
1398 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1400 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1401 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1403 wxListItemData
*item
= node
->GetData();
1404 item
->SetAttr(attr
);
1407 bool wxListLineData::SetAttributes(wxDC
*dc
,
1408 const wxListItemAttr
*attr
,
1411 wxWindow
*listctrl
= m_owner
->GetParent();
1415 // don't use foreground colour for drawing highlighted items - this might
1416 // make them completely invisible (and there is no way to do bit
1417 // arithmetics on wxColour, unfortunately)
1420 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1421 else if ( attr
&& attr
->HasTextColour() )
1422 colText
= attr
->GetTextColour();
1424 colText
= listctrl
->GetForegroundColour();
1426 dc
->SetTextForeground(colText
);
1430 if ( attr
&& attr
->HasFont() )
1431 font
= attr
->GetFont();
1433 font
= listctrl
->GetFont();
1438 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1439 if ( highlighted
|| hasBgCol
)
1442 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1444 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1446 dc
->SetPen( *wxTRANSPARENT_PEN
);
1454 void wxListLineData::Draw( wxDC
*dc
)
1456 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1457 wxCHECK_RET( node
, _T("no subitems at all??") );
1459 bool highlighted
= IsHighlighted();
1461 wxListItemAttr
*attr
= GetAttr();
1463 if ( SetAttributes(dc
, attr
, highlighted
) )
1464 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1466 // just for debugging to better see where the items are
1468 dc
->SetPen(*wxRED_PEN
);
1469 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1470 dc
->DrawRectangle( m_gi
->m_rectAll
);
1471 dc
->SetPen(*wxGREEN_PEN
);
1472 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1475 wxListItemData
*item
= node
->GetData();
1476 if (item
->HasImage())
1478 // centre the image inside our rectangle, this looks nicer when items
1479 // ae aligned in a row
1480 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1482 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1485 if (item
->HasText())
1487 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1489 wxDCClipper
clipper(*dc
, rectLabel
);
1490 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1494 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1496 const wxRect
& rectHL
,
1499 // TODO: later we should support setting different attributes for
1500 // different columns - to do it, just add "col" argument to
1501 // GetAttr() and move these lines into the loop below
1502 wxListItemAttr
*attr
= GetAttr();
1503 if ( SetAttributes(dc
, attr
, highlighted
) )
1504 dc
->DrawRectangle( rectHL
);
1506 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1507 yMid
= rect
.y
+ rect
.height
/2;
1510 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1512 node
= node
->GetNext(), col
++ )
1514 wxListItemData
*item
= node
->GetData();
1516 int width
= m_owner
->GetColumnWidth(col
);
1520 if ( item
->HasImage() )
1523 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1524 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1526 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1532 if ( item
->HasText() )
1533 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1537 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1538 const wxString
&text
,
1545 dc
->GetTextExtent(text
, &w
, &h
);
1547 const wxCoord y
= yMid
- (h
+ 1)/2;
1549 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1551 // determine if the string can fit inside the current width
1554 // it can, draw it using the items alignment
1556 m_owner
->GetColumn(col
, item
);
1557 switch ( item
.GetAlign() )
1559 case wxLIST_FORMAT_LEFT
:
1563 case wxLIST_FORMAT_RIGHT
:
1567 case wxLIST_FORMAT_CENTER
:
1568 x
+= (width
- w
) / 2;
1572 wxFAIL_MSG( _T("unknown list item format") );
1576 dc
->DrawText(text
, x
, y
);
1578 else // otherwise, truncate and add an ellipsis if possible
1580 // determine the base width
1581 wxString
ellipsis(wxT("..."));
1583 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1585 // continue until we have enough space or only one character left
1587 size_t len
= text
.length();
1588 wxString drawntext
= text
.Left(len
);
1591 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1592 drawntext
.RemoveLast();
1595 if (w
+ base_w
<= width
)
1599 // if still not enough space, remove ellipsis characters
1600 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1602 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1603 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1606 // now draw the text
1607 dc
->DrawText(drawntext
, x
, y
);
1608 dc
->DrawText(ellipsis
, x
+ w
, y
);
1612 bool wxListLineData::Highlight( bool on
)
1614 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1616 if ( on
== m_highlighted
)
1624 void wxListLineData::ReverseHighlight( void )
1626 Highlight(!IsHighlighted());
1629 //-----------------------------------------------------------------------------
1630 // wxListHeaderWindow
1631 //-----------------------------------------------------------------------------
1633 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1635 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1636 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1637 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1638 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1641 void wxListHeaderWindow::Init()
1643 m_currentCursor
= (wxCursor
*) NULL
;
1644 m_isDragging
= false;
1648 wxListHeaderWindow::wxListHeaderWindow()
1652 m_owner
= (wxListMainWindow
*) NULL
;
1653 m_resizeCursor
= (wxCursor
*) NULL
;
1656 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1658 wxListMainWindow
*owner
,
1662 const wxString
&name
)
1663 : wxWindow( win
, id
, pos
, size
, style
, name
)
1668 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1671 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1672 SetOwnForegroundColour( attr
.colFg
);
1673 SetOwnBackgroundColour( attr
.colBg
);
1675 SetOwnFont( attr
.font
);
1677 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1678 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1680 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1684 wxListHeaderWindow::~wxListHeaderWindow()
1686 delete m_resizeCursor
;
1689 #ifdef __WXUNIVERSAL__
1690 #include "wx/univ/renderer.h"
1691 #include "wx/univ/theme.h"
1694 // shift the DC origin to match the position of the main window horz
1695 // scrollbar: this allows us to always use logical coords
1696 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1699 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1702 m_owner
->GetViewStart( &view_start
, NULL
);
1704 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1706 // FIXME: we need a better way for RTL scrolling..
1707 int scroll_lines
= m_owner
->GetScrollLines( wxHORIZONTAL
);
1710 int client_size
= m_owner
->GetClientSize().x
;
1711 view_start
= scroll_lines
- (client_size
/ xpix
) - view_start
;
1712 view_start
= -view_start
;
1718 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1720 // account for the horz scrollbar offset
1721 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1724 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1726 wxPaintDC
dc( this );
1731 dc
.SetFont( GetFont() );
1733 // width and height of the entire header window
1735 GetClientSize( &w
, &h
);
1736 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1738 dc
.SetBackgroundMode(wxTRANSPARENT
);
1739 dc
.SetTextForeground(GetForegroundColour());
1741 int x
= HEADER_OFFSET_X
;
1742 int numColumns
= m_owner
->GetColumnCount();
1744 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1746 m_owner
->GetColumn( i
, item
);
1747 int wCol
= item
.m_width
;
1749 // the width of the rect to draw: make it smaller to fit entirely
1750 // inside the column rect
1759 wxRendererNative::Get().DrawHeaderButton
1763 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1764 m_parent
->IsEnabled() ? 0
1765 : (int)wxCONTROL_DISABLED
1768 // see if we have enough space for the column label
1770 // for this we need the width of the text
1773 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1774 wLabel
+= 2 * EXTRA_WIDTH
;
1776 // and the width of the icon, if any
1777 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1778 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1779 const int image
= item
.m_image
;
1780 wxImageListType
*imageList
;
1783 imageList
= m_owner
->m_small_image_list
;
1786 imageList
->GetSize(image
, ix
, iy
);
1787 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1795 // ignore alignment if there is not enough space anyhow
1797 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1800 wxFAIL_MSG( _T("unknown list item format") );
1803 case wxLIST_FORMAT_LEFT
:
1807 case wxLIST_FORMAT_RIGHT
:
1808 xAligned
= x
+ cw
- wLabel
;
1811 case wxLIST_FORMAT_CENTER
:
1812 xAligned
= x
+ (cw
- wLabel
) / 2;
1816 // if we have an image, draw it on the right of the label
1823 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1824 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1825 wxIMAGELIST_DRAW_TRANSPARENT
1828 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1831 // draw the text clipping it so that it doesn't overwrite the column
1833 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1835 dc
.DrawText( item
.GetText(),
1836 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1842 void wxListHeaderWindow::DrawCurrent()
1844 int x1
= m_currentX
;
1846 m_owner
->ClientToScreen( &x1
, &y1
);
1848 int x2
= m_currentX
;
1850 m_owner
->GetClientSize( NULL
, &y2
);
1851 m_owner
->ClientToScreen( &x2
, &y2
);
1854 dc
.SetLogicalFunction( wxINVERT
);
1855 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1856 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1860 dc
.DrawLine( x1
, y1
, x2
, y2
);
1862 dc
.SetLogicalFunction( wxCOPY
);
1864 dc
.SetPen( wxNullPen
);
1865 dc
.SetBrush( wxNullBrush
);
1868 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1870 // we want to work with logical coords
1872 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1873 int y
= event
.GetY();
1877 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1879 // we don't draw the line beyond our window, but we allow dragging it
1882 GetClientSize( &w
, NULL
);
1883 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1886 // erase the line if it was drawn
1887 if ( m_currentX
< w
)
1890 if (event
.ButtonUp())
1893 m_isDragging
= false;
1895 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1896 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1903 m_currentX
= m_minX
+ 7;
1905 // draw in the new location
1906 if ( m_currentX
< w
)
1910 else // not dragging
1913 bool hit_border
= false;
1915 // end of the current column
1918 // find the column where this event occurred
1920 countCol
= m_owner
->GetColumnCount();
1921 for (col
= 0; col
< countCol
; col
++)
1923 xpos
+= m_owner
->GetColumnWidth( col
);
1926 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1928 // near the column border
1935 // inside the column
1942 if ( col
== countCol
)
1945 if (event
.LeftDown() || event
.RightUp())
1947 if (hit_border
&& event
.LeftDown())
1949 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1950 event
.GetPosition()) )
1952 m_isDragging
= true;
1957 //else: column resizing was vetoed by the user code
1959 else // click on a column
1961 SendListEvent( event
.LeftDown()
1962 ? wxEVT_COMMAND_LIST_COL_CLICK
1963 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1964 event
.GetPosition());
1967 else if (event
.Moving())
1972 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1973 m_currentCursor
= m_resizeCursor
;
1977 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1978 m_currentCursor
= wxSTANDARD_CURSOR
;
1982 SetCursor(*m_currentCursor
);
1987 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1989 m_owner
->SetFocus();
1993 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1995 wxWindow
*parent
= GetParent();
1996 wxListEvent
le( type
, parent
->GetId() );
1997 le
.SetEventObject( parent
);
1998 le
.m_pointDrag
= pos
;
2000 // the position should be relative to the parent window, not
2001 // this one for compatibility with MSW and common sense: the
2002 // user code doesn't know anything at all about this header
2003 // window, so why should it get positions relative to it?
2004 le
.m_pointDrag
.y
-= GetSize().y
;
2006 le
.m_col
= m_column
;
2007 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2010 //-----------------------------------------------------------------------------
2011 // wxListRenameTimer (internal)
2012 //-----------------------------------------------------------------------------
2014 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2019 void wxListRenameTimer::Notify()
2021 m_owner
->OnRenameTimer();
2024 //-----------------------------------------------------------------------------
2025 // wxListTextCtrlWrapper (internal)
2026 //-----------------------------------------------------------------------------
2028 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2029 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2030 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2031 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2034 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2037 : m_startValue(owner
->GetItemText(itemEdit
)),
2038 m_itemEdited(itemEdit
)
2043 m_aboutToFinish
= false;
2045 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2047 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2048 &rectLabel
.x
, &rectLabel
.y
);
2050 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2051 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2052 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2055 m_text
->PushEventHandler(this);
2058 void wxListTextCtrlWrapper::Finish()
2064 m_text
->RemoveEventHandler(this);
2065 m_owner
->FinishEditing(m_text
);
2067 wxPendingDelete
.Append( this );
2071 bool wxListTextCtrlWrapper::AcceptChanges()
2073 const wxString value
= m_text
->GetValue();
2075 if ( value
== m_startValue
)
2076 // nothing changed, always accept
2079 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2080 // vetoed by the user
2083 // accepted, do rename the item
2084 m_owner
->SetItemText(m_itemEdited
, value
);
2089 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2091 m_aboutToFinish
= true;
2093 // Notify the owner about the changes
2096 // Even if vetoed, close the control (consistent with MSW)
2100 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2102 switch ( event
.m_keyCode
)
2105 AcceptChangesAndFinish();
2109 m_owner
->OnRenameCancelled( m_itemEdited
);
2118 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2126 // auto-grow the textctrl:
2127 wxSize parentSize
= m_owner
->GetSize();
2128 wxPoint myPos
= m_text
->GetPosition();
2129 wxSize mySize
= m_text
->GetSize();
2131 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2132 if (myPos
.x
+ sx
> parentSize
.x
)
2133 sx
= parentSize
.x
- myPos
.x
;
2136 m_text
->SetSize(sx
, wxDefaultCoord
);
2141 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2143 if ( !m_finished
&& !m_aboutToFinish
)
2145 if ( !AcceptChanges() )
2146 m_owner
->OnRenameCancelled( m_itemEdited
);
2151 // We must let the native text control handle focus
2155 //-----------------------------------------------------------------------------
2157 //-----------------------------------------------------------------------------
2159 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2161 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2162 EVT_PAINT (wxListMainWindow::OnPaint
)
2163 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2164 EVT_CHAR (wxListMainWindow::OnChar
)
2165 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2166 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2167 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2168 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2171 void wxListMainWindow::Init()
2176 m_lineTo
= (size_t)-1;
2182 m_small_image_list
= (wxImageListType
*) NULL
;
2183 m_normal_image_list
= (wxImageListType
*) NULL
;
2185 m_small_spacing
= 30;
2186 m_normal_spacing
= 40;
2190 m_isCreated
= false;
2192 m_lastOnSame
= false;
2193 m_renameTimer
= new wxListRenameTimer( this );
2194 m_textctrlWrapper
= NULL
;
2198 m_lineSelectSingleOnUp
=
2199 m_lineBeforeLastClicked
= (size_t)-1;
2204 wxListMainWindow::wxListMainWindow()
2209 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2212 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2217 const wxString
&name
)
2218 : wxScrolledWindow( parent
, id
, pos
, size
,
2219 style
| wxHSCROLL
| wxVSCROLL
, name
)
2223 m_highlightBrush
= new wxBrush
2225 wxSystemSettings::GetColour
2227 wxSYS_COLOUR_HIGHLIGHT
2232 m_highlightUnfocusedBrush
= new wxBrush
2234 wxSystemSettings::GetColour
2236 wxSYS_COLOUR_BTNSHADOW
2241 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2243 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2244 SetOwnForegroundColour( attr
.colFg
);
2245 SetOwnBackgroundColour( attr
.colBg
);
2247 SetOwnFont( attr
.font
);
2250 wxListMainWindow::~wxListMainWindow()
2253 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2254 WX_CLEAR_ARRAY(m_aColWidths
);
2256 delete m_highlightBrush
;
2257 delete m_highlightUnfocusedBrush
;
2258 delete m_renameTimer
;
2261 void wxListMainWindow::CacheLineData(size_t line
)
2263 wxGenericListCtrl
*listctrl
= GetListCtrl();
2265 wxListLineData
*ld
= GetDummyLine();
2267 size_t countCol
= GetColumnCount();
2268 for ( size_t col
= 0; col
< countCol
; col
++ )
2270 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2271 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2274 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2277 wxListLineData
*wxListMainWindow::GetDummyLine() const
2279 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2280 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2282 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2284 // we need to recreate the dummy line if the number of columns in the
2285 // control changed as it would have the incorrect number of fields
2287 if ( !m_lines
.IsEmpty() &&
2288 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2290 self
->m_lines
.Clear();
2293 if ( m_lines
.IsEmpty() )
2295 wxListLineData
*line
= new wxListLineData(self
);
2296 self
->m_lines
.Add(line
);
2298 // don't waste extra memory -- there never going to be anything
2299 // else/more in this array
2300 self
->m_lines
.Shrink();
2306 // ----------------------------------------------------------------------------
2307 // line geometry (report mode only)
2308 // ----------------------------------------------------------------------------
2310 wxCoord
wxListMainWindow::GetLineHeight() const
2312 // we cache the line height as calling GetTextExtent() is slow
2313 if ( !m_lineHeight
)
2315 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2317 wxClientDC
dc( self
);
2318 dc
.SetFont( GetFont() );
2321 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2323 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2326 m_small_image_list
->GetSize(0, iw
, ih
);
2331 self
->m_lineHeight
= y
+ LINE_SPACING
;
2334 return m_lineHeight
;
2337 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2339 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2341 return LINE_SPACING
+ line
* GetLineHeight();
2344 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2346 if ( !InReportView() )
2347 return GetLine(line
)->m_gi
->m_rectAll
;
2350 rect
.x
= HEADER_OFFSET_X
;
2351 rect
.y
= GetLineY(line
);
2352 rect
.width
= GetHeaderWidth();
2353 rect
.height
= GetLineHeight();
2358 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2360 if ( !InReportView() )
2361 return GetLine(line
)->m_gi
->m_rectLabel
;
2364 rect
.x
= HEADER_OFFSET_X
;
2365 rect
.y
= GetLineY(line
);
2366 rect
.width
= GetColumnWidth(0);
2367 rect
.height
= GetLineHeight();
2372 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2374 if ( !InReportView() )
2375 return GetLine(line
)->m_gi
->m_rectIcon
;
2377 wxListLineData
*ld
= GetLine(line
);
2378 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2381 rect
.x
= HEADER_OFFSET_X
;
2382 rect
.y
= GetLineY(line
);
2383 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2388 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2390 return InReportView() ? GetLineRect(line
)
2391 : GetLine(line
)->m_gi
->m_rectHighlight
;
2394 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2396 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2398 wxListLineData
*ld
= GetLine(line
);
2400 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2401 return wxLIST_HITTEST_ONITEMICON
;
2403 // VS: Testing for "ld->HasText() || InReportView()" instead of
2404 // "ld->HasText()" is needed to make empty lines in report view
2406 if ( ld
->HasText() || InReportView() )
2408 wxRect rect
= InReportView() ? GetLineRect(line
)
2409 : GetLineLabelRect(line
);
2411 if ( rect
.Contains(x
, y
) )
2412 return wxLIST_HITTEST_ONITEMLABEL
;
2418 // ----------------------------------------------------------------------------
2419 // highlight (selection) handling
2420 // ----------------------------------------------------------------------------
2422 bool wxListMainWindow::IsHighlighted(size_t line
) const
2426 return m_selStore
.IsSelected(line
);
2430 wxListLineData
*ld
= GetLine(line
);
2431 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2433 return ld
->IsHighlighted();
2437 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2443 wxArrayInt linesChanged
;
2444 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2447 // meny items changed state, refresh everything
2448 RefreshLines(lineFrom
, lineTo
);
2450 else // only a few items changed state, refresh only them
2452 size_t count
= linesChanged
.GetCount();
2453 for ( size_t n
= 0; n
< count
; n
++ )
2455 RefreshLine(linesChanged
[n
]);
2459 else // iterate over all items in non report view
2461 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2463 if ( HighlightLine(line
, highlight
) )
2469 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2475 changed
= m_selStore
.SelectItem(line
, highlight
);
2479 wxListLineData
*ld
= GetLine(line
);
2480 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2482 changed
= ld
->Highlight(highlight
);
2487 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2488 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2494 void wxListMainWindow::RefreshLine( size_t line
)
2496 if ( InReportView() )
2498 size_t visibleFrom
, visibleTo
;
2499 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2501 if ( line
< visibleFrom
|| line
> visibleTo
)
2505 wxRect rect
= GetLineRect(line
);
2507 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2508 RefreshRect( rect
);
2511 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2513 // we suppose that they are ordered by caller
2514 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2516 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2518 if ( InReportView() )
2520 size_t visibleFrom
, visibleTo
;
2521 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2523 if ( lineFrom
< visibleFrom
)
2524 lineFrom
= visibleFrom
;
2525 if ( lineTo
> visibleTo
)
2530 rect
.y
= GetLineY(lineFrom
);
2531 rect
.width
= GetClientSize().x
;
2532 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2534 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2535 RefreshRect( rect
);
2539 // TODO: this should be optimized...
2540 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2547 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2549 if ( InReportView() )
2551 size_t visibleFrom
, visibleTo
;
2552 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2554 if ( lineFrom
< visibleFrom
)
2555 lineFrom
= visibleFrom
;
2556 else if ( lineFrom
> visibleTo
)
2561 rect
.y
= GetLineY(lineFrom
);
2562 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2564 wxSize size
= GetClientSize();
2565 rect
.width
= size
.x
;
2567 // refresh till the bottom of the window
2568 rect
.height
= size
.y
- rect
.y
;
2570 RefreshRect( rect
);
2574 // TODO: how to do it more efficiently?
2579 void wxListMainWindow::RefreshSelected()
2585 if ( InReportView() )
2587 GetVisibleLinesRange(&from
, &to
);
2592 to
= GetItemCount() - 1;
2595 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2596 RefreshLine(m_current
);
2598 for ( size_t line
= from
; line
<= to
; line
++ )
2600 // NB: the test works as expected even if m_current == -1
2601 if ( line
!= m_current
&& IsHighlighted(line
) )
2606 void wxListMainWindow::Freeze()
2611 void wxListMainWindow::Thaw()
2613 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2615 if ( --m_freezeCount
== 0 )
2619 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2621 // Note: a wxPaintDC must be constructed even if no drawing is
2622 // done (a Windows requirement).
2623 wxPaintDC
dc( this );
2625 if ( IsEmpty() || m_freezeCount
)
2626 // nothing to draw or not the moment to draw it
2630 // delay the repainting until we calculate all the items positions
2636 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2638 dc
.SetFont( GetFont() );
2640 if ( InReportView() )
2642 int lineHeight
= GetLineHeight();
2644 size_t visibleFrom
, visibleTo
;
2645 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2648 int xOrig
= dc
.LogicalToDeviceX( 0 );
2649 int yOrig
= dc
.LogicalToDeviceY( 0 );
2651 // tell the caller cache to cache the data
2654 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2655 GetParent()->GetId());
2656 evCache
.SetEventObject( GetParent() );
2657 evCache
.m_oldItemIndex
= visibleFrom
;
2658 evCache
.m_itemIndex
= visibleTo
;
2659 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2662 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2664 rectLine
= GetLineRect(line
);
2667 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2668 rectLine
.width
, rectLine
.height
) )
2670 // don't redraw unaffected lines to avoid flicker
2674 GetLine(line
)->DrawInReportMode( &dc
,
2676 GetLineHighlightRect(line
),
2677 IsHighlighted(line
) );
2680 if ( HasFlag(wxLC_HRULES
) )
2682 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2683 wxSize clientSize
= GetClientSize();
2685 // Don't draw the first one
2686 for ( size_t i
= visibleFrom
+ 1; i
<= visibleTo
; i
++ )
2689 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2690 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2691 clientSize
.x
- dev_x
, i
* lineHeight
);
2694 // Draw last horizontal rule
2695 if ( visibleTo
== GetItemCount() - 1 )
2698 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2699 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2700 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2704 // Draw vertical rules if required
2705 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2707 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2708 wxRect firstItemRect
, lastItemRect
;
2710 GetItemRect(visibleFrom
, firstItemRect
);
2711 GetItemRect(visibleTo
, lastItemRect
);
2712 int x
= firstItemRect
.GetX();
2714 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2716 for (int col
= 0; col
< GetColumnCount(); col
++)
2718 int colWidth
= GetColumnWidth(col
);
2720 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2721 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2727 size_t count
= GetItemCount();
2728 for ( size_t i
= 0; i
< count
; i
++ )
2730 GetLine(i
)->Draw( &dc
);
2735 // Don't draw rect outline under Mac at all.
2740 dc
.SetPen( *wxBLACK_PEN
);
2741 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2742 dc
.DrawRectangle( GetLineHighlightRect( m_current
) );
2748 void wxListMainWindow::HighlightAll( bool on
)
2750 if ( IsSingleSel() )
2752 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2754 // we just have one item to turn off
2755 if ( HasCurrent() && IsHighlighted(m_current
) )
2757 HighlightLine(m_current
, false);
2758 RefreshLine(m_current
);
2763 HighlightLines(0, GetItemCount() - 1, on
);
2767 void wxListMainWindow::SendNotify( size_t line
,
2768 wxEventType command
,
2769 const wxPoint
& point
)
2771 wxListEvent
le( command
, GetParent()->GetId() );
2772 le
.SetEventObject( GetParent() );
2773 le
.m_itemIndex
= line
;
2775 // set only for events which have position
2776 if ( point
!= wxDefaultPosition
)
2777 le
.m_pointDrag
= point
;
2779 // don't try to get the line info for virtual list controls: the main
2780 // program has it anyhow and if we did it would result in accessing all
2781 // the lines, even those which are not visible now and this is precisely
2782 // what we're trying to avoid
2783 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2785 if ( line
!= (size_t)-1 )
2787 GetLine(line
)->GetItem( 0, le
.m_item
);
2789 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2791 //else: there may be no more such item
2793 GetParent()->GetEventHandler()->ProcessEvent( le
);
2796 void wxListMainWindow::ChangeCurrent(size_t current
)
2798 m_current
= current
;
2800 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2803 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2805 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2806 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2808 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2809 wxT("EditLabel() needs a text control") );
2811 size_t itemEdit
= (size_t)item
;
2813 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2814 le
.SetEventObject( GetParent() );
2815 le
.m_itemIndex
= item
;
2816 wxListLineData
*data
= GetLine(itemEdit
);
2817 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2818 data
->GetItem( 0, le
.m_item
);
2820 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2822 // vetoed by user code
2826 // We have to call this here because the label in question might just have
2827 // been added and no screen update taken place.
2831 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2832 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2833 return m_textctrlWrapper
->GetText();
2836 void wxListMainWindow::OnRenameTimer()
2838 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2840 EditLabel( m_current
);
2843 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2845 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2846 le
.SetEventObject( GetParent() );
2847 le
.m_itemIndex
= itemEdit
;
2849 wxListLineData
*data
= GetLine(itemEdit
);
2851 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2853 data
->GetItem( 0, le
.m_item
);
2854 le
.m_item
.m_text
= value
;
2855 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2859 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2861 // let owner know that the edit was cancelled
2862 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2864 le
.SetEditCanceled(true);
2866 le
.SetEventObject( GetParent() );
2867 le
.m_itemIndex
= itemEdit
;
2869 wxListLineData
*data
= GetLine(itemEdit
);
2870 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2872 data
->GetItem( 0, le
.m_item
);
2873 GetEventHandler()->ProcessEvent( le
);
2876 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2880 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2881 // shutdown the edit control when the mouse is clicked elsewhere on the
2882 // listctrl because the order of events is different (or something like
2883 // that), so explicitly end the edit if it is active.
2884 if ( event
.LeftDown() && m_textctrlWrapper
)
2885 m_textctrlWrapper
->AcceptChangesAndFinish();
2888 event
.SetEventObject( GetParent() );
2889 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2892 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2894 // let the base handle mouse wheel events.
2899 if ( !HasCurrent() || IsEmpty() )
2901 if (event
.RightDown())
2903 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2904 // Allow generation of context menu event
2913 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2914 event
.ButtonDClick()) )
2917 int x
= event
.GetX();
2918 int y
= event
.GetY();
2919 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2921 // where did we hit it (if we did)?
2924 size_t count
= GetItemCount(),
2927 if ( InReportView() )
2929 current
= y
/ GetLineHeight();
2930 if ( current
< count
)
2931 hitResult
= HitTestLine(current
, x
, y
);
2935 // TODO: optimize it too! this is less simple than for report view but
2936 // enumerating all items is still not a way to do it!!
2937 for ( current
= 0; current
< count
; current
++ )
2939 hitResult
= HitTestLine(current
, x
, y
);
2945 if (event
.Dragging())
2947 if (m_dragCount
== 0)
2949 // we have to report the raw, physical coords as we want to be
2950 // able to call HitTest(event.m_pointDrag) from the user code to
2951 // get the item being dragged
2952 m_dragStart
= event
.GetPosition();
2957 if (m_dragCount
!= 3)
2960 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2961 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2963 wxListEvent
le( command
, GetParent()->GetId() );
2964 le
.SetEventObject( GetParent() );
2965 le
.m_itemIndex
= m_lineLastClicked
;
2966 le
.m_pointDrag
= m_dragStart
;
2967 GetParent()->GetEventHandler()->ProcessEvent( le
);
2978 // outside of any item
2979 if (event
.RightDown())
2981 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2982 // Allow generation of context menu event
2987 // reset the selection and bail out
2988 HighlightAll(false);
2994 bool forceClick
= false;
2995 if (event
.ButtonDClick())
2997 m_renameTimer
->Stop();
2998 m_lastOnSame
= false;
3000 if ( current
== m_lineLastClicked
)
3002 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3008 // The first click was on another item, so don't interpret this as
3009 // a double click, but as a simple click instead
3016 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3018 // select single line
3019 HighlightAll( false );
3020 ReverseHighlight(m_lineSelectSingleOnUp
);
3025 if ((current
== m_current
) &&
3026 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3027 HasFlag(wxLC_EDIT_LABELS
) )
3029 m_renameTimer
->Start( 100, true );
3033 m_lastOnSame
= false;
3034 m_lineSelectSingleOnUp
= (size_t)-1;
3038 // This is necessary, because after a DnD operation in
3039 // from and to ourself, the up event is swallowed by the
3040 // DnD code. So on next non-up event (which means here and
3041 // now) m_lineSelectSingleOnUp should be reset.
3042 m_lineSelectSingleOnUp
= (size_t)-1;
3044 if (event
.RightDown())
3046 m_lineBeforeLastClicked
= m_lineLastClicked
;
3047 m_lineLastClicked
= current
;
3049 // If the item is already selected, do not update the selection.
3050 // Multi-selections should not be cleared if a selected item is clicked.
3051 if (!IsHighlighted(current
))
3053 HighlightAll(false);
3054 ChangeCurrent(current
);
3055 ReverseHighlight(m_current
);
3058 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3060 // Allow generation of context menu event
3063 else if (event
.MiddleDown())
3065 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3067 else if ( event
.LeftDown() || forceClick
)
3069 m_lineBeforeLastClicked
= m_lineLastClicked
;
3070 m_lineLastClicked
= current
;
3072 size_t oldCurrent
= m_current
;
3073 bool oldWasSelected
= IsHighlighted(m_current
);
3075 bool cmdModifierDown
= event
.CmdDown();
3076 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3078 if ( IsSingleSel() || !IsHighlighted(current
) )
3080 HighlightAll( false );
3082 ChangeCurrent(current
);
3084 ReverseHighlight(m_current
);
3086 else // multi sel & current is highlighted & no mod keys
3088 m_lineSelectSingleOnUp
= current
;
3089 ChangeCurrent(current
); // change focus
3092 else // multi sel & either ctrl or shift is down
3094 if (cmdModifierDown
)
3096 ChangeCurrent(current
);
3098 ReverseHighlight(m_current
);
3100 else if (event
.ShiftDown())
3102 ChangeCurrent(current
);
3104 size_t lineFrom
= oldCurrent
,
3107 if ( lineTo
< lineFrom
)
3110 lineFrom
= m_current
;
3113 HighlightLines(lineFrom
, lineTo
);
3115 else // !ctrl, !shift
3117 // test in the enclosing if should make it impossible
3118 wxFAIL_MSG( _T("how did we get here?") );
3122 if (m_current
!= oldCurrent
)
3123 RefreshLine( oldCurrent
);
3125 // forceClick is only set if the previous click was on another item
3126 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3130 void wxListMainWindow::MoveToItem(size_t item
)
3132 if ( item
== (size_t)-1 )
3135 wxRect rect
= GetLineRect(item
);
3137 int client_w
, client_h
;
3138 GetClientSize( &client_w
, &client_h
);
3140 const int hLine
= GetLineHeight();
3142 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3143 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3145 if ( InReportView() )
3147 // the next we need the range of lines shown it might be different,
3148 // so recalculate it
3149 ResetVisibleLinesRange();
3151 if (rect
.y
< view_y
)
3152 Scroll( -1, rect
.y
/ hLine
);
3153 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3154 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3158 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3161 wxPrintf( wxT("rect %d %d %d %d view_x %d\n"), rect
.x
, rect
.y
, rect
.width
, rect
.height
, view_x
);
3162 int virtual_width
= GetVirtualSize().x
;
3163 view_x
= virtual_width
- view_x
- client_w
;
3164 wxPrintf( wxT("virtual_width %d view_x = %d client_w = %d\n"), virtual_width
, view_x
, client_w
);
3169 if (rect
.x
-view_x
< 5)
3170 Scroll( (rect
.x
- 5) / SCROLL_UNIT_X
, -1 );
3171 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3172 Scroll( (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
, -1 );
3177 // ----------------------------------------------------------------------------
3178 // keyboard handling
3179 // ----------------------------------------------------------------------------
3181 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3183 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3184 _T("invalid item index in OnArrowChar()") );
3186 size_t oldCurrent
= m_current
;
3188 // in single selection we just ignore Shift as we can't select several
3190 if ( event
.ShiftDown() && !IsSingleSel() )
3192 ChangeCurrent(newCurrent
);
3194 // refresh the old focus to remove it
3195 RefreshLine( oldCurrent
);
3197 // select all the items between the old and the new one
3198 if ( oldCurrent
> newCurrent
)
3200 newCurrent
= oldCurrent
;
3201 oldCurrent
= m_current
;
3204 HighlightLines(oldCurrent
, newCurrent
);
3208 // all previously selected items are unselected unless ctrl is held
3209 if ( !event
.ControlDown() )
3210 HighlightAll(false);
3212 ChangeCurrent(newCurrent
);
3214 // refresh the old focus to remove it
3215 RefreshLine( oldCurrent
);
3217 if ( !event
.ControlDown() )
3219 HighlightLine( m_current
, true );
3223 RefreshLine( m_current
);
3228 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3230 wxWindow
*parent
= GetParent();
3232 // propagate the key event upwards
3233 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3234 ke
.m_shiftDown
= event
.m_shiftDown
;
3235 ke
.m_controlDown
= event
.m_controlDown
;
3236 ke
.m_altDown
= event
.m_altDown
;
3237 ke
.m_metaDown
= event
.m_metaDown
;
3238 ke
.m_keyCode
= event
.m_keyCode
;
3241 ke
.SetEventObject( parent
);
3242 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3247 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3249 wxWindow
*parent
= GetParent();
3251 // send a list_key event up
3254 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3255 le
.m_itemIndex
= m_current
;
3256 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3257 le
.m_code
= event
.GetKeyCode();
3258 le
.SetEventObject( parent
);
3259 parent
->GetEventHandler()->ProcessEvent( le
);
3262 // propagate the char event upwards
3263 wxKeyEvent
ke( wxEVT_CHAR
);
3264 ke
.m_shiftDown
= event
.m_shiftDown
;
3265 ke
.m_controlDown
= event
.m_controlDown
;
3266 ke
.m_altDown
= event
.m_altDown
;
3267 ke
.m_metaDown
= event
.m_metaDown
;
3268 ke
.m_keyCode
= event
.m_keyCode
;
3271 ke
.SetEventObject( parent
);
3272 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3274 if (event
.GetKeyCode() == WXK_TAB
)
3276 wxNavigationKeyEvent nevent
;
3277 nevent
.SetWindowChange( event
.ControlDown() );
3278 nevent
.SetDirection( !event
.ShiftDown() );
3279 nevent
.SetEventObject( GetParent()->GetParent() );
3280 nevent
.SetCurrentFocus( m_parent
);
3281 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3285 // no item -> nothing to do
3292 // don't use m_linesPerPage directly as it might not be computed yet
3293 const int pageSize
= GetCountPerPage();
3294 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3296 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3298 if (event
.GetKeyCode() == WXK_RIGHT
)
3299 event
.m_keyCode
= WXK_LEFT
;
3300 else if (event
.GetKeyCode() == WXK_LEFT
)
3301 event
.m_keyCode
= WXK_RIGHT
;
3304 switch ( event
.GetKeyCode() )
3307 if ( m_current
> 0 )
3308 OnArrowChar( m_current
- 1, event
);
3312 if ( m_current
< (size_t)GetItemCount() - 1 )
3313 OnArrowChar( m_current
+ 1, event
);
3318 OnArrowChar( GetItemCount() - 1, event
);
3323 OnArrowChar( 0, event
);
3328 int steps
= InReportView() ? pageSize
- 1
3329 : m_current
% pageSize
;
3331 int index
= m_current
- steps
;
3335 OnArrowChar( index
, event
);
3341 int steps
= InReportView()
3343 : pageSize
- (m_current
% pageSize
) - 1;
3345 size_t index
= m_current
+ steps
;
3346 size_t count
= GetItemCount();
3347 if ( index
>= count
)
3350 OnArrowChar( index
, event
);
3355 if ( !InReportView() )
3357 int index
= m_current
- pageSize
;
3361 OnArrowChar( index
, event
);
3366 if ( !InReportView() )
3368 size_t index
= m_current
+ pageSize
;
3370 size_t count
= GetItemCount();
3371 if ( index
>= count
)
3374 OnArrowChar( index
, event
);
3379 if ( IsSingleSel() )
3381 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3383 if ( IsHighlighted(m_current
) )
3385 // don't unselect the item in single selection mode
3388 //else: select it in ReverseHighlight() below if unselected
3391 ReverseHighlight(m_current
);
3396 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3404 // ----------------------------------------------------------------------------
3406 // ----------------------------------------------------------------------------
3408 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3412 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3413 event
.SetEventObject( GetParent() );
3414 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3418 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3419 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3420 // which are already drawn correctly resulting in horrible flicker - avoid
3430 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3434 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3435 event
.SetEventObject( GetParent() );
3436 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3444 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3446 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3448 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3450 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3452 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3454 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3456 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3458 else if ( InReportView() && (m_small_image_list
))
3460 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3464 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3466 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3468 m_normal_image_list
->GetSize( index
, width
, height
);
3470 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3472 m_small_image_list
->GetSize( index
, width
, height
);
3474 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3476 m_small_image_list
->GetSize( index
, width
, height
);
3478 else if ( InReportView() && m_small_image_list
)
3480 m_small_image_list
->GetSize( index
, width
, height
);
3489 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3491 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3492 dc
.SetFont( GetFont() );
3495 dc
.GetTextExtent( s
, &lw
, NULL
);
3497 return lw
+ AUTOSIZE_COL_MARGIN
;
3500 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3504 // calc the spacing from the icon size
3505 int width
= 0, height
= 0;
3507 if ((imageList
) && (imageList
->GetImageCount()) )
3508 imageList
->GetSize(0, width
, height
);
3510 if (which
== wxIMAGE_LIST_NORMAL
)
3512 m_normal_image_list
= imageList
;
3513 m_normal_spacing
= width
+ 8;
3516 if (which
== wxIMAGE_LIST_SMALL
)
3518 m_small_image_list
= imageList
;
3519 m_small_spacing
= width
+ 14;
3520 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3524 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3528 m_small_spacing
= spacing
;
3530 m_normal_spacing
= spacing
;
3533 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3535 return isSmall
? m_small_spacing
: m_normal_spacing
;
3538 // ----------------------------------------------------------------------------
3540 // ----------------------------------------------------------------------------
3542 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3544 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3546 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3548 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3549 item
.m_width
= GetTextLength( item
.m_text
);
3551 wxListHeaderData
*column
= node
->GetData();
3552 column
->SetItem( item
);
3554 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3556 headerWin
->m_dirty
= true;
3560 // invalidate it as it has to be recalculated
3564 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3566 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3567 _T("invalid column index") );
3569 wxCHECK_RET( InReportView(),
3570 _T("SetColumnWidth() can only be called in report mode.") );
3573 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3575 headerWin
->m_dirty
= true;
3577 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3578 wxCHECK_RET( node
, _T("no column?") );
3580 wxListHeaderData
*column
= node
->GetData();
3582 size_t count
= GetItemCount();
3584 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3586 width
= GetTextLength(column
->GetText());
3588 else if ( width
== wxLIST_AUTOSIZE
)
3592 // TODO: determine the max width somehow...
3593 width
= WIDTH_COL_DEFAULT
;
3597 wxClientDC
dc(this);
3598 dc
.SetFont( GetFont() );
3600 int max
= AUTOSIZE_COL_MARGIN
;
3602 // if the cached column width isn't valid then recalculate it
3603 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3605 for (size_t i
= 0; i
< count
; i
++)
3607 wxListLineData
*line
= GetLine( i
);
3608 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3610 wxCHECK_RET( n
, _T("no subitem?") );
3612 wxListItemData
*itemData
= n
->GetData();
3615 itemData
->GetItem(item
);
3616 int itemWidth
= GetItemWidthWithImage(&item
);
3617 if (itemWidth
> max
)
3621 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3622 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3625 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3626 width
= max
+ AUTOSIZE_COL_MARGIN
;
3630 column
->SetWidth( width
);
3632 // invalidate it as it has to be recalculated
3636 int wxListMainWindow::GetHeaderWidth() const
3638 if ( !m_headerWidth
)
3640 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3642 size_t count
= GetColumnCount();
3643 for ( size_t col
= 0; col
< count
; col
++ )
3645 self
->m_headerWidth
+= GetColumnWidth(col
);
3649 return m_headerWidth
;
3652 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3654 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3655 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3657 wxListHeaderData
*column
= node
->GetData();
3658 column
->GetItem( item
);
3661 int wxListMainWindow::GetColumnWidth( int col
) const
3663 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3664 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3666 wxListHeaderData
*column
= node
->GetData();
3667 return column
->GetWidth();
3670 // ----------------------------------------------------------------------------
3672 // ----------------------------------------------------------------------------
3674 void wxListMainWindow::SetItem( wxListItem
&item
)
3676 long id
= item
.m_itemId
;
3677 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3678 _T("invalid item index in SetItem") );
3682 wxListLineData
*line
= GetLine((size_t)id
);
3683 line
->SetItem( item
.m_col
, item
);
3685 // Set item state if user wants
3686 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3687 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3691 // update the Max Width Cache if needed
3692 int width
= GetItemWidthWithImage(&item
);
3694 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3695 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3699 // update the item on screen
3701 GetItemRect(id
, rectItem
);
3702 RefreshRect(rectItem
);
3705 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3710 // first deal with selection
3711 if ( stateMask
& wxLIST_STATE_SELECTED
)
3713 // set/clear select state
3716 // optimized version for virtual listctrl.
3717 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3720 else if ( state
& wxLIST_STATE_SELECTED
)
3722 const long count
= GetItemCount();
3723 for( long i
= 0; i
< count
; i
++ )
3725 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3731 // clear for non virtual (somewhat optimized by using GetNextItem())
3733 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3735 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3740 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3742 // unfocus all: only one item can be focussed, so clearing focus for
3743 // all items is simply clearing focus of the focussed item.
3744 SetItemState(m_current
, state
, stateMask
);
3746 //(setting focus to all items makes no sense, so it is not handled here.)
3749 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3753 SetItemStateAll(state
, stateMask
);
3757 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3758 _T("invalid list ctrl item index in SetItem") );
3760 size_t oldCurrent
= m_current
;
3761 size_t item
= (size_t)litem
; // safe because of the check above
3763 // do we need to change the focus?
3764 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3766 if ( state
& wxLIST_STATE_FOCUSED
)
3768 // don't do anything if this item is already focused
3769 if ( item
!= m_current
)
3771 ChangeCurrent(item
);
3773 if ( oldCurrent
!= (size_t)-1 )
3775 if ( IsSingleSel() )
3777 HighlightLine(oldCurrent
, false);
3780 RefreshLine(oldCurrent
);
3783 RefreshLine( m_current
);
3788 // don't do anything if this item is not focused
3789 if ( item
== m_current
)
3793 if ( IsSingleSel() )
3795 // we must unselect the old current item as well or we
3796 // might end up with more than one selected item in a
3797 // single selection control
3798 HighlightLine(oldCurrent
, false);
3801 RefreshLine( oldCurrent
);
3806 // do we need to change the selection state?
3807 if ( stateMask
& wxLIST_STATE_SELECTED
)
3809 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3811 if ( IsSingleSel() )
3815 // selecting the item also makes it the focused one in the
3817 if ( m_current
!= item
)
3819 ChangeCurrent(item
);
3821 if ( oldCurrent
!= (size_t)-1 )
3823 HighlightLine( oldCurrent
, false );
3824 RefreshLine( oldCurrent
);
3830 // only the current item may be selected anyhow
3831 if ( item
!= m_current
)
3836 if ( HighlightLine(item
, on
) )
3843 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3845 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3846 _T("invalid list ctrl item index in GetItemState()") );
3848 int ret
= wxLIST_STATE_DONTCARE
;
3850 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3852 if ( (size_t)item
== m_current
)
3853 ret
|= wxLIST_STATE_FOCUSED
;
3856 if ( stateMask
& wxLIST_STATE_SELECTED
)
3858 if ( IsHighlighted(item
) )
3859 ret
|= wxLIST_STATE_SELECTED
;
3865 void wxListMainWindow::GetItem( wxListItem
&item
) const
3867 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3868 _T("invalid item index in GetItem") );
3870 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3871 line
->GetItem( item
.m_col
, item
);
3873 // Get item state if user wants it
3874 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3875 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3876 wxLIST_STATE_FOCUSED
);
3879 // ----------------------------------------------------------------------------
3881 // ----------------------------------------------------------------------------
3883 size_t wxListMainWindow::GetItemCount() const
3885 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3888 void wxListMainWindow::SetItemCount(long count
)
3890 m_selStore
.SetItemCount(count
);
3891 m_countVirt
= count
;
3893 ResetVisibleLinesRange();
3895 // scrollbars must be reset
3899 int wxListMainWindow::GetSelectedItemCount() const
3901 // deal with the quick case first
3902 if ( IsSingleSel() )
3903 return HasCurrent() ? IsHighlighted(m_current
) : false;
3905 // virtual controls remmebers all its selections itself
3907 return m_selStore
.GetSelectedCount();
3909 // TODO: we probably should maintain the number of items selected even for
3910 // non virtual controls as enumerating all lines is really slow...
3911 size_t countSel
= 0;
3912 size_t count
= GetItemCount();
3913 for ( size_t line
= 0; line
< count
; line
++ )
3915 if ( GetLine(line
)->IsHighlighted() )
3922 // ----------------------------------------------------------------------------
3923 // item position/size
3924 // ----------------------------------------------------------------------------
3926 wxRect
wxListMainWindow::GetViewRect() const
3928 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3929 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3931 // we need to find the longest/tallest label
3932 wxCoord xMax
= 0, yMax
= 0;
3933 const int count
= GetItemCount();
3936 for ( int i
= 0; i
< count
; i
++ )
3941 wxCoord x
= r
.GetRight(),
3951 // some fudge needed to make it look prettier
3952 xMax
+= 2 * EXTRA_BORDER_X
;
3953 yMax
+= 2 * EXTRA_BORDER_Y
;
3955 // account for the scrollbars if necessary
3956 const wxSize sizeAll
= GetClientSize();
3957 if ( xMax
> sizeAll
.x
)
3958 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3959 if ( yMax
> sizeAll
.y
)
3960 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3962 return wxRect(0, 0, xMax
, yMax
);
3965 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3967 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3968 _T("invalid index in GetItemRect") );
3970 // ensure that we're laid out, otherwise we could return nonsense
3973 wxConstCast(this, wxListMainWindow
)->
3974 RecalculatePositions(true /* no refresh */);
3977 rect
= GetLineRect((size_t)index
);
3979 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3982 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3985 GetItemRect(item
, rect
);
3993 // ----------------------------------------------------------------------------
3994 // geometry calculation
3995 // ----------------------------------------------------------------------------
3997 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3999 wxClientDC
dc( this );
4000 dc
.SetFont( GetFont() );
4002 const size_t count
= GetItemCount();
4005 if ( HasFlag(wxLC_ICON
) )
4006 iconSpacing
= m_normal_spacing
;
4007 else if ( HasFlag(wxLC_SMALL_ICON
) )
4008 iconSpacing
= m_small_spacing
;
4012 // Note that we do not call GetClientSize() here but
4013 // GetSize() and subtract the border size for sunken
4014 // borders manually. This is technically incorrect,
4015 // but we need to know the client area's size WITHOUT
4016 // scrollbars here. Since we don't know if there are
4017 // any scrollbars, we use GetSize() instead. Another
4018 // solution would be to call SetScrollbars() here to
4019 // remove the scrollbars and call GetClientSize() then,
4020 // but this might result in flicker and - worse - will
4021 // reset the scrollbars to 0 which is not good at all
4022 // if you resize a dialog/window, but don't want to
4023 // reset the window scrolling. RR.
4024 // Furthermore, we actually do NOT subtract the border
4025 // width as 2 pixels is just the extra space which we
4026 // need around the actual content in the window. Other-
4027 // wise the text would e.g. touch the upper border. RR.
4030 GetSize( &clientWidth
, &clientHeight
);
4032 const int lineHeight
= GetLineHeight();
4034 if ( InReportView() )
4036 // all lines have the same height and we scroll one line per step
4037 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4039 m_linesPerPage
= clientHeight
/ lineHeight
;
4041 ResetVisibleLinesRange();
4043 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4044 GetHeaderWidth() / SCROLL_UNIT_X
,
4045 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4046 GetScrollPos(wxHORIZONTAL
),
4047 GetScrollPos(wxVERTICAL
),
4052 // we have 3 different layout strategies: either layout all items
4053 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4054 // to arrange them in top to bottom, left to right (don't ask me why
4055 // not the other way round...) order
4056 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4058 int x
= EXTRA_BORDER_X
;
4059 int y
= EXTRA_BORDER_Y
;
4061 wxCoord widthMax
= 0;
4064 for ( i
= 0; i
< count
; i
++ )
4066 wxListLineData
*line
= GetLine(i
);
4067 line
->CalculateSize( &dc
, iconSpacing
);
4068 line
->SetPosition( x
, y
, iconSpacing
);
4070 wxSize sizeLine
= GetLineSize(i
);
4072 if ( HasFlag(wxLC_ALIGN_TOP
) )
4074 if ( sizeLine
.x
> widthMax
)
4075 widthMax
= sizeLine
.x
;
4079 else // wxLC_ALIGN_LEFT
4081 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4085 if ( HasFlag(wxLC_ALIGN_TOP
) )
4087 // traverse the items again and tweak their sizes so that they are
4088 // all the same in a row
4089 for ( i
= 0; i
< count
; i
++ )
4091 wxListLineData
*line
= GetLine(i
);
4092 line
->m_gi
->ExtendWidth(widthMax
);
4100 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4101 (y
+ lineHeight
) / lineHeight
,
4102 GetScrollPos( wxHORIZONTAL
),
4103 GetScrollPos( wxVERTICAL
),
4107 else // "flowed" arrangement, the most complicated case
4109 // at first we try without any scrollbars, if the items don't fit into
4110 // the window, we recalculate after subtracting the space taken by the
4113 int entireWidth
= 0;
4115 for (int tries
= 0; tries
< 2; tries
++)
4117 entireWidth
= 2 * EXTRA_BORDER_X
;
4121 // Now we have decided that the items do not fit into the
4122 // client area, so we need a scrollbar
4123 entireWidth
+= SCROLL_UNIT_X
;
4126 int x
= EXTRA_BORDER_X
;
4127 int y
= EXTRA_BORDER_Y
;
4128 int maxWidthInThisRow
= 0;
4131 int currentlyVisibleLines
= 0;
4133 for (size_t i
= 0; i
< count
; i
++)
4135 currentlyVisibleLines
++;
4136 wxListLineData
*line
= GetLine( i
);
4137 line
->CalculateSize( &dc
, iconSpacing
);
4138 line
->SetPosition( x
, y
, iconSpacing
);
4140 wxSize sizeLine
= GetLineSize( i
);
4142 if ( maxWidthInThisRow
< sizeLine
.x
)
4143 maxWidthInThisRow
= sizeLine
.x
;
4146 if (currentlyVisibleLines
> m_linesPerPage
)
4147 m_linesPerPage
= currentlyVisibleLines
;
4149 if ( y
+ sizeLine
.y
>= clientHeight
)
4151 currentlyVisibleLines
= 0;
4153 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4154 x
+= maxWidthInThisRow
;
4155 entireWidth
+= maxWidthInThisRow
;
4156 maxWidthInThisRow
= 0;
4159 // We have reached the last item.
4160 if ( i
== count
- 1 )
4161 entireWidth
+= maxWidthInThisRow
;
4163 if ( (tries
== 0) &&
4164 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4166 clientHeight
-= wxSystemSettings::
4167 GetMetric(wxSYS_HSCROLL_Y
);
4172 if ( i
== count
- 1 )
4173 tries
= 1; // Everything fits, no second try required.
4181 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4183 GetScrollPos( wxHORIZONTAL
),
4192 // FIXME: why should we call it from here?
4199 void wxListMainWindow::RefreshAll()
4204 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4205 if ( headerWin
&& headerWin
->m_dirty
)
4207 headerWin
->m_dirty
= false;
4208 headerWin
->Refresh();
4212 void wxListMainWindow::UpdateCurrent()
4214 if ( !HasCurrent() && !IsEmpty() )
4218 long wxListMainWindow::GetNextItem( long item
,
4219 int WXUNUSED(geometry
),
4223 max
= GetItemCount();
4224 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4225 _T("invalid listctrl index in GetNextItem()") );
4227 // notice that we start with the next item (or the first one if item == -1)
4228 // and this is intentional to allow writing a simple loop to iterate over
4229 // all selected items
4232 // this is not an error because the index was OK initially,
4233 // just no such item
4240 size_t count
= GetItemCount();
4241 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4243 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4246 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4253 // ----------------------------------------------------------------------------
4255 // ----------------------------------------------------------------------------
4257 void wxListMainWindow::DeleteItem( long lindex
)
4259 size_t count
= GetItemCount();
4261 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4262 _T("invalid item index in DeleteItem") );
4264 size_t index
= (size_t)lindex
;
4266 // we don't need to adjust the index for the previous items
4267 if ( HasCurrent() && m_current
>= index
)
4269 // if the current item is being deleted, we want the next one to
4270 // become selected - unless there is no next one - so don't adjust
4271 // m_current in this case
4272 if ( m_current
!= index
|| m_current
== count
- 1 )
4276 if ( InReportView() )
4278 // mark the Column Max Width cache as dirty if the items in the line
4279 // we're deleting contain the Max Column Width
4280 wxListLineData
* const line
= GetLine(index
);
4281 wxListItemDataList::compatibility_iterator n
;
4282 wxListItemData
*itemData
;
4286 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4288 n
= line
->m_items
.Item( i
);
4289 itemData
= n
->GetData();
4290 itemData
->GetItem(item
);
4292 itemWidth
= GetItemWidthWithImage(&item
);
4294 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4295 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4298 ResetVisibleLinesRange();
4304 m_selStore
.OnItemDelete(index
);
4308 m_lines
.RemoveAt( index
);
4311 // we need to refresh the (vert) scrollbar as the number of items changed
4314 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4316 RefreshAfter(index
);
4319 void wxListMainWindow::DeleteColumn( int col
)
4321 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4323 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4326 delete node
->GetData();
4327 m_columns
.Erase( node
);
4331 // update all the items
4332 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4334 wxListLineData
* const line
= GetLine(i
);
4335 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4336 delete n
->GetData();
4337 line
->m_items
.Erase(n
);
4341 if ( InReportView() ) // we only cache max widths when in Report View
4343 delete m_aColWidths
.Item(col
);
4344 m_aColWidths
.RemoveAt(col
);
4347 // invalidate it as it has to be recalculated
4351 void wxListMainWindow::DoDeleteAllItems()
4354 // nothing to do - in particular, don't send the event
4359 // to make the deletion of all items faster, we don't send the
4360 // notifications for each item deletion in this case but only one event
4361 // for all of them: this is compatible with wxMSW and documented in
4362 // DeleteAllItems() description
4364 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4365 event
.SetEventObject( GetParent() );
4366 GetParent()->GetEventHandler()->ProcessEvent( event
);
4374 if ( InReportView() )
4376 ResetVisibleLinesRange();
4377 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4379 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4386 void wxListMainWindow::DeleteAllItems()
4390 RecalculatePositions();
4393 void wxListMainWindow::DeleteEverything()
4395 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4396 WX_CLEAR_ARRAY(m_aColWidths
);
4401 // ----------------------------------------------------------------------------
4402 // scanning for an item
4403 // ----------------------------------------------------------------------------
4405 void wxListMainWindow::EnsureVisible( long index
)
4407 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4408 _T("invalid index in EnsureVisible") );
4410 // We have to call this here because the label in question might just have
4411 // been added and its position is not known yet
4413 RecalculatePositions(true /* no refresh */);
4415 MoveToItem((size_t)index
);
4418 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4425 size_t count
= GetItemCount();
4426 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4428 wxListLineData
*line
= GetLine(i
);
4429 if ( line
->GetText(0) == tmp
)
4436 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4442 size_t count
= GetItemCount();
4443 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4445 wxListLineData
*line
= GetLine(i
);
4447 line
->GetItem( 0, item
);
4448 if (item
.m_data
== data
)
4455 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4458 GetVisibleLinesRange( &topItem
, NULL
);
4461 GetItemPosition( GetItemCount() - 1, p
);
4465 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4466 if ( id
>= 0 && id
< (long)GetItemCount() )
4472 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4474 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4476 size_t count
= GetItemCount();
4478 if ( InReportView() )
4480 size_t current
= y
/ GetLineHeight();
4481 if ( current
< count
)
4483 flags
= HitTestLine(current
, x
, y
);
4490 // TODO: optimize it too! this is less simple than for report view but
4491 // enumerating all items is still not a way to do it!!
4492 for ( size_t current
= 0; current
< count
; current
++ )
4494 flags
= HitTestLine(current
, x
, y
);
4503 // ----------------------------------------------------------------------------
4505 // ----------------------------------------------------------------------------
4507 void wxListMainWindow::InsertItem( wxListItem
&item
)
4509 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4511 int count
= GetItemCount();
4512 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4514 if (item
.m_itemId
> count
)
4515 item
.m_itemId
= count
;
4517 size_t id
= item
.m_itemId
;
4521 if ( InReportView() )
4523 ResetVisibleLinesRange();
4525 // calculate the width of the item and adjust the max column width
4526 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4527 int width
= GetItemWidthWithImage(&item
);
4528 item
.SetWidth(width
);
4529 if (width
> pWidthInfo
->nMaxWidth
)
4530 pWidthInfo
->nMaxWidth
= width
;
4533 wxListLineData
*line
= new wxListLineData(this);
4535 line
->SetItem( item
.m_col
, item
);
4537 m_lines
.Insert( line
, id
);
4541 // If an item is selected at or below the point of insertion, we need to
4542 // increment the member variables because the current row's index has gone
4544 if ( HasCurrent() && m_current
>= id
)
4547 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4549 RefreshLines(id
, GetItemCount() - 1);
4552 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4555 if ( InReportView() )
4557 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4558 item
.m_width
= GetTextLength( item
.m_text
);
4560 wxListHeaderData
*column
= new wxListHeaderData( item
);
4561 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4563 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4566 wxListHeaderDataList::compatibility_iterator
4567 node
= m_columns
.Item( col
);
4568 m_columns
.Insert( node
, column
);
4569 m_aColWidths
.Insert( colWidthInfo
, col
);
4573 m_columns
.Append( column
);
4574 m_aColWidths
.Add( colWidthInfo
);
4579 // update all the items
4580 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4582 wxListLineData
* const line
= GetLine(i
);
4583 wxListItemData
* const data
= new wxListItemData(this);
4585 line
->m_items
.Insert(col
, data
);
4587 line
->m_items
.Append(data
);
4591 // invalidate it as it has to be recalculated
4596 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4599 wxClientDC
dc(this);
4601 dc
.SetFont( GetFont() );
4603 if (item
->GetImage() != -1)
4606 GetImageSize( item
->GetImage(), ix
, iy
);
4610 if (!item
->GetText().empty())
4613 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4620 // ----------------------------------------------------------------------------
4622 // ----------------------------------------------------------------------------
4624 wxListCtrlCompare list_ctrl_compare_func_2
;
4625 long list_ctrl_compare_data
;
4627 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4629 wxListLineData
*line1
= *arg1
;
4630 wxListLineData
*line2
= *arg2
;
4632 line1
->GetItem( 0, item
);
4633 wxUIntPtr data1
= item
.m_data
;
4634 line2
->GetItem( 0, item
);
4635 wxUIntPtr data2
= item
.m_data
;
4636 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4639 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4641 list_ctrl_compare_func_2
= fn
;
4642 list_ctrl_compare_data
= data
;
4643 m_lines
.Sort( list_ctrl_compare_func_1
);
4647 // ----------------------------------------------------------------------------
4649 // ----------------------------------------------------------------------------
4651 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4653 // update our idea of which lines are shown when we redraw the window the
4655 ResetVisibleLinesRange();
4658 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4659 wxScrolledWindow::OnScroll(event
);
4661 HandleOnScroll( event
);
4664 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4666 wxGenericListCtrl
* lc
= GetListCtrl();
4667 wxCHECK_RET( lc
, _T("no listctrl window?") );
4669 lc
->m_headerWin
->Refresh();
4670 lc
->m_headerWin
->Update();
4674 int wxListMainWindow::GetCountPerPage() const
4676 if ( !m_linesPerPage
)
4678 wxConstCast(this, wxListMainWindow
)->
4679 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4682 return m_linesPerPage
;
4685 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4687 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4689 if ( m_lineFrom
== (size_t)-1 )
4691 size_t count
= GetItemCount();
4694 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4696 // this may happen if SetScrollbars() hadn't been called yet
4697 if ( m_lineFrom
>= count
)
4698 m_lineFrom
= count
- 1;
4700 // we redraw one extra line but this is needed to make the redrawing
4701 // logic work when there is a fractional number of lines on screen
4702 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4703 if ( m_lineTo
>= count
)
4704 m_lineTo
= count
- 1;
4706 else // empty control
4709 m_lineTo
= (size_t)-1;
4713 wxASSERT_MSG( IsEmpty() ||
4714 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4715 _T("GetVisibleLinesRange() returns incorrect result") );
4723 // -------------------------------------------------------------------------------------
4724 // wxGenericListCtrl
4725 // -------------------------------------------------------------------------------------
4727 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4729 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4730 EVT_SIZE(wxGenericListCtrl::OnSize
)
4733 wxGenericListCtrl::wxGenericListCtrl()
4735 m_imageListNormal
= (wxImageListType
*) NULL
;
4736 m_imageListSmall
= (wxImageListType
*) NULL
;
4737 m_imageListState
= (wxImageListType
*) NULL
;
4739 m_ownsImageListNormal
=
4740 m_ownsImageListSmall
=
4741 m_ownsImageListState
= false;
4743 m_mainWin
= (wxListMainWindow
*) NULL
;
4744 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4748 wxGenericListCtrl::~wxGenericListCtrl()
4750 if (m_ownsImageListNormal
)
4751 delete m_imageListNormal
;
4752 if (m_ownsImageListSmall
)
4753 delete m_imageListSmall
;
4754 if (m_ownsImageListState
)
4755 delete m_imageListState
;
4758 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4764 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4766 // we use 'g' to get the descent, too
4768 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4769 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4772 // only update if changed
4773 if ( h
!= m_headerHeight
)
4778 ResizeReportView(true);
4779 else //why is this needed if it doesn't have a header?
4780 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4785 void wxGenericListCtrl::CreateHeaderWindow()
4787 m_headerWin
= new wxListHeaderWindow
4789 this, wxID_ANY
, m_mainWin
,
4791 wxSize(GetClientSize().x
, m_headerHeight
),
4794 CalculateAndSetHeaderHeight();
4797 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4802 const wxValidator
&validator
,
4803 const wxString
&name
)
4807 m_imageListState
= (wxImageListType
*) NULL
;
4808 m_ownsImageListNormal
=
4809 m_ownsImageListSmall
=
4810 m_ownsImageListState
= false;
4812 m_mainWin
= (wxListMainWindow
*) NULL
;
4813 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4817 if ( !(style
& wxLC_MASK_TYPE
) )
4819 style
= style
| wxLC_LIST
;
4822 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4825 // don't create the inner window with the border
4826 style
&= ~wxBORDER_MASK
;
4828 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4830 #ifdef __WXMAC_CARBON__
4831 // Human Interface Guidelines ask us for a special font in this case
4832 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
4835 font
.MacCreateThemeFont( kThemeViewsFont
);
4840 if ( InReportView() )
4842 CreateHeaderWindow();
4844 #ifdef __WXMAC_CARBON__
4848 font
.MacCreateThemeFont( kThemeSmallSystemFont
);
4849 m_headerWin
->SetFont( font
);
4850 CalculateAndSetHeaderHeight();
4854 if ( HasFlag(wxLC_NO_HEADER
) )
4855 // VZ: why do we create it at all then?
4856 m_headerWin
->Show( false );
4864 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4866 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4867 _T("wxLC_VIRTUAL can't be [un]set") );
4869 long flag
= GetWindowStyle();
4873 if (style
& wxLC_MASK_TYPE
)
4874 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4875 if (style
& wxLC_MASK_ALIGN
)
4876 flag
&= ~wxLC_MASK_ALIGN
;
4877 if (style
& wxLC_MASK_SORT
)
4878 flag
&= ~wxLC_MASK_SORT
;
4886 SetWindowStyleFlag( flag
);
4889 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4893 m_mainWin
->DeleteEverything();
4895 // has the header visibility changed?
4896 bool hasHeader
= HasHeader();
4897 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4899 if ( hasHeader
!= willHaveHeader
)
4906 // don't delete, just hide, as we can reuse it later
4907 m_headerWin
->Show(false);
4909 //else: nothing to do
4911 else // must show header
4915 CreateHeaderWindow();
4917 else // already have it, just show
4919 m_headerWin
->Show( true );
4923 ResizeReportView(willHaveHeader
);
4927 wxWindow::SetWindowStyleFlag( flag
);
4930 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4932 m_mainWin
->GetColumn( col
, item
);
4936 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4938 m_mainWin
->SetColumn( col
, item
);
4942 int wxGenericListCtrl::GetColumnWidth( int col
) const
4944 return m_mainWin
->GetColumnWidth( col
);
4947 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4949 m_mainWin
->SetColumnWidth( col
, width
);
4953 int wxGenericListCtrl::GetCountPerPage() const
4955 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4958 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4960 m_mainWin
->GetItem( info
);
4964 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4966 m_mainWin
->SetItem( info
);
4970 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4973 info
.m_text
= label
;
4974 info
.m_mask
= wxLIST_MASK_TEXT
;
4975 info
.m_itemId
= index
;
4979 info
.m_image
= imageId
;
4980 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4983 m_mainWin
->SetItem(info
);
4987 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4989 return m_mainWin
->GetItemState( item
, stateMask
);
4992 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4994 m_mainWin
->SetItemState( item
, state
, stateMask
);
4999 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5001 return SetItemColumnImage(item
, 0, image
);
5005 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5008 info
.m_image
= image
;
5009 info
.m_mask
= wxLIST_MASK_IMAGE
;
5010 info
.m_itemId
= item
;
5011 info
.m_col
= column
;
5012 m_mainWin
->SetItem( info
);
5016 wxString
wxGenericListCtrl::GetItemText( long item
) const
5018 return m_mainWin
->GetItemText(item
);
5021 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5023 m_mainWin
->SetItemText(item
, str
);
5026 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5029 info
.m_mask
= wxLIST_MASK_DATA
;
5030 info
.m_itemId
= item
;
5031 m_mainWin
->GetItem( info
);
5035 bool wxGenericListCtrl::SetItemData( long item
, long data
)
5038 info
.m_mask
= wxLIST_MASK_DATA
;
5039 info
.m_itemId
= item
;
5041 m_mainWin
->SetItem( info
);
5045 wxRect
wxGenericListCtrl::GetViewRect() const
5047 return m_mainWin
->GetViewRect();
5050 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5052 m_mainWin
->GetItemRect( item
, rect
);
5053 if ( m_mainWin
->HasHeader() )
5054 rect
.y
+= m_headerHeight
+ 1;
5058 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5060 m_mainWin
->GetItemPosition( item
, pos
);
5064 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5069 int wxGenericListCtrl::GetItemCount() const
5071 return m_mainWin
->GetItemCount();
5074 int wxGenericListCtrl::GetColumnCount() const
5076 return m_mainWin
->GetColumnCount();
5079 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5081 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5084 wxSize
wxGenericListCtrl::GetItemSpacing() const
5086 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5088 return wxSize(spacing
, spacing
);
5091 #if WXWIN_COMPATIBILITY_2_6
5092 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5094 return m_mainWin
->GetItemSpacing( isSmall
);
5096 #endif // WXWIN_COMPATIBILITY_2_6
5098 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5101 info
.m_itemId
= item
;
5102 info
.SetTextColour( col
);
5103 m_mainWin
->SetItem( info
);
5106 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5109 info
.m_itemId
= item
;
5110 m_mainWin
->GetItem( info
);
5111 return info
.GetTextColour();
5114 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5117 info
.m_itemId
= item
;
5118 info
.SetBackgroundColour( col
);
5119 m_mainWin
->SetItem( info
);
5122 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5125 info
.m_itemId
= item
;
5126 m_mainWin
->GetItem( info
);
5127 return info
.GetBackgroundColour();
5130 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5133 info
.m_itemId
= item
;
5135 m_mainWin
->SetItem( info
);
5138 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5141 info
.m_itemId
= item
;
5142 m_mainWin
->GetItem( info
);
5143 return info
.GetFont();
5146 int wxGenericListCtrl::GetSelectedItemCount() const
5148 return m_mainWin
->GetSelectedItemCount();
5151 wxColour
wxGenericListCtrl::GetTextColour() const
5153 return GetForegroundColour();
5156 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5158 SetForegroundColour(col
);
5161 long wxGenericListCtrl::GetTopItem() const
5164 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5168 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5170 return m_mainWin
->GetNextItem( item
, geom
, state
);
5173 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
5175 if (which
== wxIMAGE_LIST_NORMAL
)
5176 return m_imageListNormal
;
5177 else if (which
== wxIMAGE_LIST_SMALL
)
5178 return m_imageListSmall
;
5179 else if (which
== wxIMAGE_LIST_STATE
)
5180 return m_imageListState
;
5182 return (wxImageListType
*) NULL
;
5185 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
5187 if ( which
== wxIMAGE_LIST_NORMAL
)
5189 if (m_ownsImageListNormal
)
5190 delete m_imageListNormal
;
5191 m_imageListNormal
= imageList
;
5192 m_ownsImageListNormal
= false;
5194 else if ( which
== wxIMAGE_LIST_SMALL
)
5196 if (m_ownsImageListSmall
)
5197 delete m_imageListSmall
;
5198 m_imageListSmall
= imageList
;
5199 m_ownsImageListSmall
= false;
5201 else if ( which
== wxIMAGE_LIST_STATE
)
5203 if (m_ownsImageListState
)
5204 delete m_imageListState
;
5205 m_imageListState
= imageList
;
5206 m_ownsImageListState
= false;
5209 m_mainWin
->SetImageList( imageList
, which
);
5212 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
5214 SetImageList(imageList
, which
);
5215 if ( which
== wxIMAGE_LIST_NORMAL
)
5216 m_ownsImageListNormal
= true;
5217 else if ( which
== wxIMAGE_LIST_SMALL
)
5218 m_ownsImageListSmall
= true;
5219 else if ( which
== wxIMAGE_LIST_STATE
)
5220 m_ownsImageListState
= true;
5223 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5228 bool wxGenericListCtrl::DeleteItem( long item
)
5230 m_mainWin
->DeleteItem( item
);
5234 bool wxGenericListCtrl::DeleteAllItems()
5236 m_mainWin
->DeleteAllItems();
5240 bool wxGenericListCtrl::DeleteAllColumns()
5242 size_t count
= m_mainWin
->m_columns
.GetCount();
5243 for ( size_t n
= 0; n
< count
; n
++ )
5248 void wxGenericListCtrl::ClearAll()
5250 m_mainWin
->DeleteEverything();
5253 bool wxGenericListCtrl::DeleteColumn( int col
)
5255 m_mainWin
->DeleteColumn( col
);
5257 // if we don't have the header any longer, we need to relayout the window
5258 if ( !GetColumnCount() )
5259 ResizeReportView(false /* no header */);
5263 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5264 wxClassInfo
* textControlClass
)
5266 return m_mainWin
->EditLabel( item
, textControlClass
);
5269 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5271 return m_mainWin
->GetEditControl();
5274 bool wxGenericListCtrl::EnsureVisible( long item
)
5276 m_mainWin
->EnsureVisible( item
);
5280 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5282 return m_mainWin
->FindItem( start
, str
, partial
);
5285 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5287 return m_mainWin
->FindItem( start
, data
);
5290 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5291 int WXUNUSED(direction
))
5293 return m_mainWin
->FindItem( pt
);
5296 // TODO: sub item hit testing
5297 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5299 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5302 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5304 m_mainWin
->InsertItem( info
);
5305 return info
.m_itemId
;
5308 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5311 info
.m_text
= label
;
5312 info
.m_mask
= wxLIST_MASK_TEXT
;
5313 info
.m_itemId
= index
;
5314 return InsertItem( info
);
5317 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5320 info
.m_mask
= wxLIST_MASK_IMAGE
;
5321 info
.m_image
= imageIndex
;
5322 info
.m_itemId
= index
;
5323 return InsertItem( info
);
5326 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5329 info
.m_text
= label
;
5330 info
.m_image
= imageIndex
;
5331 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5332 info
.m_itemId
= index
;
5333 return InsertItem( info
);
5336 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5338 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5340 m_mainWin
->InsertColumn( col
, item
);
5342 // if we hadn't had a header before but have one now
5343 // then we need to relayout the window
5344 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5345 ResizeReportView(true /* have header */);
5347 m_headerWin
->Refresh();
5352 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5353 int format
, int width
)
5356 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5357 item
.m_text
= heading
;
5360 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5361 item
.m_width
= width
;
5364 item
.m_format
= format
;
5366 return InsertColumn( col
, item
);
5369 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5375 // fn is a function which takes 3 long arguments: item1, item2, data.
5376 // item1 is the long data associated with a first item (NOT the index).
5377 // item2 is the long data associated with a second item (NOT the index).
5378 // data is the same value as passed to SortItems.
5379 // The return value is a negative number if the first item should precede the second
5380 // item, a positive number of the second item should precede the first,
5381 // or zero if the two items are equivalent.
5382 // data is arbitrary data to be passed to the sort function.
5384 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5386 m_mainWin
->SortItems( fn
, data
);
5390 // ----------------------------------------------------------------------------
5392 // ----------------------------------------------------------------------------
5394 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5399 ResizeReportView(m_mainWin
->HasHeader());
5400 m_mainWin
->RecalculatePositions();
5403 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5406 GetClientSize( &cw
, &ch
);
5410 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5411 if(ch
> m_headerHeight
)
5412 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5413 cw
, ch
- m_headerHeight
- 1 );
5415 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5418 else // no header window
5420 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5424 void wxGenericListCtrl::OnInternalIdle()
5426 wxWindow::OnInternalIdle();
5428 // do it only if needed
5429 if ( !m_mainWin
->m_dirty
)
5432 m_mainWin
->RecalculatePositions();
5435 // ----------------------------------------------------------------------------
5437 // ----------------------------------------------------------------------------
5439 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5443 m_mainWin
->SetBackgroundColour( colour
);
5444 m_mainWin
->m_dirty
= true;
5450 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5452 if ( !wxWindow::SetForegroundColour( colour
) )
5457 m_mainWin
->SetForegroundColour( colour
);
5458 m_mainWin
->m_dirty
= true;
5462 m_headerWin
->SetForegroundColour( colour
);
5467 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5469 if ( !wxWindow::SetFont( font
) )
5474 m_mainWin
->SetFont( font
);
5475 m_mainWin
->m_dirty
= true;
5480 m_headerWin
->SetFont( font
);
5481 CalculateAndSetHeaderHeight();
5491 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5494 // Use the same color scheme as wxListBox
5495 return wxListBox::GetClassDefaultAttributes(variant
);
5497 wxUnusedVar(variant
);
5498 wxVisualAttributes attr
;
5499 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5500 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5501 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5506 // ----------------------------------------------------------------------------
5507 // methods forwarded to m_mainWin
5508 // ----------------------------------------------------------------------------
5510 #if wxUSE_DRAG_AND_DROP
5512 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5514 m_mainWin
->SetDropTarget( dropTarget
);
5517 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5519 return m_mainWin
->GetDropTarget();
5524 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5526 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5529 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5531 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5534 wxColour
wxGenericListCtrl::GetForegroundColour() const
5536 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5539 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5542 return m_mainWin
->PopupMenu( menu
, x
, y
);
5548 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5550 m_mainWin
->DoClientToScreen(x
, y
);
5553 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5555 m_mainWin
->DoScreenToClient(x
, y
);
5558 void wxGenericListCtrl::SetFocus()
5560 // The test in window.cpp fails as we are a composite
5561 // window, so it checks against "this", but not m_mainWin.
5562 if ( DoFindFocus() != this )
5563 m_mainWin
->SetFocus();
5566 wxSize
wxGenericListCtrl::DoGetBestSize() const
5568 // Something is better than nothing...
5569 // 100x80 is what the MSW version will get from the default
5570 // wxControl::DoGetBestSize
5571 return wxSize(100, 80);
5574 // ----------------------------------------------------------------------------
5575 // virtual list control support
5576 // ----------------------------------------------------------------------------
5578 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5580 // this is a pure virtual function, in fact - which is not really pure
5581 // because the controls which are not virtual don't need to implement it
5582 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5584 return wxEmptyString
;
5587 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5589 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5591 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5595 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5598 return OnGetItemImage(item
);
5604 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5606 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5607 _T("invalid item index in OnGetItemAttr()") );
5609 // no attributes by default
5613 void wxGenericListCtrl::SetItemCount(long count
)
5615 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5617 m_mainWin
->SetItemCount(count
);
5620 void wxGenericListCtrl::RefreshItem(long item
)
5622 m_mainWin
->RefreshLine(item
);
5625 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5627 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5630 // Generic wxListCtrl is more or less a container for two other
5631 // windows which drawings are done upon. These are namely
5632 // 'm_headerWin' and 'm_mainWin'.
5633 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5634 // behaviour wxListCtrl has under wxMSW.
5636 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5640 // The easy case, no rectangle specified.
5642 m_headerWin
->Refresh(eraseBackground
);
5645 m_mainWin
->Refresh(eraseBackground
);
5649 // Refresh the header window
5652 wxRect rectHeader
= m_headerWin
->GetRect();
5653 rectHeader
.Intersect(*rect
);
5654 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5657 m_headerWin
->GetPosition(&x
, &y
);
5658 rectHeader
.Offset(-x
, -y
);
5659 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5663 // Refresh the main window
5666 wxRect rectMain
= m_mainWin
->GetRect();
5667 rectMain
.Intersect(*rect
);
5668 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5671 m_mainWin
->GetPosition(&x
, &y
);
5672 rectMain
.Offset(-x
, -y
);
5673 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5679 void wxGenericListCtrl::Freeze()
5681 m_mainWin
->Freeze();
5684 void wxGenericListCtrl::Thaw()
5689 #endif // wxUSE_LISTCTRL