Home | History | Annotate | Download | only in pdf
      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/control.h"
      6 
      7 #include "base/logging.h"
      8 #include "pdf/draw_utils.h"
      9 
     10 namespace chrome_pdf {
     11 
     12 Control::Control()
     13     : id_(kInvalidControlId),
     14       visible_(false),
     15       owner_(NULL),
     16       transparency_(kOpaqueAlpha) {
     17 }
     18 
     19 Control::~Control() {
     20 }
     21 
     22 bool Control::Create(uint32 id, const pp::Rect& rc,
     23                      bool visible, Owner* owner) {
     24   DCHECK(owner);
     25   if (owner_ || id == kInvalidControlId)
     26     return false;  // Already created or id is invalid.
     27   id_ = id;
     28   rc_ = rc;
     29   visible_ = visible;
     30   owner_ = owner;
     31   return true;
     32 }
     33 
     34 bool Control::HandleEvent(const pp::InputEvent& event) {
     35   return false;
     36 }
     37 
     38 void Control::PaintMultipleRects(pp::ImageData* image_data,
     39                                  const std::list<pp::Rect>& rects) {
     40   DCHECK(rects.size() > 0);
     41   if (rects.size() == 1) {
     42     Paint(image_data, rects.front());
     43     return;
     44   }
     45 
     46   // Some rects in the input list may overlap. To prevent double
     47   // painting (causes problems with semi-transparent controls) we'll
     48   // paint control into buffer image data only once and copy requested
     49   // rectangles.
     50   pp::ImageData buffer(owner()->GetInstance(), image_data->format(),
     51                        rect().size(), false);
     52   if (buffer.is_null())
     53     return;
     54 
     55   pp::Rect draw_rc = pp::Rect(image_data->size()).Intersect(rect());
     56   pp::Rect ctrl_rc = pp::Rect(draw_rc.point() - rect().point(), draw_rc.size());
     57   CopyImage(*image_data, draw_rc, &buffer, ctrl_rc, false);
     58 
     59   // Temporary move control to origin (0,0) and draw it into temp buffer.
     60   // Move to the original position afterward. Since this is going on temp
     61   // buffer, we don't need to invalidate here.
     62   pp::Rect temp = rect();
     63   MoveTo(pp::Point(0, 0), false);
     64   Paint(&buffer, ctrl_rc);
     65   MoveTo(temp.point(), false);
     66 
     67   std::list<pp::Rect>::const_iterator iter;
     68   for (iter = rects.begin(); iter != rects.end(); ++iter) {
     69     pp::Rect draw_rc = rect().Intersect(*iter);
     70     if (!draw_rc.IsEmpty()) {
     71       // Copy requested rect from the buffer image.
     72       pp::Rect src_rc = draw_rc;
     73       src_rc.Offset(-rect().x(), -rect().y());
     74       CopyImage(buffer, src_rc, image_data, draw_rc, false);
     75     }
     76   }
     77 }
     78 
     79 void Control::Show(bool visible, bool invalidate) {
     80   if (visible_ != visible) {
     81     visible_ = visible;
     82     if (invalidate)
     83       owner_->Invalidate(id_, rc_);
     84   }
     85 }
     86 
     87 void Control::AdjustTransparency(uint8 transparency, bool invalidate) {
     88   if (transparency_ != transparency) {
     89     transparency_ = transparency;
     90     if (invalidate && visible_)
     91       owner_->Invalidate(id_, rc_);
     92   }
     93 }
     94 
     95 void Control::MoveBy(const pp::Point& offset, bool invalidate) {
     96   pp::Rect old_rc = rc_;
     97   rc_.Offset(offset);
     98   if (invalidate && visible_) {
     99     owner()->Invalidate(id(), old_rc);
    100     owner()->Invalidate(id(), rect());
    101   }
    102 }
    103 
    104 void Control::SetRect(const pp::Rect& rc, bool invalidate) {
    105   pp::Rect old_rc = rc_;
    106   rc_ = rc;
    107   if (invalidate && visible_) {
    108     owner()->Invalidate(id(), old_rc);
    109     owner()->Invalidate(id(), rect());
    110   }
    111 }
    112 
    113 void Control::MoveTo(const pp::Point& origin, bool invalidate) {
    114   MoveBy(origin - rc_.point(), invalidate);
    115 }
    116 
    117 }  // namespace chrome_pdf
    118