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 "pdf/page_indicator.h"
6
7#include "base/logging.h"
8#include "base/strings/string_util.h"
9#include "pdf/draw_utils.h"
10#include "pdf/number_image_generator.h"
11#include "pdf/resource_consts.h"
12
13namespace chrome_pdf {
14
15
16PageIndicator::PageIndicator()
17    : current_page_(0),
18      fade_out_timer_id_(0),
19      splash_timeout_(kPageIndicatorSplashTimeoutMs),
20      fade_timeout_(kPageIndicatorScrollFadeTimeoutMs),
21      always_visible_(false) {
22}
23
24PageIndicator::~PageIndicator() {
25}
26
27bool PageIndicator::CreatePageIndicator(
28    uint32 id,
29    bool visible,
30    Control::Owner* delegate,
31    NumberImageGenerator* number_image_generator,
32    bool always_visible) {
33  number_image_generator_ = number_image_generator;
34  always_visible_ = always_visible;
35
36  pp::Rect rc;
37  bool res = Control::Create(id, rc, visible, delegate);
38  return res;
39}
40
41void PageIndicator::Configure(const pp::Point& origin,
42                              const pp::ImageData& background) {
43  background_ = background;
44  pp::Rect rc(origin, background_.size());
45  Control::SetRect(rc, false);
46}
47
48void PageIndicator::set_current_page(int current_page) {
49  if (current_page_ < 0)
50    return;
51
52  current_page_ = current_page;
53}
54
55void PageIndicator::Paint(pp::ImageData* image_data, const pp::Rect& rc) {
56  if (!visible())
57    return;
58
59  pp::Rect draw_rc = rc.Intersect(rect());
60  if (draw_rc.IsEmpty())
61    return;
62
63  // Copying the background image to a temporary buffer.
64  pp::ImageData buffer(owner()->GetInstance(), background_.format(),
65                       background_.size(), false);
66  CopyImage(background_, pp::Rect(background_.size()),
67            &buffer, pp::Rect(background_.size()), false);
68
69  // Creating the page number image.
70  pp::ImageData page_number_image;
71  number_image_generator_->GenerateImage(current_page_, &page_number_image);
72
73  pp::Point origin2(
74      (buffer.size().width() - page_number_image.size().width()) / 2.5,
75      (buffer.size().height() - page_number_image.size().height()) / 2);
76
77  // Drawing page number image on the buffer.
78  if (origin2.x() > 0 && origin2.y() > 0) {
79    CopyImage(page_number_image,
80              pp::Rect(pp::Point(), page_number_image.size()),
81              &buffer,
82              pp::Rect(origin2, page_number_image.size()),
83              false);
84  }
85
86  // Drawing the buffer.
87  pp::Point origin = draw_rc.point();
88  draw_rc.Offset(-rect().x(), -rect().y());
89  AlphaBlend(buffer, draw_rc, image_data, origin, transparency());
90}
91
92void PageIndicator::OnTimerFired(uint32 timer_id) {
93  FadingControl::OnTimerFired(timer_id);
94  if (timer_id == fade_out_timer_id_) {
95    Fade(false, fade_timeout_);
96  }
97}
98
99void PageIndicator::ResetFadeOutTimer() {
100  fade_out_timer_id_ =
101      owner()->ScheduleTimer(id(), splash_timeout_);
102}
103
104void PageIndicator::OnFadeInComplete() {
105  if (!always_visible_)
106    ResetFadeOutTimer();
107}
108
109void PageIndicator::Splash() {
110  Splash(kPageIndicatorSplashTimeoutMs, kPageIndicatorScrollFadeTimeoutMs);
111}
112
113void PageIndicator::Splash(uint32 splash_timeout, uint32 fade_timeout) {
114  splash_timeout_ = splash_timeout;
115  fade_timeout_ = fade_timeout;
116  if (!always_visible_)
117    fade_out_timer_id_ = 0;
118  Fade(true, fade_timeout_);
119}
120
121int PageIndicator::GetYPosition(
122    int vertical_scrollbar_y, int document_height, int plugin_height) {
123  double percent = static_cast<double>(vertical_scrollbar_y) / document_height;
124  return (plugin_height - rect().height()) * percent;
125}
126
127}  // namespace chrome_pdf
128