Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Gdk# library in C# (mono)?

I'm a complete newbie with Gtk# and Gdk# and I'm completely lost as to how to get started.

All I'm trying to do is to draw points and lines in whatever widget/pixmap/image and then display them in my gtk application.
So far, I understand I must create a Gdk.drawable object to access the DrawPoints(Point[] points) and DrawLine(Point[] points) methods. However, to instantiate a Drawable object I need a Gdk.GC object. Both object constructors take an IntPtr parameter, and I don't know what IntPtr I should pass here?! Or, a GC constructor could also take a Drawable object, and a Drawable constructor could take a GC object...I'm turning in circle here!

It's been 24h of online research and apart from some python examples that use constructors that take no parameter, I couldn't find any resource to get started in C#.

So, could anyone show me how to correctly use these GC and Drawable objects to draw a line using the DrawLine(Point[] points) method?

GDKEXAMPLE(){
    win = new Gtk.Window ("Gdk nightmare");
    win.SetDefaultSize (400, 300);
    img=new Gtk.Image();
    Drawable dr=new Drawable(null); //how to instantiate this object?
    Gdk.GC gc=new Gdk.GC(null); //how to instantiate this object?
    Point[] pts=new Point[3];
    pts[0]=new Point(10,10);
    pts[1]=new Point(15,20);
    pts[2]=new Point(20,50);

    dr.DrawLines(gc,pts);
    Pixmap pxmp=new Pixmap(dr,100,100);
    img.SetFromPixmap(pxmp,pxmp); //Requests a pixmap and pixmap mask: what mask?
    img.QueueDraw();

    win.Add(img);
    win.ShowAll();
}

Could anyone help me to use the Gdk.GC and Gdk.Drawable class and then display any points or lines in a Gtk widget please? maybe by making the above code to work? or any link to some tutorial in C# using this gdk library?

like image 973
djahma Avatar asked Feb 02 '12 13:02

djahma


1 Answers

So, after extensive patience I eventually found the answer to how getting started with Gdk#.

Basically, only events can trigger a drawing. I tried to call drawings directly from methods but it didn't work. Here is the basic code I was missing in C#:

using System;
using Gdk;
using Gtk;

class GdkApp : Gtk.Window 
{
    public static void Main()
    {
        Application.Init();
        new GdkApp();
        Application.Run();
    }

    public GdkApp() : base("Simple drawing")
    {
        SetDefaultSize(230, 150);
        DeleteEvent+=delegate {Application.Quit(); };
        ShowAll();
    }


    protected override bool OnExposeEvent (EventExpose evnt)
    {
        bool ok = base.OnExposeEvent (evnt);
        draw ();
        return ok;
    }

    void draw()
    {
        Gdk.GC gc = new Gdk.GC ((Drawable)base.GdkWindow);
        gc.RgbFgColor = new Gdk.Color (255, 50, 50);
        gc.RgbBgColor = new Gdk.Color (0, 0, 0);
        gc.SetLineAttributes (3, LineStyle.OnOffDash, 
                              CapStyle.Projecting, JoinStyle.Round);
        Gdk.Point[] pts = new Gdk.Point[8];
        pts [0] = new Gdk.Point (10, 50);
        pts [1] = new Gdk.Point (15, 70);
        pts [2] = new Gdk.Point (20, 80);
        pts [3] = new Gdk.Point (25, 70);
        pts [4] = new Gdk.Point (30, 80);
        pts [5] = new Gdk.Point (40, 90);
        pts [6] = new Gdk.Point (55, 85);
        pts [7] = new Gdk.Point (75, 65);
        base.GdkWindow.DrawLines (gc, pts);
    }
}

Here, we build the main window and write all the drawing in the OnExposeEvent method. This method gets called when any widget needs to be (fully or partially) redrawn. In there, you need to instantiate the Gdk.GC object (context) with a "Drawable" parameter. There isn't much choice: Gdk.Pixmap or Gdk.Window. I picked the Gdk.Window as it was the most straightforward surface to draw on. Then you can customize the context object to decide how it will be drawn.

Finally, and directly from the Drawable object you want to draw on, you can call the drawing methods: here I just wanted to plot a series of 8 connected points.

I hope this can help some other users trying to understand the logic of this gdk# namespace.

like image 157
djahma Avatar answered Sep 23 '22 22:09

djahma