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