Hi, sorry for the delay in replying, I do have a real life as well. I have rewritten your code, but not tested it.
The first thing I did was to changed the way you created and used the dialog window. I first removed all the depreciated uses of the internal variables of the object GtkDialog and replaced them with the relevant functions. I also did not use signals, but used the return value from gtk_dialog_run() to control what should be done next having first assigned a return value for each button with the function gtk_dialog_new_with_buttons().
Next I looked at the way you are accessing arrays. In your original form there is no way of knowing how long your array is as you have not stored a length variable or NULL terminated your array.
I have used the Glib library of data structures to help here in particular GArray and GPtrArray. These have functions to make memory management of arrays easy. as they expand as you add entries to them. Paul in a previous posting has suggested that you use this method.
Please take your time looking over the current documentation for GtkDialog, GArray and GPtrArray, and avoid the use of depreciated code. Note that what is depreciated in GTK+ 2 has now been removed in GTK+ 3.
Code:
static gboolean delete_event(GtkWidget *window, GdkEvent *event)
{
int i;
int j = -1;
GArray * filenotsavedarray = g_array_new(FALSE, FALSE, sizeof(int));
/* Search for files that have been changed, and fill an array.
* note that this could be very slow, say if files are over a
* network, or on a device that has now been un-mounted.
* Would advise the use of a boolean variable to keep track of
* when a document has been changed.
*/
for (i = 0; i < gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook)); ++i)
{
char *text;
GtkTextBuffer *buffer;
GtkTextIter start_iter, end_iter;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[i]));
gtk_text_buffer_get_start_iter(buffer, &start_iter);
gtk_text_buffer_get_end_iter(buffer, &end_iter);
text = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
if (function_filechanged(filepathsarray[i], text) == TRUE)
{
g_array_append_val(filenotsavedarray, i);
}
g_free(text);
}
if (filenotsavedarray->len != 0)
{
GtkWidget *dialog, *label, *content_area;
GPtrArray *filecheckbuttonsarray = g_ptr_array_sized_(filenotsavedarray->len);
int status;
/* Create a dialog window with some buttons and a title for the window */
dialog = gtk_dialog_new_with_buttons("Exit and save documents",
GTK_WINDOW(window), GTK_DIALOG_MODAL,
"Close without saving", GTK_RESPONSE_REJECT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
/* Add a label to the contents area of the dialog box */
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
label = gtk_label_new("Following files are not saved. Please select the files you want to save before closing.");
gtk_box_pack_start(GTK_BOX(content_area), label, FALSE, FALSE, 0);
/* Add Check Button for each unsaved file to the dialog window
* contents area and to an array
*/
for (i = 0; i < filenotsavedarray->len; ++i)
{
GtkWidget *cb;
int val = g_array_index(filenotsavedarray, int, i);
cb = gtk_check_button_new_with_label(gtk_notebook_get_tab_label_text(GTK_NOTEBOOK(notebook), scrollwin[val]));
g_ptr_array_add(filecheckbuttonsarray, (gpointer) cb);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb), TRUE);
gtk_box_pack_start(GTK_BOX(content_area), cb, FALSE, FALSE, 0);
}
/* Show the dialog window and get the returned status */
gtk_widget_show_all(dialog);
status = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
switch (status) {
/* Saved button clicked */
case GTK_RESPONSE_ACCEPT:
exitdialog_cmdsave(window, filecheckbuttonsarray, filenotsavedarray);
/* Close without saving - button clicked */
case GTK_RESPONSE_REJECT:
g_ptr_array_free(filecheckbuttonsarray, TRUE);
g_array_free(filenotsavedarray, TRUE);
return FALSE; /* Carry on and delete the window */
/* Cancel button clicked */
case GTK_RESPONSE_CANCEL:
/* Anything else ??? */
default:
g_ptr_array_free(filecheckbuttonsarray, TRUE);
g_array_free(filenotsavedarray, TRUE);
return TRUE; /* Cancel the window delete signal */
}
}
g_array_free(filenotsavedarray, TRUE);
return FALSE;
}
static void destroy(GtkWidget *window)
{
gtk_main_quit();
}
/* Work through the arrays and save the files. */
void exitdialog_cmdsave(GtkWidget *widget, GPtrArray *widgetarray, GArray *filenotsavedarray)
{
int i;
for (i = 0; i < filenotsavedarray->len; ++i)
{
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(g_ptr_array_index(widgetarray, i))) == TRUE)
{
int idx = g_array_index(filenotsavedarray, int, i);
if (strcmp(filepathsarray[idx], "") != 0)
{
GtkTextIter start_iter, end_iter;
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[idx]));
gtk_text_buffer_get_end_iter(buffer, &end_iter);
gtk_text_buffer_get_start_iter(buffer, &start_iter);
g_file_set_contents(filepathsarray[idx], gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE), -1, NULL);
}
else
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), idx);
saveas_activated(widget, window);
}
}
}
}
E.