summaryrefslogtreecommitdiff
path: root/src/layout.c
blob: 0c116602c3fdf2961b1b411cceda89b20b4c77ee (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
/*
 * This file is part of LibNSLayout
 * Licensed under the ISC License, http://opensource.org/licenses/ISC
 * Copyright 2015 Michael Drake <tlsa@netsurf-browser.org>
 */

/** \file src/layout.c
 * Layout object handling
 */

#include <assert.h>
#include <stdlib.h>

#include "libnslayout/nslayout.h"

#include "layout.h"
#include "util/util.h"
#include "dom/watcher.h"
#include "util/dom-str.h"


/**
 * The layout object for a DOM document
 */
struct nsl_layout {
	dom_document *document;
	css_select_ctx *css_ctx;
	css_media_type *media;
	nsl_callback cb;
	void *pw;

	struct nsl_dom_watcher *watcher;
};


/* Publically exported function, documented in include/libnslayout/nslayout.h */
nsl_error nsl_init(void)
{
	return nsl_dom_str_init();
}


/* Publically exported function, documented in include/libnslayout/nslayout.h */
nsl_error nsl_fini(void)
{
	return nsl_dom_str_fini();
}


/**
 * Callback function for dom modifications.
 *
 * \param[in]  type       The mutation type.
 * \param[in]  node       The target node.  (Caller yields ownership.)
 * \param[in]  node_type  The type of node.
 * \param[in]  pw         The layout object.
 * \return NSL_OK on success, appropriate error otherwise.
 */
static nsl_error nsl_layout_dom_watcher_cb(
		enum nsl_dom_watcher_type type,
		dom_event_target *node,
		dom_node_type node_type,
		void *pw)
{
	nsl_layout *layout = pw;

	UNUSED(type);
	UNUSED(layout);
	UNUSED(node_type);

	/* TODO: Based on event type:
	 *         1. call to do (re)selection:
	 *              a. all nodes?
	 *              b. just this node?
	 *         2. call to update layout, if needed.
	 */

	dom_node_unref(node);

	return NSL_OK;
}


/* Publically exported function, documented in include/libnslayout/nslayout.h */
nsl_error nsl_layout_create(
		dom_document *doc,
		css_select_ctx *css_ctx,
		css_media_type *media,
		nsl_callback cb,
		void *pw,
		nsl_layout **layout)
{
	nsl_layout *l = NULL;
	nsl_error err;

	assert(doc != NULL);
	assert(css_ctx != NULL);
	assert(media != NULL);
	assert(cb != NULL);

	l = calloc(1, sizeof(nsl_layout));
	if (l == NULL) {
		return NSL_NO_MEM;
	}

	/* TODO: Decide: ownership will probably be passed to libnslayout */
	l->document = doc;
	l->css_ctx = css_ctx;
	l->media = media;
	l->cb = cb;
	l->pw = pw;

	err = nsl_dom_watcher_create(&l->watcher, l->document,
			nsl_layout_dom_watcher_cb, l);
	if (err != NSL_OK) {
		return err;
	}

	*layout = l;
	return NSL_OK;
}


/* Publically exported function, documented in include/libnslayout/nslayout.h */
nsl_error nsl_layout_destroy(
		nsl_layout *layout)
{
	nsl_error err;

	assert(layout != NULL);

	/* TODO: free/unref the stuff we own in the layout */
	err = nsl_dom_watcher_destroy(layout->watcher);
	if (err != NSL_OK) {
		return err;
	}

	free(layout);
	return NSL_OK;
}