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.
7 #include "ppapi/cpp/graphics_2d.h"
8 #include "ppapi/cpp/image_data.h"
9 #include "ppapi/cpp/input_event.h"
10 #include "ppapi/cpp/instance.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/mouse_cursor.h"
13 #include "ppapi/cpp/view.h"
15 void FillRect(pp::ImageData
* image
, int left
, int top
, int width
, int height
,
17 for (int y
= std::max(0, top
);
18 y
< std::min(image
->size().height() - 1, top
+ height
);
20 for (int x
= std::max(0, left
);
21 x
< std::min(image
->size().width() - 1, left
+ width
);
23 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
27 class MyInstance
: public pp::Instance
{
29 MyInstance(PP_Instance instance
)
30 : pp::Instance(instance
), width_(0), height_(0) {
31 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE
);
34 virtual ~MyInstance() {
37 virtual void DidChangeView(const pp::View
& view
) {
38 width_
= view
.GetRect().width();
39 height_
= view
.GetRect().height();
42 virtual bool HandleInputEvent(const pp::InputEvent
& event
) {
43 switch (event
.GetType()) {
44 case PP_INPUTEVENT_TYPE_MOUSEDOWN
:
46 case PP_INPUTEVENT_TYPE_MOUSEMOVE
:
47 HandleMove(pp::MouseInputEvent(event
));
49 case PP_INPUTEVENT_TYPE_KEYDOWN
:
56 void HandleMove(const pp::MouseInputEvent
& event
) {
57 pp::Point point
= event
.GetPosition();
59 if (point
.y() < height_
/ segments
) {
60 // Top part gets custom cursor of wait.
61 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_WAIT
);
62 } else if (point
.y() < (height_
/ segments
) * 2) {
63 // Next part gets custom image cursor.
64 pp::ImageData
cursor(this, pp::ImageData::GetNativeImageDataFormat(),
65 pp::Size(32, 32), true);
66 // Note that in real life you will need to handle the case where the
67 // native format is different.
68 FillRect(&cursor
, 0, 0, 32, 32, 0x80000080);
69 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_CUSTOM
, cursor
,
72 // Next part gets no cursor.
73 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_NONE
);
83 class MyModule
: public pp::Module
{
85 MyModule() : pp::Module() {}
86 virtual ~MyModule() {}
88 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
89 return new MyInstance(instance
);
95 // Factory function for your specialization of the Module object.
96 Module
* CreateModule() {
97 return new MyModule();