Beispiel #1
0
        /// <summary>
        /// News the doc component.
        /// </summary>
        /// <param name="docType">Type of the doc.</param>
        /// <param name="xMsFactory"> </param>
        /// <param name="xContext"> </param>
        /// <returns></returns>
        public static XComponent OpenNewDocumentComponent(String docType, XComponentContext xContext = null, XMultiComponentFactory xMsFactory = null)
        {
            try
            {
                if (xContext == null)
                {
                    xContext = OO.GetContext();
                }

                if (xMsFactory == null)
                {
                    xMsFactory = OO.GetMultiComponentFactory(xContext);
                }

                var desktop      = xMsFactory.createInstanceWithContext(OO.Services.FRAME_DESKTOP, xContext);
                var mxCompLoader = desktop as XComponentLoader;

                String loadUrl   = OO.DocTypes.DOC_TYPE_BASE + docType;
                var    loadProps = new unoidl.com.sun.star.beans.PropertyValue[0];
                if (mxCompLoader != null)
                {
                    return(mxCompLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps));
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Desktop to XComponentLoader cast Exeption: " + ex);
            }
            return(null);
        }
        /// <summary>
        /// To get the shape (may also be a group...) as a translucent png in a byte array.
        /// </summary>
        /// <param name="pngFileData">The png file data as byte array.</value>
        /// <returns>the png file size (only > 0 if successful)</returns>
        virtual public int GetShapeAsPng(out byte[] pngFileData)
        {
            pngFileData = new byte[0];
            object doc = GetDocument();

            // see https://blog.oio.de/2010/10/27/copy-and-paste-without-clipboard-using-openoffice-org-api/
            if (IsValid(false) && doc != null && Shape != null)
            {
                System.Drawing.Rectangle rectDom = GetRelativeScreenBoundsByDom();

                // See <http://www.oooforum.org/forum/viewtopic.phtml?t=50783>
                // properties for the png export filter:
                PropertyValue[] aFilterData = new PropertyValue[5];
                aFilterData[0] = new PropertyValue(); aFilterData[0].Name = "PixelWidth"; aFilterData[0].Value = tud.mci.tangram.models.Any.Get(rectDom.Width);   // bounding box width
                aFilterData[1] = new PropertyValue(); aFilterData[1].Name = "PixelHeight"; aFilterData[1].Value = tud.mci.tangram.models.Any.Get(rectDom.Height); // bounding box height
                aFilterData[2] = new PropertyValue(); aFilterData[2].Name = "Translucent"; aFilterData[2].Value = new uno.Any(true);                              // png with translucent background
                aFilterData[3] = new PropertyValue(); aFilterData[3].Name = "Compression"; aFilterData[3].Value = new uno.Any(0);                                 // png compression level 0..9 (set to 0 to be fastest, smallest files would be produced by 9)
                aFilterData[4] = new PropertyValue(); aFilterData[4].Name = "Interlaced"; aFilterData[4].Value = new uno.Any(0);                                  // png interlacing (off=0)

                /* create com.sun.star.comp.MemoryStream, Debug.GetAllInterfacesOfObject(memoryOutStream) gets:
                 *  unoidl.com.sun.star.io.XStream
                 *  unoidl.com.sun.star.io.XSeekableInputStream
                 *  unoidl.com.sun.star.io.XOutputStream
                 *  unoidl.com.sun.star.io.XTruncate
                 *  unoidl.com.sun.star.lang.XTypeProvider
                 *  unoidl.com.sun.star.uno.XWeak
                 */
                //XMultiServiceFactory xmsf = OO.GetMultiServiceFactory(OO.GetContext(), OO.GetMultiComponentFactory(OO.GetContext()));
                XStream memoryStream = (XStream)OO.GetContext().getServiceManager().createInstanceWithContext("com.sun.star.comp.MemoryStream", OO.GetContext());
                //XStream memoryStream = (XStream)xmsf.createInstance("com.sun.star.comp.MemoryStream");

                // the filter media descriptor: media type, destination, and filterdata containing the export settings from above
                PropertyValue[] aArgs = new PropertyValue[3];
                aArgs[0] = new PropertyValue(); aArgs[0].Name = "MediaType"; aArgs[0].Value = tud.mci.tangram.models.Any.Get("image/png");
                aArgs[1] = new PropertyValue(); aArgs[1].Name = "OutputStream"; aArgs[1].Value = tud.mci.tangram.models.Any.Get(memoryStream);   // filter to our memory stream
                aArgs[2] = new PropertyValue(); aArgs[2].Name = "FilterData"; aArgs[2].Value = tud.mci.tangram.models.Any.GetAsOne(aFilterData);

                // create exporter service
                XExporter exporter = (XExporter)OO.GetContext().getServiceManager().createInstanceWithContext("com.sun.star.drawing.GraphicExportFilter", OO.GetContext());
                exporter.setSourceDocument((XComponent)Shape);

                // call the png export filter
                ((XFilter)exporter).filter(aArgs);

                // read all bytes from stream into byte array
                int pngFileSize = ((XInputStream)memoryStream).available();
                ((XInputStream)memoryStream).readBytes(out pngFileData, pngFileSize);
                return(pngFileSize);
            }
            return(0);
        }
