Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: views/controls/table/table_view2.cc

Issue 8655001: views: Move table and tree directories to ui/views/controls/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: exclude native_widget_win_unittest too Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/controls/table/table_view2.h ('k') | views/controls/table/table_view_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "views/controls/table/table_view2.h"
6
7 #include "base/logging.h"
8 #include "ui/base/models/table_model.h"
9 #include "views/controls/native/native_view_host.h"
10 #include "views/controls/table/table_view_observer.h"
11
12 namespace views {
13
14 TableView2::TableView2(ui::TableModel* model,
15 const std::vector<ui::TableColumn>& columns,
16 TableTypes table_type,
17 int options)
18 : model_(model),
19 table_type_(table_type),
20 table_view_observer_(NULL),
21 visible_columns_(),
22 all_columns_(),
23 column_count_(static_cast<int>(columns.size())),
24 single_selection_((options & SINGLE_SELECTION) != 0),
25 resizable_columns_((options & RESIZABLE_COLUMNS) != 0),
26 autosize_columns_((options & AUTOSIZE_COLUMNS) != 0),
27 horizontal_lines_((options & HORIZONTAL_LINES) != 0),
28 vertical_lines_((options & VERTICAL_LINES) != 0),
29 native_wrapper_(NULL) {
30 Init(columns);
31 }
32
33 TableView2::~TableView2() {
34 if (model_)
35 model_->SetObserver(NULL);
36 }
37
38 void TableView2::SetModel(ui::TableModel* model) {
39 if (model == model_)
40 return;
41
42 if (model_)
43 model_->SetObserver(NULL);
44 model_ = model;
45 if (model_)
46 model_->SetObserver(this);
47 if (native_wrapper_)
48 OnModelChanged();
49 }
50
51 int TableView2::GetRowCount() {
52 if (!native_wrapper_)
53 return 0;
54 return native_wrapper_->GetRowCount();
55 }
56
57 int TableView2::SelectedRowCount() {
58 if (!native_wrapper_)
59 return 0;
60 return native_wrapper_->GetSelectedRowCount();
61 }
62
63 void TableView2::ClearSelection() {
64 if (native_wrapper_)
65 native_wrapper_->ClearSelection();
66 }
67
68 void TableView2::ClearRowFocus() {
69 if (native_wrapper_)
70 native_wrapper_->ClearRowFocus();
71 }
72
73 int TableView2::GetFirstSelectedRow() {
74 if (!native_wrapper_)
75 return -1;
76 return native_wrapper_->GetFirstSelectedRow();
77 }
78
79 int TableView2::GetFirstFocusedRow() {
80 if (!native_wrapper_)
81 return -1;
82 return native_wrapper_->GetFirstFocusedRow();
83 }
84
85 void TableView2::SelectRow(int model_row) {
86 if (!native_wrapper_)
87 return;
88
89 native_wrapper_->ClearSelection();
90 native_wrapper_->SetSelectedState(model_row, true);
91 if (table_view_observer_)
92 table_view_observer_->OnSelectionChanged();
93 }
94
95 void TableView2::FocusRow(int model_row) {
96 if (!native_wrapper_)
97 return;
98
99 native_wrapper_->SetFocusState(model_row, true);
100 }
101
102 bool TableView2::IsRowSelected(int model_row) {
103 if (!native_wrapper_)
104 return false;
105
106 return native_wrapper_->IsRowSelected(model_row);
107 }
108
109 bool TableView2::IsRowFocused(int model_row) {
110 if (!native_wrapper_)
111 return false;
112
113 return native_wrapper_->IsRowFocused(model_row);
114 }
115
116 void TableView2::OnModelChanged() {
117 if (!native_wrapper_)
118 return;
119
120 int current_row_count = native_wrapper_->GetRowCount();
121 if (current_row_count > 0)
122 OnItemsRemoved(0, current_row_count);
123 if (model_ && model_->RowCount())
124 OnItemsAdded(0, model_->RowCount());
125 }
126
127 void TableView2::OnItemsChanged(int start, int length) {
128 if (!native_wrapper_)
129 return;
130
131 if (length == -1) {
132 DCHECK_GE(start, 0);
133 length = model_->RowCount() - start;
134 }
135 native_wrapper_->OnRowsChanged(start, length);
136 }
137
138 void TableView2::OnItemsAdded(int start, int length) {
139 if (!native_wrapper_)
140 return;
141
142 DCHECK(start >= 0 && length >= 0 && start <= native_wrapper_->GetRowCount());
143
144 native_wrapper_->OnRowsAdded(start, length);
145 }
146
147 void TableView2::OnItemsRemoved(int start, int length) {
148 if (!native_wrapper_)
149 return;
150
151 DCHECK(start >= 0 && length >= 0 &&
152 start + length <= native_wrapper_->GetRowCount());
153
154 native_wrapper_->OnRowsRemoved(start, length);
155 }
156
157 void TableView2::AddColumn(const ui::TableColumn& col) {
158 DCHECK_EQ(0U, all_columns_.count(col.id));
159 all_columns_[col.id] = col;
160 }
161
162 void TableView2::SetColumns(const std::vector<ui::TableColumn>& columns) {
163 // Remove the currently visible columns.
164 while (!visible_columns_.empty())
165 SetColumnVisibility(visible_columns_.front(), false);
166
167 all_columns_.clear();
168 for (std::vector<ui::TableColumn>::const_iterator i = columns.begin();
169 i != columns.end(); ++i) {
170 AddColumn(*i);
171 }
172 }
173
174 void TableView2::OnColumnsChanged() {
175 column_count_ = static_cast<int>(visible_columns_.size());
176 ResetColumnSizes();
177 }
178
179 bool TableView2::HasColumn(int id) {
180 return all_columns_.count(id) > 0;
181 }
182
183 void TableView2::SetColumnVisibility(int id, bool is_visible) {
184 bool changed = false;
185 for (std::vector<int>::iterator i = visible_columns_.begin();
186 i != visible_columns_.end(); ++i) {
187 if (*i == id) {
188 if (is_visible) {
189 // It's already visible, bail out early.
190 return;
191 } else {
192 int index = static_cast<int>(i - visible_columns_.begin());
193 // This could be called before the native list view has been created
194 // (in CreateNativeControl, called when the view is added to a
195 // Widget). In that case since the column is not in
196 // visible_columns_ it will not be added later on when it is created.
197 if (native_wrapper_)
198 native_wrapper_->RemoveColumn(index);
199 visible_columns_.erase(i);
200 changed = true;
201 break;
202 }
203 }
204 }
205 if (is_visible) {
206 DCHECK(native_wrapper_);
207 visible_columns_.push_back(id);
208 ui::TableColumn& column = all_columns_[id];
209 native_wrapper_->InsertColumn(column, column_count_);
210 changed = true;
211 }
212 if (changed)
213 OnColumnsChanged();
214
215 }
216
217 bool TableView2::IsColumnVisible(int id) const {
218 for (std::vector<int>::const_iterator i = visible_columns_.begin();
219 i != visible_columns_.end(); ++i)
220 if (*i == id) {
221 return true;
222 }
223 return false;
224 }
225
226 void TableView2::ResetColumnSizes() {
227 if (!native_wrapper_)
228 return;
229
230 // See comment in TableColumn for what this does.
231 int width = this->width();
232 gfx::Rect native_bounds = native_wrapper_->GetBounds();
233 if (!native_bounds.IsEmpty()) {
234 if (native_bounds.width() > 0) {
235 // Prefer the bounds of the window over our bounds, which may be
236 // different.
237 width = native_bounds.width();
238 }
239 }
240
241 float percent = 0;
242 int fixed_width = 0;
243 int autosize_width = 0;
244
245 for (std::vector<int>::const_iterator i = visible_columns_.begin();
246 i != visible_columns_.end(); ++i) {
247 ui::TableColumn& col = all_columns_[*i];
248 int col_index = static_cast<int>(i - visible_columns_.begin());
249 if (col.width == -1) {
250 if (col.percent > 0) {
251 percent += col.percent;
252 } else {
253 autosize_width += col.min_visible_width;
254 }
255 } else {
256 fixed_width += native_wrapper_->GetColumnWidth(col_index);
257 }
258 }
259
260 // Now do a pass to set the actual sizes of auto-sized and
261 // percent-sized columns.
262 int available_width = width - fixed_width - autosize_width;
263 for (std::vector<int>::const_iterator i = visible_columns_.begin();
264 i != visible_columns_.end(); ++i) {
265 ui::TableColumn& col = all_columns_[*i];
266 if (col.width == -1) {
267 int col_index = static_cast<int>(i - visible_columns_.begin());
268 if (col.percent > 0) {
269 if (available_width > 0) {
270 int col_width =
271 static_cast<int>(available_width * (col.percent / percent));
272 available_width -= col_width;
273 percent -= col.percent;
274 native_wrapper_->SetColumnWidth(col_index, col_width);
275 }
276 } else {
277 int col_width = col.min_visible_width;
278 // If no "percent" columns, the last column acts as one, if auto-sized.
279 if (percent == 0.f && available_width > 0 &&
280 col_index == column_count_ - 1) {
281 col_width += available_width;
282 }
283 native_wrapper_->SetColumnWidth(col_index, col_width);
284 }
285 }
286 }
287 }
288
289 void TableView2::Layout() {
290 if (native_wrapper_) {
291 native_wrapper_->GetView()->SetBounds(0, 0, width(), height());
292 native_wrapper_->GetView()->Layout();
293 }
294 }
295
296 void TableView2::OnPaintFocusBorder(gfx::Canvas* canvas) {
297 if (NativeViewHost::kRenderNativeControlFocus)
298 View::OnPaintFocusBorder(canvas);
299 }
300
301 size_t TableView2::GetVisibleColumnCount() {
302 return visible_columns_.size();
303 }
304
305 void TableView2::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
306 if (is_add && !native_wrapper_ && GetWidget()) {
307 // The native wrapper's lifetime will be managed by the view hierarchy after
308 // we call AddChildView.
309 native_wrapper_ = NativeTableWrapper::CreateNativeWrapper(this);
310 AddChildView(native_wrapper_->GetView());
311 }
312 }
313
314 void TableView2::Init(const std::vector<ui::TableColumn>& columns) {
315 for (std::vector<ui::TableColumn>::const_iterator i = columns.begin();
316 i != columns.end(); ++i) {
317 AddColumn(*i);
318 visible_columns_.push_back(i->id);
319 }
320 }
321
322 gfx::NativeView TableView2::GetTestingHandle() {
323 return native_wrapper_->GetTestingHandle();
324 }
325
326 ui::TableColumn TableView2::GetVisibleColumnAt(int index) {
327 DCHECK(index < static_cast<int>(visible_columns_.size()));
328 std::map<int, ui::TableColumn>::iterator iter =
329 all_columns_.find(index);
330 DCHECK(iter != all_columns_.end());
331 return iter->second;
332 }
333
334 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/table/table_view2.h ('k') | views/controls/table/table_view_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698