summaryrefslogtreecommitdiff
path: root/gui/sheet.c
blob: ec83a7af13417d9f5b3d7caf1b0244d834df3aaa (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * 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 "gfx/record.h"
#include "gfx/gfx.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/pop.h"
#include "gui/input.h"
#include "gui/help.h"
#include "gui/icons.h"
#include "gui/timer.h"
#include "gui/clipboard.h"
#include "gui/common.h"


#define	ZOOM_FACTOR	1.26	/* 2^(1/3) */
#define	ZOOM_MAX	1
#define	ZOOM_MIN_SIZE	16
#define	ZOOM_MARGIN	0.95


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


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

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


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


static bool zoom_in(struct gui *gui, int x, int y)
{
	float old = gui->scale;

	if (gui->scale == ZOOM_MAX)
		return 0;
	gui->scale *= ZOOM_FACTOR;
	if (gui->scale > ZOOM_MAX)
		gui->scale = ZOOM_MAX;
	gui->x = x + (gui->x - x) * old / gui->scale;
	gui->y = y + (gui->y - y) * old / gui->scale;
	redraw(gui);
	return 1;
}


static bool zoom_out(struct gui *gui, int x, int y)
{
	float old = gui->scale;

	if (gui->curr_sheet->w * gui->scale <= ZOOM_MIN_SIZE)
		return 0;
	gui->scale /= ZOOM_FACTOR;
	if (gui->curr_sheet->w <= ZOOM_MIN_SIZE)
		gui->scale = 1;
	else if (gui->curr_sheet->w * gui->scale <= ZOOM_MIN_SIZE )
		gui->scale = (float) ZOOM_MIN_SIZE / gui->curr_sheet->w;
	gui->x = x + (gui->x - x) * old / gui->scale;
	gui->y = y + (gui->y - y) * old / gui->scale;
	redraw(gui);
	return 1;
}


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

	if (!gui->old_hist) {
		*w = sheet->w;
		*h = sheet->h;
	} else {
		const struct gui_sheet *old =
		    find_corresponding_sheet(gui->old_hist->sheets,
		    gui->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);
	}
}


void zoom_to_extents(struct gui *gui)
{
	GtkAllocation alloc;
	int w, h;
	float sw, sh;

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

	gtk_widget_get_allocation(gui->da, &alloc);

	sw = w ? (float) ZOOM_MARGIN * alloc.width / w : 1;
	sh = h ? (float) ZOOM_MARGIN * alloc.height / h : 1;
	gui->scale = sw < sh ? sw : sh;

	redraw(gui);
}


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


static bool show_history_details(void *user, bool on, int dx, int dy)
{
	struct gui_hist *h = user;
	struct gui *gui = h->gui;
	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(gui);
	return 1;
}


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


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

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


static void show_diff_cb(void *user)
{
	struct gui *gui = user;

	if (gui->old_hist)
		set_diff_mode(gui,
		    gui->diff_mode == diff_delta ? diff_new : diff_delta);
	else
		show_history(gui, sel_split);
}


static void toggle_old_new(struct gui *gui)
{
	set_diff_mode(gui, gui->diff_mode == diff_new ? diff_old : diff_new);
}


static void add_delta(struct gui *gui)
{
	struct overlay *over;
	struct overlay_style style;

	over = overlay_add(&gui->hist_overlays, &gui->aois,
	    NULL, show_diff_cb, gui);
	style = overlay_style_default;
	if (gui->old_hist && gui->diff_mode == diff_delta)
		style.frame = RGBA(0, 0, 0, 1);
	style.pad = 4;
	overlay_style(over, &style);
	if (use_delta)
		overlay_icon(over, icon_delta);
	else
		overlay_icon(over, icon_diff);
}


static void revision_overlays_diff(struct gui *gui)
{
	struct gui_hist *new = gui->new_hist;
	struct gui_hist *old = gui->old_hist;
	struct overlay_style style;

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

	add_delta(gui);

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


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

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

		add_delta(gui);
	}
}


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


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 close_subsheet(void *user)
{
	struct gui_sheet *sheet = user;
	struct gui *gui = sheet->gui;
	struct gui_sheet *parent;

	parent = find_parent_sheet(gui->new_hist->sheets, sheet);
	if (parent)
		go_to_sheet(gui, parent);
	else
		show_index(gui);
}


static bool hover_sheet(void *user, bool on, int dx, int dy)
{
	struct gui_sheet *sheet = user;
	struct gui *gui = sheet->gui;
	const char *title = sheet->sch->title;

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

		for (s = gui->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>%s%s",
		    title, this, n,
		    sheet->sch->file ? "\n" : "",
		    sheet->sch->file ? sheet->sch->file : "");
	} else {
		overlay_text(sheet->over, "<b>%s</b>", title);
	}
	redraw(gui);
	return 1;
}


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

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


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


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


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