Beispiel #3
0
        /// <summary>
        /// This convert an URL (formated as a string) to a struct com.sun.star.util.URL.
        /// It use a special service to do that: the URLTransformer.
        /// Because some API calls need it and it's not allowed to set "Complete"
        /// part of the util struct only. The URL must be parsed.
        /// </summary>
        /// <param name="sUrl">URL for parsing in string notation</param>
        /// <returns>URL in UNO struct notation </returns>
        public static URL ParseUrl(String sUrl)
        {
            URL aUrl;

            if (sUrl == null || sUrl.Equals("", StringComparison.CurrentCulture))
            {
                System.Diagnostics.Debug.WriteLine("wrong using of URL parser");
                return(null);
            }

            try
            {
                XComponentContext xOfficeCtx = OO.GetContext();

                // Create special service for parsing of given URL.
                var xParser = (XURLTransformer)xOfficeCtx.getServiceManager().createInstanceWithContext(
                    OO.Services.UTIL_URL_TRANSFORMER, xOfficeCtx);

                // Because it's an in/out parameter we must use an array of URL objects.
                var aParseUrl = new URL[1];
                aParseUrl[0] = new URL {
                    Complete = sUrl
                };

                // Parse the URL
                xParser.parseStrict(ref aParseUrl[0]);

                aUrl = aParseUrl[0];
            }
            catch (RuntimeException)
            {
                // Any UNO method of this scope can throw this exception.
                // Reset the return value only.
                aUrl = null;
            }
            catch (unoidl.com.sun.star.uno.Exception)
            {
                // "createInstance()" method of used service manager can throw it.
                // Then it wasn't possible to get the URL transformer.
                // Return default instead of realy parsed URL.
                aUrl = null;
            }

            return(aUrl);
        }
