bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / dbpilots / optiongrouplayouter.cxx
blob4ac4b955e0922c2b723c6ab47f03c4b1363e4a36
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"
35 namespace dbp
39 #define BUTTON_HEIGHT 300
40 #define HEIGHT 450
41 #define OFFSET 300
42 #define MIN_WIDTH 600
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::drawing;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::awt;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::text;
51 using namespace ::com::sun::star::view;
53 OOptionGroupLayouter::OOptionGroupLayouter(const Reference< XComponentContext >& _rxContext)
54 :mxContext(_rxContext)
59 void OOptionGroupLayouter::doLayout(const OControlWizardContext& _rContext, const OOptionGroupSettings& _rSettings)
61 Reference< XShapes > xPageShapes(_rContext.xDrawPage, UNO_QUERY);
62 if (!xPageShapes.is())
64 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
65 return;
68 Reference< XMultiServiceFactory > xDocFactory(_rContext.xDocumentModel, UNO_QUERY);
69 if (!xDocFactory.is())
71 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
72 return;
75 // no. of buttons to create
76 sal_Int32 nRadioButtons = _rSettings.aLabels.size();
78 sal_Int32 nTopSpace = 0;
80 // the shape of the groupbox
81 ::com::sun::star::awt::Size aControlShapeSize = _rContext.xObjectShape->getSize();
82 // maybe need to adjust the size if the control shapes
83 sal_Int32 nMinShapeHeight = BUTTON_HEIGHT*(nRadioButtons+1) + BUTTON_HEIGHT + BUTTON_HEIGHT/4;
84 if (aControlShapeSize.Height < nMinShapeHeight)
85 aControlShapeSize.Height = nMinShapeHeight;
86 if (aControlShapeSize.Width < MIN_WIDTH)
87 aControlShapeSize.Width = MIN_WIDTH;
88 _rContext.xObjectShape->setSize(aControlShapeSize);
90 // if we're working on a writer document, we need to anchor the shape
91 implAnchorShape(Reference< XPropertySet >(_rContext.xObjectShape, UNO_QUERY));
93 // shape collection (for grouping the shapes)
94 Reference< XShapes > xButtonCollection( ShapeCollection::create(mxContext) );
95 // first member : the shape of the control
96 xButtonCollection->add(_rContext.xObjectShape.get());
98 sal_Int32 nTempHeight = (aControlShapeSize.Height - BUTTON_HEIGHT/4) / (nRadioButtons + 1);
100 ::com::sun::star::awt::Point aShapePosition = _rContext.xObjectShape->getPosition();
102 ::com::sun::star::awt::Size aButtonSize(aControlShapeSize);
103 aButtonSize.Width = aControlShapeSize.Width - OFFSET;
104 aButtonSize.Height = HEIGHT;
105 ::com::sun::star::awt::Point aButtonPosition;
106 aButtonPosition.X = aShapePosition.X + OFFSET;
108 OUString sElementsName("RadioGroup");
109 disambiguateName(Reference< XNameAccess >(_rContext.xForm, UNO_QUERY), sElementsName);
111 StringArray::const_iterator aLabelIter = _rSettings.aLabels.begin();
112 StringArray::const_iterator aValueIter = _rSettings.aValues.begin();
113 for (sal_Int32 i=0; i<nRadioButtons; ++i, ++aLabelIter, ++aValueIter)
115 aButtonPosition.Y = aShapePosition.Y + (i+1) * nTempHeight + nTopSpace;
117 Reference< XPropertySet > xRadioModel(
118 xDocFactory->createInstance("com.sun.star.form.component.RadioButton"),
119 UNO_QUERY);
121 // the label
122 xRadioModel->setPropertyValue("Label", makeAny(OUString(*aLabelIter)));
123 // the value
124 xRadioModel->setPropertyValue("RefValue", makeAny(OUString(*aValueIter)));
126 // default selection
127 if (_rSettings.sDefaultField == *aLabelIter)
128 xRadioModel->setPropertyValue("DefaultState", makeAny(sal_Int16(1)));
130 // the connection to the database field
131 if (!_rSettings.sDBField.isEmpty())
132 xRadioModel->setPropertyValue("DataField", makeAny(OUString(_rSettings.sDBField)));
134 // the name for the model
135 xRadioModel->setPropertyValue("Name", makeAny(sElementsName));
137 // create a shape for the radio button
138 Reference< XControlShape > xRadioShape(
139 xDocFactory->createInstance("com.sun.star.drawing.ControlShape"),
140 UNO_QUERY);
141 Reference< XPropertySet > xShapeProperties(xRadioShape, UNO_QUERY);
143 // if we're working on a writer document, we need to anchor the shape
144 implAnchorShape(xShapeProperties);
146 // position it
147 xRadioShape->setSize(aButtonSize);
148 xRadioShape->setPosition(aButtonPosition);
149 // knitting with the model
150 xRadioShape->setControl(Reference< XControlModel >(xRadioModel, UNO_QUERY));
152 // the name of the shape
153 if (xShapeProperties.is())
154 xShapeProperties->setPropertyValue("Name", makeAny(sElementsName));
156 // add to the page
157 xPageShapes->add(xRadioShape.get());
158 // add to the collection (for the later grouping)
159 xButtonCollection->add(xRadioShape.get());
161 // set the GroupBox as "LabelControl" for the RadioButton
162 // (_after_ having inserted the model into the page!)
163 xRadioModel->setPropertyValue("LabelControl", makeAny(_rContext.xObjectModel));
166 // group the shapes
169 Reference< XShapeGrouper > xGrouper(_rContext.xDrawPage, UNO_QUERY);
170 if (xGrouper.is())
172 Reference< XShapeGroup > xGroupedOptions = xGrouper->group(xButtonCollection);
173 Reference< XSelectionSupplier > xSelector(_rContext.xDocumentModel->getCurrentController(), UNO_QUERY);
174 if (xSelector.is())
175 xSelector->select(makeAny(xGroupedOptions));
178 catch(Exception&)
180 OSL_FAIL("OOptionGroupLayouter::doLayout: caught an exception while grouping the shapes!");
185 void OOptionGroupLayouter::implAnchorShape(const Reference< XPropertySet >& _rxShapeProps)
187 static const char s_sAnchorPropertyName[] = "AnchorType";
188 Reference< XPropertySetInfo > xPropertyInfo;
189 if (_rxShapeProps.is())
190 xPropertyInfo = _rxShapeProps->getPropertySetInfo();
191 if (xPropertyInfo.is() && xPropertyInfo->hasPropertyByName(s_sAnchorPropertyName))
192 _rxShapeProps->setPropertyValue(s_sAnchorPropertyName, makeAny(TextContentAnchorType_AT_PAGE));
196 } // namespace dbp
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */