Implement Close menu option that allows closing the data file

This requires some helper routines that allow us to clear out all the
widgets.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2012-09-10 14:32:55 -07:00
parent 78c5aa9f07
commit a8fd77865c
5 changed files with 83 additions and 2 deletions

2
dive.h
View file

@ -390,8 +390,10 @@ extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
extern void show_dive_info(struct dive *);
extern void show_dive_equipment(struct dive *, int w_idx);
extern void clear_equipment_widgets(void);
extern void show_dive_stats(struct dive *);
extern void clear_stats_widgets(void);
extern void update_dive(struct dive *new_dive);
extern void save_dives(const char *filename);

View file

@ -1612,3 +1612,9 @@ GtkWidget *equipment_widget(int w_idx)
return vbox;
}
void clear_equipment_widgets()
{
gtk_list_store_clear(cylinder_list[W_IDX_PRIMARY].model);
gtk_list_store_clear(weightsystem_list[W_IDX_PRIMARY].model);
}

View file

@ -277,11 +277,11 @@ static gboolean ask_save_changes()
if (!existing_filename){
label = gtk_label_new (
"You have unsaved changes\nWould you like to save those before exiting the program?");
"You have unsaved changes\nWould you like to save those before closing the datafile?");
} else {
char *label_text = (char*) malloc(sizeof(char) * (93 + strlen(existing_filename)));
sprintf(label_text,
"You have unsaved changes to file: %s \nWould you like to save those before exiting the program?",
"You have unsaved changes to file: %s \nWould you like to save those before closing the datafile?",
existing_filename);
label = gtk_label_new (label_text);
g_free(label_text);
@ -299,6 +299,37 @@ static gboolean ask_save_changes()
return quit;
}
static void file_close(GtkWidget *w, gpointer data)
{
int i;
if (unsaved_changes())
if (ask_save_changes() == FALSE)
return;
existing_filename = NULL;
/* free the dives and trips */
for (i = 0; i < dive_table.nr; i++)
free(get_dive(i));
dive_table.nr = 0;
dive_table.preexisting = 0;
g_list_free_full(dive_trip_list, free);
dive_trip_list = NULL;
/* clear the selection and the statistics */
amount_selected = 0;
selected_dive = 0;
process_selected_dives();
clear_stats_widgets();
/* clear the equipment page */
clear_equipment_widgets();
/* redraw the screen */
dive_list_update_dives();
show_dive_info(NULL);
}
static gboolean on_delete(GtkWidget* w, gpointer data)
{
/* Make sure to flush any modified dive data */
@ -842,6 +873,7 @@ static GtkActionEntry menu_items[] = {
{ "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
{ "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
{ "SaveAsFile", GTK_STOCK_SAVE_AS, NULL, SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) },
{ "CloseFile", GTK_STOCK_CLOSE, NULL, NULL, NULL, G_CALLBACK(file_close) },
{ "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
{ "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
{ "AddDive", GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
@ -870,6 +902,7 @@ static const gchar* ui_string = " \
<menuitem name=\"Open\" action=\"OpenFile\" /> \
<menuitem name=\"Save\" action=\"SaveFile\" /> \
<menuitem name=\"Save As\" action=\"SaveAsFile\" /> \
<menuitem name=\"Close\" action=\"CloseFile\" /> \
<menuitem name=\"Print\" action=\"Print\" /> \
<separator name=\"Separator1\"/> \
<menuitem name=\"Preferences\" action=\"Preferences\" /> \

11
info.c
View file

@ -113,6 +113,17 @@ void show_dive_info(struct dive *dive)
const char *text;
char buffer[80];
if (!dive) {
gtk_window_set_title(GTK_WINDOW(main_window), "Subsurface");
SET_TEXT_VALUE(divemaster);
SET_TEXT_VALUE(buddy);
SET_TEXT_VALUE(location);
SET_TEXT_VALUE(suit);
gtk_entry_set_text(rating, star_strings[0]);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes), "", -1);
return;
}
/* dive number and location (or lacking that, the date) go in the window title */
text = dive->location;
if (!text)

View file

@ -416,3 +416,32 @@ GtkWidget *single_stats_widget(void)
return vbox;
}
void clear_stats_widgets(void)
{
set_label(single_w.date, "");
set_label(single_w.dive_time, "");
set_label(single_w.surf_intv, "");
set_label(single_w.max_depth, "");
set_label(single_w.avg_depth, "");
set_label(single_w.water_temp, "");
set_label(single_w.sac, "");
set_label(single_w.sac, "");
set_label(single_w.otu, "");
set_label(single_w.o2he, "");
set_label(single_w.gas_used, "");
set_label(stats_w.total_time,"");
set_label(stats_w.avg_time,"");
set_label(stats_w.shortest_time,"");
set_label(stats_w.longest_time,"");
set_label(stats_w.max_overall_depth,"");
set_label(stats_w.min_overall_depth,"");
set_label(stats_w.avg_overall_depth,"");
set_label(stats_w.min_sac,"");
set_label(stats_w.avg_sac,"");
set_label(stats_w.max_sac,"");
set_label(stats_w.selection_size,"");
set_label(stats_w.max_temp,"");
set_label(stats_w.avg_temp,"");
set_label(stats_w.min_temp,"");
}