3

Is there an advantage of using a "Template" based method in GTK+ apps? I am making my first GTK+ app, and I am finding on the internet that there are many variations of creating a GTK+ apps, some older looking methods, some newer, I looked at the GTK+ dev guide and it uses a complex looking "Template" with sub-classing etc. See: https://developer.gnome.org/gtk3/stable/ch01s04.html

My question is am I fine doing it with my method or should I be using templates / sub-classing?

My way:

<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkApplicationWindow" id="window">
...

#include <gtk/gtk.h>

static void activate (GtkApplication* app, gpointer user_data) {
  GtkBuilder *builder = gtk_builder_new_from_file ("gui.glade");
  GtkWidget *window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
  gtk_builder_connect_signals (builder, NULL);
  gtk_widget_show_all (window);
  gtk_main ();
}

int main (int argc, char *argv[]) {
  GtkApplication *app = gtk_application_new ("a.b.c", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  int status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
  return status;
}

vs "Template" way:

<interface>
  <requires lib="gtk+" version="3.20"/>
  <template class="ExampleAppWindow" parent="GtkApplicationWindow">
...

#include <gtk/gtk.h>

#include "exampleapp.h"
#include "exampleappwin.h"

struct _ExampleApp
{
  GtkApplication parent;
};

G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);

static void
example_app_init (ExampleApp *app)
{
}

static void
example_app_activate (GApplication *app)
{
  ExampleAppWindow *win;

  win = example_app_window_new (EXAMPLE_APP (app));
  gtk_window_present (GTK_WINDOW (win));
}

static void
example_app_open (GApplication  *app,
                  GFile        **files,
                  gint           n_files,
                  const gchar   *hint)
{
  GList *windows;
  ExampleAppWindow *win;
  int i;
...
Community
  • 1
  • 1
sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • This seems like a highly opinion-based question, where the real answer is probably 'use whatever you prefer' until a specific problem arises where any of the available methods falls short. – underscore_d Jan 31 '18 at 09:38
  • 2
    @underscore_d I'm assuming the gtk developers had some reasons for doing things in different ways and that there may be disadvantages or advantages to certain approaches – sprocket12 Jan 31 '18 at 10:15
  • 1
    I think that in the vast majority of cases, it comes down to what you need to do and whether XML definitions can suffice, and which of XML or widget code you personally prefer to write and maintain for your given use cases. – underscore_d Jan 31 '18 at 10:16
  • Assuming that the code works correctly, you might want to write up your samples in a more-complete fashion and ask for critique over at [codereview.se]. Be sure to read [A guide to Code Review for Stack Overflow users](//codereview.meta.stackexchange.com/a/5778) first, as some things are done differently over there! – Toby Speight Jan 31 '18 at 13:39

1 Answers1

1

The difference, from what I understand, is that templates+subclass can be easily reused. Doing it your way™ you would have to use multiple GtkBuilder objects and would still have problems with defining signals in the XML.

So if you have, for example, a Dialog that you use from N different places in your code, then you should use a template.

Also if, for example, you wish to have N copies of your main window, where N is defined by user interaction, doing it your way™ would be almost impossible.

Governa
  • 908
  • 10
  • 21