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 #include "wx/listctrl.h"
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && !defined(__WXMAC__)
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
45 #include "wx/settings.h"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/osx/private.h"
57 // NOTE: If using the wxListBox visual attributes works everywhere then this can
58 // be removed, as well as the #else case below.
59 #define _USE_VISATTR 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // // the height of the header window (FIXME: should depend on its font!)
67 // static const int HEADER_HEIGHT = 23;
69 static const int SCROLL_UNIT_X
= 15;
71 // the spacing between the lines (in report mode)
72 static const int LINE_SPACING
= 0;
74 // extra margins around the text label
76 static const int EXTRA_WIDTH
= 6;
78 static const int EXTRA_WIDTH
= 4;
80 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
105 // ============================================================================
107 // ============================================================================
109 //-----------------------------------------------------------------------------
110 // wxColWidthInfo (internal)
111 //-----------------------------------------------------------------------------
113 struct wxColWidthInfo
116 bool bNeedsUpdate
; // only set to true when an item whose
117 // width == nMaxWidth is removed
119 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
122 bNeedsUpdate
= needsUpdate
;
126 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
128 //-----------------------------------------------------------------------------
129 // wxListItemData (internal)
130 //-----------------------------------------------------------------------------
135 wxListItemData(wxListMainWindow
*owner
);
138 void SetItem( const wxListItem
&info
);
139 void SetImage( int image
) { m_image
= image
; }
140 void SetData( wxUIntPtr data
) { m_data
= data
; }
141 void SetPosition( int x
, int y
);
142 void SetSize( int width
, int height
);
144 bool HasText() const { return !m_text
.empty(); }
145 const wxString
& GetText() const { return m_text
; }
146 void SetText(const wxString
& text
) { m_text
= text
; }
148 // we can't use empty string for measuring the string width/height, so
149 // always return something
150 wxString
GetTextForMeasuring() const
152 wxString s
= GetText();
159 bool IsHit( int x
, int y
) const;
163 int GetWidth() const;
164 int GetHeight() const;
166 int GetImage() const { return m_image
; }
167 bool HasImage() const { return GetImage() != -1; }
169 void GetItem( wxListItem
&info
) const;
171 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
172 wxListItemAttr
*GetAttr() const { return m_attr
; }
175 // the item image or -1
178 // user data associated with the item
181 // the item coordinates are not used in report mode; instead this pointer is
182 // NULL and the owner window is used to retrieve the item position and size
185 // the list ctrl we are in
186 wxListMainWindow
*m_owner
;
188 // custom attributes or NULL
189 wxListItemAttr
*m_attr
;
192 // common part of all ctors
198 //-----------------------------------------------------------------------------
199 // wxListHeaderData (internal)
200 //-----------------------------------------------------------------------------
202 class wxListHeaderData
: public wxObject
206 wxListHeaderData( const wxListItem
&info
);
207 void SetItem( const wxListItem
&item
);
208 void SetPosition( int x
, int y
);
209 void SetWidth( int w
);
210 void SetState( int state
);
211 void SetFormat( int format
);
212 void SetHeight( int h
);
213 bool HasImage() const;
215 bool HasText() const { return !m_text
.empty(); }
216 const wxString
& GetText() const { return m_text
; }
217 void SetText(const wxString
& text
) { m_text
= text
; }
219 void GetItem( wxListItem
&item
);
221 bool IsHit( int x
, int y
) const;
222 int GetImage() const;
223 int GetWidth() const;
224 int GetFormat() const;
225 int GetState() const;
242 //-----------------------------------------------------------------------------
243 // wxListLineData (internal)
244 //-----------------------------------------------------------------------------
246 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
247 #include "wx/listimpl.cpp"
248 WX_DEFINE_LIST(wxListItemDataList
)
253 // the list of subitems: only may have more than one item in report mode
254 wxListItemDataList m_items
;
256 // this is not used in report view
268 // the part to be highlighted
269 wxRect m_rectHighlight
;
271 // extend all our rects to be centered inside the one of given width
272 void ExtendWidth(wxCoord w
)
274 wxASSERT_MSG( m_rectAll
.width
<= w
,
275 _T("width can only be increased") );
278 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
279 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
280 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
285 // is this item selected? [NB: not used in virtual mode]
288 // back pointer to the list ctrl
289 wxListMainWindow
*m_owner
;
292 wxListLineData(wxListMainWindow
*owner
);
296 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
300 // are we in report mode?
301 inline bool InReportView() const;
303 // are we in virtual report mode?
304 inline bool IsVirtual() const;
306 // these 2 methods shouldn't be called for report view controls, in that
307 // case we determine our position/size ourselves
309 // calculate the size of the line
310 void CalculateSize( wxDC
*dc
, int spacing
);
312 // remember the position this line appears at
313 void SetPosition( int x
, int y
, int spacing
);
317 void SetImage( int image
) { SetImage(0, image
); }
318 int GetImage() const { return GetImage(0); }
319 void SetImage( int index
, int image
);
320 int GetImage( int index
) const;
322 bool HasImage() const { return GetImage() != -1; }
323 bool HasText() const { return !GetText(0).empty(); }
325 void SetItem( int index
, const wxListItem
&info
);
326 void GetItem( int index
, wxListItem
&info
);
328 wxString
GetText(int index
) const;
329 void SetText( int index
, const wxString
& s
);
331 wxListItemAttr
*GetAttr() const;
332 void SetAttr(wxListItemAttr
*attr
);
334 // return true if the highlighting really changed
335 bool Highlight( bool on
);
337 void ReverseHighlight();
339 bool IsHighlighted() const
341 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
343 return m_highlighted
;
346 // draw the line on the given DC in icon/list mode
347 void Draw( wxDC
*dc
);
349 // the same in report mode
350 void DrawInReportMode( wxDC
*dc
,
352 const wxRect
& rectHL
,
356 // set the line to contain num items (only can be > 1 in report mode)
357 void InitItems( int num
);
359 // get the mode (i.e. style) of the list control
360 inline int GetMode() const;
362 // prepare the DC for drawing with these item's attributes, return true if
363 // we need to draw the items background to highlight it, false otherwise
364 bool SetAttributes(wxDC
*dc
,
365 const wxListItemAttr
*attr
,
368 // draw the text on the DC with the correct justification; also add an
369 // ellipsis if the text is too large to fit in the current width
370 void DrawTextFormatted(wxDC
*dc
,
371 const wxString
&text
,
374 int yMid
, // this is middle, not top, of the text
378 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
379 #include "wx/arrimpl.cpp"
380 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
382 //-----------------------------------------------------------------------------
383 // wxListHeaderWindow (internal)
384 //-----------------------------------------------------------------------------
386 class wxListHeaderWindow
: public wxWindow
389 wxListMainWindow
*m_owner
;
390 const wxCursor
*m_currentCursor
;
391 wxCursor
*m_resizeCursor
;
394 // column being resized or -1
397 // divider line position in logical (unscrolled) coords
400 // minimal position beyond which the divider line
401 // can't be dragged in logical coords
405 wxListHeaderWindow();
407 wxListHeaderWindow( wxWindow
*win
,
409 wxListMainWindow
*owner
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
413 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
415 virtual ~wxListHeaderWindow();
418 void AdjustDC( wxDC
& dc
);
420 void OnPaint( wxPaintEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
428 // common part of all ctors
431 // generate and process the list event of the given type, return true if
432 // it wasn't vetoed, i.e. if we should proceed
433 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
435 DECLARE_EVENT_TABLE()
438 //-----------------------------------------------------------------------------
439 // wxListRenameTimer (internal)
440 //-----------------------------------------------------------------------------
442 class wxListRenameTimer
: public wxTimer
445 wxListMainWindow
*m_owner
;
448 wxListRenameTimer( wxListMainWindow
*owner
);
452 //-----------------------------------------------------------------------------
453 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
454 //-----------------------------------------------------------------------------
456 class wxListTextCtrlWrapper
: public wxEvtHandler
459 // NB: text must be a valid object but not Create()d yet
460 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
464 wxTextCtrl
*GetText() const { return m_text
; }
466 void EndEdit( bool discardChanges
);
469 void OnChar( wxKeyEvent
&event
);
470 void OnKeyUp( wxKeyEvent
&event
);
471 void OnKillFocus( wxFocusEvent
&event
);
473 bool AcceptChanges();
474 void Finish( bool setfocus
);
477 wxListMainWindow
*m_owner
;
479 wxString m_startValue
;
481 bool m_aboutToFinish
;
483 DECLARE_EVENT_TABLE()
486 //-----------------------------------------------------------------------------
487 // wxListMainWindow (internal)
488 //-----------------------------------------------------------------------------
490 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
491 #include "wx/listimpl.cpp"
492 WX_DEFINE_LIST(wxListHeaderDataList
)
494 class wxListMainWindow
: public wxScrolledCanvas
498 wxListMainWindow( wxWindow
*parent
,
500 const wxPoint
& pos
= wxDefaultPosition
,
501 const wxSize
& size
= wxDefaultSize
,
503 const wxString
&name
= _T("listctrlmainwindow") );
505 virtual ~wxListMainWindow();
507 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
509 // return true if this is a virtual list control
510 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
512 // return true if the control is in report mode
513 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
515 // return true if we are in single selection mode, false if multi sel
516 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
518 // do we have a header window?
519 bool HasHeader() const
520 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
522 void HighlightAll( bool on
);
524 // all these functions only do something if the line is currently visible
526 // change the line "selected" state, return true if it really changed
527 bool HighlightLine( size_t line
, bool highlight
= true);
529 // as HighlightLine() but do it for the range of lines: this is incredibly
530 // more efficient for virtual list controls!
532 // NB: unlike HighlightLine() this one does refresh the lines on screen
533 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
535 // toggle the line state and refresh it
536 void ReverseHighlight( size_t line
)
537 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
539 // return true if the line is highlighted
540 bool IsHighlighted(size_t line
) const;
542 // refresh one or several lines at once
543 void RefreshLine( size_t line
);
544 void RefreshLines( size_t lineFrom
, size_t lineTo
);
546 // refresh all selected items
547 void RefreshSelected();
549 // refresh all lines below the given one: the difference with
550 // RefreshLines() is that the index here might not be a valid one (happens
551 // when the last line is deleted)
552 void RefreshAfter( size_t lineFrom
);
554 // the methods which are forwarded to wxListLineData itself in list/icon
555 // modes but are here because the lines don't store their positions in the
558 // get the bound rect for the entire line
559 wxRect
GetLineRect(size_t line
) const;
561 // get the bound rect of the label
562 wxRect
GetLineLabelRect(size_t line
) const;
564 // get the bound rect of the items icon (only may be called if we do have
566 wxRect
GetLineIconRect(size_t line
) const;
568 // get the rect to be highlighted when the item has focus
569 wxRect
GetLineHighlightRect(size_t line
) const;
571 // get the size of the total line rect
572 wxSize
GetLineSize(size_t line
) const
573 { return GetLineRect(line
).GetSize(); }
575 // return the hit code for the corresponding position (in this line)
576 long HitTestLine(size_t line
, int x
, int y
) const;
578 // bring the selected item into view, scrolling to it if necessary
579 void MoveToItem(size_t item
);
581 bool ScrollList( int WXUNUSED(dx
), int dy
);
583 // bring the current item into view
584 void MoveToFocus() { MoveToItem(m_current
); }
586 // start editing the label of the given item
587 wxTextCtrl
*EditLabel(long item
,
588 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
589 wxTextCtrl
*GetEditControl() const
591 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
594 void ResetTextControl(wxTextCtrl
*text
)
597 m_textctrlWrapper
= NULL
;
600 void OnRenameTimer();
601 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
602 void OnRenameCancelled(size_t itemEdit
);
604 void OnMouse( wxMouseEvent
&event
);
606 // called to switch the selection from the current item to newCurrent,
607 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
609 void OnChar( wxKeyEvent
&event
);
610 void OnKeyDown( wxKeyEvent
&event
);
611 void OnKeyUp( wxKeyEvent
&event
);
612 void OnSetFocus( wxFocusEvent
&event
);
613 void OnKillFocus( wxFocusEvent
&event
);
614 void OnScroll( wxScrollWinEvent
& event
);
616 void OnPaint( wxPaintEvent
&event
);
618 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
619 void GetImageSize( int index
, int &width
, int &height
) const;
620 int GetTextLength( const wxString
&s
) const;
622 void SetImageList( wxImageList
*imageList
, int which
);
623 void SetItemSpacing( int spacing
, bool isSmall
= false );
624 int GetItemSpacing( bool isSmall
= false );
626 void SetColumn( int col
, wxListItem
&item
);
627 void SetColumnWidth( int col
, int width
);
628 void GetColumn( int col
, wxListItem
&item
) const;
629 int GetColumnWidth( int col
) const;
630 int GetColumnCount() const { return m_columns
.GetCount(); }
632 // returns the sum of the heights of all columns
633 int GetHeaderWidth() const;
635 int GetCountPerPage() const;
637 void SetItem( wxListItem
&item
);
638 void GetItem( wxListItem
&item
) const;
639 void SetItemState( long item
, long state
, long stateMask
);
640 void SetItemStateAll( long state
, long stateMask
);
641 int GetItemState( long item
, long stateMask
) const;
642 bool GetItemRect( long item
, wxRect
&rect
) const
644 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
646 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
647 wxRect
GetViewRect() const;
648 bool GetItemPosition( long item
, wxPoint
& pos
) const;
649 int GetSelectedItemCount() const;
651 wxString
GetItemText(long item
) const
654 info
.m_mask
= wxLIST_MASK_TEXT
;
655 info
.m_itemId
= item
;
660 void SetItemText(long item
, const wxString
& value
)
663 info
.m_mask
= wxLIST_MASK_TEXT
;
664 info
.m_itemId
= item
;
669 // set the scrollbars and update the positions of the items
670 void RecalculatePositions(bool noRefresh
= false);
672 // refresh the window and the header
675 long GetNextItem( long item
, int geometry
, int state
) const;
676 void DeleteItem( long index
);
677 void DeleteAllItems();
678 void DeleteColumn( int col
);
679 void DeleteEverything();
680 void EnsureVisible( long index
);
681 long FindItem( long start
, const wxString
& str
, bool partial
= false );
682 long FindItem( long start
, wxUIntPtr data
);
683 long FindItem( const wxPoint
& pt
);
684 long HitTest( int x
, int y
, int &flags
) const;
685 void InsertItem( wxListItem
&item
);
686 void InsertColumn( long col
, wxListItem
&item
);
687 int GetItemWidthWithImage(wxListItem
* item
);
688 void SortItems( wxListCtrlCompare fn
, long data
);
690 size_t GetItemCount() const;
691 bool IsEmpty() const { return GetItemCount() == 0; }
692 void SetItemCount(long count
);
694 // change the current (== focused) item, send a notification event
695 void ChangeCurrent(size_t current
);
696 void ResetCurrent() { ChangeCurrent((size_t)-1); }
697 bool HasCurrent() const { return m_current
!= (size_t)-1; }
699 // send out a wxListEvent
700 void SendNotify( size_t line
,
702 const wxPoint
& point
= wxDefaultPosition
);
704 // override base class virtual to reset m_lineHeight when the font changes
705 virtual bool SetFont(const wxFont
& font
)
707 if ( !wxScrolledCanvas::SetFont(font
) )
715 // these are for wxListLineData usage only
717 // get the backpointer to the list ctrl
718 wxGenericListCtrl
*GetListCtrl() const
720 return wxStaticCast(GetParent(), wxGenericListCtrl
);
723 // get the height of all lines (assuming they all do have the same height)
724 wxCoord
GetLineHeight() const;
726 // get the y position of the given line (only for report view)
727 wxCoord
GetLineY(size_t line
) const;
729 // get the brush to use for the item highlighting
730 wxBrush
*GetHighlightBrush() const
732 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
735 bool HasFocus() const
741 // the array of all line objects for a non virtual list control (for the
742 // virtual list control we only ever use m_lines[0])
743 wxListLineDataArray m_lines
;
745 // the list of column objects
746 wxListHeaderDataList m_columns
;
748 // currently focused item or -1
751 // the number of lines per page
754 // this flag is set when something which should result in the window
755 // redrawing happens (i.e. an item was added or deleted, or its appearance
756 // changed) and OnPaint() doesn't redraw the window while it is set which
757 // allows to minimize the number of repaintings when a lot of items are
758 // being added. The real repainting occurs only after the next OnIdle()
762 wxColour
*m_highlightColour
;
763 wxImageList
*m_small_image_list
;
764 wxImageList
*m_normal_image_list
;
766 int m_normal_spacing
;
770 wxTimer
*m_renameTimer
;
774 ColWidthArray m_aColWidths
;
776 // for double click logic
777 size_t m_lineLastClicked
,
778 m_lineBeforeLastClicked
,
779 m_lineSelectSingleOnUp
;
782 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
784 // the total count of items in a virtual list control
787 // the object maintaining the items selection state, only used in virtual
789 wxSelectionStore m_selStore
;
791 // common part of all ctors
794 // get the line data for the given index
795 wxListLineData
*GetLine(size_t n
) const
797 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
801 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
808 // get a dummy line which can be used for geometry calculations and such:
809 // you must use GetLine() if you want to really draw the line
810 wxListLineData
*GetDummyLine() const;
812 // cache the line data of the n-th line in m_lines[0]
813 void CacheLineData(size_t line
);
815 // get the range of visible lines
816 void GetVisibleLinesRange(size_t *from
, size_t *to
);
818 // force us to recalculate the range of visible lines
819 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
821 // get the colour to be used for drawing the rules
822 wxColour
GetRuleColour() const
824 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
828 // initialize the current item if needed
829 void UpdateCurrent();
831 // delete all items but don't refresh: called from dtor
832 void DoDeleteAllItems();
834 // the height of one line using the current font
835 wxCoord m_lineHeight
;
837 // the total header width or 0 if not calculated yet
838 wxCoord m_headerWidth
;
840 // the first and last lines being shown on screen right now (inclusive),
841 // both may be -1 if they must be calculated so never access them directly:
842 // use GetVisibleLinesRange() above instead
846 // the brushes to use for item highlighting when we do/don't have focus
847 wxBrush
*m_highlightBrush
,
848 *m_highlightUnfocusedBrush
;
850 // wrapper around the text control currently used for in place editing or
851 // NULL if no item is being edited
852 wxListTextCtrlWrapper
*m_textctrlWrapper
;
855 DECLARE_EVENT_TABLE()
857 friend class wxGenericListCtrl
;
861 wxListItemData::~wxListItemData()
863 // in the virtual list control the attributes are managed by the main
864 // program, so don't delete them
865 if ( !m_owner
->IsVirtual() )
871 void wxListItemData::Init()
879 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
885 if ( owner
->InReportView() )
891 void wxListItemData::SetItem( const wxListItem
&info
)
893 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
894 SetText(info
.m_text
);
895 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
896 m_image
= info
.m_image
;
897 if ( info
.m_mask
& wxLIST_MASK_DATA
)
898 m_data
= info
.m_data
;
900 if ( info
.HasAttributes() )
903 m_attr
->AssignFrom(*info
.GetAttributes());
905 m_attr
= new wxListItemAttr(*info
.GetAttributes());
913 m_rect
->width
= info
.m_width
;
917 void wxListItemData::SetPosition( int x
, int y
)
919 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
925 void wxListItemData::SetSize( int width
, int height
)
927 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
930 m_rect
->width
= width
;
932 m_rect
->height
= height
;
935 bool wxListItemData::IsHit( int x
, int y
) const
937 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
939 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
942 int wxListItemData::GetX() const
944 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
949 int wxListItemData::GetY() const
951 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
956 int wxListItemData::GetWidth() const
958 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
960 return m_rect
->width
;
963 int wxListItemData::GetHeight() const
965 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
967 return m_rect
->height
;
970 void wxListItemData::GetItem( wxListItem
&info
) const
972 long mask
= info
.m_mask
;
974 // by default, get everything for backwards compatibility
977 if ( mask
& wxLIST_MASK_TEXT
)
978 info
.m_text
= m_text
;
979 if ( mask
& wxLIST_MASK_IMAGE
)
980 info
.m_image
= m_image
;
981 if ( mask
& wxLIST_MASK_DATA
)
982 info
.m_data
= m_data
;
986 if ( m_attr
->HasTextColour() )
987 info
.SetTextColour(m_attr
->GetTextColour());
988 if ( m_attr
->HasBackgroundColour() )
989 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
990 if ( m_attr
->HasFont() )
991 info
.SetFont(m_attr
->GetFont());
995 //-----------------------------------------------------------------------------
997 //-----------------------------------------------------------------------------
999 void wxListHeaderData::Init()
1011 wxListHeaderData::wxListHeaderData()
1016 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1023 void wxListHeaderData::SetItem( const wxListItem
&item
)
1025 m_mask
= item
.m_mask
;
1027 if ( m_mask
& wxLIST_MASK_TEXT
)
1028 m_text
= item
.m_text
;
1030 if ( m_mask
& wxLIST_MASK_IMAGE
)
1031 m_image
= item
.m_image
;
1033 if ( m_mask
& wxLIST_MASK_FORMAT
)
1034 m_format
= item
.m_format
;
1036 if ( m_mask
& wxLIST_MASK_WIDTH
)
1037 SetWidth(item
.m_width
);
1039 if ( m_mask
& wxLIST_MASK_STATE
)
1040 SetState(item
.m_state
);
1043 void wxListHeaderData::SetPosition( int x
, int y
)
1049 void wxListHeaderData::SetHeight( int h
)
1054 void wxListHeaderData::SetWidth( int w
)
1056 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1059 void wxListHeaderData::SetState( int flag
)
1064 void wxListHeaderData::SetFormat( int format
)
1069 bool wxListHeaderData::HasImage() const
1071 return m_image
!= -1;
1074 bool wxListHeaderData::IsHit( int x
, int y
) const
1076 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1079 void wxListHeaderData::GetItem( wxListItem
& item
)
1081 item
.m_mask
= m_mask
;
1082 item
.m_text
= m_text
;
1083 item
.m_image
= m_image
;
1084 item
.m_format
= m_format
;
1085 item
.m_width
= m_width
;
1086 item
.m_state
= m_state
;
1089 int wxListHeaderData::GetImage() const
1094 int wxListHeaderData::GetWidth() const
1099 int wxListHeaderData::GetFormat() const
1104 int wxListHeaderData::GetState() const
1109 //-----------------------------------------------------------------------------
1111 //-----------------------------------------------------------------------------
1113 inline int wxListLineData::GetMode() const
1115 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1118 inline bool wxListLineData::InReportView() const
1120 return m_owner
->HasFlag(wxLC_REPORT
);
1123 inline bool wxListLineData::IsVirtual() const
1125 return m_owner
->IsVirtual();
1128 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1132 if ( InReportView() )
1135 m_gi
= new GeometryInfo
;
1137 m_highlighted
= false;
1139 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1142 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1144 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1145 wxCHECK_RET( node
, _T("no subitems at all??") );
1147 wxListItemData
*item
= node
->GetData();
1152 switch ( GetMode() )
1155 case wxLC_SMALL_ICON
:
1156 m_gi
->m_rectAll
.width
= spacing
;
1158 s
= item
->GetText();
1163 m_gi
->m_rectLabel
.width
=
1164 m_gi
->m_rectLabel
.height
= 0;
1168 dc
->GetTextExtent( s
, &lw
, &lh
);
1172 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1174 m_gi
->m_rectAll
.width
= lw
;
1176 m_gi
->m_rectLabel
.width
= lw
;
1177 m_gi
->m_rectLabel
.height
= lh
;
1180 if (item
->HasImage())
1183 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1184 m_gi
->m_rectIcon
.width
= w
+ 8;
1185 m_gi
->m_rectIcon
.height
= h
+ 8;
1187 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1188 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1189 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1190 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1193 if ( item
->HasText() )
1195 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1196 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1198 else // no text, highlight the icon
1200 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1201 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1206 s
= item
->GetTextForMeasuring();
1208 dc
->GetTextExtent( s
, &lw
, &lh
);
1212 m_gi
->m_rectLabel
.width
= lw
;
1213 m_gi
->m_rectLabel
.height
= lh
;
1215 m_gi
->m_rectAll
.width
= lw
;
1216 m_gi
->m_rectAll
.height
= lh
;
1218 if (item
->HasImage())
1221 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1222 m_gi
->m_rectIcon
.width
= w
;
1223 m_gi
->m_rectIcon
.height
= h
;
1225 m_gi
->m_rectAll
.width
+= 4 + w
;
1226 if (h
> m_gi
->m_rectAll
.height
)
1227 m_gi
->m_rectAll
.height
= h
;
1230 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1231 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1235 wxFAIL_MSG( _T("unexpected call to SetSize") );
1239 wxFAIL_MSG( _T("unknown mode") );
1244 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1246 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1247 wxCHECK_RET( node
, _T("no subitems at all??") );
1249 wxListItemData
*item
= node
->GetData();
1251 switch ( GetMode() )
1254 case wxLC_SMALL_ICON
:
1255 m_gi
->m_rectAll
.x
= x
;
1256 m_gi
->m_rectAll
.y
= y
;
1258 if ( item
->HasImage() )
1260 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1261 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1262 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1265 if ( item
->HasText() )
1267 if (m_gi
->m_rectAll
.width
> spacing
)
1268 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1270 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1271 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1272 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1273 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1275 else // no text, highlight the icon
1277 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1278 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1283 m_gi
->m_rectAll
.x
= x
;
1284 m_gi
->m_rectAll
.y
= y
;
1286 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1287 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1288 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1290 if (item
->HasImage())
1292 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1293 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1294 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1298 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1303 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1307 wxFAIL_MSG( _T("unknown mode") );
1312 void wxListLineData::InitItems( int num
)
1314 for (int i
= 0; i
< num
; i
++)
1315 m_items
.Append( new wxListItemData(m_owner
) );
1318 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1320 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1321 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1323 wxListItemData
*item
= node
->GetData();
1324 item
->SetItem( info
);
1327 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1329 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1332 wxListItemData
*item
= node
->GetData();
1333 item
->GetItem( info
);
1337 wxString
wxListLineData::GetText(int index
) const
1341 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1344 wxListItemData
*item
= node
->GetData();
1345 s
= item
->GetText();
1351 void wxListLineData::SetText( int index
, const wxString
& s
)
1353 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1356 wxListItemData
*item
= node
->GetData();
1361 void wxListLineData::SetImage( int index
, int image
)
1363 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1364 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1366 wxListItemData
*item
= node
->GetData();
1367 item
->SetImage(image
);
1370 int wxListLineData::GetImage( int index
) const
1372 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1373 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1375 wxListItemData
*item
= node
->GetData();
1376 return item
->GetImage();
1379 wxListItemAttr
*wxListLineData::GetAttr() const
1381 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1382 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1384 wxListItemData
*item
= node
->GetData();
1385 return item
->GetAttr();
1388 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1390 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1391 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1393 wxListItemData
*item
= node
->GetData();
1394 item
->SetAttr(attr
);
1397 bool wxListLineData::SetAttributes(wxDC
*dc
,
1398 const wxListItemAttr
*attr
,
1401 wxWindow
*listctrl
= m_owner
->GetParent();
1405 // don't use foreground colour for drawing highlighted items - this might
1406 // make them completely invisible (and there is no way to do bit
1407 // arithmetics on wxColour, unfortunately)
1412 if (m_owner
->HasFocus()
1413 #if !defined(__WXUNIVERSAL__)
1414 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1422 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1424 else if ( attr
&& attr
->HasTextColour() )
1425 colText
= attr
->GetTextColour();
1427 colText
= listctrl
->GetForegroundColour();
1429 dc
->SetTextForeground(colText
);
1433 if ( attr
&& attr
->HasFont() )
1434 font
= attr
->GetFont();
1436 font
= listctrl
->GetFont();
1441 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1442 if ( highlighted
|| hasBgCol
)
1445 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1447 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
1449 dc
->SetPen( *wxTRANSPARENT_PEN
);
1457 void wxListLineData::Draw( wxDC
*dc
)
1459 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1460 wxCHECK_RET( node
, _T("no subitems at all??") );
1462 bool highlighted
= IsHighlighted();
1464 wxListItemAttr
*attr
= GetAttr();
1466 if ( SetAttributes(dc
, attr
, highlighted
) )
1467 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1469 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1475 int flags
= wxCONTROL_SELECTED
;
1476 if (m_owner
->HasFocus()
1477 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1478 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1481 flags
|= wxCONTROL_FOCUSED
;
1482 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1487 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1492 // just for debugging to better see where the items are
1494 dc
->SetPen(*wxRED_PEN
);
1495 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1496 dc
->DrawRectangle( m_gi
->m_rectAll
);
1497 dc
->SetPen(*wxGREEN_PEN
);
1498 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1501 wxListItemData
*item
= node
->GetData();
1502 if (item
->HasImage())
1504 // centre the image inside our rectangle, this looks nicer when items
1505 // ae aligned in a row
1506 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1508 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1511 if (item
->HasText())
1513 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1515 wxDCClipper
clipper(*dc
, rectLabel
);
1516 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1520 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1522 const wxRect
& rectHL
,
1525 // TODO: later we should support setting different attributes for
1526 // different columns - to do it, just add "col" argument to
1527 // GetAttr() and move these lines into the loop below
1528 wxListItemAttr
*attr
= GetAttr();
1529 if ( SetAttributes(dc
, attr
, highlighted
) )
1530 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1532 dc
->DrawRectangle( rectHL
);
1538 int flags
= wxCONTROL_SELECTED
;
1539 if (m_owner
->HasFocus())
1540 flags
|= wxCONTROL_FOCUSED
;
1541 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1545 dc
->DrawRectangle( rectHL
);
1550 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1551 yMid
= rect
.y
+ rect
.height
/2;
1553 // This probably needs to be done
1554 // on all platforms as the icons
1555 // otherwise nearly touch the border
1560 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1562 node
= node
->GetNext(), col
++ )
1564 wxListItemData
*item
= node
->GetData();
1566 int width
= m_owner
->GetColumnWidth(col
);
1570 const int wText
= width
- 8;
1571 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1573 if ( item
->HasImage() )
1576 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1577 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1579 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1585 if ( item
->HasText() )
1586 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1590 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1591 const wxString
& textOrig
,
1597 // we don't support displaying multiple lines currently (and neither does
1598 // wxMSW FWIW) so just merge all the lines
1599 wxString
text(textOrig
);
1600 text
.Replace(_T("\n"), _T(" "));
1603 dc
->GetTextExtent(text
, &w
, &h
);
1605 const wxCoord y
= yMid
- (h
+ 1)/2;
1607 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1609 // determine if the string can fit inside the current width
1612 // it can, draw it using the items alignment
1614 m_owner
->GetColumn(col
, item
);
1615 switch ( item
.GetAlign() )
1617 case wxLIST_FORMAT_LEFT
:
1621 case wxLIST_FORMAT_RIGHT
:
1625 case wxLIST_FORMAT_CENTER
:
1626 x
+= (width
- w
) / 2;
1630 wxFAIL_MSG( _T("unknown list item format") );
1634 dc
->DrawText(text
, x
, y
);
1636 else // otherwise, truncate and add an ellipsis if possible
1638 // determine the base width
1639 wxString
ellipsis(wxT("..."));
1641 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1643 // continue until we have enough space or only one character left
1645 size_t len
= text
.length();
1646 wxString drawntext
= text
.Left(len
);
1649 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1650 drawntext
.RemoveLast();
1653 if (w
+ base_w
<= width
)
1657 // if still not enough space, remove ellipsis characters
1658 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1660 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1661 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1664 // now draw the text
1665 dc
->DrawText(drawntext
, x
, y
);
1666 dc
->DrawText(ellipsis
, x
+ w
, y
);
1670 bool wxListLineData::Highlight( bool on
)
1672 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1674 if ( on
== m_highlighted
)
1682 void wxListLineData::ReverseHighlight( void )
1684 Highlight(!IsHighlighted());
1687 //-----------------------------------------------------------------------------
1688 // wxListHeaderWindow
1689 //-----------------------------------------------------------------------------
1691 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1692 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1693 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1694 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1697 void wxListHeaderWindow::Init()
1699 m_currentCursor
= (wxCursor
*) NULL
;
1700 m_isDragging
= false;
1704 wxListHeaderWindow::wxListHeaderWindow()
1708 m_owner
= (wxListMainWindow
*) NULL
;
1709 m_resizeCursor
= (wxCursor
*) NULL
;
1712 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1714 wxListMainWindow
*owner
,
1718 const wxString
&name
)
1719 : wxWindow( win
, id
, pos
, size
, style
, name
)
1724 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1727 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1728 SetOwnForegroundColour( attr
.colFg
);
1729 SetOwnBackgroundColour( attr
.colBg
);
1731 SetOwnFont( attr
.font
);
1733 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1734 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1736 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1740 wxListHeaderWindow::~wxListHeaderWindow()
1742 delete m_resizeCursor
;
1745 #ifdef __WXUNIVERSAL__
1746 #include "wx/univ/renderer.h"
1747 #include "wx/univ/theme.h"
1750 // shift the DC origin to match the position of the main window horz
1751 // scrollbar: this allows us to always use logical coords
1752 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1755 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1758 m_owner
->GetViewStart( &view_start
, NULL
);
1763 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1765 // account for the horz scrollbar offset
1767 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1769 // Maybe we just have to check for m_signX
1770 // in the DC, but I leave the #ifdef __WXGTK__
1772 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1776 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1779 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1781 wxPaintDC
dc( this );
1786 dc
.SetFont( GetFont() );
1788 // width and height of the entire header window
1790 GetClientSize( &w
, &h
);
1791 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1793 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1794 dc
.SetTextForeground(GetForegroundColour());
1796 int x
= HEADER_OFFSET_X
;
1797 int numColumns
= m_owner
->GetColumnCount();
1799 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1801 m_owner
->GetColumn( i
, item
);
1802 int wCol
= item
.m_width
;
1808 if (!m_parent
->IsEnabled())
1809 flags
|= wxCONTROL_DISABLED
;
1811 // NB: The code below is not really Mac-specific, but since we are close
1812 // to 2.8 release and I don't have time to test on other platforms, I
1813 // defined this only for wxMac. If this behavior is desired on
1814 // other platforms, please go ahead and revise or remove the #ifdef.
1816 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1817 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1818 flags
|= wxCONTROL_SELECTED
;
1821 wxRendererNative::Get().DrawHeaderButton
1825 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1829 // see if we have enough space for the column label
1831 // for this we need the width of the text
1834 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1835 wLabel
+= 2 * EXTRA_WIDTH
;
1837 // and the width of the icon, if any
1838 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1839 const int image
= item
.m_image
;
1840 wxImageList
*imageList
;
1843 imageList
= m_owner
->m_small_image_list
;
1846 imageList
->GetSize(image
, ix
, iy
);
1847 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1855 // ignore alignment if there is not enough space anyhow
1857 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1860 wxFAIL_MSG( _T("unknown list item format") );
1863 case wxLIST_FORMAT_LEFT
:
1867 case wxLIST_FORMAT_RIGHT
:
1868 xAligned
= x
+ cw
- wLabel
;
1871 case wxLIST_FORMAT_CENTER
:
1872 xAligned
= x
+ (cw
- wLabel
) / 2;
1876 // draw the text and image clipping them so that they
1877 // don't overwrite the column boundary
1878 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1880 // if we have an image, draw it on the right of the label
1887 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1888 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1889 wxIMAGELIST_DRAW_TRANSPARENT
1893 dc
.DrawText( item
.GetText(),
1894 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1900 void wxListHeaderWindow::DrawCurrent()
1903 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1905 int x1
= m_currentX
;
1907 m_owner
->ClientToScreen( &x1
, &y1
);
1909 int x2
= m_currentX
;
1911 m_owner
->GetClientSize( NULL
, &y2
);
1912 m_owner
->ClientToScreen( &x2
, &y2
);
1915 dc
.SetLogicalFunction( wxINVERT
);
1916 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1917 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1921 dc
.DrawLine( x1
, y1
, x2
, y2
);
1923 dc
.SetLogicalFunction( wxCOPY
);
1925 dc
.SetPen( wxNullPen
);
1926 dc
.SetBrush( wxNullBrush
);
1930 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1932 // we want to work with logical coords
1934 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1935 int y
= event
.GetY();
1939 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1941 // we don't draw the line beyond our window, but we allow dragging it
1944 GetClientSize( &w
, NULL
);
1945 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1948 // erase the line if it was drawn
1949 if ( m_currentX
< w
)
1952 if (event
.ButtonUp())
1955 m_isDragging
= false;
1957 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1958 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1965 m_currentX
= m_minX
+ 7;
1967 // draw in the new location
1968 if ( m_currentX
< w
)
1972 else // not dragging
1975 bool hit_border
= false;
1977 // end of the current column
1980 // find the column where this event occurred
1982 countCol
= m_owner
->GetColumnCount();
1983 for (col
= 0; col
< countCol
; col
++)
1985 xpos
+= m_owner
->GetColumnWidth( col
);
1988 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1990 // near the column border
1997 // inside the column
2004 if ( col
== countCol
)
2007 if (event
.LeftDown() || event
.RightUp())
2009 if (hit_border
&& event
.LeftDown())
2011 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2012 event
.GetPosition()) )
2014 m_isDragging
= true;
2019 //else: column resizing was vetoed by the user code
2021 else // click on a column
2023 // record the selected state of the columns
2024 if (event
.LeftDown())
2026 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2029 m_owner
->GetColumn(i
, colItem
);
2030 long state
= colItem
.GetState();
2032 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2034 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2035 m_owner
->SetColumn(i
, colItem
);
2039 SendListEvent( event
.LeftDown()
2040 ? wxEVT_COMMAND_LIST_COL_CLICK
2041 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2042 event
.GetPosition());
2045 else if (event
.Moving())
2050 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2051 m_currentCursor
= m_resizeCursor
;
2055 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2056 m_currentCursor
= wxSTANDARD_CURSOR
;
2060 SetCursor(*m_currentCursor
);
2065 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2067 m_owner
->SetFocus();
2071 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2073 wxWindow
*parent
= GetParent();
2074 wxListEvent
le( type
, parent
->GetId() );
2075 le
.SetEventObject( parent
);
2076 le
.m_pointDrag
= pos
;
2078 // the position should be relative to the parent window, not
2079 // this one for compatibility with MSW and common sense: the
2080 // user code doesn't know anything at all about this header
2081 // window, so why should it get positions relative to it?
2082 le
.m_pointDrag
.y
-= GetSize().y
;
2084 le
.m_col
= m_column
;
2085 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2088 //-----------------------------------------------------------------------------
2089 // wxListRenameTimer (internal)
2090 //-----------------------------------------------------------------------------
2092 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2097 void wxListRenameTimer::Notify()
2099 m_owner
->OnRenameTimer();
2102 //-----------------------------------------------------------------------------
2103 // wxListTextCtrlWrapper (internal)
2104 //-----------------------------------------------------------------------------
2106 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2107 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2108 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2109 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2112 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2115 : m_startValue(owner
->GetItemText(itemEdit
)),
2116 m_itemEdited(itemEdit
)
2120 m_aboutToFinish
= false;
2122 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2124 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2125 &rectLabel
.x
, &rectLabel
.y
);
2127 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2128 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2129 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2132 m_text
->PushEventHandler(this);
2135 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2137 m_aboutToFinish
= true;
2139 if ( discardChanges
)
2141 m_owner
->OnRenameCancelled(m_itemEdited
);
2147 // Notify the owner about the changes
2150 // Even if vetoed, close the control (consistent with MSW)
2155 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2157 m_text
->RemoveEventHandler(this);
2158 m_owner
->ResetTextControl( m_text
);
2160 wxPendingDelete
.Append( this );
2163 m_owner
->SetFocus();
2166 bool wxListTextCtrlWrapper::AcceptChanges()
2168 const wxString value
= m_text
->GetValue();
2170 // notice that we should always call OnRenameAccept() to generate the "end
2171 // label editing" event, even if the user hasn't really changed anything
2172 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2174 // vetoed by the user
2178 // accepted, do rename the item (unless nothing changed)
2179 if ( value
!= m_startValue
)
2180 m_owner
->SetItemText(m_itemEdited
, value
);
2185 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2187 switch ( event
.m_keyCode
)
2202 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2204 if (m_aboutToFinish
)
2206 // auto-grow the textctrl:
2207 wxSize parentSize
= m_owner
->GetSize();
2208 wxPoint myPos
= m_text
->GetPosition();
2209 wxSize mySize
= m_text
->GetSize();
2211 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2212 if (myPos
.x
+ sx
> parentSize
.x
)
2213 sx
= parentSize
.x
- myPos
.x
;
2216 m_text
->SetSize(sx
, wxDefaultCoord
);
2222 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2224 if ( !m_aboutToFinish
)
2226 if ( !AcceptChanges() )
2227 m_owner
->OnRenameCancelled( m_itemEdited
);
2232 // We must let the native text control handle focus
2236 //-----------------------------------------------------------------------------
2238 //-----------------------------------------------------------------------------
2240 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
2241 EVT_PAINT (wxListMainWindow::OnPaint
)
2242 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2243 EVT_CHAR (wxListMainWindow::OnChar
)
2244 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2245 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2246 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2247 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2248 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2251 void wxListMainWindow::Init()
2256 m_lineTo
= (size_t)-1;
2262 m_small_image_list
= (wxImageList
*) NULL
;
2263 m_normal_image_list
= (wxImageList
*) NULL
;
2265 m_small_spacing
= 30;
2266 m_normal_spacing
= 40;
2270 m_isCreated
= false;
2272 m_lastOnSame
= false;
2273 m_renameTimer
= new wxListRenameTimer( this );
2274 m_textctrlWrapper
= NULL
;
2278 m_lineSelectSingleOnUp
=
2279 m_lineBeforeLastClicked
= (size_t)-1;
2282 wxListMainWindow::wxListMainWindow()
2287 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2290 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2295 const wxString
&name
)
2296 : wxScrolledCanvas( parent
, id
, pos
, size
,
2297 style
| wxHSCROLL
| wxVSCROLL
, name
)
2301 m_highlightBrush
= new wxBrush
2303 wxSystemSettings::GetColour
2305 wxSYS_COLOUR_HIGHLIGHT
2310 m_highlightUnfocusedBrush
= new wxBrush
2312 wxSystemSettings::GetColour
2314 wxSYS_COLOUR_BTNSHADOW
2319 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2321 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2322 SetOwnForegroundColour( attr
.colFg
);
2323 SetOwnBackgroundColour( attr
.colBg
);
2325 SetOwnFont( attr
.font
);
2328 wxListMainWindow::~wxListMainWindow()
2331 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2332 WX_CLEAR_ARRAY(m_aColWidths
);
2334 delete m_highlightBrush
;
2335 delete m_highlightUnfocusedBrush
;
2336 delete m_renameTimer
;
2339 void wxListMainWindow::CacheLineData(size_t line
)
2341 wxGenericListCtrl
*listctrl
= GetListCtrl();
2343 wxListLineData
*ld
= GetDummyLine();
2345 size_t countCol
= GetColumnCount();
2346 for ( size_t col
= 0; col
< countCol
; col
++ )
2348 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2349 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2352 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2355 wxListLineData
*wxListMainWindow::GetDummyLine() const
2357 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2358 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2360 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2362 // we need to recreate the dummy line if the number of columns in the
2363 // control changed as it would have the incorrect number of fields
2365 if ( !m_lines
.IsEmpty() &&
2366 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2368 self
->m_lines
.Clear();
2371 if ( m_lines
.IsEmpty() )
2373 wxListLineData
*line
= new wxListLineData(self
);
2374 self
->m_lines
.Add(line
);
2376 // don't waste extra memory -- there never going to be anything
2377 // else/more in this array
2378 self
->m_lines
.Shrink();
2384 // ----------------------------------------------------------------------------
2385 // line geometry (report mode only)
2386 // ----------------------------------------------------------------------------
2388 wxCoord
wxListMainWindow::GetLineHeight() const
2390 // we cache the line height as calling GetTextExtent() is slow
2391 if ( !m_lineHeight
)
2393 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2395 wxClientDC
dc( self
);
2396 dc
.SetFont( GetFont() );
2399 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2401 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2404 m_small_image_list
->GetSize(0, iw
, ih
);
2409 self
->m_lineHeight
= y
+ LINE_SPACING
;
2412 return m_lineHeight
;
2415 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2417 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2419 return LINE_SPACING
+ line
* GetLineHeight();
2422 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2424 if ( !InReportView() )
2425 return GetLine(line
)->m_gi
->m_rectAll
;
2428 rect
.x
= HEADER_OFFSET_X
;
2429 rect
.y
= GetLineY(line
);
2430 rect
.width
= GetHeaderWidth();
2431 rect
.height
= GetLineHeight();
2436 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2438 if ( !InReportView() )
2439 return GetLine(line
)->m_gi
->m_rectLabel
;
2442 wxListLineData
*data
= GetLine(line
);
2443 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2446 wxListItemData
*item
= node
->GetData();
2447 if ( item
->HasImage() )
2450 GetImageSize( item
->GetImage(), ix
, iy
);
2451 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2456 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2457 rect
.y
= GetLineY(line
);
2458 rect
.width
= GetColumnWidth(0) - image_x
;
2459 rect
.height
= GetLineHeight();
2464 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2466 if ( !InReportView() )
2467 return GetLine(line
)->m_gi
->m_rectIcon
;
2469 wxListLineData
*ld
= GetLine(line
);
2470 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2473 rect
.x
= HEADER_OFFSET_X
;
2474 rect
.y
= GetLineY(line
);
2475 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2480 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2482 return InReportView() ? GetLineRect(line
)
2483 : GetLine(line
)->m_gi
->m_rectHighlight
;
2486 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2488 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2490 wxListLineData
*ld
= GetLine(line
);
2492 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2493 return wxLIST_HITTEST_ONITEMICON
;
2495 // VS: Testing for "ld->HasText() || InReportView()" instead of
2496 // "ld->HasText()" is needed to make empty lines in report view
2498 if ( ld
->HasText() || InReportView() )
2500 wxRect rect
= InReportView() ? GetLineRect(line
)
2501 : GetLineLabelRect(line
);
2503 if ( rect
.Contains(x
, y
) )
2504 return wxLIST_HITTEST_ONITEMLABEL
;
2510 // ----------------------------------------------------------------------------
2511 // highlight (selection) handling
2512 // ----------------------------------------------------------------------------
2514 bool wxListMainWindow::IsHighlighted(size_t line
) const
2518 return m_selStore
.IsSelected(line
);
2522 wxListLineData
*ld
= GetLine(line
);
2523 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2525 return ld
->IsHighlighted();
2529 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2535 wxArrayInt linesChanged
;
2536 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2539 // meny items changed state, refresh everything
2540 RefreshLines(lineFrom
, lineTo
);
2542 else // only a few items changed state, refresh only them
2544 size_t count
= linesChanged
.GetCount();
2545 for ( size_t n
= 0; n
< count
; n
++ )
2547 RefreshLine(linesChanged
[n
]);
2551 else // iterate over all items in non report view
2553 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2555 if ( HighlightLine(line
, highlight
) )
2561 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2567 changed
= m_selStore
.SelectItem(line
, highlight
);
2571 wxListLineData
*ld
= GetLine(line
);
2572 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2574 changed
= ld
->Highlight(highlight
);
2579 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2580 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2586 void wxListMainWindow::RefreshLine( size_t line
)
2588 if ( InReportView() )
2590 size_t visibleFrom
, visibleTo
;
2591 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2593 if ( line
< visibleFrom
|| line
> visibleTo
)
2597 wxRect rect
= GetLineRect(line
);
2599 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2600 RefreshRect( rect
);
2603 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2605 // we suppose that they are ordered by caller
2606 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2608 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2610 if ( InReportView() )
2612 size_t visibleFrom
, visibleTo
;
2613 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2615 if ( lineFrom
< visibleFrom
)
2616 lineFrom
= visibleFrom
;
2617 if ( lineTo
> visibleTo
)
2622 rect
.y
= GetLineY(lineFrom
);
2623 rect
.width
= GetClientSize().x
;
2624 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2626 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2627 RefreshRect( rect
);
2631 // TODO: this should be optimized...
2632 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2639 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2641 if ( InReportView() )
2643 size_t visibleFrom
, visibleTo
;
2644 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2646 if ( lineFrom
< visibleFrom
)
2647 lineFrom
= visibleFrom
;
2648 else if ( lineFrom
> visibleTo
)
2653 rect
.y
= GetLineY(lineFrom
);
2654 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2656 wxSize size
= GetClientSize();
2657 rect
.width
= size
.x
;
2659 // refresh till the bottom of the window
2660 rect
.height
= size
.y
- rect
.y
;
2662 RefreshRect( rect
);
2666 // TODO: how to do it more efficiently?
2671 void wxListMainWindow::RefreshSelected()
2677 if ( InReportView() )
2679 GetVisibleLinesRange(&from
, &to
);
2684 to
= GetItemCount() - 1;
2687 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2688 RefreshLine(m_current
);
2690 for ( size_t line
= from
; line
<= to
; line
++ )
2692 // NB: the test works as expected even if m_current == -1
2693 if ( line
!= m_current
&& IsHighlighted(line
) )
2698 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2700 // Note: a wxPaintDC must be constructed even if no drawing is
2701 // done (a Windows requirement).
2702 wxPaintDC
dc( this );
2706 // nothing to draw or not the moment to draw it
2712 // delay the repainting until we calculate all the items positions
2719 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2721 dc
.SetFont( GetFont() );
2723 if ( InReportView() )
2725 int lineHeight
= GetLineHeight();
2727 size_t visibleFrom
, visibleTo
;
2728 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2731 int xOrig
= dc
.LogicalToDeviceX( 0 );
2732 int yOrig
= dc
.LogicalToDeviceY( 0 );
2734 // tell the caller cache to cache the data
2737 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2738 GetParent()->GetId());
2739 evCache
.SetEventObject( GetParent() );
2740 evCache
.m_oldItemIndex
= visibleFrom
;
2741 evCache
.m_itemIndex
= visibleTo
;
2742 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2745 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2747 rectLine
= GetLineRect(line
);
2750 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2751 rectLine
.width
, rectLine
.height
) )
2753 // don't redraw unaffected lines to avoid flicker
2757 GetLine(line
)->DrawInReportMode( &dc
,
2759 GetLineHighlightRect(line
),
2760 IsHighlighted(line
) );
2763 if ( HasFlag(wxLC_HRULES
) )
2765 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2766 wxSize clientSize
= GetClientSize();
2768 size_t i
= visibleFrom
;
2769 if (i
== 0) i
= 1; // Don't draw the first one
2770 for ( ; i
<= visibleTo
; i
++ )
2773 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2774 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2775 clientSize
.x
- dev_x
, i
* lineHeight
);
2778 // Draw last horizontal rule
2779 if ( visibleTo
== GetItemCount() - 1 )
2782 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2783 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2784 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2788 // Draw vertical rules if required
2789 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2791 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2792 wxRect firstItemRect
, lastItemRect
;
2794 GetItemRect(visibleFrom
, firstItemRect
);
2795 GetItemRect(visibleTo
, lastItemRect
);
2796 int x
= firstItemRect
.GetX();
2798 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2800 for (int col
= 0; col
< GetColumnCount(); col
++)
2802 int colWidth
= GetColumnWidth(col
);
2804 int x_pos
= x
- dev_x
;
2805 if (col
< GetColumnCount()-1) x_pos
-= 2;
2806 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2807 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2813 size_t count
= GetItemCount();
2814 for ( size_t i
= 0; i
< count
; i
++ )
2816 GetLine(i
)->Draw( &dc
);
2821 // Don't draw rect outline under Mac at all.
2826 wxRect
rect( GetLineHighlightRect( m_current
) );
2828 dc
.SetPen( *wxBLACK_PEN
);
2829 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2830 dc
.DrawRectangle( rect
);
2832 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2840 void wxListMainWindow::HighlightAll( bool on
)
2842 if ( IsSingleSel() )
2844 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2846 // we just have one item to turn off
2847 if ( HasCurrent() && IsHighlighted(m_current
) )
2849 HighlightLine(m_current
, false);
2850 RefreshLine(m_current
);
2853 else // multi selection
2856 HighlightLines(0, GetItemCount() - 1, on
);
2860 void wxListMainWindow::SendNotify( size_t line
,
2861 wxEventType command
,
2862 const wxPoint
& point
)
2864 wxListEvent
le( command
, GetParent()->GetId() );
2865 le
.SetEventObject( GetParent() );
2867 le
.m_itemIndex
= line
;
2869 // set only for events which have position
2870 if ( point
!= wxDefaultPosition
)
2871 le
.m_pointDrag
= point
;
2873 // don't try to get the line info for virtual list controls: the main
2874 // program has it anyhow and if we did it would result in accessing all
2875 // the lines, even those which are not visible now and this is precisely
2876 // what we're trying to avoid
2879 if ( line
!= (size_t)-1 )
2881 GetLine(line
)->GetItem( 0, le
.m_item
);
2883 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2885 //else: there may be no more such item
2887 GetParent()->GetEventHandler()->ProcessEvent( le
);
2890 void wxListMainWindow::ChangeCurrent(size_t current
)
2892 m_current
= current
;
2894 // as the current item changed, we shouldn't start editing it when the
2895 // "slow click" timer expires as the click happened on another item
2896 if ( m_renameTimer
->IsRunning() )
2897 m_renameTimer
->Stop();
2899 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2902 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2904 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2905 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2907 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2908 wxT("EditLabel() needs a text control") );
2910 size_t itemEdit
= (size_t)item
;
2912 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2913 le
.SetEventObject( GetParent() );
2914 le
.m_itemIndex
= item
;
2915 wxListLineData
*data
= GetLine(itemEdit
);
2916 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2917 data
->GetItem( 0, le
.m_item
);
2919 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2921 // vetoed by user code
2925 // We have to call this here because the label in question might just have
2926 // been added and no screen update taken place.
2931 // Pending events dispatched by wxSafeYield might have changed the item
2933 if ( (size_t)item
>= GetItemCount() )
2937 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2938 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2939 return m_textctrlWrapper
->GetText();
2942 void wxListMainWindow::OnRenameTimer()
2944 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2946 EditLabel( m_current
);
2949 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2951 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2952 le
.SetEventObject( GetParent() );
2953 le
.m_itemIndex
= itemEdit
;
2955 wxListLineData
*data
= GetLine(itemEdit
);
2957 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2959 data
->GetItem( 0, le
.m_item
);
2960 le
.m_item
.m_text
= value
;
2961 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2965 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2967 // let owner know that the edit was cancelled
2968 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2970 le
.SetEditCanceled(true);
2972 le
.SetEventObject( GetParent() );
2973 le
.m_itemIndex
= itemEdit
;
2975 wxListLineData
*data
= GetLine(itemEdit
);
2976 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2978 data
->GetItem( 0, le
.m_item
);
2979 GetEventHandler()->ProcessEvent( le
);
2982 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2986 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2987 // shutdown the edit control when the mouse is clicked elsewhere on the
2988 // listctrl because the order of events is different (or something like
2989 // that), so explicitly end the edit if it is active.
2990 if ( event
.LeftDown() && m_textctrlWrapper
)
2991 m_textctrlWrapper
->EndEdit( false );
2994 if ( event
.LeftDown() )
2997 event
.SetEventObject( GetParent() );
2998 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3001 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3003 // let the base handle mouse wheel events.
3008 if ( !HasCurrent() || IsEmpty() )
3010 if (event
.RightDown())
3012 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3014 wxContextMenuEvent
evtCtx(
3016 GetParent()->GetId(),
3017 ClientToScreen(event
.GetPosition()));
3018 evtCtx
.SetEventObject(GetParent());
3019 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3027 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3028 event
.ButtonDClick()) )
3031 int x
= event
.GetX();
3032 int y
= event
.GetY();
3033 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3035 // where did we hit it (if we did)?
3038 size_t count
= GetItemCount(),
3041 if ( InReportView() )
3043 current
= y
/ GetLineHeight();
3044 if ( current
< count
)
3045 hitResult
= HitTestLine(current
, x
, y
);
3049 // TODO: optimize it too! this is less simple than for report view but
3050 // enumerating all items is still not a way to do it!!
3051 for ( current
= 0; current
< count
; current
++ )
3053 hitResult
= HitTestLine(current
, x
, y
);
3059 if (event
.Dragging())
3061 if (m_dragCount
== 0)
3063 // we have to report the raw, physical coords as we want to be
3064 // able to call HitTest(event.m_pointDrag) from the user code to
3065 // get the item being dragged
3066 m_dragStart
= event
.GetPosition();
3071 if (m_dragCount
!= 3)
3074 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3075 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3077 wxListEvent
le( command
, GetParent()->GetId() );
3078 le
.SetEventObject( GetParent() );
3079 le
.m_itemIndex
= m_lineLastClicked
;
3080 le
.m_pointDrag
= m_dragStart
;
3081 GetParent()->GetEventHandler()->ProcessEvent( le
);
3092 // outside of any item
3093 if (event
.RightDown())
3095 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3097 wxContextMenuEvent
evtCtx(
3099 GetParent()->GetId(),
3100 ClientToScreen(event
.GetPosition()));
3101 evtCtx
.SetEventObject(GetParent());
3102 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3106 // reset the selection and bail out
3107 HighlightAll(false);
3113 bool forceClick
= false;
3114 if (event
.ButtonDClick())
3116 if ( m_renameTimer
->IsRunning() )
3117 m_renameTimer
->Stop();
3119 m_lastOnSame
= false;
3121 if ( current
== m_lineLastClicked
)
3123 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3129 // The first click was on another item, so don't interpret this as
3130 // a double click, but as a simple click instead
3137 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3139 // select single line
3140 HighlightAll( false );
3141 ReverseHighlight(m_lineSelectSingleOnUp
);
3146 if ((current
== m_current
) &&
3147 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3148 HasFlag(wxLC_EDIT_LABELS
) )
3150 if ( !InReportView() ||
3151 GetLineLabelRect(current
).Contains(x
, y
) )
3153 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3154 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3159 m_lastOnSame
= false;
3160 m_lineSelectSingleOnUp
= (size_t)-1;
3164 // This is necessary, because after a DnD operation in
3165 // from and to ourself, the up event is swallowed by the
3166 // DnD code. So on next non-up event (which means here and
3167 // now) m_lineSelectSingleOnUp should be reset.
3168 m_lineSelectSingleOnUp
= (size_t)-1;
3170 if (event
.RightDown())
3172 m_lineBeforeLastClicked
= m_lineLastClicked
;
3173 m_lineLastClicked
= current
;
3175 // If the item is already selected, do not update the selection.
3176 // Multi-selections should not be cleared if a selected item is clicked.
3177 if (!IsHighlighted(current
))
3179 HighlightAll(false);
3180 ChangeCurrent(current
);
3181 ReverseHighlight(m_current
);
3184 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3186 // Allow generation of context menu event
3189 else if (event
.MiddleDown())
3191 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3193 else if ( event
.LeftDown() || forceClick
)
3195 m_lineBeforeLastClicked
= m_lineLastClicked
;
3196 m_lineLastClicked
= current
;
3198 size_t oldCurrent
= m_current
;
3199 bool oldWasSelected
= IsHighlighted(m_current
);
3201 bool cmdModifierDown
= event
.CmdDown();
3202 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3204 if ( IsSingleSel() || !IsHighlighted(current
) )
3206 HighlightAll( false );
3208 ChangeCurrent(current
);
3210 ReverseHighlight(m_current
);
3212 else // multi sel & current is highlighted & no mod keys
3214 m_lineSelectSingleOnUp
= current
;
3215 ChangeCurrent(current
); // change focus
3218 else // multi sel & either ctrl or shift is down
3220 if (cmdModifierDown
)
3222 ChangeCurrent(current
);
3224 ReverseHighlight(m_current
);
3226 else if (event
.ShiftDown())
3228 ChangeCurrent(current
);
3230 size_t lineFrom
= oldCurrent
,
3233 if ( lineTo
< lineFrom
)
3236 lineFrom
= m_current
;
3239 HighlightLines(lineFrom
, lineTo
);
3241 else // !ctrl, !shift
3243 // test in the enclosing if should make it impossible
3244 wxFAIL_MSG( _T("how did we get here?") );
3248 if (m_current
!= oldCurrent
)
3249 RefreshLine( oldCurrent
);
3251 // forceClick is only set if the previous click was on another item
3252 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3256 void wxListMainWindow::MoveToItem(size_t item
)
3258 if ( item
== (size_t)-1 )
3261 wxRect rect
= GetLineRect(item
);
3263 int client_w
, client_h
;
3264 GetClientSize( &client_w
, &client_h
);
3266 const int hLine
= GetLineHeight();
3268 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3269 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3271 if ( InReportView() )
3273 // the next we need the range of lines shown it might be different,
3274 // so recalculate it
3275 ResetVisibleLinesRange();
3277 if (rect
.y
< view_y
)
3278 Scroll( -1, rect
.y
/ hLine
);
3279 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3280 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3283 // At least on Mac the visible lines value will get reset inside of
3284 // Scroll *before* it actually scrolls the window because of the
3285 // Update() that happens there, so it will still have the wrong value.
3286 // So let's reset it again and wait for it to be recalculated in the
3287 // next paint event. I would expect this problem to show up in wxGTK
3288 // too but couldn't duplicate it there. Perhaps the order of events
3289 // is different... --Robin
3290 ResetVisibleLinesRange();
3298 if (rect
.x
-view_x
< 5)
3299 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3300 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3301 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3303 if (rect
.y
-view_y
< 5)
3304 sy
= (rect
.y
- 5) / hLine
;
3305 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3306 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3312 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3314 if ( !InReportView() )
3316 // TODO: this should work in all views but is not implemented now
3321 GetVisibleLinesRange(&top
, &bottom
);
3323 if ( bottom
== (size_t)-1 )
3326 ResetVisibleLinesRange();
3328 int hLine
= GetLineHeight();
3330 Scroll(-1, top
+ dy
/ hLine
);
3333 // see comment in MoveToItem() for why we do this
3334 ResetVisibleLinesRange();
3340 // ----------------------------------------------------------------------------
3341 // keyboard handling
3342 // ----------------------------------------------------------------------------
3344 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3346 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3347 _T("invalid item index in OnArrowChar()") );
3349 size_t oldCurrent
= m_current
;
3351 // in single selection we just ignore Shift as we can't select several
3353 if ( event
.ShiftDown() && !IsSingleSel() )
3355 ChangeCurrent(newCurrent
);
3357 // refresh the old focus to remove it
3358 RefreshLine( oldCurrent
);
3360 // select all the items between the old and the new one
3361 if ( oldCurrent
> newCurrent
)
3363 newCurrent
= oldCurrent
;
3364 oldCurrent
= m_current
;
3367 HighlightLines(oldCurrent
, newCurrent
);
3371 // all previously selected items are unselected unless ctrl is held
3372 // in a multiselection control
3373 if ( !event
.ControlDown() || IsSingleSel() )
3374 HighlightAll(false);
3376 ChangeCurrent(newCurrent
);
3378 // refresh the old focus to remove it
3379 RefreshLine( oldCurrent
);
3381 // in single selection mode we must always have a selected item
3382 if ( !event
.ControlDown() || IsSingleSel() )
3383 HighlightLine( m_current
, true );
3386 RefreshLine( m_current
);
3391 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3393 wxWindow
*parent
= GetParent();
3395 // propagate the key event upwards
3396 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3397 ke
.m_shiftDown
= event
.m_shiftDown
;
3398 ke
.m_controlDown
= event
.m_controlDown
;
3399 ke
.m_altDown
= event
.m_altDown
;
3400 ke
.m_metaDown
= event
.m_metaDown
;
3401 ke
.m_keyCode
= event
.m_keyCode
;
3404 ke
.SetEventObject( parent
);
3405 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3410 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3412 wxWindow
*parent
= GetParent();
3414 // propagate the key event upwards
3415 wxKeyEvent
ke( wxEVT_KEY_UP
);
3416 ke
.m_shiftDown
= event
.m_shiftDown
;
3417 ke
.m_controlDown
= event
.m_controlDown
;
3418 ke
.m_altDown
= event
.m_altDown
;
3419 ke
.m_metaDown
= event
.m_metaDown
;
3420 ke
.m_keyCode
= event
.m_keyCode
;
3423 ke
.SetEventObject( parent
);
3424 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3429 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3431 wxWindow
*parent
= GetParent();
3433 // send a list_key event up
3436 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3437 le
.m_itemIndex
= m_current
;
3438 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3439 le
.m_code
= event
.GetKeyCode();
3440 le
.SetEventObject( parent
);
3441 parent
->GetEventHandler()->ProcessEvent( le
);
3444 // propagate the char event upwards
3445 wxKeyEvent
ke( wxEVT_CHAR
);
3446 ke
.m_shiftDown
= event
.m_shiftDown
;
3447 ke
.m_controlDown
= event
.m_controlDown
;
3448 ke
.m_altDown
= event
.m_altDown
;
3449 ke
.m_metaDown
= event
.m_metaDown
;
3450 ke
.m_keyCode
= event
.m_keyCode
;
3453 ke
.SetEventObject( parent
);
3454 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3456 if ( HandleAsNavigationKey(event
) )
3459 // no item -> nothing to do
3466 // don't use m_linesPerPage directly as it might not be computed yet
3467 const int pageSize
= GetCountPerPage();
3468 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3470 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3472 if (event
.GetKeyCode() == WXK_RIGHT
)
3473 event
.m_keyCode
= WXK_LEFT
;
3474 else if (event
.GetKeyCode() == WXK_LEFT
)
3475 event
.m_keyCode
= WXK_RIGHT
;
3478 switch ( event
.GetKeyCode() )
3481 if ( m_current
> 0 )
3482 OnArrowChar( m_current
- 1, event
);
3486 if ( m_current
< (size_t)GetItemCount() - 1 )
3487 OnArrowChar( m_current
+ 1, event
);
3492 OnArrowChar( GetItemCount() - 1, event
);
3497 OnArrowChar( 0, event
);
3502 int steps
= InReportView() ? pageSize
- 1
3503 : m_current
% pageSize
;
3505 int index
= m_current
- steps
;
3509 OnArrowChar( index
, event
);
3515 int steps
= InReportView()
3517 : pageSize
- (m_current
% pageSize
) - 1;
3519 size_t index
= m_current
+ steps
;
3520 size_t count
= GetItemCount();
3521 if ( index
>= count
)
3524 OnArrowChar( index
, event
);
3529 if ( !InReportView() )
3531 int index
= m_current
- pageSize
;
3535 OnArrowChar( index
, event
);
3540 if ( !InReportView() )
3542 size_t index
= m_current
+ pageSize
;
3544 size_t count
= GetItemCount();
3545 if ( index
>= count
)
3548 OnArrowChar( index
, event
);
3553 if ( IsSingleSel() )
3555 if ( event
.ControlDown() )
3557 ReverseHighlight(m_current
);
3559 else // normal space press
3561 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3564 else // multiple selection
3566 ReverseHighlight(m_current
);
3572 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3580 // ----------------------------------------------------------------------------
3582 // ----------------------------------------------------------------------------
3584 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3588 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3589 event
.SetEventObject( GetParent() );
3590 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3594 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3595 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3596 // which are already drawn correctly resulting in horrible flicker - avoid
3606 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3610 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3611 event
.SetEventObject( GetParent() );
3612 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3620 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3622 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3624 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3626 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3628 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3630 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3632 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3634 else if ( InReportView() && (m_small_image_list
))
3636 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3640 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3642 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3644 m_normal_image_list
->GetSize( index
, width
, height
);
3646 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3648 m_small_image_list
->GetSize( index
, width
, height
);
3650 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3652 m_small_image_list
->GetSize( index
, width
, height
);
3654 else if ( InReportView() && m_small_image_list
)
3656 m_small_image_list
->GetSize( index
, width
, height
);
3665 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3667 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3668 dc
.SetFont( GetFont() );
3671 dc
.GetTextExtent( s
, &lw
, NULL
);
3673 return lw
+ AUTOSIZE_COL_MARGIN
;
3676 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3680 // calc the spacing from the icon size
3681 int width
= 0, height
= 0;
3683 if ((imageList
) && (imageList
->GetImageCount()) )
3684 imageList
->GetSize(0, width
, height
);
3686 if (which
== wxIMAGE_LIST_NORMAL
)
3688 m_normal_image_list
= imageList
;
3689 m_normal_spacing
= width
+ 8;
3692 if (which
== wxIMAGE_LIST_SMALL
)
3694 m_small_image_list
= imageList
;
3695 m_small_spacing
= width
+ 14;
3696 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3700 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3704 m_small_spacing
= spacing
;
3706 m_normal_spacing
= spacing
;
3709 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3711 return isSmall
? m_small_spacing
: m_normal_spacing
;
3714 // ----------------------------------------------------------------------------
3716 // ----------------------------------------------------------------------------
3718 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3720 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3722 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3724 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3725 item
.m_width
= GetTextLength( item
.m_text
);
3727 wxListHeaderData
*column
= node
->GetData();
3728 column
->SetItem( item
);
3730 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3732 headerWin
->m_dirty
= true;
3736 // invalidate it as it has to be recalculated
3740 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3742 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3743 _T("invalid column index") );
3745 wxCHECK_RET( InReportView(),
3746 _T("SetColumnWidth() can only be called in report mode.") );
3749 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3751 headerWin
->m_dirty
= true;
3753 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3754 wxCHECK_RET( node
, _T("no column?") );
3756 wxListHeaderData
*column
= node
->GetData();
3758 size_t count
= GetItemCount();
3760 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3762 width
= GetTextLength(column
->GetText());
3763 width
+= 2*EXTRA_WIDTH
;
3765 // check for column header's image availability
3766 const int image
= column
->GetImage();
3769 if ( m_small_image_list
)
3772 m_small_image_list
->GetSize(image
, ix
, iy
);
3773 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3777 else if ( width
== wxLIST_AUTOSIZE
)
3781 // TODO: determine the max width somehow...
3782 width
= WIDTH_COL_DEFAULT
;
3786 wxClientDC
dc(this);
3787 dc
.SetFont( GetFont() );
3789 int max
= AUTOSIZE_COL_MARGIN
;
3791 // if the cached column width isn't valid then recalculate it
3792 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3794 for (size_t i
= 0; i
< count
; i
++)
3796 wxListLineData
*line
= GetLine( i
);
3797 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3799 wxCHECK_RET( n
, _T("no subitem?") );
3801 wxListItemData
*itemData
= n
->GetData();
3804 itemData
->GetItem(item
);
3805 int itemWidth
= GetItemWidthWithImage(&item
);
3806 if (itemWidth
> max
)
3810 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3811 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3814 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3815 width
= max
+ AUTOSIZE_COL_MARGIN
;
3819 column
->SetWidth( width
);
3821 // invalidate it as it has to be recalculated
3825 int wxListMainWindow::GetHeaderWidth() const
3827 if ( !m_headerWidth
)
3829 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3831 size_t count
= GetColumnCount();
3832 for ( size_t col
= 0; col
< count
; col
++ )
3834 self
->m_headerWidth
+= GetColumnWidth(col
);
3838 return m_headerWidth
;
3841 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3843 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3844 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3846 wxListHeaderData
*column
= node
->GetData();
3847 column
->GetItem( item
);
3850 int wxListMainWindow::GetColumnWidth( int col
) const
3852 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3853 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3855 wxListHeaderData
*column
= node
->GetData();
3856 return column
->GetWidth();
3859 // ----------------------------------------------------------------------------
3861 // ----------------------------------------------------------------------------
3863 void wxListMainWindow::SetItem( wxListItem
&item
)
3865 long id
= item
.m_itemId
;
3866 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3867 _T("invalid item index in SetItem") );
3871 wxListLineData
*line
= GetLine((size_t)id
);
3872 line
->SetItem( item
.m_col
, item
);
3874 // Set item state if user wants
3875 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3876 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3880 // update the Max Width Cache if needed
3881 int width
= GetItemWidthWithImage(&item
);
3883 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3884 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3888 // update the item on screen
3890 GetItemRect(id
, rectItem
);
3891 RefreshRect(rectItem
);
3894 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3899 // first deal with selection
3900 if ( stateMask
& wxLIST_STATE_SELECTED
)
3902 // set/clear select state
3905 // optimized version for virtual listctrl.
3906 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3909 else if ( state
& wxLIST_STATE_SELECTED
)
3911 const long count
= GetItemCount();
3912 for( long i
= 0; i
< count
; i
++ )
3914 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3920 // clear for non virtual (somewhat optimized by using GetNextItem())
3922 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3924 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3929 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3931 // unfocus all: only one item can be focussed, so clearing focus for
3932 // all items is simply clearing focus of the focussed item.
3933 SetItemState(m_current
, state
, stateMask
);
3935 //(setting focus to all items makes no sense, so it is not handled here.)
3938 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3942 SetItemStateAll(state
, stateMask
);
3946 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3947 _T("invalid list ctrl item index in SetItem") );
3949 size_t oldCurrent
= m_current
;
3950 size_t item
= (size_t)litem
; // safe because of the check above
3952 // do we need to change the focus?
3953 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3955 if ( state
& wxLIST_STATE_FOCUSED
)
3957 // don't do anything if this item is already focused
3958 if ( item
!= m_current
)
3960 ChangeCurrent(item
);
3962 if ( oldCurrent
!= (size_t)-1 )
3964 if ( IsSingleSel() )
3966 HighlightLine(oldCurrent
, false);
3969 RefreshLine(oldCurrent
);
3972 RefreshLine( m_current
);
3977 // don't do anything if this item is not focused
3978 if ( item
== m_current
)
3982 if ( IsSingleSel() )
3984 // we must unselect the old current item as well or we
3985 // might end up with more than one selected item in a
3986 // single selection control
3987 HighlightLine(oldCurrent
, false);
3990 RefreshLine( oldCurrent
);
3995 // do we need to change the selection state?
3996 if ( stateMask
& wxLIST_STATE_SELECTED
)
3998 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4000 if ( IsSingleSel() )
4004 // selecting the item also makes it the focused one in the
4006 if ( m_current
!= item
)
4008 ChangeCurrent(item
);
4010 if ( oldCurrent
!= (size_t)-1 )
4012 HighlightLine( oldCurrent
, false );
4013 RefreshLine( oldCurrent
);
4019 // only the current item may be selected anyhow
4020 if ( item
!= m_current
)
4025 if ( HighlightLine(item
, on
) )
4032 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4034 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4035 _T("invalid list ctrl item index in GetItemState()") );
4037 int ret
= wxLIST_STATE_DONTCARE
;
4039 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4041 if ( (size_t)item
== m_current
)
4042 ret
|= wxLIST_STATE_FOCUSED
;
4045 if ( stateMask
& wxLIST_STATE_SELECTED
)
4047 if ( IsHighlighted(item
) )
4048 ret
|= wxLIST_STATE_SELECTED
;
4054 void wxListMainWindow::GetItem( wxListItem
&item
) const
4056 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4057 _T("invalid item index in GetItem") );
4059 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4060 line
->GetItem( item
.m_col
, item
);
4062 // Get item state if user wants it
4063 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4064 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4065 wxLIST_STATE_FOCUSED
);
4068 // ----------------------------------------------------------------------------
4070 // ----------------------------------------------------------------------------
4072 size_t wxListMainWindow::GetItemCount() const
4074 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4077 void wxListMainWindow::SetItemCount(long count
)
4079 m_selStore
.SetItemCount(count
);
4080 m_countVirt
= count
;
4082 ResetVisibleLinesRange();
4084 // scrollbars must be reset
4088 int wxListMainWindow::GetSelectedItemCount() const
4090 // deal with the quick case first
4091 if ( IsSingleSel() )
4092 return HasCurrent() ? IsHighlighted(m_current
) : false;
4094 // virtual controls remmebers all its selections itself
4096 return m_selStore
.GetSelectedCount();
4098 // TODO: we probably should maintain the number of items selected even for
4099 // non virtual controls as enumerating all lines is really slow...
4100 size_t countSel
= 0;
4101 size_t count
= GetItemCount();
4102 for ( size_t line
= 0; line
< count
; line
++ )
4104 if ( GetLine(line
)->IsHighlighted() )
4111 // ----------------------------------------------------------------------------
4112 // item position/size
4113 // ----------------------------------------------------------------------------
4115 wxRect
wxListMainWindow::GetViewRect() const
4117 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
4119 // we need to find the longest/tallest label
4120 wxCoord xMax
= 0, yMax
= 0;
4121 const int count
= GetItemCount();
4124 for ( int i
= 0; i
< count
; i
++ )
4129 wxCoord x
= r
.GetRight(),
4139 // some fudge needed to make it look prettier
4140 xMax
+= 2 * EXTRA_BORDER_X
;
4141 yMax
+= 2 * EXTRA_BORDER_Y
;
4143 // account for the scrollbars if necessary
4144 const wxSize sizeAll
= GetClientSize();
4145 if ( xMax
> sizeAll
.x
)
4146 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4147 if ( yMax
> sizeAll
.y
)
4148 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4150 return wxRect(0, 0, xMax
, yMax
);
4154 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
4156 wxCHECK_MSG( InReportView(), false,
4157 _T("GetSubItemRect only meaningful in report view") );
4158 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
4159 _T("invalid item in GetSubItemRect") );
4161 // ensure that we're laid out, otherwise we could return nonsense
4164 wxConstCast(this, wxListMainWindow
)->
4165 RecalculatePositions(true /* no refresh */);
4168 rect
= GetLineRect((size_t)item
);
4170 // Adjust rect to specified column
4171 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
4173 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
4174 _T("invalid subItem in GetSubItemRect") );
4176 for (int i
= 0; i
< subItem
; i
++)
4178 rect
.x
+= GetColumnWidth(i
);
4180 rect
.width
= GetColumnWidth(subItem
);
4183 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4188 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4191 GetItemRect(item
, rect
);
4199 // ----------------------------------------------------------------------------
4200 // geometry calculation
4201 // ----------------------------------------------------------------------------
4203 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4205 const int lineHeight
= GetLineHeight();
4207 wxClientDC
dc( this );
4208 dc
.SetFont( GetFont() );
4210 const size_t count
= GetItemCount();
4213 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
4214 iconSpacing
= m_normal_spacing
;
4215 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
4216 iconSpacing
= m_small_spacing
;
4220 // Note that we do not call GetClientSize() here but
4221 // GetSize() and subtract the border size for sunken
4222 // borders manually. This is technically incorrect,
4223 // but we need to know the client area's size WITHOUT
4224 // scrollbars here. Since we don't know if there are
4225 // any scrollbars, we use GetSize() instead. Another
4226 // solution would be to call SetScrollbars() here to
4227 // remove the scrollbars and call GetClientSize() then,
4228 // but this might result in flicker and - worse - will
4229 // reset the scrollbars to 0 which is not good at all
4230 // if you resize a dialog/window, but don't want to
4231 // reset the window scrolling. RR.
4232 // Furthermore, we actually do NOT subtract the border
4233 // width as 2 pixels is just the extra space which we
4234 // need around the actual content in the window. Other-
4235 // wise the text would e.g. touch the upper border. RR.
4238 GetSize( &clientWidth
, &clientHeight
);
4240 if ( InReportView() )
4242 // all lines have the same height and we scroll one line per step
4243 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4245 m_linesPerPage
= clientHeight
/ lineHeight
;
4247 ResetVisibleLinesRange();
4249 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4250 GetHeaderWidth() / SCROLL_UNIT_X
,
4251 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4252 GetScrollPos(wxHORIZONTAL
),
4253 GetScrollPos(wxVERTICAL
),
4258 // we have 3 different layout strategies: either layout all items
4259 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4260 // to arrange them in top to bottom, left to right (don't ask me why
4261 // not the other way round...) order
4262 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4264 int x
= EXTRA_BORDER_X
;
4265 int y
= EXTRA_BORDER_Y
;
4267 wxCoord widthMax
= 0;
4270 for ( i
= 0; i
< count
; i
++ )
4272 wxListLineData
*line
= GetLine(i
);
4273 line
->CalculateSize( &dc
, iconSpacing
);
4274 line
->SetPosition( x
, y
, iconSpacing
);
4276 wxSize sizeLine
= GetLineSize(i
);
4278 if ( HasFlag(wxLC_ALIGN_TOP
) )
4280 if ( sizeLine
.x
> widthMax
)
4281 widthMax
= sizeLine
.x
;
4285 else // wxLC_ALIGN_LEFT
4287 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4291 if ( HasFlag(wxLC_ALIGN_TOP
) )
4293 // traverse the items again and tweak their sizes so that they are
4294 // all the same in a row
4295 for ( i
= 0; i
< count
; i
++ )
4297 wxListLineData
*line
= GetLine(i
);
4298 line
->m_gi
->ExtendWidth(widthMax
);
4306 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4307 (y
+ lineHeight
) / lineHeight
,
4308 GetScrollPos( wxHORIZONTAL
),
4309 GetScrollPos( wxVERTICAL
),
4313 else // "flowed" arrangement, the most complicated case
4315 // at first we try without any scrollbars, if the items don't fit into
4316 // the window, we recalculate after subtracting the space taken by the
4319 int entireWidth
= 0;
4321 for (int tries
= 0; tries
< 2; tries
++)
4323 entireWidth
= 2 * EXTRA_BORDER_X
;
4327 // Now we have decided that the items do not fit into the
4328 // client area, so we need a scrollbar
4329 entireWidth
+= SCROLL_UNIT_X
;
4332 int x
= EXTRA_BORDER_X
;
4333 int y
= EXTRA_BORDER_Y
;
4334 int maxWidthInThisRow
= 0;
4337 int currentlyVisibleLines
= 0;
4339 for (size_t i
= 0; i
< count
; i
++)
4341 currentlyVisibleLines
++;
4342 wxListLineData
*line
= GetLine( i
);
4343 line
->CalculateSize( &dc
, iconSpacing
);
4344 line
->SetPosition( x
, y
, iconSpacing
);
4346 wxSize sizeLine
= GetLineSize( i
);
4348 if ( maxWidthInThisRow
< sizeLine
.x
)
4349 maxWidthInThisRow
= sizeLine
.x
;
4352 if (currentlyVisibleLines
> m_linesPerPage
)
4353 m_linesPerPage
= currentlyVisibleLines
;
4355 if ( y
+ sizeLine
.y
>= clientHeight
)
4357 currentlyVisibleLines
= 0;
4359 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4360 x
+= maxWidthInThisRow
;
4361 entireWidth
+= maxWidthInThisRow
;
4362 maxWidthInThisRow
= 0;
4365 // We have reached the last item.
4366 if ( i
== count
- 1 )
4367 entireWidth
+= maxWidthInThisRow
;
4369 if ( (tries
== 0) &&
4370 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4372 clientHeight
-= wxSystemSettings::
4373 GetMetric(wxSYS_HSCROLL_Y
);
4378 if ( i
== count
- 1 )
4379 tries
= 1; // Everything fits, no second try required.
4387 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4389 GetScrollPos( wxHORIZONTAL
),
4398 // FIXME: why should we call it from here?
4405 void wxListMainWindow::RefreshAll()
4410 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4411 if ( headerWin
&& headerWin
->m_dirty
)
4413 headerWin
->m_dirty
= false;
4414 headerWin
->Refresh();
4418 void wxListMainWindow::UpdateCurrent()
4420 if ( !HasCurrent() && !IsEmpty() )
4424 long wxListMainWindow::GetNextItem( long item
,
4425 int WXUNUSED(geometry
),
4429 max
= GetItemCount();
4430 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4431 _T("invalid listctrl index in GetNextItem()") );
4433 // notice that we start with the next item (or the first one if item == -1)
4434 // and this is intentional to allow writing a simple loop to iterate over
4435 // all selected items
4438 // this is not an error because the index was OK initially,
4439 // just no such item
4446 size_t count
= GetItemCount();
4447 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4449 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4452 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4459 // ----------------------------------------------------------------------------
4461 // ----------------------------------------------------------------------------
4463 void wxListMainWindow::DeleteItem( long lindex
)
4465 size_t count
= GetItemCount();
4467 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4468 _T("invalid item index in DeleteItem") );
4470 size_t index
= (size_t)lindex
;
4472 // we don't need to adjust the index for the previous items
4473 if ( HasCurrent() && m_current
>= index
)
4475 // if the current item is being deleted, we want the next one to
4476 // become selected - unless there is no next one - so don't adjust
4477 // m_current in this case
4478 if ( m_current
!= index
|| m_current
== count
- 1 )
4482 if ( InReportView() )
4484 // mark the Column Max Width cache as dirty if the items in the line
4485 // we're deleting contain the Max Column Width
4486 wxListLineData
* const line
= GetLine(index
);
4487 wxListItemDataList::compatibility_iterator n
;
4488 wxListItemData
*itemData
;
4492 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4494 n
= line
->m_items
.Item( i
);
4495 itemData
= n
->GetData();
4496 itemData
->GetItem(item
);
4498 itemWidth
= GetItemWidthWithImage(&item
);
4500 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4501 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4504 ResetVisibleLinesRange();
4507 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4512 m_selStore
.OnItemDelete(index
);
4516 m_lines
.RemoveAt( index
);
4519 // we need to refresh the (vert) scrollbar as the number of items changed
4522 RefreshAfter(index
);
4525 void wxListMainWindow::DeleteColumn( int col
)
4527 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4529 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4532 delete node
->GetData();
4533 m_columns
.Erase( node
);
4537 // update all the items
4538 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4540 wxListLineData
* const line
= GetLine(i
);
4541 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4542 delete n
->GetData();
4543 line
->m_items
.Erase(n
);
4547 if ( InReportView() ) // we only cache max widths when in Report View
4549 delete m_aColWidths
.Item(col
);
4550 m_aColWidths
.RemoveAt(col
);
4553 // invalidate it as it has to be recalculated
4557 void wxListMainWindow::DoDeleteAllItems()
4560 // nothing to do - in particular, don't send the event
4565 // to make the deletion of all items faster, we don't send the
4566 // notifications for each item deletion in this case but only one event
4567 // for all of them: this is compatible with wxMSW and documented in
4568 // DeleteAllItems() description
4570 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4571 event
.SetEventObject( GetParent() );
4572 GetParent()->GetEventHandler()->ProcessEvent( event
);
4580 if ( InReportView() )
4582 ResetVisibleLinesRange();
4583 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4585 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4592 void wxListMainWindow::DeleteAllItems()
4596 RecalculatePositions();
4599 void wxListMainWindow::DeleteEverything()
4601 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4602 WX_CLEAR_ARRAY(m_aColWidths
);
4607 // ----------------------------------------------------------------------------
4608 // scanning for an item
4609 // ----------------------------------------------------------------------------
4611 void wxListMainWindow::EnsureVisible( long index
)
4613 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4614 _T("invalid index in EnsureVisible") );
4616 // We have to call this here because the label in question might just have
4617 // been added and its position is not known yet
4619 RecalculatePositions(true /* no refresh */);
4621 MoveToItem((size_t)index
);
4624 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4630 wxString str_upper
= str
.Upper();
4634 size_t count
= GetItemCount();
4635 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4637 wxListLineData
*line
= GetLine(i
);
4638 wxString line_upper
= line
->GetText(0).Upper();
4641 if (line_upper
== str_upper
)
4646 if (line_upper
.find(str_upper
) == 0)
4654 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4660 size_t count
= GetItemCount();
4661 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4663 wxListLineData
*line
= GetLine(i
);
4665 line
->GetItem( 0, item
);
4666 if (item
.m_data
== data
)
4673 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4676 GetVisibleLinesRange( &topItem
, NULL
);
4679 GetItemPosition( GetItemCount() - 1, p
);
4683 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4684 if ( id
>= 0 && id
< (long)GetItemCount() )
4690 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4692 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4694 size_t count
= GetItemCount();
4696 if ( InReportView() )
4698 size_t current
= y
/ GetLineHeight();
4699 if ( current
< count
)
4701 flags
= HitTestLine(current
, x
, y
);
4708 // TODO: optimize it too! this is less simple than for report view but
4709 // enumerating all items is still not a way to do it!!
4710 for ( size_t current
= 0; current
< count
; current
++ )
4712 flags
= HitTestLine(current
, x
, y
);
4721 // ----------------------------------------------------------------------------
4723 // ----------------------------------------------------------------------------
4725 void wxListMainWindow::InsertItem( wxListItem
&item
)
4727 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4729 int count
= GetItemCount();
4730 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4732 if (item
.m_itemId
> count
)
4733 item
.m_itemId
= count
;
4735 size_t id
= item
.m_itemId
;
4739 if ( InReportView() )
4741 ResetVisibleLinesRange();
4743 // calculate the width of the item and adjust the max column width
4744 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4745 int width
= GetItemWidthWithImage(&item
);
4746 item
.SetWidth(width
);
4747 if (width
> pWidthInfo
->nMaxWidth
)
4748 pWidthInfo
->nMaxWidth
= width
;
4751 wxListLineData
*line
= new wxListLineData(this);
4753 line
->SetItem( item
.m_col
, item
);
4755 m_lines
.Insert( line
, id
);
4759 // If an item is selected at or below the point of insertion, we need to
4760 // increment the member variables because the current row's index has gone
4762 if ( HasCurrent() && m_current
>= id
)
4765 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4767 RefreshLines(id
, GetItemCount() - 1);
4770 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4773 if ( InReportView() )
4775 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4776 item
.m_width
= GetTextLength( item
.m_text
);
4778 wxListHeaderData
*column
= new wxListHeaderData( item
);
4779 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4781 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4784 wxListHeaderDataList::compatibility_iterator
4785 node
= m_columns
.Item( col
);
4786 m_columns
.Insert( node
, column
);
4787 m_aColWidths
.Insert( colWidthInfo
, col
);
4791 m_columns
.Append( column
);
4792 m_aColWidths
.Add( colWidthInfo
);
4797 // update all the items
4798 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4800 wxListLineData
* const line
= GetLine(i
);
4801 wxListItemData
* const data
= new wxListItemData(this);
4803 line
->m_items
.Insert(col
, data
);
4805 line
->m_items
.Append(data
);
4809 // invalidate it as it has to be recalculated
4814 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4817 wxClientDC
dc(this);
4819 dc
.SetFont( GetFont() );
4821 if (item
->GetImage() != -1)
4824 GetImageSize( item
->GetImage(), ix
, iy
);
4828 if (!item
->GetText().empty())
4831 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4838 // ----------------------------------------------------------------------------
4840 // ----------------------------------------------------------------------------
4842 wxListCtrlCompare list_ctrl_compare_func_2
;
4843 long list_ctrl_compare_data
;
4845 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4847 wxListLineData
*line1
= *arg1
;
4848 wxListLineData
*line2
= *arg2
;
4850 line1
->GetItem( 0, item
);
4851 wxUIntPtr data1
= item
.m_data
;
4852 line2
->GetItem( 0, item
);
4853 wxUIntPtr data2
= item
.m_data
;
4854 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4857 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4859 // selections won't make sense any more after sorting the items so reset
4861 HighlightAll(false);
4864 list_ctrl_compare_func_2
= fn
;
4865 list_ctrl_compare_data
= data
;
4866 m_lines
.Sort( list_ctrl_compare_func_1
);
4870 // ----------------------------------------------------------------------------
4872 // ----------------------------------------------------------------------------
4874 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4877 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4878 wxScrolledCanvas::OnScroll(event
);
4880 HandleOnScroll( event
);
4883 // update our idea of which lines are shown when we redraw the window the
4885 ResetVisibleLinesRange();
4887 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4889 wxGenericListCtrl
* lc
= GetListCtrl();
4890 wxCHECK_RET( lc
, _T("no listctrl window?") );
4892 lc
->m_headerWin
->Refresh();
4893 lc
->m_headerWin
->Update();
4897 int wxListMainWindow::GetCountPerPage() const
4899 if ( !m_linesPerPage
)
4901 wxConstCast(this, wxListMainWindow
)->
4902 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4905 return m_linesPerPage
;
4908 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4910 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4912 if ( m_lineFrom
== (size_t)-1 )
4914 size_t count
= GetItemCount();
4917 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4919 // this may happen if SetScrollbars() hadn't been called yet
4920 if ( m_lineFrom
>= count
)
4921 m_lineFrom
= count
- 1;
4923 // we redraw one extra line but this is needed to make the redrawing
4924 // logic work when there is a fractional number of lines on screen
4925 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4926 if ( m_lineTo
>= count
)
4927 m_lineTo
= count
- 1;
4929 else // empty control
4932 m_lineTo
= (size_t)-1;
4936 wxASSERT_MSG( IsEmpty() ||
4937 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4938 _T("GetVisibleLinesRange() returns incorrect result") );
4946 // -------------------------------------------------------------------------------------
4947 // wxGenericListCtrl
4948 // -------------------------------------------------------------------------------------
4950 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4952 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4953 EVT_SIZE(wxGenericListCtrl::OnSize
)
4956 wxGenericListCtrl::wxGenericListCtrl()
4958 m_imageListNormal
= (wxImageList
*) NULL
;
4959 m_imageListSmall
= (wxImageList
*) NULL
;
4960 m_imageListState
= (wxImageList
*) NULL
;
4962 m_ownsImageListNormal
=
4963 m_ownsImageListSmall
=
4964 m_ownsImageListState
= false;
4966 m_mainWin
= (wxListMainWindow
*) NULL
;
4967 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4971 wxGenericListCtrl::~wxGenericListCtrl()
4973 if (m_ownsImageListNormal
)
4974 delete m_imageListNormal
;
4975 if (m_ownsImageListSmall
)
4976 delete m_imageListSmall
;
4977 if (m_ownsImageListState
)
4978 delete m_imageListState
;
4981 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4987 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4989 // we use 'g' to get the descent, too
4991 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4992 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4995 // only update if changed
4996 if ( h
!= m_headerHeight
)
5001 ResizeReportView(true);
5002 else //why is this needed if it doesn't have a header?
5003 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
5008 void wxGenericListCtrl::CreateHeaderWindow()
5010 m_headerWin
= new wxListHeaderWindow
5012 this, wxID_ANY
, m_mainWin
,
5014 wxSize(GetClientSize().x
, m_headerHeight
),
5017 CalculateAndSetHeaderHeight();
5020 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5025 const wxValidator
&validator
,
5026 const wxString
&name
)
5030 m_imageListState
= (wxImageList
*) NULL
;
5031 m_ownsImageListNormal
=
5032 m_ownsImageListSmall
=
5033 m_ownsImageListState
= false;
5035 m_mainWin
= (wxListMainWindow
*) NULL
;
5036 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5040 if ( !(style
& wxLC_MASK_TYPE
) )
5042 style
= style
| wxLC_LIST
;
5045 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5048 // this window itself shouldn't get the focus, only m_mainWin should
5051 // don't create the inner window with the border
5052 style
&= ~wxBORDER_MASK
;
5054 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5057 // Human Interface Guidelines ask us for a special font in this case
5058 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5061 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5066 if ( InReportView() )
5068 CreateHeaderWindow();
5074 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5075 m_headerWin
->SetFont( font
);
5076 CalculateAndSetHeaderHeight();
5080 if ( HasFlag(wxLC_NO_HEADER
) )
5081 // VZ: why do we create it at all then?
5082 m_headerWin
->Show( false );
5085 SetInitialSize(size
);
5090 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5092 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5093 _T("wxLC_VIRTUAL can't be [un]set") );
5095 long flag
= GetWindowStyle();
5099 if (style
& wxLC_MASK_TYPE
)
5100 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5101 if (style
& wxLC_MASK_ALIGN
)
5102 flag
&= ~wxLC_MASK_ALIGN
;
5103 if (style
& wxLC_MASK_SORT
)
5104 flag
&= ~wxLC_MASK_SORT
;
5112 // some styles can be set without recreating everything (as happens in
5113 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5114 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
5117 wxWindow::SetWindowStyleFlag(flag
);
5121 SetWindowStyleFlag( flag
);
5125 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5129 m_mainWin
->DeleteEverything();
5131 // has the header visibility changed?
5132 bool hasHeader
= HasHeader();
5133 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5135 if ( hasHeader
!= willHaveHeader
)
5142 // don't delete, just hide, as we can reuse it later
5143 m_headerWin
->Show(false);
5145 //else: nothing to do
5147 else // must show header
5151 CreateHeaderWindow();
5153 else // already have it, just show
5155 m_headerWin
->Show( true );
5159 ResizeReportView(willHaveHeader
);
5163 wxWindow::SetWindowStyleFlag( flag
);
5166 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5168 m_mainWin
->GetColumn( col
, item
);
5172 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5174 m_mainWin
->SetColumn( col
, item
);
5178 int wxGenericListCtrl::GetColumnWidth( int col
) const
5180 return m_mainWin
->GetColumnWidth( col
);
5183 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5185 m_mainWin
->SetColumnWidth( col
, width
);
5189 int wxGenericListCtrl::GetCountPerPage() const
5191 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5194 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5196 m_mainWin
->GetItem( info
);
5200 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5202 m_mainWin
->SetItem( info
);
5206 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5209 info
.m_text
= label
;
5210 info
.m_mask
= wxLIST_MASK_TEXT
;
5211 info
.m_itemId
= index
;
5215 info
.m_image
= imageId
;
5216 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5219 m_mainWin
->SetItem(info
);
5223 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5225 return m_mainWin
->GetItemState( item
, stateMask
);
5228 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5230 m_mainWin
->SetItemState( item
, state
, stateMask
);
5235 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5237 return SetItemColumnImage(item
, 0, image
);
5241 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5244 info
.m_image
= image
;
5245 info
.m_mask
= wxLIST_MASK_IMAGE
;
5246 info
.m_itemId
= item
;
5247 info
.m_col
= column
;
5248 m_mainWin
->SetItem( info
);
5252 wxString
wxGenericListCtrl::GetItemText( long item
) const
5254 return m_mainWin
->GetItemText(item
);
5257 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5259 m_mainWin
->SetItemText(item
, str
);
5262 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5265 info
.m_mask
= wxLIST_MASK_DATA
;
5266 info
.m_itemId
= item
;
5267 m_mainWin
->GetItem( info
);
5271 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5274 info
.m_mask
= wxLIST_MASK_DATA
;
5275 info
.m_itemId
= item
;
5277 m_mainWin
->SetItem( info
);
5281 wxRect
wxGenericListCtrl::GetViewRect() const
5283 return m_mainWin
->GetViewRect();
5286 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
5288 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
5291 bool wxGenericListCtrl::GetSubItemRect(long item
,
5294 int WXUNUSED(code
)) const
5296 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
5299 if ( m_mainWin
->HasHeader() )
5300 rect
.y
+= m_headerHeight
+ 1;
5305 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5307 m_mainWin
->GetItemPosition( item
, pos
);
5311 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5316 int wxGenericListCtrl::GetItemCount() const
5318 return m_mainWin
->GetItemCount();
5321 int wxGenericListCtrl::GetColumnCount() const
5323 return m_mainWin
->GetColumnCount();
5326 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5328 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5331 wxSize
wxGenericListCtrl::GetItemSpacing() const
5333 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5335 return wxSize(spacing
, spacing
);
5338 #if WXWIN_COMPATIBILITY_2_6
5339 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5341 return m_mainWin
->GetItemSpacing( isSmall
);
5343 #endif // WXWIN_COMPATIBILITY_2_6
5345 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5348 info
.m_itemId
= item
;
5349 info
.SetTextColour( col
);
5350 m_mainWin
->SetItem( info
);
5353 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5356 info
.m_itemId
= item
;
5357 m_mainWin
->GetItem( info
);
5358 return info
.GetTextColour();
5361 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5364 info
.m_itemId
= item
;
5365 info
.SetBackgroundColour( col
);
5366 m_mainWin
->SetItem( info
);
5369 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5372 info
.m_itemId
= item
;
5373 m_mainWin
->GetItem( info
);
5374 return info
.GetBackgroundColour();
5377 int wxGenericListCtrl::GetScrollPos( int orient
) const
5379 return m_mainWin
->GetScrollPos( orient
);
5382 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5384 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5387 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5390 info
.m_itemId
= item
;
5392 m_mainWin
->SetItem( info
);
5395 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5398 info
.m_itemId
= item
;
5399 m_mainWin
->GetItem( info
);
5400 return info
.GetFont();
5403 int wxGenericListCtrl::GetSelectedItemCount() const
5405 return m_mainWin
->GetSelectedItemCount();
5408 wxColour
wxGenericListCtrl::GetTextColour() const
5410 return GetForegroundColour();
5413 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5415 SetForegroundColour(col
);
5418 long wxGenericListCtrl::GetTopItem() const
5421 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5425 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5427 return m_mainWin
->GetNextItem( item
, geom
, state
);
5430 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5432 if (which
== wxIMAGE_LIST_NORMAL
)
5433 return m_imageListNormal
;
5434 else if (which
== wxIMAGE_LIST_SMALL
)
5435 return m_imageListSmall
;
5436 else if (which
== wxIMAGE_LIST_STATE
)
5437 return m_imageListState
;
5439 return (wxImageList
*) NULL
;
5442 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5444 if ( which
== wxIMAGE_LIST_NORMAL
)
5446 if (m_ownsImageListNormal
)
5447 delete m_imageListNormal
;
5448 m_imageListNormal
= imageList
;
5449 m_ownsImageListNormal
= false;
5451 else if ( which
== wxIMAGE_LIST_SMALL
)
5453 if (m_ownsImageListSmall
)
5454 delete m_imageListSmall
;
5455 m_imageListSmall
= imageList
;
5456 m_ownsImageListSmall
= false;
5458 else if ( which
== wxIMAGE_LIST_STATE
)
5460 if (m_ownsImageListState
)
5461 delete m_imageListState
;
5462 m_imageListState
= imageList
;
5463 m_ownsImageListState
= false;
5466 m_mainWin
->SetImageList( imageList
, which
);
5469 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5471 SetImageList(imageList
, which
);
5472 if ( which
== wxIMAGE_LIST_NORMAL
)
5473 m_ownsImageListNormal
= true;
5474 else if ( which
== wxIMAGE_LIST_SMALL
)
5475 m_ownsImageListSmall
= true;
5476 else if ( which
== wxIMAGE_LIST_STATE
)
5477 m_ownsImageListState
= true;
5480 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5485 bool wxGenericListCtrl::DeleteItem( long item
)
5487 m_mainWin
->DeleteItem( item
);
5491 bool wxGenericListCtrl::DeleteAllItems()
5493 m_mainWin
->DeleteAllItems();
5497 bool wxGenericListCtrl::DeleteAllColumns()
5499 size_t count
= m_mainWin
->m_columns
.GetCount();
5500 for ( size_t n
= 0; n
< count
; n
++ )
5505 void wxGenericListCtrl::ClearAll()
5507 m_mainWin
->DeleteEverything();
5510 bool wxGenericListCtrl::DeleteColumn( int col
)
5512 m_mainWin
->DeleteColumn( col
);
5514 // if we don't have the header any longer, we need to relayout the window
5515 if ( !GetColumnCount() )
5516 ResizeReportView(false /* no header */);
5520 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5521 wxClassInfo
* textControlClass
)
5523 return m_mainWin
->EditLabel( item
, textControlClass
);
5526 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5528 return m_mainWin
->GetEditControl();
5531 bool wxGenericListCtrl::EnsureVisible( long item
)
5533 m_mainWin
->EnsureVisible( item
);
5537 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5539 return m_mainWin
->FindItem( start
, str
, partial
);
5542 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5544 return m_mainWin
->FindItem( start
, data
);
5547 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5548 int WXUNUSED(direction
))
5550 return m_mainWin
->FindItem( pt
);
5553 // TODO: sub item hit testing
5554 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5556 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5559 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5561 m_mainWin
->InsertItem( info
);
5562 return info
.m_itemId
;
5565 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5568 info
.m_text
= label
;
5569 info
.m_mask
= wxLIST_MASK_TEXT
;
5570 info
.m_itemId
= index
;
5571 return InsertItem( info
);
5574 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5577 info
.m_mask
= wxLIST_MASK_IMAGE
;
5578 info
.m_image
= imageIndex
;
5579 info
.m_itemId
= index
;
5580 return InsertItem( info
);
5583 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5586 info
.m_text
= label
;
5587 info
.m_image
= imageIndex
;
5588 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5589 info
.m_itemId
= index
;
5590 return InsertItem( info
);
5593 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5595 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5597 m_mainWin
->InsertColumn( col
, item
);
5599 // if we hadn't had a header before but have one now
5600 // then we need to relayout the window
5601 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5602 ResizeReportView(true /* have header */);
5604 m_headerWin
->Refresh();
5609 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5610 int format
, int width
)
5613 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5614 item
.m_text
= heading
;
5617 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5618 item
.m_width
= width
;
5621 item
.m_format
= format
;
5623 return InsertColumn( col
, item
);
5626 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5628 return m_mainWin
->ScrollList(dx
, dy
);
5632 // fn is a function which takes 3 long arguments: item1, item2, data.
5633 // item1 is the long data associated with a first item (NOT the index).
5634 // item2 is the long data associated with a second item (NOT the index).
5635 // data is the same value as passed to SortItems.
5636 // The return value is a negative number if the first item should precede the second
5637 // item, a positive number of the second item should precede the first,
5638 // or zero if the two items are equivalent.
5639 // data is arbitrary data to be passed to the sort function.
5641 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5643 m_mainWin
->SortItems( fn
, data
);
5647 // ----------------------------------------------------------------------------
5649 // ----------------------------------------------------------------------------
5651 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5656 ResizeReportView(m_mainWin
->HasHeader());
5657 m_mainWin
->RecalculatePositions();
5660 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5663 GetClientSize( &cw
, &ch
);
5667 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5668 if(ch
> m_headerHeight
)
5669 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5670 cw
, ch
- m_headerHeight
- 1 );
5672 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5675 else // no header window
5677 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5681 void wxGenericListCtrl::OnInternalIdle()
5683 wxWindow::OnInternalIdle();
5685 // do it only if needed
5686 if ( !m_mainWin
->m_dirty
)
5689 m_mainWin
->RecalculatePositions();
5692 // ----------------------------------------------------------------------------
5694 // ----------------------------------------------------------------------------
5696 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5700 m_mainWin
->SetBackgroundColour( colour
);
5701 m_mainWin
->m_dirty
= true;
5707 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5709 if ( !wxWindow::SetForegroundColour( colour
) )
5714 m_mainWin
->SetForegroundColour( colour
);
5715 m_mainWin
->m_dirty
= true;
5719 m_headerWin
->SetForegroundColour( colour
);
5724 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5726 if ( !wxWindow::SetFont( font
) )
5731 m_mainWin
->SetFont( font
);
5732 m_mainWin
->m_dirty
= true;
5737 m_headerWin
->SetFont( font
);
5738 CalculateAndSetHeaderHeight();
5748 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5751 // Use the same color scheme as wxListBox
5752 return wxListBox::GetClassDefaultAttributes(variant
);
5754 wxUnusedVar(variant
);
5755 wxVisualAttributes attr
;
5756 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5757 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5758 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5763 // ----------------------------------------------------------------------------
5764 // methods forwarded to m_mainWin
5765 // ----------------------------------------------------------------------------
5767 #if wxUSE_DRAG_AND_DROP
5769 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5771 m_mainWin
->SetDropTarget( dropTarget
);
5774 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5776 return m_mainWin
->GetDropTarget();
5781 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5783 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5786 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5788 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5791 wxColour
wxGenericListCtrl::GetForegroundColour() const
5793 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5796 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5799 return m_mainWin
->PopupMenu( menu
, x
, y
);
5805 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5807 m_mainWin
->DoClientToScreen(x
, y
);
5810 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5812 m_mainWin
->DoScreenToClient(x
, y
);
5815 void wxGenericListCtrl::SetFocus()
5817 // The test in window.cpp fails as we are a composite
5818 // window, so it checks against "this", but not m_mainWin.
5819 if ( DoFindFocus() != this )
5820 m_mainWin
->SetFocus();
5823 wxSize
wxGenericListCtrl::DoGetBestSize() const
5825 // Something is better than nothing...
5826 // 100x80 is what the MSW version will get from the default
5827 // wxControl::DoGetBestSize
5828 return wxSize(100, 80);
5831 // ----------------------------------------------------------------------------
5832 // virtual list control support
5833 // ----------------------------------------------------------------------------
5835 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5837 // this is a pure virtual function, in fact - which is not really pure
5838 // because the controls which are not virtual don't need to implement it
5839 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5841 return wxEmptyString
;
5844 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5846 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5848 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5852 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5855 return OnGetItemImage(item
);
5861 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5863 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5864 _T("invalid item index in OnGetItemAttr()") );
5866 // no attributes by default
5870 void wxGenericListCtrl::SetItemCount(long count
)
5872 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5874 m_mainWin
->SetItemCount(count
);
5877 void wxGenericListCtrl::RefreshItem(long item
)
5879 m_mainWin
->RefreshLine(item
);
5882 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5884 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5887 // Generic wxListCtrl is more or less a container for two other
5888 // windows which drawings are done upon. These are namely
5889 // 'm_headerWin' and 'm_mainWin'.
5890 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5891 // behaviour wxListCtrl has under wxMSW.
5893 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5897 // The easy case, no rectangle specified.
5899 m_headerWin
->Refresh(eraseBackground
);
5902 m_mainWin
->Refresh(eraseBackground
);
5906 // Refresh the header window
5909 wxRect rectHeader
= m_headerWin
->GetRect();
5910 rectHeader
.Intersect(*rect
);
5911 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5914 m_headerWin
->GetPosition(&x
, &y
);
5915 rectHeader
.Offset(-x
, -y
);
5916 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5920 // Refresh the main window
5923 wxRect rectMain
= m_mainWin
->GetRect();
5924 rectMain
.Intersect(*rect
);
5925 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5928 m_mainWin
->GetPosition(&x
, &y
);
5929 rectMain
.Offset(-x
, -y
);
5930 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5936 #endif // wxUSE_LISTCTRL