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