1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/examples/scroll_view_example.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/views/background.h"
10 #include "ui/views/controls/button/label_button.h"
11 #include "ui/views/controls/button/radio_button.h"
12 #include "ui/views/layout/grid_layout.h"
13 #include "ui/views/view.h"
15 using base::ASCIIToUTF16
;
20 // ScrollView's content, which draws gradient color on background.
21 // TODO(oshima): add child views as well.
22 class ScrollViewExample::ScrollableView
: public View
{
25 SetColor(SK_ColorRED
, SK_ColorCYAN
);
26 AddChildView(new LabelButton(NULL
, ASCIIToUTF16("Button")));
27 AddChildView(new RadioButton(ASCIIToUTF16("Radio Button"), 0));
30 virtual gfx::Size
GetPreferredSize() const OVERRIDE
{
31 return gfx::Size(width(), height());
34 void SetColor(SkColor from
, SkColor to
) {
35 set_background(Background::CreateVerticalGradientBackground(from
, to
));
38 void PlaceChildY(int index
, int y
) {
39 View
* view
= child_at(index
);
40 gfx::Size size
= view
->GetPreferredSize();
41 view
->SetBounds(0, y
, size
.width(), size
.height());
44 virtual void Layout() OVERRIDE
{
46 PlaceChildY(1, height() / 2);
47 SizeToPreferredSize();
51 DISALLOW_COPY_AND_ASSIGN(ScrollableView
);
54 ScrollViewExample::ScrollViewExample() : ExampleBase("Scroll View") {
57 ScrollViewExample::~ScrollViewExample() {
60 void ScrollViewExample::CreateExampleView(View
* container
) {
61 wide_
= new LabelButton(this, ASCIIToUTF16("Wide"));
62 tall_
= new LabelButton(this, ASCIIToUTF16("Tall"));
63 big_square_
= new LabelButton(this, ASCIIToUTF16("Big Square"));
64 small_square_
= new LabelButton(this, ASCIIToUTF16("Small Square"));
65 scroll_to_
= new LabelButton(this, ASCIIToUTF16("Scroll to"));
66 scrollable_
= new ScrollableView();
67 scroll_view_
= new ScrollView();
68 scroll_view_
->SetContents(scrollable_
);
69 scrollable_
->SetBounds(0, 0, 1000, 100);
70 scrollable_
->SetColor(SK_ColorYELLOW
, SK_ColorCYAN
);
72 GridLayout
* layout
= new GridLayout(container
);
73 container
->SetLayoutManager(layout
);
76 ColumnSet
* column_set
= layout
->AddColumnSet(0);
77 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
78 GridLayout::USE_PREF
, 0, 0);
79 layout
->StartRow(1, 0);
80 layout
->AddView(scroll_view_
);
82 // Add control buttons.
83 column_set
= layout
->AddColumnSet(1);
84 for (int i
= 0; i
< 5; i
++) {
85 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
86 GridLayout::USE_PREF
, 0, 0);
88 layout
->StartRow(0, 1);
89 layout
->AddView(wide_
);
90 layout
->AddView(tall_
);
91 layout
->AddView(big_square_
);
92 layout
->AddView(small_square_
);
93 layout
->AddView(scroll_to_
);
96 void ScrollViewExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
97 if (sender
== wide_
) {
98 scrollable_
->SetBounds(0, 0, 1000, 100);
99 scrollable_
->SetColor(SK_ColorYELLOW
, SK_ColorCYAN
);
100 } else if (sender
== tall_
) {
101 scrollable_
->SetBounds(0, 0, 100, 1000);
102 scrollable_
->SetColor(SK_ColorRED
, SK_ColorCYAN
);
103 } else if (sender
== big_square_
) {
104 scrollable_
->SetBounds(0, 0, 1000, 1000);
105 scrollable_
->SetColor(SK_ColorRED
, SK_ColorGREEN
);
106 } else if (sender
== small_square_
) {
107 scrollable_
->SetBounds(0, 0, 100, 100);
108 scrollable_
->SetColor(SK_ColorYELLOW
, SK_ColorGREEN
);
109 } else if (sender
== scroll_to_
) {
110 scroll_view_
->contents()->ScrollRectToVisible(
111 gfx::Rect(20, 500, 1000, 500));
113 scroll_view_
->Layout();
116 } // namespace examples