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.
4
5#include "chrome/browser/ui/ntp_background_util.h"
6
7#include <cmath>
8
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"
17
18namespace {
19
20void PaintThemeBackground(
21    gfx::Canvas* canvas, gfx::ImageSkia* ntp_background, int tiling,
22    int alignment, const gfx::Rect& area, int tab_contents_height) {
23  int x_pos = 0;
24  int y_pos = 0;
25  int width = area.width() + ntp_background->width();
26  int height = area.height() + ntp_background->height();
27
28  if (alignment & ThemeProperties::ALIGN_BOTTOM) {
29    y_pos += area.height() + tab_contents_height - ntp_background->height();
30  } else if (alignment & ThemeProperties::ALIGN_TOP) {
31    // no op
32  } else {  // ALIGN_CENTER
33    y_pos += std::floor(area.height() + tab_contents_height / 2.0 -
34        ntp_background->height() / 2.0 + 0.5);
35  }
36
37  if (alignment & ThemeProperties::ALIGN_RIGHT) {
38    x_pos += area.width() - ntp_background->width();
39  } else if (alignment & ThemeProperties::ALIGN_LEFT) {
40    // no op
41  } else {  // ALIGN_CENTER
42    x_pos +=
43        std::floor(area.width() / 2.0 - ntp_background->width() / 2.0 + 0.5);
44  }
45
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();
51  }
52
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();
58  }
59
60  x_pos += area.x();
61  y_pos += area.y();
62
63  canvas->TileImageInt(*ntp_background, x_pos, y_pos, width, height);
64}
65
66}  // namespace
67
68// static
69void NtpBackgroundUtil::PaintBackgroundDetachedMode(ui::ThemeProvider* tp,
70                                                    gfx::Canvas* canvas,
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));
75
76  if (tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
77    int tiling = ThemeProperties::NO_REPEAT;
78    tp->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING, &tiling);
79    int alignment;
80    if (tp->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_ALIGNMENT,
81                               &alignment)) {
82      gfx::ImageSkia* ntp_background =
83          tp->GetImageSkiaNamed(IDR_THEME_NTP_BACKGROUND);
84
85      PaintThemeBackground(
86          canvas, ntp_background, tiling, alignment, area, tab_contents_height);
87    }
88  }
89}
90