Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / dbpilots / optiongrouplayouter.cxx
blob9b72a92f69386dc9f042d73dd1b4a3a9115864c4
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 "optiongrouplayouter.hxx"
21 #include <com/sun/star/awt/Size.hpp>
22 #include <com/sun/star/awt/Point.hpp>
23 #include <com/sun/star/container/XIndexAccess.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/drawing/ShapeCollection.hpp>
26 #include <com/sun/star/drawing/XShapes.hpp>
27 #include <com/sun/star/drawing/XShapeGrouper.hpp>
28 #include <com/sun/star/text/TextContentAnchorType.hpp>
29 #include <com/sun/star/view/XSelectionSupplier.hpp>
30 #include "controlwizard.hxx"
31 #include "groupboxwiz.hxx"
32 #include "dbptools.hxx"
33 #include <osl/diagnose.h>
36 namespace dbp
40 #define BUTTON_HEIGHT 300
41 #define HEIGHT 450
42 #define OFFSET 300
43 #define MIN_WIDTH 600
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::drawing;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::awt;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::text;
52 using namespace ::com::sun::star::view;
54 OOptionGroupLayouter::OOptionGroupLayouter(const Reference< XComponentContext >& _rxContext)
55 :mxContext(_rxContext)
60 void OOptionGroupLayouter::doLayout(const OControlWizardContext& _rContext, const OOptionGroupSettings& _rSettings)
62 Reference< XShapes > xPageShapes = _rContext.xDrawPage;
63 if (!xPageShapes.is())
65 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
66 return;
69 Reference< XMultiServiceFactory > xDocFactory(_rContext.xDocumentModel, UNO_QUERY);
70 if (!xDocFactory.is())
72 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
73 return;
76 // no. of buttons to create
77 sal_Int32 nRadioButtons = _rSettings.aLabels.size();
79 // the shape of the groupbox
80 css::awt::Size aControlShapeSize = _rContext.xObjectShape->getSize();
81 // maybe need to adjust the size if the control shapes
82 sal_Int32 nMinShapeHeight = BUTTON_HEIGHT*(nRadioButtons+1) + BUTTON_HEIGHT + BUTTON_HEIGHT/4;
83 if (aControlShapeSize.Height < nMinShapeHeight)
84 aControlShapeSize.Height = nMinShapeHeight;
85 if (aControlShapeSize.Width < MIN_WIDTH)
86 aControlShapeSize.Width = MIN_WIDTH;
87 _rContext.xObjectShape->setSize(aControlShapeSize);
89 // if we're working on a writer document, we need to anchor the shape
90 implAnchorShape(Reference< XPropertySet >(_rContext.xObjectShape, UNO_QUERY));
92 // shape collection (for grouping the shapes)
93 Reference< XShapes > xButtonCollection( ShapeCollection::create(mxContext) );
94 // first member : the shape of the control
95 xButtonCollection->add(_rContext.xObjectShape.get());
97 sal_Int32 nTempHeight = (aControlShapeSize.Height - BUTTON_HEIGHT/4) / (nRadioButtons + 1);
99 css::awt::Point aShapePosition = _rContext.xObjectShape->getPosition();
101 css::awt::Size aButtonSize(aControlShapeSize);
102 aButtonSize.Width = aControlShapeSize.Width - OFFSET;
103 aButtonSize.Height = HEIGHT;
104 css::awt::Point aButtonPosition;
105 aButtonPosition.X = aShapePosition.X + OFFSET;
107 OUString sElementsName("RadioGroup");
108 disambiguateName(Reference< XNameAccess >(_rContext.xForm, UNO_QUERY), sElementsName);
110 auto aLabelIter = _rSettings.aLabels.cbegin();
111 auto aValueIter = _rSettings.aValues.cbegin();
112 for (sal_Int32 i=0; i<nRadioButtons; ++i, ++aLabelIter, ++aValueIter)
114 aButtonPosition.Y = aShapePosition.Y + (i+1) * nTempHeight;
116 Reference< XPropertySet > xRadioModel(
117 xDocFactory->createInstance("com.sun.star.form.component.RadioButton"),
118 UNO_QUERY);
120 // the label
121 xRadioModel->setPropertyValue("Label", makeAny(*aLabelIter));
122 // the value
123 xRadioModel->setPropertyValue("RefValue", makeAny(*aValueIter));
125 // default selection
126 if (_rSettings.sDefaultField == *aLabelIter)
127 xRadioModel->setPropertyValue("DefaultState", makeAny(sal_Int16(1)));
129 // the connection to the database field
130 if (!_rSettings.sDBField.isEmpty())
131 xRadioModel->setPropertyValue("DataField", makeAny(_rSettings.sDBField));
133 // the name for the model
134 xRadioModel->setPropertyValue("Name", makeAny(sElementsName));
136 // create a shape for the radio button
137 Reference< XControlShape > xRadioShape(
138 xDocFactory->createInstance("com.sun.star.drawing.ControlShape"),
139 UNO_QUERY);
140 Reference< XPropertySet > xShapeProperties(xRadioShape, UNO_QUERY);
142 // if we're working on a writer document, we need to anchor the shape
143 implAnchorShape(xShapeProperties);
145 // position it
146 xRadioShape->setSize(aButtonSize);
147 xRadioShape->setPosition(aButtonPosition);
148 // knitting with the model
149 xRadioShape->setControl(Reference< XControlModel >(xRadioModel, UNO_QUERY));
151 // the name of the shape
152 // tdf#117282 com.sun.star.drawing.ControlShape *has* no property
153 // of type 'Name'. In older versions it was an error that this did
154 // not throw an UnknownPropertyException. Still, it was never set
155 // at the Shape/SdrObject and was lost.
156 // Thus - just do no tset it. It is/stays part of the FormControl
157 // data, so it will be shown in the FormControl dialogs. It is not
158 // shown/used in SdrObject::Name dialog (e.g. context menu/Name...)
159 // if (xShapeProperties.is())
160 // xShapeProperties->setPropertyValue("Name", makeAny(sElementsName));
162 // add to the page
163 xPageShapes->add(xRadioShape.get());
164 // add to the collection (for the later grouping)
165 xButtonCollection->add(xRadioShape.get());
167 // set the GroupBox as "LabelControl" for the RadioButton
168 // (_after_ having inserted the model into the page!)
169 xRadioModel->setPropertyValue("LabelControl", makeAny(_rContext.xObjectModel));
172 // group the shapes
175 Reference< XShapeGrouper > xGrouper(_rContext.xDrawPage, UNO_QUERY);
176 if (xGrouper.is())
178 Reference< XShapeGroup > xGroupedOptions = xGrouper->group(xButtonCollection);
179 Reference< XSelectionSupplier > xSelector(_rContext.xDocumentModel->getCurrentController(), UNO_QUERY);
180 if (xSelector.is())
181 xSelector->select(makeAny(xGroupedOptions));
184 catch(Exception&)
186 OSL_FAIL("OOptionGroupLayouter::doLayout: caught an exception while grouping the shapes!");
191 void OOptionGroupLayouter::implAnchorShape(const Reference< XPropertySet >& _rxShapeProps)
193 static const char s_sAnchorPropertyName[] = "AnchorType";
194 Reference< XPropertySetInfo > xPropertyInfo;
195 if (_rxShapeProps.is())
196 xPropertyInfo = _rxShapeProps->getPropertySetInfo();
197 if (xPropertyInfo.is() && xPropertyInfo->hasPropertyByName(s_sAnchorPropertyName))
198 _rxShapeProps->setPropertyValue(s_sAnchorPropertyName, makeAny(TextContentAnchorType_AT_PAGE));
202 } // namespace dbp
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */