Esempio n. 1
0
        protected override bool InternalGetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
        {
            if (format.cfFormat == DataObjectHelper.CF_EMBEDSOURCE && (format.tymed & TYMED.TYMED_ISTORAGE) != 0)
            {
                medium.tymed          = TYMED.TYMED_ISTORAGE;
                medium.pUnkForRelease = null;
                var stg = (IStorage)Marshal.GetObjectForIUnknown(medium.unionmember);
                // we don't save the document directly, since this would mean to save the whole (and probably huge) project
                // instead we first make a mini project with the neccessary data only and then save this instead
                InternalSaveMiniProject(stg, _altaxoMiniProject, _graphDocumentName);
                return(true);
            }

            if (format.cfFormat == DataObjectHelper.CF_LINKSOURCE && (format.tymed & TYMED.TYMED_ISTREAM) != 0)
            {
                // we should make sure that ComManager is already started, so that the moniker can be used by the program
                if (!_comManager.IsActive)
                {
                    _comManager.StartLocalServer();
                }

                IMoniker documentMoniker = CreateNewDocumentMoniker();

                if (null != documentMoniker)
                {
                    medium.tymed          = TYMED.TYMED_ISTREAM;
                    medium.pUnkForRelease = null;
                    var strm = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
                    DataObjectHelper.SaveMonikerToStream(documentMoniker, strm);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        private void EhFileMonikerChanged(IMoniker fileMoniker)
        {
            if (null == _document)
            {
                return;
            }

            // see Brockschmidt, Inside Ole 2nd ed., p.998
            // TODO we must pimp up this function

            RunningObjectTableHelper.ROTUnregister(ref _documentMonikerRotCookie);
            _documentMoniker = null;

            if (null != fileMoniker)
            {
                Ole32Func.CreateItemMoniker("!", DataObjectHelper.NormalStringToMonikerNameString(_document.Name), out var itemMoniker);

                if (null != itemMoniker)
                {
                    fileMoniker.ComposeWith(itemMoniker, false, out var compositeMoniker);
                    if (null != compositeMoniker)
                    {
                        _documentMoniker = compositeMoniker;
                        RunningObjectTableHelper.ROTRegisterAsRunning(_documentMoniker, this, ref _documentMonikerRotCookie, typeof(IOleObject));
                    }
                }
            }

            SendAdvise_Renamed();
        }
Esempio n. 3
0
        private IntPtr RenderWindowsMetafilePict(TYMED tymed)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.RenderWindowsMetafilePict");

            if (!(tymed == TYMED.TYMED_MFPICT))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_MFPICT");
            }

            if (null != _graphDocumentBitmapImage)
            {
                using (var rgbBitmap = GraphDocumentExportActions.ConvertBitmapToPixelFormat(_graphDocumentBitmapImage, System.Drawing.Imaging.PixelFormat.Format24bppRgb, _graphExportOptions.BackgroundColorForFormatsWithoutAlphaChannel))
                {
                    var scaledDocSize = _graphDocumentSize * _graphExportOptions.OutputScalingFactor;

                    using (var enhancedMetafile = GraphDocumentExportActions.RenderAsEnhancedMetafileBitmapFormat(rgbBitmap, scaledDocSize))
                    {
                        var hEmf = enhancedMetafile.GetHenhmetafile();
                        return(DataObjectHelper.ConvertEnhancedMetafileToWindowsMetafilePict(hEmf, scaledDocSize.X, scaledDocSize.Y));
                    }
                }
            }
            else
            {
                throw new InvalidProgramException("Please report this exception to the author of the program and describe the steps to reproduce the exception");
            }
        }
Esempio n. 4
0
        private void EnsureDropFileCreated()
        {
            var fileExtension = GraphExportOptions.GetDefaultFileNameExtension(_graphExportOptions.DropFileImageFormat);

            _graphDocumentDropdownFileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "AltaxoClipboardImage" + fileExtension);
            if (System.IO.File.Exists(_graphDocumentDropdownFileName))
            {
                try
                {
                    System.IO.File.Delete(_graphDocumentDropdownFileName);
                }
                catch (Exception)
                {
                    _graphDocumentDropdownFileName = null;
                    return;
                }
            }

            if (_graphExportOptions.DropFileImageFormat == System.Drawing.Imaging.ImageFormat.Emf)
            {
                if (!(null != _graphDocumentMetafileImage))
                {
                    throw new InvalidOperationException(nameof(_graphDocumentMetafileImage) + " should be != null");
                }

                var clonedMF = (System.Drawing.Imaging.Metafile)_graphDocumentMetafileImage.Clone(); // have to clone metafile because after calling GetHenhmetafile() metafile would be destroyed
                DataObjectHelper.SaveMetafileToDisk(clonedMF.GetHenhmetafile(), _graphDocumentDropdownFileName);
            }
            else if (_graphExportOptions.DropFileImageFormat == System.Drawing.Imaging.ImageFormat.Wmf)
            {
                using (var rgbBitmap = GraphDocumentExportActions.ConvertBitmapToPixelFormat(_graphDocumentBitmapImage, System.Drawing.Imaging.PixelFormat.Format24bppRgb, _graphExportOptions.BackgroundColorForFormatsWithoutAlphaChannel))
                {
                    var scaledDocSize = _graphDocumentSize * _graphExportOptions.OutputScalingFactor;
                    using (var enhancedMetafile = GraphDocumentExportActions.RenderAsEnhancedMetafileBitmapFormat(rgbBitmap, scaledDocSize))
                    {
                        var hEmf  = enhancedMetafile.GetHenhmetafile();
                        var bytes = DataObjectHelper.ConvertEnhancedMetafileToWindowsMetafileBytes(hEmf);
                        var placeableHeaderBytes = DataObjectHelper.GetWmfPlaceableHeaderBytes(scaledDocSize);

                        using (var stream = new System.IO.FileStream(_graphDocumentDropdownFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read))
                        {
                            stream.Write(placeableHeaderBytes, 0, placeableHeaderBytes.Length);
                            stream.Write(bytes, 0, bytes.Length);
                        }
                    }
                }
            }
            else // bitmap format
            {
                var bitmapToSave =
                    _graphExportOptions.DropFileBitmapPixelFormat == _graphDocumentBitmapImage.PixelFormat ?
                    _graphDocumentBitmapImage :
                    GraphDocumentExportActions.ConvertBitmapToPixelFormat(_graphDocumentBitmapImage, _graphExportOptions.DropFileBitmapPixelFormat, _graphExportOptions.BackgroundColorForFormatsWithoutAlphaChannel);

                bitmapToSave.Save(_graphDocumentDropdownFileName, _graphExportOptions.DropFileImageFormat);
            }
        }
