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