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.
4
5 #include "ui/views/controls/table/table_utils.h"
6
7 #include "base/logging.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/font.h"
10 #include "ui/views/controls/table/table_view.h"
11
12 namespace views {
13
14 const int kUnspecifiedColumnWidth = 90;
15
WidthForContent(const gfx::Font & header_font,const gfx::Font & content_font,int padding,int header_padding,const ui::TableColumn & column,ui::TableModel * model)16 int WidthForContent(const gfx::Font& header_font,
17 const gfx::Font& content_font,
18 int padding,
19 int header_padding,
20 const ui::TableColumn& column,
21 ui::TableModel* model) {
22 int width = header_padding;
23 if (!column.title.empty())
24 width = header_font.GetStringWidth(column.title) + header_padding;
25
26 for (int i = 0, row_count = model->RowCount(); i < row_count; ++i) {
27 const int cell_width =
28 content_font.GetStringWidth(model->GetText(i, column.id));
29 width = std::max(width, cell_width);
30 }
31 return width + padding;
32 }
33
CalculateTableColumnSizes(int width,int first_column_padding,const gfx::Font & header_font,const gfx::Font & content_font,int padding,int header_padding,const std::vector<ui::TableColumn> & columns,ui::TableModel * model)34 std::vector<int> CalculateTableColumnSizes(
35 int width,
36 int first_column_padding,
37 const gfx::Font& header_font,
38 const gfx::Font& content_font,
39 int padding,
40 int header_padding,
41 const std::vector<ui::TableColumn>& columns,
42 ui::TableModel* model) {
43 float total_percent = 0;
44 int non_percent_width = 0;
45 std::vector<int> content_widths(columns.size(), 0);
46 for (size_t i = 0; i < columns.size(); ++i) {
47 const ui::TableColumn& column(columns[i]);
48 if (column.width <= 0) {
49 if (column.percent > 0) {
50 total_percent += column.percent;
51 // Make sure there is at least enough room for the header.
52 content_widths[i] = header_font.GetStringWidth(column.title) + padding +
53 header_padding;
54 } else {
55 content_widths[i] = WidthForContent(header_font, content_font, padding,
56 header_padding, column, model);
57 if (i == 0)
58 content_widths[i] += first_column_padding;
59 }
60 non_percent_width += content_widths[i];
61 } else {
62 content_widths[i] = column.width;
63 non_percent_width += column.width;
64 }
65 }
66
67 std::vector<int> widths;
68 const int available_width = width - non_percent_width;
69 for (size_t i = 0; i < columns.size(); ++i) {
70 const ui::TableColumn& column = columns[i];
71 int column_width = content_widths[i];
72 if (column.width <= 0 && column.percent > 0 && available_width > 0) {
73 column_width += static_cast<int>(available_width *
74 (column.percent / total_percent));
75 }
76 widths.push_back(column_width == 0 ? kUnspecifiedColumnWidth :
77 column_width);
78 }
79
80 // If no columns have specified a percent give the last column all the extra
81 // space.
82 if (!columns.empty() && total_percent == 0.f && available_width > 0 &&
83 columns.back().width <= 0 && columns.back().percent == 0.f) {
84 widths.back() += available_width;
85 }
86
87 return widths;
88 }
89
TableColumnAlignmentToCanvasAlignment(ui::TableColumn::Alignment alignment)90 int TableColumnAlignmentToCanvasAlignment(
91 ui::TableColumn::Alignment alignment) {
92 switch (alignment) {
93 case ui::TableColumn::LEFT:
94 return gfx::Canvas::TEXT_ALIGN_LEFT;
95 case ui::TableColumn::CENTER:
96 return gfx::Canvas::TEXT_ALIGN_CENTER;
97 case ui::TableColumn::RIGHT:
98 return gfx::Canvas::TEXT_ALIGN_RIGHT;
99 }
100 NOTREACHED();
101 return gfx::Canvas::TEXT_ALIGN_LEFT;
102 }
103
GetClosestVisibleColumnIndex(const TableView * table,int x)104 int GetClosestVisibleColumnIndex(const TableView* table, int x) {
105 const std::vector<TableView::VisibleColumn>& columns(
106 table->visible_columns());
107 for (size_t i = 0; i < columns.size(); ++i) {
108 if (x <= columns[i].x + columns[i].width)
109 return static_cast<int>(i);
110 }
111 return static_cast<int>(columns.size()) - 1;
112 }
113
114 } // namespace views
115