/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace css; using namespace cppu; using namespace css::uno; using namespace css::lang; using namespace css::frame; using namespace css::beans; using namespace css::sheet; using namespace css::table; using namespace css::text; using namespace css::drawing; using namespace css::awt; using namespace css::container; using namespace rtl; using namespace awt; void manipulateText(const Reference& xText); void manipulateTable(const Reference& xCellRange); void manipulateShape(const Reference& xShape); void useDocuments(); void useWriter(); void useCalc(); void useDraw(); Reference getComponentContext(); Reference getRemoteServiceManager(); Reference newDocComponent(const OUString docType); Reference xRemoteContext = NULL; Reference xRemoteServiceManager = NULL; SAL_IMPLEMENT_MAIN() { try { useDocuments(); } catch (RuntimeException& e) { std::cerr << e.Message << "\n"; return 1; } return 0; } void useDocuments() { useWriter(); useCalc(); useDraw(); } void useWriter() { // create new writer document and get text, then manipulate text Reference xWriterComponent = newDocComponent("swriter"); Reference xTextDocument(xWriterComponent, UNO_QUERY_THROW); Reference xText = xTextDocument->getText(); manipulateText(xText); // get internal service factory of the document Reference xWriterFactory = Reference(xTextDocument, UNO_QUERY_THROW); // insert TextTable and get cell text, then manipulate text in cell Reference xTextContentTable = Reference( xWriterFactory->createInstance("com.sun.star.text.TextTable"), UNO_QUERY_THROW); xText->insertTextContent(xText->getEnd(), xTextContentTable, false); Reference xCellRange = Reference(xTextContentTable, UNO_QUERY_THROW); Reference xCell = xCellRange->getCellByPosition(0, 1); Reference xCellText = Reference(xCell, UNO_QUERY_THROW); manipulateText(xCellText); manipulateTable(xCellRange); Reference xWriterShape = Reference( xWriterFactory->createInstance("com.sun.star.drawing.RectangleShape"), UNO_QUERY_THROW); xWriterShape->setSize(Size(10000, 10000)); Reference xTextContentShape(xWriterShape, UNO_QUERY_THROW); xText->insertTextContent(xText->getEnd(), xTextContentShape, false); Reference xShapeProps(xWriterShape, UNO_QUERY_THROW); // wrap text inside shape xShapeProps->setPropertyValue("TextContourFrame", Any(true)); Reference xShapeText = Reference(xWriterShape, UNO_QUERY_THROW); manipulateText(xShapeText); manipulateShape(xWriterShape); // more code snippets used in the manual: Reference bookmark = xWriterFactory->createInstance("com.sun.star.text.Bookmark"); // name the bookmark Reference xNamed(bookmark, UNO_QUERY_THROW); xNamed->setName("MyUniqueBookmarkName"); // get XTextContent interface and insert it at the end of the document Reference xTextContent(bookmark, UNO_QUERY_THROW); xText->insertTextContent(xText->getEnd(), xTextContent, false); //query BookmarksSupplier Reference xBookmarksSupplier(xWriterComponent, UNO_QUERY_THROW); Reference xNamedBookmarks = xBookmarksSupplier->getBookmarks(); Any foundBookmark = xNamedBookmarks->getByName("MyUniqueBookmarkName"); Reference xFoundBookmark(foundBookmark, UNO_QUERY_THROW); Reference xFound = xFoundBookmark->getAnchor(); xFound->setString(" The throat mike, glued to her neck, " "looked as much as possible like an analgesic dermadisk."); // first query the XTextTablesSupplier interface from our document Reference xTablesSupplier(xWriterComponent, UNO_QUERY_THROW); // get the tables collection Reference xNamedTables = xTablesSupplier->getTextTables(); // now query the XIndexAccess from the tables collection Reference xIndexedTables(xNamedTables, UNO_QUERY_THROW); // we need properties Reference xTableProps = NULL; // get the tables for (int i = 0; i < xIndexedTables->getCount(); i++) { //Object table = xIndexedTables.getByIndex(i); Any table = xIndexedTables->getByIndex(i); xTableProps = Reference(table, UNO_QUERY_THROW); xTableProps->setPropertyValue("BackColor", Any(static_cast(0xC8FFB9))); } } void useCalc() { // create new calc document and manipulate cell text Reference xCalcComponent = newDocComponent("scalc"); Reference xCalcDocument(xCalcComponent, UNO_QUERY_THROW); Reference xSpreadsheetDocument = Reference(xCalcDocument, UNO_QUERY); Reference xIndexedSheets(xSpreadsheetDocument->getSheets(), UNO_QUERY); Reference xSpreadsheetCells(xIndexedSheets->getByIndex(0), UNO_QUERY_THROW); //get cell A2 in first sheet Reference xCell = xSpreadsheetCells->getCellByPosition(0, 1); Reference xCellText = Reference(xCell, UNO_QUERY_THROW); Reference xCellProps(xCell, UNO_QUERY_THROW); xCellProps->setPropertyValue("IsTextWrapped", Any(true)); manipulateText(xCellText); manipulateTable(xSpreadsheetCells); // get internal service factory of the document Reference xCalcFactory = Reference(xCalcDocument, UNO_QUERY_THROW); // get Drawpage Reference xDrawPageSupplier(xIndexedSheets->getByIndex(0), UNO_QUERY_THROW); Reference xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_QUERY_THROW); // create and insert RectangleShape and get shape text, then manipulate text Reference xCalcShape( xCalcFactory->createInstance("com.sun.star.drawing.RectangleShape"), UNO_QUERY_THROW); xCalcShape->setSize(Size(10000, 10000)); xCalcShape->setPosition(Point(7000, 3000)); xDrawPage->add(xCalcShape); Reference xShapeProps(xCalcShape, UNO_QUERY_THROW); // wrap text inside shape xShapeProps->setPropertyValue("TextContourFrame", Any(true)); Reference xShapeText = Reference(xCalcShape, UNO_QUERY_THROW); manipulateText(xShapeText); manipulateShape(xCalcShape); } void useDraw() { //create new draw document and insert ractangle shape Reference xDrawComponent = newDocComponent("sdraw"); Reference xDrawPagesSupplier(xDrawComponent, UNO_QUERY_THROW); Reference xIndexedDrawPages(xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW); Reference xDrawPage(xIndexedDrawPages->getByIndex(0), UNO_QUERY_THROW); // get internal service factory of the document Reference xDrawFactory = Reference(xDrawComponent, UNO_QUERY_THROW); Reference xDrawShape( xDrawFactory->createInstance("com.sun.star.drawing.RectangleShape"), UNO_QUERY_THROW); xDrawShape->setSize(Size(10000, 20000)); xDrawShape->setPosition(Point(5000, 5000)); xDrawPage->add(xDrawShape); Reference xShapeProps(xDrawShape, UNO_QUERY_THROW); // wrap text inside shape xShapeProps->setPropertyValue("TextContourFrame", Any(true)); Reference xShapeText = Reference(xDrawShape, UNO_QUERY_THROW); manipulateText(xShapeText); manipulateShape(xDrawShape); } void manipulateText(const Reference& xText) { // simply set whole text as one string xText->setString("He lay flat on the brown, pine-needled floor of the forest, " "his chin on his folded arms, and high overhead the wind blew in the tops " "of the pine trees."); // create text cursor for selecting and formatting Reference xTextCursor = xText->createTextCursor(); Reference xCursorProps(xTextCursor, UNO_QUERY_THROW); // use cursor to select "He lay" and apply bold italic xTextCursor->gotoStart(false); xTextCursor->goRight(6, true); // from CharacterProperties xCursorProps->setPropertyValue("CharPosture", Any(FontSlant_ITALIC)); xCursorProps->setPropertyValue("CharWeight", Any(FontWeight::BOLD)); xTextCursor->gotoEnd(false); xText->insertString( xTextCursor->getStart(), " The mountainside sloped gently where he lay; " "but below it was steep and he could see the dark of the oiled road " "winding through the pass. There was a stream alongside the road " "and far down the pass he saw a mill beside the stream and the falling water " "of the dam, white in the summer sunlight.", false); xText->insertString(xTextCursor->getStart(), "\n \"Is that the mill?\" he asked.", false); } void manipulateTable(const Reference& xCellRange) { OUString backColorPropertyName; Reference xTableProps = NULL; // enter column titles and a cell value Reference xCell = xCellRange->getCellByPosition(0, 0); Reference xCellText = Reference(xCell, UNO_QUERY_THROW); xCellText->setString("Quotation"); xCell = xCellRange->getCellByPosition(1, 0); xCellText = Reference(xCell, UNO_QUERY_THROW); xCellText->setString("Year"); xCell = xCellRange->getCellByPosition(1, 1); xCell->setValue(1940); Reference xSelectedCells = xCellRange->getCellRangeByName("A1:B1"); Reference xCellProps(xSelectedCells, UNO_QUERY_THROW); // format table headers and table borders // we need to distinguish text and sheet tables: // property name for cell colors is different in text and sheet cells // we want to apply TableBorder to whole text table, but only to sheet cells with content Reference xServiceInfo(xCellRange, UNO_QUERY_THROW); if (xServiceInfo->supportsService("com.sun.star.sheet.Spreadsheet")) { backColorPropertyName = "CellBackColor"; xSelectedCells = xCellRange->getCellRangeByName("A1:B2"); xTableProps = Reference(xSelectedCells, UNO_QUERY_THROW); } else if (xServiceInfo->supportsService("com.sun.star.text.TextTable")) { backColorPropertyName = "BackColor"; xTableProps = Reference(xCellRange, UNO_QUERY_THROW); } // set cell background color xCellProps->setPropertyValue(backColorPropertyName, Any(static_cast(0x99CCFF))); // set table borders // create description for blue line, width 10 BorderLine theLine; theLine.Color = 0x000099; theLine.OuterLineWidth = 10; // apply line description to all border lines and make them valid TableBorder bord; bord.VerticalLine = bord.HorizontalLine = bord.LeftLine = bord.RightLine = bord.TopLine = bord.BottomLine = theLine; bord.IsVerticalLineValid = bord.IsHorizontalLineValid = bord.IsLeftLineValid = bord.IsRightLineValid = bord.IsTopLineValid = bord.IsBottomLineValid = true; xTableProps->setPropertyValue("TableBorder", Any(bord)); xTableProps->getPropertyValue("TableBorder") >>= bord; theLine = bord.TopLine; int col = theLine.Color; std::cout << col << "\n"; } void manipulateShape(const Reference& xShape) { Reference xShapeProps = Reference(xShape, UNO_QUERY_THROW); xShapeProps->setPropertyValue("FillColor", Any(static_cast(0x99CCFF))); xShapeProps->setPropertyValue("LineColor", Any(static_cast(0x000099))); xShapeProps->setPropertyValue("RotateAngle", Any(static_cast(3000))); xShapeProps->setPropertyValue("TextLeftDistance", Any(static_cast(0))); xShapeProps->setPropertyValue("TextRightDistance", Any(static_cast(0))); xShapeProps->setPropertyValue("TextUpperDistance", Any(static_cast(0))); xShapeProps->setPropertyValue("TextLowerDistance", Any(static_cast(0))); } Reference newDocComponent(const OUString docType) { OUString loadUrl = "private:factory/" + docType; Reference xRemoteServiceManager = getRemoteServiceManager(); Reference desktop = xRemoteServiceManager->createInstanceWithContext( "com.sun.star.frame.Desktop", xRemoteContext); Reference xComponentLoader = Reference(desktop, UNO_QUERY_THROW); Sequence loadProps(0); Reference xDocument = xComponentLoader->loadComponentFromURL(loadUrl, "_blank", 0, loadProps); return xDocument; } Reference getRemoteServiceManager() { if (xRemoteContext == NULL && xRemoteServiceManager == NULL) { try { // First step: get the remote office component context xRemoteContext = bootstrap(); std::cout << "Connected to a running office ...\n"; xRemoteServiceManager = xRemoteContext->getServiceManager(); } catch (Exception e) { std::cerr << e.Message << "\n"; exit(1); } } return xRemoteServiceManager; } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */