base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / views / examples / widget_example.cc
blobd7a1b020d921164ab3fe5415e2833028d74e03e1
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 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);
48 return button;
51 View* DialogExample::CreateTitlebarExtraView() {
52 Label* label = new Label(ASCIIToUTF16("Extra view!"));
53 label->SetEnabledColor(SK_ColorBLUE);
54 return label;
57 View* DialogExample::CreateFootnoteView() {
58 return new Label(ASCIIToUTF16("Footnote label!"));
61 } // namespace
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);
73 #if defined(OS_LINUX)
74 // Windows does not support TYPE_CONTROL top-level widgets.
75 BuildButton(container, "Child widget", CHILD);
76 #endif
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);
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));
94 Widget* widget = new Widget();
95 widget->Init(params);
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);
106 widget->Show();
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;
119 case CHILD:
120 ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_CONTROL));
121 break;
122 case CLOSE_WIDGET:
123 sender->GetWidget()->Close();
124 break;
128 } // namespace examples
129 } // namespace views