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 "ppapi/cpp/graphics_2d.h"
6 #include "ppapi/cpp/image_data.h"
7 #include "ppapi/cpp/input_event.h"
8 #include "ppapi/cpp/instance.h"
9 #include "ppapi/cpp/module.h"
10 #include "ppapi/cpp/mouse_cursor.h"
11 #include "ppapi/cpp/view.h"
13 void FillRect(pp::ImageData
* image
, int left
, int top
, int width
, int height
,
15 for (int y
= std::max(0, top
);
16 y
< std::min(image
->size().height() - 1, top
+ height
);
18 for (int x
= std::max(0, left
);
19 x
< std::min(image
->size().width() - 1, left
+ width
);
21 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
25 class MyInstance
: public pp::Instance
{
27 MyInstance(PP_Instance instance
)
28 : pp::Instance(instance
), width_(0), height_(0) {
29 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE
);
32 virtual ~MyInstance() {
35 virtual void DidChangeView(const pp::View
& view
) {
36 width_
= view
.GetRect().width();
37 height_
= view
.GetRect().height();
40 virtual bool HandleInputEvent(const pp::InputEvent
& event
) {
41 switch (event
.GetType()) {
42 case PP_INPUTEVENT_TYPE_MOUSEDOWN
:
44 case PP_INPUTEVENT_TYPE_MOUSEMOVE
:
45 HandleMove(pp::MouseInputEvent(event
));
47 case PP_INPUTEVENT_TYPE_KEYDOWN
:
54 void HandleMove(const pp::MouseInputEvent
& event
) {
55 pp::Point point
= event
.GetPosition();
57 if (point
.y() < height_
/ segments
) {
58 // Top part gets custom cursor of wait.
59 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_WAIT
);
60 } else if (point
.y() < (height_
/ segments
) * 2) {
61 // Next part gets custom image cursor.
62 pp::ImageData
cursor(this, pp::ImageData::GetNativeImageDataFormat(),
63 pp::Size(32, 32), true);
64 // Note that in real life you will need to handle the case where the
65 // native format is different.
66 FillRect(&cursor
, 0, 0, 32, 32, 0x80000080);
67 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_CUSTOM
, cursor
,
70 // Next part gets no cursor.
71 pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_NONE
);
81 class MyModule
: public pp::Module
{
83 MyModule() : pp::Module() {}
84 virtual ~MyModule() {}
86 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
87 return new MyInstance(instance
);
93 // Factory function for your specialization of the Module object.
94 Module
* CreateModule() {
95 return new MyModule();