Is anyone running gtk4 is D?

ryuukk_ ryuukk.dev at gmail.com
Thu Jan 18 15:02:39 UTC 2024


On Thursday, 18 January 2024 at 09:11:07 UTC, aberba wrote:
> I'm building a UI component library on top of gtk3 to aid and 
> building good looking UI faster with few lines of code. 
> Currently using Gtk-d (gtk3)
>
> I know there's a gtk4 branch somewhere but doesn't seem to have 
> gained much improvement a while now. I would much rather prefer 
> going with the newest version of gtk, v4, rather than the soon 
> to be phased out, v3
>
> Is anyone able to use gtk4 in D?



GTK4 is still a C library, it's very easy to use from D

Here is ported their example from here 
https://docs.gtk.org/gtk4/getting_started.html


![screen](https://i.imgur.com/Y39rtbw.png)

```d

extern(C) void activate (GtkApplication* app, gpointer user_data)
{
   GtkWidget *window = gtk_application_window_new (app);
   gtk_window_set_title (window, "Hello Gtk4 from D!");
   gtk_window_set_default_size (window, 640, 480);
   gtk_window_present (window);
}

int main ()
{
   GtkApplication *app = gtk_application_new ("org.gtk.example", 
/*G_APPLICATION_DEFAULT_FLAGS*/ 0);
   g_signal_connect (app, "activate", cast(void*) &activate, 0);
   int status = g_application_run(app, 0, null);
   g_object_unref (app);

   return status;
}


// gtk4 binding, put that in a separate module as complete as you 
go
pragma(lib, "gtk-4");
pragma(lib, "gio-2.0");
pragma(lib, "gobject-2.0");
pragma(lib, "glib-2.0");
pragma(lib, "gthread-2.0");

alias GtkWidget = void;
alias GtkWindow = void;
alias GtkApplication = void;
alias gpointer = void*;

extern(C) GtkWidget* gtk_application_window_new(GtkApplication*);
extern(C) void gtk_window_set_title(GtkWindow*, const(char)*);
extern(C) void gtk_window_set_default_size(GtkWindow*, int, int);
extern(C) void gtk_window_present(GtkWindow*);
extern(C) GtkApplication* gtk_application_new(const(char)*, int);


// macro
extern(C) void g_signal_connect(GtkApplication* instance, 
const(char)* detailed_signal, void* c_handler, int data)
{
     g_signal_connect_data(instance, detailed_signal, c_handler, 
data, 0, 0);
}
extern(C) void g_signal_connect_data (GtkApplication*, 
const(char)*, void*, int, int, int);


extern(C) void g_object_unref(GtkApplication*);
extern(C) int g_application_run(GtkApplication*, int, void*);

```


Doing it this way, you can reuse their official documentation, 
examples, and help, complete the definition from the header as 
you go, and that's it

No need something complicated

Unless you want a OOP wrapper, in that case, all you have to do 
is wrap these calls in your classes, just like what gtkd is doing




More information about the Digitalmars-d mailing list