gui: preserve user selection when appending to main log

This commit is contained in:
UnknownShadow200 2017-04-12 21:21:12 +10:00
parent 1bc31821e9
commit d62a7b4d61
4 changed files with 34 additions and 85 deletions

View File

@ -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 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
components = new System.ComponentModel.Container();
this.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(ColoredReader_LinkClicked);
}
#endregion
}
}

View File

@ -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
namespace MCGalaxy.Gui.Components {
/// <summary> A rich text box, that can parse Minecraft/MCGalaxy color codes. </summary>
/// <summary> Extended rich text box that auto-colors minecraft classic text. </summary>
public partial class ColoredTextBox : RichTextBox {
bool _nightMode = false, _colorize = true;
@ -65,50 +62,55 @@ namespace MCGalaxy.Gui.Components {
/// <summary> Initializes a new instance of the <see cref="ColoredTextBox"/> class. </summary>
public ColoredTextBox() : base() {
InitializeComponent();
LinkClicked += HandleLinkClicked;
}
/// <summary> Appends the log. </summary>
/// <param name="text">The text to log.</param>
/// <summary> Appends text to this textbox. </summary>
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;
}
} else {
LineFormatter.Format(text, (c, s) => LineFormatter.FormatGui(c, s, this, foreColor));
}
if (AutoScroll) ScrollToEnd(line);
}
/// <summary> Appends the log. </summary>
/// <param name="text">The text to log.</param>
public void AppendLog(string text) {
AppendLog(text, ForeColor, DateStamp);
}
/// <summary> Appends text to this textbox. </summary>
public void AppendLog(string text) { AppendLog(text, ForeColor, DateStamp); }
/// <summary> Appends the log. </summary>
/// <param name="text">The text to log.</param>
/// <param name="foreColor">Color of the foreground.</param>
internal void Append(string text, Color foreColor) {
/// <summary> Appends text to this textbox. </summary>
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;
}
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 { }
}
/// <summary> Scrolls to the end of the log </summary>
@ -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);
}
}

View File

@ -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) {

View File

@ -75,9 +75,6 @@
<Compile Include="ColoredTextBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ColoredTextBox.Designer.cs">
<DependentUpon>ColoredTextBox.cs</DependentUpon>
</Compile>
<Compile Include="Eco\EcoLevelWindow.cs">
<SubType>Form</SubType>
</Compile>