diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-28 02:50:46 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-28 02:50:46 +0000 |
commit | d6a55b3e722bd1890c79d2b13e7fdf9d0e36ec2d (patch) | |
tree | 6468e9b5fa2a4668fcd159f4fed803e7b58de3ca | |
parent | fe23d5a3bceee44113c07dff58181f907d7cdb37 (diff) | |
download | chromium_src-d6a55b3e722bd1890c79d2b13e7fdf9d0e36ec2d.zip chromium_src-d6a55b3e722bd1890c79d2b13e7fdf9d0e36ec2d.tar.gz chromium_src-d6a55b3e722bd1890c79d2b13e7fdf9d0e36ec2d.tar.bz2 |
views: Move BoxLayout class into views/examples directory.
Also add horizontal support as well.
BUG=None
TEST=out/Debug/view_examples, see Widget tab.
Review URL: http://codereview.chromium.org/2145001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48460 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | views/examples/box_layout.cc | 37 | ||||
-rw-r--r-- | views/examples/box_layout.h | 40 | ||||
-rw-r--r-- | views/examples/widget_example.h | 72 | ||||
-rw-r--r-- | views/views.gyp | 4 |
4 files changed, 95 insertions, 58 deletions
diff --git a/views/examples/box_layout.cc b/views/examples/box_layout.cc new file mode 100644 index 0000000..957d0d6 --- /dev/null +++ b/views/examples/box_layout.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2010 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 "views/examples/box_layout.h" + +namespace examples { + +BoxLayout::BoxLayout(Orientation orientation, int margin) + : orientation_(orientation), + margin_(margin) { +} + +void BoxLayout::Layout(views::View* host) { + int height = host->height(); + int width = host->width(); + int count = host->GetChildViewCount(); + + int z = 0; + for (int i = 0; i < count; ++i) { + views::View* child = host->GetChildViewAt(i); + + if (orientation_ == kVertical) { + child->SetBounds(0, z, width, height / count); + z = (height + margin_) * (i + 1) / count; + } else if (orientation_ == kHorizontal) { + child->SetBounds(z, 0, width / count, height); + z = (width + margin_) * (i + 1) / count; + } + } +} + +gfx::Size BoxLayout::GetPreferredSize(views::View* host) { + return gfx::Size(); +} + +} // namespace examples diff --git a/views/examples/box_layout.h b/views/examples/box_layout.h new file mode 100644 index 0000000..2e802aa --- /dev/null +++ b/views/examples/box_layout.h @@ -0,0 +1,40 @@ +// Copyright (c) 2010 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 VIEWS_EXAMPLES_BOX_LAYOUT_H_ +#define VIEWS_EXAMPLES_BOX_LAYOUT_H_ + +#include "views/layout_manager.h" + +namespace examples { + +// A layout manager that layouts child views vertically or horizontally. +class BoxLayout : public views::LayoutManager { + public: + enum Orientation { + kHorizontal, + kVertical, + }; + + BoxLayout(Orientation orientation, int margin); + + virtual ~BoxLayout() {} + + // Overridden from views::LayoutManager: + virtual void Layout(views::View* host); + + virtual gfx::Size GetPreferredSize(views::View* host); + + private: + const Orientation orientation_; + + // The pixel distance between children. + const int margin_; + + DISALLOW_COPY_AND_ASSIGN(BoxLayout); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_BOX_LAYOUT_H_ diff --git a/views/examples/widget_example.h b/views/examples/widget_example.h index b84e8f8..7546ba4 100644 --- a/views/examples/widget_example.h +++ b/views/examples/widget_example.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -7,8 +7,8 @@ #include "views/background.h" #include "views/controls/button/text_button.h" +#include "views/examples/box_layout.h" #include "views/examples/example_base.h" -#include "views/fill_layout.h" #include "views/view.h" #include "views/widget/root_view.h" #include "views/widget/widget.h" @@ -19,55 +19,12 @@ namespace { -// A layout manager that layouts child views vertically. -// TODO(oshima): support horizontal support and -// move to views/. -class BoxLayout : public views::LayoutManager { - public: - BoxLayout() {} - virtual ~BoxLayout() { - } - - // Overridden from LayoutManager: - virtual void Layout(views::View* host) { - int height = host->height(); - int width = host->width(); - int count = host->GetChildViewCount(); - - int y = 0; - for (int i = 0; i < count; i++) { - views::View* child = host->GetChildViewAt(i); - child->SetBounds(0, y, width, height / count); - y = height * (i + 1)/ count; - } - } - - virtual gfx::Size GetPreferredSize(views::View* host) { - int count = host->GetChildViewCount(); - gfx::Rect bounds; - int y = 0; - for (int i = 0; i < count; i++) { - views::View* child = host->GetChildViewAt(i); - gfx::Size size = child->GetPreferredSize(); - gfx::Rect child_bounds(0, y, size.width(), size.height()); - bounds = bounds.Union(child_bounds); - y += size.height(); - } - return bounds.size(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(BoxLayout); -}; - // A layout manager that layouts a single child at // the center of the host view. class CenterLayout : public views::LayoutManager { public: - CenterLayout() { - } - virtual ~CenterLayout() { - } + CenterLayout() {} + virtual ~CenterLayout() {} // Overridden from LayoutManager: virtual void Layout(views::View* host) { @@ -95,24 +52,24 @@ using views::Widget; // WidgetExample demonstrates how to create a popup widget. class WidgetExample : public ExampleBase, public views::ButtonListener { public: - explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) { - } + explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) {} virtual ~WidgetExample() {} - virtual std::wstring GetExampleTitle() { - return L"Widget"; - } + virtual std::wstring GetExampleTitle() { return L"Widget"; } virtual void CreateExampleView(views::View* container) { - container->SetLayoutManager(new BoxLayout()); + container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 2)); BuildButton(container, L"Create a popup widget", POPUP); BuildButton(container, L"Create a transparent popup widget", TRANSPARENT_POPUP); #if defined(OS_LINUX) - BuildButton(container, L"Create a child widget", CHILD); - BuildButton(container, - L"Create a transparent child widget", TRANSPARENT_CHILD); + views::View* vert_container = new views::View(); + container->AddChildView(vert_container); + vert_container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 20)); + BuildButton(vert_container, L"Create a child widget", CHILD); + BuildButton(vert_container, L"Create a transparent child widget", + TRANSPARENT_CHILD); #endif } @@ -142,7 +99,8 @@ class WidgetExample : public ExampleBase, public views::ButtonListener { native_button->set_tag(CLOSE_WIDGET); views::View* button_container = new views::View(); - button_container->SetLayoutManager(new BoxLayout); + button_container->SetLayoutManager( + new BoxLayout(BoxLayout::kHorizontal, 1)); button_container->AddChildView(close_button); button_container->AddChildView(native_button); diff --git a/views/views.gyp b/views/views.gyp index 2344e00..0e58701 100644 --- a/views/views.gyp +++ b/views/views.gyp @@ -1,4 +1,4 @@ -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Copyright (c) 2010 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. @@ -373,6 +373,8 @@ '..', ], 'sources': [ + 'examples/box_layout.cc', + 'examples/box_layout.h', 'examples/button_example.h', 'examples/combobox_example.h', 'examples/example_base.cc', |