gtk_draw_area not displaying



Hi all
	got a bit of a problem, i'm doing an assinment for a job interview and am 
writing a program that displays a graph, i'm using the gtk_drawing_area 
widget and some how the widget is not displaying.

any help you can give me on this would be great.

thanks 
	Jase

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

GtkWidget *button_hbox;
GtkWidget *y_label_vbox;
GtkWidget *x_label_vbox;
int graph_data[];
char *s;

char *itoa(int i)
{
	s=malloc(42); /* Enough for a 128 bit integer */
	if (s) sprintf(s,"%d",i);
				
	return s;
}

gint draw_line(int size, gpointer data)
{
	GdkDrawable *drawable;

		
	GtkWidget * drawing_area = (GtkWidget *) data;
	
	/*int array_size = sizeof(graph_data) / sizeof(graph_data[0]);*/
	int i =0;
	int lastX =0;
	int lastY =0;
	int x_pos =0;
	int x_count =0;
	
	drawable = drawing_area->window;

	gdk_draw_rectangle (drawable, drawing_area->style->white_gc, TRUE, 0, 0,
		       drawing_area->allocation.width,					               drawing_area->allocation.height);	       
		
	x_pos = drawing_area->allocation.width / size;
	
	while(i <= size -1)
	{
		if(i == 0)
		{
			lastX = x_pos;
			lastY = graph_data[i];
			x_count = x_count + x_pos;
		}
		else
		{
			x_count = x_count + x_pos;	
		 	gdk_draw_line (drawable, drawing_area->style->black_gc, lastX, (drawing_area->allocation.height - lastY), x_count, (drawing_area->allocation.height - graph_data[i]));

			while (g_main_iteration(FALSE));

			if(i >= 1)
			{
				lastX = x_count;
				lastY = graph_data[i];
			}
			else
			{	
				lastX = x_pos;
				lastY = graph_data[i];
			}
		}
		printf("data: %d\n", graph_data[i] );
		i++;
	}

	while (g_main_iteration(FALSE));


	
	return (FALSE);
}


gint destroyapp(GtkWidget *widget, gpointer gdata)
{
	g_print ("Quitting...\n");
	gtk_main_quit();

	/*free(graph_data);
	free(s);*/
	return (FALSE);
}

int get_data()
{
	FILE *input;
	char buffer[20];
	int value =0;
	int count =0;
	int max =0;
	char env[50];
	char *temp;


	temp  = malloc(sizeof(char) * 50);	
	
	temp = getenv("PWD");
	fprintf(stderr, "temp: %s\n" , temp);
	
	strncat(temp, "/array_data", (strlen("/array_data")) );

	fprintf(stderr, "temp: %s\n" , temp);
	
	if((input = fopen(temp, "r")) == NULL)
		fprintf(stderr, "File not found \n\n");
	
	while(fscanf(input, "%s", buffer) != EOF)
	{
		(int *)graph_data[count] = malloc(sizeof(int) * 1);
		if((int *)graph_data[count] == NULL)
			fprintf(stderr, "graph_data is NULL\n");
		
		
		graph_data[count] = atoi(buffer);
	
		
		if(graph_data[count] >= max)
			max =  graph_data[count];
		
		x_labels(count);
		
		count++;
		
	}

	y_labels(max , count);
	
	fclose(input);
	
	
	
	return count;	
}


gint load_data(gpointer data)
{
	
	int array_size = 0;

	
	array_size = get_data();
	
	draw_line(array_size, data);


	
	/*gtk_main();*/
}

gint x_labels(int num)
{
	GtkWidget *label;

	label = gtk_label_new(itoa(num +1));
	gtk_box_pack_start (GTK_BOX(x_label_vbox), label, TRUE, TRUE, 0);	
	gtk_widget_show(label);
}	

 
gint y_labels(int max, int count)
{
	GtkWidget *label;
	int i = 0;
	int val =0;
	int d=0;
	
	
	if(max <= 500)
	{
		val = 50;
		count = max / val;
	}
	
	
	
	for(i =0; i<count; i++)
	{
		d = d + val;
		printf("val: %d\n", d);	
		if(i == 0)
		{
			label = gtk_label_new(itoa(0));
			gtk_box_pack_end (GTK_BOX(y_label_vbox), label, TRUE, TRUE, 0);
			gtk_widget_show(label);
		        gtk_widget_set_usize(label, 10, 10);
		}
		
			label = gtk_label_new(itoa(d));
		
		gtk_box_pack_end (GTK_BOX(y_label_vbox), label, TRUE, TRUE, 0);
		gtk_widget_show(label);
		 gtk_widget_set_usize(label, 10, 10);
	}
	

}




int main(int argc, char *argv[])
{
	GtkWidget *main_window;
	GtkWidget *main_vbox;
	GtkWidget *graph_hbox;
	GtkWidget *g_label_vbox;	
	GtkWidget *graph_vbox;
	GtkWidget *drawing_area;

	GtkWidget *button;	

	
	gtk_init (&argc, &argv);

	main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (main_window), 600, 500);

	gtk_signal_connect (GTK_OBJECT(main_window), "delete_event", 
			GTK_SIGNAL_FUNC(destroyapp), NULL);

	main_vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add(GTK_CONTAINER (main_window), main_vbox);
		
	graph_hbox = gtk_hbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER (main_vbox), graph_hbox);	

	button_hbox  = gtk_vbox_new(TRUE, 0);
	gtk_container_add(GTK_CONTAINER(main_vbox), button_hbox);
	
	graph_vbox  = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(graph_hbox), graph_vbox);
	
	
	y_label_vbox  = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(graph_hbox), y_label_vbox);
	gtk_widget_set_usize(y_label_vbox, 50, 400);	

		/*gtk_widget_set_usize(x_label_vbox, 50, 400);*/
	/** create drawing area **/
        drawing_area = gtk_drawing_area_new();

        gtk_box_pack_start (GTK_BOX(graph_vbox), drawing_area, TRUE, TRUE, 0);
        gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_area), 500, 450);
       	/*gtk_widget_set_usize(drawing_area, 510, 470);	*/

	gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK);
	
	g_label_vbox = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(graph_vbox), g_label_vbox);
	
	x_label_vbox  = gtk_hbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(g_label_vbox), x_label_vbox);

	
	button = gtk_button_new_with_label("Load Data");
	gtk_signal_connect(GTK_OBJECT (button), "clicked", 
			GTK_SIGNAL_FUNC(load_data), (gpointer) drawing_area);
	gtk_box_pack_start (GTK_BOX(button_hbox), button, FALSE, FALSE, 0);	

	gtk_widget_set_usize(button, 50, 30);

	
	
	gtk_widget_show(button);
	gtk_widget_show(main_vbox);
	gtk_widget_show(graph_hbox);
	gtk_widget_show(button_hbox);
	gtk_widget_show(graph_vbox);
	gtk_widget_show(y_label_vbox);
	gtk_widget_show(g_label_vbox);
	gtk_widget_show(x_label_vbox);
	gtk_widget_show(drawing_area);
	gtk_widget_show(main_window);

	gtk_main();  /** starting event loop **/

	return 0;
	
}
100
90
160
100
400
200
210
30
300
200
50


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]