merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / envelp / envimg.cxx
blobc81ef6cec9f2b01559cc1dc459dfb2a5444d8566
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: envimg.cxx,v $
10 * $Revision: 1.13.176.2 $
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_sw.hxx"
35 #include <hintids.hxx>
36 #include <tools/stream.hxx>
37 #include <tools/resid.hxx>
38 #include <sfx2/app.hxx>
39 #include <svx/paperinf.hxx>
40 #include <com/sun/star/uno/Sequence.hxx>
41 #include <svtools/useroptions.hxx>
42 #include <tools/shl.hxx>
43 #include <swmodule.hxx>
44 #include <errhdl.hxx>
45 #include <swtypes.hxx>
46 #include <envimg.hxx>
48 #ifndef _CMDID_H
49 #include <cmdid.h>
50 #endif
51 #ifndef _ENVELP_HRC
52 #include <envelp.hrc>
53 #endif
55 #include <unomid.h>
57 #ifdef WIN
58 #define NEXTLINE UniString::CreateFromAscii("\r\n")
59 #else
60 #define NEXTLINE '\n'
61 #endif
63 using namespace utl;
64 using namespace rtl;
65 using namespace ::com::sun::star::uno;
68 TYPEINIT1_AUTOFACTORY( SwEnvItem, SfxPoolItem );
70 // --------------------------------------------------------------------------
71 SW_DLLPUBLIC String MakeSender()
73 SvtUserOptions& rUserOpt = SW_MOD()->GetUserOptions();
75 String sRet;
76 String sSenderToken(SW_RES(STR_SENDER_TOKENS));
77 xub_StrLen nSttPos = 0, nTokenCount = sSenderToken.GetTokenCount(';');
78 BOOL bLastLength = TRUE;
79 for( xub_StrLen i = 0; i < nTokenCount; i++ )
81 String sToken = sSenderToken.GetToken( 0, ';', nSttPos );
82 if(sToken.EqualsAscii("COMPANY"))
84 xub_StrLen nOldLen = sRet.Len();
85 sRet += (String)rUserOpt.GetCompany();
86 bLastLength = sRet.Len() != nOldLen;
88 else if(sToken.EqualsAscii("CR"))
90 if(bLastLength)
91 sRet +=NEXTLINE;
92 bLastLength = TRUE;
94 else if(sToken.EqualsAscii("FIRSTNAME"))
95 sRet += (String)rUserOpt.GetFirstName();
96 else if(sToken.EqualsAscii("LASTNAME"))
97 sRet += (String)rUserOpt.GetLastName();
98 else if(sToken.EqualsAscii("ADDRESS"))
99 sRet += (String)rUserOpt.GetStreet();
100 else if(sToken.EqualsAscii("COUNTRY"))
101 sRet += (String)rUserOpt.GetCountry();
102 else if(sToken.EqualsAscii("POSTALCODE"))
103 sRet += (String)rUserOpt.GetZip();
104 else if(sToken.EqualsAscii("CITY"))
105 sRet += (String)rUserOpt.GetCity();
106 else if(sToken.EqualsAscii("STATEPROV"))
107 sRet += (String)rUserOpt.GetState();
108 else if(sToken.Len()) //spaces
109 sRet += sToken;
111 return sRet;
113 // --------------------------------------------------------------------------
114 SwEnvItem::SwEnvItem() :
115 SfxPoolItem(FN_ENVELOP)
117 aAddrText = aEmptyStr;
118 bSend = TRUE;
119 aSendText = MakeSender();
120 lSendFromLeft = 566; // 1 cm
121 lSendFromTop = 566; // 1 cm
122 Size aEnvSz = SvxPaperInfo::GetPaperSize(PAPER_ENV_C65);
123 lWidth = aEnvSz.Width();
124 lHeight = aEnvSz.Height();
125 eAlign = ENV_HOR_LEFT;
126 bPrintFromAbove = TRUE;
127 lShiftRight = 0;
128 lShiftDown = 0;
130 lAddrFromLeft = Max(lWidth, lHeight) / 2;
131 lAddrFromTop = Min(lWidth, lHeight) / 2;
133 // --------------------------------------------------------------------------
134 SwEnvItem::SwEnvItem(const SwEnvItem& rItem) :
135 SfxPoolItem(FN_ENVELOP),
136 aAddrText (rItem.aAddrText),
137 bSend (rItem.bSend),
138 aSendText (rItem.aSendText),
139 lAddrFromLeft (rItem.lAddrFromLeft),
140 lAddrFromTop (rItem.lAddrFromTop),
141 lSendFromLeft (rItem.lSendFromLeft),
142 lSendFromTop (rItem.lSendFromTop),
143 lWidth (rItem.lWidth),
144 lHeight (rItem.lHeight),
145 eAlign (rItem.eAlign),
146 bPrintFromAbove(rItem.bPrintFromAbove),
147 lShiftRight (rItem.lShiftRight),
148 lShiftDown (rItem.lShiftDown)
152 // --------------------------------------------------------------------------
153 SwEnvItem& SwEnvItem::operator =(const SwEnvItem& rItem)
155 aAddrText = rItem.aAddrText;
156 bSend = rItem.bSend;
157 aSendText = rItem.aSendText;
158 lSendFromLeft = rItem.lSendFromLeft;
159 lSendFromTop = rItem.lSendFromTop;
160 lAddrFromLeft = rItem.lAddrFromLeft;
161 lAddrFromTop = rItem.lAddrFromTop;
162 lWidth = rItem.lWidth;
163 lHeight = rItem.lHeight;
164 eAlign = rItem.eAlign;
165 bPrintFromAbove = rItem.bPrintFromAbove;
166 lShiftRight = rItem.lShiftRight;
167 lShiftDown = rItem.lShiftDown;
168 return *this;
170 // --------------------------------------------------------------------------
171 int SwEnvItem::operator ==(const SfxPoolItem& rItem) const
173 const SwEnvItem& rEnv = (const SwEnvItem&) rItem;
175 return aAddrText == rEnv.aAddrText &&
176 bSend == rEnv.bSend &&
177 aSendText == rEnv.aSendText &&
178 lSendFromLeft == rEnv.lSendFromLeft &&
179 lSendFromTop == rEnv.lSendFromTop &&
180 lAddrFromLeft == rEnv.lAddrFromLeft &&
181 lAddrFromTop == rEnv.lAddrFromTop &&
182 lWidth == rEnv.lWidth &&
183 lHeight == rEnv.lHeight &&
184 eAlign == rEnv.eAlign &&
185 bPrintFromAbove == rEnv.bPrintFromAbove &&
186 lShiftRight == rEnv.lShiftRight &&
187 lShiftDown == rEnv.lShiftDown;
190 // --------------------------------------------------------------------------
191 SfxPoolItem* SwEnvItem::Clone(SfxItemPool*) const
193 return new SwEnvItem(*this);
195 // --------------------------------------------------------------------------
196 // --------------------------------------------------------------------------
197 SwEnvCfgItem::SwEnvCfgItem() :
198 ConfigItem(C2U("Office.Writer/Envelope"))
200 Sequence<OUString> aNames = GetPropertyNames();
201 Sequence<Any> aValues = GetProperties(aNames);
202 EnableNotification(aNames);
203 const Any* pValues = aValues.getConstArray();
204 DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed");
205 if(aValues.getLength() == aNames.getLength())
207 for(int nProp = 0; nProp < aNames.getLength(); nProp++)
209 if(pValues[nProp].hasValue())
211 switch(nProp)
213 case 0: pValues[nProp] >>= aEnvItem.aAddrText; break;// "Inscription/Addressee",
214 case 1: pValues[nProp] >>= aEnvItem.aSendText; break;// "Inscription/Sender",
215 case 2: aEnvItem.bSend = *(sal_Bool*)pValues[nProp].getValue(); break;// "Inscription/UseSender",
216 case 3:
217 pValues[nProp] >>= aEnvItem.lAddrFromLeft;// "Format/AddresseeFromLeft",
218 aEnvItem.lAddrFromLeft = MM100_TO_TWIP(aEnvItem.lAddrFromLeft);
219 break;
220 case 4:
221 pValues[nProp] >>= aEnvItem.lAddrFromTop; // "Format/AddresseeFromTop",
222 aEnvItem.lAddrFromTop = MM100_TO_TWIP(aEnvItem.lAddrFromTop);
223 break;
224 case 5:
225 pValues[nProp] >>= aEnvItem.lSendFromLeft; // "Format/SenderFromLeft",
226 aEnvItem.lSendFromLeft = MM100_TO_TWIP(aEnvItem.lSendFromLeft);
227 break;
228 case 6:
229 pValues[nProp] >>= aEnvItem.lSendFromTop;// "Format/SenderFromTop",
230 aEnvItem.lSendFromTop = MM100_TO_TWIP(aEnvItem.lSendFromTop);
231 break;
232 case 7:
233 pValues[nProp] >>= aEnvItem.lWidth; // "Format/Width",
234 aEnvItem.lWidth = MM100_TO_TWIP(aEnvItem.lWidth);
235 break;
236 case 8:
237 pValues[nProp] >>= aEnvItem.lHeight; // "Format/Height",
238 aEnvItem.lHeight = MM100_TO_TWIP(aEnvItem.lHeight);
239 break;
240 case 9:
242 sal_Int32 nTemp = 0;
243 pValues[nProp] >>= nTemp; aEnvItem.eAlign = (SwEnvAlign)nTemp; break;// "Print/Alignment",
245 case 10: aEnvItem.bPrintFromAbove = *(sal_Bool*)pValues[nProp].getValue(); break;// "Print/FromAbove",
246 case 11:
247 pValues[nProp] >>= aEnvItem.lShiftRight;
248 aEnvItem.lShiftRight = MM100_TO_TWIP(aEnvItem.lShiftRight);// "Print/Right",
249 break;
250 case 12:
251 pValues[nProp] >>= aEnvItem.lShiftDown;
252 aEnvItem.lShiftDown = MM100_TO_TWIP(aEnvItem.lShiftDown);
253 break;// "Print/Down"
259 /* -----------------------------26.09.00 14:04--------------------------------
261 ---------------------------------------------------------------------------*/
262 SwEnvCfgItem::~SwEnvCfgItem()
265 /* -----------------------------26.09.00 14:05--------------------------------
267 ---------------------------------------------------------------------------*/
268 void SwEnvCfgItem::Commit()
270 Sequence<OUString> aNames = GetPropertyNames();
271 Sequence<Any> aValues(aNames.getLength());
272 Any* pValues = aValues.getArray();
274 const Type& rType = ::getBooleanCppuType();
275 for(int nProp = 0; nProp < aNames.getLength(); nProp++)
277 switch(nProp)
279 case 0: pValues[nProp] <<= aEnvItem.aAddrText; break;// "Inscription/Addressee",
280 case 1: pValues[nProp] <<= aEnvItem.aSendText; break;// "Inscription/Sender",
281 case 2: pValues[nProp].setValue(&aEnvItem.bSend, rType);break;// "Inscription/UseSender",
282 case 3: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lAddrFromLeft)) ; break;// "Format/AddresseeFromLeft",
283 case 4: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lAddrFromTop)) ; break;// "Format/AddresseeFromTop",
284 case 5: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lSendFromLeft)) ; break;// "Format/SenderFromLeft",
285 case 6: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lSendFromTop)) ; break;// "Format/SenderFromTop",
286 case 7: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lWidth)) ; break;// "Format/Width",
287 case 8: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lHeight)) ; break;// "Format/Height",
288 case 9: pValues[nProp] <<= sal_Int32(aEnvItem.eAlign); break;// "Print/Alignment",
289 case 10: pValues[nProp].setValue(&aEnvItem.bPrintFromAbove, rType); break;// "Print/FromAbove",
290 case 11: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lShiftRight));break; // "Print/Right",
291 case 12: pValues[nProp] <<= static_cast <sal_Int32>(TWIP_TO_MM100(aEnvItem.lShiftDown)); break;// "Print/Down"
294 PutProperties(aNames, aValues);
296 /* -----------------------------26.09.00 14:04--------------------------------
298 ---------------------------------------------------------------------------*/
299 Sequence<rtl::OUString> SwEnvCfgItem::GetPropertyNames()
301 static const char* aPropNames[] =
303 "Inscription/Addressee", // 0
304 "Inscription/Sender", // 1
305 "Inscription/UseSender", // 2
306 "Format/AddresseeFromLeft", // 3
307 "Format/AddresseeFromTop", // 4
308 "Format/SenderFromLeft", // 5
309 "Format/SenderFromTop", // 6
310 "Format/Width", // 7
311 "Format/Height", // 8
312 "Print/Alignment", // 9
313 "Print/FromAbove", // 10
314 "Print/Right", // 11
315 "Print/Down" // 12
317 const int nCount = 13;
318 Sequence<OUString> aNames(nCount);
319 OUString* pNames = aNames.getArray();
320 for(int i = 0; i < nCount; i++)
321 pNames[i] = OUString::createFromAscii(aPropNames[i]);
322 return aNames;
325 BOOL SwEnvItem::QueryValue( Any& rVal, BYTE nMemberId ) const
327 sal_Bool bRet = sal_True;
328 switch(nMemberId & ~CONVERT_TWIPS)
330 case MID_ENV_ADDR_TEXT : rVal <<= aAddrText; break;
331 case MID_ENV_SEND : rVal <<= bSend; break;
332 case MID_SEND_TEXT : rVal <<= aSendText; break;
333 case MID_ENV_ADDR_FROM_LEFT : rVal <<= lAddrFromLeft; break;
334 case MID_ENV_ADDR_FROM_TOP : rVal <<= lAddrFromTop; break;
335 case MID_ENV_SEND_FROM_LEFT : rVal <<= lSendFromLeft; break;
336 case MID_ENV_SEND_FROM_TOP : rVal <<= lSendFromTop; break;
337 case MID_ENV_WIDTH : rVal <<= lWidth; break;
338 case MID_ENV_HEIGHT : rVal <<= lHeight; break;
339 case MID_ENV_ALIGN : rVal <<= (sal_Int16) eAlign; break;
340 case MID_ENV_PRINT_FROM_ABOVE : rVal <<= bPrintFromAbove; break;
341 case MID_ENV_SHIFT_RIGHT : rVal <<= lShiftRight; break;
342 case MID_ENV_SHIFT_DOWN : rVal <<= lShiftDown; break;
343 default:
344 DBG_ERROR("Wrong memberId");
345 bRet = sal_False;
347 return bRet;
349 /* -----------------------------26.04.01 12:26--------------------------------
351 ---------------------------------------------------------------------------*/
352 BOOL SwEnvItem::PutValue(const Any& rVal, BYTE nMemberId)
354 BOOL bRet = FALSE;
355 switch(nMemberId & ~CONVERT_TWIPS)
357 case MID_ENV_ADDR_TEXT : bRet = (rVal >>= aAddrText); break;
358 case MID_ENV_SEND : bRet = (rVal >>= bSend); break;
359 case MID_SEND_TEXT : bRet = (rVal >>= aSendText); break;
360 case MID_ENV_ADDR_FROM_LEFT : bRet = (rVal >>= lAddrFromLeft); break;
361 case MID_ENV_ADDR_FROM_TOP : bRet = (rVal >>= lAddrFromTop); break;
362 case MID_ENV_SEND_FROM_LEFT : bRet = (rVal >>= lSendFromLeft); break;
363 case MID_ENV_SEND_FROM_TOP : bRet = (rVal >>= lSendFromTop); break;
364 case MID_ENV_WIDTH : bRet = (rVal >>= lWidth); break;
365 case MID_ENV_HEIGHT : bRet = (rVal >>= lHeight); break;
366 case MID_ENV_ALIGN :
368 sal_Int16 nTemp = 0;
369 bRet = (rVal >>= nTemp);
370 if (bRet)
371 eAlign = SwEnvAlign(nTemp);
372 break;
374 case MID_ENV_PRINT_FROM_ABOVE : bRet = (rVal >>= bPrintFromAbove); break;
375 case MID_ENV_SHIFT_RIGHT : bRet = (rVal >>= lShiftRight); break;
376 case MID_ENV_SHIFT_DOWN : bRet = (rVal >>= lShiftDown); break;
377 default:
378 DBG_ERROR("Wrong memberId");
380 return bRet;