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.
4
5 #include "ash/shell.h"
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"
15
16 namespace {
17
18 // Default window position.
19 const int kWindowLeft = 170;
20 const int kWindowTop = 200;
21
22 // Default window size.
23 const int kWindowWidth = 400;
24 const int kWindowHeight = 400;
25
26 // A window showing samples of commonly used widgets.
27 class WidgetsWindow : public views::WidgetDelegateView {
28 public:
29 WidgetsWindow();
30 virtual ~WidgetsWindow();
31
32 // Overridden from views::View:
33 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
34 virtual void Layout() OVERRIDE;
35 virtual gfx::Size GetPreferredSize() const OVERRIDE;
36
37 // Overridden from views::WidgetDelegate:
38 virtual views::View* GetContentsView() OVERRIDE;
39 virtual base::string16 GetWindowTitle() const OVERRIDE;
40 virtual bool CanResize() const OVERRIDE;
41
42 private:
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_;
53 };
54
WidgetsWindow()55 WidgetsWindow::WidgetsWindow()
56 : button_(new views::LabelButton(NULL, base::ASCIIToUTF16("Button"))),
57 disabled_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_);
95 }
96
~WidgetsWindow()97 WidgetsWindow::~WidgetsWindow() {
98 }
99
OnPaint(gfx::Canvas * canvas)100 void WidgetsWindow::OnPaint(gfx::Canvas* canvas) {
101 canvas->FillRect(GetLocalBounds(), SK_ColorWHITE);
102 }
103
Layout()104 void WidgetsWindow::Layout() {
105 const int kVerticalPad = 5;
106 int left = 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;
113 }
114 }
115
GetPreferredSize() const116 gfx::Size WidgetsWindow::GetPreferredSize() const {
117 return gfx::Size(kWindowWidth, kWindowHeight);
118 }
119
GetContentsView()120 views::View* WidgetsWindow::GetContentsView() {
121 return this;
122 }
123
GetWindowTitle() const124 base::string16 WidgetsWindow::GetWindowTitle() const {
125 return base::ASCIIToUTF16("Examples: Widgets");
126 }
127
CanResize() const128 bool WidgetsWindow::CanResize() const {
129 return true;
130 }
131
132 } // namespace
133
134 namespace ash {
135 namespace shell {
136
CreateWidgetsWindow()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");
143 widget->Show();
144 }
145
146 } // namespace shell
147 } // namespace ash
148