1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/private/listctrl.h
3 // Purpose: private definitions of wxListCtrl helpers
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_GENERIC_LISTCTRL_PRIVATE_H_
11 #define _WX_GENERIC_LISTCTRL_PRIVATE_H_
17 #include "wx/listctrl.h"
18 #include "wx/selstore.h"
20 #include "wx/settings.h"
22 // ============================================================================
24 // ============================================================================
26 //-----------------------------------------------------------------------------
27 // wxColWidthInfo (internal)
28 //-----------------------------------------------------------------------------
33 bool bNeedsUpdate
; // only set to true when an item whose
34 // width == nMaxWidth is removed
36 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
39 bNeedsUpdate
= needsUpdate
;
43 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
45 //-----------------------------------------------------------------------------
46 // wxListItemData (internal)
47 //-----------------------------------------------------------------------------
52 wxListItemData(wxListMainWindow
*owner
);
55 void SetItem( const wxListItem
&info
);
56 void SetImage( int image
) { m_image
= image
; }
57 void SetData( wxUIntPtr data
) { m_data
= data
; }
58 void SetPosition( int x
, int y
);
59 void SetSize( int width
, int height
);
61 bool HasText() const { return !m_text
.empty(); }
62 const wxString
& GetText() const { return m_text
; }
63 void SetText(const wxString
& text
) { m_text
= text
; }
65 // we can't use empty string for measuring the string width/height, so
66 // always return something
67 wxString
GetTextForMeasuring() const
69 wxString s
= GetText();
76 bool IsHit( int x
, int y
) const;
81 int GetHeight() const;
83 int GetImage() const { return m_image
; }
84 bool HasImage() const { return GetImage() != -1; }
86 void GetItem( wxListItem
&info
) const;
88 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
89 wxListItemAttr
*GetAttr() const { return m_attr
; }
92 // the item image or -1
95 // user data associated with the item
98 // the item coordinates are not used in report mode; instead this pointer is
99 // NULL and the owner window is used to retrieve the item position and size
102 // the list ctrl we are in
103 wxListMainWindow
*m_owner
;
105 // custom attributes or NULL
106 wxListItemAttr
*m_attr
;
109 // common part of all ctors
115 //-----------------------------------------------------------------------------
116 // wxListHeaderData (internal)
117 //-----------------------------------------------------------------------------
119 class wxListHeaderData
: public wxObject
123 wxListHeaderData( const wxListItem
&info
);
124 void SetItem( const wxListItem
&item
);
125 void SetPosition( int x
, int y
);
126 void SetWidth( int w
);
127 void SetState( int state
);
128 void SetFormat( int format
);
129 void SetHeight( int h
);
130 bool HasImage() const;
132 bool HasText() const { return !m_text
.empty(); }
133 const wxString
& GetText() const { return m_text
; }
134 void SetText(const wxString
& text
) { m_text
= text
; }
136 void GetItem( wxListItem
&item
);
138 bool IsHit( int x
, int y
) const;
139 int GetImage() const;
140 int GetWidth() const;
141 int GetFormat() const;
142 int GetState() const;
159 //-----------------------------------------------------------------------------
160 // wxListLineData (internal)
161 //-----------------------------------------------------------------------------
163 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
168 // the list of subitems: only may have more than one item in report mode
169 wxListItemDataList m_items
;
171 // this is not used in report view
183 // the part to be highlighted
184 wxRect m_rectHighlight
;
186 // extend all our rects to be centered inside the one of given width
187 void ExtendWidth(wxCoord w
)
189 wxASSERT_MSG( m_rectAll
.width
<= w
,
190 wxT("width can only be increased") );
193 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
194 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
195 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
200 // is this item selected? [NB: not used in virtual mode]
203 // back pointer to the list ctrl
204 wxListMainWindow
*m_owner
;
207 wxListLineData(wxListMainWindow
*owner
);
211 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
215 // called by the owner when it toggles report view
216 void SetReportView(bool inReportView
)
218 // we only need m_gi when we're not in report view so update as needed
226 m_gi
= new GeometryInfo
;
230 // are we in report mode?
231 inline bool InReportView() const;
233 // are we in virtual report mode?
234 inline bool IsVirtual() const;
236 // these 2 methods shouldn't be called for report view controls, in that
237 // case we determine our position/size ourselves
239 // calculate the size of the line
240 void CalculateSize( wxDC
*dc
, int spacing
);
242 // remember the position this line appears at
243 void SetPosition( int x
, int y
, int spacing
);
247 void SetImage( int image
) { SetImage(0, image
); }
248 int GetImage() const { return GetImage(0); }
249 void SetImage( int index
, int image
);
250 int GetImage( int index
) const;
252 bool HasImage() const { return GetImage() != -1; }
253 bool HasText() const { return !GetText(0).empty(); }
255 void SetItem( int index
, const wxListItem
&info
);
256 void GetItem( int index
, wxListItem
&info
);
258 wxString
GetText(int index
) const;
259 void SetText( int index
, const wxString
& s
);
261 wxListItemAttr
*GetAttr() const;
262 void SetAttr(wxListItemAttr
*attr
);
264 // return true if the highlighting really changed
265 bool Highlight( bool on
);
267 void ReverseHighlight();
269 bool IsHighlighted() const
271 wxASSERT_MSG( !IsVirtual(), wxT("unexpected call to IsHighlighted") );
273 return m_highlighted
;
276 // draw the line on the given DC in icon/list mode
277 void Draw( wxDC
*dc
, bool current
);
279 // the same in report mode: it needs more parameters as we don't store
280 // everything in the item in report mode
281 void DrawInReportMode( wxDC
*dc
,
283 const wxRect
& rectHL
,
288 // set the line to contain num items (only can be > 1 in report mode)
289 void InitItems( int num
);
291 // get the mode (i.e. style) of the list control
292 inline int GetMode() const;
294 // Apply this item attributes to the given DC: set the text font and colour
295 // and also erase the background appropriately.
296 void ApplyAttributes(wxDC
*dc
,
297 const wxRect
& rectHL
,
301 // draw the text on the DC with the correct justification; also add an
302 // ellipsis if the text is too large to fit in the current width
303 void DrawTextFormatted(wxDC
*dc
,
304 const wxString
&text
,
307 int yMid
, // this is middle, not top, of the text
311 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
313 //-----------------------------------------------------------------------------
314 // wxListHeaderWindow (internal)
315 //-----------------------------------------------------------------------------
317 class wxListHeaderWindow
: public wxWindow
320 wxListMainWindow
*m_owner
;
321 const wxCursor
*m_currentCursor
;
322 wxCursor
*m_resizeCursor
;
325 // column being resized or -1
328 // divider line position in logical (unscrolled) coords
331 // minimal position beyond which the divider line
332 // can't be dragged in logical coords
336 wxListHeaderWindow();
338 // We provide only Create(), not the ctor, because we need to create the
339 // C++ object before creating the window, see the explanations in
340 // CreateOrDestroyHeaderWindowAsNeeded()
341 bool Create( wxWindow
*win
,
343 wxListMainWindow
*owner
,
344 const wxPoint
&pos
= wxDefaultPosition
,
345 const wxSize
&size
= wxDefaultSize
,
347 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
349 virtual ~wxListHeaderWindow();
351 // We never need focus as we don't have any keyboard interface.
352 virtual bool AcceptsFocus() const { return false; }
355 void AdjustDC( wxDC
& dc
);
357 void OnPaint( wxPaintEvent
&event
);
358 void OnMouse( wxMouseEvent
&event
);
363 // Update main window's column later
364 bool m_sendSetColumnWidth
;
368 virtual void OnInternalIdle();
371 // common part of all ctors
374 // generate and process the list event of the given type, return true if
375 // it wasn't vetoed, i.e. if we should proceed
376 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
378 DECLARE_EVENT_TABLE()
381 //-----------------------------------------------------------------------------
382 // wxListRenameTimer (internal)
383 //-----------------------------------------------------------------------------
385 class wxListRenameTimer
: public wxTimer
388 wxListMainWindow
*m_owner
;
391 wxListRenameTimer( wxListMainWindow
*owner
);
395 //-----------------------------------------------------------------------------
396 // wxListFindTimer (internal)
397 //-----------------------------------------------------------------------------
399 class wxListFindTimer
: public wxTimer
402 // reset the current prefix after half a second of inactivity
403 enum { DELAY
= 500 };
405 wxListFindTimer( wxListMainWindow
*owner
)
410 virtual void Notify();
413 wxListMainWindow
*m_owner
;
416 //-----------------------------------------------------------------------------
417 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
418 //-----------------------------------------------------------------------------
420 class wxListTextCtrlWrapper
: public wxEvtHandler
423 // NB: text must be a valid object but not Create()d yet
424 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
428 wxTextCtrl
*GetText() const { return m_text
; }
430 // Check if the given key event should stop editing and return true if it
431 // does or false otherwise.
432 bool CheckForEndEditKey(const wxKeyEvent
& event
);
434 // Different reasons for calling EndEdit():
436 // It was called because:
439 End_Accept
, // user has accepted the changes.
440 End_Discard
, // user has cancelled editing.
441 End_Destroy
// the entire control is being destroyed.
444 void EndEdit(EndReason reason
);
447 void OnChar( wxKeyEvent
&event
);
448 void OnKeyUp( wxKeyEvent
&event
);
449 void OnKillFocus( wxFocusEvent
&event
);
451 bool AcceptChanges();
452 void Finish( bool setfocus
);
455 wxListMainWindow
*m_owner
;
457 wxString m_startValue
;
459 bool m_aboutToFinish
;
461 DECLARE_EVENT_TABLE()
464 //-----------------------------------------------------------------------------
465 // wxListMainWindow (internal)
466 //-----------------------------------------------------------------------------
468 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
470 class wxListMainWindow
: public wxWindow
474 wxListMainWindow( wxWindow
*parent
,
477 const wxSize
& size
);
479 virtual ~wxListMainWindow();
481 // called by the main control when its mode changes
482 void SetReportView(bool inReportView
);
484 // helper to simplify testing for wxLC_XXX flags
485 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
487 // return true if this is a virtual list control
488 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
490 // return true if the control is in report mode
491 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
493 // return true if we are in single selection mode, false if multi sel
494 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
496 // do we have a header window?
497 bool HasHeader() const
498 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
500 void HighlightAll( bool on
);
502 // all these functions only do something if the line is currently visible
504 // change the line "selected" state, return true if it really changed
505 bool HighlightLine( size_t line
, bool highlight
= true);
507 // as HighlightLine() but do it for the range of lines: this is incredibly
508 // more efficient for virtual list controls!
510 // NB: unlike HighlightLine() this one does refresh the lines on screen
511 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
513 // toggle the line state and refresh it
514 void ReverseHighlight( size_t line
)
515 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
517 // return true if the line is highlighted
518 bool IsHighlighted(size_t line
) const;
520 // refresh one or several lines at once
521 void RefreshLine( size_t line
);
522 void RefreshLines( size_t lineFrom
, size_t lineTo
);
524 // refresh all selected items
525 void RefreshSelected();
527 // refresh all lines below the given one: the difference with
528 // RefreshLines() is that the index here might not be a valid one (happens
529 // when the last line is deleted)
530 void RefreshAfter( size_t lineFrom
);
532 // the methods which are forwarded to wxListLineData itself in list/icon
533 // modes but are here because the lines don't store their positions in the
536 // get the bound rect for the entire line
537 wxRect
GetLineRect(size_t line
) const;
539 // get the bound rect of the label
540 wxRect
GetLineLabelRect(size_t line
) const;
542 // get the bound rect of the items icon (only may be called if we do have
544 wxRect
GetLineIconRect(size_t line
) const;
546 // get the rect to be highlighted when the item has focus
547 wxRect
GetLineHighlightRect(size_t line
) const;
549 // get the size of the total line rect
550 wxSize
GetLineSize(size_t line
) const
551 { return GetLineRect(line
).GetSize(); }
553 // return the hit code for the corresponding position (in this line)
554 long HitTestLine(size_t line
, int x
, int y
) const;
556 // bring the selected item into view, scrolling to it if necessary
557 void MoveToItem(size_t item
);
559 bool ScrollList( int WXUNUSED(dx
), int dy
);
561 // bring the current item into view
562 void MoveToFocus() { MoveToItem(m_current
); }
564 // start editing the label of the given item
565 wxTextCtrl
*EditLabel(long item
,
566 wxClassInfo
* textControlClass
= wxCLASSINFO(wxTextCtrl
));
567 wxTextCtrl
*GetEditControl() const
569 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
572 void ResetTextControl(wxTextCtrl
*text
)
575 m_textctrlWrapper
= NULL
;
578 void OnRenameTimer();
579 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
580 void OnRenameCancelled(size_t itemEdit
);
583 // set whether or not to ring the find bell
584 // (does nothing on MSW - bell is always rung)
585 void EnableBellOnNoMatch( bool on
);
587 void OnMouse( wxMouseEvent
&event
);
589 // called to switch the selection from the current item to newCurrent,
590 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
592 void OnCharHook( wxKeyEvent
&event
);
593 void OnChar( wxKeyEvent
&event
);
594 void OnKeyDown( wxKeyEvent
&event
);
595 void OnKeyUp( wxKeyEvent
&event
);
596 void OnSetFocus( wxFocusEvent
&event
);
597 void OnKillFocus( wxFocusEvent
&event
);
598 void OnScroll( wxScrollWinEvent
& event
);
600 void OnPaint( wxPaintEvent
&event
);
602 void OnChildFocus(wxChildFocusEvent
& event
);
604 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
605 void GetImageSize( int index
, int &width
, int &height
) const;
607 void SetImageList( wxImageList
*imageList
, int which
);
608 void SetItemSpacing( int spacing
, bool isSmall
= false );
609 int GetItemSpacing( bool isSmall
= false );
611 void SetColumn( int col
, const wxListItem
&item
);
612 void SetColumnWidth( int col
, int width
);
613 void GetColumn( int col
, wxListItem
&item
) const;
614 int GetColumnWidth( int col
) const;
615 int GetColumnCount() const { return m_columns
.GetCount(); }
617 // returns the sum of the heights of all columns
618 int GetHeaderWidth() const;
620 int GetCountPerPage() const;
622 void SetItem( wxListItem
&item
);
623 void GetItem( wxListItem
&item
) const;
624 void SetItemState( long item
, long state
, long stateMask
);
625 void SetItemStateAll( long state
, long stateMask
);
626 int GetItemState( long item
, long stateMask
) const;
627 bool GetItemRect( long item
, wxRect
&rect
) const
629 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
631 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
632 wxRect
GetViewRect() const;
633 bool GetItemPosition( long item
, wxPoint
& pos
) const;
634 int GetSelectedItemCount() const;
636 wxString
GetItemText(long item
, int col
= 0) const
639 info
.m_mask
= wxLIST_MASK_TEXT
;
640 info
.m_itemId
= item
;
646 void SetItemText(long item
, const wxString
& value
)
649 info
.m_mask
= wxLIST_MASK_TEXT
;
650 info
.m_itemId
= item
;
655 wxImageList
* GetSmallImageList() const
656 { return m_small_image_list
; }
658 // set the scrollbars and update the positions of the items
659 void RecalculatePositions(bool noRefresh
= false);
661 // refresh the window and the header
664 long GetNextItem( long item
, int geometry
, int state
) const;
665 void DeleteItem( long index
);
666 void DeleteAllItems();
667 void DeleteColumn( int col
);
668 void DeleteEverything();
669 void EnsureVisible( long index
);
670 long FindItem( long start
, const wxString
& str
, bool partial
= false );
671 long FindItem( long start
, wxUIntPtr data
);
672 long FindItem( const wxPoint
& pt
);
673 long HitTest( int x
, int y
, int &flags
) const;
674 void InsertItem( wxListItem
&item
);
675 long InsertColumn( long col
, const wxListItem
&item
);
676 int GetItemWidthWithImage(wxListItem
* item
);
677 void SortItems( wxListCtrlCompare fn
, wxIntPtr 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 const 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 ( !wxWindow::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
;
724 bool HasFocus() const
730 // the array of all line objects for a non virtual list control (for the
731 // virtual list control we only ever use m_lines[0])
732 wxListLineDataArray m_lines
;
734 // the list of column objects
735 wxListHeaderDataList m_columns
;
737 // currently focused item or -1
740 // the number of lines per page
743 // this flag is set when something which should result in the window
744 // redrawing happens (i.e. an item was added or deleted, or its appearance
745 // changed) and OnPaint() doesn't redraw the window while it is set which
746 // allows to minimize the number of repaintings when a lot of items are
747 // being added. The real repainting occurs only after the next OnIdle()
751 wxColour
*m_highlightColour
;
752 wxImageList
*m_small_image_list
;
753 wxImageList
*m_normal_image_list
;
755 int m_normal_spacing
;
759 wxTimer
*m_renameTimer
;
761 // incremental search data
762 wxString m_findPrefix
;
763 wxTimer
*m_findTimer
;
764 // This flag is set to 0 if the bell is disabled, 1 if it is enabled and -1
765 // if it is globally enabled but has been temporarily disabled because we
766 // had already beeped for this particular search.
772 ColWidthArray m_aColWidths
;
774 // for double click logic
775 size_t m_lineLastClicked
,
776 m_lineBeforeLastClicked
,
777 m_lineSelectSingleOnUp
;
780 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
782 // the total count of items in a virtual list control
785 // the object maintaining the items selection state, only used in virtual
787 wxSelectionStore m_selStore
;
789 // common part of all ctors
792 // get the line data for the given index
793 wxListLineData
*GetLine(size_t n
) const
795 wxASSERT_MSG( n
!= (size_t)-1, wxT("invalid line index") );
799 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
806 // get a dummy line which can be used for geometry calculations and such:
807 // you must use GetLine() if you want to really draw the line
808 wxListLineData
*GetDummyLine() const;
810 // cache the line data of the n-th line in m_lines[0]
811 void CacheLineData(size_t line
);
813 // get the range of visible lines
814 void GetVisibleLinesRange(size_t *from
, size_t *to
);
816 // force us to recalculate the range of visible lines
817 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
819 // find the first item starting with the given prefix after the given item
820 size_t PrefixFindItem(size_t item
, const wxString
& prefix
) const;
822 // get the colour to be used for drawing the rules
823 wxColour
GetRuleColour() const
825 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
829 // initialize the current item if needed
830 void UpdateCurrent();
832 // delete all items but don't refresh: called from dtor
833 void DoDeleteAllItems();
835 // Compute the minimal width needed to fully display the column header.
836 int ComputeMinHeaderWidth(const wxListHeaderData
* header
) const;
839 // the height of one line using the current font
840 wxCoord m_lineHeight
;
842 // the total header width or 0 if not calculated yet
843 wxCoord m_headerWidth
;
845 // the first and last lines being shown on screen right now (inclusive),
846 // both may be -1 if they must be calculated so never access them directly:
847 // use GetVisibleLinesRange() above instead
851 // the brushes to use for item highlighting when we do/don't have focus
852 wxBrush
*m_highlightBrush
,
853 *m_highlightUnfocusedBrush
;
855 // wrapper around the text control currently used for in place editing or
856 // NULL if no item is being edited
857 wxListTextCtrlWrapper
*m_textctrlWrapper
;
860 DECLARE_EVENT_TABLE()
862 friend class wxGenericListCtrl
;
865 #endif // wxUSE_LISTCTRL
866 #endif // _WX_GENERIC_LISTCTRL_PRIVATE_H_