aboutsummaryrefslogtreecommitdiff
path: root/src/slideshow.c
blob: 6594be80056795b5bd170ee559a92b127d24ae0d (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
/*
 * slideshow.c
 *
 * Copyright © 2013 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 <gdk/gdkkeysyms.h>

#include "presentation.h"
#include "mainwindow.h"
#include "render.h"


/* Force a redraw of the slideshow */
void redraw_slideshow(struct presentation *p)
{
	gint w, h;

	w = gtk_widget_get_allocated_width(GTK_WIDGET(p->ss_drawingarea));
	h = gtk_widget_get_allocated_height(GTK_WIDGET(p->ss_drawingarea));

	gtk_widget_queue_draw_area(p->ss_drawingarea, 0, 0, w, h);
}


static gint ss_destroy_sig(GtkWidget *widget, struct presentation *p)
{
	p->slideshow = NULL;
	g_object_unref(p->blank_cursor);
	return FALSE;
}


static gboolean ss_draw_sig(GtkWidget *da, cairo_t *cr, struct presentation *p)
{
	double xoff, yoff;
	double width, height;

	width = gtk_widget_get_allocated_width(GTK_WIDGET(da));
	height = gtk_widget_get_allocated_height(GTK_WIDGET(da));

	/* Overall background */
	cairo_rectangle(cr, 0.0, 0.0, width, height);
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
	cairo_fill(cr);

	if ( !p->ss_blank ) {

		int h;

		/* FIXME: Assumes that monitor and slide sizes are such that
		 * letterboxing at sides.  This needn't be the case. */
		h = p->proj_slide_width * p->slide_height / p->slide_width;

		/* Get the overall size */
		xoff = (width - p->proj_slide_width)/2.0;
		yoff = (height - h)/2.0;

		/* Draw the slide from the cache */
		cairo_rectangle(cr, xoff, yoff, p->proj_slide_width, h);
		cairo_set_source_surface(cr, p->cur_proj_slide->rendered_proj,
		                         xoff, yoff);
		cairo_fill(cr);

	}

	return FALSE;
}


void change_proj_slide(struct presentation *p, struct slide *np)
{
	/* If this slide is not being shown on the editor, we can free the
	 * buffers */
	if ( p->cur_proj_slide != p->cur_edit_slide ) {
		free_render_buffers_except_thumb(p->cur_proj_slide);
	}

	p->cur_proj_slide = np;

	/* The slide is already rendered, because the editor always gets there
	 * first, so we only need to do this: */
	redraw_slideshow(p);
}


static gint prev_slide_sig(GtkWidget *widget, struct presentation *p)
{
	int cur_slide_number;
	cur_slide_number = slide_number(p, p->cur_edit_slide);
	if ( cur_slide_number == 0 ) return FALSE;
	change_edit_slide(p, p->slides[cur_slide_number-1]);
	return FALSE;
}


static gint next_slide_sig(GtkWidget *widget, struct presentation *p)
{
	int cur_slide_number;
	cur_slide_number = slide_number(p, p->cur_edit_slide);
	if ( cur_slide_number == p->num_slides-1 ) return FALSE;
	change_edit_slide(p, p->slides[cur_slide_number+1]);
	return FALSE;
}


void end_slideshow(struct presentation *p)
{
	gtk_widget_destroy(p->ss_drawingarea);
	gtk_widget_destroy(p->slideshow);
	p->slideshow = NULL;
	p->cur_proj_slide = NULL;
	redraw_editor(p);
}


void toggle_slideshow_link(struct presentation *p)
{
	p->slideshow_linked = 1 - p->slideshow_linked;
	if ( p->slideshow_linked ) {
		change_proj_slide(p, p->cur_edit_slide);
	}
	redraw_editor(p);
}


void check_toggle_blank(struct presentation *p)
{
	if ( p->slideshow != NULL ) {
		//if ( p->prefs->b_splits ) {
			toggle_slideshow_link(p);
		//} else {
		//	p->ss_blank = 1-p->ss_blank;
		//	gdk_window_invalidate_rect(p->ss_drawingarea->window,
		//			           NULL, FALSE);
		//}  FIXME!
	}
}


static gboolean ss_key_press_sig(GtkWidget *da, GdkEventKey *event,
                              struct presentation *p)
{
	switch ( event->keyval ) {

	case GDK_KEY_B :
	case GDK_KEY_b :
		check_toggle_blank(p);
		break;

	case GDK_KEY_Page_Up :
	case GDK_KEY_Up :
		prev_slide_sig(NULL, p);
		break;

	case GDK_KEY_Page_Down :
	case GDK_KEY_Down :
		next_slide_sig(NULL, p);
		break;

	case GDK_KEY_Escape :
		end_slideshow(p);
		break;

	}

	return FALSE;
}


static gboolean ss_realize_sig(GtkWidget *w, struct presentation *p)
{
	GdkWindow *win;

	win = gtk_widget_get_window(w);

	p->blank_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
	gdk_window_set_cursor(GDK_WINDOW(win), p->blank_cursor);

	gtk_window_parse_geometry(GTK_WINDOW(w), p->ss_geom);

	return FALSE;
}


void try_start_slideshow(struct presentation *p)
{
	GtkWidget *n;
	GdkScreen *screen;
	int n_monitors;
	int i;

	/* Presentation already running? */
	if ( p->slideshow != NULL ) return;

	p->ss_blank = 0;

	n = gtk_window_new(GTK_WINDOW_TOPLEVEL);

	p->ss_drawingarea = gtk_drawing_area_new();
	gtk_container_add(GTK_CONTAINER(n), p->ss_drawingarea);

	gtk_widget_set_can_focus(GTK_WIDGET(p->ss_drawingarea), TRUE);
	gtk_widget_add_events(GTK_WIDGET(p->ss_drawingarea),
	                      GDK_KEY_PRESS_MASK);

	g_signal_connect(G_OBJECT(p->ss_drawingarea), "key-press-event",
			 G_CALLBACK(ss_key_press_sig), p);
	g_signal_connect(G_OBJECT(n), "destroy", G_CALLBACK(ss_destroy_sig), p);
	g_signal_connect(G_OBJECT(n), "realize", G_CALLBACK(ss_realize_sig), p);

	g_signal_connect(G_OBJECT(p->ss_drawingarea), "draw",
			 G_CALLBACK(ss_draw_sig), p);

	gtk_widget_grab_focus(GTK_WIDGET(p->ss_drawingarea));

	screen = gdk_screen_get_default();
	n_monitors = gdk_screen_get_n_monitors(screen);
	for ( i=0; i<n_monitors; i++ ) {

		GdkRectangle rect;
		int w;

		gdk_screen_get_monitor_geometry(screen, i, &rect);
		snprintf(p->ss_geom, 255, "%ix%i+%i+%i",
		         rect.width, rect.height, rect.x, rect.y);

		w = rect.height * p->slide_width/p->slide_height;
		if ( w > rect.width ) w = rect.width;
		p->proj_slide_width = w;

	} /* FIXME: Sensible (configurable) choice of monitor */

	rerender_slide(p);

	p->slideshow = n;
	p->slideshow_linked = 1;
	gtk_window_fullscreen(GTK_WINDOW(n));
	gtk_widget_show_all(GTK_WIDGET(n));

	//if ( p->prefs->open_notes ) open_notes(p);  FIXME!

	p->cur_proj_slide = p->cur_edit_slide;
}