/[paths]/src/egg_node.c
ViewVC logotype

Annotation of /src/egg_node.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 67 - (hide annotations)
Sun Aug 26 17:09:20 2012 UTC (8 years, 1 month ago) by ben
File MIME type: text/plain
File size: 1827 byte(s)
cosmetic
1 ben 38 #include <stdlib.h>
2     #include <glib.h>
3     #include <cairo/cairo.h>
4     #include <gtk/gtk.h>
5    
6     #include "egg_node.h"
7    
8     G_DEFINE_TYPE (EggNode, egg_node, GTK_TYPE_DRAWING_AREA);
9    
10     /* object constructor */
11     static void egg_node_init(EggNode *node)
12     {
13 ben 66 /* color init */
14 ben 63 node->r = node->g = node->b = 1.0;
15 ben 66 /* speed init for force-based positioning */
16 ben 64 node->x = node->y = node->dx = node->dy = 0.0;
17 ben 63
18 ben 54 gtk_widget_set_has_window (GTK_WIDGET(node), FALSE);
19 ben 66 gtk_widget_set_size_request (GTK_WIDGET(node), NODE_WIDTH, NODE_HEIGHT);
20 ben 38 }
21    
22     /* draw handler */
23     static gboolean egg_node_draw (GtkWidget *node, cairo_t *cr)
24     {
25 ben 39 GtkAllocation a;
26 ben 38 double x, y;
27     double radius;
28    
29 ben 39 gtk_widget_get_allocation (node, &a);
30 ben 38
31 ben 53 x = a.width / 2;
32     y = a.height / 2;
33 ben 38
34 ben 67 radius = MIN (a.width / 2, a.height / 2) - 2;
35 ben 38
36     cairo_arc (cr, x, y, radius, 0, 2 * G_PI);
37    
38 ben 63 cairo_set_source_rgb (cr, EGG_NODE(node)->r, EGG_NODE(node)->g, EGG_NODE(node)->b);
39 ben 38 cairo_fill_preserve (cr);
40     cairo_set_source_rgb (cr, 0, 0, 0);
41     cairo_stroke (cr);
42    
43     return FALSE;
44     }
45    
46    
47     /* class constructor */
48     static void egg_node_class_init (EggNodeClass *class)
49     {
50     GtkWidgetClass *widget_class;
51    
52     widget_class = GTK_WIDGET_CLASS(class);
53    
54     widget_class->draw = egg_node_draw;
55     }
56    
57     /* public API */
58    
59     GtkWidget *egg_node_new(void)
60     {
61     return g_object_new(EGG_TYPE_NODE, NULL);
62     }
63    
64 ben 63 void egg_node_set_color(EggNode *node, gdouble r, gdouble g, gdouble b)
65     {
66     gtk_widget_hide(GTK_WIDGET(node));
67     node->r = r;
68     node->g = g;
69     node->b = b;
70     gtk_widget_show(GTK_WIDGET(node));
71     }
72 ben 64
73     void egg_node_set_position(EggNode *node, gdouble x, gdouble y)
74     {
75     gtk_widget_hide(GTK_WIDGET(node));
76     node->x = x;
77     node->y = y;
78     gtk_widget_show(GTK_WIDGET(node));
79     }
80    
81     void egg_node_set_speed(EggNode *node, gdouble dx, gdouble dy)
82     {
83     gtk_widget_hide(GTK_WIDGET(node));
84     node->dx = dx;
85     node->dy = dy;
86     gtk_widget_show(GTK_WIDGET(node));
87     }

  ViewVC Help
Powered by ViewVC 1.1.26