summaryrefslogtreecommitdiff
path: root/gui/index.c
blob: e4a97c1fff640ae9590730a7c20be86bbbc01481 (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
/*
 * gui/index.c - Sheet index
 *
 * 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 <stdbool.h>
#include <assert.h>

#include <gtk/gtk.h>

#include "gfx/record.h"
#include "kicad/delta.h"
#include "gui/aoi.h"
#include "gui/style.h"
#include "gui/over.h"
#include "gui/input.h"
#include "gui/help.h"
#include "gui/common.h"


#define	SHEET_MAX_W	240
#define	SHEET_MAX_NAME	300
#define	SHEET_ASPECT	1.4146	/* width / height */
#define	SHEET_PAD	3
#define	SHEET_GAP	12	/* not counting the padding ! */
#define	INDEX_MARGIN	10	/* margin on each side */


/* @@@ clean all this up and move into struct gui */
static unsigned thumb_rows, thumb_cols;
static unsigned thumb_w, thumb_h;
static struct overlay *name_over = NULL;
static const struct gui_sheet *curr_sheet = NULL;


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


static void thumbnail_pos(GtkAllocation *alloc, unsigned n, int *ix, int *iy)
{
	*ix = alloc->width / 2 + (thumb_w + SHEET_GAP) *
	    (n % thumb_cols - (thumb_cols - 1) / 2.0);
	*iy = alloc->height / 2 + (thumb_h + SHEET_GAP) *
	    (n / thumb_cols - (thumb_rows - 1) / 2.0);
}


/* ----- Drawing ----------------------------------------------------------- */


void index_draw_event(const struct gui *gui, cairo_t *cr)
{
	GtkAllocation alloc;
	const struct gui_sheet *sheet;
	unsigned n = 0;
	int ix, iy, x, y;
	int w, h;
	int named = -1;

	gtk_widget_get_allocation(gui->da, &alloc);

	cairo_set_source_rgba(cr, 1, 1, 1, 0.7);
	cairo_paint(cr);

	n = 0;
	for (sheet = sheets(gui); sheet; sheet = sheet->next) {
		thumbnail_pos(&alloc, n, &ix, &iy);
		x = ix - thumb_w / 2 - SHEET_PAD;
		y = iy - thumb_h / 2 - SHEET_PAD;

		overlay_draw(sheet->thumb_over, cr, x, y, 1, 1);
		if (name_over && curr_sheet == sheet)
			named = n;
		n++;
	}

	if (named == -1)
		return;

	thumbnail_pos(&alloc, named, &ix, &iy);
	overlay_size(name_over,
	    gtk_widget_get_pango_context(curr_sheet->gui->da), &w, &h);
	x = ix - w / 2;
	if (x < INDEX_MARGIN)
		x = INDEX_MARGIN;
	if (x + w >= alloc.width - INDEX_MARGIN)
		x = alloc.width - w - INDEX_MARGIN;
	overlay_draw(name_over, cr, x, iy - h / 2, 1, 1);
}


/* ----- Thumbnail actions ------------------------------------------------- */


static void close_index(struct gui *gui)
{
	overlay_remove_all(&gui->thumb_overlays);
	name_over = NULL;
	gui->mode = showing_sheet;
	input_pop();
	redraw(gui);
}


static void thumb_click(void *user)
{
	struct gui_sheet *sheet = user;
	struct gui *gui = sheet->gui;

	go_to_sheet(gui, sheet);
	close_index(gui);
}


static void thumb_set_style(struct gui_sheet *sheet, bool selected)
{
	struct overlay_style style = overlay_style_dense;

	style.radius = 3;
	style.pad = SHEET_PAD;
	style.bg = RGBA(1, 1, 1, 0.8);

	if (selected) {
		style.width = 2;
		style.frame = RGBA(0, 0, 0, 1);
		style.bg = RGBA(1, 1, 1, 1);
	}

	if (sheet->thumb_yellow)
		style.bg = RGBA(1.0, 1.0, 0, 1);

	overlay_style(sheet->thumb_over, &style);
}


static bool thumb_hover(void *user, bool on, int dx, int dy)
{
	struct gui_sheet *sheet = user;
	struct gui *gui = sheet->gui;
	struct overlay_style style = overlay_style_default;
	const char *file;

	if (on) {
		thumb_set_style(sheet, 1);
		name_over = overlay_add(&gui->thumb_overlays, &gui->aois,
		    NULL, NULL, NULL);
		file = sheet->sch->file;
		if (sheet->sch && sheet->sch->title)
			overlay_text(name_over, "%s<small>%s%s</small>",
			    sheet->sch->title,
			    file ? "\n" : "", file ? file : "");
		else
			overlay_text(name_over, "%s", file ? file : "");
		style.font = BOLD_FONT_LARGE;
		style.width = 1;
		style.wmax = SHEET_MAX_NAME;
		overlay_style(name_over, &style);
		curr_sheet = sheet;
	} else {
		thumb_set_style(sheet, 0);
		overlay_remove(&gui->thumb_overlays, name_over);
		name_over = NULL;
	}
	redraw(gui);
	return 1;
}


/* ----- Rendering to cache ------------------------------------------------ */


static bool best_ratio(const struct gui *gui)
{
	GtkAllocation alloc;
	const struct gui_sheet *sheet;
	unsigned n = 0;
	unsigned r, c;
	float size, best_size = 0;
	int aw, ah;	/* available size */
	int w, h;

	gtk_widget_get_allocation(gui->da, &alloc);

	for (sheet = sheets(gui); sheet; sheet = sheet->next)
		n++;
	assert(n);

	for (r = 1; r <= n; r++) {
		c = (n + r - 1) / r;
		aw = alloc.width - (c - 1) * SHEET_GAP - 2 * INDEX_MARGIN;
		ah = alloc.height - (r - 1) * SHEET_GAP - 2 * INDEX_MARGIN;
		if (aw < 0 || ah < 0)
			continue;
		w = aw / c;
		h = ah / r;
		if (w > SHEET_MAX_W)
			w = SHEET_MAX_W;
		if (h * SHEET_ASPECT > w)
			 h = w / SHEET_ASPECT;
		if (w / SHEET_ASPECT > h)
			w = h * SHEET_ASPECT;
		if (!w || !h)
			continue;
		size = ((c - 1) * (w + SHEET_GAP) + w) *
		    ((r - 1) * (h + SHEET_GAP) + h);
		if (size > best_size) {
			best_size = size;
			thumb_cols = c;
			thumb_rows = r;
			thumb_w = w;
			thumb_h = h;
		}
	}

	return best_size;
}


static void index_render_sheet(const struct gui *gui,
    struct gui_sheet *sheet)
{
	int xmin, ymin, w, h;
	float fw, fh, f;
	bool yellow = 0;
	int xo, yo;
	int stride;

	if (!sheet->gfx_thumb) {
		sheet->gfx_thumb = gfx_init(&cro_canvas_ops);
		sch_render(sheet->sch, sheet->gfx_thumb);
		cro_canvas_end(gfx_user(sheet->gfx_thumb),
		    NULL, NULL, NULL, NULL);
	}

	if (gui->old_hist && gui->diff_mode == diff_delta) {
		const struct gui_sheet *old;

		old = find_corresponding_sheet(gui->old_hist->sheets,
		    gui->new_hist->sheets, sheet);
		if (!sheet_eq(sheet->sch, old->sch, 0))
			yellow = 1;
	}

	if (sheet->thumb_surf &&
	    sheet->thumb_w == thumb_w && sheet->thumb_h == thumb_h &&
	    sheet->thumb_yellow == yellow)
		return;

	if (sheet->thumb_surf) {
		// @@@ free data
		cairo_surface_destroy(sheet->thumb_surf);
		sheet->thumb_surf = NULL;
	}

	record_bbox((const struct record *) gfx_user(sheet->gfx_thumb),
	    &xmin, &ymin, &w, &h);
	if (!w || !h)
		return;

	fw = (float) thumb_w / w;
	fh = (float) thumb_h / h;
	f = fw < fh ? fw : fh;

	xo = -(xmin + w / 2) * f + thumb_w / 2;
	yo = -(ymin + h / 2) * f + thumb_h / 2;
	cro_img(gfx_user(sheet->gfx_thumb), 0,
	    xo, yo, thumb_w, thumb_h,  f, 0, NULL, &stride);

	sheet->thumb_surf = cro_img_surface(gfx_user(sheet->gfx_thumb));
	sheet->thumb_w = thumb_w;
	sheet->thumb_h = thumb_h;
	sheet->thumb_yellow = yellow;
}


static void index_add_overlay(struct gui *gui, struct gui_sheet *sheet)
{
	sheet->thumb_over = overlay_add(&gui->thumb_overlays, &gui->aois,
	    thumb_hover, thumb_click, sheet);
	overlay_icon(sheet->thumb_over, sheet->thumb_surf);
	thumb_set_style(sheet, 0);
}


static void index_render_sheets(struct gui *gui)
{
	struct gui_sheet *sheet;

	for (sheet = sheets(gui); sheet; sheet = sheet->next) {
		index_render_sheet(gui, sheet);
		index_add_overlay(gui, sheet);
	}
}


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


static bool index_hover_update(void *user, int x, int y)
{
	struct gui *gui = user;

	if (aoi_hover(&gui->aois, x, y))
		return 1;
	return 0;
}


static bool index_click(void *user, int x, int y)
{
	struct gui *gui = user;

	if (aoi_click(&gui->aois, x, y))
		return 1;
	close_index(gui);
	return 1;
}


static void index_key(void *user, int x, int y, int keyval)
{
	struct gui *gui = user;

	switch (keyval) {
	case GDK_KEY_Escape:
		gui->mode = showing_sheet;
		input_pop();
		redraw(gui);
		break;

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

	case GDK_KEY_q:
		gtk_main_quit();
	}
}


static const struct input_ops index_input_ops = {
	.click		= index_click,
	.hover_begin	= index_hover_update,
	.hover_update	= index_hover_update,
	.hover_click	= index_click,
	.key		= index_key,
};


/* ----- Resizing ---------------------------------------------------------- */


void index_resize(struct gui *gui)
{
	overlay_remove_all(&gui->thumb_overlays);
	name_over = NULL;
	if (best_ratio(gui))
		index_render_sheets(gui);
	else
		close_index(gui);
	redraw(gui);
}


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


void show_index(struct gui *gui)
{
	input_push(&index_input_ops, gui);
	gui->mode = showing_index;
	index_resize(gui);
}