cgtkgtk4

Where to pass user_data in this type of method of code


Normally I used this method of coding:

int main (int argc, char **argv)
{
  struct AppData appData; // personal structure
  GtkApplication *app;
  gint status;

  app = gtk_application_new (PROGRAM_ID, G_APPLICATION_DEFAULT_FLAGS);  
  g_signal_connect (app, "startup", G_CALLBACK (cb_startup), &appData);
  g_signal_connect (app, "activate", G_CALLBACK (cb_activate), &appData);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

I'm trying to adapt my code like the trivial example:

include <gtk/gtk.h>

#include "exampleapp.h"

int main (int argc, char *argv[])
{
  struct AppData appData; // personal structure
  return g_application_run (G_APPLICATION (example_app_new ()), argc, argv);
}

My question is: how can I pass my personal structure in the above style code?


Solution

  • I think that the first variant can be taken in a simple program.
    It is also given as an example.

    The second variant is certainly useful for more complex programs.

    I rebuilt the example mentioned in the question so that it would also work in the second variant.

    main-example.c

    #include <gtk/gtk.h>
    
    #include "exampleapp.h"
    
    int
    main (int argc, char *argv[])
    {
      static appData data;
       appData *data_ptr = &data;
       data_ptr->name = "Holger";
       data_ptr->value = 11;
    
      return g_application_run (G_APPLICATION (example_app_new (data_ptr)), argc, argv);
    }
    

    exampleapp.h

    #pragma once
    
    #include <gtk/gtk.h>
    
    #define EXAMPLE_APP_TYPE (example_app_get_type ())
    G_DECLARE_FINAL_TYPE (ExampleApp, example_app, EXAMPLE, APP, GtkApplication)
    
       typedef struct AppData
       {
               char *name;
               int  value;
       } appData; // personal structure
    
    ExampleApp     *example_app_new         (appData *data);
    

    exampleapp.c

    #include <gtk/gtk.h>
    #include "exampleapp.h"
    
    struct _ExampleApp
    {
      GtkApplication parent;
      appData *data; 
    };
    
    G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);
    
    static void
    example_app_init (ExampleApp *app)
    {
    }
    
    static void
    example_app_activate (GApplication *app)
    {
      GtkWidget  *win;
    
      g_print ("Name = %s\n",EXAMPLE_APP(app)->data->name);
      g_print ("Value = %d\n",EXAMPLE_APP(app)->data->value);
    
      win = gtk_application_window_new(GTK_APPLICATION(app));
      gtk_window_present (GTK_WINDOW (win));
    }
    
    static void
    example_app_open (GApplication  *app,
                      GFile        **files,
                      int            n_files,
                      const char    *hint)
    {
    }
    
    static void
    example_app_class_init (ExampleAppClass *class)
    {
      G_APPLICATION_CLASS (class)->activate = example_app_activate;
      G_APPLICATION_CLASS (class)->open = example_app_open;
    }
    
    ExampleApp *
    example_app_new (appData *data)
    {
      GtkWidget *window;
    
      window =  g_object_new (EXAMPLE_APP_TYPE,
                           "application-id", "org.gtk.exampleapp",
                           "flags", G_APPLICATION_HANDLES_OPEN,
                           NULL);
      EXAMPLE_APP(window)->data = data;     
    
      return EXAMPLE_APP(window);
    }
    

    I do not necessarily want to say whether this is a good option.
    But it shows the principle of how it could be realized.

    Greetings