Skip to content

Commit

Permalink
feat(save_as): add save_as button for snapshots
Browse files Browse the repository at this point in the history
Allow to save snapshots with a different name and under a different
location than specified in the configuration as default.

Signed-off-by: Max Kunzelmann <maxdev@posteo.de>
  • Loading branch information
Maaxxs committed Aug 1, 2022
1 parent 0355760 commit 07e9e45
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 64 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ paint_mode=brush
- `Ctrl+z`: Undo
- `Ctrl+Shift+z` or `Ctrl+y`: Redo
- `Ctrl+s`: Save to file (see man page)
- `Ctrl+Shift+s`: Open "Save As" dialog
- `Ctrl+c`: Copy to clipboard
- `Escape` or `q` or `Ctrl+w`: Quit swappy

Expand Down
1 change: 1 addition & 0 deletions include/pixbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

GdkPixbuf *pixbuf_init_from_file(struct swappy_state *state);
GdkPixbuf *pixbuf_get_from_state(struct swappy_state *state);
char *format_filename(char *filename_format);
void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder,
char *filename_format);
void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file);
Expand Down
21 changes: 21 additions & 0 deletions res/swappy.glade
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<property name="can_focus">False</property>
<property name="icon_name">document-properties</property>
</object>
<object class="GtkImage" id="img-save-as-surface">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">document-save-as</property>
</object>
<object class="GtkImage" id="zoom-in">
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand Down Expand Up @@ -752,6 +757,22 @@
<property name="always_show_image">True</property>
<signal name="clicked" handler="save_clicked_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="save-as">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="receives-default">True</property>
<property name="tooltip-text" translatable="yes">Save Surface As</property>
<property name="image">img-save-as-surface</property>
<property name="always-show-image">True</property>
<signal name="clicked" handler="save_as_clicked_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
Expand Down
34 changes: 34 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,37 @@ void save_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
save_state_to_file_or_folder(state, NULL);
}

void save_as_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
gchar *filename_suggestion;
GtkWidget *dialog;
GtkFileChooser *chooser;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
gint res;

commit_state(state);
filename_suggestion = format_filename(state->config->save_filename_format);

dialog = gtk_file_chooser_dialog_new(NULL, state->ui->window, action,
("_Cancel"), GTK_RESPONSE_CANCEL,
("_Save"), GTK_RESPONSE_ACCEPT, NULL);
chooser = GTK_FILE_CHOOSER(dialog);
gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
gtk_file_chooser_set_current_folder(chooser, state->config->save_dir);
gtk_file_chooser_set_current_name(chooser, filename_suggestion);

res = gtk_dialog_run(GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT) {
gchar *filename;

filename = gtk_file_chooser_get_filename(chooser);
save_state_to_file_or_folder(state, filename);
g_free(filename);
}

g_free(filename_suggestion);
gtk_widget_destroy(dialog);
}

void clear_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_clear(state);
}
Expand Down Expand Up @@ -323,6 +354,9 @@ void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
case GDK_KEY_s:
save_state_to_file_or_folder(state, NULL);
break;
case GDK_KEY_S:
save_as_clicked_handler(NULL, state);
break;
case GDK_KEY_b:
action_toggle_painting_panel(state, NULL);
break;
Expand Down
25 changes: 17 additions & 8 deletions src/pixbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,35 @@ static void write_file(GdkPixbuf *pixbuf, char *path) {
}
}

