home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / CommandBar / CommandBarObj.cs < prev    next >
Encoding:
Text File  |  2002-06-11  |  20.8 KB  |  716 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using Microsoft.Win32;
  7. using System.Threading;
  8. using System.Reflection;
  9. using System.Diagnostics;
  10. using System.Collections;
  11. using System.Windows.Forms;
  12. using System.Runtime.InteropServices;
  13.  
  14. using SHDocVw;
  15. using Shell32;
  16.  
  17. using ZCommon;
  18. using BandObjects;
  19.  
  20.  
  21. namespace CommandBar
  22. {
  23.     /// <summary>
  24.     /// Implementation of Command consoleCtrl Explorer Bar.
  25.     /// </summary>
  26.     /// <remarks>
  27.     /// Hosts ConsoleCtrl and provides toolbar with shortcut commands to several operations over console.
  28.     /// Relies on base FormBandObject class that implements all    functionality required by explorer from Band Objects.
  29.     /// </remarks>
  30.     [ComVisible(true)]
  31.     [Guid("8689A69C-CC9D-3AEC-9D7C-E7F409700C15")]
  32.     [BandObject("Command Prompt /Ctrl+M/", BandObjectStyles.Horizontal
  33.          ,HelpText="Shows Command Prompt Bar.")]
  34.     public class CommandBarObj : BandObject
  35.     {
  36.         System.ComponentModel.IContainer components;
  37.         MacroMenuBuilder macroMenuBuilder;
  38.  
  39.         ImageList        toolbarImages;
  40.         ToolBar            toolBar;
  41.         ToolBarButton    toolBarButtonNew;
  42.         ToolBarButton    toolBarButtonSync;
  43.         ToolBarButton    toolBarButtonMacro;
  44.         ToolTip            toolTips;
  45.         ToolBarButton    toolBarButtonCls;
  46.         ToolBarButton    toolBarButtonEnter;
  47.         ToolBarButton    toolBarButtonEsc;
  48.         ToolBarButton    toolBarButtonType;
  49.         ToolBarButton    toolBarButtonGrow;
  50.         ToolBarButton    toolBarButtonShrink;
  51.         ToolBarButton    toolBarButton3;
  52.         /// <summary>
  53.         /// Command prompt window incapsulated in the form of WindowsForms control.
  54.         /// </summary>
  55.         ConsoleCtrl                        consoleCtrl;
  56.         System.Windows.Forms.Timer        timer;
  57.  
  58.         static Hashtable                bars = Hashtable.Synchronized(new Hashtable());
  59.         static CreateProcessHook        createProcessHook;
  60.         Int32                            threadId;
  61.  
  62.         public static CommandBarObj GetBarFromThreadId(Int32 threadId)
  63.         {
  64.             foreach( CommandBarObj bar in bars.Values )
  65.             {
  66.                 if( bar.threadId == threadId )
  67.                     return bar;
  68.             }
  69.  
  70.             return null;
  71.         }
  72.  
  73.         [CLSCompliant(false)]
  74.         public static CommandBarObj GetBarFromExplorer(WebBrowserClass explorer)
  75.         {
  76.             CommandBarObj bar = null;
  77.             try
  78.             {
  79.                 bar = (CommandBarObj)bars[explorer];
  80.             }
  81.             catch( ArgumentException /*e*/)
  82.             {}
  83.  
  84.             return bar;
  85.         }
  86.  
  87.         public static bool HookCreateProcess
  88.         {
  89.             get
  90.             {
  91.                 return hookCreateProcess;
  92.             }
  93.             set
  94.             {
  95.                 hookCreateProcess = value;
  96.                 if( HookCreateProcess && createProcessHook == null )
  97.                     createProcessHook = new CreateProcessHook();
  98.  
  99.                 if( !HookCreateProcess && createProcessHook != null )
  100.                 {
  101.                     createProcessHook.Dispose();    
  102.                     createProcessHook = null;
  103.                 }
  104.             }
  105.         }static bool hookCreateProcess = true;
  106.  
  107.  
  108.         public CommandBarObj()
  109.         {
  110.             InitializeComponent();
  111.  
  112.             ContextMenu mnu = new ContextMenu();
  113.             mnu.Popup += new EventHandler( OnMacroMenuChanged );
  114.             toolBarButtonMacro.DropDownMenu = mnu;
  115.  
  116.             macroMenuBuilder = new MacroMenuBuilder(MacroFilePath,mnu);
  117.             macroMenuBuilder.MenuItemClick += new EventHandler( OnMacroMenuIemClick );
  118.             macroMenuBuilder.MenuChanged += new EventHandler( OnMacroMenuChanged );
  119.  
  120.             // 
  121.             // consoleCtrl
  122.             // 
  123. //            this.consoleCtrl = new ZCommon.ConsoleCtrl();
  124. //            this.consoleCtrl.Parent = this;
  125. //
  126. //            this.consoleCtrl.Anchor = System.Windows.Forms.AnchorStyles.Top 
  127. //                                    | System.Windows.Forms.AnchorStyles.Bottom
  128. //                                    | System.Windows.Forms.AnchorStyles.Left 
  129. //                                    | System.Windows.Forms.AnchorStyles.Right;
  130. //
  131. //            this.consoleCtrl.Name = "consoleCtrl";
  132. //            this.consoleCtrl.Location = new System.Drawing.Point(40, 0);
  133. //            this.consoleCtrl.Size = new System.Drawing.Size(260, 208);
  134. //            this.consoleCtrl.VisibleChanged += new System.EventHandler(this.consoleCtrl_VisibleChanged);
  135. //            this.consoleCtrl.TabStop = true;
  136. //            this.consoleCtrl.TabIndex = 1;
  137.  
  138.             ConsoleCtrl.ConsoleExited += new EventHandler( OnConsoleExited );
  139.  
  140.             HookCreateProcess = HookCreateProcess;
  141.         }
  142.  
  143.         public override Int32 HasFocusIO()
  144.         {
  145.             return base.HasFocusIO() == 0 || ConsoleCtrl.ConsoleFocused ? 0 : 1;// S_OK : S_FALSE
  146.         }
  147.  
  148.         protected override void OnExplorerAttached(EventArgs ea)
  149.         {
  150.             base.OnExplorerAttached(ea);
  151.             bars[Explorer] = this;
  152.             threadId = Win32.GetCurrentThreadId();
  153.         }
  154.  
  155.         protected  override void OnExplorerDetaching(EventArgs ea)
  156.         {
  157.             base.OnExplorerDetaching(ea);
  158.             bars.Remove(Explorer);
  159.         }
  160.  
  161.         protected override void Dispose( bool disposing )
  162.         {
  163.             if( disposing )
  164.             {
  165.                 if(components != null)
  166.                 {
  167.                     components.Dispose();
  168.                 }
  169.             }
  170.  
  171.             base.Dispose( disposing );
  172.         }
  173.  
  174.         #region Windows Form Designer generated code
  175.         private void InitializeComponent()
  176.         {
  177.             this.components = new System.ComponentModel.Container();
  178.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CommandBarObj));
  179.             this.consoleCtrl = new ZCommon.ConsoleCtrl();
  180.             this.toolbarImages = new System.Windows.Forms.ImageList(this.components);
  181.             this.toolBar = new System.Windows.Forms.ToolBar();
  182.             this.toolBarButtonNew = new System.Windows.Forms.ToolBarButton();
  183.             this.toolBarButtonSync = new System.Windows.Forms.ToolBarButton();
  184.             this.toolBarButtonCls = new System.Windows.Forms.ToolBarButton();
  185.             this.toolBarButtonType = new System.Windows.Forms.ToolBarButton();
  186.             this.toolBarButtonEnter = new System.Windows.Forms.ToolBarButton();
  187.             this.toolBarButtonEsc = new System.Windows.Forms.ToolBarButton();
  188.             this.toolBarButtonMacro = new System.Windows.Forms.ToolBarButton();
  189.             this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
  190.             this.toolBarButtonGrow = new System.Windows.Forms.ToolBarButton();
  191.             this.toolBarButtonShrink = new System.Windows.Forms.ToolBarButton();
  192.             this.toolTips = new System.Windows.Forms.ToolTip(this.components);
  193.             this.timer = new System.Windows.Forms.Timer(this.components);
  194.             this.SuspendLayout();
  195.             // 
  196.             // consoleCtrl
  197.             // 
  198.             this.consoleCtrl.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  199.                 | System.Windows.Forms.AnchorStyles.Left) 
  200.                 | System.Windows.Forms.AnchorStyles.Right);
  201.             this.consoleCtrl.Location = new System.Drawing.Point(40, 0);
  202.             this.consoleCtrl.Name = "consoleCtrl";
  203.             this.consoleCtrl.ScreenBufferAutoGrow = true;
  204.             this.consoleCtrl.Size = new System.Drawing.Size(260, 208);
  205.             this.consoleCtrl.TabIndex = 1;
  206.             this.consoleCtrl.VisibleChanged += new System.EventHandler(this.consoleCtrl_VisibleChanged);
  207.             // 
  208.             // toolbarImages
  209.             // 
  210.             this.toolbarImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
  211.             this.toolbarImages.ImageSize = new System.Drawing.Size(16, 16);
  212.             this.toolbarImages.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("toolbarImages.ImageStream")));
  213.             this.toolbarImages.TransparentColor = System.Drawing.Color.Transparent;
  214.             // 
  215.             // toolBar
  216.             // 
  217.             this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
  218.             this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  219.                                                                                        this.toolBarButtonNew,
  220.                                                                                        this.toolBarButtonSync,
  221.                                                                                        this.toolBarButtonCls,
  222.                                                                                        this.toolBarButtonType,
  223.                                                                                        this.toolBarButtonEnter,
  224.                                                                                        this.toolBarButtonEsc,
  225.                                                                                        this.toolBarButtonMacro,
  226.                                                                                        this.toolBarButton3,
  227.                                                                                        this.toolBarButtonGrow,
  228.                                                                                        this.toolBarButtonShrink});
  229.             this.toolBar.Dock = System.Windows.Forms.DockStyle.Left;
  230.             this.toolBar.DropDownArrows = true;
  231.             this.toolBar.ImageList = this.toolbarImages;
  232.             this.toolBar.Name = "toolBar";
  233.             this.toolBar.ShowToolTips = true;
  234.             this.toolBar.Size = new System.Drawing.Size(39, 208);
  235.             this.toolBar.TabIndex = 0;
  236.             this.toolBar.TabStop = true;
  237.             this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
  238.             // 
  239.             // toolBarButtonNew
  240.             // 
  241.             this.toolBarButtonNew.ImageIndex = 0;
  242.             this.toolBarButtonNew.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
  243.             this.toolBarButtonNew.ToolTipText = "Console menu";
  244.             // 
  245.             // toolBarButtonSync
  246.             // 
  247.             this.toolBarButtonSync.ImageIndex = 1;
  248.             this.toolBarButtonSync.Pushed = true;
  249.             this.toolBarButtonSync.ToolTipText = "Syncronize";
  250.             // 
  251.             // toolBarButtonCls
  252.             // 
  253.             this.toolBarButtonCls.ImageIndex = 6;
  254.             this.toolBarButtonCls.ToolTipText = "cls";
  255.             // 
  256.             // toolBarButtonType
  257.             // 
  258.             this.toolBarButtonType.ImageIndex = 5;
  259.             this.toolBarButtonType.ToolTipText = "Get selected folder items";
  260.             // 
  261.             // toolBarButtonEnter
  262.             // 
  263.             this.toolBarButtonEnter.ImageIndex = 8;
  264.             this.toolBarButtonEnter.ToolTipText = "Enter key";
  265.             // 
  266.             // toolBarButtonEsc
  267.             // 
  268.             this.toolBarButtonEsc.ImageIndex = 7;
  269.             this.toolBarButtonEsc.ToolTipText = "Escape key";
  270.             // 
  271.             // toolBarButtonMacro
  272.             // 
  273.             this.toolBarButtonMacro.ImageIndex = 3;
  274.             this.toolBarButtonMacro.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
  275.             this.toolBarButtonMacro.ToolTipText = "Macro";
  276.             // 
  277.             // toolBarButton3
  278.             // 
  279.             this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  280.             // 
  281.             // toolBarButtonGrow
  282.             // 
  283.             this.toolBarButtonGrow.ImageIndex = 9;
  284.             this.toolBarButtonGrow.ToolTipText = "Grow";
  285.             // 
  286.             // toolBarButtonShrink
  287.             // 
  288.             this.toolBarButtonShrink.ImageIndex = 10;
  289.             this.toolBarButtonShrink.ToolTipText = "Shrink";
  290.             // 
  291.             // timer
  292.             // 
  293.             this.timer.Interval = 750;
  294.             this.timer.Tick += new System.EventHandler(this.timer_Tick);
  295.             // 
  296.             // CommandBarObj
  297.             // 
  298.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  299.                                                                           this.consoleCtrl,
  300.                                                                           this.toolBar});
  301.             this.Name = "CommandBarObj";
  302.             this.Size = new System.Drawing.Size(292, 208);
  303.             this.Title = "Command Prompt";
  304.             this.VisibleChanged += new System.EventHandler(this.CommandBar_VisibleChanged);
  305.             this.ResumeLayout(false);
  306.  
  307.         }
  308.         #endregion
  309.  
  310.         /// <summary>
  311.         /// Tells explorer to show or hide CommandBar.
  312.         /// </summary>
  313.         [CLSCompliant(false)]
  314.         public static void ShowBrowserBar(bool show, WebBrowserClass explorer)
  315.         {
  316.             if( explorer != null )
  317.             {
  318.                 CommandBarObj me = GetBarFromExplorer(explorer);
  319.  
  320. //                if( !show )
  321. //                {
  322. //                    if( me != null )
  323. //                        me.Invoke( new MethodInvoker(me.NotifyExplorerLostFocus) );
  324. //                }
  325.  
  326.                 if( show )
  327.                 {
  328.                     foreach( CommandBarObj bar in bars.Values  )
  329.                     {
  330.                         if( bar.Visible && bar != me )
  331.                             ShowBrowserBar( false, bar.Explorer );
  332.                     }
  333.                 }
  334.  
  335.                 Object guid = "{8689A69C-CC9D-3AEC-9D7C-E7F409700C15}"; 
  336.                 Object oShow = show;
  337.                 Object dummy = false;
  338.                 explorer.ShowBrowserBar( ref guid, ref oShow, ref dummy);
  339.             }
  340.         }
  341.  
  342.         /// <summary>
  343.         /// Current filesystem folder that explorer looks at or "" if explorer looks at a shell namespace (My Computer).
  344.         /// </summary>
  345.         String currentDir
  346.         {
  347.             get
  348.             {
  349.                 try
  350.                 {
  351.                     Uri uri = new Uri(  Explorer.LocationURL , true );            
  352.                     return uri.LocalPath;
  353.                 }
  354.                 catch
  355.                 {
  356.                     return "";
  357.                 }
  358.             }
  359.         }
  360.  
  361.         /// <summary>
  362.         /// Path to the xml file with the set of macro commands.
  363.         /// </summary>
  364.         public static String MacroFilePath
  365.         {
  366.             get
  367.             {    
  368.                 Microsoft.Win32.RegistryKey rkCmdBar = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CommandBar");
  369.                 return rkCmdBar.GetValue("MacroFilePath","").ToString();
  370.             }
  371.         }
  372.  
  373.         class FolderSelectionFormatter : IFormatProvider, ICustomFormatter 
  374.         {
  375.             public Object GetFormat(Type formatType)
  376.             {
  377.                 if( formatType == typeof(ICustomFormatter) )
  378.                     return this;
  379.                 else
  380.                     return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
  381.             }
  382.  
  383.             String FormatFolderItem(String format, FolderItem item)
  384.             {
  385.                 String s;
  386.                 switch(format[0])
  387.                 {
  388.                     case 'F':
  389.                     case 'f':
  390.                         s = item.Path;
  391.                         break;
  392.                     case 'N':
  393.                         s = item.Name;
  394.                         break;
  395.                     case 'n':
  396.                         s = Path.GetFileNameWithoutExtension(item.Name);
  397.                         break;
  398.                     case 'P':
  399.                     case 'p':
  400.                         s = Path.GetDirectoryName(item.Path);
  401.                         break;
  402.                     case 'E':
  403.                     case 'e':
  404.                         s = Path.GetExtension(item.Name);
  405.                         break;
  406.                     default:
  407.                         s = "<<unknown format>>";
  408.                         break;
  409.                 }
  410.  
  411.                 if( format[1] == '"' || 
  412.                     (format[1] == '\'' && item.Name.IndexOf(" ") != -1) )
  413.                 {
  414.                     return "\"" + s + "\"";
  415.                 }
  416.                 else
  417.                 {
  418.                     return s;
  419.                 }
  420.             }
  421.  
  422.             public String Format(String format, Object arg, IFormatProvider formatProvider)
  423.             {
  424.                 //default formatting
  425.                 if( format == null || format == "" ) format = "N' ";
  426.                 if( format.Length == 1 ) format += "' ";
  427.                 if( format.Length == 2 ) format += " ";
  428.  
  429.                 if( arg.GetType() == typeof(Object[]) )
  430.                 {
  431.                     StringBuilder sb = new StringBuilder(40);
  432.                     bool firstItem = true;
  433.                     foreach(FolderItem item in arg as Object[])
  434.                     {
  435.                         if( !firstItem )
  436.                             sb.Append( format[2] );                        
  437.                         else
  438.                             firstItem = false;
  439.  
  440.                         sb.Append(FormatFolderItem(format,item));
  441.                     }
  442.                     return sb.ToString();
  443.                 }
  444.  
  445.                 if( arg is FolderItem )
  446.                 {
  447.                     return FormatFolderItem( format, arg as FolderItem );
  448.                 }
  449.                 
  450.                 IFormattable formattable = arg as IFormattable;
  451.                 if( formattable != null )
  452.                     return formattable.ToString(format,formatProvider);
  453.                 else
  454.                     return arg.ToString();
  455.             }
  456.         }
  457.  
  458.         String ExpandCommandRegistryKeys(String command)
  459.         {
  460.             int begin;
  461.             int end;
  462.             while( (begin=command.IndexOf('[')) != -1 )
  463.             {
  464.                 end=command.IndexOf(']',begin);
  465.                 if( end != -1 )
  466.                 {
  467.                     String registryPath = command.Substring(begin+1,end-begin-1);
  468.  
  469.                     bool commandChanged = false;
  470.                     Type type = typeof(Microsoft.Win32.Registry);
  471.                     foreach(FieldInfo field in type.GetFields())
  472.                     {
  473.                         object obj = field.GetValue(null);
  474.                         RegistryKey key = obj as RegistryKey;
  475.                         if( registryPath.StartsWith(key.Name) )
  476.                         {
  477.                             registryPath = registryPath.Remove(0,key.Name.Length+1);
  478.                             int lastSlash = registryPath.LastIndexOf('\\');
  479.                             object val = key.OpenSubKey(registryPath.Substring(0,lastSlash)).
  480.                                 GetValue(registryPath.Substring(lastSlash+1),"<not found>");
  481.                             String str = val as String;
  482.                             command = command.Substring(0,begin) + str + command.Substring(end+1);
  483.                             commandChanged = true;
  484.                             break;
  485.                         }                        
  486.                     }
  487.                     if( !commandChanged ) break;//avoiding infinite loop
  488.                 }    
  489.             }
  490.             return command;
  491.         }
  492.  
  493.  
  494.         String ExpandCommand(String command)
  495.         {
  496.             command = ExpandCommandRegistryKeys(command);
  497.             
  498.             IShellFolderViewDual folder = Explorer.Document as IShellFolderViewDual;
  499.  
  500.             ArrayList selection = new ArrayList();
  501.             foreach( FolderItem item in folder.SelectedItems())
  502.             {
  503.                 selection.Add( item );
  504.             }
  505.             StringBuilder sb = new StringBuilder(40);
  506.  
  507.             selection.Insert( 0, selection.ToArray() );
  508.  
  509.             try
  510.             {
  511.                 sb.AppendFormat( new FolderSelectionFormatter(), command, selection.ToArray() );
  512.             }
  513.             catch( System.FormatException)
  514.             {
  515.             }
  516.  
  517.  
  518.             return sb.ToString().Trim();
  519.         }
  520.  
  521.  
  522.         /// <summary>
  523.         /// Synchronizes current folder of command prompt with the one of explorer.
  524.         /// </summary>
  525.         void synchronizeConsole()
  526.         {
  527.             if( toolBarButtonSync.Pushed )
  528.             {
  529.                 ConsoleCtrl.SendEscKey();
  530.                 ConsoleCtrl.Output("cd /D "+ currentDir, true, true);// /D makes it to change drives also
  531.             }
  532.         }
  533.  
  534.         /// <summary>
  535.         /// After explorer navigated to the new folder synchronizes command prompt current folder.
  536.         /// </summary>
  537.         /// <remarks>
  538.         /// Synchronization does not appear immediately. It is delayed by 0.75 sec with the help of timer. This allows to skip some synchronizations cycles in the case user is just moving fast through folders.
  539.         /// </remarks>
  540.         void OnNavigateComplete2( object sender, ref object args)
  541.         {
  542.             timer.Stop();
  543.             timer.Start();
  544.         }
  545.  
  546.         void OnConsoleExited(Object sender, EventArgs ea )
  547.         {
  548.             if( Explorer != null )
  549.             {
  550.                 ShowBrowserBar(false, Explorer);
  551.                 Win32.SetWindowPos( (IntPtr)Explorer.HWND, IntPtr.Zero, 0, 0, 0, 0, 0x3 );
  552.             }
  553.         }
  554.  
  555.         /// <summary>
  556.         /// Called when menu displayed by Script button has been changed.
  557.         /// </summary>
  558.         /// <remarks>
  559.         /// This is happens because xml macro file was changed. So we need to check if there are any errors and update Script button icon and tooltip accordingly.
  560.         /// </remarks>
  561.         void OnMacroMenuChanged(Object sender, EventArgs ea )
  562.         {
  563.             if( macroMenuBuilder.MacroFileError != null )
  564.             {
  565.                 toolBarButtonMacro.ImageIndex = 4;//script with exclamaiton icon
  566.                 toolBarButtonMacro.ToolTipText = macroMenuBuilder.MacroFileError;
  567.             }
  568.             else
  569.             {
  570.                 toolBarButtonMacro.ImageIndex = 3;//ok, no error
  571.                 toolBarButtonMacro.ToolTipText = "";
  572.             }
  573.         }
  574.  
  575.         /// <summary>
  576.         /// Called when user clicked on one of the macro menu items.
  577.         /// </summary>
  578.         /// <remarks>
  579.         /// If menu item is script then we output its command to the console.
  580.         /// </remarks>
  581.         void OnMacroMenuIemClick( Object sender, EventArgs ea )
  582.         {
  583.             //we know for sure that menu build by MacroMenuBuilder consists of TagMenuItems
  584.             TagMenuItem mi = (TagMenuItem)sender;
  585.             //these items' tags are either MacroMenuBuilder.group(s) or MacroMenuBuilder.macro(s)
  586.             if( mi.Tag is MacroMenuBuilder.macro )
  587.             {
  588.                 MacroMenuBuilder.macro script = (MacroMenuBuilder.macro)mi.Tag;
  589.                 //if script does not have a command then its text is a command
  590.                 String command = script.command == null ? script.text : script.command;
  591.                 
  592.                 ConsoleCtrl.Output( 
  593.                     ExpandCommand( command ),
  594.                     true,
  595.                     script.execute && (Control.ModifierKeys!=Keys.Shift));
  596.             }
  597.         }
  598.  
  599.         void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
  600.         {
  601.             ToolBarButton b = e.Button;
  602.             if( b == toolBarButtonSync )
  603.             {
  604.                 toolBar.SuspendLayout();
  605.                 b.Pushed ^= true;
  606.                 if( b.Pushed )
  607.                 {
  608.                     b.ImageIndex = 1;
  609.                     toolBar.Invalidate();
  610.                     toolBar.Update();
  611.                     synchronizeConsole();
  612.                 }
  613.                 else
  614.                 {
  615.                     b.ImageIndex = 2;
  616.                     toolBar.Invalidate();
  617.                     toolBar.Update();
  618.                 }
  619.                 toolBar.ResumeLayout();
  620.             }
  621.             else if ( b == toolBarButtonNew )
  622.             {                
  623.                 new About().ShowDialog(this);
  624.             }
  625.             else if ( b == toolBarButtonMacro )
  626.             {
  627.                 //starts notepad so user can edit macro menu file
  628.                 Process.Start("notepad.exe", MacroFilePath );
  629.             }
  630.             else if ( b == toolBarButtonType )
  631.             {
  632.                 //outputs current explorer selection to the console
  633.                 ConsoleCtrl.Output( ExpandCommand("{0}"), true, false );
  634.             }
  635.             else if ( b == toolBarButtonCls )
  636.             {
  637.                 //cleans the screen
  638.                 ConsoleCtrl.SendEscKey();
  639.                 ConsoleCtrl.Output( "cls", true, true );
  640.             }
  641.             else if ( b == toolBarButtonEnter )
  642.             {
  643.                 //same as hitting an enter key
  644.                 ConsoleCtrl.Output( "", true, true );
  645.             }
  646.             else if ( b == toolBarButtonEsc )
  647.             {
  648.                 //same as hitting an Esc key
  649.                 ConsoleCtrl.SendEscKey();
  650.             }
  651.             else if ( b == toolBarButtonGrow )
  652.             {    
  653.                 //increases width of the console screen buffer
  654.                 ConsoleCtrl.ResizeConsoleScreenBuffer(4);
  655.             }
  656.             else if ( b == toolBarButtonShrink )
  657.             {
  658.                 //decreases width of the console screen buffer
  659.                 ConsoleCtrl.ResizeConsoleScreenBuffer(-4);
  660.             }
  661.         }
  662.  
  663.         /// <summary>
  664.         /// Called when command propmpt porcess (cmd.exe) exits. 
  665.         /// </summary>
  666.         /// <remarks>
  667.         /// Hides Command consoleCtrl Explore Bar. Assuming that user has typed 'exit' it looks like the right thing to do.
  668.         /// </remarks>
  669.         void consoleCtrl_ConsoleExit(object sender, System.EventArgs e)
  670.         {
  671.             if( Explorer != null )
  672.             {
  673.                 ShowBrowserBar( false, Explorer );
  674.             }
  675.         }
  676.  
  677.         /// <summary>
  678.         /// If we here then explorer navigated to another folder about 0.75 sec ago. Syncronizing.
  679.         /// </summary>
  680.         void timer_Tick(object sender, System.EventArgs e)
  681.         {
  682.             timer.Stop();
  683.             synchronizeConsole();
  684.         }
  685.  
  686.         /// <summary>
  687.         /// Called by base class when it processes explorer request to hide or show band object.
  688.         /// </summary>
  689.         void CommandBar_VisibleChanged(object sender, System.EventArgs e)
  690.         {
  691.             if( Visible )
  692.             {
  693.                 if( !ConsoleCtrl.ConsoleRunning ) ConsoleCtrl.OpenConsole();
  694.                 consoleCtrl.MainWindowHandle = (IntPtr)Explorer.HWND;
  695.                 //makes this buttons menu look like it is a console window system menu
  696.                 toolBarButtonNew.DropDownMenu = ConsoleCtrl.SystemMenu;
  697.  
  698.                 Explorer.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler( OnNavigateComplete2 );
  699.  
  700.                 synchronizeConsole();
  701.                 consoleCtrl.Show();
  702.             }
  703.             else
  704.             {
  705.                 Explorer.NavigateComplete2 -= new DWebBrowserEvents2_NavigateComplete2EventHandler( OnNavigateComplete2 );
  706.             }
  707.         }
  708.  
  709.         private void consoleCtrl_VisibleChanged(object sender, System.EventArgs e)
  710.         {
  711.             if ( !consoleCtrl.Visible )
  712.                 ShowBrowserBar( false, Explorer );
  713.         }
  714.     }
  715. }
  716.