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"
72 #include "wx/renderer.h"
76 #include "wx/mac/private.h"
81 // NOTE: If using the wxListBox visual attributes works everywhere then this can
82 // be removed, as well as the #else case below.
83 #define _USE_VISATTR 0
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
96 #if WXWIN_COMPATIBILITY_2_4
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
104 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
112 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
113 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // // the height of the header window (FIXME: should depend on its font!)
120 // static const int HEADER_HEIGHT = 23;
122 static const int SCROLL_UNIT_X
= 15;
124 // the spacing between the lines (in report mode)
125 static const int LINE_SPACING
= 0;
127 // extra margins around the text label
128 static const int EXTRA_WIDTH
= 4;
129 static const int EXTRA_HEIGHT
= 4;
131 // margin between the window and the items
132 static const int EXTRA_BORDER_X
= 2;
133 static const int EXTRA_BORDER_Y
= 2;
135 // offset for the header window
136 static const int HEADER_OFFSET_X
= 1;
137 static const int HEADER_OFFSET_Y
= 1;
139 // margin between rows of icons in [small] icon view
140 static const int MARGIN_BETWEEN_ROWS
= 6;
142 // when autosizing the columns, add some slack
143 static const int AUTOSIZE_COL_MARGIN
= 10;
145 // default and minimal widths for the header columns
146 static const int WIDTH_COL_DEFAULT
= 80;
147 static const int WIDTH_COL_MIN
= 10;
149 // the space between the image and the text in the report mode
150 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
152 // ============================================================================
154 // ============================================================================
156 //-----------------------------------------------------------------------------
157 // wxListItemData (internal)
158 //-----------------------------------------------------------------------------
160 class WXDLLEXPORT wxListItemData
163 wxListItemData(wxListMainWindow
*owner
);
166 void SetItem( const wxListItem
&info
);
167 void SetImage( int image
) { m_image
= image
; }
168 void SetData( wxUIntPtr data
) { m_data
= data
; }
169 void SetPosition( int x
, int y
);
170 void SetSize( int width
, int height
);
172 bool HasText() const { return !m_text
.empty(); }
173 const wxString
& GetText() const { return m_text
; }
174 void SetText(const wxString
& text
) { m_text
= text
; }
176 // we can't use empty string for measuring the string width/height, so
177 // always return something
178 wxString
GetTextForMeasuring() const
180 wxString s
= GetText();
187 bool IsHit( int x
, int y
) const;
191 int GetWidth() const;
192 int GetHeight() const;
194 int GetImage() const { return m_image
; }
195 bool HasImage() const { return GetImage() != -1; }
197 void GetItem( wxListItem
&info
) const;
199 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
200 wxListItemAttr
*GetAttr() const { return m_attr
; }
203 // the item image or -1
206 // user data associated with the item
209 // the item coordinates are not used in report mode, instead this pointer
210 // is NULL and the owner window is used to retrieve the item position and
214 // the list ctrl we are in
215 wxListMainWindow
*m_owner
;
217 // custom attributes or NULL
218 wxListItemAttr
*m_attr
;
221 // common part of all ctors
227 //-----------------------------------------------------------------------------
228 // wxListHeaderData (internal)
229 //-----------------------------------------------------------------------------
231 class WXDLLEXPORT wxListHeaderData
: public wxObject
235 wxListHeaderData( const wxListItem
&info
);
236 void SetItem( const wxListItem
&item
);
237 void SetPosition( int x
, int y
);
238 void SetWidth( int w
);
239 void SetFormat( int format
);
240 void SetHeight( int h
);
241 bool HasImage() const;
243 bool HasText() const { return !m_text
.empty(); }
244 const wxString
& GetText() const { return m_text
; }
245 void SetText(const wxString
& text
) { m_text
= text
; }
247 void GetItem( wxListItem
&item
);
249 bool IsHit( int x
, int y
) const;
250 int GetImage() const;
251 int GetWidth() const;
252 int GetFormat() const;
268 //-----------------------------------------------------------------------------
269 // wxListLineData (internal)
270 //-----------------------------------------------------------------------------
272 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
273 #include "wx/listimpl.cpp"
274 WX_DEFINE_LIST(wxListItemDataList
);
279 // the list of subitems: only may have more than one item in report mode
280 wxListItemDataList m_items
;
282 // this is not used in report view
294 // the part to be highlighted
295 wxRect m_rectHighlight
;
297 // extend all our rects to be centered inside theo ne of given width
298 void ExtendWidth(wxCoord w
)
300 wxASSERT_MSG( m_rectAll
.width
<= w
,
301 _T("width can only be increased") );
304 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
)/2;
305 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
)/2;
306 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
)/2;
310 // is this item selected? [NB: not used in virtual mode]
313 // back pointer to the list ctrl
314 wxListMainWindow
*m_owner
;
317 wxListLineData(wxListMainWindow
*owner
);
321 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
325 // are we in report mode?
326 inline bool InReportView() const;
328 // are we in virtual report mode?
329 inline bool IsVirtual() const;
331 // these 2 methods shouldn't be called for report view controls, in that
332 // case we determine our position/size ourselves
334 // calculate the size of the line
335 void CalculateSize( wxDC
*dc
, int spacing
);
337 // remember the position this line appears at
338 void SetPosition( int x
, int y
, int spacing
);
342 void SetImage( int image
) { SetImage(0, image
); }
343 int GetImage() const { return GetImage(0); }
344 bool HasImage() const { return GetImage() != -1; }
345 bool HasText() const { return !GetText(0).empty(); }
347 void SetItem( int index
, const wxListItem
&info
);
348 void GetItem( int index
, wxListItem
&info
);
350 wxString
GetText(int index
) const;
351 void SetText( int index
, const wxString s
);
353 wxListItemAttr
*GetAttr() const;
354 void SetAttr(wxListItemAttr
*attr
);
356 // return true if the highlighting really changed
357 bool Highlight( bool on
);
359 void ReverseHighlight();
361 bool IsHighlighted() const
363 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
365 return m_highlighted
;
368 // draw the line on the given DC in icon/list mode
369 void Draw( wxDC
*dc
);
371 // the same in report mode
372 void DrawInReportMode( wxDC
*dc
,
374 const wxRect
& rectHL
,
378 // set the line to contain num items (only can be > 1 in report mode)
379 void InitItems( int num
);
381 // get the mode (i.e. style) of the list control
382 inline int GetMode() const;
384 // prepare the DC for drawing with these item's attributes, return true if
385 // we need to draw the items background to highlight it, false otherwise
386 bool SetAttributes(wxDC
*dc
,
387 const wxListItemAttr
*attr
,
390 // draw the text on the DC with the correct justification; also add an
391 // ellipsis if the text is too large to fit in the current width
392 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
394 // these are only used by GetImage/SetImage above, we don't support images
395 // with subitems at the public API level yet
396 void SetImage( int index
, int image
);
397 int GetImage( int index
) const;
400 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
401 #include "wx/arrimpl.cpp"
402 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
404 //-----------------------------------------------------------------------------
405 // wxListHeaderWindow (internal)
406 //-----------------------------------------------------------------------------
408 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
411 wxListMainWindow
*m_owner
;
412 wxCursor
*m_currentCursor
;
413 wxCursor
*m_resizeCursor
;
416 // column being resized or -1
419 // divider line position in logical (unscrolled) coords
422 // minimal position beyond which the divider line can't be dragged in
427 wxListHeaderWindow();
429 wxListHeaderWindow( wxWindow
*win
,
431 wxListMainWindow
*owner
,
432 const wxPoint
&pos
= wxDefaultPosition
,
433 const wxSize
&size
= wxDefaultSize
,
435 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
437 virtual ~wxListHeaderWindow();
440 void AdjustDC(wxDC
& dc
);
442 void OnPaint( wxPaintEvent
&event
);
443 void OnMouse( wxMouseEvent
&event
);
444 void OnSetFocus( wxFocusEvent
&event
);
450 // common part of all ctors
453 // generate and process the list event of the given type, return true if
454 // it wasn't vetoed, i.e. if we should proceed
455 bool SendListEvent(wxEventType type
, wxPoint pos
);
457 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
458 DECLARE_EVENT_TABLE()
461 //-----------------------------------------------------------------------------
462 // wxListRenameTimer (internal)
463 //-----------------------------------------------------------------------------
465 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
468 wxListMainWindow
*m_owner
;
471 wxListRenameTimer( wxListMainWindow
*owner
);
475 //-----------------------------------------------------------------------------
476 // wxListTextCtrl (internal)
477 //-----------------------------------------------------------------------------
479 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
482 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
485 void OnChar( wxKeyEvent
&event
);
486 void OnKeyUp( wxKeyEvent
&event
);
487 void OnKillFocus( wxFocusEvent
&event
);
489 bool AcceptChanges();
493 wxListMainWindow
*m_owner
;
494 wxString m_startValue
;
498 DECLARE_EVENT_TABLE()
501 //-----------------------------------------------------------------------------
502 // wxListMainWindow (internal)
503 //-----------------------------------------------------------------------------
505 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
506 #include "wx/listimpl.cpp"
507 WX_DEFINE_LIST(wxListHeaderDataList
);
509 class wxListMainWindow
: public wxScrolledWindow
513 wxListMainWindow( wxWindow
*parent
,
515 const wxPoint
& pos
= wxDefaultPosition
,
516 const wxSize
& size
= wxDefaultSize
,
518 const wxString
&name
= _T("listctrlmainwindow") );
520 virtual ~wxListMainWindow();
522 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
524 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
526 // return true if this is a virtual list control
527 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
529 // return true if the control is in report mode
530 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
532 // return true if we are in single selection mode, false if multi sel
533 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
535 // do we have a header window?
536 bool HasHeader() const
537 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
539 void HighlightAll( bool on
);
541 // all these functions only do something if the line is currently visible
543 // change the line "selected" state, return true if it really changed
544 bool HighlightLine( size_t line
, bool highlight
= true);
546 // as HighlightLine() but do it for the range of lines: this is incredibly
547 // more efficient for virtual list controls!
549 // NB: unlike HighlightLine() this one does refresh the lines on screen
550 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
552 // toggle the line state and refresh it
553 void ReverseHighlight( size_t line
)
554 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
556 // return true if the line is highlighted
557 bool IsHighlighted(size_t line
) const;
559 // refresh one or several lines at once
560 void RefreshLine( size_t line
);
561 void RefreshLines( size_t lineFrom
, size_t lineTo
);
563 // refresh all selected items
564 void RefreshSelected();
566 // refresh all lines below the given one: the difference with
567 // RefreshLines() is that the index here might not be a valid one (happens
568 // when the last line is deleted)
569 void RefreshAfter( size_t lineFrom
);
571 // the methods which are forwarded to wxListLineData itself in list/icon
572 // modes but are here because the lines don't store their positions in the
575 // get the bound rect for the entire line
576 wxRect
GetLineRect(size_t line
) const;
578 // get the bound rect of the label
579 wxRect
GetLineLabelRect(size_t line
) const;
581 // get the bound rect of the items icon (only may be called if we do have
583 wxRect
GetLineIconRect(size_t line
) const;
585 // get the rect to be highlighted when the item has focus
586 wxRect
GetLineHighlightRect(size_t line
) const;
588 // get the size of the total line rect
589 wxSize
GetLineSize(size_t line
) const
590 { return GetLineRect(line
).GetSize(); }
592 // return the hit code for the corresponding position (in this line)
593 long HitTestLine(size_t line
, int x
, int y
) const;
595 // bring the selected item into view, scrolling to it if necessary
596 void MoveToItem(size_t item
);
598 // bring the current item into view
599 void MoveToFocus() { MoveToItem(m_current
); }
601 // start editing the label of the given item
602 void EditLabel( long item
);
604 // suspend/resume redrawing the control
608 void OnRenameTimer();
609 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
610 void OnRenameCancelled(size_t itemEdit
);
612 void OnMouse( wxMouseEvent
&event
);
614 // called to switch the selection from the current item to newCurrent,
615 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
617 void OnChar( wxKeyEvent
&event
);
618 void OnKeyDown( wxKeyEvent
&event
);
619 void OnSetFocus( wxFocusEvent
&event
);
620 void OnKillFocus( wxFocusEvent
&event
);
621 void OnScroll(wxScrollWinEvent
& event
) ;
623 void OnPaint( wxPaintEvent
&event
);
625 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
626 void GetImageSize( int index
, int &width
, int &height
) const;
627 int GetTextLength( const wxString
&s
) const;
629 void SetImageList( wxImageListType
*imageList
, int which
);
630 void SetItemSpacing( int spacing
, bool isSmall
= false );
631 int GetItemSpacing( bool isSmall
= false );
633 void SetColumn( int col
, wxListItem
&item
);
634 void SetColumnWidth( int col
, int width
);
635 void GetColumn( int col
, wxListItem
&item
) const;
636 int GetColumnWidth( int col
) const;
637 int GetColumnCount() const { return m_columns
.GetCount(); }
639 // returns the sum of the heights of all columns
640 int GetHeaderWidth() const;
642 int GetCountPerPage() const;
644 void SetItem( wxListItem
&item
);
645 void GetItem( wxListItem
&item
) const;
646 void SetItemState( long item
, long state
, long stateMask
);
647 int GetItemState( long item
, long stateMask
) const;
648 void GetItemRect( long index
, wxRect
&rect
) const;
649 wxRect
GetViewRect() const;
650 bool GetItemPosition( long item
, wxPoint
& pos
) const;
651 int GetSelectedItemCount() const;
653 wxString
GetItemText(long item
) const
656 info
.m_itemId
= item
;
661 void SetItemText(long item
, const wxString
& value
)
664 info
.m_mask
= wxLIST_MASK_TEXT
;
665 info
.m_itemId
= item
;
670 // set the scrollbars and update the positions of the items
671 void RecalculatePositions(bool noRefresh
= false);
673 // refresh the window and the header
676 long GetNextItem( long item
, int geometry
, int state
) const;
677 void DeleteItem( long index
);
678 void DeleteAllItems();
679 void DeleteColumn( int col
);
680 void DeleteEverything();
681 void EnsureVisible( long index
);
682 long FindItem( long start
, const wxString
& str
, bool partial
= false );
683 long FindItem( long start
, wxUIntPtr data
);
684 long FindItem( const wxPoint
& pt
);
685 long HitTest( int x
, int y
, int &flags
);
686 void InsertItem( wxListItem
&item
);
687 void InsertColumn( long col
, wxListItem
&item
);
688 void SortItems( wxListCtrlCompare fn
, long data
);
690 size_t GetItemCount() const;
691 bool IsEmpty() const { return GetItemCount() == 0; }
692 void SetItemCount(long count
);
694 // change the current (== focused) item, send a notification event
695 void ChangeCurrent(size_t current
);
696 void ResetCurrent() { ChangeCurrent((size_t)-1); }
697 bool HasCurrent() const { return m_current
!= (size_t)-1; }
699 // send out a wxListEvent
700 void SendNotify( size_t line
,
702 wxPoint point
= wxDefaultPosition
);
704 // override base class virtual to reset m_lineHeight when the font changes
705 virtual bool SetFont(const wxFont
& font
)
707 if ( !wxScrolledWindow::SetFont(font
) )
715 // these are for wxListLineData usage only
717 // get the backpointer to the list ctrl
718 wxGenericListCtrl
*GetListCtrl() const
720 return wxStaticCast(GetParent(), wxGenericListCtrl
);
723 // get the height of all lines (assuming they all do have the same height)
724 wxCoord
GetLineHeight() const;
726 // get the y position of the given line (only for report view)
727 wxCoord
GetLineY(size_t line
) const;
729 // get the brush to use for the item highlighting
730 wxBrush
*GetHighlightBrush() const
732 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
736 // the array of all line objects for a non virtual list control (for the
737 // virtual list control we only ever use m_lines[0])
738 wxListLineDataArray m_lines
;
740 // the list of column objects
741 wxListHeaderDataList m_columns
;
743 // currently focused item or -1
746 // the number of lines per page
749 // this flag is set when something which should result in the window
750 // redrawing happens (i.e. an item was added or deleted, or its appearance
751 // changed) and OnPaint() doesn't redraw the window while it is set which
752 // allows to minimize the number of repaintings when a lot of items are
753 // being added. The real repainting occurs only after the next OnIdle()
757 wxColour
*m_highlightColour
;
758 wxImageListType
*m_small_image_list
;
759 wxImageListType
*m_normal_image_list
;
761 int m_normal_spacing
;
765 wxTimer
*m_renameTimer
;
770 // for double click logic
771 size_t m_lineLastClicked
,
772 m_lineBeforeLastClicked
;
775 // the total count of items in a virtual list control
778 // the object maintaining the items selection state, only used in virtual
780 wxSelectionStore m_selStore
;
782 // common part of all ctors
785 // get the line data for the given index
786 wxListLineData
*GetLine(size_t n
) const
788 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
792 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
800 // get a dummy line which can be used for geometry calculations and such:
801 // you must use GetLine() if you want to really draw the line
802 wxListLineData
*GetDummyLine() const;
804 // cache the line data of the n-th line in m_lines[0]
805 void CacheLineData(size_t line
);
807 // get the range of visible lines
808 void GetVisibleLinesRange(size_t *from
, size_t *to
);
810 // force us to recalculate the range of visible lines
811 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
813 // get the colour to be used for drawing the rules
814 wxColour
GetRuleColour() const
819 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
824 // initialize the current item if needed
825 void UpdateCurrent();
827 // delete all items but don't refresh: called from dtor
828 void DoDeleteAllItems();
830 // the height of one line using the current font
831 wxCoord m_lineHeight
;
833 // the total header width or 0 if not calculated yet
834 wxCoord m_headerWidth
;
836 // the first and last lines being shown on screen right now (inclusive),
837 // both may be -1 if they must be calculated so never access them directly:
838 // use GetVisibleLinesRange() above instead
842 // the brushes to use for item highlighting when we do/don't have focus
843 wxBrush
*m_highlightBrush
,
844 *m_highlightUnfocusedBrush
;
846 // if this is > 0, the control is frozen and doesn't redraw itself
847 size_t m_freezeCount
;
849 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
850 DECLARE_EVENT_TABLE()
852 friend class wxGenericListCtrl
;
855 // ============================================================================
857 // ============================================================================
859 //-----------------------------------------------------------------------------
861 //-----------------------------------------------------------------------------
863 wxListItemData::~wxListItemData()
865 // in the virtual list control the attributes are managed by the main
866 // program, so don't delete them
867 if ( !m_owner
->IsVirtual() )
875 void wxListItemData::Init()
883 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
889 if ( owner
->InReportView() )
899 void wxListItemData::SetItem( const wxListItem
&info
)
901 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
902 SetText(info
.m_text
);
903 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
904 m_image
= info
.m_image
;
905 if ( info
.m_mask
& wxLIST_MASK_DATA
)
906 m_data
= info
.m_data
;
908 if ( info
.HasAttributes() )
911 *m_attr
= *info
.GetAttributes();
913 m_attr
= new wxListItemAttr(*info
.GetAttributes());
921 m_rect
->width
= info
.m_width
;
925 void wxListItemData::SetPosition( int x
, int y
)
927 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
933 void wxListItemData::SetSize( int width
, int height
)
935 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
938 m_rect
->width
= width
;
940 m_rect
->height
= height
;
943 bool wxListItemData::IsHit( int x
, int y
) const
945 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
947 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
950 int wxListItemData::GetX() const
952 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
957 int wxListItemData::GetY() const
959 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
964 int wxListItemData::GetWidth() const
966 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
968 return m_rect
->width
;
971 int wxListItemData::GetHeight() const
973 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
975 return m_rect
->height
;
978 void wxListItemData::GetItem( wxListItem
&info
) const
980 info
.m_text
= m_text
;
981 info
.m_image
= m_image
;
982 info
.m_data
= m_data
;
986 if ( m_attr
->HasTextColour() )
987 info
.SetTextColour(m_attr
->GetTextColour());
988 if ( m_attr
->HasBackgroundColour() )
989 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
990 if ( m_attr
->HasFont() )
991 info
.SetFont(m_attr
->GetFont());
995 //-----------------------------------------------------------------------------
997 //-----------------------------------------------------------------------------
999 void wxListHeaderData::Init()
1010 wxListHeaderData::wxListHeaderData()
1015 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1022 void wxListHeaderData::SetItem( const wxListItem
&item
)
1024 m_mask
= item
.m_mask
;
1026 if ( m_mask
& wxLIST_MASK_TEXT
)
1027 m_text
= item
.m_text
;
1029 if ( m_mask
& wxLIST_MASK_IMAGE
)
1030 m_image
= item
.m_image
;
1032 if ( m_mask
& wxLIST_MASK_FORMAT
)
1033 m_format
= item
.m_format
;
1035 if ( m_mask
& wxLIST_MASK_WIDTH
)
1036 SetWidth(item
.m_width
);
1039 void wxListHeaderData::SetPosition( int x
, int y
)
1045 void wxListHeaderData::SetHeight( int h
)
1050 void wxListHeaderData::SetWidth( int w
)
1054 m_width
= WIDTH_COL_DEFAULT
;
1055 else if (m_width
< WIDTH_COL_MIN
)
1056 m_width
= WIDTH_COL_MIN
;
1059 void wxListHeaderData::SetFormat( int format
)
1064 bool wxListHeaderData::HasImage() const
1066 return m_image
!= -1;
1069 bool wxListHeaderData::IsHit( int x
, int y
) const
1071 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1074 void wxListHeaderData::GetItem( wxListItem
& item
)
1076 item
.m_mask
= m_mask
;
1077 item
.m_text
= m_text
;
1078 item
.m_image
= m_image
;
1079 item
.m_format
= m_format
;
1080 item
.m_width
= m_width
;
1083 int wxListHeaderData::GetImage() const
1088 int wxListHeaderData::GetWidth() const
1093 int wxListHeaderData::GetFormat() const
1098 //-----------------------------------------------------------------------------
1100 //-----------------------------------------------------------------------------
1102 inline int wxListLineData::GetMode() const
1104 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1107 inline bool wxListLineData::InReportView() const
1109 return m_owner
->HasFlag(wxLC_REPORT
);
1112 inline bool wxListLineData::IsVirtual() const
1114 return m_owner
->IsVirtual();
1117 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1121 if ( InReportView() )
1127 m_gi
= new GeometryInfo
;
1130 m_highlighted
= false;
1132 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1135 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1137 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1138 wxCHECK_RET( node
, _T("no subitems at all??") );
1140 wxListItemData
*item
= node
->GetData();
1145 switch ( GetMode() )
1148 case wxLC_SMALL_ICON
:
1149 m_gi
->m_rectAll
.width
= spacing
;
1151 s
= item
->GetText();
1156 m_gi
->m_rectLabel
.width
=
1157 m_gi
->m_rectLabel
.height
= 0;
1161 dc
->GetTextExtent( s
, &lw
, &lh
);
1165 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1167 m_gi
->m_rectAll
.width
= lw
;
1169 m_gi
->m_rectLabel
.width
= lw
;
1170 m_gi
->m_rectLabel
.height
= lh
;
1173 if (item
->HasImage())
1176 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1177 m_gi
->m_rectIcon
.width
= w
+ 8;
1178 m_gi
->m_rectIcon
.height
= h
+ 8;
1180 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1181 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1182 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1183 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1186 if ( item
->HasText() )
1188 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1189 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1191 else // no text, highlight the icon
1193 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1194 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1199 s
= item
->GetTextForMeasuring();
1201 dc
->GetTextExtent( s
, &lw
, &lh
);
1205 m_gi
->m_rectLabel
.width
= lw
;
1206 m_gi
->m_rectLabel
.height
= lh
;
1208 m_gi
->m_rectAll
.width
= lw
;
1209 m_gi
->m_rectAll
.height
= lh
;
1211 if (item
->HasImage())
1214 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1215 m_gi
->m_rectIcon
.width
= w
;
1216 m_gi
->m_rectIcon
.height
= h
;
1218 m_gi
->m_rectAll
.width
+= 4 + w
;
1219 if (h
> m_gi
->m_rectAll
.height
)
1220 m_gi
->m_rectAll
.height
= h
;
1223 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1224 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1228 wxFAIL_MSG( _T("unexpected call to SetSize") );
1232 wxFAIL_MSG( _T("unknown mode") );
1236 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1238 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1239 wxCHECK_RET( node
, _T("no subitems at all??") );
1241 wxListItemData
*item
= node
->GetData();
1243 switch ( GetMode() )
1246 case wxLC_SMALL_ICON
:
1247 m_gi
->m_rectAll
.x
= x
;
1248 m_gi
->m_rectAll
.y
= y
;
1250 if ( item
->HasImage() )
1252 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1253 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1254 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1257 if ( item
->HasText() )
1259 if (m_gi
->m_rectAll
.width
> spacing
)
1260 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1262 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1263 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1264 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1265 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1267 else // no text, highlight the icon
1269 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1270 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1275 m_gi
->m_rectAll
.x
= x
;
1276 m_gi
->m_rectAll
.y
= y
;
1278 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1279 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1280 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1282 if (item
->HasImage())
1284 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1285 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1286 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1290 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1295 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1299 wxFAIL_MSG( _T("unknown mode") );
1303 void wxListLineData::InitItems( int num
)
1305 for (int i
= 0; i
< num
; i
++)
1306 m_items
.Append( new wxListItemData(m_owner
) );
1309 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1311 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1312 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1314 wxListItemData
*item
= node
->GetData();
1315 item
->SetItem( info
);
1318 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1320 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1323 wxListItemData
*item
= node
->GetData();
1324 item
->GetItem( info
);
1328 wxString
wxListLineData::GetText(int index
) const
1332 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1335 wxListItemData
*item
= node
->GetData();
1336 s
= item
->GetText();
1342 void wxListLineData::SetText( int index
, const wxString s
)
1344 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1347 wxListItemData
*item
= node
->GetData();
1352 void wxListLineData::SetImage( int index
, int image
)
1354 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1355 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1357 wxListItemData
*item
= node
->GetData();
1358 item
->SetImage(image
);
1361 int wxListLineData::GetImage( int index
) const
1363 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1364 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1366 wxListItemData
*item
= node
->GetData();
1367 return item
->GetImage();
1370 wxListItemAttr
*wxListLineData::GetAttr() const
1372 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1373 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1375 wxListItemData
*item
= node
->GetData();
1376 return item
->GetAttr();
1379 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1381 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1382 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1384 wxListItemData
*item
= node
->GetData();
1385 item
->SetAttr(attr
);
1388 bool wxListLineData::SetAttributes(wxDC
*dc
,
1389 const wxListItemAttr
*attr
,
1392 wxWindow
*listctrl
= m_owner
->GetParent();
1396 // don't use foreground colour for drawing highlighted items - this might
1397 // make them completely invisible (and there is no way to do bit
1398 // arithmetics on wxColour, unfortunately)
1402 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1406 if ( attr
&& attr
->HasTextColour() )
1408 colText
= attr
->GetTextColour();
1412 colText
= listctrl
->GetForegroundColour();
1416 dc
->SetTextForeground(colText
);
1420 if ( attr
&& attr
->HasFont() )
1422 font
= attr
->GetFont();
1426 font
= listctrl
->GetFont();
1432 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1433 if ( highlighted
|| hasBgCol
)
1437 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1441 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1444 dc
->SetPen( *wxTRANSPARENT_PEN
);
1452 void wxListLineData::Draw( wxDC
*dc
)
1454 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1455 wxCHECK_RET( node
, _T("no subitems at all??") );
1457 bool highlighted
= IsHighlighted();
1459 wxListItemAttr
*attr
= GetAttr();
1461 if ( SetAttributes(dc
, attr
, highlighted
) )
1463 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1466 // just for debugging to better see where the items are
1468 dc
->SetPen(*wxRED_PEN
);
1469 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1470 dc
->DrawRectangle( m_gi
->m_rectAll
);
1471 dc
->SetPen(*wxGREEN_PEN
);
1472 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1475 wxListItemData
*item
= node
->GetData();
1476 if (item
->HasImage())
1478 // centre the image inside our rectangle, this looks nicer when items
1479 // ae aligned in a row
1480 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1482 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1485 if (item
->HasText())
1487 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1489 wxDCClipper
clipper(*dc
, rectLabel
);
1490 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1494 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1496 const wxRect
& rectHL
,
1499 // TODO: later we should support setting different attributes for
1500 // different columns - to do it, just add "col" argument to
1501 // GetAttr() and move these lines into the loop below
1502 wxListItemAttr
*attr
= GetAttr();
1503 if ( SetAttributes(dc
, attr
, highlighted
) )
1505 dc
->DrawRectangle( rectHL
);
1508 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1509 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1512 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1514 node
= node
->GetNext(), col
++ )
1516 wxListItemData
*item
= node
->GetData();
1518 int width
= m_owner
->GetColumnWidth(col
);
1522 if ( item
->HasImage() )
1525 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1526 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1528 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1534 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1536 if ( item
->HasText() )
1538 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1543 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1544 const wxString
&text
,
1550 wxString drawntext
, ellipsis
;
1551 wxCoord w
, h
, base_w
;
1554 // determine if the string can fit inside the current width
1555 dc
->GetTextExtent(text
, &w
, &h
);
1558 // it can, draw it using the items alignment
1559 m_owner
->GetColumn(col
, item
);
1560 switch ( item
.GetAlign() )
1563 wxFAIL_MSG( _T("unknown list item format") );
1566 case wxLIST_FORMAT_LEFT
:
1570 case wxLIST_FORMAT_RIGHT
:
1574 case wxLIST_FORMAT_CENTER
:
1575 x
+= (width
- w
) / 2;
1579 dc
->DrawText(text
, x
, y
);
1581 else // otherwise, truncate and add an ellipsis if possible
1583 // determine the base width
1584 ellipsis
= wxString(wxT("..."));
1585 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1587 // continue until we have enough space or only one character left
1589 size_t len
= text
.Length();
1590 drawntext
= text
.Left(len
);
1593 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1594 drawntext
.RemoveLast();
1597 if (w
+ base_w
<= width
)
1601 // if still not enough space, remove ellipsis characters
1602 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1604 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1605 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1608 // now draw the text
1609 dc
->DrawText(drawntext
, x
, y
);
1610 dc
->DrawText(ellipsis
, x
+ w
, y
);
1614 bool wxListLineData::Highlight( bool on
)
1616 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1618 if ( on
== m_highlighted
)
1626 void wxListLineData::ReverseHighlight( void )
1628 Highlight(!IsHighlighted());
1631 //-----------------------------------------------------------------------------
1632 // wxListHeaderWindow
1633 //-----------------------------------------------------------------------------
1635 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1637 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1638 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1639 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1640 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1643 void wxListHeaderWindow::Init()
1645 m_currentCursor
= (wxCursor
*) NULL
;
1646 m_isDragging
= false;
1650 wxListHeaderWindow::wxListHeaderWindow()
1654 m_owner
= (wxListMainWindow
*) NULL
;
1655 m_resizeCursor
= (wxCursor
*) NULL
;
1658 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1660 wxListMainWindow
*owner
,
1664 const wxString
&name
)
1665 : wxWindow( win
, id
, pos
, size
, style
, name
)
1670 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1673 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1674 SetOwnForegroundColour( attr
.colFg
);
1675 SetOwnBackgroundColour( attr
.colBg
);
1677 SetOwnFont( attr
.font
);
1679 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1680 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1682 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1686 wxListHeaderWindow::~wxListHeaderWindow()
1688 delete m_resizeCursor
;
1691 #ifdef __WXUNIVERSAL__
1692 #include "wx/univ/renderer.h"
1693 #include "wx/univ/theme.h"
1696 // shift the DC origin to match the position of the main window horz
1697 // scrollbar: this allows us to always use logical coords
1698 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1701 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1704 m_owner
->GetViewStart( &x
, NULL
);
1706 // account for the horz scrollbar offset
1707 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1710 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1712 wxPaintDC
dc( this );
1719 dc
.SetFont( GetFont() );
1721 // width and height of the entire header window
1723 GetClientSize( &w
, &h
);
1724 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1726 dc
.SetBackgroundMode(wxTRANSPARENT
);
1728 dc
.SetTextForeground(GetForegroundColour());
1730 int x
= HEADER_OFFSET_X
;
1732 int numColumns
= m_owner
->GetColumnCount();
1734 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1736 m_owner
->GetColumn( i
, item
);
1737 int wCol
= item
.m_width
;
1739 // the width of the rect to draw: make it smaller to fit entirely
1740 // inside the column rect
1743 wxRendererNative::Get().DrawHeaderButton
1747 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1748 m_parent
->IsEnabled() ? 0
1749 : (int)wxCONTROL_DISABLED
1752 // see if we have enough space for the column label
1754 // for this we need the width of the text
1757 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1758 wLabel
+= 2*EXTRA_WIDTH
;
1760 // and the width of the icon, if any
1761 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1762 int ix
= 0, // init them just to suppress the compiler warnings
1764 const int image
= item
.m_image
;
1765 wxImageListType
*imageList
;
1768 imageList
= m_owner
->m_small_image_list
;
1771 imageList
->GetSize(image
, ix
, iy
);
1772 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1780 // ignore alignment if there is not enough space anyhow
1782 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1785 wxFAIL_MSG( _T("unknown list item format") );
1788 case wxLIST_FORMAT_LEFT
:
1792 case wxLIST_FORMAT_RIGHT
:
1793 xAligned
= x
+ cw
- wLabel
;
1796 case wxLIST_FORMAT_CENTER
:
1797 xAligned
= x
+ (cw
- wLabel
) / 2;
1802 // if we have an image, draw it on the right of the label
1809 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1810 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1811 wxIMAGELIST_DRAW_TRANSPARENT
1814 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1817 // draw the text clipping it so that it doesn't overwrite the column
1819 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1821 dc
.DrawText( item
.GetText(),
1822 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1830 void wxListHeaderWindow::DrawCurrent()
1832 int x1
= m_currentX
;
1834 m_owner
->ClientToScreen( &x1
, &y1
);
1836 int x2
= m_currentX
;
1838 m_owner
->GetClientSize( NULL
, &y2
);
1839 m_owner
->ClientToScreen( &x2
, &y2
);
1842 dc
.SetLogicalFunction( wxINVERT
);
1843 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1844 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1848 dc
.DrawLine( x1
, y1
, x2
, y2
);
1850 dc
.SetLogicalFunction( wxCOPY
);
1852 dc
.SetPen( wxNullPen
);
1853 dc
.SetBrush( wxNullBrush
);
1856 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1858 // we want to work with logical coords
1860 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1861 int y
= event
.GetY();
1865 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1867 // we don't draw the line beyond our window, but we allow dragging it
1870 GetClientSize( &w
, NULL
);
1871 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1874 // erase the line if it was drawn
1875 if ( m_currentX
< w
)
1878 if (event
.ButtonUp())
1881 m_isDragging
= false;
1883 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1884 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1891 m_currentX
= m_minX
+ 7;
1893 // draw in the new location
1894 if ( m_currentX
< w
)
1898 else // not dragging
1901 bool hit_border
= false;
1903 // end of the current column
1906 // find the column where this event occured
1908 countCol
= m_owner
->GetColumnCount();
1909 for (col
= 0; col
< countCol
; col
++)
1911 xpos
+= m_owner
->GetColumnWidth( col
);
1914 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1916 // near the column border
1923 // inside the column
1930 if ( col
== countCol
)
1933 if (event
.LeftDown() || event
.RightUp())
1935 if (hit_border
&& event
.LeftDown())
1937 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1938 event
.GetPosition()) )
1940 m_isDragging
= true;
1945 //else: column resizing was vetoed by the user code
1947 else // click on a column
1949 SendListEvent( event
.LeftDown()
1950 ? wxEVT_COMMAND_LIST_COL_CLICK
1951 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1952 event
.GetPosition());
1955 else if (event
.Moving())
1960 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1961 m_currentCursor
= m_resizeCursor
;
1965 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1966 m_currentCursor
= wxSTANDARD_CURSOR
;
1970 SetCursor(*m_currentCursor
);
1975 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1977 m_owner
->SetFocus();
1981 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1983 wxWindow
*parent
= GetParent();
1984 wxListEvent
le( type
, parent
->GetId() );
1985 le
.SetEventObject( parent
);
1986 le
.m_pointDrag
= pos
;
1988 // the position should be relative to the parent window, not
1989 // this one for compatibility with MSW and common sense: the
1990 // user code doesn't know anything at all about this header
1991 // window, so why should it get positions relative to it?
1992 le
.m_pointDrag
.y
-= GetSize().y
;
1994 le
.m_col
= m_column
;
1995 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1998 //-----------------------------------------------------------------------------
1999 // wxListRenameTimer (internal)
2000 //-----------------------------------------------------------------------------
2002 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2007 void wxListRenameTimer::Notify()
2009 m_owner
->OnRenameTimer();
2012 //-----------------------------------------------------------------------------
2013 // wxListTextCtrl (internal)
2014 //-----------------------------------------------------------------------------
2016 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2017 EVT_CHAR (wxListTextCtrl::OnChar
)
2018 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2019 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2022 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2023 : m_startValue(owner
->GetItemText(itemEdit
)),
2024 m_itemEdited(itemEdit
)
2029 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2031 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2032 &rectLabel
.x
, &rectLabel
.y
);
2034 (void)Create(owner
, wxID_ANY
, m_startValue
,
2035 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2036 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2039 void wxListTextCtrl::Finish()
2043 wxPendingDelete
.Append(this);
2047 m_owner
->SetFocusIgnoringChildren();
2051 bool wxListTextCtrl::AcceptChanges()
2053 const wxString value
= GetValue();
2055 if ( value
== m_startValue
)
2057 // nothing changed, always accept
2061 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2063 // vetoed by the user
2067 // accepted, do rename the item
2068 m_owner
->SetItemText(m_itemEdited
, value
);
2073 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2075 switch ( event
.m_keyCode
)
2078 if ( AcceptChanges() )
2080 // Close the text control, changes were accepted
2083 // else do nothing, do not accept and do not close
2089 m_owner
->OnRenameCancelled( m_itemEdited
);
2097 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2105 // auto-grow the textctrl:
2106 wxSize parentSize
= m_owner
->GetSize();
2107 wxPoint myPos
= GetPosition();
2108 wxSize mySize
= GetSize();
2110 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2111 if (myPos
.x
+ sx
> parentSize
.x
)
2112 sx
= parentSize
.x
- myPos
.x
;
2115 SetSize(sx
, wxDefaultCoord
);
2120 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2124 // We must finish regardless of success, otherwise we'll get
2128 if ( !AcceptChanges() )
2129 m_owner
->OnRenameCancelled( m_itemEdited
);
2132 // We must let the native text control handle focus, too, otherwise
2133 // it could have problems with the cursor (e.g., in wxGTK):
2137 //-----------------------------------------------------------------------------
2139 //-----------------------------------------------------------------------------
2141 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2143 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2144 EVT_PAINT (wxListMainWindow::OnPaint
)
2145 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2146 EVT_CHAR (wxListMainWindow::OnChar
)
2147 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2148 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2149 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2150 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2153 void wxListMainWindow::Init()
2158 m_lineTo
= (size_t)-1;
2164 m_small_image_list
= (wxImageListType
*) NULL
;
2165 m_normal_image_list
= (wxImageListType
*) NULL
;
2167 m_small_spacing
= 30;
2168 m_normal_spacing
= 40;
2172 m_isCreated
= false;
2174 m_lastOnSame
= false;
2175 m_renameTimer
= new wxListRenameTimer( this );
2179 m_lineBeforeLastClicked
= (size_t)-1;
2184 wxListMainWindow::wxListMainWindow()
2189 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2192 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2197 const wxString
&name
)
2198 : wxScrolledWindow( parent
, id
, pos
, size
,
2199 style
| wxHSCROLL
| wxVSCROLL
, name
)
2203 m_highlightBrush
= new wxBrush
2205 wxSystemSettings::GetColour
2207 wxSYS_COLOUR_HIGHLIGHT
2212 m_highlightUnfocusedBrush
= new wxBrush
2214 wxSystemSettings::GetColour
2216 wxSYS_COLOUR_BTNSHADOW
2224 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2226 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2227 SetOwnForegroundColour( attr
.colFg
);
2228 SetOwnBackgroundColour( attr
.colBg
);
2230 SetOwnFont( attr
.font
);
2233 wxListMainWindow::~wxListMainWindow()
2236 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2238 delete m_highlightBrush
;
2239 delete m_highlightUnfocusedBrush
;
2241 delete m_renameTimer
;
2244 void wxListMainWindow::CacheLineData(size_t line
)
2246 wxGenericListCtrl
*listctrl
= GetListCtrl();
2248 wxListLineData
*ld
= GetDummyLine();
2250 size_t countCol
= GetColumnCount();
2251 for ( size_t col
= 0; col
< countCol
; col
++ )
2253 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2256 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2257 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2260 wxListLineData
*wxListMainWindow::GetDummyLine() const
2262 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2264 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2266 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2268 // we need to recreate the dummy line if the number of columns in the
2269 // control changed as it would have the incorrect number of fields
2271 if ( !m_lines
.IsEmpty() &&
2272 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2274 self
->m_lines
.Clear();
2277 if ( m_lines
.IsEmpty() )
2279 wxListLineData
*line
= new wxListLineData(self
);
2280 self
->m_lines
.Add(line
);
2282 // don't waste extra memory -- there never going to be anything
2283 // else/more in this array
2284 self
->m_lines
.Shrink();
2290 // ----------------------------------------------------------------------------
2291 // line geometry (report mode only)
2292 // ----------------------------------------------------------------------------
2294 wxCoord
wxListMainWindow::GetLineHeight() const
2296 // we cache the line height as calling GetTextExtent() is slow
2297 if ( !m_lineHeight
)
2299 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2301 wxClientDC
dc( self
);
2302 dc
.SetFont( GetFont() );
2305 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2307 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2311 m_small_image_list
->GetSize(0, iw
, ih
);
2316 self
->m_lineHeight
= y
+ LINE_SPACING
;
2319 return m_lineHeight
;
2322 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2324 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2326 return LINE_SPACING
+ line
*GetLineHeight();
2329 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2331 if ( !InReportView() )
2332 return GetLine(line
)->m_gi
->m_rectAll
;
2335 rect
.x
= HEADER_OFFSET_X
;
2336 rect
.y
= GetLineY(line
);
2337 rect
.width
= GetHeaderWidth();
2338 rect
.height
= GetLineHeight();
2343 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2345 if ( !InReportView() )
2346 return GetLine(line
)->m_gi
->m_rectLabel
;
2349 rect
.x
= HEADER_OFFSET_X
;
2350 rect
.y
= GetLineY(line
);
2351 rect
.width
= GetColumnWidth(0);
2352 rect
.height
= GetLineHeight();
2357 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2359 if ( !InReportView() )
2360 return GetLine(line
)->m_gi
->m_rectIcon
;
2362 wxListLineData
*ld
= GetLine(line
);
2363 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2366 rect
.x
= HEADER_OFFSET_X
;
2367 rect
.y
= GetLineY(line
);
2368 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2373 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2375 return InReportView() ? GetLineRect(line
)
2376 : GetLine(line
)->m_gi
->m_rectHighlight
;
2379 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2381 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2383 wxListLineData
*ld
= GetLine(line
);
2385 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2386 return wxLIST_HITTEST_ONITEMICON
;
2388 // VS: Testing for "ld->HasText() || InReportView()" instead of
2389 // "ld->HasText()" is needed to make empty lines in report view
2391 if ( ld
->HasText() || InReportView() )
2393 wxRect rect
= InReportView() ? GetLineRect(line
)
2394 : GetLineLabelRect(line
);
2396 if ( rect
.Inside(x
, y
) )
2397 return wxLIST_HITTEST_ONITEMLABEL
;
2403 // ----------------------------------------------------------------------------
2404 // highlight (selection) handling
2405 // ----------------------------------------------------------------------------
2407 bool wxListMainWindow::IsHighlighted(size_t line
) const
2411 return m_selStore
.IsSelected(line
);
2415 wxListLineData
*ld
= GetLine(line
);
2416 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2418 return ld
->IsHighlighted();
2422 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2428 wxArrayInt linesChanged
;
2429 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2432 // meny items changed state, refresh everything
2433 RefreshLines(lineFrom
, lineTo
);
2435 else // only a few items changed state, refresh only them
2437 size_t count
= linesChanged
.GetCount();
2438 for ( size_t n
= 0; n
< count
; n
++ )
2440 RefreshLine(linesChanged
[n
]);
2444 else // iterate over all items in non report view
2446 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2448 if ( HighlightLine(line
, highlight
) )
2456 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2462 changed
= m_selStore
.SelectItem(line
, highlight
);
2466 wxListLineData
*ld
= GetLine(line
);
2467 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2469 changed
= ld
->Highlight(highlight
);
2474 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2475 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2481 void wxListMainWindow::RefreshLine( size_t line
)
2483 if ( InReportView() )
2485 size_t visibleFrom
, visibleTo
;
2486 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2488 if ( line
< visibleFrom
|| line
> visibleTo
)
2492 wxRect rect
= GetLineRect(line
);
2494 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2495 RefreshRect( rect
);
2498 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2500 // we suppose that they are ordered by caller
2501 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2503 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2505 if ( InReportView() )
2507 size_t visibleFrom
, visibleTo
;
2508 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2510 if ( lineFrom
< visibleFrom
)
2511 lineFrom
= visibleFrom
;
2512 if ( lineTo
> visibleTo
)
2517 rect
.y
= GetLineY(lineFrom
);
2518 rect
.width
= GetClientSize().x
;
2519 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2521 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2522 RefreshRect( rect
);
2526 // TODO: this should be optimized...
2527 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2534 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2536 if ( InReportView() )
2538 size_t visibleFrom
, visibleTo
;
2539 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2541 if ( lineFrom
< visibleFrom
)
2542 lineFrom
= visibleFrom
;
2543 else if ( lineFrom
> visibleTo
)
2548 rect
.y
= GetLineY(lineFrom
);
2549 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2551 wxSize size
= GetClientSize();
2552 rect
.width
= size
.x
;
2553 // refresh till the bottom of the window
2554 rect
.height
= size
.y
- rect
.y
;
2556 RefreshRect( rect
);
2560 // TODO: how to do it more efficiently?
2565 void wxListMainWindow::RefreshSelected()
2571 if ( InReportView() )
2573 GetVisibleLinesRange(&from
, &to
);
2578 to
= GetItemCount() - 1;
2581 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2583 RefreshLine(m_current
);
2586 for ( size_t line
= from
; line
<= to
; line
++ )
2588 // NB: the test works as expected even if m_current == -1
2589 if ( line
!= m_current
&& IsHighlighted(line
) )
2596 void wxListMainWindow::Freeze()
2601 void wxListMainWindow::Thaw()
2603 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2605 if ( !--m_freezeCount
)
2611 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2613 // Note: a wxPaintDC must be constructed even if no drawing is
2614 // done (a Windows requirement).
2615 wxPaintDC
dc( this );
2617 if ( IsEmpty() || m_freezeCount
)
2619 // nothing to draw or not the moment to draw it
2625 // delay the repainting until we calculate all the items positions
2632 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2636 dc
.SetFont( GetFont() );
2638 if ( InReportView() )
2640 int lineHeight
= GetLineHeight();
2642 size_t visibleFrom
, visibleTo
;
2643 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2646 wxCoord xOrig
, yOrig
;
2647 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2649 // tell the caller cache to cache the data
2652 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2653 GetParent()->GetId());
2654 evCache
.SetEventObject( GetParent() );
2655 evCache
.m_oldItemIndex
= visibleFrom
;
2656 evCache
.m_itemIndex
= visibleTo
;
2657 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2660 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2662 rectLine
= GetLineRect(line
);
2664 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2665 rectLine
.width
, rectLine
.height
) )
2667 // don't redraw unaffected lines to avoid flicker
2671 GetLine(line
)->DrawInReportMode( &dc
,
2673 GetLineHighlightRect(line
),
2674 IsHighlighted(line
) );
2677 if ( HasFlag(wxLC_HRULES
) )
2679 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2680 wxSize clientSize
= GetClientSize();
2682 // Don't draw the first one
2683 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2686 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2687 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2688 clientSize
.x
- dev_x
, i
*lineHeight
);
2691 // Draw last horizontal rule
2692 if ( visibleTo
== GetItemCount() - 1 )
2695 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2696 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2697 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2701 // Draw vertical rules if required
2702 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2704 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2706 wxRect firstItemRect
;
2707 wxRect lastItemRect
;
2708 GetItemRect(visibleFrom
, firstItemRect
);
2709 GetItemRect(visibleTo
, lastItemRect
);
2710 int x
= firstItemRect
.GetX();
2712 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2713 for (int col
= 0; col
< GetColumnCount(); col
++)
2715 int colWidth
= GetColumnWidth(col
);
2717 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2718 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2724 size_t count
= GetItemCount();
2725 for ( size_t i
= 0; i
< count
; i
++ )
2727 GetLine(i
)->Draw( &dc
);
2732 // Don't draw rect outline under Mac at all.
2737 dc
.SetPen( *wxBLACK_PEN
);
2738 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2739 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2747 void wxListMainWindow::HighlightAll( bool on
)
2749 if ( IsSingleSel() )
2751 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2753 // we just have one item to turn off
2754 if ( HasCurrent() && IsHighlighted(m_current
) )
2756 HighlightLine(m_current
, false);
2757 RefreshLine(m_current
);
2762 HighlightLines(0, GetItemCount() - 1, on
);
2766 void wxListMainWindow::SendNotify( size_t line
,
2767 wxEventType command
,
2770 wxListEvent
le( command
, GetParent()->GetId() );
2771 le
.SetEventObject( GetParent() );
2772 le
.m_itemIndex
= line
;
2774 // set only for events which have position
2775 if ( point
!= wxDefaultPosition
)
2776 le
.m_pointDrag
= point
;
2778 // don't try to get the line info for virtual list controls: the main
2779 // program has it anyhow and if we did it would result in accessing all
2780 // the lines, even those which are not visible now and this is precisely
2781 // what we're trying to avoid
2782 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2784 if ( line
!= (size_t)-1 )
2786 GetLine(line
)->GetItem( 0, le
.m_item
);
2788 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2790 //else: there may be no more such item
2792 GetParent()->GetEventHandler()->ProcessEvent( le
);
2795 void wxListMainWindow::ChangeCurrent(size_t current
)
2797 m_current
= current
;
2799 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2802 void wxListMainWindow::EditLabel( long item
)
2804 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2805 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2807 size_t itemEdit
= (size_t)item
;
2809 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2810 le
.SetEventObject( GetParent() );
2811 le
.m_itemIndex
= item
;
2812 wxListLineData
*data
= GetLine(itemEdit
);
2813 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2814 data
->GetItem( 0, le
.m_item
);
2815 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2817 // vetoed by user code
2821 // We have to call this here because the label in question might just have
2822 // been added and no screen update taken place.
2826 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2831 void wxListMainWindow::OnRenameTimer()
2833 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2835 EditLabel( m_current
);
2838 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2840 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2841 le
.SetEventObject( GetParent() );
2842 le
.m_itemIndex
= itemEdit
;
2844 wxListLineData
*data
= GetLine(itemEdit
);
2845 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2847 data
->GetItem( 0, le
.m_item
);
2848 le
.m_item
.m_text
= value
;
2849 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2853 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2855 // let owner know that the edit was cancelled
2856 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2858 le
.SetEditCanceled(true);
2860 le
.SetEventObject( GetParent() );
2861 le
.m_itemIndex
= itemEdit
;
2863 wxListLineData
*data
= GetLine(itemEdit
);
2864 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2866 data
->GetItem( 0, le
.m_item
);
2868 GetEventHandler()->ProcessEvent( le
);
2871 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2873 event
.SetEventObject( GetParent() );
2874 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2877 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2879 // let the base handle mouse wheel events.
2884 if ( !HasCurrent() || IsEmpty() )
2890 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2891 event
.ButtonDClick()) )
2894 int x
= event
.GetX();
2895 int y
= event
.GetY();
2896 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2898 // where did we hit it (if we did)?
2901 size_t count
= GetItemCount(),
2904 if ( InReportView() )
2906 current
= y
/ GetLineHeight();
2907 if ( current
< count
)
2908 hitResult
= HitTestLine(current
, x
, y
);
2912 // TODO: optimize it too! this is less simple than for report view but
2913 // enumerating all items is still not a way to do it!!
2914 for ( current
= 0; current
< count
; current
++ )
2916 hitResult
= HitTestLine(current
, x
, y
);
2922 if (event
.Dragging())
2924 if (m_dragCount
== 0)
2926 // we have to report the raw, physical coords as we want to be
2927 // able to call HitTest(event.m_pointDrag) from the user code to
2928 // get the item being dragged
2929 m_dragStart
= event
.GetPosition();
2934 if (m_dragCount
!= 3)
2937 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2938 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2940 wxListEvent
le( command
, GetParent()->GetId() );
2941 le
.SetEventObject( GetParent() );
2942 le
.m_itemIndex
= current
;
2943 le
.m_pointDrag
= m_dragStart
;
2944 GetParent()->GetEventHandler()->ProcessEvent( le
);
2955 // outside of any item
2959 bool forceClick
= false;
2960 if (event
.ButtonDClick())
2962 m_renameTimer
->Stop();
2963 m_lastOnSame
= false;
2965 if ( current
== m_lineLastClicked
)
2967 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2973 // the first click was on another item, so don't interpret this as
2974 // a double click, but as a simple click instead
2979 if (event
.LeftUp() && m_lastOnSame
)
2981 if ((current
== m_current
) &&
2982 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2983 HasFlag(wxLC_EDIT_LABELS
) )
2985 m_renameTimer
->Start( 100, true );
2987 m_lastOnSame
= false;
2989 else if (event
.RightDown())
2991 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2992 event
.GetPosition() );
2994 else if (event
.MiddleDown())
2996 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2998 else if ( event
.LeftDown() || forceClick
)
3000 m_lineBeforeLastClicked
= m_lineLastClicked
;
3001 m_lineLastClicked
= current
;
3003 size_t oldCurrent
= m_current
;
3004 bool cmdModifierDown
= event
.CmdDown();
3005 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3007 HighlightAll( false );
3009 ChangeCurrent(current
);
3011 ReverseHighlight(m_current
);
3013 else // multi sel & either ctrl or shift is down
3015 if (cmdModifierDown
)
3017 ChangeCurrent(current
);
3019 ReverseHighlight(m_current
);
3021 else if (event
.ShiftDown())
3023 ChangeCurrent(current
);
3025 size_t lineFrom
= oldCurrent
,
3028 if ( lineTo
< lineFrom
)
3031 lineFrom
= m_current
;
3034 HighlightLines(lineFrom
, lineTo
);
3036 else // !ctrl, !shift
3038 // test in the enclosing if should make it impossible
3039 wxFAIL_MSG( _T("how did we get here?") );
3043 if (m_current
!= oldCurrent
)
3045 RefreshLine( oldCurrent
);
3048 // forceClick is only set if the previous click was on another item
3049 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3053 void wxListMainWindow::MoveToItem(size_t item
)
3055 if ( item
== (size_t)-1 )
3058 wxRect rect
= GetLineRect(item
);
3060 int client_w
, client_h
;
3061 GetClientSize( &client_w
, &client_h
);
3063 const int hLine
= GetLineHeight();
3065 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3066 int view_y
= hLine
*GetScrollPos( wxVERTICAL
);
3068 if ( InReportView() )
3070 // the next we need the range of lines shown it might be different, so
3072 ResetVisibleLinesRange();
3074 if (rect
.y
< view_y
)
3075 Scroll( -1, rect
.y
/hLine
);
3076 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3077 Scroll( -1, (rect
.y
+rect
.height
-client_h
+hLine
)/hLine
);
3081 if (rect
.x
-view_x
< 5)
3082 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3083 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3084 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3088 // ----------------------------------------------------------------------------
3089 // keyboard handling
3090 // ----------------------------------------------------------------------------
3092 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3094 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3095 _T("invalid item index in OnArrowChar()") );
3097 size_t oldCurrent
= m_current
;
3099 // in single selection we just ignore Shift as we can't select several
3101 if ( event
.ShiftDown() && !IsSingleSel() )
3103 ChangeCurrent(newCurrent
);
3105 // refresh the old focus to remove it
3106 RefreshLine( oldCurrent
);
3108 // select all the items between the old and the new one
3109 if ( oldCurrent
> newCurrent
)
3111 newCurrent
= oldCurrent
;
3112 oldCurrent
= m_current
;
3115 HighlightLines(oldCurrent
, newCurrent
);
3119 // all previously selected items are unselected unless ctrl is held
3120 if ( !event
.ControlDown() )
3121 HighlightAll(false);
3123 ChangeCurrent(newCurrent
);
3125 // refresh the old focus to remove it
3126 RefreshLine( oldCurrent
);
3128 if ( !event
.ControlDown() )
3130 HighlightLine( m_current
, true );
3134 RefreshLine( m_current
);
3139 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3141 wxWindow
*parent
= GetParent();
3143 /* we propagate the key event up */
3144 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3145 ke
.m_shiftDown
= event
.m_shiftDown
;
3146 ke
.m_controlDown
= event
.m_controlDown
;
3147 ke
.m_altDown
= event
.m_altDown
;
3148 ke
.m_metaDown
= event
.m_metaDown
;
3149 ke
.m_keyCode
= event
.m_keyCode
;
3152 ke
.SetEventObject( parent
);
3153 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3158 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3160 wxWindow
*parent
= GetParent();
3162 /* we send a list_key event up */
3165 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3166 le
.m_itemIndex
= m_current
;
3167 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3168 le
.m_code
= event
.GetKeyCode();
3169 le
.SetEventObject( parent
);
3170 parent
->GetEventHandler()->ProcessEvent( le
);
3173 /* we propagate the char event up */
3174 wxKeyEvent
ke( wxEVT_CHAR
);
3175 ke
.m_shiftDown
= event
.m_shiftDown
;
3176 ke
.m_controlDown
= event
.m_controlDown
;
3177 ke
.m_altDown
= event
.m_altDown
;
3178 ke
.m_metaDown
= event
.m_metaDown
;
3179 ke
.m_keyCode
= event
.m_keyCode
;
3182 ke
.SetEventObject( parent
);
3183 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3185 if (event
.GetKeyCode() == WXK_TAB
)
3187 wxNavigationKeyEvent nevent
;
3188 nevent
.SetWindowChange( event
.ControlDown() );
3189 nevent
.SetDirection( !event
.ShiftDown() );
3190 nevent
.SetEventObject( GetParent()->GetParent() );
3191 nevent
.SetCurrentFocus( m_parent
);
3192 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3196 /* no item -> nothing to do */
3203 switch (event
.GetKeyCode())
3206 if ( m_current
> 0 )
3207 OnArrowChar( m_current
- 1, event
);
3211 if ( m_current
< (size_t)GetItemCount() - 1 )
3212 OnArrowChar( m_current
+ 1, event
);
3217 OnArrowChar( GetItemCount() - 1, event
);
3222 OnArrowChar( 0, event
);
3227 int steps
= InReportView() ? m_linesPerPage
- 1 : m_current
% m_linesPerPage
;
3229 int index
= m_current
- steps
;
3233 OnArrowChar( index
, event
);
3239 int steps
= InReportView()
3240 ? m_linesPerPage
- 1
3241 : m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3243 size_t index
= m_current
+ steps
;
3244 size_t count
= GetItemCount();
3245 if ( index
>= count
)
3248 OnArrowChar( index
, event
);
3253 if ( !InReportView() )
3255 int index
= m_current
- m_linesPerPage
;
3259 OnArrowChar( index
, event
);
3264 if ( !InReportView() )
3266 size_t index
= m_current
+ m_linesPerPage
;
3268 size_t count
= GetItemCount();
3269 if ( index
>= count
)
3272 OnArrowChar( index
, event
);
3277 if ( IsSingleSel() )
3279 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3281 if ( IsHighlighted(m_current
) )
3283 // don't unselect the item in single selection mode
3286 //else: select it in ReverseHighlight() below if unselected
3289 ReverseHighlight(m_current
);
3294 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3302 // ----------------------------------------------------------------------------
3304 // ----------------------------------------------------------------------------
3306 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3310 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3311 event
.SetEventObject( GetParent() );
3312 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3316 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3317 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3318 // which are already drawn correctly resulting in horrible flicker - avoid
3328 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3332 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3333 event
.SetEventObject( GetParent() );
3334 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3341 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3343 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3345 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3347 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3349 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3351 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3353 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3355 else if ( InReportView() && (m_small_image_list
))
3357 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3361 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3363 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3365 m_normal_image_list
->GetSize( index
, width
, height
);
3367 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3369 m_small_image_list
->GetSize( index
, width
, height
);
3371 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3373 m_small_image_list
->GetSize( index
, width
, height
);
3375 else if ( InReportView() && m_small_image_list
)
3377 m_small_image_list
->GetSize( index
, width
, height
);
3386 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3388 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3389 dc
.SetFont( GetFont() );
3392 dc
.GetTextExtent( s
, &lw
, NULL
);
3394 return lw
+ AUTOSIZE_COL_MARGIN
;
3397 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3401 // calc the spacing from the icon size
3404 if ((imageList
) && (imageList
->GetImageCount()) )
3406 imageList
->GetSize(0, width
, height
);
3409 if (which
== wxIMAGE_LIST_NORMAL
)
3411 m_normal_image_list
= imageList
;
3412 m_normal_spacing
= width
+ 8;
3415 if (which
== wxIMAGE_LIST_SMALL
)
3417 m_small_image_list
= imageList
;
3418 m_small_spacing
= width
+ 14;
3419 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3423 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3428 m_small_spacing
= spacing
;
3432 m_normal_spacing
= spacing
;
3436 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3438 return isSmall
? m_small_spacing
: m_normal_spacing
;
3441 // ----------------------------------------------------------------------------
3443 // ----------------------------------------------------------------------------
3445 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3447 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3449 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3451 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3452 item
.m_width
= GetTextLength( item
.m_text
);
3454 wxListHeaderData
*column
= node
->GetData();
3455 column
->SetItem( item
);
3457 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3459 headerWin
->m_dirty
= true;
3463 // invalidate it as it has to be recalculated
3467 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3469 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3470 _T("invalid column index") );
3472 wxCHECK_RET( InReportView(),
3473 _T("SetColumnWidth() can only be called in report mode.") );
3476 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3478 headerWin
->m_dirty
= true;
3480 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3481 wxCHECK_RET( node
, _T("no column?") );
3483 wxListHeaderData
*column
= node
->GetData();
3485 size_t count
= GetItemCount();
3487 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3489 width
= GetTextLength(column
->GetText());
3491 else if ( width
== wxLIST_AUTOSIZE
)
3495 // TODO: determine the max width somehow...
3496 width
= WIDTH_COL_DEFAULT
;
3500 wxClientDC
dc(this);
3501 dc
.SetFont( GetFont() );
3503 int max
= AUTOSIZE_COL_MARGIN
;
3505 for ( size_t i
= 0; i
< count
; i
++ )
3507 wxListLineData
*line
= GetLine(i
);
3508 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3510 wxCHECK_RET( n
, _T("no subitem?") );
3512 wxListItemData
*item
= n
->GetData();
3515 if (item
->HasImage())
3518 GetImageSize( item
->GetImage(), ix
, iy
);
3522 if (item
->HasText())
3525 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3533 width
= max
+ AUTOSIZE_COL_MARGIN
;
3537 column
->SetWidth( width
);
3539 // invalidate it as it has to be recalculated
3543 int wxListMainWindow::GetHeaderWidth() const
3545 if ( !m_headerWidth
)
3547 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3549 size_t count
= GetColumnCount();
3550 for ( size_t col
= 0; col
< count
; col
++ )
3552 self
->m_headerWidth
+= GetColumnWidth(col
);
3556 return m_headerWidth
;
3559 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3561 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3562 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3564 wxListHeaderData
*column
= node
->GetData();
3565 column
->GetItem( item
);
3568 int wxListMainWindow::GetColumnWidth( int col
) const
3570 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3571 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3573 wxListHeaderData
*column
= node
->GetData();
3574 return column
->GetWidth();
3577 // ----------------------------------------------------------------------------
3579 // ----------------------------------------------------------------------------
3581 void wxListMainWindow::SetItem( wxListItem
&item
)
3583 long id
= item
.m_itemId
;
3584 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3585 _T("invalid item index in SetItem") );
3589 wxListLineData
*line
= GetLine((size_t)id
);
3590 line
->SetItem( item
.m_col
, item
);
3593 // update the item on screen
3595 GetItemRect(id
, rectItem
);
3596 RefreshRect(rectItem
);
3599 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3601 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3602 _T("invalid list ctrl item index in SetItem") );
3604 size_t oldCurrent
= m_current
;
3605 size_t item
= (size_t)litem
; // safe because of the check above
3607 // do we need to change the focus?
3608 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3610 if ( state
& wxLIST_STATE_FOCUSED
)
3612 // don't do anything if this item is already focused
3613 if ( item
!= m_current
)
3615 ChangeCurrent(item
);
3617 if ( oldCurrent
!= (size_t)-1 )
3619 if ( IsSingleSel() )
3621 HighlightLine(oldCurrent
, false);
3624 RefreshLine(oldCurrent
);
3627 RefreshLine( m_current
);
3632 // don't do anything if this item is not focused
3633 if ( item
== m_current
)
3637 if ( IsSingleSel() )
3639 // we must unselect the old current item as well or we
3640 // might end up with more than one selected item in a
3641 // single selection control
3642 HighlightLine(oldCurrent
, false);
3645 RefreshLine( oldCurrent
);
3650 // do we need to change the selection state?
3651 if ( stateMask
& wxLIST_STATE_SELECTED
)
3653 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3655 if ( IsSingleSel() )
3659 // selecting the item also makes it the focused one in the
3661 if ( m_current
!= item
)
3663 ChangeCurrent(item
);
3665 if ( oldCurrent
!= (size_t)-1 )
3667 HighlightLine( oldCurrent
, false );
3668 RefreshLine( oldCurrent
);
3674 // only the current item may be selected anyhow
3675 if ( item
!= m_current
)
3680 if ( HighlightLine(item
, on
) )
3687 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3689 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3690 _T("invalid list ctrl item index in GetItemState()") );
3692 int ret
= wxLIST_STATE_DONTCARE
;
3694 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3696 if ( (size_t)item
== m_current
)
3697 ret
|= wxLIST_STATE_FOCUSED
;
3700 if ( stateMask
& wxLIST_STATE_SELECTED
)
3702 if ( IsHighlighted(item
) )
3703 ret
|= wxLIST_STATE_SELECTED
;
3709 void wxListMainWindow::GetItem( wxListItem
&item
) const
3711 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3712 _T("invalid item index in GetItem") );
3714 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3715 line
->GetItem( item
.m_col
, item
);
3718 // ----------------------------------------------------------------------------
3720 // ----------------------------------------------------------------------------
3722 size_t wxListMainWindow::GetItemCount() const
3724 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3727 void wxListMainWindow::SetItemCount(long count
)
3729 m_selStore
.SetItemCount(count
);
3730 m_countVirt
= count
;
3732 ResetVisibleLinesRange();
3734 // scrollbars must be reset
3738 int wxListMainWindow::GetSelectedItemCount() const
3740 // deal with the quick case first
3741 if ( IsSingleSel() )
3743 return HasCurrent() ? IsHighlighted(m_current
) : false;
3746 // virtual controls remmebers all its selections itself
3748 return m_selStore
.GetSelectedCount();
3750 // TODO: we probably should maintain the number of items selected even for
3751 // non virtual controls as enumerating all lines is really slow...
3752 size_t countSel
= 0;
3753 size_t count
= GetItemCount();
3754 for ( size_t line
= 0; line
< count
; line
++ )
3756 if ( GetLine(line
)->IsHighlighted() )
3763 // ----------------------------------------------------------------------------
3764 // item position/size
3765 // ----------------------------------------------------------------------------
3767 wxRect
wxListMainWindow::GetViewRect() const
3769 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3770 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3772 // we need to find the longest/tallest label
3775 const int count
= GetItemCount();
3778 for ( int i
= 0; i
< count
; i
++ )
3783 wxCoord x
= r
.GetRight(),
3793 // some fudge needed to make it look prettier
3794 xMax
+= 2*EXTRA_BORDER_X
;
3795 yMax
+= 2*EXTRA_BORDER_Y
;
3797 // account for the scrollbars if necessary
3798 const wxSize sizeAll
= GetClientSize();
3799 if ( xMax
> sizeAll
.x
)
3800 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3801 if ( yMax
> sizeAll
.y
)
3802 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3804 return wxRect(0, 0, xMax
, yMax
);
3807 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3809 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3810 _T("invalid index in GetItemRect") );
3812 // ensure that we're laid out, otherwise we could return nonsense
3815 wxConstCast(this, wxListMainWindow
)->
3816 RecalculatePositions(true /* no refresh */);
3819 rect
= GetLineRect((size_t)index
);
3821 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3824 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3827 GetItemRect(item
, rect
);
3835 // ----------------------------------------------------------------------------
3836 // geometry calculation
3837 // ----------------------------------------------------------------------------
3839 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3841 wxClientDC
dc( this );
3842 dc
.SetFont( GetFont() );
3844 const size_t count
= GetItemCount();
3847 if ( HasFlag(wxLC_ICON
) )
3848 iconSpacing
= m_normal_spacing
;
3849 else if ( HasFlag(wxLC_SMALL_ICON
) )
3850 iconSpacing
= m_small_spacing
;
3854 // Note that we do not call GetClientSize() here but
3855 // GetSize() and subtract the border size for sunken
3856 // borders manually. This is technically incorrect,
3857 // but we need to know the client area's size WITHOUT
3858 // scrollbars here. Since we don't know if there are
3859 // any scrollbars, we use GetSize() instead. Another
3860 // solution would be to call SetScrollbars() here to
3861 // remove the scrollbars and call GetClientSize() then,
3862 // but this might result in flicker and - worse - will
3863 // reset the scrollbars to 0 which is not good at all
3864 // if you resize a dialog/window, but don't want to
3865 // reset the window scrolling. RR.
3866 // Furthermore, we actually do NOT subtract the border
3867 // width as 2 pixels is just the extra space which we
3868 // need around the actual content in the window. Other-
3869 // wise the text would e.g. touch the upper border. RR.
3872 GetSize( &clientWidth
, &clientHeight
);
3874 const int lineHeight
= GetLineHeight();
3876 if ( InReportView() )
3878 // all lines have the same height and we scroll one line per step
3879 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3881 m_linesPerPage
= clientHeight
/ lineHeight
;
3883 ResetVisibleLinesRange();
3885 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3886 GetHeaderWidth() / SCROLL_UNIT_X
,
3887 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3888 GetScrollPos(wxHORIZONTAL
),
3889 GetScrollPos(wxVERTICAL
),
3894 // we have 3 different layout strategies: either layout all items
3895 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3896 // to arrange them in top to bottom, left to right (don't ask me why
3897 // not the other way round...) order
3898 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3900 int x
= EXTRA_BORDER_X
;
3901 int y
= EXTRA_BORDER_Y
;
3903 wxCoord widthMax
= 0;
3906 for ( i
= 0; i
< count
; i
++ )
3908 wxListLineData
*line
= GetLine(i
);
3909 line
->CalculateSize( &dc
, iconSpacing
);
3910 line
->SetPosition( x
, y
, iconSpacing
);
3912 wxSize sizeLine
= GetLineSize(i
);
3914 if ( HasFlag(wxLC_ALIGN_TOP
) )
3916 if ( sizeLine
.x
> widthMax
)
3917 widthMax
= sizeLine
.x
;
3921 else // wxLC_ALIGN_LEFT
3923 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3927 if ( HasFlag(wxLC_ALIGN_TOP
) )
3929 // traverse the items again and tweak their sizes so that they are
3930 // all the same in a row
3931 for ( i
= 0; i
< count
; i
++ )
3933 wxListLineData
*line
= GetLine(i
);
3934 line
->m_gi
->ExtendWidth(widthMax
);
3943 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3944 (y
+ lineHeight
) / lineHeight
,
3945 GetScrollPos( wxHORIZONTAL
),
3946 GetScrollPos( wxVERTICAL
),
3950 else // "flowed" arrangement, the most complicated case
3952 // at first we try without any scrollbars, if the items don't fit into
3953 // the window, we recalculate after subtracting the space taken by the
3956 int entireWidth
= 0;
3958 for (int tries
= 0; tries
< 2; tries
++)
3960 entireWidth
= 2*EXTRA_BORDER_X
;
3964 // Now we have decided that the items do not fit into the
3965 // client area, so we need a scrollbar
3966 entireWidth
+= SCROLL_UNIT_X
;
3969 int x
= EXTRA_BORDER_X
;
3970 int y
= EXTRA_BORDER_Y
;
3971 int maxWidthInThisRow
= 0;
3974 int currentlyVisibleLines
= 0;
3976 for (size_t i
= 0; i
< count
; i
++)
3978 currentlyVisibleLines
++;
3979 wxListLineData
*line
= GetLine(i
);
3980 line
->CalculateSize( &dc
, iconSpacing
);
3981 line
->SetPosition( x
, y
, iconSpacing
);
3983 wxSize sizeLine
= GetLineSize(i
);
3985 if ( maxWidthInThisRow
< sizeLine
.x
)
3986 maxWidthInThisRow
= sizeLine
.x
;
3989 if (currentlyVisibleLines
> m_linesPerPage
)
3990 m_linesPerPage
= currentlyVisibleLines
;
3992 if ( y
+ sizeLine
.y
>= clientHeight
)
3994 currentlyVisibleLines
= 0;
3996 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3997 x
+= maxWidthInThisRow
;
3998 entireWidth
+= maxWidthInThisRow
;
3999 maxWidthInThisRow
= 0;
4002 // We have reached the last item.
4003 if ( i
== count
- 1 )
4004 entireWidth
+= maxWidthInThisRow
;
4006 if ( (tries
== 0) &&
4007 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4009 clientHeight
-= wxSystemSettings::
4010 GetMetric(wxSYS_HSCROLL_Y
);
4015 if ( i
== count
- 1 )
4016 tries
= 1; // Everything fits, no second try required.
4024 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4026 GetScrollPos( wxHORIZONTAL
),
4035 // FIXME: why should we call it from here?
4042 void wxListMainWindow::RefreshAll()
4047 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4048 if ( headerWin
&& headerWin
->m_dirty
)
4050 headerWin
->m_dirty
= false;
4051 headerWin
->Refresh();
4055 void wxListMainWindow::UpdateCurrent()
4057 if ( !HasCurrent() && !IsEmpty() )
4063 long wxListMainWindow::GetNextItem( long item
,
4064 int WXUNUSED(geometry
),
4068 max
= GetItemCount();
4069 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4070 _T("invalid listctrl index in GetNextItem()") );
4072 // notice that we start with the next item (or the first one if item == -1)
4073 // and this is intentional to allow writing a simple loop to iterate over
4074 // all selected items
4078 // this is not an error because the index was ok initially, just no
4089 size_t count
= GetItemCount();
4090 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4092 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4095 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4102 // ----------------------------------------------------------------------------
4104 // ----------------------------------------------------------------------------
4106 void wxListMainWindow::DeleteItem( long lindex
)
4108 size_t count
= GetItemCount();
4110 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4111 _T("invalid item index in DeleteItem") );
4113 size_t index
= (size_t)lindex
;
4115 // we don't need to adjust the index for the previous items
4116 if ( HasCurrent() && m_current
>= index
)
4118 // if the current item is being deleted, we want the next one to
4119 // become selected - unless there is no next one - so don't adjust
4120 // m_current in this case
4121 if ( m_current
!= index
|| m_current
== count
- 1 )
4127 if ( InReportView() )
4129 ResetVisibleLinesRange();
4136 m_selStore
.OnItemDelete(index
);
4140 m_lines
.RemoveAt( index
);
4143 // we need to refresh the (vert) scrollbar as the number of items changed
4146 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4148 RefreshAfter(index
);
4151 void wxListMainWindow::DeleteColumn( int col
)
4153 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4155 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4158 delete node
->GetData();
4159 m_columns
.Erase( node
);
4163 // update all the items
4164 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4166 wxListLineData
* const line
= GetLine(i
);
4167 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4168 delete n
->GetData();
4169 line
->m_items
.Erase(n
);
4173 // invalidate it as it has to be recalculated
4177 void wxListMainWindow::DoDeleteAllItems()
4181 // nothing to do - in particular, don't send the event
4187 // to make the deletion of all items faster, we don't send the
4188 // notifications for each item deletion in this case but only one event
4189 // for all of them: this is compatible with wxMSW and documented in
4190 // DeleteAllItems() description
4192 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4193 event
.SetEventObject( GetParent() );
4194 GetParent()->GetEventHandler()->ProcessEvent( event
);
4203 if ( InReportView() )
4205 ResetVisibleLinesRange();
4211 void wxListMainWindow::DeleteAllItems()
4215 RecalculatePositions();
4218 void wxListMainWindow::DeleteEverything()
4220 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4225 // ----------------------------------------------------------------------------
4226 // scanning for an item
4227 // ----------------------------------------------------------------------------
4229 void wxListMainWindow::EnsureVisible( long index
)
4231 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4232 _T("invalid index in EnsureVisible") );
4234 // We have to call this here because the label in question might just have
4235 // been added and its position is not known yet
4238 RecalculatePositions(true /* no refresh */);
4241 MoveToItem((size_t)index
);
4244 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4251 size_t count
= GetItemCount();
4252 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4254 wxListLineData
*line
= GetLine(i
);
4255 if ( line
->GetText(0) == tmp
)
4262 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4268 size_t count
= GetItemCount();
4269 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4271 wxListLineData
*line
= GetLine(i
);
4273 line
->GetItem( 0, item
);
4274 if (item
.m_data
== data
)
4281 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4284 GetVisibleLinesRange(&topItem
, NULL
);
4287 GetItemPosition( GetItemCount()-1, p
);
4290 long id
= (long) floor( pt
.y
*double(GetItemCount()-topItem
-1)/p
.y
+topItem
);
4291 if( id
>= 0 && id
< (long)GetItemCount() )
4297 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4299 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4301 size_t count
= GetItemCount();
4303 if ( InReportView() )
4305 size_t current
= y
/ GetLineHeight();
4306 if ( current
< count
)
4308 flags
= HitTestLine(current
, x
, y
);
4315 // TODO: optimize it too! this is less simple than for report view but
4316 // enumerating all items is still not a way to do it!!
4317 for ( size_t current
= 0; current
< count
; current
++ )
4319 flags
= HitTestLine(current
, x
, y
);
4328 // ----------------------------------------------------------------------------
4330 // ----------------------------------------------------------------------------
4332 void wxListMainWindow::InsertItem( wxListItem
&item
)
4334 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4336 int count
= GetItemCount();
4337 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4339 if (item
.m_itemId
> count
)
4340 item
.m_itemId
= count
;
4342 size_t id
= item
.m_itemId
;
4347 // this is unused variable
4350 if ( InReportView() )
4353 // this is unused variable
4356 ResetVisibleLinesRange();
4358 else if ( HasFlag(wxLC_LIST
) )
4360 // this is unused variable
4365 else if ( HasFlag(wxLC_ICON
) )
4367 // this is unused variable
4372 else if ( HasFlag(wxLC_SMALL_ICON
) )
4374 // this is unused variable
4375 mode
= wxLC_ICON
; // no typo
4381 wxFAIL_MSG( _T("unknown mode") );
4384 wxListLineData
*line
= new wxListLineData(this);
4386 line
->SetItem( item
.m_col
, item
);
4388 m_lines
.Insert( line
, id
);
4392 // If an item is selected at or below the point of insertion, we need to
4393 // increment the member variables because the current row's index has gone
4395 if ( HasCurrent() && m_current
>= id
)
4400 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4402 RefreshLines(id
, GetItemCount() - 1);
4405 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4408 if ( InReportView() )
4410 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4411 item
.m_width
= GetTextLength( item
.m_text
);
4413 wxListHeaderData
*column
= new wxListHeaderData( item
);
4414 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4417 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4418 m_columns
.Insert( node
, column
);
4422 m_columns
.Append( column
);
4427 // update all the items
4428 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4430 wxListLineData
* const line
= GetLine(i
);
4431 wxListItemData
* const data
= new wxListItemData(this);
4433 line
->m_items
.Insert(col
, data
);
4435 line
->m_items
.Append(data
);
4439 // invalidate it as it has to be recalculated
4444 // ----------------------------------------------------------------------------
4446 // ----------------------------------------------------------------------------
4448 wxListCtrlCompare list_ctrl_compare_func_2
;
4449 long list_ctrl_compare_data
;
4451 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4453 wxListLineData
*line1
= *arg1
;
4454 wxListLineData
*line2
= *arg2
;
4456 line1
->GetItem( 0, item
);
4457 wxUIntPtr data1
= item
.m_data
;
4458 line2
->GetItem( 0, item
);
4459 wxUIntPtr data2
= item
.m_data
;
4460 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4463 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4465 list_ctrl_compare_func_2
= fn
;
4466 list_ctrl_compare_data
= data
;
4467 m_lines
.Sort( list_ctrl_compare_func_1
);
4471 // ----------------------------------------------------------------------------
4473 // ----------------------------------------------------------------------------
4475 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4477 // update our idea of which lines are shown when we redraw the window the
4479 ResetVisibleLinesRange();
4482 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4483 wxScrolledWindow::OnScroll(event
);
4485 HandleOnScroll( event
);
4488 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4490 wxGenericListCtrl
* lc
= GetListCtrl();
4491 wxCHECK_RET( lc
, _T("no listctrl window?") );
4493 lc
->m_headerWin
->Refresh();
4494 lc
->m_headerWin
->Update();
4498 int wxListMainWindow::GetCountPerPage() const
4500 if ( !m_linesPerPage
)
4502 wxConstCast(this, wxListMainWindow
)->
4503 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4506 return m_linesPerPage
;
4509 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4511 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4513 if ( m_lineFrom
== (size_t)-1 )
4515 size_t count
= GetItemCount();
4518 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4520 // this may happen if SetScrollbars() hadn't been called yet
4521 if ( m_lineFrom
>= count
)
4522 m_lineFrom
= count
- 1;
4524 // we redraw one extra line but this is needed to make the redrawing
4525 // logic work when there is a fractional number of lines on screen
4526 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4527 if ( m_lineTo
>= count
)
4528 m_lineTo
= count
- 1;
4530 else // empty control
4533 m_lineTo
= (size_t)-1;
4537 wxASSERT_MSG( IsEmpty() ||
4538 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4539 _T("GetVisibleLinesRange() returns incorrect result") );
4547 // -------------------------------------------------------------------------------------
4548 // wxGenericListCtrl
4549 // -------------------------------------------------------------------------------------
4551 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4553 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4554 EVT_SIZE(wxGenericListCtrl::OnSize
)
4557 wxGenericListCtrl::wxGenericListCtrl()
4559 m_imageListNormal
= (wxImageListType
*) NULL
;
4560 m_imageListSmall
= (wxImageListType
*) NULL
;
4561 m_imageListState
= (wxImageListType
*) NULL
;
4563 m_ownsImageListNormal
=
4564 m_ownsImageListSmall
=
4565 m_ownsImageListState
= false;
4567 m_mainWin
= (wxListMainWindow
*) NULL
;
4568 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4572 wxGenericListCtrl::~wxGenericListCtrl()
4574 if (m_ownsImageListNormal
)
4575 delete m_imageListNormal
;
4576 if (m_ownsImageListSmall
)
4577 delete m_imageListSmall
;
4578 if (m_ownsImageListState
)
4579 delete m_imageListState
;
4582 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4586 // we use 'g' to get the descent, too
4588 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4589 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4591 // only update if changed
4592 if ( h
!= m_headerHeight
)
4596 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4599 ResizeReportView(true);
4604 void wxGenericListCtrl::CreateHeaderWindow()
4606 m_headerWin
= new wxListHeaderWindow
4608 this, wxID_ANY
, m_mainWin
,
4610 wxSize(GetClientSize().x
, m_headerHeight
),
4613 CalculateAndSetHeaderHeight();
4616 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4621 const wxValidator
&validator
,
4622 const wxString
&name
)
4626 m_imageListState
= (wxImageListType
*) NULL
;
4627 m_ownsImageListNormal
=
4628 m_ownsImageListSmall
=
4629 m_ownsImageListState
= false;
4631 m_mainWin
= (wxListMainWindow
*) NULL
;
4632 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4636 if ( !(style
& wxLC_MASK_TYPE
) )
4638 style
= style
| wxLC_LIST
;
4641 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4644 // don't create the inner window with the border
4645 style
&= ~wxBORDER_MASK
;
4647 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0,0), size
, style
);
4649 #ifdef __WXMAC_CARBON__
4650 // Human Interface Guidelines ask us for a special font in this case
4651 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
4654 font
.MacCreateThemeFont( kThemeViewsFont
) ;
4658 if ( InReportView() )
4660 CreateHeaderWindow();
4662 if ( HasFlag(wxLC_NO_HEADER
) )
4664 // VZ: why do we create it at all then?
4665 m_headerWin
->Show( false );
4674 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4676 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4677 _T("wxLC_VIRTUAL can't be [un]set") );
4679 long flag
= GetWindowStyle();
4683 if (style
& wxLC_MASK_TYPE
)
4684 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4685 if (style
& wxLC_MASK_ALIGN
)
4686 flag
&= ~wxLC_MASK_ALIGN
;
4687 if (style
& wxLC_MASK_SORT
)
4688 flag
&= ~wxLC_MASK_SORT
;
4700 SetWindowStyleFlag( flag
);
4703 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4707 m_mainWin
->DeleteEverything();
4709 // has the header visibility changed?
4710 bool hasHeader
= HasHeader();
4711 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4713 if ( hasHeader
!= willHaveHeader
)
4720 // don't delete, just hide, as we can reuse it later
4721 m_headerWin
->Show(false);
4723 //else: nothing to do
4725 else // must show header
4729 CreateHeaderWindow();
4731 else // already have it, just show
4733 m_headerWin
->Show( true );
4737 ResizeReportView(willHaveHeader
);
4741 wxWindow::SetWindowStyleFlag( flag
);
4744 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4746 m_mainWin
->GetColumn( col
, item
);
4750 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4752 m_mainWin
->SetColumn( col
, item
);
4756 int wxGenericListCtrl::GetColumnWidth( int col
) const
4758 return m_mainWin
->GetColumnWidth( col
);
4761 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4763 m_mainWin
->SetColumnWidth( col
, width
);
4767 int wxGenericListCtrl::GetCountPerPage() const
4769 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4772 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4774 m_mainWin
->GetItem( info
);
4778 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4780 m_mainWin
->SetItem( info
);
4784 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4787 info
.m_text
= label
;
4788 info
.m_mask
= wxLIST_MASK_TEXT
;
4789 info
.m_itemId
= index
;
4793 info
.m_image
= imageId
;
4794 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4796 m_mainWin
->SetItem(info
);
4800 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4802 return m_mainWin
->GetItemState( item
, stateMask
);
4805 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4807 m_mainWin
->SetItemState( item
, state
, stateMask
);
4812 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4815 info
.m_image
= image
;
4816 info
.m_mask
= wxLIST_MASK_IMAGE
;
4817 info
.m_itemId
= item
;
4818 m_mainWin
->SetItem( info
);
4822 wxString
wxGenericListCtrl::GetItemText( long item
) const
4824 return m_mainWin
->GetItemText(item
);
4827 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4829 m_mainWin
->SetItemText(item
, str
);
4832 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4835 info
.m_itemId
= item
;
4836 m_mainWin
->GetItem( info
);
4840 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4843 info
.m_mask
= wxLIST_MASK_DATA
;
4844 info
.m_itemId
= item
;
4846 m_mainWin
->SetItem( info
);
4850 wxRect
wxGenericListCtrl::GetViewRect() const
4852 return m_mainWin
->GetViewRect();
4855 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4857 m_mainWin
->GetItemRect( item
, rect
);
4858 if ( m_mainWin
->HasHeader() )
4859 rect
.y
+= m_headerHeight
+ 1;
4863 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4865 m_mainWin
->GetItemPosition( item
, pos
);
4869 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4874 int wxGenericListCtrl::GetItemCount() const
4876 return m_mainWin
->GetItemCount();
4879 int wxGenericListCtrl::GetColumnCount() const
4881 return m_mainWin
->GetColumnCount();
4884 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4886 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4889 wxSize
wxGenericListCtrl::GetItemSpacing() const
4891 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4893 return wxSize(spacing
, spacing
);
4896 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4898 return m_mainWin
->GetItemSpacing( isSmall
);
4901 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4904 info
.m_itemId
= item
;
4905 info
.SetTextColour( col
);
4906 m_mainWin
->SetItem( info
);
4909 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4912 info
.m_itemId
= item
;
4913 m_mainWin
->GetItem( info
);
4914 return info
.GetTextColour();
4917 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4920 info
.m_itemId
= item
;
4921 info
.SetBackgroundColour( col
);
4922 m_mainWin
->SetItem( info
);
4925 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4928 info
.m_itemId
= item
;
4929 m_mainWin
->GetItem( info
);
4930 return info
.GetBackgroundColour();
4933 int wxGenericListCtrl::GetSelectedItemCount() const
4935 return m_mainWin
->GetSelectedItemCount();
4938 wxColour
wxGenericListCtrl::GetTextColour() const
4940 return GetForegroundColour();
4943 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4945 SetForegroundColour(col
);
4948 long wxGenericListCtrl::GetTopItem() const
4951 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4955 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4957 return m_mainWin
->GetNextItem( item
, geom
, state
);
4960 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4962 if (which
== wxIMAGE_LIST_NORMAL
)
4964 return m_imageListNormal
;
4966 else if (which
== wxIMAGE_LIST_SMALL
)
4968 return m_imageListSmall
;
4970 else if (which
== wxIMAGE_LIST_STATE
)
4972 return m_imageListState
;
4974 return (wxImageListType
*) NULL
;
4977 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4979 if ( which
== wxIMAGE_LIST_NORMAL
)
4981 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4982 m_imageListNormal
= imageList
;
4983 m_ownsImageListNormal
= false;
4985 else if ( which
== wxIMAGE_LIST_SMALL
)
4987 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4988 m_imageListSmall
= imageList
;
4989 m_ownsImageListSmall
= false;
4991 else if ( which
== wxIMAGE_LIST_STATE
)
4993 if (m_ownsImageListState
) delete m_imageListState
;
4994 m_imageListState
= imageList
;
4995 m_ownsImageListState
= false;
4998 m_mainWin
->SetImageList( imageList
, which
);
5001 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
5003 SetImageList(imageList
, which
);
5004 if ( which
== wxIMAGE_LIST_NORMAL
)
5005 m_ownsImageListNormal
= true;
5006 else if ( which
== wxIMAGE_LIST_SMALL
)
5007 m_ownsImageListSmall
= true;
5008 else if ( which
== wxIMAGE_LIST_STATE
)
5009 m_ownsImageListState
= true;
5012 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5017 bool wxGenericListCtrl::DeleteItem( long item
)
5019 m_mainWin
->DeleteItem( item
);
5023 bool wxGenericListCtrl::DeleteAllItems()
5025 m_mainWin
->DeleteAllItems();
5029 bool wxGenericListCtrl::DeleteAllColumns()
5031 size_t count
= m_mainWin
->m_columns
.GetCount();
5032 for ( size_t n
= 0; n
< count
; n
++ )
5038 void wxGenericListCtrl::ClearAll()
5040 m_mainWin
->DeleteEverything();
5043 bool wxGenericListCtrl::DeleteColumn( int col
)
5045 m_mainWin
->DeleteColumn( col
);
5047 // if we don't have the header any longer, we need to relayout the window
5048 if ( !GetColumnCount() )
5050 ResizeReportView(false /* no header */);
5056 void wxGenericListCtrl::Edit( long item
)
5058 m_mainWin
->EditLabel( item
);
5061 bool wxGenericListCtrl::EnsureVisible( long item
)
5063 m_mainWin
->EnsureVisible( item
);
5067 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5069 return m_mainWin
->FindItem( start
, str
, partial
);
5072 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5074 return m_mainWin
->FindItem( start
, data
);
5077 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5078 int WXUNUSED(direction
))
5080 return m_mainWin
->FindItem( pt
);
5083 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5085 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5088 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5090 m_mainWin
->InsertItem( info
);
5091 return info
.m_itemId
;
5094 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5097 info
.m_text
= label
;
5098 info
.m_mask
= wxLIST_MASK_TEXT
;
5099 info
.m_itemId
= index
;
5100 return InsertItem( info
);
5103 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5106 info
.m_mask
= wxLIST_MASK_IMAGE
;
5107 info
.m_image
= imageIndex
;
5108 info
.m_itemId
= index
;
5109 return InsertItem( info
);
5112 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5115 info
.m_text
= label
;
5116 info
.m_image
= imageIndex
;
5117 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5118 info
.m_itemId
= index
;
5119 return InsertItem( info
);
5122 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5124 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5126 m_mainWin
->InsertColumn( col
, item
);
5128 // if we hadn't had header before and have it now we need to relayout the
5130 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5132 ResizeReportView(true /* have header */);
5135 m_headerWin
->Refresh();
5140 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5141 int format
, int width
)
5144 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5145 item
.m_text
= heading
;
5148 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5149 item
.m_width
= width
;
5151 item
.m_format
= format
;
5153 return InsertColumn( col
, item
);
5156 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5162 // fn is a function which takes 3 long arguments: item1, item2, data.
5163 // item1 is the long data associated with a first item (NOT the index).
5164 // item2 is the long data associated with a second item (NOT the index).
5165 // data is the same value as passed to SortItems.
5166 // The return value is a negative number if the first item should precede the second
5167 // item, a positive number of the second item should precede the first,
5168 // or zero if the two items are equivalent.
5169 // data is arbitrary data to be passed to the sort function.
5171 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5173 m_mainWin
->SortItems( fn
, data
);
5177 // ----------------------------------------------------------------------------
5179 // ----------------------------------------------------------------------------
5181 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5186 ResizeReportView(m_mainWin
->HasHeader());
5188 m_mainWin
->RecalculatePositions();
5191 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5194 GetClientSize( &cw
, &ch
);
5198 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5199 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5201 else // no header window
5203 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5207 void wxGenericListCtrl::OnInternalIdle()
5209 wxWindow::OnInternalIdle();
5211 // do it only if needed
5212 if ( !m_mainWin
->m_dirty
)
5215 m_mainWin
->RecalculatePositions();
5218 // ----------------------------------------------------------------------------
5220 // ----------------------------------------------------------------------------
5222 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5226 m_mainWin
->SetBackgroundColour( colour
);
5227 m_mainWin
->m_dirty
= true;
5233 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5235 if ( !wxWindow::SetForegroundColour( colour
) )
5240 m_mainWin
->SetForegroundColour( colour
);
5241 m_mainWin
->m_dirty
= true;
5246 m_headerWin
->SetForegroundColour( colour
);
5252 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5254 if ( !wxWindow::SetFont( font
) )
5259 m_mainWin
->SetFont( font
);
5260 m_mainWin
->m_dirty
= true;
5265 m_headerWin
->SetFont( font
);
5266 CalculateAndSetHeaderHeight();
5277 #include "wx/listbox.h"
5282 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5285 // Use the same color scheme as wxListBox
5286 return wxListBox::GetClassDefaultAttributes(variant
);
5288 wxUnusedVar(variant
);
5289 wxVisualAttributes attr
;
5290 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5291 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5292 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5297 // ----------------------------------------------------------------------------
5298 // methods forwarded to m_mainWin
5299 // ----------------------------------------------------------------------------
5301 #if wxUSE_DRAG_AND_DROP
5303 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5305 m_mainWin
->SetDropTarget( dropTarget
);
5308 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5310 return m_mainWin
->GetDropTarget();
5313 #endif // wxUSE_DRAG_AND_DROP
5315 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5317 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5320 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5322 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5325 wxColour
wxGenericListCtrl::GetForegroundColour() const
5327 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5330 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5333 return m_mainWin
->PopupMenu( menu
, x
, y
);
5336 #endif // wxUSE_MENUS
5339 void wxGenericListCtrl::SetFocus()
5341 /* The test in window.cpp fails as we are a composite
5342 window, so it checks against "this", but not m_mainWin. */
5343 if ( DoFindFocus() != this )
5344 m_mainWin
->SetFocus();
5347 wxSize
wxGenericListCtrl::DoGetBestSize() const
5349 // Something is better than nothing...
5350 // 100x80 is what the MSW version will get from the default
5351 // wxControl::DoGetBestSize
5352 return wxSize(100,80);
5355 // ----------------------------------------------------------------------------
5356 // virtual list control support
5357 // ----------------------------------------------------------------------------
5359 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5361 // this is a pure virtual function, in fact - which is not really pure
5362 // because the controls which are not virtual don't need to implement it
5363 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5365 return wxEmptyString
;
5368 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5370 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5372 wxT("List control has an image list, OnGetItemImage should be overridden."));
5377 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5379 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5380 _T("invalid item index in OnGetItemAttr()") );
5382 // no attributes by default
5386 void wxGenericListCtrl::SetItemCount(long count
)
5388 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5390 m_mainWin
->SetItemCount(count
);
5393 void wxGenericListCtrl::RefreshItem(long item
)
5395 m_mainWin
->RefreshLine(item
);
5398 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5400 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5404 * Generic wxListCtrl is more or less a container for two other
5405 * windows which drawings are done upon. These are namely
5406 * 'm_headerWin' and 'm_mainWin'.
5407 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5408 * behaviour wxListCtrl has under wxMSW.
5410 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5414 // The easy case, no rectangle specified.
5416 m_headerWin
->Refresh(eraseBackground
);
5419 m_mainWin
->Refresh(eraseBackground
);
5423 // Refresh the header window
5426 wxRect rectHeader
= m_headerWin
->GetRect();
5427 rectHeader
.Intersect(*rect
);
5428 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5431 m_headerWin
->GetPosition(&x
, &y
);
5432 rectHeader
.Offset(-x
, -y
);
5433 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5438 // Refresh the main window
5441 wxRect rectMain
= m_mainWin
->GetRect();
5442 rectMain
.Intersect(*rect
);
5443 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5446 m_mainWin
->GetPosition(&x
, &y
);
5447 rectMain
.Offset(-x
, -y
);
5448 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5454 void wxGenericListCtrl::Freeze()
5456 m_mainWin
->Freeze();
5459 void wxGenericListCtrl::Thaw()
5464 #endif // wxUSE_LISTCTRL