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