void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder,
char *filename_format) {
char *format_filename(char *filename_format) {
time_t current_time = time(NULL);
char *c_time_string;
char filename[255];
char path[MAX_PATH];
size_t bytes_formated;
size_t bytes_formatted;

c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
bytes_formated = strftime(filename, sizeof(filename), filename_format,
localtime(&current_time));
if (!bytes_formated) {
bytes_formatted = strftime(filename, sizeof(filename), filename_format,
localtime(&current_time));
if (!bytes_formatted) {
g_warning(
"filename_format: %s overflows filename limit - file cannot be saved",
filename_format);
return;
return NULL;
}

return g_strdup(filename);
}

void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder,
char *filename_format) {
char path[MAX_PATH];
char *filename;
filename = format_filename(filename_format);
if (filename == NULL) return;

g_snprintf(path, MAX_PATH, "%s/%s", folder, filename);
g_free(filename);
g_info("saving surface to path: %s", path);
write_file(pixbuf, path);
}
Expand Down
23 changes: 14 additions & 9 deletions src/po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: swappy\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-21 21:57-0400\n"
"POT-Creation-Date: 2022-08-01 15:29+0200\n"
"PO-Revision-Date: 2020-11-19 18:03+0300\n"
"Last-Translator: Brodi <me@brodi.ml>\n"
"Language-Team: none\n"
Expand All @@ -17,34 +17,39 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: res/swappy.glade:455
#: res/swappy.glade:461
msgid "Line Width"
msgstr "Linienstärke"

#: res/swappy.glade:525
#: res/swappy.glade:531
msgid "Text Size"
msgstr "Textgröße"

#: res/swappy.glade:641
#: res/swappy.glade:647
msgid "Toggle Paint Panel"
msgstr "Farbtafel umschalten"

#: res/swappy.glade:667
#: res/swappy.glade:673
msgid "Undo Last Paint"
msgstr "Letzte Bemalung rückgängig machen"

#: res/swappy.glade:686
#: res/swappy.glade:692
msgid "Redo Previous Paint"
msgstr "Vorherige Bemalung wiederherstellen"

#: res/swappy.glade:705
#: res/swappy.glade:711
msgid "Clear Paints"
msgstr "Bemalung löschen"

#: res/swappy.glade:733
#: res/swappy.glade:739
msgid "Copy Surface"
msgstr "Fläche kopieren"

#: res/swappy.glade:749
#: res/swappy.glade:755
msgid "Save Surface"
msgstr "Fläche speichern"

#: res/swappy.glade:771
#, fuzzy
msgid "Save Surface As"
msgstr "Fläche speichern"
23 changes: 14 additions & 9 deletions src/po/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: swappy\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-21 21:57-0400\n"
"POT-Creation-Date: 2022-08-01 15:29+0200\n"
"PO-Revision-Date: 2020-06-21 21:57-0400\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
Expand All @@ -17,34 +17,39 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: res/swappy.glade:455
#: res/swappy.glade:461
msgid "Line Width"
msgstr "Line Width"

#: res/swappy.glade:525
#: res/swappy.glade:531
msgid "Text Size"
msgstr "Text Size"

#: res/swappy.glade:641
#: res/swappy.glade:647
msgid "Toggle Paint Panel"
msgstr "Toggle Paint Panel"

#: res/swappy.glade:667
#: res/swappy.glade:673
msgid "Undo Last Paint"
msgstr "Undo Last Paint"

#: res/swappy.glade:686
#: res/swappy.glade:692
msgid "Redo Previous Paint"
msgstr "Redo Previous Paint"

#: res/swappy.glade:705
#: res/swappy.glade:711
msgid "Clear Paints"
msgstr "Clear Paints"

#: res/swappy.glade:733
#: res/swappy.glade:739
msgid "Copy Surface"
msgstr "Copy Surface"

#: res/swappy.glade:749
#: res/swappy.glade:755
msgid "Save Surface"
msgstr "Save Surface"

#: res/swappy.glade:771
#, fuzzy
msgid "Save Surface As"
msgstr "Save Surface"
23 changes: 14 additions & 9 deletions src/po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: swappy\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-02-20 21:17-0500\n"
"POT-Creation-Date: 2022-08-01 15:29+0200\n"
"PO-Revision-Date: 2021-02-20 21:00-0500\n"
"Last-Translator: Jeremy Attali <contact@jtheoof.me>\n"
"Language-Team: none\n"
Expand All @@ -17,34 +17,39 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: res/swappy.glade:455
#: res/swappy.glade:461
msgid "Line Width"
msgstr "Epaisseur de ligne"

#: res/swappy.glade:525
#: res/swappy.glade:531
msgid "Text Size"
msgstr "Taille du texte"

#: res/swappy.glade:641
#: res/swappy.glade:647
msgid "Toggle Paint Panel"
msgstr "Afficher/Cacher le panneau de peinture"

#: res/swappy.glade:667
#: res/swappy.glade:673
msgid "Undo Last Paint"
msgstr "Annuler la dernière peinture"

#: res/swappy.glade:686
#: res/swappy.glade:692
msgid "Redo Previous Paint"
msgstr "Rétablir la dernière peinture"

#: res/swappy.glade:705
#: res/swappy.glade:711
msgid "Clear Paints"
msgstr "Supprimer les peintures"

#: res/swappy.glade:733
#: res/swappy.glade:739
msgid "Copy Surface"
msgstr "Copier la surface"

#: res/swappy.glade:749
#: res/swappy.glade:755
msgid "Save Surface"
msgstr "Sauvegarder la surface"

#: res/swappy.glade:771
#, fuzzy
msgid "Save Surface As"
msgstr "Sauvegarder la surface"
27 changes: 16 additions & 11 deletions src/po/pt_BR.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,50 @@ msgid ""
msgstr ""
"Project-Id-Version: swappy\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-21 21:57-0400\n"
"POT-Creation-Date: 2022-08-01 15:29+0200\n"
"PO-Revision-Date: 2021-02-14 20:38-0300\n"
"Last-Translator: Gustavo Costa <xfgusta@gmail.com>\n"
"Language-Team: \n"
"Language: pt_BR\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.4.2\n"
"Last-Translator: Gustavo Costa <xfgusta@gmail.com>\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"Language: pt_BR\n"

#: res/swappy.glade:455
#: res/swappy.glade:461
msgid "Line Width"
msgstr "Espessura da linha"

#: res/swappy.glade:525
#: res/swappy.glade:531
msgid "Text Size"
msgstr "Tamanho do texto"

#: res/swappy.glade:641
#: res/swappy.glade:647
msgid "Toggle Paint Panel"
msgstr "Alternar painel de pintura"

#: res/swappy.glade:667
#: res/swappy.glade:673
msgid "Undo Last Paint"
msgstr "Desfazer última pintura"

#: res/swappy.glade:686
#: res/swappy.glade:692
msgid "Redo Previous Paint"
msgstr "Refazer pintura anterior"

#: res/swappy.glade:705
#: res/swappy.glade:711
msgid "Clear Paints"
msgstr "Limpar pinturas"

#: res/swappy.glade:733
#: res/swappy.glade:739
msgid "Copy Surface"
msgstr "Copiar superfície"

#: res/swappy.glade:749
#: res/swappy.glade:755
msgid "Save Surface"
msgstr "Salvar superfície"

#: res/swappy.glade:771
#, fuzzy
msgid "Save Surface As"
msgstr "Salvar superfície"
Loading

0 comments on commit 07e9e45

Please sign in to comment.