8

I am now learning to use GTK+3.0 with C in Linux. After reading some tutorials and sample code, I have some questions regarding how to initialize an application.

Here are two versions of code I have seen.

#include <gtk/gtk.h>

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

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show_all (window);
}

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

  app = gtk_application_new ("org.gtk.example", 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;
}

This code used gtk_application_new() to init a GtkApplication and g_application_run() to start it.

This is the second one.

#include <gtk/gtk.h>

int main(int argc,char *argv[])
{
  GtkWidget *window;
  gtk_init(&argc,&argv);

  window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),"helloworld");
  gtk_widget_show(window);
  gtk_main();

  return 0;
}

This code used gtk_init() to init the application and gtk_main() to run it.

However, I can't figure out the difference between them as the running result seems the same.

ptomato
  • 56,175
  • 13
  • 112
  • 165
zhshr
  • 95
  • 1
  • 8
  • I see a huge difference. I'm running `Linux commandos 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 GNU/Linux` on a Macbook Air 5,2. The first example loads instantly, where the second example loads after about 30 seconds. – Deanie Feb 19 '22 at 23:32

3 Answers3

4

The gtk_init() function initializes internal variables used by the library, the g_application_new() calls gtk_init() internally, so there is no difference or similarity, they serve different purposes, it's simply that one of them, includes the other one.

I don't know this from reading the documentation or anything similar, it's just a logical conclusion.

Probably, GtkApplication was created to avoid using global variables inside the Gtk+ library, and instead of that now you can use a GtkApplication to hold the application wide variables in it.

So it seems like the correct way to do it, I personally like it, but it's been a while since I wrote a Gtk+ application, and it was with the version 2, so I don't know much about it.

Gtk+ has a great feature and it's that it's very well documented, just google for GtkApplication and you will understand better what it is for, and how it should be used.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I noticed that if I use the second method I don't get a variable of GtkApplication. Does it matter to further coding? – zhshr Jun 26 '15 at 21:52
  • And also you mentioned that they serve different purposes. In this case if I want to code an application, which one should I use? – zhshr Jun 26 '15 at 21:53
  • It depends on what you are trying to build, the `GtkApplication` might or not be useful, but I think it's a new approach in Gtk+3 and it was not present in the previous version, it's a more OO approach as the Glib uses a OO approach. So I believe you should go with `GtkApplication` to prevent future compatibility problems. – Iharob Al Asimi Jun 26 '15 at 21:54
  • `GtkApplication` and its base `GApplication` do far more than just avoid global variables... – underscore_d Dec 18 '18 at 21:51
  • @underscore_d Sure, but `gtk_init()` would create an application global state that I presume are static globals that have many drawbacks. I wish there was a similar `CURL_APP` too. I myself always create a global context object that would be passed across functions to do many things, and avoid having a global state, cause you know, a context like that allocated on the heap is not really global. – Iharob Al Asimi Dec 21 '18 at 01:16
3

Take a look at the GtkApplication documentation.

Class GtkApplication handles initialization, lifetime of the application, main windows, global resources and such. If you do not want to use it, all is fine, but you will have to do all these things manually if you need them.

And that includes the initialization of Gtk: that is calling gtk_init().

rodrigo
  • 94,151
  • 12
  • 143
  • 190
0
  1. GtkApplication introduces action support, you can use an action to decouple the actionable item(like menu, button, ...) and the corresponding callbacks.
  2. you can use g_application_send_notification to show the notification, and replace the deprecated GtkStatusIcon.
  3. you can use the command line to open a file in an existing app: https://developer.gnome.org/gio/stable/GApplication.html#GApplication-open
lidaobing
  • 1,005
  • 13
  • 26