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 // paining (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);
52 pp::Rect draw_rc
= pp::Rect(image_data
->size()).Intersect(rect());
53 pp::Rect ctrl_rc
= pp::Rect(rect().point() - draw_rc
.point(), draw_rc
.size());
54 CopyImage(*image_data
, draw_rc
, &buffer
, ctrl_rc
, false);
56 // Temporary move control to origin (0,0) and draw it into temp buffer.
57 // Move to the original position afterward. Since this is going on temp
58 // buffer, we don't need to invalidate here.
59 pp::Rect temp
= rect();
60 MoveTo(pp::Point(0, 0), false);
61 Paint(&buffer
, ctrl_rc
);
62 MoveTo(temp
.point(), false);
64 std::list
<pp::Rect
>::const_iterator iter
;
65 for (iter
= rects
.begin(); iter
!= rects
.end(); ++iter
) {
66 pp::Rect draw_rc
= rect().Intersect(*iter
);
67 if (!draw_rc
.IsEmpty()) {
68 // Copy requested rect from the buffer image.
69 pp::Rect src_rc
= draw_rc
;
70 src_rc
.Offset(-rect().x(), -rect().y());
71 CopyImage(buffer
, src_rc
, image_data
, draw_rc
, false);
76 void Control::Show(bool visible
, bool invalidate
) {
77 if (visible_
!= visible
) {
80 owner_
->Invalidate(id_
, rc_
);
84 void Control::AdjustTransparency(uint8 transparency
, bool invalidate
) {
85 if (transparency_
!= transparency
) {
86 transparency_
= transparency
;
87 if (invalidate
&& visible_
)
88 owner_
->Invalidate(id_
, rc_
);
92 void Control::MoveBy(const pp::Point
& offset
, bool invalidate
) {
93 pp::Rect old_rc
= rc_
;
95 if (invalidate
&& visible_
) {
96 owner()->Invalidate(id(), old_rc
);
97 owner()->Invalidate(id(), rect());
101 void Control::SetRect(const pp::Rect
& rc
, bool invalidate
) {
102 pp::Rect old_rc
= rc_
;
104 if (invalidate
&& visible_
) {
105 owner()->Invalidate(id(), old_rc
);
106 owner()->Invalidate(id(), rect());
110 void Control::MoveTo(const pp::Point
& origin
, bool invalidate
) {
111 MoveBy(origin
- rc_
.point(), invalidate
);
114 } // namespace chrome_pdf