added wxListCtrl::GetSubItemRect() and subitem hit testing (patch 1476971)
[wxWidgets.git] / include / wx / listbase.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/listctrl.h
3 // Purpose: wxListCtrl class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.12.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_LISTBASE_H_BASE_
13 #define _WX_LISTBASE_H_BASE_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_LISTCTRL
18
19 #include "wx/colour.h"
20 #include "wx/font.h"
21 #include "wx/gdicmn.h"
22
23 #include "wx/event.h"
24
25 // ----------------------------------------------------------------------------
26 // types
27 // ----------------------------------------------------------------------------
28
29 // type of compare function for wxListCtrl sort operation
30 typedef int (wxCALLBACK *wxListCtrlCompare)(long item1, long item2, long sortData);
31
32 // ----------------------------------------------------------------------------
33 // wxListCtrl constants
34 // ----------------------------------------------------------------------------
35
36 // style flags
37 #define wxLC_VRULES 0x0001
38 #define wxLC_HRULES 0x0002
39
40 #define wxLC_ICON 0x0004
41 #define wxLC_SMALL_ICON 0x0008
42 #define wxLC_LIST 0x0010
43 #define wxLC_REPORT 0x0020
44
45 #define wxLC_ALIGN_TOP 0x0040
46 #define wxLC_ALIGN_LEFT 0x0080
47 #define wxLC_AUTOARRANGE 0x0100
48 #define wxLC_VIRTUAL 0x0200
49 #define wxLC_EDIT_LABELS 0x0400
50 #define wxLC_NO_HEADER 0x0800
51 #define wxLC_NO_SORT_HEADER 0x1000
52 #define wxLC_SINGLE_SEL 0x2000
53 #define wxLC_SORT_ASCENDING 0x4000
54 #define wxLC_SORT_DESCENDING 0x8000
55
56 #define wxLC_MASK_TYPE (wxLC_ICON | wxLC_SMALL_ICON | wxLC_LIST | wxLC_REPORT)
57 #define wxLC_MASK_ALIGN (wxLC_ALIGN_TOP | wxLC_ALIGN_LEFT)
58 #define wxLC_MASK_SORT (wxLC_SORT_ASCENDING | wxLC_SORT_DESCENDING)
59
60 // for compatibility only
61 #define wxLC_USER_TEXT wxLC_VIRTUAL
62
63 // Omitted because
64 // (a) too much detail
65 // (b) not enough style flags
66 // (c) not implemented anyhow in the generic version
67 //
68 // #define wxLC_NO_SCROLL
69 // #define wxLC_NO_LABEL_WRAP
70 // #define wxLC_OWNERDRAW_FIXED
71 // #define wxLC_SHOW_SEL_ALWAYS
72
73 // Mask flags to tell app/GUI what fields of wxListItem are valid
74 #define wxLIST_MASK_STATE 0x0001
75 #define wxLIST_MASK_TEXT 0x0002
76 #define wxLIST_MASK_IMAGE 0x0004
77 #define wxLIST_MASK_DATA 0x0008
78 #define wxLIST_SET_ITEM 0x0010
79 #define wxLIST_MASK_WIDTH 0x0020
80 #define wxLIST_MASK_FORMAT 0x0040
81
82 // State flags for indicating the state of an item
83 #define wxLIST_STATE_DONTCARE 0x0000
84 #define wxLIST_STATE_DROPHILITED 0x0001 // MSW only
85 #define wxLIST_STATE_FOCUSED 0x0002
86 #define wxLIST_STATE_SELECTED 0x0004
87 #define wxLIST_STATE_CUT 0x0008 // MSW only
88 #define wxLIST_STATE_DISABLED 0x0010 // OS2 only
89 #define wxLIST_STATE_FILTERED 0x0020 // OS2 only
90 #define wxLIST_STATE_INUSE 0x0040 // OS2 only
91 #define wxLIST_STATE_PICKED 0x0080 // OS2 only
92 #define wxLIST_STATE_SOURCE 0x0100 // OS2 only
93
94 // Hit test flags, used in HitTest
95 #define wxLIST_HITTEST_ABOVE 0x0001 // Above the client area.
96 #define wxLIST_HITTEST_BELOW 0x0002 // Below the client area.
97 #define wxLIST_HITTEST_NOWHERE 0x0004 // In the client area but below the last item.
98 #define wxLIST_HITTEST_ONITEMICON 0x0020 // On the bitmap associated with an item.
99 #define wxLIST_HITTEST_ONITEMLABEL 0x0080 // On the label (string) associated with an item.
100 #define wxLIST_HITTEST_ONITEMRIGHT 0x0100 // In the area to the right of an item.
101 #define wxLIST_HITTEST_ONITEMSTATEICON 0x0200 // On the state icon for a tree view item that is in a user-defined state.
102 #define wxLIST_HITTEST_TOLEFT 0x0400 // To the left of the client area.
103 #define wxLIST_HITTEST_TORIGHT 0x0800 // To the right of the client area.
104
105 #define wxLIST_HITTEST_ONITEM (wxLIST_HITTEST_ONITEMICON | wxLIST_HITTEST_ONITEMLABEL | wxLIST_HITTEST_ONITEMSTATEICON)
106
107 // GetSubItemRect constants
108 #define wxLIST_GETSUBITEMRECT_WHOLEITEM -1l
109
110 // Flags for GetNextItem (MSW only except wxLIST_NEXT_ALL)
111 enum
112 {
113 wxLIST_NEXT_ABOVE, // Searches for an item above the specified item
114 wxLIST_NEXT_ALL, // Searches for subsequent item by index
115 wxLIST_NEXT_BELOW, // Searches for an item below the specified item
116 wxLIST_NEXT_LEFT, // Searches for an item to the left of the specified item
117 wxLIST_NEXT_RIGHT // Searches for an item to the right of the specified item
118 };
119
120 // Alignment flags for Arrange (MSW only except wxLIST_ALIGN_LEFT)
121 enum
122 {
123 wxLIST_ALIGN_DEFAULT,
124 wxLIST_ALIGN_LEFT,
125 wxLIST_ALIGN_TOP,
126 wxLIST_ALIGN_SNAP_TO_GRID
127 };
128
129 // Column format (MSW only except wxLIST_FORMAT_LEFT)
130 enum wxListColumnFormat
131 {
132 wxLIST_FORMAT_LEFT,
133 wxLIST_FORMAT_RIGHT,
134 wxLIST_FORMAT_CENTRE,
135 wxLIST_FORMAT_CENTER = wxLIST_FORMAT_CENTRE
136 };
137
138 // Autosize values for SetColumnWidth
139 enum
140 {
141 wxLIST_AUTOSIZE = -1,
142 wxLIST_AUTOSIZE_USEHEADER = -2 // partly supported by generic version
143 };
144
145 // Flag values for GetItemRect
146 enum
147 {
148 wxLIST_RECT_BOUNDS,
149 wxLIST_RECT_ICON,
150 wxLIST_RECT_LABEL
151 };
152
153 // Flag values for FindItem (MSW only)
154 enum
155 {
156 wxLIST_FIND_UP,
157 wxLIST_FIND_DOWN,
158 wxLIST_FIND_LEFT,
159 wxLIST_FIND_RIGHT
160 };
161
162 // ----------------------------------------------------------------------------
163 // wxListItemAttr: a structure containing the visual attributes of an item
164 // ----------------------------------------------------------------------------
165
166 // TODO: this should be renamed to wxItemAttr or something general like this
167 // and used as base class for wxTextAttr which duplicates this class
168 // entirely currently
169 class WXDLLEXPORT wxListItemAttr
170 {
171 public:
172 // ctors
173 wxListItemAttr() { }
174 wxListItemAttr(const wxColour& colText,
175 const wxColour& colBack,
176 const wxFont& font)
177 : m_colText(colText), m_colBack(colBack), m_font(font)
178 {
179 }
180
181 // default copy ctor, assignment operator and dtor are ok
182
183
184 // setters
185 void SetTextColour(const wxColour& colText) { m_colText = colText; }
186 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
187 void SetFont(const wxFont& font) { m_font = font; }
188
189 // accessors
190 bool HasTextColour() const { return m_colText.Ok(); }
191 bool HasBackgroundColour() const { return m_colBack.Ok(); }
192 bool HasFont() const { return m_font.Ok(); }
193
194 const wxColour& GetTextColour() const { return m_colText; }
195 const wxColour& GetBackgroundColour() const { return m_colBack; }
196 const wxFont& GetFont() const { return m_font; }
197
198
199 // this is almost like assignment operator except it doesn't overwrite the
200 // fields unset in the source attribute
201 void AssignFrom(const wxListItemAttr& source)
202 {
203 if ( source.HasTextColour() )
204 SetTextColour(source.GetTextColour());
205 if ( source.HasBackgroundColour() )
206 SetBackgroundColour(source.GetBackgroundColour());
207 if ( source.HasFont() )
208 SetFont(source.GetFont());
209 }
210
211 private:
212 wxColour m_colText,
213 m_colBack;
214 wxFont m_font;
215 };
216
217 // ----------------------------------------------------------------------------
218 // wxListItem: the item or column info, used to exchange data with wxListCtrl
219 // ----------------------------------------------------------------------------
220
221 class WXDLLEXPORT wxListItem : public wxObject
222 {
223 public:
224 wxListItem() { Init(); m_attr = NULL; }
225 wxListItem(const wxListItem& item)
226 : wxObject(),
227 m_mask(item.m_mask),
228 m_itemId(item.m_itemId),
229 m_col(item.m_col),
230 m_state(item.m_state),
231 m_stateMask(item.m_stateMask),
232 m_text(item.m_text),
233 m_image(item.m_image),
234 m_data(item.m_data),
235 m_format(item.m_format),
236 m_width(item.m_width),
237 m_attr(NULL)
238 {
239 // copy list item attributes
240 if ( item.HasAttributes() )
241 m_attr = new wxListItemAttr(*item.GetAttributes());
242 }
243 virtual ~wxListItem() { delete m_attr; }
244
245 // resetting
246 void Clear() { Init(); m_text.clear(); ClearAttributes(); }
247 void ClearAttributes() { if ( m_attr ) { delete m_attr; m_attr = NULL; } }
248
249 // setters
250 void SetMask(long mask)
251 { m_mask = mask; }
252 void SetId(long id)
253 { m_itemId = id; }
254 void SetColumn(int col)
255 { m_col = col; }
256 void SetState(long state)
257 { m_mask |= wxLIST_MASK_STATE; m_state = state; m_stateMask |= state; }
258 void SetStateMask(long stateMask)
259 { m_stateMask = stateMask; }
260 void SetText(const wxString& text)
261 { m_mask |= wxLIST_MASK_TEXT; m_text = text; }
262 void SetImage(int image)
263 { m_mask |= wxLIST_MASK_IMAGE; m_image = image; }
264 void SetData(long data)
265 { m_mask |= wxLIST_MASK_DATA; m_data = data; }
266 void SetData(void *data)
267 { m_mask |= wxLIST_MASK_DATA; m_data = wxPtrToUInt(data); }
268
269 void SetWidth(int width)
270 { m_mask |= wxLIST_MASK_WIDTH; m_width = width; }
271 void SetAlign(wxListColumnFormat align)
272 { m_mask |= wxLIST_MASK_FORMAT; m_format = align; }
273
274 void SetTextColour(const wxColour& colText)
275 { Attributes().SetTextColour(colText); }
276 void SetBackgroundColour(const wxColour& colBack)
277 { Attributes().SetBackgroundColour(colBack); }
278 void SetFont(const wxFont& font)
279 { Attributes().SetFont(font); }
280
281 // accessors
282 long GetMask() const { return m_mask; }
283 long GetId() const { return m_itemId; }
284 int GetColumn() const { return m_col; }
285 long GetState() const { return m_state & m_stateMask; }
286 const wxString& GetText() const { return m_text; }
287 int GetImage() const { return m_image; }
288 wxUIntPtr GetData() const { return m_data; }
289
290 int GetWidth() const { return m_width; }
291 wxListColumnFormat GetAlign() const { return (wxListColumnFormat)m_format; }
292
293 wxListItemAttr *GetAttributes() const { return m_attr; }
294 bool HasAttributes() const { return m_attr != NULL; }
295
296 wxColour GetTextColour() const
297 { return HasAttributes() ? m_attr->GetTextColour() : wxNullColour; }
298 wxColour GetBackgroundColour() const
299 { return HasAttributes() ? m_attr->GetBackgroundColour()
300 : wxNullColour; }
301 wxFont GetFont() const
302 { return HasAttributes() ? m_attr->GetFont() : wxNullFont; }
303
304 // this conversion is necessary to make old code using GetItem() to
305 // compile
306 operator long() const { return m_itemId; }
307
308 // these members are public for compatibility
309
310 long m_mask; // Indicates what fields are valid
311 long m_itemId; // The zero-based item position
312 int m_col; // Zero-based column, if in report mode
313 long m_state; // The state of the item
314 long m_stateMask;// Which flags of m_state are valid (uses same flags)
315 wxString m_text; // The label/header text
316 int m_image; // The zero-based index into an image list
317 wxUIntPtr m_data; // App-defined data
318
319 // For columns only
320 int m_format; // left, right, centre
321 int m_width; // width of column
322
323 #ifdef __WXPM__
324 int m_miniImage; // handle to the mini image for OS/2
325 #endif
326
327 protected:
328 // creates m_attr if we don't have it yet
329 wxListItemAttr& Attributes()
330 {
331 if ( !m_attr )
332 m_attr = new wxListItemAttr;
333
334 return *m_attr;
335 }
336
337 void Init()
338 {
339 m_mask = 0;
340 m_itemId = 0;
341 m_col = 0;
342 m_state = 0;
343 m_stateMask = 0;
344 m_image = 0;
345 m_data = 0;
346
347 m_format = wxLIST_FORMAT_CENTRE;
348 m_width = 0;
349 }
350
351 wxListItemAttr *m_attr; // optional pointer to the items style
352
353 private:
354 // VZ: this is strange, we have a copy ctor but not operator=(), why?
355 wxListItem& operator=(const wxListItem& item);
356
357 DECLARE_DYNAMIC_CLASS(wxListItem)
358 };
359
360 // ----------------------------------------------------------------------------
361 // wxListEvent - the event class for the wxListCtrl notifications
362 // ----------------------------------------------------------------------------
363
364 class WXDLLEXPORT wxListEvent : public wxNotifyEvent
365 {
366 public:
367 wxListEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
368 : wxNotifyEvent(commandType, winid)
369 , m_code(0)
370 , m_oldItemIndex(0)
371 , m_itemIndex(0)
372 , m_col(0)
373 , m_pointDrag()
374 , m_item()
375 , m_editCancelled(false)
376 { }
377
378 wxListEvent(const wxListEvent& event)
379 : wxNotifyEvent(event)
380 , m_code(event.m_code)
381 , m_oldItemIndex(event.m_oldItemIndex)
382 , m_itemIndex(event.m_itemIndex)
383 , m_col(event.m_col)
384 , m_pointDrag(event.m_pointDrag)
385 , m_item(event.m_item)
386 , m_editCancelled(event.m_editCancelled)
387 { }
388
389 int GetKeyCode() const { return m_code; }
390 long GetIndex() const { return m_itemIndex; }
391 int GetColumn() const { return m_col; }
392 wxPoint GetPoint() const { return m_pointDrag; }
393 const wxString& GetLabel() const { return m_item.m_text; }
394 const wxString& GetText() const { return m_item.m_text; }
395 int GetImage() const { return m_item.m_image; }
396 long GetData() const { return wx_static_cast(long, m_item.m_data); }
397 long GetMask() const { return m_item.m_mask; }
398 const wxListItem& GetItem() const { return m_item; }
399
400 // for wxEVT_COMMAND_LIST_CACHE_HINT only
401 long GetCacheFrom() const { return m_oldItemIndex; }
402 long GetCacheTo() const { return m_itemIndex; }
403
404 // was label editing canceled? (for wxEVT_COMMAND_LIST_END_LABEL_EDIT only)
405 bool IsEditCancelled() const { return m_editCancelled; }
406 void SetEditCanceled(bool editCancelled) { m_editCancelled = editCancelled; }
407
408 virtual wxEvent *Clone() const { return new wxListEvent(*this); }
409
410 //protected: -- not for backwards compatibility
411 int m_code;
412 long m_oldItemIndex; // only for wxEVT_COMMAND_LIST_CACHE_HINT
413 long m_itemIndex;
414 int m_col;
415 wxPoint m_pointDrag;
416
417 wxListItem m_item;
418
419 protected:
420 bool m_editCancelled;
421
422 private:
423 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxListEvent)
424 };
425
426 // ----------------------------------------------------------------------------
427 // wxListCtrl event macros
428 // ----------------------------------------------------------------------------
429
430 BEGIN_DECLARE_EVENT_TYPES()
431 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG, 700)
432 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG, 701)
433 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, 702)
434 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT, 703)
435 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM, 704)
436 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, 705)
437 #if WXWIN_COMPATIBILITY_2_4
438 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO, 706)
439 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO, 707)
440 #endif
441 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED, 708)
442 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED, 709)
443 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN, 710)
444 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM, 711)
445 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK, 712)
446 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, 713)
447 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK, 714)
448 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED, 715)
449 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT, 716)
450 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK, 717)
451 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG, 718)
452 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING, 719)
453 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG, 720)
454 DECLARE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED, 721)
455 END_DECLARE_EVENT_TYPES()
456
457 typedef void (wxEvtHandler::*wxListEventFunction)(wxListEvent&);
458
459 #define wxListEventHandler(func) \
460 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxListEventFunction, &func)
461
462 #define wx__DECLARE_LISTEVT(evt, id, fn) \
463 wx__DECLARE_EVT1(wxEVT_COMMAND_LIST_ ## evt, id, wxListEventHandler(fn))
464
465 #define EVT_LIST_BEGIN_DRAG(id, fn) wx__DECLARE_LISTEVT(BEGIN_DRAG, id, fn)
466 #define EVT_LIST_BEGIN_RDRAG(id, fn) wx__DECLARE_LISTEVT(BEGIN_RDRAG, id, fn)
467 #define EVT_LIST_BEGIN_LABEL_EDIT(id, fn) wx__DECLARE_LISTEVT(BEGIN_LABEL_EDIT, id, fn)
468 #define EVT_LIST_END_LABEL_EDIT(id, fn) wx__DECLARE_LISTEVT(END_LABEL_EDIT, id, fn)
469 #define EVT_LIST_DELETE_ITEM(id, fn) wx__DECLARE_LISTEVT(DELETE_ITEM, id, fn)
470 #define EVT_LIST_DELETE_ALL_ITEMS(id, fn) wx__DECLARE_LISTEVT(DELETE_ALL_ITEMS, id, fn)
471 #define EVT_LIST_KEY_DOWN(id, fn) wx__DECLARE_LISTEVT(KEY_DOWN, id, fn)
472 #define EVT_LIST_INSERT_ITEM(id, fn) wx__DECLARE_LISTEVT(INSERT_ITEM, id, fn)
473
474 #define EVT_LIST_COL_CLICK(id, fn) wx__DECLARE_LISTEVT(COL_CLICK, id, fn)
475 #define EVT_LIST_COL_RIGHT_CLICK(id, fn) wx__DECLARE_LISTEVT(COL_RIGHT_CLICK, id, fn)
476 #define EVT_LIST_COL_BEGIN_DRAG(id, fn) wx__DECLARE_LISTEVT(COL_BEGIN_DRAG, id, fn)
477 #define EVT_LIST_COL_DRAGGING(id, fn) wx__DECLARE_LISTEVT(COL_DRAGGING, id, fn)
478 #define EVT_LIST_COL_END_DRAG(id, fn) wx__DECLARE_LISTEVT(COL_END_DRAG, id, fn)
479
480 #define EVT_LIST_ITEM_SELECTED(id, fn) wx__DECLARE_LISTEVT(ITEM_SELECTED, id, fn)
481 #define EVT_LIST_ITEM_DESELECTED(id, fn) wx__DECLARE_LISTEVT(ITEM_DESELECTED, id, fn)
482 #define EVT_LIST_ITEM_RIGHT_CLICK(id, fn) wx__DECLARE_LISTEVT(ITEM_RIGHT_CLICK, id, fn)
483 #define EVT_LIST_ITEM_MIDDLE_CLICK(id, fn) wx__DECLARE_LISTEVT(ITEM_MIDDLE_CLICK, id, fn)
484 #define EVT_LIST_ITEM_ACTIVATED(id, fn) wx__DECLARE_LISTEVT(ITEM_ACTIVATED, id, fn)
485 #define EVT_LIST_ITEM_FOCUSED(id, fn) wx__DECLARE_LISTEVT(ITEM_FOCUSED, id, fn)
486
487 #define EVT_LIST_CACHE_HINT(id, fn) wx__DECLARE_LISTEVT(CACHE_HINT, id, fn)
488
489
490 #if WXWIN_COMPATIBILITY_2_4
491 #define EVT_LIST_GET_INFO(id, fn) wx__DECLARE_LISTEVT(GET_INFO, id, fn)
492 #define EVT_LIST_SET_INFO(id, fn) wx__DECLARE_LISTEVT(SET_INFO, id, fn)
493 #endif
494
495 #endif // wxUSE_LISTCTRL
496
497 #endif
498 // _WX_LISTCTRL_H_BASE_