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

Maarten Bosmans mkbosmans at gmail.com
Tue Jul 12 01:47:17 PDT 2011

2011/7/11 Mark Leisher <mleisher at gmail.com>:
> On 07/11/2011 12:40 PM, Maarten Bosmans wrote:
>> 2011/7/11 Mark Leisher<mleisher at gmail.com>:
>>> It took a while to piece this together, so for future reference.
>>> Is there a more elegant solution?
>>  From the subject it seems that you want the bitmap to draw inverted
>> when the widget is selected. Why don't you just give the right
>> GtkStateFlags to gtk_style_context_get_color? That would take care of
>> the right forground/background colors for normal/selected and gives a
>> result more consistent with the current theme.
> What I'm working on is porting
> http://www.math.nmsu.edu/~mleisher/Software/gbdfed to Gtk+ 3. I need to be
> able to select/deselect individual glyph bitmaps in discrete rectangular
> areas. Sort of like a spreadsheet for glyph bitmaps. Selected is not really
> a state that applies to this custom widget as a whole.

Ah, right. That provides a useful bit of context.

I still think you should use the correct gtk style instead of just
inverting. Something like:

   if (invert)
       state = GTK_STATE_FLAG_NORMAL;

   cairo_rectangle(cr, 1, 1, 13, 22);


> In response to your reply on gtk-list:
> 1. You mention the XOR operator. I never could get it to work (OpenSuSE 11.4
> 32-bit/Gtk+ 3.0.10/cairo 1.10.2) and never managed to dig up any examples
> that did. The DIFFERENCE operator worked exactly as advertised, but it
> wasn't quite what I was looking for. Before I dig into the cairo sources,
> any pointers to XOR examples that work?

No, never used it, so I'm not sure if/how it works. It's a bit of an
old school operation that's not that useful most of the times anyway.

> 2. The rectangle geometry and bitmap offsets are just chosen as an example
> "cell" in the "spreadsheet."

Rather than drawing the background rectangle with cairo, consider
using gtk's paint functions like gtk_render_background, which give a
nice themed selection background.

> Mark Leisher


More information about the cairo mailing list