Simplify using designated initializers
[LibreOffice.git] / odk / examples / cpp / Draw / Draw.cxx
blobbaed25444155984bb4ff2424516681fe78a02672
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <iostream>
22 #include <cppuhelper/bootstrap.hxx>
23 #include <rtl/bootstrap.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
26 #include <com/sun/star/frame/Desktop.hpp>
27 #include <com/sun/star/frame/XComponentLoader.hpp>
28 #include <com/sun/star/frame/XStorable.hpp>
29 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/text/XTextDocument.hpp>
32 #include <com/sun/star/drawing/XShape.hpp>
33 #include <com/sun/star/drawing/XDrawPage.hpp>
34 #include <com/sun/star/drawing/XShapeGrouper.hpp>
35 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
36 #include <com/sun/star/drawing/XDrawPages.hpp>
37 #include <com/sun/star/container/XIndexAccess.hpp>
38 #include <com/sun/star/util/Color.hpp>
40 using namespace cppu;
41 using namespace rtl;
42 using namespace css::uno;
43 using namespace css::beans;
44 using namespace css::bridge;
45 using namespace css::frame;
46 using namespace css::lang;
47 using namespace css::text;
48 using namespace css::drawing;
49 using namespace css::awt;
50 using namespace css::container;
52 css::util::Color getCol(int r, int g, int b);
53 Reference<XComponent> openDraw(Reference<XComponentContext> xComponentContext);
54 Reference<XShape> createShape(Reference<XComponent> xDocComp, int height, int width, int x, int y,
55 OUString kind, css::util::Color col);
56 Reference<XShapeGroup> createSequence(Reference<XComponent> xDocComp, Reference<XDrawPage> xDP);
58 int main()
60 Reference<XComponentContext> xContext = NULL;
62 try
64 // get the remote office component context
65 xContext = bootstrap();
67 catch (Exception& e)
69 std::cout << "Error: cannot do bootstrapping." << std::endl << e.Message << std::endl;
70 exit(1);
73 Reference<XComponent> xDrawDoc = NULL;
74 Reference<XDrawPage> xDrawPage = NULL;
76 xDrawDoc = openDraw(xContext);
78 try
80 // getting the draw page
81 Reference<XDrawPagesSupplier> xDPS(xDrawDoc, UNO_QUERY);
82 Reference<XDrawPages> xDPn = xDPS->getDrawPages();
83 Reference<XIndexAccess> xDPi(xDPn, UNO_QUERY);
84 xDrawPage = Reference<XDrawPage>(xDPi->getByIndex(0), UNO_QUERY);
86 catch (Exception& e)
88 std::cout << "Error: Document creation was not possible" << std::endl;
89 exit(1);
92 createSequence(xDrawDoc, xDrawPage);
94 // Drawing some shapes
95 Reference<XShapes> xShapes(xDrawPage, UNO_QUERY);
96 xShapes->add(createShape(xDrawDoc, 1000, 1300, 2000, 2000, "Line", getCol(0, 0, 0)));
97 xShapes->add(createShape(xDrawDoc, 2000, 4000, 15000, 2000, "Ellipse", getCol(0, 200, 200)));
98 xShapes->add(createShape(xDrawDoc, 3000, 2500, 5500, 4000, "Rectangle", getCol(100, 100, 200)));
100 exit(0);
103 Reference<XComponent> openDraw(Reference<XComponentContext> xContext)
105 Reference<XComponent> xComp;
109 // getting the remote LibreOffice service manager
110 Reference<XMultiComponentFactory> xMCF = xContext->getServiceManager();
112 Reference<XInterface> oDesktop
113 = xMCF->createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
114 Reference<XComponentLoader> xCLoader(oDesktop, UNO_QUERY);
115 Sequence<PropertyValue> szEmptyArgs(0);
116 OUString strDoc("private:factory/sdraw");
117 xComp = xCLoader->loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
119 catch (Exception e)
121 std::cout << "Error opening draw." << std::endl << e.Message << std::endl;
122 exit(1);
125 return xComp;
128 Reference<XShape> createShape(Reference<XComponent> xDocComp, int height, int width, int x, int y,
129 OUString kind, css::util::Color col)
131 // kind can be either 'Ellipse', 'Line' or 'Rectangle'
132 Size size;
133 Point position;
134 Reference<XShape> xShape;
136 // get the multiservice factory
137 Reference<XMultiServiceFactory> xDocMSF(xDocComp, UNO_QUERY);
141 Reference<XInterface> oInt
142 = xDocMSF->createInstance("com.sun.star.drawing." + kind + "Shape");
143 xShape = Reference<XShape>(oInt, UNO_QUERY);
145 size.Height = height;
146 size.Width = width;
147 position.X = x;
148 position.Y = y;
149 xShape->setSize(size);
150 xShape->setPosition(position);
152 catch (Exception e)
154 std::cout << "Could not create instance." << std::endl << e.Message << std::endl;
155 exit(1);
158 Reference<XPropertySet> xSPS(xShape, UNO_QUERY);
162 xSPS->setPropertyValue("FillColor", Any(col));
164 catch (Exception e)
166 std::cout << "Can not change the shape colors." << std::endl << e.Message << std::endl;
167 exit(1);
170 return xShape;
173 Reference<XShapeGroup> createSequence(Reference<XComponent> xDocComp, Reference<XDrawPage> xDP)
175 Size size;
176 Point position;
177 Reference<XShape> xShape;
178 Reference<XShapes> xShapes(xDP, UNO_QUERY);
179 int height = 2000;
180 int width = 2500;
181 int x = 1800;
182 int y = 22000;
183 Reference<XInterface> oInt;
184 int r = 30;
185 int g = 0;
186 int b = 70;
188 // getting the multiservice factory
189 Reference<XMultiServiceFactory> xDocMSF(xDocComp, UNO_QUERY);
191 for (int i = 0; i < 380; i = i + 30)
195 oInt = xDocMSF->createInstance("com.sun.star.drawing.EllipseShape");
196 xShape = Reference<XShape>(oInt, UNO_QUERY);
197 size.Height = height;
198 size.Width = width;
199 position.X = x + (i * 40);
200 position.Y = y + (i * 40) % 4000;
201 xShape->setSize(size);
202 xShape->setPosition(position);
204 catch (Exception e)
206 // Some exception occurs.FAILED
207 std::cout << "Could not get Shape." << std::endl << e.Message << std::endl;
208 exit(1);
211 b = b + 8;
213 Reference<XPropertySet> xSPS(xShape, UNO_QUERY);
217 xSPS->setPropertyValue("FillColor", Any(getCol(r, g, b)));
218 xSPS->setPropertyValue("Shadow", Any(true));
220 catch (Exception e)
222 std::cout << "Can not change shape colors." << std::endl << e.Message << std::endl;
223 exit(1);
225 xShapes->add(xShape);
228 Reference<XShapeGrouper> xSGrouper(xDP, UNO_QUERY);
230 return xSGrouper->group(xShapes);
233 css::util::Color getCol(int r, int g, int b) { return r * 65536 + g * 256 + b; }
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */