//
// $Id: ipfw-gtk.c,v 1.4 2001/11/22 13:29:04 mavetju Exp $
//

//
// These are the GTK-related functions to build a window, to resize
// it and pressing of a button.
//

#include <gtk/gtk.h>
#include "ipfw-graph.h"

gint		current_page=0;
GdkPixmap *	bytes_pixmap=NULL;
GtkWidget *	bytes_area=NULL;
GtkWidget *	text_area=NULL;
GdkGC *		gc_white=NULL;


//
// Get a new colour for the colourmap.
// Usage: GetPen(NewColour(red,green,blue))
//
// Credits: found on the Internet somewhere
//
GdkGC *GetPen(GdkColor *c) {
    GdkGC *gc;

    gc=gdk_gc_new(bytes_pixmap);
    gdk_gc_set_foreground(gc,c);
    return gc;
}

GdkColor *NewColour(long red,long green,long blue) {
    GdkColor *c=(GdkColor *)g_malloc(sizeof(GdkColor));

    c->red=red;
    c->green=green;
    c->blue=blue;

    gdk_color_alloc(gdk_colormap_get_system(),c);
    return c;
}

//
// Called after a switch of page
//
void notebook_switch_page(  GtkNotebook *notebook,
			    GtkNotebookPage *page,
			    gint             page_num,
			    gpointer         user_data) {
    debug("notebook_switch_page: %d",page_num);
    current_page=page_num;
    (*drawtable[current_page].bgdrawing_fun)();
    (*drawtable[current_page].drawing_fun)();
}



//
// Called after a resize of the window
//
/* Create a new backing pixmap of the appropriate size */
gint configure_bytes_event(GtkWidget *widget,GdkEventConfigure *event) {
    if (bytes_pixmap!=NULL)
	gdk_pixmap_unref(bytes_pixmap);

    ysize_bytes=bytes_area->allocation.height;
    if (data_size!=0) {
	if (bytes_area->allocation.width>MAXSTORED)
	    xsize=MAXSTORED;
	else
	    xsize=bytes_area->allocation.width;
    }

    bytes_pixmap = gdk_pixmap_new(widget->window,
			    widget->allocation.width,
			    widget->allocation.height,
			    -1);
    gdk_draw_rectangle (bytes_pixmap,
			widget->style->white_gc,
			TRUE,
			0, 0,
			widget->allocation.width,
			widget->allocation.height);
    (*drawtable[current_page].bgbytes_fun)();
    (*drawtable[current_page].bytes_fun)();
    return TRUE;
}

gint configure_drawing_event(GtkWidget *widget,GdkEventConfigure *event) {
    int i;
    for (i=0;drawtable[i].label!=NULL;i++)
	if (widget==drawtable[i].area)
	    break;
    debug("configure_drawing_event: %d (current is %d)",i,current_page);
    if (drawtable[i].label==NULL)
	return TRUE;

    if (drawtable[i].pixmap!=NULL)
	gdk_pixmap_unref(drawtable[i].pixmap);

    ysize_drawing=drawtable[i].area->allocation.height;

    drawtable[i].pixmap = gdk_pixmap_new(widget->window,
			    widget->allocation.width,
			    widget->allocation.height,
			    -1);
    gdk_draw_rectangle(drawtable[i].pixmap,
			widget->style->white_gc,
			TRUE,
			0, 0,
			widget->allocation.width,
			widget->allocation.height);
    (*drawtable[i].bgdrawing_fun)();
    (*drawtable[i].drawing_fun)();
    return TRUE;
}

//
// Redraw the screen from the bytes pixmap
//
gint expose_bytes_event(GtkWidget *widget, GdkEventExpose *event) {
    gdk_draw_pixmap(widget->window,
		    widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		    bytes_pixmap,
		    event->area.x, event->area.y,
		    event->area.x, event->area.y,
		    event->area.width, event->area.height);
    return FALSE;
}

gint expose_drawing_event(GtkWidget *widget, GdkEventExpose *event) {
    int i;
    for (i=0;drawtable[i].label!=NULL;i++)
	if (widget==drawtable[i].area)
	    break;
    debug("expose_drawing_event: %d (current is %d)",i,current_page);
    if (drawtable[i].label==NULL)
	return TRUE;

    gdk_draw_pixmap(widget->window,
		    widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		    drawtable[i].pixmap,
		    event->area.x, event->area.y,
		    event->area.x, event->area.y,
		    event->area.width, event->area.height);
    return FALSE;
}

