4 ** Made by (Johnny Q. Hacker)
5 ** Login <solarion@johnathan>
10 Copyright (C) 2008 Joseph Pingenot
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.
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.
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/>.
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
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
32 #include <graphwidget.h>
35 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
37 #define INSET_PERCENT 0.1
39 struct drawing_context {
49 static void draw_point(gpointer data, gpointer user_data) {
50 struct drawing_context *cxt = user_data;
51 struct graph_point *pt = data;
52 double x = (pt->x + cxt->xoffset)*cxt->xscaling;
53 double y = (pt->y + cxt->yoffset)*cxt->yscaling;
54 cairo_move_to(cxt->cr, x, y);
55 cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
56 if(pt->label != NULL) {
58 cairo_scale(cxt->cr, 1, -1);
59 cairo_show_text(cxt->cr, pt->label->str);
60 cairo_restore(cxt->cr);
64 static void draw_lines(gpointer data, gpointer user_data) {
65 struct drawing_context *cxt = user_data;
66 struct graph_line *l = data;
67 double p0_x = (l->p0_x + cxt->xoffset)*cxt->xscaling;
68 double p0_y = (l->p0_y + cxt->yoffset)*cxt->yscaling;
69 double p1_x = (l->p1_x + cxt->xoffset)*cxt->xscaling;
70 double p1_y = (l->p1_y + cxt->yoffset)*cxt->yscaling;
71 double p2_x = (l->p2_x + cxt->xoffset)*cxt->xscaling;
72 double p2_y = (l->p2_y + cxt->yoffset)*cxt->yscaling;
73 double p3_x = (l->p3_x + cxt->xoffset)*cxt->xscaling;
74 double p3_y = (l->p3_y + cxt->yoffset)*cxt->yscaling;
76 fprintf(stderr, "draw_lines: p0_x=%g, p0_y=%g, p1_x=%g, p1_y=%g, p2_x=%g, p2_y=%g, p3_x=%g, p3_y=%g\n", p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
78 cairo_move_to(cxt->cr, p0_x, p0_y);
79 cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
82 static void draw_axis(gpointer data, gpointer user_data) {
83 struct graph_axis *xs = data;
84 struct drawing_context *cxt = user_data;
87 static void draw(GtkWidget *graph, cairo_t *cr) {
88 struct drawing_context cxt;
89 GraphWidget *gw = GRAPH_WIDGET(graph);
91 fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
92 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);
94 double x0=graph->allocation.x;
95 double y0=graph->allocation.y;
96 double height=graph->allocation.height;
97 double width=graph->allocation.width;
98 double minx = gw->graph->minx;
99 double maxx = gw->graph->maxx;
100 double miny = gw->graph->miny;
101 double maxy = gw->graph->maxy;
102 if(gw->graph->xaxis != NULL) {
103 minx = gw->graph->xaxis->major_start;
104 maxx = gw->graph->xaxis->major_start + gw->graph->xaxis->major_step*gw->graph->xaxis->major;
106 if(gw->graph->yaxis != NULL) {
107 miny = gw->graph->yaxis->major_start;
108 maxy = gw->graph->yaxis->major_start + gw->graph->yaxis->major_step*gw->graph->yaxis->major;
113 cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(maxx - minx));
114 cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(maxy - miny));
115 cxt.xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
116 cxt.yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
118 fprintf(stderr, "minx=%g, maxx=%g, miny=%g, maxy=%g, xscaling=%g, yscaling=%g, xoffset=%g, yoffset=%g\n", minx, maxx, miny, maxy, cxt.xscaling, cxt.yscaling, cxt.xoffset, cxt.yoffset);
119 fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
120 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));
122 double offset_height = ((width>=height)?height*INSET_PERCENT:0);;
123 double offset_width = ((height>=width)?width*INSET_PERCENT:0);
124 double inset_height = height - offset_height;
125 double inset_width = width - offset_width;
127 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 );
128 cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
129 cairo_set_line_width(cr, 0.005);
130 cairo_select_font_face (cr, "Georgia",
131 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
132 cairo_set_font_size (cr, 0.05);
133 if(gw->graph->lines != NULL) {
134 cairo_set_source_rgba(cr, 0, 0, 0, 0.25);
135 g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
138 cairo_set_source_rgb(cr, 1, 1, 1);
139 cairo_fill_preserve(cr);
140 cairo_set_source_rgb(cr, 0, 0, 0);
141 g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
146 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
147 cairo_t *cr = gdk_cairo_create(graph->window);
148 cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
155 static void graph_widget_class_init(GraphWidgetClass *klass) {
156 GtkWidgetClass *widget_class;
157 widget_class = GTK_WIDGET_CLASS(klass);
158 widget_class->expose_event = graph_widget_expose;
161 static void graph_widget_init(GraphWidget *graph) {
163 graph->graph = graph_new();
166 GtkWidget *graph_widget_new(void) {
167 return g_object_new(GRAPH_WIDGET_TYPE, NULL);
170 Graph* graph_widget_get_graph(GraphWidget* gw) {