[Gtk-sharp-list] Mono-cairo and Gtk# question regarding expose event

Martin Schanzenbach mschanzenbach at gmail.com
Wed Sep 5 13:45:59 EDT 2007


Hi,

A few days ago I decided to learn mono and gtk#. I wanted to write a
simple widget that looks like libnotify widgets just bigger. But when
I tried to add widgets like a button to the window I couldn't see
anything but the white background and the colored stripe I have drawn
on the window using the expose event.
I'm going to cut things short and show you the code:

using System;
using Gtk;
using Cairo;

public class MainWindow : Gtk.Window
{	
	public MainWindow (): base (Gtk.WindowType.Popup)
	{
                this.SetSizeRequest(400, 400);
	        this.Show();
		this.AppPaintable = true;
                Gtk.Button b = new Gtk.Button("test");
                b.SetSizeRequest(20,20);
		this.Add(b);
                this.ExposeEvent += Expose;
                this.ShowAll();
        }

        public void Expose(object o, Gtk.ExposeEventArgs args)
        {
                Cairo.Context cr = Gdk.CairoHelper.Create(this.GdkWindow);
		cr.Operator = Cairo.Operator.Source;
		Cairo.Surface surface =
cr.Target.CreateSimilar(Cairo.Content.ColorAlpha, 400, 400);
		
		Cairo.Context crs = new Cairo.Context(surface);

                //FillBackground
		Gtk.Style stl = this.Style;
		Gdk.Color background = stl.Base(Gtk.StateType.Normal);
		
		crs.Color = new Cairo.Color( background.Red / 65535.0,
background.Green / 65535.0, background.Blue / 65535.0);
		crs.Rectangle( 0, 0, 400, 400);
		crs.Fill();
		
		crs.Color = new Cairo.Color(0.0,0.0,0.0);
		crs.LineWidth = 1.0;
		crs.Rectangle(0.5,0.5,400-0.5,400-0.5);
		crs.Stroke();
		
		//draw stripe
		double strx = 1.5;
		double stry = 1.5;
		
		crs.Color = new Cairo.Color(0.3,0.3,0.3);
		crs.Rectangle(strx,stry,40, 397);
		crs.Fill();
		
		cr.SetSourceSurface(surface,0,0);
		cr.PaintWithAlpha(0.8);
        }
}

class MainClass
{
	public static void Main (string[] args)
	{
		Application.Init ();
		MainWindow win = new MainWindow ();
		Application.Run ();
	}
}


That's the important stuff. Now I checked the theme.c from
notification-daemon (galago project) and the c code looks roughly the
same with the exception that the widgets obviosly DON'T get painted
over when the expose event is triggered. As you can see I used the
paint method PaintWithAlpha so that it is possible to actually see the
widgets below.
My question finally is: Why does mono behave differently here than the
c code? After all notification-daemon also repaints at every expose
event but no widget is ever hidden.
Any help would be greatly appreciated.

Martin Schanzenbach


More information about the Gtk-sharp-list mailing list