summaryrefslogtreecommitdiff
path: root/liblogdiag/ld-category-symbol-view.c
blob: a2ae4cc9e5d4b45a05cfe03385d982fa6078afc8 (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
/*
 * ld-category-symbol-view.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2012. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include "liblogdiag.h"
#include "config.h"


/**
 * SECTION:ld-category-symbol-view
 * @short_description: A widget that displays symbols in a category
 * @see_also: #LdCategory, #LdDiagramView
 *
 * #LdCategorySymbolView allows the user to drag symbols from an #LdCategory
 * onto #LdDiagramView.
 */

/* Milimetres per inch. */
#define MM_PER_INCH 25.4
/* The default screen resolution in DPI units. */
#define DEFAULT_SCREEN_RESOLUTION 96

#define SYMBOL_WIDTH    50   /* Width  of a symbol. */
#define SYMBOL_HEIGHT   40   /* Height of a symbol. */
#define SYMBOL_SPACING  10   /* Spacing between symbols, and also borders. */

typedef struct
{
	LdSymbol *symbol;        /* The associated symbol, ref'ed. */
	gchar *path;             /* Path to the symbol. */

	GdkRectangle rect;       /* Clipping rectangle. */
	gdouble scale;           /* Scale to draw the symbol in. */
	gdouble dx, dy;          /* Delta into .rect. */
}
SymbolData;

/*
 * LdCategorySymbolViewPrivate:
 * @category: a category object assigned as a model.
 * @path: path to the category within the library.
 * @layout: (element-type SymbolData *): current layout of symbols.
 * @preselected: currently preselected symbol.
 * @height_negotiation: whether we are negotiating height right now.
 */
struct _LdCategorySymbolViewPrivate
{
	LdCategory *category;
	gchar *path;
	GSList *layout;
	SymbolData *preselected;
	guint height_negotiation : 1;
};

enum
{
	PROP_0,
	PROP_CATEGORY
};

static void ld_category_symbol_view_get_property (GObject *object,
	guint property_id, GValue *value, GParamSpec *pspec);
static void ld_category_symbol_view_set_property (GObject *object,
	guint property_id, const GValue *value, GParamSpec *pspec);
static void ld_category_symbol_view_finalize (GObject *gobject);

static void ld_category_symbol_view_set_category
	(LdCategoryView *iface, LdCategory *category);
static LdCategory *ld_category_symbol_view_get_category
	(LdCategoryView *iface);


static void
ld_category_view_init (LdCategoryViewInterface *iface)
{
	iface->set_category = ld_category_symbol_view_set_category;
	iface->get_category = ld_category_symbol_view_get_category;
}

G_DEFINE_TYPE_WITH_CODE (LdCategorySymbolView,
	ld_category_symbol_view, GTK_TYPE_DRAWING_AREA,
	G_IMPLEMENT_INTERFACE (LD_TYPE_CATEGORY_VIEW, ld_category_view_init));

static void
ld_category_symbol_view_class_init (LdCategorySymbolViewClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_category_symbol_view_get_property;
	object_class->set_property = ld_category_symbol_view_set_property;
	object_class->finalize = ld_category_symbol_view_finalize;

	g_object_class_override_property (object_class, PROP_CATEGORY, "category");

	g_type_class_add_private (klass, sizeof (LdCategorySymbolViewPrivate));
}

static void
symbol_redraw (LdCategorySymbolView *self, SymbolData *symbol)
{
	gtk_widget_queue_draw_area (GTK_WIDGET (self),
		symbol->rect.x,
		symbol->rect.y,
		symbol->rect.width,
		symbol->rect.height);
}

static void
symbol_deselect (LdCategorySymbolView *self)
{
	SymbolData *preselected;

	preselected = self->priv->preselected;
	if (!preselected)
		return;

	g_signal_emit (self, LD_CATEGORY_VIEW_GET_INTERFACE (self)->
		symbol_deselected_signal, 0, preselected->symbol, preselected->path);

	symbol_redraw (self, preselected);
	self->priv->preselected = NULL;
	gtk_drag_source_unset (GTK_WIDGET (self));
}

static void
symbol_data_free (SymbolData *self)
{
	g_object_unref (self->symbol);
	g_free (self->path);
	g_slice_free (SymbolData, self);
}

typedef struct
{
	guint total_height;      /* Total height required to show the symbols. */
	guint max_width;         /* Width available to the widget. */

	GSList *cur_row;         /* Current row of symbols. */
	guint cur_width;         /* Current width of the row. */
	guint cur_height_up;     /* Current max. upper height of symbols. */
	guint cur_height_down;   /* Current max. lower height of symbols. */
}
LayoutContext;

static void
layout_destroy (LdCategorySymbolView *self)
{
	symbol_deselect (self);

	g_slist_foreach (self->priv->layout, (GFunc) symbol_data_free, NULL);
	g_slist_free (self->priv->layout);
	self->priv->layout = NULL;
	self->priv->preselected = NULL;
}

static GSList *
layout_finish_row (LayoutContext *ctx)
{
	GSList *item, *result;
	gint row_height, h_delta;

	row_height = SYMBOL_SPACING + ctx->cur_height_up + ctx->cur_height_down;
	h_delta = (ctx->max_width - ctx->cur_width) / 2;

	for (item = ctx->cur_row; item; item = item->next)
	{
		SymbolData *data;

		data = item->data;
		data->rect.x += h_delta;
		data->rect.height = row_height;
		data->dy = SYMBOL_SPACING * 0.5 + ctx->cur_height_up;
	}

	result = g_slist_reverse (ctx->cur_row);

	ctx->cur_row = NULL;
	ctx->total_height += row_height;

	ctx->cur_width = SYMBOL_SPACING;
	ctx->cur_height_up = 0;
	ctx->cur_height_down = 0;

	return result;
}

static gint
layout_for_width (LdCategorySymbolView *self, gint width)
{
	GSList *symbols, *iter;
	LayoutContext ctx = {SYMBOL_SPACING, 0, NULL, SYMBOL_SPACING, 0, 0};

	layout_destroy (self);
	ctx.max_width = width;

	symbols = (GSList *) ld_category_get_symbols (self->priv->category);
	for (iter = symbols; iter; iter = iter->next)
	{
		SymbolData *data;
		LdRectangle area;
		LdSymbol *symbol;
		gint real_width, height_up, height_down;

		symbol = LD_SYMBOL (iter->data);
		ld_symbol_get_area (symbol, &area);

		data = g_slice_new (SymbolData);
		data->symbol = g_object_ref (symbol);
		data->path = g_build_path (LD_LIBRARY_IDENTIFIER_SEPARATOR,
			self->priv->path, ld_symbol_get_name (symbol), NULL);

		/* Compute the scale to fit the symbol to an area of
		 * SYMBOL_WIDTH * SYMBOL_HEIGHT, vertically centred. */
		data->scale = SYMBOL_HEIGHT * 0.5
			/ MAX (ABS (area.y), ABS (area.y + area.height)) * 0.5;
		if (data->scale * area.width > SYMBOL_WIDTH)
			data->scale = SYMBOL_WIDTH / area.width;

		real_width = data->scale * area.width + 0.5;
		data->rect.width = real_width + SYMBOL_SPACING;
		/* Now I have no idea what this does but it worked before.
		 * When I do, I have to write it in here. */
		data->dx = data->rect.width * 0.5 + data->scale
			* (area.width * 0.5 - ABS (area.x + area.width));

		if (ctx.cur_width + real_width + SYMBOL_SPACING > ctx.max_width
			&& ctx.cur_row != NULL)
		{
			self->priv->layout = g_slist_concat (self->priv->layout,
				layout_finish_row (&ctx));
		}

		/* Half of the spacing is included on each side of the rect. */
		data->rect.x = ctx.cur_width    - SYMBOL_SPACING / 2;
		data->rect.y = ctx.total_height - SYMBOL_SPACING / 2;

		height_up   = data->scale * ABS (area.y);
		height_down = data->scale * ABS (area.y + area.height);

		if (height_up   > ctx.cur_height_up)
			ctx.cur_height_up   = height_up;
		if (height_down > ctx.cur_height_down)
			ctx.cur_height_down = height_down;

		ctx.cur_row = g_slist_prepend (ctx.cur_row, data);
		ctx.cur_width += real_width + SYMBOL_SPACING;
	}

	if (ctx.cur_row != NULL)
		self->priv->layout = g_slist_concat (self->priv->layout,
			layout_finish_row (&ctx));

	return ctx.total_height;
}

static void
on_size_request (GtkWidget *widget, GtkRequisition *requisition,
	gpointer user_data)
{
	LdCategorySymbolView *self;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);

	if (!self->priv->category
	 || !ld_category_get_symbols (self->priv->category))
	{
		requisition->width  = 0;
		requisition->height = 0;
		return;
	}

	requisition->width = SYMBOL_WIDTH + 2 * SYMBOL_SPACING;

	if (self->priv->height_negotiation)
	{
		GtkAllocation alloc;

		gtk_widget_get_allocation (widget, &alloc);
		requisition->height = layout_for_width (self, alloc.width);
	}
	else
		requisition->height = SYMBOL_HEIGHT + 2 * SYMBOL_SPACING;
}

