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 <o3tl/string_view.hxx>
22 #include <osl/diagnose.h>
23 #include <editeng/paperinf.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <tools/UnitConversion.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 constexpr OUStringLiteral NEXTLINE = u"\r\n";
39 #else
40 constexpr OUStringLiteral NEXTLINE = u"\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  std::u16string_view sToken = o3tl::getToken(sSenderToken, 0, ';', nSttPos );
62  if (sToken == u"COMPANY")
63  {
64  sal_Int32 nOldLen = sRet.getLength();
65  sRet.append(rUserOpt.GetCompany());
66  bLastLength = sRet.getLength() != nOldLen;
67  }
68  else if (sToken == u"CR")
69  {
70  if(bLastLength)
71  sRet.append(NEXTLINE);
72  bLastLength = true;
73  }
74  else if (sToken == u"FIRSTNAME")
75  sRet.append(rUserOpt.GetFirstName());
76  else if (sToken == u"LASTNAME")
77  sRet.append(rUserOpt.GetLastName());
78  else if (sToken == u"ADDRESS")
79  sRet.append(rUserOpt.GetStreet());
80  else if (sToken == u"COUNTRY")
81  sRet.append(rUserOpt.GetCountry());
82  else if (sToken == u"POSTALCODE")
83  sRet.append(rUserOpt.GetZip());
84  else if (sToken == u"CITY")
85  sRet.append(rUserOpt.GetCity());
86  else if (sToken == u"STATEPROV")
87  sRet.append(rUserOpt.GetState());
88  else if (!sToken.empty()) //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;
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  assert(SfxPoolItem::operator==(rItem));
134  const SwEnvItem& rEnv = static_cast<const SwEnvItem&>( rItem);
135 
136  return m_aAddrText == rEnv.m_aAddrText &&
137  m_bSend == rEnv.m_bSend &&
138  m_aSendText == rEnv.m_aSendText &&
140  m_nSendFromTop == rEnv.m_nSendFromTop &&
142  m_nAddrFromTop == rEnv.m_nAddrFromTop &&
143  m_nWidth == rEnv.m_nWidth &&
144  m_nHeight == rEnv.m_nHeight &&
145  m_eAlign == rEnv.m_eAlign &&
147  m_nShiftRight == rEnv.m_nShiftRight &&
148  m_nShiftDown == rEnv.m_nShiftDown;
149 }
150 
152 {
153  return new SwEnvItem(*this);
154 }
155 
157  ConfigItem("Office.Writer/Envelope")
158 {
160  Sequence<Any> aValues = GetProperties(aNames);
161  EnableNotification(aNames);
162  const Any* pValues = aValues.getConstArray();
163  OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
164  if(aValues.getLength() != aNames.getLength())
165  return;
166 
167  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
168  {
169  if(pValues[nProp].hasValue())
170  {
171  switch(nProp)
172  {
173  case 0: pValues[nProp] >>= m_aEnvItem.m_aAddrText; break;// "Inscription/Addressee",
174  case 1: pValues[nProp] >>= m_aEnvItem.m_aSendText; break;// "Inscription/Sender",
175  case 2: m_aEnvItem.m_bSend = *o3tl::doAccess<bool>(pValues[nProp]); break;// "Inscription/UseSender",
176  case 3:
177  pValues[nProp] >>= m_aEnvItem.m_nAddrFromLeft;// "Format/AddresseeFromLeft",
179  break;
180  case 4:
181  pValues[nProp] >>= m_aEnvItem.m_nAddrFromTop; // "Format/AddresseeFromTop",
183  break;
184  case 5:
185  pValues[nProp] >>= m_aEnvItem.m_nSendFromLeft; // "Format/SenderFromLeft",
187  break;
188  case 6:
189  pValues[nProp] >>= m_aEnvItem.m_nSendFromTop;// "Format/SenderFromTop",
191  break;
192  case 7:
193  pValues[nProp] >>= m_aEnvItem.m_nWidth; // "Format/Width",
195  break;
196  case 8:
197  pValues[nProp] >>= m_aEnvItem.m_nHeight; // "Format/Height",
199  break;
200  case 9:
201  {
202  sal_Int32 nTemp = 0;
203  pValues[nProp] >>= nTemp; m_aEnvItem.m_eAlign = static_cast<SwEnvAlign>(nTemp); break;// "Print/Alignment",
204  }
205  case 10: m_aEnvItem.m_bPrintFromAbove = *o3tl::doAccess<bool>(pValues[nProp]); break;// "Print/FromAbove",
206  case 11:
207  pValues[nProp] >>= m_aEnvItem.m_nShiftRight;
209  break;
210  case 12:
211  pValues[nProp] >>= m_aEnvItem.m_nShiftDown;
213  break;// "Print/Down"
214  }
215  }
216  }
217 }
218 
220 {
221 }
222 
224 {
226  Sequence<Any> aValues(aNames.getLength());
227  Any* pValues = aValues.getArray();
228 
229  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
230  {
231  switch(nProp)
232  {
233  case 0: pValues[nProp] <<= m_aEnvItem.m_aAddrText; break;// "Inscription/Addressee",
234  case 1: pValues[nProp] <<= m_aEnvItem.m_aSendText; break;// "Inscription/Sender",
235  case 2: pValues[nProp] <<= m_aEnvItem.m_bSend; break;// "Inscription/UseSender",
236  case 3: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromLeft)) ; break;// "Format/AddresseeFromLeft",
237  case 4: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nAddrFromTop)) ; break;// "Format/AddresseeFromTop",
238  case 5: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromLeft)) ; break;// "Format/SenderFromLeft",
239  case 6: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nSendFromTop)) ; break;// "Format/SenderFromTop",
240  case 7: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nWidth)) ; break;// "Format/Width",
241  case 8: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nHeight)) ; break;// "Format/Height",
242  case 9: pValues[nProp] <<= sal_Int32(m_aEnvItem.m_eAlign); break;// "Print/Alignment",
243  case 10: pValues[nProp] <<= m_aEnvItem.m_bPrintFromAbove; break;// "Print/FromAbove",
244  case 11: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftRight));break; // "Print/Right",
245  case 12: pValues[nProp] <<= static_cast <sal_Int32>(convertTwipToMm100(m_aEnvItem.m_nShiftDown)); break;// "Print/Down"
246  }
247  }
248  PutProperties(aNames, aValues);
249 }
250 
251 void SwEnvCfgItem::Notify( const css::uno::Sequence< OUString >& ) {}
252 
254 {
255  static const char* aPropNames[] =
256  {
257  "Inscription/Addressee", // 0
258  "Inscription/Sender", // 1
259  "Inscription/UseSender", // 2
260  "Format/AddresseeFromLeft", // 3
261  "Format/AddresseeFromTop", // 4
262  "Format/SenderFromLeft", // 5
263  "Format/SenderFromTop", // 6
264  "Format/Width", // 7
265  "Format/Height", // 8
266  "Print/Alignment", // 9
267  "Print/FromAbove", // 10
268  "Print/Right", // 11
269  "Print/Down" // 12
270  };
271  const int nCount = 13;
272  Sequence<OUString> aNames(nCount);
273  OUString* pNames = aNames.getArray();
274 
275  for(int i = 0; i < nCount; i++)
276  pNames[i] = OUString::createFromAscii(aPropNames[i]);
277 
278  return aNames;
279 }
280 
281 bool SwEnvItem::QueryValue( Any& rVal, sal_uInt8 nMemberId ) const
282 {
283  bool bRet = true;
284  switch(nMemberId & ~CONVERT_TWIPS)
285  {
286  case MID_ENV_ADDR_TEXT : rVal <<= m_aAddrText; break;
287  case MID_ENV_SEND : rVal <<= m_bSend; break;
288  case MID_SEND_TEXT : rVal <<= m_aSendText; break;
289  case MID_ENV_ADDR_FROM_LEFT : rVal <<= m_nAddrFromLeft; break;
290  case MID_ENV_ADDR_FROM_TOP : rVal <<= m_nAddrFromTop; break;
291  case MID_ENV_SEND_FROM_LEFT : rVal <<= m_nSendFromLeft; break;
292  case MID_ENV_SEND_FROM_TOP : rVal <<= m_nSendFromTop; break;
293  case MID_ENV_WIDTH : rVal <<= m_nWidth; break;
294  case MID_ENV_HEIGHT : rVal <<= m_nHeight; break;
295  case MID_ENV_ALIGN : rVal <<= static_cast<sal_Int16>(m_eAlign); break;
296  case MID_ENV_PRINT_FROM_ABOVE : rVal <<= m_bPrintFromAbove; break;
297  case MID_ENV_SHIFT_RIGHT : rVal <<= m_nShiftRight; break;
298  case MID_ENV_SHIFT_DOWN : rVal <<= m_nShiftDown; break;
299  default:
300  OSL_FAIL("Wrong memberId");
301  bRet = false;
302  }
303  return bRet;
304 }
305 
306 bool SwEnvItem::PutValue(const Any& rVal, sal_uInt8 nMemberId)
307 {
308  bool bRet = false;
309  switch(nMemberId & ~CONVERT_TWIPS)
310  {
311  case MID_ENV_ADDR_TEXT : bRet = (rVal >>= m_aAddrText); break;
312  case MID_ENV_SEND : bRet = (rVal >>= m_bSend); break;
313  case MID_SEND_TEXT : bRet = (rVal >>= m_aSendText); break;
314  case MID_ENV_ADDR_FROM_LEFT : bRet = (rVal >>= m_nAddrFromLeft); break;
315  case MID_ENV_ADDR_FROM_TOP : bRet = (rVal >>= m_nAddrFromTop); break;
316  case MID_ENV_SEND_FROM_LEFT : bRet = (rVal >>= m_nSendFromLeft); break;
317  case MID_ENV_SEND_FROM_TOP : bRet = (rVal >>= m_nSendFromTop); break;
318  case MID_ENV_WIDTH : bRet = (rVal >>= m_nWidth); break;
319  case MID_ENV_HEIGHT : bRet = (rVal >>= m_nHeight); break;
320  case MID_ENV_ALIGN :
321  {
322  sal_Int16 nTemp = 0;
323  bRet = (rVal >>= nTemp);
324  if (bRet)
325  m_eAlign = SwEnvAlign(nTemp);
326  break;
327  }
328  case MID_ENV_PRINT_FROM_ABOVE : bRet = (rVal >>= m_bPrintFromAbove); break;
329  case MID_ENV_SHIFT_RIGHT : bRet = (rVal >>= m_nShiftRight); break;
330  case MID_ENV_SHIFT_DOWN : bRet = (rVal >>= m_nShiftDown); break;
331  default:
332  OSL_FAIL("Wrong memberId");
333  }
334  return bRet;
335 }
336 
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetCity() const
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
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
constexpr auto toTwips(N number, Length from)
#define FN_ENVELOP
Definition: cmdid.h:761
#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
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
SwEnvItem()
Definition: envimg.cxx:94
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:281
constexpr auto convertTwipToMm100(N n)
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:164
constexpr tools::Long Width() const
static css::uno::Sequence< OUString > GetPropertyNames()
Definition: envimg.cxx:253
#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:223
OUString GetState() const
bool EnableNotification(const css::uno::Sequence< OUString > &rNames, bool bEnableInternalNotification=false)
constexpr OUStringLiteral NEXTLINE
Definition: envimg.cxx:40
#define MID_ENV_SHIFT_RIGHT
Definition: unomid.h:144
static bool PutProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues, bool bAllLocales)
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: envimg.cxx:306
bool m_bSend
Definition: envimg.hxx:43
static css::uno::Sequence< css::uno::Any > GetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, bool bAllLocales)
#define SW_MOD()
Definition: swmodule.hxx:255
static SfxPoolItem * CreateDefault()
Definition: envimg.cxx:47
int i
virtual SwEnvItem * Clone(SfxItemPool *=nullptr) const override
Definition: envimg.cxx:151
float u
const PropertyStruct aPropNames[]
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: envimg.cxx:251
#define MID_ENV_ADDR_TEXT
Definition: unomid.h:133
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
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
const PropertyValue * pValues
virtual bool operator==(const SfxPoolItem &rItem) const override
Definition: envimg.cxx:131
#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
constexpr tools::Long Height() const
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
PAPER_ENV_C65
OUString GetStreet() const
sal_Int32 m_nSendFromTop
Definition: envimg.hxx:48
virtual ~SwEnvCfgItem() override
Definition: envimg.cxx:219
sal_Int32 m_nShiftRight
Definition: envimg.hxx:53
#define CONVERT_TWIPS