Chromium Code Reviews| 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 // A layout manager that lays out the header view with proper padding, | |
| 32 // and sized to the widget's client view. | |
| 33 class HeaderLayout : public views::LayoutManager { | |
| 34 public: | |
| 35 explicit HeaderLayout(views::Widget* container) : container_(container) {} | |
| 36 virtual ~HeaderLayout() {} | |
| 37 | |
| 38 // Overridden from LayoutManager | |
| 39 virtual void Layout(views::View* host); | |
| 40 virtual gfx::Size GetPreferredSize(views::View* host); | |
| 41 | |
| 42 private: | |
| 43 views::Widget* container_; | |
| 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 horizontal_padding = ConstrainedWindow::kHorizontalPadding; | |
| 53 int vertical_padding = ConstrainedWindow::kVerticalPadding; | |
| 54 int row_padding = ConstrainedWindow::kRowPadding; | |
| 55 | |
| 56 views::View* header = host->child_at(0); | |
| 57 gfx::Size preferred_size = GetPreferredSize(host); | |
| 58 int width = preferred_size.width() - 2 * horizontal_padding; | |
| 59 int height = preferred_size.height() - vertical_padding - row_padding; | |
| 60 | |
| 61 header->SetBounds(horizontal_padding, vertical_padding, width, height); | |
| 62 } | |
| 63 | |
| 64 gfx::Size HeaderLayout::GetPreferredSize(views::View* host) { | |
| 65 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; | |
| 66 int vertical_padding = ConstrainedWindow::kVerticalPadding; | |
| 67 int row_padding = ConstrainedWindow::kRowPadding; | |
| 68 | |
| 69 views::View* header = host->child_at(0); | |
| 70 views::View* client_view = container_->client_view(); | |
| 71 | |
| 72 gfx::Size header_size = header ? header->GetPreferredSize() : gfx::Size(); | |
| 73 gfx::Size client_size = | |
| 74 client_view ? client_view->GetPreferredSize() : gfx::Size(); | |
| 75 int width = std::max(client_size.width(), header_size.width()) + | |
| 76 2 * horizontal_padding; | |
| 77 int height = vertical_padding + header_size.height() + row_padding; | |
| 78 return gfx::Size(width, height); | |
| 79 } | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 11 | 83 |
| 12 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( | 84 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( |
| 13 ConstrainedWindowViews* container) { | 85 ConstrainedWindowViews* container) |
| 14 // Draw a custom frame, ie NO frame. | 86 : container_(container), |
| 15 container->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); | 87 header_view_(NULL) { |
| 88 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); | |
| 89 | |
| 90 layout_ = new HeaderLayout(container_); | |
| 91 SetLayoutManager(layout_); | |
| 92 | |
| 93 SetHeaderView(CreateDefaultHeaderView()); | |
| 94 | |
| 95 set_background(views::Background::CreateSolidBackground( | |
| 96 ConstrainedWindow::GetBackgroundColor())); | |
| 97 } | |
| 98 | |
| 99 void ConstrainedWindowFrameSimple::SetHeaderView(views::View* view) | |
| 100 { | |
| 101 RemoveAllChildViews(true); | |
| 102 | |
| 103 header_view_ = view; | |
| 104 | |
| 105 AddChildView(header_view_); | |
| 106 } | |
| 107 | |
| 108 views::View* ConstrainedWindowFrameSimple::CreateDefaultHeaderView() { | |
| 109 views::View* header_view = new views::View; | |
| 110 | |
| 111 views::GridLayout* grid_layout = new views::GridLayout(header_view); | |
| 112 header_view->SetLayoutManager(grid_layout); | |
| 113 | |
| 114 views::ColumnSet* header_cs = grid_layout->AddColumnSet(0); | |
| 115 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
| 116 views::GridLayout::USE_PREF, 0, 0); // Title. | |
| 117 header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); | |
| 118 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
| 119 views::GridLayout::USE_PREF, 0, 0); // Close Button. | |
| 120 | |
| 121 // Header row. | |
| 122 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 123 grid_layout->StartRow(0, 0); | |
| 124 views::Label* title_label = new views::Label(); | |
| 125 title_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 126 title_label->SetFont(rb.GetFont(ConstrainedWindow::kTitleFontStyle)); | |
| 127 title_label->SetEnabledColor(ConstrainedWindow::GetTextColor()); | |
| 128 title_label->SetText(container_->widget_delegate()->GetWindowTitle()); | |
| 129 grid_layout->AddView(title_label); | |
| 130 | |
| 131 grid_layout->AddView(CreateCloseButton()); | |
| 132 | |
| 133 return header_view; | |
| 16 } | 134 } |
| 17 | 135 |
| 18 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { | 136 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { |
| 19 return bounds(); | 137 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 138 int vertical_padding = ConstrainedWindow::kVerticalPadding; | |
| 139 int row_padding = ConstrainedWindow::kRowPadding; | |
| 140 gfx::Size header_size = | |
| 141 header_view_ ? header_view_->GetPreferredSize() : gfx::Size(); | |
| 142 | |
| 143 return gfx::Rect(horizontal_padding, | |
| 144 vertical_padding + header_size.height() + row_padding, | |
| 145 std::max(0, width() - 2 * horizontal_padding), | |
| 146 std::max(0, (height() - 2 * vertical_padding - | |
| 147 header_size.height() - row_padding))); | |
| 20 } | 148 } |
| 21 | 149 |
| 22 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( | 150 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( |
| 23 const gfx::Rect& client_bounds) const { | 151 const gfx::Rect& client_bounds) const { |
| 24 return client_bounds; | 152 int horizontal_padding = ConstrainedWindow::kHorizontalPadding; |
| 153 int vertical_padding = ConstrainedWindow::kVerticalPadding; | |
| 154 int row_padding = ConstrainedWindow::kRowPadding; | |
| 155 gfx::Size header_size = | |
| 156 header_view_ ? header_view_->GetPreferredSize() : gfx::Size(); | |
| 157 | |
| 158 int x = client_bounds.x() - horizontal_padding; | |
| 159 int y = client_bounds.y() - vertical_padding - header_size.height() - | |
| 160 row_padding; | |
| 161 int width = client_bounds.width() + 2 * horizontal_padding; | |
| 162 int height = client_bounds.height() + 2 * vertical_padding + | |
| 163 header_size.height() + row_padding; | |
| 164 | |
| 165 return gfx::Rect(x, y, width, height); | |
| 25 } | 166 } |
| 26 | 167 |
| 27 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { | 168 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { |
| 28 if (!bounds().Contains(point)) | 169 if (!bounds().Contains(point)) |
| 29 return HTNOWHERE; | 170 return HTNOWHERE; |
| 30 return HTCLIENT; | 171 return HTCLIENT; |
| 31 } | 172 } |
| 32 | 173 |
| 174 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, | |
| 175 gfx::Path* window_mask) { | |
| 176 SkRect rect = {0, 0, size.width() - 1, size.height() - 1}; | |
| 177 SkScalar radius = ConstrainedWindow::kBorderRadius; | |
| 178 SkScalar radii[8] = {radius, radius, radius, radius, | |
| 179 radius, radius, radius, radius}; | |
| 180 | |
| 181 // NB: We're not using the addRoundRect uniform radius overload as it | |
| 182 // mishandles the bottom corners on Windows | |
| 183 window_mask->addRoundRect(rect, radii); | |
| 184 } | |
| 185 | |
| 186 void ConstrainedWindowFrameSimple::ResetWindowControls() { | |
| 187 } | |
| 188 | |
| 189 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { | |
| 190 } | |
| 191 | |
| 192 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { | |
| 193 return container_->non_client_view()->GetWindowBoundsForClientBounds( | |
| 194 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); | |
| 195 } | |
| 196 | |
| 197 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, | |
| 198 const ui::Event& event) { | |
| 199 switch (sender->tag()) { | |
|
Ben Goodger (Google)
2012/09/27 16:16:12
then in here you can do:
DCHECK_EQ(close_button_,
Mike Wittman
2012/09/28 22:46:09
This window allows a configurable header View (to
| |
| 200 case CLOSE_WIDGET: | |
| 201 sender->GetWidget()->Close(); | |
| 202 break; | |
| 203 | |
| 204 default: | |
| 205 break; | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 views::ImageButton* ConstrainedWindowFrameSimple::CreateCloseButton() { | |
| 210 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 211 views::ImageButton* close_button = new views::ImageButton(this); | |
| 212 close_button->SetImage(views::CustomButton::BS_NORMAL, | |
| 213 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X)); | |
| 214 close_button->SetImage(views::CustomButton::BS_HOT, | |
| 215 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); | |
| 216 close_button->SetImage(views::CustomButton::BS_PUSHED, | |
| 217 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); | |
| 218 close_button->set_tag(CLOSE_WIDGET); | |
| 219 return close_button; | |
| 220 } | |
| OLD | NEW |