OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_scrollbar.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "third_party/ppapi/c/ppp_scrollbar.h" |
| 10 #include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebScrollbar.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" |
| 14 #include "webkit/glue/plugins/pepper_event_conversion.h" |
| 15 #include "webkit/glue/plugins/pepper_image_data.h" |
| 16 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
| 17 #include "webkit/glue/plugins/pepper_plugin_module.h" |
| 18 #include "webkit/glue/webkit_glue.h" |
| 19 |
| 20 using WebKit::WebInputEvent; |
| 21 using WebKit::WebRect; |
| 22 using WebKit::WebScrollbar; |
| 23 |
| 24 namespace pepper { |
| 25 |
| 26 namespace { |
| 27 |
| 28 PP_Resource Create(PP_Instance instance_id, bool vertical) { |
| 29 PluginInstance* instance = PluginInstance::FromPPInstance(instance_id); |
| 30 if (!instance) |
| 31 return NULL; |
| 32 |
| 33 scoped_refptr<Scrollbar> scrollbar(new Scrollbar(instance, vertical)); |
| 34 scrollbar->AddRef(); // AddRef for the caller. |
| 35 |
| 36 return scrollbar->GetResource(); |
| 37 } |
| 38 |
| 39 bool IsScrollbar(PP_Resource resource) { |
| 40 return !!Resource::GetAs<Scrollbar>(resource).get(); |
| 41 } |
| 42 |
| 43 uint32_t GetThickness() { |
| 44 return WebScrollbar::defaultThickness(); |
| 45 } |
| 46 |
| 47 uint32_t GetValue(PP_Resource resource) { |
| 48 scoped_refptr<Scrollbar> scrollbar(Resource::GetAs<Scrollbar>(resource)); |
| 49 if (!scrollbar.get()) |
| 50 return 0; |
| 51 return scrollbar->GetValue(); |
| 52 } |
| 53 |
| 54 void SetValue(PP_Resource resource, uint32_t value) { |
| 55 scoped_refptr<Scrollbar> scrollbar(Resource::GetAs<Scrollbar>(resource)); |
| 56 if (scrollbar.get()) |
| 57 scrollbar->SetValue(value); |
| 58 } |
| 59 |
| 60 void SetDocumentSize(PP_Resource resource, uint32_t size) { |
| 61 scoped_refptr<Scrollbar> scrollbar(Resource::GetAs<Scrollbar>(resource)); |
| 62 if (scrollbar.get()) |
| 63 scrollbar->SetDocumentSize(size); |
| 64 } |
| 65 |
| 66 void SetTickMarks(PP_Resource resource, |
| 67 const PP_Rect* tick_marks, |
| 68 uint32_t count) { |
| 69 scoped_refptr<Scrollbar> scrollbar(Resource::GetAs<Scrollbar>(resource)); |
| 70 if (scrollbar.get()) |
| 71 scrollbar->SetTickMarks(tick_marks, count); |
| 72 } |
| 73 |
| 74 void ScrollBy(PP_Resource resource, |
| 75 PP_ScrollBy unit, |
| 76 int32_t multiplier) { |
| 77 scoped_refptr<Scrollbar> scrollbar(Resource::GetAs<Scrollbar>(resource)); |
| 78 if (scrollbar.get()) |
| 79 scrollbar->ScrollBy(unit, multiplier); |
| 80 } |
| 81 |
| 82 const PPB_Scrollbar ppb_scrollbar = { |
| 83 &Create, |
| 84 &IsScrollbar, |
| 85 &GetThickness, |
| 86 &GetValue, |
| 87 &SetValue, |
| 88 &SetDocumentSize, |
| 89 &SetTickMarks, |
| 90 &ScrollBy |
| 91 }; |
| 92 |
| 93 } // namespace |
| 94 |
| 95 Scrollbar::Scrollbar(PluginInstance* instance, bool vertical) |
| 96 : Widget(instance) { |
| 97 scrollbar_.reset(WebScrollbar::create( |
| 98 static_cast<WebKit::WebScrollbarClient*>(this), |
| 99 vertical ? WebScrollbar::Vertical : WebScrollbar::Horizontal)); |
| 100 } |
| 101 |
| 102 Scrollbar::~Scrollbar() { |
| 103 } |
| 104 |
| 105 // static |
| 106 const PPB_Scrollbar* Scrollbar::GetInterface() { |
| 107 return &ppb_scrollbar; |
| 108 } |
| 109 |
| 110 uint32_t Scrollbar::GetValue() { |
| 111 return scrollbar_->value(); |
| 112 } |
| 113 |
| 114 void Scrollbar::SetValue(uint32_t value) { |
| 115 scrollbar_->setValue(value); |
| 116 } |
| 117 |
| 118 void Scrollbar::SetDocumentSize(uint32_t size) { |
| 119 scrollbar_->setDocumentSize(size); |
| 120 } |
| 121 |
| 122 void Scrollbar::SetTickMarks(const PP_Rect* tick_marks, uint32_t count) { |
| 123 tickmarks_.resize(count); |
| 124 for (uint32 i = 0; i < count; ++i) { |
| 125 tickmarks_[i] = WebRect(tick_marks[i].point.x, |
| 126 tick_marks[i].point.y, |
| 127 tick_marks[i].size.width, |
| 128 tick_marks[i].size.height);; |
| 129 } |
| 130 Invalidate(&location()); |
| 131 } |
| 132 |
| 133 void Scrollbar::ScrollBy(PP_ScrollBy unit, int32_t multiplier) { |
| 134 WebScrollbar::ScrollDirection direction = multiplier >= 0 ? |
| 135 WebScrollbar::ScrollForward : WebScrollbar::ScrollBackward; |
| 136 float fmultiplier = 1.0; |
| 137 |
| 138 WebScrollbar::ScrollGranularity granularity; |
| 139 if (unit == PP_WIDGET_SCROLL_BY_LINE) { |
| 140 granularity = WebScrollbar::ScrollByLine; |
| 141 } else if (unit == PP_WIDGET_SCROLL_BY_PAGE) { |
| 142 granularity = WebScrollbar::ScrollByPage; |
| 143 } else if (unit == PP_WIDGET_SCROLL_BY_DOCUMENT) { |
| 144 granularity = WebScrollbar::ScrollByDocument; |
| 145 } else { |
| 146 granularity = WebScrollbar::ScrollByPixel; |
| 147 fmultiplier = static_cast<float>(multiplier); |
| 148 if (fmultiplier < 0) |
| 149 fmultiplier *= -1; |
| 150 } |
| 151 scrollbar_->scroll(direction, granularity, fmultiplier); |
| 152 } |
| 153 |
| 154 bool Scrollbar::Paint(const PP_Rect* rect, ImageData* image) { |
| 155 gfx::Rect gfx_rect(rect->point.x, |
| 156 rect->point.y, |
| 157 rect->size.width, |
| 158 rect->size.height); |
| 159 skia::PlatformCanvas* canvas = image->mapped_canvas(); |
| 160 if (!canvas) |
| 161 return false; |
| 162 scrollbar_->paint(webkit_glue::ToWebCanvas(canvas), gfx_rect); |
| 163 return true; |
| 164 } |
| 165 |
| 166 bool Scrollbar::HandleEvent(const PP_Event* event) { |
| 167 scoped_ptr<WebInputEvent> web_input_event(CreateWebInputEvent(*event)); |
| 168 if (!web_input_event.get()) |
| 169 return false; |
| 170 |
| 171 return scrollbar_->handleInputEvent(*web_input_event.get()); |
| 172 } |
| 173 |
| 174 void Scrollbar::SetLocationInternal(const PP_Rect* location) { |
| 175 scrollbar_->setLocation(WebRect(location->point.x, |
| 176 location->point.y, |
| 177 location->size.width, |
| 178 location->size.height)); |
| 179 } |
| 180 |
| 181 void Scrollbar::valueChanged(WebKit::WebScrollbar* scrollbar) { |
| 182 const PPP_Scrollbar* ppp_scrollbar = static_cast<const PPP_Scrollbar*>( |
| 183 module()->GetPluginInterface(PPP_SCROLLBAR_INTERFACE)); |
| 184 if (!ppp_scrollbar) |
| 185 return; |
| 186 ppp_scrollbar->ValueChanged( |
| 187 instance()->GetPPInstance(), GetResource(), scrollbar_->value()); |
| 188 } |
| 189 |
| 190 void Scrollbar::invalidateScrollbarRect(WebKit::WebScrollbar* scrollbar, |
| 191 const WebKit::WebRect& rect) { |
| 192 gfx::Rect gfx_rect(rect.x, |
| 193 rect.y, |
| 194 rect.width, |
| 195 rect.height); |
| 196 dirty_ = dirty_.Union(gfx_rect); |
| 197 // Can't call into the client to tell them about the invalidate right away, |
| 198 // since the Scrollbar code is still in the middle of updating its internal |
| 199 // state. |
| 200 MessageLoop::current()->PostTask( |
| 201 FROM_HERE, |
| 202 NewRunnableMethod(this, &Scrollbar::NotifyInvalidate)); |
| 203 } |
| 204 |
| 205 void Scrollbar::getTickmarks( |
| 206 WebKit::WebScrollbar* scrollbar, |
| 207 WebKit::WebVector<WebKit::WebRect>* tick_marks) const { |
| 208 if (tickmarks_.empty()) { |
| 209 WebRect* rects = NULL; |
| 210 tick_marks->assign(rects, 0); |
| 211 } else { |
| 212 tick_marks->assign(&tickmarks_[0], tickmarks_.size()); |
| 213 } |
| 214 } |
| 215 |
| 216 void Scrollbar::NotifyInvalidate() { |
| 217 if (dirty_.IsEmpty()) |
| 218 return; |
| 219 PP_Rect pp_rect; |
| 220 pp_rect.point.x = dirty_.x(); |
| 221 pp_rect.point.y = dirty_.y(); |
| 222 pp_rect.size.width = dirty_.width(); |
| 223 pp_rect.size.height = dirty_.height(); |
| 224 dirty_ = gfx::Rect(); |
| 225 Invalidate(&pp_rect); |
| 226 } |
| 227 |
| 228 } // namespace pepper |
OLD | NEW |