diff options
author | Hossein <hossein@libreoffice.org> | 2021-06-29 03:14:34 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2021-07-13 08:45:50 +0200 |
commit | 02a56168ae20b575eae532d81612eeff558a7fe1 (patch) | |
tree | 7beb879b82b1097d1bff5a9cb06d70ae32cd53b3 /odk | |
parent | d766373f3b17d2749176f77958df793cd07ceced (diff) |
Ported Draw example from Java to C++ and done some tweaks
* Created Draw.cxx
* Used Makefile from DocumentLoader with some changes
* Updated examples.html to reflect the addition
* Updated odk make files
Change-Id: I37426d63854222ef8bedb5f61edb420baec4d28a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118034
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'odk')
-rw-r--r-- | odk/CustomTarget_build-examples.mk | 1 | ||||
-rw-r--r-- | odk/Package_examples.mk | 2 | ||||
-rw-r--r-- | odk/examples/cpp/Draw/Draw.cxx | 235 | ||||
-rw-r--r-- | odk/examples/cpp/Draw/Makefile | 84 | ||||
-rw-r--r-- | odk/examples/examples.html | 7 |
5 files changed, 328 insertions, 1 deletions
diff --git a/odk/CustomTarget_build-examples.mk b/odk/CustomTarget_build-examples.mk index de3e9653f336..7861517b76b3 100644 --- a/odk/CustomTarget_build-examples.mk +++ b/odk/CustomTarget_build-examples.mk @@ -19,6 +19,7 @@ my_example_dirs = \ DevelopersGuide/ProfUNO/CppBinding \ DevelopersGuide/ProfUNO/SimpleBootstrap_cpp \ OLE/activex \ + cpp/Draw \ cpp/DocumentLoader \ cpp/complextoolbarcontrols \ cpp/counter \ diff --git a/odk/Package_examples.mk b/odk/Package_examples.mk index 80577e955794..e2cb4bd4c7a1 100644 --- a/odk/Package_examples.mk +++ b/odk/Package_examples.mk @@ -392,6 +392,8 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples, basic/text/modifying_text_automatically/inserting_bookmarks.odt \ basic/text/modifying_text_automatically/replacing_text.odt \ basic/text/modifying_text_automatically/using_regular_expressions.odt \ + cpp/Draw/Draw.cxx \ + cpp/Draw/Makefile \ cpp/DocumentLoader/DocumentLoader.cxx \ cpp/DocumentLoader/Makefile \ cpp/DocumentLoader/test.odt \ diff --git a/odk/examples/cpp/Draw/Draw.cxx b/odk/examples/cpp/Draw/Draw.cxx new file mode 100644 index 000000000000..d7924e53c539 --- /dev/null +++ b/odk/examples/cpp/Draw/Draw.cxx @@ -0,0 +1,235 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <iostream> + +#include <cppuhelper/bootstrap.hxx> +#include <rtl/bootstrap.hxx> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/bridge/XUnoUrlResolver.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/XComponentLoader.hpp> +#include <com/sun/star/frame/XStorable.hpp> +#include <com/sun/star/lang/XMultiComponentFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/text/XTextDocument.hpp> +#include <com/sun/star/drawing/XShape.hpp> +#include <com/sun/star/drawing/XDrawPage.hpp> +#include <com/sun/star/drawing/XShapeGrouper.hpp> +#include <com/sun/star/drawing/XDrawPagesSupplier.hpp> +#include <com/sun/star/drawing/XDrawPages.hpp> +#include <com/sun/star/container/XIndexAccess.hpp> + +using namespace std; +using namespace cppu; +using namespace rtl; +using namespace css::uno; +using namespace css::beans; +using namespace css::bridge; +using namespace css::frame; +using namespace css::lang; +using namespace css::text; +using namespace css::drawing; +using namespace css::awt; +using namespace css::container; + +int getCol(int r, int g, int b); +Reference<XComponent> openDraw(Reference<XComponentContext> xComponentContext); +Reference<XShape> createShape(Reference<XComponent> xDocComp, int height, int width, int x, int y, + OUString kind, int col); +Reference<XShapeGroup> createSequence(Reference<XComponent> xDocComp, Reference<XDrawPage> xDP); + +int main() +{ + Reference<XComponentContext> xContext = NULL; + + try + { + // get the remote office component context + xContext = bootstrap(); + } + catch (Exception& e) + { + cout << "Error: cannot do bootstraping." << endl << e.Message << endl; + exit(1); + } + + Reference<XComponent> xDrawDoc = NULL; + Reference<XDrawPage> xDrawPage = NULL; + + xDrawDoc = openDraw(xContext); + + try + { + // getting the draw page + Reference<XDrawPagesSupplier> xDPS(xDrawDoc, UNO_QUERY); + Reference<XDrawPages> xDPn = xDPS->getDrawPages(); + Reference<XIndexAccess> xDPi(xDPn, UNO_QUERY); + xDrawPage = Reference<XDrawPage>(xDPi->getByIndex(0), UNO_QUERY); + } + catch (Exception& e) + { + cout << "Error: Document creation was not possible" << endl; + exit(1); + } + + createSequence(xDrawDoc, xDrawPage); + + // Drawing some shapes + Reference<XShapes> xShapes(xDrawPage, UNO_QUERY); + xShapes->add(createShape(xDrawDoc, 1000, 1300, 2000, 2000, "Line", getCol(0, 0, 0))); + xShapes->add(createShape(xDrawDoc, 2000, 4000, 15000, 2000, "Ellipse", getCol(0, 200, 200))); + xShapes->add(createShape(xDrawDoc, 3000, 2500, 5500, 4000, "Rectangle", getCol(100, 100, 200))); + + exit(0); +} + +Reference<XComponent> openDraw(Reference<XComponentContext> xContext) +{ + Reference<XComponent> xComp; + + try + { + // getting the remote LibreOffice service manager + Reference<XMultiComponentFactory> xMCF = xContext->getServiceManager(); + + Reference<XInterface> oDesktop + = xMCF->createInstanceWithContext("com.sun.star.frame.Desktop", xContext); + Reference<XComponentLoader> xCLoader(oDesktop, UNO_QUERY); + Sequence<PropertyValue> szEmptyArgs(0); + OUString strDoc("private:factory/sdraw"); + xComp = xCLoader->loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs); + } + catch (Exception e) + { + cout << "Error opening draw." << endl << e.Message << endl; + exit(1); + } + + return xComp; +} + +Reference<XShape> createShape(Reference<XComponent> xDocComp, int height, int width, int x, int y, + OUString kind, int col) +{ + // kind can be either 'Ellipse', 'Line' or 'Rectangle' + Size size; + Point position; + Reference<XShape> xShape; + + // get the mutliservice factory + Reference<XMultiServiceFactory> xDocMSF(xDocComp, UNO_QUERY); + + try + { + Reference<XInterface> oInt + = xDocMSF->createInstance("com.sun.star.drawing." + kind + "Shape"); + xShape = Reference<XShape>(oInt, UNO_QUERY); + + size.Height = height; + size.Width = width; + position.X = x; + position.Y = y; + xShape->setSize(size); + xShape->setPosition(position); + } + catch (Exception e) + { + cout << "Could not create instance." << endl << e.Message << endl; + exit(1); + } + + Reference<XPropertySet> xSPS(xShape, UNO_QUERY); + + try + { + xSPS->setPropertyValue("FillColor", Any(col)); + } + catch (Exception e) + { + cout << "Can not change the shape colors." << endl << e.Message << endl; + exit(1); + } + + return xShape; +} + +Reference<XShapeGroup> createSequence(Reference<XComponent> xDocComp, Reference<XDrawPage> xDP) +{ + Size size; + Point position; + Reference<XShape> xShape; + Reference<XShapes> xShapes(xDP, UNO_QUERY); + int height = 2000; + int width = 2500; + int x = 1800; + int y = 22000; + Reference<XInterface> oInt; + int r = 30; + int g = 0; + int b = 70; + + // getting the multiservice factory + Reference<XMultiServiceFactory> xDocMSF(xDocComp, UNO_QUERY); + + for (int i = 0; i < 380; i = i + 30) + { + try + { + oInt = xDocMSF->createInstance("com.sun.star.drawing.EllipseShape"); + xShape = Reference<XShape>(oInt, UNO_QUERY); + size.Height = height; + size.Width = width; + position.X = x + (i * 40); + position.Y = y + (i * 40) % 4000; + xShape->setSize(size); + xShape->setPosition(position); + } + catch (Exception e) + { + // Some exception occurs.FAILED + cout << "Could not get Shape." << endl << e.Message << endl; + exit(1); + } + + b = b + 8; + + Reference<XPropertySet> xSPS(xShape, UNO_QUERY); + + try + { + xSPS->setPropertyValue("FillColor", Any(getCol(r, g, b))); + xSPS->setPropertyValue("Shadow", Any(true)); + } + catch (Exception e) + { + cout << "Can not change shape colors." << endl << e.Message << endl; + exit(1); + } + xShapes->add(xShape); + } + + Reference<XShapeGrouper> xSGrouper(xDP, UNO_QUERY); + + return xSGrouper->group(xShapes); +} + +int getCol(int r, int g, int b) { return r * 65536 + g * 256 + b; } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/cpp/Draw/Makefile b/odk/examples/cpp/Draw/Makefile new file mode 100644 index 000000000000..f3a3ca5ea80d --- /dev/null +++ b/odk/examples/cpp/Draw/Makefile @@ -0,0 +1,84 @@ +# +# 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/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +# Builds the C++ Draw example of the SDK. + +PRJ=../../.. +SETTINGS=$(PRJ)/settings + +include $(SETTINGS)/settings.mk +include $(SETTINGS)/std.mk + +# Define non-platform/compiler specific settings +COMPONENT_NAME=Draw + +OUT_COMP_INC = $(OUT_INC)/$(COMPONENT_NAME) +OUT_COMP_GEN = $(OUT_MISC)/$(COMPONENT_NAME) +OUT_COMP_OBJ=$(OUT_OBJ)/$(COMPONENT_NAME) + +CXXFILES = Draw.cxx + +OBJFILES = $(patsubst %.cxx,$(OUT_SLO_COMP)/%.$(OBJ_EXT),$(CXXFILES)) + +ENV_OFFICE_TYPES=-env:URE_MORE_TYPES=$(URLPREFIX)$(OFFICE_TYPES) + +# Targets +.PHONY: ALL +ALL : \ + CppDrawExample + +include $(SETTINGS)/stdtarget.mk + +$(OUT_COMP_OBJ)/%.$(OBJ_EXT) : %.cxx $(SDKTYPEFLAG) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(CC) $(CC_FLAGS) $(CC_INCLUDES) -I$(OUT_COMP_INC) $(CC_DEFINES) $(CC_OUTPUT_SWITCH)$(subst /,$(PS),$@) $< + +$(OUT_BIN)/Draw$(EXE_EXT) : $(OUT_COMP_OBJ)/Draw.$(OBJ_EXT) + -$(MKDIR) $(subst /,$(PS),$(@D)) + -$(MKDIR) $(subst /,$(PS),$(OUT_COMP_GEN)) +ifeq "$(OS)" "WIN" + $(LINK) $(EXE_LINK_FLAGS) /OUT:$@ /MAP:$(OUT_COMP_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 + +CppDrawExample : $(OUT_BIN)/Draw$(EXE_EXT) + @echo -------------------------------------------------------------------------------- + @echo The example connects to the office server, create an empy documents and puts some + @echo shapes on it. + @echo - + @echo Use the following command to execute the example! + @echo - + @echo $(MAKE) Draw.run + @echo - + @echo -------------------------------------------------------------------------------- + +%.run: $(OUT_BIN)/Draw$(EXE_EXT) + cd $(subst /,$(PS),$(OUT_BIN)) && $(basename $@) $(ENV_OFFICE_TYPES) + +.PHONY: clean +clean : + -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_COMP_INC)) + -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_COMP_GEN)) + -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_COMP_OBJ)) + -$(DEL) $(subst \\,\,$(subst /,$(PS),$(OUT_BIN)/Draw*)) diff --git a/odk/examples/examples.html b/odk/examples/examples.html index 9f64489198a4..a9ccb3a824f6 100644 --- a/odk/examples/examples.html +++ b/odk/examples/examples.html @@ -478,9 +478,14 @@ <td class="cell80">Description</td> </tr> <tr> + <td class="cell20"><a href="cpp/Draw/" title="link to the source directory of the C++ Draw example">Draw</a></td> + <td class="cell80">This is essentially the same as the appropriate Java + example, but is implemented in C++. It does not need a running office instance.</td> + </tr> + <tr> <td class="cell20"><a href="cpp/DocumentLoader/" title="link to the source directory of the C++ DocumentLoader example">DocumentLoader</a></td> <td class="cell80">This component works the same as the appropriate Java - example, but is implemented in C++.</td> + example, but is implemented in C++. It needs a running office instance.</td> </tr> <tr> <td class="cell20"><a href="cpp/counter/" title="link to the source directory of the C++ counter example">Counter</a></td> |