From 8cd9b7b51446858e9883cfff8df31273c47cf1ff Mon Sep 17 00:00:00 2001 From: werner Date: Tue, 20 Apr 2010 01:11:45 +0000 Subject: [PATCH] Added a "Save as" dialog and made fped disable "Save" if working on a manually created file. This is a precaution against accidently saving to a manual work, which would change the structure and remove all comments. - fped.h, fped.c, file.c: moved declaration of save_file_name into shared header - dump.h, dump.c (MACHINE_GENERATED): moved header marking machine-generated files into shared macro - gui.c (save_as_fpd): added "Save as" dialog - fped.h, fped.c (load_file), gui.c: disable "Save" if editing a file that doesn't have the machine-generated header git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5922 99fdad57-331a-0410-800a-d7fa5415bdb3 --- dump.c | 2 +- dump.h | 7 +++++-- file.c | 5 +---- fped.c | 20 +++++++++++++++++++- fped.h | 20 ++++++++++++++++++++ gui.c | 32 ++++++++++++++++++++++++++++++++ gui.h | 6 ++++-- 7 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 fped.h diff --git a/dump.c b/dump.c index bbd71bd..de9f1f5 100644 --- a/dump.c +++ b/dump.c @@ -536,7 +536,7 @@ int dump(FILE *file) { struct frame *frame; - fprintf(file, "/* MACHINE-GENERATED ! */\n\n"); + fprintf(file, "%s\n", MACHINE_GENERATED); for (frame = frames; frame; frame = frame->next) frame->dumped = 0; for (frame = frames; frame; frame = frame->next) { diff --git a/dump.h b/dump.h index c881814..7f59199 100644 --- a/dump.h +++ b/dump.h @@ -1,8 +1,8 @@ /* * dump.h - Dump objects in the native FPD format * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +19,9 @@ #include "obj.h" +#define MACHINE_GENERATED "/* MACHINE-GENERATED ! */\n" + + /* * vec obj * -------------------------------------------------------------- diff --git a/file.c b/file.c index a9489f0..c8a0e69 100644 --- a/file.c +++ b/file.c @@ -20,12 +20,9 @@ #include "dump.h" #include "kicad.h" #include "postscript.h" - #include "util.h" #include "file.h" - - -extern char *save_file_name; +#include "fped.h" /* ----- general helper functions ------------------------------------------ */ diff --git a/fped.c b/fped.c index 04b1b04..74e4c4f 100644 --- a/fped.c +++ b/fped.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "cpp.h" #include "util.h" @@ -21,20 +22,37 @@ #include "obj.h" #include "inst.h" #include "file.h" +#include "dump.h" #include "gui.h" #include "delete.h" #include "fpd.h" +#include "fped.h" char *save_file_name = NULL; +int no_save = 0; static void load_file(const char *name) { - if (file_exists(name) == 1) { + FILE *file; + char line[sizeof(MACHINE_GENERATED)]; + + file = fopen(name, "r"); + if (file) { + if (!fgets(line, sizeof(line), file)) { + perror(name); + exit(1); + } + no_save = strcmp(line, MACHINE_GENERATED); + fclose(file); reporter = report_parse_error; run_cpp_on_file(name); } else { + if (errno != ENOENT) { + perror(name); + exit(1); + } scan_empty(); } (void) yyparse(); diff --git a/fped.h b/fped.h new file mode 100644 index 0000000..02916aa --- /dev/null +++ b/fped.h @@ -0,0 +1,20 @@ +/* + * fped.h - Things fped.c exports + * + * Written 2010 by Werner Almesberger + * Copyright 2010 by Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef FPED_H +#define FPED_H + +extern char *save_file_name; +extern int no_save; + +#endif /* !FPED_H */ diff --git a/gui.c b/gui.c index 9d32643..25dbe9a 100644 --- a/gui.c +++ b/gui.c @@ -23,6 +23,7 @@ #include "gui_tool.h" #include "gui_frame.h" #include "gui.h" +#include "fped.h" #include "icons/stuff.xpm" #include "icons/stuff_off.xpm" @@ -50,6 +51,33 @@ static GtkWidget *bright_image[2]; static void do_build_frames(void); +/* ----- save callbacks ---------------------------------------------------- */ + + +static void save_as_fpd(void) +{ + GtkWidget *dialog; + + dialog = gtk_file_chooser_dialog_new("Save File", + NULL, GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); + gtk_file_chooser_set_do_overwrite_confirmation( + GTK_FILE_CHOOSER(dialog), TRUE); + if (save_file_name) + gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), + save_file_name); + if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { + save_file_name = + gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + save_fpd(); + /* @@@ we may leak save_file_name */ + no_save = 0; + } + gtk_widget_destroy(dialog); +} + + /* ----- view callbacks ---------------------------------------------------- */ @@ -68,6 +96,7 @@ static void swap_var_code(void) static GtkItemFactoryEntry menu_entries[] = { { "/File", NULL, NULL, 0, "" }, { "/File/Save", NULL, save_fpd, 0, "" }, + { "/File/Save as", NULL, save_as_fpd, 0, "" }, { "/File/sep1", NULL, NULL, 0, "" }, { "/File/Write KiCad", NULL, write_kicad, 0, "" }, { "/File/Write Postscript", @@ -95,6 +124,9 @@ static void make_menu_bar(GtkWidget *hbox) bar = gtk_item_factory_get_widget(factory, ""); gtk_box_pack_start(GTK_BOX(hbox), bar, TRUE, TRUE, 0); + + gtk_widget_set_sensitive( + gtk_item_factory_get_item(factory, "/File/Save"), !no_save); } diff --git a/gui.h b/gui.h index c27dc6b..7f8f876 100644 --- a/gui.h +++ b/gui.h @@ -1,8 +1,8 @@ /* * gui.h - Editor GUI core * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,8 @@ extern int show_stuff; extern int show_meas; extern int show_bright; +extern int no_save; + /* update everything after a model change */ void change_world(void);