Commit 9a171441 authored by sky@chromium.org's avatar sky@chromium.org

Couple of tweaks for tables:

. Expose TableHeader::kHorizontalPadding so that it can be used in
  calculating preferred width of a column.
. Make TableHeader::OnMousePressed return false if not the left mouse
  button. This lets context menus on an ancestor work.
. Exposes couple of methods on TableView used by TaskManager.
. Fixes sizes in TableView.
. Turns on grouping in table example.

BUG=none
TEST=none
R=sadrul@chromium.org


Review URL: https://chromiumcodereview.appspot.com/11827007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175912 0039d316-1c4b-4281-b951-d872f2087c98
parent 241d8a19
......@@ -17,7 +17,6 @@ namespace views {
namespace {
const int kVerticalPadding = 6;
const int kHorizontalPadding = 4;
// The minimum width we allow a column to go down to.
const int kMinColumnWidth = 10;
......@@ -38,6 +37,9 @@ gfx::NativeCursor GetResizeCursor() {
} // namespace
// static
const int TableHeader::kHorizontalPadding = 4;
typedef std::vector<TableView::VisibleColumn> Columns;
TableHeader::TableHeader(TableView* table) : table_(table) {
......@@ -90,8 +92,10 @@ bool TableHeader::OnMousePressed(const ui::MouseEvent& event) {
resize_details_->initial_width =
table_->visible_columns()[index].width;
}
return true;
}
return true;
// Return false so that context menus on ancestors work.
return false;
}
bool TableHeader::OnMouseDragged(const ui::MouseEvent& event) {
......
......@@ -16,6 +16,9 @@ class TableView;
// Views used to render the header for the table.
class VIEWS_EXPORT TableHeader : public views::View {
public:
// Amount the text is padded on the left/right side.
static const int kHorizontalPadding;
explicit TableHeader(TableView* table);
virtual ~TableHeader();
......
......@@ -238,6 +238,19 @@ bool TableView::IsColumnVisible(int id) const {
return false;
}
void TableView::AddColumn(const ui::TableColumn& col) {
DCHECK(!HasColumn(col.id));
columns_.push_back(col);
}
bool TableView::HasColumn(int id) const {
for (size_t i = 0; i < columns_.size(); ++i) {
if (columns_[i].id == id)
return true;
}
return false;
}
void TableView::SetVisibleColumnWidth(int index, int width) {
DCHECK(index >= 0 && index < static_cast<int>(visible_columns_.size()));
if (visible_columns_[index].width == width)
......@@ -273,9 +286,12 @@ void TableView::Layout() {
// parent()->parent() is the scrollview. When its width changes we force
// recalculating column sizes.
View* scroll_view = parent() ? parent()->parent() : NULL;
if (scroll_view && scroll_view->width() != last_parent_width_) {
last_parent_width_ = scroll_view->width();
UpdateVisibleColumnSizes();
if (scroll_view) {
const int scroll_view_width = scroll_view->GetContentsBounds().width();
if (scroll_view_width != last_parent_width_) {
last_parent_width_ = scroll_view_width;
UpdateVisibleColumnSizes();
}
}
// We have to override Layout like this since we're contained in a ScrollView.
gfx::Size pref = GetPreferredSize();
......@@ -442,7 +458,8 @@ void TableView::OnPaint(gfx::Canvas* canvas) {
canvas->DrawStringInt(
model_->GetText(model_index, visible_columns_[j].column.id), font_,
kTextColor,
GetMirroredXWithWidthInView(text_x, cell_bounds.right() - text_x),
GetMirroredXWithWidthInView(text_x, cell_bounds.right() - text_x -
kTextHorizontalPadding),
cell_bounds.y() + kTextVerticalPadding,
cell_bounds.right() - text_x,
cell_bounds.height() - kTextVerticalPadding * 2,
......@@ -536,10 +553,10 @@ void TableView::UpdateVisibleColumnSizes() {
std::vector<ui::TableColumn> columns;
for (size_t i = 0; i < visible_columns_.size(); ++i)
columns.push_back(visible_columns_[i].column);
std::vector<int> sizes =
views::CalculateTableColumnSizes(last_parent_width_, header_->font(),
font_, 0, // TODO: fix this
columns, model_);
std::vector<int> sizes = views::CalculateTableColumnSizes(
last_parent_width_, header_->font(), font_,
std::max(kTextHorizontalPadding, TableHeader::kHorizontalPadding) * 2,
columns, model_);
DCHECK_EQ(visible_columns_.size(), sizes.size());
int x = 0;
for (size_t i = 0; i < visible_columns_.size(); ++i) {
......
......@@ -143,6 +143,13 @@ class VIEWS_EXPORT TableView
void SetColumnVisibility(int id, bool is_visible);
bool IsColumnVisible(int id) const;
// Adds the specified column. |col| is not made visible.
void AddColumn(const ui::TableColumn& col);
// Returns true if the column with the specified id is known (either visible
// or not).
bool HasColumn(int id) const;
// TODO(sky): rename to set_observer().
void SetObserver(TableViewObserver* observer) {
table_view_observer_ = observer;
......
......@@ -12,6 +12,7 @@
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/table/group_table_model.h"
#include "ui/views/layout/grid_layout.h"
namespace views {
......@@ -67,6 +68,10 @@ void TableExample::CreateExampleView(View* container) {
columns.push_back(TestTableColumn(3, "Price"));
columns.back().alignment = ui::TableColumn::RIGHT;
table_ = new TableView(this, columns, ICON_AND_TEXT, true, true, true);
// TODO(sky): remove ifdef once we get rid of win32 table.
#if defined(USE_AURA)
table_->SetGrouper(this);
#endif
table_->SetObserver(this);
icon1_.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
icon1_.allocPixels();
......@@ -123,6 +128,19 @@ gfx::ImageSkia TableExample::GetIcon(int row) {
void TableExample::SetObserver(ui::TableModelObserver* observer) {}
void TableExample::GetGroupRange(int model_index, GroupRange* range) {
if (model_index < 2) {
range->start = 0;
range->length = 2;
} else if (model_index > 6) {
range->start = 7;
range->length = 3;
} else {
range->start = model_index;
range->length = 1;
}
}
void TableExample::OnSelectionChanged() {
PrintStatus("Selected: %s",
UTF16ToASCII(GetText(table_->FirstSelectedRow(), 0)).c_str());
......
......@@ -12,6 +12,7 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/models/table_model.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/table/table_grouper.h"
#include "ui/views/controls/table/table_view.h"
#include "ui/views/controls/table/table_view_observer.h"
#include "ui/views/examples/example_base.h"
......@@ -27,10 +28,12 @@ class TableView;
namespace examples {
class TableExample : public ExampleBase,
public ui::TableModel,
public TableViewObserver,
public ButtonListener {
class TableExample
: public ExampleBase,
public ui::TableModel,
public TableGrouper,
public TableViewObserver,
public ButtonListener {
public:
TableExample();
virtual ~TableExample();
......@@ -44,6 +47,9 @@ class TableExample : public ExampleBase,
virtual gfx::ImageSkia GetIcon(int row) OVERRIDE;
virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE;
// TableGrouper:
virtual void GetGroupRange(int model_index, GroupRange* range) OVERRIDE;
// TableViewObserver:
virtual void OnSelectionChanged() OVERRIDE;
virtual void OnDoubleClick() OVERRIDE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment