using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using TradeIdeas.XML; using System.IO; namespace TradeIdeas.TIProGUI { public partial class EditSymbolListDialog : Form { private List _phrases; private string _fileNameSaved; private string _fileNameLoaded; //private XmlNode _commonPhrases; //Fonts private FontManager _fontManager; private const float DEFAULT_FONT_SIZE = 8.25F; public EditSymbolListDialog(HashSet symbols, bool viewOnly = false) { InitializeComponent(); Icon = GuiEnvironment.Icon; SetSymbols(symbols); _phrases = GuiEnvironment.XmlConfig.Node("EDIT_SYMBOL_LIST").Node("PHRASES"); //_commonPhrases = GuiEnvironment.XmlConfig.Node("COMMON_PHRASES"); CultureChanged(); if (viewOnly) SetViewOnlyMode(); _fontManager = new FontManager(this, DEFAULT_FONT_SIZE); _fontManager.selectTheFont(); } private void SetViewOnlyMode() { loadButton.Enabled = false; insertButton.Enabled = false; symbolsTextBox.ReadOnly = true; Text = Text + " - View Only Mode"; } private void CultureChanged() { Text = _phrases.Node("WINDOW_TITLE").PropertyForCulture("TEXT", "***"); okButton.Text = _phrases.Node("OK").PropertyForCulture("TEXT", "***"); cancelButton.Text = _phrases.Node("CANCEL").PropertyForCulture("TEXT", "***"); loadButton.Text = _phrases.Node("LOAD_FROM_FILE").PropertyForCulture("TEXT", "***"); insertButton.Text = _phrases.Node("INSERT_FROM_FILE").PropertyForCulture("TEXT", "***"); saveButton.Text = _phrases.Node("SAVE_TO_FILE").PropertyForCulture("TEXT", "***"); } public string[] Lines { get { return symbolsTextBox.Lines; } } // Without this it selects all the text in the textBox by default because it // is the first control and automatically receives focus. protected override void OnShown( EventArgs e ) { symbolsTextBox.SelectionLength = 0; base.OnShown( e ); } private void SetSymbols(HashSet symbols) { // Make sure we don't have the empty string symbols.Remove(""); string[] newLines = new string[symbols.Count]; symbols.CopyTo(newLines); Array.Sort(newLines); symbolsTextBox.Lines = newLines; } private void AddSymbolsFromFileDialog(HashSet symbols) { using (OpenFileDialog fileDialog = new OpenFileDialog()) { fileDialog.Filter = _phrases.Node("TEXT_FILES_FILTER").PropertyForCulture("TEXT", "***") + "|*.txt"; fileDialog.FileName = FileNameMethod.QuoteFileName(_fileNameLoaded); DialogResult result = fileDialog.ShowDialog(); if (result == DialogResult.OK) { try { string[] text = File.ReadAllLines(fileDialog.FileName); foreach (string symbol in text) { if (symbol.Length > 0) symbols.Add(symbol); } _fileNameLoaded = Path.GetFileName(fileDialog.FileName); } catch (IOException ex) { MessageBox.Show(ex.Message, _phrases.Node("ERROR_READING_FILE").PropertyForCulture("TEXT", "***"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } } private void insertButton_Click(object sender, EventArgs e) { HashSet allSymbols = new HashSet(symbolsTextBox.Lines); AddSymbolsFromFileDialog(allSymbols); SetSymbols(allSymbols); } private void loadButton_Click(object sender, EventArgs e) { HashSet newSymbols = new HashSet(); AddSymbolsFromFileDialog(newSymbols); SetSymbols(newSymbols); } private void saveButton_Click(object sender, EventArgs e) { using (SaveFileDialog fileDialog = new SaveFileDialog()) { fileDialog.Filter = _phrases.Node("TEXT_FILES_FILTER").PropertyForCulture("TEXT", "***") + "|*.txt"; string fileName = ""; if (_fileNameSaved == null) { fileName = Text; // No file prefix required since title is static. } else { fileName = _fileNameSaved; } fileDialog.FileName = FileNameMethod.QuoteFileName(fileName); DialogResult result = fileDialog.ShowDialog(); if (result == DialogResult.OK) { try { using (StreamWriter file = File.CreateText(fileDialog.FileName)) { // Remove duplicates and re-sort before saving. HashSet symbols = new HashSet(symbolsTextBox.Lines); SetSymbols(symbols); foreach (string symbol in symbolsTextBox.Lines) { file.WriteLine(symbol.Trim()); } } _fileNameSaved = Path.GetFileName(fileDialog.FileName); } catch (Exception ex) { MessageBox.Show(ex.Message, _phrases.Node("ERROR_READING_FILE").PropertyForCulture("TEXT", "***"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } } private void symbolsTextBox_KeyDown(object sender, KeyEventArgs e) { // This is needed since textbox MultiLine is true causes the Ctrl-A Select All to stop working. if (e.Control && e.KeyCode == Keys.A) { symbolsTextBox.SelectAll(); e.SuppressKeyPress = true; // Suppress ding } } } }