1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
using System;
using System.IO;
namespace TextEditor
{
public class TextEditorApp
{
static string openFile = "";
private TextEditorApp()
{
}
public static string OpenFileName {
get { return openFile; }
}
public static MainWindow MainWindow {
get { return MainWindow.Instance; }
}
public static void OpenFile (string file)
{
using (StreamReader sr = new StreamReader (file)) {
MainWindow.View.Buffer.Text = sr.ReadToEnd ();
}
SetOpenFile (file);
}
public static void SaveFile ()
{
if (openFile == "") {
Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog ("Save File", null, Gtk.FileChooserAction.Save);
fcd.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
fcd.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok);
fcd.DefaultResponse = Gtk.ResponseType.Ok;
fcd.SelectMultiple = false;
Gtk.ResponseType response = (Gtk.ResponseType) fcd.Run ();
if (response != Gtk.ResponseType.Ok) {
fcd.Destroy ();
return;
}
SetOpenFile (fcd.Filename);
fcd.Destroy ();
}
using (StreamWriter sr = new StreamWriter (openFile)) {
sr.Write (TextEditorApp.MainWindow.View.Buffer.Text);
}
}
public static void NewFile (string content)
{
SetOpenFile ("");
MainWindow.View.Buffer.Text = content;
}
static void SetOpenFile (string file)
{
openFile = file;
if (file.Length > 0)
MainWindow.Title = Path.GetFileName (file);
else
MainWindow.Title = "New File";
if (OpenFileChanged != null)
OpenFileChanged (null, EventArgs.Empty);
}
public static event EventHandler OpenFileChanged;
}
}
|