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/utf_string_conversions.h" // ASCIIToUTF16
7 #include "ui/aura/root_window.h"
8 #include "ui/aura/window.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 virtual ~WidgetsWindow();
32 // Overridden from views::View:
33 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
34 virtual void Layout() OVERRIDE
;
35 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
37 // Overridden from views::WidgetDelegate:
38 virtual views::View
* GetContentsView() OVERRIDE
;
39 virtual base::string16
GetWindowTitle() const OVERRIDE
;
40 virtual 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
, ASCIIToUTF16("Button"))),
58 new views::LabelButton(NULL
, ASCIIToUTF16("Disabled button"))),
59 checkbox_(new views::Checkbox(ASCIIToUTF16("Checkbox"))),
60 checkbox_disabled_(new views::Checkbox(
61 ASCIIToUTF16("Checkbox disabled"))),
62 checkbox_checked_(new views::Checkbox(ASCIIToUTF16("Checkbox checked"))),
63 checkbox_checked_disabled_(new views::Checkbox(
64 ASCIIToUTF16("Checkbox checked disabled"))),
65 radio_button_(new views::RadioButton(ASCIIToUTF16("Radio button"), 0)),
66 radio_button_disabled_(new views::RadioButton(
67 ASCIIToUTF16("Radio button disabled"), 0)),
68 radio_button_selected_(new views::RadioButton(
69 ASCIIToUTF16("Radio button selected"), 0)),
70 radio_button_selected_disabled_(new views::RadioButton(
71 ASCIIToUTF16("Radio button selected disabled"), 1)) {
72 button_
->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON
);
73 AddChildView(button_
);
74 disabled_button_
->SetEnabled(false);
75 disabled_button_
->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON
);
76 AddChildView(disabled_button_
);
77 AddChildView(checkbox_
);
78 checkbox_disabled_
->SetEnabled(false);
79 AddChildView(checkbox_disabled_
);
80 checkbox_checked_
->SetChecked(true);
81 AddChildView(checkbox_checked_
);
82 checkbox_checked_disabled_
->SetChecked(true);
83 checkbox_checked_disabled_
->SetEnabled(false);
84 AddChildView(checkbox_checked_disabled_
);
85 AddChildView(radio_button_
);
86 radio_button_disabled_
->SetEnabled(false);
87 AddChildView(radio_button_disabled_
);
88 radio_button_selected_
->SetChecked(true);
89 AddChildView(radio_button_selected_
);
90 radio_button_selected_disabled_
->SetChecked(true);
91 radio_button_selected_disabled_
->SetEnabled(false);
92 AddChildView(radio_button_selected_disabled_
);
95 WidgetsWindow::~WidgetsWindow() {
98 void WidgetsWindow::OnPaint(gfx::Canvas
* canvas
) {
99 canvas
->FillRect(GetLocalBounds(), SK_ColorWHITE
);
102 void WidgetsWindow::Layout() {
103 const int kVerticalPad
= 5;
105 int top
= kVerticalPad
;
106 for (int i
= 0; i
< child_count(); ++i
) {
107 views::View
* view
= child_at(i
);
108 gfx::Size preferred
= view
->GetPreferredSize();
109 view
->SetBounds(left
, top
, preferred
.width(), preferred
.height());
110 top
+= preferred
.height() + kVerticalPad
;
114 gfx::Size
WidgetsWindow::GetPreferredSize() {
115 return gfx::Size(kWindowWidth
, kWindowHeight
);
118 views::View
* WidgetsWindow::GetContentsView() {
122 base::string16
WidgetsWindow::GetWindowTitle() const {
123 return ASCIIToUTF16("Examples: Widgets");
126 bool WidgetsWindow::CanResize() const {
135 void CreateWidgetsWindow() {
136 gfx::Rect
bounds(kWindowLeft
, kWindowTop
, kWindowWidth
, kWindowHeight
);
137 views::Widget
* widget
=
138 views::Widget::CreateWindowWithContextAndBounds(
139 new WidgetsWindow
, Shell::GetPrimaryRootWindow(), bounds
);
140 widget
->GetNativeView()->SetName("WidgetsWindow");