1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #include "wx/selstore.h"
73 #include "wx/renderer.h"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // // the height of the header window (FIXME: should depend on its font!)
107 // static const int HEADER_HEIGHT = 23;
109 // the scrollbar units
110 static const int SCROLL_UNIT_X
= 15;
111 static const int SCROLL_UNIT_Y
= 15;
113 // the spacing between the lines (in report mode)
114 static const int LINE_SPACING
= 0;
116 // extra margins around the text label
117 static const int EXTRA_WIDTH
= 3;
118 static const int EXTRA_HEIGHT
= 4;
120 // margin between the window and the items
121 static const int EXTRA_BORDER_X
= 2;
122 static const int EXTRA_BORDER_Y
= 2;
124 // offset for the header window
125 static const int HEADER_OFFSET_X
= 1;
126 static const int HEADER_OFFSET_Y
= 1;
128 // margin between rows of icons in [small] icon view
129 static const int MARGIN_BETWEEN_ROWS
= 6;
131 // when autosizing the columns, add some slack
132 static const int AUTOSIZE_COL_MARGIN
= 10;
134 // default and minimal widths for the header columns
135 static const int WIDTH_COL_DEFAULT
= 80;
136 static const int WIDTH_COL_MIN
= 10;
138 // the space between the image and the text in the report mode
139 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
141 // ============================================================================
143 // ============================================================================
145 //-----------------------------------------------------------------------------
146 // wxListItemData (internal)
147 //-----------------------------------------------------------------------------
149 class WXDLLEXPORT wxListItemData
152 wxListItemData(wxListMainWindow
*owner
);
155 void SetItem( const wxListItem
&info
);
156 void SetImage( int image
) { m_image
= image
; }
157 void SetData( long data
) { m_data
= data
; }
158 void SetPosition( int x
, int y
);
159 void SetSize( int width
, int height
);
161 bool HasText() const { return !m_text
.empty(); }
162 const wxString
& GetText() const { return m_text
; }
163 void SetText(const wxString
& text
) { m_text
= text
; }
165 // we can't use empty string for measuring the string width/height, so
166 // always return something
167 wxString
GetTextForMeasuring() const
169 wxString s
= GetText();
176 bool IsHit( int x
, int y
) const;
180 int GetWidth() const;
181 int GetHeight() const;
183 int GetImage() const { return m_image
; }
184 bool HasImage() const { return GetImage() != -1; }
186 void GetItem( wxListItem
&info
) const;
188 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
189 wxListItemAttr
*GetAttr() const { return m_attr
; }
192 // the item image or -1
195 // user data associated with the item
198 // the item coordinates are not used in report mode, instead this pointer
199 // is NULL and the owner window is used to retrieve the item position and
203 // the list ctrl we are in
204 wxListMainWindow
*m_owner
;
206 // custom attributes or NULL
207 wxListItemAttr
*m_attr
;
210 // common part of all ctors
216 //-----------------------------------------------------------------------------
217 // wxListHeaderData (internal)
218 //-----------------------------------------------------------------------------
220 class WXDLLEXPORT wxListHeaderData
: public wxObject
224 wxListHeaderData( const wxListItem
&info
);
225 void SetItem( const wxListItem
&item
);
226 void SetPosition( int x
, int y
);
227 void SetWidth( int w
);
228 void SetFormat( int format
);
229 void SetHeight( int h
);
230 bool HasImage() const;
232 bool HasText() const { return !m_text
.empty(); }
233 const wxString
& GetText() const { return m_text
; }
234 void SetText(const wxString
& text
) { m_text
= text
; }
236 void GetItem( wxListItem
&item
);
238 bool IsHit( int x
, int y
) const;
239 int GetImage() const;
240 int GetWidth() const;
241 int GetFormat() const;
257 //-----------------------------------------------------------------------------
258 // wxListLineData (internal)
259 //-----------------------------------------------------------------------------
261 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
262 #include "wx/listimpl.cpp"
263 WX_DEFINE_LIST(wxListItemDataList
);
268 // the list of subitems: only may have more than one item in report mode
269 wxListItemDataList m_items
;
271 // this is not used in report view
283 // the part to be highlighted
284 wxRect m_rectHighlight
;
287 // is this item selected? [NB: not used in virtual mode]
290 // back pointer to the list ctrl
291 wxListMainWindow
*m_owner
;
294 wxListLineData(wxListMainWindow
*owner
);
298 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
302 // are we in report mode?
303 inline bool InReportView() const;
305 // are we in virtual report mode?
306 inline bool IsVirtual() const;
308 // these 2 methods shouldn't be called for report view controls, in that
309 // case we determine our position/size ourselves
311 // calculate the size of the line
312 void CalculateSize( wxDC
*dc
, int spacing
);
314 // remember the position this line appears at
315 void SetPosition( int x
, int y
, int spacing
);
319 void SetImage( int image
) { SetImage(0, image
); }
320 int GetImage() const { return GetImage(0); }
321 bool HasImage() const { return GetImage() != -1; }
322 bool HasText() const { return !GetText(0).empty(); }
324 void SetItem( int index
, const wxListItem
&info
);
325 void GetItem( int index
, wxListItem
&info
);
327 wxString
GetText(int index
) const;
328 void SetText( int index
, const wxString s
);
330 wxListItemAttr
*GetAttr() const;
331 void SetAttr(wxListItemAttr
*attr
);
333 // return true if the highlighting really changed
334 bool Highlight( bool on
);
336 void ReverseHighlight();
338 bool IsHighlighted() const
340 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
342 return m_highlighted
;
345 // draw the line on the given DC in icon/list mode
346 void Draw( wxDC
*dc
);
348 // the same in report mode
349 void DrawInReportMode( wxDC
*dc
,
351 const wxRect
& rectHL
,
355 // set the line to contain num items (only can be > 1 in report mode)
356 void InitItems( int num
);
358 // get the mode (i.e. style) of the list control
359 inline int GetMode() const;
361 // prepare the DC for drawing with these item's attributes, return true if
362 // we need to draw the items background to highlight it, false otherwise
363 bool SetAttributes(wxDC
*dc
,
364 const wxListItemAttr
*attr
,
367 // draw the text on the DC with the correct justification; also add an
368 // ellipsis if the text is too large to fit in the current width
369 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
371 // these are only used by GetImage/SetImage above, we don't support images
372 // with subitems at the public API level yet
373 void SetImage( int index
, int image
);
374 int GetImage( int index
) const;
377 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
378 #include "wx/arrimpl.cpp"
379 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
381 //-----------------------------------------------------------------------------
382 // wxListHeaderWindow (internal)
383 //-----------------------------------------------------------------------------
385 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
388 wxListMainWindow
*m_owner
;
389 wxCursor
*m_currentCursor
;
390 wxCursor
*m_resizeCursor
;
393 // column being resized or -1
396 // divider line position in logical (unscrolled) coords
399 // minimal position beyond which the divider line can't be dragged in
404 wxListHeaderWindow();
406 wxListHeaderWindow( wxWindow
*win
,
408 wxListMainWindow
*owner
,
409 const wxPoint
&pos
= wxDefaultPosition
,
410 const wxSize
&size
= wxDefaultSize
,
412 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
414 virtual ~wxListHeaderWindow();
417 void AdjustDC(wxDC
& dc
);
419 void OnPaint( wxPaintEvent
&event
);
420 void OnMouse( wxMouseEvent
&event
);
421 void OnSetFocus( wxFocusEvent
&event
);
427 // common part of all ctors
430 // generate and process the list event of the given type, return true if
431 // it wasn't vetoed, i.e. if we should proceed
432 bool SendListEvent(wxEventType type
, wxPoint pos
);
434 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
435 DECLARE_EVENT_TABLE()
438 //-----------------------------------------------------------------------------
439 // wxListRenameTimer (internal)
440 //-----------------------------------------------------------------------------
442 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
445 wxListMainWindow
*m_owner
;
448 wxListRenameTimer( wxListMainWindow
*owner
);
452 //-----------------------------------------------------------------------------
453 // wxListTextCtrl (internal)
454 //-----------------------------------------------------------------------------
456 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
459 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
462 void OnChar( wxKeyEvent
&event
);
463 void OnKeyUp( wxKeyEvent
&event
);
464 void OnKillFocus( wxFocusEvent
&event
);
466 bool AcceptChanges();
470 wxListMainWindow
*m_owner
;
471 wxString m_startValue
;
475 DECLARE_EVENT_TABLE()
478 //-----------------------------------------------------------------------------
479 // wxListMainWindow (internal)
480 //-----------------------------------------------------------------------------
482 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
483 #include "wx/listimpl.cpp"
484 WX_DEFINE_LIST(wxListHeaderDataList
);
486 class wxListMainWindow
: public wxScrolledWindow
490 wxListMainWindow( wxWindow
*parent
,
492 const wxPoint
& pos
= wxDefaultPosition
,
493 const wxSize
& size
= wxDefaultSize
,
495 const wxString
&name
= _T("listctrlmainwindow") );
497 virtual ~wxListMainWindow();
499 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
501 // return true if this is a virtual list control
502 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
504 // return true if the control is in report mode
505 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
507 // return true if we are in single selection mode, false if multi sel
508 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
510 // do we have a header window?
511 bool HasHeader() const
512 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
514 void HighlightAll( bool on
);
516 // all these functions only do something if the line is currently visible
518 // change the line "selected" state, return TRUE if it really changed
519 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
521 // as HighlightLine() but do it for the range of lines: this is incredibly
522 // more efficient for virtual list controls!
524 // NB: unlike HighlightLine() this one does refresh the lines on screen
525 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
527 // toggle the line state and refresh it
528 void ReverseHighlight( size_t line
)
529 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
531 // return true if the line is highlighted
532 bool IsHighlighted(size_t line
) const;
534 // refresh one or several lines at once
535 void RefreshLine( size_t line
);
536 void RefreshLines( size_t lineFrom
, size_t lineTo
);
538 // refresh all selected items
539 void RefreshSelected();
541 // refresh all lines below the given one: the difference with
542 // RefreshLines() is that the index here might not be a valid one (happens
543 // when the last line is deleted)
544 void RefreshAfter( size_t lineFrom
);
546 // the methods which are forwarded to wxListLineData itself in list/icon
547 // modes but are here because the lines don't store their positions in the
550 // get the bound rect for the entire line
551 wxRect
GetLineRect(size_t line
) const;
553 // get the bound rect of the label
554 wxRect
GetLineLabelRect(size_t line
) const;
556 // get the bound rect of the items icon (only may be called if we do have
558 wxRect
GetLineIconRect(size_t line
) const;
560 // get the rect to be highlighted when the item has focus
561 wxRect
GetLineHighlightRect(size_t line
) const;
563 // get the size of the total line rect
564 wxSize
GetLineSize(size_t line
) const
565 { return GetLineRect(line
).GetSize(); }
567 // return the hit code for the corresponding position (in this line)
568 long HitTestLine(size_t line
, int x
, int y
) const;
570 // bring the selected item into view, scrolling to it if necessary
571 void MoveToItem(size_t item
);
573 // bring the current item into view
574 void MoveToFocus() { MoveToItem(m_current
); }
576 // start editing the label of the given item
577 void EditLabel( long item
);
579 // suspend/resume redrawing the control
585 void OnRenameTimer();
586 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
587 void OnRenameCancelled(size_t itemEdit
);
589 void OnMouse( wxMouseEvent
&event
);
591 // called to switch the selection from the current item to newCurrent,
592 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
594 void OnChar( wxKeyEvent
&event
);
595 void OnKeyDown( wxKeyEvent
&event
);
596 void OnSetFocus( wxFocusEvent
&event
);
597 void OnKillFocus( wxFocusEvent
&event
);
598 void OnScroll(wxScrollWinEvent
& event
) ;
600 void OnPaint( wxPaintEvent
&event
);
602 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
603 void GetImageSize( int index
, int &width
, int &height
) const;
604 int GetTextLength( const wxString
&s
) const;
606 void SetImageList( wxImageListType
*imageList
, int which
);
607 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
608 int GetItemSpacing( bool isSmall
= FALSE
);
610 void SetColumn( int col
, wxListItem
&item
);
611 void SetColumnWidth( int col
, int width
);
612 void GetColumn( int col
, wxListItem
&item
) const;
613 int GetColumnWidth( int col
) const;
614 int GetColumnCount() const { return m_columns
.GetCount(); }
616 // returns the sum of the heights of all columns
617 int GetHeaderWidth() const;
619 int GetCountPerPage() const;
621 void SetItem( wxListItem
&item
);
622 void GetItem( wxListItem
&item
) const;
623 void SetItemState( long item
, long state
, long stateMask
);
624 int GetItemState( long item
, long stateMask
) const;
625 void GetItemRect( long index
, wxRect
&rect
) const;
626 wxRect
GetViewRect() const;
627 bool GetItemPosition( long item
, wxPoint
& pos
) const;
628 int GetSelectedItemCount() const;
630 wxString
GetItemText(long item
) const
633 info
.m_itemId
= item
;
638 void SetItemText(long item
, const wxString
& value
)
641 info
.m_mask
= wxLIST_MASK_TEXT
;
642 info
.m_itemId
= item
;
647 // set the scrollbars and update the positions of the items
648 void RecalculatePositions(bool noRefresh
= FALSE
);
650 // refresh the window and the header
653 long GetNextItem( long item
, int geometry
, int state
) const;
654 void DeleteItem( long index
);
655 void DeleteAllItems();
656 void DeleteColumn( int col
);
657 void DeleteEverything();
658 void EnsureVisible( long index
);
659 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
660 long FindItem( long start
, long data
);
661 long HitTest( int x
, int y
, int &flags
);
662 void InsertItem( wxListItem
&item
);
663 void InsertColumn( long col
, wxListItem
&item
);
664 void SortItems( wxListCtrlCompare fn
, long data
);
666 size_t GetItemCount() const;
667 bool IsEmpty() const { return GetItemCount() == 0; }
668 void SetItemCount(long count
);
670 // change the current (== focused) item, send a notification event
671 void ChangeCurrent(size_t current
);
672 void ResetCurrent() { ChangeCurrent((size_t)-1); }
673 bool HasCurrent() const { return m_current
!= (size_t)-1; }
675 // send out a wxListEvent
676 void SendNotify( size_t line
,
678 wxPoint point
= wxDefaultPosition
);
680 // override base class virtual to reset m_lineHeight when the font changes
681 virtual bool SetFont(const wxFont
& font
)
683 if ( !wxScrolledWindow::SetFont(font
) )
691 // these are for wxListLineData usage only
693 // get the backpointer to the list ctrl
694 wxGenericListCtrl
*GetListCtrl() const
696 return wxStaticCast(GetParent(), wxGenericListCtrl
);
699 // get the height of all lines (assuming they all do have the same height)
700 wxCoord
GetLineHeight() const;
702 // get the y position of the given line (only for report view)
703 wxCoord
GetLineY(size_t line
) const;
705 // get the brush to use for the item highlighting
706 wxBrush
*GetHighlightBrush() const
708 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
712 // the array of all line objects for a non virtual list control (for the
713 // virtual list control we only ever use m_lines[0])
714 wxListLineDataArray m_lines
;
716 // the list of column objects
717 wxListHeaderDataList m_columns
;
719 // currently focused item or -1
722 // the number of lines per page
725 // this flag is set when something which should result in the window
726 // redrawing happens (i.e. an item was added or deleted, or its appearance
727 // changed) and OnPaint() doesn't redraw the window while it is set which
728 // allows to minimize the number of repaintings when a lot of items are
729 // being added. The real repainting occurs only after the next OnIdle()
733 wxColour
*m_highlightColour
;
734 wxImageListType
*m_small_image_list
;
735 wxImageListType
*m_normal_image_list
;
737 int m_normal_spacing
;
741 wxTimer
*m_renameTimer
;
746 // for double click logic
747 size_t m_lineLastClicked
,
748 m_lineBeforeLastClicked
;
751 // the total count of items in a virtual list control
754 // the object maintaining the items selection state, only used in virtual
756 wxSelectionStore m_selStore
;
758 // common part of all ctors
761 // get the line data for the given index
762 wxListLineData
*GetLine(size_t n
) const
764 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
768 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
776 // get a dummy line which can be used for geometry calculations and such:
777 // you must use GetLine() if you want to really draw the line
778 wxListLineData
*GetDummyLine() const;
780 // cache the line data of the n-th line in m_lines[0]
781 void CacheLineData(size_t line
);
783 // get the range of visible lines
784 void GetVisibleLinesRange(size_t *from
, size_t *to
);
786 // force us to recalculate the range of visible lines
787 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
789 // get the colour to be used for drawing the rules
790 wxColour
GetRuleColour() const
795 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
800 // initialize the current item if needed
801 void UpdateCurrent();
803 // delete all items but don't refresh: called from dtor
804 void DoDeleteAllItems();
806 // the height of one line using the current font
807 wxCoord m_lineHeight
;
809 // the total header width or 0 if not calculated yet
810 wxCoord m_headerWidth
;
812 // the first and last lines being shown on screen right now (inclusive),
813 // both may be -1 if they must be calculated so never access them directly:
814 // use GetVisibleLinesRange() above instead
818 // the brushes to use for item highlighting when we do/don't have focus
819 wxBrush
*m_highlightBrush
,
820 *m_highlightUnfocusedBrush
;
822 // if this is > 0, the control is frozen and doesn't redraw itself
823 size_t m_freezeCount
;
825 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
826 DECLARE_EVENT_TABLE()
828 friend class wxGenericListCtrl
;
831 // ============================================================================
833 // ============================================================================
835 //-----------------------------------------------------------------------------
837 //-----------------------------------------------------------------------------
839 wxListItemData::~wxListItemData()
841 // in the virtual list control the attributes are managed by the main
842 // program, so don't delete them
843 if ( !m_owner
->IsVirtual() )
851 void wxListItemData::Init()
859 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
865 if ( owner
->InReportView() )
875 void wxListItemData::SetItem( const wxListItem
&info
)
877 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
878 SetText(info
.m_text
);
879 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
880 m_image
= info
.m_image
;
881 if ( info
.m_mask
& wxLIST_MASK_DATA
)
882 m_data
= info
.m_data
;
884 if ( info
.HasAttributes() )
887 *m_attr
= *info
.GetAttributes();
889 m_attr
= new wxListItemAttr(*info
.GetAttributes());
897 m_rect
->width
= info
.m_width
;
901 void wxListItemData::SetPosition( int x
, int y
)
903 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
909 void wxListItemData::SetSize( int width
, int height
)
911 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
914 m_rect
->width
= width
;
916 m_rect
->height
= height
;
919 bool wxListItemData::IsHit( int x
, int y
) const
921 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
923 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
926 int wxListItemData::GetX() const
928 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
933 int wxListItemData::GetY() const
935 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
940 int wxListItemData::GetWidth() const
942 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
944 return m_rect
->width
;
947 int wxListItemData::GetHeight() const
949 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
951 return m_rect
->height
;
954 void wxListItemData::GetItem( wxListItem
&info
) const
956 info
.m_text
= m_text
;
957 info
.m_image
= m_image
;
958 info
.m_data
= m_data
;
962 if ( m_attr
->HasTextColour() )
963 info
.SetTextColour(m_attr
->GetTextColour());
964 if ( m_attr
->HasBackgroundColour() )
965 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
966 if ( m_attr
->HasFont() )
967 info
.SetFont(m_attr
->GetFont());
971 //-----------------------------------------------------------------------------
973 //-----------------------------------------------------------------------------
975 void wxListHeaderData::Init()
986 wxListHeaderData::wxListHeaderData()
991 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
998 void wxListHeaderData::SetItem( const wxListItem
&item
)
1000 m_mask
= item
.m_mask
;
1002 if ( m_mask
& wxLIST_MASK_TEXT
)
1003 m_text
= item
.m_text
;
1005 if ( m_mask
& wxLIST_MASK_IMAGE
)
1006 m_image
= item
.m_image
;
1008 if ( m_mask
& wxLIST_MASK_FORMAT
)
1009 m_format
= item
.m_format
;
1011 if ( m_mask
& wxLIST_MASK_WIDTH
)
1012 SetWidth(item
.m_width
);
1015 void wxListHeaderData::SetPosition( int x
, int y
)
1021 void wxListHeaderData::SetHeight( int h
)
1026 void wxListHeaderData::SetWidth( int w
)
1030 m_width
= WIDTH_COL_DEFAULT
;
1031 else if (m_width
< WIDTH_COL_MIN
)
1032 m_width
= WIDTH_COL_MIN
;
1035 void wxListHeaderData::SetFormat( int format
)
1040 bool wxListHeaderData::HasImage() const
1042 return m_image
!= -1;
1045 bool wxListHeaderData::IsHit( int x
, int y
) const
1047 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1050 void wxListHeaderData::GetItem( wxListItem
& item
)
1052 item
.m_mask
= m_mask
;
1053 item
.m_text
= m_text
;
1054 item
.m_image
= m_image
;
1055 item
.m_format
= m_format
;
1056 item
.m_width
= m_width
;
1059 int wxListHeaderData::GetImage() const
1064 int wxListHeaderData::GetWidth() const
1069 int wxListHeaderData::GetFormat() const
1074 //-----------------------------------------------------------------------------
1076 //-----------------------------------------------------------------------------
1078 inline int wxListLineData::GetMode() const
1080 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1083 inline bool wxListLineData::InReportView() const
1085 return m_owner
->HasFlag(wxLC_REPORT
);
1088 inline bool wxListLineData::IsVirtual() const
1090 return m_owner
->IsVirtual();
1093 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1097 if ( InReportView() )
1103 m_gi
= new GeometryInfo
;
1106 m_highlighted
= FALSE
;
1108 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1111 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1113 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1114 wxCHECK_RET( node
, _T("no subitems at all??") );
1116 wxListItemData
*item
= node
->GetData();
1118 switch ( GetMode() )
1121 case wxLC_SMALL_ICON
:
1123 m_gi
->m_rectAll
.width
= spacing
;
1125 wxString s
= item
->GetText();
1131 m_gi
->m_rectLabel
.width
=
1132 m_gi
->m_rectLabel
.height
= 0;
1136 dc
->GetTextExtent( s
, &lw
, &lh
);
1137 if (lh
< SCROLL_UNIT_Y
)
1142 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1144 m_gi
->m_rectAll
.width
= lw
;
1146 m_gi
->m_rectLabel
.width
= lw
;
1147 m_gi
->m_rectLabel
.height
= lh
;
1150 if (item
->HasImage())
1153 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1154 m_gi
->m_rectIcon
.width
= w
+ 8;
1155 m_gi
->m_rectIcon
.height
= h
+ 8;
1157 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1158 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1159 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1160 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1163 if ( item
->HasText() )
1165 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1166 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1168 else // no text, highlight the icon
1170 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1171 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1178 wxString s
= item
->GetTextForMeasuring();
1181 dc
->GetTextExtent( s
, &lw
, &lh
);
1182 if (lh
< SCROLL_UNIT_Y
)
1187 m_gi
->m_rectLabel
.width
= lw
;
1188 m_gi
->m_rectLabel
.height
= lh
;
1190 m_gi
->m_rectAll
.width
= lw
;
1191 m_gi
->m_rectAll
.height
= lh
;
1193 if (item
->HasImage())
1196 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1197 m_gi
->m_rectIcon
.width
= w
;
1198 m_gi
->m_rectIcon
.height
= h
;
1200 m_gi
->m_rectAll
.width
+= 4 + w
;
1201 if (h
> m_gi
->m_rectAll
.height
)
1202 m_gi
->m_rectAll
.height
= h
;
1205 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1206 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1211 wxFAIL_MSG( _T("unexpected call to SetSize") );
1215 wxFAIL_MSG( _T("unknown mode") );
1219 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1221 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1222 wxCHECK_RET( node
, _T("no subitems at all??") );
1224 wxListItemData
*item
= node
->GetData();
1226 switch ( GetMode() )
1229 case wxLC_SMALL_ICON
:
1230 m_gi
->m_rectAll
.x
= x
;
1231 m_gi
->m_rectAll
.y
= y
;
1233 if ( item
->HasImage() )
1235 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1236 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1237 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1240 if ( item
->HasText() )
1242 if (m_gi
->m_rectAll
.width
> spacing
)
1243 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1245 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1246 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1247 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1248 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1250 else // no text, highlight the icon
1252 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1253 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1258 m_gi
->m_rectAll
.x
= x
;
1259 m_gi
->m_rectAll
.y
= y
;
1261 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1262 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1263 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1265 if (item
->HasImage())
1267 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1268 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1269 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1273 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1278 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1282 wxFAIL_MSG( _T("unknown mode") );
1286 void wxListLineData::InitItems( int num
)
1288 for (int i
= 0; i
< num
; i
++)
1289 m_items
.Append( new wxListItemData(m_owner
) );
1292 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1294 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1295 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1297 wxListItemData
*item
= node
->GetData();
1298 item
->SetItem( info
);
1301 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1303 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1306 wxListItemData
*item
= node
->GetData();
1307 item
->GetItem( info
);
1311 wxString
wxListLineData::GetText(int index
) const
1315 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1318 wxListItemData
*item
= node
->GetData();
1319 s
= item
->GetText();
1325 void wxListLineData::SetText( int index
, const wxString s
)
1327 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1330 wxListItemData
*item
= node
->GetData();
1335 void wxListLineData::SetImage( int index
, int image
)
1337 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1338 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1340 wxListItemData
*item
= node
->GetData();
1341 item
->SetImage(image
);
1344 int wxListLineData::GetImage( int index
) const
1346 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1347 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1349 wxListItemData
*item
= node
->GetData();
1350 return item
->GetImage();
1353 wxListItemAttr
*wxListLineData::GetAttr() const
1355 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1356 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1358 wxListItemData
*item
= node
->GetData();
1359 return item
->GetAttr();
1362 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1364 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1365 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1367 wxListItemData
*item
= node
->GetData();
1368 item
->SetAttr(attr
);
1371 bool wxListLineData::SetAttributes(wxDC
*dc
,
1372 const wxListItemAttr
*attr
,
1375 wxWindow
*listctrl
= m_owner
->GetParent();
1379 // don't use foreground colour for drawing highlighted items - this might
1380 // make them completely invisible (and there is no way to do bit
1381 // arithmetics on wxColour, unfortunately)
1385 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1389 if ( attr
&& attr
->HasTextColour() )
1391 colText
= attr
->GetTextColour();
1395 colText
= listctrl
->GetForegroundColour();
1399 dc
->SetTextForeground(colText
);
1403 if ( attr
&& attr
->HasFont() )
1405 font
= attr
->GetFont();
1409 font
= listctrl
->GetFont();
1415 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1416 if ( highlighted
|| hasBgCol
)
1420 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1424 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1427 dc
->SetPen( *wxTRANSPARENT_PEN
);
1435 void wxListLineData::Draw( wxDC
*dc
)
1437 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1438 wxCHECK_RET( node
, _T("no subitems at all??") );
1440 bool highlighted
= IsHighlighted();
1442 wxListItemAttr
*attr
= GetAttr();
1444 if ( SetAttributes(dc
, attr
, highlighted
) )
1446 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1449 wxListItemData
*item
= node
->GetData();
1450 if (item
->HasImage())
1452 wxRect rectIcon
= m_gi
->m_rectIcon
;
1453 m_owner
->DrawImage( item
->GetImage(), dc
,
1454 rectIcon
.x
, rectIcon
.y
);
1457 if (item
->HasText())
1459 wxRect rectLabel
= m_gi
->m_rectLabel
;
1461 wxDCClipper
clipper(*dc
, rectLabel
);
1462 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1466 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1468 const wxRect
& rectHL
,
1471 // TODO: later we should support setting different attributes for
1472 // different columns - to do it, just add "col" argument to
1473 // GetAttr() and move these lines into the loop below
1474 wxListItemAttr
*attr
= GetAttr();
1475 if ( SetAttributes(dc
, attr
, highlighted
) )
1477 dc
->DrawRectangle( rectHL
);
1480 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1481 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1484 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1486 node
= node
->GetNext(), col
++ )
1488 wxListItemData
*item
= node
->GetData();
1490 int width
= m_owner
->GetColumnWidth(col
);
1494 if ( item
->HasImage() )
1497 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1498 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1500 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1506 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1508 if ( item
->HasText() )
1510 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1515 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1516 const wxString
&text
,
1522 wxString drawntext
, ellipsis
;
1523 wxCoord w
, h
, base_w
;
1526 // determine if the string can fit inside the current width
1527 dc
->GetTextExtent(text
, &w
, &h
);
1530 // it can, draw it using the items alignment
1531 m_owner
->GetColumn(col
, item
);
1532 switch ( item
.GetAlign() )
1535 wxFAIL_MSG( _T("unknown list item format") );
1538 case wxLIST_FORMAT_LEFT
:
1542 case wxLIST_FORMAT_RIGHT
:
1546 case wxLIST_FORMAT_CENTER
:
1547 x
+= (width
- w
) / 2;
1551 dc
->DrawText(text
, x
, y
);
1553 else // otherwise, truncate and add an ellipsis if possible
1555 // determine the base width
1556 ellipsis
= wxString(wxT("..."));
1557 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1559 // continue until we have enough space or only one character left
1561 size_t len
= text
.Length();
1562 drawntext
= text
.Left(len
);
1565 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1566 drawntext
.RemoveLast();
1569 if (w
+ base_w
<= width
)
1573 // if still not enough space, remove ellipsis characters
1574 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1576 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1577 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1580 // now draw the text
1581 dc
->DrawText(drawntext
, x
, y
);
1582 dc
->DrawText(ellipsis
, x
+ w
, y
);
1586 bool wxListLineData::Highlight( bool on
)
1588 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1590 if ( on
== m_highlighted
)
1598 void wxListLineData::ReverseHighlight( void )
1600 Highlight(!IsHighlighted());
1603 //-----------------------------------------------------------------------------
1604 // wxListHeaderWindow
1605 //-----------------------------------------------------------------------------
1607 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1609 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1610 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1611 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1612 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1615 void wxListHeaderWindow::Init()
1617 m_currentCursor
= (wxCursor
*) NULL
;
1618 m_isDragging
= FALSE
;
1622 wxListHeaderWindow::wxListHeaderWindow()
1626 m_owner
= (wxListMainWindow
*) NULL
;
1627 m_resizeCursor
= (wxCursor
*) NULL
;
1630 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1632 wxListMainWindow
*owner
,
1636 const wxString
&name
)
1637 : wxWindow( win
, id
, pos
, size
, style
, name
)
1642 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1644 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1647 wxListHeaderWindow::~wxListHeaderWindow()
1649 delete m_resizeCursor
;
1652 #ifdef __WXUNIVERSAL__
1653 #include "wx/univ/renderer.h"
1654 #include "wx/univ/theme.h"
1657 // shift the DC origin to match the position of the main window horz
1658 // scrollbar: this allows us to always use logical coords
1659 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1662 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1665 m_owner
->GetViewStart( &x
, NULL
);
1667 // account for the horz scrollbar offset
1668 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1671 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1673 wxPaintDC
dc( this );
1680 dc
.SetFont( GetFont() );
1682 // width and height of the entire header window
1684 GetClientSize( &w
, &h
);
1685 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1687 dc
.SetBackgroundMode(wxTRANSPARENT
);
1689 // do *not* use the listctrl colour for headers - one day we will have a
1690 // function to set it separately
1691 //dc.SetTextForeground( *wxBLACK );
1692 dc
.SetTextForeground(wxSystemSettings::
1693 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1695 int x
= HEADER_OFFSET_X
;
1697 int numColumns
= m_owner
->GetColumnCount();
1699 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1701 m_owner
->GetColumn( i
, item
);
1702 int wCol
= item
.m_width
;
1704 // the width of the rect to draw: make it smaller to fit entirely
1705 // inside the column rect
1708 wxRendererNative::Get().DrawHeaderButton
1712 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1713 m_parent
->IsEnabled() ? 0
1714 : wxCONTROL_DISABLED
1717 // see if we have enough space for the column label
1719 // for this we need the width of the text
1721 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1722 wLabel
+= 2*EXTRA_WIDTH
;
1724 // and the width of the icon, if any
1725 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1726 int ix
= 0, // init them just to suppress the compiler warnings
1728 const int image
= item
.m_image
;
1729 wxImageListType
*imageList
;
1732 imageList
= m_owner
->m_small_image_list
;
1735 imageList
->GetSize(image
, ix
, iy
);
1736 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1744 // ignore alignment if there is not enough space anyhow
1746 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1749 wxFAIL_MSG( _T("unknown list item format") );
1752 case wxLIST_FORMAT_LEFT
:
1756 case wxLIST_FORMAT_RIGHT
:
1757 xAligned
= x
+ cw
- wLabel
;
1760 case wxLIST_FORMAT_CENTER
:
1761 xAligned
= x
+ (cw
- wLabel
) / 2;
1766 // if we have an image, draw it on the right of the label
1773 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1774 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1775 wxIMAGELIST_DRAW_TRANSPARENT
1778 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1781 // draw the text clipping it so that it doesn't overwrite the column
1783 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1785 dc
.DrawText( item
.GetText(),
1786 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1794 void wxListHeaderWindow::DrawCurrent()
1796 int x1
= m_currentX
;
1798 m_owner
->ClientToScreen( &x1
, &y1
);
1800 int x2
= m_currentX
;
1802 m_owner
->GetClientSize( NULL
, &y2
);
1803 m_owner
->ClientToScreen( &x2
, &y2
);
1806 dc
.SetLogicalFunction( wxINVERT
);
1807 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1808 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1812 dc
.DrawLine( x1
, y1
, x2
, y2
);
1814 dc
.SetLogicalFunction( wxCOPY
);
1816 dc
.SetPen( wxNullPen
);
1817 dc
.SetBrush( wxNullBrush
);
1820 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1822 // we want to work with logical coords
1824 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1825 int y
= event
.GetY();
1829 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1831 // we don't draw the line beyond our window, but we allow dragging it
1834 GetClientSize( &w
, NULL
);
1835 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1838 // erase the line if it was drawn
1839 if ( m_currentX
< w
)
1842 if (event
.ButtonUp())
1845 m_isDragging
= FALSE
;
1847 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1848 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1855 m_currentX
= m_minX
+ 7;
1857 // draw in the new location
1858 if ( m_currentX
< w
)
1862 else // not dragging
1865 bool hit_border
= FALSE
;
1867 // end of the current column
1870 // find the column where this event occured
1872 countCol
= m_owner
->GetColumnCount();
1873 for (col
= 0; col
< countCol
; col
++)
1875 xpos
+= m_owner
->GetColumnWidth( col
);
1878 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1880 // near the column border
1887 // inside the column
1894 if ( col
== countCol
)
1897 if (event
.LeftDown() || event
.RightUp())
1899 if (hit_border
&& event
.LeftDown())
1901 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1902 event
.GetPosition()) )
1904 m_isDragging
= TRUE
;
1909 //else: column resizing was vetoed by the user code
1911 else // click on a column
1913 SendListEvent( event
.LeftDown()
1914 ? wxEVT_COMMAND_LIST_COL_CLICK
1915 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1916 event
.GetPosition());
1919 else if (event
.Moving())
1924 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1925 m_currentCursor
= m_resizeCursor
;
1929 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1930 m_currentCursor
= wxSTANDARD_CURSOR
;
1934 SetCursor(*m_currentCursor
);
1939 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1941 m_owner
->SetFocus();
1944 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1946 wxWindow
*parent
= GetParent();
1947 wxListEvent
le( type
, parent
->GetId() );
1948 le
.SetEventObject( parent
);
1949 le
.m_pointDrag
= pos
;
1951 // the position should be relative to the parent window, not
1952 // this one for compatibility with MSW and common sense: the
1953 // user code doesn't know anything at all about this header
1954 // window, so why should it get positions relative to it?
1955 le
.m_pointDrag
.y
-= GetSize().y
;
1957 le
.m_col
= m_column
;
1958 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1961 //-----------------------------------------------------------------------------
1962 // wxListRenameTimer (internal)
1963 //-----------------------------------------------------------------------------
1965 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1970 void wxListRenameTimer::Notify()
1972 m_owner
->OnRenameTimer();
1975 //-----------------------------------------------------------------------------
1976 // wxListTextCtrl (internal)
1977 //-----------------------------------------------------------------------------
1979 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
1980 EVT_CHAR (wxListTextCtrl::OnChar
)
1981 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
1982 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
1985 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
1986 : m_startValue(owner
->GetItemText(itemEdit
)),
1987 m_itemEdited(itemEdit
)
1992 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1994 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1995 &rectLabel
.x
, &rectLabel
.y
);
1997 (void)Create(owner
, wxID_ANY
, m_startValue
,
1998 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1999 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2002 void wxListTextCtrl::Finish()
2006 wxPendingDelete
.Append(this);
2010 m_owner
->SetFocus();
2014 bool wxListTextCtrl::AcceptChanges()
2016 const wxString value
= GetValue();
2018 if ( value
== m_startValue
)
2020 // nothing changed, always accept
2024 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2026 // vetoed by the user
2030 // accepted, do rename the item
2031 m_owner
->SetItemText(m_itemEdited
, value
);
2036 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2038 switch ( event
.m_keyCode
)
2041 if ( !AcceptChanges() )
2043 // vetoed by the user code
2046 //else: fall through
2050 m_owner
->OnRenameCancelled( m_itemEdited
);
2058 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2066 // auto-grow the textctrl:
2067 wxSize parentSize
= m_owner
->GetSize();
2068 wxPoint myPos
= GetPosition();
2069 wxSize mySize
= GetSize();
2071 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2072 if (myPos
.x
+ sx
> parentSize
.x
)
2073 sx
= parentSize
.x
- myPos
.x
;
2081 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2085 // We must finish regardless of success, otherwise we'll get focus problems
2088 if ( !AcceptChanges() )
2089 m_owner
->OnRenameCancelled( m_itemEdited
);
2095 //-----------------------------------------------------------------------------
2097 //-----------------------------------------------------------------------------
2099 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2101 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2102 EVT_PAINT (wxListMainWindow::OnPaint
)
2103 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2104 EVT_CHAR (wxListMainWindow::OnChar
)
2105 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2106 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2107 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2108 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2111 void wxListMainWindow::Init()
2116 m_lineTo
= (size_t)-1;
2122 m_small_image_list
= (wxImageListType
*) NULL
;
2123 m_normal_image_list
= (wxImageListType
*) NULL
;
2125 m_small_spacing
= 30;
2126 m_normal_spacing
= 40;
2130 m_isCreated
= FALSE
;
2132 m_lastOnSame
= FALSE
;
2133 m_renameTimer
= new wxListRenameTimer( this );
2137 m_lineBeforeLastClicked
= (size_t)-1;
2142 wxListMainWindow::wxListMainWindow()
2147 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2150 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2155 const wxString
&name
)
2156 : wxScrolledWindow( parent
, id
, pos
, size
,
2157 style
| wxHSCROLL
| wxVSCROLL
, name
)
2161 m_highlightBrush
= new wxBrush
2163 wxSystemSettings::GetColour
2165 wxSYS_COLOUR_HIGHLIGHT
2170 m_highlightUnfocusedBrush
= new wxBrush
2172 wxSystemSettings::GetColour
2174 wxSYS_COLOUR_BTNSHADOW
2182 SetScrollbars( SCROLL_UNIT_X
, SCROLL_UNIT_Y
, 0, 0, 0, 0 );
2184 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2187 wxListMainWindow::~wxListMainWindow()
2190 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2192 delete m_highlightBrush
;
2193 delete m_highlightUnfocusedBrush
;
2195 delete m_renameTimer
;
2198 void wxListMainWindow::CacheLineData(size_t line
)
2200 wxGenericListCtrl
*listctrl
= GetListCtrl();
2202 wxListLineData
*ld
= GetDummyLine();
2204 size_t countCol
= GetColumnCount();
2205 for ( size_t col
= 0; col
< countCol
; col
++ )
2207 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2210 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2211 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2214 wxListLineData
*wxListMainWindow::GetDummyLine() const
2216 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2218 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2220 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2222 // we need to recreate the dummy line if the number of columns in the
2223 // control changed as it would have the incorrect number of fields
2225 if ( !m_lines
.IsEmpty() &&
2226 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2228 self
->m_lines
.Clear();
2231 if ( m_lines
.IsEmpty() )
2233 wxListLineData
*line
= new wxListLineData(self
);
2234 self
->m_lines
.Add(line
);
2236 // don't waste extra memory -- there never going to be anything
2237 // else/more in this array
2238 self
->m_lines
.Shrink();
2244 // ----------------------------------------------------------------------------
2245 // line geometry (report mode only)
2246 // ----------------------------------------------------------------------------
2248 wxCoord
wxListMainWindow::GetLineHeight() const
2250 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2252 // we cache the line height as calling GetTextExtent() is slow
2253 if ( !m_lineHeight
)
2255 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2257 wxClientDC
dc( self
);
2258 dc
.SetFont( GetFont() );
2261 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2263 if ( y
< SCROLL_UNIT_Y
)
2266 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2270 m_small_image_list
->GetSize(0, iw
, ih
);
2275 self
->m_lineHeight
= y
+ LINE_SPACING
;
2278 return m_lineHeight
;
2281 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2283 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2285 return LINE_SPACING
+ line
*GetLineHeight();
2288 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2290 if ( !InReportView() )
2291 return GetLine(line
)->m_gi
->m_rectAll
;
2294 rect
.x
= HEADER_OFFSET_X
;
2295 rect
.y
= GetLineY(line
);
2296 rect
.width
= GetHeaderWidth();
2297 rect
.height
= GetLineHeight();
2302 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2304 if ( !InReportView() )
2305 return GetLine(line
)->m_gi
->m_rectLabel
;
2308 rect
.x
= HEADER_OFFSET_X
;
2309 rect
.y
= GetLineY(line
);
2310 rect
.width
= GetColumnWidth(0);
2311 rect
.height
= GetLineHeight();
2316 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2318 if ( !InReportView() )
2319 return GetLine(line
)->m_gi
->m_rectIcon
;
2321 wxListLineData
*ld
= GetLine(line
);
2322 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2325 rect
.x
= HEADER_OFFSET_X
;
2326 rect
.y
= GetLineY(line
);
2327 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2332 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2334 return InReportView() ? GetLineRect(line
)
2335 : GetLine(line
)->m_gi
->m_rectHighlight
;
2338 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2340 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2342 wxListLineData
*ld
= GetLine(line
);
2344 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2345 return wxLIST_HITTEST_ONITEMICON
;
2347 // VS: Testing for "ld->HasText() || InReportView()" instead of
2348 // "ld->HasText()" is needed to make empty lines in report view
2350 if ( ld
->HasText() || InReportView() )
2352 wxRect rect
= InReportView() ? GetLineRect(line
)
2353 : GetLineLabelRect(line
);
2355 if ( rect
.Inside(x
, y
) )
2356 return wxLIST_HITTEST_ONITEMLABEL
;
2362 // ----------------------------------------------------------------------------
2363 // highlight (selection) handling
2364 // ----------------------------------------------------------------------------
2366 bool wxListMainWindow::IsHighlighted(size_t line
) const
2370 return m_selStore
.IsSelected(line
);
2374 wxListLineData
*ld
= GetLine(line
);
2375 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2377 return ld
->IsHighlighted();
2381 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2387 wxArrayInt linesChanged
;
2388 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2391 // meny items changed state, refresh everything
2392 RefreshLines(lineFrom
, lineTo
);
2394 else // only a few items changed state, refresh only them
2396 size_t count
= linesChanged
.GetCount();
2397 for ( size_t n
= 0; n
< count
; n
++ )
2399 RefreshLine(linesChanged
[n
]);
2403 else // iterate over all items in non report view
2405 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2407 if ( HighlightLine(line
, highlight
) )
2415 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2421 changed
= m_selStore
.SelectItem(line
, highlight
);
2425 wxListLineData
*ld
= GetLine(line
);
2426 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2428 changed
= ld
->Highlight(highlight
);
2433 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2434 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2440 void wxListMainWindow::RefreshLine( size_t line
)
2442 if ( HasFlag(wxLC_REPORT
) )
2444 size_t visibleFrom
, visibleTo
;
2445 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2447 if ( line
< visibleFrom
|| line
> visibleTo
)
2451 wxRect rect
= GetLineRect(line
);
2453 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2454 RefreshRect( rect
);
2457 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2459 // we suppose that they are ordered by caller
2460 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2462 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2464 if ( HasFlag(wxLC_REPORT
) )
2466 size_t visibleFrom
, visibleTo
;
2467 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2469 if ( lineFrom
< visibleFrom
)
2470 lineFrom
= visibleFrom
;
2471 if ( lineTo
> visibleTo
)
2476 rect
.y
= GetLineY(lineFrom
);
2477 rect
.width
= GetClientSize().x
;
2478 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2480 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2481 RefreshRect( rect
);
2485 // TODO: this should be optimized...
2486 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2493 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2495 if ( HasFlag(wxLC_REPORT
) )
2497 size_t visibleFrom
, visibleTo
;
2498 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2500 if ( lineFrom
< visibleFrom
)
2501 lineFrom
= visibleFrom
;
2502 else if ( lineFrom
> visibleTo
)
2507 rect
.y
= GetLineY(lineFrom
);
2508 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2510 wxSize size
= GetClientSize();
2511 rect
.width
= size
.x
;
2512 // refresh till the bottom of the window
2513 rect
.height
= size
.y
- rect
.y
;
2515 RefreshRect( rect
);
2519 // TODO: how to do it more efficiently?
2524 void wxListMainWindow::RefreshSelected()
2530 if ( InReportView() )
2532 GetVisibleLinesRange(&from
, &to
);
2537 to
= GetItemCount() - 1;
2540 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2542 RefreshLine(m_current
);
2545 for ( size_t line
= from
; line
<= to
; line
++ )
2547 // NB: the test works as expected even if m_current == -1
2548 if ( line
!= m_current
&& IsHighlighted(line
) )
2555 void wxListMainWindow::Freeze()
2560 void wxListMainWindow::Thaw()
2562 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2564 if ( !--m_freezeCount
)
2570 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2572 // Note: a wxPaintDC must be constructed even if no drawing is
2573 // done (a Windows requirement).
2574 wxPaintDC
dc( this );
2576 if ( IsEmpty() || m_freezeCount
)
2578 // nothing to draw or not the moment to draw it
2584 // delay the repainting until we calculate all the items positions
2591 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2595 dc
.SetFont( GetFont() );
2597 if ( HasFlag(wxLC_REPORT
) )
2599 int lineHeight
= GetLineHeight();
2601 size_t visibleFrom
, visibleTo
;
2602 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2605 wxCoord xOrig
, yOrig
;
2606 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2608 // tell the caller cache to cache the data
2611 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2612 GetParent()->GetId());
2613 evCache
.SetEventObject( GetParent() );
2614 evCache
.m_oldItemIndex
= visibleFrom
;
2615 evCache
.m_itemIndex
= visibleTo
;
2616 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2619 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2621 rectLine
= GetLineRect(line
);
2623 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2624 rectLine
.width
, rectLine
.height
) )
2626 // don't redraw unaffected lines to avoid flicker
2630 GetLine(line
)->DrawInReportMode( &dc
,
2632 GetLineHighlightRect(line
),
2633 IsHighlighted(line
) );
2636 if ( HasFlag(wxLC_HRULES
) )
2638 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2639 wxSize clientSize
= GetClientSize();
2641 // Don't draw the first one
2642 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2645 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2646 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2647 clientSize
.x
- dev_x
, i
*lineHeight
);
2650 // Draw last horizontal rule
2651 if ( visibleTo
== GetItemCount() - 1 )
2654 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2655 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2656 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2660 // Draw vertical rules if required
2661 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2663 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2666 wxRect firstItemRect
;
2667 wxRect lastItemRect
;
2668 GetItemRect(visibleFrom
, firstItemRect
);
2669 GetItemRect(visibleTo
, lastItemRect
);
2670 int x
= firstItemRect
.GetX();
2672 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2673 for (col
= 0; col
< GetColumnCount(); col
++)
2675 int colWidth
= GetColumnWidth(col
);
2677 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2678 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2684 size_t count
= GetItemCount();
2685 for ( size_t i
= 0; i
< count
; i
++ )
2687 GetLine(i
)->Draw( &dc
);
2693 // don't draw rect outline under Max if we already have the background
2694 // color but under other platforms only draw it if we do: it is a bit
2695 // silly to draw "focus rect" if we don't have focus!
2700 #endif // __WXMAC__/!__WXMAC__
2702 dc
.SetPen( *wxBLACK_PEN
);
2703 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2704 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2711 void wxListMainWindow::HighlightAll( bool on
)
2713 if ( IsSingleSel() )
2715 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2717 // we just have one item to turn off
2718 if ( HasCurrent() && IsHighlighted(m_current
) )
2720 HighlightLine(m_current
, FALSE
);
2721 RefreshLine(m_current
);
2726 HighlightLines(0, GetItemCount() - 1, on
);
2730 void wxListMainWindow::SendNotify( size_t line
,
2731 wxEventType command
,
2734 wxListEvent
le( command
, GetParent()->GetId() );
2735 le
.SetEventObject( GetParent() );
2736 le
.m_itemIndex
= line
;
2738 // set only for events which have position
2739 if ( point
!= wxDefaultPosition
)
2740 le
.m_pointDrag
= point
;
2742 // don't try to get the line info for virtual list controls: the main
2743 // program has it anyhow and if we did it would result in accessing all
2744 // the lines, even those which are not visible now and this is precisely
2745 // what we're trying to avoid
2746 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2748 if ( line
!= (size_t)-1 )
2750 GetLine(line
)->GetItem( 0, le
.m_item
);
2752 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2754 //else: there may be no more such item
2756 GetParent()->GetEventHandler()->ProcessEvent( le
);
2759 void wxListMainWindow::ChangeCurrent(size_t current
)
2761 m_current
= current
;
2763 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2766 void wxListMainWindow::EditLabel( long item
)
2768 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2769 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2771 size_t itemEdit
= (size_t)item
;
2773 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2774 le
.SetEventObject( GetParent() );
2775 le
.m_itemIndex
= item
;
2776 wxListLineData
*data
= GetLine(itemEdit
);
2777 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2778 data
->GetItem( 0, le
.m_item
);
2779 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2781 // vetoed by user code
2785 // We have to call this here because the label in question might just have
2786 // been added and no screen update taken place.
2790 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2795 void wxListMainWindow::OnRenameTimer()
2797 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2799 EditLabel( m_current
);
2802 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2804 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2805 le
.SetEventObject( GetParent() );
2806 le
.m_itemIndex
= itemEdit
;
2808 wxListLineData
*data
= GetLine(itemEdit
);
2809 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2811 data
->GetItem( 0, le
.m_item
);
2812 le
.m_item
.m_text
= value
;
2813 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2817 #ifdef __VMS__ // Ignore unreacheable code
2818 # pragma message disable initnotreach
2821 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2823 // wxMSW seems not to notify the program about
2824 // cancelled label edits.
2827 // let owner know that the edit was cancelled
2828 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2830 // These only exist for wxTreeCtrl, which should probably be changed
2831 // le.m_editCancelled = TRUE;
2832 // le.m_label = wxEmptyString;
2834 le
.SetEventObject( GetParent() );
2835 le
.m_itemIndex
= itemEdit
;
2837 wxListLineData
*data
= GetLine(itemEdit
);
2838 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2840 data
->GetItem( 0, le
.m_item
);
2842 GetEventHandler()->ProcessEvent( le
);
2845 # pragma message enable initnotreach
2848 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2850 event
.SetEventObject( GetParent() );
2851 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2854 if ( !HasCurrent() || IsEmpty() )
2860 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2861 event
.ButtonDClick()) )
2864 int x
= event
.GetX();
2865 int y
= event
.GetY();
2866 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2868 // where did we hit it (if we did)?
2871 size_t count
= GetItemCount(),
2874 if ( HasFlag(wxLC_REPORT
) )
2876 current
= y
/ GetLineHeight();
2877 if ( current
< count
)
2878 hitResult
= HitTestLine(current
, x
, y
);
2882 // TODO: optimize it too! this is less simple than for report view but
2883 // enumerating all items is still not a way to do it!!
2884 for ( current
= 0; current
< count
; current
++ )
2886 hitResult
= HitTestLine(current
, x
, y
);
2892 if (event
.Dragging())
2894 if (m_dragCount
== 0)
2896 // we have to report the raw, physical coords as we want to be
2897 // able to call HitTest(event.m_pointDrag) from the user code to
2898 // get the item being dragged
2899 m_dragStart
= event
.GetPosition();
2904 if (m_dragCount
!= 3)
2907 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2908 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2910 wxListEvent
le( command
, GetParent()->GetId() );
2911 le
.SetEventObject( GetParent() );
2912 le
.m_itemIndex
= current
;
2913 le
.m_pointDrag
= m_dragStart
;
2914 GetParent()->GetEventHandler()->ProcessEvent( le
);
2925 // outside of any item
2929 bool forceClick
= FALSE
;
2930 if (event
.ButtonDClick())
2932 m_renameTimer
->Stop();
2933 m_lastOnSame
= FALSE
;
2935 if ( current
== m_lineLastClicked
)
2937 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2943 // the first click was on another item, so don't interpret this as
2944 // a double click, but as a simple click instead
2949 if (event
.LeftUp() && m_lastOnSame
)
2951 if ((current
== m_current
) &&
2952 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2953 HasFlag(wxLC_EDIT_LABELS
) )
2955 m_renameTimer
->Start( 100, TRUE
);
2957 m_lastOnSame
= FALSE
;
2959 else if (event
.RightDown())
2961 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2962 event
.GetPosition() );
2964 else if (event
.MiddleDown())
2966 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2968 else if ( event
.LeftDown() || forceClick
)
2970 m_lineBeforeLastClicked
= m_lineLastClicked
;
2971 m_lineLastClicked
= current
;
2973 size_t oldCurrent
= m_current
;
2975 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
2977 HighlightAll( FALSE
);
2979 ChangeCurrent(current
);
2981 ReverseHighlight(m_current
);
2983 else // multi sel & either ctrl or shift is down
2985 if (event
.ControlDown())
2987 ChangeCurrent(current
);
2989 ReverseHighlight(m_current
);
2991 else if (event
.ShiftDown())
2993 ChangeCurrent(current
);
2995 size_t lineFrom
= oldCurrent
,
2998 if ( lineTo
< lineFrom
)
3001 lineFrom
= m_current
;
3004 HighlightLines(lineFrom
, lineTo
);
3006 else // !ctrl, !shift
3008 // test in the enclosing if should make it impossible
3009 wxFAIL_MSG( _T("how did we get here?") );
3013 if (m_current
!= oldCurrent
)
3015 RefreshLine( oldCurrent
);
3018 // forceClick is only set if the previous click was on another item
3019 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3023 void wxListMainWindow::MoveToItem(size_t item
)
3025 if ( item
== (size_t)-1 )
3028 wxRect rect
= GetLineRect(item
);
3030 int client_w
, client_h
;
3031 GetClientSize( &client_w
, &client_h
);
3033 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3034 int view_y
= SCROLL_UNIT_Y
*GetScrollPos( wxVERTICAL
);
3036 if ( HasFlag(wxLC_REPORT
) )
3038 // the next we need the range of lines shown it might be different, so
3040 ResetVisibleLinesRange();
3042 if (rect
.y
< view_y
)
3043 Scroll( -1, rect
.y
/SCROLL_UNIT_Y
);
3044 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3045 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/SCROLL_UNIT_Y
);
3049 if (rect
.x
-view_x
< 5)
3050 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3051 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3052 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3056 // ----------------------------------------------------------------------------
3057 // keyboard handling
3058 // ----------------------------------------------------------------------------
3060 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3062 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3063 _T("invalid item index in OnArrowChar()") );
3065 size_t oldCurrent
= m_current
;
3067 // in single selection we just ignore Shift as we can't select several
3069 if ( event
.ShiftDown() && !IsSingleSel() )
3071 ChangeCurrent(newCurrent
);
3073 // select all the items between the old and the new one
3074 if ( oldCurrent
> newCurrent
)
3076 newCurrent
= oldCurrent
;
3077 oldCurrent
= m_current
;
3080 HighlightLines(oldCurrent
, newCurrent
);
3084 // all previously selected items are unselected unless ctrl is held
3085 if ( !event
.ControlDown() )
3086 HighlightAll(FALSE
);
3088 ChangeCurrent(newCurrent
);
3090 // refresh the old focus to remove it
3091 RefreshLine( oldCurrent
);
3093 if ( !event
.ControlDown() )
3095 HighlightLine( m_current
, TRUE
);
3099 RefreshLine( m_current
);
3104 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3106 wxWindow
*parent
= GetParent();
3108 /* we propagate the key event up */
3109 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3110 ke
.m_shiftDown
= event
.m_shiftDown
;
3111 ke
.m_controlDown
= event
.m_controlDown
;
3112 ke
.m_altDown
= event
.m_altDown
;
3113 ke
.m_metaDown
= event
.m_metaDown
;
3114 ke
.m_keyCode
= event
.m_keyCode
;
3117 ke
.SetEventObject( parent
);
3118 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3123 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3125 wxWindow
*parent
= GetParent();
3127 /* we send a list_key event up */
3130 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3131 le
.m_itemIndex
= m_current
;
3132 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3133 le
.m_code
= event
.GetKeyCode();
3134 le
.SetEventObject( parent
);
3135 parent
->GetEventHandler()->ProcessEvent( le
);
3138 /* we propagate the char event up */
3139 wxKeyEvent
ke( wxEVT_CHAR
);
3140 ke
.m_shiftDown
= event
.m_shiftDown
;
3141 ke
.m_controlDown
= event
.m_controlDown
;
3142 ke
.m_altDown
= event
.m_altDown
;
3143 ke
.m_metaDown
= event
.m_metaDown
;
3144 ke
.m_keyCode
= event
.m_keyCode
;
3147 ke
.SetEventObject( parent
);
3148 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3150 if (event
.GetKeyCode() == WXK_TAB
)
3152 wxNavigationKeyEvent nevent
;
3153 nevent
.SetWindowChange( event
.ControlDown() );
3154 nevent
.SetDirection( !event
.ShiftDown() );
3155 nevent
.SetEventObject( GetParent()->GetParent() );
3156 nevent
.SetCurrentFocus( m_parent
);
3157 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3161 /* no item -> nothing to do */
3168 switch (event
.GetKeyCode())
3171 if ( m_current
> 0 )
3172 OnArrowChar( m_current
- 1, event
);
3176 if ( m_current
< (size_t)GetItemCount() - 1 )
3177 OnArrowChar( m_current
+ 1, event
);
3182 OnArrowChar( GetItemCount() - 1, event
);
3187 OnArrowChar( 0, event
);
3193 if ( HasFlag(wxLC_REPORT
) )
3195 steps
= m_linesPerPage
- 1;
3199 steps
= m_current
% m_linesPerPage
;
3202 int index
= m_current
- steps
;
3206 OnArrowChar( index
, event
);
3213 if ( HasFlag(wxLC_REPORT
) )
3215 steps
= m_linesPerPage
- 1;
3219 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3222 size_t index
= m_current
+ steps
;
3223 size_t count
= GetItemCount();
3224 if ( index
>= count
)
3227 OnArrowChar( index
, event
);
3232 if ( !HasFlag(wxLC_REPORT
) )
3234 int index
= m_current
- m_linesPerPage
;
3238 OnArrowChar( index
, event
);
3243 if ( !HasFlag(wxLC_REPORT
) )
3245 size_t index
= m_current
+ m_linesPerPage
;
3247 size_t count
= GetItemCount();
3248 if ( index
>= count
)
3251 OnArrowChar( index
, event
);
3256 if ( IsSingleSel() )
3258 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3260 if ( IsHighlighted(m_current
) )
3262 // don't unselect the item in single selection mode
3265 //else: select it in ReverseHighlight() below if unselected
3268 ReverseHighlight(m_current
);
3273 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3281 // ----------------------------------------------------------------------------
3283 // ----------------------------------------------------------------------------
3285 void wxListMainWindow::SetFocus()
3287 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3288 // overrides SetFocus in such way that it does never change focus from
3289 // panel's child to the panel itself. Unfortunately, we must be able to change
3290 // focus to the panel from wxListTextCtrl because the text control should
3291 // disappear when the user clicks outside it.
3293 wxWindow
*oldFocus
= FindFocus();
3295 if ( oldFocus
&& oldFocus
->GetParent() == this )
3297 wxWindow::SetFocus();
3301 wxScrolledWindow::SetFocus();
3305 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3307 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3308 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3309 // which are already drawn correctly resulting in horrible flicker - avoid
3321 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3322 event
.SetEventObject( GetParent() );
3323 GetParent()->GetEventHandler()->ProcessEvent( event
);
3326 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3333 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3335 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3337 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3339 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3341 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3343 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3345 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3347 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3349 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3353 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3355 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3357 m_normal_image_list
->GetSize( index
, width
, height
);
3359 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3361 m_small_image_list
->GetSize( index
, width
, height
);
3363 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3365 m_small_image_list
->GetSize( index
, width
, height
);
3367 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3369 m_small_image_list
->GetSize( index
, width
, height
);
3378 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3380 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3381 dc
.SetFont( GetFont() );
3384 dc
.GetTextExtent( s
, &lw
, NULL
);
3386 return lw
+ AUTOSIZE_COL_MARGIN
;
3389 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3393 // calc the spacing from the icon size
3396 if ((imageList
) && (imageList
->GetImageCount()) )
3398 imageList
->GetSize(0, width
, height
);
3401 if (which
== wxIMAGE_LIST_NORMAL
)
3403 m_normal_image_list
= imageList
;
3404 m_normal_spacing
= width
+ 8;
3407 if (which
== wxIMAGE_LIST_SMALL
)
3409 m_small_image_list
= imageList
;
3410 m_small_spacing
= width
+ 14;
3411 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3415 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3420 m_small_spacing
= spacing
;
3424 m_normal_spacing
= spacing
;
3428 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3430 return isSmall
? m_small_spacing
: m_normal_spacing
;
3433 // ----------------------------------------------------------------------------
3435 // ----------------------------------------------------------------------------
3437 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3439 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3441 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3443 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3444 item
.m_width
= GetTextLength( item
.m_text
);
3446 wxListHeaderData
*column
= node
->GetData();
3447 column
->SetItem( item
);
3449 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3451 headerWin
->m_dirty
= TRUE
;
3455 // invalidate it as it has to be recalculated
3459 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3461 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3462 _T("invalid column index") );
3464 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3465 _T("SetColumnWidth() can only be called in report mode.") );
3468 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3470 headerWin
->m_dirty
= TRUE
;
3472 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3473 wxCHECK_RET( node
, _T("no column?") );
3475 wxListHeaderData
*column
= node
->GetData();
3477 size_t count
= GetItemCount();
3479 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3481 width
= GetTextLength(column
->GetText());
3483 else if ( width
== wxLIST_AUTOSIZE
)
3487 // TODO: determine the max width somehow...
3488 width
= WIDTH_COL_DEFAULT
;
3492 wxClientDC
dc(this);
3493 dc
.SetFont( GetFont() );
3495 int max
= AUTOSIZE_COL_MARGIN
;
3497 for ( size_t i
= 0; i
< count
; i
++ )
3499 wxListLineData
*line
= GetLine(i
);
3500 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3502 wxCHECK_RET( n
, _T("no subitem?") );
3504 wxListItemData
*item
= n
->GetData();
3507 if (item
->HasImage())
3510 GetImageSize( item
->GetImage(), ix
, iy
);
3514 if (item
->HasText())
3517 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3525 width
= max
+ AUTOSIZE_COL_MARGIN
;
3529 column
->SetWidth( width
);
3531 // invalidate it as it has to be recalculated
3535 int wxListMainWindow::GetHeaderWidth() const
3537 if ( !m_headerWidth
)
3539 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3541 size_t count
= GetColumnCount();
3542 for ( size_t col
= 0; col
< count
; col
++ )
3544 self
->m_headerWidth
+= GetColumnWidth(col
);
3548 return m_headerWidth
;
3551 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3553 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3554 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3556 wxListHeaderData
*column
= node
->GetData();
3557 column
->GetItem( item
);
3560 int wxListMainWindow::GetColumnWidth( int col
) const
3562 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3563 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3565 wxListHeaderData
*column
= node
->GetData();
3566 return column
->GetWidth();
3569 // ----------------------------------------------------------------------------
3571 // ----------------------------------------------------------------------------
3573 void wxListMainWindow::SetItem( wxListItem
&item
)
3575 long id
= item
.m_itemId
;
3576 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3577 _T("invalid item index in SetItem") );
3581 wxListLineData
*line
= GetLine((size_t)id
);
3582 line
->SetItem( item
.m_col
, item
);
3585 // update the item on screen
3587 GetItemRect(id
, rectItem
);
3588 RefreshRect(rectItem
);
3591 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3593 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3594 _T("invalid list ctrl item index in SetItem") );
3596 size_t oldCurrent
= m_current
;
3597 size_t item
= (size_t)litem
; // safe because of the check above
3599 // do we need to change the focus?
3600 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3602 if ( state
& wxLIST_STATE_FOCUSED
)
3604 // don't do anything if this item is already focused
3605 if ( item
!= m_current
)
3607 ChangeCurrent(item
);
3609 if ( oldCurrent
!= (size_t)-1 )
3611 if ( IsSingleSel() )
3613 HighlightLine(oldCurrent
, FALSE
);
3616 RefreshLine(oldCurrent
);
3619 RefreshLine( m_current
);
3624 // don't do anything if this item is not focused
3625 if ( item
== m_current
)
3629 if ( IsSingleSel() )
3631 // we must unselect the old current item as well or we
3632 // might end up with more than one selected item in a
3633 // single selection control
3634 HighlightLine(oldCurrent
, FALSE
);
3637 RefreshLine( oldCurrent
);
3642 // do we need to change the selection state?
3643 if ( stateMask
& wxLIST_STATE_SELECTED
)
3645 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3647 if ( IsSingleSel() )
3651 // selecting the item also makes it the focused one in the
3653 if ( m_current
!= item
)
3655 ChangeCurrent(item
);
3657 if ( oldCurrent
!= (size_t)-1 )
3659 HighlightLine( oldCurrent
, FALSE
);
3660 RefreshLine( oldCurrent
);
3666 // only the current item may be selected anyhow
3667 if ( item
!= m_current
)
3672 if ( HighlightLine(item
, on
) )
3679 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3681 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3682 _T("invalid list ctrl item index in GetItemState()") );
3684 int ret
= wxLIST_STATE_DONTCARE
;
3686 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3688 if ( (size_t)item
== m_current
)
3689 ret
|= wxLIST_STATE_FOCUSED
;
3692 if ( stateMask
& wxLIST_STATE_SELECTED
)
3694 if ( IsHighlighted(item
) )
3695 ret
|= wxLIST_STATE_SELECTED
;
3701 void wxListMainWindow::GetItem( wxListItem
&item
) const
3703 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3704 _T("invalid item index in GetItem") );
3706 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3707 line
->GetItem( item
.m_col
, item
);
3710 // ----------------------------------------------------------------------------
3712 // ----------------------------------------------------------------------------
3714 size_t wxListMainWindow::GetItemCount() const
3716 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3719 void wxListMainWindow::SetItemCount(long count
)
3721 m_selStore
.SetItemCount(count
);
3722 m_countVirt
= count
;
3724 ResetVisibleLinesRange();
3726 // scrollbars must be reset
3730 int wxListMainWindow::GetSelectedItemCount() const
3732 // deal with the quick case first
3733 if ( IsSingleSel() )
3735 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3738 // virtual controls remmebers all its selections itself
3740 return m_selStore
.GetSelectedCount();
3742 // TODO: we probably should maintain the number of items selected even for
3743 // non virtual controls as enumerating all lines is really slow...
3744 size_t countSel
= 0;
3745 size_t count
= GetItemCount();
3746 for ( size_t line
= 0; line
< count
; line
++ )
3748 if ( GetLine(line
)->IsHighlighted() )
3755 // ----------------------------------------------------------------------------
3756 // item position/size
3757 // ----------------------------------------------------------------------------
3759 wxRect
wxListMainWindow::GetViewRect() const
3761 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3762 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3764 // we need to find the longest/tallest label
3767 const int count
= GetItemCount();
3770 for ( int i
= 0; i
< count
; i
++ )
3775 wxCoord x
= r
.GetRight(),
3785 // some fudge needed to make it look prettier
3786 xMax
+= 2*EXTRA_BORDER_X
;
3787 yMax
+= 2*EXTRA_BORDER_Y
;
3789 // account for the scrollbars if necessary
3790 const wxSize sizeAll
= GetClientSize();
3791 if ( xMax
> sizeAll
.x
)
3792 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3793 if ( yMax
> sizeAll
.y
)
3794 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3796 return wxRect(0, 0, xMax
, yMax
);
3799 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3801 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3802 _T("invalid index in GetItemRect") );
3804 // ensure that we're laid out, otherwise we could return nonsense
3807 wxConstCast(this, wxListMainWindow
)->
3808 RecalculatePositions(TRUE
/* no refresh */);
3811 rect
= GetLineRect((size_t)index
);
3813 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3816 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3819 GetItemRect(item
, rect
);
3827 // ----------------------------------------------------------------------------
3828 // geometry calculation
3829 // ----------------------------------------------------------------------------
3831 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3833 wxClientDC
dc( this );
3834 dc
.SetFont( GetFont() );
3836 const size_t count
= GetItemCount();
3839 if ( HasFlag(wxLC_ICON
) )
3840 iconSpacing
= m_normal_spacing
;
3841 else if ( HasFlag(wxLC_SMALL_ICON
) )
3842 iconSpacing
= m_small_spacing
;
3846 // Note that we do not call GetClientSize() here but
3847 // GetSize() and substract the border size for sunken
3848 // borders manually. This is technically incorrect,
3849 // but we need to know the client area's size WITHOUT
3850 // scrollbars here. Since we don't know if there are
3851 // any scrollbars, we use GetSize() instead. Another
3852 // solution would be to call SetScrollbars() here to
3853 // remove the scrollbars and call GetClientSize() then,
3854 // but this might result in flicker and - worse - will
3855 // reset the scrollbars to 0 which is not good at all
3856 // if you resize a dialog/window, but don't want to
3857 // reset the window scrolling. RR.
3858 // Furthermore, we actually do NOT subtract the border
3859 // width as 2 pixels is just the extra space which we
3860 // need around the actual content in the window. Other-
3861 // wise the text would e.g. touch the upper border. RR.
3864 GetSize( &clientWidth
, &clientHeight
);
3866 if ( HasFlag(wxLC_REPORT
) )
3868 // all lines have the same height and we scroll one line per step
3869 int lineHeight
= GetLineHeight();
3871 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3873 m_linesPerPage
= clientHeight
/ lineHeight
;
3875 ResetVisibleLinesRange();
3877 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3878 GetHeaderWidth() / SCROLL_UNIT_X
,
3879 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3880 GetScrollPos(wxHORIZONTAL
),
3881 GetScrollPos(wxVERTICAL
),
3886 // we have 3 different layout strategies: either layout all items
3887 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3888 // to arrange them in top to bottom, left to right (don't ask me why
3889 // not the other way round...) order
3890 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3892 int x
= EXTRA_BORDER_X
;
3893 int y
= EXTRA_BORDER_Y
;
3895 for ( size_t i
= 0; i
< count
; i
++ )
3897 wxListLineData
*line
= GetLine(i
);
3898 line
->CalculateSize( &dc
, iconSpacing
);
3899 line
->SetPosition( x
, y
, iconSpacing
);
3901 wxSize sizeLine
= GetLineSize(i
);
3903 if ( HasFlag(wxLC_ALIGN_TOP
) )
3907 else // wxLC_ALIGN_LEFT
3909 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3917 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3918 (y
+ SCROLL_UNIT_Y
) / SCROLL_UNIT_Y
,
3919 GetScrollPos( wxHORIZONTAL
),
3920 GetScrollPos( wxVERTICAL
),
3924 else // "flowed" arrangement, the most complicated case
3926 // at first we try without any scrollbars, if the items don't fit into
3927 // the window, we recalculate after subtracting the space taken by the
3930 int entireWidth
= 0,
3933 for (int tries
= 0; tries
< 2; tries
++)
3935 entireWidth
= 2*EXTRA_BORDER_X
;
3936 entireHeight
= 2*EXTRA_BORDER_Y
;
3940 // Now we have decided that the items do not fit into the
3941 // client area, so we need a scrollbar
3942 entireWidth
+= SCROLL_UNIT_X
;
3945 int x
= EXTRA_BORDER_X
;
3946 int y
= EXTRA_BORDER_Y
;
3947 int maxWidthInThisRow
= 0;
3950 int currentlyVisibleLines
= 0;
3952 for (size_t i
= 0; i
< count
; i
++)
3954 currentlyVisibleLines
++;
3955 wxListLineData
*line
= GetLine(i
);
3956 line
->CalculateSize( &dc
, iconSpacing
);
3957 line
->SetPosition( x
, y
, iconSpacing
);
3959 wxSize sizeLine
= GetLineSize(i
);
3961 if ( maxWidthInThisRow
< sizeLine
.x
)
3962 maxWidthInThisRow
= sizeLine
.x
;
3965 if (currentlyVisibleLines
> m_linesPerPage
)
3966 m_linesPerPage
= currentlyVisibleLines
;
3968 if ( y
+ sizeLine
.y
>= clientHeight
)
3970 currentlyVisibleLines
= 0;
3972 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3973 x
+= maxWidthInThisRow
;
3974 entireWidth
+= maxWidthInThisRow
;
3975 maxWidthInThisRow
= 0;
3978 // We have reached the last item.
3979 if ( i
== count
- 1 )
3980 entireWidth
+= maxWidthInThisRow
;
3982 if ( (tries
== 0) &&
3983 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3985 clientHeight
-= wxSystemSettings::
3986 GetMetric(wxSYS_HSCROLL_Y
);
3988 currentlyVisibleLines
= 0;
3992 if ( i
== count
- 1 )
3993 tries
= 1; // Everything fits, no second try required.
4001 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4003 GetScrollPos( wxHORIZONTAL
),
4012 // FIXME: why should we call it from here?
4019 void wxListMainWindow::RefreshAll()
4024 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4025 if ( headerWin
&& headerWin
->m_dirty
)
4027 headerWin
->m_dirty
= FALSE
;
4028 headerWin
->Refresh();
4032 void wxListMainWindow::UpdateCurrent()
4034 if ( !HasCurrent() && !IsEmpty() )
4040 long wxListMainWindow::GetNextItem( long item
,
4041 int WXUNUSED(geometry
),
4045 max
= GetItemCount();
4046 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4047 _T("invalid listctrl index in GetNextItem()") );
4049 // notice that we start with the next item (or the first one if item == -1)
4050 // and this is intentional to allow writing a simple loop to iterate over
4051 // all selected items
4055 // this is not an error because the index was ok initially, just no
4066 size_t count
= GetItemCount();
4067 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4069 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4072 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4079 // ----------------------------------------------------------------------------
4081 // ----------------------------------------------------------------------------
4083 void wxListMainWindow::DeleteItem( long lindex
)
4085 size_t count
= GetItemCount();
4087 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4088 _T("invalid item index in DeleteItem") );
4090 size_t index
= (size_t)lindex
;
4092 // we don't need to adjust the index for the previous items
4093 if ( HasCurrent() && m_current
>= index
)
4095 // if the current item is being deleted, we want the next one to
4096 // become selected - unless there is no next one - so don't adjust
4097 // m_current in this case
4098 if ( m_current
!= index
|| m_current
== count
- 1 )
4104 if ( InReportView() )
4106 ResetVisibleLinesRange();
4113 m_selStore
.OnItemDelete(index
);
4117 m_lines
.RemoveAt( index
);
4120 // we need to refresh the (vert) scrollbar as the number of items changed
4123 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4125 RefreshAfter(index
);
4128 void wxListMainWindow::DeleteColumn( int col
)
4130 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4132 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4135 delete node
->GetData();
4136 m_columns
.Erase( node
);
4140 // update all the items
4141 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4143 wxListLineData
* const line
= GetLine(i
);
4144 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4145 delete n
->GetData();
4146 line
->m_items
.Erase(n
);
4150 // invalidate it as it has to be recalculated
4154 void wxListMainWindow::DoDeleteAllItems()
4158 // nothing to do - in particular, don't send the event
4164 // to make the deletion of all items faster, we don't send the
4165 // notifications for each item deletion in this case but only one event
4166 // for all of them: this is compatible with wxMSW and documented in
4167 // DeleteAllItems() description
4169 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4170 event
.SetEventObject( GetParent() );
4171 GetParent()->GetEventHandler()->ProcessEvent( event
);
4180 if ( InReportView() )
4182 ResetVisibleLinesRange();
4188 void wxListMainWindow::DeleteAllItems()
4192 RecalculatePositions();
4195 void wxListMainWindow::DeleteEverything()
4197 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4202 // ----------------------------------------------------------------------------
4203 // scanning for an item
4204 // ----------------------------------------------------------------------------
4206 void wxListMainWindow::EnsureVisible( long index
)
4208 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4209 _T("invalid index in EnsureVisible") );
4211 // We have to call this here because the label in question might just have
4212 // been added and its position is not known yet
4215 RecalculatePositions(TRUE
/* no refresh */);
4218 MoveToItem((size_t)index
);
4221 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4228 size_t count
= GetItemCount();
4229 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4231 wxListLineData
*line
= GetLine(i
);
4232 if ( line
->GetText(0) == tmp
)
4239 long wxListMainWindow::FindItem(long start
, long data
)
4245 size_t count
= GetItemCount();
4246 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4248 wxListLineData
*line
= GetLine(i
);
4250 line
->GetItem( 0, item
);
4251 if (item
.m_data
== data
)
4258 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4260 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4262 size_t count
= GetItemCount();
4264 if ( HasFlag(wxLC_REPORT
) )
4266 size_t current
= y
/ GetLineHeight();
4267 if ( current
< count
)
4269 flags
= HitTestLine(current
, x
, y
);
4276 // TODO: optimize it too! this is less simple than for report view but
4277 // enumerating all items is still not a way to do it!!
4278 for ( size_t current
= 0; current
< count
; current
++ )
4280 flags
= HitTestLine(current
, x
, y
);
4289 // ----------------------------------------------------------------------------
4291 // ----------------------------------------------------------------------------
4293 void wxListMainWindow::InsertItem( wxListItem
&item
)
4295 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4297 size_t count
= GetItemCount();
4298 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4299 _T("invalid item index") );
4301 size_t id
= item
.m_itemId
;
4306 if ( HasFlag(wxLC_REPORT
) )
4309 ResetVisibleLinesRange();
4311 else if ( HasFlag(wxLC_LIST
) )
4313 else if ( HasFlag(wxLC_ICON
) )
4315 else if ( HasFlag(wxLC_SMALL_ICON
) )
4316 mode
= wxLC_ICON
; // no typo
4319 wxFAIL_MSG( _T("unknown mode") );
4322 wxListLineData
*line
= new wxListLineData(this);
4324 line
->SetItem( 0, item
);
4326 m_lines
.Insert( line
, id
);
4330 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4332 RefreshLines(id
, GetItemCount() - 1);
4335 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4338 if ( HasFlag(wxLC_REPORT
) )
4340 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4341 item
.m_width
= GetTextLength( item
.m_text
);
4343 wxListHeaderData
*column
= new wxListHeaderData( item
);
4344 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4347 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4348 m_columns
.Insert( node
, column
);
4352 m_columns
.Append( column
);
4357 // update all the items
4358 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4360 wxListLineData
* const line
= GetLine(i
);
4361 wxListItemData
* const data
= new wxListItemData(this);
4363 line
->m_items
.Insert(col
, data
);
4365 line
->m_items
.Append(data
);
4369 // invalidate it as it has to be recalculated
4374 // ----------------------------------------------------------------------------
4376 // ----------------------------------------------------------------------------
4378 wxListCtrlCompare list_ctrl_compare_func_2
;
4379 long list_ctrl_compare_data
;
4381 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4383 wxListLineData
*line1
= *arg1
;
4384 wxListLineData
*line2
= *arg2
;
4386 line1
->GetItem( 0, item
);
4387 long data1
= item
.m_data
;
4388 line2
->GetItem( 0, item
);
4389 long data2
= item
.m_data
;
4390 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4393 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4395 list_ctrl_compare_func_2
= fn
;
4396 list_ctrl_compare_data
= data
;
4397 m_lines
.Sort( list_ctrl_compare_func_1
);
4401 // ----------------------------------------------------------------------------
4403 // ----------------------------------------------------------------------------
4405 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4407 // update our idea of which lines are shown when we redraw the window the
4409 ResetVisibleLinesRange();
4412 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4413 wxScrolledWindow::OnScroll(event
);
4415 HandleOnScroll( event
);
4418 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4420 wxGenericListCtrl
* lc
= GetListCtrl();
4421 wxCHECK_RET( lc
, _T("no listctrl window?") );
4423 lc
->m_headerWin
->Refresh();
4424 lc
->m_headerWin
->Update();
4428 int wxListMainWindow::GetCountPerPage() const
4430 if ( !m_linesPerPage
)
4432 wxConstCast(this, wxListMainWindow
)->
4433 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4436 return m_linesPerPage
;
4439 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4441 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4443 if ( m_lineFrom
== (size_t)-1 )
4445 size_t count
= GetItemCount();
4448 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4450 // this may happen if SetScrollbars() hadn't been called yet
4451 if ( m_lineFrom
>= count
)
4452 m_lineFrom
= count
- 1;
4454 // we redraw one extra line but this is needed to make the redrawing
4455 // logic work when there is a fractional number of lines on screen
4456 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4457 if ( m_lineTo
>= count
)
4458 m_lineTo
= count
- 1;
4460 else // empty control
4463 m_lineTo
= (size_t)-1;
4467 wxASSERT_MSG( IsEmpty() ||
4468 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4469 _T("GetVisibleLinesRange() returns incorrect result") );
4477 // -------------------------------------------------------------------------------------
4478 // wxGenericListCtrl
4479 // -------------------------------------------------------------------------------------
4481 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4483 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4484 EVT_SIZE(wxGenericListCtrl::OnSize
)
4487 wxGenericListCtrl::wxGenericListCtrl()
4489 m_imageListNormal
= (wxImageListType
*) NULL
;
4490 m_imageListSmall
= (wxImageListType
*) NULL
;
4491 m_imageListState
= (wxImageListType
*) NULL
;
4493 m_ownsImageListNormal
=
4494 m_ownsImageListSmall
=
4495 m_ownsImageListState
= FALSE
;
4497 m_mainWin
= (wxListMainWindow
*) NULL
;
4498 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4502 wxGenericListCtrl::~wxGenericListCtrl()
4504 if (m_ownsImageListNormal
)
4505 delete m_imageListNormal
;
4506 if (m_ownsImageListSmall
)
4507 delete m_imageListSmall
;
4508 if (m_ownsImageListState
)
4509 delete m_imageListState
;
4512 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4514 // we use the letter "H" for calculating the needed space, basing on the current font
4516 m_headerWin
->GetTextExtent(wxT("H"), &w
, &h
);
4517 m_headerHeight
= h
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4518 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4521 void wxGenericListCtrl::CreateHeaderWindow()
4523 m_headerWin
= new wxListHeaderWindow
4525 this, -1, m_mainWin
,
4527 wxSize(GetClientSize().x
, m_headerHeight
),
4530 CalculateAndSetHeaderHeight();
4533 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4538 const wxValidator
&validator
,
4539 const wxString
&name
)
4543 m_imageListState
= (wxImageListType
*) NULL
;
4544 m_ownsImageListNormal
=
4545 m_ownsImageListSmall
=
4546 m_ownsImageListState
= FALSE
;
4548 m_mainWin
= (wxListMainWindow
*) NULL
;
4549 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4551 if ( !(style
& wxLC_MASK_TYPE
) )
4553 style
= style
| wxLC_LIST
;
4556 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4559 // don't create the inner window with the border
4560 style
&= ~wxBORDER_MASK
;
4562 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4564 if ( HasFlag(wxLC_REPORT
) )
4566 CreateHeaderWindow();
4568 if ( HasFlag(wxLC_NO_HEADER
) )
4570 // VZ: why do we create it at all then?
4571 m_headerWin
->Show( FALSE
);
4578 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4580 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4581 _T("wxLC_VIRTUAL can't be [un]set") );
4583 long flag
= GetWindowStyle();
4587 if (style
& wxLC_MASK_TYPE
)
4588 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4589 if (style
& wxLC_MASK_ALIGN
)
4590 flag
&= ~wxLC_MASK_ALIGN
;
4591 if (style
& wxLC_MASK_SORT
)
4592 flag
&= ~wxLC_MASK_SORT
;
4604 SetWindowStyleFlag( flag
);
4607 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4611 m_mainWin
->DeleteEverything();
4613 // has the header visibility changed?
4614 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4615 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4617 if ( hasHeader
!= willHaveHeader
)
4624 // don't delete, just hide, as we can reuse it later
4625 m_headerWin
->Show(FALSE
);
4627 //else: nothing to do
4629 else // must show header
4633 CreateHeaderWindow();
4635 else // already have it, just show
4637 m_headerWin
->Show( TRUE
);
4641 ResizeReportView(willHaveHeader
);
4645 wxWindow::SetWindowStyleFlag( flag
);
4648 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4650 m_mainWin
->GetColumn( col
, item
);
4654 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4656 m_mainWin
->SetColumn( col
, item
);
4660 int wxGenericListCtrl::GetColumnWidth( int col
) const
4662 return m_mainWin
->GetColumnWidth( col
);
4665 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4667 m_mainWin
->SetColumnWidth( col
, width
);
4671 int wxGenericListCtrl::GetCountPerPage() const
4673 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4676 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4678 m_mainWin
->GetItem( info
);
4682 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4684 m_mainWin
->SetItem( info
);
4688 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4691 info
.m_text
= label
;
4692 info
.m_mask
= wxLIST_MASK_TEXT
;
4693 info
.m_itemId
= index
;
4697 info
.m_image
= imageId
;
4698 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4700 m_mainWin
->SetItem(info
);
4704 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4706 return m_mainWin
->GetItemState( item
, stateMask
);
4709 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4711 m_mainWin
->SetItemState( item
, state
, stateMask
);
4715 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4718 info
.m_image
= image
;
4719 info
.m_mask
= wxLIST_MASK_IMAGE
;
4720 info
.m_itemId
= item
;
4721 m_mainWin
->SetItem( info
);
4725 wxString
wxGenericListCtrl::GetItemText( long item
) const
4727 return m_mainWin
->GetItemText(item
);
4730 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4732 m_mainWin
->SetItemText(item
, str
);
4735 long wxGenericListCtrl::GetItemData( long item
) const
4738 info
.m_itemId
= item
;
4739 m_mainWin
->GetItem( info
);
4743 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4746 info
.m_mask
= wxLIST_MASK_DATA
;
4747 info
.m_itemId
= item
;
4749 m_mainWin
->SetItem( info
);
4753 wxRect
wxGenericListCtrl::GetViewRect() const
4755 return m_mainWin
->GetViewRect();
4758 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4760 m_mainWin
->GetItemRect( item
, rect
);
4761 if ( m_mainWin
->HasHeader() )
4762 rect
.y
+= m_headerHeight
+ 1;
4766 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4768 m_mainWin
->GetItemPosition( item
, pos
);
4772 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4777 int wxGenericListCtrl::GetItemCount() const
4779 return m_mainWin
->GetItemCount();
4782 int wxGenericListCtrl::GetColumnCount() const
4784 return m_mainWin
->GetColumnCount();
4787 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4789 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4792 wxSize
wxGenericListCtrl::GetItemSpacing() const
4794 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4796 return wxSize(spacing
, spacing
);
4799 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4801 return m_mainWin
->GetItemSpacing( isSmall
);
4804 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4807 info
.m_itemId
= item
;
4808 info
.SetTextColour( col
);
4809 m_mainWin
->SetItem( info
);
4812 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4815 info
.m_itemId
= item
;
4816 m_mainWin
->GetItem( info
);
4817 return info
.GetTextColour();
4820 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4823 info
.m_itemId
= item
;
4824 info
.SetBackgroundColour( col
);
4825 m_mainWin
->SetItem( info
);
4828 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4831 info
.m_itemId
= item
;
4832 m_mainWin
->GetItem( info
);
4833 return info
.GetBackgroundColour();
4836 int wxGenericListCtrl::GetSelectedItemCount() const
4838 return m_mainWin
->GetSelectedItemCount();
4841 wxColour
wxGenericListCtrl::GetTextColour() const
4843 return GetForegroundColour();
4846 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4848 SetForegroundColour(col
);
4851 long wxGenericListCtrl::GetTopItem() const
4854 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4858 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4860 return m_mainWin
->GetNextItem( item
, geom
, state
);
4863 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4865 if (which
== wxIMAGE_LIST_NORMAL
)
4867 return m_imageListNormal
;
4869 else if (which
== wxIMAGE_LIST_SMALL
)
4871 return m_imageListSmall
;
4873 else if (which
== wxIMAGE_LIST_STATE
)
4875 return m_imageListState
;
4877 return (wxImageListType
*) NULL
;
4880 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4882 if ( which
== wxIMAGE_LIST_NORMAL
)
4884 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4885 m_imageListNormal
= imageList
;
4886 m_ownsImageListNormal
= FALSE
;
4888 else if ( which
== wxIMAGE_LIST_SMALL
)
4890 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4891 m_imageListSmall
= imageList
;
4892 m_ownsImageListSmall
= FALSE
;
4894 else if ( which
== wxIMAGE_LIST_STATE
)
4896 if (m_ownsImageListState
) delete m_imageListState
;
4897 m_imageListState
= imageList
;
4898 m_ownsImageListState
= FALSE
;
4901 m_mainWin
->SetImageList( imageList
, which
);
4904 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4906 SetImageList(imageList
, which
);
4907 if ( which
== wxIMAGE_LIST_NORMAL
)
4908 m_ownsImageListNormal
= TRUE
;
4909 else if ( which
== wxIMAGE_LIST_SMALL
)
4910 m_ownsImageListSmall
= TRUE
;
4911 else if ( which
== wxIMAGE_LIST_STATE
)
4912 m_ownsImageListState
= TRUE
;
4915 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4920 bool wxGenericListCtrl::DeleteItem( long item
)
4922 m_mainWin
->DeleteItem( item
);
4926 bool wxGenericListCtrl::DeleteAllItems()
4928 m_mainWin
->DeleteAllItems();
4932 bool wxGenericListCtrl::DeleteAllColumns()
4934 size_t count
= m_mainWin
->m_columns
.GetCount();
4935 for ( size_t n
= 0; n
< count
; n
++ )
4941 void wxGenericListCtrl::ClearAll()
4943 m_mainWin
->DeleteEverything();
4946 bool wxGenericListCtrl::DeleteColumn( int col
)
4948 m_mainWin
->DeleteColumn( col
);
4950 // if we don't have the header any longer, we need to relayout the window
4951 if ( !GetColumnCount() )
4953 ResizeReportView(FALSE
/* no header */);
4959 void wxGenericListCtrl::Edit( long item
)
4961 m_mainWin
->EditLabel( item
);
4964 bool wxGenericListCtrl::EnsureVisible( long item
)
4966 m_mainWin
->EnsureVisible( item
);
4970 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4972 return m_mainWin
->FindItem( start
, str
, partial
);
4975 long wxGenericListCtrl::FindItem( long start
, long data
)
4977 return m_mainWin
->FindItem( start
, data
);
4980 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4981 int WXUNUSED(direction
))
4986 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4988 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4991 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4993 m_mainWin
->InsertItem( info
);
4994 return info
.m_itemId
;
4997 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5000 info
.m_text
= label
;
5001 info
.m_mask
= wxLIST_MASK_TEXT
;
5002 info
.m_itemId
= index
;
5003 return InsertItem( info
);
5006 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5009 info
.m_mask
= wxLIST_MASK_IMAGE
;
5010 info
.m_image
= imageIndex
;
5011 info
.m_itemId
= index
;
5012 return InsertItem( info
);
5015 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5018 info
.m_text
= label
;
5019 info
.m_image
= imageIndex
;
5020 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5021 info
.m_itemId
= index
;
5022 return InsertItem( info
);
5025 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5027 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5029 m_mainWin
->InsertColumn( col
, item
);
5031 // if we hadn't had header before and have it now we need to relayout the
5033 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5035 ResizeReportView(TRUE
/* have header */);
5038 m_headerWin
->Refresh();
5043 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5044 int format
, int width
)
5047 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5048 item
.m_text
= heading
;
5051 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5052 item
.m_width
= width
;
5054 item
.m_format
= format
;
5056 return InsertColumn( col
, item
);
5059 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5065 // fn is a function which takes 3 long arguments: item1, item2, data.
5066 // item1 is the long data associated with a first item (NOT the index).
5067 // item2 is the long data associated with a second item (NOT the index).
5068 // data is the same value as passed to SortItems.
5069 // The return value is a negative number if the first item should precede the second
5070 // item, a positive number of the second item should precede the first,
5071 // or zero if the two items are equivalent.
5072 // data is arbitrary data to be passed to the sort function.
5074 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5076 m_mainWin
->SortItems( fn
, data
);
5080 // ----------------------------------------------------------------------------
5082 // ----------------------------------------------------------------------------
5084 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5089 ResizeReportView(m_mainWin
->HasHeader());
5091 m_mainWin
->RecalculatePositions();
5094 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5097 GetClientSize( &cw
, &ch
);
5101 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5102 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5104 else // no header window
5106 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5110 void wxGenericListCtrl::OnInternalIdle()
5112 wxWindow::OnInternalIdle();
5114 // do it only if needed
5115 if ( !m_mainWin
->m_dirty
)
5118 m_mainWin
->RecalculatePositions();
5121 // ----------------------------------------------------------------------------
5123 // ----------------------------------------------------------------------------
5125 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5129 m_mainWin
->SetBackgroundColour( colour
);
5130 m_mainWin
->m_dirty
= TRUE
;
5136 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5138 if ( !wxWindow::SetForegroundColour( colour
) )
5143 m_mainWin
->SetForegroundColour( colour
);
5144 m_mainWin
->m_dirty
= TRUE
;
5149 m_headerWin
->SetForegroundColour( colour
);
5155 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5157 if ( !wxWindow::SetFont( font
) )
5162 m_mainWin
->SetFont( font
);
5163 m_mainWin
->m_dirty
= TRUE
;
5168 m_headerWin
->SetFont( font
);
5169 CalculateAndSetHeaderHeight();
5177 // ----------------------------------------------------------------------------
5178 // methods forwarded to m_mainWin
5179 // ----------------------------------------------------------------------------
5181 #if wxUSE_DRAG_AND_DROP
5183 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5185 m_mainWin
->SetDropTarget( dropTarget
);
5188 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5190 return m_mainWin
->GetDropTarget();
5193 #endif // wxUSE_DRAG_AND_DROP
5195 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5197 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5200 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5202 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5205 wxColour
wxGenericListCtrl::GetForegroundColour() const
5207 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5210 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5213 return m_mainWin
->PopupMenu( menu
, x
, y
);
5216 #endif // wxUSE_MENUS
5219 void wxGenericListCtrl::SetFocus()
5221 /* The test in window.cpp fails as we are a composite
5222 window, so it checks against "this", but not m_mainWin. */
5223 if ( FindFocus() != this )
5224 m_mainWin
->SetFocus();
5227 // ----------------------------------------------------------------------------
5228 // virtual list control support
5229 // ----------------------------------------------------------------------------
5231 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5233 // this is a pure virtual function, in fact - which is not really pure
5234 // because the controls which are not virtual don't need to implement it
5235 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5237 return wxEmptyString
;
5240 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5243 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5248 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5250 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5251 _T("invalid item index in OnGetItemAttr()") );
5253 // no attributes by default
5257 void wxGenericListCtrl::SetItemCount(long count
)
5259 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5261 m_mainWin
->SetItemCount(count
);
5264 void wxGenericListCtrl::RefreshItem(long item
)
5266 m_mainWin
->RefreshLine(item
);
5269 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5271 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5275 * Generic wxListCtrl is more or less a container for two other
5276 * windows which drawings are done upon. These are namely
5277 * 'm_headerWin' and 'm_mainWin'.
5278 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5279 * behaviour wxListCtrl has under wxMSW.
5281 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5285 // The easy case, no rectangle specified.
5287 m_headerWin
->Refresh(eraseBackground
);
5290 m_mainWin
->Refresh(eraseBackground
);
5294 // Refresh the header window
5297 wxRect rectHeader
= m_headerWin
->GetRect();
5298 rectHeader
.Intersect(*rect
);
5299 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5302 m_headerWin
->GetPosition(&x
, &y
);
5303 rectHeader
.Offset(-x
, -y
);
5304 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5309 // Refresh the main window
5312 wxRect rectMain
= m_mainWin
->GetRect();
5313 rectMain
.Intersect(*rect
);
5314 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5317 m_mainWin
->GetPosition(&x
, &y
);
5318 rectMain
.Offset(-x
, -y
);
5319 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5325 void wxGenericListCtrl::Freeze()
5327 m_mainWin
->Freeze();
5330 void wxGenericListCtrl::Thaw()
5335 #endif // wxUSE_LISTCTRL