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(GtkWidget *graph, cairo_t *cr) {
83 struct drawing_context cxt;
84 GraphWidget *gw = GRAPH_WIDGET(graph);
86 fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
87 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);
89 double x0=graph->allocation.x;
90 double y0=graph->allocation.y;
91 double height=graph->allocation.height;
92 double width=graph->allocation.width;
96 cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx));
97 cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny));
98 cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx);
99 cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny);
101 fprintf(stderr, "minx=%g, maxx=%g, miny=%g, maxy=%g, xscaling=%g, yscaling=%g, xoffset=%g, yoffset=%g\n", gw->graph->minx, gw->graph->maxx, gw->graph->miny, gw->graph->maxy, cxt.xscaling, cxt.yscaling, cxt.xoffset, cxt.yoffset);
102 fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
103 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));
105 double offset_height = ((width>=height)?height*INSET_PERCENT:0);;
106 double offset_width = ((height>=width)?width*INSET_PERCENT:0);
107 double inset_height = height - offset_height;
108 double inset_width = width - offset_width;
110 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 );
111 cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
112 cairo_set_line_width(cr, 0.005);
113 cairo_select_font_face (cr, "Georgia",
114 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
115 cairo_set_font_size (cr, 0.05);
116 cairo_set_source_rgb(cr, 1, 1, 1);
117 cairo_fill_preserve(cr);
118 cairo_set_source_rgb(cr, 0, 0, 0);
120 g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
121 if(gw->graph->lines != NULL) {
122 g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
128 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
129 cairo_t *cr = gdk_cairo_create(graph->window);
130 cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
137 static void graph_widget_class_init(GraphWidgetClass *klass) {
138 GtkWidgetClass *widget_class;
139 widget_class = GTK_WIDGET_CLASS(klass);
140 widget_class->expose_event = graph_widget_expose;
143 static void graph_widget_init(GraphWidget *graph) {
145 graph->graph = graph_new();
148 GtkWidget *graph_widget_new(void) {
149 return g_object_new(GRAPH_WIDGET_TYPE, NULL);
152 Graph* graph_widget_get_graph(GraphWidget* gw) {