| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/msw/listctrl.h |
| 3 | // Purpose: wxListCtrl class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_LISTCTRL_H_ |
| 13 | #define _WX_LISTCTRL_H_ |
| 14 | |
| 15 | #if wxUSE_LISTCTRL |
| 16 | |
| 17 | #include "wx/control.h" |
| 18 | #include "wx/event.h" |
| 19 | #include "wx/hash.h" |
| 20 | #include "wx/textctrl.h" |
| 21 | |
| 22 | |
| 23 | class WXDLLEXPORT wxImageList; |
| 24 | |
| 25 | /* |
| 26 | The wxListCtrl can show lists of items in four different modes: |
| 27 | wxLC_LIST: multicolumn list view, with optional small icons (icons could be |
| 28 | optional for some platforms). Columns are computed automatically, |
| 29 | i.e. you don't set columns as in wxLC_REPORT. In other words, |
| 30 | the list wraps, unlike a wxListBox. |
| 31 | wxLC_REPORT: single or multicolumn report view (with optional header) |
| 32 | wxLC_ICON: large icon view, with optional labels |
| 33 | wxLC_SMALL_ICON: small icon view, with optional labels |
| 34 | |
| 35 | You can change the style dynamically, either with SetSingleStyle or |
| 36 | SetWindowStyleFlag. |
| 37 | |
| 38 | Further window styles: |
| 39 | |
| 40 | wxLC_ALIGN_TOP icons align to the top (default) |
| 41 | wxLC_ALIGN_LEFT icons align to the left |
| 42 | wxLC_AUTOARRANGE icons arrange themselves |
| 43 | wxLC_USER_TEXT the app provides label text on demand, except for column headers |
| 44 | wxLC_EDIT_LABELS labels are editable: app will be notified. |
| 45 | wxLC_NO_HEADER no header in report mode |
| 46 | wxLC_NO_SORT_HEADER can't click on header |
| 47 | wxLC_SINGLE_SEL single selection |
| 48 | wxLC_SORT_ASCENDING sort ascending (must still supply a comparison callback in SortItems) |
| 49 | wxLC_SORT_DESCENDING sort descending (ditto) |
| 50 | |
| 51 | Items are referred to by their index (position in the list starting from zero). |
| 52 | |
| 53 | Label text is supplied via insertion/setting functions and is stored by the |
| 54 | control, unless the wxLC_USER_TEXT style has been specified, in which case |
| 55 | the app will be notified when text is required (see sample). |
| 56 | |
| 57 | Images are dealt with by (optionally) associating 3 image lists with the control. |
| 58 | Zero-based indexes into these image lists indicate which image is to be used for |
| 59 | which item. Each image in an image list can contain a mask, and can be made out |
| 60 | of either a bitmap, two bitmaps or an icon. See ImagList.h for more details. |
| 61 | |
| 62 | Notifications are passed via the wxWidgets 2.0 event system, or using virtual |
| 63 | functions in wxWidgets 1.66. |
| 64 | |
| 65 | See the sample wxListCtrl app for API usage. |
| 66 | |
| 67 | TODO: |
| 68 | - addition of further convenience functions |
| 69 | to avoid use of wxListItem in some functions |
| 70 | - state/overlay images: probably not needed. |
| 71 | - in Win95, you can be called back to supply other information |
| 72 | besides text, such as state information. This saves no memory |
| 73 | and is probably superfluous to requirements. |
| 74 | - testing of whole API, extending current sample. |
| 75 | |
| 76 | |
| 77 | */ |
| 78 | |
| 79 | class WXDLLEXPORT wxListCtrl: public wxControl |
| 80 | { |
| 81 | public: |
| 82 | /* |
| 83 | * Public interface |
| 84 | */ |
| 85 | |
| 86 | wxListCtrl() { Init(); } |
| 87 | |
| 88 | wxListCtrl(wxWindow *parent, |
| 89 | wxWindowID id = wxID_ANY, |
| 90 | const wxPoint& pos = wxDefaultPosition, |
| 91 | const wxSize& size = wxDefaultSize, |
| 92 | long style = wxLC_ICON, |
| 93 | const wxValidator& validator = wxDefaultValidator, |
| 94 | const wxString& name = wxListCtrlNameStr) |
| 95 | { |
| 96 | Init(); |
| 97 | |
| 98 | Create(parent, id, pos, size, style, validator, name); |
| 99 | } |
| 100 | |
| 101 | virtual ~wxListCtrl(); |
| 102 | |
| 103 | bool Create(wxWindow *parent, |
| 104 | wxWindowID id = wxID_ANY, |
| 105 | const wxPoint& pos = wxDefaultPosition, |
| 106 | const wxSize& size = wxDefaultSize, |
| 107 | long style = wxLC_ICON, |
| 108 | const wxValidator& validator = wxDefaultValidator, |
| 109 | const wxString& name = wxListCtrlNameStr); |
| 110 | |
| 111 | |
| 112 | // Attributes |
| 113 | //////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | // Set the control colours |
| 116 | bool SetForegroundColour(const wxColour& col); |
| 117 | bool SetBackgroundColour(const wxColour& col); |
| 118 | |
| 119 | // Gets information about this column |
| 120 | bool GetColumn(int col, wxListItem& item) const; |
| 121 | |
| 122 | // Sets information about this column |
| 123 | bool SetColumn(int col, const wxListItem& item) ; |
| 124 | |
| 125 | // Gets the column width |
| 126 | int GetColumnWidth(int col) const; |
| 127 | |
| 128 | // Sets the column width |
| 129 | bool SetColumnWidth(int col, int width) ; |
| 130 | |
| 131 | // Gets the number of items that can fit vertically in the |
| 132 | // visible area of the list control (list or report view) |
| 133 | // or the total number of items in the list control (icon |
| 134 | // or small icon view) |
| 135 | int GetCountPerPage() const; |
| 136 | |
| 137 | // return the total area occupied by all the items (icon/small icon only) |
| 138 | wxRect GetViewRect() const; |
| 139 | |
| 140 | // Gets the edit control for editing labels. |
| 141 | wxTextCtrl* GetEditControl() const; |
| 142 | |
| 143 | // Gets information about the item |
| 144 | bool GetItem(wxListItem& info) const ; |
| 145 | |
| 146 | // Sets information about the item |
| 147 | bool SetItem(wxListItem& info) ; |
| 148 | |
| 149 | // Sets a string field at a particular column |
| 150 | long SetItem(long index, int col, const wxString& label, int imageId = -1); |
| 151 | |
| 152 | // Gets the item state |
| 153 | int GetItemState(long item, long stateMask) const ; |
| 154 | |
| 155 | // Sets the item state |
| 156 | bool SetItemState(long item, long state, long stateMask) ; |
| 157 | |
| 158 | // Sets the item image |
| 159 | bool SetItemImage(long item, int image, int selImage = -1) ; |
| 160 | bool SetItemColumnImage(long item, long column, int image); |
| 161 | |
| 162 | // Gets the item text |
| 163 | wxString GetItemText(long item) const ; |
| 164 | |
| 165 | // Sets the item text |
| 166 | void SetItemText(long item, const wxString& str) ; |
| 167 | |
| 168 | // Gets the item data |
| 169 | wxUIntPtr GetItemData(long item) const ; |
| 170 | |
| 171 | // Sets the item data |
| 172 | bool SetItemData(long item, long data) ; |
| 173 | |
| 174 | // Gets the item rectangle |
| 175 | bool GetItemRect(long item, wxRect& rect, int code = wxLIST_RECT_BOUNDS) const ; |
| 176 | |
| 177 | // Gets the item position |
| 178 | bool GetItemPosition(long item, wxPoint& pos) const ; |
| 179 | |
| 180 | // Sets the item position |
| 181 | bool SetItemPosition(long item, const wxPoint& pos) ; |
| 182 | |
| 183 | // Gets the number of items in the list control |
| 184 | int GetItemCount() const; |
| 185 | |
| 186 | // Gets the number of columns in the list control |
| 187 | int GetColumnCount() const { return m_colCount; } |
| 188 | |
| 189 | // get the horizontal and vertical components of the item spacing |
| 190 | wxSize GetItemSpacing() const; |
| 191 | |
| 192 | // Foreground colour of an item. |
| 193 | void SetItemTextColour( long item, const wxColour& col); |
| 194 | wxColour GetItemTextColour( long item ) const; |
| 195 | |
| 196 | // Background colour of an item. |
| 197 | void SetItemBackgroundColour( long item, const wxColour &col); |
| 198 | wxColour GetItemBackgroundColour( long item ) const; |
| 199 | |
| 200 | // Font of an item. |
| 201 | void SetItemFont( long item, const wxFont &f); |
| 202 | wxFont GetItemFont( long item ) const; |
| 203 | |
| 204 | // Gets the number of selected items in the list control |
| 205 | int GetSelectedItemCount() const; |
| 206 | |
| 207 | // Gets the text colour of the listview |
| 208 | wxColour GetTextColour() const; |
| 209 | |
| 210 | // Sets the text colour of the listview |
| 211 | void SetTextColour(const wxColour& col); |
| 212 | |
| 213 | // Gets the index of the topmost visible item when in |
| 214 | // list or report view |
| 215 | long GetTopItem() const ; |
| 216 | |
| 217 | // Add or remove a single window style |
| 218 | void SetSingleStyle(long style, bool add = true) ; |
| 219 | |
| 220 | // Set the whole window style |
| 221 | void SetWindowStyleFlag(long style) ; |
| 222 | |
| 223 | // Searches for an item, starting from 'item'. |
| 224 | // item can be -1 to find the first item that matches the |
| 225 | // specified flags. |
| 226 | // Returns the item or -1 if unsuccessful. |
| 227 | long GetNextItem(long item, int geometry = wxLIST_NEXT_ALL, int state = wxLIST_STATE_DONTCARE) const ; |
| 228 | |
| 229 | // Gets one of the three image lists |
| 230 | wxImageList *GetImageList(int which) const ; |
| 231 | |
| 232 | // Sets the image list |
| 233 | // N.B. There's a quirk in the Win95 list view implementation. |
| 234 | // If in wxLC_LIST mode, it'll *still* display images by the labels if |
| 235 | // there's a small-icon image list set for the control - even though you |
| 236 | // haven't specified wxLIST_MASK_IMAGE when inserting. |
| 237 | // So you have to set a NULL small-icon image list to be sure that |
| 238 | // the wxLC_LIST mode works without icons. Of course, you may want icons... |
| 239 | void SetImageList(wxImageList *imageList, int which) ; |
| 240 | void AssignImageList(wxImageList *imageList, int which) ; |
| 241 | |
| 242 | // are we in report mode? |
| 243 | bool InReportView() const { return HasFlag(wxLC_REPORT); } |
| 244 | |
| 245 | // are we in virtual report mode? |
| 246 | bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); } |
| 247 | |
| 248 | // refresh items selectively (only useful for virtual list controls) |
| 249 | void RefreshItem(long item); |
| 250 | void RefreshItems(long itemFrom, long itemTo); |
| 251 | |
| 252 | // Operations |
| 253 | //////////////////////////////////////////////////////////////////////////// |
| 254 | |
| 255 | // Arranges the items |
| 256 | bool Arrange(int flag = wxLIST_ALIGN_DEFAULT); |
| 257 | |
| 258 | // Deletes an item |
| 259 | bool DeleteItem(long item); |
| 260 | |
| 261 | // Deletes all items |
| 262 | bool DeleteAllItems() ; |
| 263 | |
| 264 | // Deletes a column |
| 265 | bool DeleteColumn(int col); |
| 266 | |
| 267 | // Deletes all columns |
| 268 | bool DeleteAllColumns(); |
| 269 | |
| 270 | // Clears items, and columns if there are any. |
| 271 | void ClearAll(); |
| 272 | |
| 273 | // Edit the label |
| 274 | wxTextCtrl* EditLabel(long item, wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl)); |
| 275 | |
| 276 | // End label editing, optionally cancelling the edit |
| 277 | bool EndEditLabel(bool cancel); |
| 278 | |
| 279 | // Ensures this item is visible |
| 280 | bool EnsureVisible(long item) ; |
| 281 | |
| 282 | // Find an item whose label matches this string, starting from the item after 'start' |
| 283 | // or the beginning if 'start' is -1. |
| 284 | long FindItem(long start, const wxString& str, bool partial = false); |
| 285 | |
| 286 | // Find an item whose data matches this data, starting from the item after 'start' |
| 287 | // or the beginning if 'start' is -1. |
| 288 | long FindItem(long start, wxUIntPtr data); |
| 289 | |
| 290 | // Find an item nearest this position in the specified direction, starting from |
| 291 | // the item after 'start' or the beginning if 'start' is -1. |
| 292 | long FindItem(long start, const wxPoint& pt, int direction); |
| 293 | |
| 294 | // Determines which item (if any) is at the specified point, |
| 295 | // giving details in 'flags' (see wxLIST_HITTEST_... flags above) |
| 296 | long HitTest(const wxPoint& point, int& flags); |
| 297 | |
| 298 | // Inserts an item, returning the index of the new item if successful, |
| 299 | // -1 otherwise. |
| 300 | long InsertItem(const wxListItem& info); |
| 301 | |
| 302 | // Insert a string item |
| 303 | long InsertItem(long index, const wxString& label); |
| 304 | |
| 305 | // Insert an image item |
| 306 | long InsertItem(long index, int imageIndex); |
| 307 | |
| 308 | // Insert an image/string item |
| 309 | long InsertItem(long index, const wxString& label, int imageIndex); |
| 310 | |
| 311 | // For list view mode (only), inserts a column. |
| 312 | long InsertColumn(long col, const wxListItem& info); |
| 313 | |
| 314 | long InsertColumn(long col, |
| 315 | const wxString& heading, |
| 316 | int format = wxLIST_FORMAT_LEFT, |
| 317 | int width = -1); |
| 318 | |
| 319 | // set the number of items in a virtual list control |
| 320 | void SetItemCount(long count); |
| 321 | |
| 322 | // Scrolls the list control. If in icon, small icon or report view mode, |
| 323 | // x specifies the number of pixels to scroll. If in list view mode, x |
| 324 | // specifies the number of columns to scroll. |
| 325 | // If in icon, small icon or list view mode, y specifies the number of pixels |
| 326 | // to scroll. If in report view mode, y specifies the number of lines to scroll. |
| 327 | bool ScrollList(int dx, int dy); |
| 328 | |
| 329 | // Sort items. |
| 330 | |
| 331 | // fn is a function which takes 3 long arguments: item1, item2, data. |
| 332 | // item1 is the long data associated with a first item (NOT the index). |
| 333 | // item2 is the long data associated with a second item (NOT the index). |
| 334 | // data is the same value as passed to SortItems. |
| 335 | // The return value is a negative number if the first item should precede the second |
| 336 | // item, a positive number of the second item should precede the first, |
| 337 | // or zero if the two items are equivalent. |
| 338 | |
| 339 | // data is arbitrary data to be passed to the sort function. |
| 340 | bool SortItems(wxListCtrlCompare fn, long data); |
| 341 | |
| 342 | // IMPLEMENTATION |
| 343 | virtual bool MSWCommand(WXUINT param, WXWORD id); |
| 344 | virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result); |
| 345 | |
| 346 | // bring the control in sync with current m_windowStyle value |
| 347 | void UpdateStyle(); |
| 348 | |
| 349 | // Event handlers |
| 350 | //////////////////////////////////////////////////////////////////////////// |
| 351 | // Necessary for drawing hrules and vrules, if specified |
| 352 | void OnPaint(wxPaintEvent& event); |
| 353 | |
| 354 | |
| 355 | virtual bool ShouldInheritColours() const { return false; } |
| 356 | |
| 357 | virtual wxVisualAttributes GetDefaultAttributes() const |
| 358 | { |
| 359 | return GetClassDefaultAttributes(GetWindowVariant()); |
| 360 | } |
| 361 | |
| 362 | static wxVisualAttributes |
| 363 | GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); |
| 364 | |
| 365 | |
| 366 | #if WXWIN_COMPATIBILITY_2_6 |
| 367 | // obsolete stuff, for compatibility only -- don't use |
| 368 | wxDEPRECATED( int GetItemSpacing(bool isSmall) const); |
| 369 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 370 | |
| 371 | // convert our styles to Windows |
| 372 | virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const; |
| 373 | |
| 374 | // special Windows message handling |
| 375 | virtual WXLRESULT MSWWindowProc(WXUINT nMsg, |
| 376 | WXWPARAM wParam, |
| 377 | WXLPARAM lParam); |
| 378 | |
| 379 | protected: |
| 380 | // common part of all ctors |
| 381 | void Init(); |
| 382 | |
| 383 | // free memory taken by all internal data |
| 384 | void FreeAllInternalData(); |
| 385 | |
| 386 | // get the item attribute, either by quering it for virtual control, or by |
| 387 | // returning the one previously set using setter methods for a normal one |
| 388 | wxListItemAttr *DoGetItemAttr(long item) const; |
| 389 | |
| 390 | |
| 391 | wxTextCtrl* m_textCtrl; // The control used for editing a label |
| 392 | wxImageList * m_imageListNormal; // The image list for normal icons |
| 393 | wxImageList * m_imageListSmall; // The image list for small icons |
| 394 | wxImageList * m_imageListState; // The image list state icons (not implemented yet) |
| 395 | bool m_ownsImageListNormal, |
| 396 | m_ownsImageListSmall, |
| 397 | m_ownsImageListState; |
| 398 | |
| 399 | int m_colCount; // Windows doesn't have GetColumnCount so must |
| 400 | // keep track of inserted/deleted columns |
| 401 | long m_count; // Keep track of item count to save calls to |
| 402 | // ListView_GetItemCount |
| 403 | bool m_ignoreChangeMessages; |
| 404 | |
| 405 | // true if we have any internal data (user data & attributes) |
| 406 | bool m_AnyInternalData; |
| 407 | |
| 408 | // true if we have any items with custom attributes |
| 409 | bool m_hasAnyAttr; |
| 410 | |
| 411 | // these functions are only used for virtual list view controls, i.e. the |
| 412 | // ones with wxLC_VIRTUAL style |
| 413 | |
| 414 | // return the text for the given column of the given item |
| 415 | virtual wxString OnGetItemText(long item, long column) const; |
| 416 | |
| 417 | // return the icon for the given item. In report view, OnGetItemImage will |
| 418 | // only be called for the first column. See OnGetItemColumnImage for |
| 419 | // details. |
| 420 | virtual int OnGetItemImage(long item) const; |
| 421 | |
| 422 | // return the icon for the given item and column. |
| 423 | virtual int OnGetItemColumnImage(long item, long column) const; |
| 424 | |
| 425 | // return the attribute for the item (may return NULL if none) |
| 426 | virtual wxListItemAttr *OnGetItemAttr(long item) const; |
| 427 | |
| 428 | private: |
| 429 | // process NM_CUSTOMDRAW notification message |
| 430 | WXLPARAM OnCustomDraw(WXLPARAM lParam); |
| 431 | |
| 432 | DECLARE_DYNAMIC_CLASS(wxListCtrl) |
| 433 | DECLARE_EVENT_TABLE() |
| 434 | DECLARE_NO_COPY_CLASS(wxListCtrl) |
| 435 | }; |
| 436 | |
| 437 | #endif // wxUSE_LISTCTRL |
| 438 | |
| 439 | #endif // _WX_LISTCTRL_H_ |