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