Bump version to 24.04.3.4
[LibreOffice.git] / libreofficekit / source / gtk / tilebuffer.hxx
blob239482e34625fe5655e086b196a9e950ce6ad008
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_TILEBUFFER_HXX
11 #define INCLUDED_TILEBUFFER_HXX
13 #include <cairo.h>
14 #include <gio/gio.h>
15 #include <glib.h>
17 #include <map>
19 #define LOK_TILEBUFFER_ERROR (LOKTileBufferErrorQuark())
21 // Lets use a square of side 256 pixels for each tile.
22 const int nTileSizePixels = 256;
24 /**
25 Converts the pixel value to zoom independent twip value.
27 @param fInput value to convert
28 @param zoom the current zoom level
30 @return the pixels value corresponding to given twip value
32 float pixelToTwip(float fInput, float zoom);
34 /**
35 Converts the zoom independent twip value pixel value.
37 @param fInput value to convert
38 @param zoom the current zoom level
40 @return the twip value corresponding to given pixel value
42 float twipToPixel(float fInput, float zoom);
44 /**
45 Gets GQuark identifying this tile buffer errors
47 GQuark LOKTileBufferErrorQuark(void);
49 /**
50 This class represents a single tile in the tile buffer.
51 It encloses a reference to GdkPixBuf containing the pixel data of the tile.
53 class Tile
55 public:
56 Tile()
57 : valid(false)
58 , m_pBuffer(nullptr)
61 ~Tile()
63 if (m_pBuffer)
64 cairo_surface_destroy(m_pBuffer);
67 /**
68 Tells if this tile is valid or not. Initialised to 0 (invalid) during
69 object creation.
71 bool valid;
73 /// Function to get the pointer to enclosing cairo_surface_t
74 cairo_surface_t* getBuffer();
75 /// Used to set the pixel buffer of this object
76 void setSurface(cairo_surface_t*);
78 private:
79 /// Pixel buffer data for this tile
80 cairo_surface_t* m_pBuffer;
83 /**
84 This class represents the tile buffer which is responsible for managing,
85 reusing and caching all the already rendered tiles. If the given tile is not
86 present in the buffer, call to LOK Document's (m_pLOKDocument) paintTile
87 method is made which fetches the rendered tile from LO core and store it in
88 buffer for future reuse.
90 class TileBuffer
92 public:
93 TileBuffer(int columns = 0, int scale = 1)
94 : m_nWidth(columns)
96 cairo_surface_t* pSurface = cairo_image_surface_create(
97 CAIRO_FORMAT_ARGB32, nTileSizePixels * scale, nTileSizePixels * scale);
98 m_DummyTile.setSurface(pSurface);
99 cairo_surface_destroy(pSurface);
103 Gets the underlying Tile object for given position. The position (0, 0)
104 points to the left top most tile of the buffer.
106 If the tile is not cached by the tile buffer, it makes a paintTile call
107 to LO core asking to render the given tile. It then stores the tile for
108 future reuse.
110 @param x the tile along the x-axis of the buffer
111 @param y the tile along the y-axis of the buffer
112 @param task GTask object containing the necessary data
113 @param pool GThreadPool managed by the widget instance used for all the
114 LOK calls made by widget. It is needed here because getTile invokes one
115 of the LOK call : paintTile.
117 @return the tile at the mentioned position (x, y)
119 Tile& getTile(int x, int y, GTask* task, GThreadPool* pool);
122 Takes ownership of the surface and sets it on a tile at a given location
124 void setTile(int x, int y, cairo_surface_t* surface);
126 /// Returns true if a valid tile exists at this location
127 bool hasValidTile(int x, int y);
129 /// Destroys all the tiles in the tile buffer; also frees the memory allocated
130 /// for all the Tile objects.
131 void resetAllTiles();
133 Marks the tile as invalid. The tile (0, 0) is the left topmost tile in
134 the tile buffer.
136 @param x the position of tile along x-axis
137 @param y the position of tile along y-axis
138 @param zoom zoom factor of the document
139 @param task GTask object containing the necessary data
140 @param pool GThreadPool managed by the widget instance used for all the
141 LOK calls made by widget. It is needed here because setInvalid() invokes one
142 of the LOK call : paintTile.
144 void setInvalid(int x, int y, float zoom, GTask* task, GThreadPool*);
146 private:
147 /// Stores all the tiles cached by this tile buffer.
148 std::map<int, Tile> m_mTiles;
149 /// Width of the current tile buffer (number of columns)
150 int m_nWidth;
151 /// Dummy tile
152 Tile m_DummyTile;
155 enum
157 LOK_LOAD_DOC,
158 LOK_POST_COMMAND,
159 LOK_SET_EDIT,
160 LOK_SET_PARTMODE,
161 LOK_SET_PART,
162 LOK_POST_KEY,
163 LOK_PAINT_TILE,
164 LOK_POST_MOUSE_EVENT,
165 LOK_SET_GRAPHIC_SELECTION,
166 LOK_SET_CLIENT_ZOOM
169 enum
171 LOK_TILEBUFFER_CHANGED,
172 LOK_TILEBUFFER_MEMORY
176 A struct that we use to store the data about the LOK call.
178 Object of this type is passed with all the LOK calls,
179 so that they can be identified. Additionally, it also contains
180 the data that LOK call needs.
182 struct LOEvent
184 /// To identify the type of LOK call
185 int m_nType;
187 /// @name post_command parameters
188 ///@{
189 const gchar* m_pCommand;
190 gchar* m_pArguments;
191 gboolean m_bNotifyWhenFinished;
192 ///@}
194 /// set_edit parameter
195 gboolean m_bEdit;
197 /// set_partmode parameter
198 int m_nPartMode;
200 /// set_part parameter
201 int m_nPart;
203 /// @name postKeyEvent parameters
204 ///@{
205 int m_nKeyEvent;
206 int m_nCharCode;
207 int m_nKeyCode;
208 ///@}
210 /// @name paintTile parameters
211 ///@{
212 int m_nPaintTileX;
213 int m_nPaintTileY;
214 float m_fPaintTileZoom;
215 TileBuffer* m_pTileBuffer;
216 ///@}
218 /// @name postMouseEvent parameters
219 ///@{
220 int m_nPostMouseEventType;
221 int m_nPostMouseEventX;
222 int m_nPostMouseEventY;
223 int m_nPostMouseEventCount;
224 int m_nPostMouseEventButton;
225 int m_nPostMouseEventModifier;
226 ///@}
228 /// @name setGraphicSelection parameters
229 ///@{
230 int m_nSetGraphicSelectionType;
231 int m_nSetGraphicSelectionX;
232 int m_nSetGraphicSelectionY;
233 ///@}
235 /// @name setClientView parameters
236 ///@{
237 int m_nTilePixelWidth;
238 int m_nTilePixelHeight;
239 int m_nTileTwipWidth;
240 int m_nTileTwipHeight;
241 ///@}
243 /// Constructor to instantiate an object of type `type`.
244 explicit LOEvent(int type)
245 : m_nType(type)
246 , m_pCommand(nullptr)
247 , m_pArguments(nullptr)
248 , m_bNotifyWhenFinished(false)
249 , m_bEdit(false)
250 , m_nPartMode(0)
251 , m_nPart(0)
252 , m_nKeyEvent(0)
253 , m_nCharCode(0)
254 , m_nKeyCode(0)
255 , m_nPaintTileX(0)
256 , m_nPaintTileY(0)
257 , m_fPaintTileZoom(0)
258 , m_pTileBuffer(nullptr)
259 , m_nPostMouseEventType(0)
260 , m_nPostMouseEventX(0)
261 , m_nPostMouseEventY(0)
262 , m_nPostMouseEventCount(0)
263 , m_nPostMouseEventButton(0)
264 , m_nPostMouseEventModifier(0)
265 , m_nSetGraphicSelectionType(0)
266 , m_nSetGraphicSelectionX(0)
267 , m_nSetGraphicSelectionY(0)
268 , m_nTilePixelWidth(0)
269 , m_nTilePixelHeight(0)
270 , m_nTileTwipWidth(0)
271 , m_nTileTwipHeight(0)
275 /// Wrapper around delete to help GLib.
276 static void destroy(void* pMemory);
279 #endif // INCLUDED_TILEBUFFER_HXX
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */