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

1

7

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?

Argeliaargent answered 2/2, 2012 at 13:22 Comment(6)
Where does the compile error occur? Try to include only code relevant to your question. (A question such as "how to instantiate this Gdk object using Gdk#?". The question "someone explain Gdk# to me" will likely get closed.)Panic
Also, searching for "mono drawing tutorial" yields this: mono-project.com/Mono.CairoPanic
ok, I've rephrased my initial question, hope that's clearer now. About Cairo, that's not Gdk...what's the point of this Gdk# library if no one understands it? and why aren't there any online resource to understand it apart from their incomplete reference manual?Argeliaargent
The expectation for bindings is often that you'll understand the original version of the library (in this case, the C version of Gtk and Gdk and sundry), use the documentation for that library, and translate the various idioms between languages. (E.g. that Gtk signals will be C# events etc.)Panic
Also, if you'd read that article, you might learn that: 1. Cairo can be used for drawing in Gtk#. (In fact I believe it's the primary 2D graphics API for the Gtk ecosystem. GDK is deprecated, and its drawing functions are in fact gone in Gtk3.) 2. The second code sample includes the expression area.GdkWindow. The documentation for Gdk.Window says it inherits from Gdk.Drawable, which seems to be what you're looking for and what you can use to construct a Gdk.GC if you really want Gdk.Panic
(I'll agree that trawling like documentation this is neither intuitive or pleasant, but unfortunately it's necessary. If you're not comfortable with that you might be better served using the .NET framework or Cocoa instead of open-source libraries, both of which have extensive official documentation with step-by-step guides and samples for absolutely everything provided by the vendor.)Panic
A
6

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.

Argeliaargent answered 6/2, 2012 at 1:36 Comment(1)
I've been looking for details on how to create a GC for YEARS! I've been trashing default GCs since then. Mono docs tend to focus on basic and hyper-advanced stuf. Thank you very much!Nightgown

© 2022 - 2024 — McMap. All rights reserved.