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