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
;
23 class DialogExample
: public DialogDelegateView
{
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
{
35 ModalDialogExample() {}
38 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_WINDOW
; }
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
);
62 View
* DialogExample::CreateTitlebarExtraView() {
63 Label
* label
= new Label(ASCIIToUTF16("Extra view!"));
64 label
->SetEnabledColor(SK_ColorBLUE
);
68 View
* DialogExample::CreateFootnoteView() {
69 return new Label(ASCIIToUTF16("Footnote label!"));
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
);
86 // Windows does not support TYPE_CONTROL top-level widgets.
87 BuildButton(container
, "Child widget", CHILD
);
91 void WidgetExample::BuildButton(View
* container
,
92 const std::string
& label
,
94 LabelButton
* button
= new LabelButton(this, ASCIIToUTF16(label
));
95 button
->SetFocusable(true);
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
);
121 void WidgetExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
122 switch (sender
->tag()) {
124 ShowWidget(sender
, Widget::InitParams(Widget::InitParams::TYPE_POPUP
));
127 DialogDelegate::CreateDialogWidget(new DialogExample(), NULL
,
128 sender
->GetWidget()->GetNativeView())->Show();
132 DialogDelegate::CreateDialogWidget(new ModalDialogExample(), NULL
,
133 sender
->GetWidget()->GetNativeView())->Show();
137 ShowWidget(sender
, Widget::InitParams(Widget::InitParams::TYPE_CONTROL
));
140 sender
->GetWidget()->Close();
145 } // namespace examples