aboutsummaryrefslogtreecommitdiff
path: root/src/slide_sorter.c
blob: 91d68bddaca82d7c2ed0f5a93336141dc1ce9674 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * slide_sorter.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 "presentation.h"


struct slide_sorter
{
	GtkWidget *window;
	GtkWidget *da;

	int width;  /* Number of slides across */
	struct presentation *p;

	int tw;
	int th;
	int bw;

	int selection;
	int dragging;
	int drag_preview_pending;
	int have_drag_data;
	int drag_highlight;
};


static gint close_slidesorter_sig(GtkWidget *w, struct presentation *p)
{
	p->slide_sorter = NULL;
	return FALSE;
}


static void redraw_slidesorter(struct slide_sorter *n)
{
	gint w, h;
	w = gtk_widget_get_allocated_width(GTK_WIDGET(n->da));
	h = gtk_widget_get_allocated_height(GTK_WIDGET(n->da));
	gtk_widget_queue_draw_area(n->da, 0, 0, w, h);
}


static gboolean draw_sig(GtkWidget *da, cairo_t *cr, struct slide_sorter *n)
{
	int width, height;
	int i;

	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, 1.0, 1.0, 1.0);
	cairo_fill(cr);

	cairo_translate(cr, n->bw, n->bw);  /* Border */

	for ( i=0; i<n->p->num_slides; i++ ) {

		int x = i % n->width;
		int y = i / n->width;
		struct slide *s = n->p->slides[i];

		cairo_save(cr);

		cairo_translate(cr, x*(n->tw+2*n->bw), y*(n->th+2*n->bw));

		if ( i == n->selection ) {

			cairo_save(cr);
			cairo_rectangle(cr, 0.0,  0.0, n->tw+2*n->bw,
			                               n->th+2*n->bw);
			cairo_set_source_rgb(cr, 0.0, 0.0, 0.4);
			cairo_fill(cr);
			cairo_restore(cr);

		}

		cairo_translate(cr, n->bw, n->bw);  /* Border */
		cairo_rectangle(cr, 0.0, 0.0, n->tw, n->th);
		cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
		cairo_fill_preserve(cr);
		if ( s->rendered_thumb != NULL ) {
			cairo_set_source_surface(cr, s->rendered_thumb,
			                         0.0, 0.0);
		}
		cairo_fill(cr);

		cairo_rectangle(cr, 0.5, 0.5, n->tw, n->th);
		cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
		cairo_set_line_width(cr, 1.0);
		cairo_stroke(cr);

		cairo_restore(cr);

	}

	return FALSE;
}


static void size_sig(GtkWidget *widget, GdkRectangle *size,
                     struct slide_sorter *n)
{
	int w, h;

	w = gtk_widget_get_allocated_width(n->da);
	n->width = (w-2*n->bw) / (n->tw+2*n->bw);
	if ( n->width < 1 ) n->width = 1;

	h = ((n->p->num_slides / n->width)+1) * (n->th+2*n->bw) + 2*n->bw;

	gtk_widget_set_size_request(n->da, n->tw+2*n->bw, h);
}


static gboolean button_press_sig(GtkWidget *da, GdkEventButton *event,
                                 struct slide_sorter *n)
{
	int x, y;

	x = (event->x - n->bw) / (n->tw + 2*n->bw);
	y = (event->y - n->bw) / (n->th + 2*n->bw);
	n->selection = y*n->width + x;

	redraw_slidesorter(n);

	return FALSE;
}


static gboolean motion_sig(GtkWidget *da, GdkEventMotion *event,
                           struct slide_sorter *n)
{
	if ( !n->dragging ) {

		GtkTargetList *list;
		GtkTargetEntry targets[1];

		printf("Starting drag\n");

		targets[0].target = "application/vnd.colloquium-slide";
		targets[0].flags = 0;
		targets[0].info = 1;

		list = gtk_target_list_new(targets, 1);
		gtk_drag_begin(da, list, GDK_ACTION_COPY | GDK_ACTION_MOVE,
		               1, (GdkEvent *)event);
		//gtk_target_list_unref(list);

		n->dragging = 1;

	}

	gdk_event_request_motions(event);

	return FALSE;
}


static gboolean button_release_sig(GtkWidget *da, GdkEventButton *event,
                                   struct slide_sorter *n)
{
	return FALSE;
}


static gboolean dnd_motion(GtkWidget *widget, GdkDragContext *drag_context,
                           gint x, gint y, guint time, struct slide_sorter *n)
{
	GdkAtom target;

	printf("DND motion\n");

	/* If we haven't already requested the data, do so now */
	if ( !n->drag_preview_pending && !n->have_drag_data ) {

		target = gtk_drag_dest_find_target(widget, drag_context, NULL);

		if ( target != GDK_NONE ) {

			printf("Requesting data\n");
			n->drag_preview_pending = 1;
			/* Note: the dnd_get and dnd_receive signals occur
			 * before this call returns */
			gtk_drag_get_data(widget, drag_context, target, time);

		} else {

			printf("No match\n");
			n->drag_preview_pending = 0;
			gdk_drag_status(drag_context, 0, time);

		}

	}

	if ( n->have_drag_data ) {

		gdk_drag_status(drag_context, GDK_ACTION_MOVE, time);
		/* Draw drag box */
		n->have_drag_data = 1;

	}

	return TRUE;
}



