~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Samples/TextEditor/TextEditorLib/TextEditorApp.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

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