Roll src/third_party/WebKit 4e80045:b43d4aa (svn 187778:187783)
[chromium-blink-merge.git] / pdf / page_indicator.cc
blobc4af1e07b74133f9fdf38c0abad4d5be81c33eb2
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 "pdf/page_indicator.h"
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"
13 namespace chrome_pdf {
16 PageIndicator::PageIndicator()
17 : current_page_(0),
18 fade_out_timer_id_(0),
19 splash_timeout_(kPageIndicatorSplashTimeoutMs),
20 fade_timeout_(kPageIndicatorScrollFadeTimeoutMs),
21 always_visible_(false) {
24 PageIndicator::~PageIndicator() {
27 bool 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;
36 pp::Rect rc;
37 bool res = Control::Create(id, rc, visible, delegate);
38 return res;
41 void 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);
48 void PageIndicator::set_current_page(int current_page) {
49 if (current_page_ < 0)
50 return;
52 current_page_ = current_page;
55 void PageIndicator::Paint(pp::ImageData* image_data, const pp::Rect& rc) {
56 if (!visible())
57 return;
59 pp::Rect draw_rc = rc.Intersect(rect());
60 if (draw_rc.IsEmpty())
61 return;
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);
69 // Creating the page number image.
70 pp::ImageData page_number_image;
71 number_image_generator_->GenerateImage(current_page_, &page_number_image);
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);
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);
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());
92 void PageIndicator::OnTimerFired(uint32 timer_id) {
93 FadingControl::OnTimerFired(timer_id);
94 if (timer_id == fade_out_timer_id_) {
95 Fade(false, fade_timeout_);
99 void PageIndicator::ResetFadeOutTimer() {
100 fade_out_timer_id_ =
101 owner()->ScheduleTimer(id(), splash_timeout_);
104 void PageIndicator::OnFadeInComplete() {
105 if (!always_visible_)
106 ResetFadeOutTimer();
109 void PageIndicator::Splash() {
110 Splash(kPageIndicatorSplashTimeoutMs, kPageIndicatorScrollFadeTimeoutMs);
113 void 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_);
121 int 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;
127 } // namespace chrome_pdf