/* document.vala * * Copyright (C) 2008 Nicolas Joseph * * 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. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Author: * Nicolas Joseph */ using Gtk; public class Valide.Document : Gtk.ScrolledWindow { /* * Attributs */ protected Gtk.TextBuffer buffer; private string path_; private string filename_; private bool save_; /* * Signals */ public signal void title_change (); /* * Properties */ public bool is_save { get { return this.save_; } set { this.save_ = value; this.title_change (); } } /** * @fixme When file doesn't exist */ public string path { get { return this.path_; } set { if (value != null && this.path_ == null) { /** * @bug[valac] Undeclared inner_error in C source. */ GLib.Error inner_error = null; string contents; Gtk.TextIter iter; try { GLib.FileUtils.get_contents (value, out contents); this.buffer.get_start_iter (out iter); this.buffer.insert (iter, contents, -1); this.path_ = value; this.is_save = true; } catch (GLib.Error e) { GLib.critical (e.message); } } else { this.path_ = value; this.is_save = false; } } } /** @FIXME : Can't return alloc string public string title { get { string text = ""; if (this.path == null) { text = "New"; } else { text = this.path; if (!this.is_save) { text += " *"; } } return text; } } */ /* * Methods */ public Document (string path = null) { this.path = path; } construct { this.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); Gtk.TextView text_view = new Gtk.TextView (); this.add (text_view); this.buffer = text_view.get_buffer (); } public void save () { if (!this.is_save) { if (this.path_ == null) { var dialog = new Gtk.FileChooserDialog ("Save file", null, Gtk.FileChooserAction.SAVE, Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT, null); if (dialog.run () == Gtk.ResponseType.ACCEPT) { this.path_ = dialog.get_filename (); } dialog.destroy (); } if (this.path_ != null) { Gtk.TextIter start; Gtk.TextIter end; string contents; this.buffer.get_bounds (out start, out end); contents = this.buffer.get_text (start, end, false); try { GLib.FileUtils.set_contents (this.path, contents, -1); this.is_save = true; } catch (GLib.Error e) { GLib.critical (e.message); } } } } public void save_as () { bool old_is_save = this.is_save; string old_path = this.path; this.path = null; this.is_save = false; this.save (); if (!this.is_save) { this.is_save = old_is_save; this.path = old_path; } } public bool close () { if (!this.is_save) { Gtk.Dialog dialog = new Gtk.Dialog (); dialog.set_title ("Are you sure?"); dialog.add_button (Gtk.STOCK_YES, Gtk.ResponseType.YES); dialog.add_button (Gtk.STOCK_NO, Gtk.ResponseType.NO); dialog.add_button (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL); dialog.set_modal (true); Gtk.Label label = new Gtk.Label ("Document with unsaved changes. Save changes before closing?"); weak Gtk.Box vbox = (Gtk.Box)dialog.vbox; vbox.pack_start (label, true, false, 10); dialog.show_all (); switch (dialog.run ()) { case Gtk.ResponseType.YES: this.save (); break; case Gtk.ResponseType.NO: break; case Gtk.ResponseType.CANCEL: dialog.destroy (); return false; break; } dialog.destroy (); } this.destroy (); return true; } }