Skip to content

Commit

Permalink
Status icons now copy paste correctly as text
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman committed Dec 21, 2018
1 parent fc74c81 commit 943af61
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Compile Include="..\MySQLTuner\DataGridViewImageWithAltTextCell.cs">
<Link>DataGridViewImageWithAltTextCell.cs</Link>
</Compile>
<Compile Include="..\MySQLTuner\FormLogOn.cs">
<Link>FormLogOn.cs</Link>
<SubType>Form</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Compile Include="..\MySQLTuner\DataGridViewImageWithAltTextCell.cs">
<Link>DataGridViewImageWithAltTextCell.cs</Link>
</Compile>
<Compile Include="..\MySQLTuner\FormLogOn.cs">
<Link>FormLogOn.cs</Link>
<SubType>Form</SubType>
Expand Down
3 changes: 3 additions & 0 deletions MySQLTuner .Net 2.0/MySqlTuner .Net 2.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Compile Include="..\MySQLTuner\DataGridViewImageWithAltTextCell.cs">
<Link>DataGridViewImageWithAltTextCell.cs</Link>
</Compile>
<Compile Include="..\MySQLTuner\FormLogOn.cs">
<Link>FormLogOn.cs</Link>
<SubType>Form</SubType>
Expand Down
259 changes: 259 additions & 0 deletions MySQLTuner/DataGridViewImageWithAltTextCell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
// -----------------------------------------------------------------------
// <copyright file="DataGridViewImageWithAltTextCell.cs" company="Peter Chapman">
// Copyright 2018 Peter Chapman. See LICENCE.md for licence details.
// </copyright>
// -----------------------------------------------------------------------

