1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #include "wx/selstore.h"
73 #include "wx/renderer.h"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // // the height of the header window (FIXME: should depend on its font!)
107 // static const int HEADER_HEIGHT = 23;
109 // the scrollbar units
110 static const int SCROLL_UNIT_X
= 15;
111 static const int SCROLL_UNIT_Y
= 15;
113 // the spacing between the lines (in report mode)
114 static const int LINE_SPACING
= 0;
116 // extra margins around the text label
117 static const int EXTRA_WIDTH
= 3;
118 static const int EXTRA_HEIGHT
= 4;
120 // margin between the window and the items
121 static const int EXTRA_BORDER_X
= 2;
122 static const int EXTRA_BORDER_Y
= 2;
124 // offset for the header window
125 static const int HEADER_OFFSET_X
= 1;
126 static const int HEADER_OFFSET_Y
= 1;
128 // margin between rows of icons in [small] icon view
129 static const int MARGIN_BETWEEN_ROWS
= 6;
131 // when autosizing the columns, add some slack
132 static const int AUTOSIZE_COL_MARGIN
= 10;
134 // default and minimal widths for the header columns
135 static const int WIDTH_COL_DEFAULT
= 80;
136 static const int WIDTH_COL_MIN
= 10;
138 // the space between the image and the text in the report mode
139 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
141 // ============================================================================
143 // ============================================================================
145 //-----------------------------------------------------------------------------
146 // wxListItemData (internal)
147 //-----------------------------------------------------------------------------
149 class WXDLLEXPORT wxListItemData
152 wxListItemData(wxListMainWindow
*owner
);
155 void SetItem( const wxListItem
&info
);
156 void SetImage( int image
) { m_image
= image
; }
157 void SetData( long data
) { m_data
= data
; }
158 void SetPosition( int x
, int y
);
159 void SetSize( int width
, int height
);
161 bool HasText() const { return !m_text
.empty(); }
162 const wxString
& GetText() const { return m_text
; }
163 void SetText(const wxString
& text
) { m_text
= text
; }
165 // we can't use empty string for measuring the string width/height, so
166 // always return something
167 wxString
GetTextForMeasuring() const
169 wxString s
= GetText();
176 bool IsHit( int x
, int y
) const;
180 int GetWidth() const;
181 int GetHeight() const;
183 int GetImage() const { return m_image
; }
184 bool HasImage() const { return GetImage() != -1; }
186 void GetItem( wxListItem
&info
) const;
188 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
189 wxListItemAttr
*GetAttr() const { return m_attr
; }
192 // the item image or -1
195 // user data associated with the item
198 // the item coordinates are not used in report mode, instead this pointer
199 // is NULL and the owner window is used to retrieve the item position and
203 // the list ctrl we are in
204 wxListMainWindow
*m_owner
;
206 // custom attributes or NULL
207 wxListItemAttr
*m_attr
;
210 // common part of all ctors
216 //-----------------------------------------------------------------------------
217 // wxListHeaderData (internal)
218 //-----------------------------------------------------------------------------
220 class WXDLLEXPORT wxListHeaderData
: public wxObject
224 wxListHeaderData( const wxListItem
&info
);
225 void SetItem( const wxListItem
&item
);
226 void SetPosition( int x
, int y
);
227 void SetWidth( int w
);
228 void SetFormat( int format
);
229 void SetHeight( int h
);
230 bool HasImage() const;
232 bool HasText() const { return !m_text
.empty(); }
233 const wxString
& GetText() const { return m_text
; }
234 void SetText(const wxString
& text
) { m_text
= text
; }
236 void GetItem( wxListItem
&item
);
238 bool IsHit( int x
, int y
) const;
239 int GetImage() const;
240 int GetWidth() const;
241 int GetFormat() const;
257 //-----------------------------------------------------------------------------
258 // wxListLineData (internal)
259 //-----------------------------------------------------------------------------
261 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
262 #include "wx/listimpl.cpp"
263 WX_DEFINE_LIST(wxListItemDataList
);
268 // the list of subitems: only may have more than one item in report mode
269 wxListItemDataList m_items
;
271 // this is not used in report view
283 // the part to be highlighted
284 wxRect m_rectHighlight
;
286 // extend all our rects to be centered inside theo ne of given width
287 void ExtendWidth(wxCoord w
)
289 wxASSERT_MSG( m_rectAll
.width
<= w
,
290 _T("width can only be increased") );
293 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
)/2;
294 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
)/2;
295 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
)/2;
299 // is this item selected? [NB: not used in virtual mode]
302 // back pointer to the list ctrl
303 wxListMainWindow
*m_owner
;
306 wxListLineData(wxListMainWindow
*owner
);
310 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
314 // are we in report mode?
315 inline bool InReportView() const;
317 // are we in virtual report mode?
318 inline bool IsVirtual() const;
320 // these 2 methods shouldn't be called for report view controls, in that
321 // case we determine our position/size ourselves
323 // calculate the size of the line
324 void CalculateSize( wxDC
*dc
, int spacing
);
326 // remember the position this line appears at
327 void SetPosition( int x
, int y
, int spacing
);
331 void SetImage( int image
) { SetImage(0, image
); }
332 int GetImage() const { return GetImage(0); }
333 bool HasImage() const { return GetImage() != -1; }
334 bool HasText() const { return !GetText(0).empty(); }
336 void SetItem( int index
, const wxListItem
&info
);
337 void GetItem( int index
, wxListItem
&info
);
339 wxString
GetText(int index
) const;
340 void SetText( int index
, const wxString s
);
342 wxListItemAttr
*GetAttr() const;
343 void SetAttr(wxListItemAttr
*attr
);
345 // return true if the highlighting really changed
346 bool Highlight( bool on
);
348 void ReverseHighlight();
350 bool IsHighlighted() const
352 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
354 return m_highlighted
;
357 // draw the line on the given DC in icon/list mode
358 void Draw( wxDC
*dc
);
360 // the same in report mode
361 void DrawInReportMode( wxDC
*dc
,
363 const wxRect
& rectHL
,
367 // set the line to contain num items (only can be > 1 in report mode)
368 void InitItems( int num
);
370 // get the mode (i.e. style) of the list control
371 inline int GetMode() const;
373 // prepare the DC for drawing with these item's attributes, return true if
374 // we need to draw the items background to highlight it, false otherwise
375 bool SetAttributes(wxDC
*dc
,
376 const wxListItemAttr
*attr
,
379 // draw the text on the DC with the correct justification; also add an
380 // ellipsis if the text is too large to fit in the current width
381 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
383 // these are only used by GetImage/SetImage above, we don't support images
384 // with subitems at the public API level yet
385 void SetImage( int index
, int image
);
386 int GetImage( int index
) const;
389 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
390 #include "wx/arrimpl.cpp"
391 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
393 //-----------------------------------------------------------------------------
394 // wxListHeaderWindow (internal)
395 //-----------------------------------------------------------------------------
397 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
400 wxListMainWindow
*m_owner
;
401 wxCursor
*m_currentCursor
;
402 wxCursor
*m_resizeCursor
;
405 // column being resized or -1
408 // divider line position in logical (unscrolled) coords
411 // minimal position beyond which the divider line can't be dragged in
416 wxListHeaderWindow();
418 wxListHeaderWindow( wxWindow
*win
,
420 wxListMainWindow
*owner
,
421 const wxPoint
&pos
= wxDefaultPosition
,
422 const wxSize
&size
= wxDefaultSize
,
424 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
426 virtual ~wxListHeaderWindow();
429 void AdjustDC(wxDC
& dc
);
431 void OnPaint( wxPaintEvent
&event
);
432 void OnMouse( wxMouseEvent
&event
);
433 void OnSetFocus( wxFocusEvent
&event
);
439 // common part of all ctors
442 // generate and process the list event of the given type, return true if
443 // it wasn't vetoed, i.e. if we should proceed
444 bool SendListEvent(wxEventType type
, wxPoint pos
);
446 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
447 DECLARE_EVENT_TABLE()
450 //-----------------------------------------------------------------------------
451 // wxListRenameTimer (internal)
452 //-----------------------------------------------------------------------------
454 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
457 wxListMainWindow
*m_owner
;
460 wxListRenameTimer( wxListMainWindow
*owner
);
464 //-----------------------------------------------------------------------------
465 // wxListTextCtrl (internal)
466 //-----------------------------------------------------------------------------
468 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
471 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
474 void OnChar( wxKeyEvent
&event
);
475 void OnKeyUp( wxKeyEvent
&event
);
476 void OnKillFocus( wxFocusEvent
&event
);
478 bool AcceptChanges();
482 wxListMainWindow
*m_owner
;
483 wxString m_startValue
;
487 DECLARE_EVENT_TABLE()
490 //-----------------------------------------------------------------------------
491 // wxListMainWindow (internal)
492 //-----------------------------------------------------------------------------
494 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
495 #include "wx/listimpl.cpp"
496 WX_DEFINE_LIST(wxListHeaderDataList
);
498 class wxListMainWindow
: public wxScrolledWindow
502 wxListMainWindow( wxWindow
*parent
,
504 const wxPoint
& pos
= wxDefaultPosition
,
505 const wxSize
& size
= wxDefaultSize
,
507 const wxString
&name
= _T("listctrlmainwindow") );
509 virtual ~wxListMainWindow();
511 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
513 // return true if this is a virtual list control
514 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
516 // return true if the control is in report mode
517 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
519 // return true if we are in single selection mode, false if multi sel
520 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
522 // do we have a header window?
523 bool HasHeader() const
524 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
526 void HighlightAll( bool on
);
528 // all these functions only do something if the line is currently visible
530 // change the line "selected" state, return TRUE if it really changed
531 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
533 // as HighlightLine() but do it for the range of lines: this is incredibly
534 // more efficient for virtual list controls!
536 // NB: unlike HighlightLine() this one does refresh the lines on screen
537 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
539 // toggle the line state and refresh it
540 void ReverseHighlight( size_t line
)
541 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
543 // return true if the line is highlighted
544 bool IsHighlighted(size_t line
) const;
546 // refresh one or several lines at once
547 void RefreshLine( size_t line
);
548 void RefreshLines( size_t lineFrom
, size_t lineTo
);
550 // refresh all selected items
551 void RefreshSelected();
553 // refresh all lines below the given one: the difference with
554 // RefreshLines() is that the index here might not be a valid one (happens
555 // when the last line is deleted)
556 void RefreshAfter( size_t lineFrom
);
558 // the methods which are forwarded to wxListLineData itself in list/icon
559 // modes but are here because the lines don't store their positions in the
562 // get the bound rect for the entire line
563 wxRect
GetLineRect(size_t line
) const;
565 // get the bound rect of the label
566 wxRect
GetLineLabelRect(size_t line
) const;
568 // get the bound rect of the items icon (only may be called if we do have
570 wxRect
GetLineIconRect(size_t line
) const;
572 // get the rect to be highlighted when the item has focus
573 wxRect
GetLineHighlightRect(size_t line
) const;
575 // get the size of the total line rect
576 wxSize
GetLineSize(size_t line
) const
577 { return GetLineRect(line
).GetSize(); }
579 // return the hit code for the corresponding position (in this line)
580 long HitTestLine(size_t line
, int x
, int y
) const;
582 // bring the selected item into view, scrolling to it if necessary
583 void MoveToItem(size_t item
);
585 // bring the current item into view
586 void MoveToFocus() { MoveToItem(m_current
); }
588 // start editing the label of the given item
589 void EditLabel( long item
);
591 // suspend/resume redrawing the control
597 void OnRenameTimer();
598 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
599 void OnRenameCancelled(size_t itemEdit
);
601 void OnMouse( wxMouseEvent
&event
);
603 // called to switch the selection from the current item to newCurrent,
604 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
606 void OnChar( wxKeyEvent
&event
);
607 void OnKeyDown( wxKeyEvent
&event
);
608 void OnSetFocus( wxFocusEvent
&event
);
609 void OnKillFocus( wxFocusEvent
&event
);
610 void OnScroll(wxScrollWinEvent
& event
) ;
612 void OnPaint( wxPaintEvent
&event
);
614 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
615 void GetImageSize( int index
, int &width
, int &height
) const;
616 int GetTextLength( const wxString
&s
) const;
618 void SetImageList( wxImageListType
*imageList
, int which
);
619 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
620 int GetItemSpacing( bool isSmall
= FALSE
);
622 void SetColumn( int col
, wxListItem
&item
);
623 void SetColumnWidth( int col
, int width
);
624 void GetColumn( int col
, wxListItem
&item
) const;
625 int GetColumnWidth( int col
) const;
626 int GetColumnCount() const { return m_columns
.GetCount(); }
628 // returns the sum of the heights of all columns
629 int GetHeaderWidth() const;
631 int GetCountPerPage() const;
633 void SetItem( wxListItem
&item
);
634 void GetItem( wxListItem
&item
) const;
635 void SetItemState( long item
, long state
, long stateMask
);
636 int GetItemState( long item
, long stateMask
) const;
637 void GetItemRect( long index
, wxRect
&rect
) const;
638 wxRect
GetViewRect() const;
639 bool GetItemPosition( long item
, wxPoint
& pos
) const;
640 int GetSelectedItemCount() const;
642 wxString
GetItemText(long item
) const
645 info
.m_itemId
= item
;
650 void SetItemText(long item
, const wxString
& value
)
653 info
.m_mask
= wxLIST_MASK_TEXT
;
654 info
.m_itemId
= item
;
659 // set the scrollbars and update the positions of the items
660 void RecalculatePositions(bool noRefresh
= FALSE
);
662 // refresh the window and the header
665 long GetNextItem( long item
, int geometry
, int state
) const;
666 void DeleteItem( long index
);
667 void DeleteAllItems();
668 void DeleteColumn( int col
);
669 void DeleteEverything();
670 void EnsureVisible( long index
);
671 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
672 long FindItem( long start
, long data
);
673 long HitTest( int x
, int y
, int &flags
);
674 void InsertItem( wxListItem
&item
);
675 void InsertColumn( long col
, wxListItem
&item
);
676 void SortItems( wxListCtrlCompare fn
, long data
);
678 size_t GetItemCount() const;
679 bool IsEmpty() const { return GetItemCount() == 0; }
680 void SetItemCount(long count
);
682 // change the current (== focused) item, send a notification event
683 void ChangeCurrent(size_t current
);
684 void ResetCurrent() { ChangeCurrent((size_t)-1); }
685 bool HasCurrent() const { return m_current
!= (size_t)-1; }
687 // send out a wxListEvent
688 void SendNotify( size_t line
,
690 wxPoint point
= wxDefaultPosition
);
692 // override base class virtual to reset m_lineHeight when the font changes
693 virtual bool SetFont(const wxFont
& font
)
695 if ( !wxScrolledWindow::SetFont(font
) )
703 // these are for wxListLineData usage only
705 // get the backpointer to the list ctrl
706 wxGenericListCtrl
*GetListCtrl() const
708 return wxStaticCast(GetParent(), wxGenericListCtrl
);
711 // get the height of all lines (assuming they all do have the same height)
712 wxCoord
GetLineHeight() const;
714 // get the y position of the given line (only for report view)
715 wxCoord
GetLineY(size_t line
) const;
717 // get the brush to use for the item highlighting
718 wxBrush
*GetHighlightBrush() const
720 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
724 // the array of all line objects for a non virtual list control (for the
725 // virtual list control we only ever use m_lines[0])
726 wxListLineDataArray m_lines
;
728 // the list of column objects
729 wxListHeaderDataList m_columns
;
731 // currently focused item or -1
734 // the number of lines per page
737 // this flag is set when something which should result in the window
738 // redrawing happens (i.e. an item was added or deleted, or its appearance
739 // changed) and OnPaint() doesn't redraw the window while it is set which
740 // allows to minimize the number of repaintings when a lot of items are
741 // being added. The real repainting occurs only after the next OnIdle()
745 wxColour
*m_highlightColour
;
746 wxImageListType
*m_small_image_list
;
747 wxImageListType
*m_normal_image_list
;
749 int m_normal_spacing
;
753 wxTimer
*m_renameTimer
;
758 // for double click logic
759 size_t m_lineLastClicked
,
760 m_lineBeforeLastClicked
;
763 // the total count of items in a virtual list control
766 // the object maintaining the items selection state, only used in virtual
768 wxSelectionStore m_selStore
;
770 // common part of all ctors
773 // get the line data for the given index
774 wxListLineData
*GetLine(size_t n
) const
776 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
780 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
788 // get a dummy line which can be used for geometry calculations and such:
789 // you must use GetLine() if you want to really draw the line
790 wxListLineData
*GetDummyLine() const;
792 // cache the line data of the n-th line in m_lines[0]
793 void CacheLineData(size_t line
);
795 // get the range of visible lines
796 void GetVisibleLinesRange(size_t *from
, size_t *to
);
798 // force us to recalculate the range of visible lines
799 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
801 // get the colour to be used for drawing the rules
802 wxColour
GetRuleColour() const
807 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
812 // initialize the current item if needed
813 void UpdateCurrent();
815 // delete all items but don't refresh: called from dtor
816 void DoDeleteAllItems();
818 // the height of one line using the current font
819 wxCoord m_lineHeight
;
821 // the total header width or 0 if not calculated yet
822 wxCoord m_headerWidth
;
824 // the first and last lines being shown on screen right now (inclusive),
825 // both may be -1 if they must be calculated so never access them directly:
826 // use GetVisibleLinesRange() above instead
830 // the brushes to use for item highlighting when we do/don't have focus
831 wxBrush
*m_highlightBrush
,
832 *m_highlightUnfocusedBrush
;
834 // if this is > 0, the control is frozen and doesn't redraw itself
835 size_t m_freezeCount
;
837 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
838 DECLARE_EVENT_TABLE()
840 friend class wxGenericListCtrl
;
843 // ============================================================================
845 // ============================================================================
847 //-----------------------------------------------------------------------------
849 //-----------------------------------------------------------------------------
851 wxListItemData::~wxListItemData()
853 // in the virtual list control the attributes are managed by the main
854 // program, so don't delete them
855 if ( !m_owner
->IsVirtual() )
863 void wxListItemData::Init()
871 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
877 if ( owner
->InReportView() )
887 void wxListItemData::SetItem( const wxListItem
&info
)
889 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
890 SetText(info
.m_text
);
891 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
892 m_image
= info
.m_image
;
893 if ( info
.m_mask
& wxLIST_MASK_DATA
)
894 m_data
= info
.m_data
;
896 if ( info
.HasAttributes() )
899 *m_attr
= *info
.GetAttributes();
901 m_attr
= new wxListItemAttr(*info
.GetAttributes());
909 m_rect
->width
= info
.m_width
;
913 void wxListItemData::SetPosition( int x
, int y
)
915 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
921 void wxListItemData::SetSize( int width
, int height
)
923 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
926 m_rect
->width
= width
;
928 m_rect
->height
= height
;
931 bool wxListItemData::IsHit( int x
, int y
) const
933 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
935 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
938 int wxListItemData::GetX() const
940 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
945 int wxListItemData::GetY() const
947 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
952 int wxListItemData::GetWidth() const
954 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
956 return m_rect
->width
;
959 int wxListItemData::GetHeight() const
961 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
963 return m_rect
->height
;
966 void wxListItemData::GetItem( wxListItem
&info
) const
968 info
.m_text
= m_text
;
969 info
.m_image
= m_image
;
970 info
.m_data
= m_data
;
974 if ( m_attr
->HasTextColour() )
975 info
.SetTextColour(m_attr
->GetTextColour());
976 if ( m_attr
->HasBackgroundColour() )
977 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
978 if ( m_attr
->HasFont() )
979 info
.SetFont(m_attr
->GetFont());
983 //-----------------------------------------------------------------------------
985 //-----------------------------------------------------------------------------
987 void wxListHeaderData::Init()
998 wxListHeaderData::wxListHeaderData()
1003 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1010 void wxListHeaderData::SetItem( const wxListItem
&item
)
1012 m_mask
= item
.m_mask
;
1014 if ( m_mask
& wxLIST_MASK_TEXT
)
1015 m_text
= item
.m_text
;
1017 if ( m_mask
& wxLIST_MASK_IMAGE
)
1018 m_image
= item
.m_image
;
1020 if ( m_mask
& wxLIST_MASK_FORMAT
)
1021 m_format
= item
.m_format
;
1023 if ( m_mask
& wxLIST_MASK_WIDTH
)
1024 SetWidth(item
.m_width
);
1027 void wxListHeaderData::SetPosition( int x
, int y
)
1033 void wxListHeaderData::SetHeight( int h
)
1038 void wxListHeaderData::SetWidth( int w
)
1042 m_width
= WIDTH_COL_DEFAULT
;
1043 else if (m_width
< WIDTH_COL_MIN
)
1044 m_width
= WIDTH_COL_MIN
;
1047 void wxListHeaderData::SetFormat( int format
)
1052 bool wxListHeaderData::HasImage() const
1054 return m_image
!= -1;
1057 bool wxListHeaderData::IsHit( int x
, int y
) const
1059 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1062 void wxListHeaderData::GetItem( wxListItem
& item
)
1064 item
.m_mask
= m_mask
;
1065 item
.m_text
= m_text
;
1066 item
.m_image
= m_image
;
1067 item
.m_format
= m_format
;
1068 item
.m_width
= m_width
;
1071 int wxListHeaderData::GetImage() const
1076 int wxListHeaderData::GetWidth() const
1081 int wxListHeaderData::GetFormat() const
1086 //-----------------------------------------------------------------------------
1088 //-----------------------------------------------------------------------------
1090 inline int wxListLineData::GetMode() const
1092 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1095 inline bool wxListLineData::InReportView() const
1097 return m_owner
->HasFlag(wxLC_REPORT
);
1100 inline bool wxListLineData::IsVirtual() const
1102 return m_owner
->IsVirtual();
1105 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1109 if ( InReportView() )
1115 m_gi
= new GeometryInfo
;
1118 m_highlighted
= FALSE
;
1120 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1123 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1125 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1126 wxCHECK_RET( node
, _T("no subitems at all??") );
1128 wxListItemData
*item
= node
->GetData();
1133 switch ( GetMode() )
1136 case wxLC_SMALL_ICON
:
1137 m_gi
->m_rectAll
.width
= spacing
;
1139 s
= item
->GetText();
1144 m_gi
->m_rectLabel
.width
=
1145 m_gi
->m_rectLabel
.height
= 0;
1149 dc
->GetTextExtent( s
, &lw
, &lh
);
1150 if (lh
< SCROLL_UNIT_Y
)
1155 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1157 m_gi
->m_rectAll
.width
= lw
;
1159 m_gi
->m_rectLabel
.width
= lw
;
1160 m_gi
->m_rectLabel
.height
= lh
;
1163 if (item
->HasImage())
1166 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1167 m_gi
->m_rectIcon
.width
= w
+ 8;
1168 m_gi
->m_rectIcon
.height
= h
+ 8;
1170 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1171 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1172 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1173 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1176 if ( item
->HasText() )
1178 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1179 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1181 else // no text, highlight the icon
1183 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1184 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1189 s
= item
->GetTextForMeasuring();
1191 dc
->GetTextExtent( s
, &lw
, &lh
);
1192 if (lh
< SCROLL_UNIT_Y
)
1197 m_gi
->m_rectLabel
.width
= lw
;
1198 m_gi
->m_rectLabel
.height
= lh
;
1200 m_gi
->m_rectAll
.width
= lw
;
1201 m_gi
->m_rectAll
.height
= lh
;
1203 if (item
->HasImage())
1206 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1207 m_gi
->m_rectIcon
.width
= w
;
1208 m_gi
->m_rectIcon
.height
= h
;
1210 m_gi
->m_rectAll
.width
+= 4 + w
;
1211 if (h
> m_gi
->m_rectAll
.height
)
1212 m_gi
->m_rectAll
.height
= h
;
1215 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1216 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1220 wxFAIL_MSG( _T("unexpected call to SetSize") );
1224 wxFAIL_MSG( _T("unknown mode") );
1228 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1230 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1231 wxCHECK_RET( node
, _T("no subitems at all??") );
1233 wxListItemData
*item
= node
->GetData();
1235 switch ( GetMode() )
1238 case wxLC_SMALL_ICON
:
1239 m_gi
->m_rectAll
.x
= x
;
1240 m_gi
->m_rectAll
.y
= y
;
1242 if ( item
->HasImage() )
1244 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1245 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1246 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1249 if ( item
->HasText() )
1251 if (m_gi
->m_rectAll
.width
> spacing
)
1252 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1254 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1255 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1256 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1257 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1259 else // no text, highlight the icon
1261 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1262 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1267 m_gi
->m_rectAll
.x
= x
;
1268 m_gi
->m_rectAll
.y
= y
;
1270 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1271 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1272 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1274 if (item
->HasImage())
1276 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1277 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1278 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1282 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1287 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1291 wxFAIL_MSG( _T("unknown mode") );
1295 void wxListLineData::InitItems( int num
)
1297 for (int i
= 0; i
< num
; i
++)
1298 m_items
.Append( new wxListItemData(m_owner
) );
1301 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1303 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1304 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1306 wxListItemData
*item
= node
->GetData();
1307 item
->SetItem( info
);
1310 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1312 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1315 wxListItemData
*item
= node
->GetData();
1316 item
->GetItem( info
);
1320 wxString
wxListLineData::GetText(int index
) const
1324 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1327 wxListItemData
*item
= node
->GetData();
1328 s
= item
->GetText();
1334 void wxListLineData::SetText( int index
, const wxString s
)
1336 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1339 wxListItemData
*item
= node
->GetData();
1344 void wxListLineData::SetImage( int index
, int image
)
1346 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1347 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1349 wxListItemData
*item
= node
->GetData();
1350 item
->SetImage(image
);
1353 int wxListLineData::GetImage( int index
) const
1355 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1356 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1358 wxListItemData
*item
= node
->GetData();
1359 return item
->GetImage();
1362 wxListItemAttr
*wxListLineData::GetAttr() const
1364 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1365 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1367 wxListItemData
*item
= node
->GetData();
1368 return item
->GetAttr();
1371 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1373 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1374 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1376 wxListItemData
*item
= node
->GetData();
1377 item
->SetAttr(attr
);
1380 bool wxListLineData::SetAttributes(wxDC
*dc
,
1381 const wxListItemAttr
*attr
,
1384 wxWindow
*listctrl
= m_owner
->GetParent();
1388 // don't use foreground colour for drawing highlighted items - this might
1389 // make them completely invisible (and there is no way to do bit
1390 // arithmetics on wxColour, unfortunately)
1394 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1398 if ( attr
&& attr
->HasTextColour() )
1400 colText
= attr
->GetTextColour();
1404 colText
= listctrl
->GetForegroundColour();
1408 dc
->SetTextForeground(colText
);
1412 if ( attr
&& attr
->HasFont() )
1414 font
= attr
->GetFont();
1418 font
= listctrl
->GetFont();
1424 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1425 if ( highlighted
|| hasBgCol
)
1429 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1433 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1436 dc
->SetPen( *wxTRANSPARENT_PEN
);
1444 void wxListLineData::Draw( wxDC
*dc
)
1446 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1447 wxCHECK_RET( node
, _T("no subitems at all??") );
1449 bool highlighted
= IsHighlighted();
1451 wxListItemAttr
*attr
= GetAttr();
1453 if ( SetAttributes(dc
, attr
, highlighted
) )
1455 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1458 // just for debugging to better see where the items are
1460 dc
->SetPen(*wxRED_PEN
);
1461 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1462 dc
->DrawRectangle( m_gi
->m_rectAll
);
1463 dc
->SetPen(*wxGREEN_PEN
);
1464 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1467 wxListItemData
*item
= node
->GetData();
1468 if (item
->HasImage())
1470 // centre the image inside our rectangle, this looks nicer when items
1471 // ae aligned in a row
1472 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1474 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1477 if (item
->HasText())
1479 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1481 wxDCClipper
clipper(*dc
, rectLabel
);
1482 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1486 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1488 const wxRect
& rectHL
,
1491 // TODO: later we should support setting different attributes for
1492 // different columns - to do it, just add "col" argument to
1493 // GetAttr() and move these lines into the loop below
1494 wxListItemAttr
*attr
= GetAttr();
1495 if ( SetAttributes(dc
, attr
, highlighted
) )
1497 dc
->DrawRectangle( rectHL
);
1500 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1501 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1504 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1506 node
= node
->GetNext(), col
++ )
1508 wxListItemData
*item
= node
->GetData();
1510 int width
= m_owner
->GetColumnWidth(col
);
1514 if ( item
->HasImage() )
1517 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1518 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1520 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1526 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1528 if ( item
->HasText() )
1530 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1535 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1536 const wxString
&text
,
1542 wxString drawntext
, ellipsis
;
1543 wxCoord w
, h
, base_w
;
1546 // determine if the string can fit inside the current width
1547 dc
->GetTextExtent(text
, &w
, &h
);
1550 // it can, draw it using the items alignment
1551 m_owner
->GetColumn(col
, item
);
1552 switch ( item
.GetAlign() )
1555 wxFAIL_MSG( _T("unknown list item format") );
1558 case wxLIST_FORMAT_LEFT
:
1562 case wxLIST_FORMAT_RIGHT
:
1566 case wxLIST_FORMAT_CENTER
:
1567 x
+= (width
- w
) / 2;
1571 dc
->DrawText(text
, x
, y
);
1573 else // otherwise, truncate and add an ellipsis if possible
1575 // determine the base width
1576 ellipsis
= wxString(wxT("..."));
1577 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1579 // continue until we have enough space or only one character left
1581 size_t len
= text
.Length();
1582 drawntext
= text
.Left(len
);
1585 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1586 drawntext
.RemoveLast();
1589 if (w
+ base_w
<= width
)
1593 // if still not enough space, remove ellipsis characters
1594 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1596 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1597 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1600 // now draw the text
1601 dc
->DrawText(drawntext
, x
, y
);
1602 dc
->DrawText(ellipsis
, x
+ w
, y
);
1606 bool wxListLineData::Highlight( bool on
)
1608 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1610 if ( on
== m_highlighted
)
1618 void wxListLineData::ReverseHighlight( void )
1620 Highlight(!IsHighlighted());
1623 //-----------------------------------------------------------------------------
1624 // wxListHeaderWindow
1625 //-----------------------------------------------------------------------------
1627 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1629 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1630 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1631 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1632 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1635 void wxListHeaderWindow::Init()
1637 m_currentCursor
= (wxCursor
*) NULL
;
1638 m_isDragging
= FALSE
;
1642 wxListHeaderWindow::wxListHeaderWindow()
1646 m_owner
= (wxListMainWindow
*) NULL
;
1647 m_resizeCursor
= (wxCursor
*) NULL
;
1650 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1652 wxListMainWindow
*owner
,
1656 const wxString
&name
)
1657 : wxWindow( win
, id
, pos
, size
, style
, name
)
1662 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1664 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1667 wxListHeaderWindow::~wxListHeaderWindow()
1669 delete m_resizeCursor
;
1672 #ifdef __WXUNIVERSAL__
1673 #include "wx/univ/renderer.h"
1674 #include "wx/univ/theme.h"
1677 // shift the DC origin to match the position of the main window horz
1678 // scrollbar: this allows us to always use logical coords
1679 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1682 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1685 m_owner
->GetViewStart( &x
, NULL
);
1687 // account for the horz scrollbar offset
1688 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1691 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1693 wxPaintDC
dc( this );
1700 dc
.SetFont( GetFont() );
1702 // width and height of the entire header window
1704 GetClientSize( &w
, &h
);
1705 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1707 dc
.SetBackgroundMode(wxTRANSPARENT
);
1709 // do *not* use the listctrl colour for headers - one day we will have a
1710 // function to set it separately
1711 //dc.SetTextForeground( *wxBLACK );
1712 dc
.SetTextForeground(wxSystemSettings::
1713 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1715 int x
= HEADER_OFFSET_X
;
1717 int numColumns
= m_owner
->GetColumnCount();
1719 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1721 m_owner
->GetColumn( i
, item
);
1722 int wCol
= item
.m_width
;
1724 // the width of the rect to draw: make it smaller to fit entirely
1725 // inside the column rect
1728 wxRendererNative::Get().DrawHeaderButton
1732 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1733 m_parent
->IsEnabled() ? 0
1734 : wxCONTROL_DISABLED
1737 // see if we have enough space for the column label
1739 // for this we need the width of the text
1741 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1742 wLabel
+= 2*EXTRA_WIDTH
;
1744 // and the width of the icon, if any
1745 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1746 int ix
= 0, // init them just to suppress the compiler warnings
1748 const int image
= item
.m_image
;
1749 wxImageListType
*imageList
;
1752 imageList
= m_owner
->m_small_image_list
;
1755 imageList
->GetSize(image
, ix
, iy
);
1756 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1764 // ignore alignment if there is not enough space anyhow
1766 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1769 wxFAIL_MSG( _T("unknown list item format") );
1772 case wxLIST_FORMAT_LEFT
:
1776 case wxLIST_FORMAT_RIGHT
:
1777 xAligned
= x
+ cw
- wLabel
;
1780 case wxLIST_FORMAT_CENTER
:
1781 xAligned
= x
+ (cw
- wLabel
) / 2;
1786 // if we have an image, draw it on the right of the label
1793 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1794 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1795 wxIMAGELIST_DRAW_TRANSPARENT
1798 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1801 // draw the text clipping it so that it doesn't overwrite the column
1803 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1805 dc
.DrawText( item
.GetText(),
1806 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1814 void wxListHeaderWindow::DrawCurrent()
1816 int x1
= m_currentX
;
1818 m_owner
->ClientToScreen( &x1
, &y1
);
1820 int x2
= m_currentX
;
1822 m_owner
->GetClientSize( NULL
, &y2
);
1823 m_owner
->ClientToScreen( &x2
, &y2
);
1826 dc
.SetLogicalFunction( wxINVERT
);
1827 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1828 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1832 dc
.DrawLine( x1
, y1
, x2
, y2
);
1834 dc
.SetLogicalFunction( wxCOPY
);
1836 dc
.SetPen( wxNullPen
);
1837 dc
.SetBrush( wxNullBrush
);
1840 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1842 // we want to work with logical coords
1844 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1845 int y
= event
.GetY();
1849 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1851 // we don't draw the line beyond our window, but we allow dragging it
1854 GetClientSize( &w
, NULL
);
1855 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1858 // erase the line if it was drawn
1859 if ( m_currentX
< w
)
1862 if (event
.ButtonUp())
1865 m_isDragging
= FALSE
;
1867 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1868 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1875 m_currentX
= m_minX
+ 7;
1877 // draw in the new location
1878 if ( m_currentX
< w
)
1882 else // not dragging
1885 bool hit_border
= FALSE
;
1887 // end of the current column
1890 // find the column where this event occured
1892 countCol
= m_owner
->GetColumnCount();
1893 for (col
= 0; col
< countCol
; col
++)
1895 xpos
+= m_owner
->GetColumnWidth( col
);
1898 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1900 // near the column border
1907 // inside the column
1914 if ( col
== countCol
)
1917 if (event
.LeftDown() || event
.RightUp())
1919 if (hit_border
&& event
.LeftDown())
1921 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1922 event
.GetPosition()) )
1924 m_isDragging
= TRUE
;
1929 //else: column resizing was vetoed by the user code
1931 else // click on a column
1933 SendListEvent( event
.LeftDown()
1934 ? wxEVT_COMMAND_LIST_COL_CLICK
1935 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1936 event
.GetPosition());
1939 else if (event
.Moving())
1944 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1945 m_currentCursor
= m_resizeCursor
;
1949 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1950 m_currentCursor
= wxSTANDARD_CURSOR
;
1954 SetCursor(*m_currentCursor
);
1959 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1961 m_owner
->SetFocus();
1964 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1966 wxWindow
*parent
= GetParent();
1967 wxListEvent
le( type
, parent
->GetId() );
1968 le
.SetEventObject( parent
);
1969 le
.m_pointDrag
= pos
;
1971 // the position should be relative to the parent window, not
1972 // this one for compatibility with MSW and common sense: the
1973 // user code doesn't know anything at all about this header
1974 // window, so why should it get positions relative to it?
1975 le
.m_pointDrag
.y
-= GetSize().y
;
1977 le
.m_col
= m_column
;
1978 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1981 //-----------------------------------------------------------------------------
1982 // wxListRenameTimer (internal)
1983 //-----------------------------------------------------------------------------
1985 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1990 void wxListRenameTimer::Notify()
1992 m_owner
->OnRenameTimer();
1995 //-----------------------------------------------------------------------------
1996 // wxListTextCtrl (internal)
1997 //-----------------------------------------------------------------------------
1999 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2000 EVT_CHAR (wxListTextCtrl::OnChar
)
2001 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2002 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2005 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2006 : m_startValue(owner
->GetItemText(itemEdit
)),
2007 m_itemEdited(itemEdit
)
2012 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2014 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2015 &rectLabel
.x
, &rectLabel
.y
);
2017 (void)Create(owner
, wxID_ANY
, m_startValue
,
2018 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2019 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2022 void wxListTextCtrl::Finish()
2026 wxPendingDelete
.Append(this);
2030 m_owner
->SetFocus();
2034 bool wxListTextCtrl::AcceptChanges()
2036 const wxString value
= GetValue();
2038 if ( value
== m_startValue
)
2040 // nothing changed, always accept
2044 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2046 // vetoed by the user
2050 // accepted, do rename the item
2051 m_owner
->SetItemText(m_itemEdited
, value
);
2056 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2058 switch ( event
.m_keyCode
)
2061 if ( !AcceptChanges() )
2063 // vetoed by the user code
2066 //else: fall through
2070 m_owner
->OnRenameCancelled( m_itemEdited
);
2078 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2086 // auto-grow the textctrl:
2087 wxSize parentSize
= m_owner
->GetSize();
2088 wxPoint myPos
= GetPosition();
2089 wxSize mySize
= GetSize();
2091 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2092 if (myPos
.x
+ sx
> parentSize
.x
)
2093 sx
= parentSize
.x
- myPos
.x
;
2101 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2105 // We must finish regardless of success, otherwise we'll get focus problems
2108 if ( !AcceptChanges() )
2109 m_owner
->OnRenameCancelled( m_itemEdited
);
2115 //-----------------------------------------------------------------------------
2117 //-----------------------------------------------------------------------------
2119 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2121 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2122 EVT_PAINT (wxListMainWindow::OnPaint
)
2123 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2124 EVT_CHAR (wxListMainWindow::OnChar
)
2125 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2126 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2127 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2128 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2131 void wxListMainWindow::Init()
2136 m_lineTo
= (size_t)-1;
2142 m_small_image_list
= (wxImageListType
*) NULL
;
2143 m_normal_image_list
= (wxImageListType
*) NULL
;
2145 m_small_spacing
= 30;
2146 m_normal_spacing
= 40;
2150 m_isCreated
= FALSE
;
2152 m_lastOnSame
= FALSE
;
2153 m_renameTimer
= new wxListRenameTimer( this );
2157 m_lineBeforeLastClicked
= (size_t)-1;
2162 wxListMainWindow::wxListMainWindow()
2167 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2170 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2175 const wxString
&name
)
2176 : wxScrolledWindow( parent
, id
, pos
, size
,
2177 style
| wxHSCROLL
| wxVSCROLL
, name
)
2181 m_highlightBrush
= new wxBrush
2183 wxSystemSettings::GetColour
2185 wxSYS_COLOUR_HIGHLIGHT
2190 m_highlightUnfocusedBrush
= new wxBrush
2192 wxSystemSettings::GetColour
2194 wxSYS_COLOUR_BTNSHADOW
2202 SetScrollbars( SCROLL_UNIT_X
, SCROLL_UNIT_Y
, 0, 0, 0, 0 );
2204 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2207 wxListMainWindow::~wxListMainWindow()
2210 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2212 delete m_highlightBrush
;
2213 delete m_highlightUnfocusedBrush
;
2215 delete m_renameTimer
;
2218 void wxListMainWindow::CacheLineData(size_t line
)
2220 wxGenericListCtrl
*listctrl
= GetListCtrl();
2222 wxListLineData
*ld
= GetDummyLine();
2224 size_t countCol
= GetColumnCount();
2225 for ( size_t col
= 0; col
< countCol
; col
++ )
2227 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2230 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2231 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2234 wxListLineData
*wxListMainWindow::GetDummyLine() const
2236 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2238 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2240 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2242 // we need to recreate the dummy line if the number of columns in the
2243 // control changed as it would have the incorrect number of fields
2245 if ( !m_lines
.IsEmpty() &&
2246 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2248 self
->m_lines
.Clear();
2251 if ( m_lines
.IsEmpty() )
2253 wxListLineData
*line
= new wxListLineData(self
);
2254 self
->m_lines
.Add(line
);
2256 // don't waste extra memory -- there never going to be anything
2257 // else/more in this array
2258 self
->m_lines
.Shrink();
2264 // ----------------------------------------------------------------------------
2265 // line geometry (report mode only)
2266 // ----------------------------------------------------------------------------
2268 wxCoord
wxListMainWindow::GetLineHeight() const
2270 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2272 // we cache the line height as calling GetTextExtent() is slow
2273 if ( !m_lineHeight
)
2275 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2277 wxClientDC
dc( self
);
2278 dc
.SetFont( GetFont() );
2281 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2283 if ( y
< SCROLL_UNIT_Y
)
2286 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2290 m_small_image_list
->GetSize(0, iw
, ih
);
2295 self
->m_lineHeight
= y
+ LINE_SPACING
;
2298 return m_lineHeight
;
2301 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2303 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2305 return LINE_SPACING
+ line
*GetLineHeight();
2308 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2310 if ( !InReportView() )
2311 return GetLine(line
)->m_gi
->m_rectAll
;
2314 rect
.x
= HEADER_OFFSET_X
;
2315 rect
.y
= GetLineY(line
);
2316 rect
.width
= GetHeaderWidth();
2317 rect
.height
= GetLineHeight();
2322 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2324 if ( !InReportView() )
2325 return GetLine(line
)->m_gi
->m_rectLabel
;
2328 rect
.x
= HEADER_OFFSET_X
;
2329 rect
.y
= GetLineY(line
);
2330 rect
.width
= GetColumnWidth(0);
2331 rect
.height
= GetLineHeight();
2336 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2338 if ( !InReportView() )
2339 return GetLine(line
)->m_gi
->m_rectIcon
;
2341 wxListLineData
*ld
= GetLine(line
);
2342 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2345 rect
.x
= HEADER_OFFSET_X
;
2346 rect
.y
= GetLineY(line
);
2347 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2352 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2354 return InReportView() ? GetLineRect(line
)
2355 : GetLine(line
)->m_gi
->m_rectHighlight
;
2358 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2360 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2362 wxListLineData
*ld
= GetLine(line
);
2364 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2365 return wxLIST_HITTEST_ONITEMICON
;
2367 // VS: Testing for "ld->HasText() || InReportView()" instead of
2368 // "ld->HasText()" is needed to make empty lines in report view
2370 if ( ld
->HasText() || InReportView() )
2372 wxRect rect
= InReportView() ? GetLineRect(line
)
2373 : GetLineLabelRect(line
);
2375 if ( rect
.Inside(x
, y
) )
2376 return wxLIST_HITTEST_ONITEMLABEL
;
2382 // ----------------------------------------------------------------------------
2383 // highlight (selection) handling
2384 // ----------------------------------------------------------------------------
2386 bool wxListMainWindow::IsHighlighted(size_t line
) const
2390 return m_selStore
.IsSelected(line
);
2394 wxListLineData
*ld
= GetLine(line
);
2395 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2397 return ld
->IsHighlighted();
2401 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2407 wxArrayInt linesChanged
;
2408 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2411 // meny items changed state, refresh everything
2412 RefreshLines(lineFrom
, lineTo
);
2414 else // only a few items changed state, refresh only them
2416 size_t count
= linesChanged
.GetCount();
2417 for ( size_t n
= 0; n
< count
; n
++ )
2419 RefreshLine(linesChanged
[n
]);
2423 else // iterate over all items in non report view
2425 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2427 if ( HighlightLine(line
, highlight
) )
2435 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2441 changed
= m_selStore
.SelectItem(line
, highlight
);
2445 wxListLineData
*ld
= GetLine(line
);
2446 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2448 changed
= ld
->Highlight(highlight
);
2453 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2454 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2460 void wxListMainWindow::RefreshLine( size_t line
)
2462 if ( HasFlag(wxLC_REPORT
) )
2464 size_t visibleFrom
, visibleTo
;
2465 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2467 if ( line
< visibleFrom
|| line
> visibleTo
)
2471 wxRect rect
= GetLineRect(line
);
2473 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2474 RefreshRect( rect
);
2477 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2479 // we suppose that they are ordered by caller
2480 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2482 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2484 if ( HasFlag(wxLC_REPORT
) )
2486 size_t visibleFrom
, visibleTo
;
2487 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2489 if ( lineFrom
< visibleFrom
)
2490 lineFrom
= visibleFrom
;
2491 if ( lineTo
> visibleTo
)
2496 rect
.y
= GetLineY(lineFrom
);
2497 rect
.width
= GetClientSize().x
;
2498 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2500 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2501 RefreshRect( rect
);
2505 // TODO: this should be optimized...
2506 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2513 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2515 if ( HasFlag(wxLC_REPORT
) )
2517 size_t visibleFrom
, visibleTo
;
2518 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2520 if ( lineFrom
< visibleFrom
)
2521 lineFrom
= visibleFrom
;
2522 else if ( lineFrom
> visibleTo
)
2527 rect
.y
= GetLineY(lineFrom
);
2528 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2530 wxSize size
= GetClientSize();
2531 rect
.width
= size
.x
;
2532 // refresh till the bottom of the window
2533 rect
.height
= size
.y
- rect
.y
;
2535 RefreshRect( rect
);
2539 // TODO: how to do it more efficiently?
2544 void wxListMainWindow::RefreshSelected()
2550 if ( InReportView() )
2552 GetVisibleLinesRange(&from
, &to
);
2557 to
= GetItemCount() - 1;
2560 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2562 RefreshLine(m_current
);
2565 for ( size_t line
= from
; line
<= to
; line
++ )
2567 // NB: the test works as expected even if m_current == -1
2568 if ( line
!= m_current
&& IsHighlighted(line
) )
2575 void wxListMainWindow::Freeze()
2580 void wxListMainWindow::Thaw()
2582 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2584 if ( !--m_freezeCount
)
2590 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2592 // Note: a wxPaintDC must be constructed even if no drawing is
2593 // done (a Windows requirement).
2594 wxPaintDC
dc( this );
2596 if ( IsEmpty() || m_freezeCount
)
2598 // nothing to draw or not the moment to draw it
2604 // delay the repainting until we calculate all the items positions
2611 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2615 dc
.SetFont( GetFont() );
2617 if ( HasFlag(wxLC_REPORT
) )
2619 int lineHeight
= GetLineHeight();
2621 size_t visibleFrom
, visibleTo
;
2622 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2625 wxCoord xOrig
, yOrig
;
2626 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2628 // tell the caller cache to cache the data
2631 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2632 GetParent()->GetId());
2633 evCache
.SetEventObject( GetParent() );
2634 evCache
.m_oldItemIndex
= visibleFrom
;
2635 evCache
.m_itemIndex
= visibleTo
;
2636 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2639 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2641 rectLine
= GetLineRect(line
);
2643 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2644 rectLine
.width
, rectLine
.height
) )
2646 // don't redraw unaffected lines to avoid flicker
2650 GetLine(line
)->DrawInReportMode( &dc
,
2652 GetLineHighlightRect(line
),
2653 IsHighlighted(line
) );
2656 if ( HasFlag(wxLC_HRULES
) )
2658 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2659 wxSize clientSize
= GetClientSize();
2661 // Don't draw the first one
2662 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2665 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2666 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2667 clientSize
.x
- dev_x
, i
*lineHeight
);
2670 // Draw last horizontal rule
2671 if ( visibleTo
== GetItemCount() - 1 )
2674 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2675 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2676 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2680 // Draw vertical rules if required
2681 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2683 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2686 wxRect firstItemRect
;
2687 wxRect lastItemRect
;
2688 GetItemRect(visibleFrom
, firstItemRect
);
2689 GetItemRect(visibleTo
, lastItemRect
);
2690 int x
= firstItemRect
.GetX();
2692 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2693 for (col
= 0; col
< GetColumnCount(); col
++)
2695 int colWidth
= GetColumnWidth(col
);
2697 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2698 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2704 size_t count
= GetItemCount();
2705 for ( size_t i
= 0; i
< count
; i
++ )
2707 GetLine(i
)->Draw( &dc
);
2713 // don't draw rect outline under Max if we already have the background
2714 // color but under other platforms only draw it if we do: it is a bit
2715 // silly to draw "focus rect" if we don't have focus!
2720 #endif // __WXMAC__/!__WXMAC__
2722 dc
.SetPen( *wxBLACK_PEN
);
2723 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2724 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2731 void wxListMainWindow::HighlightAll( bool on
)
2733 if ( IsSingleSel() )
2735 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2737 // we just have one item to turn off
2738 if ( HasCurrent() && IsHighlighted(m_current
) )
2740 HighlightLine(m_current
, FALSE
);
2741 RefreshLine(m_current
);
2746 HighlightLines(0, GetItemCount() - 1, on
);
2750 void wxListMainWindow::SendNotify( size_t line
,
2751 wxEventType command
,
2754 wxListEvent
le( command
, GetParent()->GetId() );
2755 le
.SetEventObject( GetParent() );
2756 le
.m_itemIndex
= line
;
2758 // set only for events which have position
2759 if ( point
!= wxDefaultPosition
)
2760 le
.m_pointDrag
= point
;
2762 // don't try to get the line info for virtual list controls: the main
2763 // program has it anyhow and if we did it would result in accessing all
2764 // the lines, even those which are not visible now and this is precisely
2765 // what we're trying to avoid
2766 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2768 if ( line
!= (size_t)-1 )
2770 GetLine(line
)->GetItem( 0, le
.m_item
);
2772 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2774 //else: there may be no more such item
2776 GetParent()->GetEventHandler()->ProcessEvent( le
);
2779 void wxListMainWindow::ChangeCurrent(size_t current
)
2781 m_current
= current
;
2783 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2786 void wxListMainWindow::EditLabel( long item
)
2788 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2789 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2791 size_t itemEdit
= (size_t)item
;
2793 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2794 le
.SetEventObject( GetParent() );
2795 le
.m_itemIndex
= item
;
2796 wxListLineData
*data
= GetLine(itemEdit
);
2797 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2798 data
->GetItem( 0, le
.m_item
);
2799 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2801 // vetoed by user code
2805 // We have to call this here because the label in question might just have
2806 // been added and no screen update taken place.
2810 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2815 void wxListMainWindow::OnRenameTimer()
2817 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2819 EditLabel( m_current
);
2822 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2824 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2825 le
.SetEventObject( GetParent() );
2826 le
.m_itemIndex
= itemEdit
;
2828 wxListLineData
*data
= GetLine(itemEdit
);
2829 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2831 data
->GetItem( 0, le
.m_item
);
2832 le
.m_item
.m_text
= value
;
2833 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2837 #ifdef __VMS__ // Ignore unreacheable code
2838 # pragma message disable initnotreach
2841 void wxListMainWindow::OnRenameCancelled(size_t WXUNUSED(itemEdit
))
2843 // wxMSW seems not to notify the program about
2844 // cancelled label edits.
2848 // above unconditional return cause warning about not reachable code
2850 // let owner know that the edit was cancelled
2851 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2853 // These only exist for wxTreeCtrl, which should probably be changed
2854 // le.m_editCancelled = TRUE;
2855 // le.m_label = wxEmptyString;
2857 le
.SetEventObject( GetParent() );
2858 le
.m_itemIndex
= itemEdit
;
2860 wxListLineData
*data
= GetLine(itemEdit
);
2861 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2863 data
->GetItem( 0, le
.m_item
);
2865 GetEventHandler()->ProcessEvent( le
);
2869 # pragma message enable initnotreach
2872 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2874 event
.SetEventObject( GetParent() );
2875 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2878 if ( !HasCurrent() || IsEmpty() )
2884 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2885 event
.ButtonDClick()) )
2888 int x
= event
.GetX();
2889 int y
= event
.GetY();
2890 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2892 // where did we hit it (if we did)?
2895 size_t count
= GetItemCount(),
2898 if ( HasFlag(wxLC_REPORT
) )
2900 current
= y
/ GetLineHeight();
2901 if ( current
< count
)
2902 hitResult
= HitTestLine(current
, x
, y
);
2906 // TODO: optimize it too! this is less simple than for report view but
2907 // enumerating all items is still not a way to do it!!
2908 for ( current
= 0; current
< count
; current
++ )
2910 hitResult
= HitTestLine(current
, x
, y
);
2916 if (event
.Dragging())
2918 if (m_dragCount
== 0)
2920 // we have to report the raw, physical coords as we want to be
2921 // able to call HitTest(event.m_pointDrag) from the user code to
2922 // get the item being dragged
2923 m_dragStart
= event
.GetPosition();
2928 if (m_dragCount
!= 3)
2931 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2932 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2934 wxListEvent
le( command
, GetParent()->GetId() );
2935 le
.SetEventObject( GetParent() );
2936 le
.m_itemIndex
= current
;
2937 le
.m_pointDrag
= m_dragStart
;
2938 GetParent()->GetEventHandler()->ProcessEvent( le
);
2949 // outside of any item
2953 bool forceClick
= FALSE
;
2954 if (event
.ButtonDClick())
2956 m_renameTimer
->Stop();
2957 m_lastOnSame
= FALSE
;
2959 if ( current
== m_lineLastClicked
)
2961 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2967 // the first click was on another item, so don't interpret this as
2968 // a double click, but as a simple click instead
2973 if (event
.LeftUp() && m_lastOnSame
)
2975 if ((current
== m_current
) &&
2976 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2977 HasFlag(wxLC_EDIT_LABELS
) )
2979 m_renameTimer
->Start( 100, TRUE
);
2981 m_lastOnSame
= FALSE
;
2983 else if (event
.RightDown())
2985 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2986 event
.GetPosition() );
2988 else if (event
.MiddleDown())
2990 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2992 else if ( event
.LeftDown() || forceClick
)
2994 m_lineBeforeLastClicked
= m_lineLastClicked
;
2995 m_lineLastClicked
= current
;
2997 size_t oldCurrent
= m_current
;
2999 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3001 HighlightAll( FALSE
);
3003 ChangeCurrent(current
);
3005 ReverseHighlight(m_current
);
3007 else // multi sel & either ctrl or shift is down
3009 if (event
.ControlDown())
3011 ChangeCurrent(current
);
3013 ReverseHighlight(m_current
);
3015 else if (event
.ShiftDown())
3017 ChangeCurrent(current
);
3019 size_t lineFrom
= oldCurrent
,
3022 if ( lineTo
< lineFrom
)
3025 lineFrom
= m_current
;
3028 HighlightLines(lineFrom
, lineTo
);
3030 else // !ctrl, !shift
3032 // test in the enclosing if should make it impossible
3033 wxFAIL_MSG( _T("how did we get here?") );
3037 if (m_current
!= oldCurrent
)
3039 RefreshLine( oldCurrent
);
3042 // forceClick is only set if the previous click was on another item
3043 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3047 void wxListMainWindow::MoveToItem(size_t item
)
3049 if ( item
== (size_t)-1 )
3052 wxRect rect
= GetLineRect(item
);
3054 int client_w
, client_h
;
3055 GetClientSize( &client_w
, &client_h
);
3057 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3058 int view_y
= SCROLL_UNIT_Y
*GetScrollPos( wxVERTICAL
);
3060 if ( HasFlag(wxLC_REPORT
) )
3062 // the next we need the range of lines shown it might be different, so
3064 ResetVisibleLinesRange();
3066 if (rect
.y
< view_y
)
3067 Scroll( -1, rect
.y
/SCROLL_UNIT_Y
);
3068 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3069 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/SCROLL_UNIT_Y
);
3073 if (rect
.x
-view_x
< 5)
3074 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3075 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3076 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3080 // ----------------------------------------------------------------------------
3081 // keyboard handling
3082 // ----------------------------------------------------------------------------
3084 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3086 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3087 _T("invalid item index in OnArrowChar()") );
3089 size_t oldCurrent
= m_current
;
3091 // in single selection we just ignore Shift as we can't select several
3093 if ( event
.ShiftDown() && !IsSingleSel() )
3095 ChangeCurrent(newCurrent
);
3097 // select all the items between the old and the new one
3098 if ( oldCurrent
> newCurrent
)
3100 newCurrent
= oldCurrent
;
3101 oldCurrent
= m_current
;
3104 HighlightLines(oldCurrent
, newCurrent
);
3108 // all previously selected items are unselected unless ctrl is held
3109 if ( !event
.ControlDown() )
3110 HighlightAll(FALSE
);
3112 ChangeCurrent(newCurrent
);
3114 // refresh the old focus to remove it
3115 RefreshLine( oldCurrent
);
3117 if ( !event
.ControlDown() )
3119 HighlightLine( m_current
, TRUE
);
3123 RefreshLine( m_current
);
3128 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3130 wxWindow
*parent
= GetParent();
3132 /* we propagate the key event up */
3133 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3134 ke
.m_shiftDown
= event
.m_shiftDown
;
3135 ke
.m_controlDown
= event
.m_controlDown
;
3136 ke
.m_altDown
= event
.m_altDown
;
3137 ke
.m_metaDown
= event
.m_metaDown
;
3138 ke
.m_keyCode
= event
.m_keyCode
;
3141 ke
.SetEventObject( parent
);
3142 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3147 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3149 wxWindow
*parent
= GetParent();
3151 /* we send a list_key event up */
3154 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3155 le
.m_itemIndex
= m_current
;
3156 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3157 le
.m_code
= event
.GetKeyCode();
3158 le
.SetEventObject( parent
);
3159 parent
->GetEventHandler()->ProcessEvent( le
);
3162 /* we propagate the char event up */
3163 wxKeyEvent
ke( wxEVT_CHAR
);
3164 ke
.m_shiftDown
= event
.m_shiftDown
;
3165 ke
.m_controlDown
= event
.m_controlDown
;
3166 ke
.m_altDown
= event
.m_altDown
;
3167 ke
.m_metaDown
= event
.m_metaDown
;
3168 ke
.m_keyCode
= event
.m_keyCode
;
3171 ke
.SetEventObject( parent
);
3172 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3174 if (event
.GetKeyCode() == WXK_TAB
)
3176 wxNavigationKeyEvent nevent
;
3177 nevent
.SetWindowChange( event
.ControlDown() );
3178 nevent
.SetDirection( !event
.ShiftDown() );
3179 nevent
.SetEventObject( GetParent()->GetParent() );
3180 nevent
.SetCurrentFocus( m_parent
);
3181 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3185 /* no item -> nothing to do */
3192 switch (event
.GetKeyCode())
3195 if ( m_current
> 0 )
3196 OnArrowChar( m_current
- 1, event
);
3200 if ( m_current
< (size_t)GetItemCount() - 1 )
3201 OnArrowChar( m_current
+ 1, event
);
3206 OnArrowChar( GetItemCount() - 1, event
);
3211 OnArrowChar( 0, event
);
3217 if ( HasFlag(wxLC_REPORT
) )
3219 steps
= m_linesPerPage
- 1;
3223 steps
= m_current
% m_linesPerPage
;
3226 int index
= m_current
- steps
;
3230 OnArrowChar( index
, event
);
3237 if ( HasFlag(wxLC_REPORT
) )
3239 steps
= m_linesPerPage
- 1;
3243 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3246 size_t index
= m_current
+ steps
;
3247 size_t count
= GetItemCount();
3248 if ( index
>= count
)
3251 OnArrowChar( index
, event
);
3256 if ( !HasFlag(wxLC_REPORT
) )
3258 int index
= m_current
- m_linesPerPage
;
3262 OnArrowChar( index
, event
);
3267 if ( !HasFlag(wxLC_REPORT
) )
3269 size_t index
= m_current
+ m_linesPerPage
;
3271 size_t count
= GetItemCount();
3272 if ( index
>= count
)
3275 OnArrowChar( index
, event
);
3280 if ( IsSingleSel() )
3282 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3284 if ( IsHighlighted(m_current
) )
3286 // don't unselect the item in single selection mode
3289 //else: select it in ReverseHighlight() below if unselected
3292 ReverseHighlight(m_current
);
3297 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3305 // ----------------------------------------------------------------------------
3307 // ----------------------------------------------------------------------------
3309 void wxListMainWindow::SetFocus()
3311 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3312 // overrides SetFocus in such way that it does never change focus from
3313 // panel's child to the panel itself. Unfortunately, we must be able to change
3314 // focus to the panel from wxListTextCtrl because the text control should
3315 // disappear when the user clicks outside it.
3317 wxWindow
*oldFocus
= FindFocus();
3319 if ( oldFocus
&& oldFocus
->GetParent() == this )
3321 wxWindow::SetFocus();
3325 wxScrolledWindow::SetFocus();
3329 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3331 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3332 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3333 // which are already drawn correctly resulting in horrible flicker - avoid
3345 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3346 event
.SetEventObject( GetParent() );
3347 GetParent()->GetEventHandler()->ProcessEvent( event
);
3350 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3357 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3359 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3361 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3363 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3365 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3367 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3369 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3371 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3373 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3377 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3379 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3381 m_normal_image_list
->GetSize( index
, width
, height
);
3383 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3385 m_small_image_list
->GetSize( index
, width
, height
);
3387 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3389 m_small_image_list
->GetSize( index
, width
, height
);
3391 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3393 m_small_image_list
->GetSize( index
, width
, height
);
3402 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3404 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3405 dc
.SetFont( GetFont() );
3408 dc
.GetTextExtent( s
, &lw
, NULL
);
3410 return lw
+ AUTOSIZE_COL_MARGIN
;
3413 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3417 // calc the spacing from the icon size
3420 if ((imageList
) && (imageList
->GetImageCount()) )
3422 imageList
->GetSize(0, width
, height
);
3425 if (which
== wxIMAGE_LIST_NORMAL
)
3427 m_normal_image_list
= imageList
;
3428 m_normal_spacing
= width
+ 8;
3431 if (which
== wxIMAGE_LIST_SMALL
)
3433 m_small_image_list
= imageList
;
3434 m_small_spacing
= width
+ 14;
3435 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3439 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3444 m_small_spacing
= spacing
;
3448 m_normal_spacing
= spacing
;
3452 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3454 return isSmall
? m_small_spacing
: m_normal_spacing
;
3457 // ----------------------------------------------------------------------------
3459 // ----------------------------------------------------------------------------
3461 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3463 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3465 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3467 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3468 item
.m_width
= GetTextLength( item
.m_text
);
3470 wxListHeaderData
*column
= node
->GetData();
3471 column
->SetItem( item
);
3473 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3475 headerWin
->m_dirty
= TRUE
;
3479 // invalidate it as it has to be recalculated
3483 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3485 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3486 _T("invalid column index") );
3488 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3489 _T("SetColumnWidth() can only be called in report mode.") );
3492 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3494 headerWin
->m_dirty
= TRUE
;
3496 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3497 wxCHECK_RET( node
, _T("no column?") );
3499 wxListHeaderData
*column
= node
->GetData();
3501 size_t count
= GetItemCount();
3503 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3505 width
= GetTextLength(column
->GetText());
3507 else if ( width
== wxLIST_AUTOSIZE
)
3511 // TODO: determine the max width somehow...
3512 width
= WIDTH_COL_DEFAULT
;
3516 wxClientDC
dc(this);
3517 dc
.SetFont( GetFont() );
3519 int max
= AUTOSIZE_COL_MARGIN
;
3521 for ( size_t i
= 0; i
< count
; i
++ )
3523 wxListLineData
*line
= GetLine(i
);
3524 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3526 wxCHECK_RET( n
, _T("no subitem?") );
3528 wxListItemData
*item
= n
->GetData();
3531 if (item
->HasImage())
3534 GetImageSize( item
->GetImage(), ix
, iy
);
3538 if (item
->HasText())
3541 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3549 width
= max
+ AUTOSIZE_COL_MARGIN
;
3553 column
->SetWidth( width
);
3555 // invalidate it as it has to be recalculated
3559 int wxListMainWindow::GetHeaderWidth() const
3561 if ( !m_headerWidth
)
3563 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3565 size_t count
= GetColumnCount();
3566 for ( size_t col
= 0; col
< count
; col
++ )
3568 self
->m_headerWidth
+= GetColumnWidth(col
);
3572 return m_headerWidth
;
3575 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3577 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3578 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3580 wxListHeaderData
*column
= node
->GetData();
3581 column
->GetItem( item
);
3584 int wxListMainWindow::GetColumnWidth( int col
) const
3586 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3587 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3589 wxListHeaderData
*column
= node
->GetData();
3590 return column
->GetWidth();
3593 // ----------------------------------------------------------------------------
3595 // ----------------------------------------------------------------------------
3597 void wxListMainWindow::SetItem( wxListItem
&item
)
3599 long id
= item
.m_itemId
;
3600 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3601 _T("invalid item index in SetItem") );
3605 wxListLineData
*line
= GetLine((size_t)id
);
3606 line
->SetItem( item
.m_col
, item
);
3609 // update the item on screen
3611 GetItemRect(id
, rectItem
);
3612 RefreshRect(rectItem
);
3615 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3617 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3618 _T("invalid list ctrl item index in SetItem") );
3620 size_t oldCurrent
= m_current
;
3621 size_t item
= (size_t)litem
; // safe because of the check above
3623 // do we need to change the focus?
3624 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3626 if ( state
& wxLIST_STATE_FOCUSED
)
3628 // don't do anything if this item is already focused
3629 if ( item
!= m_current
)
3631 ChangeCurrent(item
);
3633 if ( oldCurrent
!= (size_t)-1 )
3635 if ( IsSingleSel() )
3637 HighlightLine(oldCurrent
, FALSE
);
3640 RefreshLine(oldCurrent
);
3643 RefreshLine( m_current
);
3648 // don't do anything if this item is not focused
3649 if ( item
== m_current
)
3653 if ( IsSingleSel() )
3655 // we must unselect the old current item as well or we
3656 // might end up with more than one selected item in a
3657 // single selection control
3658 HighlightLine(oldCurrent
, FALSE
);
3661 RefreshLine( oldCurrent
);
3666 // do we need to change the selection state?
3667 if ( stateMask
& wxLIST_STATE_SELECTED
)
3669 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3671 if ( IsSingleSel() )
3675 // selecting the item also makes it the focused one in the
3677 if ( m_current
!= item
)
3679 ChangeCurrent(item
);
3681 if ( oldCurrent
!= (size_t)-1 )
3683 HighlightLine( oldCurrent
, FALSE
);
3684 RefreshLine( oldCurrent
);
3690 // only the current item may be selected anyhow
3691 if ( item
!= m_current
)
3696 if ( HighlightLine(item
, on
) )
3703 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3705 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3706 _T("invalid list ctrl item index in GetItemState()") );
3708 int ret
= wxLIST_STATE_DONTCARE
;
3710 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3712 if ( (size_t)item
== m_current
)
3713 ret
|= wxLIST_STATE_FOCUSED
;
3716 if ( stateMask
& wxLIST_STATE_SELECTED
)
3718 if ( IsHighlighted(item
) )
3719 ret
|= wxLIST_STATE_SELECTED
;
3725 void wxListMainWindow::GetItem( wxListItem
&item
) const
3727 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3728 _T("invalid item index in GetItem") );
3730 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3731 line
->GetItem( item
.m_col
, item
);
3734 // ----------------------------------------------------------------------------
3736 // ----------------------------------------------------------------------------
3738 size_t wxListMainWindow::GetItemCount() const
3740 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3743 void wxListMainWindow::SetItemCount(long count
)
3745 m_selStore
.SetItemCount(count
);
3746 m_countVirt
= count
;
3748 ResetVisibleLinesRange();
3750 // scrollbars must be reset
3754 int wxListMainWindow::GetSelectedItemCount() const
3756 // deal with the quick case first
3757 if ( IsSingleSel() )
3759 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3762 // virtual controls remmebers all its selections itself
3764 return m_selStore
.GetSelectedCount();
3766 // TODO: we probably should maintain the number of items selected even for
3767 // non virtual controls as enumerating all lines is really slow...
3768 size_t countSel
= 0;
3769 size_t count
= GetItemCount();
3770 for ( size_t line
= 0; line
< count
; line
++ )
3772 if ( GetLine(line
)->IsHighlighted() )
3779 // ----------------------------------------------------------------------------
3780 // item position/size
3781 // ----------------------------------------------------------------------------
3783 wxRect
wxListMainWindow::GetViewRect() const
3785 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3786 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3788 // we need to find the longest/tallest label
3791 const int count
= GetItemCount();
3794 for ( int i
= 0; i
< count
; i
++ )
3799 wxCoord x
= r
.GetRight(),
3809 // some fudge needed to make it look prettier
3810 xMax
+= 2*EXTRA_BORDER_X
;
3811 yMax
+= 2*EXTRA_BORDER_Y
;
3813 // account for the scrollbars if necessary
3814 const wxSize sizeAll
= GetClientSize();
3815 if ( xMax
> sizeAll
.x
)
3816 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3817 if ( yMax
> sizeAll
.y
)
3818 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3820 return wxRect(0, 0, xMax
, yMax
);
3823 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3825 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3826 _T("invalid index in GetItemRect") );
3828 // ensure that we're laid out, otherwise we could return nonsense
3831 wxConstCast(this, wxListMainWindow
)->
3832 RecalculatePositions(TRUE
/* no refresh */);
3835 rect
= GetLineRect((size_t)index
);
3837 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3840 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3843 GetItemRect(item
, rect
);
3851 // ----------------------------------------------------------------------------
3852 // geometry calculation
3853 // ----------------------------------------------------------------------------
3855 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3857 wxClientDC
dc( this );
3858 dc
.SetFont( GetFont() );
3860 const size_t count
= GetItemCount();
3863 if ( HasFlag(wxLC_ICON
) )
3864 iconSpacing
= m_normal_spacing
;
3865 else if ( HasFlag(wxLC_SMALL_ICON
) )
3866 iconSpacing
= m_small_spacing
;
3870 // Note that we do not call GetClientSize() here but
3871 // GetSize() and substract the border size for sunken
3872 // borders manually. This is technically incorrect,
3873 // but we need to know the client area's size WITHOUT
3874 // scrollbars here. Since we don't know if there are
3875 // any scrollbars, we use GetSize() instead. Another
3876 // solution would be to call SetScrollbars() here to
3877 // remove the scrollbars and call GetClientSize() then,
3878 // but this might result in flicker and - worse - will
3879 // reset the scrollbars to 0 which is not good at all
3880 // if you resize a dialog/window, but don't want to
3881 // reset the window scrolling. RR.
3882 // Furthermore, we actually do NOT subtract the border
3883 // width as 2 pixels is just the extra space which we
3884 // need around the actual content in the window. Other-
3885 // wise the text would e.g. touch the upper border. RR.
3888 GetSize( &clientWidth
, &clientHeight
);
3890 if ( HasFlag(wxLC_REPORT
) )
3892 // all lines have the same height and we scroll one line per step
3893 int lineHeight
= GetLineHeight();
3895 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3897 m_linesPerPage
= clientHeight
/ lineHeight
;
3899 ResetVisibleLinesRange();
3901 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3902 GetHeaderWidth() / SCROLL_UNIT_X
,
3903 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3904 GetScrollPos(wxHORIZONTAL
),
3905 GetScrollPos(wxVERTICAL
),
3910 // we have 3 different layout strategies: either layout all items
3911 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3912 // to arrange them in top to bottom, left to right (don't ask me why
3913 // not the other way round...) order
3914 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3916 int x
= EXTRA_BORDER_X
;
3917 int y
= EXTRA_BORDER_Y
;
3919 wxCoord widthMax
= 0;
3922 for ( i
= 0; i
< count
; i
++ )
3924 wxListLineData
*line
= GetLine(i
);
3925 line
->CalculateSize( &dc
, iconSpacing
);
3926 line
->SetPosition( x
, y
, iconSpacing
);
3928 wxSize sizeLine
= GetLineSize(i
);
3930 if ( HasFlag(wxLC_ALIGN_TOP
) )
3932 if ( sizeLine
.x
> widthMax
)
3933 widthMax
= sizeLine
.x
;
3937 else // wxLC_ALIGN_LEFT
3939 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3943 if ( HasFlag(wxLC_ALIGN_TOP
) )
3945 // traverse the items again and tweak their sizes so that they are
3946 // all the same in a row
3947 for ( i
= 0; i
< count
; i
++ )
3949 wxListLineData
*line
= GetLine(i
);
3950 line
->m_gi
->ExtendWidth(widthMax
);
3959 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3960 (y
+ SCROLL_UNIT_Y
) / SCROLL_UNIT_Y
,
3961 GetScrollPos( wxHORIZONTAL
),
3962 GetScrollPos( wxVERTICAL
),
3966 else // "flowed" arrangement, the most complicated case
3968 // at first we try without any scrollbars, if the items don't fit into
3969 // the window, we recalculate after subtracting the space taken by the
3972 int entireWidth
= 0;
3974 // entireHeight is not used so no need to define it
3975 int entireHeight
= 0;
3978 for (int tries
= 0; tries
< 2; tries
++)
3980 entireWidth
= 2*EXTRA_BORDER_X
;
3982 // entireHeight is not used so no need to define it
3983 entireHeight
= 2*EXTRA_BORDER_Y
;
3988 // Now we have decided that the items do not fit into the
3989 // client area, so we need a scrollbar
3990 entireWidth
+= SCROLL_UNIT_X
;
3993 int x
= EXTRA_BORDER_X
;
3994 int y
= EXTRA_BORDER_Y
;
3995 int maxWidthInThisRow
= 0;
3998 int currentlyVisibleLines
= 0;
4000 for (size_t i
= 0; i
< count
; i
++)
4002 currentlyVisibleLines
++;
4003 wxListLineData
*line
= GetLine(i
);
4004 line
->CalculateSize( &dc
, iconSpacing
);
4005 line
->SetPosition( x
, y
, iconSpacing
);
4007 wxSize sizeLine
= GetLineSize(i
);
4009 if ( maxWidthInThisRow
< sizeLine
.x
)
4010 maxWidthInThisRow
= sizeLine
.x
;
4013 if (currentlyVisibleLines
> m_linesPerPage
)
4014 m_linesPerPage
= currentlyVisibleLines
;
4016 if ( y
+ sizeLine
.y
>= clientHeight
)
4018 currentlyVisibleLines
= 0;
4020 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4021 x
+= maxWidthInThisRow
;
4022 entireWidth
+= maxWidthInThisRow
;
4023 maxWidthInThisRow
= 0;
4026 // We have reached the last item.
4027 if ( i
== count
- 1 )
4028 entireWidth
+= maxWidthInThisRow
;
4030 if ( (tries
== 0) &&
4031 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4033 clientHeight
-= wxSystemSettings::
4034 GetMetric(wxSYS_HSCROLL_Y
);
4036 currentlyVisibleLines
= 0;
4040 if ( i
== count
- 1 )
4041 tries
= 1; // Everything fits, no second try required.
4049 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4051 GetScrollPos( wxHORIZONTAL
),
4060 // FIXME: why should we call it from here?
4067 void wxListMainWindow::RefreshAll()
4072 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4073 if ( headerWin
&& headerWin
->m_dirty
)
4075 headerWin
->m_dirty
= FALSE
;
4076 headerWin
->Refresh();
4080 void wxListMainWindow::UpdateCurrent()
4082 if ( !HasCurrent() && !IsEmpty() )
4088 long wxListMainWindow::GetNextItem( long item
,
4089 int WXUNUSED(geometry
),
4093 max
= GetItemCount();
4094 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4095 _T("invalid listctrl index in GetNextItem()") );
4097 // notice that we start with the next item (or the first one if item == -1)
4098 // and this is intentional to allow writing a simple loop to iterate over
4099 // all selected items
4103 // this is not an error because the index was ok initially, just no
4114 size_t count
= GetItemCount();
4115 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4117 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4120 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4127 // ----------------------------------------------------------------------------
4129 // ----------------------------------------------------------------------------
4131 void wxListMainWindow::DeleteItem( long lindex
)
4133 size_t count
= GetItemCount();
4135 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4136 _T("invalid item index in DeleteItem") );
4138 size_t index
= (size_t)lindex
;
4140 // we don't need to adjust the index for the previous items
4141 if ( HasCurrent() && m_current
>= index
)
4143 // if the current item is being deleted, we want the next one to
4144 // become selected - unless there is no next one - so don't adjust
4145 // m_current in this case
4146 if ( m_current
!= index
|| m_current
== count
- 1 )
4152 if ( InReportView() )
4154 ResetVisibleLinesRange();
4161 m_selStore
.OnItemDelete(index
);
4165 m_lines
.RemoveAt( index
);
4168 // we need to refresh the (vert) scrollbar as the number of items changed
4171 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4173 RefreshAfter(index
);
4176 void wxListMainWindow::DeleteColumn( int col
)
4178 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4180 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4183 delete node
->GetData();
4184 m_columns
.Erase( node
);
4188 // update all the items
4189 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4191 wxListLineData
* const line
= GetLine(i
);
4192 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4193 delete n
->GetData();
4194 line
->m_items
.Erase(n
);
4198 // invalidate it as it has to be recalculated
4202 void wxListMainWindow::DoDeleteAllItems()
4206 // nothing to do - in particular, don't send the event
4212 // to make the deletion of all items faster, we don't send the
4213 // notifications for each item deletion in this case but only one event
4214 // for all of them: this is compatible with wxMSW and documented in
4215 // DeleteAllItems() description
4217 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4218 event
.SetEventObject( GetParent() );
4219 GetParent()->GetEventHandler()->ProcessEvent( event
);
4228 if ( InReportView() )
4230 ResetVisibleLinesRange();
4236 void wxListMainWindow::DeleteAllItems()
4240 RecalculatePositions();
4243 void wxListMainWindow::DeleteEverything()
4245 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4250 // ----------------------------------------------------------------------------
4251 // scanning for an item
4252 // ----------------------------------------------------------------------------
4254 void wxListMainWindow::EnsureVisible( long index
)
4256 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4257 _T("invalid index in EnsureVisible") );
4259 // We have to call this here because the label in question might just have
4260 // been added and its position is not known yet
4263 RecalculatePositions(TRUE
/* no refresh */);
4266 MoveToItem((size_t)index
);
4269 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4276 size_t count
= GetItemCount();
4277 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4279 wxListLineData
*line
= GetLine(i
);
4280 if ( line
->GetText(0) == tmp
)
4287 long wxListMainWindow::FindItem(long start
, long data
)
4293 size_t count
= GetItemCount();
4294 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4296 wxListLineData
*line
= GetLine(i
);
4298 line
->GetItem( 0, item
);
4299 if (item
.m_data
== data
)
4306 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4308 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4310 size_t count
= GetItemCount();
4312 if ( HasFlag(wxLC_REPORT
) )
4314 size_t current
= y
/ GetLineHeight();
4315 if ( current
< count
)
4317 flags
= HitTestLine(current
, x
, y
);
4324 // TODO: optimize it too! this is less simple than for report view but
4325 // enumerating all items is still not a way to do it!!
4326 for ( size_t current
= 0; current
< count
; current
++ )
4328 flags
= HitTestLine(current
, x
, y
);
4337 // ----------------------------------------------------------------------------
4339 // ----------------------------------------------------------------------------
4341 void wxListMainWindow::InsertItem( wxListItem
&item
)
4343 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4345 size_t count
= GetItemCount();
4346 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4347 _T("invalid item index") );
4349 size_t id
= item
.m_itemId
;
4354 // this is unused variable
4357 if ( HasFlag(wxLC_REPORT
) )
4360 // this is unused variable
4363 ResetVisibleLinesRange();
4365 else if ( HasFlag(wxLC_LIST
) )
4367 // this is unused variable
4372 else if ( HasFlag(wxLC_ICON
) )
4374 // this is unused variable
4379 else if ( HasFlag(wxLC_SMALL_ICON
) )
4381 // this is unused variable
4382 mode
= wxLC_ICON
; // no typo
4388 wxFAIL_MSG( _T("unknown mode") );
4391 wxListLineData
*line
= new wxListLineData(this);
4393 line
->SetItem( 0, item
);
4395 m_lines
.Insert( line
, id
);
4399 // If an item is selected at or below the point of insertion, we need to
4400 // increment the member variables because the current row's index has gone
4402 if ( HasCurrent() && m_current
>= id
)
4407 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4409 RefreshLines(id
, GetItemCount() - 1);
4412 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4415 if ( HasFlag(wxLC_REPORT
) )
4417 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4418 item
.m_width
= GetTextLength( item
.m_text
);
4420 wxListHeaderData
*column
= new wxListHeaderData( item
);
4421 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4424 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4425 m_columns
.Insert( node
, column
);
4429 m_columns
.Append( column
);
4434 // update all the items
4435 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4437 wxListLineData
* const line
= GetLine(i
);
4438 wxListItemData
* const data
= new wxListItemData(this);
4440 line
->m_items
.Insert(col
, data
);
4442 line
->m_items
.Append(data
);
4446 // invalidate it as it has to be recalculated
4451 // ----------------------------------------------------------------------------
4453 // ----------------------------------------------------------------------------
4455 wxListCtrlCompare list_ctrl_compare_func_2
;
4456 long list_ctrl_compare_data
;
4458 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4460 wxListLineData
*line1
= *arg1
;
4461 wxListLineData
*line2
= *arg2
;
4463 line1
->GetItem( 0, item
);
4464 long data1
= item
.m_data
;
4465 line2
->GetItem( 0, item
);
4466 long data2
= item
.m_data
;
4467 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4470 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4472 list_ctrl_compare_func_2
= fn
;
4473 list_ctrl_compare_data
= data
;
4474 m_lines
.Sort( list_ctrl_compare_func_1
);
4478 // ----------------------------------------------------------------------------
4480 // ----------------------------------------------------------------------------
4482 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4484 // update our idea of which lines are shown when we redraw the window the
4486 ResetVisibleLinesRange();
4489 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4490 wxScrolledWindow::OnScroll(event
);
4492 HandleOnScroll( event
);
4495 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4497 wxGenericListCtrl
* lc
= GetListCtrl();
4498 wxCHECK_RET( lc
, _T("no listctrl window?") );
4500 lc
->m_headerWin
->Refresh();
4501 lc
->m_headerWin
->Update();
4505 int wxListMainWindow::GetCountPerPage() const
4507 if ( !m_linesPerPage
)
4509 wxConstCast(this, wxListMainWindow
)->
4510 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4513 return m_linesPerPage
;
4516 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4518 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4520 if ( m_lineFrom
== (size_t)-1 )
4522 size_t count
= GetItemCount();
4525 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4527 // this may happen if SetScrollbars() hadn't been called yet
4528 if ( m_lineFrom
>= count
)
4529 m_lineFrom
= count
- 1;
4531 // we redraw one extra line but this is needed to make the redrawing
4532 // logic work when there is a fractional number of lines on screen
4533 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4534 if ( m_lineTo
>= count
)
4535 m_lineTo
= count
- 1;
4537 else // empty control
4540 m_lineTo
= (size_t)-1;
4544 wxASSERT_MSG( IsEmpty() ||
4545 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4546 _T("GetVisibleLinesRange() returns incorrect result") );
4554 // -------------------------------------------------------------------------------------
4555 // wxGenericListCtrl
4556 // -------------------------------------------------------------------------------------
4558 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4560 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4561 EVT_SIZE(wxGenericListCtrl::OnSize
)
4564 wxGenericListCtrl::wxGenericListCtrl()
4566 m_imageListNormal
= (wxImageListType
*) NULL
;
4567 m_imageListSmall
= (wxImageListType
*) NULL
;
4568 m_imageListState
= (wxImageListType
*) NULL
;
4570 m_ownsImageListNormal
=
4571 m_ownsImageListSmall
=
4572 m_ownsImageListState
= FALSE
;
4574 m_mainWin
= (wxListMainWindow
*) NULL
;
4575 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4579 wxGenericListCtrl::~wxGenericListCtrl()
4581 if (m_ownsImageListNormal
)
4582 delete m_imageListNormal
;
4583 if (m_ownsImageListSmall
)
4584 delete m_imageListSmall
;
4585 if (m_ownsImageListState
)
4586 delete m_imageListState
;
4589 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4593 // we use 'g' to get the descent, too
4595 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4596 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4598 // only update if there is not enough space
4599 if ( h
> m_headerHeight
)
4603 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4605 if ( HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
) )
4606 ResizeReportView(TRUE
);
4611 void wxGenericListCtrl::CreateHeaderWindow()
4613 m_headerWin
= new wxListHeaderWindow
4615 this, -1, m_mainWin
,
4617 wxSize(GetClientSize().x
, m_headerHeight
),
4620 CalculateAndSetHeaderHeight();
4623 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4628 const wxValidator
&validator
,
4629 const wxString
&name
)
4633 m_imageListState
= (wxImageListType
*) NULL
;
4634 m_ownsImageListNormal
=
4635 m_ownsImageListSmall
=
4636 m_ownsImageListState
= FALSE
;
4638 m_mainWin
= (wxListMainWindow
*) NULL
;
4639 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4641 if ( !(style
& wxLC_MASK_TYPE
) )
4643 style
= style
| wxLC_LIST
;
4646 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4649 // don't create the inner window with the border
4650 style
&= ~wxBORDER_MASK
;
4652 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4654 if ( HasFlag(wxLC_REPORT
) )
4656 CreateHeaderWindow();
4658 if ( HasFlag(wxLC_NO_HEADER
) )
4660 // VZ: why do we create it at all then?
4661 m_headerWin
->Show( FALSE
);
4668 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4670 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4671 _T("wxLC_VIRTUAL can't be [un]set") );
4673 long flag
= GetWindowStyle();
4677 if (style
& wxLC_MASK_TYPE
)
4678 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4679 if (style
& wxLC_MASK_ALIGN
)
4680 flag
&= ~wxLC_MASK_ALIGN
;
4681 if (style
& wxLC_MASK_SORT
)
4682 flag
&= ~wxLC_MASK_SORT
;
4694 SetWindowStyleFlag( flag
);
4697 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4701 m_mainWin
->DeleteEverything();
4703 // has the header visibility changed?
4704 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4705 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4707 if ( hasHeader
!= willHaveHeader
)
4714 // don't delete, just hide, as we can reuse it later
4715 m_headerWin
->Show(FALSE
);
4717 //else: nothing to do
4719 else // must show header
4723 CreateHeaderWindow();
4725 else // already have it, just show
4727 m_headerWin
->Show( TRUE
);
4731 ResizeReportView(willHaveHeader
);
4735 wxWindow::SetWindowStyleFlag( flag
);
4738 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4740 m_mainWin
->GetColumn( col
, item
);
4744 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4746 m_mainWin
->SetColumn( col
, item
);
4750 int wxGenericListCtrl::GetColumnWidth( int col
) const
4752 return m_mainWin
->GetColumnWidth( col
);
4755 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4757 m_mainWin
->SetColumnWidth( col
, width
);
4761 int wxGenericListCtrl::GetCountPerPage() const
4763 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4766 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4768 m_mainWin
->GetItem( info
);
4772 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4774 m_mainWin
->SetItem( info
);
4778 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4781 info
.m_text
= label
;
4782 info
.m_mask
= wxLIST_MASK_TEXT
;
4783 info
.m_itemId
= index
;
4787 info
.m_image
= imageId
;
4788 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4790 m_mainWin
->SetItem(info
);
4794 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4796 return m_mainWin
->GetItemState( item
, stateMask
);
4799 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4801 m_mainWin
->SetItemState( item
, state
, stateMask
);
4805 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4808 info
.m_image
= image
;
4809 info
.m_mask
= wxLIST_MASK_IMAGE
;
4810 info
.m_itemId
= item
;
4811 m_mainWin
->SetItem( info
);
4815 wxString
wxGenericListCtrl::GetItemText( long item
) const
4817 return m_mainWin
->GetItemText(item
);
4820 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4822 m_mainWin
->SetItemText(item
, str
);
4825 long wxGenericListCtrl::GetItemData( long item
) const
4828 info
.m_itemId
= item
;
4829 m_mainWin
->GetItem( info
);
4833 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4836 info
.m_mask
= wxLIST_MASK_DATA
;
4837 info
.m_itemId
= item
;
4839 m_mainWin
->SetItem( info
);
4843 wxRect
wxGenericListCtrl::GetViewRect() const
4845 return m_mainWin
->GetViewRect();
4848 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4850 m_mainWin
->GetItemRect( item
, rect
);
4851 if ( m_mainWin
->HasHeader() )
4852 rect
.y
+= m_headerHeight
+ 1;
4856 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4858 m_mainWin
->GetItemPosition( item
, pos
);
4862 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4867 int wxGenericListCtrl::GetItemCount() const
4869 return m_mainWin
->GetItemCount();
4872 int wxGenericListCtrl::GetColumnCount() const
4874 return m_mainWin
->GetColumnCount();
4877 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4879 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4882 wxSize
wxGenericListCtrl::GetItemSpacing() const
4884 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4886 return wxSize(spacing
, spacing
);
4889 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4891 return m_mainWin
->GetItemSpacing( isSmall
);
4894 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4897 info
.m_itemId
= item
;
4898 info
.SetTextColour( col
);
4899 m_mainWin
->SetItem( info
);
4902 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4905 info
.m_itemId
= item
;
4906 m_mainWin
->GetItem( info
);
4907 return info
.GetTextColour();
4910 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4913 info
.m_itemId
= item
;
4914 info
.SetBackgroundColour( col
);
4915 m_mainWin
->SetItem( info
);
4918 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4921 info
.m_itemId
= item
;
4922 m_mainWin
->GetItem( info
);
4923 return info
.GetBackgroundColour();
4926 int wxGenericListCtrl::GetSelectedItemCount() const
4928 return m_mainWin
->GetSelectedItemCount();
4931 wxColour
wxGenericListCtrl::GetTextColour() const
4933 return GetForegroundColour();
4936 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4938 SetForegroundColour(col
);
4941 long wxGenericListCtrl::GetTopItem() const
4944 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4948 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4950 return m_mainWin
->GetNextItem( item
, geom
, state
);
4953 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4955 if (which
== wxIMAGE_LIST_NORMAL
)
4957 return m_imageListNormal
;
4959 else if (which
== wxIMAGE_LIST_SMALL
)
4961 return m_imageListSmall
;
4963 else if (which
== wxIMAGE_LIST_STATE
)
4965 return m_imageListState
;
4967 return (wxImageListType
*) NULL
;
4970 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4972 if ( which
== wxIMAGE_LIST_NORMAL
)
4974 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4975 m_imageListNormal
= imageList
;
4976 m_ownsImageListNormal
= FALSE
;
4978 else if ( which
== wxIMAGE_LIST_SMALL
)
4980 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4981 m_imageListSmall
= imageList
;
4982 m_ownsImageListSmall
= FALSE
;
4984 else if ( which
== wxIMAGE_LIST_STATE
)
4986 if (m_ownsImageListState
) delete m_imageListState
;
4987 m_imageListState
= imageList
;
4988 m_ownsImageListState
= FALSE
;
4991 m_mainWin
->SetImageList( imageList
, which
);
4994 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4996 SetImageList(imageList
, which
);
4997 if ( which
== wxIMAGE_LIST_NORMAL
)
4998 m_ownsImageListNormal
= TRUE
;
4999 else if ( which
== wxIMAGE_LIST_SMALL
)
5000 m_ownsImageListSmall
= TRUE
;
5001 else if ( which
== wxIMAGE_LIST_STATE
)
5002 m_ownsImageListState
= TRUE
;
5005 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5010 bool wxGenericListCtrl::DeleteItem( long item
)
5012 m_mainWin
->DeleteItem( item
);
5016 bool wxGenericListCtrl::DeleteAllItems()
5018 m_mainWin
->DeleteAllItems();
5022 bool wxGenericListCtrl::DeleteAllColumns()
5024 size_t count
= m_mainWin
->m_columns
.GetCount();
5025 for ( size_t n
= 0; n
< count
; n
++ )
5031 void wxGenericListCtrl::ClearAll()
5033 m_mainWin
->DeleteEverything();
5036 bool wxGenericListCtrl::DeleteColumn( int col
)
5038 m_mainWin
->DeleteColumn( col
);
5040 // if we don't have the header any longer, we need to relayout the window
5041 if ( !GetColumnCount() )
5043 ResizeReportView(FALSE
/* no header */);
5049 void wxGenericListCtrl::Edit( long item
)
5051 m_mainWin
->EditLabel( item
);
5054 bool wxGenericListCtrl::EnsureVisible( long item
)
5056 m_mainWin
->EnsureVisible( item
);
5060 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5062 return m_mainWin
->FindItem( start
, str
, partial
);
5065 long wxGenericListCtrl::FindItem( long start
, long data
)
5067 return m_mainWin
->FindItem( start
, data
);
5070 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
5071 int WXUNUSED(direction
))
5076 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5078 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5081 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5083 m_mainWin
->InsertItem( info
);
5084 return info
.m_itemId
;
5087 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5090 info
.m_text
= label
;
5091 info
.m_mask
= wxLIST_MASK_TEXT
;
5092 info
.m_itemId
= index
;
5093 return InsertItem( info
);
5096 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5099 info
.m_mask
= wxLIST_MASK_IMAGE
;
5100 info
.m_image
= imageIndex
;
5101 info
.m_itemId
= index
;
5102 return InsertItem( info
);
5105 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5108 info
.m_text
= label
;
5109 info
.m_image
= imageIndex
;
5110 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5111 info
.m_itemId
= index
;
5112 return InsertItem( info
);
5115 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5117 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5119 m_mainWin
->InsertColumn( col
, item
);
5121 // if we hadn't had header before and have it now we need to relayout the
5123 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5125 ResizeReportView(TRUE
/* have header */);
5128 m_headerWin
->Refresh();
5133 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5134 int format
, int width
)
5137 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5138 item
.m_text
= heading
;
5141 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5142 item
.m_width
= width
;
5144 item
.m_format
= format
;
5146 return InsertColumn( col
, item
);
5149 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5155 // fn is a function which takes 3 long arguments: item1, item2, data.
5156 // item1 is the long data associated with a first item (NOT the index).
5157 // item2 is the long data associated with a second item (NOT the index).
5158 // data is the same value as passed to SortItems.
5159 // The return value is a negative number if the first item should precede the second
5160 // item, a positive number of the second item should precede the first,
5161 // or zero if the two items are equivalent.
5162 // data is arbitrary data to be passed to the sort function.
5164 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5166 m_mainWin
->SortItems( fn
, data
);
5170 // ----------------------------------------------------------------------------
5172 // ----------------------------------------------------------------------------
5174 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5179 ResizeReportView(m_mainWin
->HasHeader());
5181 m_mainWin
->RecalculatePositions();
5184 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5187 GetClientSize( &cw
, &ch
);
5191 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5192 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5194 else // no header window
5196 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5200 void wxGenericListCtrl::OnInternalIdle()
5202 wxWindow::OnInternalIdle();
5204 // do it only if needed
5205 if ( !m_mainWin
->m_dirty
)
5208 m_mainWin
->RecalculatePositions();
5211 // ----------------------------------------------------------------------------
5213 // ----------------------------------------------------------------------------
5215 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5219 m_mainWin
->SetBackgroundColour( colour
);
5220 m_mainWin
->m_dirty
= TRUE
;
5226 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5228 if ( !wxWindow::SetForegroundColour( colour
) )
5233 m_mainWin
->SetForegroundColour( colour
);
5234 m_mainWin
->m_dirty
= TRUE
;
5239 m_headerWin
->SetForegroundColour( colour
);
5245 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5247 if ( !wxWindow::SetFont( font
) )
5252 m_mainWin
->SetFont( font
);
5253 m_mainWin
->m_dirty
= TRUE
;
5258 m_headerWin
->SetFont( font
);
5259 CalculateAndSetHeaderHeight();
5267 // ----------------------------------------------------------------------------
5268 // methods forwarded to m_mainWin
5269 // ----------------------------------------------------------------------------
5271 #if wxUSE_DRAG_AND_DROP
5273 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5275 m_mainWin
->SetDropTarget( dropTarget
);
5278 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5280 return m_mainWin
->GetDropTarget();
5283 #endif // wxUSE_DRAG_AND_DROP
5285 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5287 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5290 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5292 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5295 wxColour
wxGenericListCtrl::GetForegroundColour() const
5297 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5300 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5303 return m_mainWin
->PopupMenu( menu
, x
, y
);
5306 #endif // wxUSE_MENUS
5309 void wxGenericListCtrl::SetFocus()
5311 /* The test in window.cpp fails as we are a composite
5312 window, so it checks against "this", but not m_mainWin. */
5313 if ( FindFocus() != this )
5314 m_mainWin
->SetFocus();
5317 // ----------------------------------------------------------------------------
5318 // virtual list control support
5319 // ----------------------------------------------------------------------------
5321 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5323 // this is a pure virtual function, in fact - which is not really pure
5324 // because the controls which are not virtual don't need to implement it
5325 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5327 return wxEmptyString
;
5330 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5333 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5338 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5340 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5341 _T("invalid item index in OnGetItemAttr()") );
5343 // no attributes by default
5347 void wxGenericListCtrl::SetItemCount(long count
)
5349 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5351 m_mainWin
->SetItemCount(count
);
5354 void wxGenericListCtrl::RefreshItem(long item
)
5356 m_mainWin
->RefreshLine(item
);
5359 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5361 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5365 * Generic wxListCtrl is more or less a container for two other
5366 * windows which drawings are done upon. These are namely
5367 * 'm_headerWin' and 'm_mainWin'.
5368 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5369 * behaviour wxListCtrl has under wxMSW.
5371 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5375 // The easy case, no rectangle specified.
5377 m_headerWin
->Refresh(eraseBackground
);
5380 m_mainWin
->Refresh(eraseBackground
);
5384 // Refresh the header window
5387 wxRect rectHeader
= m_headerWin
->GetRect();
5388 rectHeader
.Intersect(*rect
);
5389 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5392 m_headerWin
->GetPosition(&x
, &y
);
5393 rectHeader
.Offset(-x
, -y
);
5394 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5399 // Refresh the main window
5402 wxRect rectMain
= m_mainWin
->GetRect();
5403 rectMain
.Intersect(*rect
);
5404 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5407 m_mainWin
->GetPosition(&x
, &y
);
5408 rectMain
.Offset(-x
, -y
);
5409 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5415 void wxGenericListCtrl::Freeze()
5417 m_mainWin
->Freeze();
5420 void wxGenericListCtrl::Thaw()
5425 #endif // wxUSE_LISTCTRL