The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Overwrite dialog

These changes was commited to the Birdfont repository Tue, 03 Nov 2015 19:39:32 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Tue, 03 Nov 2015 19:39:32 +0000 (20:39 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Tue, 03 Nov 2015 19:39:32 +0000 (20:39 +0100)
commit 16f97adb10772bf80110bdc20356025b6639e9d3
tree 8f218cb99e963670fc5675202fd772777e21c88d
parent cb5ad1dbcbcb0d84ec33265fa292bd863d39a9ab
Overwrite dialog

libbirdfont/FileChooser.vala
libbirdfont/OverwriteBfFile.vala [new ]
libbirdfont/QuestionDialog.vala [new ]
libbirdfont/SaveCallback.vala
--- a/libbirdfont/FileChooser.vala +++ b/libbirdfont/FileChooser.vala @@ -20,10 +20,25 @@ public static const uint SAVE = 1; 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) {
--- /dev/null +++ b/libbirdfont/OverwriteBfFile.vala @@ -1,1 +1,41 @@ + /* + 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 (); + }); + } + } + + }
--- /dev/null +++ b/libbirdfont/QuestionDialog.vala @@ -1,1 +1,121 @@ + /* + 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); + } + } + } + + }
--- a/libbirdfont/SaveCallback.vala +++ b/libbirdfont/SaveCallback.vala @@ -18,6 +18,8 @@ public signal void file_saved (); public bool is_done = false; + + public string file_name = ""; public SaveCallback () { file_saved.connect (() => { @@ -34,36 +36,32 @@ 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 @@ 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,6 +94,6 @@ } } } - + }