|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Problems with handling a xTextEmbeddedObjectHi everybody,
my teammate is trying to insert an OLE object in a writer document like in the OOo menu structure "create from file" with a given ODS file. That's what he got for now. But the inserted object shows always only an extract of the table which is just ca. nine cm in width and ca. three cm in height. But he wants to see the entire table, if the table is bigger than the size of 9x3. Here's the code so far: //this.docController.getXInterface is just a wrapper for UnoRuntime.queryInterface XTextContent xTextEmbeddedObject = (XTextContent) this.docController.getXInterface( XTextContent.class, xMultiServiceFactory.createInstance("com.sun.star.text.TextEmbeddedObject")); XPropertySet xTextEmbeddedObjectPropertySet = (XPropertySet) this.docController.getXInterface( XPropertySet.class, xTextEmbeddedObject); xTextEmbeddedObjectPropertySet.setPropertyValue("CLSID", "47BBB4CB-CE4C-4E80-a591-42d9ae74950f"); xNamed = (XNamed) this.docController.getXInterface(XNamed.class, xTextEmbeddedObject); xNamed.setName(this.docController.getLanguageController().__("Table: ") + aSource.getShortinfo()); xFrameText.insertTextContent(xFrameCursor, xTextEmbeddedObject, false); XEmbeddedObjectSupplier xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier) this.docController.getXInterface(XEmbeddedObjectSupplier.class, xTextEmbeddedObject); XComponent xEmbeddedObjectModel = xEmbeddedObjectSupplier.getEmbeddedObject(); XSpreadsheetDocument xEmbeddedObjectModelSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xEmbeddedObjectModel); String[] xEmbeddedObjectModelSpreadsheets = xEmbeddedObjectModelSpreadsheetDocument.getSheets().getElementNames(); XSpreadsheets xAllSpreadsheets = xEmbeddedObjectModelSpreadsheetDocument.getSheets(); Object sheetObj = xAllSpreadsheets.getByName(xEmbeddedObjectModelSpreadsheets[0]); XSpreadsheet xSpreadsheet = null; xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj); XSheetLinkable xSheetLinkable = (XSheetLinkable) this.docController.getXInterface(XSheetLinkable.class, xSpreadsheet); xSheetLinkable.link(sURL, "", "", "", com.sun.star.sheet.SheetLinkMode.NORMAL); How can he manipulate the size of the embedded object? Thx for helping and regards Benjamin --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@... For additional commands, e-mail: dev-help@... |
|
|
Re: Problems with handling a xTextEmbeddedObjectHello Benjamin,
On Sunday 21 June 2009, 08:20, bvollmer@... wrote: > Hi everybody, > my teammate is trying to insert an OLE object in a writer document > like in the OOo menu structure "create from file" with a given ODS > file. That's what he got for now. But the inserted object shows always > only an extract of the table which is just ca. nine cm in width and > ca. three cm in height. But he wants to see the entire table, if the > table is bigger than the size of 9x3. ... > How can he manipulate the size of the embedded object? see the Java source code attached. @Mikhail Voytenko: I took the freedom to CCed you (IIRC you master the subject). Here are some doubts I had when looking at the code that used to work fine in OOo 2.x: * if you see the C++ code attached, in previous versions I had to change first the size of the BaseFrame object's shape (that is, change the size of the visual representation), and then change the embedded object's visual area size. Now changing the embedded object's visual area updates automatically the shape's size, am I right? Changing first the shape's size makes the spreadsheet document rows higher. * May be an issue (or my code is bad...): I'm changing the embedded object's visual area size, so that it's width is no wider than the width of (the text document page - left/right margins). But the resulting width is indeed wider. See the example output: Embedded object Info: getAspect() = 1 getMapUnit() = 0 getVisualAreaSize().Width = 9,03 cm getVisualAreaSize().Height = 2,13 cm BaseFrame object Info: Width = 9,03 cm Height = 2,13 cm Spreadsheet Used Area size: Width = 22,67 cm Height = 6,41 cm TextDocument page text area size: Width = 17,00 cm Height = 25,70 cm New size for the embedded object: Width = 17,00 cm Height = 6,41 cm Press ENTER to change the size of embedded object's visual area BaseFrame object new size: Width = 18,07 cm Height = 6,38 cm Regards -- Ariel Constenla-Haile La Plata, Argentina [EmbeddedCalcResizer.java] /* * EmbeddedCalcResizer.java * */ package org.openoffice.sdk; import com.sun.star.awt.Size; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.container.XIndexAccess; import com.sun.star.container.XNameAccess; import com.sun.star.document.XEmbeddedObjectSupplier2; import com.sun.star.embed.XEmbeddedObject; import com.sun.star.frame.FrameSearchFlag; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.sheet.XCellRangeAddressable; import com.sun.star.sheet.XSheetCellCursor; import com.sun.star.sheet.XSpreadsheet; import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.sheet.XSpreadsheets; import com.sun.star.sheet.XUsedAreaCursor; import com.sun.star.style.XStyleFamiliesSupplier; import com.sun.star.table.CellRangeAddress; import com.sun.star.table.XCell; import com.sun.star.table.XColumnRowRange; import com.sun.star.table.XTableColumns; import com.sun.star.table.XTableRows; import com.sun.star.text.ControlCharacter; import com.sun.star.text.XText; import com.sun.star.text.XTextContent; import com.sun.star.text.XTextCursor; import com.sun.star.text.XTextDocument; import com.sun.star.text.XTextRange; import com.sun.star.uno.XComponentContext; import com.sun.star.uno.AnyConverter; import com.sun.star.uno.UnoRuntime; import java.io.BufferedReader; import java.io.InputStreamReader; /** * * @author ariel */ public class EmbeddedCalcResizer { private XComponentContext m_xContext; /** Creates a new instance of EmbeddedCalcResizer */ public EmbeddedCalcResizer(XComponentContext xContext) { this.m_xContext = xContext; } /** * @param args the command line arguments */ public static void main(String[] args) { int nRet = 0; try { // get the remote office component context XComponentContext xContext = Bootstrap.bootstrap(); if (xContext == null) { System.err.println("ERROR: Could not bootstrap default Office."); nRet = -1; } EmbeddedCalcResizer demo = new EmbeddedCalcResizer(xContext); demo.runDemo(); waitForInput("\nPress ENTER to finish the example\n"); } catch (java.lang.Exception e) { e.printStackTrace(); } finally { System.exit(nRet); } } protected void runDemo() { try { // load a new empty OOo Writer document XTextDocument xTextDocument = createWriterDoc(m_xContext); // get the document's factory XMultiServiceFactory xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, xTextDocument); // get the XText interface XText xText = xTextDocument.getText(); // create a text cursor XTextCursor xTextCursor = xText.createTextCursor(); xTextCursor.gotoStart(false); // insert a paragraph brake insertParaBreak(xTextCursor.getEnd()); // create the Calc text embedded oject XPropertySet xBaseFrameProps = createTextEmbeddedObject_Calc(xDocFactory); // insert the object insertTextContent( xTextCursor.getEnd(), (XTextContent) UnoRuntime.queryInterface( XTextContent.class, xBaseFrameProps)); insertParaBreak(xTextCursor.getEnd()); XEmbeddedObjectSupplier2 xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier2) UnoRuntime.queryInterface( XEmbeddedObjectSupplier2.class, xBaseFrameProps); // XEmbeddedObjectSupplier2::getExtendedControlOverEmbeddedObject() // does not return the model that is "controlled" by the embedded object, // but the embedded object itself. XEmbeddedObject xEmbeddedObject = xEmbeddedObjectSupplier.getExtendedControlOverEmbeddedObject(); // get the actual aspect of the embedded object // ::com::sun::star::embed::Aspects // this is needed for ::com::sun::star::embed::XVisualObject::setVisualAreaSize() // and ::com::sun::star::embed::XVisualObject::getMapUnit() long nAspect = xEmbeddedObjectSupplier.getAspect(); // ::com::sun::star::embed::XVisualObject::getMapUnit() // retrieves map mode the object communicates in // the Size we pass in setVisualAreaSize() must follow this map mode // Returns one of ::com::sun::star::embed::EmbedMapUnits long aMapUnit = xEmbeddedObject.getMapUnit(nAspect); // getVisualAreaSize() will show us that the visual area size is equal // to the Size of the BaseFrame object shape Size aVisualAreaSize = xEmbeddedObject.getVisualAreaSize(nAspect); System.out.printf("\n\nEmbedded object Info:\n" + "\tgetAspect() = %d\n" + "\tgetMapUnit() = %d\n" + "\tgetVisualAreaSize().Width = %.2f cm\n" + "\tgetVisualAreaSize().Height = %.2f cm\n", nAspect, aMapUnit, ((double) aVisualAreaSize.Width/ 1000), ((double) aVisualAreaSize.Height/ 1000)); // let's see the default size asigned to the BaseFrame object shape Size aBaseFrameSize = new Size(); aBaseFrameSize.Width = AnyConverter.toInt( xBaseFrameProps.getPropertyValue("Width")); aBaseFrameSize.Height = AnyConverter.toInt( xBaseFrameProps.getPropertyValue("Height")); System.out.printf("\n\nBaseFrame object Info:\n" + "\tWidth = %.2f cm\n" + "\tHeight = %.2f cm\n", ((double) aBaseFrameSize.Width/ 1000), ((double) aBaseFrameSize.Height/ 1000)); // access the model that is controlled by the embedded object // some embedded ojects have NO model, but here we are sure // because it is an office document XComponent xComponent = xEmbeddedObjectSupplier.getEmbeddedObject(); // the model is an XSpreadsheetDocument XSpreadsheetDocument xSheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface( XSpreadsheetDocument.class, xComponent); // get the sheet at index 0 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets(); XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xSpreadsheets); XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( XSpreadsheet.class, xIndexAccess.getByIndex(0)); // insert some values for (int i = 0; i < 10; i++) { for (int n = 0; n < 15; n++) { XCell xCell = xSpreadsheet.getCellByPosition(i, n); xCell.setFormula(String.valueOf(2.35 * (i + n))); } } // get the used area in the embedded sheet CellRangeAddress aUsedArea = getUsedArea(xSpreadsheet); // calulate the size of the used area Size aUsedAreaSize = calcCellRangeSize(xSpreadsheets, aUsedArea); // here we suppose that both the map mode in which the object communicates, // and the width and height of columns and rows // are mesured in 1/100 mm ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM // May this not be the case, we must convert the size of the sheet used area System.out.printf("\n\nSpreadsheet Used Area size:\n" + "\tWidth = %.2f cm\n" + "\tHeight = %.2f cm\n", ((double) aUsedAreaSize.Width / 1000), ((double) aUsedAreaSize.Height / 1000)); // do we need to resize the object? if ( ( aUsedAreaSize.Width != aVisualAreaSize.Width ) || ( aUsedAreaSize.Height != aVisualAreaSize.Height ) ) { // calculate the size of the TextDocument page text area Size aPageTextAreaSize = getPageTextAreaSize(xTextDocument, xTextCursor); System.out.printf("\n\nTextDocument page text area size:\n" + "\tWidth = %.2f cm\n" + "\tHeight = %.2f cm\n", ((double) aPageTextAreaSize.Width / 1000), ((double) aPageTextAreaSize.Height / 1000)); // if the size of the BaseFrame shape is bigger than the page text area // it will look awful, so... Size aNewSize = new Size(); aNewSize.Height = (aUsedAreaSize.Height > aPageTextAreaSize.Height) ? aPageTextAreaSize.Height : aUsedAreaSize.Height; aNewSize.Width = (aUsedAreaSize.Width > aPageTextAreaSize.Width) ? aPageTextAreaSize.Width : aUsedAreaSize.Width; System.out.printf("\n\nNew size for the embedded object:\n" + "\tWidth = %.2f cm\n" + "\tHeight = %.2f cm\n", ((double) aNewSize.Width / 1000), ((double) aNewSize.Height / 1000)); // NOW there is no need to change first the size of the BaseFrame object's shape // waitForInput("Press ENTER to change the size of the BaseFrame object"); // first change the size of the visual representation //xEmbeddedProps.setPropertyValue("Width", new Integer(aNewSize.Width)); //xEmbeddedProps.setPropertyValue("Height", new Integer(aNewSize.Height)); // NOW changing the embedded object's visual area update automatically the shape's size waitForInput("\nPress ENTER to change the size of embedded object's visual area"); // make the embedded object visual area fit in the new size xEmbeddedObject.setVisualAreaSize(nAspect, aNewSize); // let's see if the BaseFrame object shape's size has also changed aBaseFrameSize.Width = AnyConverter.toInt( xBaseFrameProps.getPropertyValue("Width")); aBaseFrameSize.Height = AnyConverter.toInt( xBaseFrameProps.getPropertyValue("Height")); System.out.printf("\n\nBaseFrame object new size:\n" + "\tWidth = %.2f cm\n" + "\tHeight = %.2f cm\n", ((double) aBaseFrameSize.Width / 1000), ((double) aBaseFrameSize.Height / 1000)); } } catch (Exception e) { e.printStackTrace(); } } //========================================================================= // // Text document helpers /** * * @param xTextRange * @param sStr */ public static void insertText(XTextRange xTextRange, String sStr) { xTextRange.getText().insertString(xTextRange, sStr, false); } /** * * @param xTextRange */ public static void insertParaBreak(XTextRange xTextRange) { try { xTextRange.getText().insertControlCharacter( xTextRange, ControlCharacter.PARAGRAPH_BREAK, false); } catch (com.sun.star.lang.IllegalArgumentException e) { e.printStackTrace(); } } /** * * @param xTextRange * @param xContent */ public static void insertTextContent(XTextRange xTextRange, XTextContent xContent) { try { xTextRange.getText().insertTextContent(xTextRange, xContent, false); } catch (Exception e) { e.printStackTrace(); } } /** * * @param xDocFactory * @param sCLSID * @return */ public static XPropertySet createTextEmbeddedObject( XMultiServiceFactory xDocFactory, String sCLSID) { XPropertySet xPropertySet = null; try { xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xDocFactory.createInstance("com.sun.star.text.TextEmbeddedObject")); xPropertySet.setPropertyValue("CLSID", sCLSID); } catch (Exception e) { e.printStackTrace(); } finally { return xPropertySet; } } /** * * @param xDocFactory * @return */ public static XPropertySet createTextEmbeddedObject_Calc( XMultiServiceFactory xDocFactory) { return createTextEmbeddedObject(xDocFactory, "47BBB4CB-CE4C-4E80-a591-42d9ae74950f"); } /** * * @param xTextDocument * @param xTextCursor * @return */ public static Size getPageTextAreaSize(XTextDocument xTextDocument, XTextCursor xTextCursor) { Size aSize = new Size(); try { XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xTextCursor); String sPageStyleName = AnyConverter.toString( xPropertySet.getPropertyValue("PageStyleName")); XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface( XStyleFamiliesSupplier.class, xTextDocument); XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies(); XNameAccess xPageStyles = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xStyleFamilies.getByName("PageStyles")); xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xPageStyles.getByName(sPageStyleName)); int iPageWidth, iPageHeight; iPageWidth = AnyConverter.toInt(xPropertySet.getPropertyValue("Width")); iPageHeight = AnyConverter.toInt(xPropertySet.getPropertyValue("Height")); int iBottomMargin, iTopMargin; iBottomMargin = AnyConverter.toInt(xPropertySet.getPropertyValue("BottomMargin")); iTopMargin = AnyConverter.toInt(xPropertySet.getPropertyValue("TopMargin")); int iLeftMargin, iRightMargin; iLeftMargin = AnyConverter.toInt(xPropertySet.getPropertyValue("LeftMargin")); iRightMargin = AnyConverter.toInt(xPropertySet.getPropertyValue("RightMargin")); aSize.Height = iPageHeight - iBottomMargin - iTopMargin; aSize.Width = iPageWidth - iLeftMargin - iRightMargin; } catch (Exception e) { e.printStackTrace(); } finally { return aSize; } } /** * * @param rContext * @return */ public static XTextDocument createWriterDoc(XComponentContext xContext) { return (XTextDocument) UnoRuntime.queryInterface( XTextDocument.class, createNewDoc(xContext, "swriter")); } //========================================================================= // // Spreadsheet document helpers /** * * @param xSpreadsheet * @return */ public static CellRangeAddress getUsedArea(XSpreadsheet xSpreadsheet) { CellRangeAddress aCellRangeAddress = null; try { XSheetCellCursor xSheetCellCursor = xSpreadsheet.createCursor(); XUsedAreaCursor xUsedAreaCursor = (XUsedAreaCursor) UnoRuntime.queryInterface( XUsedAreaCursor.class, xSheetCellCursor); xUsedAreaCursor.gotoStartOfUsedArea(false); xUsedAreaCursor.gotoEndOfUsedArea(true); XCellRangeAddressable xCellRangeAddressable = (XCellRangeAddressable) UnoRuntime.queryInterface( XCellRangeAddressable.class, xUsedAreaCursor); aCellRangeAddress = xCellRangeAddressable.getRangeAddress(); } catch (Exception e) { e.printStackTrace(); } finally { return aCellRangeAddress; } } /** * * @param xSpreadsheets * @param aCellRangeAddress * @return */ public static Size calcCellRangeSize(XSpreadsheets xSpreadsheets, CellRangeAddress aCellRangeAddress) { Size aSize = new Size(); try { XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xSpreadsheets); XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( XSpreadsheet.class, xIndexAccess.getByIndex(aCellRangeAddress.Sheet)); XColumnRowRange xColumnRowRange = (XColumnRowRange) UnoRuntime.queryInterface( XColumnRowRange.class, xSpreadsheet); XTableColumns xTableColumns = xColumnRowRange.getColumns(); XTableRows xTableRows = xColumnRowRange.getRows(); int aTotalColWidth = 0; for (int i = aCellRangeAddress.StartColumn; i <= aCellRangeAddress.EndColumn; i++) { XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xTableColumns.getByIndex(i)); int iWidth = AnyConverter.toInt( xPropertySet.getPropertyValue("Width")); aTotalColWidth += iWidth; } int aTotalRowHeight = 0; for (int i = aCellRangeAddress.StartRow; i <= aCellRangeAddress.EndRow; i++) { XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xTableRows.getByIndex(i)); int iHeight = AnyConverter.toInt( xPropertySet.getPropertyValue("Height")); aTotalRowHeight += iHeight; } aSize = new Size(aTotalColWidth, aTotalRowHeight); } catch (Exception e) { e.printStackTrace(); } finally { return aSize; } } //========================================================================= // // General helpers /** * * @param rContext * @param docType * @return */ public static XComponent createNewDoc(XComponentContext xContext, String docType) { XComponent xComponent = null; try { XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext( "com.sun.star.frame.Desktop", xContext)); xComponent = xComponentLoader.loadComponentFromURL( "private:factory/" + docType, "_blank", FrameSearchFlag.ALL, new PropertyValue[0]); } catch (Exception e) { e.printStackTrace(); } finally { return xComponent; } } public static String waitForInput(String sMessage) { String sInput = null; try { System.out.print(sMessage); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); sInput = br.readLine(); } catch (Exception e) { e.printStackTrace(); } finally { return sInput; } } } [EmbeddedCalcResizer.cxx] #include <iostream> #include <cstdio> #include <cppuhelper/bootstrap.hxx> #include <rtl/ustring.hxx> /** === begin UNO includes === **/ #include <com/sun/star/awt/Size.hpp> #include <com/sun/star/beans/PropertyValue.hpp> #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/container/XIndexAccess.hpp> #include <com/sun/star/container/XNameAccess.hpp> #include <com/sun/star/document/XEmbeddedObjectSupplier2.hpp> #include <com/sun/star/embed/XEmbeddedObject.hpp> #include <com/sun/star/frame/XComponentLoader.hpp> #include <com/sun/star/io/IOException.hpp> #include <com/sun/star/lang/IllegalArgumentException.hpp> #include <com/sun/star/lang/XComponent.hpp> #include <com/sun/star/lang/XMultiComponentFactory.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/sheet/XCellRangeAddressable.hpp> #include <com/sun/star/sheet/XCellRangeData.hpp> #include <com/sun/star/sheet/XSheetCellCursor.hpp> #include <com/sun/star/sheet/XSpreadsheet.hpp> #include <com/sun/star/sheet/XSpreadsheetDocument.hpp> #include <com/sun/star/sheet/XSpreadsheets.hpp> #include <com/sun/star/sheet/XUsedAreaCursor.hpp> #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> #include <com/sun/star/table/CellRangeAddress.hpp> #include <com/sun/star/table/XCell.hpp> #include <com/sun/star/table/XColumnRowRange.hpp> #include <com/sun/star/table/XTableColumns.hpp> #include <com/sun/star/table/XTableRows.hpp> #include <com/sun/star/text/ControlCharacter.hpp> #include <com/sun/star/text/XText.hpp> #include <com/sun/star/text/XTextContent.hpp> #include <com/sun/star/text/XTextCursor.hpp> #include <com/sun/star/text/XTextDocument.hpp> #include <com/sun/star/text/XTextRange.hpp> #include <com/sun/star/text/XTextTable.hpp> #include <com/sun/star/text/XTextTableCursor.hpp> #include <com/sun/star/uno/XComponentContext.hpp> /** === end UNO includes === **/ /** === begin UNO using === **/ using ::com::sun::star::awt::Size; using ::com::sun::star::beans::PropertyValue; using ::com::sun::star::beans::XPropertySet; using ::com::sun::star::container::XIndexAccess; using ::com::sun::star::container::XNameAccess; using ::com::sun::star::document::XEmbeddedObjectSupplier2; using ::com::sun::star::embed::XEmbeddedObject; using ::com::sun::star::frame::XComponentLoader; using ::com::sun::star::lang::XComponent; using ::com::sun::star::lang::XMultiComponentFactory; using ::com::sun::star::lang::XMultiServiceFactory; using ::com::sun::star::sheet::XCellRangeAddressable; using ::com::sun::star::sheet::XCellRangeData; using ::com::sun::star::sheet::XSheetCellCursor; using ::com::sun::star::sheet::XSpreadsheet; using ::com::sun::star::sheet::XSpreadsheetDocument; using ::com::sun::star::sheet::XSpreadsheets; using ::com::sun::star::sheet::XUsedAreaCursor; using ::com::sun::star::style::XStyleFamiliesSupplier; using ::com::sun::star::table::CellRangeAddress; using ::com::sun::star::table::XCell; using ::com::sun::star::table::XColumnRowRange; using ::com::sun::star::table::XTableColumns; using ::com::sun::star::table::XTableRows; using ::com::sun::star::text::XText; using ::com::sun::star::text::XTextContent; using ::com::sun::star::text::XTextCursor; using ::com::sun::star::text::XTextDocument; using ::com::sun::star::text::XTextRange; using ::com::sun::star::text::XTextTable; using ::com::sun::star::text::XTextTableCursor; using ::com::sun::star::uno::Any; using ::com::sun::star::uno::Exception; using ::com::sun::star::uno::makeAny; using ::com::sun::star::uno::Reference; using ::com::sun::star::uno::RuntimeException; using ::com::sun::star::uno::RuntimeException; using ::com::sun::star::uno::Sequence; using ::com::sun::star::uno::UNO_QUERY_THROW; using ::com::sun::star::uno::UNO_QUERY; using ::com::sun::star::uno::XComponentContext; using ::com::sun::star::uno::XInterface; /** === end UNO using === **/ using namespace rtl; using namespace std; namespace ControlCharacter = ::com::sun::star::text::ControlCharacter; //================================================================================== Reference< XComponent > createNewDoc(const Reference< XComponentContext >& rContext, const OUString & docType ); //================================================================================== void insertText( const Reference< XTextRange > & xTextRange, const OUString & uString); void insertParaBreak( const Reference< XTextRange > & xTextRange); void insertTextContent( const Reference< XTextRange > & xTextRange, const Reference< XTextContent > & xContent); Reference< XPropertySet > createTextEmbeddedObject( const Reference< XMultiServiceFactory >& xDocFactory, const OUString & CLSID); Reference< XPropertySet > createTextEmbeddedObject_Calc( const Reference< XMultiServiceFactory >& xDocFactory); const Size getPageTextAreaSize( const Reference< XTextDocument > & xTextDocument, const Reference< XTextCursor >& xTextCursor); Reference< XTextDocument > createWriterDoc( const Reference< XComponentContext >& rContext); //================================================================================== const CellRangeAddress getUsedArea( const Reference< XSpreadsheet >& xSpreadsheet); const Size calcCellRangeSize( const Reference< XSpreadsheets >& xSpreadsheets, const CellRangeAddress & aCellRangeAddress); //================================================================================== int SAL_CALL main( int argc, char* argv[] ) { try { // bootstrap the office Reference< XComponentContext > rContext ( ::cppu::bootstrap() ); Reference< XMultiComponentFactory > rMCF = rContext->getServiceManager(); // load a new empty OOo Writer document Reference< XTextDocument > rTextDocument = createWriterDoc( rContext ); // get the document's factory Reference< XMultiServiceFactory > rDocFactory ( rTextDocument, UNO_QUERY ); // get the XText interface Reference< XText > rText = rTextDocument->getText(); // create a text cursor Reference< XTextCursor > rTextCursor = rText->createTextCursor(); rTextCursor->gotoStart( sal_False ); // insert a paragraph brake insertParaBreak( rTextCursor->getEnd() ); // create the Calc text embedded oject Reference< XPropertySet > xEmbeddedProps = createTextEmbeddedObject_Calc( rDocFactory ); // insert the object insertTextContent( rTextCursor->getEnd(), Reference< XTextContent >(xEmbeddedProps, UNO_QUERY) ); insertParaBreak( rTextCursor->getEnd() ); Reference< XEmbeddedObjectSupplier2 > xEmbeddedObjectSupplier ( xEmbeddedProps, UNO_QUERY ); // XEmbeddedObjectSupplier2::getExtendedControlOverEmbeddedObject() // does not return the model that is controlled by the embedded object, // but the embedded object itself. Reference< XEmbeddedObject > xEmbeddedObject = xEmbeddedObjectSupplier->getExtendedControlOverEmbeddedObject(); // get the actual aspect of the embedded object // ::com::sun::star::embed::Aspects // this is needed for ::com::sun::star::embed::XVisualObject::setVisualAreaSize() // and ::com::sun::star::embed::XVisualObject::getMapUnit() sal_Int64 nAspect = xEmbeddedObjectSupplier->getAspect(); // ::com::sun::star::embed::XVisualObject::getMapUnit() retrieves map mode the object communicates in // the Size we pass in setVisualAreaSize() must follow this map mode // Returns one of ::com::sun::star::embed::EmbedMapUnits sal_Int32 aMapUnit = xEmbeddedObject->getMapUnit( nAspect ); // getVisualAreaSize() will show us that the visual area size is equal to the Size of the BaseFrame object shape Size aSize = xEmbeddedObject->getVisualAreaSize( nAspect ); // access the model that is controlled by the embedded object // some embedded ojects have NO model, but here we are sure // because it is an office document Reference< XComponent > xComponent = xEmbeddedObjectSupplier->getEmbeddedObject(); // is the same as /*Reference< com::sun::star::embed::XComponentSupplier > xComponentSupplier ( xEmbeddedObject, UNO_QUERY ); Reference< com::sun::star::util::XCloseable > xEmbeddedComp = xComponentSupplier->getComponent(); */ // the model is an XSpreadsheetDocument Reference< XSpreadsheetDocument > xSheetDoc ( xComponent, UNO_QUERY ); // get the sheet at index 0 Reference< XSpreadsheets > xSpreadsheets = xSheetDoc->getSheets(); Reference< XIndexAccess > xIndexAccess ( xSpreadsheets, UNO_QUERY ); Reference< XSpreadsheet > xSpreadsheet ( xIndexAccess->getByIndex( sal_Int32(0) ), UNO_QUERY ); // insert some values for (sal_Int32 i = 0; i < 10; i++) { for (sal_Int32 n = 0; n < 15; n++) { Reference< XCell > xCell = xSpreadsheet->getCellByPosition(i, n); xCell->setFormula( OUString::valueOf( 2.35 * (i + n) ) ); } } // get the used area in the embedded sheet CellRangeAddress aUsedArea = getUsedArea( xSpreadsheet ); // calulate the size of the used area Size aUsedAreaSize = calcCellRangeSize(xSpreadsheets, aUsedArea); // here we suppose that both the map mode in which the object communicates, // and the width and height of columns and rows // are mesured in 1/100 mm ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM // May this not be the case, we must convert the size of thesheet used area printf( "Used area size : %.2f cm x %.2f cm\n\n", ( (double) aUsedAreaSize.Width/1000), ( (double) aUsedAreaSize.Height/1000) ); // calculate the size of the page text area Size aPageTextAreaSize = getPageTextAreaSize(rTextDocument, rTextCursor); // if the size of the BaseFrame shape is bigger than the page text area // it will look awful, so... Size aNewSize; aNewSize.Height = (aUsedAreaSize.Height > aPageTextAreaSize.Height) ? aPageTextAreaSize.Height : aUsedAreaSize.Height ; aNewSize.Width = (aUsedAreaSize.Width > aPageTextAreaSize.Width) ? aPageTextAreaSize.Width : aUsedAreaSize.Width ; cout << "Press ENTER to change the size of the BaseFrame object"; cin.get(); // first change the size of the visual representation xEmbeddedProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), makeAny(aNewSize.Width) ); xEmbeddedProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), makeAny(aNewSize.Height) ); cout << "Press ENTER to change the size of embedded object's visual area"; cin.get(); // make the embedded object visual area fit in the new size xEmbeddedObject->setVisualAreaSize(nAspect, aNewSize); cout << "Press ENTER to finish the example"; cin.get(); } catch ( Exception& e ) { cerr << "caught UNO exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; return 1; } return 0; } ///////////////////////////////////////////////////////////////////////////////// void insertText( const Reference< XTextRange > & xTextRange, const OUString & uString) { xTextRange->getText()->insertString( xTextRange, uString, sal_False ); } void insertParaBreak( const Reference< XTextRange > & xTextRange) { try { xTextRange->getText()->insertControlCharacter( xTextRange, ControlCharacter::PARAGRAPH_BREAK, sal_False); } catch (com::sun::star::lang::IllegalArgumentException& e) { cerr << "caught ::com::sun::star::lang::IllegalArgumentException: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } } void insertTextContent( const Reference< XTextRange > & xTextRange, const Reference< XTextContent > & xContent) { try { xTextRange->getText()->insertTextContent( xTextRange, xContent, sal_False ); } catch (Exception& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } } Reference< XPropertySet > createTextEmbeddedObject( const Reference< XMultiServiceFactory >& xDocFactory, const OUString & CLSID) { Reference< XPropertySet > xPropertySet; try { xPropertySet = Reference< XPropertySet >( xDocFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextEmbeddedObject") ) ) , UNO_QUERY ); xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("CLSID") ), makeAny(CLSID) ); } catch (Exception& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } return xPropertySet; } Reference< XPropertySet > createTextEmbeddedObject_Calc( const Reference< XMultiServiceFactory >& xDocFactory) { return createTextEmbeddedObject(xDocFactory, OUString( RTL_CONSTASCII_USTRINGPARAM( "47BBB4CB-CE4C-4E80-a591-42d9ae74950f" ) )); } const Size getPageTextAreaSize( const Reference< XTextDocument > & xTextDocument, const Reference< XTextCursor >& xTextCursor) { Size aSize; try { Reference< XPropertySet > xPropertySet ( xTextCursor, UNO_QUERY ); OUString sPageStyleName; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("PageStyleName")) ) >>= sPageStyleName; Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier ( xTextDocument, UNO_QUERY ); Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies(); Reference< XNameAccess > xPageStyles; xStyleFamilies->getByName( OUString(RTL_CONSTASCII_USTRINGPARAM("PageStyles")) ) >>= xPageStyles; xPageStyles->getByName(sPageStyleName) >>= xPropertySet; sal_Int32 iPageWidth, iPageHeight; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Width")) ) >>= iPageWidth; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Height")) ) >>= iPageHeight; sal_Int32 iBottomMargin, iTopMargin; sal_Int32 iLeftMargin, iRightMargin; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("BottomMargin")) ) >>= iBottomMargin; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("TopMargin")) ) >>= iTopMargin; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("LeftMargin")) ) >>= iLeftMargin; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("RightMargin")) ) >>= iRightMargin; aSize.Height = iPageHeight - iBottomMargin - iTopMargin; aSize.Width = iPageWidth - iLeftMargin - iRightMargin; } catch (RuntimeException& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } catch (Exception& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } return aSize; } Reference< XTextDocument > createWriterDoc( const Reference< XComponentContext >& rContext) { return Reference< XTextDocument >( createNewDoc( rContext, OUString( RTL_CONSTASCII_USTRINGPARAM( "swriter" ) ) ), UNO_QUERY ); } /////////////////////////////////////////////////////////////////////////////////////// const CellRangeAddress getUsedArea( const Reference< XSpreadsheet >& xSpreadsheet) { CellRangeAddress aCellRangeAddress; try { Reference< XSheetCellCursor > xSheetCellCursor = xSpreadsheet->createCursor(); Reference< XUsedAreaCursor > xUsedAreaCursor ( xSheetCellCursor, UNO_QUERY ); xUsedAreaCursor->gotoStartOfUsedArea( sal_False ); xUsedAreaCursor->gotoEndOfUsedArea( sal_True ); Reference< XCellRangeAddressable > xCellRangeAddressable( xUsedAreaCursor, UNO_QUERY ); aCellRangeAddress = xCellRangeAddressable->getRangeAddress(); } catch (RuntimeException& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } return aCellRangeAddress; } const Size calcCellRangeSize( const Reference< XSpreadsheets >& xSpreadsheets, const CellRangeAddress & aCellRangeAddress) { Size aSize; try { Reference< XIndexAccess > xIndexAccess ( xSpreadsheets, UNO_QUERY ); Reference< XSpreadsheet > xSpreadsheet; xIndexAccess->getByIndex( aCellRangeAddress.Sheet ) >>= xSpreadsheet; Reference< XColumnRowRange > xColumnRowRange ( xSpreadsheet, UNO_QUERY ); Reference< XTableColumns > xTableColumns = xColumnRowRange->getColumns(); Reference< XTableRows > xTableRows = xColumnRowRange->getRows(); sal_Int32 aTotalColWidth = 0; for ( sal_Int32 i = aCellRangeAddress.StartColumn; i <= aCellRangeAddress.EndColumn; i++ ) { Reference< XPropertySet > xPropertySet ( xTableColumns->getByIndex(i), UNO_QUERY ); sal_Int32 iWidth; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Width") ) ) >>= iWidth; aTotalColWidth += iWidth; } sal_Int32 aTotalRowHeight = 0; for ( sal_Int32 i = aCellRangeAddress.StartRow; i <= aCellRangeAddress.EndRow; i++ ) { Reference< XPropertySet > xPropertySet ( xTableRows->getByIndex(i), UNO_QUERY ); sal_Int32 iHeight; xPropertySet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Height") ) ) >>= iHeight; aTotalRowHeight += iHeight; } aSize = Size( aTotalColWidth, aTotalRowHeight ); } catch (RuntimeException& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } catch (Exception& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } return aSize; } ///////////////////////////////////////////////////////////////////////////////////////// Reference< XComponent > createNewDoc(const Reference< XComponentContext >& rContext, const OUString& docType) { Reference< XComponent > xComponent; try { Reference < XComponentLoader > xComponentLoader( rContext->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), rContext ), UNO_QUERY_THROW ); xComponent = Reference< XComponent >( xComponentLoader->loadComponentFromURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/" ) ).concat( docType ), OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), 0, Sequence < ::com::sun::star::beans::PropertyValue >() ) ); } catch (Exception& e) { cerr << "caught UNO Exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; } return xComponent; } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@... For additional commands, e-mail: dev-help@... |
|
|
Re: Problems with handling a xTextEmbeddedObjectHello Ariel, Benjamin,
On 06/21/09 20:06, Ariel Constenla-Haile wrote: > Here are some doubts I had when looking at the code that used to work fine in > OOo 2.x: > > * if you see the C++ code attached, in previous versions I had to change first > the size of the BaseFrame object's shape (that is, change the size of the > visual representation), and then change the embedded object's visual area > size. > Now changing the embedded object's visual area updates automatically the > shape's size, am I right? > replacement image. From other side, resizing of the replacement image should not affect the object, it is container only change that should let object be scaled. If it was not so before, it was a bug. There were a number of problems with replacement image update in 2.x that should be fixed now. > Changing first the shape's size makes the spreadsheet document rows higher. > Exactly, resizing of the replacement image is a change in the container document. It should not affect the object until the object has a special flag that let it be notified on resize ( the chart objects for example have this flag set, so resizing of the replacement image will resize the object as well ). For normal objects resizing of replacement image let the object be scaled, since the object itself is not changed. In case of Spreadsheet the scaling changes the size of rows and columns. > * May be an issue (or my code is bad...): > > I'm changing the embedded object's visual area size, so that it's width is no > wider than the width of (the text document page - left/right margins). > But the resulting width is indeed wider. When I understand the description correctly it is neither issue nor problem with your source code. The design allows object to decide which size it accepts, you could see it while resizing Spreadsheet object using UI, the object gets closest accepted size to the one you set using the mouse. So in case of your implementation the object gets the closest accepted size to the requested one as well. Hope that helps. Best regards, Mikhail. > See the example output: > > > Embedded object Info: > getAspect() = 1 > getMapUnit() = 0 > getVisualAreaSize().Width = 9,03 cm > getVisualAreaSize().Height = 2,13 cm > > > BaseFrame object Info: > Width = 9,03 cm > Height = 2,13 cm > > > Spreadsheet Used Area size: > Width = 22,67 cm > Height = 6,41 cm > > > TextDocument page text area size: > Width = 17,00 cm > Height = 25,70 cm > > > New size for the embedded object: > Width = 17,00 cm > Height = 6,41 cm > > Press ENTER to change the size of embedded object's visual area > > BaseFrame object new size: > Width = 18,07 cm > Height = 6,38 cm > > > > Regards > > ------------------------------------------------------------------------ > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@... > For additional commands, e-mail: dev-help@... |
|
|
Re: Problems with handling a xTextEmbeddedObjectHello,
many thanks for your fast reply ariel. Your sample code was very helpful, we could solve the problem now. regards Benjamin Quoting Ariel Constenla-Haile <ariel.constenla.haile@...>: > > see the Java source code attached. --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@... For additional commands, e-mail: dev-help@... |
| Free embeddable forum powered by Nabble | Forum Help |