/* document-factory.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.DocumentFactory : Gtk.Notebook { private static Valide.DocumentFactory instance = null; public Valide.Document current { get { return (Valide.Document)this.get_nth_page (this.page); } set { this.set_current_page (this.page_num (value)); } } protected static void title_change () { Valide.DocumentFactory self = Valide.DocumentFactory.get_instance (); var title = new Gtk.HBox (false, 4); /* Too big icon ! var button = new Gtk.Button (); button.set_relief (Gtk.ReliefStyle.NONE); button.set_focus_on_click (false); button.add (new Gtk.Image.from_stock (Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)); button.set_tooltip_text ("Close document"); title.pack_start (button, false, false, 0); */ /* Can't return alloc string var text = self.current.title; */ string text = ""; if (self.current.path == null) { text = "New file"; } else { text = self.current.path; } if (!self.current.is_save) { text += " *"; } var label = new Gtk.Label (text); title.pack_start (label, false, false, 0); title.show_all (); self.set_tab_label (self.current, title); } public static void buffer_changed () { Valide.DocumentFactory self = Valide.DocumentFactory.get_instance (); self.current.is_save = false; } private DocumentFactory () { } public static Valide.DocumentFactory get_instance () { if (Valide.DocumentFactory.instance == null) { Valide.DocumentFactory.instance = new Valide.DocumentFactory (); } return Valide.DocumentFactory.instance; } public void create (string filename = null) { var document = new Document (filename); document.title_change += this.title_change; this.append_page (document, new Gtk.Label ("")); document.show_all (); this.current = document; this.current.buffer.changed += this.buffer_changed; document.title_change (); } public void open () { var dialog = new Gtk.FileChooserDialog ("Open file", null, Gtk.FileChooserAction.OPEN, Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT, null); if (dialog.run () == Gtk.ResponseType.ACCEPT) { this.create (dialog.get_filename ()); this.current.is_save = true; } dialog.destroy (); } public bool close_all () { bool ret = true; while (this.get_n_pages () != 0) { this.set_current_page (-1); if (!this.current.close ()) { ret = false; break; } } return ret; } }