tdf#143122 Port HelloTextTableShape example to C++

Port DevelopersGuide/FirstSteps/HelloTextTableShape Java SDK example to
C++ and add a GNU Makefile for the build.

Change-Id: I8798406c24ec6c566fb6494f7202f6a2a984a25c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164635
Tested-by: Jenkins
Reviewed-by: Hossein <hossein@libreoffice.org>
This commit is contained in:
Hossein
2024-03-10 20:58:37 +01:00
parent 93c5a2513c
commit d30891d6ad
4 changed files with 463 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ my_example_dirs = \
CLI/VB.NET/WriterDemo \
DevelopersGuide/FirstSteps/FirstUnoContact/cxx \
DevelopersGuide/FirstSteps/FirstLoadComponent/cxx \
DevelopersGuide/FirstSteps/HelloTextTableShape/cxx \
DevelopersGuide/BasicAndDialogs/ToolkitControls \
DevelopersGuide/Components/Addons/ProtocolHandlerAddon_cpp \
DevelopersGuide/Components/CppComponent \

View File

@@ -189,6 +189,8 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples,
DevelopersGuide/FirstSteps/FirstLoadComponent/basic/FirstLoadComponent.bas \
DevelopersGuide/FirstSteps/FirstLoadComponent/python/FirstLoadComponent.py \
DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py \
DevelopersGuide/FirstSteps/HelloTextTableShape/cxx/HelloTextTableShape.cxx \
DevelopersGuide/FirstSteps/HelloTextTableShape/cxx/Makefile \
DevelopersGuide/FirstSteps/HelloTextTableShape/java/HelloTextTableShape.java \
DevelopersGuide/FirstSteps/HelloTextTableShape/java/Makefile \
DevelopersGuide/FirstSteps/HelloTextTableShape/basic/HelloTextTableShape.bas \

View File

