Use cairo for drawing
authorPhilip Chimento <philip.chimento@gmail.com>
Sat, 25 Aug 2012 20:46:36 +0000 (22:46 +0200)
committerPhilip Chimento <philip.chimento@gmail.com>
Sat, 25 Aug 2012 20:46:36 +0000 (22:46 +0200)
GDK drawing does not exist in GTK 3 anymore. We now use cairo for
drawing. This means that the graphics window now corresponds to a
GtkDrawingArea, and we use a cairo surface as a backing store.

libchimara/graphics.c
libchimara/graphics.h
libchimara/window.c
libchimara/window.h

index 1ab5a359b68ba304bf3d0a1c5ec90d357bc13ff0..543ad43563b147a6ecd5ed0f3e0e9cdbe3f443d0 100644 (file)
@@ -386,24 +386,19 @@ glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui3
        return draw_image_common(win, info->pixbuf, val1, val2);
 }
 
-/* Internal function: draws a pixbuf to a graphics window of text buffer */
+/* Internal function: draws a pixbuf to a graphics window or text buffer */
 glui32
 draw_image_common(winid_t win, GdkPixbuf *pixbuf, glsi32 val1, glsi32 val2)
 {
        switch(win->type) {
        case wintype_Graphics:
        {
-               GdkPixmap *canvas;
-
                gdk_threads_enter();
 
-               gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
-               if(canvas == NULL) {
-                       WARNING("Could not get pixmap");
-                       return FALSE;
-               }
-
-               gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
+               cairo_t *cr = cairo_create(win->backing_store);
+               gdk_cairo_set_source_pixbuf(cr, pixbuf, val1, val2);
+               cairo_paint(cr);
+               cairo_destroy(cr);
 
                /* Update the screen */
                gtk_widget_queue_draw(win->widget);
@@ -483,6 +478,16 @@ glk_window_set_background_color(winid_t win, glui32 color)
        win->background_color = color;
 }
 
+static void
+glkcairo_set_source_glkcolor(cairo_t *cr, glui32 val)
+{
+       double r, g, b;
+       r = ((val & 0xff0000) >> 16) / 256.0;
+       g = ((val & 0x00ff00) >> 8) / 256.0;
+       b = (val & 0x0000ff) / 256.0;
+       cairo_set_source_rgb(cr, r, g, b);
+}
+
 /**
  * glk_window_fill_rect:
  * @win: A graphics window.
@@ -504,16 +509,12 @@ glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32
 
        gdk_threads_enter();
 
-       GdkPixmap *map;
-       gtk_image_get_pixmap( GTK_IMAGE(win->widget), &map, NULL );
-
-       GdkGC *gc = gdk_gc_new(map);
-       GdkColor gdkcolor;
-       glkcolor_to_gdkcolor(color, &gdkcolor);
-       gdk_gc_set_rgb_fg_color(gc, &gdkcolor);
-       gdk_draw_rectangle( GDK_DRAWABLE(map), gc, TRUE, left, top, width, height);
+       cairo_t *cr = cairo_create(win->backing_store);
+       glkcairo_set_source_glkcolor(cr, color);
+       cairo_rectangle(cr, (double)left, (double)top, (double)width, (double)height);
+       cairo_fill(cr);
        gtk_widget_queue_draw(win->widget);
-       g_object_unref(gc);
+       cairo_destroy(cr);
 
        gdk_threads_leave();
 }
@@ -579,37 +580,54 @@ void glk_window_flow_break(winid_t win)
        VALID_WINDOW(win, return);
 }
 
-/*** 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)
-{ 
-       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 */
+/* Called when the graphics window is resized, restacked, or moved. Resize the
+backing store if necessary. */
+gboolean
+on_graphics_configure(GtkWidget *widget, GdkEventConfigure *event, winid_t win)
+{
+       int oldwidth = 0, oldheight = 0;
+
+       /* Determine whether the backing store can stay the same size */
        gboolean needs_resize = FALSE;
-       if(oldmap == NULL)
+       if(win->backing_store == NULL)
                needs_resize = TRUE;
        else {
-               gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
-               if(oldwidth != allocation->width || oldheight != allocation->height)
+               oldwidth = cairo_image_surface_get_width(win->backing_store);
+               oldheight = cairo_image_surface_get_height(win->backing_store);
+               if(oldwidth != event->width || oldheight != event->height)
                        needs_resize = TRUE;
        }
 
        if(needs_resize) {
-               /* 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);
+               /* Create a new backing store */
+               cairo_surface_t *new_backing_store = gdk_window_create_similar_surface( gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, gtk_widget_get_allocated_width(widget), gtk_widget_get_allocated_height(widget) );
+               cairo_t *cr = cairo_create(new_backing_store);
+
+               /* Clear to background color */
+               glkcairo_set_source_glkcolor(cr, win->background_color);
+               cairo_paint(cr);
+
+               if(win->backing_store != NULL) {
+                       /* Copy the contents of the old backing store */
+                       cairo_set_source_surface(cr, win->backing_store, 0, 0);
+                       cairo_paint(cr);
+                       cairo_surface_destroy(win->backing_store);
+               }
+
+               cairo_destroy(cr);
+               /* Use the new backing store */
+               win->backing_store = new_backing_store;
        }
+
+       return TRUE; /* Event handled, stop processing */
 }
 
+/* Draw the backing store to the screen. Called whenever the drawing area is
+exposed. */
+gboolean
+on_graphics_draw(GtkWidget *widget, cairo_t *cr, winid_t win)
+{
+       cairo_set_source_surface(cr, win->backing_store, 0, 0);
+       cairo_paint(cr);
+       return FALSE;
+}
index fe39694c3591205161e38d150168d7bf0a1c395d..5ca8c609e72b93b182e07877f9779776417b809c 100644 (file)
@@ -21,7 +21,8 @@ struct image_info {
        gboolean scaled;
 };
 
-void on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win);
+gboolean on_graphics_configure(GtkWidget *widget, GdkEventConfigure *event, winid_t win);
+gboolean on_graphics_draw(GtkWidget *widget, cairo_t *cr, winid_t win);
 void clear_image_cache(struct image_info *data, gpointer user_data);
 
 #endif
index 7bef6d0b2fd5bbc922f615cd6352bc65ec7fe429..85348f54502ce02de90a88b2406eb363b547cc5e 100644 (file)
@@ -76,6 +76,8 @@ window_close_common(winid_t win, gboolean destroy_node)
 
        if(win->pager_layout)
                g_object_unref(win->pager_layout);
+       if(win->backing_store)
+               cairo_surface_destroy(win->backing_store);
 
        g_free(win);
 }
