LibreOffice Module sw (master)  1
envimg.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <hintids.hxx>
21 #include <o3tl/any.hxx>
22 #include <osl/diagnose.h>
23 #include <tools/stream.hxx>
24 #include <sfx2/app.hxx>
25 #include <editeng/paperinf.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <unotools/useroptions.hxx>
28 #include <swmodule.hxx>
29 #include <swtypes.hxx>
30 #include <envimg.hxx>
31 #include <strings.hrc>
32 
33 #include <cmdid.h>
34 
35 #include <unomid.h>
36 
37 #ifdef _WIN32
38 #define NEXTLINE "\r\n"
39 #else
40 #define NEXTLINE "\n"
41 #endif
42 
43 using namespace utl;
44 using namespace ::com::sun::star::uno;
45 
46 
48 
49 OUString MakeSender()
50 {
51  SvtUserOptions& rUserOpt = SW_MOD()->GetUserOptions();
52 
53  const OUString sSenderToken(SwResId(STR_SENDER_TOKENS));
54  if (sSenderToken.isEmpty())
55  return OUString();
56 
57  OUStringBuffer sRet;
58  sal_Int32 nSttPos = 0;
59  bool bLastLength = true;
60  do {
61  OUString sToken = sSenderToken.getToken( 0, ';', nSttPos );
62  if (sToken == "COMPANY")
63  {
64  sal_Int32 nOldLen = sRet.getLength();
65  sRet.append(rUserOpt.GetCompany());
66  bLastLength = sRet.getLength() != nOldLen;
67  }
68  else if (sToken == "CR")
69  {
70  if(bLastLength)
71  sRet.append(NEXTLINE);
72  bLastLength = true;
73  }
74  else if (sToken == "FIRSTNAME")
75  sRet.append(rUserOpt.GetFirstName());
76  else if (sToken == "LASTNAME")
77  sRet.append(rUserOpt.GetLastName());
78  else if (sToken == "ADDRESS")
79  sRet.append(rUserOpt.GetStreet());
80  else if (sToken == "COUNTRY")
81  sRet.append(rUserOpt.GetCountry());
82  else if (sToken == "POSTALCODE")
83  sRet.append(rUserOpt.GetZip());
84  else if (sToken == "CITY")
85  sRet.append(rUserOpt.GetCity());
86  else if (sToken == "STATEPROV")
87  sRet.append(rUserOpt.GetState());
88  else if (!sToken.isEmpty()) //spaces
89  sRet.append(sToken);
90  } while (nSttPos>=0);
91  return sRet.makeStringAndClear();
92 }
93 
96 {
97  m_bSend = true;
99  m_nSendFromLeft = 566; // 1 cm
100  m_nSendFromTop = 566; // 1 cm
102  m_nWidth = aEnvSz.Width();
103  m_nHeight = aEnvSz.Height();
105  m_bPrintFromAbove = true;
106  m_nShiftRight = 0;
107  m_nShiftDown = 0;
108 
109  m_nAddrFromLeft = std::max(m_nWidth, m_nHeight) / 2;
110  m_nAddrFromTop = std::min(m_nWidth, m_nHeight) / 2;
111 }
112 
114 {
115  m_aAddrText = rItem.m_aAddrText;
116  m_bSend = rItem.m_bSend;
117  m_aSendText = rItem.m_aSendText;
122  m_nWidth = rItem.m_nWidth;
123  m_nHeight = rItem.m_nHeight;
124  m_eAlign = rItem.m_eAlign;
127  m_nShiftDown = rItem.m_nShiftDown;
128  return *this;
129 }
130 
131 bool SwEnvItem::operator ==(const SfxPoolItem& rItem) const
132 {
133  const SwEnvItem& rEnv = static_cast<const SwEnvItem&>( rItem);
134 
135  return m_aAddrText == rEnv.m_aAddrText &&
136  m_bSend == rEnv.m_bSend &&
137  m_aSendText == rEnv.m_aSendText &&
139  m_nSendFromTop == rEnv.m_nSendFromTop &&
141  m_nAddrFromTop == rEnv.m_nAddrFromTop &&
142  m_nWidth == rEnv.m_nWidth &&
143  m_nHeight == rEnv.m_nHeight &&
144  m_eAlign == rEnv.m_eAlign &&
146  m_nShiftRight == rEnv.m_nShiftRight &&
147  m_nShiftDown == rEnv.m_nShiftDown;
148 }
149 
151 {
152  return new SwEnvItem(*this);
153 }
154 
156  ConfigItem("Office.Writer/Envelope")
157 {
159  Sequence<Any> aValues = GetProperties(aNames);
160  EnableNotification(aNames);
161  const Any* pValues = aValues.getConstArray();
162  OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
163  if(aValues.getLength() == aNames.getLength())
164  {
165  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
166  {
167  if(pValues[nProp].hasValue())
168  {
169  switch(nProp)
170  {
171  case 0: pValues[nProp] >>= m_aEnvItem.m_aAddrText; break;// "Inscription/Addressee",
172  case 1: pValues[nProp] >>= m_aEnvItem.m_aSendText; break;// "Inscription/Sender",
173  case 2: m_aEnvItem.m_bSend = *o3tl::doAccess<bool>(pValues[nProp]); break;// "Inscription/UseSender",
174  case 3:
175  pValues[nProp] >>= m_aEnvItem.m_nAddrFromLeft;// "Format/AddresseeFromLeft",
177  break;
178  case 4:
179  pValues[nProp] >>= m_aEnvItem.m_nAddrFromTop; // "Format/AddresseeFromTop",
181  break;
182  case 5:
183  pValues[nProp] >>= m_aEnvItem.m_nSendFromLeft; // "Format/SenderFromLeft",
185  break;
186  case 6:
187  pValues[nProp] >>= m_aEnvItem.m_nSendFromTop;// "Format/SenderFromTop",
189  break;
190  case 7:
191  pValues[nProp] >>= m_aEnvItem.m_nWidth; // "Format/Width",
193  break;
194  case 8:
195  pValues[nProp] >>= m_aEnvItem.m_nHeight; // "Format/Height",
197  break;
198  case 9:
199  {
200  sal_Int32 nTemp = 0;
201  pValues[nProp] >>= nTemp; m_aEnvItem.m_eAlign = static_cast<SwEnvAlign>(nTemp); break;// "Print/Alignment",
202  }
203  case 10: m_aEnvItem.m_bPrintFromAbove = *o3tl::doAccess<bool>(pValues[nProp]); break;// "Print/FromAbove",
204  case 11:
205  pValues[nProp] >>= m_aEnvItem.m_nShiftRight;
207  break;
208  case 12:
209  pValues[nProp] >>= m_aEnvItem.m_nShiftDown;
211  break;// "Print/Down"
212  }
213  }
214  }
215  }
216 }
217 
219 {
220 }
221 
223 {
225  Sequence<Any> aValues(aNames.getLength());
226  Any* pValues = aValues.getArray();
227 
228  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
229  {
230  switch(nProp)
231  {
232  case 0: pValues[nProp] <<= m_aEnvItem.m_aAddrText; break;// "Inscription/Addressee",
233  case 1: pValues[nProp] <<= m_aEnvItem.m_aSendText; break;// "Inscription/Sender",
234  case 2: pValues[nProp] <<= m_aEnvItem.m_bSend; break;// "Inscription/UseSender",
235  case 3: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromLeft)) ; break;// "Format/AddresseeFromLeft",
236  case 4: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromTop)) ; break;// "Format/AddresseeFromTop",
237  case 5: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromLeft)) ; break;// "Format/SenderFromLeft",
238  case 6: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromTop)) ; break;// "Format/SenderFromTop",
239  case 7: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nWidth)) ; break;// "Format/Width",
240  case 8: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nHeight)) ; break;// "Format/Height",
241  case 9: pValues[nProp] <<= sal_Int32(m_aEnvItem.m_eAlign); break;// "Print/Alignment",
242  case 10: pValues[nProp] <<= m_aEnvItem.m_bPrintFromAbove; break;// "Print/FromAbove",
243  case 11: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftRight));break; // "Print/Right",
244  case 12: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftDown)); break;// "Print/Down"
245  }
246  }
247  PutProperties(aNames, aValues);
248 }
249 
250 void SwEnvCfgItem::Notify( const css::uno::Sequence< OUString >& ) {}
251 
253 {
254  static const char* aPropNames[] =
255  {
256  "Inscription/Addressee", // 0
257  "Inscription/Sender", // 1
258  "Inscription/UseSender", // 2
259  "Format/AddresseeFromLeft", // 3
260  "Format/AddresseeFromTop", // 4
261  "Format/SenderFromLeft", // 5
262  "Format/SenderFromTop", // 6
263  "Format/Width", // 7
264  "Format/Height", // 8
265  "Print/Alignment", // 9
266  "Print/FromAbove", // 10
267  "Print/Right", // 11
268  "Print/Down" // 12
269  };
270  const int nCount = 13;
271  Sequence<OUString> aNames(nCount);
272  OUString* pNames = aNames.getArray();
273 
274  for(int i = 0; i < nCount; i++)
275  pNames[i] = OUString::createFromAscii(aPropNames[i]);
276 
277  return aNames;
278 }
279 
280 bool SwEnvItem::QueryValue( Any& rVal, sal_uInt8 nMemberId ) const
281 {
282  bool bRet = true;
283  switch(nMemberId & ~CONVERT_TWIPS)
284  {
285  case MID_ENV_ADDR_TEXT : rVal <<= m_aAddrText; break;
286  case MID_ENV_SEND : rVal <<= m_bSend; break;
287  case MID_SEND_TEXT : rVal <<= m_aSendText; break;
288  case MID_ENV_ADDR_FROM_LEFT : rVal <<= m_nAddrFromLeft; break;
289  case MID_ENV_ADDR_FROM_TOP : rVal <<= m_nAddrFromTop; break;
290  case MID_ENV_SEND_FROM_LEFT : rVal <<= m_nSendFromLeft; break;
291  case MID_ENV_SEND_FROM_TOP : rVal <<= m_nSendFromTop; break;
292  case MID_ENV_WIDTH : rVal <<= m_nWidth; break;
293  case MID_ENV_HEIGHT : rVal <<= m_nHeight; break;
294  case MID_ENV_ALIGN : rVal <<= static_cast<sal_Int16>(m_eAlign); break;
295  case MID_ENV_PRINT_FROM_ABOVE : rVal <<= m_bPrintFromAbove; break;
296  case MID_ENV_SHIFT_RIGHT : rVal <<= m_nShiftRight; break;
297  case MID_ENV_SHIFT_DOWN : rVal <<= m_nShiftDown; break;
298  default:
299  OSL_FAIL("Wrong memberId");
300  bRet = false;
301  }
302  return bRet;
303 }
304 
305 bool SwEnvItem::PutValue(const Any& rVal, sal_uInt8 nMemberId)
306 {
307  bool bRet = false;
308  switch(nMemberId & ~CONVERT_TWIPS)
309  {
310  case MID_ENV_ADDR_TEXT : bRet = (rVal >>= m_aAddrText); break;
311  case MID_ENV_SEND : bRet = (rVal >>= m_bSend); break;
312  case MID_SEND_TEXT : bRet = (rVal >>= m_aSendText); break;
313  case MID_ENV_ADDR_FROM_LEFT : bRet = (rVal >>= m_nAddrFromLeft); break;
314  case MID_ENV_ADDR_FROM_TOP : bRet = (rVal >>= m_nAddrFromTop); break;
315  case MID_ENV_SEND_FROM_LEFT : bRet = (rVal >>= m_nSendFromLeft); break;
316  case MID_ENV_SEND_FROM_TOP : bRet = (rVal >>= m_nSendFromTop); break;
317  case MID_ENV_WIDTH : bRet = (rVal >>= m_nWidth); break;
318  case MID_ENV_HEIGHT : bRet = (rVal >>= m_nHeight); break;
319  case MID_ENV_ALIGN :
320  {
321  sal_Int16 nTemp = 0;
322  bRet = (rVal >>= nTemp);
323  if (bRet)
324  m_eAlign = SwEnvAlign(nTemp);
325  break;
326  }
327  case MID_ENV_PRINT_FROM_ABOVE : bRet = (rVal >>= m_bPrintFromAbove); break;
328  case MID_ENV_SHIFT_RIGHT : bRet = (rVal >>= m_nShiftRight); break;
329  case MID_ENV_SHIFT_DOWN : bRet = (rVal >>= m_nShiftDown); break;
330  default:
331  OSL_FAIL("Wrong memberId");
332  }
333  return bRet;
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetCity() const
long Width() const
bool m_bPrintFromAbove
Definition: envimg.hxx:52
#define MID_ENV_WIDTH
Definition: unomid.h:140
SwEnvAlign m_eAlign
Definition: envimg.hxx:51
OUString m_aSendText
Definition: envimg.hxx:44
#define FN_ENVELOP
Definition: cmdid.h:720
#define MID_SEND_TEXT
Definition: unomid.h:135
long Height() const
sal_Int32 m_nHeight
Definition: envimg.hxx:50
#define MID_ENV_SEND_FROM_LEFT
Definition: unomid.h:138
OUString GetZip() const
sal_Int32 m_nWidth
Definition: envimg.hxx:49
sal_Int32 m_nAddrFromLeft
Definition: envimg.hxx:45
#define NEXTLINE
Definition: envimg.cxx:40
SwEnvItem()
Definition: envimg.cxx:94
PAPER_ENV_C65
OUString GetCountry() const
#define MID_ENV_HEIGHT
Definition: unomid.h:141
SwEnvItem m_aEnvItem
Definition: envimg.hxx:72
sal_Int32 m_nSendFromLeft
Definition: envimg.hxx:47
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: envimg.cxx:280
static css::uno::Sequence< OUString > GetPropertyNames()
Definition: envimg.cxx:252
#define MID_ENV_PRINT_FROM_ABOVE
Definition: unomid.h:143
sal_Int32 m_nAddrFromTop
Definition: envimg.hxx:46
virtual void ImplCommit() override
Definition: envimg.cxx:222
OUString GetState() const
bool EnableNotification(const css::uno::Sequence< OUString > &rNames, bool bEnableInternalNotification=false)
#define MID_ENV_SHIFT_RIGHT
Definition: unomid.h:144
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: envimg.cxx:305
bool m_bSend
Definition: envimg.hxx:43
#define SW_MOD()
Definition: swmodule.hxx:255
static SfxPoolItem * CreateDefault()
Definition: envimg.cxx:47
bool PutProperties(const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues)
int i
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: envimg.cxx:250
#define MID_ENV_ADDR_TEXT
Definition: unomid.h:133
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
OUString SwResId(const char *pId)
Definition: swmodule.cxx:191
OUString MakeSender()
Definition: envimg.cxx:49
SwEnvItem & operator=(const SwEnvItem &rItem)
Definition: envimg.cxx:113
sal_Int32 m_nShiftDown
Definition: envimg.hxx:54
#define MID_ENV_ALIGN
Definition: unomid.h:142
#define MID_ENV_ADDR_FROM_TOP
Definition: unomid.h:137
OUString GetFirstName() const
virtual bool operator==(const SfxPoolItem &rItem) const override
Definition: envimg.cxx:131
virtual SfxPoolItem * Clone(SfxItemPool *=nullptr) const override
Definition: envimg.cxx:150
#define MID_ENV_SHIFT_DOWN
Definition: unomid.h:145
OUString GetLastName() const
#define MID_ENV_SEND
Definition: unomid.h:134
#define MID_ENV_ADDR_FROM_LEFT
Definition: unomid.h:136
unsigned char sal_uInt8
OUString m_aAddrText
Definition: envimg.hxx:42
#define MID_ENV_SEND_FROM_TOP
Definition: unomid.h:139
OUString GetCompany() const
SwEnvAlign
Definition: envimg.hxx:28
constexpr sal_Int64 convertTwipToMm100(sal_Int64 n)
OUString GetStreet() const
sal_Int32 m_nSendFromTop
Definition: envimg.hxx:48
virtual ~SwEnvCfgItem() override
Definition: envimg.cxx:218
constexpr sal_Int64 convertMm100ToTwip(sal_Int64 n)
sal_Int32 m_nShiftRight
Definition: envimg.hxx:53
#define CONVERT_TWIPS