@@ -0,0 +1,383 @@
/* -*- 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 <iostream>
#include <sal/main.h>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XTextCursor.hpp>
#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/table/XCellRange.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/table/TableBorder.hpp>
#include <com/sun/star/table/BorderLine.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/awt/Size.hpp>
#include <com/sun/star/awt/Point.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/drawing/XDrawPage.hpp>
#include <com/sun/star/awt/FontSlant.hpp>
#include <com/sun/star/awt/FontWeight.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/text/XBookmarksSupplier.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
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>& xText);
void manipulateTable(const Reference<XCellRange>& xCellRange);
void manipulateShape(const Reference<XShape>& xShape);
void useDocuments();
void useWriter();
void useCalc();
void useDraw();
Reference<XComponentContext> getComponentContext();
Reference<XMultiComponentFactory> getRemoteServiceManager();
Reference<XComponent> newDocComponent(const OUString docType);
Reference<XComponentContext> xRemoteContext = NULL;
Reference<XMultiComponentFactory> 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<XComponent> xWriterComponent = newDocComponent("swriter");
Reference<XTextDocument> xTextDocument(xWriterComponent, UNO_QUERY_THROW);
Reference<XText> xText = xTextDocument->getText();
manipulateText(xText);
// get internal service factory of the document
Reference<lang::XMultiServiceFactory> xWriterFactory
= Reference<XMultiServiceFactory>(xTextDocument, UNO_QUERY_THROW);
// insert TextTable and get cell text, then manipulate text in cell
Reference<XTextContent> xTextContentTable = Reference<XTextContent>(
xWriterFactory->createInstance("com.sun.star.text.TextTable"), UNO_QUERY_THROW);
xText->insertTextContent(xText->getEnd(), xTextContentTable, false);
Reference<XCellRange> xCellRange = Reference<XCellRange>(xTextContentTable, UNO_QUERY_THROW);
Reference<XCell> xCell = xCellRange->getCellByPosition(0, 1);
Reference<XText> xCellText = Reference<XText>(xCell, UNO_QUERY_THROW);
manipulateText(xCellText);
manipulateTable(xCellRange);
Reference<XShape> xWriterShape = Reference<XShape>(
xWriterFactory->createInstance("com.sun.star.drawing.RectangleShape"), UNO_QUERY_THROW);
xWriterShape->setSize(Size(10000, 10000));
Reference<XTextContent> xTextContentShape(xWriterShape, UNO_QUERY_THROW);
xText->insertTextContent(xText->getEnd(), xTextContentShape, false);
Reference<XPropertySet> xShapeProps(xWriterShape, UNO_QUERY_THROW);
// wrap text inside shape
xShapeProps->setPropertyValue("TextContourFrame", Any(true));
Reference<XText> xShapeText = Reference<XText>(xWriterShape, UNO_QUERY_THROW);
manipulateText(xShapeText);
manipulateShape(xWriterShape);
// more code snippets used in the manual:
Reference<XInterface> bookmark = xWriterFactory->createInstance("com.sun.star.text.Bookmark");
// name the bookmark
Reference<XNamed> xNamed(bookmark, UNO_QUERY_THROW);
xNamed->setName("MyUniqueBookmarkName");
// get XTextContent interface and insert it at the end of the document
Reference<XTextContent> xTextContent(bookmark, UNO_QUERY_THROW);
xText->insertTextContent(xText->getEnd(), xTextContent, false);
//query BookmarksSupplier
Reference<XBookmarksSupplier> xBookmarksSupplier(xWriterComponent, UNO_QUERY_THROW);
Reference<XNameAccess> xNamedBookmarks = xBookmarksSupplier->getBookmarks();
Any foundBookmark = xNamedBookmarks->getByName("MyUniqueBookmarkName");
Reference<XTextContent> xFoundBookmark(foundBookmark, UNO_QUERY_THROW);
Reference<XTextRange> 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<XTextTablesSupplier> xTablesSupplier(xWriterComponent, UNO_QUERY_THROW);
// get the tables collection
Reference<XNameAccess> xNamedTables = xTablesSupplier->getTextTables();
// now query the XIndexAccess from the tables collection
Reference<XIndexAccess> xIndexedTables(xNamedTables, UNO_QUERY_THROW);
// we need properties
Reference<XPropertySet> 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<XPropertySet>(table, UNO_QUERY_THROW);
xTableProps->setPropertyValue("BackColor", Any(0xC8FFB9));
}
}
void useCalc()
{
// create new calc document and manipulate cell text
Reference<XComponent> xCalcComponent = newDocComponent("scalc");
Reference<XSpreadsheetDocument> xCalcDocument(xCalcComponent, UNO_QUERY_THROW);
Reference<XSpreadsheetDocument> xSpreadsheetDocument
= Reference<XSpreadsheetDocument>(xCalcDocument, UNO_QUERY);
Reference<XIndexAccess> xIndexedSheets(xSpreadsheetDocument->getSheets(), UNO_QUERY);
Reference<XCellRange> xSpreadsheetCells(xIndexedSheets->getByIndex(0), UNO_QUERY_THROW);
//get cell A2 in first sheet
Reference<XCell> xCell = xSpreadsheetCells->getCellByPosition(0, 1);
Reference<XText> xCellText = Reference<XText>(xCell, UNO_QUERY_THROW);
Reference<XPropertySet> xCellProps(xCell, UNO_QUERY_THROW);
xCellProps->setPropertyValue("IsTextWrapped", Any(true));
manipulateText(xCellText);
manipulateTable(xSpreadsheetCells);
// get internal service factory of the document
Reference<XMultiServiceFactory> xCalcFactory
= Reference<XMultiServiceFactory>(xCalcDocument, UNO_QUERY_THROW);
// get Drawpage
Reference<XDrawPageSupplier> xDrawPageSupplier(xIndexedSheets->getByIndex(0), UNO_QUERY_THROW);
Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_QUERY_THROW);
// create and insert RectangleShape and get shape text, then manipulate text
Reference<XShape> 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<XPropertySet> xShapeProps(xCalcShape, UNO_QUERY_THROW);
// wrap text inside shape
xShapeProps->setPropertyValue("TextContourFrame", Any(true));
Reference<XText> xShapeText = Reference<XText>(xCalcShape, UNO_QUERY_THROW);
manipulateText(xShapeText);
manipulateShape(xCalcShape);
}
void useDraw()
{
//create new draw document and insert ractangle shape
Reference<XComponent> xDrawComponent = newDocComponent("sdraw");
Reference<XDrawPagesSupplier> xDrawPagesSupplier(xDrawComponent, UNO_QUERY_THROW);
Reference<XIndexAccess> xIndexedDrawPages(xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW);
Reference<XDrawPage> xDrawPage(xIndexedDrawPages->getByIndex(0), UNO_QUERY_THROW);
// get internal service factory of the document
Reference<lang::XMultiServiceFactory> xDrawFactory
= Reference<lang::XMultiServiceFactory>(xDrawComponent, UNO_QUERY_THROW);
Reference<XShape> 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<XPropertySet> xShapeProps(xDrawShape, UNO_QUERY_THROW);
// wrap text inside shape
xShapeProps->setPropertyValue("TextContourFrame", Any(true));
Reference<XText> xShapeText = Reference<XText>(xDrawShape, UNO_QUERY_THROW);
manipulateText(xShapeText);
manipulateShape(xDrawShape);
}
void manipulateText(const Reference<XText>& 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> xTextCursor = xText->createTextCursor();
Reference<XPropertySet> 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>& xCellRange)
{
OUString backColorPropertyName;
Reference<XPropertySet> xTableProps = NULL;
// enter column titles and a cell value
Reference<XCell> xCell = xCellRange->getCellByPosition(0, 0);
Reference<XText> xCellText = Reference<XText>(xCell, UNO_QUERY_THROW);
xCellText->setString("Quotation");
xCell = xCellRange->getCellByPosition(1, 0);
xCellText = Reference<XText>(xCell, UNO_QUERY_THROW);
xCellText->setString("Year");
xCell = xCellRange->getCellByPosition(1, 1);
xCell->setValue(1940);
Reference<XCellRange> xSelectedCells = xCellRange->getCellRangeByName("A1:B1");
Reference<XPropertySet> 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> xServiceInfo(xCellRange, UNO_QUERY_THROW);
if (xServiceInfo->supportsService("com.sun.star.sheet.Spreadsheet"))
{
backColorPropertyName = "CellBackColor";
xSelectedCells = xCellRange->getCellRangeByName("A1:B2");
xTableProps = Reference<XPropertySet>(xSelectedCells, UNO_QUERY_THROW);
}
else if (xServiceInfo->supportsService("com.sun.star.text.TextTable"))
{
backColorPropertyName = "BackColor";
xTableProps = Reference<XPropertySet>(xCellRange, UNO_QUERY_THROW);
}
// set cell background color
xCellProps->setPropertyValue(backColorPropertyName, Any(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>& xShape)
{
Reference<XPropertySet> xShapeProps = Reference<XPropertySet>(xShape, UNO_QUERY_THROW);
xShapeProps->setPropertyValue("FillColor", Any(0x99CCFF));
xShapeProps->setPropertyValue("LineColor", Any(0x000099));
xShapeProps->setPropertyValue("RotateAngle", Any(3000));
xShapeProps->setPropertyValue("TextLeftDistance", Any(0));
xShapeProps->setPropertyValue("TextRightDistance", Any(0));
xShapeProps->setPropertyValue("TextUpperDistance", Any(0));
xShapeProps->setPropertyValue("TextLowerDistance", Any(0));
}
Reference<XComponent> newDocComponent(const OUString docType)
{
OUString loadUrl = "private:factory/" + docType;
Reference<XMultiComponentFactory> xRemoteServiceManager = getRemoteServiceManager();
Reference<XInterface> desktop = xRemoteServiceManager->createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);
Reference<XComponentLoader> xComponentLoader
= Reference<XComponentLoader>(desktop, UNO_QUERY_THROW);
Sequence<PropertyValue> loadProps(0);
Reference<XComponent> xDocument
= xComponentLoader->loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
return xDocument;
}
Reference<XMultiComponentFactory> 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: */

