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 <sal/config.h>
22 #include <o3tl/any.hxx>
23 #include <o3tl/string_view.hxx>
24 #include <osl/diagnose.h>
25 #include <editeng/paperinf.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <tools/UnitConversion.hxx>
29 #include <unotools/useroptions.hxx>
30 #include <swmodule.hxx>
31 #include <swtypes.hxx>
33 #include <strings.hrc>
40 using namespace ::com::sun::star::uno
;
43 SfxPoolItem
* SwEnvItem::CreateDefault() { return new SwEnvItem
; }
47 SvtUserOptions
& rUserOpt
= SW_MOD()->GetUserOptions();
49 const OUString
sSenderToken(SwResId(STR_SENDER_TOKENS
));
50 if (sSenderToken
.isEmpty())
54 sal_Int32 nSttPos
= 0;
55 bool bLastLength
= true;
57 std::u16string_view sToken
= o3tl::getToken(sSenderToken
, 0, ';', nSttPos
);
58 if (sToken
== u
"COMPANY")
60 sal_Int32 nOldLen
= sRet
.getLength();
61 sRet
.append(rUserOpt
.GetCompany());
62 bLastLength
= sRet
.getLength() != nOldLen
;
64 else if (sToken
== u
"CR")
67 sRet
.append(SAL_NEWLINE_STRING
);
70 else if (sToken
== u
"FIRSTNAME")
71 sRet
.append(rUserOpt
.GetFirstName());
72 else if (sToken
== u
"LASTNAME")
73 sRet
.append(rUserOpt
.GetLastName());
74 else if (sToken
== u
"ADDRESS")
75 sRet
.append(rUserOpt
.GetStreet());
76 else if (sToken
== u
"COUNTRY")
77 sRet
.append(rUserOpt
.GetCountry());
78 else if (sToken
== u
"POSTALCODE")
79 sRet
.append(rUserOpt
.GetZip());
80 else if (sToken
== u
"CITY")
81 sRet
.append(rUserOpt
.GetCity());
82 else if (sToken
== u
"STATEPROV")
83 sRet
.append(rUserOpt
.GetState());
84 else if (!sToken
.empty()) //spaces
87 return sRet
.makeStringAndClear();
90 SwEnvItem::SwEnvItem() :
91 SfxPoolItem(FN_ENVELOP
)
94 m_aSendText
= MakeSender();
95 m_nSendFromLeft
= 566; // 1 cm
96 m_nSendFromTop
= 566; // 1 cm
97 Size aEnvSz
= SvxPaperInfo::GetPaperSize(PAPER_ENV_C65
);
98 m_nWidth
= aEnvSz
.Width();
99 m_nHeight
= aEnvSz
.Height();
100 m_eAlign
= ENV_HOR_LEFT
;
101 m_bPrintFromAbove
= true;
105 m_nAddrFromLeft
= std::max(m_nWidth
, m_nHeight
) / 2;
106 m_nAddrFromTop
= std::min(m_nWidth
, m_nHeight
) / 2;
109 SwEnvItem
& SwEnvItem::operator =(const SwEnvItem
& rItem
)
111 m_aAddrText
= rItem
.m_aAddrText
;
112 m_bSend
= rItem
.m_bSend
;
113 m_aSendText
= rItem
.m_aSendText
;
114 m_nSendFromLeft
= rItem
.m_nSendFromLeft
;
115 m_nSendFromTop
= rItem
.m_nSendFromTop
;
116 m_nAddrFromLeft
= rItem
.m_nAddrFromLeft
;
117 m_nAddrFromTop
= rItem
.m_nAddrFromTop
;
118 m_nWidth
= rItem
.m_nWidth
;
119 m_nHeight
= rItem
.m_nHeight
;
120 m_eAlign
= rItem
.m_eAlign
;
121 m_bPrintFromAbove
= rItem
.m_bPrintFromAbove
;
122 m_nShiftRight
= rItem
.m_nShiftRight
;
123 m_nShiftDown
= rItem
.m_nShiftDown
;
127 bool SwEnvItem::operator ==(const SfxPoolItem
& rItem
) const
129 assert(SfxPoolItem::operator==(rItem
));
130 const SwEnvItem
& rEnv
= static_cast<const SwEnvItem
&>( rItem
);
132 return m_aAddrText
== rEnv
.m_aAddrText
&&
133 m_bSend
== rEnv
.m_bSend
&&
134 m_aSendText
== rEnv
.m_aSendText
&&
135 m_nSendFromLeft
== rEnv
.m_nSendFromLeft
&&
136 m_nSendFromTop
== rEnv
.m_nSendFromTop
&&
137 m_nAddrFromLeft
== rEnv
.m_nAddrFromLeft
&&
138 m_nAddrFromTop
== rEnv
.m_nAddrFromTop
&&
139 m_nWidth
== rEnv
.m_nWidth
&&
140 m_nHeight
== rEnv
.m_nHeight
&&
141 m_eAlign
== rEnv
.m_eAlign
&&
142 m_bPrintFromAbove
== rEnv
.m_bPrintFromAbove
&&
143 m_nShiftRight
== rEnv
.m_nShiftRight
&&
144 m_nShiftDown
== rEnv
.m_nShiftDown
;
147 SwEnvItem
* SwEnvItem::Clone(SfxItemPool
*) const
149 return new SwEnvItem(*this);
152 SwEnvCfgItem::SwEnvCfgItem() :
153 ConfigItem("Office.Writer/Envelope")
155 Sequence
<OUString
> aNames
= GetPropertyNames();
156 Sequence
<Any
> aValues
= GetProperties(aNames
);
157 EnableNotification(aNames
);
158 const Any
* pValues
= aValues
.getConstArray();
159 OSL_ENSURE(aValues
.getLength() == aNames
.getLength(), "GetProperties failed");
160 if(aValues
.getLength() != aNames
.getLength())
163 for(int nProp
= 0; nProp
< aNames
.getLength(); nProp
++)
165 if(pValues
[nProp
].hasValue())
169 case 0: pValues
[nProp
] >>= m_aEnvItem
.m_aAddrText
; break;// "Inscription/Addressee",
170 case 1: pValues
[nProp
] >>= m_aEnvItem
.m_aSendText
; break;// "Inscription/Sender",
171 case 2: m_aEnvItem
.m_bSend
= *o3tl::doAccess
<bool>(pValues
[nProp
]); break;// "Inscription/UseSender",
173 pValues
[nProp
] >>= m_aEnvItem
.m_nAddrFromLeft
;// "Format/AddresseeFromLeft",
174 m_aEnvItem
.m_nAddrFromLeft
= o3tl::toTwips(m_aEnvItem
.m_nAddrFromLeft
, o3tl::Length::mm100
);
177 pValues
[nProp
] >>= m_aEnvItem
.m_nAddrFromTop
; // "Format/AddresseeFromTop",
178 m_aEnvItem
.m_nAddrFromTop
= o3tl::toTwips(m_aEnvItem
.m_nAddrFromTop
, o3tl::Length::mm100
);
181 pValues
[nProp
] >>= m_aEnvItem
.m_nSendFromLeft
; // "Format/SenderFromLeft",
182 m_aEnvItem
.m_nSendFromLeft
= o3tl::toTwips(m_aEnvItem
.m_nSendFromLeft
, o3tl::Length::mm100
);
185 pValues
[nProp
] >>= m_aEnvItem
.m_nSendFromTop
;// "Format/SenderFromTop",
186 m_aEnvItem
.m_nSendFromTop
= o3tl::toTwips(m_aEnvItem
.m_nSendFromTop
, o3tl::Length::mm100
);
189 pValues
[nProp
] >>= m_aEnvItem
.m_nWidth
; // "Format/Width",
190 m_aEnvItem
.m_nWidth
= o3tl::toTwips(m_aEnvItem
.m_nWidth
, o3tl::Length::mm100
);
193 pValues
[nProp
] >>= m_aEnvItem
.m_nHeight
; // "Format/Height",
194 m_aEnvItem
.m_nHeight
= o3tl::toTwips(m_aEnvItem
.m_nHeight
, o3tl::Length::mm100
);
199 pValues
[nProp
] >>= nTemp
; m_aEnvItem
.m_eAlign
= static_cast<SwEnvAlign
>(nTemp
); break;// "Print/Alignment",
201 case 10: m_aEnvItem
.m_bPrintFromAbove
= *o3tl::doAccess
<bool>(pValues
[nProp
]); break;// "Print/FromAbove",
203 pValues
[nProp
] >>= m_aEnvItem
.m_nShiftRight
;
204 m_aEnvItem
.m_nShiftRight
= o3tl::toTwips(m_aEnvItem
.m_nShiftRight
, o3tl::Length::mm100
);// "Print/Right",
207 pValues
[nProp
] >>= m_aEnvItem
.m_nShiftDown
;
208 m_aEnvItem
.m_nShiftDown
= o3tl::toTwips(m_aEnvItem
.m_nShiftDown
, o3tl::Length::mm100
);
209 break;// "Print/Down"
215 SwEnvCfgItem::~SwEnvCfgItem()
219 void SwEnvCfgItem::ImplCommit()
221 Sequence
<OUString
> aNames
= GetPropertyNames();
222 Sequence
<Any
> aValues(aNames
.getLength());
223 Any
* pValues
= aValues
.getArray();
225 for(int nProp
= 0; nProp
< aNames
.getLength(); nProp
++)
229 case 0: pValues
[nProp
] <<= m_aEnvItem
.m_aAddrText
; break;// "Inscription/Addressee",
230 case 1: pValues
[nProp
] <<= m_aEnvItem
.m_aSendText
; break;// "Inscription/Sender",
231 case 2: pValues
[nProp
] <<= m_aEnvItem
.m_bSend
; break;// "Inscription/UseSender",
232 case 3: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nAddrFromLeft
)) ; break;// "Format/AddresseeFromLeft",
233 case 4: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nAddrFromTop
)) ; break;// "Format/AddresseeFromTop",
234 case 5: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nSendFromLeft
)) ; break;// "Format/SenderFromLeft",
235 case 6: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nSendFromTop
)) ; break;// "Format/SenderFromTop",
236 case 7: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nWidth
)) ; break;// "Format/Width",
237 case 8: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nHeight
)) ; break;// "Format/Height",
238 case 9: pValues
[nProp
] <<= sal_Int32(m_aEnvItem
.m_eAlign
); break;// "Print/Alignment",
239 case 10: pValues
[nProp
] <<= m_aEnvItem
.m_bPrintFromAbove
; break;// "Print/FromAbove",
240 case 11: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nShiftRight
));break; // "Print/Right",
241 case 12: pValues
[nProp
] <<= static_cast <sal_Int32
>(convertTwipToMm100(m_aEnvItem
.m_nShiftDown
)); break;// "Print/Down"
244 PutProperties(aNames
, aValues
);
247 void SwEnvCfgItem::Notify( const css::uno::Sequence
< OUString
>& ) {}
249 Sequence
<OUString
> SwEnvCfgItem::GetPropertyNames()
251 static const char* aPropNames
[] =
253 "Inscription/Addressee", // 0
254 "Inscription/Sender", // 1
255 "Inscription/UseSender", // 2
256 "Format/AddresseeFromLeft", // 3
257 "Format/AddresseeFromTop", // 4
258 "Format/SenderFromLeft", // 5
259 "Format/SenderFromTop", // 6
261 "Format/Height", // 8
262 "Print/Alignment", // 9
263 "Print/FromAbove", // 10
267 const int nCount
= 13;
268 Sequence
<OUString
> aNames(nCount
);
269 OUString
* pNames
= aNames
.getArray();
271 for(int i
= 0; i
< nCount
; i
++)
272 pNames
[i
] = OUString::createFromAscii(aPropNames
[i
]);
277 bool SwEnvItem::QueryValue( Any
& rVal
, sal_uInt8 nMemberId
) const
280 switch(nMemberId
& ~CONVERT_TWIPS
)
282 case MID_ENV_ADDR_TEXT
: rVal
<<= m_aAddrText
; break;
283 case MID_ENV_SEND
: rVal
<<= m_bSend
; break;
284 case MID_SEND_TEXT
: rVal
<<= m_aSendText
; break;
285 case MID_ENV_ADDR_FROM_LEFT
: rVal
<<= m_nAddrFromLeft
; break;
286 case MID_ENV_ADDR_FROM_TOP
: rVal
<<= m_nAddrFromTop
; break;
287 case MID_ENV_SEND_FROM_LEFT
: rVal
<<= m_nSendFromLeft
; break;
288 case MID_ENV_SEND_FROM_TOP
: rVal
<<= m_nSendFromTop
; break;
289 case MID_ENV_WIDTH
: rVal
<<= m_nWidth
; break;
290 case MID_ENV_HEIGHT
: rVal
<<= m_nHeight
; break;
291 case MID_ENV_ALIGN
: rVal
<<= static_cast<sal_Int16
>(m_eAlign
); break;
292 case MID_ENV_PRINT_FROM_ABOVE
: rVal
<<= m_bPrintFromAbove
; break;
293 case MID_ENV_SHIFT_RIGHT
: rVal
<<= m_nShiftRight
; break;
294 case MID_ENV_SHIFT_DOWN
: rVal
<<= m_nShiftDown
; break;
296 OSL_FAIL("Wrong memberId");
302 bool SwEnvItem::PutValue(const Any
& rVal
, sal_uInt8 nMemberId
)
305 switch(nMemberId
& ~CONVERT_TWIPS
)
307 case MID_ENV_ADDR_TEXT
: bRet
= (rVal
>>= m_aAddrText
); break;
308 case MID_ENV_SEND
: bRet
= (rVal
>>= m_bSend
); break;
309 case MID_SEND_TEXT
: bRet
= (rVal
>>= m_aSendText
); break;
310 case MID_ENV_ADDR_FROM_LEFT
: bRet
= (rVal
>>= m_nAddrFromLeft
); break;
311 case MID_ENV_ADDR_FROM_TOP
: bRet
= (rVal
>>= m_nAddrFromTop
); break;
312 case MID_ENV_SEND_FROM_LEFT
: bRet
= (rVal
>>= m_nSendFromLeft
); break;
313 case MID_ENV_SEND_FROM_TOP
: bRet
= (rVal
>>= m_nSendFromTop
); break;
314 case MID_ENV_WIDTH
: bRet
= (rVal
>>= m_nWidth
); break;
315 case MID_ENV_HEIGHT
: bRet
= (rVal
>>= m_nHeight
); break;
319 bRet
= (rVal
>>= nTemp
);
321 m_eAlign
= SwEnvAlign(nTemp
);
324 case MID_ENV_PRINT_FROM_ABOVE
: bRet
= (rVal
>>= m_bPrintFromAbove
); break;
325 case MID_ENV_SHIFT_RIGHT
: bRet
= (rVal
>>= m_nShiftRight
); break;
326 case MID_ENV_SHIFT_DOWN
: bRet
= (rVal
>>= m_nShiftDown
); break;
328 OSL_FAIL("Wrong memberId");
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */