OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/constrained_window_frame_simple.h" | 5 #include "chrome/browser/ui/views/constrained_window_frame_simple.h" |
6 | 6 |
| 7 #include "chrome/browser/ui/constrained_window.h" |
7 #include "chrome/browser/ui/views/constrained_window_views.h" | 8 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 9 #include "grit/ui_resources.h" |
| 10 #include "grit/chromium_strings.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "grit/google_chrome_strings.h" |
| 13 #include "grit/shared_resources.h" |
| 14 #include "grit/theme_resources.h" |
8 #include "ui/base/hit_test.h" | 15 #include "ui/base/hit_test.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/canvas.h" |
9 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
| 19 #include "ui/gfx/path.h" |
| 20 #include "ui/views/background.h" |
| 21 #include "ui/views/controls/label.h" |
| 22 #include "ui/views/controls/button/image_button.h" |
| 23 #include "ui/views/layout/grid_layout.h" |
| 24 #include "ui/views/layout/layout_manager.h" |
| 25 #include "ui/views/layout/layout_constants.h" |
10 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
| 27 #include "ui/views/widget/widget_delegate.h" |
| 28 |
| 29 namespace { |
| 30 |
| 31 typedef ConstrainedWindowFrameSimple::HeaderViews HeaderViews; |
| 32 |
| 33 // A layout manager that lays out the header view with proper padding, |
| 34 // and sized to the widget's client view. |
| 35 class HeaderLayout : public views::LayoutManager { |
| 36 public: |
| 37 explicit HeaderLayout(views::Widget* container) : container_(container) {} |
| 38 virtual ~HeaderLayout() {} |
| 39 |
| 40 // Overridden from LayoutManager |
| 41 virtual void Layout(views::View* host); |
| 42 virtual gfx::Size GetPreferredSize(views::View* host); |
| 43 |
| 44 private: |
| 45 views::Widget* container_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(HeaderLayout); |
| 48 }; |
| 49 |
| 50 void HeaderLayout::Layout(views::View* host) { |
| 51 if (!host->has_children()) |
| 52 return; |
| 53 |
| 54 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 55 int vertical_padding = ConstrainedWindow::kVerticalPadding; |
| 56 int row_padding = ConstrainedWindow::kRowPadding; |
| 57 |
| 58 views::View* header = host->child_at(0); |
| 59 gfx::Size preferred_size = GetPreferredSize(host); |
| 60 int width = preferred_size.width() - 2 * horizontal_padding; |
| 61 int height = preferred_size.height() - vertical_padding - row_padding; |
| 62 |
| 63 header->SetBounds(horizontal_padding, vertical_padding, width, height); |
| 64 } |
| 65 |
| 66 gfx::Size HeaderLayout::GetPreferredSize(views::View* host) { |
| 67 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 68 int vertical_padding = ConstrainedWindow::kVerticalPadding; |
| 69 int row_padding = ConstrainedWindow::kRowPadding; |
| 70 |
| 71 views::View* header = host->child_at(0); |
| 72 views::View* client_view = container_->client_view(); |
| 73 |
| 74 gfx::Size header_size = header ? header->GetPreferredSize() : gfx::Size(); |
| 75 gfx::Size client_size = |
| 76 client_view ? client_view->GetPreferredSize() : gfx::Size(); |
| 77 int width = std::max(client_size.width(), header_size.width()) + |
| 78 2 * horizontal_padding; |
| 79 int height = vertical_padding + header_size.height() + row_padding; |
| 80 return gfx::Size(width, height); |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 ConstrainedWindowFrameSimple::HeaderViews::HeaderViews( |
| 86 views::View* header, |
| 87 views::Label* title_label, |
| 88 views::Button* close_button) |
| 89 : header(header), |
| 90 title_label(title_label), |
| 91 close_button(close_button) { |
| 92 DCHECK(header); |
| 93 } |
11 | 94 |
12 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( | 95 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( |
13 ConstrainedWindowViews* container) { | 96 ConstrainedWindowViews* container) |
14 // Draw a custom frame, ie NO frame. | 97 : container_(container) { |
15 container->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); | 98 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); |
| 99 |
| 100 layout_ = new HeaderLayout(container_); |
| 101 SetLayoutManager(layout_); |
| 102 |
| 103 SetHeaderView(CreateDefaultHeaderView()); |
| 104 |
| 105 set_background(views::Background::CreateSolidBackground( |
| 106 ConstrainedWindow::GetBackgroundColor())); |
| 107 } |
| 108 |
| 109 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { |
| 110 } |
| 111 |
| 112 void ConstrainedWindowFrameSimple::SetHeaderView(HeaderViews* header_views) |
| 113 { |
| 114 RemoveAllChildViews(true); |
| 115 |
| 116 header_views_.reset(header_views); |
| 117 |
| 118 AddChildView(header_views_->header); |
| 119 } |
| 120 |
| 121 HeaderViews* ConstrainedWindowFrameSimple::CreateDefaultHeaderView() { |
| 122 views::View* header_view = new views::View; |
| 123 |
| 124 views::GridLayout* grid_layout = new views::GridLayout(header_view); |
| 125 header_view->SetLayoutManager(grid_layout); |
| 126 |
| 127 views::ColumnSet* header_cs = grid_layout->AddColumnSet(0); |
| 128 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
| 129 views::GridLayout::USE_PREF, 0, 0); // Title. |
| 130 header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); |
| 131 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
| 132 views::GridLayout::USE_PREF, 0, 0); // Close Button. |
| 133 |
| 134 // Header row. |
| 135 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 136 grid_layout->StartRow(0, 0); |
| 137 views::Label* title_label = new views::Label(); |
| 138 title_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 139 title_label->SetFont(rb.GetFont(ConstrainedWindow::kTitleFontStyle)); |
| 140 title_label->SetEnabledColor(ConstrainedWindow::GetTextColor()); |
| 141 title_label->SetText(container_->widget_delegate()->GetWindowTitle()); |
| 142 grid_layout->AddView(title_label); |
| 143 |
| 144 views::Button* close_button = CreateCloseButton(); |
| 145 grid_layout->AddView(close_button); |
| 146 |
| 147 return new HeaderViews(header_view, title_label, close_button); |
16 } | 148 } |
17 | 149 |
18 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { | 150 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { |
19 return bounds(); | 151 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 152 int vertical_padding = ConstrainedWindow::kVerticalPadding; |
| 153 int row_padding = ConstrainedWindow::kRowPadding; |
| 154 gfx::Size header_size = |
| 155 header_views_->header ? |
| 156 header_views_->header->GetPreferredSize() : gfx::Size(); |
| 157 |
| 158 return gfx::Rect(horizontal_padding, |
| 159 vertical_padding + header_size.height() + row_padding, |
| 160 std::max(0, width() - 2 * horizontal_padding), |
| 161 std::max(0, (height() - 2 * vertical_padding - |
| 162 header_size.height() - row_padding))); |
20 } | 163 } |
21 | 164 |
22 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( | 165 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( |
23 const gfx::Rect& client_bounds) const { | 166 const gfx::Rect& client_bounds) const { |
24 return client_bounds; | 167 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 168 int vertical_padding = ConstrainedWindow::kVerticalPadding; |
| 169 int row_padding = ConstrainedWindow::kRowPadding; |
| 170 gfx::Size header_size = |
| 171 header_views_->header ? |
| 172 header_views_->header->GetPreferredSize() : gfx::Size(); |
| 173 |
| 174 int x = client_bounds.x() - horizontal_padding; |
| 175 int y = client_bounds.y() - vertical_padding - header_size.height() - |
| 176 row_padding; |
| 177 int width = client_bounds.width() + 2 * horizontal_padding; |
| 178 int height = client_bounds.height() + 2 * vertical_padding + |
| 179 header_size.height() + row_padding; |
| 180 |
| 181 return gfx::Rect(x, y, width, height); |
25 } | 182 } |
26 | 183 |
27 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { | 184 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { |
28 if (!bounds().Contains(point)) | 185 if (!bounds().Contains(point)) |
29 return HTNOWHERE; | 186 return HTNOWHERE; |
30 return HTCLIENT; | 187 return HTCLIENT; |
31 } | 188 } |
32 | 189 |
| 190 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, |
| 191 gfx::Path* window_mask) { |
| 192 SkRect rect = {0, 0, size.width() - 1, size.height() - 1}; |
| 193 SkScalar radius = SkIntToScalar(ConstrainedWindow::kBorderRadius); |
| 194 SkScalar radii[8] = {radius, radius, radius, radius, |
| 195 radius, radius, radius, radius}; |
| 196 |
| 197 // NB: We're not using the addRoundRect uniform radius overload as it |
| 198 // mishandles the bottom corners on Windows |
| 199 window_mask->addRoundRect(rect, radii); |
| 200 } |
| 201 |
| 202 void ConstrainedWindowFrameSimple::ResetWindowControls() { |
| 203 } |
| 204 |
| 205 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { |
| 206 } |
| 207 |
| 208 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { |
| 209 if (!header_views_->title_label) |
| 210 return; |
| 211 |
| 212 string16 text = container_->widget_delegate()->GetWindowTitle(); |
| 213 header_views_->title_label->SetText(text); |
| 214 } |
| 215 |
| 216 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { |
| 217 return container_->non_client_view()->GetWindowBoundsForClientBounds( |
| 218 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); |
| 219 } |
| 220 |
| 221 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, |
| 222 const ui::Event& event) { |
| 223 if (header_views_->close_button && sender == header_views_->close_button) |
| 224 sender->GetWidget()->Close(); |
| 225 } |
| 226 |
| 227 views::ImageButton* ConstrainedWindowFrameSimple::CreateCloseButton() { |
| 228 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 229 views::ImageButton* close_button = new views::ImageButton(this); |
| 230 close_button->SetImage(views::CustomButton::BS_NORMAL, |
| 231 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X)); |
| 232 close_button->SetImage(views::CustomButton::BS_HOT, |
| 233 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); |
| 234 close_button->SetImage(views::CustomButton::BS_PUSHED, |
| 235 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); |
| 236 return close_button; |
| 237 } |
OLD | NEW |