gtkD: Minimal Drawing Example?
Asked Answered
G

1

7

I'm a fairly experienced programmer, but new to GUI programming. I'm trying to port a plotting library I wrote for DFL to gtkD, and I can't get drawings to show up. The following code produces a blank window for me. Can someone please tell me what's wrong with it, and/or post minimal example code for getting a few lines onto a DrawingArea and displaying the results in a MainWindow?

import gtk.DrawingArea, gtk.Main, gtk.MainWindow, gdk.GC, gdk.Drawable,
    gdk.Color;

void main(string[] args) {
    Main.init(args);

    auto win = new MainWindow("Hello, world");
    win.setDefaultSize(800, 600);
    auto drawingArea = new DrawingArea(800, 600);
    win.add(drawingArea);
    drawingArea.realize();

    auto drawable = drawingArea.getWindow();
    auto gc = new GC(drawable);
    gc.setForeground(new Color(255, 0, 0));
    gc.setBackground(new Color(255, 255, 255));
    drawable.drawLine(gc, 0, 0, 100, 100);

    drawingArea.showAll();
    drawingArea.queueDraw();
    win.showAll();

    Main.run();
}
Gamesmanship answered 17/7, 2010 at 5:12 Comment(1)
I think you're missing the PixMap buffer stuff. This is for PyGTK but I think it might help: pygtk.org/pygtk2tutorial/sec-DrawingAreaWidgetAndDrawing.htmlBixby
K
4

I have no experience whatsoever in D, but lots in GTK, so with the help of the gtkD tutorial I managed to hack up a minimal example:

import gtk.DrawingArea, gtk.Main, gtk.MainWindow, gdk.GC, gdk.Drawable,
    gdk.Color, gtk.Widget;

class DrawingTest : MainWindow
{
    this()
    {
        super("Hello, world");
        setDefaultSize(800, 600);
        auto drawingArea = new DrawingArea(800, 600);
        add(drawingArea);
        drawingArea.addOnExpose(&drawStuff);
        showAll();
    }

    bool drawStuff(GdkEventExpose *event, Widget self) 
    {
        auto drawable = self.getWindow();
        auto gc = new GC(drawable);
        gc.setForeground(new Color(cast(ubyte)255, cast(ubyte)0, cast(ubyte)0));
        gc.setBackground(new Color(cast(ubyte)255, cast(ubyte)255, cast(ubyte)255));
        drawable.drawLine(gc, 0, 0, 100, 100);
        return true;
    }
}

void main(string[] args) {
    Main.init(args);
    new DrawingTest();
    Main.run();
}

In GTK, a DrawingArea is actually just a blank widget for you to paint on, and painting on widgets must always be done in the expose-event handler. (Although I understand this will change in GTK 3!)

I understand you can't connect functions as signal callbacks, only delegates, so that's the reason for the DrawingTest class.

Korwun answered 16/10, 2010 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.