1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 package org.libreoffice.kit;
11 
12 import java.nio.ByteBuffer;
13 
14 public class Document {
15     public static final int PART_MODE_SLIDE = 0;
16     public static final int PART_MODE_NOTES = 1;
17 
18     /**
19      * Document types
20      */
21     public static final int DOCTYPE_TEXT = 0;
22     public static final int DOCTYPE_SPREADSHEET = 1;
23     public static final int DOCTYPE_PRESENTATION = 2;
24     public static final int DOCTYPE_DRAWING = 3;
25     public static final int DOCTYPE_OTHER = 4;
26 
27     /**
28      * Mouse event types
29      */
30     public static final int MOUSE_EVENT_BUTTON_DOWN = 0;
31     public static final int MOUSE_EVENT_BUTTON_UP = 1;
32     public static final int MOUSE_EVENT_MOVE = 2;
33 
34     /**
35      * Key event types
36      */
37     public static final int KEY_EVENT_PRESS = 0;
38     public static final int KEY_EVENT_RELEASE = 1;
39 
40     /**
41      * State change types
42      */
43     public static final int BOLD = 0;
44     public static final int ITALIC = 1;
45     public static final int UNDERLINE = 2;
46     public static final int STRIKEOUT = 3;
47 
48     public static final int ALIGN_LEFT= 4;
49     public static final int ALIGN_CENTER = 5;
50     public static final int ALIGN_RIGHT= 6;
51     public static final int ALIGN_JUSTIFY= 7;
52     public static final int NUMBERED_LIST= 8;
53     public static final int BULLET_LIST= 9;
54 
55     /**
56      * Callback message types
57      * Refer to https://opengrok.libreoffice.org/xref/core/include/LibreOfficeKit/LibreOfficeKitEnums.h
58      * for more details about each callback.
59      */
60     public static final int CALLBACK_INVALIDATE_TILES = 0;
61     public static final int CALLBACK_INVALIDATE_VISIBLE_CURSOR = 1;
62     public static final int CALLBACK_TEXT_SELECTION = 2;
63     public static final int CALLBACK_TEXT_SELECTION_START = 3;
64     public static final int CALLBACK_TEXT_SELECTION_END = 4;
65     public static final int CALLBACK_CURSOR_VISIBLE = 5;
66     public static final int CALLBACK_GRAPHIC_SELECTION = 6;
67     public static final int CALLBACK_HYPERLINK_CLICKED = 7;
68     public static final int CALLBACK_STATE_CHANGED = 8;
69     public static final int CALLBACK_STATUS_INDICATOR_START = 9;
70     public static final int CALLBACK_STATUS_INDICATOR_SET_VALUE = 10;
71     public static final int CALLBACK_STATUS_INDICATOR_FINISH = 11;
72     public static final int CALLBACK_SEARCH_NOT_FOUND = 12;
73     public static final int CALLBACK_DOCUMENT_SIZE_CHANGED = 13;
74     public static final int CALLBACK_SET_PART = 14;
75     public static final int CALLBACK_SEARCH_RESULT_SELECTION = 15;
76     public static final int CALLBACK_UNO_COMMAND_RESULT = 16;
77     public static final int CALLBACK_CELL_CURSOR = 17;
78     public static final int CALLBACK_MOUSE_POINTER = 18;
79     public static final int CALLBACK_CELL_FORMULA = 19;
80     public static final int CALLBACK_DOCUMENT_PASSWORD = 20;
81     public static final int CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY = 21;
82     public static final int CALLBACK_ERROR = 22;
83     public static final int CALLBACK_CONTEXT_MENU = 23;
84     public static final int CALLBACK_INVALIDATE_VIEW_CURSOR = 24;
85     public static final int CALLBACK_TEXT_VIEW_SELECTION = 25;
86     public static final int CALLBACK_CELL_VIEW_CURSOR = 26;
87     public static final int CALLBACK_GRAPHIC_VIEW_SELECTION = 27;
88     public static final int CALLBACK_VIEW_CURSOR_VISIBLE = 28;
89     public static final int CALLBACK_VIEW_LOCK = 29;
90     public static final int CALLBACK_REDLINE_TABLE_SIZE_CHANGED = 30;
91     public static final int CALLBACK_REDLINE_TABLE_ENTRY_MODIFIED = 31;
92     public static final int CALLBACK_COMMENT = 32;
93     public static final int CALLBACK_INVALIDATE_HEADER = 33;
94     public static final int CALLBACK_CELL_ADDRESS = 34;
95 
96     /**
97      * Set text selection types
98      */
99     public static final int SET_TEXT_SELECTION_START = 0;
100     public static final int SET_TEXT_SELECTION_END = 1;
101     public static final int SET_TEXT_SELECTION_RESET = 2;
102 
103     /**
104      * Set graphic selection types
105      */
106     public static final int SET_GRAPHIC_SELECTION_START = 0;
107     public static final int SET_GRAPHIC_SELECTION_END = 1;
108 
109     /**
110      * Mouse button type
111      */
112     public static final int MOUSE_BUTTON_LEFT = 1;
113     public static final int MOUSE_BUTTON_MIDDLE = 2;
114     public static final int MOUSE_BUTTON_RIGHT = 4;
115 
116     public static final int KEYBOARD_MODIFIER_NONE = 0x0000;
117     public static final int KEYBOARD_MODIFIER_SHIFT = 0x1000;
118     public static final int KEYBOARD_MODIFIER_MOD1 = 0x2000;
119     public static final int KEYBOARD_MODIFIER_MOD2 = 0x4000;
120     public static final int KEYBOARD_MODIFIER_MOD3 = 0x8000;
121 
122     /** Optional features of LibreOfficeKit, in particular callbacks that block
123      *  LibreOfficeKit until the corresponding reply is received, which would
124      *  deadlock if the client does not support the feature.
125      */
126     public static final long LOK_FEATURE_DOCUMENT_PASSWORD = 1;
127     public static final long LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY = (1 << 1);
128     public static final long LOK_FEATURE_PART_IN_INVALIDATION_CALLBACK = (1 << 2);
129     public static final long LOK_FEATURE_NO_TILED_ANNOTATIONS = (1 << 3);
130 
131     private final ByteBuffer handle;
132     private MessageCallback messageCallback = null;
133 
Document(ByteBuffer handle)134     public Document(ByteBuffer handle) {
135         this.handle = handle;
136         bindMessageCallback();
137     }
138 
setMessageCallback(MessageCallback messageCallback)139     public void setMessageCallback(MessageCallback messageCallback) {
140         this.messageCallback = messageCallback;
141     }
142 
143     /**
144      * Callback triggered through JNI to indicate that a new signal
145      * from LibreOfficeKit was retrieved.
146      */
messageRetrieved(int signalNumber, String payload)147     private void messageRetrieved(int signalNumber, String payload) {
148         if (messageCallback != null) {
149             messageCallback.messageRetrieved(signalNumber, payload);
150         }
151     }
152 
153     /**
154      * Bind the signal callback in LOK.
155      */
bindMessageCallback()156     private native void bindMessageCallback();
157 
destroy()158     public native void destroy();
159 
getPart()160     public native int getPart();
161 
setPart(int partIndex)162     public native void setPart(int partIndex);
163 
getParts()164     public native int getParts();
165 
getPartName(int partIndex)166     public native String getPartName(int partIndex);
167 
setPartMode(int partMode)168     public native void setPartMode(int partMode);
169 
getPartPageRectangles()170     public native String getPartPageRectangles();
171 
getDocumentHeight()172     public native long getDocumentHeight();
173 
getDocumentWidth()174     public native long getDocumentWidth();
175 
getDocumentTypeNative()176     private native int getDocumentTypeNative();
177 
setClientZoom(int nTilePixelWidth, int nTilePixelHeight, int nTileTwipWidth, int nTileTwipHeight)178     public native void setClientZoom(int nTilePixelWidth, int nTilePixelHeight, int nTileTwipWidth, int nTileTwipHeight);
179 
saveAs(String url, String format, String options)180     public native void saveAs(String url, String format, String options);
181 
paintTileNative(ByteBuffer buffer, int canvasWidth, int canvasHeight, int tilePositionX, int tilePositionY, int tileWidth, int tileHeight)182     private native void paintTileNative(ByteBuffer buffer, int canvasWidth, int canvasHeight, int tilePositionX, int tilePositionY, int tileWidth, int tileHeight);
183 
getDocumentType()184     public int getDocumentType() {
185         return getDocumentTypeNative();
186     }
187 
paintTile(ByteBuffer buffer, int canvasWidth, int canvasHeight, int tilePositionX, int tilePositionY, int tileWidth, int tileHeight)188     public void paintTile(ByteBuffer buffer, int canvasWidth, int canvasHeight, int tilePositionX, int tilePositionY, int tileWidth, int tileHeight) {
189         paintTileNative(buffer, canvasWidth, canvasHeight, tilePositionX, tilePositionY, tileWidth, tileHeight);
190     }
191 
initializeForRendering()192     public native void initializeForRendering();
193 
194     /**
195      * Post a key event to LibreOffice.
196      * @param type - type of key event
197      * @param charCode - the Unicode character generated by this event or 0.
198      * @param keyCode - the integer code representing the key of the event (non-zero for control keys).
199      */
postKeyEvent(int type, int charCode, int keyCode)200     public native void postKeyEvent(int type, int charCode, int keyCode);
201 
202     /**
203      * Post a mouse event to LOK
204      * @param type - mouse event type
205      * @param x - x coordinate
206      * @param y - y coordinate
207      * @param count - number of events
208      */
postMouseEvent(int type, int x, int y, int count, int button, int modifier)209     public native void postMouseEvent(int type, int x, int y, int count, int button, int modifier);
210 
211     /**
212      * Post a .uno: command to LOK
213      * @param command - the command, like ".uno:Bold"
214      * @param arguments
215      */
postUnoCommand(String command, String arguments, boolean notifyWhenFinished)216     public native void postUnoCommand(String command, String arguments, boolean notifyWhenFinished);
217 
218     /**
219      * Change text selection.
220      * @param type - text selection type
221      * @param x - x coordinate
222      * @param y - y coordinate
223      */
setTextSelection(int type, int x, int y)224     public native void setTextSelection(int type, int x, int y);
225 
226     /**
227      * Change graphic selection.
228      * @param type - graphic selection type
229      * @param x - x coordinate
230      * @param y - y coordinate
231      */
setGraphicSelection(int type, int x, int y)232     public native void setGraphicSelection(int type, int x, int y);
233 
234     /**
235      * Get selected text
236      * @param mimeType
237      * @return
238      */
getTextSelection(String mimeType)239     public native String getTextSelection(String mimeType);
240 
241     /**
242      * paste
243      * @param mimeType
244      * @param data
245      * @return
246      */
paste(String mimeType, String data)247     public native boolean paste(String mimeType, String data);
248 
249     /**
250      * Reset current (any kind of) selection.
251      */
resetSelection()252     public native void resetSelection();
253 
getCommandValues(String command)254     public native String getCommandValues(String command);
255 
256     /**
257      * Callback to retrieve messages from LOK
258      */
259     public interface MessageCallback {
260         /**
261          * Invoked when a message is retrieved from LOK
262          * @param signalNumber - signal type / number
263          * @param payload - retrieved for the signal
264          */
messageRetrieved(int signalNumber, String payload)265         void messageRetrieved(int signalNumber, String payload);
266     }
267 
268 }
269