FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

An example of a GTK app?

Post new topic   Reply to topic     Forum Index -> Bindings
View previous topic :: View next topic  
Author Message

Joined: 09 Apr 2005
Posts: 16
Location: Ohio, USA

PostPosted: Sun May 01, 2005 6:58 pm    Post subject: An example of a GTK app? Reply with quote

Does anyone have one?
Back to top
View user's profile Send private message

Joined: 22 Feb 2004
Posts: 1104

PostPosted: Mon May 02, 2005 1:51 am    Post subject: Reply with quote

Not that I know of. DUI wraps the gtk bindings into an OOP style for D. That's the closest use of gtk I've seen in D. It's probably much easier to use than pure procedural gtk. For that matter, you could take a peek at that wrapper to see how it uses gtk -- not exactly an app, but it certainly uses gtk. MinWin also makes use of gtk for the Linux version (I think?).

If you want a pure gtk based app, it should be almost exactly the same as a C version (except for module imports and other minor D'isms). So your best bet would be to look at the C gtk samples. The trick here is in the linking: you'll need all those gtk dependency import libraries available; and they have to be link compatible with dmd/dmc. DUI, I believe done all the hard work in this regard already: I think these import libs are included in the DUI project source.

Somebody else noted that glade is a much easier tool to use to build the GUI's than programmed gtk code.

Back to top
View user's profile Send private message

Joined: 13 Dec 2004
Posts: 1

PostPosted: Wed May 25, 2005 11:00 am    Post subject: Reply with quote

Here's a D port of the GTK+ hello world example.

The only difference is that I only pass the button number to the callback.
Didn't want to mix C and D strings.

There are a few gotchas when porting from C to D (that I ran into):
* Callbacks (of course) have to be within extern(C) blocks
* There seems to be no way to get gtk_init to work like it should, just pass null.
* None of the FOO_BAR macros are defined. Normally you pass cast(FooBar *) widget instead.
* Few or none of the macros are defined. I first tried to use g_signal_connect, but found out it wasn't defined. I looked at the C header, and it is just a convenience wrapper for g_signal_connect_data. Just use g_signal_connect_data and add null, 0 to the function argument where you would normally use g_signal_connect.

I compiled it like this:
libs=$(for l in $(pkg-config --libs gtk+-2.0); do echo "-L$l"; done)
dmd 4_3_upgraded_helloworld.d $libs
import gtk;

extern(C) {
    /* Our new improved callback.  The data passed to this function
     * is printed to stdout. */
    private void callback(GtkWidget* widget, gpointer data)
        g_print("Hello again - button ?d was pressed\n", cast(int) data);

    /* another callback */
    private gboolean delete_event (GtkWidget* widget, GdkEvent* event, gpointer data)
        return false;

int main (char[][] args)
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box1;

    /* This is called in all GTK applications. Arguments are parsed
    * from the command line and are returned to the application. */
    gtk_init(null, null);

    /* Create a new window */
    window = gtk_window_new(GtkWindowType.GTK_WINDOW_TOPLEVEL);

    /* This is a new call, which just sets the title of our
    * new window to "Hello Buttons!" */
    gtk_window_set_title(cast(GtkWindow *) window, "Hello Buttons!");

    /* Here we just set a handler for delete_event that immediately
    * exits GTK. */
    g_signal_connect_data(cast(GObject *)window, "delete_event", cast(GCallback) &delete_event, null, null, 0);

    /* Sets the border width of the window. */
    gtk_container_set_border_width(cast(GtkContainer *)window, 10);

    /* We create a box to pack widgets into.  This is described in detail
    * in the "packing" section. The box is not really visible, it
    * is just used as a tool to arrange widgets. */
    box1 = gtk_hbox_new (false, 0);

    /* Put the box into the main window. */
    gtk_container_add(cast(GtkContainer *)window, box1);

    /* Creates a new button with the label "Button 1". */
    button = gtk_button_new_with_label("Button 1");

    /* Now when the button is clicked, we call the "callback" function
    * with a pointer to "button 1" as its argument */
    g_signal_connect_data(cast(GObject *) button, "clicked", cast(GCallback) &callback, cast(gpointer) 1, null, 0);

    /* Instead of gtk_container_add, we pack this button into the invisible
    * box, which has been packed into the window. */
    gtk_box_pack_start(cast(GtkBox *) box1, button, true, true, 0);

    /* Always remember this step, this tells GTK that our preparation for
    * this button is complete, and it can now be displayed. */

    /* Do these same steps again to create a second button */
    button = gtk_button_new_with_label("Button 2");

    /* Call the same callback function with a different argument,
    * passing a pointer to "button 2" instead. */
    g_signal_connect_data(cast(GObject *)button, "clicked", cast(GCallback) &callback, cast(gpointer) 2, null, 0);

    gtk_box_pack_start(cast(GtkBox *)box1, button, true, true, 0);

    /* The order in which we show the buttons is not really important, but I
    * recommend showing the window last, so it all pops up at once. */



    /* Rest in gtk_main and wait for the fun to begin! */

    return 0;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Bindings All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group