//
// Quit button
//
void quit(void) {
    gtk_exit(0);
}

//
// Initialize the GTK system
//
void init_gtk(int *argc,char **argv[]) {
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *notebook;
    GtkWidget *label;

    GtkWidget *button;
    GtkStyle *style;

    int i;

    gtk_init(argc,argv);

    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_name(window,"Test Input");
    gtk_window_set_policy(GTK_WINDOW(window),TRUE,TRUE,FALSE);

    vbox=gtk_vbox_new(FALSE,0);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    gtk_widget_show(vbox);

    gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(quit),NULL);

    //
    // notebook
    //
    notebook=gtk_notebook_new();
    gtk_widget_ref(notebook);
    gtk_object_set_data_full(GTK_OBJECT(window),"notebook",
		    notebook,(GtkDestroyNotify)gtk_widget_unref);
    gtk_widget_show(notebook);
    gtk_box_pack_start(GTK_BOX(vbox),notebook,TRUE,TRUE,0);

    gtk_signal_connect(	GTK_OBJECT(notebook),"switch_page",
			GTK_SIGNAL_FUNC(notebook_switch_page),
			NULL);


    for (i=0;drawtable[i].label!=NULL;i++) {
	drawtable[i].area=gtk_drawing_area_new();
	gtk_widget_ref(drawtable[i].area);
	gtk_object_set_data_full(GTK_OBJECT(window),drawtable[i].label,
				drawtable[i].area,
				(GtkDestroyNotify)gtk_widget_unref);
	gtk_drawing_area_size(GTK_DRAWING_AREA(drawtable[i].area),200,300);
	gtk_widget_show(drawtable[i].area);
	gtk_container_add(GTK_CONTAINER(notebook),drawtable[i].area);

	label=gtk_label_new(drawtable[i].label);
	gtk_widget_ref(label);
	gtk_object_set_data_full(GTK_OBJECT(window),drawtable[i].label,label,
			(GtkDestroyNotify)gtk_widget_unref);
	gtk_widget_show(label);
	gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook),
			gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook),i),
			label);

	gtk_signal_connect(GTK_OBJECT(drawtable[i].area),"expose_event",
			    (GtkSignalFunc)expose_drawing_event,NULL);
	gtk_signal_connect(GTK_OBJECT(drawtable[i].area),"configure_event",
			    (GtkSignalFunc)configure_drawing_event,NULL);
	gtk_widget_set_events(drawtable[i].area,GDK_EXPOSURE_MASK );
	gtk_widget_show(drawtable[i].area);
    }

    bytes_area=gtk_drawing_area_new();
    gtk_drawing_area_size(GTK_DRAWING_AREA(bytes_area),200,50);
    gtk_box_pack_start(GTK_BOX(vbox),bytes_area,TRUE,TRUE,0);
    gtk_widget_show(bytes_area);

    text_area=gtk_entry_new();
    gtk_entry_set_text((GtkEntry *)text_area,"blaat");
    gtk_box_pack_start(GTK_BOX(vbox),text_area,FALSE,FALSE,0);
    gtk_widget_show(text_area);

    /* Signals used to handle backing pixmap */
    gtk_signal_connect(GTK_OBJECT(bytes_area),"expose_event",
			(GtkSignalFunc)expose_bytes_event,NULL);
    gtk_signal_connect(GTK_OBJECT(bytes_area),"configure_event",
			(GtkSignalFunc)configure_bytes_event,NULL);

    /* .. And some buttons */
    button=gtk_button_new_with_label("Quit");
    gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,0);

    gtk_signal_connect_object(GTK_OBJECT(button),"clicked",
				GTK_SIGNAL_FUNC(gtk_widget_destroy),
				GTK_OBJECT(window));
    gtk_widget_show(button);
    gtk_widget_show(window);

    style=gtk_widget_get_default_style();
    gc_white=GetPen(NewColour(0xffff,0xffff,0xffff));

    xsize=drawtable[0].area->allocation.width;
    ysize_drawing=drawtable[0].area->allocation.height;
    ysize_bytes=bytes_area->allocation.height;
}


syntax highlighted by Code2HTML, v. 0.9.1