Home | History | Annotate | Download | only in examples
      1 // Copyright (c) 2012 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/widget_example.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "ui/views/background.h"
      9 #include "ui/views/controls/button/label_button.h"
     10 #include "ui/views/controls/label.h"
     11 #include "ui/views/layout/box_layout.h"
     12 #include "ui/views/view.h"
     13 #include "ui/views/widget/widget.h"
     14 #include "ui/views/window/dialog_delegate.h"
     15 
     16 namespace views {
     17 namespace examples {
     18 
     19 namespace {
     20 
     21 class DialogExample : public DialogDelegateView {
     22  public:
     23   DialogExample();
     24   virtual ~DialogExample();
     25   virtual string16 GetWindowTitle() const OVERRIDE;
     26   virtual View* CreateExtraView() OVERRIDE;
     27   virtual View* CreateTitlebarExtraView() OVERRIDE;
     28   virtual View* CreateFootnoteView() OVERRIDE;
     29 };
     30 
     31 DialogExample::DialogExample() {
     32   set_background(Background::CreateSolidBackground(SK_ColorGRAY));
     33   SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
     34   AddChildView(new Label(ASCIIToUTF16("Dialog contents label!")));
     35 }
     36 
     37 DialogExample::~DialogExample() {}
     38 
     39 string16 DialogExample::GetWindowTitle() const {
     40   return ASCIIToUTF16("Dialog Widget Example");
     41 }
     42 
     43 View* DialogExample::CreateExtraView() {
     44   LabelButton* button = new LabelButton(NULL, ASCIIToUTF16("Extra button!"));
     45   button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON);
     46   return button;
     47 }
     48 
     49 View* DialogExample::CreateTitlebarExtraView() {
     50   Label* label = new Label(ASCIIToUTF16("Extra view!"));
     51   label->SetEnabledColor(SK_ColorBLUE);
     52   return label;
     53 }
     54 
     55 View* DialogExample::CreateFootnoteView() {
     56   return new Label(ASCIIToUTF16("Footnote label!"));
     57 }
     58 
     59 }  // namespace
     60 
     61 WidgetExample::WidgetExample() : ExampleBase("Widget") {
     62 }
     63 
     64 WidgetExample::~WidgetExample() {
     65 }
     66 
     67 void WidgetExample::CreateExampleView(View* container) {
     68   container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 10));
     69   BuildButton(container, "Popup widget", POPUP);
     70   BuildButton(container, "Dialog widget", DIALOG);
     71 #if defined(OS_LINUX)
     72   // Windows does not support TYPE_CONTROL top-level widgets.
     73   BuildButton(container, "Child widget", CHILD);
     74 #endif
     75 }
     76 
     77 void WidgetExample::BuildButton(View* container,
     78                                 const std::string& label,
     79                                 int tag) {
     80   LabelButton* button = new LabelButton(this, ASCIIToUTF16(label));
     81   button->SetFocusable(true);
     82   button->set_tag(tag);
     83   container->AddChildView(button);
     84 }
     85 
     86 void WidgetExample::ShowWidget(View* sender, Widget::InitParams params) {
     87   // Setup shared Widget heirarchy and bounds parameters.
     88   params.parent = sender->GetWidget()->GetNativeView();
     89   params.bounds = gfx::Rect(sender->GetBoundsInScreen().CenterPoint(),
     90                             gfx::Size(300, 200));
     91 
     92   Widget* widget = new Widget();
     93   widget->Init(params);
     94 
     95   // If the Widget has no contents by default, add a view with a 'Close' button.
     96   if (!widget->GetContentsView()) {
     97     View* contents = new View();
     98     contents->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
     99     contents->set_background(Background::CreateSolidBackground(SK_ColorGRAY));
    100     BuildButton(contents, "Close", CLOSE_WIDGET);
    101     widget->SetContentsView(contents);
    102   }
    103 
    104   widget->Show();
    105 }
    106 
    107 void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) {
    108   switch (sender->tag()) {
    109     case POPUP:
    110       ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_POPUP));
    111       break;
    112     case DIALOG: {
    113       DialogDelegate::CreateDialogWidget(new DialogExample(), NULL,
    114           sender->GetWidget()->GetNativeView())->Show();
    115       break;
    116     }
    117     case CHILD:
    118       ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_CONTROL));
    119       break;
    120     case CLOSE_WIDGET:
    121       sender->GetWidget()->Close();
    122       break;
    123   }
    124 }
    125 
    126 }  // namespace examples
    127 }  // namespace views
    128