aboutsummaryrefslogtreecommitdiff
path: root/src/presentation.c
blob: b8b4589d09d723737e10147c45a103d47329e886 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * presentation.c
 *
 * Copyright © 2013-2018 Thomas White <taw@bitwiz.org.uk>
 *
 * This file is part of Colloquium.
 *
 * Colloquium is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <gtk/gtk.h>

#include "presentation.h"
#include "slide_window.h"
#include "frame.h"
#include "imagestore.h"
#include "render.h"
#include "sc_interp.h"
#include "utils.h"


void free_presentation(struct presentation *p)
{
	int final = 0;

	/* FIXME: Loads of stuff leaks here */
	free(p->uri);
	imagestore_destroy(p->is);
	free(p);

	if ( final ) {
		gtk_main_quit();
	}
}


char *get_titlebar_string(struct presentation *p)
{
	if ( p == NULL || p->uri == NULL ) {
		return strdup(_("(untitled)"));
	} else {
		GFile *f = g_file_new_for_uri(p->uri);
		char *bn = g_file_get_basename(f);
		g_object_unref(f);
		return bn;
	}
}


struct presentation *new_presentation(const char *imagestore)
{
	struct presentation *new;

	new = calloc(1, sizeof(struct presentation));
	if ( new == NULL ) return NULL;

	new->uri = NULL;

	new->scblocks = NULL;

	/* Default slide size */
	new->slide_width = 1024.0;
	new->slide_height = 768.0;

	new->completely_empty = 1;
	new->saved = 1;
	new->stylesheet = NULL;
	new->is = imagestore_new(imagestore);

	new->lang = pango_language_get_default();

	return new;
}


int save_presentation(struct presentation *p, GFile *file)
{
	GFileOutputStream *fh;
	int r;
	GError *error = NULL;

	fh = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
	if ( fh == NULL ) {
		fprintf(stderr, _("Open failed: %s\n"), error->message);
		return 1;
	}
	r = save_sc_block(G_OUTPUT_STREAM(fh), p->scblocks);
	g_object_unref(fh);

	if ( r ) return 1;

	imagestore_set_parent(p->is, g_file_get_parent(file));
	p->uri = g_file_get_uri(file);
	p->saved = 1;
	update_titlebar(p->narrative_window);
	return 0;
}


int slide_number(struct presentation *p, SCBlock *sl)
{
	SCBlock *bl = p->scblocks;
	int n = 0;

	while ( bl != NULL ) {
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			n++;
			if ( bl == sl ) return n;
		}
		bl = sc_block_next(bl);
	}

	return 0;
}


int num_slides(struct presentation *p)
{
	SCBlock *bl = p->scblocks;
	int n = 0;

	while ( bl != NULL ) {
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			n++;
		}
		bl = sc_block_next(bl);
	}

	return n;
}


SCBlock *first_slide(struct presentation *p)
{
	SCBlock *bl = p->scblocks;

	while ( bl != NULL ) {
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			return bl;
		}
		bl = sc_block_next(bl);
	}

	fprintf(stderr, _("Couldn't find first slide!\n"));
	return NULL;
}


SCBlock *last_slide(struct presentation *p)
{
	SCBlock *bl = p->scblocks;
	SCBlock *l = NULL;

	while ( bl != NULL ) {
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			l = bl;
		}
		bl = sc_block_next(bl);
	}

	if ( l == NULL ) {
		fprintf(stderr, _("Couldn't find last slide!\n"));
	}
	return l;
}


SCBlock *next_slide(struct presentation *p, SCBlock *sl)
{
	SCBlock *bl = sl;
	int found = 0;

	while ( bl != NULL ) {
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			if ( found ) return bl;
		}
		if ( bl == sl ) {
			found = 1;
		}
		bl = sc_block_next(bl);
	}

	fprintf(stderr, _("Couldn't find next slide!\n"));
	return NULL;
}


SCBlock *prev_slide(struct presentation *p, SCBlock *sl)
{
	SCBlock *bl = p->scblocks;
	SCBlock *l = NULL;

	while ( bl != NULL ) {
		if ( bl == sl ) {
			if ( l == NULL ) return sl; /* Already on first slide */
			return l;
		}
		if ( safe_strcmp(sc_block_name(bl), "slide") == 0 ) {
			l = bl;
		}
		bl = sc_block_next(bl);
	}

	fprintf(stderr, _("Couldn't find prev slide!\n"));
	return NULL;
}


int replace_stylesheet(struct presentation *p, SCBlock *ss)
{
	/* Create style sheet from union of old and new,
	 * preferring items from the new one */

	/* If there was no stylesheet before, add a dummy one */
	if ( p->stylesheet == NULL ) {
		p->stylesheet = sc_block_append_end(p->scblocks,
		                                    "stylesheet", NULL, NULL);
	}

	/* Cut the old stylesheet out of the presentation,
	 * and put in the new one */
	sc_block_substitute(&p->scblocks, p->stylesheet, ss);
	p->stylesheet = ss;

	return 0;
}


SCBlock *find_stylesheet(SCBlock *bl)
{
	while ( bl != NULL ) {

		const char *name = sc_block_name(bl);

		if ( (name != NULL) && (strcmp(name, "stylesheet") == 0) ) {
			return bl;
		}

		bl = sc_block_next(bl);

	}

	return NULL;
}


static void install_stylesheet(struct presentation *p)
{
	if ( p->stylesheet != NULL ) {
		fprintf(stderr, _("Duplicate style sheet!\n"));
		return;
	}

	p->stylesheet = find_stylesheet(p->scblocks);

	if ( p->stylesheet == NULL ) {
		fprintf(stderr, _("No style sheet.\n"));
	}
}


static void set_slide_size_from_stylesheet(struct presentation *p)
{
	SCInterpreter *scin;
	double w, h;
	int r;

	if ( p->stylesheet == NULL ) return;

	scin = sc_interp_new(NULL, NULL, NULL, NULL);
	sc_interp_run_stylesheet(scin, p->stylesheet);  /* ss == NULL is OK */
	r = sc_interp_get_slide_size(scin, &w, &h);
	sc_interp_destroy(scin);

	if ( r == 0 ) {
		p->slide_width = w;
		p->slide_height = h;
	}
}


int load_presentation(struct presentation *p, GFile *file)
{
	int r = 0;
	char *everything;

	assert(p->completely_empty);

	if ( !g_file_load_contents(file, NULL, &everything, NULL, NULL, NULL) ) {
		fprintf(stderr, _("Failed to load '%s'\n"), g_file_get_uri(file));
		return 1;
	}

	p->scblocks = sc_parse(everything);
	g_free(everything);

	p->lang = pango_language_get_default();

	if ( p->scblocks == NULL ) r = 1;

	if ( r ) {
		p->completely_empty = 1;
		fprintf(stderr, _("Parse error.\n"));
		return r;  /* Error */
	}

	install_stylesheet(p);
	set_slide_size_from_stylesheet(p);

	assert(p->uri == NULL);
	p->uri = g_file_get_uri(file);
	imagestore_set_parent(p->is, g_file_get_parent(file));

	return 0;
}