5 #include <gdk/gdkkeysyms.h>
8 typedef struct _point {
15 typedef struct _contour {
16 struct _contour *next, *prev;
23 typedef struct _TrapView {
26 cairo_surface_t *pixmap;
27 int pixmap_width, pixmap_height;
38 gint mag_drag_x, mag_drag_y;
41 typedef struct _TrapViewClass {
42 GtkWidgetClass parent_class;
45 G_DEFINE_TYPE (TrapView, trap_view, GTK_TYPE_WIDGET)
47 static cairo_surface_t *
48 pixmap_create (TrapView *self, cairo_surface_t *target)
50 cairo_surface_t *surface =
51 cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR,
52 self->widget.allocation.width,
53 self->widget.allocation.height);
54 cairo_t *cr = cairo_create (surface);
56 gdouble sf_x, sf_y, sf;
62 cairo_set_source_rgb (cr, 1, 1, 1);
65 if (self->contours == NULL) {
70 extents = self->extents;
72 mid = (extents.p2.x + extents.p1.x) / 2.;
73 dim = (extents.p2.x - extents.p1.x) / 2. * 1.25;
74 sf_x = self->widget.allocation.width / dim / 2;
76 mid = (extents.p2.y + extents.p1.y) / 2.;
77 dim = (extents.p2.y - extents.p1.y) / 2. * 1.25;
78 sf_y = self->widget.allocation.height / dim / 2;
80 sf = MIN (sf_x, sf_y);
82 mid = (extents.p2.x + extents.p1.x) / 2.;
83 dim = sf_x / sf * (extents.p2.x - extents.p1.x) / 2. * 1.25;
85 mid = (extents.p2.y + extents.p1.y) / 2.;
86 dim = sf_y / sf * (extents.p2.y - extents.p1.y) / 2. * 1.25;
89 for (contour = self->contours; contour; contour = contour->next) {
90 if (contour->num_points == 0)
94 cairo_scale (cr, sf, sf);
95 cairo_translate (cr, -x0, -y0);
96 switch (contour->direction) {
98 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
101 cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
104 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
108 const point_t *p = &contour->points[0];
109 cairo_arc (cr, p->x, p->y, 4/sf, 0, 2 * M_PI);
111 cairo_identity_matrix (cr);
115 for (n = 0; n < contour->num_points; n++) {
116 const point_t *p = &contour->points[n];
117 cairo_arc (cr, p->x, p->y, 2/sf, 0, 2 * M_PI);
120 for (n = 0; n < contour->num_points; n++) {
121 const point_t *p = &contour->points[n];
122 cairo_line_to (cr, p->x, p->y);
124 } cairo_restore (cr);
126 switch (contour->direction) {
128 cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
131 cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
134 cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
137 cairo_set_line_width (cr, 1.);
146 trap_view_draw (TrapView *self, cairo_t *cr)
149 gdouble sf_x, sf_y, sf;
155 extents = self->extents;
157 mid = (extents.p2.x + extents.p1.x) / 2.;
158 dim = (extents.p2.x - extents.p1.x) / 2. * 1.25;
159 sf_x = self->widget.allocation.width / dim / 2;
161 mid = (extents.p2.y + extents.p1.y) / 2.;
162 dim = (extents.p2.y - extents.p1.y) / 2. * 1.25;
163 sf_y = self->widget.allocation.height / dim / 2;
165 sf = MIN (sf_x, sf_y);
167 mid = (extents.p2.x + extents.p1.x) / 2.;
168 dim = sf_x / sf * (extents.p2.x - extents.p1.x) / 2. * 1.25;
170 mid = (extents.p2.y + extents.p1.y) / 2.;
171 dim = sf_y / sf * (extents.p2.y - extents.p1.y) / 2. * 1.25;
174 if (self->pixmap_width != self->widget.allocation.width ||
175 self->pixmap_height != self->widget.allocation.height)
177 cairo_surface_destroy (self->pixmap);
178 self->pixmap = pixmap_create (self, cairo_get_target (cr));
179 self->pixmap_width = self->widget.allocation.width;
180 self->pixmap_height = self->widget.allocation.height;
183 cairo_set_source_surface (cr, self->pixmap, 0, 0);
186 if (self->contours == NULL)
189 /* draw a zoom view of the area around the mouse */
191 double zoom = self->mag_zoom;
192 int size = self->mag_size;
193 int mag_x = self->mag_x;
194 int mag_y = self->mag_y;
197 if (self->px + size < self->widget.allocation.width/2)
198 mag_x = self->px + size/4;
200 mag_x = self->px - size/4 - size;
201 mag_y = self->py - size/2;
204 if (mag_y + size > self->widget.allocation.height)
205 mag_y = self->widget.allocation.height - size;
210 cairo_rectangle (cr, mag_x, mag_y, size, size);
211 cairo_stroke_preserve (cr);
212 cairo_set_source_rgb (cr, 1, 1, 1);
213 cairo_fill_preserve (cr);
216 /* compute roi in extents */
217 cairo_translate (cr, mag_x + size/2, mag_y + size/2);
220 for (contour = self->contours; contour; contour = contour->next) {
221 if (contour->num_points == 0)
225 cairo_scale (cr, zoom, zoom);
226 cairo_translate (cr, -(self->px / sf + x0), -(self->py /sf + y0));
227 switch (contour->direction) {
229 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
232 cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
235 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
239 const point_t *p = &contour->points[0];
240 cairo_arc (cr, p->x, p->y, 4/zoom, 0, 2 * M_PI);
242 cairo_identity_matrix (cr);
246 for (n = 0; n < contour->num_points; n++) {
247 const point_t *p = &contour->points[n];
248 cairo_arc (cr, p->x, p->y, 2/zoom, 0, 2 * M_PI);
251 for (n = 0; n < contour->num_points; n++) {
252 const point_t *p = &contour->points[n];
253 cairo_line_to (cr, p->x, p->y);
255 } cairo_restore (cr);
257 switch (contour->direction) {
259 cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
262 cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
265 cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
270 } cairo_restore (cr);
277 -zoom*fmod (self->px/sf + x0, 1.),
278 -zoom*fmod (self->py/sf + y0, 1.));
280 for (i = -size/2/zoom; i <= size/2/zoom + 1; i+=2) {
281 cairo_move_to (cr, zoom*i, -size/2);
282 cairo_line_to (cr, zoom*i, size/2 + zoom);
283 cairo_move_to (cr, -size/2, zoom*i);
284 cairo_line_to (cr, size/2 + zoom, zoom*i);
287 cairo_set_source_rgba (cr, .7, .7, .7, .5);
288 cairo_set_line_width (cr, 1.);
291 for (i = -size/2/zoom - 1; i <= size/2/zoom + 1; i++) {
292 cairo_move_to (cr, zoom*i, -size/2);
293 cairo_line_to (cr, zoom*i, size/2 + zoom);
294 cairo_move_to (cr, -size/2, zoom*i);
295 cairo_line_to (cr, size/2 + zoom, zoom*i);
297 cairo_set_source_rgba (cr, .1, .1, .1, .5);
298 cairo_set_line_width (cr, 2.);
300 } cairo_restore (cr);
302 } cairo_restore (cr);
308 edge_length (const point_t *p1, const point_t *p2)
310 return hypot (p2->x - p1->x, p2->y - p1->y);
314 contour_compute_total_length (const contour_t *contour)
318 for (n = 1; n < contour->num_points; n++)
319 len += edge_length (&contour->points[n-1], &contour->points[n]);
324 trap_view_draw_labels (TrapView *self, cairo_t *cr)
329 for (contour = self->contours; contour; contour = contour->next) {
330 double total_length = contour_compute_total_length (contour) / 256.;
336 if (contour->num_points == 0)
339 string = g_string_new (NULL);
340 g_string_append_printf (string,
341 "Number of points:\t%d\n"
342 "Total length of contour: \t%.2f",
346 str = g_string_free (string, FALSE);
347 layout = gtk_widget_create_pango_layout (&self->widget, str);
350 pango_layout_get_pixel_size (layout, &width, &height);
352 switch (contour->direction) {
354 cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
357 cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
360 cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
364 cairo_move_to (cr, 10, y);
365 pango_cairo_show_layout (cr, layout);
366 g_object_unref (layout);
373 trap_view_expose (GtkWidget *w, GdkEventExpose *ev)
375 TrapView *self = (TrapView *) w;
378 cr = gdk_cairo_create (w->window);
379 gdk_cairo_region (cr, ev->region);
382 trap_view_draw (self, cr);
383 trap_view_draw_labels (self, cr);
390 trap_view_key_press (GtkWidget *w, GdkEventKey *ev)
392 switch (ev->keyval) {
403 trap_view_button_press (GtkWidget *w, GdkEventButton *ev)
405 TrapView *self = (TrapView *) w;
407 if (ev->x < self->mag_x ||
408 ev->y < self->mag_y ||
409 ev->x > self->mag_x + self->mag_size ||
410 ev->y > self->mag_y + self->mag_size)
415 self->in_mag_drag = TRUE;
416 self->mag_drag_x = ev->x;
417 self->mag_drag_y = ev->y;
424 trap_view_button_release (GtkWidget *w, GdkEventButton *ev)
426 TrapView *self = (TrapView *) w;
428 self->in_mag_drag = FALSE;
434 trap_view_update_mouse (TrapView *self, GdkEventMotion *ev)
439 gtk_widget_queue_draw (&self->widget);
443 trap_view_update_magnifier (TrapView *self, gint *xy)
448 gtk_widget_queue_draw (&self->widget);
452 trap_view_motion (GtkWidget *w, GdkEventMotion *ev)
454 TrapView *self = (TrapView *) w;
456 if (self->in_mag_drag) {
459 xy[0] = self->mag_x + ev->x - self->mag_drag_x;
460 xy[1] = self->mag_y + ev->y - self->mag_drag_y;
462 trap_view_update_magnifier (self, xy);
464 self->mag_drag_x = ev->x;
465 self->mag_drag_y = ev->y;
466 } else if (ev->x < self->mag_x ||
467 ev->y < self->mag_y ||
468 ev->x > self->mag_x + self->mag_size ||
469 ev->y > self->mag_y + self->mag_size)
471 trap_view_update_mouse (self, ev);
478 trap_view_realize (GtkWidget *widget)
480 GdkWindowAttr attributes;
482 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
484 attributes.window_type = GDK_WINDOW_CHILD;
485 attributes.x = widget->allocation.x;
486 attributes.y = widget->allocation.y;
487 attributes.width = widget->allocation.width;
488 attributes.height = widget->allocation.height;
489 attributes.wclass = GDK_INPUT_OUTPUT;
490 attributes.visual = gtk_widget_get_visual (widget);
491 attributes.colormap = gtk_widget_get_colormap (widget);
492 attributes.event_mask = gtk_widget_get_events (widget) |
493 GDK_BUTTON_PRESS_MASK |
494 GDK_BUTTON_RELEASE_MASK |
496 GDK_KEY_RELEASE_MASK |
497 GDK_POINTER_MOTION_MASK |
498 GDK_BUTTON_MOTION_MASK |
501 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
503 GDK_WA_X | GDK_WA_Y |
504 GDK_WA_VISUAL | GDK_WA_COLORMAP);
505 gdk_window_set_user_data (widget->window, widget);
507 widget->style = gtk_style_attach (widget->style, widget->window);
508 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
512 trap_view_size_allocate (GtkWidget *w, GdkRectangle *r)
514 TrapView *self = (TrapView *) w;
516 GTK_WIDGET_CLASS (trap_view_parent_class)->size_allocate (w, r);
518 self->mag_x = w->allocation.width - self->mag_size - 10;
519 self->mag_y = w->allocation.height - self->mag_size - 10;
523 trap_view_finalize (GObject *obj)
525 G_OBJECT_CLASS (trap_view_parent_class)->finalize (obj);
529 trap_view_class_init (TrapViewClass *klass)
531 GObjectClass *object_class = (GObjectClass *) klass;
532 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
534 object_class->finalize = trap_view_finalize;
536 widget_class->realize = trap_view_realize;
537 widget_class->size_allocate = trap_view_size_allocate;
538 widget_class->expose_event = trap_view_expose;
539 widget_class->key_press_event = trap_view_key_press;
540 widget_class->button_press_event = trap_view_button_press;
541 widget_class->button_release_event = trap_view_button_release;
542 widget_class->motion_notify_event = trap_view_motion;
546 trap_view_init (TrapView *self)
549 self->mag_size = 200;
551 self->extents.p1.x = G_MAXDOUBLE;
552 self->extents.p1.y = G_MAXDOUBLE;
553 self->extents.p2.x = -G_MAXDOUBLE;
554 self->extents.p2.y = -G_MAXDOUBLE;
556 GTK_WIDGET_SET_FLAGS (self, GTK_CAN_FOCUS);
560 _contour_add_point (TrapView *tv, contour_t *contour, point_t *p)
565 if (p->y < tv->extents.p1.y)
566 tv->extents.p1.y = p->y;
567 if (p->y > tv->extents.p2.y)
568 tv->extents.p2.y = p->y;
570 if (p->x < tv->extents.p1.x)
571 tv->extents.p1.x = p->x;
572 if (p->x > tv->extents.p2.x)
573 tv->extents.p2.x = p->x;
575 if (contour->num_points == contour->size) {
576 int newsize = 2 * contour->size;
579 newcontour = g_realloc (contour,
580 sizeof (contour_t) + newsize * sizeof (point_t));
581 if (newcontour == NULL)
584 contour = newcontour;
585 contour->size = newsize;
587 if (contour->next != NULL)
588 contour->next->prev = newcontour;
589 if (contour->prev != NULL)
590 contour->prev->next = newcontour;
592 tv->contours = newcontour;
595 contour->points[contour->num_points++] = *p;
601 contour_new (TrapView *tv, int direction)
605 t = g_malloc (sizeof (contour_t) + 128 * sizeof (point_t));
606 t->direction = direction;
608 t->next = tv->contours;
610 tv->contours->prev = t;
620 main (int argc, char **argv)
623 contour_t *contour = NULL;
629 gtk_init (&argc, &argv);
631 tv = g_object_new (trap_view_get_type (), NULL);
633 file = fopen (argv[1], "r");
635 while (getline (&line, &len, file) != -1) {
639 if (sscanf (line, "contour: direction=%d", &direction)) {
641 g_print ("read %d contour\n", contour->num_points);
643 contour = contour_new (tv, direction);
644 } else if (sscanf (line, " [%*d] = (%lf, %lf)", &p.x, &p.y) == 2) {
645 contour = _contour_add_point (tv, contour, &p);
650 g_print ("read %d contour\n", contour->num_points);
652 g_print ("extents=(%lg, %lg), (%lg, %lg)\n",
653 tv->extents.p1.x, tv->extents.p1.y,
654 tv->extents.p2.x, tv->extents.p2.y);
658 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
659 g_signal_connect (window, "delete-event",
660 G_CALLBACK (gtk_main_quit), NULL);
661 gtk_widget_set_size_request (window, 800, 800);
662 gtk_container_add (GTK_CONTAINER (window), &tv->widget);
663 gtk_widget_show_all (window);