zhshr
zhshr

Reputation: 95

What's the difference between GtkApplication and gtk_init?

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.

Upvotes: 8

Views: 4671

Answers (3)

lidaobing
lidaobing

Reputation: 1075

  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

Upvotes: 0

rodrigo
rodrigo

Reputation: 98348

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().

Upvotes: 3

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

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.

Upvotes: 4

Related Questions