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.
6 #include "base/strings/utf_string_conversions.h"
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_event_dispatcher.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/controls/button/checkbox.h"
11 #include "ui/views/controls/button/label_button.h"
12 #include "ui/views/controls/button/radio_button.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_delegate.h"
18 // Default window position.
19 const int kWindowLeft
= 170;
20 const int kWindowTop
= 200;
22 // Default window size.
23 const int kWindowWidth
= 400;
24 const int kWindowHeight
= 400;
26 // A window showing samples of commonly used widgets.
27 class WidgetsWindow
: public views::WidgetDelegateView
{
30 ~WidgetsWindow() override
;
32 // Overridden from views::View:
33 void OnPaint(gfx::Canvas
* canvas
) override
;
34 void Layout() override
;
35 gfx::Size
GetPreferredSize() const override
;
37 // Overridden from views::WidgetDelegate:
38 views::View
* GetContentsView() override
;
39 base::string16
GetWindowTitle() const override
;
40 bool CanResize() const override
;
43 views::LabelButton
* button_
;
44 views::LabelButton
* disabled_button_
;
45 views::Checkbox
* checkbox_
;
46 views::Checkbox
* checkbox_disabled_
;
47 views::Checkbox
* checkbox_checked_
;
48 views::Checkbox
* checkbox_checked_disabled_
;
49 views::RadioButton
* radio_button_
;
50 views::RadioButton
* radio_button_disabled_
;
51 views::RadioButton
* radio_button_selected_
;
52 views::RadioButton
* radio_button_selected_disabled_
;
55 WidgetsWindow::WidgetsWindow()
56 : button_(new views::LabelButton(NULL
, base::ASCIIToUTF16("Button"))),
58 new views::LabelButton(NULL
, base::ASCIIToUTF16("Disabled button"))),
59 checkbox_(new views::Checkbox(base::ASCIIToUTF16("Checkbox"))),
60 checkbox_disabled_(new views::Checkbox(
61 base::ASCIIToUTF16("Checkbox disabled"))),
62 checkbox_checked_(new views::Checkbox(
63 base::ASCIIToUTF16("Checkbox checked"))),
64 checkbox_checked_disabled_(new views::Checkbox(
65 base::ASCIIToUTF16("Checkbox checked disabled"))),
66 radio_button_(new views::RadioButton(
67 base::ASCIIToUTF16("Radio button"), 0)),
68 radio_button_disabled_(new views::RadioButton(
69 base::ASCIIToUTF16("Radio button disabled"), 0)),
70 radio_button_selected_(new views::RadioButton(
71 base::ASCIIToUTF16("Radio button selected"), 0)),
72 radio_button_selected_disabled_(new views::RadioButton(
73 base::ASCIIToUTF16("Radio button selected disabled"), 1)) {
74 button_
->SetStyle(views::Button::STYLE_BUTTON
);
75 AddChildView(button_
);
76 disabled_button_
->SetEnabled(false);
77 disabled_button_
->SetStyle(views::Button::STYLE_BUTTON
);
78 AddChildView(disabled_button_
);
79 AddChildView(checkbox_
);
80 checkbox_disabled_
->SetEnabled(false);
81 AddChildView(checkbox_disabled_
);
82 checkbox_checked_
->SetChecked(true);
83 AddChildView(checkbox_checked_
);
84 checkbox_checked_disabled_
->SetChecked(true);
85 checkbox_checked_disabled_
->SetEnabled(false);
86 AddChildView(checkbox_checked_disabled_
);
87 AddChildView(radio_button_
);
88 radio_button_disabled_
->SetEnabled(false);
89 AddChildView(radio_button_disabled_
);
90 radio_button_selected_
->SetChecked(true);
91 AddChildView(radio_button_selected_
);
92 radio_button_selected_disabled_
->SetChecked(true);
93 radio_button_selected_disabled_
->SetEnabled(false);
94 AddChildView(radio_button_selected_disabled_
);
97 WidgetsWindow::~WidgetsWindow() {
100 void WidgetsWindow::OnPaint(gfx::Canvas
* canvas
) {
101 canvas
->FillRect(GetLocalBounds(), SK_ColorWHITE
);
104 void WidgetsWindow::Layout() {
105 const int kVerticalPad
= 5;
107 int top
= kVerticalPad
;
108 for (int i
= 0; i
< child_count(); ++i
) {
109 views::View
* view
= child_at(i
);
110 gfx::Size preferred
= view
->GetPreferredSize();
111 view
->SetBounds(left
, top
, preferred
.width(), preferred
.height());
112 top
+= preferred
.height() + kVerticalPad
;
116 gfx::Size
WidgetsWindow::GetPreferredSize() const {
117 return gfx::Size(kWindowWidth
, kWindowHeight
);
120 views::View
* WidgetsWindow::GetContentsView() {
124 base::string16
WidgetsWindow::GetWindowTitle() const {
125 return base::ASCIIToUTF16("Examples: Widgets");
128 bool WidgetsWindow::CanResize() const {
137 void CreateWidgetsWindow() {
138 gfx::Rect
bounds(kWindowLeft
, kWindowTop
, kWindowWidth
, kWindowHeight
);
139 views::Widget
* widget
=
140 views::Widget::CreateWindowWithContextAndBounds(
141 new WidgetsWindow
, Shell::GetPrimaryRootWindow(), bounds
);
142 widget
->GetNativeView()->SetName("WidgetsWindow");