table_view.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// Use of this source code is governed by a BSD-style license that can be
3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// found in the LICENSE file.
4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
5b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang#ifndef UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_
6b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#define UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_
7b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
8b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include <vector>
9b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
10b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "base/memory/scoped_ptr.h"
11b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/base/models/list_selection_model.h"
12b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/base/models/table_model.h"
13b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/base/models/table_model_observer.h"
14b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/gfx/font_list.h"
15b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/views/view.h"
16b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato#include "ui/views/views_export.h"
17b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
18b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// A TableView is a view that displays multiple rows with any number of columns.
19b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// TableView is driven by a TableModel. The model returns the contents
20b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// to display. TableModel also has an Observer which is used to notify
21b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// TableView of changes to the model so that the display may be updated
22b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// appropriately.
23b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato//
24b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// TableView itself has an observer that is notified when the selection
25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// changes.
26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato//
27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// When a table is sorted the model coordinates do not necessarily match the
28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// view coordinates. All table methods are in terms of the model. If you need to
29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// convert to view coordinates use ModelToView().
30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato//
31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// Sorting is done by a locale sensitive string sort. You can customize the
32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// sort by way of overriding TableModel::CompareValues().
33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratonamespace views {
34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratostruct GroupRange;
36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass TableGrouper;
37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass TableHeader;
38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass TableViewObserver;
39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass TableViewRowBackgroundPainter;
40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass TableViewTestHelper;
41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// The cells in the first column of a table can contain:
43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// - only text
44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// - a small icon (16x16) and some text
45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato// - a check box and some text
46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoenum TableTypes {
47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  TEXT_ONLY = 0,
48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  ICON_AND_TEXT,
49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato};
50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoclass VIEWS_EXPORT TableView
52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    : public views::View,
53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato      public ui::TableModelObserver {
54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public:
55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Used to track a visible column. Useful only for the header.
56b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  struct VIEWS_EXPORT VisibleColumn {
57b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    VisibleColumn();
58b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    ~VisibleColumn();
59b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // The column.
61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    ui::TableColumn column;
62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Starting x-coordinate of the column.
64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int x;
65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Width of the column.
67b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    int width;
68b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  };
69b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
70b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  // Describes a sorted column.
71b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  struct VIEWS_EXPORT SortDescriptor {
72b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    SortDescriptor() : column_id(-1), ascending(true) {}
73b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    SortDescriptor(int column_id, bool ascending)
74b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        : column_id(column_id),
75b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang          ascending(ascending) {}
76b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // ID of the sorted column.
78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int column_id;
79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Is the sort ascending?
81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    bool ascending;
82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  };
83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
84b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  typedef std::vector<SortDescriptor> SortDescriptors;
85b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
86b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  // Creates a new table using the model and columns specified.
87b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang  // The table type applies to the content of the first column (text, icon and
88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // text, checkbox and text).
89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  TableView(ui::TableModel* model,
90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            const std::vector<ui::TableColumn>& columns,
91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            TableTypes table_type,
92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            bool single_selection);
93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual ~TableView();
94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Assigns a new model to the table view, detaching the old one if present.
96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // If |model| is NULL, the table view cannot be used after this call. This
97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // should be called in the containing view's destructor to avoid destruction
98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // issues when the model needs to be deleted before the table.
99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetModel(ui::TableModel* model);
100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  ui::TableModel* model() const { return model_; }
101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
102b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns a new ScrollView that contains the receiver.
103b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  View* CreateParentIfNecessary();
104b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
105b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetRowBackgroundPainter(
106b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato      scoped_ptr<TableViewRowBackgroundPainter> painter);
107b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
108b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Sets the TableGrouper. TableView does not own |grouper| (common use case is
109b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // to have TableModel implement TableGrouper).
110b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetGrouper(TableGrouper* grouper);
111b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
112b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the number of rows in the TableView.
113b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int RowCount() const;
114b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
115b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the number of selected rows.
116b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // TODO(sky): remove this and force callers to use selection_model().
117b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int SelectedRowCount();
118b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
119b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Selects the specified item, making sure it's visible.
120b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void Select(int model_row);
121b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
122b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the first selected row in terms of the model.
123b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int FirstSelectedRow();
124b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
125b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  const ui::ListSelectionModel& selection_model() const {
126b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    return selection_model_;
127b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  }
128b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
129b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Changes the visibility of the specified column (by id).
130b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetColumnVisibility(int id, bool is_visible);
131b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  bool IsColumnVisible(int id) const;
132b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
133b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Adds the specified column. |col| is not made visible.
134b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void AddColumn(const ui::TableColumn& col);
135b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
136b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns true if the column with the specified id is known (either visible
137b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // or not).
138b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  bool HasColumn(int id) const;
139b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
140b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // TODO(sky): rename to set_observer().
141b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetObserver(TableViewObserver* observer) {
142b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    table_view_observer_ = observer;
143b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  }
144b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  TableViewObserver* observer() const { return table_view_observer_; }
145b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
146b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  const std::vector<VisibleColumn>& visible_columns() const {
147b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    return visible_columns_;
148b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  }
149b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
150b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Sets the width of the column. |index| is in terms of |visible_columns_|.
151b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetVisibleColumnWidth(int index, int width);
152b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
153b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Toggles the sort order of the specified visible column index.
154b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void ToggleSortOrder(int visible_column_index);
155b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
156b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  const SortDescriptors& sort_descriptors() const { return sort_descriptors_; }
157b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  bool is_sorted() const { return !sort_descriptors_.empty(); }
158b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
159b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Maps from the index in terms of the model to that of the view.
160b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int ModelToView(int model_index) const;
161b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
162b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Maps from the index in terms of the view to that of the model.
163b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int ViewToModel(int view_index) const;
164b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
165b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int row_height() const { return row_height_; }
166b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
167b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // View overrides:
168b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void Layout() OVERRIDE;
169b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual gfx::Size GetPreferredSize() const OVERRIDE;
170b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
171b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
172b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
173b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual bool GetTooltipText(const gfx::Point& p,
174b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                              base::string16* tooltip) const OVERRIDE;
175b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual bool GetTooltipTextOrigin(const gfx::Point& p,
176b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                    gfx::Point* loc) const OVERRIDE;
177b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
178b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // ui::TableModelObserver overrides:
179b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnModelChanged() OVERRIDE;
180b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnItemsChanged(int start, int length) OVERRIDE;
181b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnItemsAdded(int start, int length) OVERRIDE;
182b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnItemsRemoved(int start, int length) OVERRIDE;
183b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
184b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato protected:
185b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // View overrides:
186b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual gfx::Point GetKeyboardContextMenuLocation() OVERRIDE;
187b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
188b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnFocus() OVERRIDE;
189b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  virtual void OnBlur() OVERRIDE;
190b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
191b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private:
192b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  friend class TableViewTestHelper;
193b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  struct GroupSortHelper;
194b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  struct SortHelper;
195b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
196b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Used during painting to determine the range of cells that need to be
197b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // painted.
198b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // NOTE: the row indices returned by this are in terms of the view and column
199b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // indices in terms of |visible_columns_|.
200b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  struct VIEWS_EXPORT PaintRegion {
201b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    PaintRegion();
202b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    ~PaintRegion();
203b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
204b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int min_row;
205b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int max_row;
206b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int min_column;
207b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    int max_column;
208b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  };
209b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
210b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Used by AdvanceSelection() to determine the direction to change the
211b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // selection.
212b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  enum AdvanceDirection {
213b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    ADVANCE_DECREMENT,
214b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    ADVANCE_INCREMENT,
215b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  };
216b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
217b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Invoked when the number of rows changes in some way.
218b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void NumRowsChanged();
219b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
220b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Resets the sort descriptions.
221b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SetSortDescriptors(const SortDescriptors& sort_descriptors);
222b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
223b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Does the actual sort and updates the mappings (|view_to_model_| and
224b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // |model_to_view_|) appropriately.
225b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void SortItemsAndUpdateMapping();
226b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
227b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Used to sort the two rows. Returns a value < 0, == 0 or > 0 indicating
228b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // whether the row2 comes before row1, row2 is the same as row1 or row1 comes
229b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // after row2. This invokes CompareValues on the model with the sorted column.
230b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  int CompareRows(int model_row1, int model_row2);
231b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
232b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the bounds of the specified row.
233b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  gfx::Rect GetRowBounds(int row) const;
234b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
235b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the bounds of the specified cell. |visible_column_index| indexes
236b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // into |visible_columns_|.
237b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  gfx::Rect GetCellBounds(int row, int visible_column_index) const;
238b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
239b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Adjusts |bounds| based on where the text should be painted. |bounds| comes
240b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // from GetCellBounds() and |visible_column_index| is the corresponding column
241b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // (in terms of |visible_columns_|).
242b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void AdjustCellBoundsForText(int visible_column_index,
243b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                               gfx::Rect* bounds) const;
244b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
245b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Creates |header_| if necessary.
246b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void CreateHeaderIfNecessary();
247b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
248b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Updates the |x| and |width| of each of the columns in |visible_columns_|.
249b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  void UpdateVisibleColumnSizes();
250b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
251b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the cells that need to be painted for the specified region.
252b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // |bounds| is in terms of |this|.
253b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  PaintRegion GetPaintRegion(const gfx::Rect& bounds) const;
254b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
255b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // Returns the bounds that need to be painted based on the clip set on
256b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato  // |canvas|.
257  gfx::Rect GetPaintBounds(gfx::Canvas* canvas) const;
258
259  // Invokes SchedulePaint() for the selected rows.
260  void SchedulePaintForSelection();
261
262  // Returns the TableColumn matching the specified id.
263  ui::TableColumn FindColumnByID(int id) const;
264
265  // Sets the selection to the specified index (in terms of the view).
266  void SelectByViewIndex(int view_index);
267
268  // Sets the selection model to |new_selection|.
269  void SetSelectionModel(const ui::ListSelectionModel& new_selection);
270
271  // Advances the selection (from the active index) in the specified direction.
272  void AdvanceSelection(AdvanceDirection direction);
273
274  // Sets |model| appropriately based on a event.
275  void ConfigureSelectionModelForEvent(const ui::LocatedEvent& event,
276                                       ui::ListSelectionModel* model) const;
277
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,
282                             bool select,
283                             ui::ListSelectionModel* model) const;
284
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
287  // 1.
288  GroupRange GetGroupRange(int model_index) const;
289
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
292  // NULL.
293  bool GetTooltipImpl(const gfx::Point& location,
294                      base::string16* tooltip,
295                      gfx::Point* tooltip_origin) const;
296
297  ui::TableModel* model_;
298
299  std::vector<ui::TableColumn> columns_;
300
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_;
304
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_;
308
309  const TableTypes table_type_;
310
311  const bool single_selection_;
312
313  // TODO(sky): rename to observer_.
314  TableViewObserver* table_view_observer_;
315
316  // The selection, in terms of the model.
317  ui::ListSelectionModel selection_model_;
318
319  gfx::FontList font_list_;
320
321  int row_height_;
322
323  // Width of the ScrollView last time Layout() was invoked. Used to determine
324  // when we should invoke UpdateVisibleColumnSizes().
325  int last_parent_width_;
326
327  // The width we layout to. This may differ from |last_parent_width_|.
328  int layout_width_;
329
330  // Current sort.
331  SortDescriptors sort_descriptors_;
332
333  // Mappings used when sorted.
334  std::vector<int> view_to_model_;
335  std::vector<int> model_to_view_;
336
337  scoped_ptr<TableViewRowBackgroundPainter> row_background_painter_;
338
339  TableGrouper* grouper_;
340
341  // True if in SetVisibleColumnWidth().
342  bool in_set_visible_column_width_;
343
344  DISALLOW_COPY_AND_ASSIGN(TableView);
345};
346
347}  // namespace views
348
349#endif  // UI_VIEWS_CONTROLS_TABLE_TABLE_VIEW_VIEWS_H_
350