OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/views/detachable_toolbar_view.h" |
| 6 |
| 7 #include "app/gfx/canvas.h" |
| 8 #include "chrome/browser/browser_theme_provider.h" |
| 9 #include "grit/theme_resources.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 |
| 12 // How round the 'new tab' style bookmarks bar is. |
| 13 static const int kNewtabBarRoundness = 5; |
| 14 |
| 15 // static |
| 16 void DetachableToolbarView::PaintBackgroundDetachedMode(gfx::Canvas* canvas, |
| 17 views::View* view) { |
| 18 int browser_height = view->GetParent()->GetBounds( |
| 19 views::View::APPLY_MIRRORING_TRANSFORMATION).height(); |
| 20 |
| 21 // Draw the background to match the new tab page. |
| 22 ThemeProvider* tp = view->GetThemeProvider(); |
| 23 canvas->FillRectInt(tp->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND), |
| 24 0, 0, view->width(), view->height()); |
| 25 |
| 26 if (tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 27 int tiling = BrowserThemeProvider::NO_REPEAT; |
| 28 tp->GetDisplayProperty(BrowserThemeProvider::NTP_BACKGROUND_TILING, |
| 29 &tiling); |
| 30 int alignment; |
| 31 if (tp->GetDisplayProperty(BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, |
| 32 &alignment)) { |
| 33 SkBitmap* ntp_background = tp->GetBitmapNamed(IDR_THEME_NTP_BACKGROUND); |
| 34 |
| 35 if (alignment & BrowserThemeProvider::ALIGN_TOP) { |
| 36 PaintThemeBackgroundTopAligned( |
| 37 canvas, ntp_background, tiling, alignment, |
| 38 view->width(), view->height()); |
| 39 } else { |
| 40 PaintThemeBackgroundBottomAligned( |
| 41 canvas, ntp_background, tiling, alignment, |
| 42 view->width(), view->height(), browser_height); |
| 43 } |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 // static |
| 49 void DetachableToolbarView::PaintBackgroundAttachedMode(gfx::Canvas* canvas, |
| 50 views::View* view) { |
| 51 gfx::Rect bounds = |
| 52 view->GetBounds(views::View::APPLY_MIRRORING_TRANSFORMATION); |
| 53 |
| 54 ThemeProvider* tp = view->GetThemeProvider(); |
| 55 SkColor theme_toolbar_color = |
| 56 tp->GetColor(BrowserThemeProvider::COLOR_TOOLBAR); |
| 57 canvas->FillRectInt(theme_toolbar_color, 0, 0, |
| 58 view->width(), view->height()); |
| 59 |
| 60 canvas->TileImageInt(*tp->GetBitmapNamed(IDR_THEME_TOOLBAR), |
| 61 view->GetParent()->GetBounds( |
| 62 views::View::APPLY_MIRRORING_TRANSFORMATION).x() + bounds.x(), |
| 63 bounds.y(), 0, 0, view->width(), view->height()); |
| 64 } |
| 65 |
| 66 // static |
| 67 void DetachableToolbarView::CalculateContentArea( |
| 68 double animation_state, double horizontal_padding, |
| 69 double vertical_padding, SkRect* rect, |
| 70 double* roundness, views::View* view) { |
| 71 // The 0.5 is to correct for Skia's "draw on pixel boundaries"ness. |
| 72 rect->set(SkDoubleToScalar(horizontal_padding - 0.5), |
| 73 SkDoubleToScalar(vertical_padding - 0.5), |
| 74 SkDoubleToScalar(view->width() - horizontal_padding - 0.5), |
| 75 SkDoubleToScalar(view->height() - vertical_padding - 0.5)); |
| 76 |
| 77 *roundness = static_cast<double>(kNewtabBarRoundness) * animation_state; |
| 78 } |
| 79 |
| 80 // static |
| 81 void DetachableToolbarView::PaintHorizontalBorder(gfx::Canvas* canvas, |
| 82 DetachableToolbarView* view) { |
| 83 // Border can be at the top or at the bottom of the view depending on whether |
| 84 // the view (bar/shelf) is at the top/at the bottom and whether it is attached |
| 85 // or detached. |
| 86 int y = view->IsOnTop() == !view->IsDetached() ? view->height() - 1 : 0; |
| 87 canvas->FillRectInt(ResourceBundle::toolbar_separator_color, |
| 88 0, y, view->width(), 1); |
| 89 } |
| 90 |
| 91 // static |
| 92 void DetachableToolbarView::PaintContentAreaBackground( |
| 93 gfx::Canvas* canvas, ThemeProvider* theme_provider, |
| 94 const SkRect& rect, double roundness) { |
| 95 SkPaint paint; |
| 96 paint.setAntiAlias(true); |
| 97 paint.setColor(theme_provider->GetColor(BrowserThemeProvider::COLOR_TOOLBAR)); |
| 98 |
| 99 canvas->drawRoundRect( |
| 100 rect, SkDoubleToScalar(roundness), SkDoubleToScalar(roundness), paint); |
| 101 } |
| 102 |
| 103 // static |
| 104 void DetachableToolbarView::PaintContentAreaBorder( |
| 105 gfx::Canvas* canvas, ThemeProvider* theme_provider, |
| 106 const SkRect& rect, double roundness) { |
| 107 SkPaint border_paint; |
| 108 border_paint.setColor( |
| 109 theme_provider->GetColor(BrowserThemeProvider::COLOR_NTP_HEADER)); |
| 110 border_paint.setStyle(SkPaint::kStroke_Style); |
| 111 border_paint.setAlpha(96); |
| 112 border_paint.setAntiAlias(true); |
| 113 |
| 114 canvas->drawRoundRect(rect, |
| 115 SkDoubleToScalar(roundness), |
| 116 SkDoubleToScalar(roundness), border_paint); |
| 117 } |
| 118 |
| 119 // static |
| 120 void DetachableToolbarView::PaintVerticalDivider( |
| 121 gfx::Canvas* canvas, int x, int height, int vertical_padding, |
| 122 const SkColor& top_color, |
| 123 const SkColor& middle_color, |
| 124 const SkColor& bottom_color) { |
| 125 // Draw the upper half of the divider. |
| 126 SkPaint paint; |
| 127 paint.setShader(skia::CreateGradientShader(vertical_padding + 1, |
| 128 height / 2, |
| 129 top_color, |
| 130 middle_color))->safeUnref(); |
| 131 SkRect rc = { SkIntToScalar(x), |
| 132 SkIntToScalar(vertical_padding + 1), |
| 133 SkIntToScalar(x + 1), |
| 134 SkIntToScalar(height / 2) }; |
| 135 canvas->drawRect(rc, paint); |
| 136 |
| 137 // Draw the lower half of the divider. |
| 138 SkPaint paint_down; |
| 139 paint_down.setShader(skia::CreateGradientShader(height / 2, |
| 140 height - vertical_padding, |
| 141 middle_color, |
| 142 bottom_color))->safeUnref(); |
| 143 SkRect rc_down = { SkIntToScalar(x), |
| 144 SkIntToScalar(height / 2), |
| 145 SkIntToScalar(x + 1), |
| 146 SkIntToScalar(height - vertical_padding) }; |
| 147 canvas->drawRect(rc_down, paint_down); |
| 148 } |
| 149 |
| 150 // static |
| 151 void DetachableToolbarView::PaintThemeBackgroundTopAligned( |
| 152 gfx::Canvas* canvas, SkBitmap* ntp_background, int tiling, int alignment, |
| 153 int width, int height) { |
| 154 if (alignment & BrowserThemeProvider::ALIGN_LEFT) { |
| 155 if (tiling == BrowserThemeProvider::REPEAT) { |
| 156 canvas->TileImageInt(*ntp_background, 0, 0, width, height); |
| 157 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 158 canvas->TileImageInt(*ntp_background, 0, 0, width, |
| 159 ntp_background->height()); |
| 160 } else { |
| 161 canvas->TileImageInt(*ntp_background, 0, 0, |
| 162 ntp_background->width(), ntp_background->height()); |
| 163 } |
| 164 } else if (alignment & BrowserThemeProvider::ALIGN_RIGHT) { |
| 165 int x_pos = width % ntp_background->width() - ntp_background->width(); |
| 166 if (tiling == BrowserThemeProvider::REPEAT) { |
| 167 canvas->TileImageInt(*ntp_background, x_pos, 0, |
| 168 width + ntp_background->width(), height); |
| 169 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 170 canvas->TileImageInt(*ntp_background, x_pos, |
| 171 0, width + ntp_background->width(), ntp_background->height()); |
| 172 } else { |
| 173 canvas->TileImageInt(*ntp_background, width - ntp_background->width(), |
| 174 0, ntp_background->width(), ntp_background->height()); |
| 175 } |
| 176 } else { // ALIGN == CENTER |
| 177 int x_pos = width > ntp_background->width() ? |
| 178 ((width / 2 - ntp_background->width() / 2) % |
| 179 ntp_background->width()) - ntp_background->width() : |
| 180 width / 2 - ntp_background->width() / 2; |
| 181 if (tiling == BrowserThemeProvider::REPEAT) { |
| 182 canvas->TileImageInt(*ntp_background, x_pos, 0, |
| 183 width + ntp_background->width(), height); |
| 184 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 185 canvas->TileImageInt(*ntp_background, x_pos, 0, |
| 186 width + ntp_background->width(), |
| 187 ntp_background->height()); |
| 188 } else { |
| 189 canvas->TileImageInt(*ntp_background, |
| 190 width / 2 - ntp_background->width() / 2, |
| 191 0, ntp_background->width(), ntp_background->height()); |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 // static |
| 197 void DetachableToolbarView::PaintThemeBackgroundBottomAligned( |
| 198 gfx::Canvas* canvas, SkBitmap* ntp_background, int tiling, int alignment, |
| 199 int width, int height, int browser_height) { |
| 200 int border_width = 5; |
| 201 int y_pos = ((tiling == BrowserThemeProvider::REPEAT_X) || |
| 202 (tiling == BrowserThemeProvider::NO_REPEAT)) ? |
| 203 browser_height - ntp_background->height() - height - border_width : |
| 204 browser_height % ntp_background->height() - height - border_width - |
| 205 ntp_background->height(); |
| 206 |
| 207 if (alignment & BrowserThemeProvider::ALIGN_LEFT) { |
| 208 if (tiling == BrowserThemeProvider::REPEAT) { |
| 209 canvas->TileImageInt(*ntp_background, 0, y_pos, width, |
| 210 2 * height + ntp_background->height() + 5); |
| 211 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 212 canvas->TileImageInt(*ntp_background, 0, y_pos, width, |
| 213 ntp_background->height()); |
| 214 } else if (tiling == BrowserThemeProvider::REPEAT_Y) { |
| 215 canvas->TileImageInt(*ntp_background, 0, y_pos, |
| 216 ntp_background->width(), |
| 217 2 * height + ntp_background->height() + 5); |
| 218 } else { |
| 219 canvas->TileImageInt(*ntp_background, 0, y_pos, ntp_background->width(), |
| 220 ntp_background->height()); |
| 221 } |
| 222 } else if (alignment & BrowserThemeProvider::ALIGN_RIGHT) { |
| 223 int x_pos = width % ntp_background->width() - ntp_background->width(); |
| 224 if (tiling == BrowserThemeProvider::REPEAT) { |
| 225 canvas->TileImageInt(*ntp_background, x_pos, y_pos, |
| 226 width + ntp_background->width(), |
| 227 2 * height + ntp_background->height() + 5); |
| 228 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 229 canvas->TileImageInt(*ntp_background, x_pos, y_pos, |
| 230 width + ntp_background->width(), ntp_background->height()); |
| 231 } else if (tiling == BrowserThemeProvider::REPEAT_Y) { |
| 232 canvas->TileImageInt(*ntp_background, width - ntp_background->width(), |
| 233 y_pos, ntp_background->width(), |
| 234 2 * height + ntp_background->height() + 5); |
| 235 } else { |
| 236 canvas->TileImageInt(*ntp_background, width - ntp_background->width(), |
| 237 y_pos, ntp_background->width(), ntp_background->height()); |
| 238 } |
| 239 } else { // ALIGN == CENTER |
| 240 int x_pos = width > ntp_background->width() ? |
| 241 ((width / 2 - ntp_background->width() / 2) % |
| 242 ntp_background->width()) - ntp_background->width() : |
| 243 width / 2 - ntp_background->width() / 2; |
| 244 if (tiling == BrowserThemeProvider::REPEAT) { |
| 245 canvas->TileImageInt(*ntp_background, x_pos, y_pos, |
| 246 width + ntp_background->width(), |
| 247 2 * height + ntp_background->height() + 5); |
| 248 } else if (tiling == BrowserThemeProvider::REPEAT_X) { |
| 249 canvas->TileImageInt(*ntp_background, x_pos, y_pos, |
| 250 width + ntp_background->width(), ntp_background->height()); |
| 251 } else if (tiling == BrowserThemeProvider::REPEAT_Y) { |
| 252 canvas->TileImageInt(*ntp_background, |
| 253 width / 2 - ntp_background->width() / 2, |
| 254 y_pos, ntp_background->width(), |
| 255 2 * height + ntp_background->height() + 5); |
| 256 } else { |
| 257 canvas->TileImageInt(*ntp_background, |
| 258 width / 2 - ntp_background->width() / 2, |
| 259 y_pos, ntp_background->width(), ntp_background->height()); |
| 260 } |
| 261 } |
| 262 } |
OLD | NEW |