static gboolean dnd_drop(GtkWidget *widget, GdkDragContext *drag_context,
                     gint x, gint y, guint time, struct slide_sorter *n)
{
	GdkAtom target;

	printf("Drop.\n");

	target = gtk_drag_dest_find_target(widget, drag_context, NULL);

	if ( target == GDK_NONE ) {
		printf("No match.\n");
		gtk_drag_finish(drag_context, FALSE, FALSE, time);
	} else {
		gtk_drag_get_data(widget, drag_context, target, time);
	}

	return TRUE;
}


static void dnd_receive(GtkWidget *widget, GdkDragContext *drag_context,
                        gint x, gint y, GtkSelectionData *seldata,
                        guint info, guint time, struct slide_sorter *n)
{
	printf("Receive %i\n", n->drag_preview_pending);

	if ( n->drag_preview_pending ) {

		n->have_drag_data = 1;
		n->drag_preview_pending = 0;
		printf("Got preview data.\n");

	} else {

		printf("Drop! Got data\n");
		n->dragging = 0;
		gtk_drag_finish(drag_context, TRUE, TRUE, time);

	}

}


static void dnd_get(GtkWidget *widget, GdkDragContext *drag_context,
                    GtkSelectionData *seldata, guint info, guint time,
                    struct slide_sorter *n)
{
	printf("Get data!\n");

	gtk_selection_data_set_text(seldata, "Hello!", -1);
}


static void dnd_leave(GtkWidget *widget, GdkDragContext *drag_context,
                      guint time, struct slide_sorter *n)
{
	printf("Leave.\n");

	n->have_drag_data = 0;
}


static void dnd_end(GtkWidget *widget, GdkDragContext *drag_context,
                    struct slide_sorter *n)
{
	printf("End.\n");
	n->dragging = 0;
	n->have_drag_data = 0;
	n->drag_preview_pending = 0;
}


void open_slidesorter(struct presentation *p)
{
	struct slide_sorter *n;
	GtkWidget *sw;
	GtkTargetEntry targets[1];

	if ( p->slide_sorter != NULL ) return;  /* Already open */

	n = malloc(sizeof(struct slide_sorter));
	if ( n == NULL ) return;
	p->slide_sorter = n;

	n->p = p;
	n->width = 6;
	n->bw = 5;
	n->selection = 0;
	n->tw = p->thumb_slide_width;
	n->th = (p->slide_height/p->slide_width) * p->thumb_slide_width;
	n->drag_preview_pending = 0;
	n->have_drag_data = 0;
	n->dragging = 0;

	n->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(n->window), 500, 500);

	n->da = gtk_drawing_area_new();

	sw = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
	                               GTK_POLICY_NEVER,
	                               GTK_POLICY_ALWAYS);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), n->da);
	gtk_container_add(GTK_CONTAINER(n->window), sw);

	gtk_window_set_title(GTK_WINDOW(n->window), "Slide sorter");

	g_signal_connect(G_OBJECT(n->da), "draw", G_CALLBACK(draw_sig), n);
	g_signal_connect(G_OBJECT(n->window),
	                 "size-allocate", G_CALLBACK(size_sig), n);
	g_signal_connect(G_OBJECT(n->window), "destroy",
	                 G_CALLBACK(close_slidesorter_sig), p);

	g_signal_connect(G_OBJECT(n->da), "button-press-event",
	                 G_CALLBACK(button_press_sig), n);
	g_signal_connect(G_OBJECT(n->da), "motion-notify-event",
	                 G_CALLBACK(motion_sig), n);
	g_signal_connect(G_OBJECT(n->da), "button-release-event",
	                 G_CALLBACK(button_release_sig), n);

	/* Drag and drop */
	targets[0].target = "application/vnd.colloquium-slide";
	targets[0].flags = 0;
	targets[0].info = 1;
	gtk_drag_dest_set(n->da, 0, targets, 1,
	                  GDK_ACTION_MOVE);
	g_signal_connect(n->da, "drag-data-received",
	                 G_CALLBACK(dnd_receive), n);
	g_signal_connect(n->da, "drag-data-get", G_CALLBACK(dnd_get), n);
	g_signal_connect(n->da, "drag-motion", G_CALLBACK(dnd_motion), n);
	g_signal_connect(n->da, "drag-drop", G_CALLBACK(dnd_drop), n);
	g_signal_connect(n->da, "drag-leave", G_CALLBACK(dnd_leave), n);
	g_signal_connect(n->da, "drag-end", G_CALLBACK(dnd_end), n);

	gtk_widget_set_can_focus(GTK_WIDGET(n->da), TRUE);
	gtk_widget_add_events(GTK_WIDGET(n->da),
	                      GDK_POINTER_MOTION_HINT_MASK
	                       | GDK_BUTTON1_MOTION_MASK
	                       | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
	                       | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);

	gtk_widget_show_all(n->window);
}