aboutsummaryrefslogtreecommitdiff
path: root/src/ld-canvas.c
blob: 345703888a4a7e289acc1e3f3781ebd53ab80bd5 (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
/*
 * ld-canvas.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <gtk/gtk.h>

#include "config.h"

#include "ld-marshal.h"
#include "ld-document.h"
#include "ld-symbol.h"
#include "ld-library.h"
#include "ld-canvas.h"


/**
 * SECTION:ld-canvas
 * @short_description: A canvas.
 * @see_also: #LdDocument
 *
 * #LdCanvas is used for displaying #LdDocument objects.
 */

/*
 * LdCanvasPrivate:
 * @document: A document object assigned to this canvas as a model.
 */
struct _LdCanvasPrivate
{
	LdDocument *document;
	LdLibrary *library;
};

G_DEFINE_TYPE (LdCanvas, ld_canvas, GTK_TYPE_DRAWING_AREA);

enum
{
	PROP_0,
	PROP_DOCUMENT,
	PROP_LIBRARY
};

static void
ld_canvas_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec);

static void
ld_canvas_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec);

static void
ld_canvas_finalize (GObject *gobject);


static gboolean
on_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data);

static void
draw_grid (GtkWidget *widget, cairo_t *cr);

static void
canvas_paint (GtkWidget *widget, cairo_t *cr);


static void
ld_canvas_class_init (LdCanvasClass *klass)
{
	GObjectClass *object_class;
	GtkWidgetClass *widget_class;
	GParamSpec *pspec;

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_canvas_get_property;
	object_class->set_property = ld_canvas_set_property;
	object_class->finalize = ld_canvas_finalize;

/**
 * LdCanvas:document:
 *
 * The underlying #LdDocument object of this canvas.
 */
	pspec = g_param_spec_object ("document", "Document",
		"The underlying document object of this canvas.",
		LD_TYPE_DOCUMENT, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_DOCUMENT, pspec);

/**
 * LdCanvas:library:
 *
 * The #LdLibrary that this canvas retrieves symbols from.
 */
	pspec = g_param_spec_object ("library", "Library",
		"The library that this canvas retrieves symbols from.",
		LD_TYPE_LIBRARY, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_DOCUMENT, pspec);

	widget_class = GTK_WIDGET_CLASS (klass);

/* TODO: Scrolling support; make the comment bellow a gtk-doc comment then. */
/*
 * LdCanvas::set-scroll-adjustments:
 * @canvas: The canvas object.
 *
 * Contents of the library have changed.
 */
/*
	widget_class->set_scroll_adjustments_signal = g_signal_new
		("set-scroll-adjustments", G_TYPE_FROM_CLASS (widget_class),
		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		G_STRUCT_OFFSET (LdCanvasClass, set_scroll_adjustments),
		NULL, NULL,
		g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
		G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
*/
	g_type_class_add_private (klass, sizeof (LdCanvasPrivate));
}

static void
ld_canvas_init (LdCanvas *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_CANVAS, LdCanvasPrivate);

	g_signal_connect (self, "expose-event", G_CALLBACK (on_expose_event), NULL);
}

static void
ld_canvas_finalize (GObject *gobject)
{
	LdCanvas *self;

	self = LD_CANVAS (gobject);

	if (self->priv->document)
		g_object_unref (self->priv->document);
	if (self->priv->library)
		g_object_unref (self->priv->library);

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

static void
ld_canvas_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	LdCanvas *self;

	self = LD_CANVAS (object);
	switch (property_id)
	{
	case PROP_DOCUMENT:
		g_value_set_object (value, ld_canvas_get_document (self));
		break;
	case PROP_LIBRARY:
		g_value_set_object (value, ld_canvas_get_library (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_canvas_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	LdCanvas *self;

	self = LD_CANVAS (object);
	switch (property_id)
	{
	case PROP_DOCUMENT:
		ld_canvas_set_document (self, LD_DOCUMENT (g_value_get_object (value)));
		break;
	case PROP_LIBRARY:
		ld_canvas_set_library (self, LD_LIBRARY (g_value_get_object (value)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

/**
 * ld_canvas_new:
 *
 * Create an instance.
 */
LdCanvas *
ld_canvas_new (void)
{
	return g_object_new (LD_TYPE_CANVAS, NULL);
}

/**
 * ld_canvas_set_document:
 * @self: An #LdCanvas object.
 * @document: The #LdDocument to be assigned to the canvas.
 *
 * Assign an #LdDocument object to the canvas.
 */
void
ld_canvas_set_document (LdCanvas *self, LdDocument *document)
{
	if (self->priv->document)
		g_object_unref (self->priv->document);

	self->priv->document = document;
	g_object_ref (document);
}

/**
 * ld_canvas_get_document:
 * @self: An #LdCanvas object.
 *
 * Get the #LdDocument object assigned to this canvas.
 * The reference count on the document is not incremented.
 */
LdDocument *
ld_canvas_get_document (LdCanvas *self)
{
	return self->priv->document;
}

/**
 * ld_canvas_set_library:
 * @self: An #LdCanvas object.
 * @library: The #LdLibrary to be assigned to the canvas.
 *
 * Assign an #LdLibrary object to the canvas.
 */
void
ld_canvas_set_library (LdCanvas *self, LdLibrary *library)
{
	if (self->priv->library)
		g_object_unref (self->priv->library);

	self->priv->library = library;
	g_object_ref (library);
}

/**
 * ld_canvas_get_library:
 * @self: An #LdCanvas object.
 *
 * Get the #LdLibrary object assigned to this canvas.
 * The reference count on the library is not incremented.
 */
LdLibrary *
ld_canvas_get_library (LdCanvas *self)
{
	return self->priv->library;
}

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

	cr = gdk_cairo_create (widget->window);

	cairo_rectangle (cr, event->area.x, event->area.y,
		event->area.width, event->area.height);
	cairo_clip (cr);

	canvas_paint (widget, cr);

	cairo_destroy (cr);

	return FALSE;
}

static void
draw_grid (GtkWidget *widget, cairo_t *cr)
{
	int x, y;

	/* Drawing points:
	 * http://lists.freedesktop.org/archives/cairo/2009-June/017459.html
	 */
	cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
	cairo_set_line_width (cr, 1);
	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

	/* FIXME: Invariable grid size; it's slow because it draws whole area. */
	for (x = 0; x < widget->allocation.width; x += 20)
	{
		for (y = 0; y < widget->allocation.height; y += 20)
		{
			/* Drawing sharp lines (also applies to these dots):
			 * http://www.cairographics.org/FAQ/#sharp_lines
			 */
			cairo_move_to (cr, x + 0.5, y + 0.5);
			cairo_close_path (cr);
			cairo_stroke (cr);
		}
	}
}

static void
canvas_paint (GtkWidget *widget, cairo_t *cr)
{
	/* Paint a white background. */
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_paint (cr);

	draw_grid (widget, cr);
}