Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion InfoBox.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 18
VisualStudioVersion = 18.2.11408.102 d18.0
VisualStudioVersion = 18.2.11408.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{11BF73EF-1C37-4B34-BB1B-E378D733E8DC}"
EndProject
Expand All @@ -14,6 +14,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InfoBoxCore.Designer", "Inf
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InfoBoxCore.Designer.Tests", "InfoBoxCore.Designer.Tests\InfoBoxCore.Designer.Tests.csproj", "{A597A632-9151-4FB3-8696-8830D4B32170}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InfoBoxCore.Tests", "InfoBoxCore.Tests\InfoBoxCore.Tests.csproj", "{B8F3C21D-7A42-4E91-9C5A-1F2D4E5B6C7D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InfoBoxCore.ManualTests", "InfoBoxCore.ManualTests\InfoBoxCore.ManualTests.csproj", "{C3CC7542-1BA9-4BC4-9D3E-18B0A2DCE537}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -40,6 +44,14 @@ Global
{A597A632-9151-4FB3-8696-8830D4B32170}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A597A632-9151-4FB3-8696-8830D4B32170}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A597A632-9151-4FB3-8696-8830D4B32170}.Release|Any CPU.Build.0 = Release|Any CPU
{B8F3C21D-7A42-4E91-9C5A-1F2D4E5B6C7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8F3C21D-7A42-4E91-9C5A-1F2D4E5B6C7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8F3C21D-7A42-4E91-9C5A-1F2D4E5B6C7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8F3C21D-7A42-4E91-9C5A-1F2D4E5B6C7D}.Release|Any CPU.Build.0 = Release|Any CPU
{C3CC7542-1BA9-4BC4-9D3E-18B0A2DCE537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3CC7542-1BA9-4BC4-9D3E-18B0A2DCE537}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3CC7542-1BA9-4BC4-9D3E-18B0A2DCE537}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3CC7542-1BA9-4BC4-9D3E-18B0A2DCE537}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
38 changes: 14 additions & 24 deletions InfoBox/Form/InformationBoxForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal partial class InformationBoxForm : Form
/// <summary>
/// Padding for the borders
/// </summary>
private const int BorderPadding = 20;
private const int BorderPadding = 10;

#endregion Consts

Expand Down Expand Up @@ -169,11 +169,6 @@ internal partial class InformationBoxForm : Form
/// </summary>
private IconType iconType = IconType.Internal;

/// <summary>
/// Contains the text
/// </summary>
private StringBuilder internalText;

/// <summary>
/// Contains if the help button should be displayed
/// </summary>
Expand Down Expand Up @@ -1253,36 +1248,37 @@ private void SetText()
Screen currentScreen = Screen.FromControl(this);
int screenWidth = currentScreen.WorkingArea.Width;

this.messageText.Text = this.messageText.Text.Replace("\r\n", "\n");
this.messageText.Text = this.messageText.Text.Replace("\n", Environment.NewLine);
var internalText = String.Empty;
internalText = TextHelper.NormalizeLineBreaks(this.messageText.Text);

if (this.autoSizeMode == InformationBoxAutoSizeMode.FitToText)
{
this.messageText.WordWrap = false;
this.messageText.Size = TextRenderer.MeasureText(this.messageText.Text, this.messageText.Font, currentScreen.WorkingArea.Size, TextFormatFlags.TextBoxControl) + new Size(1, 1);
this.messageText.Text = internalText.ToString();
this.messageText.Size = TextRenderer.MeasureText(internalText, this.messageText.Font, Size.Empty, TextFormatFlags.NoPadding);
}
else
{
if (this.autoSizeMode == InformationBoxAutoSizeMode.None)
{
this.messageText.WordWrap = true;
this.messageText.Size = TextRenderer.MeasureText(this.messageText.Text, this.messageText.Font, new Size(screenWidth / 2, 0), TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak);
this.messageText.Text = internalText.ToString();
this.messageText.Size = TextRenderer.MeasureText(internalText, this.messageText.Font, new Size(screenWidth / 2, 0), TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak);
}
else
{
this.internalText = new StringBuilder(this.messageText.Text);

if (this.autoSizeMode == InformationBoxAutoSizeMode.MinimumHeight)
{
// Remove line breaks.
this.internalText = this.internalText.Replace(Environment.NewLine, " ");
Regex splitter = new Regex(@"(?<sentence>.+?(\. |$))", RegexOptions.Compiled);
MatchCollection sentences = splitter.Matches(this.internalText.ToString());
internalText = TextHelper.ReplaceLineBreaksWithSpaces(internalText);
var sentences = TextHelper.SplitTextIntoSentences(internalText);

StringBuilder formattedText = new StringBuilder();
int currentWidth = 0;

foreach (Match sentence in sentences)
{
// FIX: In case an icon is configured, the maximum width of the text should be reduced to accomodate the icon width and avoid the horizontal scrollbar.
int sentenceLength = TextRenderer.MeasureText(sentence.Value, this.messageText.Font, Size.Empty, TextFormatFlags.TextBoxControl | TextFormatFlags.NoPadding).Width;
if (currentWidth != 0 && (sentenceLength + currentWidth) > (screenWidth - 50))
{
Expand All @@ -1294,20 +1290,14 @@ private void SetText()
formattedText.Append(sentence.Value);
}

this.internalText = formattedText;
internalText = formattedText.ToString();
}
else if (this.autoSizeMode == InformationBoxAutoSizeMode.MinimumWidth)
{
this.internalText.Replace(". ", "." + Environment.NewLine);
this.internalText.Replace("? ", "?" + Environment.NewLine);
this.internalText.Replace("! ", "!" + Environment.NewLine);
this.internalText.Replace(": ", ":" + Environment.NewLine);
this.internalText.Replace(") ", ")" + Environment.NewLine);
this.internalText.Replace(", ", "," + Environment.NewLine);
internalText = TextHelper.AddLineBreaksAfterPunctuation(internalText);
}

this.messageText.Text = this.internalText.ToString();

this.messageText.Text = internalText.ToString();
this.messageText.Size = TextRenderer.MeasureText(this.messageText.Text, this.messageText.Font, Size.Empty, TextFormatFlags.TextBoxControl);
}
}
Expand Down
1 change: 1 addition & 0 deletions InfoBox/InfoBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<Compile Include="Enums\AutoCloseDefinedParameters.cs" />
<Compile Include="Internals\IconHelper.cs" />
<Compile Include="Internals\MessageBoxEnumConverter.cs" />
<Compile Include="Internals\TextHelper.cs" />
<Compile Include="Parameters\AutoCloseParameters.cs" />
<Compile Include="Parameters\DesignParameters.cs" />
<Compile Include="Parameters\FontParameters.cs" />
Expand Down
3 changes: 1 addition & 2 deletions InfoBox/InformationBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
namespace InfoBox
{
using System.Drawing;
using System.Security.Permissions;
using System.Windows.Forms;

/// <summary>
/// Displays a message box that can contain text, buttons, and symbols that inform and instruct the user.
/// </summary>
#if !NET5_0_OR_GREATER
[UIPermission(SecurityAction.Demand)]
[System.Security.Permissions.UIPermission(System.Security.Permissions.SecurityAction.Demand)]
#endif
public static class InformationBox
{
Expand Down
76 changes: 76 additions & 0 deletions InfoBox/Internals/TextHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace InfoBox.Internals
{
/// <summary>
/// Contains methods to process the content of the InformationBox
/// </summary>
internal static class TextHelper
{
private static readonly Regex _regex;

/// <summary>
/// Initialises the static members of the <see cref="TextHelper"/> class.
/// </summary>
static TextHelper()
{
_regex = new Regex(@"(?<sentence>.+?(\. |$))", RegexOptions.Compiled, TimeSpan.FromSeconds(10));
}

/// <summary>
/// Returns a string containing the same content with all line breaks normalized as <see cref="System.Environment.NewLine"/>
/// </summary>
/// <param name="text">The text to normalize</param>
/// <returns>a string containing the same content with all line breaks normalized as <see cref="System.Environment.NewLine"/></returns>
public static string NormalizeLineBreaks(string text)
{
var builder = new StringBuilder(text);
builder.Replace("\r\n", "\n");
builder.Replace("\n", Environment.NewLine);
return builder.ToString();
}

/// <summary>
/// Returns a string containing the same content with all line breaks replaced with a single space per line break.
/// </summary>
/// <param name="text">The text to normalize</param>
/// <returns>a string containing the same content with all line breaks replaced with spaces.</returns>
public static string ReplaceLineBreaksWithSpaces(string text)
{
var builder = new StringBuilder(text);
builder.Replace(Environment.NewLine, " ");
return builder.ToString();
}

/// <summary>
/// Transform a text into a list of sentences.
/// </summary>
/// <param name="text">The text to split into sentences</param>
/// <returns>a <see cref="System.Text.RegularExpressions.MatchCollection"/> containing the list of sentences</returns>
public static MatchCollection SplitTextIntoSentences(string text)
{
return _regex.Matches(text);
}

/// <summary>
/// Adds a line break after most punctuation symbols
/// </summary>
/// <param name="text">the text to update</param>
/// <returns>a string containing the content with the additional line breaks.</returns>
public static string AddLineBreaksAfterPunctuation(string text)
{
var builder = new StringBuilder(text);

builder.Replace(". ", "." + Environment.NewLine);
builder.Replace("? ", "?" + Environment.NewLine);
builder.Replace("! ", "!" + Environment.NewLine);
builder.Replace(": ", ":" + Environment.NewLine);
builder.Replace(") ", ")" + Environment.NewLine);
builder.Replace(", ", "," + Environment.NewLine);

return builder.ToString();
}
}
}
4 changes: 4 additions & 0 deletions InfoBox/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#if NET6_0_OR_GREATER
Expand Down Expand Up @@ -36,6 +37,9 @@
// Assembly is CLS compliant
[assembly: CLSCompliant(true)]

// Make internal types visible to test assemblies
[assembly: InternalsVisibleTo("InfoBoxCore.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000d083a79d3b652c8e75ecc5b0eaf93f8160a1de04d6f967a87d4a7f6e2b5916017afef3cb81a9a6789079138170385c6e30dfdbb8b9999e08e29436e87bb10044b637e6c9cf0f6e52b64ba19001b5181839a5471dff368d415d29cbaae2189f89d7b5f736ef3e7692e257a35c0836ec97e5a2a950864617b8642590517bf8c9a")]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e14a6ac1-494d-481d-8510-d652082e43ba")]

Expand Down
15 changes: 15 additions & 0 deletions InfoBoxCore.ManualTests/InfoBoxCore.ManualTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\InfoBoxCore\InfoBoxCore.csproj" />
</ItemGroup>

</Project>
98 changes: 98 additions & 0 deletions InfoBoxCore.ManualTests/ManualTestsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading