summaryrefslogtreecommitdiff
path: root/include/libnslayout/nslayout.h
blob: b486cbaa0f294187c2d0d1f57386a528d36bcb04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * This file is part of LibNSLayout
 * Licensed under the ISC License, http://opensource.org/licenses/ISC
 * Copyright 2015 Michael Drake <tlsa@netsurf-browser.org>
 * Copyright 2015 John-Mark Bell <jmb@netsurf-browser.org>
 */

/** \file include/libnslayout/nslayout.h
 * Layout object handling
 */

#ifndef nslayout_nslayout_h_
#define nslayout_nslayout_h_

#ifdef __cplusplus
extern "C"
{
#endif

#include <libcss/libcss.h>
#include <dom/dom.h>

#include <libnslayout/error.h>

/** An opaque client-owned replaced element */
typedef void nslayout_replaced;

/** A rectangle */
typedef struct nslayout_rect {
	int x; /**< X position of left of rect in px */
	int y; /**< Y position of top  of rect in px */
	int w; /**< Width  of rect in px */
	int h; /**< Height of rect in px */
} nslayout_rect;

/** Render list */
typedef struct nslayout_render_list {
} nslayout_render_list;

/** Render list */
typedef struct nslayout_layout nslayout_layout;

/**
 * A LibNSLayout request
 *
 * Passed to the client via nslayout_callback
 */
typedef struct nslayout_request {
	/** Request type */
	enum {
		NSLAYOUT_GET_RESOURCE,
		NSLAYOUT_CREATE_REPLACED,
		NSLAYOUT_RENDER,
		NSLAYOUT_SET_EXTENTS,
		NSLAYOUT_GET_INTRINSIC_SIZE
	} type;
	/** Request's type-specific parameters */
	union {
		struct {
			const char *url; /**< Absolute URL */
		} get_resource;
		struct {
			dom_element *element; /**< DOM element */
		} create_replaced;
		struct {
			nslayout_render_list *list; /**< Render list */
		} render;
		struct {
			unsigned int width;  /**< Document width  in px */
			unsigned int height; /**< Document height in px */
		} set_extents;
		struct {
			nslayout_replaced *replaced; /** A replacement object */
		} get_intrinsic_size;
	} request;
	/** Request's type-specific return values */
	union {
		struct {
			nslayout_replaced **replaced; /** Replacement object */
		} get_resource;
		struct {
			nslayout_replaced **replaced; /** Replacement object */
		} create_replaced;
		struct {
			unsigned int *width;  /** Replacement object's width  */
			unsigned int *height; /** Replacement object's height */
		} get_intrinsic_size;
	} response;
} nslayout_request;


/**
 * Initialise LibNSLayout
 *
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_init(void);

/**
 * Finalise LibNSLayout
 *
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_fini(void);

/**
 * LibNSLayout client callback function
 *
 * \param[in]     layout	The layout we're making a request for.
 * \param[in]     pw		The client's private data for this layout.
 * \param[in,out] req		The request details.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
typedef nslayout_error (*nslayout_callback)(
		nslayout_layout *layout,
		void *pw,
		nslayout_request *req);

/**
 * Create a Layout object for a given DOM
 *
 * \param[in]  doc		The LibDOM document to build a layout for.
 * \param[in]  css_ctx		The LibCSS selection context for the document.
 * \param[in]  media		The LibCSS media to use when selecting.
 * \param[in]  cb		The client's private data for this layout.
 * \param[in]  pw		The client's private data for this layout.
 * \param[out] layout	Returns a pointer to the created layout object.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_layout_create(
		dom_document *doc,
		css_select_ctx *css_ctx,
		css_media_type *media,
		nslayout_callback cb,
		void *pw,
		nslayout_layout **layout);

/**
 * Destroy a Layout object
 *
 * \param[in] layout	Returns a pointer to the created layout object.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_layout_destroy(
		nslayout_layout *layout);

/**
 * Update the viewport for a layout
 *
 * Note: Before this is called, the layout engine will create internal
 *       data structures for the document, but will not start to position
 *       things and will not emit render lists.
 *
 * \param[in] layout	Layout to set viewport for.
 * \param[in] viewport	Viewport dimensions and offset.
 * \param[in] scale		Rendering scale.
 * \param[in] dpi		DPI of render target with viewport.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_update_viewport(
		nslayout_layout *layout,
		nslayout_rect *viewport,
		css_fixed scale,
		unsigned int dpi);

/**
 * Find the area occupied by element.
 *
 * \param[in]  layout	Layout to locate an element in.
 * \param[in]  element	Element to get area of.
 * \param[out] area	Returns area with position relative to viewport.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_element_get_location(
		nslayout_layout *layout,
		dom_element *element,
		nslayout_rect *area);

/**
 * Find the top-most element at a given point, in terms of z-order.
 *
 * \param[in]  layout	Layout to find an element in.
 * \param[in]  x	Mouse x-coordinate (viewport relative).
 * \param[in]  y	Mouse y-coordinate (viewport relative).
 * \param[out] element	Returns the element we found.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_element_at_point(
		nslayout_layout *layout,
		unsigned int x,
		unsigned int y,
		dom_event_target **element);

/**
 * Mark an element (or part of it) as needing redraw.
 *
 * \param[in] layout	Layout to indicate redraw is required for.
 * \param[in] element	Element to mark as needing redraw.
 * \param[in] rel_area	Area of element to redraw relative to object's top-left.
 *			May be NULL, to redraw whole element.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_layout_dirty_element(
		nslayout_layout *layout,
		dom_element *element,
		nslayout_rect *rel_area);

/**
 * Mark an area as needing redraw.
 *
 * \param[in] layout	Layout to indicate redraw is required for.
 * \param[in] area	Area to redraw relative to viewport's top-left.
 * \return NSLAYOUT_OK on success, appropriate error otherwise.
 */
nslayout_error nslayout_layout_dirty_area(
		nslayout_layout *layout,
		nslayout_rect *area);

#ifdef __cplusplus
}
#endif

#endif