diff --git a/GUI/ColoredTextBox.Designer.cs b/GUI/ColoredTextBox.Designer.cs deleted file mode 100644 index 78b338eff..000000000 --- a/GUI/ColoredTextBox.Designer.cs +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright 2012 MCForge - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at' - -http://www.opensource.org/licenses/ecl2.php -http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. -*/ - -namespace MCGalaxy.Gui.Components { - partial class ColoredTextBox { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) { - if (disposing && (components != null)) { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Component Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() { - components = new System.ComponentModel.Container(); - this.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(ColoredReader_LinkClicked); - } - - #endregion - } -} diff --git a/GUI/ColoredTextBox.cs b/GUI/ColoredTextBox.cs index 45bc1f193..38888837a 100644 --- a/GUI/ColoredTextBox.cs +++ b/GUI/ColoredTextBox.cs @@ -16,17 +16,14 @@ http://www.gnu.org/licenses/gpl-3.0.html */ using System; -using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; -using System.Windows.Forms; - -//MCGalaxy 6 Preview :D +using System.Windows.Forms; namespace MCGalaxy.Gui.Components { - /// A rich text box, that can parse Minecraft/MCGalaxy color codes. + /// Extended rich text box that auto-colors minecraft classic text. public partial class ColoredTextBox : RichTextBox { bool _nightMode = false, _colorize = true; @@ -65,50 +62,55 @@ namespace MCGalaxy.Gui.Components { /// Initializes a new instance of the class. public ColoredTextBox() : base() { - InitializeComponent(); + LinkClicked += HandleLinkClicked; } - /// Appends the log. - /// The text to log. + /// Appends text to this textbox. public void AppendLog(string text, Color foreColor, bool dateStamp) { if (InvokeRequired) { Invoke((MethodInvoker)(() => AppendLog(text, foreColor, dateStamp))); return; } - if (dateStamp) Append(CurrentDate, Color.Gray); + if (dateStamp) AppendLog(CurrentDate, Color.Gray); int line = GetLineFromCharIndex(Math.Max(0, TextLength - 1)); - if (!Colorize) { - AppendText(text); - if (AutoScroll) ScrollToEnd(line); - return; - } - LineFormatter.Format(text, (c, s) => LineFormatter.FormatGui(c, s, this, foreColor)); + if (!Colorize) { + AppendText(text); + } else { + LineFormatter.Format(text, (c, s) => LineFormatter.FormatGui(c, s, this, foreColor)); + } if (AutoScroll) ScrollToEnd(line); } - /// Appends the log. - /// The text to log. - public void AppendLog(string text) { - AppendLog(text, ForeColor, DateStamp); - } + /// Appends text to this textbox. + public void AppendLog(string text) { AppendLog(text, ForeColor, DateStamp); } - /// Appends the log. - /// The text to log. - /// Color of the foreground. - internal void Append(string text, Color foreColor) { + /// Appends text to this textbox. + internal void AppendLog(string text, Color color) { if (InvokeRequired) { - Invoke((MethodInvoker)(() => Append(text, foreColor))); return; + Invoke((MethodInvoker)(() => AppendLog(text, color))); return; } - + + int selLength = SelectionLength, selStart = 0; + if (selLength > 0) selStart = SelectionStart; + AppendColoredText(text, color); + + // preserve user's selection when appending text + if (selLength == 0) return; + SelectionStart = selStart; + SelectionLength = selLength; + } + + void AppendColoredText(string text, Color color) { SelectionStart = TextLength; SelectionLength = 0; - SelectionColor = foreColor; + + SelectionColor = color; AppendText(text); - SelectionColor = ForeColor; + SelectionColor = ForeColor; } - void ColoredReader_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e) { + void HandleLinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e) { if ( !e.LinkText.StartsWith("http://www.minecraft.net/classic/play/") ) { if ( MessageBox.Show("Never open links from people that you don't trust!", "Warning!!", MessageBoxButtons.OKCancel) == DialogResult.Cancel ) return; @@ -116,7 +118,6 @@ namespace MCGalaxy.Gui.Components { try { Process.Start(e.LinkText); } catch { } - } /// Scrolls to the end of the log @@ -125,7 +126,7 @@ namespace MCGalaxy.Gui.Components { Invoke((MethodInvoker)(() => ScrollToEnd(startIndex))); return; } - + int lines = GetLineFromCharIndex(TextLength - 1) - startIndex + 1; for (int i = 0; i < lines; i++) { SendMessage(Handle, 0xB5, (IntPtr)1, IntPtr.Zero); @@ -134,6 +135,6 @@ namespace MCGalaxy.Gui.Components { } [DllImport("user32.dll", CharSet = CharSet.Auto)] - public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); + static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); } } diff --git a/GUI/LineFormatter.cs b/GUI/LineFormatter.cs index 6448d47bf..c985186ab 100644 --- a/GUI/LineFormatter.cs +++ b/GUI/LineFormatter.cs @@ -93,7 +93,7 @@ namespace MCGalaxy.Gui { public static void FormatGui(char col, string message, ColoredTextBox box, Color foreCol) { - box.Append(message, GetGuiCol(col, foreCol)); + box.AppendLog(message, GetGuiCol(col, foreCol)); } static Color GetGuiCol(char c, Color foreCol) { diff --git a/GUI/Starter.csproj b/GUI/Starter.csproj index 23e5bf347..2daa9e12c 100644 --- a/GUI/Starter.csproj +++ b/GUI/Starter.csproj @@ -75,9 +75,6 @@ Component - - ColoredTextBox.cs - Form