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