1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_
6 #define UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/base/models/list_selection_model.h"
12 #include "ui/base/models/table_model.h"
13 #include "ui/base/models/table_model_observer.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/views/view.h"
16 #include "ui/views/views_export.h"
18 // A TableView is a view that displays multiple rows with any number of columns.
19 // TableView is driven by a TableModel. The model returns the contents
20 // to display. TableModel also has an Observer which is used to notify
21 // TableView of changes to the model so that the display may be updated
24 // TableView itself has an observer that is notified when the selection
27 // When a table is sorted the model coordinates do not necessarily match the
28 // view coordinates. All table methods are in terms of the model. If you need to
29 // convert to view coordinates use ModelToView().
31 // Sorting is done by a locale sensitive string sort. You can customize the
32 // sort by way of overriding TableModel::CompareValues().
38 class TableViewObserver
;
39 class TableViewRowBackgroundPainter
;
40 class TableViewTestHelper
;
42 // The cells in the first column of a table can contain:
44 // - a small icon (16x16) and some text
45 // - a check box and some text
51 class VIEWS_EXPORT TableView
53 public ui::TableModelObserver
{
55 // Used to track a visible column. Useful only for the header.
56 struct VIEWS_EXPORT VisibleColumn
{
61 ui::TableColumn column
;
63 // Starting x-coordinate of the column.
66 // Width of the column.
70 // Describes a sorted column.
71 struct VIEWS_EXPORT SortDescriptor
{
72 SortDescriptor() : column_id(-1), ascending(true) {}
73 SortDescriptor(int column_id
, bool ascending
)
74 : column_id(column_id
),
75 ascending(ascending
) {}
77 // ID of the sorted column.
80 // Is the sort ascending?
84 typedef std::vector
<SortDescriptor
> SortDescriptors
;
86 // Creates a new table using the model and columns specified.
87 // The table type applies to the content of the first column (text, icon and
88 // text, checkbox and text).
89 TableView(ui::TableModel
* model
,
90 const std::vector
<ui::TableColumn
>& columns
,
91 TableTypes table_type
,
92 bool single_selection
);
95 // Assigns a new model to the table view, detaching the old one if present.
96 // If |model| is NULL, the table view cannot be used after this call. This
97 // should be called in the containing view's destructor to avoid destruction
98 // issues when the model needs to be deleted before the table.
99 void SetModel(ui::TableModel
* model
);
100 ui::TableModel
* model() const { return model_
; }
102 // Returns a new ScrollView that contains the receiver.
103 View
* CreateParentIfNecessary();
105 void SetRowBackgroundPainter(
106 scoped_ptr
<TableViewRowBackgroundPainter
> painter
);
108 // Sets the TableGrouper. TableView does not own |grouper| (common use case is
109 // to have TableModel implement TableGrouper).
110 void SetGrouper(TableGrouper
* grouper
);
112 // Returns the number of rows in the TableView.
113 int RowCount() const;
115 // Returns the number of selected rows.
116 // TODO(sky): remove this and force callers to use selection_model().
117 int SelectedRowCount();
119 // Selects the specified item, making sure it's visible.
120 void Select(int model_row
);
122 // Returns the first selected row in terms of the model.
123 int FirstSelectedRow();
125 const ui::ListSelectionModel
& selection_model() const {
126 return selection_model_
;
129 // Changes the visibility of the specified column (by id).
130 void SetColumnVisibility(int id
, bool is_visible
);
131 bool IsColumnVisible(int id
) const;
133 // Adds the specified column. |col| is not made visible.
134 void AddColumn(const ui::TableColumn
& col
);
136 // Returns true if the column with the specified id is known (either visible
138 bool HasColumn(int id
) const;
140 // TODO(sky): rename to set_observer().
141 void SetObserver(TableViewObserver
* observer
) {
142 table_view_observer_
= observer
;
144 TableViewObserver
* observer() const { return table_view_observer_
; }
146 const std::vector
<VisibleColumn
>& visible_columns() const {
147 return visible_columns_
;
150 // Sets the width of the column. |index| is in terms of |visible_columns_|.
151 void SetVisibleColumnWidth(int index
, int width
);
153 // Toggles the sort order of the specified visible column index.
154 void ToggleSortOrder(int visible_column_index
);
156 const SortDescriptors
& sort_descriptors() const { return sort_descriptors_
; }
157 bool is_sorted() const { return !sort_descriptors_
.empty(); }
159 // Maps from the index in terms of the model to that of the view.
160 int ModelToView(int model_index
) const;
162 // Maps from the index in terms of the view to that of the model.
163 int ViewToModel(int view_index
) const;
165 int row_height() const { return row_height_
; }
168 virtual void Layout() OVERRIDE
;
169 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
170 virtual bool OnKeyPressed(const ui::KeyEvent
& event
) OVERRIDE
;
171 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
172 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
173 virtual bool GetTooltipText(const gfx::Point
& p
,
174 base::string16
* tooltip
) const OVERRIDE
;
175 virtual bool GetTooltipTextOrigin(const gfx::Point
& p
,
176 gfx::Point
* loc
) const OVERRIDE
;
178 // ui::TableModelObserver overrides:
179 virtual void OnModelChanged() OVERRIDE
;
180 virtual void OnItemsChanged(int start
, int length
) OVERRIDE
;
181 virtual void OnItemsAdded(int start
, int length
) OVERRIDE
;
182 virtual void OnItemsRemoved(int start
, int length
) OVERRIDE
;
186 virtual gfx::Point
GetKeyboardContextMenuLocation() OVERRIDE
;
187 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
188 virtual void OnFocus() OVERRIDE
;
189 virtual void OnBlur() OVERRIDE
;
192 friend class TableViewTestHelper
;
193 struct GroupSortHelper
;
196 // Used during painting to determine the range of cells that need to be
198 // NOTE: the row indices returned by this are in terms of the view and column
199 // indices in terms of |visible_columns_|.
200 struct VIEWS_EXPORT PaintRegion
{
210 // Used by AdvanceSelection() to determine the direction to change the
212 enum AdvanceDirection
{
217 // Invoked when the number of rows changes in some way.
218 void NumRowsChanged();
220 // Resets the sort descriptions.
221 void SetSortDescriptors(const SortDescriptors
& sort_descriptors
);
223 // Does the actual sort and updates the mappings (|view_to_model_| and
224 // |model_to_view_|) appropriately.
225 void SortItemsAndUpdateMapping();
227 // Used to sort the two rows. Returns a value < 0, == 0 or > 0 indicating
228 // whether the row2 comes before row1, row2 is the same as row1 or row1 comes
229 // after row2. This invokes CompareValues on the model with the sorted column.
230 int CompareRows(int model_row1
, int model_row2
);
232 // Returns the bounds of the specified row.
233 gfx::Rect
GetRowBounds(int row
) const;
235 // Returns the bounds of the specified cell. |visible_column_index| indexes
236 // into |visible_columns_|.
237 gfx::Rect
GetCellBounds(int row
, int visible_column_index
) const;
239 // Adjusts |bounds| based on where the text should be painted. |bounds| comes
240 // from GetCellBounds() and |visible_column_index| is the corresponding column
241 // (in terms of |visible_columns_|).
242 void AdjustCellBoundsForText(int visible_column_index
,
243 gfx::Rect
* bounds
) const;
245 // Creates |header_| if necessary.
246 void CreateHeaderIfNecessary();
248 // Updates the |x| and |width| of each of the columns in |visible_columns_|.
249 void UpdateVisibleColumnSizes();
251 // Returns the cells that need to be painted for the specified region.
252 // |bounds| is in terms of |this|.
253 PaintRegion
GetPaintRegion(const gfx::Rect
& bounds
) const;
255 // Returns the bounds that need to be painted based on the clip set on
257 gfx::Rect
GetPaintBounds(gfx::Canvas
* canvas
) const;
259 // Invokes SchedulePaint() for the selected rows.
260 void SchedulePaintForSelection();
262 // Returns the TableColumn matching the specified id.
263 ui::TableColumn
FindColumnByID(int id
) const;
265 // Sets the selection to the specified index (in terms of the view).
266 void SelectByViewIndex(int view_index
);
268 // Sets the selection model to |new_selection|.
269 void SetSelectionModel(const ui::ListSelectionModel
& new_selection
);
271 // Advances the selection (from the active index) in the specified direction.
272 void AdvanceSelection(AdvanceDirection direction
);
274 // Sets |model| appropriately based on a event.
275 void ConfigureSelectionModelForEvent(const ui::LocatedEvent
& event
,
276 ui::ListSelectionModel
* model
) const;
278 // Set the selection state of row at |view_index| to |select|, additionally
279 // any other rows in the GroupRange containing |view_index| are updated as
280 // well. This does not change the anchor or active index of |model|.
281 void SelectRowsInRangeFrom(int view_index
,
283 ui::ListSelectionModel
* model
) const;
285 // Returns the range of the specified model index. If a TableGrouper has not
286 // been set this returns a group with a start of |model_index| and length of
288 GroupRange
GetGroupRange(int model_index
) const;
290 // Used by both GetTooltipText methods. Returns true if there is a tooltip and
291 // sets |tooltip| and/or |tooltip_origin| as appropriate, each of which may be
293 bool GetTooltipImpl(const gfx::Point
& location
,
294 base::string16
* tooltip
,
295 gfx::Point
* tooltip_origin
) const;
297 ui::TableModel
* model_
;
299 std::vector
<ui::TableColumn
> columns_
;
301 // The set of visible columns. The values of these point to |columns_|. This
302 // may contain a subset of |columns_|.
303 std::vector
<VisibleColumn
> visible_columns_
;
305 // The header. This is only created if more than one column is specified or
306 // the first column has a non-empty title.
307 TableHeader
* header_
;
309 const TableTypes table_type_
;
311 const bool single_selection_
;
313 // TODO(sky): rename to observer_.
314 TableViewObserver
* table_view_observer_
;
316 // The selection, in terms of the model.
317 ui::ListSelectionModel selection_model_
;
319 gfx::FontList font_list_
;
323 // Width of the ScrollView last time Layout() was invoked. Used to determine
324 // when we should invoke UpdateVisibleColumnSizes().
325 int last_parent_width_
;
327 // The width we layout to. This may differ from |last_parent_width_|.
331 SortDescriptors sort_descriptors_
;
333 // Mappings used when sorted.
334 std::vector
<int> view_to_model_
;
335 std::vector
<int> model_to_view_
;
337 scoped_ptr
<TableViewRowBackgroundPainter
> row_background_painter_
;
339 TableGrouper
* grouper_
;
341 // True if in SetVisibleColumnWidth().
342 bool in_set_visible_column_width_
;
344 DISALLOW_COPY_AND_ASSIGN(TableView
);
349 #endif // UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_