summaryrefslogtreecommitdiff
path: root/gui/sheet.c
blob: 53858501d1a8a0044f8eaba23beef670bce395a4 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * gui/sheet.c - Sheet navigation
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 */

#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>

#include <gtk/gtk.h>

#include "file/git-hist.h"
#include "kicad/sch.h"
#include "kicad/delta.h"
#include "gui/fmt-pango.h"
#include "gui/style.h"
#include "gui/aoi.h"
#include "gui/over.h"
#include "gui/input.h"
#include "gui/common.h"


/* ----- Tools ------------------------------------------------------------- */


static void canvas_coord(const struct gui_ctx *ctx,
    int ex, int ey, int *x, int *y)
{
	GtkAllocation alloc;
	int sx, sy;

	gtk_widget_get_allocation(ctx->da, &alloc);
	sx = ex - alloc.width / 2;
	sy = ey - alloc.height / 2;
	*x = (sx << ctx->zoom) + ctx->x;
	*y = (sy << ctx->zoom) + ctx->y;
}


/* ----- Zoom -------------------------------------------------------------- */


static bool zoom_in(struct gui_ctx *ctx, int x, int y)
{
	if (ctx->zoom == 0)
		return 0;
	ctx->zoom--;
	ctx->x = (ctx->x + x) / 2;
	ctx->y = (ctx->y + y) / 2;
	redraw(ctx);
	return 1;
}


static bool zoom_out(struct gui_ctx *ctx, int x, int y)
{
	if (ctx->curr_sheet->w >> ctx->zoom <= 16)
		return 0;
	ctx->zoom++;
	ctx->x = 2 * ctx->x - x;
	ctx->y = 2 * ctx->y - y;
	redraw(ctx);
	return 1;
}


static void curr_sheet_size(struct gui_ctx *ctx, int *w, int *h)
{
	const struct gui_sheet *sheet = ctx->curr_sheet;
	int ax1, ay1, bx1, by1;

	if (!ctx->old_hist) {
		*w = sheet->w;
		*h = sheet->h;
	} else {
		const struct gui_sheet *old =
		    find_corresponding_sheet(ctx->old_hist->sheets,
		    ctx->new_hist->sheets, sheet);

		/*
		 * We're only interested in differences here, so no need for
		 * the usual "-1" in x1 = x0 + w - 1
		 */
		ax1 = sheet->xmin + sheet->w;
		ay1 = sheet->ymin + sheet->h;
		bx1 = old->xmin + old->w;
		by1 = old->ymin + old->h;
		*w = (ax1 > bx1 ? ax1 : bx1) -
		    (sheet->xmin < old->xmin ? sheet->xmin : old->xmin);
		*h = (ay1 > by1 ? ay1 : by1) -
		    (sheet->ymin < old->ymin ? sheet->ymin : old->ymin);
	}
}


static void zoom_to_extents(struct gui_ctx *ctx)
{
	GtkAllocation alloc;
	int w, h;

	curr_sheet_size(ctx, &w, &h);
	ctx->x = w / 2;
	ctx->y = h / 2;

	gtk_widget_get_allocation(ctx->da, &alloc);
	ctx->zoom = 0;
	while (w >> ctx->zoom > alloc.width || h >> ctx->zoom > alloc.height)
		ctx->zoom++;

	redraw(ctx);
}


/* ----- Revision selection overlays --------------------------------------- */


static bool show_history_details(void *user, bool on)
{
	struct gui_hist *h = user;
	struct gui_ctx *ctx = h->ctx;
	char *s;

	if (on) {
		s = vcs_git_long_for_pango(h->vcs_hist, fmt_pango);
		overlay_text_raw(h->over, s);
		free(s);
	} else {
		overlay_text(h->over, "%.40s", vcs_git_summary(h->vcs_hist));
	}
	redraw(ctx);
	return 1;
}


static void set_diff_mode(struct gui_ctx *ctx, enum diff_mode mode)
{
	ctx->diff_mode = mode;
	do_revision_overlays(ctx);
	redraw(ctx);
}


static void show_history_cb(void *user)
{
	struct gui_hist *h = user;
	struct gui_ctx *ctx = h->ctx;
	enum selecting sel = sel_only;

	if (ctx->old_hist) {
		if (h == ctx->new_hist && ctx->diff_mode != diff_new) {
			set_diff_mode(ctx, diff_new);
			return;
		}
		if (h == ctx->old_hist && ctx->diff_mode != diff_old) {
			set_diff_mode(ctx, diff_old);
			return;
		}
		sel = h == ctx->new_hist ? sel_new : sel_old;
	}
	show_history(ctx, sel);
}


static void show_diff_cb(void *user)
{
	struct gui_ctx *ctx = user;

	set_diff_mode(ctx,
	    ctx->diff_mode == diff_delta ? diff_new : diff_delta);
}


