headercol.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/headercol.h
  3. // Purpose: declaration of wxHeaderColumn class
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-12-02
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_HEADERCOL_H_
  10. #define _WX_HEADERCOL_H_
  11. #include "wx/bitmap.h"
  12. #if wxUSE_HEADERCTRL
  13. // ----------------------------------------------------------------------------
  14. // constants
  15. // ----------------------------------------------------------------------------
  16. enum
  17. {
  18. // special value for column width meaning unspecified/default
  19. wxCOL_WIDTH_DEFAULT = -1,
  20. // size the column automatically to fit all values
  21. wxCOL_WIDTH_AUTOSIZE = -2
  22. };
  23. // bit masks for the various column attributes
  24. enum
  25. {
  26. // column can be resized (included in default flags)
  27. wxCOL_RESIZABLE = 1,
  28. // column can be clicked to toggle the sort order by its contents
  29. wxCOL_SORTABLE = 2,
  30. // column can be dragged to change its order (included in default)
  31. wxCOL_REORDERABLE = 4,
  32. // column is not shown at all
  33. wxCOL_HIDDEN = 8,
  34. // default flags for wxHeaderColumn ctor
  35. wxCOL_DEFAULT_FLAGS = wxCOL_RESIZABLE | wxCOL_REORDERABLE
  36. };
  37. // ----------------------------------------------------------------------------
  38. // wxHeaderColumn: interface for a column in a header of controls such as
  39. // wxListCtrl, wxDataViewCtrl or wxGrid
  40. // ----------------------------------------------------------------------------
  41. class WXDLLIMPEXP_CORE wxHeaderColumn
  42. {
  43. public:
  44. // ctors and dtor
  45. // --------------
  46. /*
  47. Derived classes must provide ctors with the following signatures
  48. (notice that they shouldn't be explicit to allow passing strings/bitmaps
  49. directly to methods such wxHeaderCtrl::AppendColumn()):
  50. wxHeaderColumn(const wxString& title,
  51. int width = wxCOL_WIDTH_DEFAULT,
  52. wxAlignment align = wxALIGN_NOT,
  53. int flags = wxCOL_DEFAULT_FLAGS);
  54. wxHeaderColumn(const wxBitmap &bitmap,
  55. int width = wxDVC_DEFAULT_WIDTH,
  56. wxAlignment align = wxALIGN_CENTER,
  57. int flags = wxCOL_DEFAULT_FLAGS);
  58. */
  59. // virtual dtor for the base class to avoid gcc warnings even though we
  60. // don't normally delete the objects of this class via a pointer to
  61. // wxHeaderColumn so it's not necessary, strictly speaking
  62. virtual ~wxHeaderColumn() { }
  63. // getters for various attributes
  64. // ------------------------------
  65. // notice that wxHeaderColumn only provides getters as this is all the
  66. // wxHeaderCtrl needs, various derived class must also provide some way to
  67. // change these attributes but this can be done either at the column level
  68. // (in which case they should inherit from wxSettableHeaderColumn) or via
  69. // the methods of the main control in which case you don't need setters in
  70. // the column class at all
  71. // title is the string shown for this column
  72. virtual wxString GetTitle() const = 0;
  73. // bitmap shown (instead of text) in the column header
  74. virtual wxBitmap GetBitmap() const = 0; \
  75. // width of the column in pixels, can be set to wxCOL_WIDTH_DEFAULT meaning
  76. // unspecified/default
  77. virtual int GetWidth() const = 0;
  78. // minimal width can be set for resizable columns to forbid resizing them
  79. // below the specified size (set to 0 to remove)
  80. virtual int GetMinWidth() const = 0;
  81. // alignment of the text: wxALIGN_CENTRE, wxALIGN_LEFT or wxALIGN_RIGHT
  82. virtual wxAlignment GetAlignment() const = 0;
  83. // flags manipulations:
  84. // --------------------
  85. // notice that while we make GetFlags() pure virtual here and implement the
  86. // individual flags access in terms of it, for some derived classes it is
  87. // more natural to implement access to each flag individually, in which
  88. // case they can use our GetFromIndividualFlags() helper below to implement
  89. // GetFlags()
  90. // retrieve all column flags at once: combination of wxCOL_XXX values above
  91. virtual int GetFlags() const = 0;
  92. bool HasFlag(int flag) const { return (GetFlags() & flag) != 0; }
  93. // wxCOL_RESIZABLE
  94. virtual bool IsResizeable() const
  95. { return HasFlag(wxCOL_RESIZABLE); }
  96. // wxCOL_SORTABLE
  97. virtual bool IsSortable() const
  98. { return HasFlag(wxCOL_SORTABLE); }
  99. // wxCOL_REORDERABLE
  100. virtual bool IsReorderable() const
  101. { return HasFlag(wxCOL_REORDERABLE); }
  102. // wxCOL_HIDDEN
  103. virtual bool IsHidden() const
  104. { return HasFlag(wxCOL_HIDDEN); }
  105. bool IsShown() const
  106. { return !IsHidden(); }
  107. // sorting
  108. // -------
  109. // return true if the column is the one currently used for sorting
  110. virtual bool IsSortKey() const = 0;
  111. // for sortable columns indicate whether we should sort in ascending or
  112. // descending order (this should only be taken into account if IsSortKey())
  113. virtual bool IsSortOrderAscending() const = 0;
  114. protected:
  115. // helper for the class overriding IsXXX()
  116. int GetFromIndividualFlags() const;
  117. };
  118. // ----------------------------------------------------------------------------
  119. // wxSettableHeaderColumn: column which allows to change its fields too
  120. // ----------------------------------------------------------------------------
  121. class WXDLLIMPEXP_CORE wxSettableHeaderColumn : public wxHeaderColumn
  122. {
  123. public:
  124. virtual void SetTitle(const wxString& title) = 0;
  125. virtual void SetBitmap(const wxBitmap& bitmap) = 0;
  126. virtual void SetWidth(int width) = 0;
  127. virtual void SetMinWidth(int minWidth) = 0;
  128. virtual void SetAlignment(wxAlignment align) = 0;
  129. // see comment for wxHeaderColumn::GetFlags() about the relationship
  130. // between SetFlags() and Set{Sortable,Reorderable,...}
  131. // change, set, clear, toggle or test for any individual flag
  132. virtual void SetFlags(int flags) = 0;
  133. void ChangeFlag(int flag, bool set);
  134. void SetFlag(int flag);
  135. void ClearFlag(int flag);
  136. void ToggleFlag(int flag);
  137. virtual void SetResizeable(bool resizable)
  138. { ChangeFlag(wxCOL_RESIZABLE, resizable); }
  139. virtual void SetSortable(bool sortable)
  140. { ChangeFlag(wxCOL_SORTABLE, sortable); }
  141. virtual void SetReorderable(bool reorderable)
  142. { ChangeFlag(wxCOL_REORDERABLE, reorderable); }
  143. virtual void SetHidden(bool hidden)
  144. { ChangeFlag(wxCOL_HIDDEN, hidden); }
  145. // This function can be called to indicate that this column is not used for
  146. // sorting any more. Under some platforms it's not necessary to do anything
  147. // in this case as just setting another column as a sort key takes care of
  148. // everything but under MSW we currently need to call this explicitly to
  149. // reset the sort indicator displayed on the column.
  150. virtual void UnsetAsSortKey() { }
  151. virtual void SetSortOrder(bool ascending) = 0;
  152. void ToggleSortOrder() { SetSortOrder(!IsSortOrderAscending()); }
  153. protected:
  154. // helper for the class overriding individual SetXXX() methods instead of
  155. // overriding SetFlags()
  156. void SetIndividualFlags(int flags);
  157. };
  158. // ----------------------------------------------------------------------------
  159. // wxHeaderColumnSimple: trivial generic implementation of wxHeaderColumn
  160. // ----------------------------------------------------------------------------
  161. class wxHeaderColumnSimple : public wxSettableHeaderColumn
  162. {
  163. public:
  164. // ctors and dtor
  165. wxHeaderColumnSimple(const wxString& title,
  166. int width = wxCOL_WIDTH_DEFAULT,
  167. wxAlignment align = wxALIGN_NOT,
  168. int flags = wxCOL_DEFAULT_FLAGS)
  169. : m_title(title),
  170. m_width(width),
  171. m_align(align),
  172. m_flags(flags)
  173. {
  174. Init();
  175. }
  176. wxHeaderColumnSimple(const wxBitmap& bitmap,
  177. int width = wxCOL_WIDTH_DEFAULT,
  178. wxAlignment align = wxALIGN_CENTER,
  179. int flags = wxCOL_DEFAULT_FLAGS)
  180. : m_bitmap(bitmap),
  181. m_width(width),
  182. m_align(align),
  183. m_flags(flags)
  184. {
  185. Init();
  186. }
  187. // implement base class pure virtuals
  188. virtual void SetTitle(const wxString& title) { m_title = title; }
  189. virtual wxString GetTitle() const { return m_title; }
  190. virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
  191. wxBitmap GetBitmap() const { return m_bitmap; }
  192. virtual void SetWidth(int width) { m_width = width; }
  193. virtual int GetWidth() const { return m_width; }
  194. virtual void SetMinWidth(int minWidth) { m_minWidth = minWidth; }
  195. virtual int GetMinWidth() const { return m_minWidth; }
  196. virtual void SetAlignment(wxAlignment align) { m_align = align; }
  197. virtual wxAlignment GetAlignment() const { return m_align; }
  198. virtual void SetFlags(int flags) { m_flags = flags; }
  199. virtual int GetFlags() const { return m_flags; }
  200. virtual bool IsSortKey() const { return m_sort; }
  201. virtual void UnsetAsSortKey() { m_sort = false; }
  202. virtual void SetSortOrder(bool ascending)
  203. {
  204. m_sort = true;
  205. m_sortAscending = ascending;
  206. }
  207. virtual bool IsSortOrderAscending() const { return m_sortAscending; }
  208. private:
  209. // common part of all ctors
  210. void Init()
  211. {
  212. m_minWidth = 0;
  213. m_sort = false;
  214. m_sortAscending = true;
  215. }
  216. wxString m_title;
  217. wxBitmap m_bitmap;
  218. int m_width,
  219. m_minWidth;
  220. wxAlignment m_align;
  221. int m_flags;
  222. bool m_sort,
  223. m_sortAscending;
  224. };
  225. #endif // wxUSE_HEADERCTRL
  226. #endif // _WX_HEADERCOL_H_