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"
76 #include "wx/mac/private.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // // the height of the header window (FIXME: should depend on its font!)
110 // static const int HEADER_HEIGHT = 23;
112 static const int SCROLL_UNIT_X
= 15;
114 // the spacing between the lines (in report mode)
115 static const int LINE_SPACING
= 0;
117 // extra margins around the text label
118 static const int EXTRA_WIDTH
= 4;
119 static const int EXTRA_HEIGHT
= 4;
121 // margin between the window and the items
122 static const int EXTRA_BORDER_X
= 2;
123 static const int EXTRA_BORDER_Y
= 2;
125 // offset for the header window
126 static const int HEADER_OFFSET_X
= 1;
127 static const int HEADER_OFFSET_Y
= 1;
129 // margin between rows of icons in [small] icon view
130 static const int MARGIN_BETWEEN_ROWS
= 6;
132 // when autosizing the columns, add some slack
133 static const int AUTOSIZE_COL_MARGIN
= 10;
135 // default and minimal widths for the header columns
136 static const int WIDTH_COL_DEFAULT
= 80;
137 static const int WIDTH_COL_MIN
= 10;
139 // the space between the image and the text in the report mode
140 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
142 // ============================================================================
144 // ============================================================================
146 //-----------------------------------------------------------------------------
147 // wxListItemData (internal)
148 //-----------------------------------------------------------------------------
150 class WXDLLEXPORT wxListItemData
153 wxListItemData(wxListMainWindow
*owner
);
156 void SetItem( const wxListItem
&info
);
157 void SetImage( int image
) { m_image
= image
; }
158 void SetData( long data
) { m_data
= data
; }
159 void SetPosition( int x
, int y
);
160 void SetSize( int width
, int height
);
162 bool HasText() const { return !m_text
.empty(); }
163 const wxString
& GetText() const { return m_text
; }
164 void SetText(const wxString
& text
) { m_text
= text
; }
166 // we can't use empty string for measuring the string width/height, so
167 // always return something
168 wxString
GetTextForMeasuring() const
170 wxString s
= GetText();
177 bool IsHit( int x
, int y
) const;
181 int GetWidth() const;
182 int GetHeight() const;
184 int GetImage() const { return m_image
; }
185 bool HasImage() const { return GetImage() != -1; }
187 void GetItem( wxListItem
&info
) const;
189 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
190 wxListItemAttr
*GetAttr() const { return m_attr
; }
193 // the item image or -1
196 // user data associated with the item
199 // the item coordinates are not used in report mode, instead this pointer
200 // is NULL and the owner window is used to retrieve the item position and
204 // the list ctrl we are in
205 wxListMainWindow
*m_owner
;
207 // custom attributes or NULL
208 wxListItemAttr
*m_attr
;
211 // common part of all ctors
217 //-----------------------------------------------------------------------------
218 // wxListHeaderData (internal)
219 //-----------------------------------------------------------------------------
221 class WXDLLEXPORT wxListHeaderData
: public wxObject
225 wxListHeaderData( const wxListItem
&info
);
226 void SetItem( const wxListItem
&item
);
227 void SetPosition( int x
, int y
);
228 void SetWidth( int w
);
229 void SetFormat( int format
);
230 void SetHeight( int h
);
231 bool HasImage() const;
233 bool HasText() const { return !m_text
.empty(); }
234 const wxString
& GetText() const { return m_text
; }
235 void SetText(const wxString
& text
) { m_text
= text
; }
237 void GetItem( wxListItem
&item
);
239 bool IsHit( int x
, int y
) const;
240 int GetImage() const;
241 int GetWidth() const;
242 int GetFormat() const;
258 //-----------------------------------------------------------------------------
259 // wxListLineData (internal)
260 //-----------------------------------------------------------------------------
262 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
263 #include "wx/listimpl.cpp"
264 WX_DEFINE_LIST(wxListItemDataList
);
269 // the list of subitems: only may have more than one item in report mode
270 wxListItemDataList m_items
;
272 // this is not used in report view
284 // the part to be highlighted
285 wxRect m_rectHighlight
;
287 // extend all our rects to be centered inside theo ne of given width
288 void ExtendWidth(wxCoord w
)
290 wxASSERT_MSG( m_rectAll
.width
<= w
,
291 _T("width can only be increased") );
294 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
)/2;
295 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
)/2;
296 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
)/2;
300 // is this item selected? [NB: not used in virtual mode]
303 // back pointer to the list ctrl
304 wxListMainWindow
*m_owner
;
307 wxListLineData(wxListMainWindow
*owner
);
311 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
315 // are we in report mode?
316 inline bool InReportView() const;
318 // are we in virtual report mode?
319 inline bool IsVirtual() const;
321 // these 2 methods shouldn't be called for report view controls, in that
322 // case we determine our position/size ourselves
324 // calculate the size of the line
325 void CalculateSize( wxDC
*dc
, int spacing
);
327 // remember the position this line appears at
328 void SetPosition( int x
, int y
, int spacing
);
332 void SetImage( int image
) { SetImage(0, image
); }
333 int GetImage() const { return GetImage(0); }
334 bool HasImage() const { return GetImage() != -1; }
335 bool HasText() const { return !GetText(0).empty(); }
337 void SetItem( int index
, const wxListItem
&info
);
338 void GetItem( int index
, wxListItem
&info
);
340 wxString
GetText(int index
) const;
341 void SetText( int index
, const wxString s
);
343 wxListItemAttr
*GetAttr() const;
344 void SetAttr(wxListItemAttr
*attr
);
346 // return true if the highlighting really changed
347 bool Highlight( bool on
);
349 void ReverseHighlight();
351 bool IsHighlighted() const
353 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
355 return m_highlighted
;
358 // draw the line on the given DC in icon/list mode
359 void Draw( wxDC
*dc
);
361 // the same in report mode
362 void DrawInReportMode( wxDC
*dc
,
364 const wxRect
& rectHL
,
368 // set the line to contain num items (only can be > 1 in report mode)
369 void InitItems( int num
);
371 // get the mode (i.e. style) of the list control
372 inline int GetMode() const;
374 // prepare the DC for drawing with these item's attributes, return true if
375 // we need to draw the items background to highlight it, false otherwise
376 bool SetAttributes(wxDC
*dc
,
377 const wxListItemAttr
*attr
,
380 // draw the text on the DC with the correct justification; also add an
381 // ellipsis if the text is too large to fit in the current width
382 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
384 // these are only used by GetImage/SetImage above, we don't support images
385 // with subitems at the public API level yet
386 void SetImage( int index
, int image
);
387 int GetImage( int index
) const;
390 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
391 #include "wx/arrimpl.cpp"
392 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
394 //-----------------------------------------------------------------------------
395 // wxListHeaderWindow (internal)
396 //-----------------------------------------------------------------------------
398 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
401 wxListMainWindow
*m_owner
;
402 wxCursor
*m_currentCursor
;
403 wxCursor
*m_resizeCursor
;
406 // column being resized or -1
409 // divider line position in logical (unscrolled) coords
412 // minimal position beyond which the divider line can't be dragged in
417 wxListHeaderWindow();
419 wxListHeaderWindow( wxWindow
*win
,
421 wxListMainWindow
*owner
,
422 const wxPoint
&pos
= wxDefaultPosition
,
423 const wxSize
&size
= wxDefaultSize
,
425 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
427 virtual ~wxListHeaderWindow();
430 void AdjustDC(wxDC
& dc
);
432 void OnPaint( wxPaintEvent
&event
);
433 void OnMouse( wxMouseEvent
&event
);
434 void OnSetFocus( wxFocusEvent
&event
);
440 // common part of all ctors
443 // generate and process the list event of the given type, return true if
444 // it wasn't vetoed, i.e. if we should proceed
445 bool SendListEvent(wxEventType type
, wxPoint pos
);
447 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
448 DECLARE_EVENT_TABLE()
451 //-----------------------------------------------------------------------------
452 // wxListRenameTimer (internal)
453 //-----------------------------------------------------------------------------
455 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
458 wxListMainWindow
*m_owner
;
461 wxListRenameTimer( wxListMainWindow
*owner
);
465 //-----------------------------------------------------------------------------
466 // wxListTextCtrl (internal)
467 //-----------------------------------------------------------------------------
469 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
472 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
475 void OnChar( wxKeyEvent
&event
);
476 void OnKeyUp( wxKeyEvent
&event
);
477 void OnKillFocus( wxFocusEvent
&event
);
479 bool AcceptChanges();
483 wxListMainWindow
*m_owner
;
484 wxString m_startValue
;
488 DECLARE_EVENT_TABLE()
491 //-----------------------------------------------------------------------------
492 // wxListMainWindow (internal)
493 //-----------------------------------------------------------------------------
495 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
496 #include "wx/listimpl.cpp"
497 WX_DEFINE_LIST(wxListHeaderDataList
);
499 class wxListMainWindow
: public wxScrolledWindow
503 wxListMainWindow( wxWindow
*parent
,
505 const wxPoint
& pos
= wxDefaultPosition
,
506 const wxSize
& size
= wxDefaultSize
,
508 const wxString
&name
= _T("listctrlmainwindow") );
510 virtual ~wxListMainWindow();
512 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
514 // return true if this is a virtual list control
515 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
517 // return true if the control is in report mode
518 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
520 // return true if we are in single selection mode, false if multi sel
521 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
523 // do we have a header window?
524 bool HasHeader() const
525 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
527 void HighlightAll( bool on
);
529 // all these functions only do something if the line is currently visible
531 // change the line "selected" state, return TRUE if it really changed
532 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
534 // as HighlightLine() but do it for the range of lines: this is incredibly
535 // more efficient for virtual list controls!
537 // NB: unlike HighlightLine() this one does refresh the lines on screen
538 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
540 // toggle the line state and refresh it
541 void ReverseHighlight( size_t line
)
542 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
544 // return true if the line is highlighted
545 bool IsHighlighted(size_t line
) const;
547 // refresh one or several lines at once
548 void RefreshLine( size_t line
);
549 void RefreshLines( size_t lineFrom
, size_t lineTo
);
551 // refresh all selected items
552 void RefreshSelected();
554 // refresh all lines below the given one: the difference with
555 // RefreshLines() is that the index here might not be a valid one (happens
556 // when the last line is deleted)
557 void RefreshAfter( size_t lineFrom
);
559 // the methods which are forwarded to wxListLineData itself in list/icon
560 // modes but are here because the lines don't store their positions in the
563 // get the bound rect for the entire line
564 wxRect
GetLineRect(size_t line
) const;
566 // get the bound rect of the label
567 wxRect
GetLineLabelRect(size_t line
) const;
569 // get the bound rect of the items icon (only may be called if we do have
571 wxRect
GetLineIconRect(size_t line
) const;
573 // get the rect to be highlighted when the item has focus
574 wxRect
GetLineHighlightRect(size_t line
) const;
576 // get the size of the total line rect
577 wxSize
GetLineSize(size_t line
) const
578 { return GetLineRect(line
).GetSize(); }
580 // return the hit code for the corresponding position (in this line)
581 long HitTestLine(size_t line
, int x
, int y
) const;
583 // bring the selected item into view, scrolling to it if necessary
584 void MoveToItem(size_t item
);
586 // bring the current item into view
587 void MoveToFocus() { MoveToItem(m_current
); }
589 // start editing the label of the given item
590 void EditLabel( long item
);
592 // suspend/resume redrawing the control
598 void OnRenameTimer();
599 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
600 void OnRenameCancelled(size_t itemEdit
);
602 void OnMouse( wxMouseEvent
&event
);
604 // called to switch the selection from the current item to newCurrent,
605 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
607 void OnChar( wxKeyEvent
&event
);
608 void OnKeyDown( wxKeyEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
611 void OnScroll(wxScrollWinEvent
& event
) ;
613 void OnPaint( wxPaintEvent
&event
);
615 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
616 void GetImageSize( int index
, int &width
, int &height
) const;
617 int GetTextLength( const wxString
&s
) const;
619 void SetImageList( wxImageListType
*imageList
, int which
);
620 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
621 int GetItemSpacing( bool isSmall
= FALSE
);
623 void SetColumn( int col
, wxListItem
&item
);
624 void SetColumnWidth( int col
, int width
);
625 void GetColumn( int col
, wxListItem
&item
) const;
626 int GetColumnWidth( int col
) const;
627 int GetColumnCount() const { return m_columns
.GetCount(); }
629 // returns the sum of the heights of all columns
630 int GetHeaderWidth() const;
632 int GetCountPerPage() const;
634 void SetItem( wxListItem
&item
);
635 void GetItem( wxListItem
&item
) const;
636 void SetItemState( long item
, long state
, long stateMask
);
637 int GetItemState( long item
, long stateMask
) const;
638 void GetItemRect( long index
, wxRect
&rect
) const;
639 wxRect
GetViewRect() const;
640 bool GetItemPosition( long item
, wxPoint
& pos
) const;
641 int GetSelectedItemCount() const;
643 wxString
GetItemText(long item
) const
646 info
.m_itemId
= item
;
651 void SetItemText(long item
, const wxString
& value
)
654 info
.m_mask
= wxLIST_MASK_TEXT
;
655 info
.m_itemId
= item
;
660 // set the scrollbars and update the positions of the items
661 void RecalculatePositions(bool noRefresh
= FALSE
);
663 // refresh the window and the header
666 long GetNextItem( long item
, int geometry
, int state
) const;
667 void DeleteItem( long index
);
668 void DeleteAllItems();
669 void DeleteColumn( int col
);
670 void DeleteEverything();
671 void EnsureVisible( long index
);
672 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
673 long FindItem( long start
, long data
);
674 long HitTest( int x
, int y
, int &flags
);
675 void InsertItem( wxListItem
&item
);
676 void InsertColumn( long col
, wxListItem
&item
);
677 void SortItems( wxListCtrlCompare fn
, long data
);
679 size_t GetItemCount() const;
680 bool IsEmpty() const { return GetItemCount() == 0; }
681 void SetItemCount(long count
);
683 // change the current (== focused) item, send a notification event
684 void ChangeCurrent(size_t current
);
685 void ResetCurrent() { ChangeCurrent((size_t)-1); }
686 bool HasCurrent() const { return m_current
!= (size_t)-1; }
688 // send out a wxListEvent
689 void SendNotify( size_t line
,
691 wxPoint point
= wxDefaultPosition
);
693 // override base class virtual to reset m_lineHeight when the font changes
694 virtual bool SetFont(const wxFont
& font
)
696 if ( !wxScrolledWindow::SetFont(font
) )
704 // these are for wxListLineData usage only
706 // get the backpointer to the list ctrl
707 wxGenericListCtrl
*GetListCtrl() const
709 return wxStaticCast(GetParent(), wxGenericListCtrl
);
712 // get the height of all lines (assuming they all do have the same height)
713 wxCoord
GetLineHeight() const;
715 // get the y position of the given line (only for report view)
716 wxCoord
GetLineY(size_t line
) const;
718 // get the brush to use for the item highlighting
719 wxBrush
*GetHighlightBrush() const
721 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
725 // the array of all line objects for a non virtual list control (for the
726 // virtual list control we only ever use m_lines[0])
727 wxListLineDataArray m_lines
;
729 // the list of column objects
730 wxListHeaderDataList m_columns
;
732 // currently focused item or -1
735 // the number of lines per page
738 // this flag is set when something which should result in the window
739 // redrawing happens (i.e. an item was added or deleted, or its appearance
740 // changed) and OnPaint() doesn't redraw the window while it is set which
741 // allows to minimize the number of repaintings when a lot of items are
742 // being added. The real repainting occurs only after the next OnIdle()
746 wxColour
*m_highlightColour
;
747 wxImageListType
*m_small_image_list
;
748 wxImageListType
*m_normal_image_list
;
750 int m_normal_spacing
;
754 wxTimer
*m_renameTimer
;
759 // for double click logic
760 size_t m_lineLastClicked
,
761 m_lineBeforeLastClicked
;
764 // the total count of items in a virtual list control
767 // the object maintaining the items selection state, only used in virtual
769 wxSelectionStore m_selStore
;
771 // common part of all ctors
774 // get the line data for the given index
775 wxListLineData
*GetLine(size_t n
) const
777 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
781 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
789 // get a dummy line which can be used for geometry calculations and such:
790 // you must use GetLine() if you want to really draw the line
791 wxListLineData
*GetDummyLine() const;
793 // cache the line data of the n-th line in m_lines[0]
794 void CacheLineData(size_t line
);
796 // get the range of visible lines
797 void GetVisibleLinesRange(size_t *from
, size_t *to
);
799 // force us to recalculate the range of visible lines
800 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
802 // get the colour to be used for drawing the rules
803 wxColour
GetRuleColour() const
808 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
813 // initialize the current item if needed
814 void UpdateCurrent();
816 // delete all items but don't refresh: called from dtor
817 void DoDeleteAllItems();
819 // the height of one line using the current font
820 wxCoord m_lineHeight
;
822 // the total header width or 0 if not calculated yet
823 wxCoord m_headerWidth
;
825 // the first and last lines being shown on screen right now (inclusive),
826 // both may be -1 if they must be calculated so never access them directly:
827 // use GetVisibleLinesRange() above instead
831 // the brushes to use for item highlighting when we do/don't have focus
832 wxBrush
*m_highlightBrush
,
833 *m_highlightUnfocusedBrush
;
835 // if this is > 0, the control is frozen and doesn't redraw itself
836 size_t m_freezeCount
;
838 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
839 DECLARE_EVENT_TABLE()
841 friend class wxGenericListCtrl
;
844 // ============================================================================
846 // ============================================================================
848 //-----------------------------------------------------------------------------
850 //-----------------------------------------------------------------------------
852 wxListItemData::~wxListItemData()
854 // in the virtual list control the attributes are managed by the main
855 // program, so don't delete them
856 if ( !m_owner
->IsVirtual() )
864 void wxListItemData::Init()
872 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
878 if ( owner
->InReportView() )
888 void wxListItemData::SetItem( const wxListItem
&info
)
890 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
891 SetText(info
.m_text
);
892 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
893 m_image
= info
.m_image
;
894 if ( info
.m_mask
& wxLIST_MASK_DATA
)
895 m_data
= info
.m_data
;
897 if ( info
.HasAttributes() )
900 *m_attr
= *info
.GetAttributes();
902 m_attr
= new wxListItemAttr(*info
.GetAttributes());
910 m_rect
->width
= info
.m_width
;
914 void wxListItemData::SetPosition( int x
, int y
)
916 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
922 void wxListItemData::SetSize( int width
, int height
)
924 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
927 m_rect
->width
= width
;
929 m_rect
->height
= height
;
932 bool wxListItemData::IsHit( int x
, int y
) const
934 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
936 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
939 int wxListItemData::GetX() const
941 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
946 int wxListItemData::GetY() const
948 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
953 int wxListItemData::GetWidth() const
955 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
957 return m_rect
->width
;
960 int wxListItemData::GetHeight() const
962 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
964 return m_rect
->height
;
967 void wxListItemData::GetItem( wxListItem
&info
) const
969 info
.m_text
= m_text
;
970 info
.m_image
= m_image
;
971 info
.m_data
= m_data
;
975 if ( m_attr
->HasTextColour() )
976 info
.SetTextColour(m_attr
->GetTextColour());
977 if ( m_attr
->HasBackgroundColour() )
978 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
979 if ( m_attr
->HasFont() )
980 info
.SetFont(m_attr
->GetFont());
984 //-----------------------------------------------------------------------------
986 //-----------------------------------------------------------------------------
988 void wxListHeaderData::Init()
999 wxListHeaderData::wxListHeaderData()
1004 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1011 void wxListHeaderData::SetItem( const wxListItem
&item
)
1013 m_mask
= item
.m_mask
;
1015 if ( m_mask
& wxLIST_MASK_TEXT
)
1016 m_text
= item
.m_text
;
1018 if ( m_mask
& wxLIST_MASK_IMAGE
)
1019 m_image
= item
.m_image
;
1021 if ( m_mask
& wxLIST_MASK_FORMAT
)
1022 m_format
= item
.m_format
;
1024 if ( m_mask
& wxLIST_MASK_WIDTH
)
1025 SetWidth(item
.m_width
);
1028 void wxListHeaderData::SetPosition( int x
, int y
)
1034 void wxListHeaderData::SetHeight( int h
)
1039 void wxListHeaderData::SetWidth( int w
)
1043 m_width
= WIDTH_COL_DEFAULT
;
1044 else if (m_width
< WIDTH_COL_MIN
)
1045 m_width
= WIDTH_COL_MIN
;
1048 void wxListHeaderData::SetFormat( int format
)
1053 bool wxListHeaderData::HasImage() const
1055 return m_image
!= -1;
1058 bool wxListHeaderData::IsHit( int x
, int y
) const
1060 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1063 void wxListHeaderData::GetItem( wxListItem
& item
)
1065 item
.m_mask
= m_mask
;
1066 item
.m_text
= m_text
;
1067 item
.m_image
= m_image
;
1068 item
.m_format
= m_format
;
1069 item
.m_width
= m_width
;
1072 int wxListHeaderData::GetImage() const
1077 int wxListHeaderData::GetWidth() const
1082 int wxListHeaderData::GetFormat() const
1087 //-----------------------------------------------------------------------------
1089 //-----------------------------------------------------------------------------
1091 inline int wxListLineData::GetMode() const
1093 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1096 inline bool wxListLineData::InReportView() const
1098 return m_owner
->HasFlag(wxLC_REPORT
);
1101 inline bool wxListLineData::IsVirtual() const
1103 return m_owner
->IsVirtual();
1106 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1110 if ( InReportView() )
1116 m_gi
= new GeometryInfo
;
1119 m_highlighted
= FALSE
;
1121 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1124 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1126 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1127 wxCHECK_RET( node
, _T("no subitems at all??") );
1129 wxListItemData
*item
= node
->GetData();
1134 switch ( GetMode() )
1137 case wxLC_SMALL_ICON
:
1138 m_gi
->m_rectAll
.width
= spacing
;
1140 s
= item
->GetText();
1145 m_gi
->m_rectLabel
.width
=
1146 m_gi
->m_rectLabel
.height
= 0;
1150 dc
->GetTextExtent( s
, &lw
, &lh
);
1154 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1156 m_gi
->m_rectAll
.width
= lw
;
1158 m_gi
->m_rectLabel
.width
= lw
;
1159 m_gi
->m_rectLabel
.height
= lh
;
1162 if (item
->HasImage())
1165 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1166 m_gi
->m_rectIcon
.width
= w
+ 8;
1167 m_gi
->m_rectIcon
.height
= h
+ 8;
1169 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1170 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1171 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1172 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1175 if ( item
->HasText() )
1177 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1178 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1180 else // no text, highlight the icon
1182 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1183 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1188 s
= item
->GetTextForMeasuring();
1190 dc
->GetTextExtent( s
, &lw
, &lh
);
1194 m_gi
->m_rectLabel
.width
= lw
;
1195 m_gi
->m_rectLabel
.height
= lh
;
1197 m_gi
->m_rectAll
.width
= lw
;
1198 m_gi
->m_rectAll
.height
= lh
;
1200 if (item
->HasImage())
1203 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1204 m_gi
->m_rectIcon
.width
= w
;
1205 m_gi
->m_rectIcon
.height
= h
;
1207 m_gi
->m_rectAll
.width
+= 4 + w
;
1208 if (h
> m_gi
->m_rectAll
.height
)
1209 m_gi
->m_rectAll
.height
= h
;
1212 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1213 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1217 wxFAIL_MSG( _T("unexpected call to SetSize") );
1221 wxFAIL_MSG( _T("unknown mode") );
1225 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1227 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1228 wxCHECK_RET( node
, _T("no subitems at all??") );
1230 wxListItemData
*item
= node
->GetData();
1232 switch ( GetMode() )
1235 case wxLC_SMALL_ICON
:
1236 m_gi
->m_rectAll
.x
= x
;
1237 m_gi
->m_rectAll
.y
= y
;
1239 if ( item
->HasImage() )
1241 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1242 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1243 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1246 if ( item
->HasText() )
1248 if (m_gi
->m_rectAll
.width
> spacing
)
1249 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1251 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1252 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1253 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1254 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1256 else // no text, highlight the icon
1258 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1259 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1264 m_gi
->m_rectAll
.x
= x
;
1265 m_gi
->m_rectAll
.y
= y
;
1267 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1268 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1269 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1271 if (item
->HasImage())
1273 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1274 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1275 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1279 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1284 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1288 wxFAIL_MSG( _T("unknown mode") );
1292 void wxListLineData::InitItems( int num
)
1294 for (int i
= 0; i
< num
; i
++)
1295 m_items
.Append( new wxListItemData(m_owner
) );
1298 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1300 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1301 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1303 wxListItemData
*item
= node
->GetData();
1304 item
->SetItem( info
);
1307 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1309 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1312 wxListItemData
*item
= node
->GetData();
1313 item
->GetItem( info
);
1317 wxString
wxListLineData::GetText(int index
) const
1321 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1324 wxListItemData
*item
= node
->GetData();
1325 s
= item
->GetText();
1331 void wxListLineData::SetText( int index
, const wxString s
)
1333 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1336 wxListItemData
*item
= node
->GetData();
1341 void wxListLineData::SetImage( int index
, int image
)
1343 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1344 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1346 wxListItemData
*item
= node
->GetData();
1347 item
->SetImage(image
);
1350 int wxListLineData::GetImage( int index
) const
1352 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1353 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1355 wxListItemData
*item
= node
->GetData();
1356 return item
->GetImage();
1359 wxListItemAttr
*wxListLineData::GetAttr() const
1361 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1362 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1364 wxListItemData
*item
= node
->GetData();
1365 return item
->GetAttr();
1368 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1370 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1371 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1373 wxListItemData
*item
= node
->GetData();
1374 item
->SetAttr(attr
);
1377 bool wxListLineData::SetAttributes(wxDC
*dc
,
1378 const wxListItemAttr
*attr
,
1381 wxWindow
*listctrl
= m_owner
->GetParent();
1385 // don't use foreground colour for drawing highlighted items - this might
1386 // make them completely invisible (and there is no way to do bit
1387 // arithmetics on wxColour, unfortunately)
1391 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1395 if ( attr
&& attr
->HasTextColour() )
1397 colText
= attr
->GetTextColour();
1401 colText
= listctrl
->GetForegroundColour();
1405 dc
->SetTextForeground(colText
);
1409 if ( attr
&& attr
->HasFont() )
1411 font
= attr
->GetFont();
1415 font
= listctrl
->GetFont();
1421 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1422 if ( highlighted
|| hasBgCol
)
1426 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1430 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1433 dc
->SetPen( *wxTRANSPARENT_PEN
);
1441 void wxListLineData::Draw( wxDC
*dc
)
1443 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1444 wxCHECK_RET( node
, _T("no subitems at all??") );
1446 bool highlighted
= IsHighlighted();
1448 wxListItemAttr
*attr
= GetAttr();
1450 if ( SetAttributes(dc
, attr
, highlighted
) )
1452 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1455 // just for debugging to better see where the items are
1457 dc
->SetPen(*wxRED_PEN
);
1458 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1459 dc
->DrawRectangle( m_gi
->m_rectAll
);
1460 dc
->SetPen(*wxGREEN_PEN
);
1461 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1464 wxListItemData
*item
= node
->GetData();
1465 if (item
->HasImage())
1467 // centre the image inside our rectangle, this looks nicer when items
1468 // ae aligned in a row
1469 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1471 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1474 if (item
->HasText())
1476 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1478 wxDCClipper
clipper(*dc
, rectLabel
);
1479 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1483 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1485 const wxRect
& rectHL
,
1488 // TODO: later we should support setting different attributes for
1489 // different columns - to do it, just add "col" argument to
1490 // GetAttr() and move these lines into the loop below
1491 wxListItemAttr
*attr
= GetAttr();
1492 if ( SetAttributes(dc
, attr
, highlighted
) )
1494 dc
->DrawRectangle( rectHL
);
1497 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1498 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1501 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1503 node
= node
->GetNext(), col
++ )
1505 wxListItemData
*item
= node
->GetData();
1507 int width
= m_owner
->GetColumnWidth(col
);
1511 if ( item
->HasImage() )
1514 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1515 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1517 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1523 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1525 if ( item
->HasText() )
1527 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1532 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1533 const wxString
&text
,
1539 wxString drawntext
, ellipsis
;
1540 wxCoord w
, h
, base_w
;
1543 // determine if the string can fit inside the current width
1544 dc
->GetTextExtent(text
, &w
, &h
);
1547 // it can, draw it using the items alignment
1548 m_owner
->GetColumn(col
, item
);
1549 switch ( item
.GetAlign() )
1552 wxFAIL_MSG( _T("unknown list item format") );
1555 case wxLIST_FORMAT_LEFT
:
1559 case wxLIST_FORMAT_RIGHT
:
1563 case wxLIST_FORMAT_CENTER
:
1564 x
+= (width
- w
) / 2;
1568 dc
->DrawText(text
, x
, y
);
1570 else // otherwise, truncate and add an ellipsis if possible
1572 // determine the base width
1573 ellipsis
= wxString(wxT("..."));
1574 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1576 // continue until we have enough space or only one character left
1578 size_t len
= text
.Length();
1579 drawntext
= text
.Left(len
);
1582 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1583 drawntext
.RemoveLast();
1586 if (w
+ base_w
<= width
)
1590 // if still not enough space, remove ellipsis characters
1591 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1593 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1594 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1597 // now draw the text
1598 dc
->DrawText(drawntext
, x
, y
);
1599 dc
->DrawText(ellipsis
, x
+ w
, y
);
1603 bool wxListLineData::Highlight( bool on
)
1605 wxCHECK_MSG( !IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1607 if ( on
== m_highlighted
)
1615 void wxListLineData::ReverseHighlight( void )
1617 Highlight(!IsHighlighted());
1620 //-----------------------------------------------------------------------------
1621 // wxListHeaderWindow
1622 //-----------------------------------------------------------------------------
1624 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1626 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1627 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1628 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1629 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1632 void wxListHeaderWindow::Init()
1634 m_currentCursor
= (wxCursor
*) NULL
;
1635 m_isDragging
= FALSE
;
1639 wxListHeaderWindow::wxListHeaderWindow()
1643 m_owner
= (wxListMainWindow
*) NULL
;
1644 m_resizeCursor
= (wxCursor
*) NULL
;
1647 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1649 wxListMainWindow
*owner
,
1653 const wxString
&name
)
1654 : wxWindow( win
, id
, pos
, size
, style
, name
)
1659 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1661 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1664 wxListHeaderWindow::~wxListHeaderWindow()
1666 delete m_resizeCursor
;
1669 #ifdef __WXUNIVERSAL__
1670 #include "wx/univ/renderer.h"
1671 #include "wx/univ/theme.h"
1674 // shift the DC origin to match the position of the main window horz
1675 // scrollbar: this allows us to always use logical coords
1676 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1679 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1682 m_owner
->GetViewStart( &x
, NULL
);
1684 // account for the horz scrollbar offset
1685 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1688 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1690 wxPaintDC
dc( this );
1697 dc
.SetFont( GetFont() );
1699 // width and height of the entire header window
1701 GetClientSize( &w
, &h
);
1702 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1704 dc
.SetBackgroundMode(wxTRANSPARENT
);
1706 // do *not* use the listctrl colour for headers - one day we will have a
1707 // function to set it separately
1708 //dc.SetTextForeground( *wxBLACK );
1709 dc
.SetTextForeground(wxSystemSettings::
1710 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1712 int x
= HEADER_OFFSET_X
;
1714 int numColumns
= m_owner
->GetColumnCount();
1716 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1718 m_owner
->GetColumn( i
, item
);
1719 int wCol
= item
.m_width
;
1721 // the width of the rect to draw: make it smaller to fit entirely
1722 // inside the column rect
1725 wxRendererNative::Get().DrawHeaderButton
1729 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1730 m_parent
->IsEnabled() ? 0
1731 : wxCONTROL_DISABLED
1734 // see if we have enough space for the column label
1736 // for this we need the width of the text
1739 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1740 wLabel
+= 2*EXTRA_WIDTH
;
1742 // and the width of the icon, if any
1743 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1744 int ix
= 0, // init them just to suppress the compiler warnings
1746 const int image
= item
.m_image
;
1747 wxImageListType
*imageList
;
1750 imageList
= m_owner
->m_small_image_list
;
1753 imageList
->GetSize(image
, ix
, iy
);
1754 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1762 // ignore alignment if there is not enough space anyhow
1764 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1767 wxFAIL_MSG( _T("unknown list item format") );
1770 case wxLIST_FORMAT_LEFT
:
1774 case wxLIST_FORMAT_RIGHT
:
1775 xAligned
= x
+ cw
- wLabel
;
1778 case wxLIST_FORMAT_CENTER
:
1779 xAligned
= x
+ (cw
- wLabel
) / 2;
1784 // if we have an image, draw it on the right of the label
1791 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1792 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1793 wxIMAGELIST_DRAW_TRANSPARENT
1796 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1799 // draw the text clipping it so that it doesn't overwrite the column
1801 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1803 dc
.DrawText( item
.GetText(),
1804 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1812 void wxListHeaderWindow::DrawCurrent()
1814 int x1
= m_currentX
;
1816 m_owner
->ClientToScreen( &x1
, &y1
);
1818 int x2
= m_currentX
;
1820 m_owner
->GetClientSize( NULL
, &y2
);
1821 m_owner
->ClientToScreen( &x2
, &y2
);
1824 dc
.SetLogicalFunction( wxINVERT
);
1825 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1826 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1830 dc
.DrawLine( x1
, y1
, x2
, y2
);
1832 dc
.SetLogicalFunction( wxCOPY
);
1834 dc
.SetPen( wxNullPen
);
1835 dc
.SetBrush( wxNullBrush
);
1838 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1840 // we want to work with logical coords
1842 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1843 int y
= event
.GetY();
1847 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1849 // we don't draw the line beyond our window, but we allow dragging it
1852 GetClientSize( &w
, NULL
);
1853 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1856 // erase the line if it was drawn
1857 if ( m_currentX
< w
)
1860 if (event
.ButtonUp())
1863 m_isDragging
= FALSE
;
1865 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1866 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1873 m_currentX
= m_minX
+ 7;
1875 // draw in the new location
1876 if ( m_currentX
< w
)
1880 else // not dragging
1883 bool hit_border
= FALSE
;
1885 // end of the current column
1888 // find the column where this event occured
1890 countCol
= m_owner
->GetColumnCount();
1891 for (col
= 0; col
< countCol
; col
++)
1893 xpos
+= m_owner
->GetColumnWidth( col
);
1896 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1898 // near the column border
1905 // inside the column
1912 if ( col
== countCol
)
1915 if (event
.LeftDown() || event
.RightUp())
1917 if (hit_border
&& event
.LeftDown())
1919 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1920 event
.GetPosition()) )
1922 m_isDragging
= TRUE
;
1927 //else: column resizing was vetoed by the user code
1929 else // click on a column
1931 SendListEvent( event
.LeftDown()
1932 ? wxEVT_COMMAND_LIST_COL_CLICK
1933 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1934 event
.GetPosition());
1937 else if (event
.Moving())
1942 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1943 m_currentCursor
= m_resizeCursor
;
1947 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1948 m_currentCursor
= wxSTANDARD_CURSOR
;
1952 SetCursor(*m_currentCursor
);
1957 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1959 m_owner
->SetFocus();
1963 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1965 wxWindow
*parent
= GetParent();
1966 wxListEvent
le( type
, parent
->GetId() );
1967 le
.SetEventObject( parent
);
1968 le
.m_pointDrag
= pos
;
1970 // the position should be relative to the parent window, not
1971 // this one for compatibility with MSW and common sense: the
1972 // user code doesn't know anything at all about this header
1973 // window, so why should it get positions relative to it?
1974 le
.m_pointDrag
.y
-= GetSize().y
;
1976 le
.m_col
= m_column
;
1977 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1980 //-----------------------------------------------------------------------------
1981 // wxListRenameTimer (internal)
1982 //-----------------------------------------------------------------------------
1984 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1989 void wxListRenameTimer::Notify()
1991 m_owner
->OnRenameTimer();
1994 //-----------------------------------------------------------------------------
1995 // wxListTextCtrl (internal)
1996 //-----------------------------------------------------------------------------
1998 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
1999 EVT_CHAR (wxListTextCtrl::OnChar
)
2000 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2001 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2004 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2005 : m_startValue(owner
->GetItemText(itemEdit
)),
2006 m_itemEdited(itemEdit
)
2011 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2013 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2014 &rectLabel
.x
, &rectLabel
.y
);
2016 (void)Create(owner
, wxID_ANY
, m_startValue
,
2017 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2018 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2021 void wxListTextCtrl::Finish()
2025 wxPendingDelete
.Append(this);
2029 m_owner
->SetFocus();
2033 bool wxListTextCtrl::AcceptChanges()
2035 const wxString value
= GetValue();
2037 if ( value
== m_startValue
)
2039 // nothing changed, always accept
2043 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2045 // vetoed by the user
2049 // accepted, do rename the item
2050 m_owner
->SetItemText(m_itemEdited
, value
);
2055 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2057 switch ( event
.m_keyCode
)
2060 if ( !AcceptChanges() )
2062 // vetoed by the user code
2065 //else: fall through
2069 m_owner
->OnRenameCancelled( m_itemEdited
);
2077 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2085 // auto-grow the textctrl:
2086 wxSize parentSize
= m_owner
->GetSize();
2087 wxPoint myPos
= GetPosition();
2088 wxSize mySize
= GetSize();
2090 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2091 if (myPos
.x
+ sx
> parentSize
.x
)
2092 sx
= parentSize
.x
- myPos
.x
;
2100 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2104 // We must finish regardless of success, otherwise we'll get focus problems
2107 if ( !AcceptChanges() )
2108 m_owner
->OnRenameCancelled( m_itemEdited
);
2114 //-----------------------------------------------------------------------------
2116 //-----------------------------------------------------------------------------
2118 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2120 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2121 EVT_PAINT (wxListMainWindow::OnPaint
)
2122 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2123 EVT_CHAR (wxListMainWindow::OnChar
)
2124 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2125 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2126 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2127 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2130 void wxListMainWindow::Init()
2135 m_lineTo
= (size_t)-1;
2141 m_small_image_list
= (wxImageListType
*) NULL
;
2142 m_normal_image_list
= (wxImageListType
*) NULL
;
2144 m_small_spacing
= 30;
2145 m_normal_spacing
= 40;
2149 m_isCreated
= FALSE
;
2151 m_lastOnSame
= FALSE
;
2152 m_renameTimer
= new wxListRenameTimer( this );
2156 m_lineBeforeLastClicked
= (size_t)-1;
2161 wxListMainWindow::wxListMainWindow()
2166 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2169 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2174 const wxString
&name
)
2175 : wxScrolledWindow( parent
, id
, pos
, size
,
2176 style
| wxHSCROLL
| wxVSCROLL
, name
)
2180 m_highlightBrush
= new wxBrush
2182 wxSystemSettings::GetColour
2184 wxSYS_COLOUR_HIGHLIGHT
2189 m_highlightUnfocusedBrush
= new wxBrush
2191 wxSystemSettings::GetColour
2193 wxSYS_COLOUR_BTNSHADOW
2201 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2203 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2206 wxListMainWindow::~wxListMainWindow()
2209 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2211 delete m_highlightBrush
;
2212 delete m_highlightUnfocusedBrush
;
2214 delete m_renameTimer
;
2217 void wxListMainWindow::CacheLineData(size_t line
)
2219 wxGenericListCtrl
*listctrl
= GetListCtrl();
2221 wxListLineData
*ld
= GetDummyLine();
2223 size_t countCol
= GetColumnCount();
2224 for ( size_t col
= 0; col
< countCol
; col
++ )
2226 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2229 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2230 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2233 wxListLineData
*wxListMainWindow::GetDummyLine() const
2235 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2237 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2239 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2241 // we need to recreate the dummy line if the number of columns in the
2242 // control changed as it would have the incorrect number of fields
2244 if ( !m_lines
.IsEmpty() &&
2245 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2247 self
->m_lines
.Clear();
2250 if ( m_lines
.IsEmpty() )
2252 wxListLineData
*line
= new wxListLineData(self
);
2253 self
->m_lines
.Add(line
);
2255 // don't waste extra memory -- there never going to be anything
2256 // else/more in this array
2257 self
->m_lines
.Shrink();
2263 // ----------------------------------------------------------------------------
2264 // line geometry (report mode only)
2265 // ----------------------------------------------------------------------------
2267 wxCoord
wxListMainWindow::GetLineHeight() const
2269 // we cache the line height as calling GetTextExtent() is slow
2270 if ( !m_lineHeight
)
2272 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2274 wxClientDC
dc( self
);
2275 dc
.SetFont( GetFont() );
2278 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2280 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2284 m_small_image_list
->GetSize(0, iw
, ih
);
2289 self
->m_lineHeight
= y
+ LINE_SPACING
;
2292 return m_lineHeight
;
2295 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2297 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2299 return LINE_SPACING
+ line
*GetLineHeight();
2302 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2304 if ( !InReportView() )
2305 return GetLine(line
)->m_gi
->m_rectAll
;
2308 rect
.x
= HEADER_OFFSET_X
;
2309 rect
.y
= GetLineY(line
);
2310 rect
.width
= GetHeaderWidth();
2311 rect
.height
= GetLineHeight();
2316 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2318 if ( !InReportView() )
2319 return GetLine(line
)->m_gi
->m_rectLabel
;
2322 rect
.x
= HEADER_OFFSET_X
;
2323 rect
.y
= GetLineY(line
);
2324 rect
.width
= GetColumnWidth(0);
2325 rect
.height
= GetLineHeight();
2330 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2332 if ( !InReportView() )
2333 return GetLine(line
)->m_gi
->m_rectIcon
;
2335 wxListLineData
*ld
= GetLine(line
);
2336 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2339 rect
.x
= HEADER_OFFSET_X
;
2340 rect
.y
= GetLineY(line
);
2341 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2346 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2348 return InReportView() ? GetLineRect(line
)
2349 : GetLine(line
)->m_gi
->m_rectHighlight
;
2352 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2354 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2356 wxListLineData
*ld
= GetLine(line
);
2358 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2359 return wxLIST_HITTEST_ONITEMICON
;
2361 // VS: Testing for "ld->HasText() || InReportView()" instead of
2362 // "ld->HasText()" is needed to make empty lines in report view
2364 if ( ld
->HasText() || InReportView() )
2366 wxRect rect
= InReportView() ? GetLineRect(line
)
2367 : GetLineLabelRect(line
);
2369 if ( rect
.Inside(x
, y
) )
2370 return wxLIST_HITTEST_ONITEMLABEL
;
2376 // ----------------------------------------------------------------------------
2377 // highlight (selection) handling
2378 // ----------------------------------------------------------------------------
2380 bool wxListMainWindow::IsHighlighted(size_t line
) const
2384 return m_selStore
.IsSelected(line
);
2388 wxListLineData
*ld
= GetLine(line
);
2389 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2391 return ld
->IsHighlighted();
2395 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2401 wxArrayInt linesChanged
;
2402 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2405 // meny items changed state, refresh everything
2406 RefreshLines(lineFrom
, lineTo
);
2408 else // only a few items changed state, refresh only them
2410 size_t count
= linesChanged
.GetCount();
2411 for ( size_t n
= 0; n
< count
; n
++ )
2413 RefreshLine(linesChanged
[n
]);
2417 else // iterate over all items in non report view
2419 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2421 if ( HighlightLine(line
, highlight
) )
2429 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2435 changed
= m_selStore
.SelectItem(line
, highlight
);
2439 wxListLineData
*ld
= GetLine(line
);
2440 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2442 changed
= ld
->Highlight(highlight
);
2447 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2448 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2454 void wxListMainWindow::RefreshLine( size_t line
)
2456 if ( InReportView() )
2458 size_t visibleFrom
, visibleTo
;
2459 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2461 if ( line
< visibleFrom
|| line
> visibleTo
)
2465 wxRect rect
= GetLineRect(line
);
2467 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2468 RefreshRect( rect
);
2471 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2473 // we suppose that they are ordered by caller
2474 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2476 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2478 if ( InReportView() )
2480 size_t visibleFrom
, visibleTo
;
2481 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2483 if ( lineFrom
< visibleFrom
)
2484 lineFrom
= visibleFrom
;
2485 if ( lineTo
> visibleTo
)
2490 rect
.y
= GetLineY(lineFrom
);
2491 rect
.width
= GetClientSize().x
;
2492 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2494 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2495 RefreshRect( rect
);
2499 // TODO: this should be optimized...
2500 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2507 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2509 if ( InReportView() )
2511 size_t visibleFrom
, visibleTo
;
2512 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2514 if ( lineFrom
< visibleFrom
)
2515 lineFrom
= visibleFrom
;
2516 else if ( lineFrom
> visibleTo
)
2521 rect
.y
= GetLineY(lineFrom
);
2522 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2524 wxSize size
= GetClientSize();
2525 rect
.width
= size
.x
;
2526 // refresh till the bottom of the window
2527 rect
.height
= size
.y
- rect
.y
;
2529 RefreshRect( rect
);
2533 // TODO: how to do it more efficiently?
2538 void wxListMainWindow::RefreshSelected()
2544 if ( InReportView() )
2546 GetVisibleLinesRange(&from
, &to
);
2551 to
= GetItemCount() - 1;
2554 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2556 RefreshLine(m_current
);
2559 for ( size_t line
= from
; line
<= to
; line
++ )
2561 // NB: the test works as expected even if m_current == -1
2562 if ( line
!= m_current
&& IsHighlighted(line
) )
2569 void wxListMainWindow::Freeze()
2574 void wxListMainWindow::Thaw()
2576 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2578 if ( !--m_freezeCount
)
2584 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2586 // Note: a wxPaintDC must be constructed even if no drawing is
2587 // done (a Windows requirement).
2588 wxPaintDC
dc( this );
2590 if ( IsEmpty() || m_freezeCount
)
2592 // nothing to draw or not the moment to draw it
2598 // delay the repainting until we calculate all the items positions
2605 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2609 dc
.SetFont( GetFont() );
2611 if ( InReportView() )
2613 int lineHeight
= GetLineHeight();
2615 size_t visibleFrom
, visibleTo
;
2616 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2619 wxCoord xOrig
, yOrig
;
2620 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2622 // tell the caller cache to cache the data
2625 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2626 GetParent()->GetId());
2627 evCache
.SetEventObject( GetParent() );
2628 evCache
.m_oldItemIndex
= visibleFrom
;
2629 evCache
.m_itemIndex
= visibleTo
;
2630 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2633 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2635 rectLine
= GetLineRect(line
);
2637 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2638 rectLine
.width
, rectLine
.height
) )
2640 // don't redraw unaffected lines to avoid flicker
2644 GetLine(line
)->DrawInReportMode( &dc
,
2646 GetLineHighlightRect(line
),
2647 IsHighlighted(line
) );
2650 if ( HasFlag(wxLC_HRULES
) )
2652 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2653 wxSize clientSize
= GetClientSize();
2655 // Don't draw the first one
2656 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2659 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2660 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2661 clientSize
.x
- dev_x
, i
*lineHeight
);
2664 // Draw last horizontal rule
2665 if ( visibleTo
== GetItemCount() - 1 )
2668 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2669 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2670 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2674 // Draw vertical rules if required
2675 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2677 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2679 wxRect firstItemRect
;
2680 wxRect lastItemRect
;
2681 GetItemRect(visibleFrom
, firstItemRect
);
2682 GetItemRect(visibleTo
, lastItemRect
);
2683 int x
= firstItemRect
.GetX();
2685 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2686 for (int col
= 0; col
< GetColumnCount(); col
++)
2688 int colWidth
= GetColumnWidth(col
);
2690 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2691 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2697 size_t count
= GetItemCount();
2698 for ( size_t i
= 0; i
< count
; i
++ )
2700 GetLine(i
)->Draw( &dc
);
2705 // Don't draw rect outline under Mac at all.
2710 dc
.SetPen( *wxBLACK_PEN
);
2711 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2712 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2720 void wxListMainWindow::HighlightAll( bool on
)
2722 if ( IsSingleSel() )
2724 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2726 // we just have one item to turn off
2727 if ( HasCurrent() && IsHighlighted(m_current
) )
2729 HighlightLine(m_current
, FALSE
);
2730 RefreshLine(m_current
);
2735 HighlightLines(0, GetItemCount() - 1, on
);
2739 void wxListMainWindow::SendNotify( size_t line
,
2740 wxEventType command
,
2743 wxListEvent
le( command
, GetParent()->GetId() );
2744 le
.SetEventObject( GetParent() );
2745 le
.m_itemIndex
= line
;
2747 // set only for events which have position
2748 if ( point
!= wxDefaultPosition
)
2749 le
.m_pointDrag
= point
;
2751 // don't try to get the line info for virtual list controls: the main
2752 // program has it anyhow and if we did it would result in accessing all
2753 // the lines, even those which are not visible now and this is precisely
2754 // what we're trying to avoid
2755 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2757 if ( line
!= (size_t)-1 )
2759 GetLine(line
)->GetItem( 0, le
.m_item
);
2761 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2763 //else: there may be no more such item
2765 GetParent()->GetEventHandler()->ProcessEvent( le
);
2768 void wxListMainWindow::ChangeCurrent(size_t current
)
2770 m_current
= current
;
2772 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2775 void wxListMainWindow::EditLabel( long item
)
2777 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2778 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2780 size_t itemEdit
= (size_t)item
;
2782 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2783 le
.SetEventObject( GetParent() );
2784 le
.m_itemIndex
= item
;
2785 wxListLineData
*data
= GetLine(itemEdit
);
2786 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2787 data
->GetItem( 0, le
.m_item
);
2788 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2790 // vetoed by user code
2794 // We have to call this here because the label in question might just have
2795 // been added and no screen update taken place.
2799 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2804 void wxListMainWindow::OnRenameTimer()
2806 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2808 EditLabel( m_current
);
2811 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2813 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2814 le
.SetEventObject( GetParent() );
2815 le
.m_itemIndex
= itemEdit
;
2817 wxListLineData
*data
= GetLine(itemEdit
);
2818 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2820 data
->GetItem( 0, le
.m_item
);
2821 le
.m_item
.m_text
= value
;
2822 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2826 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2828 // let owner know that the edit was cancelled
2829 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2831 le
.SetEditCanceled(TRUE
);
2833 le
.SetEventObject( GetParent() );
2834 le
.m_itemIndex
= itemEdit
;
2836 wxListLineData
*data
= GetLine(itemEdit
);
2837 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2839 data
->GetItem( 0, le
.m_item
);
2841 GetEventHandler()->ProcessEvent( le
);
2844 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2846 event
.SetEventObject( GetParent() );
2847 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2850 if ( !HasCurrent() || IsEmpty() )
2856 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2857 event
.ButtonDClick()) )
2860 int x
= event
.GetX();
2861 int y
= event
.GetY();
2862 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2864 // where did we hit it (if we did)?
2867 size_t count
= GetItemCount(),
2870 if ( InReportView() )
2872 current
= y
/ GetLineHeight();
2873 if ( current
< count
)
2874 hitResult
= HitTestLine(current
, x
, y
);
2878 // TODO: optimize it too! this is less simple than for report view but
2879 // enumerating all items is still not a way to do it!!
2880 for ( current
= 0; current
< count
; current
++ )
2882 hitResult
= HitTestLine(current
, x
, y
);
2888 if (event
.Dragging())
2890 if (m_dragCount
== 0)
2892 // we have to report the raw, physical coords as we want to be
2893 // able to call HitTest(event.m_pointDrag) from the user code to
2894 // get the item being dragged
2895 m_dragStart
= event
.GetPosition();
2900 if (m_dragCount
!= 3)
2903 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2904 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2906 wxListEvent
le( command
, GetParent()->GetId() );
2907 le
.SetEventObject( GetParent() );
2908 le
.m_itemIndex
= current
;
2909 le
.m_pointDrag
= m_dragStart
;
2910 GetParent()->GetEventHandler()->ProcessEvent( le
);
2921 // outside of any item
2925 bool forceClick
= FALSE
;
2926 if (event
.ButtonDClick())
2928 m_renameTimer
->Stop();
2929 m_lastOnSame
= FALSE
;
2931 if ( current
== m_lineLastClicked
)
2933 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2939 // the first click was on another item, so don't interpret this as
2940 // a double click, but as a simple click instead
2945 if (event
.LeftUp() && m_lastOnSame
)
2947 if ((current
== m_current
) &&
2948 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2949 HasFlag(wxLC_EDIT_LABELS
) )
2951 m_renameTimer
->Start( 100, TRUE
);
2953 m_lastOnSame
= FALSE
;
2955 else if (event
.RightDown())
2957 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2958 event
.GetPosition() );
2960 else if (event
.MiddleDown())
2962 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2964 else if ( event
.LeftDown() || forceClick
)
2966 m_lineBeforeLastClicked
= m_lineLastClicked
;
2967 m_lineLastClicked
= current
;
2969 size_t oldCurrent
= m_current
;
2971 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
2973 HighlightAll( FALSE
);
2975 ChangeCurrent(current
);
2977 ReverseHighlight(m_current
);
2979 else // multi sel & either ctrl or shift is down
2981 if (event
.ControlDown())
2983 ChangeCurrent(current
);
2985 ReverseHighlight(m_current
);
2987 else if (event
.ShiftDown())
2989 ChangeCurrent(current
);
2991 size_t lineFrom
= oldCurrent
,
2994 if ( lineTo
< lineFrom
)
2997 lineFrom
= m_current
;
3000 HighlightLines(lineFrom
, lineTo
);
3002 else // !ctrl, !shift
3004 // test in the enclosing if should make it impossible
3005 wxFAIL_MSG( _T("how did we get here?") );
3009 if (m_current
!= oldCurrent
)
3011 RefreshLine( oldCurrent
);
3014 // forceClick is only set if the previous click was on another item
3015 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3019 void wxListMainWindow::MoveToItem(size_t item
)
3021 if ( item
== (size_t)-1 )
3024 wxRect rect
= GetLineRect(item
);
3026 int client_w
, client_h
;
3027 GetClientSize( &client_w
, &client_h
);
3029 const int hLine
= GetLineHeight();
3031 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3032 int view_y
= hLine
*GetScrollPos( wxVERTICAL
);
3034 if ( InReportView() )
3036 // the next we need the range of lines shown it might be different, so
3038 ResetVisibleLinesRange();
3040 if (rect
.y
< view_y
)
3041 Scroll( -1, rect
.y
/hLine
);
3042 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3043 Scroll( -1, (rect
.y
+rect
.height
-client_h
+hLine
)/hLine
);
3047 if (rect
.x
-view_x
< 5)
3048 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3049 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3050 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3054 // ----------------------------------------------------------------------------
3055 // keyboard handling
3056 // ----------------------------------------------------------------------------
3058 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3060 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3061 _T("invalid item index in OnArrowChar()") );
3063 size_t oldCurrent
= m_current
;
3065 // in single selection we just ignore Shift as we can't select several
3067 if ( event
.ShiftDown() && !IsSingleSel() )
3069 ChangeCurrent(newCurrent
);
3071 // refresh the old focus to remove it
3072 RefreshLine( oldCurrent
);
3074 // select all the items between the old and the new one
3075 if ( oldCurrent
> newCurrent
)
3077 newCurrent
= oldCurrent
;
3078 oldCurrent
= m_current
;
3081 HighlightLines(oldCurrent
, newCurrent
);
3085 // all previously selected items are unselected unless ctrl is held
3086 if ( !event
.ControlDown() )
3087 HighlightAll(FALSE
);
3089 ChangeCurrent(newCurrent
);
3091 // refresh the old focus to remove it
3092 RefreshLine( oldCurrent
);
3094 if ( !event
.ControlDown() )
3096 HighlightLine( m_current
, TRUE
);
3100 RefreshLine( m_current
);
3105 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3107 wxWindow
*parent
= GetParent();
3109 /* we propagate the key event up */
3110 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3111 ke
.m_shiftDown
= event
.m_shiftDown
;
3112 ke
.m_controlDown
= event
.m_controlDown
;
3113 ke
.m_altDown
= event
.m_altDown
;
3114 ke
.m_metaDown
= event
.m_metaDown
;
3115 ke
.m_keyCode
= event
.m_keyCode
;
3118 ke
.SetEventObject( parent
);
3119 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3124 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3126 wxWindow
*parent
= GetParent();
3128 /* we send a list_key event up */
3131 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3132 le
.m_itemIndex
= m_current
;
3133 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3134 le
.m_code
= event
.GetKeyCode();
3135 le
.SetEventObject( parent
);
3136 parent
->GetEventHandler()->ProcessEvent( le
);
3139 /* we propagate the char event up */
3140 wxKeyEvent
ke( wxEVT_CHAR
);
3141 ke
.m_shiftDown
= event
.m_shiftDown
;
3142 ke
.m_controlDown
= event
.m_controlDown
;
3143 ke
.m_altDown
= event
.m_altDown
;
3144 ke
.m_metaDown
= event
.m_metaDown
;
3145 ke
.m_keyCode
= event
.m_keyCode
;
3148 ke
.SetEventObject( parent
);
3149 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3151 if (event
.GetKeyCode() == WXK_TAB
)
3153 wxNavigationKeyEvent nevent
;
3154 nevent
.SetWindowChange( event
.ControlDown() );
3155 nevent
.SetDirection( !event
.ShiftDown() );
3156 nevent
.SetEventObject( GetParent()->GetParent() );
3157 nevent
.SetCurrentFocus( m_parent
);
3158 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3162 /* no item -> nothing to do */
3169 switch (event
.GetKeyCode())
3172 if ( m_current
> 0 )
3173 OnArrowChar( m_current
- 1, event
);
3177 if ( m_current
< (size_t)GetItemCount() - 1 )
3178 OnArrowChar( m_current
+ 1, event
);
3183 OnArrowChar( GetItemCount() - 1, event
);
3188 OnArrowChar( 0, event
);
3193 int steps
= InReportView() ? m_linesPerPage
- 1 : m_current
% m_linesPerPage
;
3195 int index
= m_current
- steps
;
3199 OnArrowChar( index
, event
);
3205 int steps
= InReportView()
3206 ? m_linesPerPage
- 1
3207 : m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3209 size_t index
= m_current
+ steps
;
3210 size_t count
= GetItemCount();
3211 if ( index
>= count
)
3214 OnArrowChar( index
, event
);
3219 if ( !InReportView() )
3221 int index
= m_current
- m_linesPerPage
;
3225 OnArrowChar( index
, event
);
3230 if ( !InReportView() )
3232 size_t index
= m_current
+ m_linesPerPage
;
3234 size_t count
= GetItemCount();
3235 if ( index
>= count
)
3238 OnArrowChar( index
, event
);
3243 if ( IsSingleSel() )
3245 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3247 if ( IsHighlighted(m_current
) )
3249 // don't unselect the item in single selection mode
3252 //else: select it in ReverseHighlight() below if unselected
3255 ReverseHighlight(m_current
);
3260 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3268 // ----------------------------------------------------------------------------
3270 // ----------------------------------------------------------------------------
3272 void wxListMainWindow::SetFocus()
3274 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3275 // overrides SetFocus in such way that it does never change focus from
3276 // panel's child to the panel itself. Unfortunately, we must be able to change
3277 // focus to the panel from wxListTextCtrl because the text control should
3278 // disappear when the user clicks outside it.
3280 wxWindow
*oldFocus
= FindFocus();
3282 if ( oldFocus
&& oldFocus
->GetParent() == this )
3284 wxWindow::SetFocus();
3288 wxScrolledWindow::SetFocus();
3292 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3296 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3297 event
.SetEventObject( GetParent() );
3298 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3302 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3303 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3304 // which are already drawn correctly resulting in horrible flicker - avoid
3314 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3318 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3319 event
.SetEventObject( GetParent() );
3320 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3327 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3329 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3331 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3333 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3335 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3337 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3339 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3341 else if ( InReportView() && (m_small_image_list
))
3343 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3347 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3349 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3351 m_normal_image_list
->GetSize( index
, width
, height
);
3353 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3355 m_small_image_list
->GetSize( index
, width
, height
);
3357 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3359 m_small_image_list
->GetSize( index
, width
, height
);
3361 else if ( InReportView() && m_small_image_list
)
3363 m_small_image_list
->GetSize( index
, width
, height
);
3372 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3374 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3375 dc
.SetFont( GetFont() );
3378 dc
.GetTextExtent( s
, &lw
, NULL
);
3380 return lw
+ AUTOSIZE_COL_MARGIN
;
3383 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3387 // calc the spacing from the icon size
3390 if ((imageList
) && (imageList
->GetImageCount()) )
3392 imageList
->GetSize(0, width
, height
);
3395 if (which
== wxIMAGE_LIST_NORMAL
)
3397 m_normal_image_list
= imageList
;
3398 m_normal_spacing
= width
+ 8;
3401 if (which
== wxIMAGE_LIST_SMALL
)
3403 m_small_image_list
= imageList
;
3404 m_small_spacing
= width
+ 14;
3405 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3409 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3414 m_small_spacing
= spacing
;
3418 m_normal_spacing
= spacing
;
3422 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3424 return isSmall
? m_small_spacing
: m_normal_spacing
;
3427 // ----------------------------------------------------------------------------
3429 // ----------------------------------------------------------------------------
3431 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3433 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3435 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3437 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3438 item
.m_width
= GetTextLength( item
.m_text
);
3440 wxListHeaderData
*column
= node
->GetData();
3441 column
->SetItem( item
);
3443 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3445 headerWin
->m_dirty
= TRUE
;
3449 // invalidate it as it has to be recalculated
3453 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3455 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3456 _T("invalid column index") );
3458 wxCHECK_RET( InReportView(),
3459 _T("SetColumnWidth() can only be called in report mode.") );
3462 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3464 headerWin
->m_dirty
= TRUE
;
3466 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3467 wxCHECK_RET( node
, _T("no column?") );
3469 wxListHeaderData
*column
= node
->GetData();
3471 size_t count
= GetItemCount();
3473 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3475 width
= GetTextLength(column
->GetText());
3477 else if ( width
== wxLIST_AUTOSIZE
)
3481 // TODO: determine the max width somehow...
3482 width
= WIDTH_COL_DEFAULT
;
3486 wxClientDC
dc(this);
3487 dc
.SetFont( GetFont() );
3489 int max
= AUTOSIZE_COL_MARGIN
;
3491 for ( size_t i
= 0; i
< count
; i
++ )
3493 wxListLineData
*line
= GetLine(i
);
3494 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3496 wxCHECK_RET( n
, _T("no subitem?") );
3498 wxListItemData
*item
= n
->GetData();
3501 if (item
->HasImage())
3504 GetImageSize( item
->GetImage(), ix
, iy
);
3508 if (item
->HasText())
3511 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3519 width
= max
+ AUTOSIZE_COL_MARGIN
;
3523 column
->SetWidth( width
);
3525 // invalidate it as it has to be recalculated
3529 int wxListMainWindow::GetHeaderWidth() const
3531 if ( !m_headerWidth
)
3533 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3535 size_t count
= GetColumnCount();
3536 for ( size_t col
= 0; col
< count
; col
++ )
3538 self
->m_headerWidth
+= GetColumnWidth(col
);
3542 return m_headerWidth
;
3545 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3547 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3548 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3550 wxListHeaderData
*column
= node
->GetData();
3551 column
->GetItem( item
);
3554 int wxListMainWindow::GetColumnWidth( int col
) const
3556 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3557 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3559 wxListHeaderData
*column
= node
->GetData();
3560 return column
->GetWidth();
3563 // ----------------------------------------------------------------------------
3565 // ----------------------------------------------------------------------------
3567 void wxListMainWindow::SetItem( wxListItem
&item
)
3569 long id
= item
.m_itemId
;
3570 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3571 _T("invalid item index in SetItem") );
3575 wxListLineData
*line
= GetLine((size_t)id
);
3576 line
->SetItem( item
.m_col
, item
);
3579 // update the item on screen
3581 GetItemRect(id
, rectItem
);
3582 RefreshRect(rectItem
);
3585 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3587 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3588 _T("invalid list ctrl item index in SetItem") );
3590 size_t oldCurrent
= m_current
;
3591 size_t item
= (size_t)litem
; // safe because of the check above
3593 // do we need to change the focus?
3594 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3596 if ( state
& wxLIST_STATE_FOCUSED
)
3598 // don't do anything if this item is already focused
3599 if ( item
!= m_current
)
3601 ChangeCurrent(item
);
3603 if ( oldCurrent
!= (size_t)-1 )
3605 if ( IsSingleSel() )
3607 HighlightLine(oldCurrent
, FALSE
);
3610 RefreshLine(oldCurrent
);
3613 RefreshLine( m_current
);
3618 // don't do anything if this item is not focused
3619 if ( item
== m_current
)
3623 if ( IsSingleSel() )
3625 // we must unselect the old current item as well or we
3626 // might end up with more than one selected item in a
3627 // single selection control
3628 HighlightLine(oldCurrent
, FALSE
);
3631 RefreshLine( oldCurrent
);
3636 // do we need to change the selection state?
3637 if ( stateMask
& wxLIST_STATE_SELECTED
)
3639 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3641 if ( IsSingleSel() )
3645 // selecting the item also makes it the focused one in the
3647 if ( m_current
!= item
)
3649 ChangeCurrent(item
);
3651 if ( oldCurrent
!= (size_t)-1 )
3653 HighlightLine( oldCurrent
, FALSE
);
3654 RefreshLine( oldCurrent
);
3660 // only the current item may be selected anyhow
3661 if ( item
!= m_current
)
3666 if ( HighlightLine(item
, on
) )
3673 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3675 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3676 _T("invalid list ctrl item index in GetItemState()") );
3678 int ret
= wxLIST_STATE_DONTCARE
;
3680 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3682 if ( (size_t)item
== m_current
)
3683 ret
|= wxLIST_STATE_FOCUSED
;
3686 if ( stateMask
& wxLIST_STATE_SELECTED
)
3688 if ( IsHighlighted(item
) )
3689 ret
|= wxLIST_STATE_SELECTED
;
3695 void wxListMainWindow::GetItem( wxListItem
&item
) const
3697 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3698 _T("invalid item index in GetItem") );
3700 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3701 line
->GetItem( item
.m_col
, item
);
3704 // ----------------------------------------------------------------------------
3706 // ----------------------------------------------------------------------------
3708 size_t wxListMainWindow::GetItemCount() const
3710 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3713 void wxListMainWindow::SetItemCount(long count
)
3715 m_selStore
.SetItemCount(count
);
3716 m_countVirt
= count
;
3718 ResetVisibleLinesRange();
3720 // scrollbars must be reset
3724 int wxListMainWindow::GetSelectedItemCount() const
3726 // deal with the quick case first
3727 if ( IsSingleSel() )
3729 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3732 // virtual controls remmebers all its selections itself
3734 return m_selStore
.GetSelectedCount();
3736 // TODO: we probably should maintain the number of items selected even for
3737 // non virtual controls as enumerating all lines is really slow...
3738 size_t countSel
= 0;
3739 size_t count
= GetItemCount();
3740 for ( size_t line
= 0; line
< count
; line
++ )
3742 if ( GetLine(line
)->IsHighlighted() )
3749 // ----------------------------------------------------------------------------
3750 // item position/size
3751 // ----------------------------------------------------------------------------
3753 wxRect
wxListMainWindow::GetViewRect() const
3755 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3756 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3758 // we need to find the longest/tallest label
3761 const int count
= GetItemCount();
3764 for ( int i
= 0; i
< count
; i
++ )
3769 wxCoord x
= r
.GetRight(),
3779 // some fudge needed to make it look prettier
3780 xMax
+= 2*EXTRA_BORDER_X
;
3781 yMax
+= 2*EXTRA_BORDER_Y
;
3783 // account for the scrollbars if necessary
3784 const wxSize sizeAll
= GetClientSize();
3785 if ( xMax
> sizeAll
.x
)
3786 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3787 if ( yMax
> sizeAll
.y
)
3788 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3790 return wxRect(0, 0, xMax
, yMax
);
3793 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3795 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3796 _T("invalid index in GetItemRect") );
3798 // ensure that we're laid out, otherwise we could return nonsense
3801 wxConstCast(this, wxListMainWindow
)->
3802 RecalculatePositions(TRUE
/* no refresh */);
3805 rect
= GetLineRect((size_t)index
);
3807 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3810 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3813 GetItemRect(item
, rect
);
3821 // ----------------------------------------------------------------------------
3822 // geometry calculation
3823 // ----------------------------------------------------------------------------
3825 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3827 wxClientDC
dc( this );
3828 dc
.SetFont( GetFont() );
3830 const size_t count
= GetItemCount();
3833 if ( HasFlag(wxLC_ICON
) )
3834 iconSpacing
= m_normal_spacing
;
3835 else if ( HasFlag(wxLC_SMALL_ICON
) )
3836 iconSpacing
= m_small_spacing
;
3840 // Note that we do not call GetClientSize() here but
3841 // GetSize() and substract the border size for sunken
3842 // borders manually. This is technically incorrect,
3843 // but we need to know the client area's size WITHOUT
3844 // scrollbars here. Since we don't know if there are
3845 // any scrollbars, we use GetSize() instead. Another
3846 // solution would be to call SetScrollbars() here to
3847 // remove the scrollbars and call GetClientSize() then,
3848 // but this might result in flicker and - worse - will
3849 // reset the scrollbars to 0 which is not good at all
3850 // if you resize a dialog/window, but don't want to
3851 // reset the window scrolling. RR.
3852 // Furthermore, we actually do NOT subtract the border
3853 // width as 2 pixels is just the extra space which we
3854 // need around the actual content in the window. Other-
3855 // wise the text would e.g. touch the upper border. RR.
3858 GetSize( &clientWidth
, &clientHeight
);
3860 const int lineHeight
= GetLineHeight();
3862 if ( InReportView() )
3864 // all lines have the same height and we scroll one line per step
3865 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3867 m_linesPerPage
= clientHeight
/ lineHeight
;
3869 ResetVisibleLinesRange();
3871 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3872 GetHeaderWidth() / SCROLL_UNIT_X
,
3873 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3874 GetScrollPos(wxHORIZONTAL
),
3875 GetScrollPos(wxVERTICAL
),
3880 // we have 3 different layout strategies: either layout all items
3881 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3882 // to arrange them in top to bottom, left to right (don't ask me why
3883 // not the other way round...) order
3884 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3886 int x
= EXTRA_BORDER_X
;
3887 int y
= EXTRA_BORDER_Y
;
3889 wxCoord widthMax
= 0;
3892 for ( i
= 0; i
< count
; i
++ )
3894 wxListLineData
*line
= GetLine(i
);
3895 line
->CalculateSize( &dc
, iconSpacing
);
3896 line
->SetPosition( x
, y
, iconSpacing
);
3898 wxSize sizeLine
= GetLineSize(i
);
3900 if ( HasFlag(wxLC_ALIGN_TOP
) )
3902 if ( sizeLine
.x
> widthMax
)
3903 widthMax
= sizeLine
.x
;
3907 else // wxLC_ALIGN_LEFT
3909 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3913 if ( HasFlag(wxLC_ALIGN_TOP
) )
3915 // traverse the items again and tweak their sizes so that they are
3916 // all the same in a row
3917 for ( i
= 0; i
< count
; i
++ )
3919 wxListLineData
*line
= GetLine(i
);
3920 line
->m_gi
->ExtendWidth(widthMax
);
3929 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3930 (y
+ lineHeight
) / lineHeight
,
3931 GetScrollPos( wxHORIZONTAL
),
3932 GetScrollPos( wxVERTICAL
),
3936 else // "flowed" arrangement, the most complicated case
3938 // at first we try without any scrollbars, if the items don't fit into
3939 // the window, we recalculate after subtracting the space taken by the
3942 int entireWidth
= 0;
3944 for (int tries
= 0; tries
< 2; tries
++)
3946 entireWidth
= 2*EXTRA_BORDER_X
;
3950 // Now we have decided that the items do not fit into the
3951 // client area, so we need a scrollbar
3952 entireWidth
+= SCROLL_UNIT_X
;
3955 int x
= EXTRA_BORDER_X
;
3956 int y
= EXTRA_BORDER_Y
;
3957 int maxWidthInThisRow
= 0;
3960 int currentlyVisibleLines
= 0;
3962 for (size_t i
= 0; i
< count
; i
++)
3964 currentlyVisibleLines
++;
3965 wxListLineData
*line
= GetLine(i
);
3966 line
->CalculateSize( &dc
, iconSpacing
);
3967 line
->SetPosition( x
, y
, iconSpacing
);
3969 wxSize sizeLine
= GetLineSize(i
);
3971 if ( maxWidthInThisRow
< sizeLine
.x
)
3972 maxWidthInThisRow
= sizeLine
.x
;
3975 if (currentlyVisibleLines
> m_linesPerPage
)
3976 m_linesPerPage
= currentlyVisibleLines
;
3978 if ( y
+ sizeLine
.y
>= clientHeight
)
3980 currentlyVisibleLines
= 0;
3982 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3983 x
+= maxWidthInThisRow
;
3984 entireWidth
+= maxWidthInThisRow
;
3985 maxWidthInThisRow
= 0;
3988 // We have reached the last item.
3989 if ( i
== count
- 1 )
3990 entireWidth
+= maxWidthInThisRow
;
3992 if ( (tries
== 0) &&
3993 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3995 clientHeight
-= wxSystemSettings::
3996 GetMetric(wxSYS_HSCROLL_Y
);
4001 if ( i
== count
- 1 )
4002 tries
= 1; // Everything fits, no second try required.
4010 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4012 GetScrollPos( wxHORIZONTAL
),
4021 // FIXME: why should we call it from here?
4028 void wxListMainWindow::RefreshAll()
4033 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4034 if ( headerWin
&& headerWin
->m_dirty
)
4036 headerWin
->m_dirty
= FALSE
;
4037 headerWin
->Refresh();
4041 void wxListMainWindow::UpdateCurrent()
4043 if ( !HasCurrent() && !IsEmpty() )
4049 long wxListMainWindow::GetNextItem( long item
,
4050 int WXUNUSED(geometry
),
4054 max
= GetItemCount();
4055 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4056 _T("invalid listctrl index in GetNextItem()") );
4058 // notice that we start with the next item (or the first one if item == -1)
4059 // and this is intentional to allow writing a simple loop to iterate over
4060 // all selected items
4064 // this is not an error because the index was ok initially, just no
4075 size_t count
= GetItemCount();
4076 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4078 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4081 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4088 // ----------------------------------------------------------------------------
4090 // ----------------------------------------------------------------------------
4092 void wxListMainWindow::DeleteItem( long lindex
)
4094 size_t count
= GetItemCount();
4096 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4097 _T("invalid item index in DeleteItem") );
4099 size_t index
= (size_t)lindex
;
4101 // we don't need to adjust the index for the previous items
4102 if ( HasCurrent() && m_current
>= index
)
4104 // if the current item is being deleted, we want the next one to
4105 // become selected - unless there is no next one - so don't adjust
4106 // m_current in this case
4107 if ( m_current
!= index
|| m_current
== count
- 1 )
4113 if ( InReportView() )
4115 ResetVisibleLinesRange();
4122 m_selStore
.OnItemDelete(index
);
4126 m_lines
.RemoveAt( index
);
4129 // we need to refresh the (vert) scrollbar as the number of items changed
4132 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4134 RefreshAfter(index
);
4137 void wxListMainWindow::DeleteColumn( int col
)
4139 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4141 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4144 delete node
->GetData();
4145 m_columns
.Erase( node
);
4149 // update all the items
4150 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4152 wxListLineData
* const line
= GetLine(i
);
4153 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4154 delete n
->GetData();
4155 line
->m_items
.Erase(n
);
4159 // invalidate it as it has to be recalculated
4163 void wxListMainWindow::DoDeleteAllItems()
4167 // nothing to do - in particular, don't send the event
4173 // to make the deletion of all items faster, we don't send the
4174 // notifications for each item deletion in this case but only one event
4175 // for all of them: this is compatible with wxMSW and documented in
4176 // DeleteAllItems() description
4178 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4179 event
.SetEventObject( GetParent() );
4180 GetParent()->GetEventHandler()->ProcessEvent( event
);
4189 if ( InReportView() )
4191 ResetVisibleLinesRange();
4197 void wxListMainWindow::DeleteAllItems()
4201 RecalculatePositions();
4204 void wxListMainWindow::DeleteEverything()
4206 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4211 // ----------------------------------------------------------------------------
4212 // scanning for an item
4213 // ----------------------------------------------------------------------------
4215 void wxListMainWindow::EnsureVisible( long index
)
4217 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4218 _T("invalid index in EnsureVisible") );
4220 // We have to call this here because the label in question might just have
4221 // been added and its position is not known yet
4224 RecalculatePositions(TRUE
/* no refresh */);
4227 MoveToItem((size_t)index
);
4230 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4237 size_t count
= GetItemCount();
4238 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4240 wxListLineData
*line
= GetLine(i
);
4241 if ( line
->GetText(0) == tmp
)
4248 long wxListMainWindow::FindItem(long start
, long data
)
4254 size_t count
= GetItemCount();
4255 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4257 wxListLineData
*line
= GetLine(i
);
4259 line
->GetItem( 0, item
);
4260 if (item
.m_data
== data
)
4267 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4269 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4271 size_t count
= GetItemCount();
4273 if ( InReportView() )
4275 size_t current
= y
/ GetLineHeight();
4276 if ( current
< count
)
4278 flags
= HitTestLine(current
, x
, y
);
4285 // TODO: optimize it too! this is less simple than for report view but
4286 // enumerating all items is still not a way to do it!!
4287 for ( size_t current
= 0; current
< count
; current
++ )
4289 flags
= HitTestLine(current
, x
, y
);
4298 // ----------------------------------------------------------------------------
4300 // ----------------------------------------------------------------------------
4302 void wxListMainWindow::InsertItem( wxListItem
&item
)
4304 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4306 size_t count
= GetItemCount();
4307 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4308 _T("invalid item index") );
4310 size_t id
= item
.m_itemId
;
4315 // this is unused variable
4318 if ( InReportView() )
4321 // this is unused variable
4324 ResetVisibleLinesRange();
4326 else if ( HasFlag(wxLC_LIST
) )
4328 // this is unused variable
4333 else if ( HasFlag(wxLC_ICON
) )
4335 // this is unused variable
4340 else if ( HasFlag(wxLC_SMALL_ICON
) )
4342 // this is unused variable
4343 mode
= wxLC_ICON
; // no typo
4349 wxFAIL_MSG( _T("unknown mode") );
4352 wxListLineData
*line
= new wxListLineData(this);
4354 line
->SetItem( 0, item
);
4356 m_lines
.Insert( line
, id
);
4360 // If an item is selected at or below the point of insertion, we need to
4361 // increment the member variables because the current row's index has gone
4363 if ( HasCurrent() && m_current
>= id
)
4368 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4370 RefreshLines(id
, GetItemCount() - 1);
4373 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4376 if ( InReportView() )
4378 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4379 item
.m_width
= GetTextLength( item
.m_text
);
4381 wxListHeaderData
*column
= new wxListHeaderData( item
);
4382 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4385 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4386 m_columns
.Insert( node
, column
);
4390 m_columns
.Append( column
);
4395 // update all the items
4396 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4398 wxListLineData
* const line
= GetLine(i
);
4399 wxListItemData
* const data
= new wxListItemData(this);
4401 line
->m_items
.Insert(col
, data
);
4403 line
->m_items
.Append(data
);
4407 // invalidate it as it has to be recalculated
4412 // ----------------------------------------------------------------------------
4414 // ----------------------------------------------------------------------------
4416 wxListCtrlCompare list_ctrl_compare_func_2
;
4417 long list_ctrl_compare_data
;
4419 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4421 wxListLineData
*line1
= *arg1
;
4422 wxListLineData
*line2
= *arg2
;
4424 line1
->GetItem( 0, item
);
4425 long data1
= item
.m_data
;
4426 line2
->GetItem( 0, item
);
4427 long data2
= item
.m_data
;
4428 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4431 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4433 list_ctrl_compare_func_2
= fn
;
4434 list_ctrl_compare_data
= data
;
4435 m_lines
.Sort( list_ctrl_compare_func_1
);
4439 // ----------------------------------------------------------------------------
4441 // ----------------------------------------------------------------------------
4443 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4445 // update our idea of which lines are shown when we redraw the window the
4447 ResetVisibleLinesRange();
4450 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4451 wxScrolledWindow::OnScroll(event
);
4453 HandleOnScroll( event
);
4456 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4458 wxGenericListCtrl
* lc
= GetListCtrl();
4459 wxCHECK_RET( lc
, _T("no listctrl window?") );
4461 lc
->m_headerWin
->Refresh();
4462 lc
->m_headerWin
->Update();
4466 int wxListMainWindow::GetCountPerPage() const
4468 if ( !m_linesPerPage
)
4470 wxConstCast(this, wxListMainWindow
)->
4471 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4474 return m_linesPerPage
;
4477 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4479 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4481 if ( m_lineFrom
== (size_t)-1 )
4483 size_t count
= GetItemCount();
4486 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4488 // this may happen if SetScrollbars() hadn't been called yet
4489 if ( m_lineFrom
>= count
)
4490 m_lineFrom
= count
- 1;
4492 // we redraw one extra line but this is needed to make the redrawing
4493 // logic work when there is a fractional number of lines on screen
4494 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4495 if ( m_lineTo
>= count
)
4496 m_lineTo
= count
- 1;
4498 else // empty control
4501 m_lineTo
= (size_t)-1;
4505 wxASSERT_MSG( IsEmpty() ||
4506 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4507 _T("GetVisibleLinesRange() returns incorrect result") );
4515 // -------------------------------------------------------------------------------------
4516 // wxGenericListCtrl
4517 // -------------------------------------------------------------------------------------
4519 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4521 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4522 EVT_SIZE(wxGenericListCtrl::OnSize
)
4525 wxGenericListCtrl::wxGenericListCtrl()
4527 m_imageListNormal
= (wxImageListType
*) NULL
;
4528 m_imageListSmall
= (wxImageListType
*) NULL
;
4529 m_imageListState
= (wxImageListType
*) NULL
;
4531 m_ownsImageListNormal
=
4532 m_ownsImageListSmall
=
4533 m_ownsImageListState
= FALSE
;
4535 m_mainWin
= (wxListMainWindow
*) NULL
;
4536 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4540 wxGenericListCtrl::~wxGenericListCtrl()
4542 if (m_ownsImageListNormal
)
4543 delete m_imageListNormal
;
4544 if (m_ownsImageListSmall
)
4545 delete m_imageListSmall
;
4546 if (m_ownsImageListState
)
4547 delete m_imageListState
;
4550 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4554 // we use 'g' to get the descent, too
4556 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4557 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4559 // only update if changed
4560 if ( h
!= m_headerHeight
)
4564 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4567 ResizeReportView(TRUE
);
4572 void wxGenericListCtrl::CreateHeaderWindow()
4574 m_headerWin
= new wxListHeaderWindow
4576 this, -1, m_mainWin
,
4578 wxSize(GetClientSize().x
, m_headerHeight
),
4581 CalculateAndSetHeaderHeight();
4584 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4589 const wxValidator
&validator
,
4590 const wxString
&name
)
4594 m_imageListState
= (wxImageListType
*) NULL
;
4595 m_ownsImageListNormal
=
4596 m_ownsImageListSmall
=
4597 m_ownsImageListState
= FALSE
;
4599 m_mainWin
= (wxListMainWindow
*) NULL
;
4600 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4604 if ( !(style
& wxLC_MASK_TYPE
) )
4606 style
= style
| wxLC_LIST
;
4609 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4612 // don't create the inner window with the border
4613 style
&= ~wxBORDER_MASK
;
4615 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4617 #if defined( __WXMAC__ ) && __WXMAC_CARBON__
4619 font
.MacCreateThemeFont( kThemeViewsFont
) ;
4622 if ( InReportView() )
4624 CreateHeaderWindow();
4626 if ( HasFlag(wxLC_NO_HEADER
) )
4628 // VZ: why do we create it at all then?
4629 m_headerWin
->Show( FALSE
);
4636 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4638 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4639 _T("wxLC_VIRTUAL can't be [un]set") );
4641 long flag
= GetWindowStyle();
4645 if (style
& wxLC_MASK_TYPE
)
4646 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4647 if (style
& wxLC_MASK_ALIGN
)
4648 flag
&= ~wxLC_MASK_ALIGN
;
4649 if (style
& wxLC_MASK_SORT
)
4650 flag
&= ~wxLC_MASK_SORT
;
4662 SetWindowStyleFlag( flag
);
4665 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4669 m_mainWin
->DeleteEverything();
4671 // has the header visibility changed?
4672 bool hasHeader
= HasHeader();
4673 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4675 if ( hasHeader
!= willHaveHeader
)
4682 // don't delete, just hide, as we can reuse it later
4683 m_headerWin
->Show(FALSE
);
4685 //else: nothing to do
4687 else // must show header
4691 CreateHeaderWindow();
4693 else // already have it, just show
4695 m_headerWin
->Show( TRUE
);
4699 ResizeReportView(willHaveHeader
);
4703 wxWindow::SetWindowStyleFlag( flag
);
4706 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4708 m_mainWin
->GetColumn( col
, item
);
4712 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4714 m_mainWin
->SetColumn( col
, item
);
4718 int wxGenericListCtrl::GetColumnWidth( int col
) const
4720 return m_mainWin
->GetColumnWidth( col
);
4723 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4725 m_mainWin
->SetColumnWidth( col
, width
);
4729 int wxGenericListCtrl::GetCountPerPage() const
4731 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4734 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4736 m_mainWin
->GetItem( info
);
4740 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4742 m_mainWin
->SetItem( info
);
4746 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4749 info
.m_text
= label
;
4750 info
.m_mask
= wxLIST_MASK_TEXT
;
4751 info
.m_itemId
= index
;
4755 info
.m_image
= imageId
;
4756 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4758 m_mainWin
->SetItem(info
);
4762 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4764 return m_mainWin
->GetItemState( item
, stateMask
);
4767 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4769 m_mainWin
->SetItemState( item
, state
, stateMask
);
4773 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4776 info
.m_image
= image
;
4777 info
.m_mask
= wxLIST_MASK_IMAGE
;
4778 info
.m_itemId
= item
;
4779 m_mainWin
->SetItem( info
);
4783 wxString
wxGenericListCtrl::GetItemText( long item
) const
4785 return m_mainWin
->GetItemText(item
);
4788 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4790 m_mainWin
->SetItemText(item
, str
);
4793 long wxGenericListCtrl::GetItemData( long item
) const
4796 info
.m_itemId
= item
;
4797 m_mainWin
->GetItem( info
);
4801 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4804 info
.m_mask
= wxLIST_MASK_DATA
;
4805 info
.m_itemId
= item
;
4807 m_mainWin
->SetItem( info
);
4811 wxRect
wxGenericListCtrl::GetViewRect() const
4813 return m_mainWin
->GetViewRect();
4816 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4818 m_mainWin
->GetItemRect( item
, rect
);
4819 if ( m_mainWin
->HasHeader() )
4820 rect
.y
+= m_headerHeight
+ 1;
4824 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4826 m_mainWin
->GetItemPosition( item
, pos
);
4830 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4835 int wxGenericListCtrl::GetItemCount() const
4837 return m_mainWin
->GetItemCount();
4840 int wxGenericListCtrl::GetColumnCount() const
4842 return m_mainWin
->GetColumnCount();
4845 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4847 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4850 wxSize
wxGenericListCtrl::GetItemSpacing() const
4852 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4854 return wxSize(spacing
, spacing
);
4857 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4859 return m_mainWin
->GetItemSpacing( isSmall
);
4862 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4865 info
.m_itemId
= item
;
4866 info
.SetTextColour( col
);
4867 m_mainWin
->SetItem( info
);
4870 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4873 info
.m_itemId
= item
;
4874 m_mainWin
->GetItem( info
);
4875 return info
.GetTextColour();
4878 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4881 info
.m_itemId
= item
;
4882 info
.SetBackgroundColour( col
);
4883 m_mainWin
->SetItem( info
);
4886 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4889 info
.m_itemId
= item
;
4890 m_mainWin
->GetItem( info
);
4891 return info
.GetBackgroundColour();
4894 int wxGenericListCtrl::GetSelectedItemCount() const
4896 return m_mainWin
->GetSelectedItemCount();
4899 wxColour
wxGenericListCtrl::GetTextColour() const
4901 return GetForegroundColour();
4904 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4906 SetForegroundColour(col
);
4909 long wxGenericListCtrl::GetTopItem() const
4912 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4916 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4918 return m_mainWin
->GetNextItem( item
, geom
, state
);
4921 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4923 if (which
== wxIMAGE_LIST_NORMAL
)
4925 return m_imageListNormal
;
4927 else if (which
== wxIMAGE_LIST_SMALL
)
4929 return m_imageListSmall
;
4931 else if (which
== wxIMAGE_LIST_STATE
)
4933 return m_imageListState
;
4935 return (wxImageListType
*) NULL
;
4938 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4940 if ( which
== wxIMAGE_LIST_NORMAL
)
4942 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4943 m_imageListNormal
= imageList
;
4944 m_ownsImageListNormal
= FALSE
;
4946 else if ( which
== wxIMAGE_LIST_SMALL
)
4948 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4949 m_imageListSmall
= imageList
;
4950 m_ownsImageListSmall
= FALSE
;
4952 else if ( which
== wxIMAGE_LIST_STATE
)
4954 if (m_ownsImageListState
) delete m_imageListState
;
4955 m_imageListState
= imageList
;
4956 m_ownsImageListState
= FALSE
;
4959 m_mainWin
->SetImageList( imageList
, which
);
4962 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4964 SetImageList(imageList
, which
);
4965 if ( which
== wxIMAGE_LIST_NORMAL
)
4966 m_ownsImageListNormal
= TRUE
;
4967 else if ( which
== wxIMAGE_LIST_SMALL
)
4968 m_ownsImageListSmall
= TRUE
;
4969 else if ( which
== wxIMAGE_LIST_STATE
)
4970 m_ownsImageListState
= TRUE
;
4973 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4978 bool wxGenericListCtrl::DeleteItem( long item
)
4980 m_mainWin
->DeleteItem( item
);
4984 bool wxGenericListCtrl::DeleteAllItems()
4986 m_mainWin
->DeleteAllItems();
4990 bool wxGenericListCtrl::DeleteAllColumns()
4992 size_t count
= m_mainWin
->m_columns
.GetCount();
4993 for ( size_t n
= 0; n
< count
; n
++ )
4999 void wxGenericListCtrl::ClearAll()
5001 m_mainWin
->DeleteEverything();
5004 bool wxGenericListCtrl::DeleteColumn( int col
)
5006 m_mainWin
->DeleteColumn( col
);
5008 // if we don't have the header any longer, we need to relayout the window
5009 if ( !GetColumnCount() )
5011 ResizeReportView(FALSE
/* no header */);
5017 void wxGenericListCtrl::Edit( long item
)
5019 m_mainWin
->EditLabel( item
);
5022 bool wxGenericListCtrl::EnsureVisible( long item
)
5024 m_mainWin
->EnsureVisible( item
);
5028 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5030 return m_mainWin
->FindItem( start
, str
, partial
);
5033 long wxGenericListCtrl::FindItem( long start
, long data
)
5035 return m_mainWin
->FindItem( start
, data
);
5038 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
5039 int WXUNUSED(direction
))
5044 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5046 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5049 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5051 m_mainWin
->InsertItem( info
);
5052 return info
.m_itemId
;
5055 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5058 info
.m_text
= label
;
5059 info
.m_mask
= wxLIST_MASK_TEXT
;
5060 info
.m_itemId
= index
;
5061 return InsertItem( info
);
5064 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5067 info
.m_mask
= wxLIST_MASK_IMAGE
;
5068 info
.m_image
= imageIndex
;
5069 info
.m_itemId
= index
;
5070 return InsertItem( info
);
5073 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5076 info
.m_text
= label
;
5077 info
.m_image
= imageIndex
;
5078 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5079 info
.m_itemId
= index
;
5080 return InsertItem( info
);
5083 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5085 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5087 m_mainWin
->InsertColumn( col
, item
);
5089 // if we hadn't had header before and have it now we need to relayout the
5091 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5093 ResizeReportView(TRUE
/* have header */);
5096 m_headerWin
->Refresh();
5101 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5102 int format
, int width
)
5105 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5106 item
.m_text
= heading
;
5109 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5110 item
.m_width
= width
;
5112 item
.m_format
= format
;
5114 return InsertColumn( col
, item
);
5117 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5123 // fn is a function which takes 3 long arguments: item1, item2, data.
5124 // item1 is the long data associated with a first item (NOT the index).
5125 // item2 is the long data associated with a second item (NOT the index).
5126 // data is the same value as passed to SortItems.
5127 // The return value is a negative number if the first item should precede the second
5128 // item, a positive number of the second item should precede the first,
5129 // or zero if the two items are equivalent.
5130 // data is arbitrary data to be passed to the sort function.
5132 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5134 m_mainWin
->SortItems( fn
, data
);
5138 // ----------------------------------------------------------------------------
5140 // ----------------------------------------------------------------------------
5142 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5147 ResizeReportView(m_mainWin
->HasHeader());
5149 m_mainWin
->RecalculatePositions();
5152 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5155 GetClientSize( &cw
, &ch
);
5159 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5160 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5162 else // no header window
5164 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5168 void wxGenericListCtrl::OnInternalIdle()
5170 wxWindow::OnInternalIdle();
5172 // do it only if needed
5173 if ( !m_mainWin
->m_dirty
)
5176 m_mainWin
->RecalculatePositions();
5179 // ----------------------------------------------------------------------------
5181 // ----------------------------------------------------------------------------
5183 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5187 m_mainWin
->SetBackgroundColour( colour
);
5188 m_mainWin
->m_dirty
= TRUE
;
5194 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5196 if ( !wxWindow::SetForegroundColour( colour
) )
5201 m_mainWin
->SetForegroundColour( colour
);
5202 m_mainWin
->m_dirty
= TRUE
;
5207 m_headerWin
->SetForegroundColour( colour
);
5213 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5215 if ( !wxWindow::SetFont( font
) )
5220 m_mainWin
->SetFont( font
);
5221 m_mainWin
->m_dirty
= TRUE
;
5226 m_headerWin
->SetFont( font
);
5227 CalculateAndSetHeaderHeight();
5235 // ----------------------------------------------------------------------------
5236 // methods forwarded to m_mainWin
5237 // ----------------------------------------------------------------------------
5239 #if wxUSE_DRAG_AND_DROP
5241 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5243 m_mainWin
->SetDropTarget( dropTarget
);
5246 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5248 return m_mainWin
->GetDropTarget();
5251 #endif // wxUSE_DRAG_AND_DROP
5253 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5255 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5258 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5260 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5263 wxColour
wxGenericListCtrl::GetForegroundColour() const
5265 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5268 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5271 return m_mainWin
->PopupMenu( menu
, x
, y
);
5274 #endif // wxUSE_MENUS
5277 void wxGenericListCtrl::SetFocus()
5279 /* The test in window.cpp fails as we are a composite
5280 window, so it checks against "this", but not m_mainWin. */
5281 if ( FindFocus() != this )
5282 m_mainWin
->SetFocus();
5285 // ----------------------------------------------------------------------------
5286 // virtual list control support
5287 // ----------------------------------------------------------------------------
5289 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5291 // this is a pure virtual function, in fact - which is not really pure
5292 // because the controls which are not virtual don't need to implement it
5293 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5295 return wxEmptyString
;
5298 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5301 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5307 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5309 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5310 _T("invalid item index in OnGetItemAttr()") );
5312 // no attributes by default
5316 void wxGenericListCtrl::SetItemCount(long count
)
5318 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5320 m_mainWin
->SetItemCount(count
);
5323 void wxGenericListCtrl::RefreshItem(long item
)
5325 m_mainWin
->RefreshLine(item
);
5328 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5330 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5334 * Generic wxListCtrl is more or less a container for two other
5335 * windows which drawings are done upon. These are namely
5336 * 'm_headerWin' and 'm_mainWin'.
5337 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5338 * behaviour wxListCtrl has under wxMSW.
5340 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5344 // The easy case, no rectangle specified.
5346 m_headerWin
->Refresh(eraseBackground
);
5349 m_mainWin
->Refresh(eraseBackground
);
5353 // Refresh the header window
5356 wxRect rectHeader
= m_headerWin
->GetRect();
5357 rectHeader
.Intersect(*rect
);
5358 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5361 m_headerWin
->GetPosition(&x
, &y
);
5362 rectHeader
.Offset(-x
, -y
);
5363 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5368 // Refresh the main window
5371 wxRect rectMain
= m_mainWin
->GetRect();
5372 rectMain
.Intersect(*rect
);
5373 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5376 m_mainWin
->GetPosition(&x
, &y
);
5377 rectMain
.Offset(-x
, -y
);
5378 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5384 void wxGenericListCtrl::Freeze()
5386 m_mainWin
->Freeze();
5389 void wxGenericListCtrl::Thaw()
5394 #endif // wxUSE_LISTCTRL