File: table_utils.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download | duplicates (6)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2012 The Chromium Authors
// 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 <stddef.h>

#include <algorithm>

#include "base/i18n/rtl.h"
#include "base/notreached.h"
#include "ui/base/models/table_model.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/controls/table/table_view.h"

namespace views {

const int kUnspecifiedColumnWidth = 90;

int WidthForContent(const gfx::FontList& header_font_list,
                    const gfx::FontList& content_font_list,
                    int padding,
                    int header_padding,
                    const ui::TableColumn& column,
                    ui::TableModel* model) {
  int width = header_padding;
  if (!column.title.empty()) {
    width =
        gfx::GetStringWidth(column.title, header_font_list) + header_padding;
  }

  for (size_t i = 0, row_count = model->RowCount(); i < row_count; ++i) {
    const int cell_width =
        gfx::GetStringWidth(model->GetText(i, column.id), content_font_list);
    width = std::max(width, cell_width);
  }
  return width + padding;
}

std::vector<int> CalculateTableColumnSizes(
    int width,
    int first_column_padding,
    const gfx::FontList& header_font_list,
    const gfx::FontList& content_font_list,
    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] =
            gfx::GetStringWidth(column.title, header_font_list) + padding +
            header_padding;
      } else {
        content_widths[i] =
            WidthForContent(header_font_list, content_font_list, 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();
}

std::optional<size_t> GetClosestVisibleColumnIndex(const TableView& table,
                                                   int x) {
  const std::vector<TableView::VisibleColumn>& columns(table.visible_columns());
  if (columns.empty()) {
    return std::nullopt;
  }
  for (size_t i = 0; i < columns.size(); ++i) {
    if (x <= columns[i].x + columns[i].width) {
      return i;
    }
  }
  return columns.size() - 1;
}

ui::TableColumn::Alignment GetMirroredTableColumnAlignment(
    ui::TableColumn::Alignment alignment) {
  if (!base::i18n::IsRTL()) {
    return alignment;
  }

  switch (alignment) {
    case ui::TableColumn::LEFT:
      return ui::TableColumn::RIGHT;
    case ui::TableColumn::RIGHT:
      return ui::TableColumn::LEFT;
    case ui::TableColumn::CENTER:
      return ui::TableColumn::CENTER;
  }
}

}  // namespace views