Esempio n. 5
0
        private IntPtr RenderMoniker(TYMED tymed)
        {
            ComDebug.ReportInfo("{0}.RenderMoniker", GetType().Name);

            if (!(tymed == TYMED.TYMED_ISTREAM))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_ISTREAM");
            }

            return(DataObjectHelper.RenderMonikerToNewStream(tymed, CreateNewDocumentMoniker()));
        }
Esempio n. 6
0
        public static void ROTRegisterAsRunning(IMoniker new_moniker, object o, ref int rot_cookie, Type intf)
        {
            // Revoke any existing file moniker. See Brockschmidt, Inside Ole 2nd ed. p988
            ROTUnregister(ref rot_cookie);

            // Register the moniker in the running object table (ROT).
            ComDebug.ReportInfo("Registering {0} in ROT", DataObjectHelper.GetDisplayName(new_moniker));
            IRunningObjectTable rot = GetROT();

            // This flag solved a terrible problem where Word would stop
            // communicating after its first call to GetObject().
            rot_cookie = rot.Register(1 /*ROTFLAGS_REGISTRATIONKEEPSALIVE*/, o, new_moniker);
        }
        /// <summary>
        /// Renders the provided graph document to an device independent bitmap (TYMED_HGLOBAL). Please not that this format does not support transparancy, thus the back color provided in the rendering options is used as ground brush first.
        /// </summary>
        /// <param name="tymed">The tymed to check.</param>
        /// <param name="document">The graph document.</param>
        /// <returns>Pointer to the device independent bitmap (TYMED_HGLOBAL).</returns>
        public static IntPtr RenderAsDIBBitmap_TYMED_HGLOBAL(TYMED tymed, GraphDocumentBase document)
        {
            if (!(tymed == TYMED.TYMED_HGLOBAL))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_HGLOBAL");
            }

            var renderingOptions = GetRenderingOptions(document);

            using (var bmp = Altaxo.Graph.GraphDocumentBaseExportActions.RenderAsBitmap(document, renderingOptions, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
            {
                return(DataObjectHelper.RenderDIBBitmapToHGLOBAL(bmp));
            }
        }
Esempio n. 8
0
 protected override bool InternalGetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
 {
     if (format.cfFormat == DataObjectHelper.CF_LINKSOURCE && (format.tymed & TYMED.TYMED_ISTREAM) != 0)
     {
         var moniker = Moniker;
         if (null != moniker)
         {
             medium.tymed          = TYMED.TYMED_ISTREAM;
             medium.pUnkForRelease = null;
             var strm = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
             DataObjectHelper.SaveMonikerToStream(Moniker, strm);
             return(true);
         }
     }
     return(false);
 }
Esempio n. 9
0
        private IntPtr RenderBitmapAsDropFile(TYMED tymed)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.BitmapAsDropFile");

            if (!(tymed == TYMED.TYMED_HGLOBAL))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_HGLOBAL");
            }

            EnsureDropFileCreated();

            if (!string.IsNullOrEmpty(_graphDocumentDropdownFileName))
            {
                return(DataObjectHelper.RenderFiles(new string[] { _graphDocumentDropdownFileName }));
            }
            return(IntPtr.Zero);
        }
