I have a main window with a menu that opens another window. This secondary window has a Close button. This button has a connected signal. My problem is that I do not know how to close / destroy this parent window. I tried with gtk_widget_destroy, but an error appears because the window is not widgets .... I did not find any function to destroy the parent window ....
Can someone show me the way please? Thanks in advance.
-----------------------------------------------
Ok I am posting a piece of code. When I run the program, I click the "Open window" button. A new window is opened with one "Close" button. If I click the Close button, I will get the following error in the terminal: (Windows: 13801): Gtk-CRITICAL **: gtk_widget_destroy: statement `GTK_IS_WIDGET (widget) 'failed
Code:
#include <stdlib.h> #include <gtk/gtk.h> #include <gdk/gdkkeysyms.h> void open_window(GtkWidget *widget, gpointer window); void close_window(GtkWidget *widget, gpointer window); int main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *fixed; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Windows"); gtk_window_set_default_size(GTK_WINDOW(window), 230, 150); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window), fixed); button = gtk_button_new_with_label("Open window"); gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50); gtk_widget_set_size_request(button, 80, 35); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(open_window), G_OBJECT(window)); g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0; } void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); gtk_builder_connect_signals (builder, NULL); g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } void close_window(GtkWidget *widget, gpointer window) { gtk_widget_destroy(GTK_WIDGET(window)); }
The file "secondWindow.glade" defines the window, table and button placed in the middle cell of the table. In addition, it is defined by the handle of the clicked button with the name close_window.
Link to the glade file if someone wants to execute it: https://sites.google.com/site/marvalsiteimages/secondWindow.glade
Hope this helps you understand my problem. Thansk.
----------------------------------------------- -
Result based code based on answer:
void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; GtkWidget *closeButton = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton")); g_signal_connect (G_OBJECT (closeButton), "clicked", G_CALLBACK (close_window), G_OBJECT (secondWindow));