gpkplotting.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * GTK plotting routines source file
  3. *
  4. * Copyright (c) 1999 Mark Taylor
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /* $Id$ */
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. #include "gpkplotting.h"
  26. #ifdef STDC_HEADERS
  27. # include <string.h>
  28. #else
  29. # ifndef HAVE_STRCHR
  30. # define strchr index
  31. # define strrchr rindex
  32. # endif
  33. char *strchr(), *strrchr();
  34. # ifndef HAVE_MEMCPY
  35. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  36. # define memmove(d, s, n) bcopy ((s), (d), (n))
  37. # endif
  38. #endif
  39. #ifdef WITH_DMALLOC
  40. #include <dmalloc.h>
  41. #endif
  42. static gint num_plotwindows = 0;
  43. static gint max_plotwindows = 10;
  44. static GdkPixmap *pixmaps[10];
  45. static GtkWidget *pixmapboxes[10];
  46. /* compute a gdkcolor */
  47. void
  48. setcolor(GtkWidget * widget, GdkColor * color, gint red, gint green, gint blue)
  49. {
  50. /* colors in GdkColor are taken from 0 to 65535, not 0 to 255. */
  51. color->red = red * (65535 / 255);
  52. color->green = green * (65535 / 255);
  53. color->blue = blue * (65535 / 255);
  54. color->pixel = (gulong) (color->red * 65536 + color->green * 256 + color->blue);
  55. /* find closest in colormap, if needed */
  56. gdk_color_alloc(gtk_widget_get_colormap(widget), color);
  57. }
  58. void
  59. gpk_redraw(GdkPixmap * pixmap, GtkWidget * pixmapbox)
  60. {
  61. /* redraw the entire pixmap */
  62. gdk_draw_pixmap(pixmapbox->window,
  63. pixmapbox->style->fg_gc[GTK_WIDGET_STATE(pixmapbox)],
  64. pixmap, 0, 0, 0, 0, pixmapbox->allocation.width, pixmapbox->allocation.height);
  65. }
  66. static GdkPixmap **
  67. findpixmap(GtkWidget * widget)
  68. {
  69. int i;
  70. for (i = 0; i < num_plotwindows && widget != pixmapboxes[i]; i++);
  71. if (i >= num_plotwindows) {
  72. g_print("findpixmap(): bad argument widget \n");
  73. return NULL;
  74. }
  75. return &pixmaps[i];
  76. }
  77. void
  78. gpk_graph_draw(GtkWidget * widget, /* plot on this widged */
  79. int n, /* number of data points */
  80. gdouble * xcord, gdouble * ycord, /* data */
  81. gdouble xmn, gdouble ymn, /* coordinates of corners */
  82. gdouble xmx, gdouble ymx, int clear, /* clear old plot first */
  83. char *title, /* add a title (only if clear=1) */
  84. GdkColor * color)
  85. {
  86. GdkPixmap **ppixmap;
  87. GdkPoint *points;
  88. int i;
  89. gint16 width, height;
  90. GdkFont *fixed_font;
  91. GdkGC *gc;
  92. gc = gdk_gc_new(widget->window);
  93. gdk_gc_set_foreground(gc, color);
  94. if ((ppixmap = findpixmap(widget))) {
  95. width = widget->allocation.width;
  96. height = widget->allocation.height;
  97. if (clear) {
  98. /* white background */
  99. gdk_draw_rectangle(*ppixmap, widget->style->white_gc, TRUE, 0, 0, width, height);
  100. /* title */
  101. #ifdef _WIN32
  102. fixed_font = gdk_font_load("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
  103. #else
  104. fixed_font = gdk_font_load("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
  105. #endif
  106. gdk_draw_text(*ppixmap, fixed_font,
  107. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  108. 0, 10, title, strlen(title));
  109. }
  110. points = g_malloc(n * sizeof(GdkPoint));
  111. for (i = 0; i < n; i++) {
  112. points[i].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
  113. points[i].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
  114. }
  115. gdk_draw_lines(*ppixmap, gc, points, n);
  116. g_free(points);
  117. gpk_redraw(*ppixmap, widget);
  118. }
  119. gdk_gc_destroy(gc);
  120. }
  121. void
  122. gpk_rectangle_draw(GtkWidget * widget, /* plot on this widged */
  123. gdouble * xcord, gdouble * ycord, /* corners */
  124. gdouble xmn, gdouble ymn, /* coordinates of corners */
  125. gdouble xmx, gdouble ymx, GdkColor * color)
  126. {
  127. GdkPixmap **ppixmap;
  128. GdkPoint points[2];
  129. int i;
  130. gint16 width, height;
  131. GdkGC *gc;
  132. gc = gdk_gc_new(widget->window);
  133. gdk_gc_set_foreground(gc, color);
  134. if ((ppixmap = findpixmap(widget))) {
  135. width = widget->allocation.width;
  136. height = widget->allocation.height;
  137. for (i = 0; i < 2; i++) {
  138. points[i].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
  139. points[i].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
  140. }
  141. width = points[1].x - points[0].x + 1;
  142. height = points[1].y - points[0].y + 1;
  143. gdk_draw_rectangle(*ppixmap, gc, TRUE, points[0].x, points[0].y, width, height);
  144. gpk_redraw(*ppixmap, widget);
  145. }
  146. gdk_gc_destroy(gc);
  147. }
  148. void
  149. gpk_bargraph_draw(GtkWidget * widget, /* plot on this widged */
  150. int n, /* number of data points */
  151. gdouble * xcord, gdouble * ycord, /* data */
  152. gdouble xmn, gdouble ymn, /* coordinates of corners */
  153. gdouble xmx, gdouble ymx, int clear, /* clear old plot first */
  154. char *title, /* add a title (only if clear=1) */
  155. int barwidth, /* bar width. 0=compute based on window size */
  156. GdkColor * color)
  157. {
  158. GdkPixmap **ppixmap;
  159. GdkPoint points[2];
  160. int i;
  161. gint16 width, height, x, y, barheight;
  162. GdkFont *fixed_font;
  163. GdkGC *gc;
  164. int titleSplit;
  165. gc = gdk_gc_new(widget->window);
  166. gdk_gc_set_foreground(gc, color);
  167. if ((ppixmap = findpixmap(widget))) {
  168. width = widget->allocation.width;
  169. height = widget->allocation.height;
  170. if (clear) {
  171. /* white background */
  172. gdk_draw_rectangle(*ppixmap, widget->style->white_gc, TRUE, 0, 0, width, height);
  173. /* title */
  174. #ifdef _WIN32
  175. fixed_font = gdk_font_load("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
  176. #else
  177. fixed_font = gdk_font_load("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
  178. #endif
  179. titleSplit = strcspn(title, "\n");
  180. if (titleSplit && (titleSplit != strlen(title))) {
  181. gdk_draw_text(*ppixmap, fixed_font,
  182. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  183. 0, 10, title, titleSplit);
  184. gdk_draw_text(*ppixmap, fixed_font,
  185. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  186. 0, 22, title + titleSplit + 1, (strlen(title) - titleSplit) - 1);
  187. }
  188. else {
  189. gdk_draw_text(*ppixmap, fixed_font,
  190. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  191. 0, 10, title, strlen(title));
  192. }
  193. }
  194. for (i = 0; i < n; i++) {
  195. points[1].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
  196. points[1].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
  197. points[0].x = points[1].x;
  198. points[0].y = height - 1;
  199. x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
  200. y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
  201. if (!barwidth)
  202. barwidth = (width / (n + 1)) - 1;
  203. barwidth = barwidth > 5 ? 5 : barwidth;
  204. barwidth = barwidth < 1 ? 1 : barwidth;
  205. barheight = height - 1 - y;
  206. /* gdk_draw_lines(*ppixmap,gc,points,2); */
  207. gdk_draw_rectangle(*ppixmap, gc, TRUE, x, y, barwidth, barheight);
  208. }
  209. gpk_redraw(*ppixmap, widget);
  210. }
  211. gdk_gc_destroy(gc);
  212. }
  213. /* Create a new backing pixmap of the appropriate size */
  214. static gint
  215. configure_event(GtkWidget * widget, GdkEventConfigure * event, gpointer data)
  216. {
  217. GdkPixmap **ppixmap;
  218. if ((ppixmap = findpixmap(widget))) {
  219. if (*ppixmap)
  220. gdk_pixmap_unref(*ppixmap);
  221. *ppixmap = gdk_pixmap_new(widget->window,
  222. widget->allocation.width, widget->allocation.height, -1);
  223. gdk_draw_rectangle(*ppixmap,
  224. widget->style->white_gc,
  225. TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
  226. }
  227. return TRUE;
  228. }
  229. /* Redraw the screen from the backing pixmap */
  230. static gint
  231. expose_event(GtkWidget * widget, GdkEventExpose * event, gpointer data)
  232. {
  233. GdkPixmap **ppixmap;
  234. if ((ppixmap = findpixmap(widget))) {
  235. gdk_draw_pixmap(widget->window,
  236. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  237. *ppixmap,
  238. event->area.x, event->area.y,
  239. event->area.x, event->area.y, event->area.width, event->area.height);
  240. }
  241. return FALSE;
  242. }
  243. GtkWidget *
  244. gpk_plot_new(int width, int height)
  245. {
  246. GtkWidget *pixmapbox;
  247. pixmapbox = gtk_drawing_area_new();
  248. gtk_drawing_area_size(GTK_DRAWING_AREA(pixmapbox), width, height);
  249. gtk_signal_connect(GTK_OBJECT(pixmapbox), "expose_event", (GtkSignalFunc) expose_event, NULL);
  250. gtk_signal_connect(GTK_OBJECT(pixmapbox), "configure_event",
  251. (GtkSignalFunc) configure_event, NULL);
  252. gtk_widget_set_events(pixmapbox, GDK_EXPOSURE_MASK);
  253. if (num_plotwindows < max_plotwindows) {
  254. pixmapboxes[num_plotwindows] = pixmapbox;
  255. pixmaps[num_plotwindows] = NULL;
  256. num_plotwindows++;
  257. }
  258. else {
  259. g_print("gtk_plotarea_new(): exceeded maximum of 10 plotarea windows\n");
  260. }
  261. return pixmapbox;
  262. }