1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_LISTCTRL_H_
13 #define _WX_LISTCTRL_H_
16 #include "wx/generic/listctrl.h"
18 #define wxMAC_ALWAYS_USE_GENERIC_LISTCTRL wxT("mac.listctrl.always_use_generic")
20 class wxMacDataBrowserListCtrlControl
;
21 class wxListCtrlTextCtrlWrapper
;
22 class wxListCtrlRenameTimer
;
24 WX_DECLARE_EXPORTED_LIST(wxListItem
, wxColumnList
);
26 class WXDLLIMPEXP_CORE wxListCtrl
: public wxControl
28 DECLARE_DYNAMIC_CLASS(wxListCtrl
)
34 wxListCtrl() { Init(); }
36 wxListCtrl(wxWindow
*parent
,
37 wxWindowID id
= wxID_ANY
,
38 const wxPoint
& pos
= wxDefaultPosition
,
39 const wxSize
& size
= wxDefaultSize
,
40 long style
= wxLC_ICON
,
41 const wxValidator
& validator
= wxDefaultValidator
,
42 const wxString
& name
= wxListCtrlNameStr
)
46 Create(parent
, id
, pos
, size
, style
, validator
, name
);
49 virtual ~wxListCtrl();
51 bool Create(wxWindow
*parent
,
52 wxWindowID id
= wxID_ANY
,
53 const wxPoint
& pos
= wxDefaultPosition
,
54 const wxSize
& size
= wxDefaultSize
,
55 long style
= wxLC_ICON
,
56 const wxValidator
& validator
= wxDefaultValidator
,
57 const wxString
& name
= wxListCtrlNameStr
);
60 ////////////////////////////////////////////////////////////////////////////
62 // Gets information about this column
63 bool GetColumn(int col
, wxListItem
& item
) const;
65 // Sets information about this column
66 // TODO: NOT const to be compatible with wxGenericListCtrl API
67 bool SetColumn(int col
, wxListItem
& item
) ;
69 // Gets the column width
70 int GetColumnWidth(int col
) const;
72 // Sets the column width
73 bool SetColumnWidth(int col
, int width
) ;
75 // Gets the number of items that can fit vertically in the
76 // visible area of the list control (list or report view)
77 // or the total number of items in the list control (icon
78 // or small icon view)
79 int GetCountPerPage() const;
81 // Gets the edit control for editing labels.
82 wxTextCtrl
* GetEditControl() const;
84 // Gets information about the item
85 bool GetItem(wxListItem
& info
) const ;
87 // Sets information about the item
88 bool SetItem(wxListItem
& info
) ;
90 // Sets a string field at a particular column
91 long SetItem(long index
, int col
, const wxString
& label
, int imageId
= -1);
93 // Gets the item state
94 int GetItemState(long item
, long stateMask
) const ;
96 // Sets the item state
97 bool SetItemState(long item
, long state
, long stateMask
) ;
99 void AssignImageList(wxImageList
*imageList
, int which
);
101 // Sets the item image
102 bool SetItemImage(long item
, int image
, int selImage
= -1) ;
103 bool SetItemColumnImage(long item
, long column
, int image
);
105 // Gets the item text
106 wxString
GetItemText(long item
, int col
= 0) const ;
108 // Sets the item text
109 void SetItemText(long item
, const wxString
& str
) ;
111 void SetItemTextColour(long item
, const wxColour
& colour
) ;
112 wxColour
GetItemTextColour(long item
) const;
114 void SetItemBackgroundColour(long item
, const wxColour
& colour
) ;
115 wxColour
GetItemBackgroundColour(long item
) const;
117 void SetItemFont( long item
, const wxFont
&f
);
118 wxFont
GetItemFont( long item
) const;
120 // Gets the item data
121 long GetItemData(long item
) const ;
123 // Sets the item data
124 bool SetItemPtrData(long item
, wxUIntPtr data
);
125 bool SetItemData(long item
, long data
) { return SetItemPtrData(item
, data
); }
127 // Gets the item rectangle
128 bool GetItemRect(long item
, wxRect
& rect
, int code
= wxLIST_RECT_BOUNDS
) const ;
130 // Gets the item rectangle of a subitem
131 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
, int code
= wxLIST_RECT_BOUNDS
) const;
133 // Gets the item position
134 bool GetItemPosition(long item
, wxPoint
& pos
) const ;
136 // Sets the item position
137 bool SetItemPosition(long item
, const wxPoint
& pos
) ;
139 // Gets the number of items in the list control
140 int GetItemCount() const;
142 // Gets the number of columns in the list control
143 int GetColumnCount() const;
145 void SetItemSpacing( int spacing
, bool isSmall
= false );
146 wxSize
GetItemSpacing() const;
148 // Gets the number of selected items in the list control
149 int GetSelectedItemCount() const;
151 wxRect
GetViewRect() const;
153 // Gets the text colour of the listview
154 wxColour
GetTextColour() const;
156 // Sets the text colour of the listview
157 void SetTextColour(const wxColour
& col
);
159 // Gets the index of the topmost visible item when in
160 // list or report view
161 long GetTopItem() const ;
163 // are we in report mode?
164 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
166 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
168 // Add or remove a single window style
169 void SetSingleStyle(long style
, bool add
= true) ;
171 // Set the whole window style
172 void SetWindowStyleFlag(long style
) ;
174 // Searches for an item, starting from 'item'.
175 // item can be -1 to find the first item that matches the
177 // Returns the item or -1 if unsuccessful.
178 long GetNextItem(long item
, int geometry
= wxLIST_NEXT_ALL
, int state
= wxLIST_STATE_DONTCARE
) const ;
180 // Implementation: converts wxWidgets style to MSW style.
181 // Can be a single style flag or a bit list.
182 // oldStyle is 'normalised' so that it doesn't contain
183 // conflicting styles.
184 long ConvertToMSWStyle(long& oldStyle
, long style
) const;
186 // Gets one of the three image lists
187 wxImageList
*GetImageList(int which
) const ;
189 // Sets the image list
190 // N.B. There's a quirk in the Win95 list view implementation.
191 // If in wxLC_LIST mode, it'll *still* display images by the labels if
192 // there's a small-icon image list set for the control - even though you
193 // haven't specified wxLIST_MASK_IMAGE when inserting.
194 // So you have to set a NULL small-icon image list to be sure that
195 // the wxLC_LIST mode works without icons. Of course, you may want icons...
196 void SetImageList(wxImageList
*imageList
, int which
) ;
199 ////////////////////////////////////////////////////////////////////////////
201 // Arranges the items
202 bool Arrange(int flag
= wxLIST_ALIGN_DEFAULT
);
205 bool DeleteItem(long item
);
208 bool DeleteAllItems() ;
211 bool DeleteColumn(int col
);
213 // Deletes all columns
214 bool DeleteAllColumns();
216 // Clears items, and columns if there are any.
220 wxTextCtrl
* EditLabel(long item
, wxClassInfo
* textControlClass
= wxCLASSINFO(wxTextCtrl
));
222 // End label editing, optionally cancelling the edit
223 bool EndEditLabel(bool cancel
);
225 // Ensures this item is visible
226 bool EnsureVisible(long item
) ;
228 // Find an item whose label matches this string, starting from the item after 'start'
229 // or the beginning if 'start' is -1.
230 long FindItem(long start
, const wxString
& str
, bool partial
= false);
232 // Find an item whose data matches this data, starting from the item after 'start'
233 // or the beginning if 'start' is -1.
234 long FindItem(long start
, long data
);
236 // Find an item nearest this position in the specified direction, starting from
237 // the item after 'start' or the beginning if 'start' is -1.
238 long FindItem(long start
, const wxPoint
& pt
, int direction
);
240 // Determines which item (if any) is at the specified point,
241 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
242 // Request the subitem number as well at the given coordinate.
243 long HitTest(const wxPoint
& point
, int& flags
, long* ptrSubItem
= NULL
) const;
245 // Inserts an item, returning the index of the new item if successful,
247 // TOD: Should also have some further convenience functions
248 // which don't require setting a wxListItem object
249 long InsertItem(wxListItem
& info
);
251 // Insert a string item
252 long InsertItem(long index
, const wxString
& label
);
254 // Insert an image item
255 long InsertItem(long index
, int imageIndex
);
257 // Insert an image/string item
258 long InsertItem(long index
, const wxString
& label
, int imageIndex
);
260 // For list view mode (only), inserts a column.
261 long InsertColumn(long col
, wxListItem
& info
);
263 long InsertColumn(long col
, const wxString
& heading
, int format
= wxLIST_FORMAT_LEFT
,
266 // Scrolls the list control. If in icon, small icon or report view mode,
267 // x specifies the number of pixels to scroll. If in list view mode, x
268 // specifies the number of columns to scroll.
269 // If in icon, small icon or list view mode, y specifies the number of pixels
270 // to scroll. If in report view mode, y specifies the number of lines to scroll.
271 bool ScrollList(int dx
, int dy
);
275 // fn is a function which takes 3 long arguments: item1, item2, data.
276 // item1 is the long data associated with a first item (NOT the index).
277 // item2 is the long data associated with a second item (NOT the index).
278 // data is the same value as passed to SortItems.
279 // The return value is a negative number if the first item should precede the second
280 // item, a positive number of the second item should precede the first,
281 // or zero if the two items are equivalent.
283 // data is arbitrary data to be passed to the sort function.
284 bool SortItems(wxListCtrlCompare fn
, wxIntPtr data
);
286 wxMacDataBrowserListCtrlControl
* GetListPeer() const;
288 // these functions are only used for virtual list view controls, i.e. the
289 // ones with wxLC_VIRTUAL style
291 void SetItemCount(long count
);
292 void RefreshItem(long item
);
293 void RefreshItems(long itemFrom
, long itemTo
);
295 // return the text for the given column of the given item
296 virtual wxString
OnGetItemText(long item
, long column
) const;
298 // return the icon for the given item. In report view, OnGetItemImage will
299 // only be called for the first column. See OnGetItemColumnImage for
301 virtual int OnGetItemImage(long item
) const;
303 // return the icon for the given item and column.
304 virtual int OnGetItemColumnImage(long item
, long column
) const;
306 // return the attribute for the item (may return NULL if none)
307 virtual wxListItemAttr
*OnGetItemAttr(long item
) const;
309 /* Why should we need this function? Leave for now.
310 * We might need it because item data may have changed,
311 * but the display needs refreshing (in string callback mode)
312 // Updates an item. If the list control has the wxLI_AUTO_ARRANGE style,
313 // the items will be rearranged.
314 bool Update(long item);
317 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
319 wxListCtrlCompare
GetCompareFunc() { return m_compareFunc
; };
320 wxIntPtr
GetCompareFuncData() { return m_compareFuncData
; };
323 // public overrides needed for pimpl approach
324 virtual bool SetFont(const wxFont
& font
);
325 virtual bool SetForegroundColour(const wxColour
& colour
);
326 virtual bool SetBackgroundColour(const wxColour
& colour
);
327 virtual wxColour
GetBackgroundColour() const;
329 virtual void Freeze ();
330 virtual void Thaw ();
331 virtual void Update ();
333 // functions for editing/timer
334 void OnRenameTimer();
335 bool OnRenameAccept(long itemEdit
, const wxString
& value
);
336 void OnRenameCancelled(long itemEdit
);
338 void ChangeCurrent(long current
);
339 void ResetCurrent() { ChangeCurrent((long)-1); }
340 bool HasCurrent() const { return m_current
!= (long)-1; }
342 void OnLeftDown(wxMouseEvent
& event
);
343 void OnDblClick(wxMouseEvent
& event
);
345 void FinishEditing(wxTextCtrl
*text
)
348 m_textctrlWrapper
= NULL
;
352 virtual int GetScrollPos(int orient
) const;
354 void OnRightDown(wxMouseEvent
& event
);
355 void OnMiddleDown(wxMouseEvent
& event
);
356 void OnChar(wxKeyEvent
& event
);
357 virtual void SetFocus();
358 void FireMouseEvent(wxEventType eventType
, wxPoint position
);
360 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
361 virtual wxDropTarget
* GetDropTarget() const;
363 // with CG, we need to get the context from an kEventControlDraw event
364 // unfortunately, the DataBrowser callbacks don't provide the context
365 // and we need it, so we need to set/remove it before and after draw
366 // events so we can access it in the callbacks.
367 void MacSetDrawingContext(void* context
) { m_cgContext
= context
; }
368 void* MacGetDrawingContext() { return m_cgContext
; }
370 virtual wxVisualAttributes
GetDefaultAttributes() const
372 return GetClassDefaultAttributes(GetWindowVariant());
375 static wxVisualAttributes
376 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
380 // protected overrides needed for pimpl approach
381 virtual void DoSetSize(int x
, int y
,
382 int width
, int height
,
383 int sizeFlags
= wxSIZE_AUTO
);
385 virtual wxSize
DoGetBestSize() const;
388 wxListCtrlTextCtrlWrapper
*m_textctrlWrapper
;
389 wxListCtrlRenameTimer
*m_renameTimer
;
390 // common part of all ctors
393 wxGenericListCtrl
* m_genericImpl
; // allow use of the generic impl.
394 wxMacDataBrowserListCtrlControl
* m_dbImpl
;
395 void* m_macListCtrlEventHandler
;
397 wxListCtrlCompare m_compareFunc
;
398 wxIntPtr m_compareFuncData
;
400 wxTextCtrl
* m_textCtrl
; // The control used for editing a label
401 wxImageList
* m_imageListNormal
; // The image list for normal icons
402 wxImageList
* m_imageListSmall
; // The image list for small icons
403 wxImageList
* m_imageListState
; // The image list state icons (not implemented yet)
405 wxColumnList m_colsInfo
; // for storing info about each column
406 wxColour m_textColor
;
409 // keep track of whether or not we should delete the image list ourselves.
410 bool m_ownsImageListNormal
,
411 m_ownsImageListSmall
,
412 m_ownsImageListState
;
414 long m_baseStyle
; // Basic Windows style flags, for recreation purposes
415 int m_colCount
; // Windows doesn't have GetColumnCount so must
416 // keep track of inserted/deleted columns
418 int m_count
; // for virtual lists, store item count
421 int CalcColumnAutoWidth(int col
) const;