| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #ifndef VIEWS_CONTROLS_TABLE_TABLE_VIEW2_H_ | |
| 6 #define VIEWS_CONTROLS_TABLE_TABLE_VIEW2_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "third_party/skia/include/core/SkColor.h" | |
| 15 #include "ui/base/models/table_model_observer.h" | |
| 16 #include "ui/gfx/canvas.h" | |
| 17 #include "views/controls/table/native_table_wrapper.h" | |
| 18 #include "views/controls/table/table_view.h" | |
| 19 #include "views/view.h" | |
| 20 | |
| 21 namespace ui { | |
| 22 struct TableColumn; | |
| 23 class TableModel; | |
| 24 } | |
| 25 | |
| 26 // A TableView2 is a view that displays multiple rows with any number of | |
| 27 // columns. | |
| 28 // TableView is driven by a TableModel. The model returns the contents | |
| 29 // to display. TableModel also has an Observer which is used to notify | |
| 30 // TableView of changes to the model so that the display may be updated | |
| 31 // appropriately. | |
| 32 // | |
| 33 // TableView2 itself has an observer that is notified when the selection | |
| 34 // changes. | |
| 35 // | |
| 36 // TableView2 is the current port of TableView to use a NativeControl for | |
| 37 // portability. | |
| 38 // | |
| 39 // TODO(jcampan): add sorting. | |
| 40 // TODO(jcampan): add group support. | |
| 41 | |
| 42 namespace views { | |
| 43 | |
| 44 class ListView; | |
| 45 class ListViewParent; | |
| 46 class TableView; | |
| 47 class TableViewObserver; | |
| 48 class View; | |
| 49 | |
| 50 class VIEWS_EXPORT TableView2 : public View, public ui::TableModelObserver { | |
| 51 public: | |
| 52 typedef TableSelectionIterator iterator; | |
| 53 | |
| 54 // A helper struct for GetCellColors. Set |color_is_set| to true if color is | |
| 55 // set. See OnCustomDraw for more details on why we need this. | |
| 56 struct ItemColor { | |
| 57 bool color_is_set; | |
| 58 SkColor color; | |
| 59 }; | |
| 60 | |
| 61 // Bitmasks of options for creating an instance of the table view. See | |
| 62 // comments next to the corresponding members in TableView2 for details | |
| 63 // (ex. SINGLE_SELECTION -> single_selection_). | |
| 64 enum Options { | |
| 65 NONE = 0, | |
| 66 SINGLE_SELECTION = 1 << 0, | |
| 67 RESIZABLE_COLUMNS = 1 << 1, | |
| 68 AUTOSIZE_COLUMNS = 1 << 2, | |
| 69 HORIZONTAL_LINES = 1 << 3, | |
| 70 VERTICAL_LINES = 1 << 4, | |
| 71 }; | |
| 72 | |
| 73 // Creates a new table using the model and columns specified. | |
| 74 // The table type applies to the content of the first column (text, icon and | |
| 75 // text, checkbox and text). | |
| 76 // When autosize_columns is true, columns always fill the available width. If | |
| 77 // false, columns are not resized when the table is resized. An extra empty | |
| 78 // column at the right fills the remaining space. | |
| 79 // When resizable_columns is true, users can resize columns by dragging the | |
| 80 // separator on the column header. NOTE: Right now this is always true. The | |
| 81 // code to set it false is still in place to be a base for future, better | |
| 82 // resizing behavior (see http://b/issue?id=874646 ), but no one uses or | |
| 83 // tests the case where this flag is false. | |
| 84 // Note that setting both resizable_columns and autosize_columns to false is | |
| 85 // probably not a good idea, as there is no way for the user to increase a | |
| 86 // column's size in that case. | |
| 87 // |options| is a bitmask of options. See comments at Options. | |
| 88 TableView2(ui::TableModel* model, const std::vector<ui::TableColumn>& columns, | |
| 89 TableTypes table_type, int options); | |
| 90 virtual ~TableView2(); | |
| 91 | |
| 92 // Assigns a new model to the table view, detaching the old one if present. | |
| 93 // If |model| is NULL, the table view cannot be used after this call. This | |
| 94 // should be called in the containing view's destructor to avoid destruction | |
| 95 // issues when the model needs to be deleted before the table. | |
| 96 void SetModel(ui::TableModel* model); | |
| 97 ui::TableModel* model() const { return model_; } | |
| 98 | |
| 99 // Returns the number of rows in the table. | |
| 100 int GetRowCount(); | |
| 101 | |
| 102 // Returns the number of selected rows. | |
| 103 int SelectedRowCount(); | |
| 104 | |
| 105 // Makes all row not selected. | |
| 106 void ClearSelection(); | |
| 107 | |
| 108 // Makes all row not focused. | |
| 109 void ClearRowFocus(); | |
| 110 | |
| 111 // Returns the index of the first selected row. | |
| 112 int GetFirstSelectedRow(); | |
| 113 | |
| 114 // Returns the index of the first focused row. | |
| 115 int GetFirstFocusedRow(); | |
| 116 | |
| 117 // Selects the specified row, making sure it's visible. | |
| 118 void SelectRow(int model_row); | |
| 119 | |
| 120 // Sets the focus to the row at the given index. | |
| 121 void FocusRow(int model_row); | |
| 122 | |
| 123 // Returns true if the row at the specified index is selected. | |
| 124 bool IsRowSelected(int model_row); | |
| 125 | |
| 126 // Returns true if the row at the specified index has the focus. | |
| 127 bool IsRowFocused(int model_row); | |
| 128 | |
| 129 // Returns an iterator over the selection. The iterator proceeds from the | |
| 130 // last index to the first. | |
| 131 // | |
| 132 // NOTE: the iterator iterates over the visual order (but returns coordinates | |
| 133 // in terms of the model). | |
| 134 iterator SelectionBegin(); | |
| 135 iterator SelectionEnd(); | |
| 136 | |
| 137 // ui::TableModelObserver methods. | |
| 138 virtual void OnModelChanged(); | |
| 139 virtual void OnItemsChanged(int start, int length); | |
| 140 virtual void OnItemsAdded(int start, int length); | |
| 141 virtual void OnItemsRemoved(int start, int length); | |
| 142 | |
| 143 void SetObserver(TableViewObserver* observer) { | |
| 144 table_view_observer_ = observer; | |
| 145 } | |
| 146 TableViewObserver* observer() const { return table_view_observer_; } | |
| 147 | |
| 148 // Replaces the set of known columns without changing the current visible | |
| 149 // columns. | |
| 150 void SetColumns(const std::vector<ui::TableColumn>& columns); | |
| 151 void AddColumn(const ui::TableColumn& col); | |
| 152 bool HasColumn(int id); | |
| 153 | |
| 154 // Sets which columns (by id) are displayed. All transient size and position | |
| 155 // information is lost. | |
| 156 void SetVisibleColumns(const std::vector<int>& columns); | |
| 157 void SetColumnVisibility(int id, bool is_visible); | |
| 158 bool IsColumnVisible(int id) const; | |
| 159 | |
| 160 ui::TableColumn GetVisibleColumnAt(int index); | |
| 161 size_t GetVisibleColumnCount(); | |
| 162 | |
| 163 // Resets the size of the columns based on the sizes passed to the | |
| 164 // constructor. Your normally needn't invoked this, it's done for you the | |
| 165 // first time the TableView is given a valid size. | |
| 166 void ResetColumnSizes(); | |
| 167 | |
| 168 bool single_selection() const { | |
| 169 return single_selection_; | |
| 170 } | |
| 171 | |
| 172 TableTypes type() const { | |
| 173 return table_type_; | |
| 174 } | |
| 175 | |
| 176 bool resizable_columns() const { | |
| 177 return resizable_columns_; | |
| 178 } | |
| 179 | |
| 180 bool autosize_columns() const { | |
| 181 return autosize_columns_; | |
| 182 } | |
| 183 | |
| 184 bool horizontal_lines() const { | |
| 185 return horizontal_lines_; | |
| 186 } | |
| 187 | |
| 188 bool vertical_lines() const { | |
| 189 return vertical_lines_; | |
| 190 } | |
| 191 | |
| 192 virtual void Layout(); | |
| 193 | |
| 194 virtual void OnPaintFocusBorder(gfx::Canvas* canvas); | |
| 195 | |
| 196 // Used by tests. | |
| 197 virtual gfx::NativeView GetTestingHandle(); | |
| 198 | |
| 199 protected: | |
| 200 virtual void ViewHierarchyChanged(bool is_add, | |
| 201 View* parent, | |
| 202 View* child) OVERRIDE; | |
| 203 | |
| 204 private: | |
| 205 friend class ListViewParent; | |
| 206 friend class TableSelectionIterator; | |
| 207 | |
| 208 // Used in the constructors. | |
| 209 void Init(const std::vector<ui::TableColumn>& columns); | |
| 210 | |
| 211 // We need this wrapper to pass the table view to the windows proc handler | |
| 212 // when subclassing the list view and list view header, as the reinterpret | |
| 213 // cast from GetWindowLongPtr would break the pointer if it is pointing to a | |
| 214 // subclass (in the OO sense of TableView). | |
| 215 struct TableViewWrapper { | |
| 216 explicit TableViewWrapper(TableView2* view) : table_view(view) {} | |
| 217 TableView2* table_view; | |
| 218 }; | |
| 219 | |
| 220 // Adds a new column. | |
| 221 void InsertColumn(const ui::TableColumn& tc, int index); | |
| 222 | |
| 223 // Update headers and internal state after columns have changed | |
| 224 void OnColumnsChanged(); | |
| 225 | |
| 226 ui::TableModel* model_; | |
| 227 TableTypes table_type_; | |
| 228 TableViewObserver* table_view_observer_; | |
| 229 | |
| 230 // An ordered list of id's into all_columns_ representing current visible | |
| 231 // columns. | |
| 232 std::vector<int> visible_columns_; | |
| 233 | |
| 234 // Mapping of an int id to a TableColumn representing all possible columns. | |
| 235 std::map<int, ui::TableColumn> all_columns_; | |
| 236 | |
| 237 // Cached value of columns_.size() | |
| 238 int column_count_; | |
| 239 | |
| 240 // Selection mode. | |
| 241 bool single_selection_; | |
| 242 | |
| 243 // Whether or not the user can resize columns. | |
| 244 bool resizable_columns_; | |
| 245 | |
| 246 // Whether or not columns should automatically be resized to fill the | |
| 247 // the available width when the list view is resized. | |
| 248 bool autosize_columns_; | |
| 249 | |
| 250 // Whether or not horizontal grid lines should be drawn. | |
| 251 bool horizontal_lines_; | |
| 252 | |
| 253 // Whether or not vertical grid lines should be drawn. | |
| 254 bool vertical_lines_; | |
| 255 | |
| 256 // Mappings used when sorted. | |
| 257 // scoped_array<int> view_to_model_; | |
| 258 // scoped_array<int> model_to_view_; | |
| 259 | |
| 260 // The object that actually implements the table. | |
| 261 NativeTableWrapper* native_wrapper_; | |
| 262 | |
| 263 DISALLOW_COPY_AND_ASSIGN(TableView2); | |
| 264 }; | |
| 265 | |
| 266 } // namespace views | |
| 267 | |
| 268 #endif // VIEWS_CONTROLS_TABLE_TABLE_VIEW2_H_ | |
| OLD | NEW |