1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #if defined(__WXGTK__)
73 #include "wx/gtk/win_gtk.h"
76 #include "wx/selstore.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // the height of the header window (FIXME: should depend on its font!)
110 static const int HEADER_HEIGHT
= 23;
112 // the scrollbar units
113 static const int SCROLL_UNIT_X
= 15;
114 static const int SCROLL_UNIT_Y
= 15;
116 // the spacing between the lines (in report mode)
117 static const int LINE_SPACING
= 0;
119 // extra margins around the text label
120 static const int EXTRA_WIDTH
= 3;
121 static const int EXTRA_HEIGHT
= 4;
123 // offset for the header window
124 static const int HEADER_OFFSET_X
= 1;
125 static const int HEADER_OFFSET_Y
= 1;
127 // when autosizing the columns, add some slack
128 static const int AUTOSIZE_COL_MARGIN
= 10;
130 // default and minimal widths for the header columns
131 static const int WIDTH_COL_DEFAULT
= 80;
132 static const int WIDTH_COL_MIN
= 10;
134 // the space between the image and the text in the report mode
135 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
137 // ============================================================================
139 // ============================================================================
141 //-----------------------------------------------------------------------------
142 // wxListItemData (internal)
143 //-----------------------------------------------------------------------------
145 class WXDLLEXPORT wxListItemData
148 wxListItemData(wxListMainWindow
*owner
);
151 void SetItem( const wxListItem
&info
);
152 void SetImage( int image
) { m_image
= image
; }
153 void SetData( long data
) { m_data
= data
; }
154 void SetPosition( int x
, int y
);
155 void SetSize( int width
, int height
);
157 bool HasText() const { return !m_text
.empty(); }
158 const wxString
& GetText() const { return m_text
; }
159 void SetText(const wxString
& text
) { m_text
= text
; }
161 // we can't use empty string for measuring the string width/height, so
162 // always return something
163 wxString
GetTextForMeasuring() const
165 wxString s
= GetText();
172 bool IsHit( int x
, int y
) const;
176 int GetWidth() const;
177 int GetHeight() const;
179 int GetImage() const { return m_image
; }
180 bool HasImage() const { return GetImage() != -1; }
182 void GetItem( wxListItem
&info
) const;
184 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
185 wxListItemAttr
*GetAttr() const { return m_attr
; }
188 // the item image or -1
191 // user data associated with the item
194 // the item coordinates are not used in report mode, instead this pointer
195 // is NULL and the owner window is used to retrieve the item position and
199 // the list ctrl we are in
200 wxListMainWindow
*m_owner
;
202 // custom attributes or NULL
203 wxListItemAttr
*m_attr
;
206 // common part of all ctors
212 //-----------------------------------------------------------------------------
213 // wxListHeaderData (internal)
214 //-----------------------------------------------------------------------------
216 class WXDLLEXPORT wxListHeaderData
: public wxObject
220 wxListHeaderData( const wxListItem
&info
);
221 void SetItem( const wxListItem
&item
);
222 void SetPosition( int x
, int y
);
223 void SetWidth( int w
);
224 void SetFormat( int format
);
225 void SetHeight( int h
);
226 bool HasImage() const;
228 bool HasText() const { return !m_text
.empty(); }
229 const wxString
& GetText() const { return m_text
; }
230 void SetText(const wxString
& text
) { m_text
= text
; }
232 void GetItem( wxListItem
&item
);
234 bool IsHit( int x
, int y
) const;
235 int GetImage() const;
236 int GetWidth() const;
237 int GetFormat() const;
253 //-----------------------------------------------------------------------------
254 // wxListLineData (internal)
255 //-----------------------------------------------------------------------------
257 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
258 #include "wx/listimpl.cpp"
259 WX_DEFINE_LIST(wxListItemDataList
);
261 class WXDLLEXPORT wxListLineData
264 // the list of subitems: only may have more than one item in report mode
265 wxListItemDataList m_items
;
267 // this is not used in report view
279 // the part to be highlighted
280 wxRect m_rectHighlight
;
283 // is this item selected? [NB: not used in virtual mode]
286 // back pointer to the list ctrl
287 wxListMainWindow
*m_owner
;
290 wxListLineData(wxListMainWindow
*owner
);
294 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
298 // are we in report mode?
299 inline bool InReportView() const;
301 // are we in virtual report mode?
302 inline bool IsVirtual() const;
304 // these 2 methods shouldn't be called for report view controls, in that
305 // case we determine our position/size ourselves
307 // calculate the size of the line
308 void CalculateSize( wxDC
*dc
, int spacing
);
310 // remember the position this line appears at
311 void SetPosition( int x
, int y
, int window_width
, int spacing
);
315 void SetImage( int image
) { SetImage(0, image
); }
316 int GetImage() const { return GetImage(0); }
317 bool HasImage() const { return GetImage() != -1; }
318 bool HasText() const { return !GetText(0).empty(); }
320 void SetItem( int index
, const wxListItem
&info
);
321 void GetItem( int index
, wxListItem
&info
);
323 wxString
GetText(int index
) const;
324 void SetText( int index
, const wxString s
);
326 wxListItemAttr
*GetAttr() const;
327 void SetAttr(wxListItemAttr
*attr
);
329 // return true if the highlighting really changed
330 bool Highlight( bool on
);
332 void ReverseHighlight();
334 bool IsHighlighted() const
336 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
338 return m_highlighted
;
341 // draw the line on the given DC in icon/list mode
342 void Draw( wxDC
*dc
);
344 // the same in report mode
345 void DrawInReportMode( wxDC
*dc
,
347 const wxRect
& rectHL
,
351 // set the line to contain num items (only can be > 1 in report mode)
352 void InitItems( int num
);
354 // get the mode (i.e. style) of the list control
355 inline int GetMode() const;
357 // prepare the DC for drawing with these item's attributes, return true if
358 // we need to draw the items background to highlight it, false otherwise
359 bool SetAttributes(wxDC
*dc
,
360 const wxListItemAttr
*attr
,
363 // draw the text on the DC with the correct justification; also add an
364 // ellipsis if the text is too large to fit in the current width
365 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
367 // these are only used by GetImage/SetImage above, we don't support images
368 // with subitems at the public API level yet
369 void SetImage( int index
, int image
);
370 int GetImage( int index
) const;
373 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
374 #include "wx/arrimpl.cpp"
375 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
377 //-----------------------------------------------------------------------------
378 // wxListHeaderWindow (internal)
379 //-----------------------------------------------------------------------------
381 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
384 wxListMainWindow
*m_owner
;
385 wxCursor
*m_currentCursor
;
386 wxCursor
*m_resizeCursor
;
389 // column being resized or -1
392 // divider line position in logical (unscrolled) coords
395 // minimal position beyond which the divider line can't be dragged in
400 wxListHeaderWindow();
402 wxListHeaderWindow( wxWindow
*win
,
404 wxListMainWindow
*owner
,
405 const wxPoint
&pos
= wxDefaultPosition
,
406 const wxSize
&size
= wxDefaultSize
,
408 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
410 virtual ~wxListHeaderWindow();
412 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
414 void AdjustDC(wxDC
& dc
);
416 void OnPaint( wxPaintEvent
&event
);
417 void OnMouse( wxMouseEvent
&event
);
418 void OnSetFocus( wxFocusEvent
&event
);
424 // common part of all ctors
427 // generate and process the list event of the given type, return true if
428 // it wasn't vetoed, i.e. if we should proceed
429 bool SendListEvent(wxEventType type
, wxPoint pos
);
431 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
432 DECLARE_EVENT_TABLE()
435 //-----------------------------------------------------------------------------
436 // wxListRenameTimer (internal)
437 //-----------------------------------------------------------------------------
439 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
442 wxListMainWindow
*m_owner
;
445 wxListRenameTimer( wxListMainWindow
*owner
);
449 //-----------------------------------------------------------------------------
450 // wxListTextCtrl (internal)
451 //-----------------------------------------------------------------------------
453 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
456 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
459 void OnChar( wxKeyEvent
&event
);
460 void OnKeyUp( wxKeyEvent
&event
);
461 void OnKillFocus( wxFocusEvent
&event
);
463 bool AcceptChanges();
467 wxListMainWindow
*m_owner
;
468 wxString m_startValue
;
472 DECLARE_EVENT_TABLE()
475 //-----------------------------------------------------------------------------
476 // wxListMainWindow (internal)
477 //-----------------------------------------------------------------------------
479 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
480 #include "wx/listimpl.cpp"
481 WX_DEFINE_LIST(wxListHeaderDataList
);
483 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
487 wxListMainWindow( wxWindow
*parent
,
489 const wxPoint
& pos
= wxDefaultPosition
,
490 const wxSize
& size
= wxDefaultSize
,
492 const wxString
&name
= _T("listctrlmainwindow") );
494 virtual ~wxListMainWindow();
496 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
498 // return true if this is a virtual list control
499 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
501 // return true if the control is in report mode
502 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
504 // return true if we are in single selection mode, false if multi sel
505 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
507 // do we have a header window?
508 bool HasHeader() const
509 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
511 void HighlightAll( bool on
);
513 // all these functions only do something if the line is currently visible
515 // change the line "selected" state, return TRUE if it really changed
516 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
518 // as HighlightLine() but do it for the range of lines: this is incredibly
519 // more efficient for virtual list controls!
521 // NB: unlike HighlightLine() this one does refresh the lines on screen
522 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
524 // toggle the line state and refresh it
525 void ReverseHighlight( size_t line
)
526 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
528 // return true if the line is highlighted
529 bool IsHighlighted(size_t line
) const;
531 // refresh one or several lines at once
532 void RefreshLine( size_t line
);
533 void RefreshLines( size_t lineFrom
, size_t lineTo
);
535 // refresh all selected items
536 void RefreshSelected();
538 // refresh all lines below the given one: the difference with
539 // RefreshLines() is that the index here might not be a valid one (happens
540 // when the last line is deleted)
541 void RefreshAfter( size_t lineFrom
);
543 // the methods which are forwarded to wxListLineData itself in list/icon
544 // modes but are here because the lines don't store their positions in the
547 // get the bound rect for the entire line
548 wxRect
GetLineRect(size_t line
) const;
550 // get the bound rect of the label
551 wxRect
GetLineLabelRect(size_t line
) const;
553 // get the bound rect of the items icon (only may be called if we do have
555 wxRect
GetLineIconRect(size_t line
) const;
557 // get the rect to be highlighted when the item has focus
558 wxRect
GetLineHighlightRect(size_t line
) const;
560 // get the size of the total line rect
561 wxSize
GetLineSize(size_t line
) const
562 { return GetLineRect(line
).GetSize(); }
564 // return the hit code for the corresponding position (in this line)
565 long HitTestLine(size_t line
, int x
, int y
) const;
567 // bring the selected item into view, scrolling to it if necessary
568 void MoveToItem(size_t item
);
570 // bring the current item into view
571 void MoveToFocus() { MoveToItem(m_current
); }
573 // start editing the label of the given item
574 void EditLabel( long item
);
576 // suspend/resume redrawing the control
582 void OnRenameTimer();
583 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
584 void OnRenameCancelled(size_t itemEdit
);
586 void OnMouse( wxMouseEvent
&event
);
588 // called to switch the selection from the current item to newCurrent,
589 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
591 void OnChar( wxKeyEvent
&event
);
592 void OnKeyDown( wxKeyEvent
&event
);
593 void OnSetFocus( wxFocusEvent
&event
);
594 void OnKillFocus( wxFocusEvent
&event
);
595 void OnScroll(wxScrollWinEvent
& event
) ;
597 void OnPaint( wxPaintEvent
&event
);
599 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
600 void GetImageSize( int index
, int &width
, int &height
) const;
601 int GetTextLength( const wxString
&s
) const;
603 void SetImageList( wxImageListType
*imageList
, int which
);
604 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
605 int GetItemSpacing( bool isSmall
= FALSE
);
607 void SetColumn( int col
, wxListItem
&item
);
608 void SetColumnWidth( int col
, int width
);
609 void GetColumn( int col
, wxListItem
&item
) const;
610 int GetColumnWidth( int col
) const;
611 int GetColumnCount() const { return m_columns
.GetCount(); }
613 // returns the sum of the heights of all columns
614 int GetHeaderWidth() const;
616 int GetCountPerPage() const;
618 void SetItem( wxListItem
&item
);
619 void GetItem( wxListItem
&item
) const;
620 void SetItemState( long item
, long state
, long stateMask
);
621 int GetItemState( long item
, long stateMask
) const;
622 void GetItemRect( long index
, wxRect
&rect
) const;
623 bool GetItemPosition( long item
, wxPoint
& pos
) const;
624 int GetSelectedItemCount() const;
626 wxString
GetItemText(long item
) const
629 info
.m_itemId
= item
;
634 void SetItemText(long item
, const wxString
& value
)
637 info
.m_mask
= wxLIST_MASK_TEXT
;
638 info
.m_itemId
= item
;
643 // set the scrollbars and update the positions of the items
644 void RecalculatePositions(bool noRefresh
= FALSE
);
646 // refresh the window and the header
649 long GetNextItem( long item
, int geometry
, int state
) const;
650 void DeleteItem( long index
);
651 void DeleteAllItems();
652 void DeleteColumn( int col
);
653 void DeleteEverything();
654 void EnsureVisible( long index
);
655 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
656 long FindItem( long start
, long data
);
657 long HitTest( int x
, int y
, int &flags
);
658 void InsertItem( wxListItem
&item
);
659 void InsertColumn( long col
, wxListItem
&item
);
660 void SortItems( wxListCtrlCompare fn
, long data
);
662 size_t GetItemCount() const;
663 bool IsEmpty() const { return GetItemCount() == 0; }
664 void SetItemCount(long count
);
666 // change the current (== focused) item, send a notification event
667 void ChangeCurrent(size_t current
);
668 void ResetCurrent() { ChangeCurrent((size_t)-1); }
669 bool HasCurrent() const { return m_current
!= (size_t)-1; }
671 // send out a wxListEvent
672 void SendNotify( size_t line
,
674 wxPoint point
= wxDefaultPosition
);
676 // override base class virtual to reset m_lineHeight when the font changes
677 virtual bool SetFont(const wxFont
& font
)
679 if ( !wxScrolledWindow::SetFont(font
) )
687 // these are for wxListLineData usage only
689 // get the backpointer to the list ctrl
690 wxGenericListCtrl
*GetListCtrl() const
692 return wxStaticCast(GetParent(), wxGenericListCtrl
);
695 // get the height of all lines (assuming they all do have the same height)
696 wxCoord
GetLineHeight() const;
698 // get the y position of the given line (only for report view)
699 wxCoord
GetLineY(size_t line
) const;
701 // get the brush to use for the item highlighting
702 wxBrush
*GetHighlightBrush() const
704 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
708 // the array of all line objects for a non virtual list control (for the
709 // virtual list control we only ever use m_lines[0])
710 wxListLineDataArray m_lines
;
712 // the list of column objects
713 wxListHeaderDataList m_columns
;
715 // currently focused item or -1
718 // the number of lines per page
721 // this flag is set when something which should result in the window
722 // redrawing happens (i.e. an item was added or deleted, or its appearance
723 // changed) and OnPaint() doesn't redraw the window while it is set which
724 // allows to minimize the number of repaintings when a lot of items are
725 // being added. The real repainting occurs only after the next OnIdle()
729 wxColour
*m_highlightColour
;
732 wxImageListType
*m_small_image_list
;
733 wxImageListType
*m_normal_image_list
;
735 int m_normal_spacing
;
739 wxTimer
*m_renameTimer
;
744 // for double click logic
745 size_t m_lineLastClicked
,
746 m_lineBeforeLastClicked
;
749 // the total count of items in a virtual list control
752 // the object maintaining the items selection state, only used in virtual
754 wxSelectionStore m_selStore
;
756 // common part of all ctors
759 // intiialize m_[xy]Scroll
760 void InitScrolling();
762 // get the line data for the given index
763 wxListLineData
*GetLine(size_t n
) const
765 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
769 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
777 // get a dummy line which can be used for geometry calculations and such:
778 // you must use GetLine() if you want to really draw the line
779 wxListLineData
*GetDummyLine() const;
781 // cache the line data of the n-th line in m_lines[0]
782 void CacheLineData(size_t line
);
784 // get the range of visible lines
785 void GetVisibleLinesRange(size_t *from
, size_t *to
);
787 // force us to recalculate the range of visible lines
788 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
790 // get the colour to be used for drawing the rules
791 wxColour
GetRuleColour() const
796 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
801 // initialize the current item if needed
802 void UpdateCurrent();
804 // delete all items but don't refresh: called from dtor
805 void DoDeleteAllItems();
807 // the height of one line using the current font
808 wxCoord m_lineHeight
;
810 // the total header width or 0 if not calculated yet
811 wxCoord m_headerWidth
;
813 // the first and last lines being shown on screen right now (inclusive),
814 // both may be -1 if they must be calculated so never access them directly:
815 // use GetVisibleLinesRange() above instead
819 // the brushes to use for item highlighting when we do/don't have focus
820 wxBrush
*m_highlightBrush
,
821 *m_highlightUnfocusedBrush
;
823 // if this is > 0, the control is frozen and doesn't redraw itself
824 size_t m_freezeCount
;
826 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
827 DECLARE_EVENT_TABLE()
829 friend class wxGenericListCtrl
;
832 // ============================================================================
834 // ============================================================================
836 //-----------------------------------------------------------------------------
838 //-----------------------------------------------------------------------------
840 wxListItemData::~wxListItemData()
842 // in the virtual list control the attributes are managed by the main
843 // program, so don't delete them
844 if ( !m_owner
->IsVirtual() )
852 void wxListItemData::Init()
860 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
866 if ( owner
->InReportView() )
876 void wxListItemData::SetItem( const wxListItem
&info
)
878 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
879 SetText(info
.m_text
);
880 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
881 m_image
= info
.m_image
;
882 if ( info
.m_mask
& wxLIST_MASK_DATA
)
883 m_data
= info
.m_data
;
885 if ( info
.HasAttributes() )
888 *m_attr
= *info
.GetAttributes();
890 m_attr
= new wxListItemAttr(*info
.GetAttributes());
898 m_rect
->width
= info
.m_width
;
902 void wxListItemData::SetPosition( int x
, int y
)
904 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
910 void wxListItemData::SetSize( int width
, int height
)
912 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
915 m_rect
->width
= width
;
917 m_rect
->height
= height
;
920 bool wxListItemData::IsHit( int x
, int y
) const
922 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
924 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
927 int wxListItemData::GetX() const
929 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
934 int wxListItemData::GetY() const
936 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
941 int wxListItemData::GetWidth() const
943 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
945 return m_rect
->width
;
948 int wxListItemData::GetHeight() const
950 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
952 return m_rect
->height
;
955 void wxListItemData::GetItem( wxListItem
&info
) const
957 info
.m_text
= m_text
;
958 info
.m_image
= m_image
;
959 info
.m_data
= m_data
;
963 if ( m_attr
->HasTextColour() )
964 info
.SetTextColour(m_attr
->GetTextColour());
965 if ( m_attr
->HasBackgroundColour() )
966 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
967 if ( m_attr
->HasFont() )
968 info
.SetFont(m_attr
->GetFont());
972 //-----------------------------------------------------------------------------
974 //-----------------------------------------------------------------------------
976 void wxListHeaderData::Init()
987 wxListHeaderData::wxListHeaderData()
992 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
999 void wxListHeaderData::SetItem( const wxListItem
&item
)
1001 m_mask
= item
.m_mask
;
1003 if ( m_mask
& wxLIST_MASK_TEXT
)
1004 m_text
= item
.m_text
;
1006 if ( m_mask
& wxLIST_MASK_IMAGE
)
1007 m_image
= item
.m_image
;
1009 if ( m_mask
& wxLIST_MASK_FORMAT
)
1010 m_format
= item
.m_format
;
1012 if ( m_mask
& wxLIST_MASK_WIDTH
)
1013 SetWidth(item
.m_width
);
1016 void wxListHeaderData::SetPosition( int x
, int y
)
1022 void wxListHeaderData::SetHeight( int h
)
1027 void wxListHeaderData::SetWidth( int w
)
1031 m_width
= WIDTH_COL_DEFAULT
;
1032 else if (m_width
< WIDTH_COL_MIN
)
1033 m_width
= WIDTH_COL_MIN
;
1036 void wxListHeaderData::SetFormat( int format
)
1041 bool wxListHeaderData::HasImage() const
1043 return m_image
!= -1;
1046 bool wxListHeaderData::IsHit( int x
, int y
) const
1048 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1051 void wxListHeaderData::GetItem( wxListItem
& item
)
1053 item
.m_mask
= m_mask
;
1054 item
.m_text
= m_text
;
1055 item
.m_image
= m_image
;
1056 item
.m_format
= m_format
;
1057 item
.m_width
= m_width
;
1060 int wxListHeaderData::GetImage() const
1065 int wxListHeaderData::GetWidth() const
1070 int wxListHeaderData::GetFormat() const
1075 //-----------------------------------------------------------------------------
1077 //-----------------------------------------------------------------------------
1079 inline int wxListLineData::GetMode() const
1081 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1084 inline bool wxListLineData::InReportView() const
1086 return m_owner
->HasFlag(wxLC_REPORT
);
1089 inline bool wxListLineData::IsVirtual() const
1091 return m_owner
->IsVirtual();
1094 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1098 if ( InReportView() )
1104 m_gi
= new GeometryInfo
;
1107 m_highlighted
= FALSE
;
1109 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1112 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1114 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1115 wxCHECK_RET( node
, _T("no subitems at all??") );
1117 wxListItemData
*item
= node
->GetData();
1119 switch ( GetMode() )
1122 case wxLC_SMALL_ICON
:
1124 m_gi
->m_rectAll
.width
= spacing
;
1126 wxString s
= item
->GetText();
1132 m_gi
->m_rectLabel
.width
=
1133 m_gi
->m_rectLabel
.height
= 0;
1137 dc
->GetTextExtent( s
, &lw
, &lh
);
1138 if (lh
< SCROLL_UNIT_Y
)
1143 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1145 m_gi
->m_rectAll
.width
= lw
;
1147 m_gi
->m_rectLabel
.width
= lw
;
1148 m_gi
->m_rectLabel
.height
= lh
;
1151 if (item
->HasImage())
1154 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1155 m_gi
->m_rectIcon
.width
= w
+ 8;
1156 m_gi
->m_rectIcon
.height
= h
+ 8;
1158 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1159 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1160 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1161 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1164 if ( item
->HasText() )
1166 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1167 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1169 else // no text, highlight the icon
1171 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1172 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1179 wxString s
= item
->GetTextForMeasuring();
1182 dc
->GetTextExtent( s
, &lw
, &lh
);
1183 if (lh
< SCROLL_UNIT_Y
)
1188 m_gi
->m_rectLabel
.width
= lw
;
1189 m_gi
->m_rectLabel
.height
= lh
;
1191 m_gi
->m_rectAll
.width
= lw
;
1192 m_gi
->m_rectAll
.height
= lh
;
1194 if (item
->HasImage())
1197 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1198 m_gi
->m_rectIcon
.width
= w
;
1199 m_gi
->m_rectIcon
.height
= h
;
1201 m_gi
->m_rectAll
.width
+= 4 + w
;
1202 if (h
> m_gi
->m_rectAll
.height
)
1203 m_gi
->m_rectAll
.height
= h
;
1206 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1207 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1212 wxFAIL_MSG( _T("unexpected call to SetSize") );
1216 wxFAIL_MSG( _T("unknown mode") );
1220 void wxListLineData::SetPosition( int x
, int y
,
1224 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1225 wxCHECK_RET( node
, _T("no subitems at all??") );
1227 wxListItemData
*item
= node
->GetData();
1229 switch ( GetMode() )
1232 case wxLC_SMALL_ICON
:
1233 m_gi
->m_rectAll
.x
= x
;
1234 m_gi
->m_rectAll
.y
= y
;
1236 if ( item
->HasImage() )
1238 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1239 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1240 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1243 if ( item
->HasText() )
1245 if (m_gi
->m_rectAll
.width
> spacing
)
1246 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1248 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1249 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1250 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1251 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1253 else // no text, highlight the icon
1255 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1256 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1261 m_gi
->m_rectAll
.x
= x
;
1262 m_gi
->m_rectAll
.y
= y
;
1264 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1265 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1266 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1268 if (item
->HasImage())
1270 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1271 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1276 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1281 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1285 wxFAIL_MSG( _T("unknown mode") );
1289 void wxListLineData::InitItems( int num
)
1291 for (int i
= 0; i
< num
; i
++)
1292 m_items
.Append( new wxListItemData(m_owner
) );
1295 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1297 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1298 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1300 wxListItemData
*item
= node
->GetData();
1301 item
->SetItem( info
);
1304 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1306 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1309 wxListItemData
*item
= node
->GetData();
1310 item
->GetItem( info
);
1314 wxString
wxListLineData::GetText(int index
) const
1318 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1321 wxListItemData
*item
= node
->GetData();
1322 s
= item
->GetText();
1328 void wxListLineData::SetText( int index
, const wxString s
)
1330 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1333 wxListItemData
*item
= node
->GetData();
1338 void wxListLineData::SetImage( int index
, int image
)
1340 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1341 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1343 wxListItemData
*item
= node
->GetData();
1344 item
->SetImage(image
);
1347 int wxListLineData::GetImage( int index
) const
1349 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1350 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1352 wxListItemData
*item
= node
->GetData();
1353 return item
->GetImage();
1356 wxListItemAttr
*wxListLineData::GetAttr() const
1358 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1359 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1361 wxListItemData
*item
= node
->GetData();
1362 return item
->GetAttr();
1365 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1367 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1368 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1370 wxListItemData
*item
= node
->GetData();
1371 item
->SetAttr(attr
);
1374 bool wxListLineData::SetAttributes(wxDC
*dc
,
1375 const wxListItemAttr
*attr
,
1378 wxWindow
*listctrl
= m_owner
->GetParent();
1382 // don't use foreground colour for drawing highlighted items - this might
1383 // make them completely invisible (and there is no way to do bit
1384 // arithmetics on wxColour, unfortunately)
1388 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1392 if ( attr
&& attr
->HasTextColour() )
1394 colText
= attr
->GetTextColour();
1398 colText
= listctrl
->GetForegroundColour();
1402 dc
->SetTextForeground(colText
);
1406 if ( attr
&& attr
->HasFont() )
1408 font
= attr
->GetFont();
1412 font
= listctrl
->GetFont();
1418 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1419 if ( highlighted
|| hasBgCol
)
1423 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1427 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1430 dc
->SetPen( *wxTRANSPARENT_PEN
);
1438 void wxListLineData::Draw( wxDC
*dc
)
1440 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1441 wxCHECK_RET( node
, _T("no subitems at all??") );
1443 bool highlighted
= IsHighlighted();
1445 wxListItemAttr
*attr
= GetAttr();
1447 if ( SetAttributes(dc
, attr
, highlighted
) )
1449 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1452 wxListItemData
*item
= node
->GetData();
1453 if (item
->HasImage())
1455 wxRect rectIcon
= m_gi
->m_rectIcon
;
1456 m_owner
->DrawImage( item
->GetImage(), dc
,
1457 rectIcon
.x
, rectIcon
.y
);
1460 if (item
->HasText())
1462 wxRect rectLabel
= m_gi
->m_rectLabel
;
1464 wxDCClipper
clipper(*dc
, rectLabel
);
1465 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1469 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1471 const wxRect
& rectHL
,
1474 // TODO: later we should support setting different attributes for
1475 // different columns - to do it, just add "col" argument to
1476 // GetAttr() and move these lines into the loop below
1477 wxListItemAttr
*attr
= GetAttr();
1478 if ( SetAttributes(dc
, attr
, highlighted
) )
1480 dc
->DrawRectangle( rectHL
);
1483 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1484 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1487 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1489 node
= node
->GetNext(), col
++ )
1491 wxListItemData
*item
= node
->GetData();
1493 int width
= m_owner
->GetColumnWidth(col
);
1497 if ( item
->HasImage() )
1500 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1501 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1503 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1509 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1511 if ( item
->HasText() )
1513 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1518 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1519 const wxString
&text
,
1525 wxString drawntext
, ellipsis
;
1526 wxCoord w
, h
, base_w
;
1529 // determine if the string can fit inside the current width
1530 dc
->GetTextExtent(text
, &w
, &h
);
1533 // it can, draw it using the items alignment
1534 m_owner
->GetColumn(col
, item
);
1535 switch ( item
.GetAlign() )
1538 wxFAIL_MSG( _T("unknown list item format") );
1541 case wxLIST_FORMAT_LEFT
:
1545 case wxLIST_FORMAT_RIGHT
:
1549 case wxLIST_FORMAT_CENTER
:
1550 x
+= (width
- w
) / 2;
1554 dc
->DrawText(text
, x
, y
);
1556 else // otherwise, truncate and add an ellipsis if possible
1558 // determine the base width
1559 ellipsis
= wxString(wxT("..."));
1560 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1562 // continue until we have enough space or only one character left
1564 size_t len
= text
.Length();
1565 drawntext
= text
.Left(len
);
1568 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1569 drawntext
.RemoveLast();
1572 if (w
+ base_w
<= width
)
1576 // if still not enough space, remove ellipsis characters
1577 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1579 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1580 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1583 // now draw the text
1584 dc
->DrawText(drawntext
, x
, y
);
1585 dc
->DrawText(ellipsis
, x
+ w
, y
);
1589 bool wxListLineData::Highlight( bool on
)
1591 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1593 if ( on
== m_highlighted
)
1601 void wxListLineData::ReverseHighlight( void )
1603 Highlight(!IsHighlighted());
1606 //-----------------------------------------------------------------------------
1607 // wxListHeaderWindow
1608 //-----------------------------------------------------------------------------
1610 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1612 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1613 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1614 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1615 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1618 void wxListHeaderWindow::Init()
1620 m_currentCursor
= (wxCursor
*) NULL
;
1621 m_isDragging
= FALSE
;
1625 wxListHeaderWindow::wxListHeaderWindow()
1629 m_owner
= (wxListMainWindow
*) NULL
;
1630 m_resizeCursor
= (wxCursor
*) NULL
;
1633 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1635 wxListMainWindow
*owner
,
1639 const wxString
&name
)
1640 : wxWindow( win
, id
, pos
, size
, style
, name
)
1645 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1647 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1650 wxListHeaderWindow::~wxListHeaderWindow()
1652 delete m_resizeCursor
;
1655 #ifdef __WXUNIVERSAL__
1656 #include "wx/univ/renderer.h"
1657 #include "wx/univ/theme.h"
1660 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1662 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1663 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1664 : GTK_STATE_INSENSITIVE
;
1666 x
= dc
->XLOG2DEV( x
);
1668 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1669 state
, GTK_SHADOW_OUT
,
1670 (GdkRectangle
*) NULL
, m_wxwindow
,
1671 (char *)"button", // const_cast
1672 x
-1, y
-1, w
+2, h
+2);
1673 #elif defined(__WXUNIVERSAL__)
1674 wxTheme
*theme
= wxTheme::Get();
1675 wxRenderer
*renderer
= theme
->GetRenderer();
1676 renderer
->DrawBorder( *dc
, wxBORDER_RAISED
, wxRect(x
,y
,w
,h
), 0 );
1677 #elif defined(__WXMAC__)
1678 const int m_corner
= 1;
1680 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1682 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1683 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1684 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1686 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1689 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1690 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1692 dc
->SetPen( *wxWHITE_PEN
);
1693 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1694 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1695 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1696 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1698 const int m_corner
= 1;
1700 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1702 dc
->SetPen( *wxBLACK_PEN
);
1703 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1704 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1706 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1709 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1710 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1712 dc
->SetPen( *wxWHITE_PEN
);
1713 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1714 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1715 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1716 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1720 // shift the DC origin to match the position of the main window horz
1721 // scrollbar: this allows us to always use logical coords
1722 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1725 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1728 m_owner
->GetViewStart( &x
, NULL
);
1730 // account for the horz scrollbar offset
1731 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1734 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1736 #if defined(__WXGTK__)
1737 wxClientDC
dc( this );
1739 wxPaintDC
dc( this );
1747 dc
.SetFont( GetFont() );
1749 // width and height of the entire header window
1751 GetClientSize( &w
, &h
);
1752 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1754 dc
.SetBackgroundMode(wxTRANSPARENT
);
1756 // do *not* use the listctrl colour for headers - one day we will have a
1757 // function to set it separately
1758 //dc.SetTextForeground( *wxBLACK );
1759 dc
.SetTextForeground(wxSystemSettings::
1760 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1762 int x
= HEADER_OFFSET_X
;
1764 int numColumns
= m_owner
->GetColumnCount();
1766 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1768 m_owner
->GetColumn( i
, item
);
1769 int wCol
= item
.m_width
;
1771 // the width of the rect to draw: make it smaller to fit entirely
1772 // inside the column rect
1775 dc
.SetPen( *wxWHITE_PEN
);
1777 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1779 // see if we have enough space for the column label
1781 // for this we need the width of the text
1783 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1784 wLabel
+= 2*EXTRA_WIDTH
;
1786 // and the width of the icon, if any
1787 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1788 int ix
= 0, // init them just to suppress the compiler warnings
1790 const int image
= item
.m_image
;
1791 wxImageListType
*imageList
;
1794 imageList
= m_owner
->m_small_image_list
;
1797 imageList
->GetSize(image
, ix
, iy
);
1798 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1806 // ignore alignment if there is not enough space anyhow
1808 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1811 wxFAIL_MSG( _T("unknown list item format") );
1814 case wxLIST_FORMAT_LEFT
:
1818 case wxLIST_FORMAT_RIGHT
:
1819 xAligned
= x
+ cw
- wLabel
;
1822 case wxLIST_FORMAT_CENTER
:
1823 xAligned
= x
+ (cw
- wLabel
) / 2;
1828 // if we have an image, draw it on the right of the label
1835 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1836 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1837 wxIMAGELIST_DRAW_TRANSPARENT
1840 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1843 // draw the text clipping it so that it doesn't overwrite the column
1845 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1847 dc
.DrawText( item
.GetText(),
1848 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1856 void wxListHeaderWindow::DrawCurrent()
1858 int x1
= m_currentX
;
1860 m_owner
->ClientToScreen( &x1
, &y1
);
1862 int x2
= m_currentX
;
1864 m_owner
->GetClientSize( NULL
, &y2
);
1865 m_owner
->ClientToScreen( &x2
, &y2
);
1868 dc
.SetLogicalFunction( wxINVERT
);
1869 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1870 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1874 dc
.DrawLine( x1
, y1
, x2
, y2
);
1876 dc
.SetLogicalFunction( wxCOPY
);
1878 dc
.SetPen( wxNullPen
);
1879 dc
.SetBrush( wxNullBrush
);
1882 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1884 // we want to work with logical coords
1886 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1887 int y
= event
.GetY();
1891 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1893 // we don't draw the line beyond our window, but we allow dragging it
1896 GetClientSize( &w
, NULL
);
1897 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1900 // erase the line if it was drawn
1901 if ( m_currentX
< w
)
1904 if (event
.ButtonUp())
1907 m_isDragging
= FALSE
;
1909 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1910 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1917 m_currentX
= m_minX
+ 7;
1919 // draw in the new location
1920 if ( m_currentX
< w
)
1924 else // not dragging
1927 bool hit_border
= FALSE
;
1929 // end of the current column
1932 // find the column where this event occured
1934 countCol
= m_owner
->GetColumnCount();
1935 for (col
= 0; col
< countCol
; col
++)
1937 xpos
+= m_owner
->GetColumnWidth( col
);
1940 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1942 // near the column border
1949 // inside the column
1956 if ( col
== countCol
)
1959 if (event
.LeftDown() || event
.RightUp())
1961 if (hit_border
&& event
.LeftDown())
1963 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1964 event
.GetPosition()) )
1966 m_isDragging
= TRUE
;
1971 //else: column resizing was vetoed by the user code
1973 else // click on a column
1975 SendListEvent( event
.LeftDown()
1976 ? wxEVT_COMMAND_LIST_COL_CLICK
1977 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1978 event
.GetPosition());
1981 else if (event
.Moving())
1986 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1987 m_currentCursor
= m_resizeCursor
;
1991 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1992 m_currentCursor
= wxSTANDARD_CURSOR
;
1996 SetCursor(*m_currentCursor
);
2001 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2003 m_owner
->SetFocus();
2006 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2008 wxWindow
*parent
= GetParent();
2009 wxListEvent
le( type
, parent
->GetId() );
2010 le
.SetEventObject( parent
);
2011 le
.m_pointDrag
= pos
;
2013 // the position should be relative to the parent window, not
2014 // this one for compatibility with MSW and common sense: the
2015 // user code doesn't know anything at all about this header
2016 // window, so why should it get positions relative to it?
2017 le
.m_pointDrag
.y
-= GetSize().y
;
2019 le
.m_col
= m_column
;
2020 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2023 //-----------------------------------------------------------------------------
2024 // wxListRenameTimer (internal)
2025 //-----------------------------------------------------------------------------
2027 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2032 void wxListRenameTimer::Notify()
2034 m_owner
->OnRenameTimer();
2037 //-----------------------------------------------------------------------------
2038 // wxListTextCtrl (internal)
2039 //-----------------------------------------------------------------------------
2041 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2042 EVT_CHAR (wxListTextCtrl::OnChar
)
2043 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2044 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2047 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2048 : m_startValue(owner
->GetItemText(itemEdit
)),
2049 m_itemEdited(itemEdit
)
2054 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2056 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2057 &rectLabel
.x
, &rectLabel
.y
);
2059 (void)Create(owner
, wxID_ANY
, m_startValue
,
2060 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2061 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2064 void wxListTextCtrl::Finish()
2068 wxPendingDelete
.Append(this);
2072 m_owner
->SetFocus();
2076 bool wxListTextCtrl::AcceptChanges()
2078 const wxString value
= GetValue();
2080 if ( value
== m_startValue
)
2082 // nothing changed, always accept
2086 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2088 // vetoed by the user
2092 // accepted, do rename the item
2093 m_owner
->SetItemText(m_itemEdited
, value
);
2098 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2100 switch ( event
.m_keyCode
)
2103 if ( !AcceptChanges() )
2105 // vetoed by the user code
2108 //else: fall through
2112 m_owner
->OnRenameCancelled( m_itemEdited
);
2120 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2128 // auto-grow the textctrl:
2129 wxSize parentSize
= m_owner
->GetSize();
2130 wxPoint myPos
= GetPosition();
2131 wxSize mySize
= GetSize();
2133 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2134 if (myPos
.x
+ sx
> parentSize
.x
)
2135 sx
= parentSize
.x
- myPos
.x
;
2143 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2147 // We must finish regardless of success, otherwise we'll get focus problems
2150 if ( !AcceptChanges() )
2151 m_owner
->OnRenameCancelled( m_itemEdited
);
2157 //-----------------------------------------------------------------------------
2159 //-----------------------------------------------------------------------------
2161 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2163 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2164 EVT_PAINT (wxListMainWindow::OnPaint
)
2165 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2166 EVT_CHAR (wxListMainWindow::OnChar
)
2167 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2168 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2169 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2170 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2173 void wxListMainWindow::Init()
2178 m_lineTo
= (size_t)-1;
2184 m_small_image_list
= (wxImageListType
*) NULL
;
2185 m_normal_image_list
= (wxImageListType
*) NULL
;
2187 m_small_spacing
= 30;
2188 m_normal_spacing
= 40;
2192 m_isCreated
= FALSE
;
2194 m_lastOnSame
= FALSE
;
2195 m_renameTimer
= new wxListRenameTimer( this );
2199 m_lineBeforeLastClicked
= (size_t)-1;
2204 void wxListMainWindow::InitScrolling()
2206 if ( HasFlag(wxLC_REPORT
) )
2208 m_xScroll
= SCROLL_UNIT_X
;
2209 m_yScroll
= SCROLL_UNIT_Y
;
2213 m_xScroll
= SCROLL_UNIT_Y
;
2218 wxListMainWindow::wxListMainWindow()
2223 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2229 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2234 const wxString
&name
)
2235 : wxScrolledWindow( parent
, id
, pos
, size
,
2236 style
| wxHSCROLL
| wxVSCROLL
, name
)
2240 m_highlightBrush
= new wxBrush
2242 wxSystemSettings::GetColour
2244 wxSYS_COLOUR_HIGHLIGHT
2249 m_highlightUnfocusedBrush
= new wxBrush
2251 wxSystemSettings::GetColour
2253 wxSYS_COLOUR_BTNSHADOW
2262 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2264 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2267 wxListMainWindow::~wxListMainWindow()
2270 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2272 delete m_highlightBrush
;
2273 delete m_highlightUnfocusedBrush
;
2275 delete m_renameTimer
;
2278 void wxListMainWindow::CacheLineData(size_t line
)
2280 wxGenericListCtrl
*listctrl
= GetListCtrl();
2282 wxListLineData
*ld
= GetDummyLine();
2284 size_t countCol
= GetColumnCount();
2285 for ( size_t col
= 0; col
< countCol
; col
++ )
2287 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2290 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2291 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2294 wxListLineData
*wxListMainWindow::GetDummyLine() const
2296 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2298 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2300 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2302 // we need to recreate the dummy line if the number of columns in the
2303 // control changed as it would have the incorrect number of fields
2305 if ( !m_lines
.IsEmpty() &&
2306 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2308 self
->m_lines
.Clear();
2311 if ( m_lines
.IsEmpty() )
2313 wxListLineData
*line
= new wxListLineData(self
);
2314 self
->m_lines
.Add(line
);
2316 // don't waste extra memory -- there never going to be anything
2317 // else/more in this array
2318 self
->m_lines
.Shrink();
2324 // ----------------------------------------------------------------------------
2325 // line geometry (report mode only)
2326 // ----------------------------------------------------------------------------
2328 wxCoord
wxListMainWindow::GetLineHeight() const
2330 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2332 // we cache the line height as calling GetTextExtent() is slow
2333 if ( !m_lineHeight
)
2335 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2337 wxClientDC
dc( self
);
2338 dc
.SetFont( GetFont() );
2341 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2343 if ( y
< SCROLL_UNIT_Y
)
2346 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2350 m_small_image_list
->GetSize(0, iw
, ih
);
2355 self
->m_lineHeight
= y
+ LINE_SPACING
;
2358 return m_lineHeight
;
2361 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2363 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2365 return LINE_SPACING
+ line
*GetLineHeight();
2368 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2370 if ( !InReportView() )
2371 return GetLine(line
)->m_gi
->m_rectAll
;
2374 rect
.x
= HEADER_OFFSET_X
;
2375 rect
.y
= GetLineY(line
);
2376 rect
.width
= GetHeaderWidth();
2377 rect
.height
= GetLineHeight();
2382 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2384 if ( !InReportView() )
2385 return GetLine(line
)->m_gi
->m_rectLabel
;
2388 rect
.x
= HEADER_OFFSET_X
;
2389 rect
.y
= GetLineY(line
);
2390 rect
.width
= GetColumnWidth(0);
2391 rect
.height
= GetLineHeight();
2396 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2398 if ( !InReportView() )
2399 return GetLine(line
)->m_gi
->m_rectIcon
;
2401 wxListLineData
*ld
= GetLine(line
);
2402 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2405 rect
.x
= HEADER_OFFSET_X
;
2406 rect
.y
= GetLineY(line
);
2407 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2412 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2414 return InReportView() ? GetLineRect(line
)
2415 : GetLine(line
)->m_gi
->m_rectHighlight
;
2418 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2420 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2422 wxListLineData
*ld
= GetLine(line
);
2424 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2425 return wxLIST_HITTEST_ONITEMICON
;
2427 // VS: Testing for "ld->HasText() || InReportView()" instead of
2428 // "ld->HasText()" is needed to make empty lines in report view
2430 if ( ld
->HasText() || InReportView() )
2432 wxRect rect
= InReportView() ? GetLineRect(line
)
2433 : GetLineLabelRect(line
);
2435 if ( rect
.Inside(x
, y
) )
2436 return wxLIST_HITTEST_ONITEMLABEL
;
2442 // ----------------------------------------------------------------------------
2443 // highlight (selection) handling
2444 // ----------------------------------------------------------------------------
2446 bool wxListMainWindow::IsHighlighted(size_t line
) const
2450 return m_selStore
.IsSelected(line
);
2454 wxListLineData
*ld
= GetLine(line
);
2455 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2457 return ld
->IsHighlighted();
2461 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2467 wxArrayInt linesChanged
;
2468 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2471 // meny items changed state, refresh everything
2472 RefreshLines(lineFrom
, lineTo
);
2474 else // only a few items changed state, refresh only them
2476 size_t count
= linesChanged
.GetCount();
2477 for ( size_t n
= 0; n
< count
; n
++ )
2479 RefreshLine(linesChanged
[n
]);
2483 else // iterate over all items in non report view
2485 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2487 if ( HighlightLine(line
, highlight
) )
2495 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2501 changed
= m_selStore
.SelectItem(line
, highlight
);
2505 wxListLineData
*ld
= GetLine(line
);
2506 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2508 changed
= ld
->Highlight(highlight
);
2513 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2514 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2520 void wxListMainWindow::RefreshLine( size_t line
)
2522 if ( HasFlag(wxLC_REPORT
) )
2524 size_t visibleFrom
, visibleTo
;
2525 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2527 if ( line
< visibleFrom
|| line
> visibleTo
)
2531 wxRect rect
= GetLineRect(line
);
2533 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2534 RefreshRect( rect
);
2537 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2539 // we suppose that they are ordered by caller
2540 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2542 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2544 if ( HasFlag(wxLC_REPORT
) )
2546 size_t visibleFrom
, visibleTo
;
2547 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2549 if ( lineFrom
< visibleFrom
)
2550 lineFrom
= visibleFrom
;
2551 if ( lineTo
> visibleTo
)
2556 rect
.y
= GetLineY(lineFrom
);
2557 rect
.width
= GetClientSize().x
;
2558 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2560 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2561 RefreshRect( rect
);
2565 // TODO: this should be optimized...
2566 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2573 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2575 if ( HasFlag(wxLC_REPORT
) )
2577 size_t visibleFrom
, visibleTo
;
2578 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2580 if ( lineFrom
< visibleFrom
)
2581 lineFrom
= visibleFrom
;
2582 else if ( lineFrom
> visibleTo
)
2587 rect
.y
= GetLineY(lineFrom
);
2588 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2590 wxSize size
= GetClientSize();
2591 rect
.width
= size
.x
;
2592 // refresh till the bottom of the window
2593 rect
.height
= size
.y
- rect
.y
;
2595 RefreshRect( rect
);
2599 // TODO: how to do it more efficiently?
2604 void wxListMainWindow::RefreshSelected()
2610 if ( InReportView() )
2612 GetVisibleLinesRange(&from
, &to
);
2617 to
= GetItemCount() - 1;
2620 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2622 RefreshLine(m_current
);
2625 for ( size_t line
= from
; line
<= to
; line
++ )
2627 // NB: the test works as expected even if m_current == -1
2628 if ( line
!= m_current
&& IsHighlighted(line
) )
2635 void wxListMainWindow::Freeze()
2640 void wxListMainWindow::Thaw()
2642 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2644 if ( !--m_freezeCount
)
2650 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2652 // Note: a wxPaintDC must be constructed even if no drawing is
2653 // done (a Windows requirement).
2654 wxPaintDC
dc( this );
2656 if ( IsEmpty() || m_freezeCount
)
2658 // nothing to draw or not the moment to draw it
2664 // delay the repainting until we calculate all the items positions
2671 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2675 dc
.SetFont( GetFont() );
2677 if ( HasFlag(wxLC_REPORT
) )
2679 int lineHeight
= GetLineHeight();
2681 size_t visibleFrom
, visibleTo
;
2682 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2685 wxCoord xOrig
, yOrig
;
2686 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2688 // tell the caller cache to cache the data
2691 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2692 GetParent()->GetId());
2693 evCache
.SetEventObject( GetParent() );
2694 evCache
.m_oldItemIndex
= visibleFrom
;
2695 evCache
.m_itemIndex
= visibleTo
;
2696 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2699 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2701 rectLine
= GetLineRect(line
);
2703 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2704 rectLine
.width
, rectLine
.height
) )
2706 // don't redraw unaffected lines to avoid flicker
2710 GetLine(line
)->DrawInReportMode( &dc
,
2712 GetLineHighlightRect(line
),
2713 IsHighlighted(line
) );
2716 if ( HasFlag(wxLC_HRULES
) )
2718 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2719 wxSize clientSize
= GetClientSize();
2721 // Don't draw the first one
2722 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2725 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2726 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2727 clientSize
.x
- dev_x
, i
*lineHeight
);
2730 // Draw last horizontal rule
2731 if ( visibleTo
== GetItemCount() - 1 )
2734 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2735 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2736 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2740 // Draw vertical rules if required
2741 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2743 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2746 wxRect firstItemRect
;
2747 wxRect lastItemRect
;
2748 GetItemRect(visibleFrom
, firstItemRect
);
2749 GetItemRect(visibleTo
, lastItemRect
);
2750 int x
= firstItemRect
.GetX();
2752 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2753 for (col
= 0; col
< GetColumnCount(); col
++)
2755 int colWidth
= GetColumnWidth(col
);
2757 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2758 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2764 size_t count
= GetItemCount();
2765 for ( size_t i
= 0; i
< count
; i
++ )
2767 GetLine(i
)->Draw( &dc
);
2773 // don't draw rect outline under Max if we already have the background
2774 // color but under other platforms only draw it if we do: it is a bit
2775 // silly to draw "focus rect" if we don't have focus!
2780 #endif // __WXMAC__/!__WXMAC__
2782 dc
.SetPen( *wxBLACK_PEN
);
2783 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2784 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2791 void wxListMainWindow::HighlightAll( bool on
)
2793 if ( IsSingleSel() )
2795 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2797 // we just have one item to turn off
2798 if ( HasCurrent() && IsHighlighted(m_current
) )
2800 HighlightLine(m_current
, FALSE
);
2801 RefreshLine(m_current
);
2806 HighlightLines(0, GetItemCount() - 1, on
);
2810 void wxListMainWindow::SendNotify( size_t line
,
2811 wxEventType command
,
2814 wxListEvent
le( command
, GetParent()->GetId() );
2815 le
.SetEventObject( GetParent() );
2816 le
.m_itemIndex
= line
;
2818 // set only for events which have position
2819 if ( point
!= wxDefaultPosition
)
2820 le
.m_pointDrag
= point
;
2822 // don't try to get the line info for virtual list controls: the main
2823 // program has it anyhow and if we did it would result in accessing all
2824 // the lines, even those which are not visible now and this is precisely
2825 // what we're trying to avoid
2826 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2828 if ( line
!= (size_t)-1 )
2830 GetLine(line
)->GetItem( 0, le
.m_item
);
2832 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2834 //else: there may be no more such item
2836 GetParent()->GetEventHandler()->ProcessEvent( le
);
2839 void wxListMainWindow::ChangeCurrent(size_t current
)
2841 m_current
= current
;
2843 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2846 void wxListMainWindow::EditLabel( long item
)
2848 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2849 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2851 size_t itemEdit
= (size_t)item
;
2853 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2854 le
.SetEventObject( GetParent() );
2855 le
.m_itemIndex
= item
;
2856 wxListLineData
*data
= GetLine(itemEdit
);
2857 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2858 data
->GetItem( 0, le
.m_item
);
2859 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2861 // vetoed by user code
2865 // We have to call this here because the label in question might just have
2866 // been added and no screen update taken place.
2870 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2875 void wxListMainWindow::OnRenameTimer()
2877 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2879 EditLabel( m_current
);
2882 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2884 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2885 le
.SetEventObject( GetParent() );
2886 le
.m_itemIndex
= itemEdit
;
2888 wxListLineData
*data
= GetLine(itemEdit
);
2889 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2891 data
->GetItem( 0, le
.m_item
);
2892 le
.m_item
.m_text
= value
;
2893 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2897 #ifdef __VMS__ // Ignore unreacheable code
2898 # pragma message disable initnotreach
2901 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2903 // wxMSW seems not to notify the program about
2904 // cancelled label edits.
2907 // let owner know that the edit was cancelled
2908 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2910 // These only exist for wxTreeCtrl, which should probably be changed
2911 // le.m_editCancelled = TRUE;
2912 // le.m_label = wxEmptyString;
2914 le
.SetEventObject( GetParent() );
2915 le
.m_itemIndex
= itemEdit
;
2917 wxListLineData
*data
= GetLine(itemEdit
);
2918 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2920 data
->GetItem( 0, le
.m_item
);
2922 GetEventHandler()->ProcessEvent( le
);
2925 # pragma message enable initnotreach
2928 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2930 event
.SetEventObject( GetParent() );
2931 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2934 if ( !HasCurrent() || IsEmpty() )
2940 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2941 event
.ButtonDClick()) )
2944 int x
= event
.GetX();
2945 int y
= event
.GetY();
2946 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2948 // where did we hit it (if we did)?
2951 size_t count
= GetItemCount(),
2954 if ( HasFlag(wxLC_REPORT
) )
2956 current
= y
/ GetLineHeight();
2957 if ( current
< count
)
2958 hitResult
= HitTestLine(current
, x
, y
);
2962 // TODO: optimize it too! this is less simple than for report view but
2963 // enumerating all items is still not a way to do it!!
2964 for ( current
= 0; current
< count
; current
++ )
2966 hitResult
= HitTestLine(current
, x
, y
);
2972 if (event
.Dragging())
2974 if (m_dragCount
== 0)
2976 // we have to report the raw, physical coords as we want to be
2977 // able to call HitTest(event.m_pointDrag) from the user code to
2978 // get the item being dragged
2979 m_dragStart
= event
.GetPosition();
2984 if (m_dragCount
!= 3)
2987 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2988 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2990 wxListEvent
le( command
, GetParent()->GetId() );
2991 le
.SetEventObject( GetParent() );
2992 le
.m_itemIndex
= current
;
2993 le
.m_pointDrag
= m_dragStart
;
2994 GetParent()->GetEventHandler()->ProcessEvent( le
);
3005 // outside of any item
3009 bool forceClick
= FALSE
;
3010 if (event
.ButtonDClick())
3012 m_renameTimer
->Stop();
3013 m_lastOnSame
= FALSE
;
3015 if ( current
== m_lineLastClicked
)
3017 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3023 // the first click was on another item, so don't interpret this as
3024 // a double click, but as a simple click instead
3029 if (event
.LeftUp() && m_lastOnSame
)
3031 if ((current
== m_current
) &&
3032 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3033 HasFlag(wxLC_EDIT_LABELS
) )
3035 m_renameTimer
->Start( 100, TRUE
);
3037 m_lastOnSame
= FALSE
;
3039 else if (event
.RightDown())
3041 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3042 event
.GetPosition() );
3044 else if (event
.MiddleDown())
3046 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3048 else if ( event
.LeftDown() || forceClick
)
3050 m_lineBeforeLastClicked
= m_lineLastClicked
;
3051 m_lineLastClicked
= current
;
3053 size_t oldCurrent
= m_current
;
3055 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3057 HighlightAll( FALSE
);
3059 ChangeCurrent(current
);
3061 ReverseHighlight(m_current
);
3063 else // multi sel & either ctrl or shift is down
3065 if (event
.ControlDown())
3067 ChangeCurrent(current
);
3069 ReverseHighlight(m_current
);
3071 else if (event
.ShiftDown())
3073 ChangeCurrent(current
);
3075 size_t lineFrom
= oldCurrent
,
3078 if ( lineTo
< lineFrom
)
3081 lineFrom
= m_current
;
3084 HighlightLines(lineFrom
, lineTo
);
3086 else // !ctrl, !shift
3088 // test in the enclosing if should make it impossible
3089 wxFAIL_MSG( _T("how did we get here?") );
3093 if (m_current
!= oldCurrent
)
3095 RefreshLine( oldCurrent
);
3098 // forceClick is only set if the previous click was on another item
3099 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3103 void wxListMainWindow::MoveToItem(size_t item
)
3105 if ( item
== (size_t)-1 )
3108 wxRect rect
= GetLineRect(item
);
3110 int client_w
, client_h
;
3111 GetClientSize( &client_w
, &client_h
);
3113 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3114 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3116 if ( HasFlag(wxLC_REPORT
) )
3118 // the next we need the range of lines shown it might be different, so
3120 ResetVisibleLinesRange();
3122 if (rect
.y
< view_y
)
3123 Scroll( -1, rect
.y
/m_yScroll
);
3124 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3125 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3129 if (rect
.x
-view_x
< 5)
3130 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3131 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3132 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3136 // ----------------------------------------------------------------------------
3137 // keyboard handling
3138 // ----------------------------------------------------------------------------
3140 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3142 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3143 _T("invalid item index in OnArrowChar()") );
3145 size_t oldCurrent
= m_current
;
3147 // in single selection we just ignore Shift as we can't select several
3149 if ( event
.ShiftDown() && !IsSingleSel() )
3151 ChangeCurrent(newCurrent
);
3153 // select all the items between the old and the new one
3154 if ( oldCurrent
> newCurrent
)
3156 newCurrent
= oldCurrent
;
3157 oldCurrent
= m_current
;
3160 HighlightLines(oldCurrent
, newCurrent
);
3164 // all previously selected items are unselected unless ctrl is held
3165 if ( !event
.ControlDown() )
3166 HighlightAll(FALSE
);
3168 ChangeCurrent(newCurrent
);
3170 // refresh the old focus to remove it
3171 RefreshLine( oldCurrent
);
3173 if ( !event
.ControlDown() )
3175 HighlightLine( m_current
, TRUE
);
3179 RefreshLine( m_current
);
3184 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3186 wxWindow
*parent
= GetParent();
3188 /* we propagate the key event up */
3189 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3190 ke
.m_shiftDown
= event
.m_shiftDown
;
3191 ke
.m_controlDown
= event
.m_controlDown
;
3192 ke
.m_altDown
= event
.m_altDown
;
3193 ke
.m_metaDown
= event
.m_metaDown
;
3194 ke
.m_keyCode
= event
.m_keyCode
;
3197 ke
.SetEventObject( parent
);
3198 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3203 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3205 wxWindow
*parent
= GetParent();
3207 /* we send a list_key event up */
3210 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3211 le
.m_itemIndex
= m_current
;
3212 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3213 le
.m_code
= event
.GetKeyCode();
3214 le
.SetEventObject( parent
);
3215 parent
->GetEventHandler()->ProcessEvent( le
);
3218 /* we propagate the char event up */
3219 wxKeyEvent
ke( wxEVT_CHAR
);
3220 ke
.m_shiftDown
= event
.m_shiftDown
;
3221 ke
.m_controlDown
= event
.m_controlDown
;
3222 ke
.m_altDown
= event
.m_altDown
;
3223 ke
.m_metaDown
= event
.m_metaDown
;
3224 ke
.m_keyCode
= event
.m_keyCode
;
3227 ke
.SetEventObject( parent
);
3228 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3230 if (event
.GetKeyCode() == WXK_TAB
)
3232 wxNavigationKeyEvent nevent
;
3233 nevent
.SetWindowChange( event
.ControlDown() );
3234 nevent
.SetDirection( !event
.ShiftDown() );
3235 nevent
.SetEventObject( GetParent()->GetParent() );
3236 nevent
.SetCurrentFocus( m_parent
);
3237 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3241 /* no item -> nothing to do */
3248 switch (event
.GetKeyCode())
3251 if ( m_current
> 0 )
3252 OnArrowChar( m_current
- 1, event
);
3256 if ( m_current
< (size_t)GetItemCount() - 1 )
3257 OnArrowChar( m_current
+ 1, event
);
3262 OnArrowChar( GetItemCount() - 1, event
);
3267 OnArrowChar( 0, event
);
3273 if ( HasFlag(wxLC_REPORT
) )
3275 steps
= m_linesPerPage
- 1;
3279 steps
= m_current
% m_linesPerPage
;
3282 int index
= m_current
- steps
;
3286 OnArrowChar( index
, event
);
3293 if ( HasFlag(wxLC_REPORT
) )
3295 steps
= m_linesPerPage
- 1;
3299 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3302 size_t index
= m_current
+ steps
;
3303 size_t count
= GetItemCount();
3304 if ( index
>= count
)
3307 OnArrowChar( index
, event
);
3312 if ( !HasFlag(wxLC_REPORT
) )
3314 int index
= m_current
- m_linesPerPage
;
3318 OnArrowChar( index
, event
);
3323 if ( !HasFlag(wxLC_REPORT
) )
3325 size_t index
= m_current
+ m_linesPerPage
;
3327 size_t count
= GetItemCount();
3328 if ( index
>= count
)
3331 OnArrowChar( index
, event
);
3336 if ( IsSingleSel() )
3338 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3340 if ( IsHighlighted(m_current
) )
3342 // don't unselect the item in single selection mode
3345 //else: select it in ReverseHighlight() below if unselected
3348 ReverseHighlight(m_current
);
3353 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3361 // ----------------------------------------------------------------------------
3363 // ----------------------------------------------------------------------------
3365 void wxListMainWindow::SetFocus()
3367 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3368 // overrides SetFocus in such way that it does never change focus from
3369 // panel's child to the panel itself. Unfortunately, we must be able to change
3370 // focus to the panel from wxListTextCtrl because the text control should
3371 // disappear when the user clicks outside it.
3373 wxWindow
*oldFocus
= FindFocus();
3375 if ( oldFocus
&& oldFocus
->GetParent() == this )
3377 wxWindow::SetFocus();
3381 wxScrolledWindow::SetFocus();
3385 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3387 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3388 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3389 // which are already drawn correctly resulting in horrible flicker - avoid
3401 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3402 event
.SetEventObject( GetParent() );
3403 GetParent()->GetEventHandler()->ProcessEvent( event
);
3406 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3413 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3415 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3417 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3419 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3421 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3423 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3425 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3427 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3429 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3433 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3435 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3437 m_normal_image_list
->GetSize( index
, width
, height
);
3439 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3441 m_small_image_list
->GetSize( index
, width
, height
);
3443 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3445 m_small_image_list
->GetSize( index
, width
, height
);
3447 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3449 m_small_image_list
->GetSize( index
, width
, height
);
3458 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3460 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3461 dc
.SetFont( GetFont() );
3464 dc
.GetTextExtent( s
, &lw
, NULL
);
3466 return lw
+ AUTOSIZE_COL_MARGIN
;
3469 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3473 // calc the spacing from the icon size
3476 if ((imageList
) && (imageList
->GetImageCount()) )
3478 imageList
->GetSize(0, width
, height
);
3481 if (which
== wxIMAGE_LIST_NORMAL
)
3483 m_normal_image_list
= imageList
;
3484 m_normal_spacing
= width
+ 8;
3487 if (which
== wxIMAGE_LIST_SMALL
)
3489 m_small_image_list
= imageList
;
3490 m_small_spacing
= width
+ 14;
3491 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3495 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3500 m_small_spacing
= spacing
;
3504 m_normal_spacing
= spacing
;
3508 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3510 return isSmall
? m_small_spacing
: m_normal_spacing
;
3513 // ----------------------------------------------------------------------------
3515 // ----------------------------------------------------------------------------
3517 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3519 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3521 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3523 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3524 item
.m_width
= GetTextLength( item
.m_text
);
3526 wxListHeaderData
*column
= node
->GetData();
3527 column
->SetItem( item
);
3529 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3531 headerWin
->m_dirty
= TRUE
;
3535 // invalidate it as it has to be recalculated
3539 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3541 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3542 _T("invalid column index") );
3544 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3545 _T("SetColumnWidth() can only be called in report mode.") );
3548 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3550 headerWin
->m_dirty
= TRUE
;
3552 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3553 wxCHECK_RET( node
, _T("no column?") );
3555 wxListHeaderData
*column
= node
->GetData();
3557 size_t count
= GetItemCount();
3559 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3561 width
= GetTextLength(column
->GetText());
3563 else if ( width
== wxLIST_AUTOSIZE
)
3567 // TODO: determine the max width somehow...
3568 width
= WIDTH_COL_DEFAULT
;
3572 wxClientDC
dc(this);
3573 dc
.SetFont( GetFont() );
3575 int max
= AUTOSIZE_COL_MARGIN
;
3577 for ( size_t i
= 0; i
< count
; i
++ )
3579 wxListLineData
*line
= GetLine(i
);
3580 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3582 wxCHECK_RET( n
, _T("no subitem?") );
3584 wxListItemData
*item
= n
->GetData();
3587 if (item
->HasImage())
3590 GetImageSize( item
->GetImage(), ix
, iy
);
3594 if (item
->HasText())
3597 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3605 width
= max
+ AUTOSIZE_COL_MARGIN
;
3609 column
->SetWidth( width
);
3611 // invalidate it as it has to be recalculated
3615 int wxListMainWindow::GetHeaderWidth() const
3617 if ( !m_headerWidth
)
3619 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3621 size_t count
= GetColumnCount();
3622 for ( size_t col
= 0; col
< count
; col
++ )
3624 self
->m_headerWidth
+= GetColumnWidth(col
);
3628 return m_headerWidth
;
3631 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3633 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3634 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3636 wxListHeaderData
*column
= node
->GetData();
3637 column
->GetItem( item
);
3640 int wxListMainWindow::GetColumnWidth( int col
) const
3642 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3643 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3645 wxListHeaderData
*column
= node
->GetData();
3646 return column
->GetWidth();
3649 // ----------------------------------------------------------------------------
3651 // ----------------------------------------------------------------------------
3653 void wxListMainWindow::SetItem( wxListItem
&item
)
3655 long id
= item
.m_itemId
;
3656 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3657 _T("invalid item index in SetItem") );
3661 wxListLineData
*line
= GetLine((size_t)id
);
3662 line
->SetItem( item
.m_col
, item
);
3665 // update the item on screen
3667 GetItemRect(id
, rectItem
);
3668 RefreshRect(rectItem
);
3671 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3673 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3674 _T("invalid list ctrl item index in SetItem") );
3676 size_t oldCurrent
= m_current
;
3677 size_t item
= (size_t)litem
; // safe because of the check above
3679 // do we need to change the focus?
3680 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3682 if ( state
& wxLIST_STATE_FOCUSED
)
3684 // don't do anything if this item is already focused
3685 if ( item
!= m_current
)
3687 ChangeCurrent(item
);
3689 if ( oldCurrent
!= (size_t)-1 )
3691 if ( IsSingleSel() )
3693 HighlightLine(oldCurrent
, FALSE
);
3696 RefreshLine(oldCurrent
);
3699 RefreshLine( m_current
);
3704 // don't do anything if this item is not focused
3705 if ( item
== m_current
)
3709 if ( IsSingleSel() )
3711 // we must unselect the old current item as well or we
3712 // might end up with more than one selected item in a
3713 // single selection control
3714 HighlightLine(oldCurrent
, FALSE
);
3717 RefreshLine( oldCurrent
);
3722 // do we need to change the selection state?
3723 if ( stateMask
& wxLIST_STATE_SELECTED
)
3725 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3727 if ( IsSingleSel() )
3731 // selecting the item also makes it the focused one in the
3733 if ( m_current
!= item
)
3735 ChangeCurrent(item
);
3737 if ( oldCurrent
!= (size_t)-1 )
3739 HighlightLine( oldCurrent
, FALSE
);
3740 RefreshLine( oldCurrent
);
3746 // only the current item may be selected anyhow
3747 if ( item
!= m_current
)
3752 if ( HighlightLine(item
, on
) )
3759 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3761 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3762 _T("invalid list ctrl item index in GetItemState()") );
3764 int ret
= wxLIST_STATE_DONTCARE
;
3766 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3768 if ( (size_t)item
== m_current
)
3769 ret
|= wxLIST_STATE_FOCUSED
;
3772 if ( stateMask
& wxLIST_STATE_SELECTED
)
3774 if ( IsHighlighted(item
) )
3775 ret
|= wxLIST_STATE_SELECTED
;
3781 void wxListMainWindow::GetItem( wxListItem
&item
) const
3783 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3784 _T("invalid item index in GetItem") );
3786 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3787 line
->GetItem( item
.m_col
, item
);
3790 // ----------------------------------------------------------------------------
3792 // ----------------------------------------------------------------------------
3794 size_t wxListMainWindow::GetItemCount() const
3796 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3799 void wxListMainWindow::SetItemCount(long count
)
3801 m_selStore
.SetItemCount(count
);
3802 m_countVirt
= count
;
3804 ResetVisibleLinesRange();
3806 // scrollbars must be reset
3810 int wxListMainWindow::GetSelectedItemCount() const
3812 // deal with the quick case first
3813 if ( IsSingleSel() )
3815 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3818 // virtual controls remmebers all its selections itself
3820 return m_selStore
.GetSelectedCount();
3822 // TODO: we probably should maintain the number of items selected even for
3823 // non virtual controls as enumerating all lines is really slow...
3824 size_t countSel
= 0;
3825 size_t count
= GetItemCount();
3826 for ( size_t line
= 0; line
< count
; line
++ )
3828 if ( GetLine(line
)->IsHighlighted() )
3835 // ----------------------------------------------------------------------------
3836 // item position/size
3837 // ----------------------------------------------------------------------------
3839 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3841 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3842 _T("invalid index in GetItemRect") );
3844 rect
= GetLineRect((size_t)index
);
3846 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3849 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3852 GetItemRect(item
, rect
);
3860 // ----------------------------------------------------------------------------
3861 // geometry calculation
3862 // ----------------------------------------------------------------------------
3864 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3866 wxClientDC
dc( this );
3867 dc
.SetFont( GetFont() );
3870 if ( HasFlag(wxLC_ICON
) )
3871 iconSpacing
= m_normal_spacing
;
3872 else if ( HasFlag(wxLC_SMALL_ICON
) )
3873 iconSpacing
= m_small_spacing
;
3877 // Note that we do not call GetClientSize() here but
3878 // GetSize() and substract the border size for sunken
3879 // borders manually. This is technically incorrect,
3880 // but we need to know the client area's size WITHOUT
3881 // scrollbars here. Since we don't know if there are
3882 // any scrollbars, we use GetSize() instead. Another
3883 // solution would be to call SetScrollbars() here to
3884 // remove the scrollbars and call GetClientSize() then,
3885 // but this might result in flicker and - worse - will
3886 // reset the scrollbars to 0 which is not good at all
3887 // if you resize a dialog/window, but don't want to
3888 // reset the window scrolling. RR.
3889 // Furthermore, we actually do NOT subtract the border
3890 // width as 2 pixels is just the extra space which we
3891 // need around the actual content in the window. Other-
3892 // wise the text would e.g. touch the upper border. RR.
3895 GetSize( &clientWidth
, &clientHeight
);
3897 if ( HasFlag(wxLC_REPORT
) )
3899 // all lines have the same height
3900 int lineHeight
= GetLineHeight();
3902 // scroll one line per step
3903 m_yScroll
= lineHeight
;
3905 size_t lineCount
= GetItemCount();
3906 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3908 m_linesPerPage
= clientHeight
/ lineHeight
;
3910 ResetVisibleLinesRange();
3912 SetScrollbars( m_xScroll
, m_yScroll
,
3913 GetHeaderWidth() / m_xScroll
,
3914 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3915 GetScrollPos(wxHORIZONTAL
),
3916 GetScrollPos(wxVERTICAL
),
3921 // at first we try without any scrollbar. if the items don't
3922 // fit into the window, we recalculate after subtracting an
3923 // approximated 15 pt for the horizontal scrollbar
3925 int entireWidth
= 0;
3927 for (int tries
= 0; tries
< 2; tries
++)
3929 // We start with 4 for the border around all items
3934 // Now we have decided that the items do not fit into the
3935 // client area. Unfortunately, wxWindows sometimes thinks
3936 // that it does fit and therefore NO horizontal scrollbar
3937 // is inserted. This looks ugly, so we fudge here and make
3938 // the calculated width bigger than was actually has been
3939 // calculated. This ensures that wxScrolledWindows puts
3940 // a scrollbar at the bottom of its client area.
3941 entireWidth
+= SCROLL_UNIT_X
;
3944 // Start at 2,2 so the text does not touch the border
3949 int currentlyVisibleLines
= 0;
3951 size_t count
= GetItemCount();
3952 for (size_t i
= 0; i
< count
; i
++)
3954 currentlyVisibleLines
++;
3955 wxListLineData
*line
= GetLine(i
);
3956 line
->CalculateSize( &dc
, iconSpacing
);
3957 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
3959 wxSize sizeLine
= GetLineSize(i
);
3961 if ( maxWidth
< sizeLine
.x
)
3962 maxWidth
= sizeLine
.x
;
3965 if (currentlyVisibleLines
> m_linesPerPage
)
3966 m_linesPerPage
= currentlyVisibleLines
;
3968 // Assume that the size of the next one is the same... (FIXME)
3969 if ( y
+ sizeLine
.y
>= clientHeight
)
3971 currentlyVisibleLines
= 0;
3974 entireWidth
+= maxWidth
+6;
3978 // We have reached the last item.
3979 if ( i
== count
- 1 )
3980 entireWidth
+= maxWidth
;
3982 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
3984 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
3986 currentlyVisibleLines
= 0;
3990 if ( i
== count
- 1 )
3991 tries
= 1; // Everything fits, no second try required.
3995 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3996 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4001 // FIXME: why should we call it from here?
4008 void wxListMainWindow::RefreshAll()
4013 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4014 if ( headerWin
&& headerWin
->m_dirty
)
4016 headerWin
->m_dirty
= FALSE
;
4017 headerWin
->Refresh();
4021 void wxListMainWindow::UpdateCurrent()
4023 if ( !HasCurrent() && !IsEmpty() )
4029 long wxListMainWindow::GetNextItem( long item
,
4030 int WXUNUSED(geometry
),
4034 max
= GetItemCount();
4035 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4036 _T("invalid listctrl index in GetNextItem()") );
4038 // notice that we start with the next item (or the first one if item == -1)
4039 // and this is intentional to allow writing a simple loop to iterate over
4040 // all selected items
4044 // this is not an error because the index was ok initially, just no
4055 size_t count
= GetItemCount();
4056 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4058 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4061 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4068 // ----------------------------------------------------------------------------
4070 // ----------------------------------------------------------------------------
4072 void wxListMainWindow::DeleteItem( long lindex
)
4074 size_t count
= GetItemCount();
4076 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4077 _T("invalid item index in DeleteItem") );
4079 size_t index
= (size_t)lindex
;
4081 // we don't need to adjust the index for the previous items
4082 if ( HasCurrent() && m_current
>= index
)
4084 // if the current item is being deleted, we want the next one to
4085 // become selected - unless there is no next one - so don't adjust
4086 // m_current in this case
4087 if ( m_current
!= index
|| m_current
== count
- 1 )
4093 if ( InReportView() )
4095 ResetVisibleLinesRange();
4102 m_selStore
.OnItemDelete(index
);
4106 m_lines
.RemoveAt( index
);
4109 // we need to refresh the (vert) scrollbar as the number of items changed
4112 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4114 RefreshAfter(index
);
4117 void wxListMainWindow::DeleteColumn( int col
)
4119 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4121 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4124 delete node
->GetData();
4125 m_columns
.Erase( node
);
4129 // update all the items
4130 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4132 wxListLineData
* const line
= GetLine(i
);
4133 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4134 delete n
->GetData();
4135 line
->m_items
.Erase(n
);
4139 // invalidate it as it has to be recalculated
4143 void wxListMainWindow::DoDeleteAllItems()
4147 // nothing to do - in particular, don't send the event
4153 // to make the deletion of all items faster, we don't send the
4154 // notifications for each item deletion in this case but only one event
4155 // for all of them: this is compatible with wxMSW and documented in
4156 // DeleteAllItems() description
4158 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4159 event
.SetEventObject( GetParent() );
4160 GetParent()->GetEventHandler()->ProcessEvent( event
);
4169 if ( InReportView() )
4171 ResetVisibleLinesRange();
4177 void wxListMainWindow::DeleteAllItems()
4181 RecalculatePositions();
4184 void wxListMainWindow::DeleteEverything()
4186 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4191 // ----------------------------------------------------------------------------
4192 // scanning for an item
4193 // ----------------------------------------------------------------------------
4195 void wxListMainWindow::EnsureVisible( long index
)
4197 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4198 _T("invalid index in EnsureVisible") );
4200 // We have to call this here because the label in question might just have
4201 // been added and its position is not known yet
4204 RecalculatePositions(TRUE
/* no refresh */);
4207 MoveToItem((size_t)index
);
4210 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4217 size_t count
= GetItemCount();
4218 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4220 wxListLineData
*line
= GetLine(i
);
4221 if ( line
->GetText(0) == tmp
)
4228 long wxListMainWindow::FindItem(long start
, long data
)
4234 size_t count
= GetItemCount();
4235 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4237 wxListLineData
*line
= GetLine(i
);
4239 line
->GetItem( 0, item
);
4240 if (item
.m_data
== data
)
4247 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4249 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4251 size_t count
= GetItemCount();
4253 if ( HasFlag(wxLC_REPORT
) )
4255 size_t current
= y
/ GetLineHeight();
4256 if ( current
< count
)
4258 flags
= HitTestLine(current
, x
, y
);
4265 // TODO: optimize it too! this is less simple than for report view but
4266 // enumerating all items is still not a way to do it!!
4267 for ( size_t current
= 0; current
< count
; current
++ )
4269 flags
= HitTestLine(current
, x
, y
);
4278 // ----------------------------------------------------------------------------
4280 // ----------------------------------------------------------------------------
4282 void wxListMainWindow::InsertItem( wxListItem
&item
)
4284 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4286 size_t count
= GetItemCount();
4287 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4288 _T("invalid item index") );
4290 size_t id
= item
.m_itemId
;
4295 if ( HasFlag(wxLC_REPORT
) )
4298 ResetVisibleLinesRange();
4300 else if ( HasFlag(wxLC_LIST
) )
4302 else if ( HasFlag(wxLC_ICON
) )
4304 else if ( HasFlag(wxLC_SMALL_ICON
) )
4305 mode
= wxLC_ICON
; // no typo
4308 wxFAIL_MSG( _T("unknown mode") );
4311 wxListLineData
*line
= new wxListLineData(this);
4313 line
->SetItem( 0, item
);
4315 m_lines
.Insert( line
, id
);
4319 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4321 RefreshLines(id
, GetItemCount() - 1);
4324 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4327 if ( HasFlag(wxLC_REPORT
) )
4329 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4330 item
.m_width
= GetTextLength( item
.m_text
);
4332 wxListHeaderData
*column
= new wxListHeaderData( item
);
4333 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4336 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4337 m_columns
.Insert( node
, column
);
4341 m_columns
.Append( column
);
4346 // update all the items
4347 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4349 wxListLineData
* const line
= GetLine(i
);
4350 wxListItemData
* const data
= new wxListItemData(this);
4352 line
->m_items
.Insert(col
, data
);
4354 line
->m_items
.Append(data
);
4358 // invalidate it as it has to be recalculated
4363 // ----------------------------------------------------------------------------
4365 // ----------------------------------------------------------------------------
4367 wxListCtrlCompare list_ctrl_compare_func_2
;
4368 long list_ctrl_compare_data
;
4370 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4372 wxListLineData
*line1
= *arg1
;
4373 wxListLineData
*line2
= *arg2
;
4375 line1
->GetItem( 0, item
);
4376 long data1
= item
.m_data
;
4377 line2
->GetItem( 0, item
);
4378 long data2
= item
.m_data
;
4379 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4382 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4384 list_ctrl_compare_func_2
= fn
;
4385 list_ctrl_compare_data
= data
;
4386 m_lines
.Sort( list_ctrl_compare_func_1
);
4390 // ----------------------------------------------------------------------------
4392 // ----------------------------------------------------------------------------
4394 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4396 // update our idea of which lines are shown when we redraw the window the
4398 ResetVisibleLinesRange();
4401 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4402 wxScrolledWindow::OnScroll(event
);
4404 HandleOnScroll( event
);
4407 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4409 wxGenericListCtrl
* lc
= GetListCtrl();
4410 wxCHECK_RET( lc
, _T("no listctrl window?") );
4412 lc
->m_headerWin
->Refresh();
4413 lc
->m_headerWin
->Update();
4417 int wxListMainWindow::GetCountPerPage() const
4419 if ( !m_linesPerPage
)
4421 wxConstCast(this, wxListMainWindow
)->
4422 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4425 return m_linesPerPage
;
4428 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4430 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4432 if ( m_lineFrom
== (size_t)-1 )
4434 size_t count
= GetItemCount();
4437 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4439 // this may happen if SetScrollbars() hadn't been called yet
4440 if ( m_lineFrom
>= count
)
4441 m_lineFrom
= count
- 1;
4443 // we redraw one extra line but this is needed to make the redrawing
4444 // logic work when there is a fractional number of lines on screen
4445 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4446 if ( m_lineTo
>= count
)
4447 m_lineTo
= count
- 1;
4449 else // empty control
4452 m_lineTo
= (size_t)-1;
4456 wxASSERT_MSG( IsEmpty() ||
4457 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4458 _T("GetVisibleLinesRange() returns incorrect result") );
4466 // -------------------------------------------------------------------------------------
4467 // wxGenericListCtrl
4468 // -------------------------------------------------------------------------------------
4470 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4472 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4473 EVT_SIZE(wxGenericListCtrl::OnSize
)
4476 wxGenericListCtrl::wxGenericListCtrl()
4478 m_imageListNormal
= (wxImageListType
*) NULL
;
4479 m_imageListSmall
= (wxImageListType
*) NULL
;
4480 m_imageListState
= (wxImageListType
*) NULL
;
4482 m_ownsImageListNormal
=
4483 m_ownsImageListSmall
=
4484 m_ownsImageListState
= FALSE
;
4486 m_mainWin
= (wxListMainWindow
*) NULL
;
4487 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4490 wxGenericListCtrl::~wxGenericListCtrl()
4492 if (m_ownsImageListNormal
)
4493 delete m_imageListNormal
;
4494 if (m_ownsImageListSmall
)
4495 delete m_imageListSmall
;
4496 if (m_ownsImageListState
)
4497 delete m_imageListState
;
4500 void wxGenericListCtrl::CreateHeaderWindow()
4502 m_headerWin
= new wxListHeaderWindow
4504 this, -1, m_mainWin
,
4506 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4511 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4516 const wxValidator
&validator
,
4517 const wxString
&name
)
4521 m_imageListState
= (wxImageListType
*) NULL
;
4522 m_ownsImageListNormal
=
4523 m_ownsImageListSmall
=
4524 m_ownsImageListState
= FALSE
;
4526 m_mainWin
= (wxListMainWindow
*) NULL
;
4527 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4529 if ( !(style
& wxLC_MASK_TYPE
) )
4531 style
= style
| wxLC_LIST
;
4534 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4537 // don't create the inner window with the border
4538 style
&= ~wxBORDER_MASK
;
4540 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4542 if ( HasFlag(wxLC_REPORT
) )
4544 CreateHeaderWindow();
4546 if ( HasFlag(wxLC_NO_HEADER
) )
4548 // VZ: why do we create it at all then?
4549 m_headerWin
->Show( FALSE
);
4556 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4558 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4559 _T("wxLC_VIRTUAL can't be [un]set") );
4561 long flag
= GetWindowStyle();
4565 if (style
& wxLC_MASK_TYPE
)
4566 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4567 if (style
& wxLC_MASK_ALIGN
)
4568 flag
&= ~wxLC_MASK_ALIGN
;
4569 if (style
& wxLC_MASK_SORT
)
4570 flag
&= ~wxLC_MASK_SORT
;
4582 SetWindowStyleFlag( flag
);
4585 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4589 m_mainWin
->DeleteEverything();
4591 // has the header visibility changed?
4592 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4593 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4595 if ( hasHeader
!= willHaveHeader
)
4602 // don't delete, just hide, as we can reuse it later
4603 m_headerWin
->Show(FALSE
);
4605 //else: nothing to do
4607 else // must show header
4611 CreateHeaderWindow();
4613 else // already have it, just show
4615 m_headerWin
->Show( TRUE
);
4619 ResizeReportView(willHaveHeader
);
4623 wxWindow::SetWindowStyleFlag( flag
);
4626 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4628 m_mainWin
->GetColumn( col
, item
);
4632 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4634 m_mainWin
->SetColumn( col
, item
);
4638 int wxGenericListCtrl::GetColumnWidth( int col
) const
4640 return m_mainWin
->GetColumnWidth( col
);
4643 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4645 m_mainWin
->SetColumnWidth( col
, width
);
4649 int wxGenericListCtrl::GetCountPerPage() const
4651 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4654 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4656 m_mainWin
->GetItem( info
);
4660 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4662 m_mainWin
->SetItem( info
);
4666 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4669 info
.m_text
= label
;
4670 info
.m_mask
= wxLIST_MASK_TEXT
;
4671 info
.m_itemId
= index
;
4675 info
.m_image
= imageId
;
4676 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4678 m_mainWin
->SetItem(info
);
4682 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4684 return m_mainWin
->GetItemState( item
, stateMask
);
4687 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4689 m_mainWin
->SetItemState( item
, state
, stateMask
);
4693 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4696 info
.m_image
= image
;
4697 info
.m_mask
= wxLIST_MASK_IMAGE
;
4698 info
.m_itemId
= item
;
4699 m_mainWin
->SetItem( info
);
4703 wxString
wxGenericListCtrl::GetItemText( long item
) const
4705 return m_mainWin
->GetItemText(item
);
4708 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4710 m_mainWin
->SetItemText(item
, str
);
4713 long wxGenericListCtrl::GetItemData( long item
) const
4716 info
.m_itemId
= item
;
4717 m_mainWin
->GetItem( info
);
4721 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4724 info
.m_mask
= wxLIST_MASK_DATA
;
4725 info
.m_itemId
= item
;
4727 m_mainWin
->SetItem( info
);
4731 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4733 m_mainWin
->GetItemRect( item
, rect
);
4734 if ( m_mainWin
->HasHeader() )
4735 rect
.y
+= HEADER_HEIGHT
+ 1;
4739 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4741 m_mainWin
->GetItemPosition( item
, pos
);
4745 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4750 int wxGenericListCtrl::GetItemCount() const
4752 return m_mainWin
->GetItemCount();
4755 int wxGenericListCtrl::GetColumnCount() const
4757 return m_mainWin
->GetColumnCount();
4760 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4762 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4765 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4767 return m_mainWin
->GetItemSpacing( isSmall
);
4770 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4773 info
.m_itemId
= item
;
4774 info
.SetTextColour( col
);
4775 m_mainWin
->SetItem( info
);
4778 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4781 info
.m_itemId
= item
;
4782 m_mainWin
->GetItem( info
);
4783 return info
.GetTextColour();
4786 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4789 info
.m_itemId
= item
;
4790 info
.SetBackgroundColour( col
);
4791 m_mainWin
->SetItem( info
);
4794 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4797 info
.m_itemId
= item
;
4798 m_mainWin
->GetItem( info
);
4799 return info
.GetBackgroundColour();
4802 int wxGenericListCtrl::GetSelectedItemCount() const
4804 return m_mainWin
->GetSelectedItemCount();
4807 wxColour
wxGenericListCtrl::GetTextColour() const
4809 return GetForegroundColour();
4812 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4814 SetForegroundColour(col
);
4817 long wxGenericListCtrl::GetTopItem() const
4820 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4824 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4826 return m_mainWin
->GetNextItem( item
, geom
, state
);
4829 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4831 if (which
== wxIMAGE_LIST_NORMAL
)
4833 return m_imageListNormal
;
4835 else if (which
== wxIMAGE_LIST_SMALL
)
4837 return m_imageListSmall
;
4839 else if (which
== wxIMAGE_LIST_STATE
)
4841 return m_imageListState
;
4843 return (wxImageListType
*) NULL
;
4846 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4848 if ( which
== wxIMAGE_LIST_NORMAL
)
4850 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4851 m_imageListNormal
= imageList
;
4852 m_ownsImageListNormal
= FALSE
;
4854 else if ( which
== wxIMAGE_LIST_SMALL
)
4856 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4857 m_imageListSmall
= imageList
;
4858 m_ownsImageListSmall
= FALSE
;
4860 else if ( which
== wxIMAGE_LIST_STATE
)
4862 if (m_ownsImageListState
) delete m_imageListState
;
4863 m_imageListState
= imageList
;
4864 m_ownsImageListState
= FALSE
;
4867 m_mainWin
->SetImageList( imageList
, which
);
4870 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4872 SetImageList(imageList
, which
);
4873 if ( which
== wxIMAGE_LIST_NORMAL
)
4874 m_ownsImageListNormal
= TRUE
;
4875 else if ( which
== wxIMAGE_LIST_SMALL
)
4876 m_ownsImageListSmall
= TRUE
;
4877 else if ( which
== wxIMAGE_LIST_STATE
)
4878 m_ownsImageListState
= TRUE
;
4881 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4886 bool wxGenericListCtrl::DeleteItem( long item
)
4888 m_mainWin
->DeleteItem( item
);
4892 bool wxGenericListCtrl::DeleteAllItems()
4894 m_mainWin
->DeleteAllItems();
4898 bool wxGenericListCtrl::DeleteAllColumns()
4900 size_t count
= m_mainWin
->m_columns
.GetCount();
4901 for ( size_t n
= 0; n
< count
; n
++ )
4907 void wxGenericListCtrl::ClearAll()
4909 m_mainWin
->DeleteEverything();
4912 bool wxGenericListCtrl::DeleteColumn( int col
)
4914 m_mainWin
->DeleteColumn( col
);
4916 // if we don't have the header any longer, we need to relayout the window
4917 if ( !GetColumnCount() )
4919 ResizeReportView(FALSE
/* no header */);
4925 void wxGenericListCtrl::Edit( long item
)
4927 m_mainWin
->EditLabel( item
);
4930 bool wxGenericListCtrl::EnsureVisible( long item
)
4932 m_mainWin
->EnsureVisible( item
);
4936 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4938 return m_mainWin
->FindItem( start
, str
, partial
);
4941 long wxGenericListCtrl::FindItem( long start
, long data
)
4943 return m_mainWin
->FindItem( start
, data
);
4946 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4947 int WXUNUSED(direction
))
4952 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4954 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4957 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4959 m_mainWin
->InsertItem( info
);
4960 return info
.m_itemId
;
4963 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4966 info
.m_text
= label
;
4967 info
.m_mask
= wxLIST_MASK_TEXT
;
4968 info
.m_itemId
= index
;
4969 return InsertItem( info
);
4972 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4975 info
.m_mask
= wxLIST_MASK_IMAGE
;
4976 info
.m_image
= imageIndex
;
4977 info
.m_itemId
= index
;
4978 return InsertItem( info
);
4981 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4984 info
.m_text
= label
;
4985 info
.m_image
= imageIndex
;
4986 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4987 info
.m_itemId
= index
;
4988 return InsertItem( info
);
4991 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4993 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
4995 m_mainWin
->InsertColumn( col
, item
);
4997 // if we hadn't had header before and have it now we need to relayout the
4999 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5001 ResizeReportView(TRUE
/* have header */);
5004 m_headerWin
->Refresh();
5009 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5010 int format
, int width
)
5013 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5014 item
.m_text
= heading
;
5017 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5018 item
.m_width
= width
;
5020 item
.m_format
= format
;
5022 return InsertColumn( col
, item
);
5025 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5031 // fn is a function which takes 3 long arguments: item1, item2, data.
5032 // item1 is the long data associated with a first item (NOT the index).
5033 // item2 is the long data associated with a second item (NOT the index).
5034 // data is the same value as passed to SortItems.
5035 // The return value is a negative number if the first item should precede the second
5036 // item, a positive number of the second item should precede the first,
5037 // or zero if the two items are equivalent.
5038 // data is arbitrary data to be passed to the sort function.
5040 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5042 m_mainWin
->SortItems( fn
, data
);
5046 // ----------------------------------------------------------------------------
5048 // ----------------------------------------------------------------------------
5050 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5055 ResizeReportView(m_mainWin
->HasHeader());
5057 m_mainWin
->RecalculatePositions();
5060 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5063 GetClientSize( &cw
, &ch
);
5067 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5068 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5070 else // no header window
5072 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5076 void wxGenericListCtrl::OnInternalIdle()
5078 wxWindow::OnInternalIdle();
5080 // do it only if needed
5081 if ( !m_mainWin
->m_dirty
)
5084 m_mainWin
->RecalculatePositions();
5087 // ----------------------------------------------------------------------------
5089 // ----------------------------------------------------------------------------
5091 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5095 m_mainWin
->SetBackgroundColour( colour
);
5096 m_mainWin
->m_dirty
= TRUE
;
5102 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5104 if ( !wxWindow::SetForegroundColour( colour
) )
5109 m_mainWin
->SetForegroundColour( colour
);
5110 m_mainWin
->m_dirty
= TRUE
;
5115 m_headerWin
->SetForegroundColour( colour
);
5121 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5123 if ( !wxWindow::SetFont( font
) )
5128 m_mainWin
->SetFont( font
);
5129 m_mainWin
->m_dirty
= TRUE
;
5134 m_headerWin
->SetFont( font
);
5140 // ----------------------------------------------------------------------------
5141 // methods forwarded to m_mainWin
5142 // ----------------------------------------------------------------------------
5144 #if wxUSE_DRAG_AND_DROP
5146 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5148 m_mainWin
->SetDropTarget( dropTarget
);
5151 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5153 return m_mainWin
->GetDropTarget();
5156 #endif // wxUSE_DRAG_AND_DROP
5158 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5160 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5163 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5165 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5168 wxColour
wxGenericListCtrl::GetForegroundColour() const
5170 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5173 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5176 return m_mainWin
->PopupMenu( menu
, x
, y
);
5179 #endif // wxUSE_MENUS
5182 void wxGenericListCtrl::SetFocus()
5184 /* The test in window.cpp fails as we are a composite
5185 window, so it checks against "this", but not m_mainWin. */
5186 if ( FindFocus() != this )
5187 m_mainWin
->SetFocus();
5190 // ----------------------------------------------------------------------------
5191 // virtual list control support
5192 // ----------------------------------------------------------------------------
5194 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5196 // this is a pure virtual function, in fact - which is not really pure
5197 // because the controls which are not virtual don't need to implement it
5198 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5200 return wxEmptyString
;
5203 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5206 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5211 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5213 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5214 _T("invalid item index in OnGetItemAttr()") );
5216 // no attributes by default
5220 void wxGenericListCtrl::SetItemCount(long count
)
5222 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5224 m_mainWin
->SetItemCount(count
);
5227 void wxGenericListCtrl::RefreshItem(long item
)
5229 m_mainWin
->RefreshLine(item
);
5232 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5234 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5238 * Generic wxListCtrl is more or less a container for two other
5239 * windows which drawings are done upon. These are namely
5240 * 'm_headerWin' and 'm_mainWin'.
5241 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5242 * behaviour wxListCtrl has under wxMSW.
5244 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5248 // The easy case, no rectangle specified.
5250 m_headerWin
->Refresh(eraseBackground
);
5253 m_mainWin
->Refresh(eraseBackground
);
5257 // Refresh the header window
5260 wxRect rectHeader
= m_headerWin
->GetRect();
5261 rectHeader
.Intersect(*rect
);
5262 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5265 m_headerWin
->GetPosition(&x
, &y
);
5266 rectHeader
.Offset(-x
, -y
);
5267 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5272 // Refresh the main window
5275 wxRect rectMain
= m_mainWin
->GetRect();
5276 rectMain
.Intersect(*rect
);
5277 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5280 m_mainWin
->GetPosition(&x
, &y
);
5281 rectMain
.Offset(-x
, -y
);
5282 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5288 void wxGenericListCtrl::Freeze()
5290 m_mainWin
->Freeze();
5293 void wxGenericListCtrl::Thaw()
5298 #endif // wxUSE_LISTCTRL