summaryrefslogtreecommitdiffstats
path: root/ui/views/rendering
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-01 22:14:38 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-01 22:14:38 +0000
commit2a3851ec96ce92e9d51ef2737bf8a7137dad49e3 (patch)
treea0abc20bc21cef3eda852c0b2556f2ffe2b527be /ui/views/rendering
parent4258ecebbc45339f390bf874726014eeff2400ed (diff)
downloadchromium_src-2a3851ec96ce92e9d51ef2737bf8a7137dad49e3.zip
chromium_src-2a3851ec96ce92e9d51ef2737bf8a7137dad49e3.tar.gz
chromium_src-2a3851ec96ce92e9d51ef2737bf8a7137dad49e3.tar.bz2
sFirst cut at an experiment with what a simplified View/Widget API would look like.
Of note: - Widget is a cross-platform class encapsulating widget-specific state - NativeWidget interface wraps different implementations of a native widget. Starting with HWND. - NativeWidget implementation should eventually be swappable at runtime. - Simpler Event construction directly from a NativeEvent (e.g. MSG struct) - Simpler View API with fewer, more clearly delineated overrides. Notes: - Window* are just empty files for now while I get View/Widget to work. BUG=none TEST=see unittests, in development Review URL: http://codereview.chromium.org/6286013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73353 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views/rendering')
-rw-r--r--ui/views/rendering/border.cc69
-rw-r--r--ui/views/rendering/border.h50
-rw-r--r--ui/views/rendering/border_unittest.cc63
3 files changed, 182 insertions, 0 deletions
diff --git a/ui/views/rendering/border.cc b/ui/views/rendering/border.cc
new file mode 100644
index 0000000..34493e6
--- /dev/null
+++ b/ui/views/rendering/border.cc
@@ -0,0 +1,69 @@
+// Copyright (c) 2011 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/rendering/border.h"
+
+#include "gfx/canvas.h"
+#include "ui/views/view.h"
+
+namespace ui {
+
+namespace internal {
+
+class SolidColorBorder : public Border {
+ public:
+ SolidColorBorder(int thickness, SkColor color) : color_(color) {
+ set_insets(gfx::Insets(thickness, thickness, thickness, thickness));
+ }
+ virtual ~SolidColorBorder() {
+ }
+
+ // Overridden from Border:
+ virtual void Paint(const View* view, gfx::Canvas* canvas) const {
+ canvas->FillRectInt(color_, 0, 0, view->width(), insets().top());
+ canvas->FillRectInt(color_, 0, 0, insets().left(), view->height());
+ canvas->FillRectInt(color_, 0, view->height() - insets().bottom(),
+ view->width(), insets().bottom());
+ canvas->FillRectInt(color_, view->width() - insets().right(), 0,
+ insets().right(), view->height());
+ }
+
+ private:
+ SkColor color_;
+
+ DISALLOW_COPY_AND_ASSIGN(SolidColorBorder);
+};
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Border, public:
+
+Border::~Border() {
+}
+
+// static
+Border* Border::CreateSolidBorder(int thickness, SkColor color) {
+ return new internal::SolidColorBorder(thickness, color);
+}
+
+// static
+Border* Border::CreateTransparentBorder(const gfx::Insets& insets) {
+ Border* b = new Border;
+ b->set_insets(insets);
+ return b;
+}
+
+void Border::Paint(const View* view, gfx::Canvas* canvas) const {
+ // Nothing to do.
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Border, private:
+
+Border::Border() {
+}
+
+} // namespace ui
+
diff --git a/ui/views/rendering/border.h b/ui/views/rendering/border.h
new file mode 100644
index 0000000..e1b196f
--- /dev/null
+++ b/ui/views/rendering/border.h
@@ -0,0 +1,50 @@
+// Copyright (c) 2011 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.
+
+#ifndef UI_VIEWS_BORDER_H_
+#define UI_VIEWS_BORDER_H_
+
+#include "base/logging.h"
+#include "gfx/insets.h"
+#include "third_party/skia/include/core/SkColor.h"
+
+namespace gfx {
+class Canvas;
+}
+
+namespace ui {
+
+class View;
+
+////////////////////////////////////////////////////////////////////////////////
+// Border class
+//
+// A class that provides padding for a View. Subclass to provide custom
+// rendering of the Border. Insets determine the size of border.
+//
+class Border {
+ public:
+ virtual ~Border();
+
+ // Create various common border types.
+ static Border* CreateSolidBorder(int thickness, SkColor color);
+ static Border* CreateTransparentBorder(const gfx::Insets& insets);
+
+ gfx::Insets insets() const { return insets_; }
+ void set_insets(const gfx::Insets& insets) { insets_ = insets; }
+
+ virtual void Paint(const View* view, gfx::Canvas* canvas) const;
+
+ protected:
+ Border();
+
+ private:
+ gfx::Insets insets_;
+
+ DISALLOW_COPY_AND_ASSIGN(Border);
+};
+
+} // namespace ui
+
+#endif // UI_VIEWS_BORDER_H_
diff --git a/ui/views/rendering/border_unittest.cc b/ui/views/rendering/border_unittest.cc
new file mode 100644
index 0000000..52e62bb
--- /dev/null
+++ b/ui/views/rendering/border_unittest.cc
@@ -0,0 +1,63 @@
+// Copyright (c) 2011 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 <algorithm>
+
+#include "gfx/canvas.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/views/rendering/border.h"
+#include "ui/views/view.h"
+
+namespace ui {
+
+class BorderTest : public testing::Test {
+ public:
+ BorderTest() {}
+ virtual ~BorderTest() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(BorderTest);
+};
+
+class TestBorder : public Border {
+ public:
+ TestBorder() : painted_(false) {}
+
+ bool painted() const { return painted_; }
+
+ // Overridden from Border:
+ virtual void Paint(const View* view, gfx::Canvas* canvas) const {
+ painted_ = true;
+ }
+
+ private:
+ mutable bool painted_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestBorder);
+};
+
+TEST_F(BorderTest, Basic) {
+ const int kViewSize = 100;
+ View v;
+ v.SetBounds(10, 10, kViewSize, kViewSize);
+
+ // With no border, the content size is the view size.
+ EXPECT_EQ(gfx::Rect(0, 0, kViewSize, kViewSize), v.GetContentsBounds());
+
+ const int kViewInset = 10;
+ v.SetBorder(Border::CreateTransparentBorder(
+ gfx::Insets(kViewInset, kViewInset, kViewInset, kViewInset)));
+
+ // With the border, the content bounds are inset by the border's insets.
+ EXPECT_EQ(gfx::Rect(kViewInset, kViewInset, kViewSize - 2 * kViewInset,
+ kViewSize - 2 * kViewInset),
+ v.GetContentsBounds());
+
+ TestBorder* border = new TestBorder;
+ v.SetBorder(border);
+ v.OnPaint(NULL);
+ EXPECT_TRUE(border->painted());
+}
+
+} // namespace ui