Blame frontend/gpkplotting.c

Packit 47f805
/*
Packit 47f805
 *	GTK plotting routines source file
Packit 47f805
 *
Packit 47f805
 *	Copyright (c) 1999 Mark Taylor
Packit 47f805
 *
Packit 47f805
 * This library is free software; you can redistribute it and/or
Packit 47f805
 * modify it under the terms of the GNU Library General Public
Packit 47f805
 * License as published by the Free Software Foundation; either
Packit 47f805
 * version 2 of the License, or (at your option) any later version.
Packit 47f805
 *
Packit 47f805
 * This library is distributed in the hope that it will be useful,
Packit 47f805
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47f805
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit 47f805
 * Library General Public License for more details.
Packit 47f805
 *
Packit 47f805
 * You should have received a copy of the GNU Library General Public
Packit 47f805
 * License along with this library; if not, write to the
Packit 47f805
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 47f805
 * Boston, MA 02111-1307, USA.
Packit 47f805
 */
Packit 47f805
Packit 47f805
/* $Id: gpkplotting.c,v 1.12 2011/05/07 16:05:17 rbrito Exp $ */
Packit 47f805
Packit 47f805
#ifdef HAVE_CONFIG_H
Packit 47f805
# include <config.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#include "gpkplotting.h"
Packit 47f805
Packit 47f805
#ifdef STDC_HEADERS
Packit 47f805
# include <string.h>
Packit 47f805
#else
Packit 47f805
# ifndef HAVE_STRCHR
Packit 47f805
#  define strchr index
Packit 47f805
#  define strrchr rindex
Packit 47f805
# endif
Packit 47f805
char   *strchr(), *strrchr();
Packit 47f805
# ifndef HAVE_MEMCPY
Packit 47f805
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
Packit 47f805
#  define memmove(d, s, n) bcopy ((s), (d), (n))
Packit 47f805
# endif
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#ifdef WITH_DMALLOC
Packit 47f805
#include <dmalloc.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
static gint num_plotwindows = 0;
Packit 47f805
static gint max_plotwindows = 10;
Packit 47f805
static GdkPixmap *pixmaps[10];
Packit 47f805
static GtkWidget *pixmapboxes[10];
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/* compute a gdkcolor */
Packit 47f805
void
Packit 47f805
setcolor(GtkWidget * widget, GdkColor * color, gint red, gint green, gint blue)
Packit 47f805
{
Packit 47f805
Packit 47f805
    /* colors in GdkColor are taken from 0 to 65535, not 0 to 255.    */
Packit 47f805
    color->red = red * (65535 / 255);
Packit 47f805
    color->green = green * (65535 / 255);
Packit 47f805
    color->blue = blue * (65535 / 255);
Packit 47f805
    color->pixel = (gulong) (color->red * 65536 + color->green * 256 + color->blue);
Packit 47f805
    /* find closest in colormap, if needed */
Packit 47f805
    gdk_color_alloc(gtk_widget_get_colormap(widget), color);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
gpk_redraw(GdkPixmap * pixmap, GtkWidget * pixmapbox)
Packit 47f805
{
Packit 47f805
    /* redraw the entire pixmap */
Packit 47f805
    gdk_draw_pixmap(pixmapbox->window,
Packit 47f805
                    pixmapbox->style->fg_gc[GTK_WIDGET_STATE(pixmapbox)],
Packit 47f805
                    pixmap, 0, 0, 0, 0, pixmapbox->allocation.width, pixmapbox->allocation.height);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
static GdkPixmap **
Packit 47f805
findpixmap(GtkWidget * widget)
Packit 47f805
{
Packit 47f805
    int     i;
Packit 47f805
    for (i = 0; i < num_plotwindows && widget != pixmapboxes[i]; i++);
Packit 47f805
    if (i >= num_plotwindows) {
Packit 47f805
        g_print("findpixmap(): bad argument widget \n");
Packit 47f805
        return NULL;
Packit 47f805
    }
Packit 47f805
    return &pixmaps[i];
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
gpk_graph_draw(GtkWidget * widget, /* plot on this widged */
Packit 47f805
               int n,        /* number of data points */
Packit 47f805
               gdouble * xcord, gdouble * ycord, /* data */
Packit 47f805
               gdouble xmn, gdouble ymn, /* coordinates of corners */
Packit 47f805
               gdouble xmx, gdouble ymx, int clear, /* clear old plot first */
Packit 47f805
               char *title,  /* add a title (only if clear=1) */
Packit 47f805
               GdkColor * color)
Packit 47f805
{
Packit 47f805
    GdkPixmap **ppixmap;
Packit 47f805
    GdkPoint *points;
Packit 47f805
    int     i;
Packit 47f805
    gint16  width, height;
Packit 47f805
    GdkFont *fixed_font;
Packit 47f805
    GdkGC  *gc;
Packit 47f805
Packit 47f805
    gc = gdk_gc_new(widget->window);
Packit 47f805
    gdk_gc_set_foreground(gc, color);
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
    if ((ppixmap = findpixmap(widget))) {
Packit 47f805
        width = widget->allocation.width;
Packit 47f805
        height = widget->allocation.height;
Packit 47f805
Packit 47f805
Packit 47f805
        if (clear) {
Packit 47f805
            /* white background */
Packit 47f805
            gdk_draw_rectangle(*ppixmap, widget->style->white_gc, TRUE, 0, 0, width, height);
Packit 47f805
            /* title */
Packit 47f805
#ifdef _WIN32
Packit 47f805
            fixed_font = gdk_font_load("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
Packit 47f805
#else
Packit 47f805
            fixed_font = gdk_font_load("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
Packit 47f805
#endif
Packit 47f805
Packit 47f805
            gdk_draw_text(*ppixmap, fixed_font,
Packit 47f805
                          widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
Packit 47f805
                          0, 10, title, strlen(title));
Packit 47f805
        }
Packit 47f805
Packit 47f805
Packit 47f805
        points = g_malloc(n * sizeof(GdkPoint));
Packit 47f805
        for (i = 0; i < n; i++) {
Packit 47f805
            points[i].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
Packit 47f805
            points[i].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
Packit 47f805
        }
Packit 47f805
        gdk_draw_lines(*ppixmap, gc, points, n);
Packit 47f805
        g_free(points);
Packit 47f805
        gpk_redraw(*ppixmap, widget);
Packit 47f805
    }
Packit 47f805
    gdk_gc_destroy(gc);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
gpk_rectangle_draw(GtkWidget * widget, /* plot on this widged */
Packit 47f805
                   gdouble * xcord, gdouble * ycord, /* corners */
Packit 47f805
                   gdouble xmn, gdouble ymn, /* coordinates of corners */
Packit 47f805
                   gdouble xmx, gdouble ymx, GdkColor * color)
Packit 47f805
{
Packit 47f805
    GdkPixmap **ppixmap;
Packit 47f805
    GdkPoint points[2];
Packit 47f805
    int     i;
Packit 47f805
    gint16  width, height;
Packit 47f805
    GdkGC  *gc;
Packit 47f805
Packit 47f805
Packit 47f805
    gc = gdk_gc_new(widget->window);
Packit 47f805
    gdk_gc_set_foreground(gc, color);
Packit 47f805
Packit 47f805
Packit 47f805
    if ((ppixmap = findpixmap(widget))) {
Packit 47f805
        width = widget->allocation.width;
Packit 47f805
        height = widget->allocation.height;
Packit 47f805
Packit 47f805
Packit 47f805
        for (i = 0; i < 2; i++) {
Packit 47f805
            points[i].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
Packit 47f805
            points[i].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
Packit 47f805
        }
Packit 47f805
        width = points[1].x - points[0].x + 1;
Packit 47f805
        height = points[1].y - points[0].y + 1;
Packit 47f805
        gdk_draw_rectangle(*ppixmap, gc, TRUE, points[0].x, points[0].y, width, height);
Packit 47f805
        gpk_redraw(*ppixmap, widget);
Packit 47f805
    }
Packit 47f805
    gdk_gc_destroy(gc);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
gpk_bargraph_draw(GtkWidget * widget, /* plot on this widged */
Packit 47f805
                  int n,     /* number of data points */
Packit 47f805
                  gdouble * xcord, gdouble * ycord, /* data */
Packit 47f805
                  gdouble xmn, gdouble ymn, /* coordinates of corners */
Packit 47f805
                  gdouble xmx, gdouble ymx, int clear, /* clear old plot first */
Packit 47f805
                  char *title, /* add a title (only if clear=1) */
Packit 47f805
                  int barwidth, /* bar width. 0=compute based on window size */
Packit 47f805
                  GdkColor * color)
Packit 47f805
{
Packit 47f805
    GdkPixmap **ppixmap;
Packit 47f805
    GdkPoint points[2];
Packit 47f805
    int     i;
Packit 47f805
    gint16  width, height, x, y, barheight;
Packit 47f805
    GdkFont *fixed_font;
Packit 47f805
    GdkGC  *gc;
Packit 47f805
    int     titleSplit;
Packit 47f805
Packit 47f805
Packit 47f805
    gc = gdk_gc_new(widget->window);
Packit 47f805
    gdk_gc_set_foreground(gc, color);
Packit 47f805
Packit 47f805
Packit 47f805
    if ((ppixmap = findpixmap(widget))) {
Packit 47f805
        width = widget->allocation.width;
Packit 47f805
        height = widget->allocation.height;
Packit 47f805
Packit 47f805
Packit 47f805
        if (clear) {
Packit 47f805
            /* white background */
Packit 47f805
            gdk_draw_rectangle(*ppixmap, widget->style->white_gc, TRUE, 0, 0, width, height);
Packit 47f805
            /* title */
Packit 47f805
#ifdef _WIN32
Packit 47f805
            fixed_font = gdk_font_load("-misc-fixed-large-r-*-*-*-100-*-*-*-*-*-*");
Packit 47f805
#else
Packit 47f805
            fixed_font = gdk_font_load("-misc-fixed-medium-r-*-*-*-100-*-*-*-*-iso8859-1");
Packit 47f805
#endif
Packit 47f805
Packit 47f805
            titleSplit = strcspn(title, "\n");
Packit 47f805
Packit 47f805
            if (titleSplit && (titleSplit != strlen(title))) {
Packit 47f805
                gdk_draw_text(*ppixmap, fixed_font,
Packit 47f805
                              widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
Packit 47f805
                              0, 10, title, titleSplit);
Packit 47f805
Packit 47f805
                gdk_draw_text(*ppixmap, fixed_font,
Packit 47f805
                              widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
Packit 47f805
                              0, 22, title + titleSplit + 1, (strlen(title) - titleSplit) - 1);
Packit 47f805
Packit 47f805
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                gdk_draw_text(*ppixmap, fixed_font,
Packit 47f805
                              widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
Packit 47f805
                              0, 10, title, strlen(title));
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
Packit 47f805
Packit 47f805
        for (i = 0; i < n; i++) {
Packit 47f805
            points[1].x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
Packit 47f805
            points[1].y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
Packit 47f805
            points[0].x = points[1].x;
Packit 47f805
            points[0].y = height - 1;
Packit 47f805
Packit 47f805
            x = .5 + ((xcord[i] - xmn) * (width - 1) / (xmx - xmn));
Packit 47f805
            y = .5 + ((ycord[i] - ymx) * (height - 1) / (ymn - ymx));
Packit 47f805
            if (!barwidth)
Packit 47f805
                barwidth = (width / (n + 1)) - 1;
Packit 47f805
            barwidth = barwidth > 5 ? 5 : barwidth;
Packit 47f805
            barwidth = barwidth < 1 ? 1 : barwidth;
Packit 47f805
            barheight = height - 1 - y;
Packit 47f805
            /* gdk_draw_lines(*ppixmap,gc,points,2); */
Packit 47f805
            gdk_draw_rectangle(*ppixmap, gc, TRUE, x, y, barwidth, barheight);
Packit 47f805
Packit 47f805
        }
Packit 47f805
        gpk_redraw(*ppixmap, widget);
Packit 47f805
    }
Packit 47f805
    gdk_gc_destroy(gc);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/* Create a new backing pixmap of the appropriate size */
Packit 47f805
static  gint
Packit 47f805
configure_event(GtkWidget * widget, GdkEventConfigure * event, gpointer data)
Packit 47f805
{
Packit 47f805
    GdkPixmap **ppixmap;
Packit 47f805
    if ((ppixmap = findpixmap(widget))) {
Packit 47f805
        if (*ppixmap)
Packit 47f805
            gdk_pixmap_unref(*ppixmap);
Packit 47f805
        *ppixmap = gdk_pixmap_new(widget->window,
Packit 47f805
                                  widget->allocation.width, widget->allocation.height, -1);
Packit 47f805
        gdk_draw_rectangle(*ppixmap,
Packit 47f805
                           widget->style->white_gc,
Packit 47f805
                           TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
Packit 47f805
    }
Packit 47f805
    return TRUE;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/* Redraw the screen from the backing pixmap */
Packit 47f805
static  gint
Packit 47f805
expose_event(GtkWidget * widget, GdkEventExpose * event, gpointer data)
Packit 47f805
{
Packit 47f805
    GdkPixmap **ppixmap;
Packit 47f805
    if ((ppixmap = findpixmap(widget))) {
Packit 47f805
        gdk_draw_pixmap(widget->window,
Packit 47f805
                        widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
Packit 47f805
                        *ppixmap,
Packit 47f805
                        event->area.x, event->area.y,
Packit 47f805
                        event->area.x, event->area.y, event->area.width, event->area.height);
Packit 47f805
    }
Packit 47f805
Packit 47f805
    return FALSE;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
GtkWidget *
Packit 47f805
gpk_plot_new(int width, int height)
Packit 47f805
{
Packit 47f805
    GtkWidget *pixmapbox;
Packit 47f805
Packit 47f805
    pixmapbox = gtk_drawing_area_new();
Packit 47f805
    gtk_drawing_area_size(GTK_DRAWING_AREA(pixmapbox), width, height);
Packit 47f805
    gtk_signal_connect(GTK_OBJECT(pixmapbox), "expose_event", (GtkSignalFunc) expose_event, NULL);
Packit 47f805
    gtk_signal_connect(GTK_OBJECT(pixmapbox), "configure_event",
Packit 47f805
                       (GtkSignalFunc) configure_event, NULL);
Packit 47f805
    gtk_widget_set_events(pixmapbox, GDK_EXPOSURE_MASK);
Packit 47f805
Packit 47f805
    if (num_plotwindows < max_plotwindows) {
Packit 47f805
        pixmapboxes[num_plotwindows] = pixmapbox;
Packit 47f805
        pixmaps[num_plotwindows] = NULL;
Packit 47f805
        num_plotwindows++;
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        g_print("gtk_plotarea_new(): exceeded maximum of 10 plotarea windows\n");
Packit 47f805
    }
Packit 47f805
Packit 47f805
    return pixmapbox;
Packit 47f805
}