123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using System;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog {
- [ScenarioMetadata (Name: "Editor", Description: "A Terminal.Gui Text Editor via TextView")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Text")]
- class Editor : Scenario {
- private string _fileName = "demo.txt";
- private TextView _textView;
- private bool _saved = true;
- public override void Init (Toplevel top)
- {
- Top = top;
- }
- public override void Setup ()
- {
- var menu = new MenuBar (new MenuBarItem [] {
- new MenuBarItem ("_File", new MenuItem [] {
- new MenuItem ("_New", "", () => New()),
- new MenuItem ("_Open", "", () => Open()),
- new MenuItem ("_Save", "", () => Save()),
- null,
- new MenuItem ("_Quit", "", () => Quit()),
- }),
- new MenuBarItem ("_Edit", new MenuItem [] {
- new MenuItem ("_Copy", "", () => Copy()),
- new MenuItem ("C_ut", "", () => Cut()),
- new MenuItem ("_Paste", "", () => Paste())
- }),
- });
- Top.Add (menu);
- var statusBar = new StatusBar (new StatusItem [] {
- new StatusItem(Key.F2, "~F2~ Open", () => Open()),
- new StatusItem(Key.F3, "~F3~ Save", () => Save()),
- new StatusItem(Key.ControlQ, "~^Q~ Quit", () => Quit()),
- });
- Top.Add (statusBar);
- CreateDemoFile (_fileName);
- Win = new Window (_fileName ?? "Untitled") {
- X = 0,
- Y = 1,
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- Top.Add (Win);
- _textView = new TextView () {
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
-
- };
- LoadFile ();
- Win.Add (_textView);
- }
- private void New ()
- {
- Win.Title = _fileName = "Untitled";
- throw new NotImplementedException ();
- }
- private void LoadFile ()
- {
- if (!_saved) {
- MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
- }
- if (_fileName != null) {
- // BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
- //_textView.LoadFile(_fileName);
- _textView.Text = System.IO.File.ReadAllText (_fileName);
- Win.Title = _fileName;
- _saved = true;
- }
- }
- private void Paste ()
- {
- MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
- }
- private void Cut ()
- {
- MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
- }
- private void Copy ()
- {
- MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
- //if (_textView != null && _textView.SelectedLength != 0) {
- // _textView.Copy ();
- //}
- }
- private void Open ()
- {
- var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
- Application.Run (d);
- if (!d.Canceled) {
- _fileName = d.FilePaths [0];
- LoadFile ();
- }
- }
- private void Save ()
- {
- if (_fileName != null) {
- // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
- // As a result files saved on Windows and then read back will show invalid chars.
- System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
- _saved = true;
- }
- }
- private void Quit ()
- {
- Application.RequestStop ();
- }
- private void CreateDemoFile(string fileName)
- {
- var sb = new StringBuilder ();
- // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
- sb.Append ("Hello world.\n");
- sb.Append ("This is a test of the Emergency Broadcast System.\n");
- var sw = System.IO.File.CreateText (fileName);
- sw.Write (sb.ToString ());
- sw.Close ();
- }
- public override void Run ()
- {
- Application.Run (Top);
- }
- }
- }
|