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;
75 cairo_move_to(cxt->cr, p0_x, p0_y);
76 cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
79 static void draw(GtkWidget *graph, cairo_t *cr) {
80 struct drawing_context cxt;
81 GraphWidget *gw = GRAPH_WIDGET(graph);
83 fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
84 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);
86 double x0=graph->allocation.x;
87 double y0=graph->allocation.y;
88 double height=graph->allocation.height;
89 double width=graph->allocation.width;
93 cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx));
94 cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny));
95 cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx);
96 cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny);
98 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);
99 fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
100 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));
102 double offset_height = ((width>=height)?height*INSET_PERCENT:0);;
103 double offset_width = ((height>=width)?width*INSET_PERCENT:0);
104 double inset_height = height - offset_height;
105 double inset_width = width - offset_width;
107 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 );
108 cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
109 cairo_set_line_width(cr, 0.005);
110 cairo_select_font_face (cr, "Georgia",
111 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
112 cairo_set_font_size (cr, 0.05);
113 g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
114 cairo_set_source_rgb(cr, 1, 1, 1);
115 cairo_fill_preserve(cr);
116 cairo_set_source_rgb(cr, 0, 0, 0);
121 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
122 cairo_t *cr = gdk_cairo_create(graph->window);
123 cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
130 static void graph_widget_class_init(GraphWidgetClass *klass) {
131 GtkWidgetClass *widget_class;
132 widget_class = GTK_WIDGET_CLASS(klass);
133 widget_class->expose_event = graph_widget_expose;
136 static void graph_widget_init(GraphWidget *graph) {
138 graph->graph = graph_new();
141 GtkWidget *graph_widget_new(void) {
142 return g_object_new(GRAPH_WIDGET_TYPE, NULL);
145 Graph* graph_widget_get_graph(GraphWidget* gw) {