[cairo] How to render 1bpp bitmaps normally & selected (in GTK+ 3)

Mark Leisher mleisher at gmail.com
Mon Jul 11 09:02:32 PDT 2011


It took a while to piece this together, so for future reference.

Is there a more elegant solution?

===================================================

/*
  * The 'data' parameter is a GtkDrawingArea widget.
  */

static gboolean invert = TRUE;
static cairo_surface_t *s;

/*
  * Function type just to simplify the code.
  */
typedef void (*get_color_func_t)(GtkStyleContext *,GtkStateFlags,GdkRGBA *);

static void
invert_bitmap(GtkWidget *w, gpointer data)
{
     cairo_t *cr = 
gdk_cairo_create(gtk_widget_get_window(GTK_WIDGET(data)));
     GtkStyleContext *ctx = gtk_widget_get_style_context(GTK_WIDGET(data));
     GdkRGBA c;
     get_color_func_t get_bg, get_fg;

     if (invert) {
         /*
          * Display inverted form: background of rectangle in foreground 
color
          * and bitmap in background color.
          */
         get_bg = gtk_style_context_get_color;
         get_fg = gtk_style_context_get_background_color;
     } else {
         /*
          * Show normal bitmap while clearing the background behind it.
          */
         get_fg = gtk_style_context_get_color;
         get_bg = gtk_style_context_get_background_color;
     }

     (*get_bg)(ctx,GTK_STATE_FLAG_NORMAL,&c);
     cairo_set_source_rgb(cr,c.red,c.green,c.blue);
     cairo_rectangle(cr, 1, 1, 13, 22);
     cairo_fill(cr);

     (*get_fg)(ctx,GTK_STATE_FLAG_NORMAL,&c);
     cairo_set_source_rgb(cr,c.red,c.green,c.blue);
     cairo_mask_surface(cr,s,2.0,1.0);

     cairo_destroy(cr);
     invert = !invert;
}

-- 
Mark Leisher


More information about the cairo mailing list