static void
on_size_allocate (GtkWidget *widget, GdkRectangle *allocation,
	gpointer user_data)
{
	LdCategorySymbolView *self;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);

	if (self->priv->height_negotiation)
		self->priv->height_negotiation = FALSE;
	else
	{
		self->priv->height_negotiation = TRUE;
		gtk_widget_queue_resize (widget);
	}
}

static gboolean
on_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
	LdCategorySymbolView *self;
	cairo_t *cr;
	GSList *iter;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);
	cr = gdk_cairo_create (gtk_widget_get_window (widget));
	gdk_cairo_rectangle (cr, &event->area);
	cairo_clip (cr);

	gdk_cairo_set_source_color (cr,
		&gtk_widget_get_style (widget)->base[GTK_STATE_NORMAL]);
	cairo_paint (cr);

	for (iter = self->priv->layout; iter; iter = iter->next)
	{
		SymbolData *data;

		data = iter->data;
		if (!gdk_rectangle_intersect (&data->rect, &event->area, NULL))
			continue;

		cairo_save (cr);
		gdk_cairo_rectangle (cr, &data->rect);
		cairo_clip (cr);

		gdk_cairo_set_source_color (cr,
			&gtk_widget_get_style (widget)->text[GTK_STATE_NORMAL]);

		if (data == self->priv->preselected)
			cairo_paint_with_alpha (cr, 0.1);

		cairo_translate (cr, data->rect.x + data->dx, data->rect.y + data->dy);
		cairo_scale (cr, data->scale, data->scale);

		cairo_set_line_width (cr, 1 / data->scale);
		ld_symbol_draw (data->symbol, cr);

		cairo_restore (cr);
	}

	cairo_destroy (cr);
	return FALSE;
}