Beispiel #4
0
        /// <summary>
        /// Try to find an unique frame name, which isn't currently used inside
        /// remote office instance. Because we create top level frames
        /// only, it's enough to check the names of existing child frames on the
        /// desktop only.
        /// </summary>
        /// <returns>
        /// should represent an unique frame name, which currently isn't
        /// used inside the remote office frame tree
        /// (Couldn't guaranteed for a real multi threaded environment.
        /// But we try it ...)
        /// </returns>
        public static String GetUniqueFrameName()
        {
            String sName = null;

            XComponentContext xCtx = OO.GetContext();

            try
            {
                var xSupplier = (XFramesSupplier)xCtx.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xCtx);

                var xContainer = (XIndexAccess)xSupplier.getFrames();

                int nCount = xContainer.getCount();
                for (int i = 0; i < nCount; ++i)
                {
                    XFrame xFrame = (XFrame)xContainer.getByIndex(i).Value;
                    sName = BASEFRAMENAME + _mnViewCount;
                    while (String.Compare(sName, xFrame.getName(), System.StringComparison.Ordinal) == 0)
                    {
                        ++_mnViewCount;
                        sName = BASEFRAMENAME + _mnViewCount;
                    }
                }
            }
            catch (unoidl.com.sun.star.uno.Exception)
            {
                sName = BASEFRAMENAME;
            }

            if (sName == null)
            {
                System.Diagnostics.Debug.WriteLine("invalid name!");
                sName = BASEFRAMENAME;
            }

            return(sName);
        }
