#62 GtkPlot: no screen updated while plotting in real time

open
nobody
None
5
2006-02-23
2006-02-23
Anonymous
No

Below is a test case based closely on the
testrealtime.c example that comes with GtkExtra v2.1.1.
To reproduce the bug,

1. compile and run the code as is. THEN
2. uncomment the line that reads:

/* #define SHOW_EACH_WIDGET */

and

3. compare the outcomes from (1) and (2).

Under normal circumstances, both versions must behave
identically. However, you will see that the screen
does not get updated when the #define directive is
commented out.

If necessary, please feel free to contact me at
ndo<<<brynin -- g<<<<<m ail <<<<< c<<o m. (No dashes,
spaces, and < signs).

Regards,

Nickolai Dobrynin

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "gtkextra/gtkplot.h"
#include "gtkextra/gtkplotdata.h"
#include "gtkextra/gtkplotcanvas.h"
#include "gtkextra/gtkplotcanvasplot.h"
#include "gtkextra/gtkplotcanvastext.h"

/* #define SHOW_EACH_WIDGET */

GdkPixmap *pixmap=NULL;
GtkWidget **plots;
GtkPlotData *dataset;
gint nlayers = 0;
gint32 timer;
GtkWidget *canvas;
gdouble *px;
gdouble *py;

void
quit ()
{
gtk_timeout_remove(timer);
gtk_main_quit();

g_free(px);
g_free(py);
g_free(plots);
}

GtkWidget *
new_layer(GtkWidget *)
{
gchar label[10];

nlayers++;

plots = (GtkWidget **)g_realloc(plots, nlayers *
sizeof(GtkWidget *));

sprintf(label, "%d", nlayers);

plots[nlayers-1] = gtk_plot_new_with_size(NULL, .65, .45);

return plots[nlayers-1];
}

gint
update(gpointer data)
{
GtkPlot *plot;
gdouble x, y;
gdouble xmin, xmax;

plot = GTK_PLOT(data);

px = (gdouble *)g_realloc(px,
(dataset->num_points+1)*sizeof(gdouble));
py = (gdouble *)g_realloc(py,
(dataset->num_points+1)*sizeof(gdouble));

y = rand()%10/10.;

if(dataset->num_points == 0)
x = 1;
else
x = px[dataset->num_points-1] + 1;

px[dataset->num_points] = x;
py[dataset->num_points] = y;

gtk_plot_data_set_numpoints(dataset,
dataset->num_points+1);
gtk_plot_data_set_x(dataset, px);
gtk_plot_data_set_y(dataset, py);

gtk_plot_get_xrange(plot, &xmin , &xmax);

if(x > xmax){
gtk_plot_set_xrange(plot, xmin + 5. , xmax + 5.);
gtk_plot_canvas_paint(GTK_PLOT_CANVAS(canvas));
gtk_widget_queue_draw(GTK_WIDGET(canvas));
} else {
gtk_plot_data_draw_points(dataset, 1);
gtk_plot_refresh(plot, NULL);
}

return TRUE;
}

int main(int argc, char *argv[]){

GtkWidget *window1;
GtkWidget *active_plot;
GtkPlotCanvasChild *child;
GdkColor color;
gint page_width, page_height;
gfloat scale = 1.;

page_width = (guint)(GTK_PLOT_LETTER_W * scale);
page_height = (guint)(GTK_PLOT_LETTER_H * scale);

gtk_init(&argc,&argv);

/* Main window: */
window1=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window1), "GtkPlot
Real Time Demo");
gtk_widget_set_usize(window1,550,600);
gtk_container_border_width(GTK_CONTAINER(window1),0);
#ifdef SHOW_EACH_WIDGET
gtk_widget_show(window1);
#endif
gtk_signal_connect (GTK_OBJECT (window1), "destroy",
GTK_SIGNAL_FUNC (quit), NULL);

/* Canvas: */
canvas = gtk_plot_canvas_new(page_width, page_height, 1.);
GTK_PLOT_CANVAS_UNSET_FLAGS(GTK_PLOT_CANVAS(canvas),
GTK_PLOT_CANVAS_DND_FLAGS);
gtk_container_add(GTK_CONTAINER(window1), canvas);

gdk_color_parse("light blue", &color);
gdk_color_alloc(gtk_widget_get_colormap(canvas), &color);
gtk_plot_canvas_set_background(GTK_PLOT_CANVAS(canvas),
&color);
#ifdef SHOW_EACH_WIDGET
gtk_widget_show(canvas);
#endif

/* Plot: */
active_plot = new_layer(canvas);

gtk_plot_set_range(GTK_PLOT(active_plot), 0. ,20., 0.,
1.);
gtk_plot_set_ticks(GTK_PLOT(active_plot),
GTK_PLOT_AXIS_X, 2, 1);
gtk_plot_set_ticks(GTK_PLOT(active_plot),
GTK_PLOT_AXIS_Y, .1, 1);

/* Plot canvas: */
child = gtk_plot_canvas_plot_new(GTK_PLOT(active_plot));
gtk_plot_canvas_put_child(GTK_PLOT_CANVAS(canvas),
child, .15, .15, .80, .65);
#ifdef SHOW_EACH_WIDGET
gtk_widget_show(active_plot);
#endif

/* Dataset: */
dataset = GTK_PLOT_DATA(gtk_plot_data_new());
gtk_plot_add_data(GTK_PLOT(active_plot), dataset);
#ifdef SHOW_EACH_WIDGET
gtk_widget_show(GTK_WIDGET(dataset));
#endif

gtk_plot_canvas_paint(GTK_PLOT_CANVAS(canvas));
gtk_plot_canvas_refresh(GTK_PLOT_CANVAS(canvas));

gtk_plot_clip_data(GTK_PLOT(active_plot), TRUE);

#ifndef SHOW_EACH_WIDGET
gtk_widget_show_all(GTK_WIDGET(window1));
#endif
timer = gtk_timeout_add(1000, update, active_plot);

gtk_main();

return(0);
}

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks