aboutsummaryrefslogtreecommitdiff
path: root/src/slide_window.c
blob: 33a4fe0d2479c797a56e0a096c30088d7062667c (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * slide_window.c
 *
 * Copyright © 2013-2016 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 <gtk/gtk.h>
#include <assert.h>
#include <gdk/gdkkeysyms.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <math.h>

#include "presentation.h"
#include "slide_window.h"
#include "render.h"
#include "frame.h"
#include "slideshow.h"
#include "pr_clock.h"
#include "sc_parse.h"
#include "sc_interp.h"
#include "sc_editor.h"


struct _slidewindow
{
	struct presentation *p;
	GtkWidget           *window;
	GtkToolItem         *bfirst;
	GtkToolItem         *bprev;
	GtkToolItem         *bnext;
	GtkToolItem         *blast;

	/* The slide being displayed.  Note that the SCEditor is using the
	 * child of this block, but we need to keep a record of it for changing
	 * the slide. */
	SCBlock             *scblocks;

	SCEditor            *sceditor;

	struct menu_pl      *style_menu;
	int                  n_style_menu;
};


/* Inelegance to make furniture selection menus work */
struct menu_pl
{
	SlideWindow *sw;
	char *style_name;
	GtkWidget *widget;
};


static gint UNUSED add_furniture(GtkWidget *widget, struct menu_pl *pl)
{
	sc_block_append_end(pl->sw->scblocks,
	                    strdup(pl->style_name), NULL, NULL);

	//do_slide_update(pl->p, pl->sw->pc); FIXME

	return 0;
}


static void UNUSED update_style_menus(SlideWindow *sw)
{
	//GtkWidget *menu;
	SCInterpreter *scin;
	struct style_id *styles;
	int i, n_sty;

	/* Free old list */
	for ( i=0; i<sw->n_style_menu; i++ ) {
		gtk_widget_destroy(sw->style_menu[i].widget);
		free(sw->style_menu[i].style_name);
	}
	free(sw->style_menu);

	/* Get the list of styles from the style sheet */
	scin = sc_interp_new(NULL, sw->p->lang, NULL);
	if ( scin == NULL ) {
		fprintf(stderr, "Failed to set up interpreter.\n");
		return;
	}
	sc_interp_run_stylesheet(scin, sw->p->stylesheet);

	styles = list_styles(scin, &n_sty);
	if ( styles == NULL ) return;

	sc_interp_destroy(scin);

	/* Set up list for next time */
	sw->style_menu = calloc(n_sty, sizeof(struct menu_pl));
	if ( sw->style_menu == NULL ) return;

#if 0   //  FIXME
	/* Add the styles to the "Insert" menu */
	menu = gtk_ui_manager_get_widget(sw->ui, "/displaywindow/insert");
	menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu));

	for ( i=0; i<n_sty; i++ ) {

		GtkWidget *item;

		item = gtk_menu_item_new_with_label(styles[i].friendlyname);
		gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);

		sw->style_menu[i].sw = sw;
		sw->style_menu[i].widget = item;
		sw->style_menu[i].style_name = styles[i].name;

		g_signal_connect(G_OBJECT(item), "activate",
		                 G_CALLBACK(add_furniture),
		                 &sw->style_menu[i]);

		free(styles[i].friendlyname);
	}

	gtk_widget_show_all(menu);
	free(styles);
#endif
}


static void delete_frame_sig(GSimpleAction *action, GVariant *parameter,
                             gpointer vp)
{
	SlideWindow *sw = vp;
	sc_editor_delete_selected_frame(sw->sceditor);
}


/* Change the editor's slide to "np" */
void change_edit_slide(SlideWindow *sw, SCBlock *np)
{
	SCBlock *ch;

	sc_editor_set_slidenum(sw->sceditor, slide_number(sw->p, np));

	ch = sc_block_child(np);
	if ( ch == NULL ) {
		ch = sc_block_append_inside(sw->scblocks, NULL, NULL, "");
	}
	sc_editor_set_scblock(sw->sceditor, ch);
	sw->scblocks = np;
}


static void change_slide_first(SlideWindow *sw)
{
	change_edit_slide(sw, first_slide(sw->p));
}


static void change_slide_backwards(SlideWindow *sw)
{
	SCBlock *tt;
	tt = prev_slide(sw->p, sw->scblocks);
	if ( tt == NULL ) return;
	change_edit_slide(sw, tt);
}


static void change_slide_forwards(SlideWindow *sw)
{
	SCBlock *tt;
	tt = next_slide(sw->p, sw->scblocks);
	if ( tt == NULL ) return;
	change_edit_slide(sw, tt);
}


