summaryrefslogtreecommitdiffstats
path: root/chromium/ash/shell/widgets.cc
blob: 01654906fbc3abe43f23c6aa13632f2524f187cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shell.h"
#include "base/strings/utf_string_conversions.h"  // ASCIIToUTF16
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/canvas.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/radio_button.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace {

// Default window position.
const int kWindowLeft = 170;
const int kWindowTop = 200;

// Default window size.
const int kWindowWidth = 400;
const int kWindowHeight = 400;

// A window showing samples of commonly used widgets.
class WidgetsWindow : public views::WidgetDelegateView {
 public:
  WidgetsWindow();
  virtual ~WidgetsWindow();

  // Overridden from views::View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
  virtual void Layout() OVERRIDE;
  virtual gfx::Size GetPreferredSize() OVERRIDE;

  // Overridden from views::WidgetDelegate:
  virtual views::View* GetContentsView() OVERRIDE;
  virtual base::string16 GetWindowTitle() const OVERRIDE;
  virtual bool CanResize() const OVERRIDE;

 private:
  views::LabelButton* button_;
  views::LabelButton* disabled_button_;
  views::Checkbox* checkbox_;
  views::Checkbox* checkbox_disabled_;
  views::Checkbox* checkbox_checked_;
  views::Checkbox* checkbox_checked_disabled_;
  views::RadioButton* radio_button_;
  views::RadioButton* radio_button_disabled_;
  views::RadioButton* radio_button_selected_;
  views::RadioButton* radio_button_selected_disabled_;
};

WidgetsWindow::WidgetsWindow()
    : button_(new views::LabelButton(NULL, ASCIIToUTF16("Button"))),
      disabled_button_(
          new views::LabelButton(NULL, ASCIIToUTF16("Disabled button"))),
      checkbox_(new views::Checkbox(ASCIIToUTF16("Checkbox"))),
      checkbox_disabled_(new views::Checkbox(
          ASCIIToUTF16("Checkbox disabled"))),
      checkbox_checked_(new views::Checkbox(ASCIIToUTF16("Checkbox checked"))),
      checkbox_checked_disabled_(new views::Checkbox(
          ASCIIToUTF16("Checkbox checked disabled"))),
      radio_button_(new views::RadioButton(ASCIIToUTF16("Radio button"), 0)),
      radio_button_disabled_(new views::RadioButton(
          ASCIIToUTF16("Radio button disabled"), 0)),
      radio_button_selected_(new views::RadioButton(
          ASCIIToUTF16("Radio button selected"), 0)),
      radio_button_selected_disabled_(new views::RadioButton(
          ASCIIToUTF16("Radio button selected disabled"), 1)) {
  button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
  AddChildView(button_);
  disabled_button_->SetEnabled(false);
  disabled_button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
  AddChildView(disabled_button_);
  AddChildView(checkbox_);
  checkbox_disabled_->SetEnabled(false);
  AddChildView(checkbox_disabled_);
  checkbox_checked_->SetChecked(true);
  AddChildView(checkbox_checked_);
  checkbox_checked_disabled_->SetChecked(true);
  checkbox_checked_disabled_->SetEnabled(false);
  AddChildView(checkbox_checked_disabled_);
  AddChildView(radio_button_);
  radio_button_disabled_->SetEnabled(false);
  AddChildView(radio_button_disabled_);
  radio_button_selected_->SetChecked(true);
  AddChildView(radio_button_selected_);
  radio_button_selected_disabled_->SetChecked(true);
  radio_button_selected_disabled_->SetEnabled(false);
  AddChildView(radio_button_selected_disabled_);
}

WidgetsWindow::~WidgetsWindow() {
}

void WidgetsWindow::OnPaint(gfx::Canvas* canvas) {
  canvas->FillRect(GetLocalBounds(), SK_ColorWHITE);
}

void WidgetsWindow::Layout() {
  const int kVerticalPad = 5;
  int left = 5;
  int top = kVerticalPad;
  for (int i = 0; i < child_count(); ++i) {
    views::View* view = child_at(i);
    gfx::Size preferred = view->GetPreferredSize();
    view->SetBounds(left, top, preferred.width(), preferred.height());
    top += preferred.height() + kVerticalPad;
  }
}

gfx::Size WidgetsWindow::GetPreferredSize() {
  return gfx::Size(kWindowWidth, kWindowHeight);
}

views::View* WidgetsWindow::GetContentsView() {
  return this;
}

base::string16 WidgetsWindow::GetWindowTitle() const {
  return ASCIIToUTF16("Examples: Widgets");
}

bool WidgetsWindow::CanResize() const {
  return true;
}

}  // namespace

namespace ash {
namespace shell {

void CreateWidgetsWindow() {
  gfx::Rect bounds(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight);
  views::Widget* widget =
      views::Widget::CreateWindowWithContextAndBounds(
          new WidgetsWindow, Shell::GetPrimaryRootWindow(), bounds);
  widget->GetNativeView()->SetName("WidgetsWindow");
  widget->Show();
}

}  // namespace shell
}  // namespace ash