Beispiel #5
0
        /// <summary>
        /// It try to export given document in HTML format.
        /// Current document will be converted to HTML and moved to new place on disk.
        /// A "new" file will be created by given URL (may be overwritten
        /// if it already exist). Right filter will be used automatically if factory of
        /// this document support it. If no valid filter can be found for export,
        /// nothing will be done here.
        /// </summary>
        /// <param name="xDocument">document which should be exported</param>
        /// <param name="sUrl">target URL for converted document</param>
        public static void SaveAsHtml(XComponent xDocument, String sUrl)
        {
            try
            {
                // First detect factory of this document.
                // Ask for the supported service name of this document.
                // If information is available it can be used to find out which
                // filter exist for HTML export. Normally this filter should be searched
                // inside the filter configuration but this little demo doesn't do so.
                // (see service com.sun.star.document.FilterFactory for further
                // informations too)
                // Well known filter names are used directly. They must exist in current
                // office installation. Otherwise this code will fail. But to prevent
                // this code against missing filters it check for existing state of it.
                XServiceInfo xInfo = (XServiceInfo)xDocument;

                if (xInfo != null)
                {
                    // Find out possible filter name.
                    String sFilter = null;
                    if (xInfo.supportsService(OO.Services.DOCUMENT_TEXT))
                    {
                        sFilter = "HTML (StarWriter)";
                    }
                    else
                    if (xInfo.supportsService(OO.Services.DOCUMENT_WEB))
                    {
                        sFilter = "HTML";
                    }
                    else
                    if (xInfo.supportsService(OO.Services.DOCUMENT_SPREADSHEET))
                    {
                        sFilter = "HTML (StarCalc)";
                    }

                    // Check for existing state of this filter.
                    if (sFilter != null)
                    {
                        XComponentContext xCtx = OO.GetContext();

                        var xFilterContainer = (XNameAccess)xCtx.getServiceManager().createInstanceWithContext(
                            OO.Services.DOCUMENT_FILTER_FACTORY, xCtx);

                        if (xFilterContainer.hasByName(sFilter) == false)
                        {
                            sFilter = null;
                        }
                    }

                    // Use this filter for export.
                    if (sFilter != null)
                    {
                        // Export can be forced by saving the document and using a
                        // special filter name which can write needed format. Build
                        // necessary argument list now.
                        // Use special flag "Overwrite" too, to prevent operation
                        // against possible exceptions, if file already exist.
                        var lProperties = new PropertyValue[2];
                        lProperties[0] = new PropertyValue {
                            Name = "FilterName", Value = Any.Get(sFilter)
                        };
                        lProperties[1] = new PropertyValue {
                            Name = "Overwrite", Value = Any.Get(true)
                        };

                        XStorable xStore = (XStorable)xDocument;

                        xStore.storeAsURL(sUrl, lProperties);
                    }
                }
            }
            catch (unoidl.com.sun.star.io.IOException exIo)
            {
                // Can be thrown by "store()" call.
                // Do nothing then. Saving failed - that's it.
                System.Diagnostics.Debug.WriteLine(exIo);
            }
            catch (RuntimeException exRuntime)
            {
                // Can be thrown by any uno call.
                // Do nothing here. Saving failed - that's it.
                System.Diagnostics.Debug.WriteLine(exRuntime);
            }
            catch (unoidl.com.sun.star.uno.Exception exUno)
            {
                // Can be thrown by "createInstance()" call of service manager.
                // Do nothing here. Saving failed - that's it.
                System.Diagnostics.Debug.WriteLine(exUno);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Load document specified by an URL into given frame synchronously.
        /// The result of this operation will be the loaded document for success
        /// or null if loading failed.
        /// </summary>
        /// <param name="xFrame">frame wich should be the target of this load call</param>
        /// <param name="sUrl">unparsed URL for loading</param>
        /// <param name="lProperties">optional arguments</param>
        /// <returns>
        /// the loaded document for success or null if it's failed
        /// </returns>
        public static XComponent LoadDocument(XFrame xFrame, String sUrl, PropertyValue[] lProperties)
        {
            XComponent xDocument;
            String     sOldName = null;

            try
            {
                XComponentContext xCtx = OO.GetContext();

                // First prepare frame for loading
                // We must address it inside the frame tree without any complications.
                // So we set an unambiguous (we hope it) name and use it later.
                // Don't forget to reset original name after that.
                sOldName = xFrame.getName();
                String sTarget = "odk_officedev_desk";
                xFrame.setName(sTarget);

                // Get access to the global component loader of the office
                // for synchronous loading the document.
                var xLoader =
                    (XComponentLoader)xCtx.getServiceManager().createInstanceWithContext(OO.Services.FRAME_DESKTOP, xCtx);

                // Load the document into the target frame by using his name and
                // special search flags.
                xDocument = xLoader.loadComponentFromURL(
                    sUrl,
                    sTarget,
                    FrameSearchFlag.CHILDREN,
                    lProperties);

                // don't forget to restore old frame name ...
                xFrame.setName(sOldName);
            }
            catch (unoidl.com.sun.star.io.IOException exIo)
            {
                // Can be thrown by "loadComponentFromURL()" call.
                // The only thing we should do then is to reset changed frame name!
                System.Diagnostics.Debug.WriteLine(exIo);
                xDocument = null;
                if (sOldName != null)
                {
                    xFrame.setName(sOldName);
                }
            }
            catch (IllegalArgumentException exIllegal)
            {
                // Can be thrown by "loadComponentFromURL()" call.
                // The only thing we should do then is to reset changed frame name!
                System.Diagnostics.Debug.WriteLine(exIllegal);
                xDocument = null;
                if (sOldName != null)
                {
                    xFrame.setName(sOldName);
                }
            }
            catch (RuntimeException exRuntime)
            {
                // Any UNO method of this scope can throw this exception.
                // The only thing we can try(!) is to reset changed frame name.
                System.Diagnostics.Debug.WriteLine(exRuntime);
                xDocument = null;
                if (sOldName != null)
                {
                    xFrame.setName(sOldName);
                }
            }
            catch (unoidl.com.sun.star.uno.Exception exUno)
            {
                // "createInstance()" method of used service manager can throw it.
                // The only thing we should do then is to reset changed frame name!
                System.Diagnostics.Debug.WriteLine(exUno);
                xDocument = null;
                if (sOldName != null)
                {
                    xFrame.setName(sOldName);
                }
            }

            return(xDocument);
        }
 public AbstactUnoDialogBase() : this(OO.GetContext())
 {
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ToolBar"/> class.
        /// </summary>
        /// <param name="xMsf">the XMultiComponentFactory.</param>
        /// <param name="name">The UIName Parameter of the ToolBar.</param>
        /// <param name="documentModluleIdentifier">The document modlule identifier for the ToolBar.
        /// E.g. "com.sun.star.text.TextDocument" to add the ToolBar to all text documents</param>
        public ToolBar(string name, string documentModluleIdentifier, XMultiComponentFactory xMsf = null)
        {
            if (name.Trim().Length < 1)
            {
                throw new NullReferenceException("Name for ToolBar creation is empty");
            }

            XMcf        = xMsf ?? OO.GetMultiComponentFactory();
            Name        = name;
            RecourceUrl = GetToolBarUrl(name);

            // Retrieve the module configuration manager supplier to  get
            // the configuration setting from the module
            _xModCfgMgrSupplier =
                (XModuleUIConfigurationManagerSupplier)
                XMcf.createInstanceWithContext(OO.Services.UI_MOD_UI_CONF_MGR_SUPPLIER, OO.GetContext());

            // Retrieve the module configuration manager
            XCfgMgr =
                _xModCfgMgrSupplier.getUIConfigurationManager(documentModluleIdentifier);

            if (XCfgMgr.hasSettings(RecourceUrl) == false)
            {
                // Creates a copy of the user interface configuration manager
                Settings = XCfgMgr.createSettings();

                // Defines the name of the toolbar
                var xToolbarProperties = (XPropertySet)Settings;
                xToolbarProperties.setPropertyValue("UIName", Any.Get(name));

                // Makes the toolbar to the UI manager available
                try
                {
                    XCfgMgr.insertSettings(RecourceUrl, Settings);
                }
                catch (ElementExistException)
                {
                    Settings = XCfgMgr.getSettings(RecourceUrl, true) as XIndexContainer;
                }
            }
            else
            {
                Settings = XCfgMgr.getSettings(RecourceUrl, true) as XIndexContainer;
            }
        }
        public XWindow createItemWindow(XWindow xWindow)
        {
            // xMSF is set by initialize(Object[])
            try
            {
                // get XWindowPeer
                XWindowPeer xWinPeer = (XWindowPeer)xWindow;
                Object      o        = _xMsf.createInstanceWithContext("com.sun.star.awt.Toolkit", OO.GetContext());
                XToolkit    xToolkit = (XToolkit)o;
                // create WindowDescriptor
                WindowDescriptor wd = new WindowDescriptor();
                wd.Type              = WindowClass.SIMPLE;
                wd.Parent            = xWinPeer;
                wd.Bounds            = new Rectangle(0, 0, 20, 23);
                wd.ParentIndex       = (short)-1;
                wd.WindowAttributes  = WindowAttribute.SHOW;
                wd.WindowServiceName = "pushbutton";
                // create Button
                XWindowPeer cBox_xWinPeer = xToolkit.createWindow(wd);// null here
                var         xButton       = (XButton)cBox_xWinPeer;
                xButton.setLabel("My Texte");
                XWindow cBox_xWindow = (XWindow)cBox_xWinPeer;
                cBox_xWindow.addKeyListener(this);

                return(cBox_xWindow);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("createItemWindow left not o.k.\n" + e);
                return(null);
            }
        }
Beispiel #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        public static Object GetGraphicFromUrl(string url)
        {
            try
            {
                var msf = OO.GetMultiComponentFactory();
                if (msf != null)
                {
                    var graphicProvider = msf.createInstanceWithContext(OO.Services.GRAPHIC_GRAPHICPROVIDER, OO.GetContext());
                    if (graphicProvider != null && graphicProvider is XGraphicProvider)
                    {
                        PropertyValue val = new PropertyValue();
                        val.Name = "URL";

                        val.Value = Any.Get(CreateBitmapUrl(url));
                        PropertyValue val2 = new PropertyValue();
                        val2.Name  = "MimeType";
                        val2.Value = Any.Get(@"image/png");
                        var graphic = ((XGraphicProvider)graphicProvider).queryGraphic(new PropertyValue[] { val });
                        return(graphic);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Log(LogPriority.DEBUG, "oOUtils", "Cannot get Graphic provider: " + ex);
            } return(null);
        }