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