* Offsets work great now. Graph now gets positioned correctly.
[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 #define INSET_PERCENT 0.1
38
39 struct drawing_context {
40   GtkWidget *widget;
41   cairo_t *cr;
42   double radius;
43   double xoffset;
44   double yoffset;
45   double xscaling;
46   double yscaling;
47 };
48
49 static void draw_point(gpointer data, gpointer user_data) {
50   struct drawing_context *cxt = (struct drawing_context *) user_data;
51   double *d = (double*)data;
52   fprintf(stderr, "draw_point(x=%g(%g) y=%g(%g)) xscaling=%g yscaling=%g xoffset=%g yoffset=%g\n", d[0], (d[0] + cxt->xoffset)*cxt->xscaling, d[1], (d[1] + cxt->yoffset)*cxt->yscaling, cxt->xscaling, cxt->yscaling, cxt->xoffset, cxt->yoffset);
53   cairo_arc(cxt->cr, (d[0] + cxt->xoffset)*cxt->xscaling, (d[1] + cxt->yoffset)*cxt->yscaling, cxt->radius, 0, 2*M_PI);
54   cairo_set_source_rgb(cxt->cr, 1, 1, 1);
55   cairo_fill_preserve(cxt->cr);
56   cairo_set_source_rgb(cxt->cr, 0, 0, 0);
57   cairo_stroke(cxt->cr);
58 }
59
60 static void draw(GtkWidget *graph, cairo_t *cr) {
61   struct drawing_context cxt;
62   GraphWidget *gw = GRAPH_WIDGET(graph);
63   #ifdef DEBUG
64   fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
65   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);
66   #endif
67   double x0=graph->allocation.x;
68   double y0=graph->allocation.y;
69   double height=graph->allocation.height;
70   double width=graph->allocation.width;
71   cxt.widget = graph;
72   cxt.cr = cr;
73   cxt.radius = 0.01;
74   cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx));
75   cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny));
76   cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx);
77   cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny);
78   #ifdef DEBUG
79   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
80   fprintf(stderr, "translate=(%g, %g)\n", x0 + ((width>height)?(width-height)/2.0:0), y0+((width > height)?height:width) + ((height>width)?(height-width)/2.0:0));
81 #endif
82   double offset_height =  ((width>=height)?height*INSET_PERCENT:0);;
83   double offset_width = ((height>=width)?width*INSET_PERCENT:0); 
84   double inset_height = height - offset_height;
85   double inset_width = width - offset_width;
86   cairo_save(cr);
87   cairo_translate(cr, x0 + ((inset_width>inset_height)?(inset_width-inset_height)/2.0:0) + offset_width/2, y0+((inset_width > inset_height)?inset_height:inset_width) + ((inset_width>=inset_height)?0:(inset_height-inset_width)/2.0) + offset_height/2 );
88   cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
89   cairo_set_line_width(cr, 0.005);
90   g_ptr_array_foreach(GRAPH_WIDGET(graph)->graph->points, &draw_point, (gpointer)&cxt);
91   cairo_restore(cr);
92 }
93
94 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
95   cairo_t *cr = gdk_cairo_create(graph->window);
96   cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
97   cairo_clip(cr);
98   draw(graph, cr);
99   cairo_destroy(cr);
100   return FALSE;
101 }
102
103 static void graph_widget_class_init(GraphWidgetClass *klass) {
104   GtkWidgetClass *widget_class;
105   widget_class = GTK_WIDGET_CLASS(klass);
106   widget_class->expose_event = graph_widget_expose;
107 }
108
109 static void graph_widget_init(GraphWidget *graph) {
110   /*Get a graph.*/
111   graph->graph = graph_new();
112 }
113
114 GtkWidget *graph_widget_new(void) {
115   return g_object_new(GRAPH_WIDGET_TYPE, NULL);
116 }
117
118 Graph* graph_widget_get_graph(GraphWidget* gw) {
119   return gw->graph;
120 }