1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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"
34 //.........................................................................
37 //.........................................................................
39 #define BUTTON_HEIGHT 300
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
;
53 //=====================================================================
54 //= OOptionGroupLayouter
55 //=====================================================================
56 //---------------------------------------------------------------------
57 OOptionGroupLayouter::OOptionGroupLayouter(const Reference
< XComponentContext
>& _rxContext
)
58 :mxContext(_rxContext
)
62 //---------------------------------------------------------------------
63 void OOptionGroupLayouter::doLayout(const OControlWizardContext
& _rContext
, const OOptionGroupSettings
& _rSettings
)
65 Reference
< XShapes
> xPageShapes(_rContext
.xDrawPage
, UNO_QUERY
);
66 if (!xPageShapes
.is())
68 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
72 Reference
< XMultiServiceFactory
> xDocFactory(_rContext
.xDocumentModel
, UNO_QUERY
);
73 if (!xDocFactory
.is())
75 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
79 // no. of buttons to create
80 sal_Int32 nRadioButtons
= _rSettings
.aLabels
.size();
82 sal_Int32 nTopSpace
= 0;
84 // the shape of the groupbox
85 ::com::sun::star::awt::Size aControlShapeSize
= _rContext
.xObjectShape
->getSize();
86 // maybe need to adjust the size if the control shapes
87 sal_Int32 nMinShapeHeight
= BUTTON_HEIGHT
*(nRadioButtons
+1) + BUTTON_HEIGHT
+ BUTTON_HEIGHT
/4;
88 if (aControlShapeSize
.Height
< nMinShapeHeight
)
89 aControlShapeSize
.Height
= nMinShapeHeight
;
90 if (aControlShapeSize
.Width
< MIN_WIDTH
)
91 aControlShapeSize
.Width
= MIN_WIDTH
;
92 _rContext
.xObjectShape
->setSize(aControlShapeSize
);
94 // if we're working on a writer document, we need to anchor the shape
95 implAnchorShape(Reference
< XPropertySet
>(_rContext
.xObjectShape
, UNO_QUERY
));
97 // shape collection (for grouping the shapes)
98 Reference
< XShapes
> xButtonCollection( ShapeCollection::create(mxContext
) );
99 // first member : the shape of the control
100 xButtonCollection
->add(_rContext
.xObjectShape
.get());
102 sal_Int32 nTempHeight
= (aControlShapeSize
.Height
- BUTTON_HEIGHT
/4) / (nRadioButtons
+ 1);
104 ::com::sun::star::awt::Point aShapePosition
= _rContext
.xObjectShape
->getPosition();
106 ::com::sun::star::awt::Size
aButtonSize(aControlShapeSize
);
107 aButtonSize
.Width
= aControlShapeSize
.Width
- OFFSET
;
108 aButtonSize
.Height
= HEIGHT
;
109 ::com::sun::star::awt::Point aButtonPosition
;
110 aButtonPosition
.X
= aShapePosition
.X
+ OFFSET
;
112 OUString
sElementsName("RadioGroup");
113 disambiguateName(Reference
< XNameAccess
>(_rContext
.xForm
, UNO_QUERY
), sElementsName
);
115 StringArray::const_iterator aLabelIter
= _rSettings
.aLabels
.begin();
116 StringArray::const_iterator aValueIter
= _rSettings
.aValues
.begin();
117 for (sal_Int32 i
=0; i
<nRadioButtons
; ++i
, ++aLabelIter
, ++aValueIter
)
119 aButtonPosition
.Y
= aShapePosition
.Y
+ (i
+1) * nTempHeight
+ nTopSpace
;
121 Reference
< XPropertySet
> xRadioModel(
122 xDocFactory
->createInstance(OUString("com.sun.star.form.component.RadioButton")),
126 xRadioModel
->setPropertyValue(OUString("Label"), makeAny(OUString(*aLabelIter
)));
128 xRadioModel
->setPropertyValue(OUString("RefValue"), makeAny(OUString(*aValueIter
)));
131 if (_rSettings
.sDefaultField
== *aLabelIter
)
132 xRadioModel
->setPropertyValue(OUString("DefaultState"), makeAny(sal_Int16(1)));
134 // the connection to the database field
135 if (0 != _rSettings
.sDBField
.Len())
136 xRadioModel
->setPropertyValue(OUString("DataField"), makeAny(OUString(_rSettings
.sDBField
)));
138 // the name for the model
139 xRadioModel
->setPropertyValue(OUString("Name"), makeAny(sElementsName
));
141 // create a shape for the radio button
142 Reference
< XControlShape
> xRadioShape(
143 xDocFactory
->createInstance(OUString("com.sun.star.drawing.ControlShape")),
145 Reference
< XPropertySet
> xShapeProperties(xRadioShape
, UNO_QUERY
);
147 // if we're working on a writer document, we need to anchor the shape
148 implAnchorShape(xShapeProperties
);
151 xRadioShape
->setSize(aButtonSize
);
152 xRadioShape
->setPosition(aButtonPosition
);
153 // knitting with the model
154 xRadioShape
->setControl(Reference
< XControlModel
>(xRadioModel
, UNO_QUERY
));
156 // the name of the shape
157 if (xShapeProperties
.is())
158 xShapeProperties
->setPropertyValue(OUString("Name"), makeAny(sElementsName
));
161 xPageShapes
->add(xRadioShape
.get());
162 // add to the collection (for the later grouping)
163 xButtonCollection
->add(xRadioShape
.get());
165 // set the GroupBox as "LabelControl" for the RadioButton
166 // (_after_ having inserted the model into the page!)
167 xRadioModel
->setPropertyValue(OUString("LabelControl"), makeAny(_rContext
.xObjectModel
));
173 Reference
< XShapeGrouper
> xGrouper(_rContext
.xDrawPage
, UNO_QUERY
);
176 Reference
< XShapeGroup
> xGroupedOptions
= xGrouper
->group(xButtonCollection
);
177 Reference
< XSelectionSupplier
> xSelector(_rContext
.xDocumentModel
->getCurrentController(), UNO_QUERY
);
179 xSelector
->select(makeAny(xGroupedOptions
));
184 OSL_FAIL("OOptionGroupLayouter::doLayout: caught an exception while grouping the shapes!");
188 //---------------------------------------------------------------------
189 void OOptionGroupLayouter::implAnchorShape(const Reference
< XPropertySet
>& _rxShapeProps
)
191 static const OUString
s_sAnchorPropertyName("AnchorType");
192 Reference
< XPropertySetInfo
> xPropertyInfo
;
193 if (_rxShapeProps
.is())
194 xPropertyInfo
= _rxShapeProps
->getPropertySetInfo();
195 if (xPropertyInfo
.is() && xPropertyInfo
->hasPropertyByName(s_sAnchorPropertyName
))
196 _rxShapeProps
->setPropertyValue(s_sAnchorPropertyName
, makeAny(TextContentAnchorType_AT_PAGE
));
199 //.........................................................................
201 //.........................................................................
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */