Files
Terminal.Gui/UICatalog/Scenarios/Editor.cs
Charlie Kindel 5b845307a2 UI catalog (#387)
* key down/up support

* line endings?

* line endings

* KeyDown/Up support

* line endings

* line endings

* Revert "Drop NuGet restore"

This reverts commit 5c7a0d05f0.

* Revert "Revert "Drop NuGet restore""

This reverts commit 2dc5fce865.

* updated demo

* defined styles

* Smarter StatusBar bottom tracking.

* Prepping for https://github.com/migueldeicaza/gui.cs/issues/376

* Oops.

* Fixed StatusBar 'snap to bottom'

* line endings

* Revert "Fixed StatusBar 'snap to bottom'"

This reverts commit 9a91c957e2.

* started UICatalog project

* Initial working POC.

* Fix newlines

* merge

* textalignment demo tweaks

* textalignment demo tweaks

* Unicode Menu Scenario

* not sure why this keeps changing

* re-added project to .sln file

* re-enabled status bar

* moved scenarios to dir

* building a dim and pos demo

* terminal.sln

* progress...barely

* fixed exit

* progress with some underlying fixes to Label

* added readme

* fixes build issue

* launch

* made default colors readable on Windows

* major UI Catalog upgrade

* added more demos and updated readme

* refactored and added more tests

* added ref to Issue #437

* added OnKeyUp support to Curses and Net drivers

* more tweaks - grab PR #438 first

* Added a OpenSelectedItem event to the ListView #429

* updates

* moved KeyUpHandler out of special ESC stuff

* more tweaks & improvements

* testing top window bug

* supported OpenSelectedItem

* lots of updates

* fixed regression, fixed #444

* better button scenario

* tweaks

* add Ready event to Toplevel

* dotfx .gitignroe

* ready for ready

* updated colors based on feedback; consolodated config code

* tweaked readme

* readme

* Added Editor demonstrating TextView

* Added Editor demonstrating TextView

* added hexeditor scenario

Co-authored-by: Miguel de Icaza <miguel@gnome.org>
Co-authored-by: BDisp <bd.bdisp@gmail.com>
2020-05-20 13:37:12 -04:00

151 lines
3.6 KiB
C#

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);
}
}
}