diff --git a/src/application.c b/src/application.c index 35569fd..8301798 100644 --- a/src/application.c +++ b/src/application.c @@ -179,6 +179,13 @@ static void save_state_to_file_or_folder(struct swappy_state *state, g_object_unref(pixbuf); } +void on_destroy(GtkApplication *application, gpointer data) { + struct swappy_state *state = (struct swappy_state *)data; + if (state->output_file != NULL) { + save_state_to_file_or_folder(state, state->output_file); + } +} + void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state) { switch_mode_to_brush(state); } @@ -200,9 +207,6 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state) { } void application_finish(struct swappy_state *state) { - if (state->output_file != NULL) { - save_state_to_file_or_folder(state, state->output_file); - } paint_free_all(state); buffer_free_all(state); cairo_surface_destroy(state->cairo_surface); @@ -519,6 +523,8 @@ static bool load_layout(struct swappy_state *state) { GtkWindow *window = GTK_WINDOW(gtk_builder_get_object(builder, "paint-window")); + g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), state); + state->ui->undo = GTK_BUTTON(gtk_builder_get_object(builder, "undo-button")); state->ui->redo = GTK_BUTTON(gtk_builder_get_object(builder, "redo-button")); diff --git a/src/pixbuf.c b/src/pixbuf.c index 79d4c71..bc70fe3 100644 --- a/src/pixbuf.c +++ b/src/pixbuf.c @@ -13,18 +13,8 @@ GdkPixbuf *pixbuf_get_from_state(struct swappy_state *state) { return pixbuf; } -void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) { +static void write_file(GdkPixbuf *pixbuf, char *path) { GError *error = NULL; - - time_t current_time; - char *c_time_string; - - time(¤t_time); - - c_time_string = ctime(¤t_time); - c_time_string[strlen(c_time_string) - 1] = '\0'; - char path[MAX_PATH]; - snprintf(path, MAX_PATH, "%s/%s %s.png", folder, "Swappshot", c_time_string); gdk_pixbuf_savev(pixbuf, path, "png", NULL, NULL, &error); if (error != NULL) { @@ -40,6 +30,19 @@ void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) { g_free(message); } +void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) { + time_t current_time; + char *c_time_string; + + time(¤t_time); + + c_time_string = ctime(¤t_time); + c_time_string[strlen(c_time_string) - 1] = '\0'; + char path[MAX_PATH]; + snprintf(path, MAX_PATH, "%s/%s %s.png", folder, "Swappshot", c_time_string); + write_file(pixbuf, path); +} + void pixbuf_save_to_stdout(GdkPixbuf *pixbuf) { GOutputStream *out; GError *error = NULL; @@ -61,6 +64,6 @@ void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file) { if (g_strcmp0(file, "-") == 0) { pixbuf_save_to_stdout(pixbuf); } else { - pixbuf_save_to_file(pixbuf, file); + write_file(pixbuf, file); } -} \ No newline at end of file +}