home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / gpkplotting.c < prev    next >
C/C++ Source or Header  |  2000-06-14  |  8KB  |  283 lines

  1. #ifdef HAVEGTK
  2. #include "gpkplotting.h"
  3. #include "string.h"
  4.  
  5. static gint num_plotwindows = 0;
  6. static gint max_plotwindows = 10;
  7. static GdkPixmap *pixmaps[10];
  8. static GtkWidget *pixmapboxes[10];
  9.  
  10.  
  11.  
  12.  
  13. /* compute a gdkcolor */
  14. void setcolor(GtkWidget *widget, GdkColor *color, gint red,gint green,gint blue)
  15. {
  16.  
  17.   /* colors in GdkColor are taken from 0 to 65535, not 0 to 255.    */
  18.   color->red = red * (65535/255);
  19.   color->green = green * (65535/255);
  20.   color->blue = blue * (65535/255);
  21.   color->pixel = (gulong)(color->red*65536 + color->green*256 + color->blue);
  22.   /* find closest in colormap, if needed */
  23.   gdk_color_alloc(gtk_widget_get_colormap(widget),color);
  24. }
  25.  
  26.  
  27. void gpk_redraw(GdkPixmap *pixmap, GtkWidget *pixmapbox)
  28. {
  29.   /* redraw the entire pixmap */
  30.   gdk_draw_pixmap(pixmapbox->window,
  31.           pixmapbox->style->fg_gc[GTK_WIDGET_STATE (pixmapbox)],
  32.           pixmap,0,0,0,0,
  33.           pixmapbox->allocation.width,
  34.           pixmapbox->allocation.height);
  35. }
  36.  
  37.  
  38. static GdkPixmap **findpixmap(GtkWidget *widget)
  39. {
  40.   int i;
  41.   for (i=0; i<num_plotwindows  && widget != pixmapboxes[i] ; i++);
  42.   if (i>=num_plotwindows) {
  43.     g_print("findpixmap(): bad argument widget \n");
  44.     return NULL;
  45.   }
  46.   return &pixmaps[i];
  47. }
  48.  
  49. void gpk_graph_draw(GtkWidget *widget,               /* plot on this widged */
  50.            int n,                           /* number of data points */
  51.            gdouble *xcord, gdouble *ycord,  /* data */
  52.            gdouble xmn,gdouble ymn,         /* coordinates of corners */
  53.            gdouble xmx,gdouble ymx,
  54.                    int clear,                       /* clear old plot first */
  55.            char *title,                     /* add a title (only if clear=1) */
  56.                    GdkColor *color)            
  57. {
  58.   GdkPixmap **ppixmap;
  59.   GdkPoint *points;
  60.   int i;
  61.   gint16 width,height;
  62.   GdkFont *fixed_font;
  63.   GdkGC *gc;
  64.  
  65.   gc = gdk_gc_new(widget->window);
  66.   gdk_gc_set_foreground(gc, color);
  67.  
  68.  
  69.  
  70.   if ((ppixmap=findpixmap(widget))) {
  71.     width = widget->allocation.width;
  72.     height = widget->allocation.height;
  73.  
  74.  
  75.     if (clear) {
  76.       /* white background */
  77.       gdk_draw_rectangle (*ppixmap,
  78.               widget->style->white_gc,
  79.               TRUE,0, 0,width,height);
  80.       /* title */
  81. #ifdef _WIN32
  82.       fixed_font = gdk_font_load ("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
  83. #else
  84.       fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
  85. #endif
  86.  
  87.       gdk_draw_text (*ppixmap,fixed_font,
  88.              widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
  89.              0,10,title,strlen(title));
  90.     }
  91.       
  92.  
  93.     points = g_malloc(n*sizeof(GdkPoint));
  94.     for (i=0; i<n ; i++) {
  95.       points[i].x =.5+  ((xcord[i]-xmn)*(width-1)/(xmx-xmn));
  96.       points[i].y =.5+  ((ycord[i]-ymx)*(height-1)/(ymn-ymx));
  97.     }
  98.     gdk_draw_lines(*ppixmap,gc,points,n);
  99.     g_free(points);
  100.     gpk_redraw(*ppixmap,widget);
  101.   }
  102.   gdk_gc_destroy(gc);
  103. }
  104.  
  105.  
  106.  
  107. void gpk_rectangle_draw(GtkWidget *widget,              /* plot on this widged */
  108.             gdouble *xcord, gdouble *ycord, /* corners */
  109.             gdouble xmn,gdouble ymn,        /* coordinates of corners */
  110.             gdouble xmx,gdouble ymx,
  111.             GdkColor *color)
  112. {
  113.   GdkPixmap **ppixmap;
  114.   GdkPoint points[2];
  115.   int i;
  116.   gint16 width,height;
  117.   GdkGC *gc;
  118.  
  119.  
  120.   gc = gdk_gc_new(widget->window);
  121.   gdk_gc_set_foreground(gc, color);
  122.  
  123.  
  124.   if ((ppixmap=findpixmap(widget))) {
  125.     width = widget->allocation.width;
  126.     height = widget->allocation.height;
  127.  
  128.  
  129.     for (i=0; i<2 ; i++) {
  130.       points[i].x =.5+  ((xcord[i]-xmn)*(width-1)/(xmx-xmn));
  131.       points[i].y =.5+  ((ycord[i]-ymx)*(height-1)/(ymn-ymx));
  132.     }
  133.     width=points[1].x-points[0].x + 1;
  134.     height=points[1].y-points[0].y + 1;
  135.     gdk_draw_rectangle(*ppixmap,gc,TRUE,
  136.                points[0].x,points[0].y,width,height);
  137.     gpk_redraw(*ppixmap,widget);
  138.   }
  139.   gdk_gc_destroy(gc);
  140. }
  141.  
  142.  
  143.  
  144. void gpk_bargraph_draw(GtkWidget *widget,           /* plot on this widged */
  145.            int n,                           /* number of data points */
  146.            gdouble *xcord, gdouble *ycord,  /* data */
  147.            gdouble xmn,gdouble ymn,         /* coordinates of corners */
  148.            gdouble xmx,gdouble ymx,
  149.                    int clear,                       /* clear old plot first */
  150.            char *title,                     /* add a title (only if clear=1) */
  151.                    int barwidth,                    /* bar width. 0=compute based on window size */    
  152.                    GdkColor *color)            
  153. {
  154.   GdkPixmap **ppixmap;
  155.   GdkPoint points[2];
  156.   int i;
  157.   gint16 width,height,x,y,barheight;
  158.   GdkFont *fixed_font;
  159.   GdkGC *gc;
  160.  
  161.  
  162.   gc = gdk_gc_new(widget->window);
  163.   gdk_gc_set_foreground(gc, color);
  164.  
  165.  
  166.   if ((ppixmap=findpixmap(widget))) {
  167.     width = widget->allocation.width;
  168.     height = widget->allocation.height;
  169.  
  170.  
  171.     if (clear) {
  172.       /* white background */
  173.       gdk_draw_rectangle (*ppixmap,
  174.               widget->style->white_gc,
  175.               TRUE,0, 0,width,height);
  176.       /* title */
  177. #ifdef _WIN32
  178.       fixed_font = gdk_font_load ("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
  179. #else
  180.       fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
  181. #endif
  182.  
  183.       gdk_draw_text (*ppixmap,fixed_font,
  184.              widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
  185.              0,10,title,strlen(title));
  186.     }
  187.       
  188.  
  189.     for (i=0; i<n ; i++) {
  190.       points[1].x =.5+  ((xcord[i]-xmn)*(width-1)/(xmx-xmn));
  191.       points[1].y =.5+  ((ycord[i]-ymx)*(height-1)/(ymn-ymx));
  192.       points[0].x = points[1].x;
  193.       points[0].y = height-1;
  194.  
  195.       x = .5+  ((xcord[i]-xmn)*(width-1)/(xmx-xmn));
  196.       y = .5+((ycord[i]-ymx)*(height-1)/(ymn-ymx));
  197.       if (!barwidth) barwidth  = (width/(n+1))-1;
  198.       barwidth = barwidth > 5 ? 5 : barwidth;
  199.       barwidth = barwidth < 1 ? 1 : barwidth;
  200.       barheight = height-1 - y;
  201.       /* gdk_draw_lines(*ppixmap,gc,points,2); */
  202.       gdk_draw_rectangle(*ppixmap,gc,TRUE,x,y,barwidth,barheight);
  203.  
  204.     }
  205.     gpk_redraw(*ppixmap,widget);
  206.   }
  207.   gdk_gc_destroy(gc);
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /* Create a new backing pixmap of the appropriate size */
  215. static gint
  216. configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data)
  217. {
  218.   GdkPixmap **ppixmap;
  219.   if ((ppixmap=findpixmap(widget))){
  220.     if (*ppixmap) gdk_pixmap_unref(*ppixmap);
  221.     *ppixmap = gdk_pixmap_new(widget->window,
  222.                 widget->allocation.width,
  223.                 widget->allocation.height,
  224.                 -1);
  225.     gdk_draw_rectangle (*ppixmap,
  226.             widget->style->white_gc,
  227.             TRUE,
  228.             0, 0,
  229.             widget->allocation.width,
  230.             widget->allocation.height);
  231.   }
  232.   return TRUE;
  233. }
  234.  
  235.  
  236.  
  237. /* Redraw the screen from the backing pixmap */
  238. static gint
  239. expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data)
  240. {
  241.   GdkPixmap **ppixmap;
  242.   if ((ppixmap=findpixmap(widget))){
  243.     gdk_draw_pixmap(widget->window,
  244.             widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
  245.             *ppixmap,
  246.             event->area.x, event->area.y,
  247.             event->area.x, event->area.y,
  248.             event->area.width, event->area.height);
  249.   }
  250.  
  251.   return FALSE;
  252. }
  253.  
  254.  
  255.  
  256.  
  257.  
  258. GtkWidget *gpk_plot_new(int width, int height)
  259. {
  260.   GtkWidget *pixmapbox;
  261.    
  262.   pixmapbox = gtk_drawing_area_new();
  263.   gtk_drawing_area_size(GTK_DRAWING_AREA(pixmapbox),width,height);
  264.   gtk_signal_connect (GTK_OBJECT (pixmapbox), "expose_event",
  265.               (GtkSignalFunc) expose_event, NULL);
  266.   gtk_signal_connect (GTK_OBJECT(pixmapbox),"configure_event",
  267.               (GtkSignalFunc) configure_event, NULL);
  268.   gtk_widget_set_events (pixmapbox, GDK_EXPOSURE_MASK);
  269.  
  270.   if (num_plotwindows < max_plotwindows) {
  271.     pixmapboxes[num_plotwindows] = pixmapbox;
  272.     pixmaps[num_plotwindows] = NULL;
  273.     num_plotwindows ++;
  274.   } else {
  275.     g_print("gtk_plotarea_new(): exceeded maximum of 10 plotarea windows\n");
  276.   }
  277.  
  278.   return pixmapbox;
  279. }
  280.  
  281.  
  282. #endif
  283.