Suggestions about g_signal_connect_data and/or g_object_set_data_full



Hello all,

I've the code below, so here are just a couple of questions:

1) I'm curious, if I close the app by clicking the button that calls
gtk_main_quit, instead of forceing the window to be destroyed, it
doesn't call my GClosureNotify/GDestroyNotify functions. So, I wrote a
small code that list all toplevels and then destroy it, so it's
working pretty well so far, just curious if that's the normal
behavior.

2) in the line below: "gtk_container_add(GTK_CONTAINER (window),
button);", if I forget to create a relation between button widget to
another container, the GClosureNotify/GDestroyNotify will not be
called too. I know that's a bad programming practice, but it would
happen in a large program and have a orphan widget. So, is there any
way to overcome this issue?

Best Regards,
Rodrigo


#include <gtk/gtk.h>

GtkWidget *window;

void test(gpointer data)
{
    g_print("%s\n", (gchar *) data);
    g_free(data);
}

void close(GtkButton *button, gpointer data)
{
    gtk_widget_destroy(window);
    gtk_main_quit();
}


int main (int argc, char **argv)
{
  GtkWidget *button;
  gchar *a, *b;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect(window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);

  button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
  //gtk_container_add(GTK_CONTAINER (window), button);

  a = g_strdup("test 1");
  g_signal_connect_data(button, "clicked", G_CALLBACK (gtk_main_quit),
a,  (GClosureNotify) test, (GConnectFlags) 0);
  //g_signal_connect_data(button, "clicked", G_CALLBACK (close), a,
(GClosureNotify) test, (GConnectFlags) 0);

  b = g_strdup("test 2");
  g_object_set_data_full( G_OBJECT(button), "test_key", b,
(GDestroyNotify) test );

  gtk_widget_show_all(window);
  gtk_main ();

  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]