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/control.h"
7 #include "base/logging.h"
8 #include "pdf/draw_utils.h"
10 namespace chrome_pdf
{
13 : id_(kInvalidControlId
),
16 transparency_(kOpaqueAlpha
) {
22 bool Control::Create(uint32 id
, const pp::Rect
& rc
,
23 bool visible
, Owner
* owner
) {
25 if (owner_
|| id
== kInvalidControlId
)
26 return false; // Already created or id is invalid.
34 bool Control::HandleEvent(const pp::InputEvent
& event
) {
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());
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
50 pp::ImageData
buffer(owner()->GetInstance(), image_data
->format(),
51 rect().size(), false);
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);
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);
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);
79 void Control::Show(bool visible
, bool invalidate
) {
80 if (visible_
!= visible
) {
83 owner_
->Invalidate(id_
, rc_
);
87 void Control::AdjustTransparency(uint8 transparency
, bool invalidate
) {
88 if (transparency_
!= transparency
) {
89 transparency_
= transparency
;
90 if (invalidate
&& visible_
)
91 owner_
->Invalidate(id_
, rc_
);
95 void Control::MoveBy(const pp::Point
& offset
, bool invalidate
) {
96 pp::Rect old_rc
= rc_
;
98 if (invalidate
&& visible_
) {
99 owner()->Invalidate(id(), old_rc
);
100 owner()->Invalidate(id(), rect());
104 void Control::SetRect(const pp::Rect
& rc
, bool invalidate
) {
105 pp::Rect old_rc
= rc_
;
107 if (invalidate
&& visible_
) {
108 owner()->Invalidate(id(), old_rc
);
109 owner()->Invalidate(id(), rect());
113 void Control::MoveTo(const pp::Point
& origin
, bool invalidate
) {
114 MoveBy(origin
- rc_
.point(), invalidate
);
117 } // namespace chrome_pdf