|
@@ -14,6 +14,7 @@ namespace UICatalog {
|
|
private TextView _textView;
|
|
private TextView _textView;
|
|
private bool _saved = true;
|
|
private bool _saved = true;
|
|
private ScrollBarView _scrollBar;
|
|
private ScrollBarView _scrollBar;
|
|
|
|
+ private byte [] _originalText;
|
|
|
|
|
|
public override void Init (Toplevel top, ColorScheme colorScheme)
|
|
public override void Init (Toplevel top, ColorScheme colorScheme)
|
|
{
|
|
{
|
|
@@ -28,13 +29,14 @@ namespace UICatalog {
|
|
new MenuItem ("_New", "", () => New()),
|
|
new MenuItem ("_New", "", () => New()),
|
|
new MenuItem ("_Open", "", () => Open()),
|
|
new MenuItem ("_Open", "", () => Open()),
|
|
new MenuItem ("_Save", "", () => Save()),
|
|
new MenuItem ("_Save", "", () => Save()),
|
|
|
|
+ new MenuItem ("_Save As", "", () => SaveAs()),
|
|
null,
|
|
null,
|
|
new MenuItem ("_Quit", "", () => Quit()),
|
|
new MenuItem ("_Quit", "", () => Quit()),
|
|
}),
|
|
}),
|
|
new MenuBarItem ("_Edit", new MenuItem [] {
|
|
new MenuBarItem ("_Edit", new MenuItem [] {
|
|
- new MenuItem ("_Copy", "", () => Copy()),
|
|
|
|
- new MenuItem ("C_ut", "", () => Cut()),
|
|
|
|
- new MenuItem ("_Paste", "", () => Paste())
|
|
|
|
|
|
+ new MenuItem ("_Copy", "", () => Copy(),null,null, Key.CtrlMask | Key.C),
|
|
|
|
+ new MenuItem ("C_ut", "", () => Cut(),null,null, Key.CtrlMask | Key.W),
|
|
|
|
+ new MenuItem ("_Paste", "", () => Paste(),null,null, Key.CtrlMask | Key.Y)
|
|
}),
|
|
}),
|
|
new MenuBarItem ("_ScrollBarView", CreateKeepChecked ()),
|
|
new MenuBarItem ("_ScrollBarView", CreateKeepChecked ()),
|
|
new MenuBarItem ("_Cursor", new MenuItem [] {
|
|
new MenuBarItem ("_Cursor", new MenuItem [] {
|
|
@@ -49,7 +51,8 @@ namespace UICatalog {
|
|
new MenuItem (" V_ertical Fix", "", () => SetCursor(CursorVisibility.VerticalFix)),
|
|
new MenuItem (" V_ertical Fix", "", () => SetCursor(CursorVisibility.VerticalFix)),
|
|
new MenuItem (" B_ox Fix", "", () => SetCursor(CursorVisibility.BoxFix)),
|
|
new MenuItem (" B_ox Fix", "", () => SetCursor(CursorVisibility.BoxFix)),
|
|
new MenuItem (" U_nderline Fix","", () => SetCursor(CursorVisibility.UnderlineFix))
|
|
new MenuItem (" U_nderline Fix","", () => SetCursor(CursorVisibility.UnderlineFix))
|
|
- })
|
|
|
|
|
|
+ }),
|
|
|
|
+ new MenuBarItem ("Forma_t", CreateWrapChecked ())
|
|
});
|
|
});
|
|
Top.Add (menu);
|
|
Top.Add (menu);
|
|
|
|
|
|
@@ -76,7 +79,8 @@ namespace UICatalog {
|
|
Y = 0,
|
|
Y = 0,
|
|
Width = Dim.Fill (),
|
|
Width = Dim.Fill (),
|
|
Height = Dim.Fill (),
|
|
Height = Dim.Fill (),
|
|
-
|
|
|
|
|
|
+ BottomOffset = 1,
|
|
|
|
+ RightOffset = 1
|
|
};
|
|
};
|
|
|
|
|
|
LoadFile ();
|
|
LoadFile ();
|
|
@@ -102,10 +106,12 @@ namespace UICatalog {
|
|
};
|
|
};
|
|
|
|
|
|
_textView.DrawContent += (e) => {
|
|
_textView.DrawContent += (e) => {
|
|
- _scrollBar.Size = _textView.Lines - 1;
|
|
|
|
|
|
+ _scrollBar.Size = _textView.Lines;
|
|
_scrollBar.Position = _textView.TopRow;
|
|
_scrollBar.Position = _textView.TopRow;
|
|
- _scrollBar.OtherScrollBarView.Size = _textView.Maxlength;
|
|
|
|
- _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
|
|
|
|
|
|
+ if (_scrollBar.OtherScrollBarView != null) {
|
|
|
|
+ _scrollBar.OtherScrollBarView.Size = _textView.Maxlength;
|
|
|
|
+ _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
|
|
|
|
+ }
|
|
_scrollBar.LayoutSubviews ();
|
|
_scrollBar.LayoutSubviews ();
|
|
_scrollBar.Refresh ();
|
|
_scrollBar.Refresh ();
|
|
};
|
|
};
|
|
@@ -117,20 +123,23 @@ namespace UICatalog {
|
|
|
|
|
|
private void New ()
|
|
private void New ()
|
|
{
|
|
{
|
|
- Win.Title = _fileName = "Untitled";
|
|
|
|
- throw new NotImplementedException ();
|
|
|
|
|
|
+ if (!CanCloseFile ()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Win.Title = "Untitled.txt";
|
|
|
|
+ _fileName = null;
|
|
|
|
+ _originalText = new System.IO.MemoryStream ().ToArray ();
|
|
|
|
+ _textView.Text = _originalText;
|
|
}
|
|
}
|
|
|
|
|
|
private void LoadFile ()
|
|
private void LoadFile ()
|
|
{
|
|
{
|
|
- if (!_saved) {
|
|
|
|
- MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
if (_fileName != null) {
|
|
if (_fileName != null) {
|
|
// BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
|
|
// BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
|
|
//_textView.LoadFile(_fileName);
|
|
//_textView.LoadFile(_fileName);
|
|
_textView.Text = System.IO.File.ReadAllText (_fileName);
|
|
_textView.Text = System.IO.File.ReadAllText (_fileName);
|
|
|
|
+ _originalText = _textView.Text.ToByteArray ();
|
|
Win.Title = _fileName;
|
|
Win.Title = _fileName;
|
|
_saved = true;
|
|
_saved = true;
|
|
}
|
|
}
|
|
@@ -138,20 +147,23 @@ namespace UICatalog {
|
|
|
|
|
|
private void Paste ()
|
|
private void Paste ()
|
|
{
|
|
{
|
|
- MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
|
|
|
|
|
|
+ if (_textView != null) {
|
|
|
|
+ _textView.Paste ();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private void Cut ()
|
|
private void Cut ()
|
|
{
|
|
{
|
|
- MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
|
|
|
|
|
|
+ if (_textView != null) {
|
|
|
|
+ _textView.Cut ();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private void Copy ()
|
|
private void Copy ()
|
|
{
|
|
{
|
|
- MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
|
|
|
|
- //if (_textView != null && _textView.SelectedLength != 0) {
|
|
|
|
- // _textView.Copy ();
|
|
|
|
- //}
|
|
|
|
|
|
+ if (_textView != null) {
|
|
|
|
+ _textView.Copy ();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private void SetCursor (CursorVisibility visibility)
|
|
private void SetCursor (CursorVisibility visibility)
|
|
@@ -159,8 +171,29 @@ namespace UICatalog {
|
|
_textView.DesiredCursorVisibility = visibility;
|
|
_textView.DesiredCursorVisibility = visibility;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private bool CanCloseFile ()
|
|
|
|
+ {
|
|
|
|
+ if (_textView.Text == _originalText) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var r = MessageBox.ErrorQuery ("Save File",
|
|
|
|
+ $"Do you want save changes in {Win.Title}?", "Yes", "No", "Cancel");
|
|
|
|
+ if (r == 0) {
|
|
|
|
+ return Save ();
|
|
|
|
+ } else if (r == 1) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
private void Open ()
|
|
private void Open ()
|
|
{
|
|
{
|
|
|
|
+ if (!CanCloseFile ()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
|
|
var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
|
|
Application.Run (d);
|
|
Application.Run (d);
|
|
|
|
|
|
@@ -170,14 +203,54 @@ namespace UICatalog {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void Save ()
|
|
|
|
|
|
+ private bool Save ()
|
|
{
|
|
{
|
|
if (_fileName != null) {
|
|
if (_fileName != null) {
|
|
// BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
|
|
// 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.
|
|
// As a result files saved on Windows and then read back will show invalid chars.
|
|
- System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
|
|
|
|
|
|
+ return SaveFile (Win.Title.ToString (), _fileName);
|
|
|
|
+ } else {
|
|
|
|
+ return SaveAs ();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private bool SaveAs ()
|
|
|
|
+ {
|
|
|
|
+ var sd = new SaveDialog ("Save file", "Choose the path where to save the file.");
|
|
|
|
+ sd.FilePath = System.IO.Path.Combine (sd.FilePath.ToString (), Win.Title.ToString ());
|
|
|
|
+ Application.Run (sd);
|
|
|
|
+
|
|
|
|
+ if (!sd.Canceled) {
|
|
|
|
+ if (System.IO.File.Exists (sd.FilePath.ToString ())) {
|
|
|
|
+ if (MessageBox.Query ("Save File",
|
|
|
|
+ "File already exists. Overwrite any way?", "No", "Ok") == 1) {
|
|
|
|
+ return SaveFile (sd.FileName.ToString (), sd.FilePath.ToString ());
|
|
|
|
+ } else {
|
|
|
|
+ return _saved = false;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return SaveFile (sd.FileName.ToString (), sd.FilePath.ToString ());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return _saved = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private bool SaveFile (string title, string file)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ Win.Title = title;
|
|
|
|
+ _fileName = file;
|
|
|
|
+ System.IO.File.WriteAllText (_fileName, _textView.Text.ToString ());
|
|
_saved = true;
|
|
_saved = true;
|
|
|
|
+ MessageBox.Query ("Save File", "File was successfully saved.", "Ok");
|
|
|
|
+
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ MessageBox.ErrorQuery ("Error", ex.Message, "Ok");
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
private void Quit ()
|
|
private void Quit ()
|
|
@@ -185,7 +258,7 @@ namespace UICatalog {
|
|
Application.RequestStop ();
|
|
Application.RequestStop ();
|
|
}
|
|
}
|
|
|
|
|
|
- private void CreateDemoFile(string fileName)
|
|
|
|
|
|
+ private void CreateDemoFile (string fileName)
|
|
{
|
|
{
|
|
var sb = new StringBuilder ();
|
|
var sb = new StringBuilder ();
|
|
// BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
|
|
// BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
|
|
@@ -211,6 +284,27 @@ namespace UICatalog {
|
|
return new MenuItem [] { item };
|
|
return new MenuItem [] { item };
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private MenuItem [] CreateWrapChecked ()
|
|
|
|
+ {
|
|
|
|
+ var item = new MenuItem ();
|
|
|
|
+ item.Title = "Word Wrap";
|
|
|
|
+ item.CheckType |= MenuItemCheckStyle.Checked;
|
|
|
|
+ item.Checked = false;
|
|
|
|
+ item.Action += () => {
|
|
|
|
+ _textView.WordWrap = item.Checked = !item.Checked;
|
|
|
|
+ if (_textView.WordWrap) {
|
|
|
|
+ _scrollBar.AutoHideScrollBars = false;
|
|
|
|
+ _scrollBar.OtherScrollBarView.ShowScrollIndicator = false;
|
|
|
|
+ _textView.BottomOffset = 0;
|
|
|
|
+ } else {
|
|
|
|
+ _scrollBar.AutoHideScrollBars = true;
|
|
|
|
+ _textView.BottomOffset = 1;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ return new MenuItem [] { item };
|
|
|
|
+ }
|
|
|
|
+
|
|
public override void Run ()
|
|
public override void Run ()
|
|
{
|
|
{
|
|
base.Run ();
|
|
base.Run ();
|