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