/* agblock.c */ #include "agblock.h" #include #include static void ag_block_class_init(AgBlockClass *klass); static void ag_block_init(AgBlock *agblock); static void ag_block_realize(GtkWidget *widget); static void ag_block_size_allocate(GtkWidget *widget, GtkAllocation *allocation); static void ag_block_send_configure(AgBlock *agblock); static void ag_block_draw(GtkWidget *widget, GdkRectangle *area); static gint ag_block_expose_event(GtkWidget *widget, GdkEvent *event); static void ag_block_size_request(GtkWidget *widget, GtkRequisition *requisition); static void ag_block_destroy(GtkObject *object); guint ag_block_get_type(void) { static guint ag_block_type = 0; if(!ag_block_type) { static const GtkTypeInfo ag_block_info = { "AgBlock", sizeof(AgBlock), sizeof(AgBlockClass), (GtkClassInitFunc)ag_block_class_init, (GtkObjectInitFunc)ag_block_init, NULL, NULL, (GtkClassInitFunc)NULL }; ag_block_type = gtk_type_unique( gtk_widget_get_type(),&ag_block_info); } return(ag_block_type); } GtkWidget *ag_block_new() { return(GTK_WIDGET(gtk_type_new(ag_block_get_type()))); } static GtkWidgetClass *parent_class = NULL; static void ag_block_class_init(AgBlockClass *klass) { GtkWidgetClass *widget_class; GtkObjectClass *object_class; widget_class = (GtkWidgetClass *)klass; widget_class->realize = ag_block_realize; widget_class->draw = ag_block_draw; widget_class->event = ag_block_expose_event; widget_class->size_request = ag_block_size_request; widget_class->size_allocate = ag_block_size_allocate; object_class = (GtkObjectClass *)klass; object_class->destroy = ag_block_destroy; parent_class = gtk_type_class(gtk_widget_get_type()); } static void ag_block_init(AgBlock *agblock) { GdkColor *color = g_malloc(sizeof(GdkColor)); color->red = (((double)rand()*0xFFFF)/RAND_MAX); color->green = (((double)rand()*0xFFFF)/RAND_MAX); color->blue = (((double)rand()*0xFFFF)/RAND_MAX); agblock->color = color; } static void ag_block_realize(GtkWidget *widget) { AgBlock *agblock; GdkWindowAttr attributes; guint attributes_mask; g_return_if_fail(widget != NULL); g_return_if_fail(AG_IS_BLOCK(widget)); agblock = AG_BLOCK(widget); GTK_WIDGET_SET_FLAGS(widget,GTK_REALIZED); attributes.window_type = GDK_WINDOW_CHILD; attributes.x = widget->allocation.x; attributes.y = widget->allocation.y; attributes.width = widget->allocation.width; attributes.height = widget->allocation.height; attributes.wclass = GDK_INPUT_OUTPUT; attributes.visual = gtk_widget_get_visual(widget); attributes.colormap = gtk_widget_get_colormap(widget); attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK; attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; gdk_color_alloc(attributes.colormap,agblock->color); widget->window = gdk_window_new( gtk_widget_get_parent_window(widget), &attributes,attributes_mask); gdk_window_set_user_data(widget->window,agblock); widget->style = gtk_style_attach(widget->style, widget->window); gtk_style_set_background(widget->style, widget->window,GTK_STATE_NORMAL); ag_block_send_configure(AG_BLOCK(widget)); } static void ag_block_destroy(GtkObject *object) { AgBlock *agblock; g_return_if_fail(object != NULL); g_return_if_fail(AG_IS_BLOCK(object)); agblock = AG_BLOCK(object); g_free(agblock->color); if(GTK_OBJECT_CLASS(parent_class)->destroy) (* GTK_OBJECT_CLASS(parent_class)->destroy)(object); } static void ag_block_draw(GtkWidget *widget, GdkRectangle *area) { AgBlock *agblock; g_return_if_fail(widget != NULL); g_return_if_fail(AG_IS_BLOCK(widget)); agblock = AG_BLOCK(widget); gdk_window_set_background(widget->window, agblock->color); gdk_window_clear(widget->window); } static gint ag_block_expose_event(GtkWidget *widget, GdkEvent *event) { AgBlock *agblock; agblock = AG_BLOCK(widget); gdk_window_set_background(widget->window, agblock->color); gdk_window_clear(widget->window); return(TRUE); } static void ag_block_size_request(GtkWidget *widget, GtkRequisition *requisition) { g_return_if_fail(widget != NULL); g_return_if_fail(AG_IS_BLOCK(widget)); requisition->width = 80; requisition->height = 80; } static void ag_block_size_allocate(GtkWidget *widget, GtkAllocation *allocation) { g_return_if_fail(widget != NULL); g_return_if_fail(AG_IS_BLOCK(widget)); g_return_if_fail(allocation != NULL); widget->allocation.x = allocation->x; widget->allocation.y = allocation->y; widget->allocation.width = allocation->width; widget->allocation.height = allocation->height; if(GTK_WIDGET_REALIZED(widget)) { gdk_window_move_resize(widget->window, allocation->x,allocation->y, allocation->width,allocation->height); ag_block_send_configure(AG_BLOCK(widget)); } } static void ag_block_send_configure(AgBlock *agblock) { GtkWidget *widget; GdkEventConfigure event; widget = GTK_WIDGET(agblock); event.type = GDK_CONFIGURE; event.window = widget->window; event.x = widget->allocation.x; event.y = widget->allocation.y; event.width = widget->allocation.width; event.height = widget->allocation.height; gtk_widget_event(widget,(GdkEvent *)&event); }