namespace MySqlTuner
{
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;

/// <summary>
/// Provides alternate text for copying to the clipboard.
/// </summary>
/// <seealso cref="System.Windows.Forms.DataGridViewImageCell" />
public class DataGridViewImageWithAltTextCell : DataGridViewImageCell
{
/// <summary>
/// Gets or sets the alternate text.
/// </summary>
/// <value>
/// The alternate text.
/// </value>
public string AltText { get; set; }

/// <summary>
/// Retrieves the formatted value of the cell to copy to the <see cref="T:System.Windows.Forms.Clipboard" />.
/// </summary>
/// <param name="rowIndex">The zero-based index of the row containing the cell.</param>
/// <param name="firstCell"><see langword="true" /> to indicate that the cell is in the first column of the region defined by the selected cells; otherwise, <see langword="false" />.</param>
/// <param name="lastCell"><see langword="true" /> to indicate that the cell is the last column of the region defined by the selected cells; otherwise, <see langword="false" />.</param>
/// <param name="inFirstRow"><see langword="true" /> to indicate that the cell is in the first row of the region defined by the selected cells; otherwise, <see langword="false" />.</param>
/// <param name="inLastRow"><see langword="true" /> to indicate that the cell is in the last row of the region defined by the selected cells; otherwise, <see langword="false" />.</param>
/// <param name="format">The current format string of the cell.</param>
/// <returns>
/// An <see cref="T:System.Object" /> that represents the value of the cell to copy to the <see cref="T:System.Windows.Forms.Clipboard" />.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">rowIndex</exception>
protected override object GetClipboardContent(int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)
{
if (this.DataGridView == null)
{
return null;
}

// Header Cell classes override this implementation - this implementation is only for inner cells
if (rowIndex < 0 || rowIndex >= this.DataGridView.Rows.Count)
{
throw new ArgumentOutOfRangeException(nameof(rowIndex));
}

StringBuilder sb = new StringBuilder(64);
if (string.Equals(format, DataFormats.Html, StringComparison.OrdinalIgnoreCase))
{
if (firstCell)
{
if (inFirstRow)
{
sb.Append("<TABLE>");
}

sb.Append("<TR>");
}

sb.Append("<TD>");
if (!string.IsNullOrEmpty(this.AltText))
{
FormatPlainTextAsHtml(this.AltText, new StringWriter(sb, CultureInfo.CurrentCulture));
}
else
{
sb.Append("&nbsp;");
}

sb.Append("</TD>");
if (lastCell)
{
sb.Append("</TR>");
if (inLastRow)
{
sb.Append("</TABLE>");
}
}

return sb.ToString();
}
else
{
bool csv = string.Equals(format, DataFormats.CommaSeparatedValue, StringComparison.OrdinalIgnoreCase);
if (csv ||
string.Equals(format, DataFormats.Text, StringComparison.OrdinalIgnoreCase) ||
string.Equals(format, DataFormats.UnicodeText, StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(this.AltText))
{
if (firstCell && lastCell && inFirstRow && inLastRow)
{
sb.Append(this.AltText);
}
else
{
bool escapeApplied = false;
int insertionPoint = sb.Length;
FormatPlainText(this.AltText, csv, new StringWriter(sb, CultureInfo.CurrentCulture), ref escapeApplied);
if (escapeApplied)
{
sb.Insert(insertionPoint, '"');
}
}
}

if (lastCell)
{
if (!inLastRow)
{
sb.Append((char)Keys.Return);
sb.Append((char)Keys.LineFeed);
}
}
else
{
sb.Append(csv ? ',' : (char)Keys.Tab);
}

return sb.ToString();
}
else
{
return null;
}
}
}

/// <summary>
/// Formats the plain text.
/// </summary>
/// <param name="s">The text.</param>
/// <param name="csv">If set to <c>true</c> format as CSV.</param>
/// <param name="output">The output.</param>
/// <param name="escapeApplied">If set to <c>true</c> escape was applied.</param>
private static void FormatPlainText(string s, bool csv, TextWriter output, ref bool escapeApplied)
{
if (s == null)
{
return;
}

int cb = s.Length;
for (int i = 0; i < cb; i++)
{
char ch = s[i];
switch (ch)
{
case '"':
if (csv)
{
output.Write("\"\"");
escapeApplied = true;
}
else
{
output.Write('"');
}

break;
case ',':
if (csv)
{
escapeApplied = true;
}

output.Write(',');
break;
case '\t':
if (!csv)
{
output.Write(' ');
}
else
{
output.Write('\t');
}

break;
default:
output.Write(ch);
break;
}
}

if (escapeApplied)
{
output.Write('"'); // terminating double-quote.
// the caller is responsible for inserting the opening double-quote.
}
}

/// <summary>
/// Formats the plain text as HTML.
/// </summary>
/// <param name="s">The text.</param>
/// <param name="output">The output.</param>
private static void FormatPlainTextAsHtml(string s, TextWriter output)
{
if (s == null)
{
return;
}

int cb = s.Length;
char prevCh = '\0';

for (int i = 0; i < cb; i++)
{
char ch = s[i];
switch (ch)
{
case '<':
output.Write("&lt;");
break;
case '>':
output.Write("&gt;");
break;
case '"':
output.Write("&quot;");
break;
case '&':
output.Write("&amp;");
break;
case ' ':
if (prevCh == ' ')
{
output.Write("&nbsp;");
}
else
{
output.Write(ch);
}

break;
case '\r':
// Ignore \r, only handle \n
break;
case '\n':
output.Write("<br>");
break;
default:
output.Write(ch);
break;
}

prevCh = ch;
}
}
}
}
14 changes: 9 additions & 5 deletions MySQLTuner/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,26 @@ public override void PrintMessage(Status messageStatus, string message)
// Setup the cells and add the row
using (DataGridViewRow row = new DataGridViewRow())
{
using (DataGridViewCell statusCell = new DataGridViewImageCell())
using (DataGridViewImageWithAltTextCell statusCell = new DataGridViewImageWithAltTextCell())
{
switch (messageStatus)
{
case Status.Pass:
statusCell.Value = (Image)Properties.Resources.Pass;
statusCell.AltText = "Pass";
statusCell.Value = Properties.Resources.Pass;
break;
case Status.Fail:
statusCell.Value = (Image)Properties.Resources.Fail;
statusCell.AltText = "Fail";
statusCell.Value = Properties.Resources.Fail;
break;
case Status.Info:
default:
statusCell.Value = (Image)Properties.Resources.Info;
statusCell.AltText = "Info";
statusCell.Value = Properties.Resources.Info;
break;
case Status.Recommendation:
statusCell.Value = (Image)Properties.Resources.Recommendation;
statusCell.AltText = "Recommendation";
statusCell.Value = Properties.Resources.Recommendation;
break;
}

Expand Down
1 change: 1 addition & 0 deletions MySQLTuner/MySqlTuner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataGridViewImageWithAltTextCell.cs" />
<Compile Include="FormLogOn.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
8 changes: 4 additions & 4 deletions MySQLTuner/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// -----------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="Peter Chapman">
// Copyright 2018 Peter Chapman. See LICENCE.md for licence details.ls.
// Copyright 2018 Peter Chapman. See LICENCE.md for licence details.
// </copyright>
// -----------------------------------------------------------------------

Expand Down Expand Up @@ -39,7 +39,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.8.3.0")]
[assembly: AssemblyFileVersion("0.8.3.0")]
[assembly: AssemblyVersion("0.8.4.0")]
[assembly: AssemblyFileVersion("0.8.4.0")]
[assembly: CLSCompliant(true)]
[assembly: NeutralResourcesLanguageAttribute("en-NZ")]
[assembly: NeutralResourcesLanguage("en-NZ")]
8 changes: 4 additions & 4 deletions MySqlTuner Test Cases/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MySQL Tuner Test Cases")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Database Optimiser")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Peter Chapman")]
[assembly: AssemblyProduct("MySQL Tuner Test Cases")]
Expand All @@ -38,7 +38,7 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.8.4.0")]
[assembly: AssemblyFileVersion("0.8.4.0")]
[assembly: CLSCompliant(true)]
[assembly: NeutralResourcesLanguageAttribute("en-NZ")]
[assembly: NeutralResourcesLanguage("en-NZ")]

0 comments on commit 943af61

Please sign in to comment.