Aveeral Jain
Aveeral Jain

Reputation: 89

Dialog boxes not opening second time. GTK+, Glade and C

I am designing an interface using GTK+ and Glade. I have created a few dialog boxes using glade and have connected through a C code. They work perfectly fine for the first time but when the user tries to reuse it the program stops working. The command window is giving

(program_name.exe:1668): GLib-GObject-Warning **: invalid (NULL) pointer instance
(program_name.exe:1688): GLib-Gobject-Critical **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
(program)name.exe:1668): Gtk-CRITICAL **: gtk_widget_show: assertion 'GTK_IS_WIDGET(widget)' failed

Here is the code

GtkWidget *entry_obs_cells[12];
void
on_observation_cells_activate(GtkWidget *widget, gpointer user_data){
GtkWidget *observation_cells;
observation_cells = glade_xml_get_widget (gxml, "dialog3_obs_cells");
GtkWidget *label1, *label2, *label3, *label4, *label5, *label6, *label7, *label8, *label9, *label10, *time_interval;
label1 = glade_xml_get_widget(gxml, "label1");
label2 = glade_xml_get_widget(gxml, "label2_obs_cells");
label3 = glade_xml_get_widget(gxml, "label3_obs_cells");
label4 = glade_xml_get_widget(gxml, "label4_obs_cells");
label5 = glade_xml_get_widget(gxml, "label5_obs_cells");
label6 = glade_xml_get_widget(gxml, "label6_obs_cells");
label7 = glade_xml_get_widget(gxml, "label7_obs_cells");
label8 = glade_xml_get_widget(gxml, "label8_obs_cells");
label9 = glade_xml_get_widget(gxml, "label9_obs_cells");
label10 = glade_xml_get_widget(gxml, "label10_obs_cells");


GtkWidget *button1,*button2;
button1 = glade_xml_get_widget(gxml,"Save_obs_cells");
button2 = glade_xml_get_widget(gxml,"Cancel_obs_cells");

entry_obs_cells[1] = glade_xml_get_widget(gxml, "entry1_obs_cells");
entry_obs_cells[2] = glade_xml_get_widget(gxml, "entry2_obs_cells");
entry_obs_cells[3] = glade_xml_get_widget(gxml, "entry3_obs_cells");
entry_obs_cells[4] = glade_xml_get_widget(gxml, "entry4_obs_cells");
entry_obs_cells[5] = glade_xml_get_widget(gxml, "entry5_obs_cells");
entry_obs_cells[6] = glade_xml_get_widget(gxml, "entry6_obs_cells");
entry_obs_cells[7] = glade_xml_get_widget(gxml, "entry7_obs_cells");
entry_obs_cells[8] = glade_xml_get_widget(gxml, "entry8_obs_cells");
entry_obs_cells[9] = glade_xml_get_widget(gxml, "entry9_obs_cells");
entry_obs_cells[10] = glade_xml_get_widget(gxml, "entry10_obs_cells");
entry_obs_cells[11] = glade_xml_get_widget(gxml, "entry_obs_cells");
entry_obs_cells[12] = glade_xml_get_widget(gxml, "entry12_obs_cells");

gtk_widget_show (observation_cells);
g_signal_connect(GTK_BUTTON(button1), "clicked", G_CALLBACK(on_Save_obs_cells_activate), observation_cells);
g_signal_connect(GTK_BUTTON(button2), "clicked", G_CALLBACK(on_Cancel_obs_cells_activate), NULL);

}

on_Save_obs_cells_activate()
{
    char *record_results=(char*)malloc(500);
    strcpy(record_results,project_dir);
    strcat(record_results,"\\Record_results.txt");
    printf("\n%s\n",record_results);
    FILE *file;
    file = fopen(record_results, "w+");
    fprintf(file, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[11])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[11])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[1])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[2])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[3])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[4])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[5])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[6])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[7])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[8])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[9])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[10])));
    fclose(file);

}

void
on_Cancel_obs_cells_activate (GtkButton *button, gpointer dialog)
{
    gtk_widget_destroy(dialog);
}

Upvotes: 0

Views: 584

Answers (1)

Phillip Wood
Phillip Wood

Reputation: 841

As mfro said above you need to call gtk_widget_hide() instead of gtk_widget_destroy() also you will need to connect the delete-event signal to gtk_widget_hide_on_delete() to stop the window being destroyed when you click on the close button in the top right corner.

Glade XML is deprecated, it has been replaced by GtkBuilder which is included with Gtk itself. It should be quite easy to use search and replace to change the glade_xml function calls to the corresponding GtkBuilder ones. If you created the xml interface descriptions in glade they will work with GtkBuilder without any problem.

Upvotes: 1

Related Questions