Home | History | Annotate | Download | only in examples
      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/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"
     14 
     15 namespace views {
     16 namespace examples {
     17 
     18 // ScrollView's content, which draws gradient color on background.
     19 // TODO(oshima): add child views as well.
     20 class ScrollViewExample::ScrollableView : public View {
     21  public:
     22   ScrollableView() {
     23     SetColor(SK_ColorRED, SK_ColorCYAN);
     24     AddChildView(new LabelButton(NULL, ASCIIToUTF16("Button")));
     25     AddChildView(new RadioButton(ASCIIToUTF16("Radio Button"), 0));
     26   }
     27 
     28   virtual gfx::Size GetPreferredSize() OVERRIDE {
     29     return gfx::Size(width(), height());
     30   }
     31 
     32   void SetColor(SkColor from, SkColor to) {
     33     set_background(Background::CreateVerticalGradientBackground(from, to));
     34   }
     35 
     36   void PlaceChildY(int index, int y) {
     37     View* view = child_at(index);
     38     gfx::Size size = view->GetPreferredSize();
     39     view->SetBounds(0, y, size.width(), size.height());
     40   }
     41 
     42   virtual void Layout() OVERRIDE {
     43     PlaceChildY(0, 0);
     44     PlaceChildY(1, height() / 2);
     45     SizeToPreferredSize();
     46   }
     47 
     48  private:
     49   DISALLOW_COPY_AND_ASSIGN(ScrollableView);
     50 };
     51 
     52 ScrollViewExample::ScrollViewExample() : ExampleBase("Scroll View") {
     53 }
     54 
     55 ScrollViewExample::~ScrollViewExample() {
     56 }
     57 
     58 void ScrollViewExample::CreateExampleView(View* container) {
     59   wide_ = new LabelButton(this, ASCIIToUTF16("Wide"));
     60   tall_ = new LabelButton(this, ASCIIToUTF16("Tall"));
     61   big_square_ = new LabelButton(this, ASCIIToUTF16("Big Square"));
     62   small_square_ = new LabelButton(this, ASCIIToUTF16("Small Square"));
     63   scroll_to_ = new LabelButton(this, ASCIIToUTF16("Scroll to"));
     64   scrollable_ = new ScrollableView();
     65   scroll_view_ = new ScrollView();
     66   scroll_view_->SetContents(scrollable_);
     67   scrollable_->SetBounds(0, 0, 1000, 100);
     68   scrollable_->SetColor(SK_ColorYELLOW, SK_ColorCYAN);
     69 
     70   GridLayout* layout = new GridLayout(container);
     71   container->SetLayoutManager(layout);
     72 
     73   // Add scroll view.
     74   ColumnSet* column_set = layout->AddColumnSet(0);
     75   column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
     76                         GridLayout::USE_PREF, 0, 0);
     77   layout->StartRow(1, 0);
     78   layout->AddView(scroll_view_);
     79 
     80   // Add control buttons.
     81   column_set = layout->AddColumnSet(1);
     82   for (int i = 0; i < 5; i++) {
     83     column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
     84                           GridLayout::USE_PREF, 0, 0);
     85   }
     86   layout->StartRow(0, 1);
     87   layout->AddView(wide_);
     88   layout->AddView(tall_);
     89   layout->AddView(big_square_);
     90   layout->AddView(small_square_);
     91   layout->AddView(scroll_to_);
     92 }
     93 
     94 void ScrollViewExample::ButtonPressed(Button* sender, const ui::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_->contents()->ScrollRectToVisible(
    109         gfx::Rect(20, 500, 1000, 500));
    110   }
    111   scroll_view_->Layout();
    112 }
    113 
    114 }  // namespace examples
    115 }  // namespace views
    116