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