static void revision_overlays_diff(struct gui_ctx *ctx)
{
	struct gui_hist *new = ctx->new_hist;
	struct gui_hist *old = ctx->old_hist;
	struct overlay *over;
	struct overlay_style style;

	new->over = overlay_add(&ctx->hist_overlays, &ctx->aois,
	    show_history_details, show_history_cb, new);
	style = overlay_style_diff_new;
	if (ctx->diff_mode == diff_new)
		style.frame = RGBA(0, 0, 0, 1);
	overlay_style(new->over, &style);
	show_history_details(new, 0);

	over = overlay_add(&ctx->hist_overlays, &ctx->aois,
	    NULL, show_diff_cb, ctx);
	style = overlay_style_default;
	if (ctx->diff_mode == diff_delta)
		style.frame = RGBA(0, 0, 0, 1);
	overlay_style(over, &style);
	overlay_text(over, "&#916;");

	old->over = overlay_add(&ctx->hist_overlays, &ctx->aois,
	    show_history_details, show_history_cb, old);
	style = overlay_style_diff_old;
	if (ctx->diff_mode == diff_old)
		style.frame = RGBA(0, 0, 0, 1);
	overlay_style(old->over, &style);
	show_history_details(old, 0);
}


void do_revision_overlays(struct gui_ctx *ctx)
{
	overlay_remove_all(&ctx->hist_overlays);

	if (ctx->old_hist) {
		revision_overlays_diff(ctx);
	} else {
		ctx->new_hist->over = overlay_add(&ctx->hist_overlays,
		    &ctx->aois, show_history_details, show_history_cb,
		    ctx->new_hist);
		overlay_style(ctx->new_hist->over, &overlay_style_default);
		show_history_details(ctx->new_hist, 0);
	}
}


/* ----- Sheet selection overlays ------------------------------------------ */


static void close_subsheet(void *user)
{
	struct gui_sheet *sheet = user;
	struct gui_ctx *ctx = sheet->ctx;

	go_to_sheet(ctx, sheet);
}


static bool hover_sheet(void *user, bool on)
{
	struct gui_sheet *sheet = user;
	struct gui_ctx *ctx = sheet->ctx;
	const char *title = sheet->sch->title;

	if (!title)
		title = "(unnamed)";
	if (on) {
		const struct gui_sheet *s;
		int n = 0, this = -1;

		for (s = ctx->new_hist->sheets; s; s = s->next) {
			n++;
			if (s == sheet)
				this = n;
		}
		overlay_text(sheet->over, "<b>%s</b>\n<big>%d / %d</big>",
		    title, this, n);
	} else {
		overlay_text(sheet->over, "<b>%s</b>", title);
	}
	redraw(ctx);
	return 1;
}


static struct gui_sheet *find_parent_sheet(struct gui_sheet *sheets,
    const struct gui_sheet *ref)
{
	struct gui_sheet *parent;
	const struct sch_obj *obj;

	for (parent = sheets; parent; parent = parent->next)
		for (obj = parent->sch->objs; obj; obj = obj->next)
			if (obj->type == sch_obj_sheet &&
			    obj->u.sheet.sheet == ref->sch)
				return parent;
	return NULL;
}


static void sheet_selector_recurse(struct gui_ctx *ctx, struct gui_sheet *sheet)
{
	struct gui_sheet *parent;

	parent = find_parent_sheet(ctx->new_hist->sheets, sheet);
	if (parent)
		sheet_selector_recurse(ctx, parent);
	sheet->over = overlay_add(&ctx->sheet_overlays, &ctx->aois,
	    hover_sheet, close_subsheet, sheet);
	hover_sheet(sheet, 0);
}


static void do_sheet_overlays(struct gui_ctx *ctx)
{
	overlay_remove_all(&ctx->sheet_overlays);
	sheet_selector_recurse(ctx, ctx->curr_sheet);
}


/* ----- Navigate sheets --------------------------------------------------- */


void go_to_sheet(struct gui_ctx *ctx, struct gui_sheet *sheet)
{
	aoi_dehover();
	overlay_remove_all(&ctx->pop_overlays);
	overlay_remove_all(&ctx->pop_underlays);
	if (!sheet->rendered) {
		render_sheet(sheet);
		mark_aois(ctx, sheet);
	}
	ctx->curr_sheet = sheet;
	if (ctx->old_hist)
		render_delta(ctx);
	if (ctx->vcs_hist)
		do_revision_overlays(ctx);
	do_sheet_overlays(ctx);
	zoom_to_extents(ctx);
}


static bool go_up_sheet(struct gui_ctx *ctx)
{
	struct gui_sheet *parent;

	parent = find_parent_sheet(ctx->new_hist->sheets, ctx->curr_sheet);
	if (!parent)
		return 0;
	go_to_sheet(ctx, parent);
	return 1;
}


static bool go_prev_sheet(struct gui_ctx *ctx)
{
	struct gui_sheet *sheet;

	for (sheet = ctx->new_hist->sheets; sheet; sheet = sheet->next)
		if (sheet->next && sheet->next == ctx->curr_sheet) {
			go_to_sheet(ctx, sheet);
			return 1;
		}
	return 0;
}


