Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / examples / widget_example.cc
blob3a9506f52722b66397c68ce504025daa8f46a5ff
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.
5 #include "ui/views/examples/widget_example.h"
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"
16 using base::ASCIIToUTF16;
18 namespace views {
19 namespace examples {
21 namespace {
23 class DialogExample : public DialogDelegateView {
24 public:
25 DialogExample();
26 ~DialogExample() override;
27 base::string16 GetWindowTitle() const override;
28 View* CreateExtraView() override;
29 View* CreateTitlebarExtraView() override;
30 View* CreateFootnoteView() override;
33 class ModalDialogExample : public DialogExample {
34 public:
35 ModalDialogExample() {}
37 // WidgetDelegate:
38 ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_WINDOW; }
40 private:
41 DISALLOW_COPY_AND_ASSIGN(ModalDialogExample);
44 DialogExample::DialogExample() {
45 set_background(Background::CreateSolidBackground(SK_ColorGRAY));
46 SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
47 AddChildView(new Label(ASCIIToUTF16("Dialog contents label!")));
50 DialogExample::~DialogExample() {}
52 base::string16 DialogExample::GetWindowTitle() const {
53 return ASCIIToUTF16("Dialog Widget Example");
56 View* DialogExample::CreateExtraView() {
57 LabelButton* button = new LabelButton(NULL, ASCIIToUTF16("Extra button!"));
58 button->SetStyle(Button::STYLE_BUTTON);
59 return button;
62 View* DialogExample::CreateTitlebarExtraView() {
63 Label* label = new Label(ASCIIToUTF16("Extra view!"));
64 label->SetEnabledColor(SK_ColorBLUE);
65 return label;
68 View* DialogExample::CreateFootnoteView() {
69 return new Label(ASCIIToUTF16("Footnote label!"));
72 } // namespace
74 WidgetExample::WidgetExample() : ExampleBase("Widget") {
77 WidgetExample::~WidgetExample() {
80 void WidgetExample::CreateExampleView(View* container) {
81 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 10));
82 BuildButton(container, "Popup widget", POPUP);
83 BuildButton(container, "Dialog widget", DIALOG);
84 BuildButton(container, "Modal Dialog", MODAL_DIALOG);
85 #if defined(OS_LINUX)
86 // Windows does not support TYPE_CONTROL top-level widgets.
87 BuildButton(container, "Child widget", CHILD);
88 #endif
91 void WidgetExample::BuildButton(View* container,
92 const std::string& label,
93 int tag) {
94 LabelButton* button = new LabelButton(this, ASCIIToUTF16(label));
95 button->SetFocusable(true);
96 button->set_tag(tag);
97 container->AddChildView(button);
100 void WidgetExample::ShowWidget(View* sender, Widget::InitParams params) {
101 // Setup shared Widget heirarchy and bounds parameters.
102 params.parent = sender->GetWidget()->GetNativeView();
103 params.bounds = gfx::Rect(sender->GetBoundsInScreen().CenterPoint(),
104 gfx::Size(300, 200));
106 Widget* widget = new Widget();
107 widget->Init(params);
109 // If the Widget has no contents by default, add a view with a 'Close' button.
110 if (!widget->GetContentsView()) {
111 View* contents = new View();
112 contents->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
113 contents->set_background(Background::CreateSolidBackground(SK_ColorGRAY));
114 BuildButton(contents, "Close", CLOSE_WIDGET);
115 widget->SetContentsView(contents);
118 widget->Show();
121 void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) {
122 switch (sender->tag()) {
123 case POPUP:
124 ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_POPUP));
125 break;
126 case DIALOG: {
127 DialogDelegate::CreateDialogWidget(new DialogExample(), NULL,
128 sender->GetWidget()->GetNativeView())->Show();
129 break;
131 case MODAL_DIALOG: {
132 DialogDelegate::CreateDialogWidget(new ModalDialogExample(), NULL,
133 sender->GetWidget()->GetNativeView())->Show();
134 break;
136 case CHILD:
137 ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_CONTROL));
138 break;
139 case CLOSE_WIDGET:
140 sender->GetWidget()->Close();
141 break;
145 } // namespace examples
146 } // namespace views