1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #include "wx/selstore.h"
73 #include "wx/renderer.h"
76 #include "wx/mac/private.h"
82 // NOTE: If using the wxListBox visual attributes works everywhere then this can
83 // be removed, as well as the #else case below.
84 #define _USE_VISATTR 0
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
112 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // // the height of the header window (FIXME: should depend on its font!)
119 // static const int HEADER_HEIGHT = 23;
121 static const int SCROLL_UNIT_X
= 15;
123 // the spacing between the lines (in report mode)
124 static const int LINE_SPACING
= 0;
126 // extra margins around the text label
127 static const int EXTRA_WIDTH
= 4;
128 static const int EXTRA_HEIGHT
= 4;
130 // margin between the window and the items
131 static const int EXTRA_BORDER_X
= 2;
132 static const int EXTRA_BORDER_Y
= 2;
134 // offset for the header window
135 static const int HEADER_OFFSET_X
= 1;
136 static const int HEADER_OFFSET_Y
= 1;
138 // margin between rows of icons in [small] icon view
139 static const int MARGIN_BETWEEN_ROWS
= 6;
141 // when autosizing the columns, add some slack
142 static const int AUTOSIZE_COL_MARGIN
= 10;
144 // default and minimal widths for the header columns
145 static const int WIDTH_COL_DEFAULT
= 80;
146 static const int WIDTH_COL_MIN
= 10;
148 // the space between the image and the text in the report mode
149 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
151 // ============================================================================
153 // ============================================================================
155 //-----------------------------------------------------------------------------
156 // wxListItemData (internal)
157 //-----------------------------------------------------------------------------
159 class WXDLLEXPORT wxListItemData
162 wxListItemData(wxListMainWindow
*owner
);
165 void SetItem( const wxListItem
&info
);
166 void SetImage( int image
) { m_image
= image
; }
167 void SetData( long data
) { m_data
= data
; }
168 void SetPosition( int x
, int y
);
169 void SetSize( int width
, int height
);
171 bool HasText() const { return !m_text
.empty(); }
172 const wxString
& GetText() const { return m_text
; }
173 void SetText(const wxString
& text
) { m_text
= text
; }
175 // we can't use empty string for measuring the string width/height, so
176 // always return something
177 wxString
GetTextForMeasuring() const
179 wxString s
= GetText();
186 bool IsHit( int x
, int y
) const;
190 int GetWidth() const;
191 int GetHeight() const;
193 int GetImage() const { return m_image
; }
194 bool HasImage() const { return GetImage() != -1; }
196 void GetItem( wxListItem
&info
) const;
198 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
199 wxListItemAttr
*GetAttr() const { return m_attr
; }
202 // the item image or -1
205 // user data associated with the item
208 // the item coordinates are not used in report mode, instead this pointer
209 // is NULL and the owner window is used to retrieve the item position and
213 // the list ctrl we are in
214 wxListMainWindow
*m_owner
;
216 // custom attributes or NULL
217 wxListItemAttr
*m_attr
;
220 // common part of all ctors
226 //-----------------------------------------------------------------------------
227 // wxListHeaderData (internal)
228 //-----------------------------------------------------------------------------
230 class WXDLLEXPORT wxListHeaderData
: public wxObject
234 wxListHeaderData( const wxListItem
&info
);
235 void SetItem( const wxListItem
&item
);
236 void SetPosition( int x
, int y
);
237 void SetWidth( int w
);
238 void SetFormat( int format
);
239 void SetHeight( int h
);
240 bool HasImage() const;
242 bool HasText() const { return !m_text
.empty(); }
243 const wxString
& GetText() const { return m_text
; }
244 void SetText(const wxString
& text
) { m_text
= text
; }
246 void GetItem( wxListItem
&item
);
248 bool IsHit( int x
, int y
) const;
249 int GetImage() const;
250 int GetWidth() const;
251 int GetFormat() const;
267 //-----------------------------------------------------------------------------
268 // wxListLineData (internal)
269 //-----------------------------------------------------------------------------
271 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
272 #include "wx/listimpl.cpp"
273 WX_DEFINE_LIST(wxListItemDataList
);
278 // the list of subitems: only may have more than one item in report mode
279 wxListItemDataList m_items
;
281 // this is not used in report view
293 // the part to be highlighted
294 wxRect m_rectHighlight
;
296 // extend all our rects to be centered inside theo ne of given width
297 void ExtendWidth(wxCoord w
)
299 wxASSERT_MSG( m_rectAll
.width
<= w
,
300 _T("width can only be increased") );
303 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
)/2;
304 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
)/2;
305 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
)/2;
309 // is this item selected? [NB: not used in virtual mode]
312 // back pointer to the list ctrl
313 wxListMainWindow
*m_owner
;
316 wxListLineData(wxListMainWindow
*owner
);
320 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
324 // are we in report mode?
325 inline bool InReportView() const;
327 // are we in virtual report mode?
328 inline bool IsVirtual() const;
330 // these 2 methods shouldn't be called for report view controls, in that
331 // case we determine our position/size ourselves
333 // calculate the size of the line
334 void CalculateSize( wxDC
*dc
, int spacing
);
336 // remember the position this line appears at
337 void SetPosition( int x
, int y
, int spacing
);
341 void SetImage( int image
) { SetImage(0, image
); }
342 int GetImage() const { return GetImage(0); }
343 bool HasImage() const { return GetImage() != -1; }
344 bool HasText() const { return !GetText(0).empty(); }
346 void SetItem( int index
, const wxListItem
&info
);
347 void GetItem( int index
, wxListItem
&info
);
349 wxString
GetText(int index
) const;
350 void SetText( int index
, const wxString s
);
352 wxListItemAttr
*GetAttr() const;
353 void SetAttr(wxListItemAttr
*attr
);
355 // return true if the highlighting really changed
356 bool Highlight( bool on
);
358 void ReverseHighlight();
360 bool IsHighlighted() const
362 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
364 return m_highlighted
;
367 // draw the line on the given DC in icon/list mode
368 void Draw( wxDC
*dc
);
370 // the same in report mode
371 void DrawInReportMode( wxDC
*dc
,
373 const wxRect
& rectHL
,
377 // set the line to contain num items (only can be > 1 in report mode)
378 void InitItems( int num
);
380 // get the mode (i.e. style) of the list control
381 inline int GetMode() const;
383 // prepare the DC for drawing with these item's attributes, return true if
384 // we need to draw the items background to highlight it, false otherwise
385 bool SetAttributes(wxDC
*dc
,
386 const wxListItemAttr
*attr
,
389 // draw the text on the DC with the correct justification; also add an
390 // ellipsis if the text is too large to fit in the current width
391 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
393 // these are only used by GetImage/SetImage above, we don't support images
394 // with subitems at the public API level yet
395 void SetImage( int index
, int image
);
396 int GetImage( int index
) const;
399 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
400 #include "wx/arrimpl.cpp"
401 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
403 //-----------------------------------------------------------------------------
404 // wxListHeaderWindow (internal)
405 //-----------------------------------------------------------------------------
407 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
410 wxListMainWindow
*m_owner
;
411 wxCursor
*m_currentCursor
;
412 wxCursor
*m_resizeCursor
;
415 // column being resized or -1
418 // divider line position in logical (unscrolled) coords
421 // minimal position beyond which the divider line can't be dragged in
426 wxListHeaderWindow();
428 wxListHeaderWindow( wxWindow
*win
,
430 wxListMainWindow
*owner
,
431 const wxPoint
&pos
= wxDefaultPosition
,
432 const wxSize
&size
= wxDefaultSize
,
434 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
436 virtual ~wxListHeaderWindow();
439 void AdjustDC(wxDC
& dc
);
441 void OnPaint( wxPaintEvent
&event
);
442 void OnMouse( wxMouseEvent
&event
);
443 void OnSetFocus( wxFocusEvent
&event
);
449 // common part of all ctors
452 // generate and process the list event of the given type, return true if
453 // it wasn't vetoed, i.e. if we should proceed
454 bool SendListEvent(wxEventType type
, wxPoint pos
);
456 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
457 DECLARE_EVENT_TABLE()
460 //-----------------------------------------------------------------------------
461 // wxListRenameTimer (internal)
462 //-----------------------------------------------------------------------------
464 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
467 wxListMainWindow
*m_owner
;
470 wxListRenameTimer( wxListMainWindow
*owner
);
474 //-----------------------------------------------------------------------------
475 // wxListTextCtrl (internal)
476 //-----------------------------------------------------------------------------
478 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
481 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
484 void OnChar( wxKeyEvent
&event
);
485 void OnKeyUp( wxKeyEvent
&event
);
486 void OnKillFocus( wxFocusEvent
&event
);
488 bool AcceptChanges();
492 wxListMainWindow
*m_owner
;
493 wxString m_startValue
;
497 DECLARE_EVENT_TABLE()
500 //-----------------------------------------------------------------------------
501 // wxListMainWindow (internal)
502 //-----------------------------------------------------------------------------
504 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
505 #include "wx/listimpl.cpp"
506 WX_DEFINE_LIST(wxListHeaderDataList
);
508 class wxListMainWindow
: public wxScrolledWindow
512 wxListMainWindow( wxWindow
*parent
,
514 const wxPoint
& pos
= wxDefaultPosition
,
515 const wxSize
& size
= wxDefaultSize
,
517 const wxString
&name
= _T("listctrlmainwindow") );
519 virtual ~wxListMainWindow();
521 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
523 // return true if this is a virtual list control
524 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
526 // return true if the control is in report mode
527 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
529 // return true if we are in single selection mode, false if multi sel
530 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
532 // do we have a header window?
533 bool HasHeader() const
534 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
536 void HighlightAll( bool on
);
538 // all these functions only do something if the line is currently visible
540 // change the line "selected" state, return true if it really changed
541 bool HighlightLine( size_t line
, bool highlight
= true);
543 // as HighlightLine() but do it for the range of lines: this is incredibly
544 // more efficient for virtual list controls!
546 // NB: unlike HighlightLine() this one does refresh the lines on screen
547 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
549 // toggle the line state and refresh it
550 void ReverseHighlight( size_t line
)
551 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
553 // return true if the line is highlighted
554 bool IsHighlighted(size_t line
) const;
556 // refresh one or several lines at once
557 void RefreshLine( size_t line
);
558 void RefreshLines( size_t lineFrom
, size_t lineTo
);
560 // refresh all selected items
561 void RefreshSelected();
563 // refresh all lines below the given one: the difference with
564 // RefreshLines() is that the index here might not be a valid one (happens
565 // when the last line is deleted)
566 void RefreshAfter( size_t lineFrom
);
568 // the methods which are forwarded to wxListLineData itself in list/icon
569 // modes but are here because the lines don't store their positions in the
572 // get the bound rect for the entire line
573 wxRect
GetLineRect(size_t line
) const;
575 // get the bound rect of the label
576 wxRect
GetLineLabelRect(size_t line
) const;
578 // get the bound rect of the items icon (only may be called if we do have
580 wxRect
GetLineIconRect(size_t line
) const;
582 // get the rect to be highlighted when the item has focus
583 wxRect
GetLineHighlightRect(size_t line
) const;
585 // get the size of the total line rect
586 wxSize
GetLineSize(size_t line
) const
587 { return GetLineRect(line
).GetSize(); }
589 // return the hit code for the corresponding position (in this line)
590 long HitTestLine(size_t line
, int x
, int y
) const;
592 // bring the selected item into view, scrolling to it if necessary
593 void MoveToItem(size_t item
);
595 // bring the current item into view
596 void MoveToFocus() { MoveToItem(m_current
); }
598 // start editing the label of the given item
599 void EditLabel( long item
);
601 // suspend/resume redrawing the control
607 void OnRenameTimer();
608 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
609 void OnRenameCancelled(size_t itemEdit
);
611 void OnMouse( wxMouseEvent
&event
);
613 // called to switch the selection from the current item to newCurrent,
614 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
616 void OnChar( wxKeyEvent
&event
);
617 void OnKeyDown( wxKeyEvent
&event
);
618 void OnSetFocus( wxFocusEvent
&event
);
619 void OnKillFocus( wxFocusEvent
&event
);
620 void OnScroll(wxScrollWinEvent
& event
) ;
622 void OnPaint( wxPaintEvent
&event
);
624 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
625 void GetImageSize( int index
, int &width
, int &height
) const;
626 int GetTextLength( const wxString
&s
) const;
628 void SetImageList( wxImageListType
*imageList
, int which
);
629 void SetItemSpacing( int spacing
, bool isSmall
= false );
630 int GetItemSpacing( bool isSmall
= false );
632 void SetColumn( int col
, wxListItem
&item
);
633 void SetColumnWidth( int col
, int width
);
634 void GetColumn( int col
, wxListItem
&item
) const;
635 int GetColumnWidth( int col
) const;
636 int GetColumnCount() const { return m_columns
.GetCount(); }
638 // returns the sum of the heights of all columns
639 int GetHeaderWidth() const;
641 int GetCountPerPage() const;
643 void SetItem( wxListItem
&item
);
644 void GetItem( wxListItem
&item
) const;
645 void SetItemState( long item
, long state
, long stateMask
);
646 int GetItemState( long item
, long stateMask
) const;
647 void GetItemRect( long index
, wxRect
&rect
) const;
648 wxRect
GetViewRect() const;
649 bool GetItemPosition( long item
, wxPoint
& pos
) const;
650 int GetSelectedItemCount() const;
652 wxString
GetItemText(long item
) const
655 info
.m_itemId
= item
;
660 void SetItemText(long item
, const wxString
& value
)
663 info
.m_mask
= wxLIST_MASK_TEXT
;
664 info
.m_itemId
= item
;
669 // set the scrollbars and update the positions of the items
670 void RecalculatePositions(bool noRefresh
= false);
672 // refresh the window and the header
675 long GetNextItem( long item
, int geometry
, int state
) const;
676 void DeleteItem( long index
);
677 void DeleteAllItems();
678 void DeleteColumn( int col
);
679 void DeleteEverything();
680 void EnsureVisible( long index
);
681 long FindItem( long start
, const wxString
& str
, bool partial
= false );
682 long FindItem( long start
, long data
);
683 long FindItem( const wxPoint
& pt
);
684 long HitTest( int x
, int y
, int &flags
);
685 void InsertItem( wxListItem
&item
);
686 void InsertColumn( long col
, wxListItem
&item
);
687 void SortItems( wxListCtrlCompare fn
, long data
);
689 size_t GetItemCount() const;
690 bool IsEmpty() const { return GetItemCount() == 0; }
691 void SetItemCount(long count
);
693 // change the current (== focused) item, send a notification event
694 void ChangeCurrent(size_t current
);
695 void ResetCurrent() { ChangeCurrent((size_t)-1); }
696 bool HasCurrent() const { return m_current
!= (size_t)-1; }
698 // send out a wxListEvent
699 void SendNotify( size_t line
,
701 wxPoint point
= wxDefaultPosition
);
703 // override base class virtual to reset m_lineHeight when the font changes
704 virtual bool SetFont(const wxFont
& font
)
706 if ( !wxScrolledWindow::SetFont(font
) )
714 // these are for wxListLineData usage only
716 // get the backpointer to the list ctrl
717 wxGenericListCtrl
*GetListCtrl() const
719 return wxStaticCast(GetParent(), wxGenericListCtrl
);
722 // get the height of all lines (assuming they all do have the same height)
723 wxCoord
GetLineHeight() const;
725 // get the y position of the given line (only for report view)
726 wxCoord
GetLineY(size_t line
) const;
728 // get the brush to use for the item highlighting
729 wxBrush
*GetHighlightBrush() const
731 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
735 // the array of all line objects for a non virtual list control (for the
736 // virtual list control we only ever use m_lines[0])
737 wxListLineDataArray m_lines
;
739 // the list of column objects
740 wxListHeaderDataList m_columns
;
742 // currently focused item or -1
745 // the number of lines per page
748 // this flag is set when something which should result in the window
749 // redrawing happens (i.e. an item was added or deleted, or its appearance
750 // changed) and OnPaint() doesn't redraw the window while it is set which
751 // allows to minimize the number of repaintings when a lot of items are
752 // being added. The real repainting occurs only after the next OnIdle()
756 wxColour
*m_highlightColour
;
757 wxImageListType
*m_small_image_list
;
758 wxImageListType
*m_normal_image_list
;
760 int m_normal_spacing
;
764 wxTimer
*m_renameTimer
;
769 // for double click logic
770 size_t m_lineLastClicked
,
771 m_lineBeforeLastClicked
;
774 // the total count of items in a virtual list control
777 // the object maintaining the items selection state, only used in virtual
779 wxSelectionStore m_selStore
;
781 // common part of all ctors
784 // get the line data for the given index
785 wxListLineData
*GetLine(size_t n
) const
787 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
791 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
799 // get a dummy line which can be used for geometry calculations and such:
800 // you must use GetLine() if you want to really draw the line
801 wxListLineData
*GetDummyLine() const;
803 // cache the line data of the n-th line in m_lines[0]
804 void CacheLineData(size_t line
);
806 // get the range of visible lines
807 void GetVisibleLinesRange(size_t *from
, size_t *to
);
809 // force us to recalculate the range of visible lines
810 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
812 // get the colour to be used for drawing the rules
813 wxColour
GetRuleColour() const
818 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
823 // initialize the current item if needed
824 void UpdateCurrent();
826 // delete all items but don't refresh: called from dtor
827 void DoDeleteAllItems();
829 // the height of one line using the current font
830 wxCoord m_lineHeight
;
832 // the total header width or 0 if not calculated yet
833 wxCoord m_headerWidth
;
835 // the first and last lines being shown on screen right now (inclusive),
836 // both may be -1 if they must be calculated so never access them directly:
837 // use GetVisibleLinesRange() above instead
841 // the brushes to use for item highlighting when we do/don't have focus
842 wxBrush
*m_highlightBrush
,
843 *m_highlightUnfocusedBrush
;
845 // if this is > 0, the control is frozen and doesn't redraw itself
846 size_t m_freezeCount
;
848 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
849 DECLARE_EVENT_TABLE()
851 friend class wxGenericListCtrl
;
854 // ============================================================================
856 // ============================================================================
858 //-----------------------------------------------------------------------------
860 //-----------------------------------------------------------------------------
862 wxListItemData::~wxListItemData()
864 // in the virtual list control the attributes are managed by the main
865 // program, so don't delete them
866 if ( !m_owner
->IsVirtual() )
874 void wxListItemData::Init()
882 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
888 if ( owner
->InReportView() )
898 void wxListItemData::SetItem( const wxListItem
&info
)
900 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
901 SetText(info
.m_text
);
902 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
903 m_image
= info
.m_image
;
904 if ( info
.m_mask
& wxLIST_MASK_DATA
)
905 m_data
= info
.m_data
;
907 if ( info
.HasAttributes() )
910 *m_attr
= *info
.GetAttributes();
912 m_attr
= new wxListItemAttr(*info
.GetAttributes());
920 m_rect
->width
= info
.m_width
;
924 void wxListItemData::SetPosition( int x
, int y
)
926 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
932 void wxListItemData::SetSize( int width
, int height
)
934 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
937 m_rect
->width
= width
;
939 m_rect
->height
= height
;
942 bool wxListItemData::IsHit( int x
, int y
) const
944 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
946 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
949 int wxListItemData::GetX() const
951 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
956 int wxListItemData::GetY() const
958 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
963 int wxListItemData::GetWidth() const
965 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
967 return m_rect
->width
;
970 int wxListItemData::GetHeight() const
972 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
974 return m_rect
->height
;
977 void wxListItemData::GetItem( wxListItem
&info
) const
979 info
.m_text
= m_text
;
980 info
.m_image
= m_image
;
981 info
.m_data
= m_data
;
985 if ( m_attr
->HasTextColour() )
986 info
.SetTextColour(m_attr
->GetTextColour());
987 if ( m_attr
->HasBackgroundColour() )
988 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
989 if ( m_attr
->HasFont() )
990 info
.SetFont(m_attr
->GetFont());
994 //-----------------------------------------------------------------------------
996 //-----------------------------------------------------------------------------
998 void wxListHeaderData::Init()
1009 wxListHeaderData::wxListHeaderData()
1014 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1021 void wxListHeaderData::SetItem( const wxListItem
&item
)
1023 m_mask
= item
.m_mask
;
1025 if ( m_mask
& wxLIST_MASK_TEXT
)
1026 m_text
= item
.m_text
;
1028 if ( m_mask
& wxLIST_MASK_IMAGE
)
1029 m_image
= item
.m_image
;
1031 if ( m_mask
& wxLIST_MASK_FORMAT
)
1032 m_format
= item
.m_format
;
1034 if ( m_mask
& wxLIST_MASK_WIDTH
)
1035 SetWidth(item
.m_width
);
1038 void wxListHeaderData::SetPosition( int x
, int y
)
1044 void wxListHeaderData::SetHeight( int h
)
1049 void wxListHeaderData::SetWidth( int w
)
1053 m_width
= WIDTH_COL_DEFAULT
;
1054 else if (m_width
< WIDTH_COL_MIN
)
1055 m_width
= WIDTH_COL_MIN
;
1058 void wxListHeaderData::SetFormat( int format
)
1063 bool wxListHeaderData::HasImage() const
1065 return m_image
!= -1;
1068 bool wxListHeaderData::IsHit( int x
, int y
) const
1070 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1073 void wxListHeaderData::GetItem( wxListItem
& item
)
1075 item
.m_mask
= m_mask
;
1076 item
.m_text
= m_text
;
1077 item
.m_image
= m_image
;
1078 item
.m_format
= m_format
;
1079 item
.m_width
= m_width
;
1082 int wxListHeaderData::GetImage() const
1087 int wxListHeaderData::GetWidth() const
1092 int wxListHeaderData::GetFormat() const
1097 //-----------------------------------------------------------------------------
1099 //-----------------------------------------------------------------------------
1101 inline int wxListLineData::GetMode() const
1103 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1106 inline bool wxListLineData::InReportView() const
1108 return m_owner
->HasFlag(wxLC_REPORT
);
1111 inline bool wxListLineData::IsVirtual() const
1113 return m_owner
->IsVirtual();
1116 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1120 if ( InReportView() )
1126 m_gi
= new GeometryInfo
;
1129 m_highlighted
= false;
1131 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1134 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1136 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1137 wxCHECK_RET( node
, _T("no subitems at all??") );
1139 wxListItemData
*item
= node
->GetData();
1144 switch ( GetMode() )
1147 case wxLC_SMALL_ICON
:
1148 m_gi
->m_rectAll
.width
= spacing
;
1150 s
= item
->GetText();
1155 m_gi
->m_rectLabel
.width
=
1156 m_gi
->m_rectLabel
.height
= 0;
1160 dc
->GetTextExtent( s
, &lw
, &lh
);
1164 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1166 m_gi
->m_rectAll
.width
= lw
;
1168 m_gi
->m_rectLabel
.width
= lw
;
1169 m_gi
->m_rectLabel
.height
= lh
;
1172 if (item
->HasImage())
1175 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1176 m_gi
->m_rectIcon
.width
= w
+ 8;
1177 m_gi
->m_rectIcon
.height
= h
+ 8;
1179 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1180 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1181 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1182 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1185 if ( item
->HasText() )
1187 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1188 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1190 else // no text, highlight the icon
1192 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1193 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1198 s
= item
->GetTextForMeasuring();
1200 dc
->GetTextExtent( s
, &lw
, &lh
);
1204 m_gi
->m_rectLabel
.width
= lw
;
1205 m_gi
->m_rectLabel
.height
= lh
;
1207 m_gi
->m_rectAll
.width
= lw
;
1208 m_gi
->m_rectAll
.height
= lh
;
1210 if (item
->HasImage())
1213 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1214 m_gi
->m_rectIcon
.width
= w
;
1215 m_gi
->m_rectIcon
.height
= h
;
1217 m_gi
->m_rectAll
.width
+= 4 + w
;
1218 if (h
> m_gi
->m_rectAll
.height
)
1219 m_gi
->m_rectAll
.height
= h
;
1222 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1223 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1227 wxFAIL_MSG( _T("unexpected call to SetSize") );
1231 wxFAIL_MSG( _T("unknown mode") );
1235 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1237 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1238 wxCHECK_RET( node
, _T("no subitems at all??") );
1240 wxListItemData
*item
= node
->GetData();
1242 switch ( GetMode() )
1245 case wxLC_SMALL_ICON
:
1246 m_gi
->m_rectAll
.x
= x
;
1247 m_gi
->m_rectAll
.y
= y
;
1249 if ( item
->HasImage() )
1251 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1252 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1253 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1256 if ( item
->HasText() )
1258 if (m_gi
->m_rectAll
.width
> spacing
)
1259 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1261 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1262 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1263 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1264 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1266 else // no text, highlight the icon
1268 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1269 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1274 m_gi
->m_rectAll
.x
= x
;
1275 m_gi
->m_rectAll
.y
= y
;
1277 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1278 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1279 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1281 if (item
->HasImage())
1283 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1284 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1285 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1289 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1294 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1298 wxFAIL_MSG( _T("unknown mode") );
1302 void wxListLineData::InitItems( int num
)
1304 for (int i
= 0; i
< num
; i
++)
1305 m_items
.Append( new wxListItemData(m_owner
) );
1308 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1310 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1311 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1313 wxListItemData
*item
= node
->GetData();
1314 item
->SetItem( info
);
1317 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1319 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1322 wxListItemData
*item
= node
->GetData();
1323 item
->GetItem( info
);
1327 wxString
wxListLineData::GetText(int index
) const
1331 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1334 wxListItemData
*item
= node
->GetData();
1335 s
= item
->GetText();
1341 void wxListLineData::SetText( int index
, const wxString s
)
1343 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1346 wxListItemData
*item
= node
->GetData();
1351 void wxListLineData::SetImage( int index
, int image
)
1353 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1354 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1356 wxListItemData
*item
= node
->GetData();
1357 item
->SetImage(image
);
1360 int wxListLineData::GetImage( int index
) const
1362 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1363 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1365 wxListItemData
*item
= node
->GetData();
1366 return item
->GetImage();
1369 wxListItemAttr
*wxListLineData::GetAttr() const
1371 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1372 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1374 wxListItemData
*item
= node
->GetData();
1375 return item
->GetAttr();
1378 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1380 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1381 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1383 wxListItemData
*item
= node
->GetData();
1384 item
->SetAttr(attr
);
1387 bool wxListLineData::SetAttributes(wxDC
*dc
,
1388 const wxListItemAttr
*attr
,
1391 wxWindow
*listctrl
= m_owner
->GetParent();
1395 // don't use foreground colour for drawing highlighted items - this might
1396 // make them completely invisible (and there is no way to do bit
1397 // arithmetics on wxColour, unfortunately)
1401 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1405 if ( attr
&& attr
->HasTextColour() )
1407 colText
= attr
->GetTextColour();
1411 colText
= listctrl
->GetForegroundColour();
1415 dc
->SetTextForeground(colText
);
1419 if ( attr
&& attr
->HasFont() )
1421 font
= attr
->GetFont();
1425 font
= listctrl
->GetFont();
1431 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1432 if ( highlighted
|| hasBgCol
)
1436 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1440 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1443 dc
->SetPen( *wxTRANSPARENT_PEN
);
1451 void wxListLineData::Draw( wxDC
*dc
)
1453 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1454 wxCHECK_RET( node
, _T("no subitems at all??") );
1456 bool highlighted
= IsHighlighted();
1458 wxListItemAttr
*attr
= GetAttr();
1460 if ( SetAttributes(dc
, attr
, highlighted
) )
1462 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1465 // just for debugging to better see where the items are
1467 dc
->SetPen(*wxRED_PEN
);
1468 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1469 dc
->DrawRectangle( m_gi
->m_rectAll
);
1470 dc
->SetPen(*wxGREEN_PEN
);
1471 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1474 wxListItemData
*item
= node
->GetData();
1475 if (item
->HasImage())
1477 // centre the image inside our rectangle, this looks nicer when items
1478 // ae aligned in a row
1479 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1481 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1484 if (item
->HasText())
1486 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1488 wxDCClipper
clipper(*dc
, rectLabel
);
1489 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1493 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1495 const wxRect
& rectHL
,
1498 // TODO: later we should support setting different attributes for
1499 // different columns - to do it, just add "col" argument to
1500 // GetAttr() and move these lines into the loop below
1501 wxListItemAttr
*attr
= GetAttr();
1502 if ( SetAttributes(dc
, attr
, highlighted
) )
1504 dc
->DrawRectangle( rectHL
);
1507 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1508 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1511 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1513 node
= node
->GetNext(), col
++ )
1515 wxListItemData
*item
= node
->GetData();
1517 int width
= m_owner
->GetColumnWidth(col
);
1521 if ( item
->HasImage() )
1524 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1525 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1527 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1533 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1535 if ( item
->HasText() )
1537 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1542 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1543 const wxString
&text
,
1549 wxString drawntext
, ellipsis
;
1550 wxCoord w
, h
, base_w
;
1553 // determine if the string can fit inside the current width
1554 dc
->GetTextExtent(text
, &w
, &h
);
1557 // it can, draw it using the items alignment
1558 m_owner
->GetColumn(col
, item
);
1559 switch ( item
.GetAlign() )
1562 wxFAIL_MSG( _T("unknown list item format") );
1565 case wxLIST_FORMAT_LEFT
:
1569 case wxLIST_FORMAT_RIGHT
:
1573 case wxLIST_FORMAT_CENTER
:
1574 x
+= (width
- w
) / 2;
1578 dc
->DrawText(text
, x
, y
);
1580 else // otherwise, truncate and add an ellipsis if possible
1582 // determine the base width
1583 ellipsis
= wxString(wxT("..."));
1584 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1586 // continue until we have enough space or only one character left
1588 size_t len
= text
.Length();
1589 drawntext
= text
.Left(len
);
1592 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1593 drawntext
.RemoveLast();
1596 if (w
+ base_w
<= width
)
1600 // if still not enough space, remove ellipsis characters
1601 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1603 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1604 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1607 // now draw the text
1608 dc
->DrawText(drawntext
, x
, y
);
1609 dc
->DrawText(ellipsis
, x
+ w
, y
);
1613 bool wxListLineData::Highlight( bool on
)
1615 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1617 if ( on
== m_highlighted
)
1625 void wxListLineData::ReverseHighlight( void )
1627 Highlight(!IsHighlighted());
1630 //-----------------------------------------------------------------------------
1631 // wxListHeaderWindow
1632 //-----------------------------------------------------------------------------
1634 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1636 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1637 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1638 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1639 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1642 void wxListHeaderWindow::Init()
1644 m_currentCursor
= (wxCursor
*) NULL
;
1645 m_isDragging
= false;
1649 wxListHeaderWindow::wxListHeaderWindow()
1653 m_owner
= (wxListMainWindow
*) NULL
;
1654 m_resizeCursor
= (wxCursor
*) NULL
;
1657 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1659 wxListMainWindow
*owner
,
1663 const wxString
&name
)
1664 : wxWindow( win
, id
, pos
, size
, style
, name
)
1669 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1672 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1673 SetOwnForegroundColour( attr
.colFg
);
1674 SetOwnBackgroundColour( attr
.colBg
);
1676 SetOwnFont( attr
.font
);
1678 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1679 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1681 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1685 wxListHeaderWindow::~wxListHeaderWindow()
1687 delete m_resizeCursor
;
1690 #ifdef __WXUNIVERSAL__
1691 #include "wx/univ/renderer.h"
1692 #include "wx/univ/theme.h"
1695 // shift the DC origin to match the position of the main window horz
1696 // scrollbar: this allows us to always use logical coords
1697 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1700 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1703 m_owner
->GetViewStart( &x
, NULL
);
1705 // account for the horz scrollbar offset
1706 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1709 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1711 wxPaintDC
dc( this );
1718 dc
.SetFont( GetFont() );
1720 // width and height of the entire header window
1722 GetClientSize( &w
, &h
);
1723 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1725 dc
.SetBackgroundMode(wxTRANSPARENT
);
1727 dc
.SetTextForeground(GetForegroundColour());
1729 int x
= HEADER_OFFSET_X
;
1731 int numColumns
= m_owner
->GetColumnCount();
1733 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1735 m_owner
->GetColumn( i
, item
);
1736 int wCol
= item
.m_width
;
1738 // the width of the rect to draw: make it smaller to fit entirely
1739 // inside the column rect
1742 wxRendererNative::Get().DrawHeaderButton
1746 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1747 m_parent
->IsEnabled() ? 0
1748 : wxCONTROL_DISABLED
1751 // see if we have enough space for the column label
1753 // for this we need the width of the text
1756 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1757 wLabel
+= 2*EXTRA_WIDTH
;
1759 // and the width of the icon, if any
1760 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1761 int ix
= 0, // init them just to suppress the compiler warnings
1763 const int image
= item
.m_image
;
1764 wxImageListType
*imageList
;
1767 imageList
= m_owner
->m_small_image_list
;
1770 imageList
->GetSize(image
, ix
, iy
);
1771 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1779 // ignore alignment if there is not enough space anyhow
1781 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1784 wxFAIL_MSG( _T("unknown list item format") );
1787 case wxLIST_FORMAT_LEFT
:
1791 case wxLIST_FORMAT_RIGHT
:
1792 xAligned
= x
+ cw
- wLabel
;
1795 case wxLIST_FORMAT_CENTER
:
1796 xAligned
= x
+ (cw
- wLabel
) / 2;
1801 // if we have an image, draw it on the right of the label
1808 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1809 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1810 wxIMAGELIST_DRAW_TRANSPARENT
1813 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1816 // draw the text clipping it so that it doesn't overwrite the column
1818 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1820 dc
.DrawText( item
.GetText(),
1821 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1829 void wxListHeaderWindow::DrawCurrent()
1831 int x1
= m_currentX
;
1833 m_owner
->ClientToScreen( &x1
, &y1
);
1835 int x2
= m_currentX
;
1837 m_owner
->GetClientSize( NULL
, &y2
);
1838 m_owner
->ClientToScreen( &x2
, &y2
);
1841 dc
.SetLogicalFunction( wxINVERT
);
1842 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1843 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1847 dc
.DrawLine( x1
, y1
, x2
, y2
);
1849 dc
.SetLogicalFunction( wxCOPY
);
1851 dc
.SetPen( wxNullPen
);
1852 dc
.SetBrush( wxNullBrush
);
1855 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1857 // we want to work with logical coords
1859 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1860 int y
= event
.GetY();
1864 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1866 // we don't draw the line beyond our window, but we allow dragging it
1869 GetClientSize( &w
, NULL
);
1870 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1873 // erase the line if it was drawn
1874 if ( m_currentX
< w
)
1877 if (event
.ButtonUp())
1880 m_isDragging
= false;
1882 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1883 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1890 m_currentX
= m_minX
+ 7;
1892 // draw in the new location
1893 if ( m_currentX
< w
)
1897 else // not dragging
1900 bool hit_border
= false;
1902 // end of the current column
1905 // find the column where this event occured
1907 countCol
= m_owner
->GetColumnCount();
1908 for (col
= 0; col
< countCol
; col
++)
1910 xpos
+= m_owner
->GetColumnWidth( col
);
1913 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1915 // near the column border
1922 // inside the column
1929 if ( col
== countCol
)
1932 if (event
.LeftDown() || event
.RightUp())
1934 if (hit_border
&& event
.LeftDown())
1936 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1937 event
.GetPosition()) )
1939 m_isDragging
= true;
1944 //else: column resizing was vetoed by the user code
1946 else // click on a column
1948 SendListEvent( event
.LeftDown()
1949 ? wxEVT_COMMAND_LIST_COL_CLICK
1950 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1951 event
.GetPosition());
1954 else if (event
.Moving())
1959 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1960 m_currentCursor
= m_resizeCursor
;
1964 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1965 m_currentCursor
= wxSTANDARD_CURSOR
;
1969 SetCursor(*m_currentCursor
);
1974 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1976 m_owner
->SetFocus();
1980 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1982 wxWindow
*parent
= GetParent();
1983 wxListEvent
le( type
, parent
->GetId() );
1984 le
.SetEventObject( parent
);
1985 le
.m_pointDrag
= pos
;
1987 // the position should be relative to the parent window, not
1988 // this one for compatibility with MSW and common sense: the
1989 // user code doesn't know anything at all about this header
1990 // window, so why should it get positions relative to it?
1991 le
.m_pointDrag
.y
-= GetSize().y
;
1993 le
.m_col
= m_column
;
1994 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1997 //-----------------------------------------------------------------------------
1998 // wxListRenameTimer (internal)
1999 //-----------------------------------------------------------------------------
2001 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2006 void wxListRenameTimer::Notify()
2008 m_owner
->OnRenameTimer();
2011 //-----------------------------------------------------------------------------
2012 // wxListTextCtrl (internal)
2013 //-----------------------------------------------------------------------------
2015 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2016 EVT_CHAR (wxListTextCtrl::OnChar
)
2017 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2018 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2021 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2022 : m_startValue(owner
->GetItemText(itemEdit
)),
2023 m_itemEdited(itemEdit
)
2028 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2030 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2031 &rectLabel
.x
, &rectLabel
.y
);
2033 (void)Create(owner
, wxID_ANY
, m_startValue
,
2034 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2035 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2038 void wxListTextCtrl::Finish()
2042 wxPendingDelete
.Append(this);
2046 m_owner
->SetFocus();
2050 bool wxListTextCtrl::AcceptChanges()
2052 const wxString value
= GetValue();
2054 if ( value
== m_startValue
)
2056 // nothing changed, always accept
2060 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2062 // vetoed by the user
2066 // accepted, do rename the item
2067 m_owner
->SetItemText(m_itemEdited
, value
);
2072 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2074 switch ( event
.m_keyCode
)
2077 if ( !AcceptChanges() )
2079 // vetoed by the user code
2082 //else: fall through
2086 m_owner
->OnRenameCancelled( m_itemEdited
);
2094 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2102 // auto-grow the textctrl:
2103 wxSize parentSize
= m_owner
->GetSize();
2104 wxPoint myPos
= GetPosition();
2105 wxSize mySize
= GetSize();
2107 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2108 if (myPos
.x
+ sx
> parentSize
.x
)
2109 sx
= parentSize
.x
- myPos
.x
;
2112 SetSize(sx
, wxDefaultCoord
);
2117 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2121 // We must finish regardless of success, otherwise we'll get
2125 if ( !AcceptChanges() )
2126 m_owner
->OnRenameCancelled( m_itemEdited
);
2129 // We must let the native text control handle focus, too, otherwise
2130 // it could have problems with the cursor (e.g., in wxGTK):
2134 //-----------------------------------------------------------------------------
2136 //-----------------------------------------------------------------------------
2138 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2140 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2141 EVT_PAINT (wxListMainWindow::OnPaint
)
2142 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2143 EVT_CHAR (wxListMainWindow::OnChar
)
2144 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2145 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2146 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2147 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2150 void wxListMainWindow::Init()
2155 m_lineTo
= (size_t)-1;
2161 m_small_image_list
= (wxImageListType
*) NULL
;
2162 m_normal_image_list
= (wxImageListType
*) NULL
;
2164 m_small_spacing
= 30;
2165 m_normal_spacing
= 40;
2169 m_isCreated
= false;
2171 m_lastOnSame
= false;
2172 m_renameTimer
= new wxListRenameTimer( this );
2176 m_lineBeforeLastClicked
= (size_t)-1;
2181 wxListMainWindow::wxListMainWindow()
2186 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2189 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2194 const wxString
&name
)
2195 : wxScrolledWindow( parent
, id
, pos
, size
,
2196 style
| wxHSCROLL
| wxVSCROLL
, name
)
2200 m_highlightBrush
= new wxBrush
2202 wxSystemSettings::GetColour
2204 wxSYS_COLOUR_HIGHLIGHT
2209 m_highlightUnfocusedBrush
= new wxBrush
2211 wxSystemSettings::GetColour
2213 wxSYS_COLOUR_BTNSHADOW
2221 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2223 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2224 SetOwnForegroundColour( attr
.colFg
);
2225 SetOwnBackgroundColour( attr
.colBg
);
2227 SetOwnFont( attr
.font
);
2230 wxListMainWindow::~wxListMainWindow()
2233 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2235 delete m_highlightBrush
;
2236 delete m_highlightUnfocusedBrush
;
2238 delete m_renameTimer
;
2241 void wxListMainWindow::CacheLineData(size_t line
)
2243 wxGenericListCtrl
*listctrl
= GetListCtrl();
2245 wxListLineData
*ld
= GetDummyLine();
2247 size_t countCol
= GetColumnCount();
2248 for ( size_t col
= 0; col
< countCol
; col
++ )
2250 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2253 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2254 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2257 wxListLineData
*wxListMainWindow::GetDummyLine() const
2259 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2261 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2263 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2265 // we need to recreate the dummy line if the number of columns in the
2266 // control changed as it would have the incorrect number of fields
2268 if ( !m_lines
.IsEmpty() &&
2269 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2271 self
->m_lines
.Clear();
2274 if ( m_lines
.IsEmpty() )
2276 wxListLineData
*line
= new wxListLineData(self
);
2277 self
->m_lines
.Add(line
);
2279 // don't waste extra memory -- there never going to be anything
2280 // else/more in this array
2281 self
->m_lines
.Shrink();
2287 // ----------------------------------------------------------------------------
2288 // line geometry (report mode only)
2289 // ----------------------------------------------------------------------------
2291 wxCoord
wxListMainWindow::GetLineHeight() const
2293 // we cache the line height as calling GetTextExtent() is slow
2294 if ( !m_lineHeight
)
2296 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2298 wxClientDC
dc( self
);
2299 dc
.SetFont( GetFont() );
2302 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2304 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2308 m_small_image_list
->GetSize(0, iw
, ih
);
2313 self
->m_lineHeight
= y
+ LINE_SPACING
;
2316 return m_lineHeight
;
2319 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2321 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2323 return LINE_SPACING
+ line
*GetLineHeight();
2326 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2328 if ( !InReportView() )
2329 return GetLine(line
)->m_gi
->m_rectAll
;
2332 rect
.x
= HEADER_OFFSET_X
;
2333 rect
.y
= GetLineY(line
);
2334 rect
.width
= GetHeaderWidth();
2335 rect
.height
= GetLineHeight();
2340 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2342 if ( !InReportView() )
2343 return GetLine(line
)->m_gi
->m_rectLabel
;
2346 rect
.x
= HEADER_OFFSET_X
;
2347 rect
.y
= GetLineY(line
);
2348 rect
.width
= GetColumnWidth(0);
2349 rect
.height
= GetLineHeight();
2354 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2356 if ( !InReportView() )
2357 return GetLine(line
)->m_gi
->m_rectIcon
;
2359 wxListLineData
*ld
= GetLine(line
);
2360 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2363 rect
.x
= HEADER_OFFSET_X
;
2364 rect
.y
= GetLineY(line
);
2365 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2370 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2372 return InReportView() ? GetLineRect(line
)
2373 : GetLine(line
)->m_gi
->m_rectHighlight
;
2376 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2378 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2380 wxListLineData
*ld
= GetLine(line
);
2382 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2383 return wxLIST_HITTEST_ONITEMICON
;
2385 // VS: Testing for "ld->HasText() || InReportView()" instead of
2386 // "ld->HasText()" is needed to make empty lines in report view
2388 if ( ld
->HasText() || InReportView() )
2390 wxRect rect
= InReportView() ? GetLineRect(line
)
2391 : GetLineLabelRect(line
);
2393 if ( rect
.Inside(x
, y
) )
2394 return wxLIST_HITTEST_ONITEMLABEL
;
2400 // ----------------------------------------------------------------------------
2401 // highlight (selection) handling
2402 // ----------------------------------------------------------------------------
2404 bool wxListMainWindow::IsHighlighted(size_t line
) const
2408 return m_selStore
.IsSelected(line
);
2412 wxListLineData
*ld
= GetLine(line
);
2413 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2415 return ld
->IsHighlighted();
2419 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2425 wxArrayInt linesChanged
;
2426 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2429 // meny items changed state, refresh everything
2430 RefreshLines(lineFrom
, lineTo
);
2432 else // only a few items changed state, refresh only them
2434 size_t count
= linesChanged
.GetCount();
2435 for ( size_t n
= 0; n
< count
; n
++ )
2437 RefreshLine(linesChanged
[n
]);
2441 else // iterate over all items in non report view
2443 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2445 if ( HighlightLine(line
, highlight
) )
2453 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2459 changed
= m_selStore
.SelectItem(line
, highlight
);
2463 wxListLineData
*ld
= GetLine(line
);
2464 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2466 changed
= ld
->Highlight(highlight
);
2471 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2472 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2478 void wxListMainWindow::RefreshLine( size_t line
)
2480 if ( InReportView() )
2482 size_t visibleFrom
, visibleTo
;
2483 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2485 if ( line
< visibleFrom
|| line
> visibleTo
)
2489 wxRect rect
= GetLineRect(line
);
2491 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2492 RefreshRect( rect
);
2495 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2497 // we suppose that they are ordered by caller
2498 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2500 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2502 if ( InReportView() )
2504 size_t visibleFrom
, visibleTo
;
2505 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2507 if ( lineFrom
< visibleFrom
)
2508 lineFrom
= visibleFrom
;
2509 if ( lineTo
> visibleTo
)
2514 rect
.y
= GetLineY(lineFrom
);
2515 rect
.width
= GetClientSize().x
;
2516 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2518 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2519 RefreshRect( rect
);
2523 // TODO: this should be optimized...
2524 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2531 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2533 if ( InReportView() )
2535 size_t visibleFrom
, visibleTo
;
2536 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2538 if ( lineFrom
< visibleFrom
)
2539 lineFrom
= visibleFrom
;
2540 else if ( lineFrom
> visibleTo
)
2545 rect
.y
= GetLineY(lineFrom
);
2546 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2548 wxSize size
= GetClientSize();
2549 rect
.width
= size
.x
;
2550 // refresh till the bottom of the window
2551 rect
.height
= size
.y
- rect
.y
;
2553 RefreshRect( rect
);
2557 // TODO: how to do it more efficiently?
2562 void wxListMainWindow::RefreshSelected()
2568 if ( InReportView() )
2570 GetVisibleLinesRange(&from
, &to
);
2575 to
= GetItemCount() - 1;
2578 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2580 RefreshLine(m_current
);
2583 for ( size_t line
= from
; line
<= to
; line
++ )
2585 // NB: the test works as expected even if m_current == -1
2586 if ( line
!= m_current
&& IsHighlighted(line
) )
2593 void wxListMainWindow::Freeze()
2598 void wxListMainWindow::Thaw()
2600 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2602 if ( !--m_freezeCount
)
2608 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2610 // Note: a wxPaintDC must be constructed even if no drawing is
2611 // done (a Windows requirement).
2612 wxPaintDC
dc( this );
2614 if ( IsEmpty() || m_freezeCount
)
2616 // nothing to draw or not the moment to draw it
2622 // delay the repainting until we calculate all the items positions
2629 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2633 dc
.SetFont( GetFont() );
2635 if ( InReportView() )
2637 int lineHeight
= GetLineHeight();
2639 size_t visibleFrom
, visibleTo
;
2640 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2643 wxCoord xOrig
, yOrig
;
2644 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2646 // tell the caller cache to cache the data
2649 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2650 GetParent()->GetId());
2651 evCache
.SetEventObject( GetParent() );
2652 evCache
.m_oldItemIndex
= visibleFrom
;
2653 evCache
.m_itemIndex
= visibleTo
;
2654 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2657 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2659 rectLine
= GetLineRect(line
);
2661 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2662 rectLine
.width
, rectLine
.height
) )
2664 // don't redraw unaffected lines to avoid flicker
2668 GetLine(line
)->DrawInReportMode( &dc
,
2670 GetLineHighlightRect(line
),
2671 IsHighlighted(line
) );
2674 if ( HasFlag(wxLC_HRULES
) )
2676 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2677 wxSize clientSize
= GetClientSize();
2679 // Don't draw the first one
2680 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2683 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2684 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2685 clientSize
.x
- dev_x
, i
*lineHeight
);
2688 // Draw last horizontal rule
2689 if ( visibleTo
== GetItemCount() - 1 )
2692 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2693 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2694 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2698 // Draw vertical rules if required
2699 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2701 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2703 wxRect firstItemRect
;
2704 wxRect lastItemRect
;
2705 GetItemRect(visibleFrom
, firstItemRect
);
2706 GetItemRect(visibleTo
, lastItemRect
);
2707 int x
= firstItemRect
.GetX();
2709 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2710 for (int col
= 0; col
< GetColumnCount(); col
++)
2712 int colWidth
= GetColumnWidth(col
);
2714 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2715 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2721 size_t count
= GetItemCount();
2722 for ( size_t i
= 0; i
< count
; i
++ )
2724 GetLine(i
)->Draw( &dc
);
2729 // Don't draw rect outline under Mac at all.
2734 dc
.SetPen( *wxBLACK_PEN
);
2735 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2736 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2744 void wxListMainWindow::HighlightAll( bool on
)
2746 if ( IsSingleSel() )
2748 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2750 // we just have one item to turn off
2751 if ( HasCurrent() && IsHighlighted(m_current
) )
2753 HighlightLine(m_current
, false);
2754 RefreshLine(m_current
);
2759 HighlightLines(0, GetItemCount() - 1, on
);
2763 void wxListMainWindow::SendNotify( size_t line
,
2764 wxEventType command
,
2767 wxListEvent
le( command
, GetParent()->GetId() );
2768 le
.SetEventObject( GetParent() );
2769 le
.m_itemIndex
= line
;
2771 // set only for events which have position
2772 if ( point
!= wxDefaultPosition
)
2773 le
.m_pointDrag
= point
;
2775 // don't try to get the line info for virtual list controls: the main
2776 // program has it anyhow and if we did it would result in accessing all
2777 // the lines, even those which are not visible now and this is precisely
2778 // what we're trying to avoid
2779 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2781 if ( line
!= (size_t)-1 )
2783 GetLine(line
)->GetItem( 0, le
.m_item
);
2785 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2787 //else: there may be no more such item
2789 GetParent()->GetEventHandler()->ProcessEvent( le
);
2792 void wxListMainWindow::ChangeCurrent(size_t current
)
2794 m_current
= current
;
2796 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2799 void wxListMainWindow::EditLabel( long item
)
2801 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2802 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2804 size_t itemEdit
= (size_t)item
;
2806 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2807 le
.SetEventObject( GetParent() );
2808 le
.m_itemIndex
= item
;
2809 wxListLineData
*data
= GetLine(itemEdit
);
2810 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2811 data
->GetItem( 0, le
.m_item
);
2812 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2814 // vetoed by user code
2818 // We have to call this here because the label in question might just have
2819 // been added and no screen update taken place.
2823 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2828 void wxListMainWindow::OnRenameTimer()
2830 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2832 EditLabel( m_current
);
2835 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2837 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2838 le
.SetEventObject( GetParent() );
2839 le
.m_itemIndex
= itemEdit
;
2841 wxListLineData
*data
= GetLine(itemEdit
);
2842 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2844 data
->GetItem( 0, le
.m_item
);
2845 le
.m_item
.m_text
= value
;
2846 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2850 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2852 // let owner know that the edit was cancelled
2853 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2855 le
.SetEditCanceled(true);
2857 le
.SetEventObject( GetParent() );
2858 le
.m_itemIndex
= itemEdit
;
2860 wxListLineData
*data
= GetLine(itemEdit
);
2861 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2863 data
->GetItem( 0, le
.m_item
);
2865 GetEventHandler()->ProcessEvent( le
);
2868 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2870 event
.SetEventObject( GetParent() );
2871 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2874 if ( !HasCurrent() || IsEmpty() )
2880 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2881 event
.ButtonDClick()) )
2884 int x
= event
.GetX();
2885 int y
= event
.GetY();
2886 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2888 // where did we hit it (if we did)?
2891 size_t count
= GetItemCount(),
2894 if ( InReportView() )
2896 current
= y
/ GetLineHeight();
2897 if ( current
< count
)
2898 hitResult
= HitTestLine(current
, x
, y
);
2902 // TODO: optimize it too! this is less simple than for report view but
2903 // enumerating all items is still not a way to do it!!
2904 for ( current
= 0; current
< count
; current
++ )
2906 hitResult
= HitTestLine(current
, x
, y
);
2912 if (event
.Dragging())
2914 if (m_dragCount
== 0)
2916 // we have to report the raw, physical coords as we want to be
2917 // able to call HitTest(event.m_pointDrag) from the user code to
2918 // get the item being dragged
2919 m_dragStart
= event
.GetPosition();
2924 if (m_dragCount
!= 3)
2927 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2928 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2930 wxListEvent
le( command
, GetParent()->GetId() );
2931 le
.SetEventObject( GetParent() );
2932 le
.m_itemIndex
= current
;
2933 le
.m_pointDrag
= m_dragStart
;
2934 GetParent()->GetEventHandler()->ProcessEvent( le
);
2945 // outside of any item
2949 bool forceClick
= false;
2950 if (event
.ButtonDClick())
2952 m_renameTimer
->Stop();
2953 m_lastOnSame
= false;
2955 if ( current
== m_lineLastClicked
)
2957 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2963 // the first click was on another item, so don't interpret this as
2964 // a double click, but as a simple click instead
2969 if (event
.LeftUp() && m_lastOnSame
)
2971 if ((current
== m_current
) &&
2972 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2973 HasFlag(wxLC_EDIT_LABELS
) )
2975 m_renameTimer
->Start( 100, true );
2977 m_lastOnSame
= false;
2979 else if (event
.RightDown())
2981 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2982 event
.GetPosition() );
2984 else if (event
.MiddleDown())
2986 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2988 else if ( event
.LeftDown() || forceClick
)
2990 m_lineBeforeLastClicked
= m_lineLastClicked
;
2991 m_lineLastClicked
= current
;
2993 size_t oldCurrent
= m_current
;
2994 bool cmdModifierDown
= event
.CmdDown();
2995 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2997 HighlightAll( false );
2999 ChangeCurrent(current
);
3001 ReverseHighlight(m_current
);
3003 else // multi sel & either ctrl or shift is down
3005 if (cmdModifierDown
)
3007 ChangeCurrent(current
);
3009 ReverseHighlight(m_current
);
3011 else if (event
.ShiftDown())
3013 ChangeCurrent(current
);
3015 size_t lineFrom
= oldCurrent
,
3018 if ( lineTo
< lineFrom
)
3021 lineFrom
= m_current
;
3024 HighlightLines(lineFrom
, lineTo
);
3026 else // !ctrl, !shift
3028 // test in the enclosing if should make it impossible
3029 wxFAIL_MSG( _T("how did we get here?") );
3033 if (m_current
!= oldCurrent
)
3035 RefreshLine( oldCurrent
);
3038 // forceClick is only set if the previous click was on another item
3039 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3043 void wxListMainWindow::MoveToItem(size_t item
)
3045 if ( item
== (size_t)-1 )
3048 wxRect rect
= GetLineRect(item
);
3050 int client_w
, client_h
;
3051 GetClientSize( &client_w
, &client_h
);
3053 const int hLine
= GetLineHeight();
3055 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3056 int view_y
= hLine
*GetScrollPos( wxVERTICAL
);
3058 if ( InReportView() )
3060 // the next we need the range of lines shown it might be different, so
3062 ResetVisibleLinesRange();
3064 if (rect
.y
< view_y
)
3065 Scroll( -1, rect
.y
/hLine
);
3066 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3067 Scroll( -1, (rect
.y
+rect
.height
-client_h
+hLine
)/hLine
);
3071 if (rect
.x
-view_x
< 5)
3072 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3073 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3074 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3078 // ----------------------------------------------------------------------------
3079 // keyboard handling
3080 // ----------------------------------------------------------------------------
3082 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3084 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3085 _T("invalid item index in OnArrowChar()") );
3087 size_t oldCurrent
= m_current
;
3089 // in single selection we just ignore Shift as we can't select several
3091 if ( event
.ShiftDown() && !IsSingleSel() )
3093 ChangeCurrent(newCurrent
);
3095 // refresh the old focus to remove it
3096 RefreshLine( oldCurrent
);
3098 // select all the items between the old and the new one
3099 if ( oldCurrent
> newCurrent
)
3101 newCurrent
= oldCurrent
;
3102 oldCurrent
= m_current
;
3105 HighlightLines(oldCurrent
, newCurrent
);
3109 // all previously selected items are unselected unless ctrl is held
3110 if ( !event
.ControlDown() )
3111 HighlightAll(false);
3113 ChangeCurrent(newCurrent
);
3115 // refresh the old focus to remove it
3116 RefreshLine( oldCurrent
);
3118 if ( !event
.ControlDown() )
3120 HighlightLine( m_current
, true );
3124 RefreshLine( m_current
);
3129 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3131 wxWindow
*parent
= GetParent();
3133 /* we propagate the key event up */
3134 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3135 ke
.m_shiftDown
= event
.m_shiftDown
;
3136 ke
.m_controlDown
= event
.m_controlDown
;
3137 ke
.m_altDown
= event
.m_altDown
;
3138 ke
.m_metaDown
= event
.m_metaDown
;
3139 ke
.m_keyCode
= event
.m_keyCode
;
3142 ke
.SetEventObject( parent
);
3143 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3148 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3150 wxWindow
*parent
= GetParent();
3152 /* we send a list_key event up */
3155 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3156 le
.m_itemIndex
= m_current
;
3157 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3158 le
.m_code
= event
.GetKeyCode();
3159 le
.SetEventObject( parent
);
3160 parent
->GetEventHandler()->ProcessEvent( le
);
3163 /* we propagate the char event up */
3164 wxKeyEvent
ke( wxEVT_CHAR
);
3165 ke
.m_shiftDown
= event
.m_shiftDown
;
3166 ke
.m_controlDown
= event
.m_controlDown
;
3167 ke
.m_altDown
= event
.m_altDown
;
3168 ke
.m_metaDown
= event
.m_metaDown
;
3169 ke
.m_keyCode
= event
.m_keyCode
;
3172 ke
.SetEventObject( parent
);
3173 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3175 if (event
.GetKeyCode() == WXK_TAB
)
3177 wxNavigationKeyEvent nevent
;
3178 nevent
.SetWindowChange( event
.ControlDown() );
3179 nevent
.SetDirection( !event
.ShiftDown() );
3180 nevent
.SetEventObject( GetParent()->GetParent() );
3181 nevent
.SetCurrentFocus( m_parent
);
3182 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3186 /* no item -> nothing to do */
3193 switch (event
.GetKeyCode())
3196 if ( m_current
> 0 )
3197 OnArrowChar( m_current
- 1, event
);
3201 if ( m_current
< (size_t)GetItemCount() - 1 )
3202 OnArrowChar( m_current
+ 1, event
);
3207 OnArrowChar( GetItemCount() - 1, event
);
3212 OnArrowChar( 0, event
);
3217 int steps
= InReportView() ? m_linesPerPage
- 1 : m_current
% m_linesPerPage
;
3219 int index
= m_current
- steps
;
3223 OnArrowChar( index
, event
);
3229 int steps
= InReportView()
3230 ? m_linesPerPage
- 1
3231 : m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3233 size_t index
= m_current
+ steps
;
3234 size_t count
= GetItemCount();
3235 if ( index
>= count
)
3238 OnArrowChar( index
, event
);
3243 if ( !InReportView() )
3245 int index
= m_current
- m_linesPerPage
;
3249 OnArrowChar( index
, event
);
3254 if ( !InReportView() )
3256 size_t index
= m_current
+ m_linesPerPage
;
3258 size_t count
= GetItemCount();
3259 if ( index
>= count
)
3262 OnArrowChar( index
, event
);
3267 if ( IsSingleSel() )
3269 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3271 if ( IsHighlighted(m_current
) )
3273 // don't unselect the item in single selection mode
3276 //else: select it in ReverseHighlight() below if unselected
3279 ReverseHighlight(m_current
);
3284 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3292 // ----------------------------------------------------------------------------
3294 // ----------------------------------------------------------------------------
3296 void wxListMainWindow::SetFocus()
3298 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3299 // overrides SetFocus in such way that it does never change focus from
3300 // panel's child to the panel itself. Unfortunately, we must be able to change
3301 // focus to the panel from wxListTextCtrl because the text control should
3302 // disappear when the user clicks outside it.
3304 wxWindow
*oldFocus
= FindFocus();
3306 if ( oldFocus
&& oldFocus
->GetParent() == this )
3308 wxWindow::SetFocus();
3312 wxScrolledWindow::SetFocus();
3316 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3320 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3321 event
.SetEventObject( GetParent() );
3322 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3326 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3327 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3328 // which are already drawn correctly resulting in horrible flicker - avoid
3338 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3342 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3343 event
.SetEventObject( GetParent() );
3344 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3351 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3353 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3355 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3357 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3359 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3361 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3363 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3365 else if ( InReportView() && (m_small_image_list
))
3367 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3371 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3373 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3375 m_normal_image_list
->GetSize( index
, width
, height
);
3377 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3379 m_small_image_list
->GetSize( index
, width
, height
);
3381 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3383 m_small_image_list
->GetSize( index
, width
, height
);
3385 else if ( InReportView() && m_small_image_list
)
3387 m_small_image_list
->GetSize( index
, width
, height
);
3396 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3398 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3399 dc
.SetFont( GetFont() );
3402 dc
.GetTextExtent( s
, &lw
, NULL
);
3404 return lw
+ AUTOSIZE_COL_MARGIN
;
3407 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3411 // calc the spacing from the icon size
3414 if ((imageList
) && (imageList
->GetImageCount()) )
3416 imageList
->GetSize(0, width
, height
);
3419 if (which
== wxIMAGE_LIST_NORMAL
)
3421 m_normal_image_list
= imageList
;
3422 m_normal_spacing
= width
+ 8;
3425 if (which
== wxIMAGE_LIST_SMALL
)
3427 m_small_image_list
= imageList
;
3428 m_small_spacing
= width
+ 14;
3429 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3433 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3438 m_small_spacing
= spacing
;
3442 m_normal_spacing
= spacing
;
3446 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3448 return isSmall
? m_small_spacing
: m_normal_spacing
;
3451 // ----------------------------------------------------------------------------
3453 // ----------------------------------------------------------------------------
3455 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3457 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3459 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3461 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3462 item
.m_width
= GetTextLength( item
.m_text
);
3464 wxListHeaderData
*column
= node
->GetData();
3465 column
->SetItem( item
);
3467 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3469 headerWin
->m_dirty
= true;
3473 // invalidate it as it has to be recalculated
3477 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3479 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3480 _T("invalid column index") );
3482 wxCHECK_RET( InReportView(),
3483 _T("SetColumnWidth() can only be called in report mode.") );
3486 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3488 headerWin
->m_dirty
= true;
3490 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3491 wxCHECK_RET( node
, _T("no column?") );
3493 wxListHeaderData
*column
= node
->GetData();
3495 size_t count
= GetItemCount();
3497 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3499 width
= GetTextLength(column
->GetText());
3501 else if ( width
== wxLIST_AUTOSIZE
)
3505 // TODO: determine the max width somehow...
3506 width
= WIDTH_COL_DEFAULT
;
3510 wxClientDC
dc(this);
3511 dc
.SetFont( GetFont() );
3513 int max
= AUTOSIZE_COL_MARGIN
;
3515 for ( size_t i
= 0; i
< count
; i
++ )
3517 wxListLineData
*line
= GetLine(i
);
3518 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3520 wxCHECK_RET( n
, _T("no subitem?") );
3522 wxListItemData
*item
= n
->GetData();
3525 if (item
->HasImage())
3528 GetImageSize( item
->GetImage(), ix
, iy
);
3532 if (item
->HasText())
3535 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3543 width
= max
+ AUTOSIZE_COL_MARGIN
;
3547 column
->SetWidth( width
);
3549 // invalidate it as it has to be recalculated
3553 int wxListMainWindow::GetHeaderWidth() const
3555 if ( !m_headerWidth
)
3557 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3559 size_t count
= GetColumnCount();
3560 for ( size_t col
= 0; col
< count
; col
++ )
3562 self
->m_headerWidth
+= GetColumnWidth(col
);
3566 return m_headerWidth
;
3569 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3571 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3572 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3574 wxListHeaderData
*column
= node
->GetData();
3575 column
->GetItem( item
);
3578 int wxListMainWindow::GetColumnWidth( int col
) const
3580 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3581 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3583 wxListHeaderData
*column
= node
->GetData();
3584 return column
->GetWidth();
3587 // ----------------------------------------------------------------------------
3589 // ----------------------------------------------------------------------------
3591 void wxListMainWindow::SetItem( wxListItem
&item
)
3593 long id
= item
.m_itemId
;
3594 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3595 _T("invalid item index in SetItem") );
3599 wxListLineData
*line
= GetLine((size_t)id
);
3600 line
->SetItem( item
.m_col
, item
);
3603 // update the item on screen
3605 GetItemRect(id
, rectItem
);
3606 RefreshRect(rectItem
);
3609 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3611 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3612 _T("invalid list ctrl item index in SetItem") );
3614 size_t oldCurrent
= m_current
;
3615 size_t item
= (size_t)litem
; // safe because of the check above
3617 // do we need to change the focus?
3618 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3620 if ( state
& wxLIST_STATE_FOCUSED
)
3622 // don't do anything if this item is already focused
3623 if ( item
!= m_current
)
3625 ChangeCurrent(item
);
3627 if ( oldCurrent
!= (size_t)-1 )
3629 if ( IsSingleSel() )
3631 HighlightLine(oldCurrent
, false);
3634 RefreshLine(oldCurrent
);
3637 RefreshLine( m_current
);
3642 // don't do anything if this item is not focused
3643 if ( item
== m_current
)
3647 if ( IsSingleSel() )
3649 // we must unselect the old current item as well or we
3650 // might end up with more than one selected item in a
3651 // single selection control
3652 HighlightLine(oldCurrent
, false);
3655 RefreshLine( oldCurrent
);
3660 // do we need to change the selection state?
3661 if ( stateMask
& wxLIST_STATE_SELECTED
)
3663 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3665 if ( IsSingleSel() )
3669 // selecting the item also makes it the focused one in the
3671 if ( m_current
!= item
)
3673 ChangeCurrent(item
);
3675 if ( oldCurrent
!= (size_t)-1 )
3677 HighlightLine( oldCurrent
, false );
3678 RefreshLine( oldCurrent
);
3684 // only the current item may be selected anyhow
3685 if ( item
!= m_current
)
3690 if ( HighlightLine(item
, on
) )
3697 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3699 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3700 _T("invalid list ctrl item index in GetItemState()") );
3702 int ret
= wxLIST_STATE_DONTCARE
;
3704 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3706 if ( (size_t)item
== m_current
)
3707 ret
|= wxLIST_STATE_FOCUSED
;
3710 if ( stateMask
& wxLIST_STATE_SELECTED
)
3712 if ( IsHighlighted(item
) )
3713 ret
|= wxLIST_STATE_SELECTED
;
3719 void wxListMainWindow::GetItem( wxListItem
&item
) const
3721 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3722 _T("invalid item index in GetItem") );
3724 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3725 line
->GetItem( item
.m_col
, item
);
3728 // ----------------------------------------------------------------------------
3730 // ----------------------------------------------------------------------------
3732 size_t wxListMainWindow::GetItemCount() const
3734 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3737 void wxListMainWindow::SetItemCount(long count
)
3739 m_selStore
.SetItemCount(count
);
3740 m_countVirt
= count
;
3742 ResetVisibleLinesRange();
3744 // scrollbars must be reset
3748 int wxListMainWindow::GetSelectedItemCount() const
3750 // deal with the quick case first
3751 if ( IsSingleSel() )
3753 return HasCurrent() ? IsHighlighted(m_current
) : false;
3756 // virtual controls remmebers all its selections itself
3758 return m_selStore
.GetSelectedCount();
3760 // TODO: we probably should maintain the number of items selected even for
3761 // non virtual controls as enumerating all lines is really slow...
3762 size_t countSel
= 0;
3763 size_t count
= GetItemCount();
3764 for ( size_t line
= 0; line
< count
; line
++ )
3766 if ( GetLine(line
)->IsHighlighted() )
3773 // ----------------------------------------------------------------------------
3774 // item position/size
3775 // ----------------------------------------------------------------------------
3777 wxRect
wxListMainWindow::GetViewRect() const
3779 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3780 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3782 // we need to find the longest/tallest label
3785 const int count
= GetItemCount();
3788 for ( int i
= 0; i
< count
; i
++ )
3793 wxCoord x
= r
.GetRight(),
3803 // some fudge needed to make it look prettier
3804 xMax
+= 2*EXTRA_BORDER_X
;
3805 yMax
+= 2*EXTRA_BORDER_Y
;
3807 // account for the scrollbars if necessary
3808 const wxSize sizeAll
= GetClientSize();
3809 if ( xMax
> sizeAll
.x
)
3810 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3811 if ( yMax
> sizeAll
.y
)
3812 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3814 return wxRect(0, 0, xMax
, yMax
);
3817 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3819 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3820 _T("invalid index in GetItemRect") );
3822 // ensure that we're laid out, otherwise we could return nonsense
3825 wxConstCast(this, wxListMainWindow
)->
3826 RecalculatePositions(true /* no refresh */);
3829 rect
= GetLineRect((size_t)index
);
3831 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3834 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3837 GetItemRect(item
, rect
);
3845 // ----------------------------------------------------------------------------
3846 // geometry calculation
3847 // ----------------------------------------------------------------------------
3849 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3851 wxClientDC
dc( this );
3852 dc
.SetFont( GetFont() );
3854 const size_t count
= GetItemCount();
3857 if ( HasFlag(wxLC_ICON
) )
3858 iconSpacing
= m_normal_spacing
;
3859 else if ( HasFlag(wxLC_SMALL_ICON
) )
3860 iconSpacing
= m_small_spacing
;
3864 // Note that we do not call GetClientSize() here but
3865 // GetSize() and substract the border size for sunken
3866 // borders manually. This is technically incorrect,
3867 // but we need to know the client area's size WITHOUT
3868 // scrollbars here. Since we don't know if there are
3869 // any scrollbars, we use GetSize() instead. Another
3870 // solution would be to call SetScrollbars() here to
3871 // remove the scrollbars and call GetClientSize() then,
3872 // but this might result in flicker and - worse - will
3873 // reset the scrollbars to 0 which is not good at all
3874 // if you resize a dialog/window, but don't want to
3875 // reset the window scrolling. RR.
3876 // Furthermore, we actually do NOT subtract the border
3877 // width as 2 pixels is just the extra space which we
3878 // need around the actual content in the window. Other-
3879 // wise the text would e.g. touch the upper border. RR.
3882 GetSize( &clientWidth
, &clientHeight
);
3884 const int lineHeight
= GetLineHeight();
3886 if ( InReportView() )
3888 // all lines have the same height and we scroll one line per step
3889 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3891 m_linesPerPage
= clientHeight
/ lineHeight
;
3893 ResetVisibleLinesRange();
3895 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3896 GetHeaderWidth() / SCROLL_UNIT_X
,
3897 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3898 GetScrollPos(wxHORIZONTAL
),
3899 GetScrollPos(wxVERTICAL
),
3904 // we have 3 different layout strategies: either layout all items
3905 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3906 // to arrange them in top to bottom, left to right (don't ask me why
3907 // not the other way round...) order
3908 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3910 int x
= EXTRA_BORDER_X
;
3911 int y
= EXTRA_BORDER_Y
;
3913 wxCoord widthMax
= 0;
3916 for ( i
= 0; i
< count
; i
++ )
3918 wxListLineData
*line
= GetLine(i
);
3919 line
->CalculateSize( &dc
, iconSpacing
);
3920 line
->SetPosition( x
, y
, iconSpacing
);
3922 wxSize sizeLine
= GetLineSize(i
);
3924 if ( HasFlag(wxLC_ALIGN_TOP
) )
3926 if ( sizeLine
.x
> widthMax
)
3927 widthMax
= sizeLine
.x
;
3931 else // wxLC_ALIGN_LEFT
3933 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3937 if ( HasFlag(wxLC_ALIGN_TOP
) )
3939 // traverse the items again and tweak their sizes so that they are
3940 // all the same in a row
3941 for ( i
= 0; i
< count
; i
++ )
3943 wxListLineData
*line
= GetLine(i
);
3944 line
->m_gi
->ExtendWidth(widthMax
);
3953 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3954 (y
+ lineHeight
) / lineHeight
,
3955 GetScrollPos( wxHORIZONTAL
),
3956 GetScrollPos( wxVERTICAL
),
3960 else // "flowed" arrangement, the most complicated case
3962 // at first we try without any scrollbars, if the items don't fit into
3963 // the window, we recalculate after subtracting the space taken by the
3966 int entireWidth
= 0;
3968 for (int tries
= 0; tries
< 2; tries
++)
3970 entireWidth
= 2*EXTRA_BORDER_X
;
3974 // Now we have decided that the items do not fit into the
3975 // client area, so we need a scrollbar
3976 entireWidth
+= SCROLL_UNIT_X
;
3979 int x
= EXTRA_BORDER_X
;
3980 int y
= EXTRA_BORDER_Y
;
3981 int maxWidthInThisRow
= 0;
3984 int currentlyVisibleLines
= 0;
3986 for (size_t i
= 0; i
< count
; i
++)
3988 currentlyVisibleLines
++;
3989 wxListLineData
*line
= GetLine(i
);
3990 line
->CalculateSize( &dc
, iconSpacing
);
3991 line
->SetPosition( x
, y
, iconSpacing
);
3993 wxSize sizeLine
= GetLineSize(i
);
3995 if ( maxWidthInThisRow
< sizeLine
.x
)
3996 maxWidthInThisRow
= sizeLine
.x
;
3999 if (currentlyVisibleLines
> m_linesPerPage
)
4000 m_linesPerPage
= currentlyVisibleLines
;
4002 if ( y
+ sizeLine
.y
>= clientHeight
)
4004 currentlyVisibleLines
= 0;
4006 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4007 x
+= maxWidthInThisRow
;
4008 entireWidth
+= maxWidthInThisRow
;
4009 maxWidthInThisRow
= 0;
4012 // We have reached the last item.
4013 if ( i
== count
- 1 )
4014 entireWidth
+= maxWidthInThisRow
;
4016 if ( (tries
== 0) &&
4017 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4019 clientHeight
-= wxSystemSettings::
4020 GetMetric(wxSYS_HSCROLL_Y
);
4025 if ( i
== count
- 1 )
4026 tries
= 1; // Everything fits, no second try required.
4034 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4036 GetScrollPos( wxHORIZONTAL
),
4045 // FIXME: why should we call it from here?
4052 void wxListMainWindow::RefreshAll()
4057 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4058 if ( headerWin
&& headerWin
->m_dirty
)
4060 headerWin
->m_dirty
= false;
4061 headerWin
->Refresh();
4065 void wxListMainWindow::UpdateCurrent()
4067 if ( !HasCurrent() && !IsEmpty() )
4073 long wxListMainWindow::GetNextItem( long item
,
4074 int WXUNUSED(geometry
),
4078 max
= GetItemCount();
4079 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4080 _T("invalid listctrl index in GetNextItem()") );
4082 // notice that we start with the next item (or the first one if item == -1)
4083 // and this is intentional to allow writing a simple loop to iterate over
4084 // all selected items
4088 // this is not an error because the index was ok initially, just no
4099 size_t count
= GetItemCount();
4100 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4102 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4105 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4112 // ----------------------------------------------------------------------------
4114 // ----------------------------------------------------------------------------
4116 void wxListMainWindow::DeleteItem( long lindex
)
4118 size_t count
= GetItemCount();
4120 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4121 _T("invalid item index in DeleteItem") );
4123 size_t index
= (size_t)lindex
;
4125 // we don't need to adjust the index for the previous items
4126 if ( HasCurrent() && m_current
>= index
)
4128 // if the current item is being deleted, we want the next one to
4129 // become selected - unless there is no next one - so don't adjust
4130 // m_current in this case
4131 if ( m_current
!= index
|| m_current
== count
- 1 )
4137 if ( InReportView() )
4139 ResetVisibleLinesRange();
4146 m_selStore
.OnItemDelete(index
);
4150 m_lines
.RemoveAt( index
);
4153 // we need to refresh the (vert) scrollbar as the number of items changed
4156 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4158 RefreshAfter(index
);
4161 void wxListMainWindow::DeleteColumn( int col
)
4163 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4165 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4168 delete node
->GetData();
4169 m_columns
.Erase( node
);
4173 // update all the items
4174 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4176 wxListLineData
* const line
= GetLine(i
);
4177 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4178 delete n
->GetData();
4179 line
->m_items
.Erase(n
);
4183 // invalidate it as it has to be recalculated
4187 void wxListMainWindow::DoDeleteAllItems()
4191 // nothing to do - in particular, don't send the event
4197 // to make the deletion of all items faster, we don't send the
4198 // notifications for each item deletion in this case but only one event
4199 // for all of them: this is compatible with wxMSW and documented in
4200 // DeleteAllItems() description
4202 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4203 event
.SetEventObject( GetParent() );
4204 GetParent()->GetEventHandler()->ProcessEvent( event
);
4213 if ( InReportView() )
4215 ResetVisibleLinesRange();
4221 void wxListMainWindow::DeleteAllItems()
4225 RecalculatePositions();
4228 void wxListMainWindow::DeleteEverything()
4230 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4235 // ----------------------------------------------------------------------------
4236 // scanning for an item
4237 // ----------------------------------------------------------------------------
4239 void wxListMainWindow::EnsureVisible( long index
)
4241 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4242 _T("invalid index in EnsureVisible") );
4244 // We have to call this here because the label in question might just have
4245 // been added and its position is not known yet
4248 RecalculatePositions(true /* no refresh */);
4251 MoveToItem((size_t)index
);
4254 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4261 size_t count
= GetItemCount();
4262 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4264 wxListLineData
*line
= GetLine(i
);
4265 if ( line
->GetText(0) == tmp
)
4272 long wxListMainWindow::FindItem(long start
, long data
)
4278 size_t count
= GetItemCount();
4279 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4281 wxListLineData
*line
= GetLine(i
);
4283 line
->GetItem( 0, item
);
4284 if (item
.m_data
== data
)
4291 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4294 GetVisibleLinesRange(&topItem
, NULL
);
4297 GetItemPosition( GetItemCount()-1, p
);
4300 long id
= (long) floor( pt
.y
*(GetItemCount()-topItem
-1)/p
.y
+topItem
);
4301 if( id
>= 0 && id
< (long)GetItemCount() )
4307 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4309 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4311 size_t count
= GetItemCount();
4313 if ( InReportView() )
4315 size_t current
= y
/ GetLineHeight();
4316 if ( current
< count
)
4318 flags
= HitTestLine(current
, x
, y
);
4325 // TODO: optimize it too! this is less simple than for report view but
4326 // enumerating all items is still not a way to do it!!
4327 for ( size_t current
= 0; current
< count
; current
++ )
4329 flags
= HitTestLine(current
, x
, y
);
4338 // ----------------------------------------------------------------------------
4340 // ----------------------------------------------------------------------------
4342 void wxListMainWindow::InsertItem( wxListItem
&item
)
4344 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4346 int count
= GetItemCount();
4347 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4349 if (item
.m_itemId
> count
)
4350 item
.m_itemId
= count
;
4352 size_t id
= item
.m_itemId
;
4357 // this is unused variable
4360 if ( InReportView() )
4363 // this is unused variable
4366 ResetVisibleLinesRange();
4368 else if ( HasFlag(wxLC_LIST
) )
4370 // this is unused variable
4375 else if ( HasFlag(wxLC_ICON
) )
4377 // this is unused variable
4382 else if ( HasFlag(wxLC_SMALL_ICON
) )
4384 // this is unused variable
4385 mode
= wxLC_ICON
; // no typo
4391 wxFAIL_MSG( _T("unknown mode") );
4394 wxListLineData
*line
= new wxListLineData(this);
4396 line
->SetItem( 0, item
);
4398 m_lines
.Insert( line
, id
);
4402 // If an item is selected at or below the point of insertion, we need to
4403 // increment the member variables because the current row's index has gone
4405 if ( HasCurrent() && m_current
>= id
)
4410 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4412 RefreshLines(id
, GetItemCount() - 1);
4415 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4418 if ( InReportView() )
4420 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4421 item
.m_width
= GetTextLength( item
.m_text
);
4423 wxListHeaderData
*column
= new wxListHeaderData( item
);
4424 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4427 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4428 m_columns
.Insert( node
, column
);
4432 m_columns
.Append( column
);
4437 // update all the items
4438 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4440 wxListLineData
* const line
= GetLine(i
);
4441 wxListItemData
* const data
= new wxListItemData(this);
4443 line
->m_items
.Insert(col
, data
);
4445 line
->m_items
.Append(data
);
4449 // invalidate it as it has to be recalculated
4454 // ----------------------------------------------------------------------------
4456 // ----------------------------------------------------------------------------
4458 wxListCtrlCompare list_ctrl_compare_func_2
;
4459 long list_ctrl_compare_data
;
4461 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4463 wxListLineData
*line1
= *arg1
;
4464 wxListLineData
*line2
= *arg2
;
4466 line1
->GetItem( 0, item
);
4467 long data1
= item
.m_data
;
4468 line2
->GetItem( 0, item
);
4469 long data2
= item
.m_data
;
4470 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4473 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4475 list_ctrl_compare_func_2
= fn
;
4476 list_ctrl_compare_data
= data
;
4477 m_lines
.Sort( list_ctrl_compare_func_1
);
4481 // ----------------------------------------------------------------------------
4483 // ----------------------------------------------------------------------------
4485 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4487 // update our idea of which lines are shown when we redraw the window the
4489 ResetVisibleLinesRange();
4492 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4493 wxScrolledWindow::OnScroll(event
);
4495 HandleOnScroll( event
);
4498 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4500 wxGenericListCtrl
* lc
= GetListCtrl();
4501 wxCHECK_RET( lc
, _T("no listctrl window?") );
4503 lc
->m_headerWin
->Refresh();
4504 lc
->m_headerWin
->Update();
4508 int wxListMainWindow::GetCountPerPage() const
4510 if ( !m_linesPerPage
)
4512 wxConstCast(this, wxListMainWindow
)->
4513 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4516 return m_linesPerPage
;
4519 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4521 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4523 if ( m_lineFrom
== (size_t)-1 )
4525 size_t count
= GetItemCount();
4528 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4530 // this may happen if SetScrollbars() hadn't been called yet
4531 if ( m_lineFrom
>= count
)
4532 m_lineFrom
= count
- 1;
4534 // we redraw one extra line but this is needed to make the redrawing
4535 // logic work when there is a fractional number of lines on screen
4536 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4537 if ( m_lineTo
>= count
)
4538 m_lineTo
= count
- 1;
4540 else // empty control
4543 m_lineTo
= (size_t)-1;
4547 wxASSERT_MSG( IsEmpty() ||
4548 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4549 _T("GetVisibleLinesRange() returns incorrect result") );
4557 // -------------------------------------------------------------------------------------
4558 // wxGenericListCtrl
4559 // -------------------------------------------------------------------------------------
4561 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4563 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4564 EVT_SIZE(wxGenericListCtrl::OnSize
)
4567 wxGenericListCtrl::wxGenericListCtrl()
4569 m_imageListNormal
= (wxImageListType
*) NULL
;
4570 m_imageListSmall
= (wxImageListType
*) NULL
;
4571 m_imageListState
= (wxImageListType
*) NULL
;
4573 m_ownsImageListNormal
=
4574 m_ownsImageListSmall
=
4575 m_ownsImageListState
= false;
4577 m_mainWin
= (wxListMainWindow
*) NULL
;
4578 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4582 wxGenericListCtrl::~wxGenericListCtrl()
4584 if (m_ownsImageListNormal
)
4585 delete m_imageListNormal
;
4586 if (m_ownsImageListSmall
)
4587 delete m_imageListSmall
;
4588 if (m_ownsImageListState
)
4589 delete m_imageListState
;
4592 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4596 // we use 'g' to get the descent, too
4598 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4599 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4601 // only update if changed
4602 if ( h
!= m_headerHeight
)
4606 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4609 ResizeReportView(true);
4614 void wxGenericListCtrl::CreateHeaderWindow()
4616 m_headerWin
= new wxListHeaderWindow
4618 this, wxID_ANY
, m_mainWin
,
4620 wxSize(GetClientSize().x
, m_headerHeight
),
4623 CalculateAndSetHeaderHeight();
4626 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4631 const wxValidator
&validator
,
4632 const wxString
&name
)
4636 m_imageListState
= (wxImageListType
*) NULL
;
4637 m_ownsImageListNormal
=
4638 m_ownsImageListSmall
=
4639 m_ownsImageListState
= false;
4641 m_mainWin
= (wxListMainWindow
*) NULL
;
4642 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4646 if ( !(style
& wxLC_MASK_TYPE
) )
4648 style
= style
| wxLC_LIST
;
4651 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4654 // don't create the inner window with the border
4655 style
&= ~wxBORDER_MASK
;
4657 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0,0), size
, style
);
4659 #if defined( __WXMAC__ ) && __WXMAC_CARBON__
4661 font
.MacCreateThemeFont( kThemeViewsFont
) ;
4664 if ( InReportView() )
4666 CreateHeaderWindow();
4668 if ( HasFlag(wxLC_NO_HEADER
) )
4670 // VZ: why do we create it at all then?
4671 m_headerWin
->Show( false );
4680 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4682 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4683 _T("wxLC_VIRTUAL can't be [un]set") );
4685 long flag
= GetWindowStyle();
4689 if (style
& wxLC_MASK_TYPE
)
4690 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4691 if (style
& wxLC_MASK_ALIGN
)
4692 flag
&= ~wxLC_MASK_ALIGN
;
4693 if (style
& wxLC_MASK_SORT
)
4694 flag
&= ~wxLC_MASK_SORT
;
4706 SetWindowStyleFlag( flag
);
4709 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4713 m_mainWin
->DeleteEverything();
4715 // has the header visibility changed?
4716 bool hasHeader
= HasHeader();
4717 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4719 if ( hasHeader
!= willHaveHeader
)
4726 // don't delete, just hide, as we can reuse it later
4727 m_headerWin
->Show(false);
4729 //else: nothing to do
4731 else // must show header
4735 CreateHeaderWindow();
4737 else // already have it, just show
4739 m_headerWin
->Show( true );
4743 ResizeReportView(willHaveHeader
);
4747 wxWindow::SetWindowStyleFlag( flag
);
4750 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4752 m_mainWin
->GetColumn( col
, item
);
4756 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4758 m_mainWin
->SetColumn( col
, item
);
4762 int wxGenericListCtrl::GetColumnWidth( int col
) const
4764 return m_mainWin
->GetColumnWidth( col
);
4767 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4769 m_mainWin
->SetColumnWidth( col
, width
);
4773 int wxGenericListCtrl::GetCountPerPage() const
4775 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4778 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4780 m_mainWin
->GetItem( info
);
4784 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4786 m_mainWin
->SetItem( info
);
4790 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4793 info
.m_text
= label
;
4794 info
.m_mask
= wxLIST_MASK_TEXT
;
4795 info
.m_itemId
= index
;
4799 info
.m_image
= imageId
;
4800 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4802 m_mainWin
->SetItem(info
);
4806 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4808 return m_mainWin
->GetItemState( item
, stateMask
);
4811 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4813 m_mainWin
->SetItemState( item
, state
, stateMask
);
4817 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4820 info
.m_image
= image
;
4821 info
.m_mask
= wxLIST_MASK_IMAGE
;
4822 info
.m_itemId
= item
;
4823 m_mainWin
->SetItem( info
);
4827 wxString
wxGenericListCtrl::GetItemText( long item
) const
4829 return m_mainWin
->GetItemText(item
);
4832 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4834 m_mainWin
->SetItemText(item
, str
);
4837 long wxGenericListCtrl::GetItemData( long item
) const
4840 info
.m_itemId
= item
;
4841 m_mainWin
->GetItem( info
);
4845 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4848 info
.m_mask
= wxLIST_MASK_DATA
;
4849 info
.m_itemId
= item
;
4851 m_mainWin
->SetItem( info
);
4855 wxRect
wxGenericListCtrl::GetViewRect() const
4857 return m_mainWin
->GetViewRect();
4860 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4862 m_mainWin
->GetItemRect( item
, rect
);
4863 if ( m_mainWin
->HasHeader() )
4864 rect
.y
+= m_headerHeight
+ 1;
4868 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4870 m_mainWin
->GetItemPosition( item
, pos
);
4874 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4879 int wxGenericListCtrl::GetItemCount() const
4881 return m_mainWin
->GetItemCount();
4884 int wxGenericListCtrl::GetColumnCount() const
4886 return m_mainWin
->GetColumnCount();
4889 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4891 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4894 wxSize
wxGenericListCtrl::GetItemSpacing() const
4896 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4898 return wxSize(spacing
, spacing
);
4901 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4903 return m_mainWin
->GetItemSpacing( isSmall
);
4906 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4909 info
.m_itemId
= item
;
4910 info
.SetTextColour( col
);
4911 m_mainWin
->SetItem( info
);
4914 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4917 info
.m_itemId
= item
;
4918 m_mainWin
->GetItem( info
);
4919 return info
.GetTextColour();
4922 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4925 info
.m_itemId
= item
;
4926 info
.SetBackgroundColour( col
);
4927 m_mainWin
->SetItem( info
);
4930 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4933 info
.m_itemId
= item
;
4934 m_mainWin
->GetItem( info
);
4935 return info
.GetBackgroundColour();
4938 int wxGenericListCtrl::GetSelectedItemCount() const
4940 return m_mainWin
->GetSelectedItemCount();
4943 wxColour
wxGenericListCtrl::GetTextColour() const
4945 return GetForegroundColour();
4948 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4950 SetForegroundColour(col
);
4953 long wxGenericListCtrl::GetTopItem() const
4956 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4960 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4962 return m_mainWin
->GetNextItem( item
, geom
, state
);
4965 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4967 if (which
== wxIMAGE_LIST_NORMAL
)
4969 return m_imageListNormal
;
4971 else if (which
== wxIMAGE_LIST_SMALL
)
4973 return m_imageListSmall
;
4975 else if (which
== wxIMAGE_LIST_STATE
)
4977 return m_imageListState
;
4979 return (wxImageListType
*) NULL
;
4982 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4984 if ( which
== wxIMAGE_LIST_NORMAL
)
4986 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4987 m_imageListNormal
= imageList
;
4988 m_ownsImageListNormal
= false;
4990 else if ( which
== wxIMAGE_LIST_SMALL
)
4992 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4993 m_imageListSmall
= imageList
;
4994 m_ownsImageListSmall
= false;
4996 else if ( which
== wxIMAGE_LIST_STATE
)
4998 if (m_ownsImageListState
) delete m_imageListState
;
4999 m_imageListState
= imageList
;
5000 m_ownsImageListState
= false;
5003 m_mainWin
->SetImageList( imageList
, which
);
5006 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
5008 SetImageList(imageList
, which
);
5009 if ( which
== wxIMAGE_LIST_NORMAL
)
5010 m_ownsImageListNormal
= true;
5011 else if ( which
== wxIMAGE_LIST_SMALL
)
5012 m_ownsImageListSmall
= true;
5013 else if ( which
== wxIMAGE_LIST_STATE
)
5014 m_ownsImageListState
= true;
5017 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5022 bool wxGenericListCtrl::DeleteItem( long item
)
5024 m_mainWin
->DeleteItem( item
);
5028 bool wxGenericListCtrl::DeleteAllItems()
5030 m_mainWin
->DeleteAllItems();
5034 bool wxGenericListCtrl::DeleteAllColumns()
5036 size_t count
= m_mainWin
->m_columns
.GetCount();
5037 for ( size_t n
= 0; n
< count
; n
++ )
5043 void wxGenericListCtrl::ClearAll()
5045 m_mainWin
->DeleteEverything();
5048 bool wxGenericListCtrl::DeleteColumn( int col
)
5050 m_mainWin
->DeleteColumn( col
);
5052 // if we don't have the header any longer, we need to relayout the window
5053 if ( !GetColumnCount() )
5055 ResizeReportView(false /* no header */);
5061 void wxGenericListCtrl::Edit( long item
)
5063 m_mainWin
->EditLabel( item
);
5066 bool wxGenericListCtrl::EnsureVisible( long item
)
5068 m_mainWin
->EnsureVisible( item
);
5072 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5074 return m_mainWin
->FindItem( start
, str
, partial
);
5077 long wxGenericListCtrl::FindItem( long start
, long data
)
5079 return m_mainWin
->FindItem( start
, data
);
5082 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5083 int WXUNUSED(direction
))
5085 return m_mainWin
->FindItem( pt
);
5088 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5090 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5093 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5095 m_mainWin
->InsertItem( info
);
5096 return info
.m_itemId
;
5099 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5102 info
.m_text
= label
;
5103 info
.m_mask
= wxLIST_MASK_TEXT
;
5104 info
.m_itemId
= index
;
5105 return InsertItem( info
);
5108 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5111 info
.m_mask
= wxLIST_MASK_IMAGE
;
5112 info
.m_image
= imageIndex
;
5113 info
.m_itemId
= index
;
5114 return InsertItem( info
);
5117 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5120 info
.m_text
= label
;
5121 info
.m_image
= imageIndex
;
5122 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5123 info
.m_itemId
= index
;
5124 return InsertItem( info
);
5127 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5129 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5131 m_mainWin
->InsertColumn( col
, item
);
5133 // if we hadn't had header before and have it now we need to relayout the
5135 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5137 ResizeReportView(true /* have header */);
5140 m_headerWin
->Refresh();
5145 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5146 int format
, int width
)
5149 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5150 item
.m_text
= heading
;
5153 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5154 item
.m_width
= width
;
5156 item
.m_format
= format
;
5158 return InsertColumn( col
, item
);
5161 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5167 // fn is a function which takes 3 long arguments: item1, item2, data.
5168 // item1 is the long data associated with a first item (NOT the index).
5169 // item2 is the long data associated with a second item (NOT the index).
5170 // data is the same value as passed to SortItems.
5171 // The return value is a negative number if the first item should precede the second
5172 // item, a positive number of the second item should precede the first,
5173 // or zero if the two items are equivalent.
5174 // data is arbitrary data to be passed to the sort function.
5176 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5178 m_mainWin
->SortItems( fn
, data
);
5182 // ----------------------------------------------------------------------------
5184 // ----------------------------------------------------------------------------
5186 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5191 ResizeReportView(m_mainWin
->HasHeader());
5193 m_mainWin
->RecalculatePositions();
5196 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5199 GetClientSize( &cw
, &ch
);
5203 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5204 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5206 else // no header window
5208 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5212 void wxGenericListCtrl::OnInternalIdle()
5214 wxWindow::OnInternalIdle();
5216 // do it only if needed
5217 if ( !m_mainWin
->m_dirty
)
5220 m_mainWin
->RecalculatePositions();
5223 // ----------------------------------------------------------------------------
5225 // ----------------------------------------------------------------------------
5227 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5231 m_mainWin
->SetBackgroundColour( colour
);
5232 m_mainWin
->m_dirty
= true;
5238 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5240 if ( !wxWindow::SetForegroundColour( colour
) )
5245 m_mainWin
->SetForegroundColour( colour
);
5246 m_mainWin
->m_dirty
= true;
5251 m_headerWin
->SetForegroundColour( colour
);
5257 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5259 if ( !wxWindow::SetFont( font
) )
5264 m_mainWin
->SetFont( font
);
5265 m_mainWin
->m_dirty
= true;
5270 m_headerWin
->SetFont( font
);
5271 CalculateAndSetHeaderHeight();
5282 #include "wx/listbox.h"
5287 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5290 // Use the same color scheme as wxListBox
5291 return wxListBox::GetClassDefaultAttributes(variant
);
5293 wxUnusedVar(variant
);
5294 wxVisualAttributes attr
;
5295 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5296 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5297 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5302 // ----------------------------------------------------------------------------
5303 // methods forwarded to m_mainWin
5304 // ----------------------------------------------------------------------------
5306 #if wxUSE_DRAG_AND_DROP
5308 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5310 m_mainWin
->SetDropTarget( dropTarget
);
5313 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5315 return m_mainWin
->GetDropTarget();
5318 #endif // wxUSE_DRAG_AND_DROP
5320 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5322 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5325 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5327 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5330 wxColour
wxGenericListCtrl::GetForegroundColour() const
5332 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5335 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5338 return m_mainWin
->PopupMenu( menu
, x
, y
);
5341 #endif // wxUSE_MENUS
5344 void wxGenericListCtrl::SetFocus()
5346 /* The test in window.cpp fails as we are a composite
5347 window, so it checks against "this", but not m_mainWin. */
5348 if ( FindFocus() != this )
5349 m_mainWin
->SetFocus();
5352 // ----------------------------------------------------------------------------
5353 // virtual list control support
5354 // ----------------------------------------------------------------------------
5356 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5358 // this is a pure virtual function, in fact - which is not really pure
5359 // because the controls which are not virtual don't need to implement it
5360 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5362 return wxEmptyString
;
5365 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5368 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5374 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5376 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5377 _T("invalid item index in OnGetItemAttr()") );
5379 // no attributes by default
5383 void wxGenericListCtrl::SetItemCount(long count
)
5385 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5387 m_mainWin
->SetItemCount(count
);
5390 void wxGenericListCtrl::RefreshItem(long item
)
5392 m_mainWin
->RefreshLine(item
);
5395 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5397 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5401 * Generic wxListCtrl is more or less a container for two other
5402 * windows which drawings are done upon. These are namely
5403 * 'm_headerWin' and 'm_mainWin'.
5404 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5405 * behaviour wxListCtrl has under wxMSW.
5407 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5411 // The easy case, no rectangle specified.
5413 m_headerWin
->Refresh(eraseBackground
);
5416 m_mainWin
->Refresh(eraseBackground
);
5420 // Refresh the header window
5423 wxRect rectHeader
= m_headerWin
->GetRect();
5424 rectHeader
.Intersect(*rect
);
5425 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5428 m_headerWin
->GetPosition(&x
, &y
);
5429 rectHeader
.Offset(-x
, -y
);
5430 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5435 // Refresh the main window
5438 wxRect rectMain
= m_mainWin
->GetRect();
5439 rectMain
.Intersect(*rect
);
5440 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5443 m_mainWin
->GetPosition(&x
, &y
);
5444 rectMain
.Offset(-x
, -y
);
5445 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5451 void wxGenericListCtrl::Freeze()
5453 m_mainWin
->Freeze();
5456 void wxGenericListCtrl::Thaw()
5461 #endif // wxUSE_LISTCTRL