Added correct resize handling
authorrodin <rodin@ddfedd41-794f-dd11-ae45-00112f111e67>
Fri, 29 Jan 2010 14:05:57 +0000 (14:05 +0000)
committerrodin <rodin@ddfedd41-794f-dd11-ae45-00112f111e67>
Fri, 29 Jan 2010 14:05:57 +0000 (14:05 +0000)
libchimara/graphics.c
libchimara/graphics.h
libchimara/window.c

index 48b3a12dfd4d18550e61ca54855dd1838b9b16e0..f94b90c8616fa81699ad42aefd3e5b0e5e676b06 100644 (file)
@@ -62,7 +62,7 @@ glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
 
        /* Determine the image dimensions */
        if(!size_determined) {
-               WARNING("Cannot read image size: file data trimmed?");
+               WARNING("Cannot read image size");
                g_free(info);
                return FALSE;
        }
@@ -73,8 +73,6 @@ glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
                *height =info->height;
        g_free(info);
 
-       //printf("size loaded: %d x %d\n", (int) *width, (int) *height);
-
        return TRUE;
 }
 
@@ -86,10 +84,40 @@ on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_
        size_determined = TRUE;
 }
 
+/*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/
 void
-on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t *win)
+on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win)
 {
-       printf("size allocated: %dx%d\n", allocation->width, allocation->height);
+       printf("allocate to: %dx%d\n", allocation->width, allocation->height);
+       GdkPixmap *oldmap;
+       gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL );
+       gint oldwidth = 0;
+       gint oldheight = 0;
+       /* Determine whether a pixmap exists with the correct size */
+       gboolean needs_resize = FALSE;
+       if(oldmap == NULL)
+               needs_resize = TRUE;
+       else {
+               gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
+               if(oldwidth != allocation->width || oldheight != allocation->height)
+                       needs_resize = TRUE;
+       }
+
+       if(needs_resize) {
+               printf("needs resize\n");
+               /* Create a new pixmap */
+               GdkPixmap *newmap = gdk_pixmap_new(widget->window, allocation->width, allocation->height, -1);
+               gdk_draw_rectangle( GDK_DRAWABLE(newmap), widget->style->white_gc, TRUE, 0, 0, allocation->width, allocation->height);
+
+               /* Copy the contents of the old pixmap */
+               if(oldmap != NULL)
+                       gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight);
+               
+               /* Use the new pixmap */
+               gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL );
+               g_object_unref(newmap);
+       }
 }
 
 glui32
@@ -107,8 +135,6 @@ glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) {
        guchar *buffer;
        GdkPixmap *canvas;
 
-       //printf("glk_image_get_info(%d)\n", image);
-
        /* Lookup the proper resource */
        blorb_error = giblorb_load_resource(glk_data->resource_map, giblorb_method_FilePos, &res, giblorb_ID_Pict, image);
        if(blorb_error != giblorb_err_None) {
@@ -135,6 +161,7 @@ glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) {
 
                total_read += num_read;
        }
+       printf("Loading done\n");
        giblorb_unload_chunk(glk_data->resource_map, image);
        g_free(buffer);
 
@@ -152,11 +179,13 @@ glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) {
                return FALSE;
        }
 
+       // TODO: FIX hang?
        gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
        gdk_pixbuf_loader_close(loader, &pixbuf_error);
 
        /* Update the screen */
        gtk_widget_queue_draw(win->widget);
+
        return TRUE;
 }
 
index cf81d59dd141e143b7bb87f319508fe4b0e13d37..34cde77db454609e5c49b4435e8251fd730b192e 100644 (file)
@@ -14,6 +14,6 @@ struct image_info {
        gint width, height;
 };
 
-void on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t *win);
+void on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win);
 
 #endif
index 209cda689a57e9cb5bd6515ecf723c11f798078d..02f1e5ceb0e58d47ee8dca4abbf96d4e1e5f4cde 100644 (file)
@@ -561,16 +561,13 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
 
                case wintype_Graphics:
                {
-                       // TODO: Find real size
-                       GdkPixmap *newmap = gdk_pixmap_new(NULL, 800, 600, 24);
-                   GtkWidget *image = gtk_image_new_from_pixmap(newmap, NULL);
-                       g_object_unref(newmap);
-
+                   GtkWidget *image = gtk_image_new_from_pixmap(NULL, NULL);
                        gtk_widget_show(image);
 
+                       win->unit_width = 1;
+                       win->unit_height = 1;
                    win->widget = image;
                    win->frame = image;
-
                                
                        /* Connect signal handlers */
                        win->button_press_event_handler = g_signal_connect(image, "button-press-event", G_CALLBACK(on_window_button_press), win);