Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 :
34 :
35 : namespace dbp
36 : {
37 :
38 :
39 : #define BUTTON_HEIGHT 300
40 : #define HEIGHT 450
41 : #define OFFSET 300
42 : #define MIN_WIDTH 600
43 :
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;
52 :
53 0 : OOptionGroupLayouter::OOptionGroupLayouter(const Reference< XComponentContext >& _rxContext)
54 0 : :mxContext(_rxContext)
55 : {
56 0 : }
57 :
58 :
59 0 : void OOptionGroupLayouter::doLayout(const OControlWizardContext& _rContext, const OOptionGroupSettings& _rSettings)
60 : {
61 0 : Reference< XShapes > xPageShapes(_rContext.xDrawPage, UNO_QUERY);
62 0 : if (!xPageShapes.is())
63 : {
64 : OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
65 0 : return;
66 : }
67 :
68 0 : Reference< XMultiServiceFactory > xDocFactory(_rContext.xDocumentModel, UNO_QUERY);
69 0 : if (!xDocFactory.is())
70 : {
71 : OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
72 0 : return;
73 : }
74 :
75 : // no. of buttons to create
76 0 : sal_Int32 nRadioButtons = _rSettings.aLabels.size();
77 :
78 0 : sal_Int32 nTopSpace = 0;
79 :
80 : // the shape of the groupbox
81 0 : ::com::sun::star::awt::Size aControlShapeSize = _rContext.xObjectShape->getSize();
82 : // maybe need to adjust the size if the control shapes
83 0 : sal_Int32 nMinShapeHeight = BUTTON_HEIGHT*(nRadioButtons+1) + BUTTON_HEIGHT + BUTTON_HEIGHT/4;
84 0 : if (aControlShapeSize.Height < nMinShapeHeight)
85 0 : aControlShapeSize.Height = nMinShapeHeight;
86 0 : if (aControlShapeSize.Width < MIN_WIDTH)
87 0 : aControlShapeSize.Width = MIN_WIDTH;
88 0 : _rContext.xObjectShape->setSize(aControlShapeSize);
89 :
90 : // if we're working on a writer document, we need to anchor the shape
91 0 : implAnchorShape(Reference< XPropertySet >(_rContext.xObjectShape, UNO_QUERY));
92 :
93 : // shape collection (for grouping the shapes)
94 0 : Reference< XShapes > xButtonCollection( ShapeCollection::create(mxContext) );
95 : // first member : the shape of the control
96 0 : xButtonCollection->add(_rContext.xObjectShape.get());
97 :
98 0 : sal_Int32 nTempHeight = (aControlShapeSize.Height - BUTTON_HEIGHT/4) / (nRadioButtons + 1);
99 :
100 0 : ::com::sun::star::awt::Point aShapePosition = _rContext.xObjectShape->getPosition();
101 :
102 0 : ::com::sun::star::awt::Size aButtonSize(aControlShapeSize);
103 0 : aButtonSize.Width = aControlShapeSize.Width - OFFSET;
104 0 : aButtonSize.Height = HEIGHT;
105 0 : ::com::sun::star::awt::Point aButtonPosition;
106 0 : aButtonPosition.X = aShapePosition.X + OFFSET;
107 :
108 0 : OUString sElementsName("RadioGroup");
109 0 : disambiguateName(Reference< XNameAccess >(_rContext.xForm, UNO_QUERY), sElementsName);
110 :
111 0 : StringArray::const_iterator aLabelIter = _rSettings.aLabels.begin();
112 0 : StringArray::const_iterator aValueIter = _rSettings.aValues.begin();
113 0 : for (sal_Int32 i=0; i<nRadioButtons; ++i, ++aLabelIter, ++aValueIter)
114 : {
115 0 : aButtonPosition.Y = aShapePosition.Y + (i+1) * nTempHeight + nTopSpace;
116 :
117 : Reference< XPropertySet > xRadioModel(
118 0 : xDocFactory->createInstance("com.sun.star.form.component.RadioButton"),
119 0 : UNO_QUERY);
120 :
121 : // the label
122 0 : xRadioModel->setPropertyValue("Label", makeAny(OUString(*aLabelIter)));
123 : // the value
124 0 : xRadioModel->setPropertyValue("RefValue", makeAny(OUString(*aValueIter)));
125 :
126 : // default selection
127 0 : if (_rSettings.sDefaultField == *aLabelIter)
128 0 : xRadioModel->setPropertyValue("DefaultState", makeAny(sal_Int16(1)));
129 :
130 : // the connection to the database field
131 0 : if (!_rSettings.sDBField.isEmpty())
132 0 : xRadioModel->setPropertyValue("DataField", makeAny(OUString(_rSettings.sDBField)));
133 :
134 : // the name for the model
135 0 : xRadioModel->setPropertyValue("Name", makeAny(sElementsName));
136 :
137 : // create a shape for the radio button
138 : Reference< XControlShape > xRadioShape(
139 0 : xDocFactory->createInstance("com.sun.star.drawing.ControlShape"),
140 0 : UNO_QUERY);
141 0 : Reference< XPropertySet > xShapeProperties(xRadioShape, UNO_QUERY);
142 :
143 : // if we're working on a writer document, we need to anchor the shape
144 0 : implAnchorShape(xShapeProperties);
145 :
146 : // position it
147 0 : xRadioShape->setSize(aButtonSize);
148 0 : xRadioShape->setPosition(aButtonPosition);
149 : // knitting with the model
150 0 : xRadioShape->setControl(Reference< XControlModel >(xRadioModel, UNO_QUERY));
151 :
152 : // the name of the shape
153 0 : if (xShapeProperties.is())
154 0 : xShapeProperties->setPropertyValue("Name", makeAny(sElementsName));
155 :
156 : // add to the page
157 0 : xPageShapes->add(xRadioShape.get());
158 : // add to the collection (for the later grouping)
159 0 : xButtonCollection->add(xRadioShape.get());
160 :
161 : // set the GroupBox as "LabelControl" for the RadioButton
162 : // (_after_ having inserted the model into the page!)
163 0 : xRadioModel->setPropertyValue("LabelControl", makeAny(_rContext.xObjectModel));
164 0 : }
165 :
166 : // group the shapes
167 : try
168 : {
169 0 : Reference< XShapeGrouper > xGrouper(_rContext.xDrawPage, UNO_QUERY);
170 0 : if (xGrouper.is())
171 : {
172 0 : Reference< XShapeGroup > xGroupedOptions = xGrouper->group(xButtonCollection);
173 0 : Reference< XSelectionSupplier > xSelector(_rContext.xDocumentModel->getCurrentController(), UNO_QUERY);
174 0 : if (xSelector.is())
175 0 : xSelector->select(makeAny(xGroupedOptions));
176 0 : }
177 : }
178 0 : catch(Exception&)
179 : {
180 : OSL_FAIL("OOptionGroupLayouter::doLayout: caught an exception while grouping the shapes!");
181 0 : }
182 : }
183 :
184 :
185 0 : void OOptionGroupLayouter::implAnchorShape(const Reference< XPropertySet >& _rxShapeProps)
186 : {
187 : static const char s_sAnchorPropertyName[] = "AnchorType";
188 0 : Reference< XPropertySetInfo > xPropertyInfo;
189 0 : if (_rxShapeProps.is())
190 0 : xPropertyInfo = _rxShapeProps->getPropertySetInfo();
191 0 : if (xPropertyInfo.is() && xPropertyInfo->hasPropertyByName(s_sAnchorPropertyName))
192 0 : _rxShapeProps->setPropertyValue(s_sAnchorPropertyName, makeAny(TextContentAnchorType_AT_PAGE));
193 0 : }
194 :
195 :
196 0 : } // namespace dbp
197 :
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|