Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Save as dialog #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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>
Maaxxs marked this conversation as resolved.
Show resolved Hide resolved
<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
10 changes: 10 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#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;

// set locales according to environment variables
setlocale(LC_ALL, "");
// set base directory for translated messages
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
// explicitly set encoding of message translations to UTF-8
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");

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"
35 changes: 24 additions & 11 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: 2022-11-18 16:07-0500\n"
"POT-Creation-Date: 2022-12-04 22:52+0100\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,42 +17,55 @@ 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 "Epaisseur de ligne"

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

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

#: res/swappy.glade:597
#: res/swappy.glade:602
msgid "Toggle shape filling"
msgstr "Activer/Désactiver le remplissage de forme"

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

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

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

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

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

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

#: res/swappy.glade:800
#, fuzzy
msgid "Save Surface As"
msgstr "Sauvegarder la surface"

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

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