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 <o3tl/any.hxx>
21 #include <osl/diagnose.h>
22 #include <editeng/paperinf.hxx>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <tools/UnitConversion.hxx>
26 #include <unotools/useroptions.hxx>
27 #include <swmodule.hxx>
28 #include <swtypes.hxx>
29 #include <envimg.hxx>
30 #include <strings.hrc>
31 
32 #include <cmdid.h>
33 
34 #include <unomid.h>
35 
36 #ifdef _WIN32
37 #define NEXTLINE "\r\n"
38 #else
39 #define NEXTLINE "\n"
40 #endif
41 
42 using namespace utl;
43 using namespace ::com::sun::star::uno;
44 
45 
47 
48 OUString MakeSender()
49 {
50  SvtUserOptions& rUserOpt = SW_MOD()->GetUserOptions();
51 
52  const OUString sSenderToken(SwResId(STR_SENDER_TOKENS));
53  if (sSenderToken.isEmpty())
54  return OUString();
55 
56  OUStringBuffer sRet;
57  sal_Int32 nSttPos = 0;
58  bool bLastLength = true;
59  do {
60  OUString sToken = sSenderToken.getToken( 0, ';', nSttPos );
61  if (sToken == "COMPANY")
62  {
63  sal_Int32 nOldLen = sRet.getLength();
64  sRet.append(rUserOpt.GetCompany());
65  bLastLength = sRet.getLength() != nOldLen;
66  }
67  else if (sToken == "CR")
68  {
69  if(bLastLength)
70  sRet.append(NEXTLINE);
71  bLastLength = true;
72  }
73  else if (sToken == "FIRSTNAME")
74  sRet.append(rUserOpt.GetFirstName());
75  else if (sToken == "LASTNAME")
76  sRet.append(rUserOpt.GetLastName());
77  else if (sToken == "ADDRESS")
78  sRet.append(rUserOpt.GetStreet());
79  else if (sToken == "COUNTRY")
80  sRet.append(rUserOpt.GetCountry());
81  else if (sToken == "POSTALCODE")
82  sRet.append(rUserOpt.GetZip());
83  else if (sToken == "CITY")
84  sRet.append(rUserOpt.GetCity());
85  else if (sToken == "STATEPROV")
86  sRet.append(rUserOpt.GetState());
87  else if (!sToken.isEmpty()) //spaces
88  sRet.append(sToken);
89  } while (nSttPos>=0);
90  return sRet.makeStringAndClear();
91 }
92 
95 {
96  m_bSend = true;
98  m_nSendFromLeft = 566; // 1 cm
99  m_nSendFromTop = 566; // 1 cm
101  m_nWidth = aEnvSz.Width();
102  m_nHeight = aEnvSz.Height();
104  m_bPrintFromAbove = true;
105  m_nShiftRight = 0;
106  m_nShiftDown = 0;
107 
108  m_nAddrFromLeft = std::max(m_nWidth, m_nHeight) / 2;
109  m_nAddrFromTop = std::min(m_nWidth, m_nHeight) / 2;
110 }
111 
113 {
114  m_aAddrText = rItem.m_aAddrText;
115  m_bSend = rItem.m_bSend;
116  m_aSendText = rItem.m_aSendText;
121  m_nWidth = rItem.m_nWidth;
122  m_nHeight = rItem.m_nHeight;
123  m_eAlign = rItem.m_eAlign;
126  m_nShiftDown = rItem.m_nShiftDown;
127  return *this;
128 }
129 
130 bool SwEnvItem::operator ==(const SfxPoolItem& rItem) const
131 {
132  const SwEnvItem& rEnv = static_cast<const SwEnvItem&>( rItem);
133 
134  return m_aAddrText == rEnv.m_aAddrText &&
135  m_bSend == rEnv.m_bSend &&
136  m_aSendText == rEnv.m_aSendText &&
138  m_nSendFromTop == rEnv.m_nSendFromTop &&
140  m_nAddrFromTop == rEnv.m_nAddrFromTop &&
141  m_nWidth == rEnv.m_nWidth &&
142  m_nHeight == rEnv.m_nHeight &&
143  m_eAlign == rEnv.m_eAlign &&
145  m_nShiftRight == rEnv.m_nShiftRight &&
146  m_nShiftDown == rEnv.m_nShiftDown;
147 }
148 
150 {
151  return new SwEnvItem(*this);
152 }
153 
155  ConfigItem("Office.Writer/Envelope")
156 {
158  Sequence<Any> aValues = GetProperties(aNames);
159  EnableNotification(aNames);
160  const Any* pValues = aValues.getConstArray();
161  OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
162  if(aValues.getLength() != aNames.getLength())
163  return;
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 
218 {
219 }
220 
222 {
224  Sequence<Any> aValues(aNames.getLength());
225  Any* pValues = aValues.getArray();
226 
227  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
228  {
229  switch(nProp)
230  {
231  case 0: pValues[nProp] <<= m_aEnvItem.m_aAddrText; break;// "Inscription/Addressee",
232  case 1: pValues[nProp] <<= m_aEnvItem.m_aSendText; break;// "Inscription/Sender",
233  case 2: pValues[nProp] <<= m_aEnvItem.m_bSend; break;// "Inscription/UseSender",
234  case 3: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromLeft)) ; break;// "Format/AddresseeFromLeft",
235  case 4: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromTop)) ; break;// "Format/AddresseeFromTop",
236  case 5: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromLeft)) ; break;// "Format/SenderFromLeft",
237  case 6: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromTop)) ; break;// "Format/SenderFromTop",
238  case 7: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nWidth)) ; break;// "Format/Width",
239  case 8: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nHeight)) ; break;// "Format/Height",
240  case 9: pValues[nProp] <<= sal_Int32(m_aEnvItem.m_eAlign); break;// "Print/Alignment",
241  case 10: pValues[nProp] <<= m_aEnvItem.m_bPrintFromAbove; break;// "Print/FromAbove",
242  case 11: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftRight));break; // "Print/Right",
243  case 12: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftDown)); break;// "Print/Down"
244  }
245  }
246  PutProperties(aNames, aValues);
247 }
248 
249 void SwEnvCfgItem::Notify( const css::uno::Sequence< OUString >& ) {}
250 
252 {
253  static const char* aPropNames[] =
254  {
255  "Inscription/Addressee", // 0
256  "Inscription/Sender", // 1
257  "Inscription/UseSender", // 2
258  "Format/AddresseeFromLeft", // 3
259  "Format/AddresseeFromTop", // 4
260  "Format/SenderFromLeft", // 5
261  "Format/SenderFromTop", // 6
262  "Format/Width", // 7
263  "Format/Height", // 8
264  "Print/Alignment", // 9
265  "Print/FromAbove", // 10
266  "Print/Right", // 11
267  "Print/Down" // 12
268  };
269  const int nCount = 13;
270  Sequence<OUString> aNames(nCount);
271  OUString* pNames = aNames.getArray();
272 
273  for(int i = 0; i < nCount; i++)
274  pNames[i] = OUString::createFromAscii(aPropNames[i]);
275 
276  return aNames;
277 }
278 
279 bool SwEnvItem::QueryValue( Any& rVal, sal_uInt8 nMemberId ) const
280 {
281  bool bRet = true;
282  switch(nMemberId & ~CONVERT_TWIPS)
283  {
284  case MID_ENV_ADDR_TEXT : rVal <<= m_aAddrText; break;
285  case MID_ENV_SEND : rVal <<= m_bSend; break;
286  case MID_SEND_TEXT : rVal <<= m_aSendText; break;
287  case MID_ENV_ADDR_FROM_LEFT : rVal <<= m_nAddrFromLeft; break;
288  case MID_ENV_ADDR_FROM_TOP : rVal <<= m_nAddrFromTop; break;
289  case MID_ENV_SEND_FROM_LEFT : rVal <<= m_nSendFromLeft; break;
290  case MID_ENV_SEND_FROM_TOP : rVal <<= m_nSendFromTop; break;
291  case MID_ENV_WIDTH : rVal <<= m_nWidth; break;
292  case MID_ENV_HEIGHT : rVal <<= m_nHeight; break;
293  case MID_ENV_ALIGN : rVal <<= static_cast<sal_Int16>(m_eAlign); break;
294  case MID_ENV_PRINT_FROM_ABOVE : rVal <<= m_bPrintFromAbove; break;
295  case MID_ENV_SHIFT_RIGHT : rVal <<= m_nShiftRight; break;
296  case MID_ENV_SHIFT_DOWN : rVal <<= m_nShiftDown; break;
297  default:
298  OSL_FAIL("Wrong memberId");
299  bRet = false;
300  }
301  return bRet;
302 }
303 
304 bool SwEnvItem::PutValue(const Any& rVal, sal_uInt8 nMemberId)
305 {
306  bool bRet = false;
307  switch(nMemberId & ~CONVERT_TWIPS)
308  {
309  case MID_ENV_ADDR_TEXT : bRet = (rVal >>= m_aAddrText); break;
310  case MID_ENV_SEND : bRet = (rVal >>= m_bSend); break;
311  case MID_SEND_TEXT : bRet = (rVal >>= m_aSendText); break;
312  case MID_ENV_ADDR_FROM_LEFT : bRet = (rVal >>= m_nAddrFromLeft); break;
313  case MID_ENV_ADDR_FROM_TOP : bRet = (rVal >>= m_nAddrFromTop); break;
314  case MID_ENV_SEND_FROM_LEFT : bRet = (rVal >>= m_nSendFromLeft); break;
315  case MID_ENV_SEND_FROM_TOP : bRet = (rVal >>= m_nSendFromTop); break;
316  case MID_ENV_WIDTH : bRet = (rVal >>= m_nWidth); break;
317  case MID_ENV_HEIGHT : bRet = (rVal >>= m_nHeight); break;
318  case MID_ENV_ALIGN :
319  {
320  sal_Int16 nTemp = 0;
321  bRet = (rVal >>= nTemp);
322  if (bRet)
323  m_eAlign = SwEnvAlign(nTemp);
324  break;
325  }
326  case MID_ENV_PRINT_FROM_ABOVE : bRet = (rVal >>= m_bPrintFromAbove); break;
327  case MID_ENV_SHIFT_RIGHT : bRet = (rVal >>= m_nShiftRight); break;
328  case MID_ENV_SHIFT_DOWN : bRet = (rVal >>= m_nShiftDown); break;
329  default:
330  OSL_FAIL("Wrong memberId");
331  }
332  return bRet;
333 }
334 
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetCity() 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:744
#define MID_SEND_TEXT
Definition: unomid.h:135
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:39
SwEnvItem()
Definition: envimg.cxx:93
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:279
static css::uno::Sequence< OUString > GetPropertyNames()
Definition: envimg.cxx:251
#define MID_ENV_PRINT_FROM_ABOVE
Definition: unomid.h:143
sal_Int32 m_nAddrFromTop
Definition: envimg.hxx:46
int nCount
virtual void ImplCommit() override
Definition: envimg.cxx:221
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:304
bool m_bSend
Definition: envimg.hxx:43
#define SW_MOD()
Definition: swmodule.hxx:255
static SfxPoolItem * CreateDefault()
Definition: envimg.cxx:46
int i
bool PutProperties(const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues)
virtual SwEnvItem * Clone(SfxItemPool *=nullptr) const override
Definition: envimg.cxx:149
tools::Long Width() const
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
const PropertyStruct aPropNames[]
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: envimg.cxx:249
#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:165
OUString MakeSender()
Definition: envimg.cxx:48
SwEnvItem & operator=(const SwEnvItem &rItem)
Definition: envimg.cxx:112
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
const PropertyValue * pValues
constexpr sal_Int64 convertTwipToMm100(sal_Int64 n)
virtual bool operator==(const SfxPoolItem &rItem) const override
Definition: envimg.cxx:130
#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
tools::Long Height() const
OUString GetCompany() const
constexpr sal_Int64 convertMm100ToTwip(sal_Int64 n)
SwEnvAlign
Definition: envimg.hxx:28
PAPER_ENV_C65
OUString GetStreet() const
sal_Int32 m_nSendFromTop
Definition: envimg.hxx:48
virtual ~SwEnvCfgItem() override
Definition: envimg.cxx:217
sal_Int32 m_nShiftRight
Definition: envimg.hxx:53
#define CONVERT_TWIPS