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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/ntp_background_util.h"
9 #include "base/logging.h"
10 #include "chrome/browser/themes/theme_properties.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/theme_provider.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/image/image_skia.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/skia_util.h"
20 void PaintThemeBackground(
21 gfx::Canvas
* canvas
, gfx::ImageSkia
* ntp_background
, int tiling
,
22 int alignment
, const gfx::Rect
& area
, int tab_contents_height
) {
25 int width
= area
.width() + ntp_background
->width();
26 int height
= area
.height() + ntp_background
->height();
28 if (alignment
& ThemeProperties::ALIGN_BOTTOM
) {
29 y_pos
+= area
.height() + tab_contents_height
- ntp_background
->height();
30 } else if (alignment
& ThemeProperties::ALIGN_TOP
) {
32 } else { // ALIGN_CENTER
33 y_pos
+= std::floor(area
.height() + tab_contents_height
/ 2.0 -
34 ntp_background
->height() / 2.0 + 0.5);
37 if (alignment
& ThemeProperties::ALIGN_RIGHT
) {
38 x_pos
+= area
.width() - ntp_background
->width();
39 } else if (alignment
& ThemeProperties::ALIGN_LEFT
) {
41 } else { // ALIGN_CENTER
43 std::floor(area
.width() / 2.0 - ntp_background
->width() / 2.0 + 0.5);
46 if (tiling
!= ThemeProperties::REPEAT
&&
47 tiling
!= ThemeProperties::REPEAT_X
) {
48 width
= ntp_background
->width();
49 } else if (x_pos
> 0) {
50 x_pos
= x_pos
% ntp_background
->width() - ntp_background
->width();
53 if (tiling
!= ThemeProperties::REPEAT
&&
54 tiling
!= ThemeProperties::REPEAT_Y
) {
55 height
= ntp_background
->height();
56 } else if (y_pos
> 0) {
57 y_pos
= y_pos
% ntp_background
->height() - ntp_background
->height();
63 canvas
->TileImageInt(*ntp_background
, x_pos
, y_pos
, width
, height
);
69 void NtpBackgroundUtil::PaintBackgroundDetachedMode(ui::ThemeProvider
* tp
,
71 const gfx::Rect
& area
,
72 int tab_contents_height
) {
73 // Draw the background to match the new tab page.
74 canvas
->FillRect(area
, tp
->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND
));
76 if (tp
->HasCustomImage(IDR_THEME_NTP_BACKGROUND
)) {
77 int tiling
= tp
->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING
);
78 int alignment
= tp
->GetDisplayProperty(
79 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
80 gfx::ImageSkia
* ntp_background
=
81 tp
->GetImageSkiaNamed(IDR_THEME_NTP_BACKGROUND
);
84 canvas
, ntp_background
, tiling
, alignment
, area
, tab_contents_height
);