1

Hi I'm trying to program with GTK+ 2 and when I need to create a popup window after the "clicked' signal of a button, I use " gtk_widget_set_sensitive(button, FALSE) " so as not to be able to launch another popoup window from the same button when one is already launched. Yet the only way to close the popup with another callback function containing " gtk_widget_destroy(popup) " also means this callback can't "see" the first button to turn it sensitive again. Should I use a global variable and asign the popup window to it so my second callback function will see it? Or do I define more global widgets ?

What I'm really getting at is using global variables in something like GTK a common thing to do?, such as in header files, or is it bad practice at all?

Here is a sample program (with less stuff in the first callback than normal!)

#include <gtk/gtk.h>

// Is it good to set a temporary global GtkWidget * so I can asign it to 
// button1 later and then use this temporary variable to set the widget sensitive again.

// GtkWidget * temp;

static void popup_win(GtkWidget *, gpointer data);
static void close_up(GtkWidget *, GtkWidget *);


int main(int argc, char * argv[])
{

    GtkWidget * window, * button1, * vbox;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "EXAMPLE PROGRAM");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_widget_set_size_request(window, 800, 300);


    button1 = gtk_button_new_with_label("Click here");

    g_signal_connect(G_OBJECT(button1), "clicked",
                     G_CALLBACK(popup_win), NULL);


    vbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button1, FALSE, FALSE, 0);



    gtk_container_add(GTK_CONTAINER(window), vbox);

    gtk_widget_show_all(window);


    gtk_main();

    return 0;
}

static void popup_win(GtkWidget * btn, gpointer data)
{

    gtk_widget_set_sensitive(btn, FALSE);

//  I could use  temp  here. Then get to it in close_up function.
//  temp = btn

    GtkWidget * window2, * button2, * hbox;

    window2 = gtk_window_new(GTK_WINDOW_POPUP);
    gtk_window_set_title(GTK_WINDOW(window2), "POPUP WINDOW");
    gtk_container_set_border_width(GTK_CONTAINER(window2), 10);
    gtk_widget_set_size_request(window2, 100, 200);


    button2 = gtk_button_new_with_label("Click me too");

    g_signal_connect(G_OBJECT(button2), "clicked",
                     G_CALLBACK(close_up), (gpointer) window2);


    hbox = gtk_hbox_new(TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);



    gtk_container_add(GTK_CONTAINER(window2), hbox);

    gtk_widget_show_all(window2);

}

static void close_up(GtkWidget * btn, GtkWidget * win)
{    
//  can't get at button1 so I can't set widget sensitve again.
//  gtk_widget_set_sensitive(button1, TRUE);

//  Unless I'm using  temp  variable.
//  gtk_widget_set_sensitive(temp, TRUE);

    gtk_widget_destroy(win);

}```
Ad C.
  • 11
  • 2
  • You're passing in window2 into the callback. Instead of that, pass in a pointer to a struct that contains all information? – Antti Haapala -- Слава Україні Jan 01 '20 at 09:22
  • Hi antti, I've tried to create a struct at the top of the first callaback but my compiler says "error: unknown type name ‘all’", where "all" is the struct name. Should this structure be totally global? – Ad C. Jan 01 '20 at 09:51
  • Please see this one: https://stackoverflow.com/questions/15625761/why-struct-tags-are-not-type-names-in-c – Antti Haapala -- Слава Україні Jan 01 '20 at 09:53
  • For the record, the supposedly-redundant questions that already answer this question address how to pass data around GTK functions via a `gpointer`, which the asker of this question seemed not to initially understand, but this question is different: should you use global state or pass state around via GTK functions' `gpointer` argument? The question is closer to when/whether one should use globals at all (https://stackoverflow.com/q/872943/9959012) with the added quirk of stuffing all your state in briefcase structs that you pass around GTK functions in the "pro" column of using globals (imo). – 6equj5 Oct 08 '22 at 16:48

0 Answers0