dataview.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/dataview.h
  3. // Purpose: wxDataViewCtrl generic implementation header
  4. // Author: Robert Roebling
  5. // Modified By: Bo Yang
  6. // Copyright: (c) 1998 Robert Roebling
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef __GENERICDATAVIEWCTRLH__
  10. #define __GENERICDATAVIEWCTRLH__
  11. #include "wx/defs.h"
  12. #include "wx/object.h"
  13. #include "wx/list.h"
  14. #include "wx/control.h"
  15. #include "wx/scrolwin.h"
  16. #include "wx/icon.h"
  17. #include "wx/vector.h"
  18. class WXDLLIMPEXP_FWD_ADV wxDataViewMainWindow;
  19. class WXDLLIMPEXP_FWD_ADV wxDataViewHeaderWindow;
  20. // ---------------------------------------------------------
  21. // wxDataViewColumn
  22. // ---------------------------------------------------------
  23. class WXDLLIMPEXP_ADV wxDataViewColumn : public wxDataViewColumnBase
  24. {
  25. public:
  26. wxDataViewColumn(const wxString& title,
  27. wxDataViewRenderer *renderer,
  28. unsigned int model_column,
  29. int width = wxDVC_DEFAULT_WIDTH,
  30. wxAlignment align = wxALIGN_CENTER,
  31. int flags = wxDATAVIEW_COL_RESIZABLE)
  32. : wxDataViewColumnBase(renderer, model_column),
  33. m_title(title)
  34. {
  35. Init(width, align, flags);
  36. }
  37. wxDataViewColumn(const wxBitmap& bitmap,
  38. wxDataViewRenderer *renderer,
  39. unsigned int model_column,
  40. int width = wxDVC_DEFAULT_WIDTH,
  41. wxAlignment align = wxALIGN_CENTER,
  42. int flags = wxDATAVIEW_COL_RESIZABLE)
  43. : wxDataViewColumnBase(bitmap, renderer, model_column)
  44. {
  45. Init(width, align, flags);
  46. }
  47. // implement wxHeaderColumnBase methods
  48. virtual void SetTitle(const wxString& title) { m_title = title; UpdateDisplay(); }
  49. virtual wxString GetTitle() const { return m_title; }
  50. virtual void SetWidth(int width) { m_width = width; UpdateDisplay(); }
  51. virtual int GetWidth() const;
  52. virtual void SetMinWidth(int minWidth) { m_minWidth = minWidth; UpdateDisplay(); }
  53. virtual int GetMinWidth() const { return m_minWidth; }
  54. virtual void SetAlignment(wxAlignment align) { m_align = align; UpdateDisplay(); }
  55. virtual wxAlignment GetAlignment() const { return m_align; }
  56. virtual void SetFlags(int flags) { m_flags = flags; UpdateDisplay(); }
  57. virtual int GetFlags() const { return m_flags; }
  58. virtual bool IsSortKey() const { return m_sort; }
  59. virtual void UnsetAsSortKey();
  60. virtual void SetSortOrder(bool ascending);
  61. virtual bool IsSortOrderAscending() const { return m_sortAscending; }
  62. virtual void SetBitmap( const wxBitmap& bitmap ) { wxDataViewColumnBase::SetBitmap(bitmap); UpdateDisplay(); }
  63. private:
  64. // common part of all ctors
  65. void Init(int width, wxAlignment align, int flags);
  66. void UpdateDisplay();
  67. wxString m_title;
  68. int m_width,
  69. m_minWidth;
  70. wxAlignment m_align;
  71. int m_flags;
  72. bool m_sort,
  73. m_sortAscending;
  74. friend class wxDataViewHeaderWindowBase;
  75. friend class wxDataViewHeaderWindow;
  76. friend class wxDataViewHeaderWindowMSW;
  77. };
  78. // ---------------------------------------------------------
  79. // wxDataViewCtrl
  80. // ---------------------------------------------------------
  81. WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn, wxDataViewColumnList,
  82. class WXDLLIMPEXP_ADV);
  83. class WXDLLIMPEXP_ADV wxDataViewCtrl : public wxDataViewCtrlBase,
  84. public wxScrollHelper
  85. {
  86. friend class wxDataViewMainWindow;
  87. friend class wxDataViewHeaderWindowBase;
  88. friend class wxDataViewHeaderWindow;
  89. friend class wxDataViewHeaderWindowMSW;
  90. friend class wxDataViewColumn;
  91. public:
  92. wxDataViewCtrl() : wxScrollHelper(this)
  93. {
  94. Init();
  95. }
  96. wxDataViewCtrl( wxWindow *parent, wxWindowID id,
  97. const wxPoint& pos = wxDefaultPosition,
  98. const wxSize& size = wxDefaultSize, long style = 0,
  99. const wxValidator& validator = wxDefaultValidator,
  100. const wxString& name = wxDataViewCtrlNameStr )
  101. : wxScrollHelper(this)
  102. {
  103. Create(parent, id, pos, size, style, validator, name);
  104. }
  105. virtual ~wxDataViewCtrl();
  106. void Init();
  107. bool Create(wxWindow *parent, wxWindowID id,
  108. const wxPoint& pos = wxDefaultPosition,
  109. const wxSize& size = wxDefaultSize, long style = 0,
  110. const wxValidator& validator = wxDefaultValidator,
  111. const wxString& name = wxDataViewCtrlNameStr);
  112. virtual bool AssociateModel( wxDataViewModel *model );
  113. virtual bool AppendColumn( wxDataViewColumn *col );
  114. virtual bool PrependColumn( wxDataViewColumn *col );
  115. virtual bool InsertColumn( unsigned int pos, wxDataViewColumn *col );
  116. virtual void DoSetExpanderColumn();
  117. virtual void DoSetIndent();
  118. virtual unsigned int GetColumnCount() const;
  119. virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
  120. virtual bool DeleteColumn( wxDataViewColumn *column );
  121. virtual bool ClearColumns();
  122. virtual int GetColumnPosition( const wxDataViewColumn *column ) const;
  123. virtual wxDataViewColumn *GetSortingColumn() const;
  124. virtual int GetSelectedItemsCount() const;
  125. virtual int GetSelections( wxDataViewItemArray & sel ) const;
  126. virtual void SetSelections( const wxDataViewItemArray & sel );
  127. virtual void Select( const wxDataViewItem & item );
  128. virtual void Unselect( const wxDataViewItem & item );
  129. virtual bool IsSelected( const wxDataViewItem & item ) const;
  130. virtual void SelectAll();
  131. virtual void UnselectAll();
  132. virtual void EnsureVisible( const wxDataViewItem & item,
  133. const wxDataViewColumn *column = NULL );
  134. virtual void HitTest( const wxPoint & point, wxDataViewItem & item,
  135. wxDataViewColumn* &column ) const;
  136. virtual wxRect GetItemRect( const wxDataViewItem & item,
  137. const wxDataViewColumn *column = NULL ) const;
  138. virtual bool SetRowHeight( int rowHeight );
  139. virtual void Expand( const wxDataViewItem & item );
  140. virtual void Collapse( const wxDataViewItem & item );
  141. virtual bool IsExpanded( const wxDataViewItem & item ) const;
  142. virtual void SetFocus();
  143. virtual bool SetFont(const wxFont & font);
  144. #if wxUSE_DRAG_AND_DROP
  145. virtual bool EnableDragSource( const wxDataFormat &format );
  146. virtual bool EnableDropTarget( const wxDataFormat &format );
  147. #endif // wxUSE_DRAG_AND_DROP
  148. virtual wxBorder GetDefaultBorder() const;
  149. virtual void EditItem(const wxDataViewItem& item, const wxDataViewColumn *column);
  150. // These methods are specific to generic wxDataViewCtrl implementation and
  151. // should not be used in portable code.
  152. wxColour GetAlternateRowColour() const { return m_alternateRowColour; }
  153. void SetAlternateRowColour(const wxColour& colour);
  154. protected:
  155. virtual void EnsureVisible( int row, int column );
  156. // Notice that row here may be invalid (i.e. >= GetRowCount()), this is not
  157. // an error and this function simply returns an invalid item in this case.
  158. virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
  159. virtual int GetRowByItem( const wxDataViewItem & item ) const;
  160. int GetSortingColumnIndex() const { return m_sortingColumnIdx; }
  161. void SetSortingColumnIndex(int idx) { m_sortingColumnIdx = idx; }
  162. public: // utility functions not part of the API
  163. // returns the "best" width for the idx-th column
  164. unsigned int GetBestColumnWidth(int idx) const;
  165. // called by header window after reorder
  166. void ColumnMoved( wxDataViewColumn* col, unsigned int new_pos );
  167. // update the display after a change to an individual column
  168. void OnColumnChange(unsigned int idx);
  169. // update after a change to the number of columns
  170. void OnColumnsCountChanged();
  171. wxWindow *GetMainWindow() { return (wxWindow*) m_clientArea; }
  172. // return the index of the given column in m_cols
  173. int GetColumnIndex(const wxDataViewColumn *column) const;
  174. // Return the index of the column having the given model index.
  175. int GetModelColumnIndex(unsigned int model_column) const;
  176. // return the column displayed at the given position in the control
  177. wxDataViewColumn *GetColumnAt(unsigned int pos) const;
  178. virtual wxDataViewColumn *GetCurrentColumn() const;
  179. virtual void OnInternalIdle();
  180. private:
  181. virtual wxDataViewItem DoGetCurrentItem() const;
  182. virtual void DoSetCurrentItem(const wxDataViewItem& item);
  183. void InvalidateColBestWidths();
  184. void InvalidateColBestWidth(int idx);
  185. void UpdateColWidths();
  186. wxDataViewColumnList m_cols;
  187. // cached column best widths information, values are for
  188. // respective columns from m_cols and the arrays have same size
  189. struct CachedColWidthInfo
  190. {
  191. CachedColWidthInfo() : width(0), dirty(true) {}
  192. int width; // cached width or 0 if not computed
  193. bool dirty; // column was invalidated, header needs updating
  194. };
  195. wxVector<CachedColWidthInfo> m_colsBestWidths;
  196. // This indicates that at least one entry in m_colsBestWidths has 'dirty'
  197. // flag set. It's cheaper to check one flag in OnInternalIdle() than to
  198. // iterate over m_colsBestWidths to check if anything needs to be done.
  199. bool m_colsDirty;
  200. wxDataViewModelNotifier *m_notifier;
  201. wxDataViewMainWindow *m_clientArea;
  202. wxDataViewHeaderWindow *m_headerArea;
  203. // user defined color to draw row lines, may be invalid
  204. wxColour m_alternateRowColour;
  205. // the index of the column currently used for sorting or -1
  206. int m_sortingColumnIdx;
  207. private:
  208. void OnSize( wxSizeEvent &event );
  209. virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
  210. // we need to return a special WM_GETDLGCODE value to process just the
  211. // arrows but let the other navigation characters through
  212. #ifdef __WXMSW__
  213. virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
  214. #endif // __WXMSW__
  215. WX_FORWARD_TO_SCROLL_HELPER()
  216. private:
  217. DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
  218. wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl);
  219. DECLARE_EVENT_TABLE()
  220. };
  221. #endif // __GENERICDATAVIEWCTRLH__