Commit 16f97ad
Overwrite dialog
Changed files (4)
modifiedlibbirdfont/FileChooser.valaaddedlibbirdfont/OverwriteBfFile.valaaddedlibbirdfont/QuestionDialog.valamodifiedlibbirdfont/SaveCallback.vala
Diff
@@ -21,11 +21,26 @@ public class FileChooser : GLib.Object { public static const uint LOAD = 1 << 1; public static const uint DIRECTORY = 1 << 2; + Gee.ArrayList<string> extensions = new Gee.ArrayList<string> ();+ public signal void file_selected (string? path); public FileChooser () { } + public int extensions_size () {+ return extensions.size;+ }++ public string get_extension (int i) {+ return_val_if_fail (0 <= i < extensions.size, "".dup ());+ return extensions.get (i);+ }+ + public void add_extension (string file_extension) {+ extensions.add (file_extension);+ }+ public void selected (string? path) { file_selected (path); }new file mode 100644@@ -0,0 +1,40 @@+/*+ Copyright (C) 2015 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Cairo;++namespace BirdFont {++public class OverwriteBfFile : QuestionDialog {++ Button replace;+ Button cancel;+ + public OverwriteBfFile (SaveCallback save_callback) {+ base(t_("This file already exists. Do you want to replace it?"), 200);+ + replace = new Button (t_("Replace"));+ replace.action.connect (() => {+ save_callback.save ();+ MainWindow.hide_dialog ();+ });++ cancel = new Button (t_("Cancel"));+ cancel.action.connect (() => {+ MainWindow.hide_dialog ();+ });+ }+}++}new file mode 100644@@ -0,0 +1,120 @@+/*+ Copyright (C) 2015 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Cairo;++namespace BirdFont {++public class QuestionDialog : Dialog {+ TextArea question;+ public Gee.ArrayList<Button> buttons;++ double width = 0;+ double height;+ + static const double font_size = 20;+ static const double margin = 20;+ + public QuestionDialog (string message, int height) {+ question = new TextArea (font_size);+ question.min_width = 300;+ question.set_editable (false);+ question.draw_border = false;+ question.text_color = Theme.get_color ("Text Tool Box");+ question.set_text (message);+ + this.height = height;+ }++ public void add_button (Button button) {+ buttons.add (button);+ }++ void layout () {+ double cx = 0;+ double cy = (allocation.height - height) / 2.0;+ double center;+ double h;+ + cx = margin;+ + foreach (Button button in buttons) {+ button.widget_x = cx;+ cx += margin + button.get_width ();+ }+ + width = question.get_width () + margin;+ center = (allocation.width - width) / 2.0;+ + question.widget_x = margin + center;+ question.widget_y = cy + margin;+ question.allocation = new WidgetAllocation.for_area (0, 0, 300, 450);+ question.layout ();+ + h = question.get_height () + margin;++ foreach (Button button in buttons) {+ button.widget_x += center;+ button.widget_y = cy + h + margin;+ }+ }++ public override void draw (Context cr) { + double cx, cy;+ + layout ();++ cx = (allocation.width - width) / 2.0;+ cy = (allocation.height - height) / 2.0;+ + cr.save ();+ Theme.color (cr, "Dialog Shadow");+ cr.rectangle (0, 0, allocation.width, allocation.height);+ cr.fill ();+ cr.restore ();+ + cr.save ();+ Theme.color (cr, "Dialog Background");+ draw_rounded_rectangle (cr, cx, cy, width, height, 10 * MainWindow.units);+ cr.fill ();+ cr.restore ();+ + cr.save ();+ Theme.color (cr, "Button Border 4");+ cr.set_line_width (1);+ draw_rounded_rectangle (cr, cx, cy, width, height, 10 * MainWindow.units);+ cr.stroke ();+ cr.restore ();++ foreach (Button button in buttons) {+ button.draw (cr);+ }+ + question.draw (cr);+ }++ public override void button_press (uint button, double x, double y) {+ foreach (Button b in buttons) {+ b.button_press (button, x, y);+ }+ }++ public override void button_release (uint button, double x, double y) {+ foreach (Button b in buttons) {+ b.button_release (button, x, y);+ }+ }+}++}@@ -19,6 +19,8 @@ public class SaveCallback : GLib.Object { public signal void file_saved (); public bool is_done = false; + public string file_name = "";+ public SaveCallback () { file_saved.connect (() => { is_done = true;@@ -34,36 +36,32 @@ public class SaveCallback : GLib.Object { FileChooser fc = new FileChooser (); fc.file_selected.connect ((fn) => { string f;- string file_name; File file;- bool saved = false;- Font font = BirdFont.get_current_font ();- int i; + OverwriteBfFile dialog; if (fn != null) { f = (!) fn;- ++#if MAC+ save ();+#else if (f.has_suffix (".bf")) { f = f.replace (".bf", ""); } - file_name = @"$(f).bf";+ file_name = @"$(f)"; file = File.new_for_path (file_name);- i = 2;- while (file.query_exists ()) {- file_name = @"$(f)_$i.bf";- file = File.new_for_path (file_name);- i++;+ if (!file.query_exists ()) {+ save ();+ } else {+ dialog = new OverwriteBfFile (this);+ MainWindow.show_dialog (dialog); }- - Preferences.add_recent_files (file_name);- - font.font_file = file_name;- save ();- saved = true;+#endif } }); + fc.add_extension ("bf"); MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); } @@ -78,6 +76,9 @@ public class SaveCallback : GLib.Object { f = BirdFont.get_current_font (); + Preferences.add_recent_files (file_name);+ f.font_file = file_name;+ if (f.is_bfp ()) { MainWindow.native_window.save (); } else {@@ -93,5 +94,5 @@ public class SaveCallback : GLib.Object { } } }- + }