1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir #include "optiongrouplayouter.hxx"
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <tools/string.hxx>
29cdf0e10cSrcweir #include <com/sun/star/awt/Size.hpp>
30cdf0e10cSrcweir #include <com/sun/star/awt/Point.hpp>
31cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
32cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
33cdf0e10cSrcweir #include <com/sun/star/drawing/XShapes.hpp>
34cdf0e10cSrcweir #include <com/sun/star/drawing/XShapeGrouper.hpp>
35cdf0e10cSrcweir #include <com/sun/star/text/TextContentAnchorType.hpp>
36cdf0e10cSrcweir #include <com/sun/star/view/XSelectionSupplier.hpp>
37cdf0e10cSrcweir #include "controlwizard.hxx"
38cdf0e10cSrcweir #include "groupboxwiz.hxx"
39cdf0e10cSrcweir #include "dbptools.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //.........................................................................
42cdf0e10cSrcweir namespace dbp
43cdf0e10cSrcweir {
44cdf0e10cSrcweir //.........................................................................
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define BUTTON_HEIGHT		300
47cdf0e10cSrcweir #define TOP_HEIGHT			300
48cdf0e10cSrcweir #define HEIGHT				450
49cdf0e10cSrcweir #define OFFSET				300
50cdf0e10cSrcweir #define MIN_WIDTH			600
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
53cdf0e10cSrcweir 	using namespace ::com::sun::star::drawing;
54cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
55cdf0e10cSrcweir 	using namespace ::com::sun::star::awt;
56cdf0e10cSrcweir 	using namespace ::com::sun::star::container;
57cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
58cdf0e10cSrcweir 	using namespace ::com::sun::star::text;
59cdf0e10cSrcweir 	using namespace ::com::sun::star::view;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	//=====================================================================
62cdf0e10cSrcweir 	//= OOptionGroupLayouter
63cdf0e10cSrcweir 	//=====================================================================
64cdf0e10cSrcweir 	//---------------------------------------------------------------------
OOptionGroupLayouter(const Reference<XMultiServiceFactory> & _rxORB)65cdf0e10cSrcweir 	OOptionGroupLayouter::OOptionGroupLayouter(const Reference< XMultiServiceFactory >& _rxORB)
66cdf0e10cSrcweir 		:m_xORB(_rxORB)
67cdf0e10cSrcweir 	{
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	//---------------------------------------------------------------------
doLayout(const OControlWizardContext & _rContext,const OOptionGroupSettings & _rSettings)71cdf0e10cSrcweir 	void OOptionGroupLayouter::doLayout(const OControlWizardContext& _rContext, const OOptionGroupSettings& _rSettings)
72cdf0e10cSrcweir 	{
73cdf0e10cSrcweir 		Reference< XShapes > xPageShapes(_rContext.xDrawPage, UNO_QUERY);
74cdf0e10cSrcweir 		if (!xPageShapes.is())
75cdf0e10cSrcweir 		{
76cdf0e10cSrcweir 			DBG_ERROR("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
77cdf0e10cSrcweir 			return;
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		Reference< XMultiServiceFactory > xDocFactory(_rContext.xDocumentModel, UNO_QUERY);
81cdf0e10cSrcweir 		if (!xDocFactory.is())
82cdf0e10cSrcweir 		{
83cdf0e10cSrcweir 			DBG_ERROR("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
84cdf0e10cSrcweir 			return;
85cdf0e10cSrcweir 		}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 		// no. of buttons to create
88cdf0e10cSrcweir 		sal_Int32 nRadioButtons = _rSettings.aLabels.size();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 		sal_Int32 nTopSpace = 0;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 		// the shape of the groupbox
93cdf0e10cSrcweir 		::com::sun::star::awt::Size aControlShapeSize = _rContext.xObjectShape->getSize();
94cdf0e10cSrcweir 		// maybe need to adjust the size if the control shapes
95cdf0e10cSrcweir 		sal_Int32 nMinShapeHeight = BUTTON_HEIGHT*(nRadioButtons+1) + BUTTON_HEIGHT + BUTTON_HEIGHT/4;
96cdf0e10cSrcweir 		if (aControlShapeSize.Height < nMinShapeHeight)
97cdf0e10cSrcweir 			aControlShapeSize.Height = nMinShapeHeight;
98cdf0e10cSrcweir 		if (aControlShapeSize.Width < MIN_WIDTH)
99cdf0e10cSrcweir 			aControlShapeSize.Width = MIN_WIDTH;
100cdf0e10cSrcweir 		_rContext.xObjectShape->setSize(aControlShapeSize);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		// if we're working on a writer document, we need to anchor the shape
103cdf0e10cSrcweir 		implAnchorShape(Reference< XPropertySet >(_rContext.xObjectShape, UNO_QUERY));
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 		// shape collection (for grouping the shapes)
106cdf0e10cSrcweir 		Reference< XShapes > xButtonCollection(m_xORB->createInstance(
107cdf0e10cSrcweir 				::rtl::OUString::createFromAscii("com.sun.star.drawing.ShapeCollection")),
108cdf0e10cSrcweir 			UNO_QUERY);
109cdf0e10cSrcweir 		// first member : the shape of the control
110cdf0e10cSrcweir 		xButtonCollection->add(_rContext.xObjectShape.get());
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 		sal_Int32 nTempHeight = (aControlShapeSize.Height - BUTTON_HEIGHT/4) / (nRadioButtons + 1);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 		::com::sun::star::awt::Point aShapePosition = _rContext.xObjectShape->getPosition();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		::com::sun::star::awt::Size aButtonSize(aControlShapeSize);
117cdf0e10cSrcweir 		aButtonSize.Width = aControlShapeSize.Width - OFFSET;
118cdf0e10cSrcweir 		aButtonSize.Height = HEIGHT;
119cdf0e10cSrcweir 		::com::sun::star::awt::Point aButtonPosition;
120cdf0e10cSrcweir 		aButtonPosition.X = aShapePosition.X + OFFSET;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 		::rtl::OUString sElementsName = ::rtl::OUString::createFromAscii("RadioGroup");
123cdf0e10cSrcweir 		disambiguateName(Reference< XNameAccess >(_rContext.xForm, UNO_QUERY), sElementsName);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		StringArray::const_iterator aLabelIter = _rSettings.aLabels.begin();
126cdf0e10cSrcweir 		StringArray::const_iterator aValueIter = _rSettings.aValues.begin();
127cdf0e10cSrcweir 		for (sal_Int32 i=0; i<nRadioButtons; ++i, ++aLabelIter, ++aValueIter)
128cdf0e10cSrcweir 		{
129cdf0e10cSrcweir 			aButtonPosition.Y = aShapePosition.Y + (i+1) * nTempHeight + nTopSpace;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 			Reference< XPropertySet > xRadioModel(
132cdf0e10cSrcweir 					xDocFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.form.component.RadioButton")),
133cdf0e10cSrcweir 				UNO_QUERY);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 			// the label
136cdf0e10cSrcweir 			xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("Label"), makeAny(rtl::OUString(*aLabelIter)));
137cdf0e10cSrcweir 			// the value
138cdf0e10cSrcweir 			xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("RefValue"), makeAny(rtl::OUString(*aValueIter)));
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			// default selection
141cdf0e10cSrcweir 			if (_rSettings.sDefaultField == *aLabelIter)
142cdf0e10cSrcweir 				xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("DefaultState"), makeAny(sal_Int16(1)));
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 			// the connection to the database field
145cdf0e10cSrcweir 			if (0 != _rSettings.sDBField.Len())
146cdf0e10cSrcweir 				xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("DataField"), makeAny(::rtl::OUString(_rSettings.sDBField)));
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			// the name for the model
149cdf0e10cSrcweir 			xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("Name"), makeAny(sElementsName));
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			// create a shape for the radio button
152cdf0e10cSrcweir 			Reference< XControlShape > xRadioShape(
153cdf0e10cSrcweir 					xDocFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.drawing.ControlShape")),
154cdf0e10cSrcweir 				UNO_QUERY);
155cdf0e10cSrcweir 			Reference< XPropertySet > xShapeProperties(xRadioShape, UNO_QUERY);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 			// if we're working on a writer document, we need to anchor the shape
158cdf0e10cSrcweir 			implAnchorShape(xShapeProperties);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			// position it
161cdf0e10cSrcweir 			xRadioShape->setSize(aButtonSize);
162cdf0e10cSrcweir 			xRadioShape->setPosition(aButtonPosition);
163cdf0e10cSrcweir 			// knitting with the model
164cdf0e10cSrcweir 			xRadioShape->setControl(Reference< XControlModel >(xRadioModel, UNO_QUERY));
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			// the name of the shape
167cdf0e10cSrcweir 			if (xShapeProperties.is())
168cdf0e10cSrcweir 				xShapeProperties->setPropertyValue(::rtl::OUString::createFromAscii("Name"), makeAny(sElementsName));
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			// add to the page
171cdf0e10cSrcweir 			xPageShapes->add(xRadioShape.get());
172cdf0e10cSrcweir 			// add to the collection (for the later grouping)
173cdf0e10cSrcweir 			xButtonCollection->add(xRadioShape.get());
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 			// set the GroupBox as "LabelControl" for the RadioButton
176cdf0e10cSrcweir 			// (_after_ having inserted the model into the page!)
177cdf0e10cSrcweir 			xRadioModel->setPropertyValue(::rtl::OUString::createFromAscii("LabelControl"), makeAny(_rContext.xObjectModel));
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		// group the shapes
181cdf0e10cSrcweir 		try
182cdf0e10cSrcweir 		{
183cdf0e10cSrcweir 			Reference< XShapeGrouper > xGrouper(_rContext.xDrawPage, UNO_QUERY);
184cdf0e10cSrcweir 			if (xGrouper.is())
185cdf0e10cSrcweir 			{
186cdf0e10cSrcweir 				Reference< XShapeGroup > xGroupedOptions = xGrouper->group(xButtonCollection);
187cdf0e10cSrcweir 				Reference< XSelectionSupplier > xSelector(_rContext.xDocumentModel->getCurrentController(), UNO_QUERY);
188cdf0e10cSrcweir 				if (xSelector.is())
189cdf0e10cSrcweir 					xSelector->select(makeAny(xGroupedOptions));
190cdf0e10cSrcweir 			}
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		catch(Exception&)
193cdf0e10cSrcweir 		{
194cdf0e10cSrcweir 			DBG_ERROR("OOptionGroupLayouter::doLayout: caught an exception while grouping the shapes!");
195cdf0e10cSrcweir 		}
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	//---------------------------------------------------------------------
implAnchorShape(const Reference<XPropertySet> & _rxShapeProps)199cdf0e10cSrcweir 	void OOptionGroupLayouter::implAnchorShape(const Reference< XPropertySet >& _rxShapeProps)
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		static const ::rtl::OUString s_sAnchorPropertyName = ::rtl::OUString::createFromAscii("AnchorType");
202cdf0e10cSrcweir 		Reference< XPropertySetInfo > xPropertyInfo;
203cdf0e10cSrcweir 		if (_rxShapeProps.is())
204cdf0e10cSrcweir 			xPropertyInfo = _rxShapeProps->getPropertySetInfo();
205cdf0e10cSrcweir 		if (xPropertyInfo.is() && xPropertyInfo->hasPropertyByName(s_sAnchorPropertyName))
206cdf0e10cSrcweir 			_rxShapeProps->setPropertyValue(s_sAnchorPropertyName, makeAny(TextContentAnchorType_AT_PAGE));
207cdf0e10cSrcweir 	}
208cdf0e10cSrcweir 
209cdf0e10cSrcweir //.........................................................................
210cdf0e10cSrcweir }	// namespace dbp
211cdf0e10cSrcweir //.........................................................................
212cdf0e10cSrcweir 
213