Esempio n. 10
0
        private IMoniker CreateNewDocumentMoniker()
        {
            // create the moniker on the fly
            IMoniker documentMoniker = null;

            var fileMoniker = _comManager.FileComObject.FileMoniker;

            if (null != fileMoniker)
            {
                Ole32Func.CreateItemMoniker("!", DataObjectHelper.NormalStringToMonikerNameString(_graphDocumentName), out var itemMoniker);
                if (null != itemMoniker)
                {
                    fileMoniker.ComposeWith(itemMoniker, false, out documentMoniker);
                }
            }
            return(documentMoniker);
        }
Esempio n. 11
0
 public void GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
 {
     ComDebug.ReportInfo("{0}.IDataObject.GetDataHere({1})", GetType().Name, DataObjectHelper.ClipboardFormatName(format.cfFormat));
     // Allows containers to duplicate this into their own storage.
     try
     {
         if (InternalGetDataHere(ref format, ref medium))
         {
             return; // data could be provided
         }
     }
     catch (Exception e)
     {
         ComDebug.ReportError("{0}.IDataObject.GetDataHere threw an exception: {1}", GetType().Name, e);
         throw;
     }
     Marshal.ThrowExceptionForHR(ComReturnValue.DATA_E_FORMATETC);
 }
Esempio n. 12
0
        private IntPtr RenderBitmapDIBV5(TYMED tymed)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.RenderBitmapDIBV5");

            if (!(tymed == TYMED.TYMED_HGLOBAL))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_HGLOBAL");
            }

            if (null != _graphDocumentBitmapImage)
            {
                return(DataObjectHelper.RenderDIBV5BitmapToHGLOBAL(_graphDocumentBitmapImage));
            }
            else
            {
                throw new InvalidProgramException("Please report this exception to the author of the program and describe the steps to reproduce the exception");
            }
        }
