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