2020-08-14 15:10:06 -07:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.ComponentModel ;
using System.Drawing ;
using Common.ComInterlop ;
namespace Common
{
/// <summary>
/// Preview Handler base class implementing interfaces required by Preview Handler.
/// </summary>
public abstract class PreviewHandlerBase : IPreviewHandler , IOleWindow , IObjectWithSite , IPreviewHandlerVisuals
{
/// <summary>
/// An instance of Preview Control Used by the Handler.
/// </summary>
private IPreviewHandlerControl previewControl ;
/// <summary>
/// Hold reference for the window handle.
/// </summary>
private IntPtr parentHwnd ;
/// <summary>
/// Hold the bounds of the window.
/// </summary>
private Rectangle windowBounds ;
/// <summary>
/// Holds the site pointer.
/// </summary>
private object unkSite ;
/// <summary>
/// Holds reference for the IPreviewHandlerFrame.
/// </summary>
private IPreviewHandlerFrame frame ;
/// <summary>
/// Initializes a new instance of the <see cref="PreviewHandlerBase"/> class.
/// </summary>
public PreviewHandlerBase ( )
{
2020-09-23 10:22:17 -07:00
}
/// <summary>
/// Initializes a new instance of the <see cref="PreviewHandlerBase"/> class.
/// </summary>
public void Initialize ( )
{
previewControl = CreatePreviewHandlerControl ( ) ;
2020-08-14 15:10:06 -07:00
}
/// <inheritdoc />
public abstract void DoPreview ( ) ;
/// <inheritdoc />
public void SetWindow ( IntPtr hwnd , ref RECT rect )
{
this . parentHwnd = hwnd ;
this . windowBounds = rect . ToRectangle ( ) ;
this . previewControl . SetWindow ( hwnd , this . windowBounds ) ;
}
/// <inheritdoc />
public void SetRect ( ref RECT rect )
{
this . windowBounds = rect . ToRectangle ( ) ;
this . previewControl . SetRect ( this . windowBounds ) ;
}
/// <inheritdoc />
public void Unload ( )
{
this . previewControl . Unload ( ) ;
}
/// <inheritdoc />
public void SetFocus ( )
{
this . previewControl . SetFocus ( ) ;
}
/// <inheritdoc />
public void QueryFocus ( out IntPtr phwnd )
{
this . previewControl . QueryFocus ( out IntPtr result ) ;
phwnd = result ;
if ( phwnd = = IntPtr . Zero )
{
throw new Win32Exception ( ) ;
}
}
/// <inheritdoc />
public uint TranslateAccelerator ( ref MSG pmsg )
{
// Current implementation simply directs all Keystrokes to IPreviewHandlerFrame. This is the recommended approach to handle keystokes for all low-integrity preview handlers.
2022-09-28 18:18:55 +02:00
// Source: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#ipreviewhandlertranslateaccelerator
2020-08-14 15:10:06 -07:00
if ( this . frame ! = null )
{
return this . frame . TranslateAccelerator ( ref pmsg ) ;
}
const uint S_FALSE = 1 ;
return S_FALSE ;
}
/// <inheritdoc />
public void GetWindow ( out IntPtr phwnd )
{
2020-09-23 10:22:17 -07:00
phwnd = this . previewControl . GetWindowHandle ( ) ;
2020-08-14 15:10:06 -07:00
}
/// <inheritdoc />
public void ContextSensitiveHelp ( bool fEnterMode )
{
2022-09-28 18:18:55 +02:00
// Should always return NotImplementedException. Source: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#iolewindowcontextsensitivehelp
2020-08-14 15:10:06 -07:00
throw new NotImplementedException ( ) ;
}
/// <inheritdoc />
public void SetSite ( object pUnkSite )
{
2022-09-28 18:18:55 +02:00
// Implementation logic details: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#iobjectwithsitesetsite
2020-08-14 15:10:06 -07:00
this . unkSite = pUnkSite ;
this . frame = this . unkSite as IPreviewHandlerFrame ;
}
/// <inheritdoc />
public void GetSite ( ref Guid riid , out object ppvSite )
{
ppvSite = this . unkSite ;
}
/// <inheritdoc />
public void SetBackgroundColor ( COLORREF color )
{
this . previewControl . SetBackgroundColor ( color . Color ) ;
}
/// <inheritdoc />
public void SetFont ( ref LOGFONT plf )
{
this . previewControl . SetFont ( Font . FromLogFont ( plf ) ) ;
}
/// <inheritdoc />
public void SetTextColor ( COLORREF color )
{
this . previewControl . SetTextColor ( color . Color ) ;
}
/// <summary>
/// Provide instance of the implementation of <see cref="IPreviewHandlerControl"/>. Should be overridden by the implementation class with a control object to be used.
/// </summary>
/// <returns>Instance of the <see cref="IPreviewHandlerControl"/>.</returns>
protected abstract IPreviewHandlerControl CreatePreviewHandlerControl ( ) ;
}
}