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