home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / BandObjects / BandObject.cs < prev    next >
Encoding:
Text File  |  2002-06-10  |  9.9 KB  |  388 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using System.Drawing;
  5. using Microsoft.Win32;
  6. using System.Reflection;
  7. using System.Diagnostics;
  8. using System.Windows.Forms;
  9. using System.ComponentModel;
  10. using System.Security.Permissions;
  11. using System.Runtime.InteropServices;
  12.  
  13. using SHDocVw;
  14.  
  15. namespace BandObjects
  16. {
  17.     /// <summary>
  18.     /// Implements generic Band Object functionality. 
  19.     /// </summary>
  20.     /// <example>
  21.     /// [Guid("YOURGUID-GOES-HERE-YOUR-GUIDGOESHERE")]
  22.     /// [BandObject("Hello World Bar", BandObjectStyles.Horizontal | BandObjectStyles.ExplorerToolbar , HelpText = "Shows bar that says hello.")]
  23.     /// public class HelloWorldBar : BandObject
  24.     /// { /*...*/ }
  25.     /// </example>
  26.     public class BandObject : UserControl, IObjectWithSite, IDeskBand, IDockingWindow, IOleWindow, IInputObject 
  27.     {
  28.         /// <summary>
  29.         /// Reference to the host explorer.
  30.         /// </summary>
  31.         [CLSCompliant(false)]
  32.         WebBrowserClass    explorer;
  33.         protected IInputObjectSite     BandObjectSite;
  34.         /// <summary>
  35.         /// This event is fired after reference to hosting explorer is retreived and stored in Explorer property.
  36.         /// </summary>
  37.         public event EventHandler ExplorerAttached;
  38.  
  39.         public event EventHandler ExplorerDetaching;
  40.  
  41.         [CLSCompliant(false)]
  42.         public WebBrowserClass Explorer
  43.         {
  44.             get
  45.             {
  46.                 return explorer;
  47.             }
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Title of the band object. Displayed at the left or on top of the band object.
  52.         /// </summary>
  53.         [Browsable(true)]
  54.         [DefaultValue("")]
  55.         public String Title
  56.         {
  57.             get
  58.             {
  59.                 return title;
  60.             }
  61.             set
  62.             {
  63.                 title = value;
  64.             }
  65.         }String title;
  66.  
  67.         /// <summary>
  68.         /// Minimum size of the band object. Default value of -1 sets no minimum constraint.
  69.         /// </summary>
  70.         [Browsable(true)]
  71.         [DefaultValue(typeof(Size),"-1,-1")]
  72.         public Size MinSize
  73.         {
  74.             get
  75.             {
  76.                 return minSize;
  77.             }
  78.             set
  79.             {
  80.                 minSize = value;
  81.             }
  82.         }Size minSize = new Size(-1,-1);
  83.  
  84.         /// <summary>
  85.         /// Maximum size of the band object. Default value of -1 sets no maximum constraint.
  86.         /// </summary>
  87.         [Browsable(true)]
  88.         [DefaultValue(typeof(Size),"-1,-1")]
  89.         public Size MaxSize
  90.         {
  91.             get
  92.             {
  93.                 return maxSize;
  94.             }
  95.             set
  96.             {
  97.                 maxSize = value;
  98.             }
  99.         }Size maxSize = new Size(-1,-1);
  100.  
  101.         /// <summary>
  102.         /// Says that band object's size must be multiple of this size. Defauilt value of -1 does not set this constraint.
  103.         /// </summary>
  104.         [Browsable(true)]
  105.         [DefaultValue(typeof(Size),"1,1")]
  106.         public Size IntegralSize
  107.         {
  108.             get
  109.             {
  110.                 return integralSize;
  111.             }
  112.             set
  113.             {
  114.                 integralSize = value;
  115.             }
  116.         }Size integralSize = new Size(1,1);
  117.  
  118.  
  119.         public virtual void GetBandInfo(
  120.             Int32 bandID,
  121.             Int32 viewMode,
  122.             ref DESKBANDINFO info)
  123.         {
  124.             info.wszTitle = this.Title;
  125.         
  126.             info.ptActual.X = this.Size.Width;
  127.             info.ptActual.Y = this.Size.Height;
  128.  
  129.             info.ptMaxSize.X = this.MaxSize.Width;
  130.             info.ptMaxSize.Y = this.MaxSize.Height;
  131.  
  132.             info.ptMinSize.X = this.MinSize.Width;
  133.             info.ptMinSize.Y = this.MinSize.Height;
  134.  
  135.             info.ptIntegral.X = this.IntegralSize.Width;
  136.             info.ptIntegral.Y = this.IntegralSize.Height;
  137.  
  138.             info.dwModeFlags = DBIMF.DBIMF_VARIABLEHEIGHT;
  139.  
  140.             info.dwMask &= ~DBIM.BKCOLOR;
  141.         }
  142.  
  143.         /// <summary>
  144.         /// Called by explorer when band object needs to be showed or hidden.
  145.         /// </summary>
  146.         /// <param name="fShow"></param>
  147.         public virtual void ShowDW(bool show)
  148.         {
  149.             if( show )
  150.                 Show();
  151.             else
  152.                 Hide();
  153.         }
  154.  
  155.         /// <summary>
  156.         /// Called by explorer when window is about to close.
  157.         /// </summary>
  158.         public virtual void CloseDW(Int32  reserved)
  159.         {
  160.             Dispose( true );
  161.         }
  162.  
  163.         /// <summary>
  164.         /// Not used.
  165.         /// </summary>
  166.         public virtual void ResizeBorderDW(IntPtr borderRectangle, Object toolbarSite, bool reserved){}
  167.  
  168.         public virtual void GetWindow(out System.IntPtr hwnd)
  169.         {        
  170.             hwnd = Handle;
  171.         }
  172.  
  173.         public virtual void ContextSensitiveHelp(bool enterMode){}
  174.  
  175.         public virtual void SetSite(Object site)
  176.         {
  177.             if( site != BandObjectSite )
  178.             {
  179.                 if( explorer != null )
  180.                     OnExplorerDetaching(EventArgs.Empty);
  181.  
  182.                 if( BandObjectSite != null )
  183.                     Marshal.ReleaseComObject( BandObjectSite );
  184.  
  185.                 if( explorer != null )
  186.                 {
  187.                     Marshal.ReleaseComObject( explorer );
  188.                     explorer = null;
  189.                 }
  190.             }
  191.  
  192.             BandObjectSite = (IInputObjectSite)site;
  193.             if( BandObjectSite != null )
  194.             {
  195.                 //pUnkSite is a pointer to object that implements IOleWindowSite or something  similar
  196.                 //we need to get access to the top level object - explorer itself
  197.                 //to allows this explorer objects also implement IServiceProvider interface
  198.                 //(don't mix it with System.IServiceProvider!)
  199.                 //we get this interface and ask it to find WebBrowserApp
  200.                 IServiceProviderCOM sp = (IServiceProviderCOM)BandObjectSite;
  201.                 Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
  202.                 Guid riid = ExplorerGUIDs.IID_IUnknown;
  203.                 
  204.                 try
  205.                 {
  206.                     object w;
  207.                     sp.QueryService( 
  208.                         ref guid,
  209.                         ref riid,
  210.                         out w );
  211.                 
  212.                     //once we have interface to the COM object we can create RCW from it
  213.                     explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
  214.                         w as IWebBrowser,
  215.                         typeof(WebBrowserClass)
  216.                         );
  217.  
  218.                     OnExplorerAttached(EventArgs.Empty);
  219.                 }
  220.                 catch( COMException )
  221.                 {
  222.                     //we anticipate this exception in case our object instantiated 
  223.                     //as a Desk Band. There is no web browser service available.
  224.                 }
  225.             }
  226.         }
  227.  
  228.         public virtual void GetSite(ref Guid riid, out Object site)
  229.         {
  230.             site = BandObjectSite;            
  231.         }
  232.  
  233.         /// <summary>
  234.         /// Called explorer when focus has to be chenged.
  235.         /// </summary>
  236.         public virtual void UIActivateIO(Int32 activate, ref MSG msg)
  237.         {
  238.             if( activate != 0 ) 
  239.             {
  240.                 Control ctrl = GetNextControl(this, Control.ModifierKeys != Keys.Shift);//first or last
  241.                 this.Focus();
  242.                 if( ctrl != null ) ctrl.Select();
  243.             }
  244.         }
  245.  
  246.         public virtual Int32 HasFocusIO()
  247.         {
  248.             return this.ContainsFocus ? 0 : 1; //S_OK : S_FALSE;
  249.         }
  250.  
  251.         /// <summary>
  252.         /// Called by explorer to process keyboard events. Undersatands Tab and F6.
  253.         /// </summary>
  254.         /// <param name="msg"></param>
  255.         /// <returns>S_OK if message was processed, S_FALSE otherwise.</returns>
  256.         public virtual Int32 TranslateAcceleratorIO(ref MSG msg)
  257.         {
  258.             if( msg.message == 0x100 )//WM_KEYDOWN
  259.                 if( msg.wParam == (uint)Keys.Tab || msg.wParam == (uint)Keys.F6 )//keys used by explorer to navigate from control to control
  260.                 {
  261.                     Control next = GetNextControl( ActiveControl, Control.ModifierKeys != Keys.Shift );
  262.  
  263.                     if( SelectNextControl( 
  264.                         ActiveControl,
  265.                         ModifierKeys == Keys.Shift  ? false : true,
  266.                         true,
  267.                         true,
  268.                         false ) 
  269.                         )
  270.                         return 0;//S_OK
  271.                 }
  272.  
  273.             return 1;//S_FALSE
  274.         }
  275.  
  276.         /// <summary>
  277.         /// Override this method to handle ExplorerAttached event.
  278.         /// </summary>
  279.         /// <param name="ea"></param>
  280.         protected virtual void OnExplorerAttached(EventArgs ea)
  281.         {
  282.             if ( ExplorerAttached != null )
  283.                 ExplorerAttached(this, ea);
  284.         }
  285.  
  286.         protected virtual void OnExplorerDetaching(EventArgs ea)
  287.         {
  288.             if ( ExplorerDetaching != null )
  289.                 ExplorerDetaching(this, ea);
  290.         }
  291.  
  292.         /// <summary>
  293.         /// Notifies explorer of focus change.
  294.         /// </summary>
  295.         protected override void OnGotFocus(System.EventArgs e)
  296.         {
  297.             base.OnGotFocus(e);
  298.             NotifyExplorerGotFocus();
  299.         }
  300.         /// <summary>
  301.         /// Notifies explorer of focus change.
  302.         /// </summary>
  303.         protected override void OnLostFocus(System.EventArgs e)
  304.         {
  305.             base.OnLostFocus(e);
  306.             if( HasFocusIO() != 0 )//S_OK
  307.                 NotifyExplorerLostFocus();
  308.         }
  309.  
  310.         public void NotifyExplorerLostFocus()
  311.         {
  312.             BandObjectSite.OnFocusChangeIS(this as IInputObject, 0);
  313.         }
  314.  
  315.         public void NotifyExplorerGotFocus()
  316.         {
  317.             BandObjectSite.OnFocusChangeIS(this as IInputObject, 1);
  318.         }
  319.  
  320.  
  321.         /// <summary>
  322.         /// Called when derived class is registered as a COM server.
  323.         /// </summary>
  324.         [ComRegisterFunctionAttribute]
  325.         public static void Register(Type t)
  326.         {
  327.             string guid = t.GUID.ToString("B");
  328.             
  329.             RegistryKey rkClass = Registry.ClassesRoot.CreateSubKey(@"CLSID\"+guid );
  330.             RegistryKey rkCat = rkClass.CreateSubKey("Implemented Categories");
  331.  
  332.             BandObjectAttribute[] boa = (BandObjectAttribute[])t.GetCustomAttributes(
  333.                 typeof(BandObjectAttribute),
  334.                 false );
  335.  
  336.             string name = t.Name;
  337.             string help = t.Name;
  338.             BandObjectStyles style = 0;
  339.             if( boa.Length == 1 )
  340.             {
  341.                 if( boa[0].Name != null )
  342.                     name = boa[0].Name;
  343.  
  344.                 if( boa[0].HelpText != null )
  345.                     help = boa[0].HelpText;
  346.  
  347.                 style = boa[0].Style;
  348.             }
  349.  
  350.             rkClass.SetValue(null, name );
  351.             rkClass.SetValue("MenuText", name );
  352.             rkClass.SetValue("HelpText", help );
  353.  
  354.             if( 0 != (style & BandObjectStyles.Vertical) )
  355.                 rkCat.CreateSubKey("{00021493-0000-0000-C000-000000000046}");
  356.  
  357.             if( 0 != (style & BandObjectStyles.Horizontal) )
  358.                 rkCat.CreateSubKey("{00021494-0000-0000-C000-000000000046}");
  359.  
  360.             if( 0 != (style & BandObjectStyles.TaskbarToolBar) )
  361.                 rkCat.CreateSubKey("{00021492-0000-0000-C000-000000000046}");
  362.  
  363.             if( 0 != (style & BandObjectStyles.ExplorerToolbar) )
  364.                 Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Toolbar").SetValue(guid,name);
  365.  
  366.         }
  367.    
  368.         /// <summary>
  369.         /// Called when derived class is unregistered as a COM server.
  370.         /// </summary>
  371.         [ComUnregisterFunctionAttribute]
  372.         public static void Unregister(Type t)
  373.         {
  374.             string guid = t.GUID.ToString("B");
  375.             BandObjectAttribute[] boa = (BandObjectAttribute[])t.GetCustomAttributes(
  376.                 typeof(BandObjectAttribute),
  377.                 false );
  378.  
  379.             BandObjectStyles style = 0;
  380.             if( boa.Length == 1 ) style = boa[0].Style;
  381.  
  382.             if( 0 != (style & BandObjectStyles.ExplorerToolbar) )
  383.                 Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Toolbar").DeleteValue(guid,false);
  384.  
  385.             Registry.ClassesRoot.CreateSubKey(@"CLSID").DeleteSubKeyTree(guid);
  386.         }
  387.     }
  388. }