Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dialogboxes_colorselectbox.cs

(view source code of dialogboxes_colorselectbox.cs as plain text)

  1. using System.Linq;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	[System.Runtime.InteropServices.Guid( "ACCB429D-7E28-4D60-8B80-44CCE5E7D966" )]
  7. 	public interface ColorSelectBox_Interface
  8. 	{
  9. 		[System.Runtime.InteropServices.DispId( 1 )]
  10. 		void CheckUpdate( );
  11.  
  12. 		[System.Runtime.InteropServices.DispId( 2 )]
  13. 		string Credits( );
  14.  
  15. 		[System.Runtime.InteropServices.DispId( 3 )]
  16. 		string Help( int html = 0 );
  17.  
  18. 		[System.Runtime.InteropServices.DispId( 4 )]
  19. 		string ListConsoleColors( );
  20.  
  21. 		[System.Runtime.InteropServices.DispId( 5 )]
  22. 		string ListKnownColors( );
  23.  
  24. 		[System.Runtime.InteropServices.DispId( 6 )]
  25. 		string ListProperties( );
  26.  
  27. 		[System.Runtime.InteropServices.DispId( 7 )]
  28. 		string SampleCode( );
  29.  
  30. 		[System.Runtime.InteropServices.DispId( 8 )]
  31. 		void Show( );
  32. 	}
  33.  
  34.  
  35. 	[System.Runtime.InteropServices.Guid( "0896FD0D-B25E-45DF-898C-7FA60C84C06D" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
  36. 	public interface ColorSelectBox_Events
  37. 	{
  38. 	}
  39.  
  40.  
  41. 	[System.Runtime.InteropServices.Guid( "AA6AA980-FDE6-4233-95FD-A4A4A4F3B691" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( ColorSelectBox_Events ) )]
  42. 	public class ColorSelectBox : ColorSelectBox_Interface
  43. 	{
  44. 		#region Default values
  45.  
  46. 		static readonly System.Drawing.Color defaultcolor = System.Drawing.Color.Black;
  47. 		static string defaulttitle = "Pick a Console Color";
  48.  
  49. 		#endregion Default values
  50.  
  51.  
  52. 		#region Controls
  53.  
  54. 		static System.Windows.Forms.Form colordialog;
  55. 		static System.Drawing.Size dialogsize = new System.Drawing.Size( 230, 110 );
  56. 		static System.Timers.Timer timer;
  57.  
  58. 		#endregion Controls
  59.  
  60.  
  61. 		#region Methods
  62.  
  63. 		public void CheckUpdate( )
  64. 		{
  65. 			Global.Common.ProgramInfo.CheckUpdate( );
  66. 		}
  67.  
  68.  
  69. 		static int[] Color2RGB( int color )
  70. 		{
  71. 			int red = System.Convert.ToInt32( System.Math.Floor( ( (decimal) color ) / 65536 ) );
  72. 			int green = System.Convert.ToInt32( System.Math.Floor( ( ( (decimal) color ) - red * 65536 ) / 256 ) );
  73. 			int blue = color - red * 65536 - green * 256;
  74. 			return new int[3] { red, green, blue };
  75. 		}
  76.  
  77.  
  78. 		static string ColorName( System.Drawing.Color color, bool returnallnames = false )
  79. 		{
  80. 			System.Collections.Generic.List<string> list;
  81. 			if ( _consolecolorsonly )
  82. 			{
  83. 				list = Global.Common.Lists.ConsoleColors( );
  84. 			}
  85. 			else
  86. 			{
  87. 				list = Global.Common.Lists.KnownColors( );
  88. 			}
  89. 			uint rgb = (uint) color.ToArgb( ) % 0x01000000;
  90. 			System.Globalization.NumberStyles hex = System.Globalization.NumberStyles.HexNumber;
  91. 			var queryobject = list.Where( c => uint.Parse( c.Split( '=' )[1].Split( "xX".ToCharArray( ) )[1], hex ).Equals( rgb ) );
  92. 			if ( queryobject.Count( ) == 0 )
  93. 			{
  94. 				return color.Name;
  95. 			}
  96. 			else
  97. 			{
  98. 				if ( returnallnames )
  99. 				{
  100. 					return string.Join( ";", queryobject.Select( c => c.Split( '=' )[0] ).ToArray( ) );
  101. 				}
  102. 				else
  103. 				{
  104. 					return queryobject.First( ).Split( '=' )[0];
  105. 				}
  106. 			}
  107. 		}
  108.  
  109.  
  110. 		public string Credits( )
  111. 		{
  112. 			return Global.Common.Credits( );
  113. 		}
  114.  
  115.  
  116. 		static void GetSelectedColor( )
  117. 		{
  118. 			foreach ( System.Windows.Forms.Control colorbox in colordialog.Controls )
  119. 			{
  120. 				if ( colorbox.GetType( ) == typeof( System.Windows.Forms.TextBox ) )
  121. 				{
  122. 					// If the box has focus, it is the one that was clicked
  123. 					if ( colorbox.Focused )
  124. 					{
  125. 						_selectedcolor = colorbox.BackColor;
  126. 						colordialog.Close( );
  127. 					}
  128. 				}
  129. 			}
  130. 		}
  131.  
  132.  
  133. 		public string Help( int html = 0 )
  134. 		{
  135. 			int col1width = 0;
  136. 			int col2width = 0;
  137. 			int col3width = 0;
  138. 			int col4width = 0;
  139. 			int col5width = 0;
  140. 			string help = string.Empty;
  141. 			string linetemplate = string.Empty;
  142. 			string separatorline = string.Empty;
  143.  
  144. 			if ( html == 1 )
  145. 			{
  146. 				help += "<h1>Help for ColorSelectBox class</h1>\n\n";
  147. 				help += "<p>Present a color picker dialog, and return the selected color</p>\n\n";
  148. 				help += "<h2>COM ProgID: RobvanderWoude.ColorSelectBox</h2>\n\n";
  149. 			}
  150. 			else
  151. 			{
  152. 				help += "Help for ColorSelectBox class\n";
  153. 				help += new string( '\u2500', help.Length ) + "\n\n";
  154. 				help += "Present a color picker dialog, and return the selected color\n\n\n";
  155. 				help += "COM ProgID: RobvanderWoude.ColorSelectBox\n\n\n";
  156. 			}
  157.  
  158.  
  159. 			#region Properties
  160.  
  161. 			if ( html == 1 )
  162. 			{
  163. 				separatorline = string.Empty;
  164. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n\t<td>{3}</td>\n\t<td>{4}</td>\n</tr>\n";
  165.  
  166. 				help += "<table>\n";
  167. 				help += "<tr>\n";
  168. 				help += "\t<th colspan=\"5\">Properties (Note that all properties are string or integer, no booleans, objects or arrays)</th>\n";
  169. 				help += "</tr>\n";
  170.  
  171. 				help += "<tr>\n";
  172. 				help += "\t<th>Property Name</th>\n";
  173. 				help += "\t<th>Description</th>\n";
  174. 				help += "\t<th>Mandatory</th>\n";
  175. 				help += "\t<th>Read-Only</th>\n";
  176. 				help += "\t<th>Default (Allowed) Values</th>\n";
  177. 				help += "</tr>\n";
  178. 			}
  179. 			else
  180. 			{
  181. 				col1width = 18;
  182. 				col2width = 50;
  183. 				col3width = 9;
  184. 				col4width = 9;
  185. 				col5width = 42;
  186.  
  187. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u253C" + new string( '\u2500', 2 + col4width ) + "\u253C" + new string( '\u2500', 2 + col5width ) + "\u2524\n";
  188. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n";
  189.  
  190. 				help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n";
  191. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  192. 				help += string.Format( "\u2502 {0,-" + ( 12 + col1width + col2width + col3width + col4width + col5width ).ToString( ) + "} \u2502\n", "Properties        (Note that all properties are string or integer, no booleans, objects or arrays)" );
  193. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  194. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2564" + new string( '\u2550', 2 + col4width ) + "\u2564" + new string( '\u2550', 2 + col5width ) + "\u2561\n";
  195. 				help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" );
  196. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u256A" + new string( '\u2550', 2 + col4width ) + "\u256A" + new string( '\u2550', 2 + col5width ) + "\u2561\n";
  197. 			}
  198.  
  199. 			help += HelpTableRow( linetemplate, "allnames", "Return a semi-colon list of all names matching the selected RGB value, instead of the first match only (0=false; 1=true)", "no", "no", "0", html );
  200. 			help += separatorline;
  201.  
  202. 			help += HelpTableRow( linetemplate, "allowcustomcolors", "Enable custom colors in dialog   (0=false; 1=true)\n(ignored if consolecolorsonly equals 1)", "no", "no", "1: allow", html );
  203. 			help += separatorline;
  204.  
  205. 			help += HelpTableRow( linetemplate, "consolecolorsonly", "Standard 16 console colors only  (0=false; 1=true)", "no", "no", "1", html );
  206. 			help += separatorline;
  207.  
  208. 			help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
  209. 			help += separatorline;
  210.  
  211. 			help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
  212. 			help += separatorline;
  213.  
  214. 			help += HelpTableRow( linetemplate, "left", Global.Common.Help.left + "\n(ignored if consolecolorsonly equals 0)", "no", "no", "Centered (0..screenwidth - dialogwidth)", html );
  215. 			help += separatorline;
  216.  
  217. 			help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal + "\n(ignored if consolecolorsonly equals 0)", "no", "no", "1: always on top", html );
  218. 			help += separatorline;
  219.  
  220. 			help += HelpTableRow( linetemplate, "selectedcolorB", "Blue component of selected color", "no", "no", "0 (0..255)", html );
  221. 			help += separatorline;
  222.  
  223. 			help += HelpTableRow( linetemplate, "selectedcolorG", "Green component of selected color", "no", "no", "0 (0..255)", html );
  224. 			help += separatorline;
  225.  
  226. 			help += HelpTableRow( linetemplate, "selectedcolorname", "Name of selected color", "no", "no", defaultcolor.Name, html );
  227. 			help += separatorline;
  228.  
  229. 			help += HelpTableRow( linetemplate, "selectedcolorR", "Red component of selected color", "no", "no", "0 (0..255)", html );
  230. 			help += separatorline;
  231.  
  232. 			help += HelpTableRow( linetemplate, "selectedcolorRGB", "RGB values of selected color", "no", "no", "0,0,0", html );
  233. 			help += separatorline;
  234.  
  235. 			help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout + "\n(ignored if consolecolorsonly equals 0)", "no", "no", "0: no timeout", html );
  236. 			help += separatorline;
  237.  
  238. 			help += HelpTableRow( linetemplate, "timeoutelapsed", Global.Common.Help.timeoutelapsed, "N/A", "YES", "", html );
  239. 			help += separatorline;
  240.  
  241. 			help += HelpTableRow( linetemplate, "title", Global.Common.Help.title + "\n(ignored if consolecolorsonly equals 0)", "no", "no", defaulttitle, html );
  242. 			help += separatorline;
  243.  
  244. 			help += HelpTableRow( linetemplate, "top", Global.Common.Help.top + "\n(ignored if consolecolorsonly equals 0)", "no", "no", "Centered (0..screenheight - dialogheight)", html );
  245.  
  246. 			if ( html == 1 )
  247. 			{
  248. 				help += "</table>\n\n\n";
  249. 			}
  250. 			else
  251. 			{
  252. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2534" + new string( '\u2500', 2 + col4width ) + "\u2534" + new string( '\u2500', 2 + col5width ) + "\u2518\n\n\n";
  253. 			}
  254.  
  255. 			#endregion Properties
  256.  
  257.  
  258. 			#region Methods
  259.  
  260. 			if ( html == 1 )
  261. 			{
  262. 				separatorline = string.Empty;
  263. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  264.  
  265. 				help += "<table>\n";
  266. 				help += "<tr>\n";
  267. 				help += "\t<th colspan=\"3\">Methods</th>\n";
  268. 				help += "</tr>\n";
  269.  
  270. 				help += "<tr>\n";
  271. 				help += "\t<th>Method Name</th>\n";
  272. 				help += "\t<th>Description</th>\n";
  273. 				help += "\t<th>Requirements</th>\n";
  274. 				help += "</tr>\n";
  275. 			}
  276. 			else
  277. 			{
  278. 				col1width = 18;
  279. 				col2width = 74;
  280. 				col3width = 42;
  281.  
  282. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n";
  283. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n";
  284.  
  285. 				help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n";
  286. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  287. 				help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" );
  288. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  289. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  290. 				help += string.Format( linetemplate, "Method Name", "Description", "Requirements" );
  291. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  292. 			}
  293.  
  294.  
  295. 			help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html );
  296. 			help += separatorline;
  297.  
  298. 			help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html );
  299. 			help += separatorline;
  300.  
  301. 			help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html );
  302. 			help += separatorline;
  303.  
  304. 			help += HelpTableRow( linetemplate, "ListConSoleColors", "Returns a semicolon-separated list of console color name=value pairs", "", html );
  305. 			help += separatorline;
  306.  
  307. 			help += HelpTableRow( linetemplate, "ListKnownColors", "Returns a semicolon-separated list of known color name=value pairs", "", html );
  308. 			help += separatorline;
  309.  
  310. 			help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html );
  311. 			help += separatorline;
  312.  
  313. 			help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html );
  314. 			help += separatorline;
  315.  
  316. 			help += HelpTableRow( linetemplate, "Show", "Presents a color picker dialog based on the current property values, and if \"OK\" is clicked, saves the selected color name in the \"selectedcolorname\" property, and the RGB color values in the \"selectedcolorRGB\", \"selectedcolorR\", \"selectedcolorG\" and selectedcolorB\" properties (if \"Cancel\" is clicked, the values for the default color Black are returned).", "", html );
  317.  
  318. 			if ( html == 1 )
  319. 			{
  320. 				help += "</table>\n\n\n";
  321. 			}
  322. 			else
  323. 			{
  324. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n";
  325. 			}
  326.  
  327. 			#endregion Methods
  328.  
  329.  
  330. 			#region Notes
  331.  
  332. 			if ( html == 1 )
  333. 			{
  334. 				help += "<table>\n<tr>\n\t<th>";
  335. 			}
  336.  
  337. 			help += "Notes:";
  338.  
  339. 			if ( html == 1 )
  340. 			{
  341. 				help += "</th>\n\t<td>";
  342. 			}
  343. 			else
  344. 			{
  345. 				help += "\n\t";
  346. 			}
  347.  
  348. 			help += "ColorSelectBox actually uses 2 different color picker dialogs:";
  349.  
  350. 			if ( html == 1 )
  351. 			{
  352. 				help += "<br />\n\t\t";
  353. 			}
  354. 			else
  355. 			{
  356. 				help += "\n\t";
  357. 			}
  358.  
  359. 			help += "if \"consolecolorsonly\" equals 0 (false), Windows' native color dialog is used, and you have a choice to allow custom colors or not;";
  360.  
  361. 			if ( html == 1 )
  362. 			{
  363. 				help += "<br />\n\t\t";
  364. 			}
  365. 			else
  366. 			{
  367. 				help += "\n\t";
  368. 			}
  369.  
  370. 			help += "if \"consolecolorsonly\" equals 1 (true), a custom, minimalistic dialog is used, with only the 16 standard console colors and nothing";
  371.  
  372. 			if ( html == 1 )
  373. 			{
  374. 				help += " ";
  375. 			}
  376. 			else
  377. 			{
  378. 				help += "\n\t";
  379. 			}
  380.  
  381. 			help += "else, not even \"OK\" and \"Cancel\" buttons, but it does come with a timeout option.";
  382.  
  383. 			if ( html == 1 )
  384. 			{
  385. 				help += "</td>\n</tr>\n</table>";
  386. 			}
  387.  
  388. 			help += "\n\n\n";
  389.  
  390. 			#endregion Notes
  391.  
  392.  
  393. 			#region Example
  394.  
  395. 			if ( html == 1 )
  396. 			{
  397. 				help += "<h2>VBScript usage example:</h2>\n\n<pre>";
  398. 			}
  399. 			else
  400. 			{
  401. 				help += "VBScript usage example:\n";
  402. 				help += new string( '\u2500', 23 ) + "\n\n";
  403. 			}
  404.  
  405. 			help += SampleCode( );
  406.  
  407. 			if ( html == 1 )
  408. 			{
  409. 				help += "</pre>";
  410. 			}
  411.  
  412. 			help += "\n\n";
  413.  
  414. 			#endregion Example
  415.  
  416.  
  417. 			help = help.Replace( "\n", System.Environment.NewLine );
  418. 			return help;
  419. 		}
  420.  
  421.  
  422. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html )
  423. 		{
  424. 			if ( html == 1 )
  425. 			{
  426. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text );
  427. 			}
  428. 			else
  429. 			{
  430. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" );
  431. 			}
  432. 		}
  433.  
  434.  
  435. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html )
  436. 		{
  437. 			if ( html == 1 )
  438. 			{
  439. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text );
  440. 			}
  441. 			else
  442. 			{
  443. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text );
  444. 			}
  445. 		}
  446.  
  447.  
  448. 		public string ListConsoleColors( )
  449. 		{
  450. 			return string.Join( ";", Global.Common.Lists.ConsoleColors( ) );
  451. 		}
  452.  
  453.  
  454. 		public string ListKnownColors( )
  455. 		{
  456. 			return string.Join( ";", Global.Common.Lists.KnownColors( ) );
  457. 		}
  458.  
  459.  
  460. 		public string ListProperties( )
  461. 		{
  462. 			return Global.Common.Lists.Properties( this );
  463. 		}
  464.  
  465.  
  466. 		public string SampleCode( )
  467. 		{
  468. 			string code = "Set objColorSelectBox = CreateObject( \"RobvanderWoude.ColorSelectBox\" )\n\n";
  469. 			code += "With objColorSelectBox\n";
  470. 			code += "\t.allowcustomcolors = 1\n";
  471. 			code += "\t.consolecolorsonly = 0\n";
  472. 			code += "\t.Show\n";
  473. 			code += "\tWScript.Echo .ListProperties( )\n";
  474. 			code += "\tWScript.Echo\n";
  475. 			code += "\t.consolecolorsonly = 1\n";
  476. 			code += "\t.Show\n";
  477. 			code += "\tWScript.Echo .ListProperties( )\n";
  478. 			code += "End With\n\n";
  479. 			code += "Set objColorSelectBox = Nothing";
  480. 			return code;
  481. 		}
  482.  
  483.  
  484. 		public void Show( )
  485. 		{
  486. 			_debuginfo.Add( timestamp + "consolecolorsonly = " + consolecolorsonly );
  487. 			_debuginfo.Add( timestamp + "_consolecolorsonly = " + _consolecolorsonly );
  488. 			if ( _consolecolorsonly )
  489. 			{
  490. 				_debuginfo.Add( timestamp + "Starting ShowConsoleColorsOnly( )" );
  491. 				ShowConsoleColorsOnly( );
  492. 			}
  493. 			else
  494. 			{
  495. 				_debuginfo.Add( timestamp + "Starting ShowAllColors( )" );
  496. 				ShowAllColors( );
  497. 			}
  498. 		}
  499.  
  500.  
  501. 		private void ShowAllColors( )
  502. 		{
  503. 			System.Windows.Forms.ColorDialog allcolordialog = new System.Windows.Forms.ColorDialog
  504. 			{
  505. 				AllowFullOpen = _allowcustomcolors,
  506. 				AnyColor = true,
  507. 				FullOpen = false,
  508. 				SolidColorOnly = !_allowcustomcolors
  509. 			};
  510. 			System.Windows.Forms.DialogResult result = allcolordialog.ShowDialog( );
  511. 			_debuginfo.Add( timestamp + "AllColors dialog button clicked: " + result.ToString( ) );
  512. 			if ( result == System.Windows.Forms.DialogResult.OK )
  513. 			{
  514. 				_selectedcolor = allcolordialog.Color;
  515. 			}
  516. 		}
  517.  
  518.  
  519. 		private void ShowConsoleColorsOnly( )
  520. 		{
  521. 			#region Timer
  522.  
  523. 			_timeoutelapsed = false;
  524. 			if ( timeout > 0 )
  525. 			{
  526. 				timer = new System.Timers.Timer( );
  527. 				timer.Elapsed += new System.Timers.ElapsedEventHandler( ConsoleColorSelectBox_Timer_Elapsed );
  528. 				timer.Interval = timeout * 1000;
  529. 				timer.Start( );
  530. 				_debuginfo.Add( timestamp + "Timer started with interval " + timeout.ToString( ) );
  531. 			}
  532.  
  533. 			#endregion Timer
  534.  
  535.  
  536. 			// Define dialog window
  537. 			colordialog = new System.Windows.Forms.Form( );
  538. 			colordialog.KeyUp += ConsoleColorSelectBox_KeyUp;
  539. 			colordialog.MaximizeBox = false;
  540. 			colordialog.MinimizeBox = false;
  541. 			colordialog.ShowInTaskbar = true;
  542. 			colordialog.Size = new System.Drawing.Size( 230, 110 );
  543. 			if ( top < 0 || left < 0 )
  544. 			{
  545. 				colordialog.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  546. 			}
  547. 			else
  548. 			{
  549. 				colordialog.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  550. 				colordialog.Location = new System.Drawing.Point( left, top );
  551. 			}
  552. 			colordialog.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  553. 			colordialog.Text = title;
  554. 			colordialog.TopMost = _modal;
  555. 			int[] cols = new int[8];
  556. 			int[] rows = new int[2];
  557. 			// Fill dialog with color boxes
  558. 			for ( int col = 0; col < cols.Length; col++ )
  559. 			{
  560. 				for ( int row = 0; row < rows.Length; row++ )
  561. 				{
  562. 					System.Windows.Forms.TextBox colorbox = new System.Windows.Forms.TextBox( );
  563. 					colorbox.Size = new System.Drawing.Size( 20, 20 );
  564. 					colorbox.Location = new System.Drawing.Point( 10 + col * 25, 10 + row * 30 );
  565. 					int[] rgb = Color2RGB( Global.Common.consolecolors[8 * row + col] );
  566. 					colorbox.BackColor = System.Drawing.Color.FromArgb( rgb[0], rgb[1], rgb[2] );
  567. 					colorbox.Click += ConsoleColorSelectBox_Colorbox_Click;
  568. 					colorbox.KeyUp += ConsoleColorSelectBox_KeyUp;
  569. 					colordialog.Controls.Add( colorbox );
  570. 				}
  571. 			}
  572. 			// Make sure the cursor won't be visible in one of the color boxes
  573. 			colordialog.Select( );
  574. 			// Show the dialog window
  575. 			colordialog.ShowDialog( );
  576. 		}
  577.  
  578. 		#endregion Methods
  579.  
  580.  
  581. 		#region Properties
  582.  
  583. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  584. 		private bool _allnames = false;
  585. 		[System.Runtime.InteropServices.ComVisible( true )]
  586. 		public int allnames
  587. 		{
  588. 			get
  589. 			{
  590. 				return ( _allnames ? 1 : 0 );
  591. 			}
  592. 			set
  593. 			{
  594. 				_allnames = ( value == 1 );
  595. 			}
  596. 		}
  597.  
  598.  
  599. 		// Available only for system colorpicker (_consolecolorsonly = false)
  600. 		private bool _allowcustomcolors = true;
  601. 		[System.Runtime.InteropServices.ComVisible( true )]
  602. 		public int allowcustomcolors
  603. 		{
  604. 			get
  605. 			{
  606. 				return ( _allowcustomcolors ? 1 : 0 );
  607. 			}
  608. 			set
  609. 			{
  610. 				_allowcustomcolors = ( value == 1 );
  611. 			}
  612. 		}
  613.  
  614.  
  615. 		// Choose between system colopicker (false) or customized one (true)
  616. 		private static bool _consolecolorsonly = true;
  617. 		[System.Runtime.InteropServices.ComVisible( true )]
  618. 		public int consolecolorsonly
  619. 		{
  620. 			get
  621. 			{
  622. 				return ( _consolecolorsonly ? 1 : 0 );
  623. 			}
  624. 			set
  625. 			{
  626. 				_consolecolorsonly = ( value == 1 );
  627. 			}
  628. 		}
  629.  
  630.  
  631. 		private System.Collections.Generic.List<string> _debuginfo = new System.Collections.Generic.List<string>( );
  632. 		[System.Runtime.InteropServices.ComVisible( true )]
  633. 		public string debuginfo
  634. 		{
  635. 			get
  636. 			{
  637. 				return string.Join( "\n", _debuginfo.ToArray( ) );
  638. 			}
  639. 		}
  640.  
  641.  
  642. 		private System.Collections.Generic.List<string> _errors = new System.Collections.Generic.List<string>( );
  643. 		[System.Runtime.InteropServices.ComVisible( true )]
  644. 		public string errors
  645. 		{
  646. 			get
  647. 			{
  648. 				return string.Join( "\n", _errors.ToArray( ) );
  649. 			}
  650. 		}
  651.  
  652.  
  653. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  654. 		private int _left = -1;
  655. 		[System.Runtime.InteropServices.ComVisible( true )]
  656. 		public int left
  657. 		{
  658. 			get
  659. 			{
  660. 				return _left;
  661. 			}
  662. 			set
  663. 			{
  664. 				if ( value < 0 )
  665. 				{
  666. 					_left = 0;
  667. 				}
  668. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - dialogsize.Width )
  669. 				{
  670. 					_left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - dialogsize.Width;
  671. 				}
  672. 				else
  673. 				{
  674. 					_left = value;
  675. 				}
  676. 			}
  677. 		}
  678.  
  679.  
  680. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  681. 		private bool _modal = true;
  682. 		[System.Runtime.InteropServices.ComVisible( true )]
  683. 		public int modal
  684. 		{
  685. 			get
  686. 			{
  687. 				return ( _modal ? 1 : 0 );
  688. 			}
  689. 			set
  690. 			{
  691. 				_modal = ( value == 1 );
  692. 			}
  693. 		}
  694.  
  695.  
  696. 		private static System.Drawing.Color _selectedcolor = defaultcolor;
  697. 		[System.Runtime.InteropServices.ComVisible( true )]
  698. 		public string selectedcolorname
  699. 		{
  700. 			get
  701. 			{
  702. 				return ColorName( _selectedcolor, _allnames );
  703. 				/*
  704. 				System.Collections.Generic.List<string> list;
  705. 				if ( _consolecolorsonly )
  706. 				{
  707. 					list = Global.Common.Lists.ConsoleColors( );
  708. 				}
  709. 				else
  710. 				{
  711. 					list = Global.Common.Lists.KnownColors( );
  712. 				}
  713. 				uint rgb = (uint) _selectedcolor.ToArgb( ) % 0x01000000;
  714. 				System.Globalization.NumberStyles hex = System.Globalization.NumberStyles.HexNumber;
  715. 				var queryobject = list.Where( c => uint.Parse( c.Split( '=' )[1].Split( 'x' )[1], hex ).Equals( rgb ) );
  716. 				if ( queryobject.Count( ) == 0 )
  717. 				{
  718. 					return _selectedcolor.Name;
  719. 				}
  720. 				else
  721. 				{
  722. 					string colorname = queryobject.First( ).Split( '=' )[0];
  723. 					return colorname;
  724. 				}
  725. 				*/
  726. 			}
  727. 		}
  728. 		[System.Runtime.InteropServices.ComVisible( true )]
  729. 		public int selectedcolorB
  730. 		{
  731. 			get
  732. 			{
  733. 				return _selectedcolor.B;
  734. 			}
  735. 		}
  736. 		[System.Runtime.InteropServices.ComVisible( true )]
  737. 		public int selectedcolorG
  738. 		{
  739. 			get
  740. 			{
  741. 				return _selectedcolor.G;
  742. 			}
  743. 		}
  744. 		[System.Runtime.InteropServices.ComVisible( true )]
  745. 		public int selectedcolorR
  746. 		{
  747. 			get
  748. 			{
  749. 				return _selectedcolor.R;
  750. 			}
  751. 		}
  752. 		[System.Runtime.InteropServices.ComVisible( true )]
  753. 		public string selectedcolorRGB
  754. 		{
  755. 			get
  756. 			{
  757. 				return string.Format( "{0},{1},{2}", _selectedcolor.R, _selectedcolor.G, _selectedcolor.B );
  758. 			}
  759. 		}
  760.  
  761.  
  762. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  763. 		private static int _timeout = 0;
  764. 		[System.Runtime.InteropServices.ComVisible( true )]
  765. 		public int timeout
  766. 		{
  767. 			get
  768. 			{
  769. 				return _timeout;
  770. 			}
  771. 			set
  772. 			{
  773. 				if ( value < 0 || value > 3600 )
  774. 				{
  775. 					_timeout = 0;
  776. 				}
  777. 				else
  778. 				{
  779. 					_timeout = value;
  780. 				}
  781. 			}
  782. 		}
  783.  
  784.  
  785. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  786. 		private bool _timeoutelapsed = false;
  787. 		[System.Runtime.InteropServices.ComVisible( true )]
  788. 		public int timeoutelapsed
  789. 		{
  790. 			get
  791. 			{
  792. 				return ( _timeoutelapsed ? 1 : 0 );
  793. 			}
  794. 		}
  795.  
  796.  
  797. 		public static string timestamp => Global.Common.TimeStamp( );
  798.  
  799.  
  800. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  801. 		private static string _title = defaulttitle;
  802. 		[System.Runtime.InteropServices.ComVisible( true )]
  803. 		public string title
  804. 		{
  805. 			get
  806. 			{
  807. 				return _title;
  808. 			}
  809. 			set
  810. 			{
  811. 				_title = value.Trim( );
  812. 			}
  813. 		}
  814.  
  815.  
  816. 		// Available only for customized colorpicker (_consolecolorsonly = true)
  817. 		private int _top = -1;
  818. 		[System.Runtime.InteropServices.ComVisible( true )]
  819. 		public int top
  820. 		{
  821. 			get
  822. 			{
  823. 				return _top;
  824. 			}
  825. 			set
  826. 			{
  827. 				if ( value < 0 )
  828. 				{
  829. 					_top = 0;
  830. 				}
  831. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - dialogsize.Height )
  832. 				{
  833. 					_top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - dialogsize.Height;
  834. 				}
  835. 				else
  836. 				{
  837. 					_top = value;
  838. 				}
  839. 			}
  840. 		}
  841.  
  842.  
  843. 		[System.Runtime.InteropServices.ComVisible( true )]
  844. 		public string Version
  845. 		{
  846. 			get
  847. 			{
  848. 				return Global.Common.ProgramInfo.FileVersion;
  849. 			}
  850. 		}
  851.  
  852. 		#endregion Properties
  853.  
  854.  
  855. 		#region Event handlers
  856.  
  857. 		private static void ConsoleColorSelectBox_Colorbox_Click( object sender, System.EventArgs e )
  858. 		{
  859. 			if ( _timeout > 0 )
  860. 			{
  861. 				timer.Stop( );
  862. 			}
  863. 			GetSelectedColor( );
  864. 		}
  865.  
  866.  
  867. 		private void ConsoleColorSelectBox_KeyUp( object sender, System.Windows.Forms.KeyEventArgs e )
  868. 		{
  869. 			if ( e.KeyCode == System.Windows.Forms.Keys.Enter )
  870. 			{
  871. 				GetSelectedColor( );
  872. 				// In case no selected color was found
  873. 				_selectedcolor = defaultcolor;
  874. 				colordialog.Close( );
  875. 			}
  876. 			else if ( e.KeyCode == System.Windows.Forms.Keys.Escape )
  877. 			{
  878. 				_selectedcolor = defaultcolor;
  879. 				colordialog.Close( );
  880. 			}
  881. 		}
  882.  
  883.  
  884. 		private void ConsoleColorSelectBox_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
  885. 		{
  886. 			_timeoutelapsed = true;
  887. 			_selectedcolor = defaultcolor;
  888. 			colordialog.Close( );
  889. 		}
  890.  
  891. 		#endregion Event Handlers
  892. 	}
  893. }

page last uploaded: 2019-01-21, 22:48