static void change_slide_last(SlideWindow *sw)
{
	change_edit_slide(sw, last_slide(sw->p));
}


static void first_slide_sig(GSimpleAction *action, GVariant *parameter,
                           gpointer vp)
{
	SlideWindow *sw = vp;
	change_slide_first(sw);
}


static void prev_slide_sig(GSimpleAction *action, GVariant *parameter,
                           gpointer vp)
{
	SlideWindow *sw = vp;
	change_slide_backwards(sw);
}


static void next_slide_sig(GSimpleAction *action, GVariant *parameter,
                           gpointer vp)
{
	SlideWindow *sw = vp;
	change_slide_forwards(sw);
}


static void last_slide_sig(GSimpleAction *action, GVariant *parameter,
                           gpointer vp)
{
	SlideWindow *sw = vp;
	change_slide_last(sw);
}


void slidewindow_redraw(SlideWindow *sw)
{
	sc_editor_redraw(sw->sceditor);
}


void update_titlebar(struct presentation *p)
{
	get_titlebar_string(p);

	if ( p->slidewindow != NULL ) {

		char *title;

		title = malloc(strlen(p->titlebar)+14);
		sprintf(title, "%s - Colloquium", p->titlebar);
		gtk_window_set_title(GTK_WINDOW(p->slidewindow->window), title);
		free(title);

       }

}


static gboolean close_sig(GtkWidget *w, SlideWindow *sw)
{
	sw->p->slidewindow = NULL;
	return FALSE;
}


static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event,
                              SlideWindow *sw)
{
	switch ( event->keyval ) {

		case GDK_KEY_Page_Up :
		change_slide_backwards(sw);
		break;

		case GDK_KEY_Page_Down :
		change_slide_forwards(sw);
		break;

	}

	return FALSE;
}


GActionEntry sw_entries[] = {

	{ "deleteframe", delete_frame_sig, NULL, NULL, NULL },
	{ "first", first_slide_sig, NULL, NULL, NULL },
	{ "prev", prev_slide_sig, NULL, NULL, NULL },
	{ "next", next_slide_sig, NULL, NULL, NULL },
	{ "last", last_slide_sig, NULL, NULL, NULL },
};


SlideWindow *slide_window_open(struct presentation *p, SCBlock *scblocks,
                               GApplication *app)
{
	GtkWidget *window;
	GtkWidget *scroll;
	SlideWindow *sw;
	SCBlock *stylesheets[2];
	SCBlock *ch;

	sw = calloc(1, sizeof(SlideWindow));
	if ( sw == NULL ) return NULL;

	window = gtk_application_window_new(GTK_APPLICATION(app));
	gtk_window_set_role(GTK_WINDOW(window), "slide");
	sw->window = window;
	sw->p = p;

	g_action_map_add_action_entries(G_ACTION_MAP(window), sw_entries,
	                                G_N_ELEMENTS(sw_entries), sw);

	update_titlebar(p);

	g_signal_connect(G_OBJECT(window), "destroy",
	                 G_CALLBACK(close_sig), sw);

	stylesheets[0] = p->stylesheet;
	stylesheets[1] = NULL;

	sw->scblocks = scblocks;
	ch = sc_block_child(scblocks);
	if ( ch == NULL ) {
		ch = sc_block_append_inside(scblocks, NULL, NULL, "");
	}
	sw->sceditor = sc_editor_new(ch, stylesheets, p->lang);
	sc_editor_set_slidenum(sw->sceditor, slide_number(sw->p, scblocks));

	scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
	                               GTK_POLICY_AUTOMATIC,
	                               GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(sw->sceditor));
	gtk_window_set_focus(GTK_WINDOW(window), GTK_WIDGET(sw->sceditor));
	g_signal_connect(G_OBJECT(sw->sceditor), "key-press-event",
			 G_CALLBACK(key_press_sig), sw);

	/* Size of SCEditor surface in pixels */
	/* FIXME: Somewhat arbitrary.  Should come from slide itself */
	sc_editor_set_size(sw->sceditor, 1024, 768);
	sc_editor_set_logical_size(sw->sceditor, 1024.0, 768.0);

	gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scroll));

	/* Default size */
	gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scroll),
	                                          1024);
	gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll),
	                                           768);
	gtk_window_set_resizable(GTK_WINDOW(sw->window), TRUE);

	gtk_widget_show_all(window);
	gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scroll),
	                                          100);
	gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll),
	                                           100);

	return sw;
}