[Gtk-sharp-list] Widget Background Color 'anomaly', and request for further reading

Michael Hutchinson m.j.hutchinson at gmail.com
Fri Jan 23 18:15:58 EST 2009

On Thu, Jan 22, 2009 at 10:20 AM, Hywel Thomas
<hywel at controlspecials.demon.co.uk> wrote:
> Hello,
> I'm struggling to understand just which Background color Widgets are
> drawn with, and how to get the current Background correctly.  In the
> following example (taken roughly from the Gtk Example on the Mono site)
> I need to save the current Background color of the Drawing Area, use a
> specific value (black), then restore the original, eg
> ...(unnecessary code removed)....
> ColourExample()
>     {
>         win = new Window ("Background Colour Example");
>         win.SetDefaultSize (400, 300);
>         win.BorderWidth = 24;  // to show parent's color clearly
>         da = new DrawingArea();
>         da.ExposeEvent += OnExposed;
>         win.Add (da);
>         Gdk.Color bg = da.Style.Backgrounds[(int)StateType.Normal]; // A
>        da.ModifyBg (StateType.Normal, new Gdk.Color(0,0,0));   // B
>        //da.ModifyBg(StateType.Normal, bg);    //  C
>        //win.ModifyBg(StateType.Normal, bg);   //  D
>        win.ShowAll ();
>     }
>     void OnExposed (object o, ExposeEventArgs args) {
>         da.GdkWindow.DrawLine(da.Style.BaseGC(StateType.Normal),
> 0, 0,                   352, 252);
>     }
> At line A I read the current Background color (or so I thought...), at
> line B set up the new one, and let the ExposeEvent draw a line.This
> works, with the set background color.  However, if I uncomment line C,
> so that the original background is restored before drawing the line, I
> see that the background is NOT the same as the parent's background. Line
> A is obviously not the correct call to read the correct background, but
> the documentation appears to suggest that this is the way.  If I also
> uncomment line D, so that the parent's background is set to the same
> value bg, both appear the same, but it is not the color that the window
> and widget are originally drawn with. I can also get it to work if I use
> a call to set the default background ( da.ModifyBg(StateType.Normal) ),
> but I'd really like to be able to read this somehow.
> The other 'obvious' property for the widget Background is write-only
> (why?) so this also won't read back the current value!
> I'd be grateful for any enlightenment, or references that I can follow
> up, as I seem to have exhausted most avenues so far.

I took a look in the GTK+ docs, and I concur with your reading of the
Backgrounds array. However, from poking around in the GTK source, it
seems that widgets have a "modifier style" that has flags to indicate
which colours should override the base style. Unless you're using
these flags correctly, things may not work as you intend. The Modify*
methods handle correctly setting and unsetting all this for you, but I
don't see an obvious supported way to *read* the colour.


For advanced details like this, you'll probably have more luck on the
GTK+ lists.

Michael Hutchinson

More information about the Gtk-sharp-list mailing list