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/.
10 #include <com/sun/star/awt/Size.hpp>
11 #include <com/sun/star/awt/XControlModel.hpp>
12 #include <com/sun/star/drawing/XControlShape.hpp>
13 #include <com/sun/star/text/VertOrientation.hpp>
15 #include <vcl/outdev.hxx>
16 #include <vcl/svapp.hxx>
18 #include <DomainMapper_Impl.hxx>
19 #include <StyleSheetTable.hxx>
20 #include <SdtHelper.hxx>
22 namespace writerfilter
{
25 using namespace ::com::sun::star
;
27 /// w:sdt's w:dropDownList doesn't have width, so guess the size based on the longest string.
28 awt::Size
lcl_getOptimalWidth(StyleSheetTablePtr pStyleSheet
, OUString
& rDefault
, std::vector
<OUString
>& rItems
)
30 OUString aLongest
= rDefault
;
31 sal_Int32 nHeight
= 0;
32 for (size_t i
= 0; i
< rItems
.size(); ++i
)
33 if (rItems
[i
].getLength() > aLongest
.getLength())
36 MapMode
aMap(MAP_100TH_MM
);
37 OutputDevice
* pOut
= Application::GetDefaultDevice();
38 pOut
->Push(PUSH_FONT
| PUSH_MAPMODE
);
40 PropertyMapPtr pDefaultCharProps
= pStyleSheet
->GetDefaultCharProps();
41 Font
aFont(pOut
->GetFont());
42 PropertyMap::iterator aFontName
= pDefaultCharProps
->find(PropertyDefinition(PROP_CHAR_FONT_NAME
, false));
43 if (aFontName
!= pDefaultCharProps
->end())
44 aFont
.SetName(aFontName
->second
.get
<OUString
>());
45 PropertyMap::iterator aHeight
= pDefaultCharProps
->find(PropertyDefinition(PROP_CHAR_HEIGHT
, false));
46 if (aHeight
!= pDefaultCharProps
->end())
48 nHeight
= aHeight
->second
.get
<double>() * 35; // points -> mm100
49 aFont
.SetSize(Size(0, nHeight
));
52 pOut
->SetMapMode(aMap
);
53 sal_Int32 nWidth
= pOut
->GetTextWidth(aLongest
);
57 // Border: see PDFWriterImpl::drawFieldBorder(), border size is font height / 4,
58 // so additional width / height needed is height / 2.
59 sal_Int32 nBorder
= nHeight
/ 2;
61 // Width: space for the text + the square having the dropdown arrow.
62 return awt::Size(nWidth
+ nBorder
+ nHeight
, nHeight
+ nBorder
);
65 SdtHelper::SdtHelper(DomainMapper_Impl
& rDM_Impl
)
66 : m_rDM_Impl(rDM_Impl
)
67 , m_bHasElements(false)
71 SdtHelper::~SdtHelper()
75 void SdtHelper::createDropDownControl()
77 OUString aDefaultText
= m_aSdtTexts
.makeStringAndClear();
78 uno::Reference
<awt::XControlModel
> xControlModel(m_rDM_Impl
.GetTextFactory()->createInstance("com.sun.star.form.component.ComboBox"), uno::UNO_QUERY
);
79 uno::Reference
<beans::XPropertySet
> xPropertySet(xControlModel
, uno::UNO_QUERY
);
80 xPropertySet
->setPropertyValue("DefaultText", uno::makeAny(aDefaultText
));
81 xPropertySet
->setPropertyValue("Dropdown", uno::makeAny(sal_True
));
82 uno::Sequence
<OUString
> aItems(m_aDropDownItems
.size());
83 for (size_t i
= 0; i
< m_aDropDownItems
.size(); ++i
)
84 aItems
[i
] = m_aDropDownItems
[i
];
85 xPropertySet
->setPropertyValue("StringItemList", uno::makeAny(aItems
));
87 createControlShape(lcl_getOptimalWidth(m_rDM_Impl
.GetStyleSheetTable(), aDefaultText
, m_aDropDownItems
), xControlModel
);
88 m_aDropDownItems
.clear();
91 void SdtHelper::createDateControl(OUString
& rDefaultText
)
93 uno::Reference
<awt::XControlModel
> xControlModel(m_rDM_Impl
.GetTextFactory()->createInstance("com.sun.star.form.component.DateField"), uno::UNO_QUERY
);
94 uno::Reference
<beans::XPropertySet
> xPropertySet(xControlModel
, uno::UNO_QUERY
);
95 xPropertySet
->setPropertyValue("HelpText", uno::makeAny(rDefaultText
));
96 xPropertySet
->setPropertyValue("Dropdown", uno::makeAny(sal_True
));
97 xPropertySet
->setPropertyValue("DateFormat", uno::makeAny(*m_oDateFormat
));
98 m_oDateFormat
.reset();
100 std::vector
<OUString
> aItems
;
101 createControlShape(lcl_getOptimalWidth(m_rDM_Impl
.GetStyleSheetTable(), rDefaultText
, aItems
), xControlModel
);
104 void SdtHelper::createControlShape(awt::Size aSize
, uno::Reference
<awt::XControlModel
> xControlModel
)
106 uno::Reference
<drawing::XControlShape
> xControlShape(m_rDM_Impl
.GetTextFactory()->createInstance("com.sun.star.drawing.ControlShape"), uno::UNO_QUERY
);
107 xControlShape
->setSize(aSize
);
108 xControlShape
->setControl(xControlModel
);
110 uno::Reference
<beans::XPropertySet
> xPropertySet(xControlShape
, uno::UNO_QUERY
);
111 xPropertySet
->setPropertyValue("VertOrient", uno::makeAny(text::VertOrientation::CENTER
));
113 uno::Reference
<text::XTextContent
> xTextContent(xControlShape
, uno::UNO_QUERY
);
114 m_rDM_Impl
.appendTextContent(xTextContent
, uno::Sequence
< beans::PropertyValue
>());
115 m_bHasElements
= true;
118 std::vector
<OUString
>& SdtHelper::getDropDownItems()
120 return m_aDropDownItems
;
123 OUStringBuffer
& SdtHelper::getSdtTexts()
128 boost::optional
<sal_Int16
>& SdtHelper::getDateFormat()
130 return m_oDateFormat
;
133 bool SdtHelper::hasElements()
135 return m_bHasElements
;
138 } // namespace dmapper
139 } // namespace writerfilter
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */