Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1888 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
6 changed files with 172 additions and 2 deletions
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using ICSharpCode.Core; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Text; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Gui |
||||
{ |
||||
/// <summary>
|
||||
/// A ColorDialog that remembers any custom colors defined.
|
||||
/// </summary>
|
||||
public class SharpDevelopColorDialog : ColorDialog |
||||
{ |
||||
const string CustomColorsPropertyName = "SharpDevelopColorDialog.CustomColors"; |
||||
|
||||
public SharpDevelopColorDialog() |
||||
{ |
||||
LoadCustomColors(); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Converts a string of colors separated by the '|' character
|
||||
/// into an array of colors.
|
||||
/// </summary>
|
||||
public static int[] CustomColorsFromString(string s) |
||||
{ |
||||
if (String.IsNullOrEmpty(s)) { |
||||
return null; |
||||
} |
||||
|
||||
string[] items = s.Split('|'); |
||||
List<int> colors = new List<int>(); |
||||
foreach (string item in items) { |
||||
int color; |
||||
if (Int32.TryParse(item, out color)) { |
||||
colors.Add(color); |
||||
} |
||||
} |
||||
return colors.ToArray(); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Converts an integer array of colors into a string.
|
||||
/// </summary>
|
||||
public static string CustomColorsToString(int[] colors) |
||||
{ |
||||
if (colors == null) { |
||||
return String.Empty; |
||||
} |
||||
|
||||
StringBuilder s = new StringBuilder(); |
||||
for (int i = 0; i < colors.Length; ++i) { |
||||
if (i != 0) { |
||||
s.Append('|'); |
||||
} |
||||
s.Append(colors[i]); |
||||
} |
||||
return s.ToString(); |
||||
} |
||||
|
||||
protected override bool RunDialog(IntPtr hwndOwner) |
||||
{ |
||||
bool result = base.RunDialog(hwndOwner); |
||||
SaveCustomColors(); |
||||
return result; |
||||
} |
||||
|
||||
void LoadCustomColors() |
||||
{ |
||||
CustomColors = CustomColorsFromString(PropertyService.Get(CustomColorsPropertyName)); |
||||
} |
||||
|
||||
void SaveCustomColors() |
||||
{ |
||||
PropertyService.Set(CustomColorsPropertyName, CustomColorsToString(CustomColors)); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using ICSharpCode.SharpDevelop.Gui; |
||||
using NUnit.Framework; |
||||
using System; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Tests |
||||
{ |
||||
[TestFixture] |
||||
public class SharpDevelopColorDialogTests |
||||
{ |
||||
[Test] |
||||
public void NullString() |
||||
{ |
||||
Assert.IsNull(SharpDevelopColorDialog.CustomColorsFromString(null)); |
||||
} |
||||
|
||||
[Test] |
||||
public void EmptyString() |
||||
{ |
||||
Assert.IsNull(SharpDevelopColorDialog.CustomColorsFromString(String.Empty)); |
||||
} |
||||
|
||||
[Test] |
||||
public void OneColorInString() |
||||
{ |
||||
int[] colors = SharpDevelopColorDialog.CustomColorsFromString("34"); |
||||
Assert.AreEqual(1, colors.Length); |
||||
Assert.AreEqual(34, colors[0]); |
||||
} |
||||
|
||||
[Test] |
||||
public void TwoColorsInString() |
||||
{ |
||||
int[] colors = SharpDevelopColorDialog.CustomColorsFromString("20|30"); |
||||
Assert.AreEqual(2, colors.Length); |
||||
Assert.AreEqual(20, colors[0]); |
||||
Assert.AreEqual(30, colors[1]); |
||||
} |
||||
|
||||
[Test] |
||||
public void SecondColorIsInvalid() |
||||
{ |
||||
int[] colors = SharpDevelopColorDialog.CustomColorsFromString("20|Test"); |
||||
Assert.AreEqual(1, colors.Length); |
||||
Assert.AreEqual(20, colors[0]); |
||||
} |
||||
|
||||
[Test] |
||||
public void FirstColorIsInvalid() |
||||
{ |
||||
int[] colors = SharpDevelopColorDialog.CustomColorsFromString("Test|20"); |
||||
Assert.AreEqual(1, colors.Length); |
||||
Assert.AreEqual(20, colors[0]); |
||||
} |
||||
|
||||
[Test] |
||||
public void NullIntColorsArray() |
||||
{ |
||||
Assert.AreEqual(String.Empty, SharpDevelopColorDialog.CustomColorsToString(null)); |
||||
} |
||||
|
||||
[Test] |
||||
public void OneCustomColor() |
||||
{ |
||||
int[] colors = new int[] { 10 }; |
||||
Assert.AreEqual("10", SharpDevelopColorDialog.CustomColorsToString(colors)); |
||||
} |
||||
|
||||
[Test] |
||||
public void TwoCustomColors() |
||||
{ |
||||
int[] colors = new int[] { 10, 20 }; |
||||
Assert.AreEqual("10|20", SharpDevelopColorDialog.CustomColorsToString(colors)); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue