blob: 1db9a8c8ab8e50c9205d4cbe4ce6d08e22dd3958 [file] [log] [blame]
Dan Sinclaircb876822018-03-22 16:21:04 +00001// Copyright 2018 The PDFium 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 "samples/pdfium_test_event_helper.h"
6
7#include <stdio.h>
8
9#include <string>
Henrique Nakashima08b68192018-06-26 15:28:28 +000010#include <vector>
Dan Sinclaircb876822018-03-22 16:21:04 +000011
Henrique Nakashima08b68192018-06-26 15:28:28 +000012#include "public/fpdf_fwlevent.h"
Dan Sinclaircb876822018-03-22 16:21:04 +000013#include "public/fpdfview.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000014#include "testing/fx_string_testhelpers.h"
Dan Sinclaircb876822018-03-22 16:21:04 +000015
Henrique Nakashima08b68192018-06-26 15:28:28 +000016namespace {
17void SendCharCodeEvent(FPDF_FORMHANDLE form,
18 FPDF_PAGE page,
19 const std::vector<std::string>& tokens) {
20 if (tokens.size() != 2) {
21 fprintf(stderr, "charcode: bad args\n");
22 return;
23 }
24
25 int keycode = atoi(tokens[1].c_str());
26 FORM_OnChar(form, page, keycode, 0);
27}
28
29void SendKeyCodeEvent(FPDF_FORMHANDLE form,
30 FPDF_PAGE page,
31 const std::vector<std::string>& tokens) {
32 if (tokens.size() != 2) {
33 fprintf(stderr, "keycode: bad args\n");
34 return;
35 }
36
37 int keycode = atoi(tokens[1].c_str());
38 FORM_OnKeyDown(form, page, keycode, 0);
39 FORM_OnKeyUp(form, page, keycode, 0);
40}
41
42uint32_t GetModifiers(std::string modifiers_string) {
43 int modifiers = 0;
44 if (modifiers_string.find("shift") != std::string::npos)
45 modifiers |= FWL_EVENTFLAG_ShiftKey;
46 if (modifiers_string.find("control") != std::string::npos)
47 modifiers |= FWL_EVENTFLAG_ControlKey;
48 if (modifiers_string.find("alt") != std::string::npos)
49 modifiers |= FWL_EVENTFLAG_AltKey;
50
51 return modifiers;
52}
53
54void SendMouseDownEvent(FPDF_FORMHANDLE form,
55 FPDF_PAGE page,
56 const std::vector<std::string>& tokens) {
57 if (tokens.size() != 4 && tokens.size() != 5) {
58 fprintf(stderr, "mousedown: bad args\n");
59 return;
60 }
61
62 int x = atoi(tokens[2].c_str());
63 int y = atoi(tokens[3].c_str());
64 uint32_t modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
65
66 if (tokens[1] == "left")
67 FORM_OnLButtonDown(form, page, modifiers, x, y);
68#ifdef PDF_ENABLE_XFA
69 else if (tokens[1] == "right")
70 FORM_OnRButtonDown(form, page, modifiers, x, y);
71#endif
72 else
73 fprintf(stderr, "mousedown: bad button name\n");
74}
75
76void SendMouseUpEvent(FPDF_FORMHANDLE form,
77 FPDF_PAGE page,
78 const std::vector<std::string>& tokens) {
79 if (tokens.size() != 4 && tokens.size() != 5) {
80 fprintf(stderr, "mouseup: bad args\n");
81 return;
82 }
83
84 int x = atoi(tokens[2].c_str());
85 int y = atoi(tokens[3].c_str());
86 int modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
87 if (tokens[1] == "left")
88 FORM_OnLButtonUp(form, page, modifiers, x, y);
89#ifdef PDF_ENABLE_XFA
90 else if (tokens[1] == "right")
91 FORM_OnRButtonUp(form, page, modifiers, x, y);
92#endif
93 else
94 fprintf(stderr, "mouseup: bad button name\n");
95}
96
97void SendMouseMoveEvent(FPDF_FORMHANDLE form,
98 FPDF_PAGE page,
99 const std::vector<std::string>& tokens) {
100 if (tokens.size() != 3) {
101 fprintf(stderr, "mousemove: bad args\n");
102 return;
103 }
104
105 int x = atoi(tokens[1].c_str());
106 int y = atoi(tokens[2].c_str());
107 FORM_OnMouseMove(form, page, 0, x, y);
108}
109
110void SendFocusEvent(FPDF_FORMHANDLE form,
111 FPDF_PAGE page,
112 const std::vector<std::string>& tokens) {
113 if (tokens.size() != 3) {
114 fprintf(stderr, "focus: bad args\n");
115 return;
116 }
117
118 int x = atoi(tokens[1].c_str());
119 int y = atoi(tokens[2].c_str());
120 FORM_OnFocus(form, page, 0, x, y);
121}
122} // namespace
123
Dan Sinclaircb876822018-03-22 16:21:04 +0000124void SendPageEvents(FPDF_FORMHANDLE form,
125 FPDF_PAGE page,
126 const std::string& events) {
127 auto lines = StringSplit(events, '\n');
128 for (auto line : lines) {
129 auto command = StringSplit(line, '#');
130 if (command[0].empty())
131 continue;
132 auto tokens = StringSplit(command[0], ',');
133 if (tokens[0] == "charcode") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000134 SendCharCodeEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000135 } else if (tokens[0] == "keycode") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000136 SendKeyCodeEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000137 } else if (tokens[0] == "mousedown") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000138 SendMouseDownEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000139 } else if (tokens[0] == "mouseup") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000140 SendMouseUpEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000141 } else if (tokens[0] == "mousemove") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000142 SendMouseMoveEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000143 } else if (tokens[0] == "focus") {
Henrique Nakashima08b68192018-06-26 15:28:28 +0000144 SendFocusEvent(form, page, tokens);
Dan Sinclaircb876822018-03-22 16:21:04 +0000145 } else {
146 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
147 }
148 }
149}