@@ -603,7 +605,7 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
 
                case wintype_Graphics:
                {
-                   GtkWidget *image = gtk_image_new_from_pixmap(NULL, NULL);
+                   GtkWidget *image = gtk_drawing_area_new();
                        gtk_widget_show(image);
 
                        win->unit_width = 1;
@@ -611,13 +613,15 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                    win->widget = image;
                    win->frame = image;
                        win->background_color = 0x00FFFFFF;
-                               
+                       win->backing_store = NULL;
+
                        /* Connect signal handlers */
                        win->button_press_event_handler = g_signal_connect(image, "button-press-event", G_CALLBACK(on_window_button_press), win);
                        g_signal_handler_block(image, win->button_press_event_handler);
                        win->shutdown_keypress_handler = g_signal_connect(image, "key-press-event", G_CALLBACK(on_shutdown_key_press_event), win);
                        g_signal_handler_block(image, win->shutdown_keypress_handler);                  
-                       win->size_allocate_handler = g_signal_connect(image, "size-allocate", G_CALLBACK(on_graphics_size_allocate), win);
+                       g_signal_connect(image, "configure-event", G_CALLBACK(on_graphics_configure), win);
+                       g_signal_connect(image, "draw", G_CALLBACK(on_graphics_draw), win);
                }
                    break;
                        
index c67654a15b3f6a3fcbaad2442de719412a9272c8..72ee91b529c3e51ebbab3b756373c1c1fbaa678e 100644 (file)
@@ -95,6 +95,7 @@ struct glk_window_struct
        gboolean hyperlink_event_requested;
        /* Graphics */
        glui32 background_color;
+       cairo_surface_t *backing_store;
        /* Pager (textbuffer only) */
        gboolean currently_paging;
        PangoLayout *pager_layout;