Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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/ntp_background_util.h"
      6 
      7 #include "base/logging.h"
      8 #include "chrome/browser/themes/theme_service.h"
      9 #include "grit/theme_resources.h"
     10 #include "third_party/skia/include/core/SkBitmap.h"
     11 #include "ui/gfx/canvas.h"
     12 #include "ui/gfx/rect.h"
     13 #include "ui/gfx/skia_util.h"
     14 
     15 namespace {
     16 
     17 void PaintThemeBackground(
     18     gfx::Canvas* canvas, SkBitmap* ntp_background, int tiling, int alignment,
     19     const gfx::Rect& area, int tab_contents_height) {
     20   int x_pos = 0;
     21   int y_pos = 0;
     22   int width = area.width() + ntp_background->width();
     23   int height = area.height() + ntp_background->height();
     24 
     25   if (alignment & ThemeService::ALIGN_BOTTOM)
     26     y_pos += area.height() + tab_contents_height - ntp_background->height();
     27 
     28   if (alignment & ThemeService::ALIGN_RIGHT) {
     29     x_pos += area.width() - ntp_background->width();
     30   } else if (alignment & ThemeService::ALIGN_LEFT) {
     31     // no op
     32   } else {  // ALIGN_CENTER
     33     x_pos += area.width() / 2 - ntp_background->width() / 2;
     34   }
     35 
     36   if (tiling != ThemeService::REPEAT &&
     37       tiling != ThemeService::REPEAT_X) {
     38     width = ntp_background->width();
     39   } else if (x_pos > 0) {
     40     x_pos = x_pos % ntp_background->width() - ntp_background->width();
     41   }
     42 
     43   if (tiling != ThemeService::REPEAT &&
     44       tiling != ThemeService::REPEAT_Y) {
     45     height = ntp_background->height();
     46   } else if (y_pos > 0) {
     47     y_pos = y_pos % ntp_background->height() - ntp_background->height();
     48   }
     49 
     50   x_pos += area.x();
     51   y_pos += area.y();
     52 
     53   canvas->TileImageInt(*ntp_background, x_pos, y_pos, width, height);
     54 }
     55 
     56 }  // namespace
     57 
     58 // static
     59 void NtpBackgroundUtil::PaintBackgroundDetachedMode(
     60     ui::ThemeProvider* tp, gfx::Canvas* canvas, const gfx::Rect& area,
     61     int tab_contents_height) {
     62   // Draw the background to match the new tab page.
     63   canvas->FillRectInt(tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND),
     64                       area.x(), area.y(), area.width(), area.height());
     65 
     66   if (tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
     67     int tiling = ThemeService::NO_REPEAT;
     68     tp->GetDisplayProperty(ThemeService::NTP_BACKGROUND_TILING,
     69                            &tiling);
     70     int alignment;
     71     if (tp->GetDisplayProperty(ThemeService::NTP_BACKGROUND_ALIGNMENT,
     72         &alignment)) {
     73       SkBitmap* ntp_background = tp->GetBitmapNamed(IDR_THEME_NTP_BACKGROUND);
     74 
     75       PaintThemeBackground(
     76           canvas, ntp_background, tiling, alignment, area, tab_contents_height);
     77     }
     78   }
     79 }
     80