1
votes

On on GTK window there is a red close icon rendered in the title bar. Normally when you click on this, the window is closed and it's resources released.

Is there a way of intercepting the normal flow to prevent the window from being destroyed so that I can show it again later? i.e. I want to hide the window not close/destroy it.

This is what I have so far.

void destroy_window_callback(GtkWidget* widget, WebWindow_Linux* source)
{
  printf("Don't destroy the window, just hide it.\n");
}


g_signal_connect(web_window, "destroy", G_CALLBACK(destroy_window_callback), this);
1
Where is your code? Have you tried anything at all. Are you handling the "destroy" signal? - Iharob Al Asimi
Also, where do you want to hide it? - Iharob Al Asimi
@iharob - I haven't tried attaching a signal because I don't know which one to catch. Sorry I'm fairly green on GTK programming. I just want to hide the window and not have it visible in the dock. - hookenz
It's delete-event. Please give me a second while I post an example. - Iharob Al Asimi

1 Answers

4
votes

This is probably what you need

#include <gtk/gtk.h>

void
on_button_clicked(GtkButton *button, gpointer data)
{
    GtkWidget *widget;
    widget = (GtkWidget *) data;
    if (widget == NULL)
        return;
    gtk_widget_show(widget);
    return;
}

gboolean
on_widget_deleted(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    gtk_widget_hide(widget);
    return TRUE;
}

int
main(int argc, char **argv)
{
    GtkWidget *window1;
    GtkWidget *window2;
    GtkWidget *button;
    gtk_init(&argc, &argv);

    window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("Show again...");

    g_signal_connect(G_OBJECT(window1),
        "destroy", gtk_main_quit, NULL);
    g_signal_connect(G_OBJECT(window2), 
        "delete-event", G_CALLBACK(on_widget_deleted), NULL);
    g_signal_connect(G_OBJECT(button), 
        "clicked", G_CALLBACK(on_button_clicked), window2);
    gtk_container_add(GTK_CONTAINER(window1), button);
    gtk_widget_set_size_request(window1, 300, 100);
    gtk_widget_set_size_request(window2, 300, 100);

    gtk_widget_show_all(window1);
    gtk_widget_show(window2);

    gtk_main();
    return 0;
}

We basically have three widgets, two top level windows and a button. The first window has it's "destroy" event connected to gtk_main_quit() quitting the application when the window's close button is pressed. The second window has it's "delete-event" connected to a custom function. This is the important one. As you see it returns TRUE indicating that the signal was handled and thus preventing to call the default handler and hence preventing the call to gtk_widget_destroy(). Also in it we can hide the widget if we want.