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 // offset for the header window
121 static const int HEADER_OFFSET_X
= 1;
122 static const int HEADER_OFFSET_Y
= 1;
124 // when autosizing the columns, add some slack
125 static const int AUTOSIZE_COL_MARGIN
= 10;
127 // default and minimal widths for the header columns
128 static const int WIDTH_COL_DEFAULT
= 80;
129 static const int WIDTH_COL_MIN
= 10;
131 // the space between the image and the text in the report mode
132 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
134 // ============================================================================
136 // ============================================================================
138 //-----------------------------------------------------------------------------
139 // wxListItemData (internal)
140 //-----------------------------------------------------------------------------
142 class WXDLLEXPORT wxListItemData
145 wxListItemData(wxListMainWindow
*owner
);
148 void SetItem( const wxListItem
&info
);
149 void SetImage( int image
) { m_image
= image
; }
150 void SetData( long data
) { m_data
= data
; }
151 void SetPosition( int x
, int y
);
152 void SetSize( int width
, int height
);
154 bool HasText() const { return !m_text
.empty(); }
155 const wxString
& GetText() const { return m_text
; }
156 void SetText(const wxString
& text
) { m_text
= text
; }
158 // we can't use empty string for measuring the string width/height, so
159 // always return something
160 wxString
GetTextForMeasuring() const
162 wxString s
= GetText();
169 bool IsHit( int x
, int y
) const;
173 int GetWidth() const;
174 int GetHeight() const;
176 int GetImage() const { return m_image
; }
177 bool HasImage() const { return GetImage() != -1; }
179 void GetItem( wxListItem
&info
) const;
181 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
182 wxListItemAttr
*GetAttr() const { return m_attr
; }
185 // the item image or -1
188 // user data associated with the item
191 // the item coordinates are not used in report mode, instead this pointer
192 // is NULL and the owner window is used to retrieve the item position and
196 // the list ctrl we are in
197 wxListMainWindow
*m_owner
;
199 // custom attributes or NULL
200 wxListItemAttr
*m_attr
;
203 // common part of all ctors
209 //-----------------------------------------------------------------------------
210 // wxListHeaderData (internal)
211 //-----------------------------------------------------------------------------
213 class WXDLLEXPORT wxListHeaderData
: public wxObject
217 wxListHeaderData( const wxListItem
&info
);
218 void SetItem( const wxListItem
&item
);
219 void SetPosition( int x
, int y
);
220 void SetWidth( int w
);
221 void SetFormat( int format
);
222 void SetHeight( int h
);
223 bool HasImage() const;
225 bool HasText() const { return !m_text
.empty(); }
226 const wxString
& GetText() const { return m_text
; }
227 void SetText(const wxString
& text
) { m_text
= text
; }
229 void GetItem( wxListItem
&item
);
231 bool IsHit( int x
, int y
) const;
232 int GetImage() const;
233 int GetWidth() const;
234 int GetFormat() const;
250 //-----------------------------------------------------------------------------
251 // wxListLineData (internal)
252 //-----------------------------------------------------------------------------
254 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
255 #include "wx/listimpl.cpp"
256 WX_DEFINE_LIST(wxListItemDataList
);
261 // the list of subitems: only may have more than one item in report mode
262 wxListItemDataList m_items
;
264 // this is not used in report view
276 // the part to be highlighted
277 wxRect m_rectHighlight
;
280 // is this item selected? [NB: not used in virtual mode]
283 // back pointer to the list ctrl
284 wxListMainWindow
*m_owner
;
287 wxListLineData(wxListMainWindow
*owner
);
291 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
295 // are we in report mode?
296 inline bool InReportView() const;
298 // are we in virtual report mode?
299 inline bool IsVirtual() const;
301 // these 2 methods shouldn't be called for report view controls, in that
302 // case we determine our position/size ourselves
304 // calculate the size of the line
305 void CalculateSize( wxDC
*dc
, int spacing
);
307 // remember the position this line appears at
308 void SetPosition( int x
, int y
, int window_width
, int spacing
);
312 void SetImage( int image
) { SetImage(0, image
); }
313 int GetImage() const { return GetImage(0); }
314 bool HasImage() const { return GetImage() != -1; }
315 bool HasText() const { return !GetText(0).empty(); }
317 void SetItem( int index
, const wxListItem
&info
);
318 void GetItem( int index
, wxListItem
&info
);
320 wxString
GetText(int index
) const;
321 void SetText( int index
, const wxString s
);
323 wxListItemAttr
*GetAttr() const;
324 void SetAttr(wxListItemAttr
*attr
);
326 // return true if the highlighting really changed
327 bool Highlight( bool on
);
329 void ReverseHighlight();
331 bool IsHighlighted() const
333 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
335 return m_highlighted
;
338 // draw the line on the given DC in icon/list mode
339 void Draw( wxDC
*dc
);
341 // the same in report mode
342 void DrawInReportMode( wxDC
*dc
,
344 const wxRect
& rectHL
,
348 // set the line to contain num items (only can be > 1 in report mode)
349 void InitItems( int num
);
351 // get the mode (i.e. style) of the list control
352 inline int GetMode() const;
354 // prepare the DC for drawing with these item's attributes, return true if
355 // we need to draw the items background to highlight it, false otherwise
356 bool SetAttributes(wxDC
*dc
,
357 const wxListItemAttr
*attr
,
360 // draw the text on the DC with the correct justification; also add an
361 // ellipsis if the text is too large to fit in the current width
362 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
364 // these are only used by GetImage/SetImage above, we don't support images
365 // with subitems at the public API level yet
366 void SetImage( int index
, int image
);
367 int GetImage( int index
) const;
370 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
371 #include "wx/arrimpl.cpp"
372 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
374 //-----------------------------------------------------------------------------
375 // wxListHeaderWindow (internal)
376 //-----------------------------------------------------------------------------
378 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
381 wxListMainWindow
*m_owner
;
382 wxCursor
*m_currentCursor
;
383 wxCursor
*m_resizeCursor
;
386 // column being resized or -1
389 // divider line position in logical (unscrolled) coords
392 // minimal position beyond which the divider line can't be dragged in
397 wxListHeaderWindow();
399 wxListHeaderWindow( wxWindow
*win
,
401 wxListMainWindow
*owner
,
402 const wxPoint
&pos
= wxDefaultPosition
,
403 const wxSize
&size
= wxDefaultSize
,
405 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
407 virtual ~wxListHeaderWindow();
410 void AdjustDC(wxDC
& dc
);
412 void OnPaint( wxPaintEvent
&event
);
413 void OnMouse( wxMouseEvent
&event
);
414 void OnSetFocus( wxFocusEvent
&event
);
420 // common part of all ctors
423 // generate and process the list event of the given type, return true if
424 // it wasn't vetoed, i.e. if we should proceed
425 bool SendListEvent(wxEventType type
, wxPoint pos
);
427 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
428 DECLARE_EVENT_TABLE()
431 //-----------------------------------------------------------------------------
432 // wxListRenameTimer (internal)
433 //-----------------------------------------------------------------------------
435 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
438 wxListMainWindow
*m_owner
;
441 wxListRenameTimer( wxListMainWindow
*owner
);
445 //-----------------------------------------------------------------------------
446 // wxListTextCtrl (internal)
447 //-----------------------------------------------------------------------------
449 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
452 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
455 void OnChar( wxKeyEvent
&event
);
456 void OnKeyUp( wxKeyEvent
&event
);
457 void OnKillFocus( wxFocusEvent
&event
);
459 bool AcceptChanges();
463 wxListMainWindow
*m_owner
;
464 wxString m_startValue
;
468 DECLARE_EVENT_TABLE()
471 //-----------------------------------------------------------------------------
472 // wxListMainWindow (internal)
473 //-----------------------------------------------------------------------------
475 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
476 #include "wx/listimpl.cpp"
477 WX_DEFINE_LIST(wxListHeaderDataList
);
479 class wxListMainWindow
: public wxScrolledWindow
483 wxListMainWindow( wxWindow
*parent
,
485 const wxPoint
& pos
= wxDefaultPosition
,
486 const wxSize
& size
= wxDefaultSize
,
488 const wxString
&name
= _T("listctrlmainwindow") );
490 virtual ~wxListMainWindow();
492 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
494 // return true if this is a virtual list control
495 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
497 // return true if the control is in report mode
498 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
500 // return true if we are in single selection mode, false if multi sel
501 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
503 // do we have a header window?
504 bool HasHeader() const
505 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
507 void HighlightAll( bool on
);
509 // all these functions only do something if the line is currently visible
511 // change the line "selected" state, return TRUE if it really changed
512 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
514 // as HighlightLine() but do it for the range of lines: this is incredibly
515 // more efficient for virtual list controls!
517 // NB: unlike HighlightLine() this one does refresh the lines on screen
518 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
520 // toggle the line state and refresh it
521 void ReverseHighlight( size_t line
)
522 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
524 // return true if the line is highlighted
525 bool IsHighlighted(size_t line
) const;
527 // refresh one or several lines at once
528 void RefreshLine( size_t line
);
529 void RefreshLines( size_t lineFrom
, size_t lineTo
);
531 // refresh all selected items
532 void RefreshSelected();
534 // refresh all lines below the given one: the difference with
535 // RefreshLines() is that the index here might not be a valid one (happens
536 // when the last line is deleted)
537 void RefreshAfter( size_t lineFrom
);
539 // the methods which are forwarded to wxListLineData itself in list/icon
540 // modes but are here because the lines don't store their positions in the
543 // get the bound rect for the entire line
544 wxRect
GetLineRect(size_t line
) const;
546 // get the bound rect of the label
547 wxRect
GetLineLabelRect(size_t line
) const;
549 // get the bound rect of the items icon (only may be called if we do have
551 wxRect
GetLineIconRect(size_t line
) const;
553 // get the rect to be highlighted when the item has focus
554 wxRect
GetLineHighlightRect(size_t line
) const;
556 // get the size of the total line rect
557 wxSize
GetLineSize(size_t line
) const
558 { return GetLineRect(line
).GetSize(); }
560 // return the hit code for the corresponding position (in this line)
561 long HitTestLine(size_t line
, int x
, int y
) const;
563 // bring the selected item into view, scrolling to it if necessary
564 void MoveToItem(size_t item
);
566 // bring the current item into view
567 void MoveToFocus() { MoveToItem(m_current
); }
569 // start editing the label of the given item
570 void EditLabel( long item
);
572 // suspend/resume redrawing the control
578 void OnRenameTimer();
579 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
580 void OnRenameCancelled(size_t itemEdit
);
582 void OnMouse( wxMouseEvent
&event
);
584 // called to switch the selection from the current item to newCurrent,
585 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
587 void OnChar( wxKeyEvent
&event
);
588 void OnKeyDown( wxKeyEvent
&event
);
589 void OnSetFocus( wxFocusEvent
&event
);
590 void OnKillFocus( wxFocusEvent
&event
);
591 void OnScroll(wxScrollWinEvent
& event
) ;
593 void OnPaint( wxPaintEvent
&event
);
595 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
596 void GetImageSize( int index
, int &width
, int &height
) const;
597 int GetTextLength( const wxString
&s
) const;
599 void SetImageList( wxImageListType
*imageList
, int which
);
600 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
601 int GetItemSpacing( bool isSmall
= FALSE
);
603 void SetColumn( int col
, wxListItem
&item
);
604 void SetColumnWidth( int col
, int width
);
605 void GetColumn( int col
, wxListItem
&item
) const;
606 int GetColumnWidth( int col
) const;
607 int GetColumnCount() const { return m_columns
.GetCount(); }
609 // returns the sum of the heights of all columns
610 int GetHeaderWidth() const;
612 int GetCountPerPage() const;
614 void SetItem( wxListItem
&item
);
615 void GetItem( wxListItem
&item
) const;
616 void SetItemState( long item
, long state
, long stateMask
);
617 int GetItemState( long item
, long stateMask
) const;
618 void GetItemRect( long index
, wxRect
&rect
) const;
619 wxRect
GetViewRect() const;
620 bool GetItemPosition( long item
, wxPoint
& pos
) const;
621 int GetSelectedItemCount() const;
623 wxString
GetItemText(long item
) const
626 info
.m_itemId
= item
;
631 void SetItemText(long item
, const wxString
& value
)
634 info
.m_mask
= wxLIST_MASK_TEXT
;
635 info
.m_itemId
= item
;
640 // set the scrollbars and update the positions of the items
641 void RecalculatePositions(bool noRefresh
= FALSE
);
643 // refresh the window and the header
646 long GetNextItem( long item
, int geometry
, int state
) const;
647 void DeleteItem( long index
);
648 void DeleteAllItems();
649 void DeleteColumn( int col
);
650 void DeleteEverything();
651 void EnsureVisible( long index
);
652 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
653 long FindItem( long start
, long data
);
654 long HitTest( int x
, int y
, int &flags
);
655 void InsertItem( wxListItem
&item
);
656 void InsertColumn( long col
, wxListItem
&item
);
657 void SortItems( wxListCtrlCompare fn
, long data
);
659 size_t GetItemCount() const;
660 bool IsEmpty() const { return GetItemCount() == 0; }
661 void SetItemCount(long count
);
663 // change the current (== focused) item, send a notification event
664 void ChangeCurrent(size_t current
);
665 void ResetCurrent() { ChangeCurrent((size_t)-1); }
666 bool HasCurrent() const { return m_current
!= (size_t)-1; }
668 // send out a wxListEvent
669 void SendNotify( size_t line
,
671 wxPoint point
= wxDefaultPosition
);
673 // override base class virtual to reset m_lineHeight when the font changes
674 virtual bool SetFont(const wxFont
& font
)
676 if ( !wxScrolledWindow::SetFont(font
) )
684 // these are for wxListLineData usage only
686 // get the backpointer to the list ctrl
687 wxGenericListCtrl
*GetListCtrl() const
689 return wxStaticCast(GetParent(), wxGenericListCtrl
);
692 // get the height of all lines (assuming they all do have the same height)
693 wxCoord
GetLineHeight() const;
695 // get the y position of the given line (only for report view)
696 wxCoord
GetLineY(size_t line
) const;
698 // get the brush to use for the item highlighting
699 wxBrush
*GetHighlightBrush() const
701 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
705 // the array of all line objects for a non virtual list control (for the
706 // virtual list control we only ever use m_lines[0])
707 wxListLineDataArray m_lines
;
709 // the list of column objects
710 wxListHeaderDataList m_columns
;
712 // currently focused item or -1
715 // the number of lines per page
718 // this flag is set when something which should result in the window
719 // redrawing happens (i.e. an item was added or deleted, or its appearance
720 // changed) and OnPaint() doesn't redraw the window while it is set which
721 // allows to minimize the number of repaintings when a lot of items are
722 // being added. The real repainting occurs only after the next OnIdle()
726 wxColour
*m_highlightColour
;
729 wxImageListType
*m_small_image_list
;
730 wxImageListType
*m_normal_image_list
;
732 int m_normal_spacing
;
736 wxTimer
*m_renameTimer
;
741 // for double click logic
742 size_t m_lineLastClicked
,
743 m_lineBeforeLastClicked
;
746 // the total count of items in a virtual list control
749 // the object maintaining the items selection state, only used in virtual
751 wxSelectionStore m_selStore
;
753 // common part of all ctors
756 // intiialize m_[xy]Scroll
757 void InitScrolling();
759 // get the line data for the given index
760 wxListLineData
*GetLine(size_t n
) const
762 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
766 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
774 // get a dummy line which can be used for geometry calculations and such:
775 // you must use GetLine() if you want to really draw the line
776 wxListLineData
*GetDummyLine() const;
778 // cache the line data of the n-th line in m_lines[0]
779 void CacheLineData(size_t line
);
781 // get the range of visible lines
782 void GetVisibleLinesRange(size_t *from
, size_t *to
);
784 // force us to recalculate the range of visible lines
785 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
787 // get the colour to be used for drawing the rules
788 wxColour
GetRuleColour() const
793 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
798 // initialize the current item if needed
799 void UpdateCurrent();
801 // delete all items but don't refresh: called from dtor
802 void DoDeleteAllItems();
804 // the height of one line using the current font
805 wxCoord m_lineHeight
;
807 // the total header width or 0 if not calculated yet
808 wxCoord m_headerWidth
;
810 // the first and last lines being shown on screen right now (inclusive),
811 // both may be -1 if they must be calculated so never access them directly:
812 // use GetVisibleLinesRange() above instead
816 // the brushes to use for item highlighting when we do/don't have focus
817 wxBrush
*m_highlightBrush
,
818 *m_highlightUnfocusedBrush
;
820 // if this is > 0, the control is frozen and doesn't redraw itself
821 size_t m_freezeCount
;
823 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
824 DECLARE_EVENT_TABLE()
826 friend class wxGenericListCtrl
;
829 // ============================================================================
831 // ============================================================================
833 //-----------------------------------------------------------------------------
835 //-----------------------------------------------------------------------------
837 wxListItemData::~wxListItemData()
839 // in the virtual list control the attributes are managed by the main
840 // program, so don't delete them
841 if ( !m_owner
->IsVirtual() )
849 void wxListItemData::Init()
857 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
863 if ( owner
->InReportView() )
873 void wxListItemData::SetItem( const wxListItem
&info
)
875 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
876 SetText(info
.m_text
);
877 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
878 m_image
= info
.m_image
;
879 if ( info
.m_mask
& wxLIST_MASK_DATA
)
880 m_data
= info
.m_data
;
882 if ( info
.HasAttributes() )
885 *m_attr
= *info
.GetAttributes();
887 m_attr
= new wxListItemAttr(*info
.GetAttributes());
895 m_rect
->width
= info
.m_width
;
899 void wxListItemData::SetPosition( int x
, int y
)
901 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
907 void wxListItemData::SetSize( int width
, int height
)
909 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
912 m_rect
->width
= width
;
914 m_rect
->height
= height
;
917 bool wxListItemData::IsHit( int x
, int y
) const
919 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
921 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
924 int wxListItemData::GetX() const
926 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
931 int wxListItemData::GetY() const
933 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
938 int wxListItemData::GetWidth() const
940 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
942 return m_rect
->width
;
945 int wxListItemData::GetHeight() const
947 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
949 return m_rect
->height
;
952 void wxListItemData::GetItem( wxListItem
&info
) const
954 info
.m_text
= m_text
;
955 info
.m_image
= m_image
;
956 info
.m_data
= m_data
;
960 if ( m_attr
->HasTextColour() )
961 info
.SetTextColour(m_attr
->GetTextColour());
962 if ( m_attr
->HasBackgroundColour() )
963 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
964 if ( m_attr
->HasFont() )
965 info
.SetFont(m_attr
->GetFont());
969 //-----------------------------------------------------------------------------
971 //-----------------------------------------------------------------------------
973 void wxListHeaderData::Init()
984 wxListHeaderData::wxListHeaderData()
989 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
996 void wxListHeaderData::SetItem( const wxListItem
&item
)
998 m_mask
= item
.m_mask
;
1000 if ( m_mask
& wxLIST_MASK_TEXT
)
1001 m_text
= item
.m_text
;
1003 if ( m_mask
& wxLIST_MASK_IMAGE
)
1004 m_image
= item
.m_image
;
1006 if ( m_mask
& wxLIST_MASK_FORMAT
)
1007 m_format
= item
.m_format
;
1009 if ( m_mask
& wxLIST_MASK_WIDTH
)
1010 SetWidth(item
.m_width
);
1013 void wxListHeaderData::SetPosition( int x
, int y
)
1019 void wxListHeaderData::SetHeight( int h
)
1024 void wxListHeaderData::SetWidth( int w
)
1028 m_width
= WIDTH_COL_DEFAULT
;
1029 else if (m_width
< WIDTH_COL_MIN
)
1030 m_width
= WIDTH_COL_MIN
;
1033 void wxListHeaderData::SetFormat( int format
)
1038 bool wxListHeaderData::HasImage() const
1040 return m_image
!= -1;
1043 bool wxListHeaderData::IsHit( int x
, int y
) const
1045 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1048 void wxListHeaderData::GetItem( wxListItem
& item
)
1050 item
.m_mask
= m_mask
;
1051 item
.m_text
= m_text
;
1052 item
.m_image
= m_image
;
1053 item
.m_format
= m_format
;
1054 item
.m_width
= m_width
;
1057 int wxListHeaderData::GetImage() const
1062 int wxListHeaderData::GetWidth() const
1067 int wxListHeaderData::GetFormat() const
1072 //-----------------------------------------------------------------------------
1074 //-----------------------------------------------------------------------------
1076 inline int wxListLineData::GetMode() const
1078 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1081 inline bool wxListLineData::InReportView() const
1083 return m_owner
->HasFlag(wxLC_REPORT
);
1086 inline bool wxListLineData::IsVirtual() const
1088 return m_owner
->IsVirtual();
1091 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1095 if ( InReportView() )
1101 m_gi
= new GeometryInfo
;
1104 m_highlighted
= FALSE
;
1106 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1109 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1111 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1112 wxCHECK_RET( node
, _T("no subitems at all??") );
1114 wxListItemData
*item
= node
->GetData();
1116 switch ( GetMode() )
1119 case wxLC_SMALL_ICON
:
1121 m_gi
->m_rectAll
.width
= spacing
;
1123 wxString s
= item
->GetText();
1129 m_gi
->m_rectLabel
.width
=
1130 m_gi
->m_rectLabel
.height
= 0;
1134 dc
->GetTextExtent( s
, &lw
, &lh
);
1135 if (lh
< SCROLL_UNIT_Y
)
1140 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1142 m_gi
->m_rectAll
.width
= lw
;
1144 m_gi
->m_rectLabel
.width
= lw
;
1145 m_gi
->m_rectLabel
.height
= lh
;
1148 if (item
->HasImage())
1151 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1152 m_gi
->m_rectIcon
.width
= w
+ 8;
1153 m_gi
->m_rectIcon
.height
= h
+ 8;
1155 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1156 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1157 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1158 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1161 if ( item
->HasText() )
1163 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1164 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1166 else // no text, highlight the icon
1168 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1169 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1176 wxString s
= item
->GetTextForMeasuring();
1179 dc
->GetTextExtent( s
, &lw
, &lh
);
1180 if (lh
< SCROLL_UNIT_Y
)
1185 m_gi
->m_rectLabel
.width
= lw
;
1186 m_gi
->m_rectLabel
.height
= lh
;
1188 m_gi
->m_rectAll
.width
= lw
;
1189 m_gi
->m_rectAll
.height
= lh
;
1191 if (item
->HasImage())
1194 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1195 m_gi
->m_rectIcon
.width
= w
;
1196 m_gi
->m_rectIcon
.height
= h
;
1198 m_gi
->m_rectAll
.width
+= 4 + w
;
1199 if (h
> m_gi
->m_rectAll
.height
)
1200 m_gi
->m_rectAll
.height
= h
;
1203 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1204 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1209 wxFAIL_MSG( _T("unexpected call to SetSize") );
1213 wxFAIL_MSG( _T("unknown mode") );
1217 void wxListLineData::SetPosition( int x
, int y
,
1218 int WXUNUSED(window_width
),
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 void wxListMainWindow::InitScrolling()
2144 if ( HasFlag(wxLC_REPORT
) )
2146 m_xScroll
= SCROLL_UNIT_X
;
2147 m_yScroll
= SCROLL_UNIT_Y
;
2151 m_xScroll
= SCROLL_UNIT_Y
;
2156 wxListMainWindow::wxListMainWindow()
2161 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2167 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2172 const wxString
&name
)
2173 : wxScrolledWindow( parent
, id
, pos
, size
,
2174 style
| wxHSCROLL
| wxVSCROLL
, name
)
2178 m_highlightBrush
= new wxBrush
2180 wxSystemSettings::GetColour
2182 wxSYS_COLOUR_HIGHLIGHT
2187 m_highlightUnfocusedBrush
= new wxBrush
2189 wxSystemSettings::GetColour
2191 wxSYS_COLOUR_BTNSHADOW
2200 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2202 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2205 wxListMainWindow::~wxListMainWindow()
2208 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2210 delete m_highlightBrush
;
2211 delete m_highlightUnfocusedBrush
;
2213 delete m_renameTimer
;
2216 void wxListMainWindow::CacheLineData(size_t line
)
2218 wxGenericListCtrl
*listctrl
= GetListCtrl();
2220 wxListLineData
*ld
= GetDummyLine();
2222 size_t countCol
= GetColumnCount();
2223 for ( size_t col
= 0; col
< countCol
; col
++ )
2225 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2228 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2229 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2232 wxListLineData
*wxListMainWindow::GetDummyLine() const
2234 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2236 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2238 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2240 // we need to recreate the dummy line if the number of columns in the
2241 // control changed as it would have the incorrect number of fields
2243 if ( !m_lines
.IsEmpty() &&
2244 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2246 self
->m_lines
.Clear();
2249 if ( m_lines
.IsEmpty() )
2251 wxListLineData
*line
= new wxListLineData(self
);
2252 self
->m_lines
.Add(line
);
2254 // don't waste extra memory -- there never going to be anything
2255 // else/more in this array
2256 self
->m_lines
.Shrink();
2262 // ----------------------------------------------------------------------------
2263 // line geometry (report mode only)
2264 // ----------------------------------------------------------------------------
2266 wxCoord
wxListMainWindow::GetLineHeight() const
2268 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2270 // we cache the line height as calling GetTextExtent() is slow
2271 if ( !m_lineHeight
)
2273 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2275 wxClientDC
dc( self
);
2276 dc
.SetFont( GetFont() );
2279 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2281 if ( y
< SCROLL_UNIT_Y
)
2284 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2288 m_small_image_list
->GetSize(0, iw
, ih
);
2293 self
->m_lineHeight
= y
+ LINE_SPACING
;
2296 return m_lineHeight
;
2299 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2301 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2303 return LINE_SPACING
+ line
*GetLineHeight();
2306 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2308 if ( !InReportView() )
2309 return GetLine(line
)->m_gi
->m_rectAll
;
2312 rect
.x
= HEADER_OFFSET_X
;
2313 rect
.y
= GetLineY(line
);
2314 rect
.width
= GetHeaderWidth();
2315 rect
.height
= GetLineHeight();
2320 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2322 if ( !InReportView() )
2323 return GetLine(line
)->m_gi
->m_rectLabel
;
2326 rect
.x
= HEADER_OFFSET_X
;
2327 rect
.y
= GetLineY(line
);
2328 rect
.width
= GetColumnWidth(0);
2329 rect
.height
= GetLineHeight();
2334 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2336 if ( !InReportView() )
2337 return GetLine(line
)->m_gi
->m_rectIcon
;
2339 wxListLineData
*ld
= GetLine(line
);
2340 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2343 rect
.x
= HEADER_OFFSET_X
;
2344 rect
.y
= GetLineY(line
);
2345 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2350 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2352 return InReportView() ? GetLineRect(line
)
2353 : GetLine(line
)->m_gi
->m_rectHighlight
;
2356 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2358 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2360 wxListLineData
*ld
= GetLine(line
);
2362 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2363 return wxLIST_HITTEST_ONITEMICON
;
2365 // VS: Testing for "ld->HasText() || InReportView()" instead of
2366 // "ld->HasText()" is needed to make empty lines in report view
2368 if ( ld
->HasText() || InReportView() )
2370 wxRect rect
= InReportView() ? GetLineRect(line
)
2371 : GetLineLabelRect(line
);
2373 if ( rect
.Inside(x
, y
) )
2374 return wxLIST_HITTEST_ONITEMLABEL
;
2380 // ----------------------------------------------------------------------------
2381 // highlight (selection) handling
2382 // ----------------------------------------------------------------------------
2384 bool wxListMainWindow::IsHighlighted(size_t line
) const
2388 return m_selStore
.IsSelected(line
);
2392 wxListLineData
*ld
= GetLine(line
);
2393 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2395 return ld
->IsHighlighted();
2399 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2405 wxArrayInt linesChanged
;
2406 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2409 // meny items changed state, refresh everything
2410 RefreshLines(lineFrom
, lineTo
);
2412 else // only a few items changed state, refresh only them
2414 size_t count
= linesChanged
.GetCount();
2415 for ( size_t n
= 0; n
< count
; n
++ )
2417 RefreshLine(linesChanged
[n
]);
2421 else // iterate over all items in non report view
2423 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2425 if ( HighlightLine(line
, highlight
) )
2433 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2439 changed
= m_selStore
.SelectItem(line
, highlight
);
2443 wxListLineData
*ld
= GetLine(line
);
2444 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2446 changed
= ld
->Highlight(highlight
);
2451 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2452 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2458 void wxListMainWindow::RefreshLine( size_t line
)
2460 if ( HasFlag(wxLC_REPORT
) )
2462 size_t visibleFrom
, visibleTo
;
2463 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2465 if ( line
< visibleFrom
|| line
> visibleTo
)
2469 wxRect rect
= GetLineRect(line
);
2471 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2472 RefreshRect( rect
);
2475 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2477 // we suppose that they are ordered by caller
2478 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2480 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2482 if ( HasFlag(wxLC_REPORT
) )
2484 size_t visibleFrom
, visibleTo
;
2485 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2487 if ( lineFrom
< visibleFrom
)
2488 lineFrom
= visibleFrom
;
2489 if ( lineTo
> visibleTo
)
2494 rect
.y
= GetLineY(lineFrom
);
2495 rect
.width
= GetClientSize().x
;
2496 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2498 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2499 RefreshRect( rect
);
2503 // TODO: this should be optimized...
2504 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2511 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2513 if ( HasFlag(wxLC_REPORT
) )
2515 size_t visibleFrom
, visibleTo
;
2516 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2518 if ( lineFrom
< visibleFrom
)
2519 lineFrom
= visibleFrom
;
2520 else if ( lineFrom
> visibleTo
)
2525 rect
.y
= GetLineY(lineFrom
);
2526 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2528 wxSize size
= GetClientSize();
2529 rect
.width
= size
.x
;
2530 // refresh till the bottom of the window
2531 rect
.height
= size
.y
- rect
.y
;
2533 RefreshRect( rect
);
2537 // TODO: how to do it more efficiently?
2542 void wxListMainWindow::RefreshSelected()
2548 if ( InReportView() )
2550 GetVisibleLinesRange(&from
, &to
);
2555 to
= GetItemCount() - 1;
2558 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2560 RefreshLine(m_current
);
2563 for ( size_t line
= from
; line
<= to
; line
++ )
2565 // NB: the test works as expected even if m_current == -1
2566 if ( line
!= m_current
&& IsHighlighted(line
) )
2573 void wxListMainWindow::Freeze()
2578 void wxListMainWindow::Thaw()
2580 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2582 if ( !--m_freezeCount
)
2588 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2590 // Note: a wxPaintDC must be constructed even if no drawing is
2591 // done (a Windows requirement).
2592 wxPaintDC
dc( this );
2594 if ( IsEmpty() || m_freezeCount
)
2596 // nothing to draw or not the moment to draw it
2602 // delay the repainting until we calculate all the items positions
2609 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2613 dc
.SetFont( GetFont() );
2615 if ( HasFlag(wxLC_REPORT
) )
2617 int lineHeight
= GetLineHeight();
2619 size_t visibleFrom
, visibleTo
;
2620 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2623 wxCoord xOrig
, yOrig
;
2624 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2626 // tell the caller cache to cache the data
2629 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2630 GetParent()->GetId());
2631 evCache
.SetEventObject( GetParent() );
2632 evCache
.m_oldItemIndex
= visibleFrom
;
2633 evCache
.m_itemIndex
= visibleTo
;
2634 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2637 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2639 rectLine
= GetLineRect(line
);
2641 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2642 rectLine
.width
, rectLine
.height
) )
2644 // don't redraw unaffected lines to avoid flicker
2648 GetLine(line
)->DrawInReportMode( &dc
,
2650 GetLineHighlightRect(line
),
2651 IsHighlighted(line
) );
2654 if ( HasFlag(wxLC_HRULES
) )
2656 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2657 wxSize clientSize
= GetClientSize();
2659 // Don't draw the first one
2660 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2663 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2664 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2665 clientSize
.x
- dev_x
, i
*lineHeight
);
2668 // Draw last horizontal rule
2669 if ( visibleTo
== GetItemCount() - 1 )
2672 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2673 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2674 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2678 // Draw vertical rules if required
2679 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2681 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2684 wxRect firstItemRect
;
2685 wxRect lastItemRect
;
2686 GetItemRect(visibleFrom
, firstItemRect
);
2687 GetItemRect(visibleTo
, lastItemRect
);
2688 int x
= firstItemRect
.GetX();
2690 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2691 for (col
= 0; col
< GetColumnCount(); col
++)
2693 int colWidth
= GetColumnWidth(col
);
2695 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2696 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2702 size_t count
= GetItemCount();
2703 for ( size_t i
= 0; i
< count
; i
++ )
2705 GetLine(i
)->Draw( &dc
);
2711 // don't draw rect outline under Max if we already have the background
2712 // color but under other platforms only draw it if we do: it is a bit
2713 // silly to draw "focus rect" if we don't have focus!
2718 #endif // __WXMAC__/!__WXMAC__
2720 dc
.SetPen( *wxBLACK_PEN
);
2721 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2722 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2729 void wxListMainWindow::HighlightAll( bool on
)
2731 if ( IsSingleSel() )
2733 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2735 // we just have one item to turn off
2736 if ( HasCurrent() && IsHighlighted(m_current
) )
2738 HighlightLine(m_current
, FALSE
);
2739 RefreshLine(m_current
);
2744 HighlightLines(0, GetItemCount() - 1, on
);
2748 void wxListMainWindow::SendNotify( size_t line
,
2749 wxEventType command
,
2752 wxListEvent
le( command
, GetParent()->GetId() );
2753 le
.SetEventObject( GetParent() );
2754 le
.m_itemIndex
= line
;
2756 // set only for events which have position
2757 if ( point
!= wxDefaultPosition
)
2758 le
.m_pointDrag
= point
;
2760 // don't try to get the line info for virtual list controls: the main
2761 // program has it anyhow and if we did it would result in accessing all
2762 // the lines, even those which are not visible now and this is precisely
2763 // what we're trying to avoid
2764 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2766 if ( line
!= (size_t)-1 )
2768 GetLine(line
)->GetItem( 0, le
.m_item
);
2770 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2772 //else: there may be no more such item
2774 GetParent()->GetEventHandler()->ProcessEvent( le
);
2777 void wxListMainWindow::ChangeCurrent(size_t current
)
2779 m_current
= current
;
2781 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2784 void wxListMainWindow::EditLabel( long item
)
2786 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2787 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2789 size_t itemEdit
= (size_t)item
;
2791 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2792 le
.SetEventObject( GetParent() );
2793 le
.m_itemIndex
= item
;
2794 wxListLineData
*data
= GetLine(itemEdit
);
2795 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2796 data
->GetItem( 0, le
.m_item
);
2797 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2799 // vetoed by user code
2803 // We have to call this here because the label in question might just have
2804 // been added and no screen update taken place.
2808 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2813 void wxListMainWindow::OnRenameTimer()
2815 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2817 EditLabel( m_current
);
2820 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2822 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2823 le
.SetEventObject( GetParent() );
2824 le
.m_itemIndex
= itemEdit
;
2826 wxListLineData
*data
= GetLine(itemEdit
);
2827 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2829 data
->GetItem( 0, le
.m_item
);
2830 le
.m_item
.m_text
= value
;
2831 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2835 #ifdef __VMS__ // Ignore unreacheable code
2836 # pragma message disable initnotreach
2839 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2841 // wxMSW seems not to notify the program about
2842 // cancelled label edits.
2845 // let owner know that the edit was cancelled
2846 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2848 // These only exist for wxTreeCtrl, which should probably be changed
2849 // le.m_editCancelled = TRUE;
2850 // le.m_label = wxEmptyString;
2852 le
.SetEventObject( GetParent() );
2853 le
.m_itemIndex
= itemEdit
;
2855 wxListLineData
*data
= GetLine(itemEdit
);
2856 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2858 data
->GetItem( 0, le
.m_item
);
2860 GetEventHandler()->ProcessEvent( le
);
2863 # pragma message enable initnotreach
2866 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2868 event
.SetEventObject( GetParent() );
2869 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2872 if ( !HasCurrent() || IsEmpty() )
2878 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2879 event
.ButtonDClick()) )
2882 int x
= event
.GetX();
2883 int y
= event
.GetY();
2884 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2886 // where did we hit it (if we did)?
2889 size_t count
= GetItemCount(),
2892 if ( HasFlag(wxLC_REPORT
) )
2894 current
= y
/ GetLineHeight();
2895 if ( current
< count
)
2896 hitResult
= HitTestLine(current
, x
, y
);
2900 // TODO: optimize it too! this is less simple than for report view but
2901 // enumerating all items is still not a way to do it!!
2902 for ( current
= 0; current
< count
; current
++ )
2904 hitResult
= HitTestLine(current
, x
, y
);
2910 if (event
.Dragging())
2912 if (m_dragCount
== 0)
2914 // we have to report the raw, physical coords as we want to be
2915 // able to call HitTest(event.m_pointDrag) from the user code to
2916 // get the item being dragged
2917 m_dragStart
= event
.GetPosition();
2922 if (m_dragCount
!= 3)
2925 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2926 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2928 wxListEvent
le( command
, GetParent()->GetId() );
2929 le
.SetEventObject( GetParent() );
2930 le
.m_itemIndex
= current
;
2931 le
.m_pointDrag
= m_dragStart
;
2932 GetParent()->GetEventHandler()->ProcessEvent( le
);
2943 // outside of any item
2947 bool forceClick
= FALSE
;
2948 if (event
.ButtonDClick())
2950 m_renameTimer
->Stop();
2951 m_lastOnSame
= FALSE
;
2953 if ( current
== m_lineLastClicked
)
2955 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2961 // the first click was on another item, so don't interpret this as
2962 // a double click, but as a simple click instead
2967 if (event
.LeftUp() && m_lastOnSame
)
2969 if ((current
== m_current
) &&
2970 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2971 HasFlag(wxLC_EDIT_LABELS
) )
2973 m_renameTimer
->Start( 100, TRUE
);
2975 m_lastOnSame
= FALSE
;
2977 else if (event
.RightDown())
2979 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2980 event
.GetPosition() );
2982 else if (event
.MiddleDown())
2984 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2986 else if ( event
.LeftDown() || forceClick
)
2988 m_lineBeforeLastClicked
= m_lineLastClicked
;
2989 m_lineLastClicked
= current
;
2991 size_t oldCurrent
= m_current
;
2993 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
2995 HighlightAll( FALSE
);
2997 ChangeCurrent(current
);
2999 ReverseHighlight(m_current
);
3001 else // multi sel & either ctrl or shift is down
3003 if (event
.ControlDown())
3005 ChangeCurrent(current
);
3007 ReverseHighlight(m_current
);
3009 else if (event
.ShiftDown())
3011 ChangeCurrent(current
);
3013 size_t lineFrom
= oldCurrent
,
3016 if ( lineTo
< lineFrom
)
3019 lineFrom
= m_current
;
3022 HighlightLines(lineFrom
, lineTo
);
3024 else // !ctrl, !shift
3026 // test in the enclosing if should make it impossible
3027 wxFAIL_MSG( _T("how did we get here?") );
3031 if (m_current
!= oldCurrent
)
3033 RefreshLine( oldCurrent
);
3036 // forceClick is only set if the previous click was on another item
3037 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3041 void wxListMainWindow::MoveToItem(size_t item
)
3043 if ( item
== (size_t)-1 )
3046 wxRect rect
= GetLineRect(item
);
3048 int client_w
, client_h
;
3049 GetClientSize( &client_w
, &client_h
);
3051 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3052 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3054 if ( HasFlag(wxLC_REPORT
) )
3056 // the next we need the range of lines shown it might be different, so
3058 ResetVisibleLinesRange();
3060 if (rect
.y
< view_y
)
3061 Scroll( -1, rect
.y
/m_yScroll
);
3062 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3063 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3067 if (rect
.x
-view_x
< 5)
3068 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3069 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3070 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3074 // ----------------------------------------------------------------------------
3075 // keyboard handling
3076 // ----------------------------------------------------------------------------
3078 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3080 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3081 _T("invalid item index in OnArrowChar()") );
3083 size_t oldCurrent
= m_current
;
3085 // in single selection we just ignore Shift as we can't select several
3087 if ( event
.ShiftDown() && !IsSingleSel() )
3089 ChangeCurrent(newCurrent
);
3091 // select all the items between the old and the new one
3092 if ( oldCurrent
> newCurrent
)
3094 newCurrent
= oldCurrent
;
3095 oldCurrent
= m_current
;
3098 HighlightLines(oldCurrent
, newCurrent
);
3102 // all previously selected items are unselected unless ctrl is held
3103 if ( !event
.ControlDown() )
3104 HighlightAll(FALSE
);
3106 ChangeCurrent(newCurrent
);
3108 // refresh the old focus to remove it
3109 RefreshLine( oldCurrent
);
3111 if ( !event
.ControlDown() )
3113 HighlightLine( m_current
, TRUE
);
3117 RefreshLine( m_current
);
3122 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3124 wxWindow
*parent
= GetParent();
3126 /* we propagate the key event up */
3127 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3128 ke
.m_shiftDown
= event
.m_shiftDown
;
3129 ke
.m_controlDown
= event
.m_controlDown
;
3130 ke
.m_altDown
= event
.m_altDown
;
3131 ke
.m_metaDown
= event
.m_metaDown
;
3132 ke
.m_keyCode
= event
.m_keyCode
;
3135 ke
.SetEventObject( parent
);
3136 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3141 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3143 wxWindow
*parent
= GetParent();
3145 /* we send a list_key event up */
3148 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3149 le
.m_itemIndex
= m_current
;
3150 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3151 le
.m_code
= event
.GetKeyCode();
3152 le
.SetEventObject( parent
);
3153 parent
->GetEventHandler()->ProcessEvent( le
);
3156 /* we propagate the char event up */
3157 wxKeyEvent
ke( wxEVT_CHAR
);
3158 ke
.m_shiftDown
= event
.m_shiftDown
;
3159 ke
.m_controlDown
= event
.m_controlDown
;
3160 ke
.m_altDown
= event
.m_altDown
;
3161 ke
.m_metaDown
= event
.m_metaDown
;
3162 ke
.m_keyCode
= event
.m_keyCode
;
3165 ke
.SetEventObject( parent
);
3166 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3168 if (event
.GetKeyCode() == WXK_TAB
)
3170 wxNavigationKeyEvent nevent
;
3171 nevent
.SetWindowChange( event
.ControlDown() );
3172 nevent
.SetDirection( !event
.ShiftDown() );
3173 nevent
.SetEventObject( GetParent()->GetParent() );
3174 nevent
.SetCurrentFocus( m_parent
);
3175 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3179 /* no item -> nothing to do */
3186 switch (event
.GetKeyCode())
3189 if ( m_current
> 0 )
3190 OnArrowChar( m_current
- 1, event
);
3194 if ( m_current
< (size_t)GetItemCount() - 1 )
3195 OnArrowChar( m_current
+ 1, event
);
3200 OnArrowChar( GetItemCount() - 1, event
);
3205 OnArrowChar( 0, event
);
3211 if ( HasFlag(wxLC_REPORT
) )
3213 steps
= m_linesPerPage
- 1;
3217 steps
= m_current
% m_linesPerPage
;
3220 int index
= m_current
- steps
;
3224 OnArrowChar( index
, event
);
3231 if ( HasFlag(wxLC_REPORT
) )
3233 steps
= m_linesPerPage
- 1;
3237 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3240 size_t index
= m_current
+ steps
;
3241 size_t count
= GetItemCount();
3242 if ( index
>= count
)
3245 OnArrowChar( index
, event
);
3250 if ( !HasFlag(wxLC_REPORT
) )
3252 int index
= m_current
- m_linesPerPage
;
3256 OnArrowChar( index
, event
);
3261 if ( !HasFlag(wxLC_REPORT
) )
3263 size_t index
= m_current
+ m_linesPerPage
;
3265 size_t count
= GetItemCount();
3266 if ( index
>= count
)
3269 OnArrowChar( index
, event
);
3274 if ( IsSingleSel() )
3276 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3278 if ( IsHighlighted(m_current
) )
3280 // don't unselect the item in single selection mode
3283 //else: select it in ReverseHighlight() below if unselected
3286 ReverseHighlight(m_current
);
3291 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3299 // ----------------------------------------------------------------------------
3301 // ----------------------------------------------------------------------------
3303 void wxListMainWindow::SetFocus()
3305 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3306 // overrides SetFocus in such way that it does never change focus from
3307 // panel's child to the panel itself. Unfortunately, we must be able to change
3308 // focus to the panel from wxListTextCtrl because the text control should
3309 // disappear when the user clicks outside it.
3311 wxWindow
*oldFocus
= FindFocus();
3313 if ( oldFocus
&& oldFocus
->GetParent() == this )
3315 wxWindow::SetFocus();
3319 wxScrolledWindow::SetFocus();
3323 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3325 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3326 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3327 // which are already drawn correctly resulting in horrible flicker - avoid
3339 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3340 event
.SetEventObject( GetParent() );
3341 GetParent()->GetEventHandler()->ProcessEvent( event
);
3344 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3351 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3353 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3355 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3357 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3359 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3361 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3363 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3365 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3367 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3371 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3373 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3375 m_normal_image_list
->GetSize( index
, width
, height
);
3377 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3379 m_small_image_list
->GetSize( index
, width
, height
);
3381 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3383 m_small_image_list
->GetSize( index
, width
, height
);
3385 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3387 m_small_image_list
->GetSize( index
, width
, height
);
3396 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3398 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3399 dc
.SetFont( GetFont() );
3402 dc
.GetTextExtent( s
, &lw
, NULL
);
3404 return lw
+ AUTOSIZE_COL_MARGIN
;
3407 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3411 // calc the spacing from the icon size
3414 if ((imageList
) && (imageList
->GetImageCount()) )
3416 imageList
->GetSize(0, width
, height
);
3419 if (which
== wxIMAGE_LIST_NORMAL
)
3421 m_normal_image_list
= imageList
;
3422 m_normal_spacing
= width
+ 8;
3425 if (which
== wxIMAGE_LIST_SMALL
)
3427 m_small_image_list
= imageList
;
3428 m_small_spacing
= width
+ 14;
3429 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3433 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3438 m_small_spacing
= spacing
;
3442 m_normal_spacing
= spacing
;
3446 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3448 return isSmall
? m_small_spacing
: m_normal_spacing
;
3451 // ----------------------------------------------------------------------------
3453 // ----------------------------------------------------------------------------
3455 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3457 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3459 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3461 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3462 item
.m_width
= GetTextLength( item
.m_text
);
3464 wxListHeaderData
*column
= node
->GetData();
3465 column
->SetItem( item
);
3467 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3469 headerWin
->m_dirty
= TRUE
;
3473 // invalidate it as it has to be recalculated
3477 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3479 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3480 _T("invalid column index") );
3482 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3483 _T("SetColumnWidth() can only be called in report mode.") );
3486 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3488 headerWin
->m_dirty
= TRUE
;
3490 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3491 wxCHECK_RET( node
, _T("no column?") );
3493 wxListHeaderData
*column
= node
->GetData();
3495 size_t count
= GetItemCount();
3497 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3499 width
= GetTextLength(column
->GetText());
3501 else if ( width
== wxLIST_AUTOSIZE
)
3505 // TODO: determine the max width somehow...
3506 width
= WIDTH_COL_DEFAULT
;
3510 wxClientDC
dc(this);
3511 dc
.SetFont( GetFont() );
3513 int max
= AUTOSIZE_COL_MARGIN
;
3515 for ( size_t i
= 0; i
< count
; i
++ )
3517 wxListLineData
*line
= GetLine(i
);
3518 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3520 wxCHECK_RET( n
, _T("no subitem?") );
3522 wxListItemData
*item
= n
->GetData();
3525 if (item
->HasImage())
3528 GetImageSize( item
->GetImage(), ix
, iy
);
3532 if (item
->HasText())
3535 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3543 width
= max
+ AUTOSIZE_COL_MARGIN
;
3547 column
->SetWidth( width
);
3549 // invalidate it as it has to be recalculated
3553 int wxListMainWindow::GetHeaderWidth() const
3555 if ( !m_headerWidth
)
3557 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3559 size_t count
= GetColumnCount();
3560 for ( size_t col
= 0; col
< count
; col
++ )
3562 self
->m_headerWidth
+= GetColumnWidth(col
);
3566 return m_headerWidth
;
3569 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3571 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3572 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3574 wxListHeaderData
*column
= node
->GetData();
3575 column
->GetItem( item
);
3578 int wxListMainWindow::GetColumnWidth( int col
) const
3580 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3581 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3583 wxListHeaderData
*column
= node
->GetData();
3584 return column
->GetWidth();
3587 // ----------------------------------------------------------------------------
3589 // ----------------------------------------------------------------------------
3591 void wxListMainWindow::SetItem( wxListItem
&item
)
3593 long id
= item
.m_itemId
;
3594 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3595 _T("invalid item index in SetItem") );
3599 wxListLineData
*line
= GetLine((size_t)id
);
3600 line
->SetItem( item
.m_col
, item
);
3603 // update the item on screen
3605 GetItemRect(id
, rectItem
);
3606 RefreshRect(rectItem
);
3609 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3611 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3612 _T("invalid list ctrl item index in SetItem") );
3614 size_t oldCurrent
= m_current
;
3615 size_t item
= (size_t)litem
; // safe because of the check above
3617 // do we need to change the focus?
3618 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3620 if ( state
& wxLIST_STATE_FOCUSED
)
3622 // don't do anything if this item is already focused
3623 if ( item
!= m_current
)
3625 ChangeCurrent(item
);
3627 if ( oldCurrent
!= (size_t)-1 )
3629 if ( IsSingleSel() )
3631 HighlightLine(oldCurrent
, FALSE
);
3634 RefreshLine(oldCurrent
);
3637 RefreshLine( m_current
);
3642 // don't do anything if this item is not focused
3643 if ( item
== m_current
)
3647 if ( IsSingleSel() )
3649 // we must unselect the old current item as well or we
3650 // might end up with more than one selected item in a
3651 // single selection control
3652 HighlightLine(oldCurrent
, FALSE
);
3655 RefreshLine( oldCurrent
);
3660 // do we need to change the selection state?
3661 if ( stateMask
& wxLIST_STATE_SELECTED
)
3663 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3665 if ( IsSingleSel() )
3669 // selecting the item also makes it the focused one in the
3671 if ( m_current
!= item
)
3673 ChangeCurrent(item
);
3675 if ( oldCurrent
!= (size_t)-1 )
3677 HighlightLine( oldCurrent
, FALSE
);
3678 RefreshLine( oldCurrent
);
3684 // only the current item may be selected anyhow
3685 if ( item
!= m_current
)
3690 if ( HighlightLine(item
, on
) )
3697 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3699 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3700 _T("invalid list ctrl item index in GetItemState()") );
3702 int ret
= wxLIST_STATE_DONTCARE
;
3704 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3706 if ( (size_t)item
== m_current
)
3707 ret
|= wxLIST_STATE_FOCUSED
;
3710 if ( stateMask
& wxLIST_STATE_SELECTED
)
3712 if ( IsHighlighted(item
) )
3713 ret
|= wxLIST_STATE_SELECTED
;
3719 void wxListMainWindow::GetItem( wxListItem
&item
) const
3721 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3722 _T("invalid item index in GetItem") );
3724 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3725 line
->GetItem( item
.m_col
, item
);
3728 // ----------------------------------------------------------------------------
3730 // ----------------------------------------------------------------------------
3732 size_t wxListMainWindow::GetItemCount() const
3734 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3737 void wxListMainWindow::SetItemCount(long count
)
3739 m_selStore
.SetItemCount(count
);
3740 m_countVirt
= count
;
3742 ResetVisibleLinesRange();
3744 // scrollbars must be reset
3748 int wxListMainWindow::GetSelectedItemCount() const
3750 // deal with the quick case first
3751 if ( IsSingleSel() )
3753 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3756 // virtual controls remmebers all its selections itself
3758 return m_selStore
.GetSelectedCount();
3760 // TODO: we probably should maintain the number of items selected even for
3761 // non virtual controls as enumerating all lines is really slow...
3762 size_t countSel
= 0;
3763 size_t count
= GetItemCount();
3764 for ( size_t line
= 0; line
< count
; line
++ )
3766 if ( GetLine(line
)->IsHighlighted() )
3773 // ----------------------------------------------------------------------------
3774 // item position/size
3775 // ----------------------------------------------------------------------------
3777 wxRect
wxListMainWindow::GetViewRect() const
3779 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3780 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3782 // we need to find the longest/tallest label
3785 const int count
= GetItemCount();
3788 for ( int i
= 0; i
< count
; i
++ )
3793 wxCoord x
= r
.GetRight(),
3803 // some fudge needed to make it look prettier
3804 xMax
+= EXTRA_WIDTH
;
3805 yMax
+= EXTRA_HEIGHT
;
3807 // account for the scrollbars if necessary
3808 const wxSize sizeAll
= GetClientSize();
3809 if ( xMax
> sizeAll
.x
)
3810 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3811 if ( yMax
> sizeAll
.y
)
3812 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3814 return wxRect(0, 0, xMax
, yMax
);
3817 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3819 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3820 _T("invalid index in GetItemRect") );
3822 // ensure that we're laid out, otherwise we could return nonsense
3825 wxConstCast(this, wxListMainWindow
)->
3826 RecalculatePositions(TRUE
/* no refresh */);
3829 rect
= GetLineRect((size_t)index
);
3831 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3834 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3837 GetItemRect(item
, rect
);
3845 // ----------------------------------------------------------------------------
3846 // geometry calculation
3847 // ----------------------------------------------------------------------------
3849 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3851 wxClientDC
dc( this );
3852 dc
.SetFont( GetFont() );
3855 if ( HasFlag(wxLC_ICON
) )
3856 iconSpacing
= m_normal_spacing
;
3857 else if ( HasFlag(wxLC_SMALL_ICON
) )
3858 iconSpacing
= m_small_spacing
;
3862 // Note that we do not call GetClientSize() here but
3863 // GetSize() and substract the border size for sunken
3864 // borders manually. This is technically incorrect,
3865 // but we need to know the client area's size WITHOUT
3866 // scrollbars here. Since we don't know if there are
3867 // any scrollbars, we use GetSize() instead. Another
3868 // solution would be to call SetScrollbars() here to
3869 // remove the scrollbars and call GetClientSize() then,
3870 // but this might result in flicker and - worse - will
3871 // reset the scrollbars to 0 which is not good at all
3872 // if you resize a dialog/window, but don't want to
3873 // reset the window scrolling. RR.
3874 // Furthermore, we actually do NOT subtract the border
3875 // width as 2 pixels is just the extra space which we
3876 // need around the actual content in the window. Other-
3877 // wise the text would e.g. touch the upper border. RR.
3880 GetSize( &clientWidth
, &clientHeight
);
3882 if ( HasFlag(wxLC_REPORT
) )
3884 // all lines have the same height
3885 int lineHeight
= GetLineHeight();
3887 // scroll one line per step
3888 m_yScroll
= lineHeight
;
3890 size_t lineCount
= GetItemCount();
3891 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3893 m_linesPerPage
= clientHeight
/ lineHeight
;
3895 ResetVisibleLinesRange();
3897 SetScrollbars( m_xScroll
, m_yScroll
,
3898 GetHeaderWidth() / m_xScroll
,
3899 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3900 GetScrollPos(wxHORIZONTAL
),
3901 GetScrollPos(wxVERTICAL
),
3906 // at first we try without any scrollbar. if the items don't
3907 // fit into the window, we recalculate after subtracting an
3908 // approximated 15 pt for the horizontal scrollbar
3910 int entireWidth
= 0;
3912 for (int tries
= 0; tries
< 2; tries
++)
3914 // We start with 4 for the border around all items
3919 // Now we have decided that the items do not fit into the
3920 // client area. Unfortunately, wxWindows sometimes thinks
3921 // that it does fit and therefore NO horizontal scrollbar
3922 // is inserted. This looks ugly, so we fudge here and make
3923 // the calculated width bigger than was actually has been
3924 // calculated. This ensures that wxScrolledWindows puts
3925 // a scrollbar at the bottom of its client area.
3926 entireWidth
+= SCROLL_UNIT_X
;
3929 // Start at 2,2 so the text does not touch the border
3934 int currentlyVisibleLines
= 0;
3936 size_t count
= GetItemCount();
3937 for (size_t i
= 0; i
< count
; i
++)
3939 currentlyVisibleLines
++;
3940 wxListLineData
*line
= GetLine(i
);
3941 line
->CalculateSize( &dc
, iconSpacing
);
3942 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
3944 wxSize sizeLine
= GetLineSize(i
);
3946 if ( maxWidth
< sizeLine
.x
)
3947 maxWidth
= sizeLine
.x
;
3950 if (currentlyVisibleLines
> m_linesPerPage
)
3951 m_linesPerPage
= currentlyVisibleLines
;
3953 // Assume that the size of the next one is the same... (FIXME)
3954 if ( y
+ sizeLine
.y
>= clientHeight
)
3956 currentlyVisibleLines
= 0;
3959 entireWidth
+= maxWidth
+6;
3963 // We have reached the last item.
3964 if ( i
== count
- 1 )
3965 entireWidth
+= maxWidth
;
3967 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
3969 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
3971 currentlyVisibleLines
= 0;
3975 if ( i
== count
- 1 )
3976 tries
= 1; // Everything fits, no second try required.
3980 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3981 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3986 // FIXME: why should we call it from here?
3993 void wxListMainWindow::RefreshAll()
3998 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3999 if ( headerWin
&& headerWin
->m_dirty
)
4001 headerWin
->m_dirty
= FALSE
;
4002 headerWin
->Refresh();
4006 void wxListMainWindow::UpdateCurrent()
4008 if ( !HasCurrent() && !IsEmpty() )
4014 long wxListMainWindow::GetNextItem( long item
,
4015 int WXUNUSED(geometry
),
4019 max
= GetItemCount();
4020 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4021 _T("invalid listctrl index in GetNextItem()") );
4023 // notice that we start with the next item (or the first one if item == -1)
4024 // and this is intentional to allow writing a simple loop to iterate over
4025 // all selected items
4029 // this is not an error because the index was ok initially, just no
4040 size_t count
= GetItemCount();
4041 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4043 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4046 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4053 // ----------------------------------------------------------------------------
4055 // ----------------------------------------------------------------------------
4057 void wxListMainWindow::DeleteItem( long lindex
)
4059 size_t count
= GetItemCount();
4061 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4062 _T("invalid item index in DeleteItem") );
4064 size_t index
= (size_t)lindex
;
4066 // we don't need to adjust the index for the previous items
4067 if ( HasCurrent() && m_current
>= index
)
4069 // if the current item is being deleted, we want the next one to
4070 // become selected - unless there is no next one - so don't adjust
4071 // m_current in this case
4072 if ( m_current
!= index
|| m_current
== count
- 1 )
4078 if ( InReportView() )
4080 ResetVisibleLinesRange();
4087 m_selStore
.OnItemDelete(index
);
4091 m_lines
.RemoveAt( index
);
4094 // we need to refresh the (vert) scrollbar as the number of items changed
4097 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4099 RefreshAfter(index
);
4102 void wxListMainWindow::DeleteColumn( int col
)
4104 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4106 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4109 delete node
->GetData();
4110 m_columns
.Erase( node
);
4114 // update all the items
4115 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4117 wxListLineData
* const line
= GetLine(i
);
4118 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4119 delete n
->GetData();
4120 line
->m_items
.Erase(n
);
4124 // invalidate it as it has to be recalculated
4128 void wxListMainWindow::DoDeleteAllItems()
4132 // nothing to do - in particular, don't send the event
4138 // to make the deletion of all items faster, we don't send the
4139 // notifications for each item deletion in this case but only one event
4140 // for all of them: this is compatible with wxMSW and documented in
4141 // DeleteAllItems() description
4143 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4144 event
.SetEventObject( GetParent() );
4145 GetParent()->GetEventHandler()->ProcessEvent( event
);
4154 if ( InReportView() )
4156 ResetVisibleLinesRange();
4162 void wxListMainWindow::DeleteAllItems()
4166 RecalculatePositions();
4169 void wxListMainWindow::DeleteEverything()
4171 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4176 // ----------------------------------------------------------------------------
4177 // scanning for an item
4178 // ----------------------------------------------------------------------------
4180 void wxListMainWindow::EnsureVisible( long index
)
4182 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4183 _T("invalid index in EnsureVisible") );
4185 // We have to call this here because the label in question might just have
4186 // been added and its position is not known yet
4189 RecalculatePositions(TRUE
/* no refresh */);
4192 MoveToItem((size_t)index
);
4195 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4202 size_t count
= GetItemCount();
4203 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4205 wxListLineData
*line
= GetLine(i
);
4206 if ( line
->GetText(0) == tmp
)
4213 long wxListMainWindow::FindItem(long start
, long data
)
4219 size_t count
= GetItemCount();
4220 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4222 wxListLineData
*line
= GetLine(i
);
4224 line
->GetItem( 0, item
);
4225 if (item
.m_data
== data
)
4232 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4234 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4236 size_t count
= GetItemCount();
4238 if ( HasFlag(wxLC_REPORT
) )
4240 size_t current
= y
/ GetLineHeight();
4241 if ( current
< count
)
4243 flags
= HitTestLine(current
, x
, y
);
4250 // TODO: optimize it too! this is less simple than for report view but
4251 // enumerating all items is still not a way to do it!!
4252 for ( size_t current
= 0; current
< count
; current
++ )
4254 flags
= HitTestLine(current
, x
, y
);
4263 // ----------------------------------------------------------------------------
4265 // ----------------------------------------------------------------------------
4267 void wxListMainWindow::InsertItem( wxListItem
&item
)
4269 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4271 size_t count
= GetItemCount();
4272 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4273 _T("invalid item index") );
4275 size_t id
= item
.m_itemId
;
4280 if ( HasFlag(wxLC_REPORT
) )
4283 ResetVisibleLinesRange();
4285 else if ( HasFlag(wxLC_LIST
) )
4287 else if ( HasFlag(wxLC_ICON
) )
4289 else if ( HasFlag(wxLC_SMALL_ICON
) )
4290 mode
= wxLC_ICON
; // no typo
4293 wxFAIL_MSG( _T("unknown mode") );
4296 wxListLineData
*line
= new wxListLineData(this);
4298 line
->SetItem( 0, item
);
4300 m_lines
.Insert( line
, id
);
4304 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4306 RefreshLines(id
, GetItemCount() - 1);
4309 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4312 if ( HasFlag(wxLC_REPORT
) )
4314 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4315 item
.m_width
= GetTextLength( item
.m_text
);
4317 wxListHeaderData
*column
= new wxListHeaderData( item
);
4318 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4321 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4322 m_columns
.Insert( node
, column
);
4326 m_columns
.Append( column
);
4331 // update all the items
4332 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4334 wxListLineData
* const line
= GetLine(i
);
4335 wxListItemData
* const data
= new wxListItemData(this);
4337 line
->m_items
.Insert(col
, data
);
4339 line
->m_items
.Append(data
);
4343 // invalidate it as it has to be recalculated
4348 // ----------------------------------------------------------------------------
4350 // ----------------------------------------------------------------------------
4352 wxListCtrlCompare list_ctrl_compare_func_2
;
4353 long list_ctrl_compare_data
;
4355 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4357 wxListLineData
*line1
= *arg1
;
4358 wxListLineData
*line2
= *arg2
;
4360 line1
->GetItem( 0, item
);
4361 long data1
= item
.m_data
;
4362 line2
->GetItem( 0, item
);
4363 long data2
= item
.m_data
;
4364 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4367 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4369 list_ctrl_compare_func_2
= fn
;
4370 list_ctrl_compare_data
= data
;
4371 m_lines
.Sort( list_ctrl_compare_func_1
);
4375 // ----------------------------------------------------------------------------
4377 // ----------------------------------------------------------------------------
4379 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4381 // update our idea of which lines are shown when we redraw the window the
4383 ResetVisibleLinesRange();
4386 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4387 wxScrolledWindow::OnScroll(event
);
4389 HandleOnScroll( event
);
4392 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4394 wxGenericListCtrl
* lc
= GetListCtrl();
4395 wxCHECK_RET( lc
, _T("no listctrl window?") );
4397 lc
->m_headerWin
->Refresh();
4398 lc
->m_headerWin
->Update();
4402 int wxListMainWindow::GetCountPerPage() const
4404 if ( !m_linesPerPage
)
4406 wxConstCast(this, wxListMainWindow
)->
4407 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4410 return m_linesPerPage
;
4413 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4415 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4417 if ( m_lineFrom
== (size_t)-1 )
4419 size_t count
= GetItemCount();
4422 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4424 // this may happen if SetScrollbars() hadn't been called yet
4425 if ( m_lineFrom
>= count
)
4426 m_lineFrom
= count
- 1;
4428 // we redraw one extra line but this is needed to make the redrawing
4429 // logic work when there is a fractional number of lines on screen
4430 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4431 if ( m_lineTo
>= count
)
4432 m_lineTo
= count
- 1;
4434 else // empty control
4437 m_lineTo
= (size_t)-1;
4441 wxASSERT_MSG( IsEmpty() ||
4442 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4443 _T("GetVisibleLinesRange() returns incorrect result") );
4451 // -------------------------------------------------------------------------------------
4452 // wxGenericListCtrl
4453 // -------------------------------------------------------------------------------------
4455 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4457 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4458 EVT_SIZE(wxGenericListCtrl::OnSize
)
4461 wxGenericListCtrl::wxGenericListCtrl()
4463 m_imageListNormal
= (wxImageListType
*) NULL
;
4464 m_imageListSmall
= (wxImageListType
*) NULL
;
4465 m_imageListState
= (wxImageListType
*) NULL
;
4467 m_ownsImageListNormal
=
4468 m_ownsImageListSmall
=
4469 m_ownsImageListState
= FALSE
;
4471 m_mainWin
= (wxListMainWindow
*) NULL
;
4472 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4476 wxGenericListCtrl::~wxGenericListCtrl()
4478 if (m_ownsImageListNormal
)
4479 delete m_imageListNormal
;
4480 if (m_ownsImageListSmall
)
4481 delete m_imageListSmall
;
4482 if (m_ownsImageListState
)
4483 delete m_imageListState
;
4486 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4488 // we use the letter "H" for calculating the needed space, basing on the current font
4490 m_headerWin
->GetTextExtent(wxT("H"), &w
, &h
);
4491 m_headerHeight
= h
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4492 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4495 void wxGenericListCtrl::CreateHeaderWindow()
4497 m_headerWin
= new wxListHeaderWindow
4499 this, -1, m_mainWin
,
4501 wxSize(GetClientSize().x
, m_headerHeight
),
4504 CalculateAndSetHeaderHeight();
4507 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4512 const wxValidator
&validator
,
4513 const wxString
&name
)
4517 m_imageListState
= (wxImageListType
*) NULL
;
4518 m_ownsImageListNormal
=
4519 m_ownsImageListSmall
=
4520 m_ownsImageListState
= FALSE
;
4522 m_mainWin
= (wxListMainWindow
*) NULL
;
4523 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4525 if ( !(style
& wxLC_MASK_TYPE
) )
4527 style
= style
| wxLC_LIST
;
4530 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4533 // don't create the inner window with the border
4534 style
&= ~wxBORDER_MASK
;
4536 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4538 if ( HasFlag(wxLC_REPORT
) )
4540 CreateHeaderWindow();
4542 if ( HasFlag(wxLC_NO_HEADER
) )
4544 // VZ: why do we create it at all then?
4545 m_headerWin
->Show( FALSE
);
4552 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4554 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4555 _T("wxLC_VIRTUAL can't be [un]set") );
4557 long flag
= GetWindowStyle();
4561 if (style
& wxLC_MASK_TYPE
)
4562 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4563 if (style
& wxLC_MASK_ALIGN
)
4564 flag
&= ~wxLC_MASK_ALIGN
;
4565 if (style
& wxLC_MASK_SORT
)
4566 flag
&= ~wxLC_MASK_SORT
;
4578 SetWindowStyleFlag( flag
);
4581 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4585 m_mainWin
->DeleteEverything();
4587 // has the header visibility changed?
4588 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4589 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4591 if ( hasHeader
!= willHaveHeader
)
4598 // don't delete, just hide, as we can reuse it later
4599 m_headerWin
->Show(FALSE
);
4601 //else: nothing to do
4603 else // must show header
4607 CreateHeaderWindow();
4609 else // already have it, just show
4611 m_headerWin
->Show( TRUE
);
4615 ResizeReportView(willHaveHeader
);
4619 wxWindow::SetWindowStyleFlag( flag
);
4622 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4624 m_mainWin
->GetColumn( col
, item
);
4628 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4630 m_mainWin
->SetColumn( col
, item
);
4634 int wxGenericListCtrl::GetColumnWidth( int col
) const
4636 return m_mainWin
->GetColumnWidth( col
);
4639 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4641 m_mainWin
->SetColumnWidth( col
, width
);
4645 int wxGenericListCtrl::GetCountPerPage() const
4647 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4650 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4652 m_mainWin
->GetItem( info
);
4656 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4658 m_mainWin
->SetItem( info
);
4662 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4665 info
.m_text
= label
;
4666 info
.m_mask
= wxLIST_MASK_TEXT
;
4667 info
.m_itemId
= index
;
4671 info
.m_image
= imageId
;
4672 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4674 m_mainWin
->SetItem(info
);
4678 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4680 return m_mainWin
->GetItemState( item
, stateMask
);
4683 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4685 m_mainWin
->SetItemState( item
, state
, stateMask
);
4689 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4692 info
.m_image
= image
;
4693 info
.m_mask
= wxLIST_MASK_IMAGE
;
4694 info
.m_itemId
= item
;
4695 m_mainWin
->SetItem( info
);
4699 wxString
wxGenericListCtrl::GetItemText( long item
) const
4701 return m_mainWin
->GetItemText(item
);
4704 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4706 m_mainWin
->SetItemText(item
, str
);
4709 long wxGenericListCtrl::GetItemData( long item
) const
4712 info
.m_itemId
= item
;
4713 m_mainWin
->GetItem( info
);
4717 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4720 info
.m_mask
= wxLIST_MASK_DATA
;
4721 info
.m_itemId
= item
;
4723 m_mainWin
->SetItem( info
);
4727 wxRect
wxGenericListCtrl::GetViewRect() const
4729 return m_mainWin
->GetViewRect();
4732 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4734 m_mainWin
->GetItemRect( item
, rect
);
4735 if ( m_mainWin
->HasHeader() )
4736 rect
.y
+= m_headerHeight
+ 1;
4740 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4742 m_mainWin
->GetItemPosition( item
, pos
);
4746 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4751 int wxGenericListCtrl::GetItemCount() const
4753 return m_mainWin
->GetItemCount();
4756 int wxGenericListCtrl::GetColumnCount() const
4758 return m_mainWin
->GetColumnCount();
4761 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4763 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4766 wxSize
wxGenericListCtrl::GetItemSpacing() const
4768 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4770 return wxSize(spacing
, spacing
);
4773 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4775 return m_mainWin
->GetItemSpacing( isSmall
);
4778 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4781 info
.m_itemId
= item
;
4782 info
.SetTextColour( col
);
4783 m_mainWin
->SetItem( info
);
4786 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4789 info
.m_itemId
= item
;
4790 m_mainWin
->GetItem( info
);
4791 return info
.GetTextColour();
4794 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4797 info
.m_itemId
= item
;
4798 info
.SetBackgroundColour( col
);
4799 m_mainWin
->SetItem( info
);
4802 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4805 info
.m_itemId
= item
;
4806 m_mainWin
->GetItem( info
);
4807 return info
.GetBackgroundColour();
4810 int wxGenericListCtrl::GetSelectedItemCount() const
4812 return m_mainWin
->GetSelectedItemCount();
4815 wxColour
wxGenericListCtrl::GetTextColour() const
4817 return GetForegroundColour();
4820 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4822 SetForegroundColour(col
);
4825 long wxGenericListCtrl::GetTopItem() const
4828 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4832 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4834 return m_mainWin
->GetNextItem( item
, geom
, state
);
4837 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4839 if (which
== wxIMAGE_LIST_NORMAL
)
4841 return m_imageListNormal
;
4843 else if (which
== wxIMAGE_LIST_SMALL
)
4845 return m_imageListSmall
;
4847 else if (which
== wxIMAGE_LIST_STATE
)
4849 return m_imageListState
;
4851 return (wxImageListType
*) NULL
;
4854 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4856 if ( which
== wxIMAGE_LIST_NORMAL
)
4858 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4859 m_imageListNormal
= imageList
;
4860 m_ownsImageListNormal
= FALSE
;
4862 else if ( which
== wxIMAGE_LIST_SMALL
)
4864 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4865 m_imageListSmall
= imageList
;
4866 m_ownsImageListSmall
= FALSE
;
4868 else if ( which
== wxIMAGE_LIST_STATE
)
4870 if (m_ownsImageListState
) delete m_imageListState
;
4871 m_imageListState
= imageList
;
4872 m_ownsImageListState
= FALSE
;
4875 m_mainWin
->SetImageList( imageList
, which
);
4878 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4880 SetImageList(imageList
, which
);
4881 if ( which
== wxIMAGE_LIST_NORMAL
)
4882 m_ownsImageListNormal
= TRUE
;
4883 else if ( which
== wxIMAGE_LIST_SMALL
)
4884 m_ownsImageListSmall
= TRUE
;
4885 else if ( which
== wxIMAGE_LIST_STATE
)
4886 m_ownsImageListState
= TRUE
;
4889 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4894 bool wxGenericListCtrl::DeleteItem( long item
)
4896 m_mainWin
->DeleteItem( item
);
4900 bool wxGenericListCtrl::DeleteAllItems()
4902 m_mainWin
->DeleteAllItems();
4906 bool wxGenericListCtrl::DeleteAllColumns()
4908 size_t count
= m_mainWin
->m_columns
.GetCount();
4909 for ( size_t n
= 0; n
< count
; n
++ )
4915 void wxGenericListCtrl::ClearAll()
4917 m_mainWin
->DeleteEverything();
4920 bool wxGenericListCtrl::DeleteColumn( int col
)
4922 m_mainWin
->DeleteColumn( col
);
4924 // if we don't have the header any longer, we need to relayout the window
4925 if ( !GetColumnCount() )
4927 ResizeReportView(FALSE
/* no header */);
4933 void wxGenericListCtrl::Edit( long item
)
4935 m_mainWin
->EditLabel( item
);
4938 bool wxGenericListCtrl::EnsureVisible( long item
)
4940 m_mainWin
->EnsureVisible( item
);
4944 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4946 return m_mainWin
->FindItem( start
, str
, partial
);
4949 long wxGenericListCtrl::FindItem( long start
, long data
)
4951 return m_mainWin
->FindItem( start
, data
);
4954 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4955 int WXUNUSED(direction
))
4960 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4962 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4965 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4967 m_mainWin
->InsertItem( info
);
4968 return info
.m_itemId
;
4971 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4974 info
.m_text
= label
;
4975 info
.m_mask
= wxLIST_MASK_TEXT
;
4976 info
.m_itemId
= index
;
4977 return InsertItem( info
);
4980 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4983 info
.m_mask
= wxLIST_MASK_IMAGE
;
4984 info
.m_image
= imageIndex
;
4985 info
.m_itemId
= index
;
4986 return InsertItem( info
);
4989 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4992 info
.m_text
= label
;
4993 info
.m_image
= imageIndex
;
4994 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4995 info
.m_itemId
= index
;
4996 return InsertItem( info
);
4999 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5001 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5003 m_mainWin
->InsertColumn( col
, item
);
5005 // if we hadn't had header before and have it now we need to relayout the
5007 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5009 ResizeReportView(TRUE
/* have header */);
5012 m_headerWin
->Refresh();
5017 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5018 int format
, int width
)
5021 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5022 item
.m_text
= heading
;
5025 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5026 item
.m_width
= width
;
5028 item
.m_format
= format
;
5030 return InsertColumn( col
, item
);
5033 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5039 // fn is a function which takes 3 long arguments: item1, item2, data.
5040 // item1 is the long data associated with a first item (NOT the index).
5041 // item2 is the long data associated with a second item (NOT the index).
5042 // data is the same value as passed to SortItems.
5043 // The return value is a negative number if the first item should precede the second
5044 // item, a positive number of the second item should precede the first,
5045 // or zero if the two items are equivalent.
5046 // data is arbitrary data to be passed to the sort function.
5048 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5050 m_mainWin
->SortItems( fn
, data
);
5054 // ----------------------------------------------------------------------------
5056 // ----------------------------------------------------------------------------
5058 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5063 ResizeReportView(m_mainWin
->HasHeader());
5065 m_mainWin
->RecalculatePositions();
5068 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5071 GetClientSize( &cw
, &ch
);
5075 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5076 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5078 else // no header window
5080 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5084 void wxGenericListCtrl::OnInternalIdle()
5086 wxWindow::OnInternalIdle();
5088 // do it only if needed
5089 if ( !m_mainWin
->m_dirty
)
5092 m_mainWin
->RecalculatePositions();
5095 // ----------------------------------------------------------------------------
5097 // ----------------------------------------------------------------------------
5099 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5103 m_mainWin
->SetBackgroundColour( colour
);
5104 m_mainWin
->m_dirty
= TRUE
;
5110 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5112 if ( !wxWindow::SetForegroundColour( colour
) )
5117 m_mainWin
->SetForegroundColour( colour
);
5118 m_mainWin
->m_dirty
= TRUE
;
5123 m_headerWin
->SetForegroundColour( colour
);
5129 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5131 if ( !wxWindow::SetFont( font
) )
5136 m_mainWin
->SetFont( font
);
5137 m_mainWin
->m_dirty
= TRUE
;
5142 m_headerWin
->SetFont( font
);
5143 CalculateAndSetHeaderHeight();
5151 // ----------------------------------------------------------------------------
5152 // methods forwarded to m_mainWin
5153 // ----------------------------------------------------------------------------
5155 #if wxUSE_DRAG_AND_DROP
5157 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5159 m_mainWin
->SetDropTarget( dropTarget
);
5162 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5164 return m_mainWin
->GetDropTarget();
5167 #endif // wxUSE_DRAG_AND_DROP
5169 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5171 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5174 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5176 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5179 wxColour
wxGenericListCtrl::GetForegroundColour() const
5181 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5184 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5187 return m_mainWin
->PopupMenu( menu
, x
, y
);
5190 #endif // wxUSE_MENUS
5193 void wxGenericListCtrl::SetFocus()
5195 /* The test in window.cpp fails as we are a composite
5196 window, so it checks against "this", but not m_mainWin. */
5197 if ( FindFocus() != this )
5198 m_mainWin
->SetFocus();
5201 // ----------------------------------------------------------------------------
5202 // virtual list control support
5203 // ----------------------------------------------------------------------------
5205 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5207 // this is a pure virtual function, in fact - which is not really pure
5208 // because the controls which are not virtual don't need to implement it
5209 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5211 return wxEmptyString
;
5214 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5217 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5222 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5224 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5225 _T("invalid item index in OnGetItemAttr()") );
5227 // no attributes by default
5231 void wxGenericListCtrl::SetItemCount(long count
)
5233 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5235 m_mainWin
->SetItemCount(count
);
5238 void wxGenericListCtrl::RefreshItem(long item
)
5240 m_mainWin
->RefreshLine(item
);
5243 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5245 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5249 * Generic wxListCtrl is more or less a container for two other
5250 * windows which drawings are done upon. These are namely
5251 * 'm_headerWin' and 'm_mainWin'.
5252 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5253 * behaviour wxListCtrl has under wxMSW.
5255 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5259 // The easy case, no rectangle specified.
5261 m_headerWin
->Refresh(eraseBackground
);
5264 m_mainWin
->Refresh(eraseBackground
);
5268 // Refresh the header window
5271 wxRect rectHeader
= m_headerWin
->GetRect();
5272 rectHeader
.Intersect(*rect
);
5273 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5276 m_headerWin
->GetPosition(&x
, &y
);
5277 rectHeader
.Offset(-x
, -y
);
5278 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5283 // Refresh the main window
5286 wxRect rectMain
= m_mainWin
->GetRect();
5287 rectMain
.Intersect(*rect
);
5288 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5291 m_mainWin
->GetPosition(&x
, &y
);
5292 rectMain
.Offset(-x
, -y
);
5293 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5299 void wxGenericListCtrl::Freeze()
5301 m_mainWin
->Freeze();
5304 void wxGenericListCtrl::Thaw()
5309 #endif // wxUSE_LISTCTRL