/* * TextForm.cs - Simple text viewer with search options. * * Copyright (C) 2004 - 2011 Andreas Scherrer * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace Level9 { /// /// Simple text viewer with search options. /// public partial class TextForm : Form { // -- Attributes ----------------------------------------------------- /// /// This property indicates whether the dialog disposes with the close /// operation. /// bool disposeOnClose = true; /// /// The Config.WindowSize instance that stores the dialog size. /// Config.WindowSize dialogSize = null; // -- Constructors --------------------------------------------------- /// /// Initializes a new instance of the TextForm class. /// /// /// if set to true the dialog disposes with the close operation /// /// /// the Config.WindowSize instance that stores the dialog size /// public TextForm(bool disposeOnClose, Config.WindowSize dialogSize) { InitializeComponent(); this.disposeOnClose = disposeOnClose; this.dialogSize = dialogSize; this.ClientSize = dialogSize.ToSize(); // // Set images // buttonNext.Image = Images.Get(Images.Stock.Down_16x16); buttonPrevious.Image = Images.Get(Images.Stock.Up_16x16); menuItemCopy.Image = Images.Get(Images.Stock.Copy_16x16); menuItemSave.Image = Images.Get(Images.Stock.Save_16x16); menuItemClose.Image = Images.Get(Images.Stock.Close_16x16); } // -- Event handling ------------------------------------------------- /// /// Invoked when the form is being closed. /// /// the object that originated the event /// /// a FormClosingEventArgs that contains the event data /// void TextFormFormClosing(object sender, FormClosingEventArgs e) { if (this.WindowState == FormWindowState.Normal) { dialogSize.FromSize(this.ClientSize); } if (!disposeOnClose && e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; this.DialogResult = DialogResult.OK; this.Hide(); } else { e.Cancel = false; } } /// /// Invoked when the 'Close' menu item has been clicked. /// /// the object that originated the event /// an EventArgs that contains the event data void MenuItemCloseClick(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } /// /// Invoked when the 'Copy to clipboard' menu item has been clicked. /// /// the object that originated the event /// an EventArgs that contains the event data void MenuItemCopyClick(object sender, EventArgs e) { textBox.SuspendLayout(); textBox.SelectAll(); textBox.Copy(); textBox.SelectionLength = 0; textBox.ResumeLayout(true); } /// /// Invoked when the 'Save' menu item has been clicked. /// /// the object that originated the event /// an EventArgs that contains the event data void MenuItemSaveClick(object sender, EventArgs e) { string filename = FileUtils.SFDTextFiles(menuItemSave.Text); if (filename != null) { try { FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter writer = new StreamWriter(fs); writer.Write(textBox.Text); writer.Close(); fs.Close(); } catch (Exception ex) { ErrorHandler.Handle(this, "Error writing output file.", ex); } } } /// /// Invoked when the 'Find next' button has been pressed. /// /// the object that originated the event /// an EventArgs that contains the event data void ButtonNextClick(object sender, EventArgs e) { FindText(false); } /// /// Invoked when the 'Find previous' button has been pressed. /// /// the object that originated the event /// an EventArgs that contains the event data void ButtonPreviousClick(object sender, EventArgs e) { FindText(true); } /// /// Invoked when the form has focus and the user presses a key. /// /// the object that originated the event /// an EventArgs that contains the event data void TextFormKeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Return: if (textBoxFind.Focused) { ButtonNextClick(sender, e); } break; case Keys.F3: if (e.Modifiers == Keys.Shift) { ButtonPreviousClick(sender, e); } else { ButtonNextClick(sender, e); } break; default: break; } } /// /// Invoked when the text in the 'Find' text box has been changed. /// /// the object that originated the event /// an EventArgs that contains the event data void TextBoxFindTextChanged(object sender, EventArgs e) { textBoxFind.BackColor = SystemColors.HighlightText; } // -- Methods -------------------------------------------------------- /// /// Searches for a search term in the text associated with this control. /// /// /// if set to true the search looks for previous occurrences of the /// search term /// /// /// true if the search term has been found, else false /// bool FindText(bool previous) { bool found = false; if (textBoxFind.TextLength > 1) { int index, startIndex, endIndex; RichTextBoxFinds finds = RichTextBoxFinds.None; textBoxFind.BackColor = SystemColors.HighlightText; if (previous) { finds |= RichTextBoxFinds.Reverse; startIndex = 0; endIndex = textBox.SelectionStart; } else { startIndex = textBox.SelectionStart + textBox.SelectionLength; textBox.SelectionLength = 0; textBox.SelectionStart = startIndex; endIndex = textBox.TextLength; } index = textBox.Find(textBoxFind.Text, startIndex, endIndex, finds); if (index < 0) { index = textBox.Find(textBoxFind.Text, 0, textBox.TextLength, finds); } if (index > 0) { found = true; textBox.Focus(); } else { textBoxFind.BackColor = Color.Pink; textBoxFind.SelectionLength = 0; } } return found; } /// /// Appends a string to the text associated with this control. /// /// the text to append public void AppendText(string text) { if (text == null || text.Length == 0) { return; } textBox.AppendText(text); } /// /// Appends a string to the text associated with this control. /// /// the text to append /// the text color public void AppendText(string text, Color color) { if (text == null || text.Length == 0) { return; } textBox.AppendText(text); textBox.SelectionStart = textBox.TextLength - text.Length; textBox.SelectionLength = text.Length; textBox.SelectionColor = color; textBox.SelectionLength = 0; } /// /// Sets the caret position. /// /// the caret position public void SetCaretPosition(int pos) { if (pos >= 0 && pos < textBox.TextLength) { textBox.SelectionStart = pos; textBox.SelectionLength = 0; } } /// /// Returns the length of the text associated with this control. /// public int GetTextLength() { return textBox.TextLength; } /// /// Clears the text associated with this control. /// public void Clear() { textBox.SelectionStart = 0; textBox.Clear(); } } }