static gboolean
on_motion_notify (GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
{
	LdCategorySymbolView *self;
	GSList *iter;

	if (event->state & GDK_BUTTON1_MASK)
		return FALSE;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);
	for (iter = self->priv->layout; iter; iter = iter->next)
	{
		SymbolData *data;

		data = iter->data;
		if (event->x <  data->rect.x
		 || event->y <  data->rect.y
		 || event->x >= data->rect.x + data->rect.width
		 || event->y >= data->rect.y + data->rect.height)
			continue;

		if (data != self->priv->preselected)
		{
			GtkTargetEntry target = {"ld-symbol", GTK_TARGET_SAME_APP, 0};

			symbol_deselect (self);
			self->priv->preselected = data;
			symbol_redraw (self, data);

			gtk_drag_source_set (widget,
				GDK_BUTTON1_MASK, &target, 1, GDK_ACTION_COPY);

			g_signal_emit (self, LD_CATEGORY_VIEW_GET_INTERFACE (self)->
				symbol_selected_signal, 0, data->symbol, data->path);
		}
		return FALSE;
	}

	symbol_deselect (self);
	return FALSE;
}

static gboolean
on_leave_notify (GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
{
	symbol_deselect (LD_CATEGORY_SYMBOL_VIEW (widget));
	return FALSE;
}

static void
on_drag_data_get
(GtkWidget *widget, GdkDragContext *ctx, GtkSelectionData *selection_data,
	guint target_type, guint time, gpointer user_data)
{
	LdCategorySymbolView *self;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);
	g_return_if_fail (self->priv->preselected != NULL);

	gtk_selection_data_set (selection_data,
		gtk_selection_data_get_target (selection_data),
		8, (guchar *) self->priv->preselected->path,
		strlen (self->priv->preselected->path));
}

static void
on_drag_begin (GtkWidget *widget, GdkDragContext *ctx, gpointer user_data)
{
	LdCategorySymbolView *self;
	GdkPixbuf *pbuf;

	self = LD_CATEGORY_SYMBOL_VIEW (widget);
	g_return_if_fail (self->priv->preselected != NULL);

	/* Some of the larger previews didn't work, and we have to get rid of
	 * the icon later when we're hovering above LdDiagramView anyway. */
	pbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
	gdk_pixbuf_fill (pbuf, 0x00000000);
	gtk_drag_set_icon_pixbuf (ctx, pbuf, 0, 0);
	g_object_unref (pbuf);
}

static void
on_drag_end (GtkWidget *widget, GdkDragContext *ctx, gpointer user_data)
{
	symbol_deselect (LD_CATEGORY_SYMBOL_VIEW (widget));
}

static void
ld_category_symbol_view_init (LdCategorySymbolView *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_CATEGORY_SYMBOL_VIEW, LdCategorySymbolViewPrivate);

	g_signal_connect (self, "size-allocate",
		G_CALLBACK (on_size_allocate), NULL);
	g_signal_connect (self, "size-request",
		G_CALLBACK (on_size_request), NULL);
	g_signal_connect (self, "expose-event",
		G_CALLBACK (on_expose_event), NULL);

	g_signal_connect (self, "motion-notify-event",
		G_CALLBACK (on_motion_notify), NULL);
	g_signal_connect (self, "leave-notify-event",
		G_CALLBACK (on_leave_notify), NULL);

	g_signal_connect (self, "drag-begin",
		G_CALLBACK (on_drag_begin), NULL);
	g_signal_connect (self, "drag-data-get",
		G_CALLBACK (on_drag_data_get), NULL);
	g_signal_connect (self, "drag-end",
		G_CALLBACK (on_drag_end), NULL);

	gtk_widget_add_events (GTK_WIDGET (self),
		GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK
		| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
		| GDK_LEAVE_NOTIFY_MASK);
}

static void
ld_category_symbol_view_finalize (GObject *gobject)
{
	LdCategorySymbolView *self;

	self = LD_CATEGORY_SYMBOL_VIEW (gobject);

	layout_destroy (self);
	if (self->priv->category)
		g_object_unref (self->priv->category);
	g_free (self->priv->path);

	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_category_symbol_view_parent_class)->finalize (gobject);
}

static void
ld_category_symbol_view_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	switch (property_id)
	{
	case PROP_CATEGORY:
		g_value_set_object (value,
			ld_category_view_get_category (LD_CATEGORY_VIEW (object)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_category_symbol_view_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	switch (property_id)
	{
	case PROP_CATEGORY:
		ld_category_view_set_category (LD_CATEGORY_VIEW (object),
			LD_CATEGORY (g_value_get_object (value)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}


/* ===== Interface ========================================================= */

/**
 * ld_category_symbol_view_new:
 * @category: (allow-none): a category to be assigned to the widget.
 *
 * Create an instance.
 */
GtkWidget *
ld_category_symbol_view_new (LdCategory *category)
{
	LdCategorySymbolView *self;

	self = g_object_new (LD_TYPE_CATEGORY_SYMBOL_VIEW, NULL);
	ld_category_view_set_category (LD_CATEGORY_VIEW (self), category);
	return GTK_WIDGET (self);
}

static void
ld_category_symbol_view_set_category (LdCategoryView *iface,
	LdCategory *category)
{
	LdCategorySymbolView *self;

	g_return_if_fail (LD_IS_CATEGORY_SYMBOL_VIEW (iface));
	g_return_if_fail (LD_IS_CATEGORY (category));

	self = LD_CATEGORY_SYMBOL_VIEW (iface);
	if (self->priv->category)
	{
		g_object_unref (self->priv->category);

		g_free (self->priv->path);
		self->priv->path = NULL;
	}

	/* XXX: We should rebuild the path if the name changes but it shouldn't
	 *      happen and we would have to track the parents, too. */
	self->priv->path = ld_category_get_path (category);

	self->priv->category = category;
	g_object_ref (category);

	g_object_notify (G_OBJECT (self), "category");
	gtk_widget_queue_resize (GTK_WIDGET (self));
}

static LdCategory *
ld_category_symbol_view_get_category (LdCategoryView *iface)
{
	g_return_val_if_fail (LD_IS_CATEGORY_SYMBOL_VIEW (iface), NULL);
	return LD_CATEGORY_SYMBOL_VIEW (iface)->priv->category;
}