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(__WXMAC__)) || defined(__WXUNIVERSAL__))
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 OnChildFocus(wxChildFocusEvent
& event
);
620 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
621 void GetImageSize( int index
, int &width
, int &height
) const;
622 int GetTextLength( const wxString
&s
) const;
624 void SetImageList( wxImageList
*imageList
, int which
);
625 void SetItemSpacing( int spacing
, bool isSmall
= false );
626 int GetItemSpacing( bool isSmall
= false );
628 void SetColumn( int col
, wxListItem
&item
);
629 void SetColumnWidth( int col
, int width
);
630 void GetColumn( int col
, wxListItem
&item
) const;
631 int GetColumnWidth( int col
) const;
632 int GetColumnCount() const { return m_columns
.GetCount(); }
634 // returns the sum of the heights of all columns
635 int GetHeaderWidth() const;
637 int GetCountPerPage() const;
639 void SetItem( wxListItem
&item
);
640 void GetItem( wxListItem
&item
) const;
641 void SetItemState( long item
, long state
, long stateMask
);
642 void SetItemStateAll( long state
, long stateMask
);
643 int GetItemState( long item
, long stateMask
) const;
644 bool GetItemRect( long item
, wxRect
&rect
) const
646 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
648 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
649 wxRect
GetViewRect() const;
650 bool GetItemPosition( long item
, wxPoint
& pos
) const;
651 int GetSelectedItemCount() const;
653 wxString
GetItemText(long item
) const
656 info
.m_mask
= wxLIST_MASK_TEXT
;
657 info
.m_itemId
= item
;
662 void SetItemText(long item
, const wxString
& value
)
665 info
.m_mask
= wxLIST_MASK_TEXT
;
666 info
.m_itemId
= item
;
671 // set the scrollbars and update the positions of the items
672 void RecalculatePositions(bool noRefresh
= false);
674 // refresh the window and the header
677 long GetNextItem( long item
, int geometry
, int state
) const;
678 void DeleteItem( long index
);
679 void DeleteAllItems();
680 void DeleteColumn( int col
);
681 void DeleteEverything();
682 void EnsureVisible( long index
);
683 long FindItem( long start
, const wxString
& str
, bool partial
= false );
684 long FindItem( long start
, wxUIntPtr data
);
685 long FindItem( const wxPoint
& pt
);
686 long HitTest( int x
, int y
, int &flags
) const;
687 void InsertItem( wxListItem
&item
);
688 void InsertColumn( long col
, wxListItem
&item
);
689 int GetItemWidthWithImage(wxListItem
* item
);
690 void SortItems( wxListCtrlCompare fn
, long data
);
692 size_t GetItemCount() const;
693 bool IsEmpty() const { return GetItemCount() == 0; }
694 void SetItemCount(long count
);
696 // change the current (== focused) item, send a notification event
697 void ChangeCurrent(size_t current
);
698 void ResetCurrent() { ChangeCurrent((size_t)-1); }
699 bool HasCurrent() const { return m_current
!= (size_t)-1; }
701 // send out a wxListEvent
702 void SendNotify( size_t line
,
704 const wxPoint
& point
= wxDefaultPosition
);
706 // override base class virtual to reset m_lineHeight when the font changes
707 virtual bool SetFont(const wxFont
& font
)
709 if ( !wxScrolledCanvas::SetFont(font
) )
717 // these are for wxListLineData usage only
719 // get the backpointer to the list ctrl
720 wxGenericListCtrl
*GetListCtrl() const
722 return wxStaticCast(GetParent(), wxGenericListCtrl
);
725 // get the height of all lines (assuming they all do have the same height)
726 wxCoord
GetLineHeight() const;
728 // get the y position of the given line (only for report view)
729 wxCoord
GetLineY(size_t line
) const;
731 // get the brush to use for the item highlighting
732 wxBrush
*GetHighlightBrush() const
734 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
737 bool HasFocus() const
743 // the array of all line objects for a non virtual list control (for the
744 // virtual list control we only ever use m_lines[0])
745 wxListLineDataArray m_lines
;
747 // the list of column objects
748 wxListHeaderDataList m_columns
;
750 // currently focused item or -1
753 // the number of lines per page
756 // this flag is set when something which should result in the window
757 // redrawing happens (i.e. an item was added or deleted, or its appearance
758 // changed) and OnPaint() doesn't redraw the window while it is set which
759 // allows to minimize the number of repaintings when a lot of items are
760 // being added. The real repainting occurs only after the next OnIdle()
764 wxColour
*m_highlightColour
;
765 wxImageList
*m_small_image_list
;
766 wxImageList
*m_normal_image_list
;
768 int m_normal_spacing
;
772 wxTimer
*m_renameTimer
;
776 ColWidthArray m_aColWidths
;
778 // for double click logic
779 size_t m_lineLastClicked
,
780 m_lineBeforeLastClicked
,
781 m_lineSelectSingleOnUp
;
784 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
786 // the total count of items in a virtual list control
789 // the object maintaining the items selection state, only used in virtual
791 wxSelectionStore m_selStore
;
793 // common part of all ctors
796 // get the line data for the given index
797 wxListLineData
*GetLine(size_t n
) const
799 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
803 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
810 // get a dummy line which can be used for geometry calculations and such:
811 // you must use GetLine() if you want to really draw the line
812 wxListLineData
*GetDummyLine() const;
814 // cache the line data of the n-th line in m_lines[0]
815 void CacheLineData(size_t line
);
817 // get the range of visible lines
818 void GetVisibleLinesRange(size_t *from
, size_t *to
);
820 // force us to recalculate the range of visible lines
821 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
823 // get the colour to be used for drawing the rules
824 wxColour
GetRuleColour() const
826 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
830 // initialize the current item if needed
831 void UpdateCurrent();
833 // delete all items but don't refresh: called from dtor
834 void DoDeleteAllItems();
836 // the height of one line using the current font
837 wxCoord m_lineHeight
;
839 // the total header width or 0 if not calculated yet
840 wxCoord m_headerWidth
;
842 // the first and last lines being shown on screen right now (inclusive),
843 // both may be -1 if they must be calculated so never access them directly:
844 // use GetVisibleLinesRange() above instead
848 // the brushes to use for item highlighting when we do/don't have focus
849 wxBrush
*m_highlightBrush
,
850 *m_highlightUnfocusedBrush
;
852 // wrapper around the text control currently used for in place editing or
853 // NULL if no item is being edited
854 wxListTextCtrlWrapper
*m_textctrlWrapper
;
857 DECLARE_EVENT_TABLE()
859 friend class wxGenericListCtrl
;
863 wxListItemData::~wxListItemData()
865 // in the virtual list control the attributes are managed by the main
866 // program, so don't delete them
867 if ( !m_owner
->IsVirtual() )
873 void wxListItemData::Init()
881 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
887 if ( owner
->InReportView() )
893 void wxListItemData::SetItem( const wxListItem
&info
)
895 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
896 SetText(info
.m_text
);
897 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
898 m_image
= info
.m_image
;
899 if ( info
.m_mask
& wxLIST_MASK_DATA
)
900 m_data
= info
.m_data
;
902 if ( info
.HasAttributes() )
905 m_attr
->AssignFrom(*info
.GetAttributes());
907 m_attr
= new wxListItemAttr(*info
.GetAttributes());
915 m_rect
->width
= info
.m_width
;
919 void wxListItemData::SetPosition( int x
, int y
)
921 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
927 void wxListItemData::SetSize( int width
, int height
)
929 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
932 m_rect
->width
= width
;
934 m_rect
->height
= height
;
937 bool wxListItemData::IsHit( int x
, int y
) const
939 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
941 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
944 int wxListItemData::GetX() const
946 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
951 int wxListItemData::GetY() const
953 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
958 int wxListItemData::GetWidth() const
960 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
962 return m_rect
->width
;
965 int wxListItemData::GetHeight() const
967 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
969 return m_rect
->height
;
972 void wxListItemData::GetItem( wxListItem
&info
) const
974 long mask
= info
.m_mask
;
976 // by default, get everything for backwards compatibility
979 if ( mask
& wxLIST_MASK_TEXT
)
980 info
.m_text
= m_text
;
981 if ( mask
& wxLIST_MASK_IMAGE
)
982 info
.m_image
= m_image
;
983 if ( mask
& wxLIST_MASK_DATA
)
984 info
.m_data
= m_data
;
988 if ( m_attr
->HasTextColour() )
989 info
.SetTextColour(m_attr
->GetTextColour());
990 if ( m_attr
->HasBackgroundColour() )
991 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
992 if ( m_attr
->HasFont() )
993 info
.SetFont(m_attr
->GetFont());
997 //-----------------------------------------------------------------------------
999 //-----------------------------------------------------------------------------
1001 void wxListHeaderData::Init()
1013 wxListHeaderData::wxListHeaderData()
1018 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1025 void wxListHeaderData::SetItem( const wxListItem
&item
)
1027 m_mask
= item
.m_mask
;
1029 if ( m_mask
& wxLIST_MASK_TEXT
)
1030 m_text
= item
.m_text
;
1032 if ( m_mask
& wxLIST_MASK_IMAGE
)
1033 m_image
= item
.m_image
;
1035 if ( m_mask
& wxLIST_MASK_FORMAT
)
1036 m_format
= item
.m_format
;
1038 if ( m_mask
& wxLIST_MASK_WIDTH
)
1039 SetWidth(item
.m_width
);
1041 if ( m_mask
& wxLIST_MASK_STATE
)
1042 SetState(item
.m_state
);
1045 void wxListHeaderData::SetPosition( int x
, int y
)
1051 void wxListHeaderData::SetHeight( int h
)
1056 void wxListHeaderData::SetWidth( int w
)
1058 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1061 void wxListHeaderData::SetState( int flag
)
1066 void wxListHeaderData::SetFormat( int format
)
1071 bool wxListHeaderData::HasImage() const
1073 return m_image
!= -1;
1076 bool wxListHeaderData::IsHit( int x
, int y
) const
1078 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1081 void wxListHeaderData::GetItem( wxListItem
& item
)
1083 item
.m_mask
= m_mask
;
1084 item
.m_text
= m_text
;
1085 item
.m_image
= m_image
;
1086 item
.m_format
= m_format
;
1087 item
.m_width
= m_width
;
1088 item
.m_state
= m_state
;
1091 int wxListHeaderData::GetImage() const
1096 int wxListHeaderData::GetWidth() const
1101 int wxListHeaderData::GetFormat() const
1106 int wxListHeaderData::GetState() const
1111 //-----------------------------------------------------------------------------
1113 //-----------------------------------------------------------------------------
1115 inline int wxListLineData::GetMode() const
1117 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1120 inline bool wxListLineData::InReportView() const
1122 return m_owner
->HasFlag(wxLC_REPORT
);
1125 inline bool wxListLineData::IsVirtual() const
1127 return m_owner
->IsVirtual();
1130 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1134 if ( InReportView() )
1137 m_gi
= new GeometryInfo
;
1139 m_highlighted
= false;
1141 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1144 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1146 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1147 wxCHECK_RET( node
, _T("no subitems at all??") );
1149 wxListItemData
*item
= node
->GetData();
1154 switch ( GetMode() )
1157 case wxLC_SMALL_ICON
:
1158 m_gi
->m_rectAll
.width
= spacing
;
1160 s
= item
->GetText();
1165 m_gi
->m_rectLabel
.width
=
1166 m_gi
->m_rectLabel
.height
= 0;
1170 dc
->GetTextExtent( s
, &lw
, &lh
);
1174 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1176 m_gi
->m_rectAll
.width
= lw
;
1178 m_gi
->m_rectLabel
.width
= lw
;
1179 m_gi
->m_rectLabel
.height
= lh
;
1182 if (item
->HasImage())
1185 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1186 m_gi
->m_rectIcon
.width
= w
+ 8;
1187 m_gi
->m_rectIcon
.height
= h
+ 8;
1189 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1190 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1191 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1192 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1195 if ( item
->HasText() )
1197 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1198 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1200 else // no text, highlight the icon
1202 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1203 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1208 s
= item
->GetTextForMeasuring();
1210 dc
->GetTextExtent( s
, &lw
, &lh
);
1214 m_gi
->m_rectLabel
.width
= lw
;
1215 m_gi
->m_rectLabel
.height
= lh
;
1217 m_gi
->m_rectAll
.width
= lw
;
1218 m_gi
->m_rectAll
.height
= lh
;
1220 if (item
->HasImage())
1223 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1224 m_gi
->m_rectIcon
.width
= w
;
1225 m_gi
->m_rectIcon
.height
= h
;
1227 m_gi
->m_rectAll
.width
+= 4 + w
;
1228 if (h
> m_gi
->m_rectAll
.height
)
1229 m_gi
->m_rectAll
.height
= h
;
1232 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1233 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1237 wxFAIL_MSG( _T("unexpected call to SetSize") );
1241 wxFAIL_MSG( _T("unknown mode") );
1246 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1248 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1249 wxCHECK_RET( node
, _T("no subitems at all??") );
1251 wxListItemData
*item
= node
->GetData();
1253 switch ( GetMode() )
1256 case wxLC_SMALL_ICON
:
1257 m_gi
->m_rectAll
.x
= x
;
1258 m_gi
->m_rectAll
.y
= y
;
1260 if ( item
->HasImage() )
1262 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1263 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1264 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1267 if ( item
->HasText() )
1269 if (m_gi
->m_rectAll
.width
> spacing
)
1270 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1273 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1274 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1275 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1277 else // no text, highlight the icon
1279 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1280 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1285 m_gi
->m_rectAll
.x
= x
;
1286 m_gi
->m_rectAll
.y
= y
;
1288 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1289 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1290 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1292 if (item
->HasImage())
1294 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1295 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1296 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1300 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1305 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1309 wxFAIL_MSG( _T("unknown mode") );
1314 void wxListLineData::InitItems( int num
)
1316 for (int i
= 0; i
< num
; i
++)
1317 m_items
.Append( new wxListItemData(m_owner
) );
1320 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1322 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1323 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1325 wxListItemData
*item
= node
->GetData();
1326 item
->SetItem( info
);
1329 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1331 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1334 wxListItemData
*item
= node
->GetData();
1335 item
->GetItem( info
);
1339 wxString
wxListLineData::GetText(int index
) const
1343 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1346 wxListItemData
*item
= node
->GetData();
1347 s
= item
->GetText();
1353 void wxListLineData::SetText( int index
, const wxString
& s
)
1355 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1358 wxListItemData
*item
= node
->GetData();
1363 void wxListLineData::SetImage( int index
, int image
)
1365 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1366 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1368 wxListItemData
*item
= node
->GetData();
1369 item
->SetImage(image
);
1372 int wxListLineData::GetImage( int index
) const
1374 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1375 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1377 wxListItemData
*item
= node
->GetData();
1378 return item
->GetImage();
1381 wxListItemAttr
*wxListLineData::GetAttr() const
1383 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1384 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1386 wxListItemData
*item
= node
->GetData();
1387 return item
->GetAttr();
1390 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1392 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1393 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1395 wxListItemData
*item
= node
->GetData();
1396 item
->SetAttr(attr
);
1399 bool wxListLineData::SetAttributes(wxDC
*dc
,
1400 const wxListItemAttr
*attr
,
1403 wxWindow
*listctrl
= m_owner
->GetParent();
1407 // don't use foreground colour for drawing highlighted items - this might
1408 // make them completely invisible (and there is no way to do bit
1409 // arithmetics on wxColour, unfortunately)
1414 if (m_owner
->HasFocus()
1415 #if !defined(__WXUNIVERSAL__)
1416 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1424 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1426 else if ( attr
&& attr
->HasTextColour() )
1427 colText
= attr
->GetTextColour();
1429 colText
= listctrl
->GetForegroundColour();
1431 dc
->SetTextForeground(colText
);
1435 if ( attr
&& attr
->HasFont() )
1436 font
= attr
->GetFont();
1438 font
= listctrl
->GetFont();
1443 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1444 if ( highlighted
|| hasBgCol
)
1447 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1449 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
1451 dc
->SetPen( *wxTRANSPARENT_PEN
);
1459 void wxListLineData::Draw( wxDC
*dc
)
1461 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1462 wxCHECK_RET( node
, _T("no subitems at all??") );
1464 bool highlighted
= IsHighlighted();
1466 wxListItemAttr
*attr
= GetAttr();
1468 if ( SetAttributes(dc
, attr
, highlighted
) )
1469 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1471 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1477 int flags
= wxCONTROL_SELECTED
;
1478 if (m_owner
->HasFocus()
1479 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1480 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1483 flags
|= wxCONTROL_FOCUSED
;
1484 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1489 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1494 // just for debugging to better see where the items are
1496 dc
->SetPen(*wxRED_PEN
);
1497 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1498 dc
->DrawRectangle( m_gi
->m_rectAll
);
1499 dc
->SetPen(*wxGREEN_PEN
);
1500 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1503 wxListItemData
*item
= node
->GetData();
1504 if (item
->HasImage())
1506 // centre the image inside our rectangle, this looks nicer when items
1507 // ae aligned in a row
1508 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1510 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1513 if (item
->HasText())
1515 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1517 wxDCClipper
clipper(*dc
, rectLabel
);
1518 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1522 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1524 const wxRect
& rectHL
,
1527 // TODO: later we should support setting different attributes for
1528 // different columns - to do it, just add "col" argument to
1529 // GetAttr() and move these lines into the loop below
1530 wxListItemAttr
*attr
= GetAttr();
1531 if ( SetAttributes(dc
, attr
, highlighted
) )
1532 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1534 dc
->DrawRectangle( rectHL
);
1540 int flags
= wxCONTROL_SELECTED
;
1541 if (m_owner
->HasFocus())
1542 flags
|= wxCONTROL_FOCUSED
;
1543 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1547 dc
->DrawRectangle( rectHL
);
1552 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1553 yMid
= rect
.y
+ rect
.height
/2;
1555 // This probably needs to be done
1556 // on all platforms as the icons
1557 // otherwise nearly touch the border
1562 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1564 node
= node
->GetNext(), col
++ )
1566 wxListItemData
*item
= node
->GetData();
1568 int width
= m_owner
->GetColumnWidth(col
);
1572 const int wText
= width
- 8;
1573 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1575 if ( item
->HasImage() )
1578 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1579 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1581 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1587 if ( item
->HasText() )
1588 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1592 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1593 const wxString
& textOrig
,
1599 // we don't support displaying multiple lines currently (and neither does
1600 // wxMSW FWIW) so just merge all the lines
1601 wxString
text(textOrig
);
1602 text
.Replace(_T("\n"), _T(" "));
1605 dc
->GetTextExtent(text
, &w
, &h
);
1607 const wxCoord y
= yMid
- (h
+ 1)/2;
1609 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1611 // determine if the string can fit inside the current width
1614 // it can, draw it using the items alignment
1616 m_owner
->GetColumn(col
, item
);
1617 switch ( item
.GetAlign() )
1619 case wxLIST_FORMAT_LEFT
:
1623 case wxLIST_FORMAT_RIGHT
:
1627 case wxLIST_FORMAT_CENTER
:
1628 x
+= (width
- w
) / 2;
1632 wxFAIL_MSG( _T("unknown list item format") );
1636 dc
->DrawText(text
, x
, y
);
1638 else // otherwise, truncate and add an ellipsis if possible
1640 // determine the base width
1641 wxString
ellipsis(wxT("..."));
1643 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1645 // continue until we have enough space or only one character left
1647 size_t len
= text
.length();
1648 wxString drawntext
= text
.Left(len
);
1651 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1652 drawntext
.RemoveLast();
1655 if (w
+ base_w
<= width
)
1659 // if still not enough space, remove ellipsis characters
1660 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1662 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1663 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1666 // now draw the text
1667 dc
->DrawText(drawntext
, x
, y
);
1668 dc
->DrawText(ellipsis
, x
+ w
, y
);
1672 bool wxListLineData::Highlight( bool on
)
1674 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1676 if ( on
== m_highlighted
)
1684 void wxListLineData::ReverseHighlight( void )
1686 Highlight(!IsHighlighted());
1689 //-----------------------------------------------------------------------------
1690 // wxListHeaderWindow
1691 //-----------------------------------------------------------------------------
1693 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1694 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1695 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1696 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1699 void wxListHeaderWindow::Init()
1701 m_currentCursor
= (wxCursor
*) NULL
;
1702 m_isDragging
= false;
1706 wxListHeaderWindow::wxListHeaderWindow()
1710 m_owner
= (wxListMainWindow
*) NULL
;
1711 m_resizeCursor
= (wxCursor
*) NULL
;
1714 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1716 wxListMainWindow
*owner
,
1720 const wxString
&name
)
1721 : wxWindow( win
, id
, pos
, size
, style
, name
)
1726 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1729 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1730 SetOwnForegroundColour( attr
.colFg
);
1731 SetOwnBackgroundColour( attr
.colBg
);
1733 SetOwnFont( attr
.font
);
1735 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1736 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1738 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1742 wxListHeaderWindow::~wxListHeaderWindow()
1744 delete m_resizeCursor
;
1747 #ifdef __WXUNIVERSAL__
1748 #include "wx/univ/renderer.h"
1749 #include "wx/univ/theme.h"
1752 // shift the DC origin to match the position of the main window horz
1753 // scrollbar: this allows us to always use logical coords
1754 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1757 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1760 m_owner
->GetViewStart( &view_start
, NULL
);
1765 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1767 // account for the horz scrollbar offset
1769 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1771 // Maybe we just have to check for m_signX
1772 // in the DC, but I leave the #ifdef __WXGTK__
1774 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1778 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1781 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1783 wxPaintDC
dc( this );
1788 dc
.SetFont( GetFont() );
1790 // width and height of the entire header window
1792 GetClientSize( &w
, &h
);
1793 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1795 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1796 dc
.SetTextForeground(GetForegroundColour());
1798 int x
= HEADER_OFFSET_X
;
1799 int numColumns
= m_owner
->GetColumnCount();
1801 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1803 m_owner
->GetColumn( i
, item
);
1804 int wCol
= item
.m_width
;
1810 if (!m_parent
->IsEnabled())
1811 flags
|= wxCONTROL_DISABLED
;
1813 // NB: The code below is not really Mac-specific, but since we are close
1814 // to 2.8 release and I don't have time to test on other platforms, I
1815 // defined this only for wxMac. If this behavior is desired on
1816 // other platforms, please go ahead and revise or remove the #ifdef.
1818 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1819 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1820 flags
|= wxCONTROL_SELECTED
;
1823 wxRendererNative::Get().DrawHeaderButton
1827 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1831 // see if we have enough space for the column label
1833 // for this we need the width of the text
1836 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1837 wLabel
+= 2 * EXTRA_WIDTH
;
1839 // and the width of the icon, if any
1840 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1841 const int image
= item
.m_image
;
1842 wxImageList
*imageList
;
1845 imageList
= m_owner
->m_small_image_list
;
1848 imageList
->GetSize(image
, ix
, iy
);
1849 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1857 // ignore alignment if there is not enough space anyhow
1859 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1862 wxFAIL_MSG( _T("unknown list item format") );
1865 case wxLIST_FORMAT_LEFT
:
1869 case wxLIST_FORMAT_RIGHT
:
1870 xAligned
= x
+ cw
- wLabel
;
1873 case wxLIST_FORMAT_CENTER
:
1874 xAligned
= x
+ (cw
- wLabel
) / 2;
1878 // draw the text and image clipping them so that they
1879 // don't overwrite the column boundary
1880 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1882 // if we have an image, draw it on the right of the label
1889 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1890 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1891 wxIMAGELIST_DRAW_TRANSPARENT
1895 dc
.DrawText( item
.GetText(),
1896 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1901 // Fill in what's missing to the right of the columns, otherwise we will
1902 // leave an unpainted area when columns are removed (and it looks better)
1905 wxRendererNative::Get().DrawHeaderButton
1909 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1915 void wxListHeaderWindow::DrawCurrent()
1918 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1920 int x1
= m_currentX
;
1922 m_owner
->ClientToScreen( &x1
, &y1
);
1924 int x2
= m_currentX
;
1926 m_owner
->GetClientSize( NULL
, &y2
);
1927 m_owner
->ClientToScreen( &x2
, &y2
);
1930 dc
.SetLogicalFunction( wxINVERT
);
1931 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1932 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1936 dc
.DrawLine( x1
, y1
, x2
, y2
);
1938 dc
.SetLogicalFunction( wxCOPY
);
1940 dc
.SetPen( wxNullPen
);
1941 dc
.SetBrush( wxNullBrush
);
1945 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1947 // we want to work with logical coords
1949 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1950 int y
= event
.GetY();
1954 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1956 // we don't draw the line beyond our window, but we allow dragging it
1959 GetClientSize( &w
, NULL
);
1960 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1963 // erase the line if it was drawn
1964 if ( m_currentX
< w
)
1967 if (event
.ButtonUp())
1970 m_isDragging
= false;
1972 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1973 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1980 m_currentX
= m_minX
+ 7;
1982 // draw in the new location
1983 if ( m_currentX
< w
)
1987 else // not dragging
1990 bool hit_border
= false;
1992 // end of the current column
1995 // find the column where this event occurred
1997 countCol
= m_owner
->GetColumnCount();
1998 for (col
= 0; col
< countCol
; col
++)
2000 xpos
+= m_owner
->GetColumnWidth( col
);
2003 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2005 // near the column border
2012 // inside the column
2019 if ( col
== countCol
)
2022 if (event
.LeftDown() || event
.RightUp())
2024 if (hit_border
&& event
.LeftDown())
2026 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2027 event
.GetPosition()) )
2029 m_isDragging
= true;
2034 //else: column resizing was vetoed by the user code
2036 else // click on a column
2038 // record the selected state of the columns
2039 if (event
.LeftDown())
2041 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2044 m_owner
->GetColumn(i
, colItem
);
2045 long state
= colItem
.GetState();
2047 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2049 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2050 m_owner
->SetColumn(i
, colItem
);
2054 SendListEvent( event
.LeftDown()
2055 ? wxEVT_COMMAND_LIST_COL_CLICK
2056 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2057 event
.GetPosition());
2060 else if (event
.Moving())
2065 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2066 m_currentCursor
= m_resizeCursor
;
2070 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2071 m_currentCursor
= wxSTANDARD_CURSOR
;
2075 SetCursor(*m_currentCursor
);
2080 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2082 m_owner
->SetFocus();
2086 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2088 wxWindow
*parent
= GetParent();
2089 wxListEvent
le( type
, parent
->GetId() );
2090 le
.SetEventObject( parent
);
2091 le
.m_pointDrag
= pos
;
2093 // the position should be relative to the parent window, not
2094 // this one for compatibility with MSW and common sense: the
2095 // user code doesn't know anything at all about this header
2096 // window, so why should it get positions relative to it?
2097 le
.m_pointDrag
.y
-= GetSize().y
;
2099 le
.m_col
= m_column
;
2100 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2103 //-----------------------------------------------------------------------------
2104 // wxListRenameTimer (internal)
2105 //-----------------------------------------------------------------------------
2107 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2112 void wxListRenameTimer::Notify()
2114 m_owner
->OnRenameTimer();
2117 //-----------------------------------------------------------------------------
2118 // wxListTextCtrlWrapper (internal)
2119 //-----------------------------------------------------------------------------
2121 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2122 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2123 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2124 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2127 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2130 : m_startValue(owner
->GetItemText(itemEdit
)),
2131 m_itemEdited(itemEdit
)
2135 m_aboutToFinish
= false;
2137 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2139 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2140 &rectLabel
.x
, &rectLabel
.y
);
2142 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2143 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2144 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2147 m_text
->PushEventHandler(this);
2150 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2152 m_aboutToFinish
= true;
2154 if ( discardChanges
)
2156 m_owner
->OnRenameCancelled(m_itemEdited
);
2162 // Notify the owner about the changes
2165 // Even if vetoed, close the control (consistent with MSW)
2170 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2172 m_text
->RemoveEventHandler(this);
2173 m_owner
->ResetTextControl( m_text
);
2175 wxPendingDelete
.Append( this );
2178 m_owner
->SetFocus();
2181 bool wxListTextCtrlWrapper::AcceptChanges()
2183 const wxString value
= m_text
->GetValue();
2185 // notice that we should always call OnRenameAccept() to generate the "end
2186 // label editing" event, even if the user hasn't really changed anything
2187 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2189 // vetoed by the user
2193 // accepted, do rename the item (unless nothing changed)
2194 if ( value
!= m_startValue
)
2195 m_owner
->SetItemText(m_itemEdited
, value
);
2200 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2202 switch ( event
.m_keyCode
)
2217 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2219 if (m_aboutToFinish
)
2221 // auto-grow the textctrl:
2222 wxSize parentSize
= m_owner
->GetSize();
2223 wxPoint myPos
= m_text
->GetPosition();
2224 wxSize mySize
= m_text
->GetSize();
2226 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2227 if (myPos
.x
+ sx
> parentSize
.x
)
2228 sx
= parentSize
.x
- myPos
.x
;
2231 m_text
->SetSize(sx
, wxDefaultCoord
);
2237 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2239 if ( !m_aboutToFinish
)
2241 if ( !AcceptChanges() )
2242 m_owner
->OnRenameCancelled( m_itemEdited
);
2247 // We must let the native text control handle focus
2251 //-----------------------------------------------------------------------------
2253 //-----------------------------------------------------------------------------
2255 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
2256 EVT_PAINT (wxListMainWindow::OnPaint
)
2257 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2258 EVT_CHAR (wxListMainWindow::OnChar
)
2259 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2260 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2261 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2262 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2263 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2264 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
2267 void wxListMainWindow::Init()
2272 m_lineTo
= (size_t)-1;
2278 m_small_image_list
= (wxImageList
*) NULL
;
2279 m_normal_image_list
= (wxImageList
*) NULL
;
2281 m_small_spacing
= 30;
2282 m_normal_spacing
= 40;
2286 m_isCreated
= false;
2288 m_lastOnSame
= false;
2289 m_renameTimer
= new wxListRenameTimer( this );
2290 m_textctrlWrapper
= NULL
;
2294 m_lineSelectSingleOnUp
=
2295 m_lineBeforeLastClicked
= (size_t)-1;
2298 wxListMainWindow::wxListMainWindow()
2303 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2306 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2311 const wxString
&name
)
2312 : wxScrolledCanvas( parent
, id
, pos
, size
,
2313 style
| wxHSCROLL
| wxVSCROLL
, name
)
2317 m_highlightBrush
= new wxBrush
2319 wxSystemSettings::GetColour
2321 wxSYS_COLOUR_HIGHLIGHT
2326 m_highlightUnfocusedBrush
= new wxBrush
2328 wxSystemSettings::GetColour
2330 wxSYS_COLOUR_BTNSHADOW
2335 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2337 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2338 SetOwnForegroundColour( attr
.colFg
);
2339 SetOwnBackgroundColour( attr
.colBg
);
2341 SetOwnFont( attr
.font
);
2344 wxListMainWindow::~wxListMainWindow()
2347 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2348 WX_CLEAR_ARRAY(m_aColWidths
);
2350 delete m_highlightBrush
;
2351 delete m_highlightUnfocusedBrush
;
2352 delete m_renameTimer
;
2355 void wxListMainWindow::CacheLineData(size_t line
)
2357 wxGenericListCtrl
*listctrl
= GetListCtrl();
2359 wxListLineData
*ld
= GetDummyLine();
2361 size_t countCol
= GetColumnCount();
2362 for ( size_t col
= 0; col
< countCol
; col
++ )
2364 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2365 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2368 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2371 wxListLineData
*wxListMainWindow::GetDummyLine() const
2373 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2374 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2376 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2378 // we need to recreate the dummy line if the number of columns in the
2379 // control changed as it would have the incorrect number of fields
2381 if ( !m_lines
.IsEmpty() &&
2382 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2384 self
->m_lines
.Clear();
2387 if ( m_lines
.IsEmpty() )
2389 wxListLineData
*line
= new wxListLineData(self
);
2390 self
->m_lines
.Add(line
);
2392 // don't waste extra memory -- there never going to be anything
2393 // else/more in this array
2394 self
->m_lines
.Shrink();
2400 // ----------------------------------------------------------------------------
2401 // line geometry (report mode only)
2402 // ----------------------------------------------------------------------------
2404 wxCoord
wxListMainWindow::GetLineHeight() const
2406 // we cache the line height as calling GetTextExtent() is slow
2407 if ( !m_lineHeight
)
2409 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2411 wxClientDC
dc( self
);
2412 dc
.SetFont( GetFont() );
2415 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2417 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2420 m_small_image_list
->GetSize(0, iw
, ih
);
2425 self
->m_lineHeight
= y
+ LINE_SPACING
;
2428 return m_lineHeight
;
2431 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2433 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2435 return LINE_SPACING
+ line
* GetLineHeight();
2438 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2440 if ( !InReportView() )
2441 return GetLine(line
)->m_gi
->m_rectAll
;
2444 rect
.x
= HEADER_OFFSET_X
;
2445 rect
.y
= GetLineY(line
);
2446 rect
.width
= GetHeaderWidth();
2447 rect
.height
= GetLineHeight();
2452 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2454 if ( !InReportView() )
2455 return GetLine(line
)->m_gi
->m_rectLabel
;
2458 wxListLineData
*data
= GetLine(line
);
2459 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2462 wxListItemData
*item
= node
->GetData();
2463 if ( item
->HasImage() )
2466 GetImageSize( item
->GetImage(), ix
, iy
);
2467 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2472 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2473 rect
.y
= GetLineY(line
);
2474 rect
.width
= GetColumnWidth(0) - image_x
;
2475 rect
.height
= GetLineHeight();
2480 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2482 if ( !InReportView() )
2483 return GetLine(line
)->m_gi
->m_rectIcon
;
2485 wxListLineData
*ld
= GetLine(line
);
2486 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2489 rect
.x
= HEADER_OFFSET_X
;
2490 rect
.y
= GetLineY(line
);
2491 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2496 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2498 return InReportView() ? GetLineRect(line
)
2499 : GetLine(line
)->m_gi
->m_rectHighlight
;
2502 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2504 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2506 wxListLineData
*ld
= GetLine(line
);
2508 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2509 return wxLIST_HITTEST_ONITEMICON
;
2511 // VS: Testing for "ld->HasText() || InReportView()" instead of
2512 // "ld->HasText()" is needed to make empty lines in report view
2514 if ( ld
->HasText() || InReportView() )
2516 wxRect rect
= InReportView() ? GetLineRect(line
)
2517 : GetLineLabelRect(line
);
2519 if ( rect
.Contains(x
, y
) )
2520 return wxLIST_HITTEST_ONITEMLABEL
;
2526 // ----------------------------------------------------------------------------
2527 // highlight (selection) handling
2528 // ----------------------------------------------------------------------------
2530 bool wxListMainWindow::IsHighlighted(size_t line
) const
2534 return m_selStore
.IsSelected(line
);
2538 wxListLineData
*ld
= GetLine(line
);
2539 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2541 return ld
->IsHighlighted();
2545 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2551 wxArrayInt linesChanged
;
2552 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2555 // meny items changed state, refresh everything
2556 RefreshLines(lineFrom
, lineTo
);
2558 else // only a few items changed state, refresh only them
2560 size_t count
= linesChanged
.GetCount();
2561 for ( size_t n
= 0; n
< count
; n
++ )
2563 RefreshLine(linesChanged
[n
]);
2567 else // iterate over all items in non report view
2569 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2571 if ( HighlightLine(line
, highlight
) )
2577 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2583 changed
= m_selStore
.SelectItem(line
, highlight
);
2587 wxListLineData
*ld
= GetLine(line
);
2588 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2590 changed
= ld
->Highlight(highlight
);
2595 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2596 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2602 void wxListMainWindow::RefreshLine( size_t line
)
2604 if ( InReportView() )
2606 size_t visibleFrom
, visibleTo
;
2607 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2609 if ( line
< visibleFrom
|| line
> visibleTo
)
2613 wxRect rect
= GetLineRect(line
);
2615 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2616 RefreshRect( rect
);
2619 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2621 // we suppose that they are ordered by caller
2622 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2624 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2626 if ( InReportView() )
2628 size_t visibleFrom
, visibleTo
;
2629 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2631 if ( lineFrom
< visibleFrom
)
2632 lineFrom
= visibleFrom
;
2633 if ( lineTo
> visibleTo
)
2638 rect
.y
= GetLineY(lineFrom
);
2639 rect
.width
= GetClientSize().x
;
2640 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2642 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2643 RefreshRect( rect
);
2647 // TODO: this should be optimized...
2648 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2655 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2657 if ( InReportView() )
2659 size_t visibleFrom
, visibleTo
;
2660 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2662 if ( lineFrom
< visibleFrom
)
2663 lineFrom
= visibleFrom
;
2664 else if ( lineFrom
> visibleTo
)
2669 rect
.y
= GetLineY(lineFrom
);
2670 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2672 wxSize size
= GetClientSize();
2673 rect
.width
= size
.x
;
2675 // refresh till the bottom of the window
2676 rect
.height
= size
.y
- rect
.y
;
2678 RefreshRect( rect
);
2682 // TODO: how to do it more efficiently?
2687 void wxListMainWindow::RefreshSelected()
2693 if ( InReportView() )
2695 GetVisibleLinesRange(&from
, &to
);
2700 to
= GetItemCount() - 1;
2703 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2704 RefreshLine(m_current
);
2706 for ( size_t line
= from
; line
<= to
; line
++ )
2708 // NB: the test works as expected even if m_current == -1
2709 if ( line
!= m_current
&& IsHighlighted(line
) )
2714 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2716 // Note: a wxPaintDC must be constructed even if no drawing is
2717 // done (a Windows requirement).
2718 wxPaintDC
dc( this );
2722 // nothing to draw or not the moment to draw it
2728 // delay the repainting until we calculate all the items positions
2735 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2737 dc
.SetFont( GetFont() );
2739 if ( InReportView() )
2741 int lineHeight
= GetLineHeight();
2743 size_t visibleFrom
, visibleTo
;
2744 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2747 int xOrig
= dc
.LogicalToDeviceX( 0 );
2748 int yOrig
= dc
.LogicalToDeviceY( 0 );
2750 // tell the caller cache to cache the data
2753 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2754 GetParent()->GetId());
2755 evCache
.SetEventObject( GetParent() );
2756 evCache
.m_oldItemIndex
= visibleFrom
;
2757 evCache
.m_itemIndex
= visibleTo
;
2758 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2761 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2763 rectLine
= GetLineRect(line
);
2766 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2767 rectLine
.width
, rectLine
.height
) )
2769 // don't redraw unaffected lines to avoid flicker
2773 GetLine(line
)->DrawInReportMode( &dc
,
2775 GetLineHighlightRect(line
),
2776 IsHighlighted(line
) );
2779 if ( HasFlag(wxLC_HRULES
) )
2781 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2782 wxSize clientSize
= GetClientSize();
2784 size_t i
= visibleFrom
;
2785 if (i
== 0) i
= 1; // Don't draw the first one
2786 for ( ; i
<= visibleTo
; i
++ )
2789 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2790 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2791 clientSize
.x
- dev_x
, i
* lineHeight
);
2794 // Draw last horizontal rule
2795 if ( visibleTo
== GetItemCount() - 1 )
2798 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2799 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2800 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2804 // Draw vertical rules if required
2805 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2807 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2808 wxRect firstItemRect
, lastItemRect
;
2810 GetItemRect(visibleFrom
, firstItemRect
);
2811 GetItemRect(visibleTo
, lastItemRect
);
2812 int x
= firstItemRect
.GetX();
2814 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2816 for (int col
= 0; col
< GetColumnCount(); col
++)
2818 int colWidth
= GetColumnWidth(col
);
2820 int x_pos
= x
- dev_x
;
2821 if (col
< GetColumnCount()-1) x_pos
-= 2;
2822 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2823 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2829 size_t count
= GetItemCount();
2830 for ( size_t i
= 0; i
< count
; i
++ )
2832 GetLine(i
)->Draw( &dc
);
2837 // Don't draw rect outline under Mac at all.
2842 wxRect
rect( GetLineHighlightRect( m_current
) );
2844 dc
.SetPen( *wxBLACK_PEN
);
2845 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2846 dc
.DrawRectangle( rect
);
2848 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2856 void wxListMainWindow::HighlightAll( bool on
)
2858 if ( IsSingleSel() )
2860 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2862 // we just have one item to turn off
2863 if ( HasCurrent() && IsHighlighted(m_current
) )
2865 HighlightLine(m_current
, false);
2866 RefreshLine(m_current
);
2869 else // multi selection
2872 HighlightLines(0, GetItemCount() - 1, on
);
2876 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2878 // Do nothing here. This prevents the default handler in wxScrolledWindow
2879 // from needlessly scrolling the window when the edit control is
2880 // dismissed. See ticket #9563.
2883 void wxListMainWindow::SendNotify( size_t line
,
2884 wxEventType command
,
2885 const wxPoint
& point
)
2887 wxListEvent
le( command
, GetParent()->GetId() );
2888 le
.SetEventObject( GetParent() );
2890 le
.m_itemIndex
= line
;
2892 // set only for events which have position
2893 if ( point
!= wxDefaultPosition
)
2894 le
.m_pointDrag
= point
;
2896 // don't try to get the line info for virtual list controls: the main
2897 // program has it anyhow and if we did it would result in accessing all
2898 // the lines, even those which are not visible now and this is precisely
2899 // what we're trying to avoid
2902 if ( line
!= (size_t)-1 )
2904 GetLine(line
)->GetItem( 0, le
.m_item
);
2906 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2908 //else: there may be no more such item
2910 GetParent()->GetEventHandler()->ProcessEvent( le
);
2913 void wxListMainWindow::ChangeCurrent(size_t current
)
2915 m_current
= current
;
2917 // as the current item changed, we shouldn't start editing it when the
2918 // "slow click" timer expires as the click happened on another item
2919 if ( m_renameTimer
->IsRunning() )
2920 m_renameTimer
->Stop();
2922 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2925 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2927 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2928 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2930 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2931 wxT("EditLabel() needs a text control") );
2933 size_t itemEdit
= (size_t)item
;
2935 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2936 le
.SetEventObject( GetParent() );
2937 le
.m_itemIndex
= item
;
2938 wxListLineData
*data
= GetLine(itemEdit
);
2939 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2940 data
->GetItem( 0, le
.m_item
);
2942 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2944 // vetoed by user code
2948 // We have to call this here because the label in question might just have
2949 // been added and no screen update taken place.
2954 // Pending events dispatched by wxSafeYield might have changed the item
2956 if ( (size_t)item
>= GetItemCount() )
2960 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2961 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2962 return m_textctrlWrapper
->GetText();
2965 void wxListMainWindow::OnRenameTimer()
2967 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2969 EditLabel( m_current
);
2972 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2974 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2975 le
.SetEventObject( GetParent() );
2976 le
.m_itemIndex
= itemEdit
;
2978 wxListLineData
*data
= GetLine(itemEdit
);
2980 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2982 data
->GetItem( 0, le
.m_item
);
2983 le
.m_item
.m_text
= value
;
2984 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2988 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2990 // let owner know that the edit was cancelled
2991 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2993 le
.SetEditCanceled(true);
2995 le
.SetEventObject( GetParent() );
2996 le
.m_itemIndex
= itemEdit
;
2998 wxListLineData
*data
= GetLine(itemEdit
);
2999 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
3001 data
->GetItem( 0, le
.m_item
);
3002 GetEventHandler()->ProcessEvent( le
);
3005 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3009 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3010 // shutdown the edit control when the mouse is clicked elsewhere on the
3011 // listctrl because the order of events is different (or something like
3012 // that), so explicitly end the edit if it is active.
3013 if ( event
.LeftDown() && m_textctrlWrapper
)
3014 m_textctrlWrapper
->EndEdit( false );
3017 if ( event
.LeftDown() )
3020 event
.SetEventObject( GetParent() );
3021 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3024 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3026 // let the base handle mouse wheel events.
3031 if ( !HasCurrent() || IsEmpty() )
3033 if (event
.RightDown())
3035 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3037 wxContextMenuEvent
evtCtx(
3039 GetParent()->GetId(),
3040 ClientToScreen(event
.GetPosition()));
3041 evtCtx
.SetEventObject(GetParent());
3042 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3050 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3051 event
.ButtonDClick()) )
3054 int x
= event
.GetX();
3055 int y
= event
.GetY();
3056 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3058 // where did we hit it (if we did)?
3061 size_t count
= GetItemCount(),
3064 if ( InReportView() )
3066 current
= y
/ GetLineHeight();
3067 if ( current
< count
)
3068 hitResult
= HitTestLine(current
, x
, y
);
3072 // TODO: optimize it too! this is less simple than for report view but
3073 // enumerating all items is still not a way to do it!!
3074 for ( current
= 0; current
< count
; current
++ )
3076 hitResult
= HitTestLine(current
, x
, y
);
3082 if (event
.Dragging())
3084 if (m_dragCount
== 0)
3086 // we have to report the raw, physical coords as we want to be
3087 // able to call HitTest(event.m_pointDrag) from the user code to
3088 // get the item being dragged
3089 m_dragStart
= event
.GetPosition();
3094 if (m_dragCount
!= 3)
3097 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3098 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3100 wxListEvent
le( command
, GetParent()->GetId() );
3101 le
.SetEventObject( GetParent() );
3102 le
.m_itemIndex
= m_lineLastClicked
;
3103 le
.m_pointDrag
= m_dragStart
;
3104 GetParent()->GetEventHandler()->ProcessEvent( le
);
3115 // outside of any item
3116 if (event
.RightDown())
3118 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3120 wxContextMenuEvent
evtCtx(
3122 GetParent()->GetId(),
3123 ClientToScreen(event
.GetPosition()));
3124 evtCtx
.SetEventObject(GetParent());
3125 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3129 // reset the selection and bail out
3130 HighlightAll(false);
3136 bool forceClick
= false;
3137 if (event
.ButtonDClick())
3139 if ( m_renameTimer
->IsRunning() )
3140 m_renameTimer
->Stop();
3142 m_lastOnSame
= false;
3144 if ( current
== m_lineLastClicked
)
3146 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3152 // The first click was on another item, so don't interpret this as
3153 // a double click, but as a simple click instead
3160 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3162 // select single line
3163 HighlightAll( false );
3164 ReverseHighlight(m_lineSelectSingleOnUp
);
3169 if ((current
== m_current
) &&
3170 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3171 HasFlag(wxLC_EDIT_LABELS
) )
3173 if ( !InReportView() ||
3174 GetLineLabelRect(current
).Contains(x
, y
) )
3176 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3177 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3182 m_lastOnSame
= false;
3183 m_lineSelectSingleOnUp
= (size_t)-1;
3187 // This is necessary, because after a DnD operation in
3188 // from and to ourself, the up event is swallowed by the
3189 // DnD code. So on next non-up event (which means here and
3190 // now) m_lineSelectSingleOnUp should be reset.
3191 m_lineSelectSingleOnUp
= (size_t)-1;
3193 if (event
.RightDown())
3195 m_lineBeforeLastClicked
= m_lineLastClicked
;
3196 m_lineLastClicked
= current
;
3198 // If the item is already selected, do not update the selection.
3199 // Multi-selections should not be cleared if a selected item is clicked.
3200 if (!IsHighlighted(current
))
3202 HighlightAll(false);
3203 ChangeCurrent(current
);
3204 ReverseHighlight(m_current
);
3207 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3209 // Allow generation of context menu event
3212 else if (event
.MiddleDown())
3214 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3216 else if ( event
.LeftDown() || forceClick
)
3218 m_lineBeforeLastClicked
= m_lineLastClicked
;
3219 m_lineLastClicked
= current
;
3221 size_t oldCurrent
= m_current
;
3222 bool oldWasSelected
= IsHighlighted(m_current
);
3224 bool cmdModifierDown
= event
.CmdDown();
3225 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3227 if ( IsSingleSel() || !IsHighlighted(current
) )
3229 HighlightAll( false );
3231 ChangeCurrent(current
);
3233 ReverseHighlight(m_current
);
3235 else // multi sel & current is highlighted & no mod keys
3237 m_lineSelectSingleOnUp
= current
;
3238 ChangeCurrent(current
); // change focus
3241 else // multi sel & either ctrl or shift is down
3243 if (cmdModifierDown
)
3245 ChangeCurrent(current
);
3247 ReverseHighlight(m_current
);
3249 else if (event
.ShiftDown())
3251 ChangeCurrent(current
);
3253 size_t lineFrom
= oldCurrent
,
3256 if ( lineTo
< lineFrom
)
3259 lineFrom
= m_current
;
3262 HighlightLines(lineFrom
, lineTo
);
3264 else // !ctrl, !shift
3266 // test in the enclosing if should make it impossible
3267 wxFAIL_MSG( _T("how did we get here?") );
3271 if (m_current
!= oldCurrent
)
3272 RefreshLine( oldCurrent
);
3274 // forceClick is only set if the previous click was on another item
3275 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3279 void wxListMainWindow::MoveToItem(size_t item
)
3281 if ( item
== (size_t)-1 )
3284 wxRect rect
= GetLineRect(item
);
3286 int client_w
, client_h
;
3287 GetClientSize( &client_w
, &client_h
);
3289 const int hLine
= GetLineHeight();
3291 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3292 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3294 if ( InReportView() )
3296 // the next we need the range of lines shown it might be different,
3297 // so recalculate it
3298 ResetVisibleLinesRange();
3300 if (rect
.y
< view_y
)
3301 Scroll( -1, rect
.y
/ hLine
);
3302 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3303 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3306 // At least on Mac the visible lines value will get reset inside of
3307 // Scroll *before* it actually scrolls the window because of the
3308 // Update() that happens there, so it will still have the wrong value.
3309 // So let's reset it again and wait for it to be recalculated in the
3310 // next paint event. I would expect this problem to show up in wxGTK
3311 // too but couldn't duplicate it there. Perhaps the order of events
3312 // is different... --Robin
3313 ResetVisibleLinesRange();
3321 if (rect
.x
-view_x
< 5)
3322 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3323 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3324 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3326 if (rect
.y
-view_y
< 5)
3327 sy
= (rect
.y
- 5) / hLine
;
3328 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3329 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3335 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3337 if ( !InReportView() )
3339 // TODO: this should work in all views but is not implemented now
3344 GetVisibleLinesRange(&top
, &bottom
);
3346 if ( bottom
== (size_t)-1 )
3349 ResetVisibleLinesRange();
3351 int hLine
= GetLineHeight();
3353 Scroll(-1, top
+ dy
/ hLine
);
3356 // see comment in MoveToItem() for why we do this
3357 ResetVisibleLinesRange();
3363 // ----------------------------------------------------------------------------
3364 // keyboard handling
3365 // ----------------------------------------------------------------------------
3367 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3369 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3370 _T("invalid item index in OnArrowChar()") );
3372 size_t oldCurrent
= m_current
;
3374 // in single selection we just ignore Shift as we can't select several
3376 if ( event
.ShiftDown() && !IsSingleSel() )
3378 ChangeCurrent(newCurrent
);
3380 // refresh the old focus to remove it
3381 RefreshLine( oldCurrent
);
3383 // select all the items between the old and the new one
3384 if ( oldCurrent
> newCurrent
)
3386 newCurrent
= oldCurrent
;
3387 oldCurrent
= m_current
;
3390 HighlightLines(oldCurrent
, newCurrent
);
3394 // all previously selected items are unselected unless ctrl is held
3395 // in a multiselection control
3396 if ( !event
.ControlDown() || IsSingleSel() )
3397 HighlightAll(false);
3399 ChangeCurrent(newCurrent
);
3401 // refresh the old focus to remove it
3402 RefreshLine( oldCurrent
);
3404 // in single selection mode we must always have a selected item
3405 if ( !event
.ControlDown() || IsSingleSel() )
3406 HighlightLine( m_current
, true );
3409 RefreshLine( m_current
);
3414 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3416 wxWindow
*parent
= GetParent();
3418 // propagate the key event upwards
3419 wxKeyEvent
ke(event
);
3420 ke
.SetEventObject( parent
);
3421 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3427 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3429 wxWindow
*parent
= GetParent();
3431 // propagate the key event upwards
3432 wxKeyEvent
ke(event
);
3433 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3439 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3441 wxWindow
*parent
= GetParent();
3443 // send a list_key event up
3446 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3447 le
.m_itemIndex
= m_current
;
3448 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3449 le
.m_code
= event
.GetKeyCode();
3450 le
.SetEventObject( parent
);
3451 parent
->GetEventHandler()->ProcessEvent( le
);
3454 // propagate the char event upwards
3455 wxKeyEvent
ke(event
);
3456 ke
.SetEventObject( parent
);
3457 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3460 if ( HandleAsNavigationKey(event
) )
3463 // no item -> nothing to do
3470 // don't use m_linesPerPage directly as it might not be computed yet
3471 const int pageSize
= GetCountPerPage();
3472 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3474 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3476 if (event
.GetKeyCode() == WXK_RIGHT
)
3477 event
.m_keyCode
= WXK_LEFT
;
3478 else if (event
.GetKeyCode() == WXK_LEFT
)
3479 event
.m_keyCode
= WXK_RIGHT
;
3482 switch ( event
.GetKeyCode() )
3485 if ( m_current
> 0 )
3486 OnArrowChar( m_current
- 1, event
);
3490 if ( m_current
< (size_t)GetItemCount() - 1 )
3491 OnArrowChar( m_current
+ 1, event
);
3496 OnArrowChar( GetItemCount() - 1, event
);
3501 OnArrowChar( 0, event
);
3506 int steps
= InReportView() ? pageSize
- 1
3507 : m_current
% pageSize
;
3509 int index
= m_current
- steps
;
3513 OnArrowChar( index
, event
);
3519 int steps
= InReportView()
3521 : pageSize
- (m_current
% pageSize
) - 1;
3523 size_t index
= m_current
+ steps
;
3524 size_t count
= GetItemCount();
3525 if ( index
>= count
)
3528 OnArrowChar( index
, event
);
3533 if ( !InReportView() )
3535 int index
= m_current
- pageSize
;
3539 OnArrowChar( index
, event
);
3544 if ( !InReportView() )
3546 size_t index
= m_current
+ pageSize
;
3548 size_t count
= GetItemCount();
3549 if ( index
>= count
)
3552 OnArrowChar( index
, event
);
3557 if ( IsSingleSel() )
3559 if ( event
.ControlDown() )
3561 ReverseHighlight(m_current
);
3563 else // normal space press
3565 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3568 else // multiple selection
3570 ReverseHighlight(m_current
);
3576 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3584 // ----------------------------------------------------------------------------
3586 // ----------------------------------------------------------------------------
3588 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3592 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3593 event
.SetEventObject( GetParent() );
3594 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3598 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3599 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3600 // which are already drawn correctly resulting in horrible flicker - avoid
3610 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3614 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3615 event
.SetEventObject( GetParent() );
3616 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3624 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3626 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3628 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3630 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3632 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3634 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3636 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3638 else if ( InReportView() && (m_small_image_list
))
3640 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3644 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3646 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3648 m_normal_image_list
->GetSize( index
, width
, height
);
3650 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3652 m_small_image_list
->GetSize( index
, width
, height
);
3654 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3656 m_small_image_list
->GetSize( index
, width
, height
);
3658 else if ( InReportView() && m_small_image_list
)
3660 m_small_image_list
->GetSize( index
, width
, height
);
3669 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3671 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3672 dc
.SetFont( GetFont() );
3675 dc
.GetTextExtent( s
, &lw
, NULL
);
3677 return lw
+ AUTOSIZE_COL_MARGIN
;
3680 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3684 // calc the spacing from the icon size
3685 int width
= 0, height
= 0;
3687 if ((imageList
) && (imageList
->GetImageCount()) )
3688 imageList
->GetSize(0, width
, height
);
3690 if (which
== wxIMAGE_LIST_NORMAL
)
3692 m_normal_image_list
= imageList
;
3693 m_normal_spacing
= width
+ 8;
3696 if (which
== wxIMAGE_LIST_SMALL
)
3698 m_small_image_list
= imageList
;
3699 m_small_spacing
= width
+ 14;
3700 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3704 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3708 m_small_spacing
= spacing
;
3710 m_normal_spacing
= spacing
;
3713 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3715 return isSmall
? m_small_spacing
: m_normal_spacing
;
3718 // ----------------------------------------------------------------------------
3720 // ----------------------------------------------------------------------------
3722 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3724 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3726 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3728 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3729 item
.m_width
= GetTextLength( item
.m_text
);
3731 wxListHeaderData
*column
= node
->GetData();
3732 column
->SetItem( item
);
3734 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3736 headerWin
->m_dirty
= true;
3740 // invalidate it as it has to be recalculated
3744 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3746 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3747 _T("invalid column index") );
3749 wxCHECK_RET( InReportView(),
3750 _T("SetColumnWidth() can only be called in report mode.") );
3753 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3755 headerWin
->m_dirty
= true;
3757 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3758 wxCHECK_RET( node
, _T("no column?") );
3760 wxListHeaderData
*column
= node
->GetData();
3762 size_t count
= GetItemCount();
3764 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3766 width
= GetTextLength(column
->GetText());
3767 width
+= 2*EXTRA_WIDTH
;
3769 // check for column header's image availability
3770 const int image
= column
->GetImage();
3773 if ( m_small_image_list
)
3776 m_small_image_list
->GetSize(image
, ix
, iy
);
3777 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3781 else if ( width
== wxLIST_AUTOSIZE
)
3785 // TODO: determine the max width somehow...
3786 width
= WIDTH_COL_DEFAULT
;
3790 wxClientDC
dc(this);
3791 dc
.SetFont( GetFont() );
3793 int max
= AUTOSIZE_COL_MARGIN
;
3795 // if the cached column width isn't valid then recalculate it
3796 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3798 for (size_t i
= 0; i
< count
; i
++)
3800 wxListLineData
*line
= GetLine( i
);
3801 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3803 wxCHECK_RET( n
, _T("no subitem?") );
3805 wxListItemData
*itemData
= n
->GetData();
3808 itemData
->GetItem(item
);
3809 int itemWidth
= GetItemWidthWithImage(&item
);
3810 if (itemWidth
> max
)
3814 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3815 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3818 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3819 width
= max
+ AUTOSIZE_COL_MARGIN
;
3823 column
->SetWidth( width
);
3825 // invalidate it as it has to be recalculated
3829 int wxListMainWindow::GetHeaderWidth() const
3831 if ( !m_headerWidth
)
3833 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3835 size_t count
= GetColumnCount();
3836 for ( size_t col
= 0; col
< count
; col
++ )
3838 self
->m_headerWidth
+= GetColumnWidth(col
);
3842 return m_headerWidth
;
3845 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3847 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3848 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3850 wxListHeaderData
*column
= node
->GetData();
3851 column
->GetItem( item
);
3854 int wxListMainWindow::GetColumnWidth( int col
) const
3856 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3857 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3859 wxListHeaderData
*column
= node
->GetData();
3860 return column
->GetWidth();
3863 // ----------------------------------------------------------------------------
3865 // ----------------------------------------------------------------------------
3867 void wxListMainWindow::SetItem( wxListItem
&item
)
3869 long id
= item
.m_itemId
;
3870 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3871 _T("invalid item index in SetItem") );
3875 wxListLineData
*line
= GetLine((size_t)id
);
3876 line
->SetItem( item
.m_col
, item
);
3878 // Set item state if user wants
3879 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3880 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3884 // update the Max Width Cache if needed
3885 int width
= GetItemWidthWithImage(&item
);
3887 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3888 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3892 // update the item on screen
3894 GetItemRect(id
, rectItem
);
3895 RefreshRect(rectItem
);
3898 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3903 // first deal with selection
3904 if ( stateMask
& wxLIST_STATE_SELECTED
)
3906 // set/clear select state
3909 // optimized version for virtual listctrl.
3910 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3913 else if ( state
& wxLIST_STATE_SELECTED
)
3915 const long count
= GetItemCount();
3916 for( long i
= 0; i
< count
; i
++ )
3918 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3924 // clear for non virtual (somewhat optimized by using GetNextItem())
3926 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3928 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3933 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3935 // unfocus all: only one item can be focussed, so clearing focus for
3936 // all items is simply clearing focus of the focussed item.
3937 SetItemState(m_current
, state
, stateMask
);
3939 //(setting focus to all items makes no sense, so it is not handled here.)
3942 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3946 SetItemStateAll(state
, stateMask
);
3950 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3951 _T("invalid list ctrl item index in SetItem") );
3953 size_t oldCurrent
= m_current
;
3954 size_t item
= (size_t)litem
; // safe because of the check above
3956 // do we need to change the focus?
3957 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3959 if ( state
& wxLIST_STATE_FOCUSED
)
3961 // don't do anything if this item is already focused
3962 if ( item
!= m_current
)
3964 ChangeCurrent(item
);
3966 if ( oldCurrent
!= (size_t)-1 )
3968 if ( IsSingleSel() )
3970 HighlightLine(oldCurrent
, false);
3973 RefreshLine(oldCurrent
);
3976 RefreshLine( m_current
);
3981 // don't do anything if this item is not focused
3982 if ( item
== m_current
)
3986 if ( IsSingleSel() )
3988 // we must unselect the old current item as well or we
3989 // might end up with more than one selected item in a
3990 // single selection control
3991 HighlightLine(oldCurrent
, false);
3994 RefreshLine( oldCurrent
);
3999 // do we need to change the selection state?
4000 if ( stateMask
& wxLIST_STATE_SELECTED
)
4002 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4004 if ( IsSingleSel() )
4008 // selecting the item also makes it the focused one in the
4010 if ( m_current
!= item
)
4012 ChangeCurrent(item
);
4014 if ( oldCurrent
!= (size_t)-1 )
4016 HighlightLine( oldCurrent
, false );
4017 RefreshLine( oldCurrent
);
4023 // only the current item may be selected anyhow
4024 if ( item
!= m_current
)
4029 if ( HighlightLine(item
, on
) )
4036 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4038 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4039 _T("invalid list ctrl item index in GetItemState()") );
4041 int ret
= wxLIST_STATE_DONTCARE
;
4043 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4045 if ( (size_t)item
== m_current
)
4046 ret
|= wxLIST_STATE_FOCUSED
;
4049 if ( stateMask
& wxLIST_STATE_SELECTED
)
4051 if ( IsHighlighted(item
) )
4052 ret
|= wxLIST_STATE_SELECTED
;
4058 void wxListMainWindow::GetItem( wxListItem
&item
) const
4060 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4061 _T("invalid item index in GetItem") );
4063 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4064 line
->GetItem( item
.m_col
, item
);
4066 // Get item state if user wants it
4067 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4068 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4069 wxLIST_STATE_FOCUSED
);
4072 // ----------------------------------------------------------------------------
4074 // ----------------------------------------------------------------------------
4076 size_t wxListMainWindow::GetItemCount() const
4078 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4081 void wxListMainWindow::SetItemCount(long count
)
4083 m_selStore
.SetItemCount(count
);
4084 m_countVirt
= count
;
4086 ResetVisibleLinesRange();
4088 // scrollbars must be reset
4092 int wxListMainWindow::GetSelectedItemCount() const
4094 // deal with the quick case first
4095 if ( IsSingleSel() )
4096 return HasCurrent() ? IsHighlighted(m_current
) : false;
4098 // virtual controls remmebers all its selections itself
4100 return m_selStore
.GetSelectedCount();
4102 // TODO: we probably should maintain the number of items selected even for
4103 // non virtual controls as enumerating all lines is really slow...
4104 size_t countSel
= 0;
4105 size_t count
= GetItemCount();
4106 for ( size_t line
= 0; line
< count
; line
++ )
4108 if ( GetLine(line
)->IsHighlighted() )
4115 // ----------------------------------------------------------------------------
4116 // item position/size
4117 // ----------------------------------------------------------------------------
4119 wxRect
wxListMainWindow::GetViewRect() const
4121 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
4123 // we need to find the longest/tallest label
4124 wxCoord xMax
= 0, yMax
= 0;
4125 const int count
= GetItemCount();
4128 for ( int i
= 0; i
< count
; i
++ )
4130 // we need logical, not physical, coordinates here, so use
4131 // GetLineRect() instead of GetItemRect()
4132 wxRect r
= GetLineRect(i
);
4134 wxCoord x
= r
.GetRight(),
4144 // some fudge needed to make it look prettier
4145 xMax
+= 2 * EXTRA_BORDER_X
;
4146 yMax
+= 2 * EXTRA_BORDER_Y
;
4148 // account for the scrollbars if necessary
4149 const wxSize sizeAll
= GetClientSize();
4150 if ( xMax
> sizeAll
.x
)
4151 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4152 if ( yMax
> sizeAll
.y
)
4153 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4155 return wxRect(0, 0, xMax
, yMax
);
4159 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
4161 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
4163 _T("GetSubItemRect only meaningful in report view") );
4164 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
4165 _T("invalid item in GetSubItemRect") );
4167 // ensure that we're laid out, otherwise we could return nonsense
4170 wxConstCast(this, wxListMainWindow
)->
4171 RecalculatePositions(true /* no refresh */);
4174 rect
= GetLineRect((size_t)item
);
4176 // Adjust rect to specified column
4177 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
4179 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
4180 _T("invalid subItem in GetSubItemRect") );
4182 for (int i
= 0; i
< subItem
; i
++)
4184 rect
.x
+= GetColumnWidth(i
);
4186 rect
.width
= GetColumnWidth(subItem
);
4189 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4194 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4197 GetItemRect(item
, rect
);
4205 // ----------------------------------------------------------------------------
4206 // geometry calculation
4207 // ----------------------------------------------------------------------------
4209 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4211 const int lineHeight
= GetLineHeight();
4213 wxClientDC
dc( this );
4214 dc
.SetFont( GetFont() );
4216 const size_t count
= GetItemCount();
4219 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
4220 iconSpacing
= m_normal_spacing
;
4221 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
4222 iconSpacing
= m_small_spacing
;
4226 // Note that we do not call GetClientSize() here but
4227 // GetSize() and subtract the border size for sunken
4228 // borders manually. This is technically incorrect,
4229 // but we need to know the client area's size WITHOUT
4230 // scrollbars here. Since we don't know if there are
4231 // any scrollbars, we use GetSize() instead. Another
4232 // solution would be to call SetScrollbars() here to
4233 // remove the scrollbars and call GetClientSize() then,
4234 // but this might result in flicker and - worse - will
4235 // reset the scrollbars to 0 which is not good at all
4236 // if you resize a dialog/window, but don't want to
4237 // reset the window scrolling. RR.
4238 // Furthermore, we actually do NOT subtract the border
4239 // width as 2 pixels is just the extra space which we
4240 // need around the actual content in the window. Other-
4241 // wise the text would e.g. touch the upper border. RR.
4244 GetSize( &clientWidth
, &clientHeight
);
4246 if ( InReportView() )
4248 // all lines have the same height and we scroll one line per step
4249 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4251 m_linesPerPage
= clientHeight
/ lineHeight
;
4253 ResetVisibleLinesRange();
4255 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4256 GetHeaderWidth() / SCROLL_UNIT_X
,
4257 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4258 GetScrollPos(wxHORIZONTAL
),
4259 GetScrollPos(wxVERTICAL
),
4264 // we have 3 different layout strategies: either layout all items
4265 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4266 // to arrange them in top to bottom, left to right (don't ask me why
4267 // not the other way round...) order
4268 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4270 int x
= EXTRA_BORDER_X
;
4271 int y
= EXTRA_BORDER_Y
;
4273 wxCoord widthMax
= 0;
4276 for ( i
= 0; i
< count
; i
++ )
4278 wxListLineData
*line
= GetLine(i
);
4279 line
->CalculateSize( &dc
, iconSpacing
);
4280 line
->SetPosition( x
, y
, iconSpacing
);
4282 wxSize sizeLine
= GetLineSize(i
);
4284 if ( HasFlag(wxLC_ALIGN_TOP
) )
4286 if ( sizeLine
.x
> widthMax
)
4287 widthMax
= sizeLine
.x
;
4291 else // wxLC_ALIGN_LEFT
4293 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4297 if ( HasFlag(wxLC_ALIGN_TOP
) )
4299 // traverse the items again and tweak their sizes so that they are
4300 // all the same in a row
4301 for ( i
= 0; i
< count
; i
++ )
4303 wxListLineData
*line
= GetLine(i
);
4304 line
->m_gi
->ExtendWidth(widthMax
);
4312 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4313 (y
+ lineHeight
) / lineHeight
,
4314 GetScrollPos( wxHORIZONTAL
),
4315 GetScrollPos( wxVERTICAL
),
4319 else // "flowed" arrangement, the most complicated case
4321 // at first we try without any scrollbars, if the items don't fit into
4322 // the window, we recalculate after subtracting the space taken by the
4325 int entireWidth
= 0;
4327 for (int tries
= 0; tries
< 2; tries
++)
4329 entireWidth
= 2 * EXTRA_BORDER_X
;
4333 // Now we have decided that the items do not fit into the
4334 // client area, so we need a scrollbar
4335 entireWidth
+= SCROLL_UNIT_X
;
4338 int x
= EXTRA_BORDER_X
;
4339 int y
= EXTRA_BORDER_Y
;
4340 int maxWidthInThisRow
= 0;
4343 int currentlyVisibleLines
= 0;
4345 for (size_t i
= 0; i
< count
; i
++)
4347 currentlyVisibleLines
++;
4348 wxListLineData
*line
= GetLine( i
);
4349 line
->CalculateSize( &dc
, iconSpacing
);
4350 line
->SetPosition( x
, y
, iconSpacing
);
4352 wxSize sizeLine
= GetLineSize( i
);
4354 if ( maxWidthInThisRow
< sizeLine
.x
)
4355 maxWidthInThisRow
= sizeLine
.x
;
4358 if (currentlyVisibleLines
> m_linesPerPage
)
4359 m_linesPerPage
= currentlyVisibleLines
;
4361 if ( y
+ sizeLine
.y
>= clientHeight
)
4363 currentlyVisibleLines
= 0;
4365 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4366 x
+= maxWidthInThisRow
;
4367 entireWidth
+= maxWidthInThisRow
;
4368 maxWidthInThisRow
= 0;
4371 // We have reached the last item.
4372 if ( i
== count
- 1 )
4373 entireWidth
+= maxWidthInThisRow
;
4375 if ( (tries
== 0) &&
4376 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4378 clientHeight
-= wxSystemSettings::
4379 GetMetric(wxSYS_HSCROLL_Y
);
4384 if ( i
== count
- 1 )
4385 tries
= 1; // Everything fits, no second try required.
4393 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4395 GetScrollPos( wxHORIZONTAL
),
4404 // FIXME: why should we call it from here?
4411 void wxListMainWindow::RefreshAll()
4416 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4417 if ( headerWin
&& headerWin
->m_dirty
)
4419 headerWin
->m_dirty
= false;
4420 headerWin
->Refresh();
4424 void wxListMainWindow::UpdateCurrent()
4426 if ( !HasCurrent() && !IsEmpty() )
4430 long wxListMainWindow::GetNextItem( long item
,
4431 int WXUNUSED(geometry
),
4435 max
= GetItemCount();
4436 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4437 _T("invalid listctrl index in GetNextItem()") );
4439 // notice that we start with the next item (or the first one if item == -1)
4440 // and this is intentional to allow writing a simple loop to iterate over
4441 // all selected items
4444 // this is not an error because the index was OK initially,
4445 // just no such item
4452 size_t count
= GetItemCount();
4453 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4455 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4458 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4465 // ----------------------------------------------------------------------------
4467 // ----------------------------------------------------------------------------
4469 void wxListMainWindow::DeleteItem( long lindex
)
4471 size_t count
= GetItemCount();
4473 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4474 _T("invalid item index in DeleteItem") );
4476 size_t index
= (size_t)lindex
;
4478 // we don't need to adjust the index for the previous items
4479 if ( HasCurrent() && m_current
>= index
)
4481 // if the current item is being deleted, we want the next one to
4482 // become selected - unless there is no next one - so don't adjust
4483 // m_current in this case
4484 if ( m_current
!= index
|| m_current
== count
- 1 )
4488 if ( InReportView() )
4490 // mark the Column Max Width cache as dirty if the items in the line
4491 // we're deleting contain the Max Column Width
4492 wxListLineData
* const line
= GetLine(index
);
4493 wxListItemDataList::compatibility_iterator n
;
4494 wxListItemData
*itemData
;
4498 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4500 n
= line
->m_items
.Item( i
);
4501 itemData
= n
->GetData();
4502 itemData
->GetItem(item
);
4504 itemWidth
= GetItemWidthWithImage(&item
);
4506 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4507 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4510 ResetVisibleLinesRange();
4513 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4518 m_selStore
.OnItemDelete(index
);
4522 m_lines
.RemoveAt( index
);
4525 // we need to refresh the (vert) scrollbar as the number of items changed
4528 RefreshAfter(index
);
4531 void wxListMainWindow::DeleteColumn( int col
)
4533 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4535 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4538 delete node
->GetData();
4539 m_columns
.Erase( node
);
4543 // update all the items
4544 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4546 wxListLineData
* const line
= GetLine(i
);
4547 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4548 delete n
->GetData();
4549 line
->m_items
.Erase(n
);
4553 if ( InReportView() ) // we only cache max widths when in Report View
4555 delete m_aColWidths
.Item(col
);
4556 m_aColWidths
.RemoveAt(col
);
4559 // invalidate it as it has to be recalculated
4563 void wxListMainWindow::DoDeleteAllItems()
4566 // nothing to do - in particular, don't send the event
4571 // to make the deletion of all items faster, we don't send the
4572 // notifications for each item deletion in this case but only one event
4573 // for all of them: this is compatible with wxMSW and documented in
4574 // DeleteAllItems() description
4576 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4577 event
.SetEventObject( GetParent() );
4578 GetParent()->GetEventHandler()->ProcessEvent( event
);
4586 if ( InReportView() )
4588 ResetVisibleLinesRange();
4589 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4591 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4598 void wxListMainWindow::DeleteAllItems()
4602 RecalculatePositions();
4605 void wxListMainWindow::DeleteEverything()
4607 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4608 WX_CLEAR_ARRAY(m_aColWidths
);
4613 // ----------------------------------------------------------------------------
4614 // scanning for an item
4615 // ----------------------------------------------------------------------------
4617 void wxListMainWindow::EnsureVisible( long index
)
4619 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4620 _T("invalid index in EnsureVisible") );
4622 // We have to call this here because the label in question might just have
4623 // been added and its position is not known yet
4625 RecalculatePositions(true /* no refresh */);
4627 MoveToItem((size_t)index
);
4630 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4636 wxString str_upper
= str
.Upper();
4640 size_t count
= GetItemCount();
4641 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4643 wxListLineData
*line
= GetLine(i
);
4644 wxString line_upper
= line
->GetText(0).Upper();
4647 if (line_upper
== str_upper
)
4652 if (line_upper
.find(str_upper
) == 0)
4660 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4666 size_t count
= GetItemCount();
4667 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4669 wxListLineData
*line
= GetLine(i
);
4671 line
->GetItem( 0, item
);
4672 if (item
.m_data
== data
)
4679 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4682 GetVisibleLinesRange( &topItem
, NULL
);
4685 GetItemPosition( GetItemCount() - 1, p
);
4689 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4690 if ( id
>= 0 && id
< (long)GetItemCount() )
4696 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4698 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4700 size_t count
= GetItemCount();
4702 if ( InReportView() )
4704 size_t current
= y
/ GetLineHeight();
4705 if ( current
< count
)
4707 flags
= HitTestLine(current
, x
, y
);
4714 // TODO: optimize it too! this is less simple than for report view but
4715 // enumerating all items is still not a way to do it!!
4716 for ( size_t current
= 0; current
< count
; current
++ )
4718 flags
= HitTestLine(current
, x
, y
);
4727 // ----------------------------------------------------------------------------
4729 // ----------------------------------------------------------------------------
4731 void wxListMainWindow::InsertItem( wxListItem
&item
)
4733 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4735 int count
= GetItemCount();
4736 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4738 if (item
.m_itemId
> count
)
4739 item
.m_itemId
= count
;
4741 size_t id
= item
.m_itemId
;
4745 if ( InReportView() )
4747 ResetVisibleLinesRange();
4749 // calculate the width of the item and adjust the max column width
4750 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4751 int width
= GetItemWidthWithImage(&item
);
4752 item
.SetWidth(width
);
4753 if (width
> pWidthInfo
->nMaxWidth
)
4754 pWidthInfo
->nMaxWidth
= width
;
4757 wxListLineData
*line
= new wxListLineData(this);
4759 line
->SetItem( item
.m_col
, item
);
4761 m_lines
.Insert( line
, id
);
4765 // If an item is selected at or below the point of insertion, we need to
4766 // increment the member variables because the current row's index has gone
4768 if ( HasCurrent() && m_current
>= id
)
4771 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4773 RefreshLines(id
, GetItemCount() - 1);
4776 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4779 if ( InReportView() )
4781 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4782 item
.m_width
= GetTextLength( item
.m_text
);
4784 wxListHeaderData
*column
= new wxListHeaderData( item
);
4785 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4787 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4790 wxListHeaderDataList::compatibility_iterator
4791 node
= m_columns
.Item( col
);
4792 m_columns
.Insert( node
, column
);
4793 m_aColWidths
.Insert( colWidthInfo
, col
);
4797 m_columns
.Append( column
);
4798 m_aColWidths
.Add( colWidthInfo
);
4803 // update all the items
4804 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4806 wxListLineData
* const line
= GetLine(i
);
4807 wxListItemData
* const data
= new wxListItemData(this);
4809 line
->m_items
.Insert(col
, data
);
4811 line
->m_items
.Append(data
);
4815 // invalidate it as it has to be recalculated
4820 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4823 wxClientDC
dc(this);
4825 dc
.SetFont( GetFont() );
4827 if (item
->GetImage() != -1)
4830 GetImageSize( item
->GetImage(), ix
, iy
);
4834 if (!item
->GetText().empty())
4837 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4844 // ----------------------------------------------------------------------------
4846 // ----------------------------------------------------------------------------
4848 wxListCtrlCompare list_ctrl_compare_func_2
;
4849 long list_ctrl_compare_data
;
4851 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4853 wxListLineData
*line1
= *arg1
;
4854 wxListLineData
*line2
= *arg2
;
4856 line1
->GetItem( 0, item
);
4857 wxUIntPtr data1
= item
.m_data
;
4858 line2
->GetItem( 0, item
);
4859 wxUIntPtr data2
= item
.m_data
;
4860 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4863 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4865 // selections won't make sense any more after sorting the items so reset
4867 HighlightAll(false);
4870 list_ctrl_compare_func_2
= fn
;
4871 list_ctrl_compare_data
= data
;
4872 m_lines
.Sort( list_ctrl_compare_func_1
);
4876 // ----------------------------------------------------------------------------
4878 // ----------------------------------------------------------------------------
4880 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4883 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4884 wxScrolledCanvas::OnScroll(event
);
4886 HandleOnScroll( event
);
4889 // update our idea of which lines are shown when we redraw the window the
4891 ResetVisibleLinesRange();
4893 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4895 wxGenericListCtrl
* lc
= GetListCtrl();
4896 wxCHECK_RET( lc
, _T("no listctrl window?") );
4898 lc
->m_headerWin
->Refresh();
4899 lc
->m_headerWin
->Update();
4903 int wxListMainWindow::GetCountPerPage() const
4905 if ( !m_linesPerPage
)
4907 wxConstCast(this, wxListMainWindow
)->
4908 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4911 return m_linesPerPage
;
4914 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4916 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4918 if ( m_lineFrom
== (size_t)-1 )
4920 size_t count
= GetItemCount();
4923 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4925 // this may happen if SetScrollbars() hadn't been called yet
4926 if ( m_lineFrom
>= count
)
4927 m_lineFrom
= count
- 1;
4929 // we redraw one extra line but this is needed to make the redrawing
4930 // logic work when there is a fractional number of lines on screen
4931 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4932 if ( m_lineTo
>= count
)
4933 m_lineTo
= count
- 1;
4935 else // empty control
4938 m_lineTo
= (size_t)-1;
4942 wxASSERT_MSG( IsEmpty() ||
4943 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4944 _T("GetVisibleLinesRange() returns incorrect result") );
4952 // -------------------------------------------------------------------------------------
4953 // wxGenericListCtrl
4954 // -------------------------------------------------------------------------------------
4956 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4958 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4959 EVT_SIZE(wxGenericListCtrl::OnSize
)
4962 wxGenericListCtrl::wxGenericListCtrl()
4964 m_imageListNormal
= (wxImageList
*) NULL
;
4965 m_imageListSmall
= (wxImageList
*) NULL
;
4966 m_imageListState
= (wxImageList
*) NULL
;
4968 m_ownsImageListNormal
=
4969 m_ownsImageListSmall
=
4970 m_ownsImageListState
= false;
4972 m_mainWin
= (wxListMainWindow
*) NULL
;
4973 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4977 wxGenericListCtrl::~wxGenericListCtrl()
4979 if (m_ownsImageListNormal
)
4980 delete m_imageListNormal
;
4981 if (m_ownsImageListSmall
)
4982 delete m_imageListSmall
;
4983 if (m_ownsImageListState
)
4984 delete m_imageListState
;
4987 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4991 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
4993 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4995 // we use 'g' to get the descent, too
4997 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4998 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
5001 // only update if changed
5002 if ( h
!= m_headerHeight
)
5007 ResizeReportView(true);
5008 else //why is this needed if it doesn't have a header?
5009 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
5014 void wxGenericListCtrl::CreateHeaderWindow()
5016 m_headerWin
= new wxListHeaderWindow
5018 this, wxID_ANY
, m_mainWin
,
5020 wxSize(GetClientSize().x
, m_headerHeight
),
5023 CalculateAndSetHeaderHeight();
5026 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5031 const wxValidator
&validator
,
5032 const wxString
&name
)
5036 m_imageListState
= (wxImageList
*) NULL
;
5037 m_ownsImageListNormal
=
5038 m_ownsImageListSmall
=
5039 m_ownsImageListState
= false;
5041 m_mainWin
= (wxListMainWindow
*) NULL
;
5042 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5046 if ( !(style
& wxLC_MASK_TYPE
) )
5048 style
= style
| wxLC_LIST
;
5051 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5054 // this window itself shouldn't get the focus, only m_mainWin should
5057 // don't create the inner window with the border
5058 style
&= ~wxBORDER_MASK
;
5060 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5062 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
5063 // Human Interface Guidelines ask us for a special font in this case
5064 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5067 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5072 if ( InReportView() )
5074 CreateHeaderWindow();
5076 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
5080 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5081 m_headerWin
->SetFont( font
);
5082 CalculateAndSetHeaderHeight();
5086 if ( HasFlag(wxLC_NO_HEADER
) )
5087 // VZ: why do we create it at all then?
5088 m_headerWin
->Show( false );
5091 SetInitialSize(size
);
5096 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5098 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5099 _T("wxLC_VIRTUAL can't be [un]set") );
5101 long flag
= GetWindowStyle();
5105 if (style
& wxLC_MASK_TYPE
)
5106 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5107 if (style
& wxLC_MASK_ALIGN
)
5108 flag
&= ~wxLC_MASK_ALIGN
;
5109 if (style
& wxLC_MASK_SORT
)
5110 flag
&= ~wxLC_MASK_SORT
;
5118 // some styles can be set without recreating everything (as happens in
5119 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5120 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
5123 wxWindow::SetWindowStyleFlag(flag
);
5127 SetWindowStyleFlag( flag
);
5131 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5135 m_mainWin
->DeleteEverything();
5137 // has the header visibility changed?
5138 bool hasHeader
= HasHeader();
5139 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5141 if ( hasHeader
!= willHaveHeader
)
5148 // don't delete, just hide, as we can reuse it later
5149 m_headerWin
->Show(false);
5151 //else: nothing to do
5153 else // must show header
5157 CreateHeaderWindow();
5159 else // already have it, just show
5161 m_headerWin
->Show( true );
5165 ResizeReportView(willHaveHeader
);
5169 wxWindow::SetWindowStyleFlag( flag
);
5172 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5174 m_mainWin
->GetColumn( col
, item
);
5178 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5180 m_mainWin
->SetColumn( col
, item
);
5184 int wxGenericListCtrl::GetColumnWidth( int col
) const
5186 return m_mainWin
->GetColumnWidth( col
);
5189 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5191 m_mainWin
->SetColumnWidth( col
, width
);
5195 int wxGenericListCtrl::GetCountPerPage() const
5197 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5200 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5202 m_mainWin
->GetItem( info
);
5206 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5208 m_mainWin
->SetItem( info
);
5212 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5215 info
.m_text
= label
;
5216 info
.m_mask
= wxLIST_MASK_TEXT
;
5217 info
.m_itemId
= index
;
5221 info
.m_image
= imageId
;
5222 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5225 m_mainWin
->SetItem(info
);
5229 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5231 return m_mainWin
->GetItemState( item
, stateMask
);
5234 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5236 m_mainWin
->SetItemState( item
, state
, stateMask
);
5241 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5243 return SetItemColumnImage(item
, 0, image
);
5247 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5250 info
.m_image
= image
;
5251 info
.m_mask
= wxLIST_MASK_IMAGE
;
5252 info
.m_itemId
= item
;
5253 info
.m_col
= column
;
5254 m_mainWin
->SetItem( info
);
5258 wxString
wxGenericListCtrl::GetItemText( long item
) const
5260 return m_mainWin
->GetItemText(item
);
5263 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5265 m_mainWin
->SetItemText(item
, str
);
5268 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5271 info
.m_mask
= wxLIST_MASK_DATA
;
5272 info
.m_itemId
= item
;
5273 m_mainWin
->GetItem( info
);
5277 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5280 info
.m_mask
= wxLIST_MASK_DATA
;
5281 info
.m_itemId
= item
;
5283 m_mainWin
->SetItem( info
);
5287 wxRect
wxGenericListCtrl::GetViewRect() const
5289 return m_mainWin
->GetViewRect();
5292 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
5294 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
5297 bool wxGenericListCtrl::GetSubItemRect(long item
,
5300 int WXUNUSED(code
)) const
5302 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
5305 if ( m_mainWin
->HasHeader() )
5306 rect
.y
+= m_headerHeight
+ 1;
5311 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5313 m_mainWin
->GetItemPosition( item
, pos
);
5317 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5322 int wxGenericListCtrl::GetItemCount() const
5324 return m_mainWin
->GetItemCount();
5327 int wxGenericListCtrl::GetColumnCount() const
5329 return m_mainWin
->GetColumnCount();
5332 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5334 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5337 wxSize
wxGenericListCtrl::GetItemSpacing() const
5339 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5341 return wxSize(spacing
, spacing
);
5344 #if WXWIN_COMPATIBILITY_2_6
5345 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5347 return m_mainWin
->GetItemSpacing( isSmall
);
5349 #endif // WXWIN_COMPATIBILITY_2_6
5351 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5354 info
.m_itemId
= item
;
5355 info
.SetTextColour( col
);
5356 m_mainWin
->SetItem( info
);
5359 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5362 info
.m_itemId
= item
;
5363 m_mainWin
->GetItem( info
);
5364 return info
.GetTextColour();
5367 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5370 info
.m_itemId
= item
;
5371 info
.SetBackgroundColour( col
);
5372 m_mainWin
->SetItem( info
);
5375 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5378 info
.m_itemId
= item
;
5379 m_mainWin
->GetItem( info
);
5380 return info
.GetBackgroundColour();
5383 int wxGenericListCtrl::GetScrollPos( int orient
) const
5385 return m_mainWin
->GetScrollPos( orient
);
5388 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5390 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5393 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5396 info
.m_itemId
= item
;
5398 m_mainWin
->SetItem( info
);
5401 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5404 info
.m_itemId
= item
;
5405 m_mainWin
->GetItem( info
);
5406 return info
.GetFont();
5409 int wxGenericListCtrl::GetSelectedItemCount() const
5411 return m_mainWin
->GetSelectedItemCount();
5414 wxColour
wxGenericListCtrl::GetTextColour() const
5416 return GetForegroundColour();
5419 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5421 SetForegroundColour(col
);
5424 long wxGenericListCtrl::GetTopItem() const
5427 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5431 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5433 return m_mainWin
->GetNextItem( item
, geom
, state
);
5436 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5438 if (which
== wxIMAGE_LIST_NORMAL
)
5439 return m_imageListNormal
;
5440 else if (which
== wxIMAGE_LIST_SMALL
)
5441 return m_imageListSmall
;
5442 else if (which
== wxIMAGE_LIST_STATE
)
5443 return m_imageListState
;
5445 return (wxImageList
*) NULL
;
5448 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5450 if ( which
== wxIMAGE_LIST_NORMAL
)
5452 if (m_ownsImageListNormal
)
5453 delete m_imageListNormal
;
5454 m_imageListNormal
= imageList
;
5455 m_ownsImageListNormal
= false;
5457 else if ( which
== wxIMAGE_LIST_SMALL
)
5459 if (m_ownsImageListSmall
)
5460 delete m_imageListSmall
;
5461 m_imageListSmall
= imageList
;
5462 m_ownsImageListSmall
= false;
5464 else if ( which
== wxIMAGE_LIST_STATE
)
5466 if (m_ownsImageListState
)
5467 delete m_imageListState
;
5468 m_imageListState
= imageList
;
5469 m_ownsImageListState
= false;
5472 m_mainWin
->SetImageList( imageList
, which
);
5475 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5477 SetImageList(imageList
, which
);
5478 if ( which
== wxIMAGE_LIST_NORMAL
)
5479 m_ownsImageListNormal
= true;
5480 else if ( which
== wxIMAGE_LIST_SMALL
)
5481 m_ownsImageListSmall
= true;
5482 else if ( which
== wxIMAGE_LIST_STATE
)
5483 m_ownsImageListState
= true;
5486 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5491 bool wxGenericListCtrl::DeleteItem( long item
)
5493 m_mainWin
->DeleteItem( item
);
5497 bool wxGenericListCtrl::DeleteAllItems()
5499 m_mainWin
->DeleteAllItems();
5503 bool wxGenericListCtrl::DeleteAllColumns()
5505 size_t count
= m_mainWin
->m_columns
.GetCount();
5506 for ( size_t n
= 0; n
< count
; n
++ )
5511 void wxGenericListCtrl::ClearAll()
5513 m_mainWin
->DeleteEverything();
5516 bool wxGenericListCtrl::DeleteColumn( int col
)
5518 m_mainWin
->DeleteColumn( col
);
5520 // if we don't have the header any longer, we need to relayout the window
5521 if ( !GetColumnCount() )
5522 ResizeReportView(false /* no header */);
5526 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5527 wxClassInfo
* textControlClass
)
5529 return m_mainWin
->EditLabel( item
, textControlClass
);
5532 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5534 return m_mainWin
->GetEditControl();
5537 bool wxGenericListCtrl::EnsureVisible( long item
)
5539 m_mainWin
->EnsureVisible( item
);
5543 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5545 return m_mainWin
->FindItem( start
, str
, partial
);
5548 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5550 return m_mainWin
->FindItem( start
, data
);
5553 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5554 int WXUNUSED(direction
))
5556 return m_mainWin
->FindItem( pt
);
5559 // TODO: sub item hit testing
5560 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5562 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5565 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5567 m_mainWin
->InsertItem( info
);
5568 return info
.m_itemId
;
5571 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5574 info
.m_text
= label
;
5575 info
.m_mask
= wxLIST_MASK_TEXT
;
5576 info
.m_itemId
= index
;
5577 return InsertItem( info
);
5580 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5583 info
.m_mask
= wxLIST_MASK_IMAGE
;
5584 info
.m_image
= imageIndex
;
5585 info
.m_itemId
= index
;
5586 return InsertItem( info
);
5589 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5592 info
.m_text
= label
;
5593 info
.m_image
= imageIndex
;
5594 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5595 info
.m_itemId
= index
;
5596 return InsertItem( info
);
5599 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5601 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5603 m_mainWin
->InsertColumn( col
, item
);
5605 // if we hadn't had a header before but have one now
5606 // then we need to relayout the window
5607 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5608 ResizeReportView(true /* have header */);
5610 m_headerWin
->Refresh();
5615 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5616 int format
, int width
)
5619 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5620 item
.m_text
= heading
;
5623 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5624 item
.m_width
= width
;
5627 item
.m_format
= format
;
5629 return InsertColumn( col
, item
);
5632 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5634 return m_mainWin
->ScrollList(dx
, dy
);
5638 // fn is a function which takes 3 long arguments: item1, item2, data.
5639 // item1 is the long data associated with a first item (NOT the index).
5640 // item2 is the long data associated with a second item (NOT the index).
5641 // data is the same value as passed to SortItems.
5642 // The return value is a negative number if the first item should precede the second
5643 // item, a positive number of the second item should precede the first,
5644 // or zero if the two items are equivalent.
5645 // data is arbitrary data to be passed to the sort function.
5647 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5649 m_mainWin
->SortItems( fn
, data
);
5653 // ----------------------------------------------------------------------------
5655 // ----------------------------------------------------------------------------
5657 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5662 ResizeReportView(m_mainWin
->HasHeader());
5663 m_mainWin
->RecalculatePositions();
5666 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5669 GetClientSize( &cw
, &ch
);
5673 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5674 if(ch
> m_headerHeight
)
5675 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5676 cw
, ch
- m_headerHeight
- 1 );
5678 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5681 else // no header window
5683 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5687 void wxGenericListCtrl::OnInternalIdle()
5689 wxWindow::OnInternalIdle();
5691 // do it only if needed
5692 if ( !m_mainWin
->m_dirty
)
5695 m_mainWin
->RecalculatePositions();
5698 // ----------------------------------------------------------------------------
5700 // ----------------------------------------------------------------------------
5702 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5706 m_mainWin
->SetBackgroundColour( colour
);
5707 m_mainWin
->m_dirty
= true;
5713 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5715 if ( !wxWindow::SetForegroundColour( colour
) )
5720 m_mainWin
->SetForegroundColour( colour
);
5721 m_mainWin
->m_dirty
= true;
5725 m_headerWin
->SetForegroundColour( colour
);
5730 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5732 if ( !wxWindow::SetFont( font
) )
5737 m_mainWin
->SetFont( font
);
5738 m_mainWin
->m_dirty
= true;
5743 m_headerWin
->SetFont( font
);
5744 CalculateAndSetHeaderHeight();
5754 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5757 // Use the same color scheme as wxListBox
5758 return wxListBox::GetClassDefaultAttributes(variant
);
5760 wxUnusedVar(variant
);
5761 wxVisualAttributes attr
;
5762 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5763 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5764 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5769 // ----------------------------------------------------------------------------
5770 // methods forwarded to m_mainWin
5771 // ----------------------------------------------------------------------------
5773 #if wxUSE_DRAG_AND_DROP
5775 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5777 m_mainWin
->SetDropTarget( dropTarget
);
5780 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5782 return m_mainWin
->GetDropTarget();
5787 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5789 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5792 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5794 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5797 wxColour
wxGenericListCtrl::GetForegroundColour() const
5799 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5802 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5805 return m_mainWin
->PopupMenu( menu
, x
, y
);
5811 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5813 m_mainWin
->DoClientToScreen(x
, y
);
5816 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5818 m_mainWin
->DoScreenToClient(x
, y
);
5821 void wxGenericListCtrl::SetFocus()
5823 // The test in window.cpp fails as we are a composite
5824 // window, so it checks against "this", but not m_mainWin.
5825 if ( DoFindFocus() != this )
5826 m_mainWin
->SetFocus();
5829 wxSize
wxGenericListCtrl::DoGetBestSize() const
5831 // Something is better than nothing...
5832 // 100x80 is what the MSW version will get from the default
5833 // wxControl::DoGetBestSize
5834 return wxSize(100, 80);
5837 // ----------------------------------------------------------------------------
5838 // virtual list control support
5839 // ----------------------------------------------------------------------------
5841 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5843 // this is a pure virtual function, in fact - which is not really pure
5844 // because the controls which are not virtual don't need to implement it
5845 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5847 return wxEmptyString
;
5850 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5852 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5854 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5858 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5861 return OnGetItemImage(item
);
5867 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5869 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5870 _T("invalid item index in OnGetItemAttr()") );
5872 // no attributes by default
5876 void wxGenericListCtrl::SetItemCount(long count
)
5878 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5880 m_mainWin
->SetItemCount(count
);
5883 void wxGenericListCtrl::RefreshItem(long item
)
5885 m_mainWin
->RefreshLine(item
);
5888 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5890 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5893 // Generic wxListCtrl is more or less a container for two other
5894 // windows which drawings are done upon. These are namely
5895 // 'm_headerWin' and 'm_mainWin'.
5896 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5897 // behaviour wxListCtrl has under wxMSW.
5899 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5903 // The easy case, no rectangle specified.
5905 m_headerWin
->Refresh(eraseBackground
);
5908 m_mainWin
->Refresh(eraseBackground
);
5912 // Refresh the header window
5915 wxRect rectHeader
= m_headerWin
->GetRect();
5916 rectHeader
.Intersect(*rect
);
5917 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5920 m_headerWin
->GetPosition(&x
, &y
);
5921 rectHeader
.Offset(-x
, -y
);
5922 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5926 // Refresh the main window
5929 wxRect rectMain
= m_mainWin
->GetRect();
5930 rectMain
.Intersect(*rect
);
5931 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5934 m_mainWin
->GetPosition(&x
, &y
);
5935 rectMain
.Offset(-x
, -y
);
5936 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5942 #endif // wxUSE_LISTCTRL