Skip to content

Instantly share code, notes, and snippets.

@samuelcaldas
Last active April 1, 2023 05:41
Show Gist options
  • Save samuelcaldas/70134c2ef9e5ef417725f50422c7ddc3 to your computer and use it in GitHub Desktop.
Save samuelcaldas/70134c2ef9e5ef417725f50422c7ddc3 to your computer and use it in GitHub Desktop.
#region Using Statements
using NinjaTrader.Gui;
using NinjaTrader.Gui.Tools;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
#endregion
namespace NinjaTrader.NinjaScript.AddOns
{
public partial class NTConsoleBase : AddOnBase
{
//These variables will be used in checking if the current Addon already has a menu item, and also stores the new menu item to be added.
private NTMenuItem existingMenuItems;
private NTMenuItem newMenuItem;
private void OnMenuItemClick(object sender, RoutedEventArgs e)
{
new NTConsole().Show();
// Escreve algo no console
//Console.WriteLine("Ol�, mundo!");
}
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Show addon console output";
Name = "Console Emulator";
}
}
protected override void OnWindowCreated(Window window)
{
ControlCenter controlCenter = window as ControlCenter;
if (controlCenter == null)
return;
existingMenuItems = controlCenter.FindFirst("ControlCenterMenuItemNew") as NTMenuItem;
if (existingMenuItems == null)
return;
// This is the new menu item to be created, this assigns the menu text and will be used to Add this item to the Main Menu.
newMenuItem = new NTMenuItem { Header = Name, Style = Application.Current.TryFindResource("MainMenuItem") as Style };
existingMenuItems.Items.Add(newMenuItem);
// The new menu item will do nothing by its self, a click handler is added to complete the menu item and allow for clicks.
newMenuItem.Click += OnMenuItemClick;
}
protected override void OnWindowDestroyed(Window window)
{
// This checks if there is not a menu item or if the destroyed window is not the control center.
if (newMenuItem == null || !(window is ControlCenter) ||
existingMenuItems == null ||
!existingMenuItems.Items.Contains(newMenuItem))
return;
// if the destroyed window was the control center, we clean up the click handler and remove the custom menu item and set it to null.
newMenuItem.Click -= OnMenuItemClick;
existingMenuItems.Items.Remove(newMenuItem);
newMenuItem = null;
}
}
public partial class NTConsole : NTWindow, IWorkspacePersistence
{
#region Variables
private const string AddOnName = "Console Emulator";
private TextBox consoleTextBox;
private Menu menu;
private NTMenuItem saveMenuItem;
private NTMenuItem clearMenuItem;
#endregion
public NTConsole()
{
Caption = AddOnName;
Width = 600;
Height = 400;
// Cria uma nova barra de menus
menu = new System.Windows.Controls.Menu();
// Create menu items
saveMenuItem = new NTMenuItem { Header = "Save" };
menu.Items.Add(saveMenuItem);
saveMenuItem.Click += SaveMenuItem_Click;
clearMenuItem = new NTMenuItem { Header = "Clear" };
menu.Items.Add(clearMenuItem);
clearMenuItem.Click += ClearMenuItem_Click;
// Add menu items to window menu
//Menu.Items.Add(saveMenuItem);
//Menu.Items.Add(clearMenuItem);
// Create console text box
consoleTextBox = new TextBox
{
AcceptsReturn = true,
AcceptsTab = true,
IsReadOnly = true,
TextWrapping = TextWrapping.Wrap,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
FontFamily = new System.Windows.Media.FontFamily("Consolas")
};
consoleTextBox.MouseDoubleClick += (sender, e) => consoleTextBox.SelectAll();
Content = consoleTextBox;
// Redirect console output to text box
System.Console.SetOut(new TextBoxWriter(consoleTextBox));
System.Console.SetError(new TextBoxWriter(consoleTextBox));
}
private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
{
string fileName = "console_output" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
//string fileName = "console_output.txt";
File.WriteAllText(fileName, consoleTextBox.Text);
}
private void ClearMenuItem_Click(object sender, RoutedEventArgs e)
{
consoleTextBox.Clear();
}
public void Restore(XDocument document, XElement element)
{
}
public void Save(XDocument document, XElement element)
{
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}
public WorkspaceOptions WorkspaceOptions { get; set; }
}
public class TextBoxWriter : TextWriter
{
private TextBox textBox;
public TextBoxWriter(TextBox textBox)
{
this.textBox = textBox;
}
public override void Write(char value)
{
textBox.Dispatcher.BeginInvoke(new Action(() => textBox.AppendText(value.ToString())));
}
public override void Write(string value)
{
textBox.Dispatcher.BeginInvoke(new Action(() => textBox.AppendText(value)));
}
public override System.Text.Encoding Encoding
{
get
{
return System.Text.Encoding.UTF8;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment