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 Dec 4, 2022
1 parent 596b9a8 commit 18a899a
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 74 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ fill_shape=false
- `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
11 changes: 11 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ project(
],
)

i18n = import('i18n')
LOCALEDIR = join_paths(get_option('prefix'), get_option('localedir'))

conf = configuration_data()
conf.set_quoted('PACKAGE', meson.project_name())
conf.set_quoted('LOCALEDIR', LOCALEDIR)
config_h = configure_file(
output : 'generated_config.h',
configuration : conf)
add_project_arguments('-include', 'generated_config.h', language : 'c')

version = '"@0@"'.format(meson.project_version())
git = find_program('git', native: true, required: false)
if git.found()
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-symbolic</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 @@ -781,6 +786,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
35 changes: 35 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gdk/gdk.h>
#include <glib-2.0/glib.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <stdio.h>
Expand Down Expand Up @@ -312,6 +313,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);
g_free(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);
}

gtk_widget_destroy(dialog);
}

void clear_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_clear(state);
}
Expand Down Expand Up @@ -353,6 +385,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
8 changes: 8 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#define _POSIX_C_SOURCE 200809L

#include <libintl.h>
#include <locale.h>

#include "application.h"
#include "config.h"

int main(int argc, char *argv[]) {
struct swappy_state state = {0};
int status;

setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
bind_textdomain_codeset(PACKAGE, "UTF-8");
textdomain(PACKAGE);

state.argc = argc;
state.argv = argv;
state.mode = SWAPPY_PAINT_MODE_BRUSH;
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
1 change: 1 addition & 0 deletions src/po/POTFILES
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
res/swappy.glade
src/application.c
34 changes: 23 additions & 11 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: 2022-11-18 16:07-0500\n"
"POT-Creation-Date: 2022-12-04 22:52+0100\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,42 +17,54 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

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

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

#: res/swappy.glade:592
#: res/swappy.glade:597
msgid "Fill shape"
msgstr ""

#: res/swappy.glade:597
#: res/swappy.glade:602
msgid "Toggle shape filling"
msgstr ""

#: res/swappy.glade:671
#: res/swappy.glade:676
msgid "Toggle Paint Panel"
msgstr "Farbtafel umschalten"

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

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

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

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

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

#: res/swappy.glade:800
msgid "Save Surface As"
msgstr "Fläche speichern unter"

#: src/application.c:327
msgid "_Cancel"
msgstr "_Abbrechen"

#: src/application.c:328
msgid "_Save"
msgstr "_Speichern"
34 changes: 23 additions & 11 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: 2022-11-18 16:07-0500\n"
"POT-Creation-Date: 2022-12-04 22:52+0100\n"
"PO-Revision-Date: 2020-06-21 21:57-0400\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
Expand All @@ -17,42 +17,54 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

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

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

#: res/swappy.glade:592
#: res/swappy.glade:597
msgid "Fill shape"
msgstr "Fill shape"

#: res/swappy.glade:597
#: res/swappy.glade:602
msgid "Toggle shape filling"
msgstr "Toggle shape filling"

#: res/swappy.glade:671
#: res/swappy.glade:676
msgid "Toggle Paint Panel"
msgstr "Toggle Paint Panel"

#: res/swappy.glade:697
#: res/swappy.glade:702
msgid "Undo Last Paint"
msgstr "Undo Last Paint"

#: res/swappy.glade:716
#: res/swappy.glade:721
msgid "Redo Previous Paint"
msgstr "Redo Previous Paint"

#: res/swappy.glade:735
#: res/swappy.glade:740
msgid "Clear Paints"
msgstr "Clear Paints"

#: res/swappy.glade:763
#: res/swappy.glade:768
msgid "Copy Surface"
msgstr "Copy Surface"

#: res/swappy.glade:779
#: res/swappy.glade:784
msgid "Save Surface"
msgstr "Save Surface"

#: res/swappy.glade:800
msgid "Save Surface As"
msgstr "Save Surface As"

#: src/application.c:327
msgid "_Cancel"
msgstr "_Cancel"

#: src/application.c:328
msgid "_Save"
msgstr "_Save"
Loading

0 comments on commit 18a899a

Please sign in to comment.