View File

@@ -0,0 +1,77 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; 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/.
#
# Builds the HelloTextTableShape example of the SDK.
PRJ=../../../../..
SETTINGS=$(PRJ)/settings
include $(SETTINGS)/settings.mk
include $(SETTINGS)/std.mk
# Define non-platform/compiler specific settings
APP_NAME=HelloTextTableShape
OUT_APP_INC = $(OUT_INC)/$(APP_NAME)
OUT_APP_GEN = $(OUT_MISC)/$(APP_NAME)
OUT_APP_OBJ=$(OUT_OBJ)/$(APP_NAME)
CXXFILES = HelloTextTableShape.cxx
OBJFILES = $(patsubst %.cxx,$(OUT_SLO_COMP)/%.$(OBJ_EXT),$(CXXFILES))
# Targets
.PHONY: ALL
ALL : \
HelloTextTableShape
include $(SETTINGS)/stdtarget.mk
$(OUT_APP_OBJ)/%.$(OBJ_EXT) : %.cxx $(SDKTYPEFLAG)
-$(MKDIR) $(subst /,$(PS),$(@D))
$(CC) $(CC_FLAGS) $(CC_INCLUDES) -I$(OUT_APP_INC) $(CC_DEFINES) $(CC_OUTPUT_SWITCH)$(subst /,$(PS),$@) $<
$(OUT_BIN)/_$(APP_NAME)$(EXE_EXT) : $(OUT_APP_OBJ)/$(APP_NAME).$(OBJ_EXT)
-$(MKDIR) $(subst /,$(PS),$(@D))
-$(MKDIR) $(subst /,$(PS),$(OUT_APP_GEN))
ifeq "$(OS)" "WIN"
$(LINK) $(EXE_LINK_FLAGS) /OUT:$@ /MAP:$(OUT_APP_GEN)/$(basename $(@F)).map \
$< $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB)
else
$(LINK) $(EXE_LINK_FLAGS) $(LINK_LIBS) -o $@ $< \
$(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB) $(STDC++LIB)
ifeq "$(OS)" "MACOSX"
$(INSTALL_NAME_URELIBS_BIN) $@
endif
endif
$(OUT_BIN)/$(APP_NAME)$(EXE_EXT) : $(OUT_BIN)/_$(APP_NAME)$(EXE_EXT)
-$(MKDIR) $(subst /,$(PS),$(@D))
$(COPY) $(subst /,$(PS),$(BIN_DIR)/unoapploader$(EXE_EXT)) $(subst /,$(PS),$@)
# workaround for touch problem under Windows with full qualified paths
make -t $@
HelloTextTableShape: $(OUT_BIN)/$(APP_NAME)$(EXE_EXT)
@echo --------------------------------------------------------------------------------
@echo Please use the following command to execute the example!
@echo -
@echo $(MAKE) HelloTextTableShap.run
@echo --------------------------------------------------------------------------------
%.run: $(OUT_BIN)/HelloTextTableShape$(EXE_EXT)
cd $(subst /,$(PS),$(OUT_BIN)) && $(basename $@)
.PHONY: clean
clean :
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_INC))
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_GEN))
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_OBJ))
-$(DEL) $(subst \\,\,$(subst /,$(PS),$(OUT_BIN)/*HelloTextTableShape*))
# vim: set noet sw=4 ts=4: