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_constants.h" |
7 #include "chrome/browser/ui/constrained_window.h" | 8 #include "chrome/browser/ui/constrained_window.h" |
8 #include "chrome/browser/ui/constrained_window_constants.h" | |
9 #include "chrome/browser/ui/views/constrained_window_views.h" | 9 #include "chrome/browser/ui/views/constrained_window_views.h" |
10 #include "grit/ui_resources.h" | |
11 #include "grit/chromium_strings.h" | 10 #include "grit/chromium_strings.h" |
12 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
13 #include "grit/google_chrome_strings.h" | 12 #include "grit/google_chrome_strings.h" |
14 #include "grit/shared_resources.h" | 13 #include "grit/shared_resources.h" |
15 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
| 15 #include "grit/ui_resources.h" |
16 #include "ui/base/hit_test.h" | 16 #include "ui/base/hit_test.h" |
17 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/path.h" |
19 #include "ui/gfx/rect.h" | 20 #include "ui/gfx/rect.h" |
20 #include "ui/gfx/path.h" | |
21 #include "ui/views/background.h" | 21 #include "ui/views/background.h" |
| 22 #include "ui/views/controls/button/image_button.h" |
22 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
23 #include "ui/views/controls/button/image_button.h" | |
24 #include "ui/views/layout/grid_layout.h" | 24 #include "ui/views/layout/grid_layout.h" |
| 25 #include "ui/views/layout/layout_constants.h" |
25 #include "ui/views/layout/layout_manager.h" | 26 #include "ui/views/layout/layout_manager.h" |
26 #include "ui/views/layout/layout_constants.h" | 27 #include "ui/views/widget/widget_delegate.h" |
27 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
28 #include "ui/views/widget/widget_delegate.h" | |
29 | |
30 namespace { | |
31 | |
32 typedef ConstrainedWindowFrameSimple::HeaderViews HeaderViews; | |
33 | |
34 // A layout manager that lays out the header view with proper padding, | |
35 // and sized to the widget's client view. | |
36 class HeaderLayout : public views::LayoutManager { | |
37 public: | |
38 explicit HeaderLayout() {} | |
39 virtual ~HeaderLayout() {} | |
40 | |
41 // Overridden from LayoutManager | |
42 virtual void Layout(views::View* host); | |
43 virtual gfx::Size GetPreferredSize(views::View* host); | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(HeaderLayout); | |
46 }; | |
47 | |
48 void HeaderLayout::Layout(views::View* host) { | |
49 if (!host->has_children()) | |
50 return; | |
51 | |
52 int top_padding = ConstrainedWindowConstants::kCloseButtonPadding; | |
53 int left_padding = ConstrainedWindowConstants::kHorizontalPadding; | |
54 int right_padding = ConstrainedWindowConstants::kCloseButtonPadding; | |
55 | |
56 views::View* header = host->child_at(0); | |
57 gfx::Size preferred_size = GetPreferredSize(host); | |
58 int width = preferred_size.width() - left_padding - right_padding; | |
59 int height = preferred_size.height() - top_padding; | |
60 | |
61 header->SetBounds(left_padding, top_padding, width, height); | |
62 } | |
63 | |
64 gfx::Size HeaderLayout::GetPreferredSize(views::View* host) { | |
65 int top_padding = ConstrainedWindowConstants::kCloseButtonPadding; | |
66 int left_padding = ConstrainedWindowConstants::kHorizontalPadding; | |
67 int right_padding = ConstrainedWindowConstants::kCloseButtonPadding; | |
68 | |
69 views::View* header = host->child_at(0); | |
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; | |
74 | |
75 return gfx::Size(width, height); | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 ConstrainedWindowFrameSimple::HeaderViews::HeaderViews( | |
81 views::View* header, | |
82 views::Label* title_label, | |
83 views::Button* close_button) | |
84 : header(header), | |
85 title_label(title_label), | |
86 close_button(close_button) { | |
87 DCHECK(header); | |
88 } | |
89 | 29 |
90 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( | 30 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( |
91 ConstrainedWindowViews* container) | 31 ConstrainedWindowViews* container, |
92 : container_(container) { | 32 ConstrainedWindowViews::ChromeStyleClientInsets client_insets) |
| 33 : container_(container), |
| 34 title_label_( |
| 35 new views::Label(container->widget_delegate()->GetWindowTitle())), |
| 36 ALLOW_THIS_IN_INITIALIZER_LIST(close_button_( |
| 37 new views::ImageButton(this))) { |
93 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); | 38 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); |
94 | 39 |
95 layout_ = new HeaderLayout(); | 40 views::GridLayout* layout = new views::GridLayout(this); |
96 SetLayoutManager(layout_); | 41 const int kHeaderTopPadding = std::min( |
| 42 ConstrainedWindowConstants::kCloseButtonPadding, |
| 43 ConstrainedWindowConstants::kTitleTopPadding); |
| 44 layout->SetInsets(kHeaderTopPadding, |
| 45 ConstrainedWindowConstants::kHorizontalPadding, |
| 46 0, |
| 47 ConstrainedWindowConstants::kCloseButtonPadding); |
| 48 SetLayoutManager(layout); |
| 49 views::ColumnSet* cs = layout->AddColumnSet(0); |
| 50 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, 1, |
| 51 views::GridLayout::USE_PREF, 0, 0); // Title. |
| 52 cs->AddPaddingColumn(0, ConstrainedWindowConstants::kCloseButtonPadding); |
| 53 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, 0, |
| 54 views::GridLayout::USE_PREF, 0, 0); // Close Button. |
97 | 55 |
98 SetHeaderView(CreateDefaultHeaderView()); | 56 layout->StartRow(0, 0); |
| 57 |
| 58 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 59 title_label_->SetFont(rb.GetFont( |
| 60 ConstrainedWindowConstants::kTitleFontStyle)); |
| 61 title_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 62 title_label_->SetEnabledColor(ConstrainedWindow::GetTextColor()); |
| 63 title_label_->set_border(views::Border::CreateEmptyBorder( |
| 64 ConstrainedWindowConstants::kTitleTopPadding - kHeaderTopPadding, |
| 65 0, 0, 0)); |
| 66 layout->AddView(title_label_); |
| 67 |
| 68 close_button_->SetImage(views::CustomButton::BS_NORMAL, |
| 69 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X)); |
| 70 close_button_->SetImage(views::CustomButton::BS_HOT, |
| 71 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); |
| 72 close_button_->SetImage(views::CustomButton::BS_PUSHED, |
| 73 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_PRESSED)); |
| 74 close_button_->set_border(views::Border::CreateEmptyBorder( |
| 75 ConstrainedWindowConstants::kCloseButtonPadding - kHeaderTopPadding, |
| 76 0, 0, 0)); |
| 77 layout->AddView(close_button_); |
99 | 78 |
100 set_background(views::Background::CreateSolidBackground( | 79 set_background(views::Background::CreateSolidBackground( |
101 ConstrainedWindow::GetBackgroundColor())); | 80 ConstrainedWindow::GetBackgroundColor())); |
102 | 81 |
103 set_border(views::Border::CreateEmptyBorder( | 82 // Client insets have no relation to header insets: |
104 ConstrainedWindowConstants::kClientTopPadding, | 83 // - The client insets are the distance from the window border to the client |
105 ConstrainedWindowConstants::kHorizontalPadding, | 84 // view. |
106 ConstrainedWindowConstants::kClientBottomPadding, | 85 // - The header insets are the distance from the window border to the header |
107 ConstrainedWindowConstants::kHorizontalPadding)); | 86 // elements. |
| 87 // |
| 88 // The NO_ISNETS consumers draw atop the views above. |
| 89 if (client_insets == ConstrainedWindowViews::DEFAULT_INSETS) { |
| 90 const int kTitleBuiltinBottomPadding = 4; |
| 91 set_border(views::Border::CreateEmptyBorder( |
| 92 ConstrainedWindowConstants::kClientTopPadding + kHeaderTopPadding + |
| 93 std::max(close_button_->GetPreferredSize().height(), |
| 94 title_label_->GetPreferredSize().height()) - |
| 95 kTitleBuiltinBottomPadding, |
| 96 ConstrainedWindowConstants::kHorizontalPadding, |
| 97 ConstrainedWindowConstants::kClientBottomPadding, |
| 98 ConstrainedWindowConstants::kHorizontalPadding)); |
| 99 } |
108 } | 100 } |
109 | 101 |
110 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { | 102 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { |
111 } | 103 } |
112 | 104 |
113 void ConstrainedWindowFrameSimple::SetHeaderView(HeaderViews* header_views) | |
114 { | |
115 RemoveAllChildViews(true); | |
116 | |
117 header_views_.reset(header_views); | |
118 | |
119 AddChildView(header_views_->header); | |
120 } | |
121 | |
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 | |
129 views::View* header_view = new views::View; | |
130 | |
131 views::GridLayout* grid_layout = new views::GridLayout(header_view); | |
132 header_view->SetLayoutManager(grid_layout); | |
133 | |
134 views::ColumnSet* header_cs = grid_layout->AddColumnSet(0); | |
135 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
136 views::GridLayout::USE_PREF, 0, 0); // Title. | |
137 header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); | |
138 header_cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, | |
139 0, views::GridLayout::USE_PREF, 0, 0); // Close Button. | |
140 | |
141 // Header row. | |
142 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
143 grid_layout->StartRow(0, 0); | |
144 | |
145 views::Label* title_label = new views::Label(); | |
146 title_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
147 title_label->SetFont(rb.GetFont(ConstrainedWindowConstants::kTitleFontStyle)); | |
148 title_label->SetEnabledColor(ConstrainedWindow::GetTextColor()); | |
149 title_label->SetText(container_->widget_delegate()->GetWindowTitle()); | |
150 title_label->set_border(views::Border::CreateEmptyBorder(kTitleTopPadding, | |
151 kTitleLeftPadding, kTitleBottomPadding, kTitleRightPadding)); | |
152 grid_layout->AddView(title_label); | |
153 | |
154 views::Button* close_button = CreateCloseButton(); | |
155 grid_layout->AddView(close_button); | |
156 | |
157 return new HeaderViews(header_view, title_label, close_button); | |
158 } | |
159 | |
160 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { | 105 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { |
161 gfx::Rect bounds(GetContentsBounds()); | 106 return GetContentsBounds(); |
162 if (header_views_->header) | |
163 bounds.Inset(0, header_views_->header->GetPreferredSize().height(), 0, 0); | |
164 return bounds; | |
165 } | 107 } |
166 | 108 |
167 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( | 109 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( |
168 const gfx::Rect& client_bounds) const { | 110 const gfx::Rect& client_bounds) const { |
169 gfx::Rect bounds(client_bounds); | 111 gfx::Rect bounds(client_bounds); |
170 bounds.Inset(-GetInsets()); | 112 bounds.Inset(-GetInsets()); |
171 if (header_views_->header) | 113 bounds.set_width(std::max( |
172 bounds.Inset(0, -header_views_->header->GetPreferredSize().height(), 0, 0); | 114 bounds.width(), |
| 115 ConstrainedWindowConstants::kHorizontalPadding + |
| 116 2 * ConstrainedWindowConstants::kCloseButtonPadding + |
| 117 title_label_->GetPreferredSize().width() + |
| 118 close_button_->GetPreferredSize().width())); |
173 return bounds; | 119 return bounds; |
174 } | 120 } |
175 | 121 |
176 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { | 122 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { |
177 if (!bounds().Contains(point)) | 123 if (!bounds().Contains(point)) |
178 return HTNOWHERE; | 124 return HTNOWHERE; |
179 return HTCLIENT; | 125 return HTCLIENT; |
180 } | 126 } |
181 | 127 |
182 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, | 128 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, |
(...skipping 14 matching lines...) Expand all Loading... |
197 window_mask->addRoundRect(rect, radii); | 143 window_mask->addRoundRect(rect, radii); |
198 } | 144 } |
199 | 145 |
200 void ConstrainedWindowFrameSimple::ResetWindowControls() { | 146 void ConstrainedWindowFrameSimple::ResetWindowControls() { |
201 } | 147 } |
202 | 148 |
203 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { | 149 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { |
204 } | 150 } |
205 | 151 |
206 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { | 152 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { |
207 if (!header_views_->title_label) | 153 title_label_->SetText(container_->widget_delegate()->GetWindowTitle()); |
208 return; | |
209 | |
210 string16 text = container_->widget_delegate()->GetWindowTitle(); | |
211 header_views_->title_label->SetText(text); | |
212 } | 154 } |
213 | 155 |
214 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { | 156 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { |
215 return container_->non_client_view()->GetWindowBoundsForClientBounds( | 157 return GetWindowBoundsForClientBounds( |
216 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); | 158 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); |
217 } | 159 } |
218 | 160 |
219 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, | 161 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, |
220 const ui::Event& event) { | 162 const ui::Event& event) { |
221 if (header_views_->close_button && sender == header_views_->close_button) | 163 if (sender == close_button_) |
222 sender->GetWidget()->Close(); | 164 sender->GetWidget()->Close(); |
223 } | 165 } |
224 | |
225 views::ImageButton* ConstrainedWindowFrameSimple::CreateCloseButton() { | |
226 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
227 views::ImageButton* close_button = new views::ImageButton(this); | |
228 close_button->SetImage(views::CustomButton::BS_NORMAL, | |
229 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X)); | |
230 close_button->SetImage(views::CustomButton::BS_HOT, | |
231 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); | |
232 close_button->SetImage(views::CustomButton::BS_PUSHED, | |
233 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_PRESSED)); | |
234 return close_button; | |
235 } | |
OLD | NEW |