Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dialogboxes_openfolderbox.cs

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

  1. namespace RobvanderWoude
  2. {
  3. 	[System.Runtime.InteropServices.Guid( "F0C24C90-8D15-49DA-882C-8AFBEBF3D534" )]
  4. 	public interface OpenFolderBox_Interface
  5. 	{
  6. 		[System.Runtime.InteropServices.DispId( 1 )]
  7. 		void CheckUpdate( );
  8.  
  9. 		[System.Runtime.InteropServices.DispId( 2 )]
  10. 		string Credits( );
  11.  
  12. 		[System.Runtime.InteropServices.DispId( 3 )]
  13. 		string Help( int html = 0 );
  14.  
  15. 		[System.Runtime.InteropServices.DispId( 4 )]
  16. 		string ListProperties( );
  17.  
  18. 		[System.Runtime.InteropServices.DispId( 5 )]
  19. 		string ListSpecialFolders( );
  20.  
  21. 		[System.Runtime.InteropServices.DispId( 6 )]
  22. 		string SampleCode( );
  23.  
  24. 		[System.Runtime.InteropServices.DispId( 7 )]
  25. 		void Show( );
  26. 	}
  27.  
  28.  
  29. 	[System.Runtime.InteropServices.Guid( "7451DB13-75E8-478B-8EAA-4B2BF26EA195" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
  30. 	public interface OpenFolderBox_Events
  31. 	{
  32. 	}
  33.  
  34.  
  35. 	[System.Runtime.InteropServices.Guid( "68B64FDE-9021-469D-B9AD-9782E7968B01" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( OpenFolderBox_Events ) )]
  36. 	public class OpenFolderBox : OpenFolderBox_Interface
  37. 	{
  38. 		#region Default values
  39.  
  40. 		//static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.MyDocuments; // User's documents only
  41. 		//static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.Desktop; // Entire computer and network
  42. 		static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.MyComputer; // Entire computer
  43. 		static readonly string defaultstartfolder = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments );
  44.  
  45. 		#endregion Default values
  46.  
  47.  
  48. 		#region Methods
  49.  
  50. 		public void CheckUpdate( )
  51. 		{
  52. 			Global.Common.ProgramInfo.CheckUpdate( );
  53. 		}
  54.  
  55.  
  56. 		public string Credits( )
  57. 		{
  58. 			return Global.Common.Credits( );
  59. 		}
  60.  
  61.  
  62. 		public string Help( int html = 0 )
  63. 		{
  64. 			int col1width = 0;
  65. 			int col2width = 0;
  66. 			int col3width = 0;
  67. 			int col4width = 0;
  68. 			int col5width = 0;
  69. 			string help = string.Empty;
  70. 			string linetemplate = string.Empty;
  71. 			string separatorline = string.Empty;
  72.  
  73. 			if ( html == 1 )
  74. 			{
  75. 				help += "<h1>Help for OpenFolderBox class</h1>\n\n";
  76. 				help += "<p>Present a Browse Folder dialog, and return the selected folder path</p>\n\n";
  77. 				help += "<h2>COM ProgID: RobvanderWoude.OpenFolderBox</h2>\n\n";
  78. 			}
  79. 			else
  80. 			{
  81. 				help += "Help for OpenFolderBox class\n";
  82. 				help += new string( '\u2500', help.Length ) + "\n\n";
  83. 				help += "Present a Browse Folder dialog, and return the selected folder path\n\n\n";
  84. 				help += "COM ProgID: RobvanderWoude.OpenFolderBox\n\n\n";
  85. 			}
  86.  
  87.  
  88. 			#region Properties
  89.  
  90. 			if ( html == 1 )
  91. 			{
  92. 				separatorline = string.Empty;
  93. 				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";
  94.  
  95. 				help += "<table>\n";
  96. 				help += "<tr>\n";
  97. 				help += "\t<th colspan=\"5\">Properties (Note that all properties are string or integer, no booleans, objects or arrays)</th>\n";
  98. 				help += "</tr>\n";
  99.  
  100. 				help += "<tr>\n";
  101. 				help += "\t<th>Property Name</th>\n";
  102. 				help += "\t<th>Description</th>\n";
  103. 				help += "\t<th>Mandatory</th>\n";
  104. 				help += "\t<th>Read-Only</th>\n";
  105. 				help += "\t<th>Default (Allowed) Values</th>\n";
  106. 				help += "</tr>\n";
  107. 			}
  108. 			else
  109. 			{
  110. 				col1width = 16;
  111. 				col2width = 50;
  112. 				col3width = 9;
  113. 				col4width = 9;
  114. 				col5width = 44;
  115.  
  116. 				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";
  117. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n";
  118.  
  119. 				help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n";
  120. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  121. 				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)" );
  122. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  123. 				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";
  124. 				help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" );
  125. 				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";
  126. 			}
  127.  
  128. 			help += HelpTableRow( linetemplate, "allowmakedir", "Show \"Make New Folder\" button    (0=false; 1=true)", "no", "no", "0: don't show", html );
  129. 			help += separatorline;
  130.  
  131. 			help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
  132. 			help += separatorline;
  133.  
  134. 			help += HelpTableRow( linetemplate, "description", "Text above directory tree in dialog", "no", "no", "", html );
  135. 			help += separatorline;
  136.  
  137. 			help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
  138. 			help += separatorline;
  139.  
  140. 			help += HelpTableRow( linetemplate, "rootfolderfolder", "Windows Special Folder that is used as the directory tree root", "no", "no", defaultrootfolder.ToString( ) + " (use the \"ListSpecialFolders\" method to get a list of valid names)", html );
  141. 			help += separatorline;
  142.  
  143. 			help += HelpTableRow( linetemplate, "selectedfolder", "The path of the selected folder", "N/A", "YES", "", html );
  144. 			help += separatorline;
  145.  
  146. 			help += HelpTableRow( linetemplate, "startfolder", "The initial folder for the dialog", "no", "no", defaultstartfolder, html );
  147.  
  148. 			if ( html == 1 )
  149. 			{
  150. 				help += "</table>\n\n\n";
  151. 			}
  152. 			else
  153. 			{
  154. 				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";
  155. 			}
  156.  
  157. 			#endregion Properties
  158.  
  159.  
  160. 			#region Methods
  161.  
  162. 			if ( html == 1 )
  163. 			{
  164. 				separatorline = string.Empty;
  165. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  166.  
  167. 				help += "<table>\n";
  168. 				help += "<tr>\n";
  169. 				help += "\t<th colspan=\"3\">Methods</th>\n";
  170. 				help += "</tr>\n";
  171.  
  172. 				help += "<tr>\n";
  173. 				help += "\t<th>Method Name</th>\n";
  174. 				help += "\t<th>Description</th>\n";
  175. 				help += "\t<th>Requirements</th>\n";
  176. 				help += "</tr>\n";
  177. 			}
  178. 			else
  179. 			{
  180. 				col1width = 18;
  181. 				col2width = 74;
  182. 				col3width = 42;
  183.  
  184. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n";
  185. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n";
  186.  
  187. 				help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n";
  188. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  189. 				help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" );
  190. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  191. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  192. 				help += string.Format( linetemplate, "Method Name", "Description", "Requirements" );
  193. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  194. 			}
  195.  
  196. 			help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html );
  197. 			help += separatorline;
  198.  
  199. 			help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html );
  200. 			help += separatorline;
  201.  
  202. 			help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html );
  203. 			help += separatorline;
  204.  
  205. 			help += HelpTableRow( linetemplate, "ListSpecialFolders", "Returns a semicolon-separated list of Windows Special Folders", "", html );
  206. 			help += separatorline;
  207.  
  208. 			help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html );
  209. 			help += separatorline;
  210.  
  211. 			help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html );
  212. 			help += separatorline;
  213.  
  214. 			help += HelpTableRow( linetemplate, "Show", "Presents a Browse Folder dialog based on the current property values, and if \"OK\"is clicked, saves the selected folder path in the \"selectedfolder\" property (if \"Cancel\" is clicked, the startfolder is assumed selected).", "", html );
  215.  
  216. 			if ( html == 1 )
  217. 			{
  218. 				help += "</table>\n\n\n";
  219. 			}
  220. 			else
  221. 			{
  222. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n";
  223. 			}
  224.  
  225. 			#endregion Methods
  226.  
  227.  
  228. 			#region Example
  229.  
  230. 			if ( html == 1 )
  231. 			{
  232. 				help += "<h2>VBScript usage example:</h2>\n\n<pre>";
  233. 			}
  234. 			else
  235. 			{
  236. 				help += "VBScript usage example:\n";
  237. 				help += new string( '\u2500', 23 ) + "\n\n";
  238. 			}
  239.  
  240. 			help += SampleCode( );
  241.  
  242. 			if ( html == 1 )
  243. 			{
  244. 				help += "</pre>";
  245. 			}
  246.  
  247. 			help += "\n\n";
  248.  
  249. 			#endregion Example
  250.  
  251.  
  252. 			help = help.Replace( "\n", System.Environment.NewLine );
  253. 			return help;
  254. 		}
  255.  
  256.  
  257. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html )
  258. 		{
  259. 			if ( html == 1 )
  260. 			{
  261. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text );
  262. 			}
  263. 			else
  264. 			{
  265. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" );
  266. 			}
  267. 		}
  268.  
  269.  
  270. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html )
  271. 		{
  272. 			if ( html == 1 )
  273. 			{
  274. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text );
  275. 			}
  276. 			else
  277. 			{
  278. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text );
  279. 			}
  280. 		}
  281.  
  282.  
  283. 		public string ListProperties( )
  284. 		{
  285. 			return Global.Common.Lists.Properties( this );
  286. 		}
  287.  
  288.  
  289. 		public string ListSpecialFolders( )
  290. 		{
  291. 			return string.Join( ";", Global.Common.Lists.SpecialFolders( ) );
  292. 		}
  293.  
  294.  
  295. 		public string SampleCode( )
  296. 		{
  297. 			string code = "Set objOpenFolderBox = CreateObject( \"RobvanderWoude.OpenFolderBox\" )\n\n";
  298. 			code += "With objOpenFolderBox\n";
  299. 			code += "\t.rootfolder = \"MyDocuments\"\n";
  300. 			code += "\t.Show\n";
  301. 			code += "\tWScript.Echo \"Selected folder: \" & .selectedfolder\n";
  302. 			code += "\tWScript.Echo\n";
  303. 			code += "\tWScript.Echo .ListProperties( )\n";
  304. 			code += "End With\n\n";
  305. 			code += "Set objOpenFolderBox = Nothing";
  306. 			return code;
  307. 		}
  308.  
  309.  
  310. 		[System.STAThread]
  311. 		public void Show( )
  312. 		{
  313. 			try
  314. 			{
  315. 				using ( System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog( ) )
  316. 				{
  317. 					dialog.RootFolder = _rootfolder;
  318. 					if ( !string.IsNullOrWhiteSpace( startfolder ) )
  319. 					{
  320. 						dialog.SelectedPath = startfolder;
  321. 					}
  322. 					dialog.Description = description;
  323. 					dialog.ShowNewFolderButton = _allowmakedir;
  324. 					if ( dialog.ShowDialog( ) == System.Windows.Forms.DialogResult.OK )
  325. 					{
  326. 						startfolder = dialog.SelectedPath;
  327. 					}
  328. 				}
  329. 			}
  330. 			catch ( System.Exception e )
  331. 			{
  332. 				_errors.Add( Global.Common.TimeStamp( ) + "Error while trying to show dialog: " + e.Message );
  333. 			}
  334. 		}
  335.  
  336. 		#endregion Methods
  337.  
  338.  
  339. 		#region Properties
  340.  
  341. 		private bool _allowmakedir = false;
  342. 		[System.Runtime.InteropServices.ComVisible( true )]
  343. 		public int allowmakedir
  344. 		{
  345. 			get
  346. 			{
  347. 				return ( _allowmakedir ? 1 : 0 );
  348. 			}
  349. 			set
  350. 			{
  351. 				_allowmakedir = ( value == 1 );
  352. 			}
  353. 		}
  354.  
  355.  
  356. 		private System.Collections.Generic.List<string> _debuginfo = new System.Collections.Generic.List<string>( );
  357. 		[System.Runtime.InteropServices.ComVisible( true )]
  358. 		public string debuginfo
  359. 		{
  360. 			get
  361. 			{
  362. 				return string.Join( "\n", _debuginfo.ToArray( ) );
  363. 			}
  364. 		}
  365.  
  366.  
  367. 		private string _description = string.Empty;
  368. 		[System.Runtime.InteropServices.ComVisible( true )]
  369. 		public string description
  370. 		{
  371. 			get
  372. 			{
  373. 				return _description;
  374. 			}
  375. 			set
  376. 			{
  377. 				_description = value.Trim( );
  378. 			}
  379. 		}
  380.  
  381.  
  382. 		private System.Collections.Generic.List<string> _errors = new System.Collections.Generic.List<string>( );
  383. 		[System.Runtime.InteropServices.ComVisible( true )]
  384. 		public string errors
  385. 		{
  386. 			get
  387. 			{
  388. 				return string.Join( "\n", _errors.ToArray( ) );
  389. 			}
  390. 		}
  391.  
  392.  
  393. 		private System.Environment.SpecialFolder _rootfolder = defaultrootfolder;
  394. 		[System.Runtime.InteropServices.ComVisible( true )]
  395. 		public string rootfolder
  396. 		{
  397. 			get
  398. 			{
  399. 				return _rootfolder.ToString( );
  400. 			}
  401. 			set
  402. 			{
  403. 				if ( Global.Common.Lists.SpecialFolders( ).Contains( value ) )
  404. 				{
  405. 					try
  406. 					{
  407. 						_rootfolder = (System.Environment.SpecialFolder) System.Enum.Parse( typeof( System.Environment.SpecialFolder ), value, true );
  408. 					}
  409. 					catch( System.Exception e )
  410. 					{
  411. 						_debuginfo.Add( timestamp + "Error while trying to make sense of specified root directory: " + e.Message );
  412. 						_debuginfo.Add( timestamp + "Using " + defaultrootfolder.ToString( ) + " instead" );
  413. 						_rootfolder = defaultrootfolder;
  414. 					}
  415. 				}
  416. 				else
  417. 				{
  418. 					_debuginfo.Add( timestamp + "Invalid root directory, using " + defaultrootfolder.ToString( ) + " instead" );
  419. 					_rootfolder = defaultrootfolder;
  420. 				}
  421. 			}
  422. 		}
  423.  
  424.  
  425. 		private string _selectedfolder = string.Empty;
  426. 		[System.Runtime.InteropServices.ComVisible( true )]
  427. 		public string selectedfolder
  428. 		{
  429. 			get
  430. 			{
  431. 				return _selectedfolder;
  432. 			}
  433. 		}
  434.  
  435.  
  436. 		private string _startfolder = defaultstartfolder;
  437. 		[System.Runtime.InteropServices.ComVisible( true )]
  438. 		public string startfolder
  439. 		{
  440. 			get
  441. 			{
  442. 				return _startfolder;
  443. 			}
  444. 			set
  445. 			{
  446. 				if ( System.IO.Directory.Exists( value ) )
  447. 				{
  448. 					try
  449. 					{
  450. 						_startfolder = System.IO.Path.GetFullPath( value );
  451. 					}
  452. 					catch ( System.ArgumentException )
  453. 					{
  454. 						try
  455. 						{
  456. 							// Assuming the error is caused by a trailing backslash in doublequotes
  457. 							_startfolder = System.IO.Path.GetFullPath( value.Substring( 0, startfolder.IndexOf( '"' ) ) + "." );
  458. 						}
  459. 						catch ( System.Exception )
  460. 						{
  461. 							_debuginfo.Add( timestamp + "Invalid start directory, using " + defaultrootfolder.ToString( ) + " instead" );
  462. 							_startfolder = string.Empty;
  463. 						}
  464. 					}
  465. 				}
  466. 				else
  467. 				{
  468. 					_debuginfo.Add( timestamp + "Invalid start directory, using " + defaultrootfolder.ToString( ) + " instead" );
  469. 					_startfolder = string.Empty;
  470. 				}
  471. 				_selectedfolder = _startfolder;
  472. 			}
  473. 		}
  474.  
  475.  
  476. 		public static string timestamp => Global.Common.TimeStamp( );
  477.  
  478.  
  479. 		[System.Runtime.InteropServices.ComVisible( true )]
  480. 		public string Version
  481. 		{
  482. 			get
  483. 			{
  484. 				return Global.Common.ProgramInfo.FileVersion;
  485. 			}
  486. 		}
  487.  
  488. 		#endregion Properties
  489. 	}
  490. }

page last modified: 2024-04-16; loaded in 0.0384 seconds