Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to write GTK-applications?

Tags:

c

gtk

gtk3

I recently started learning how to create GUI-applications with GTK+(3) by following the tutorials on the gnome website (first link down below).

So, I started going through the first couple of tutorials and everything was going great. Here is some of the early code I wrote:

#include <gtk/gtk.h>

static void activate (GtkApplication* app, gpointer user_data){
  GtkWidget *window;
  GtkWidget *button_box;
  GtkWidget *button;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "First application");
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);

  button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add(GTK_CONTAINER(window), button_box);

  button = gtk_button_new_with_label("Click this!");
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(g_print), "Hello, world!\n");
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
  gtk_container_add(GTK_CONTAINER(button_box), button);

  gtk_widget_show_all (window);
}

int main(int argc, char *argv[]){
  GtkApplication *app;
  int status;

  app = gtk_application_new ("com.example.application", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Everything worked exactly like I wanted it to. Then, they introduced the Gtk Builder, and I thought it was really neat, so I then used that to create an application.

However, suddenly, they started writing code that looked nothing like anything they'd written ever before(second link down below). So, I got quite confused and decided to look up some code from other sources to see how they wrote their GTK-applications. I saw them using things like gtk_init(), gtk_main() and gtk_window_new(GTK_WINDOW_TOPLEVEL). It did not at all look like the code I previously posted.

So, as I currently understand it, there are multiple ways of writing GTK-applications, using either GtkApplication or gtk_init(). Is one of them outdated and no longer preferred? If so, which one is preferred or should be preferred?

A few links to the two ways I found in the official Gtk tutorial:

  • Gtk: Getting started.
  • Gtk: Building applications.
like image 401
Zyphicx Avatar asked Jan 27 '17 10:01

Zyphicx


1 Answers

Using GtkApplication is now the preferred way for new applications. Older code calls gtk_init and gtk_main manually.

gtk_init only initializes GTK+ and handles some GTK+-specific command line arguments. You have to do pretty much everything else by yourself, even adding code to quit GTK+ when the user tries to close the application.

On the other hand GtkApplication calls gtk_init for you, and provides a way to manage stuff needed by many applications, like instance uniqueness, inhibition handling, menu bar management, etc. Of course you could write that by yourself, but it would take you some time to write and debug it, while by using GtkApplication you just get these features for free.

People also often get confused on how to interface their code with an event loop system, like the GLib's main loop. Using GtkApplication forces you to immediately use the event loop and understand what it does to get stuff done. This way all the code you write is run in reaction to an event. Compare this to calling gtk_init manually, and having some code in your main and the rest in event handlers.

like image 77
liberforce Avatar answered Nov 01 '22 10:11

liberforce