* Updated files. Now it compiles (though it crashes).
[scdataviz] / graphwidget.c
1 /*
2 ** Graph.c
3 ** 
4 ** Made by (Johnny Q. Hacker)
5 ** Login   <solarion@johnathan>
6 ** 
7
8
9
10     Copyright (C) 2008 Joseph Pingenot
11
12     This program is free software: you can redistribute it and/or modify
13     it under the terms of the GNU Affero General Public License as published by
14     the Free Software Foundation, either version 3 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU Affero General Public License for more details.
21
22     You should have received a copy of the GNU Affero General Public License
23     along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25 **Many thanks to Davyd Madeley for the excellent Cairo Widget Tutorial
26 **  at http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28
27
28 ** Started on  Thu Jul 17 10:51:32 2008 Johnny Q. Hacker
29 ** Last update Sun May 12 01:17:25 2002 Speed Blue
30 */
31
32 #include <graphwidget.h>
33 #include <math.h>
34
35 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
36
37 struct drawing_context {
38   GtkWidget *widget;
39   cairo_t *cr;
40   double radius;
41 };
42
43 static void draw_point(gpointer data, gpointer user_data) {
44   struct drawing_context *cxt = (struct drawing_context *) user_data;
45   double *d = (double*)data;
46   cairo_arc(cxt->cr, d[0], d[1], cxt->radius, 0, 2*M_PI);
47 }
48
49 static void draw(GtkWidget *graph, cairo_t *cr) {
50   struct drawing_context cxt;
51   #ifdef DEBUG
52     fprintf(stderr, "draw(%d, %d)\n", graph, cr);
53     fprintf(stderr, "allocation.x=%d, allocation.y=%d, allocation.height=%d, allocation.width=%d\n", graph->allocation.x, graph->allocation.y, graph->allocation.height, graph->allocation.width);
54   #endif
55   double x0=graph->allocation.x;
56   double y0=graph->allocation.y;
57   double height=graph->allocation.height;
58   double width=graph->allocation.width;
59   cxt.widget = graph;
60   cxt.cr = cr;
61   cxt.radius = ((height < width)? height : width)*0.01;
62   #ifdef DEBUG
63   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
64   fprintf(stderr, "move_to(%d, %g->%g, %g->%g)\n", cr, graph_data[0], graph->allocation.x + graph_data[0]*(graph->allocation.width), graph_data[1], graph->allocation.y + graph_data[1]*(graph->allocation.height));
65   #endif
66   cairo_save(cr);
67   cairo_translate(cr, x0, y0);
68   cairo_scale(cr, width, height);
69   g_ptr_array_foreach(GRAPH_WIDGET(graph)->graph->points, &draw_point, (gpointer)&cxt);
70   cairo_set_source_rgb(cr, 0, 0, 0);
71   cairo_stroke(cr);
72   cairo_restore(cr);
73 }
74
75 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
76   cairo_t *cr = gdk_cairo_create(graph->window);
77   cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
78   cairo_clip(cr);
79   draw(graph, cr);
80   cairo_destroy(cr);
81   return FALSE;
82 }
83
84 static void graph_widget_class_init(GraphWidgetClass *klass) {
85   GtkWidgetClass *widget_class;
86   widget_class = GTK_WIDGET_CLASS(klass);
87   widget_class->expose_event = graph_widget_expose;
88 }
89
90 static void graph_widget_init(GraphWidget *graph) {
91   /*Get a graph.*/
92   graph->graph = graph_new();
93 }
94
95 GtkWidget *graph_widget_new(void) {
96   return g_object_new(GRAPH_WIDGET_TYPE, NULL);
97 }
98
99 Graph* graph_widget_get_graph(GraphWidget* gw) {
100   return gw->graph;
101 }