Esempio n. 13
0
        public void GetData(ref FORMATETC format, out STGMEDIUM medium)
        {
            ComDebug.ReportInfo("{0}.IDataObject.GetData({1})", GetType().Name, DataObjectHelper.FormatEtcToString(format));

            try
            {
                // Locate the data
                foreach (var rendering in Renderings)
                {
                    if ((rendering.format.tymed & format.tymed) > 0 &&
                        rendering.format.dwAspect == format.dwAspect &&
                        rendering.format.cfFormat == format.cfFormat &&
                        rendering.renderer != null)
                    {
                        // Found it. Return a copy of the data.

                        medium = new STGMEDIUM
                        {
                            tymed = rendering.format.tymed & format.tymed
                        };
                        medium.unionmember = rendering.renderer(medium.tymed);
                        if (medium.tymed == TYMED.TYMED_ISTORAGE || medium.tymed == TYMED.TYMED_ISTREAM)
                        {
                            medium.pUnkForRelease = Marshal.GetObjectForIUnknown(medium.unionmember);
                        }
                        else
                        {
                            medium.pUnkForRelease = null;
                        }
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                ComDebug.ReportError("{0}.IDataObject.GetData threw an exception {1}", GetType().Name, e);
                throw;
            }

            ComDebug.ReportInfo("{0}.IDataObject.GetData, no data delivered!", GetType().Name);
            medium = new STGMEDIUM();
            // Marshal.ThrowExceptionForHR(ComReturnValue.DV_E_FORMATETC);
        }
        /// <summary>
        /// Renders the provided graph document to an windows metafile picture (TYMED_MFPICT). Please not that this format does not support transparancy, thus the back color provided in the rendering options is used as ground brush first.
        /// </summary>
        /// <param name="tymed">The tymed to check.</param>
        /// <param name="document">The graph document.</param>
        /// <returns>Pointer to windows metafile picture (TYMED_MFPICT).</returns>
        public static IntPtr RenderWindowsMetafilePict_TYMED_MFPICT(TYMED tymed, GraphDocumentBase document)
        {
            if (!(tymed == TYMED.TYMED_MFPICT))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_MFPICT");
            }

            var renderingOptions = GetRenderingOptions(document);

            using (var rgbBitmap = Altaxo.Graph.GraphDocumentBaseExportActions.RenderAsBitmap(document, renderingOptions, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
            {
                var scaledDocSize = document.Size * renderingOptions.OutputScalingFactor;

                using (var enhancedMetafile = GraphDocumentExportActions.RenderAsEnhancedMetafileBitmapFormat(rgbBitmap, scaledDocSize))
                {
                    var hEmf = enhancedMetafile.GetHenhmetafile();
                    return(DataObjectHelper.ConvertEnhancedMetafileToWindowsMetafilePict(hEmf, scaledDocSize.X, scaledDocSize.Y));
                }
            }
        }
Esempio n. 15
0
        private IntPtr RenderBitmapDIB(TYMED tymed)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.RenderBitmapDIB");

            if (!(tymed == TYMED.TYMED_HGLOBAL))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_HGLOBAL");
            }

            if (null != _graphDocumentBitmapImage)
            {
                using (var convertedBitmap = GraphDocumentExportActions.ConvertBitmapToPixelFormat(_graphDocumentBitmapImage, System.Drawing.Imaging.PixelFormat.Format24bppRgb, _graphExportOptions.BackgroundColorForFormatsWithoutAlphaChannel))
                {
                    return(DataObjectHelper.RenderDIBBitmapToHGLOBAL(convertedBitmap));
                }
            }
            else
            {
                throw new InvalidProgramException("Please report this exception to the author of the program and describe the steps to reproduce the exception");
            }
        }
Esempio n. 16
0
        public IntPtr GetObject(string pszItem, int dwSpeedNeeded, IBindCtx pbc, ref Guid riid)
        {
            // Brockschmidt, Inside Ole 2nd ed. page 1003

            pszItem = DataObjectHelper.MonikerNameStringToNormalString(pszItem);

            ComDebug.ReportInfo("{0}.GetObject {1}, Requesting Interface : {2}", GetType().Name, pszItem, riid);

            bool isRunning = _currentProject.GraphDocumentCollection.TryGetValue(pszItem, out var doc);

            if (((int)BINDSPEED.BINDSPEED_IMMEDIATE == dwSpeedNeeded || (int)BINDSPEED.BINDSPEED_MODERATE == dwSpeedNeeded) && !isRunning)
            {
                throw Marshal.GetExceptionForHR(ComReturnValue.MK_E_EXCEEDEDDEADLINE);
            }

            if (null == doc) // in this application we can do nothing but to return intptr.Zero
            {
                return(IntPtr.Zero);
            }

            if (riid == Marshal.GenerateGuidForType(typeof(System.Runtime.InteropServices.ComTypes.IDataObject)) ||
                riid == Marshal.GenerateGuidForType(typeof(IOleObject)) ||
                riid == InterfaceGuid.IID_IDispatch ||
                riid == InterfaceGuid.IID_IUnknown)
            {
                var    documentComObject = _comManager.GetDocumentsComObjectForGraphDocument(doc);
                IntPtr ppvObject         = Marshal.GetComInterfaceForObject(documentComObject, typeof(System.Runtime.InteropServices.ComTypes.IDataObject));

                var action = new Action(() => Current.IProjectService.ShowDocumentView(doc));
                Current.Dispatcher.InvokeAndForget(action);

                return(ppvObject);
            }
            else
            {
                throw new COMException("No interface", unchecked ((int)0x80004002));
            }
        }
Esempio n. 17
0
        public int GetCanonicalFormatEtc(ref FORMATETC formatIn, out FORMATETC formatOut)
        {
            ComDebug.ReportInfo("{0}.IDataObject.GetCanonicalFormatEtc {1}", GetType().Name, DataObjectHelper.FormatEtcToString(formatIn));

            formatOut = default(FORMATETC);

            return(ComReturnValue.DATA_S_SAMEFORMATETC);
        }
Esempio n. 18
0
        public int DAdvise(ref FORMATETC pFormatetc, ADVF advf, IAdviseSink adviseSink, out int connection)
        {
            if (null == DataAdviseHolder)
            {
                ComDebug.ReportInfo("{0}.IDataObject.DAdvise -> not implemented!", GetType().Name);
                connection = 0;
                return(ComReturnValue.E_NOTIMPL);
            }
            else
            {
                ComDebug.ReportInfo("{0}.IDataObject.DAdvise {1}, {2}", GetType().Name, DataObjectHelper.FormatEtcToString(pFormatetc), advf);

                try
                {
                    if (pFormatetc.cfFormat != 0)               // if a special format is required
                    {
                        int res = QueryGetData(ref pFormatetc); // ask the render helper for availability of that format
                        if (res != ComReturnValue.S_OK)         // if the required format is not available
                        {
                            connection = 0;                     //  return an invalid connection cookie
                            return(res);                        // and the error
                        }
                    }
                    FORMATETC etc = pFormatetc;
                    DataAdviseHolder.Advise((IDataObject)this, ref etc, advf, adviseSink, out var conn);
                    connection = conn;
                    return(ComReturnValue.NOERROR);
                }
                catch (Exception e)
                {
                    ComDebug.ReportError("{0}.IDataObject.DAdvise exception: {1}", GetType().Name, e);
                    throw;
                }
            }
        }
Esempio n. 19
0
 public IntPtr RenderLink(TYMED tymed)
 {
     return(DataObjectHelper.RenderMonikerToNewStream(tymed, Moniker));
 }