static bool go_next_sheet(struct gui_ctx *ctx)
{
	if (!ctx->curr_sheet->next)
		return 0;
	go_to_sheet(ctx, ctx->curr_sheet->next);
	return 1;
}


/* ----- Input ------------------------------------------------------------- */


static bool sheet_click(void *user, int x, int y)
{
	struct gui_ctx *ctx = user;
	const struct gui_sheet *curr_sheet = ctx->curr_sheet;
	int ex, ey;

	canvas_coord(ctx, x, y, &ex, &ey);

	if (aoi_click(ctx->aois, x, y))
		return 1;
	if (aoi_click(curr_sheet->aois,
	    ex + curr_sheet->xmin, ey + curr_sheet->ymin))
		return 1;

	overlay_remove_all(&ctx->pop_overlays);
	overlay_remove_all(&ctx->pop_underlays);
	redraw(ctx);
	return 1;
}


static bool sheet_hover_update(void *user, int x, int y)
{
	struct gui_ctx *ctx = user;
	const struct gui_sheet *curr_sheet = ctx->curr_sheet;
	int ex, ey;

	canvas_coord(ctx, x, y, &ex, &ey);

	if (aoi_hover(ctx->aois, x, y))
		return 1;
	return aoi_hover(curr_sheet->aois,
	    ex + curr_sheet->xmin, ey + curr_sheet->ymin);
}


static bool sheet_drag_begin(void *user, int x, int y)
{
	dehover_glabel(user);
	return 1;
}


static void sheet_drag_move(void *user, int dx, int dy)
{
	struct gui_ctx *ctx = user;

	ctx->x -= dx << ctx->zoom;
	ctx->y -= dy << ctx->zoom;
	redraw(ctx);
}


static void sheet_drag_end(void *user)
{
	input_update();
}


static void sheet_scroll(void *user, int x, int y, int dy)
{
	struct gui_ctx *ctx = user;
	int ex, ey;


	canvas_coord(ctx, x, y, &ex, &ey);
	if (dy < 0) {
		if (!zoom_in(ctx, ex, ey))
			return;
	} else {
		if (!zoom_out(ctx, ex, ey))
			return;
	}
	dehover_glabel(ctx);
	input_update();
}


static void sheet_key(void *user, int x, int y, int keyval)
{
	struct gui_ctx *ctx = user;
	struct gui_sheet *sheet = ctx->curr_sheet;
	int ex, ey;

	canvas_coord(ctx, x, y, &ex, &ey);

	switch (keyval) {
	case '+':
	case '=':
		zoom_in(ctx, x, y);
		break;
	case '-':
		zoom_out(ctx, x, y);
		break;
	case '*':
		zoom_to_extents(ctx);
		break;

	case GDK_KEY_Home:
	case GDK_KEY_KP_Home:
		if (sheet != ctx->new_hist->sheets)
			go_to_sheet(ctx, ctx->new_hist->sheets);
		break;
	case GDK_KEY_BackSpace:
	case GDK_KEY_Delete:
	case GDK_KEY_KP_Delete:
		go_up_sheet(ctx);
		break;
	case GDK_KEY_Page_Up:
	case GDK_KEY_KP_Page_Up:
		go_prev_sheet(ctx);
		break;
	case GDK_KEY_Page_Down:
	case GDK_KEY_KP_Page_Down:
		go_next_sheet(ctx);
		break;
	case GDK_KEY_Up:
	case GDK_KEY_KP_Up:
		show_history(ctx, sel_new);
		break;
	case GDK_KEY_Down:
	case GDK_KEY_KP_Down:
		show_history(ctx, sel_old);
		break;

	case GDK_KEY_n:
		ctx->diff_mode = diff_new;
		redraw(ctx);
		break;
	case GDK_KEY_o:
		ctx->diff_mode = diff_old;
		redraw(ctx);
		break;
	case GDK_KEY_d:
		ctx->diff_mode = diff_delta;
		redraw(ctx);
		break;

	case GDK_KEY_q:
		gtk_main_quit();
	}
}


static const struct input_ops sheet_input_ops = {
	.click		= sheet_click,
	.hover_begin	= sheet_hover_update,
	.hover_update	= sheet_hover_update,
	.hover_click	= sheet_click,
	.scroll		= sheet_scroll,
	.drag_begin	= sheet_drag_begin,
	.drag_move	= sheet_drag_move,
	.drag_end	= sheet_drag_end,
	.key		= sheet_key,
};


/* ----- Event handlers ---------------------------------------------------- */


static void size_allocate_event(GtkWidget *widget, GdkRectangle *allocation,
    gpointer data)
{
	struct gui_ctx *ctx = data;

	zoom_to_extents(ctx);
}


/* ----- Initialization ---------------------------------------------------- */


void sheet_setup(struct gui_ctx *ctx)
{
	g_signal_connect(G_OBJECT(ctx->da), "size_allocate",
	    G_CALLBACK(size_allocate_event), ctx);
	input_push(&sheet_input_ops, ctx);
}