static bool go_up_sheet(struct gui *gui)
{
	struct gui_sheet *parent;

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


static bool go_prev_sheet(struct gui *gui)
{
	struct gui_sheet *sheet;

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


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


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


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

	canvas_coord(gui, x, y, &ex, &ey);
	ex += curr_sheet->xmin;
	ey += curr_sheet->ymin;

	if (gui->old_hist && gui->diff_mode == diff_old)
		curr_sheet = find_corresponding_sheet(gui->old_hist->sheets,
		    gui->new_hist->sheets, gui->curr_sheet);

	if (aoi_click(&gui->aois, x, y))
		return 1;
	if (aoi_click(&curr_sheet->aois, ex, ey))
		return 1;

	if (gui->pop_overlays || gui->pop_underlays) {
		overlay_remove_all(&gui->pop_overlays);
		overlay_remove_all(&gui->pop_underlays);
		redraw(gui);
		return 1;
	}

	return 1;
}


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

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

	if (gui->old_hist && gui->diff_mode == diff_old)
		curr_sheet = find_corresponding_sheet(gui->old_hist->sheets,
		    gui->new_hist->sheets, gui->curr_sheet);

	if (aoi_hover(&gui->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)
{
	struct gui *gui = user;
	const struct gui_sheet *curr_sheet = gui->curr_sheet;
	int ex, ey;
	struct record_bbox rec_bbox;

	dehover_pop(user);

	canvas_coord(gui, x, y, &ex, &ey);
	ex += curr_sheet->xmin;
	ey += curr_sheet->ymin;

	if (gui->old_hist && gui->diff_mode == diff_old)
		curr_sheet = find_corresponding_sheet(gui->old_hist->sheets,
		    gui->new_hist->sheets, gui->curr_sheet);

	gui->drag_text = record_find_text_bbox(gfx_user(curr_sheet->gfx),
	    show_extra, ex, ey, &rec_bbox);
	if (gui->drag_text) {
		struct overlay *over;
		struct overlay_style style = {
			.radius	= 5,
			.pad	= 3,
			.skip	= 0,
			.fg	= RGBA(0, 0, 0, 0),
			.bg	= RGBA(0, 0, 0, 0.15),
			.frame	= RGBA(0, 0, 0, 0),
			.width	= 0
		};
		struct dwg_bbox bbox = {
			.x	= rec_bbox.xmin,
			.y	= rec_bbox.ymin,
			.w	= rec_bbox.xmax - rec_bbox.xmin,
			.h	= rec_bbox.ymax - rec_bbox.ymin,
		};

		over = overlay_add(&gui->pop_overlays, NULL, NULL, NULL, NULL);
		place_pop_cover(gui, &bbox, &style);
		overlay_style(over, &style);
		redraw(gui);
	}

	return 1;
}


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

	if (gui->drag_text)
		return;

	gui->x -= dx / gui->scale;
	gui->y -= dy / gui->scale;
	redraw(gui);
}


static void sheet_drag_end(void *user)
{
	struct gui *gui = user;

	if (gui->drag_text) {
		overlay_remove_all(&gui->pop_overlays);
		copy_to_clipboard(gui->drag_text);
		gui->drag_text = NULL;
		redraw(gui);
		
	}
	input_update();
}


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


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


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

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

	switch (keyval) {
	case '+':
	case '=':
	case GDK_KEY_KP_Add:
	case GDK_KEY_KP_Equal:
		zoom_in(gui, x, y);
		break;
	case '-':
	case GDK_KEY_KP_Subtract:
		zoom_out(gui, x, y);
		break;
	case '*':
	case GDK_KEY_KP_Multiply:
		zoom_to_extents(gui);
		break;

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

	case GDK_KEY_Escape:
		dehover_pop(user);
		gui->glabel = NULL;
		redraw(gui);
		break;

	case GDK_KEY_e:
		show_extra ^= gfx_pin_type;
		redraw(gui);
		break;
	case GDK_KEY_f:
		show_extra ^= gfx_footprint;
		redraw(gui);
		break;
	case GDK_KEY_R:	/* Shift + R */
		show_extra ^= gfx_reference;
		redraw(gui);
		break;

	case GDK_KEY_n:
		gui->diff_mode = diff_new;
		do_revision_overlays(gui);
		redraw(gui);
		break;
	case GDK_KEY_o:
		gui->diff_mode = diff_old;
		do_revision_overlays(gui);
		redraw(gui);
		break;
	case GDK_KEY_d:
		gui->diff_mode = diff_delta;
		do_revision_overlays(gui);
		redraw(gui);
		break;
	case GDK_KEY_D:	/* Shift + D */
		use_delta = !use_delta;
		do_revision_overlays(gui);
		redraw(gui);
		break;

	case GDK_KEY_h:
	case GDK_KEY_Help:
		help();
		break;

	case GDK_KEY_t:
		timer_toggle();
		redraw(gui);
		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,
};


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


void sheet_setup(struct gui *gui)
{
	input_push(&sheet_input_ops, gui);
}