OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ | 5 #ifndef VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ |
6 #define VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ | 6 #define VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | |
10 | |
11 #if defined(OS_WIN) | |
12 #include <windows.h> | |
13 typedef struct tagNMLVCUSTOMDRAW NMLVCUSTOMDRAW; | |
14 #endif // defined(OS_WIN) | |
15 | |
16 #include <map> | 9 #include <map> |
17 #include <vector> | 10 #include <vector> |
18 | 11 |
19 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "build/build_config.h" |
20 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
21 #include "ui/base/keycodes/keyboard_codes.h" | 15 #include "ui/base/keycodes/keyboard_codes.h" |
22 #include "ui/base/models/table_model_observer.h" | 16 #include "ui/base/models/table_model_observer.h" |
23 #include "views/views_export.h" | 17 #include "views/views_export.h" |
24 | 18 |
25 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 #include <windows.h> |
| 21 |
26 // TODO(port): remove the ifdef when native_control.h is ported. | 22 // TODO(port): remove the ifdef when native_control.h is ported. |
27 #include "views/controls/native_control.h" | 23 #include "views/controls/native_control.h" |
| 24 |
| 25 typedef struct tagNMLVCUSTOMDRAW NMLVCUSTOMDRAW; |
28 #endif // defined(OS_WIN) | 26 #endif // defined(OS_WIN) |
29 | 27 |
30 class SkBitmap; | 28 class SkBitmap; |
31 | 29 |
| 30 namespace gfx { |
| 31 class Font; |
| 32 } |
| 33 |
32 namespace ui { | 34 namespace ui { |
33 struct TableColumn; | 35 struct TableColumn; |
34 class TableModel; | 36 class TableModel; |
35 } | 37 } |
36 using ui::TableColumn; | |
37 using ui::TableModel; | |
38 using ui::TableModelObserver; // TODO(beng): remove these. | |
39 | |
40 namespace gfx { | |
41 class Font; | |
42 } | |
43 | 38 |
44 // A TableView is a view that displays multiple rows with any number of columns. | 39 // A TableView is a view that displays multiple rows with any number of columns. |
45 // TableView is driven by a TableModel. The model returns the contents | 40 // TableView is driven by a TableModel. The model returns the contents |
46 // to display. TableModel also has an Observer which is used to notify | 41 // to display. TableModel also has an Observer which is used to notify |
47 // TableView of changes to the model so that the display may be updated | 42 // TableView of changes to the model so that the display may be updated |
48 // appropriately. | 43 // appropriately. |
49 // | 44 // |
50 // TableView itself has an observer that is notified when the selection | 45 // TableView itself has an observer that is notified when the selection |
51 // changes. | 46 // changes. |
52 // | 47 // |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 int view_index_; | 92 int view_index_; |
98 | 93 |
99 // The index in terms of the model. This is returned from the * operator. This | 94 // The index in terms of the model. This is returned from the * operator. This |
100 // is cached to avoid dependencies on the view_to_model mapping. | 95 // is cached to avoid dependencies on the view_to_model mapping. |
101 int model_index_; | 96 int model_index_; |
102 }; | 97 }; |
103 | 98 |
104 #if defined(OS_WIN) | 99 #if defined(OS_WIN) |
105 // TODO(port): Port TableView. | 100 // TODO(port): Port TableView. |
106 class VIEWS_EXPORT TableView : public NativeControl, | 101 class VIEWS_EXPORT TableView : public NativeControl, |
107 public TableModelObserver { | 102 public ui::TableModelObserver { |
108 public: | 103 public: |
109 typedef TableSelectionIterator iterator; | 104 typedef TableSelectionIterator iterator; |
110 | 105 |
111 // A helper struct for GetCellColors. Set |color_is_set| to true if color is | 106 // A helper struct for GetCellColors. Set |color_is_set| to true if color is |
112 // set. See OnCustomDraw for more details on why we need this. | 107 // set. See OnCustomDraw for more details on why we need this. |
113 struct ItemColor { | 108 struct ItemColor { |
114 bool color_is_set; | 109 bool color_is_set; |
115 SkColor color; | 110 SkColor color; |
116 }; | 111 }; |
117 | 112 |
(...skipping 20 matching lines...) Expand all Loading... |
138 // false, columns are not resized when the table is resized. An extra empty | 133 // false, columns are not resized when the table is resized. An extra empty |
139 // column at the right fills the remaining space. | 134 // column at the right fills the remaining space. |
140 // When resizable_columns is true, users can resize columns by dragging the | 135 // When resizable_columns is true, users can resize columns by dragging the |
141 // separator on the column header. NOTE: Right now this is always true. The | 136 // separator on the column header. NOTE: Right now this is always true. The |
142 // code to set it false is still in place to be a base for future, better | 137 // code to set it false is still in place to be a base for future, better |
143 // resizing behavior (see http://b/issue?id=874646 ), but no one uses or | 138 // resizing behavior (see http://b/issue?id=874646 ), but no one uses or |
144 // tests the case where this flag is false. | 139 // tests the case where this flag is false. |
145 // Note that setting both resizable_columns and autosize_columns to false is | 140 // Note that setting both resizable_columns and autosize_columns to false is |
146 // probably not a good idea, as there is no way for the user to increase a | 141 // probably not a good idea, as there is no way for the user to increase a |
147 // column's size in that case. | 142 // column's size in that case. |
148 TableView(TableModel* model, const std::vector<TableColumn>& columns, | 143 TableView(ui::TableModel* model, const std::vector<ui::TableColumn>& columns, |
149 TableTypes table_type, bool single_selection, | 144 TableTypes table_type, bool single_selection, |
150 bool resizable_columns, bool autosize_columns); | 145 bool resizable_columns, bool autosize_columns); |
151 virtual ~TableView(); | 146 virtual ~TableView(); |
152 | 147 |
153 // Assigns a new model to the table view, detaching the old one if present. | 148 // Assigns a new model to the table view, detaching the old one if present. |
154 // If |model| is NULL, the table view cannot be used after this call. This | 149 // If |model| is NULL, the table view cannot be used after this call. This |
155 // should be called in the containing view's destructor to avoid destruction | 150 // should be called in the containing view's destructor to avoid destruction |
156 // issues when the model needs to be deleted before the table. | 151 // issues when the model needs to be deleted before the table. |
157 void SetModel(TableModel* model); | 152 void SetModel(ui::TableModel* model); |
158 TableModel* model() const { return model_; } | 153 ui::TableModel* model() const { return model_; } |
159 | 154 |
160 // Resorts the contents. | 155 // Resorts the contents. |
161 void SetSortDescriptors(const SortDescriptors& sort_descriptors); | 156 void SetSortDescriptors(const SortDescriptors& sort_descriptors); |
162 | 157 |
163 // Current sort. | 158 // Current sort. |
164 const SortDescriptors& sort_descriptors() const { return sort_descriptors_; } | 159 const SortDescriptors& sort_descriptors() const { return sort_descriptors_; } |
165 | 160 |
166 // Returns the number of rows in the TableView. | 161 // Returns the number of rows in the TableView. |
167 int RowCount() const; | 162 int RowCount() const; |
168 | 163 |
(...skipping 21 matching lines...) Expand all Loading... |
190 bool ItemHasTheFocus(int model_row); | 185 bool ItemHasTheFocus(int model_row); |
191 | 186 |
192 // Returns an iterator over the selection. The iterator proceeds from the | 187 // Returns an iterator over the selection. The iterator proceeds from the |
193 // last index to the first. | 188 // last index to the first. |
194 // | 189 // |
195 // NOTE: the iterator iterates over the visual order (but returns coordinates | 190 // NOTE: the iterator iterates over the visual order (but returns coordinates |
196 // in terms of the model). | 191 // in terms of the model). |
197 iterator SelectionBegin(); | 192 iterator SelectionBegin(); |
198 iterator SelectionEnd(); | 193 iterator SelectionEnd(); |
199 | 194 |
200 // TableModelObserver methods. | 195 // ui::TableModelObserver methods. |
201 virtual void OnModelChanged(); | 196 virtual void OnModelChanged(); |
202 virtual void OnItemsChanged(int start, int length); | 197 virtual void OnItemsChanged(int start, int length); |
203 virtual void OnItemsAdded(int start, int length); | 198 virtual void OnItemsAdded(int start, int length); |
204 virtual void OnItemsRemoved(int start, int length); | 199 virtual void OnItemsRemoved(int start, int length); |
205 | 200 |
206 void SetObserver(TableViewObserver* observer) { | 201 void SetObserver(TableViewObserver* observer) { |
207 table_view_observer_ = observer; | 202 table_view_observer_ = observer; |
208 } | 203 } |
209 TableViewObserver* observer() const { return table_view_observer_; } | 204 TableViewObserver* observer() const { return table_view_observer_; } |
210 | 205 |
211 // Replaces the set of known columns without changing the current visible | 206 // Replaces the set of known columns without changing the current visible |
212 // columns. | 207 // columns. |
213 void SetColumns(const std::vector<TableColumn>& columns); | 208 void SetColumns(const std::vector<ui::TableColumn>& columns); |
214 void AddColumn(const TableColumn& col); | 209 void AddColumn(const ui::TableColumn& col); |
215 bool HasColumn(int id); | 210 bool HasColumn(int id); |
216 | 211 |
217 // Sets which columns (by id) are displayed. All transient size and position | 212 // Sets which columns (by id) are displayed. All transient size and position |
218 // information is lost. | 213 // information is lost. |
219 void SetVisibleColumns(const std::vector<int>& columns); | 214 void SetVisibleColumns(const std::vector<int>& columns); |
220 void SetColumnVisibility(int id, bool is_visible); | 215 void SetColumnVisibility(int id, bool is_visible); |
221 bool IsColumnVisible(int id) const; | 216 bool IsColumnVisible(int id) const; |
222 | 217 |
223 // Resets the size of the columns based on the sizes passed to the | 218 // Resets the size of the columns based on the sizes passed to the |
224 // constructor. Your normally needn't invoked this, it's done for you the | 219 // constructor. Your normally needn't invoked this, it's done for you the |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 // Method invoked by ListView when sorting back to natural state. Returns | 362 // Method invoked by ListView when sorting back to natural state. Returns |
368 // model_index_1_p - model_index_2_p. | 363 // model_index_1_p - model_index_2_p. |
369 static int CALLBACK NaturalSortFunc(LPARAM model_index_1_p, | 364 static int CALLBACK NaturalSortFunc(LPARAM model_index_1_p, |
370 LPARAM model_index_2_p, | 365 LPARAM model_index_2_p, |
371 LPARAM table_view_param); | 366 LPARAM table_view_param); |
372 | 367 |
373 // Resets the sort image displayed for the specified column. | 368 // Resets the sort image displayed for the specified column. |
374 void ResetColumnSortImage(int column_id, SortDirection direction); | 369 void ResetColumnSortImage(int column_id, SortDirection direction); |
375 | 370 |
376 // Adds a new column. | 371 // Adds a new column. |
377 void InsertColumn(const TableColumn& tc, int index); | 372 void InsertColumn(const ui::TableColumn& tc, int index); |
378 | 373 |
379 // Update headers and internal state after columns have changed | 374 // Update headers and internal state after columns have changed |
380 void OnColumnsChanged(); | 375 void OnColumnsChanged(); |
381 | 376 |
382 // Updates the ListView with values from the model. See UpdateListViewCache0 | 377 // Updates the ListView with values from the model. See UpdateListViewCache0 |
383 // for a complete description. | 378 // for a complete description. |
384 // This turns off redrawing, and invokes UpdateListViewCache0 to do the | 379 // This turns off redrawing, and invokes UpdateListViewCache0 to do the |
385 // actual updating. | 380 // actual updating. |
386 void UpdateListViewCache(int start, int length, bool add); | 381 void UpdateListViewCache(int start, int length, bool add); |
387 | 382 |
388 // Updates ListView with values from the model. | 383 // Updates ListView with values from the model. |
389 // If add is true, this adds length items starting at index start. | 384 // If add is true, this adds length items starting at index start. |
390 // If add is not true, the items are not added, the but the values in the | 385 // If add is not true, the items are not added, the but the values in the |
391 // range start - [start + length] are updated from the model. | 386 // range start - [start + length] are updated from the model. |
392 void UpdateListViewCache0(int start, int length, bool add); | 387 void UpdateListViewCache0(int start, int length, bool add); |
393 | 388 |
394 // Returns the index of the selected item before |view_index|, or -1 if | 389 // Returns the index of the selected item before |view_index|, or -1 if |
395 // |view_index| is the first selected item. | 390 // |view_index| is the first selected item. |
396 // | 391 // |
397 // WARNING: this returns coordinates in terms of the view, NOT the model. | 392 // WARNING: this returns coordinates in terms of the view, NOT the model. |
398 int PreviousSelectedViewIndex(int view_index); | 393 int PreviousSelectedViewIndex(int view_index); |
399 | 394 |
400 // Returns the last selected view index in the table view, or -1 if the table | 395 // Returns the last selected view index in the table view, or -1 if the table |
401 // is empty, or nothing is selected. | 396 // is empty, or nothing is selected. |
402 // | 397 // |
403 // WARNING: this returns coordinates in terms of the view, NOT the model. | 398 // WARNING: this returns coordinates in terms of the view, NOT the model. |
404 int LastSelectedViewIndex(); | 399 int LastSelectedViewIndex(); |
405 | 400 |
406 // The TableColumn visible at position pos. | 401 // The TableColumn visible at position pos. |
407 const TableColumn& GetColumnAtPosition(int pos); | 402 const ui::TableColumn& GetColumnAtPosition(int pos); |
408 | 403 |
409 // Window procedure of the list view class. We subclass the list view to | 404 // Window procedure of the list view class. We subclass the list view to |
410 // ignore WM_ERASEBKGND, which gives smoother painting during resizing. | 405 // ignore WM_ERASEBKGND, which gives smoother painting during resizing. |
411 static LRESULT CALLBACK TableWndProc(HWND window, | 406 static LRESULT CALLBACK TableWndProc(HWND window, |
412 UINT message, | 407 UINT message, |
413 WPARAM w_param, | 408 WPARAM w_param, |
414 LPARAM l_param); | 409 LPARAM l_param); |
415 | 410 |
416 // Window procedure of the header class. We subclass the header of the table | 411 // Window procedure of the header class. We subclass the header of the table |
417 // to disable resizing of columns. | 412 // to disable resizing of columns. |
418 static LRESULT CALLBACK TableHeaderWndProc(HWND window, UINT message, | 413 static LRESULT CALLBACK TableHeaderWndProc(HWND window, UINT message, |
419 WPARAM w_param, LPARAM l_param); | 414 WPARAM w_param, LPARAM l_param); |
420 | 415 |
421 // Updates content_offset_ from the position of the header. | 416 // Updates content_offset_ from the position of the header. |
422 void UpdateContentOffset(); | 417 void UpdateContentOffset(); |
423 | 418 |
424 // Reloads the groups from the model if there is one and it has groups. | 419 // Reloads the groups from the model if there is one and it has groups. |
425 void UpdateGroups(); | 420 void UpdateGroups(); |
426 | 421 |
427 // Returns the bounds of the alt text. | 422 // Returns the bounds of the alt text. |
428 gfx::Rect GetAltTextBounds(); | 423 gfx::Rect GetAltTextBounds(); |
429 | 424 |
430 // Returns the font used for alt text. | 425 // Returns the font used for alt text. |
431 gfx::Font GetAltTextFont(); | 426 gfx::Font GetAltTextFont(); |
432 | 427 |
433 // Overriden in order to update the column sizes, which can only be sized | 428 // Overriden in order to update the column sizes, which can only be sized |
434 // accurately when the native control is available. | 429 // accurately when the native control is available. |
435 virtual void VisibilityChanged(View* starting_from, bool is_visible); | 430 virtual void VisibilityChanged(View* starting_from, bool is_visible); |
436 | 431 |
437 TableModel* model_; | 432 ui::TableModel* model_; |
438 TableTypes table_type_; | 433 TableTypes table_type_; |
439 TableViewObserver* table_view_observer_; | 434 TableViewObserver* table_view_observer_; |
440 | 435 |
441 // An ordered list of id's into all_columns_ representing current visible | 436 // An ordered list of id's into |all_columns_| representing current visible |
442 // columns. | 437 // columns. |
443 std::vector<int> visible_columns_; | 438 std::vector<int> visible_columns_; |
444 | 439 |
445 // Mapping of an int id to a TableColumn representing all possible columns. | 440 // Mapping of an int id to a TableColumn representing all possible columns. |
446 std::map<int, TableColumn> all_columns_; | 441 std::map<int, ui::TableColumn> all_columns_; |
447 | 442 |
448 // Cached value of columns_.size() | 443 // Cached value of columns_.size() |
449 int column_count_; | 444 int column_count_; |
450 | 445 |
451 // Selection mode. | 446 // Selection mode. |
452 bool single_selection_; | 447 bool single_selection_; |
453 | 448 |
454 // If true, any events that would normally be propagated to the observer | 449 // If true, any events that would normally be propagated to the observer |
455 // are ignored. For example, if this is true and the selection changes in | 450 // are ignored. For example, if this is true and the selection changes in |
456 // the listview, the observer is not notified. | 451 // the listview, the observer is not notified. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 | 495 |
501 string16 alt_text_; | 496 string16 alt_text_; |
502 | 497 |
503 DISALLOW_COPY_AND_ASSIGN(TableView); | 498 DISALLOW_COPY_AND_ASSIGN(TableView); |
504 }; | 499 }; |
505 #endif // defined(OS_WIN) | 500 #endif // defined(OS_WIN) |
506 | 501 |
507 } // namespace views | 502 } // namespace views |
508 | 503 |
509 #endif // VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ | 504 #endif // VIEWS_CONTROLS_TABLE_TABLE_VIEW_H_ |
OLD | NEW |