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 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!")));
39 DialogExample::~DialogExample() {}
41 base::string16
DialogExample::GetWindowTitle() const {
42 return ASCIIToUTF16("Dialog Widget Example");
45 View
* DialogExample::CreateExtraView() {
46 LabelButton
* button
= new LabelButton(NULL
, ASCIIToUTF16("Extra button!"));
47 button
->SetStyle(Button::STYLE_BUTTON
);
51 View
* DialogExample::CreateTitlebarExtraView() {
52 Label
* label
= new Label(ASCIIToUTF16("Extra view!"));
53 label
->SetEnabledColor(SK_ColorBLUE
);
57 View
* DialogExample::CreateFootnoteView() {
58 return new Label(ASCIIToUTF16("Footnote label!"));
63 WidgetExample::WidgetExample() : ExampleBase("Widget") {
66 WidgetExample::~WidgetExample() {
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
);
74 // Windows does not support TYPE_CONTROL top-level widgets.
75 BuildButton(container
, "Child widget", CHILD
);
79 void WidgetExample::BuildButton(View
* container
,
80 const std::string
& label
,
82 LabelButton
* button
= new LabelButton(this, ASCIIToUTF16(label
));
83 button
->SetFocusable(true);
85 container
->AddChildView(button
);
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(),
94 Widget
* widget
= new Widget();
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
);
109 void WidgetExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
110 switch (sender
->tag()) {
112 ShowWidget(sender
, Widget::InitParams(Widget::InitParams::TYPE_POPUP
));
115 DialogDelegate::CreateDialogWidget(new DialogExample(), NULL
,
116 sender
->GetWidget()->GetNativeView())->Show();
120 ShowWidget(sender
, Widget::InitParams(Widget::InitParams::TYPE_CONTROL
));
123 sender
->GetWidget()->Close();
128 } // namespace examples