LibreOffice Module sw (master)  1
mmlayoutpage.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 <swtypes.hxx>
21 #include "mmlayoutpage.hxx"
22 #include <mailmergewizard.hxx>
23 #include <mmconfigitem.hxx>
24 #include <mailmergehelper.hxx>
25 #include <unotools.hxx>
26 #include <comphelper/string.hxx>
27 #include <i18nutil/unicode.hxx>
28 #include <unotools/tempfile.hxx>
29 #include <uitool.hxx>
30 #include <svx/dlgutil.hxx>
31 #include <view.hxx>
32 #include <swundo.hxx>
33 #include <sfx2/dispatch.hxx>
34 #include <svl/stritem.hxx>
35 #include <sfx2/docfilt.hxx>
36 #include <svtools/unitconv.hxx>
37 #include <com/sun/star/text/XParagraphCursor.hpp>
38 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
39 #include <com/sun/star/view/DocumentZoomType.hpp>
40 #include <fldmgr.hxx>
41 #include <fldbas.hxx>
42 #include <poolfmt.hxx>
43 #include <unotxdoc.hxx>
44 #include <docsh.hxx>
45 #include <doc.hxx>
46 #include <wrtsh.hxx>
47 #include <fmtsrnd.hxx>
48 #include <pagedesc.hxx>
49 #include <fmtanchr.hxx>
50 #include <fmtornt.hxx>
51 #include <fmtfsize.hxx>
52 #include <editeng/boxitem.hxx>
53 #include <svl/urihelper.hxx>
54 #include <shellio.hxx>
55 #include <osl/file.hxx>
56 #include <vcl/settings.hxx>
57 #include <unoprnms.hxx>
58 #include <iodetect.hxx>
59 
60 #include <dbui.hrc>
61 #include <unomid.h>
62 
63 using namespace osl;
64 using namespace svt;
65 using namespace ::com::sun::star;
66 using namespace ::com::sun::star::uno;
67 using namespace ::com::sun::star::text;
68 using namespace ::com::sun::star::frame;
69 using namespace ::com::sun::star::lang;
70 using namespace ::com::sun::star::view;
71 
72 #define DEFAULT_LEFT_DISTANCE (MM50*5) // 2,5 cm
73 #define DEFAULT_TOP_DISTANCE (MM50*11) // 5,5 cm
74 #define GREETING_TOP_DISTANCE (MM50*25) //12,5 cm
75 #define DEFAULT_ADDRESS_WIDTH (MM50*15)// 7,5 cm
76 #define DEFAULT_ADDRESS_HEIGHT (MM50*7) // 3,5cm
77 
79  : vcl::OWizardPage(pPage, pWizard, "modules/swriter/ui/mmlayoutpage.ui", "MMLayoutPage")
80  , m_pExampleWrtShell(nullptr)
81  , m_pAddressBlockFormat(nullptr)
82  , m_bIsGreetingInserted(false)
83  , m_pWizard(pWizard)
84  , m_xPosition(m_xBuilder->weld_container("addresspos"))
85  , m_xAlignToBodyCB(m_xBuilder->weld_check_button("align"))
86  , m_xLeftFT(m_xBuilder->weld_label("leftft"))
87  , m_xLeftMF(m_xBuilder->weld_metric_spin_button("left", FieldUnit::CM))
88  , m_xTopMF(m_xBuilder->weld_metric_spin_button("top", FieldUnit::CM))
89  , m_xGreetingLine(m_xBuilder->weld_container("greetingspos"))
90  , m_xUpPB(m_xBuilder->weld_button("up"))
91  , m_xDownPB(m_xBuilder->weld_button("down"))
92  , m_xZoomLB(m_xBuilder->weld_combo_box("zoom"))
93 {
94  std::shared_ptr<const SfxFilter> pSfxFlt = SwIoSystem::GetFilterOfFormat(
95  FILTER_XML,
96  SwDocShell::Factory().GetFilterContainer() );
97  //save the current document into a temporary file
98  {
99  //temp file needs its own block
100  //creating with extension is not supported by a static method :-(
101  OUString const sExt(
102  comphelper::string::stripStart(pSfxFlt->GetDefaultExtension(),'*'));
103  utl::TempFile aTempFile( OUString(), true, &sExt );
104  m_sExampleURL = aTempFile.GetURL();
105  aTempFile.EnableKillingFile();
106  }
107  SwView* pView = m_pWizard->GetSwView();
108  uno::Sequence< beans::PropertyValue > aValues(2);
109  beans::PropertyValue* pValues = aValues.getArray();
110  pValues[0].Name = "FilterName";
111  pValues[0].Value <<= pSfxFlt->GetFilterName();
112  // Don't save embedded data set! It would steal it from current document.
113  pValues[1].Name = "NoEmbDataSet";
114  pValues[1].Value <<= true;
115 
116  uno::Reference< frame::XStorable > xStore( pView->GetDocShell()->GetModel(), uno::UNO_QUERY);
117  xStore->storeToURL( m_sExampleURL, aValues );
118 
119  Link<SwOneExampleFrame&,void> aLink(LINK(this, SwMailMergeLayoutPage, PreviewLoadedHdl_Impl));
122 
123  Size aSize = m_xExampleFrame->GetDrawingArea()->get_ref_device().LogicToPixel(
124  Size(124, 159), MapMode(MapUnit::MapAppFont));
125  m_xExampleFrame->set_size_request(aSize.Width(), aSize.Height());
126 
127  m_xExampleContainerWIN->hide();
128 
129  m_xLeftMF->set_value(m_xLeftMF->normalize(DEFAULT_LEFT_DISTANCE), FieldUnit::TWIP);
130  m_xTopMF->set_value(m_xTopMF->normalize(DEFAULT_TOP_DISTANCE), FieldUnit::TWIP);
131 
133  m_xZoomLB->append_text(unicode::formatPercent(50, rLang));
134  m_xZoomLB->append_text(unicode::formatPercent(75, rLang));
135  m_xZoomLB->append_text(unicode::formatPercent(100, rLang));
136  m_xZoomLB->set_active(0); //page size
137  m_xZoomLB->connect_changed(LINK(this, SwMailMergeLayoutPage, ZoomHdl_Impl));
138 
139  Link<weld::MetricSpinButton&,void> aFrameHdl = LINK(this, SwMailMergeLayoutPage, ChangeAddressHdl_Impl);
140  m_xLeftMF->connect_value_changed(aFrameHdl);
141  m_xTopMF->connect_value_changed(aFrameHdl);
142 
143  FieldUnit eFieldUnit = ::GetDfltMetric(false);
144  ::SetFieldUnit( *m_xLeftMF, eFieldUnit );
145  ::SetFieldUnit( *m_xTopMF, eFieldUnit );
146 
147  Link<weld::Button&,void> aUpDownHdl = LINK(this, SwMailMergeLayoutPage, GreetingsHdl_Impl );
148  m_xUpPB->connect_clicked(aUpDownHdl);
149  m_xDownPB->connect_clicked(aUpDownHdl);
150  m_xAlignToBodyCB->connect_toggled(LINK(this, SwMailMergeLayoutPage, AlignToTextHdl_Impl));
151  m_xAlignToBodyCB->set_active(true);
152 }
153 
155 {
156  File::remove( m_sExampleURL );
157 }
158 
160 {
162  bool bGreetingLine = rConfigItem.IsGreetingLine(false) && !rConfigItem.IsGreetingInserted();
163  bool bAddressBlock = rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted();
164 
165  m_xPosition->set_sensitive(bAddressBlock);
166  AlignToTextHdl_Impl(*m_xAlignToBodyCB);
167 
168  m_xGreetingLine->set_sensitive(bGreetingLine);
169 
170  //check if greeting and/or address frame have to be inserted/removed
171  if(m_pExampleWrtShell) // initially there's nothing to check
172  {
173  if(!rConfigItem.IsGreetingInserted() &&
174  m_bIsGreetingInserted != bGreetingLine )
175  {
177  {
179  m_bIsGreetingInserted = false;
180  }
181  else
182  {
184  m_bIsGreetingInserted = true;
185  }
186  }
187  if(!rConfigItem.IsAddressInserted() &&
188  rConfigItem.IsAddressBlock() != ( nullptr != m_pAddressBlockFormat ))
189  {
191  {
195  m_pAddressBlockFormat = nullptr;
197  }
198  else
199  {
200  long nLeft = static_cast< long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
201  long nTop = static_cast< long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
204  Point(nLeft, nTop),
205  m_xAlignToBodyCB->get_active(), true);
206  }
207  }
208  m_xExampleFrame->Invalidate();
209  }
210 }
211 
213 {
214  //now insert the frame and the greeting
216  if (eReason == ::vcl::WizardTypes::eTravelForward || eReason == ::vcl::WizardTypes::eFinish)
217  {
218  long nLeft = static_cast< long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
219  long nTop = static_cast< long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
221  m_pWizard->GetSwView(),
222  rConfigItem,
223  Point(nLeft, nTop),
224  m_xAlignToBodyCB->get_active());
225  }
226  return true;
227 }
228 
230  SwMailMergeConfigItem& rConfigItem,
231  const Point& rAddressPosition,
232  bool bAlignToBody)
233 {
234  SwFrameFormat* pAddressBlockFormat = nullptr;
236  if(rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted())
237  {
238  //insert the frame
240  if(rAddressPosition.X() > 0 && rAddressPosition.Y() > 0)
241  aAddressPosition = rAddressPosition;
242  pAddressBlockFormat = InsertAddressFrame( pView->GetWrtShell(),
243  rConfigItem,
244  aAddressPosition, bAlignToBody, false);
245  rConfigItem.SetAddressInserted();
246  }
247  //now the greeting
248  if(rConfigItem.IsGreetingLine(false) && !rConfigItem.IsGreetingInserted())
249  {
250  InsertGreeting( pView->GetWrtShell(), rConfigItem, false);
251  rConfigItem.SetGreetingInserted();
252  }
254  return pAddressBlockFormat;
255 }
256 
258  SwWrtShell& rShell,
259  SwMailMergeConfigItem const & rConfigItem,
260  const Point& rDestination,
261  bool bAlignLeft,
262  bool bExample)
263 {
264  // insert the address block and the greeting line
265  SfxItemSet aSet(
266  rShell.GetAttrPool(),
267  svl::Items<
270  RES_BOX, RES_BOX>{} );
271  aSet.Put(SwFormatAnchor(RndStdIds::FLY_AT_PAGE, 1));
272  if(bAlignLeft)
273  aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
274  else
275  aSet.Put(SwFormatHoriOrient( rDestination.X(), text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
276  aSet.Put(SwFormatVertOrient( rDestination.Y(), text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
278  // the example gets a border around the frame, the real document doesn't get one
279  if(!bExample)
280  aSet.Put(SvxBoxItem( RES_BOX ));
281  aSet.Put(SwFormatSurround( css::text::WrapTextMode_NONE ));
282 
283  rShell.NewFlyFrame(aSet, true );
284  SwFrameFormat* pRet = rShell.GetFlyFrameFormat();
285  OSL_ENSURE( pRet, "Fly not inserted" );
286 
287  rShell.UnSelectFrame();
288  const Sequence< OUString> aBlocks = rConfigItem.GetAddressBlocks();
289  if(bExample)
290  {
291  rShell.Insert(aBlocks[0]);
292  }
293  else
294  {
295  //the placeholders should be replaced by the appropriate fields
296  SwFieldMgr aFieldMgr(&rShell);
297  //create a database string source.command.commandtype.column
298  const SwDBData& rData = rConfigItem.GetCurrentDBData();
299  OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
300  + rData.sCommand + OUStringChar(DB_DELIM));
301  const OUString sDatabaseConditionPrefix(sDBName.replace(DB_DELIM, '.'));
302  sDBName += OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM);
303 
304  // if only the country is in an address line the
305  // paragraph has to be hidden depending on the
306  // IsIncludeCountry()/GetExcludeCountry() settings
307 
308  bool bIncludeCountry = rConfigItem.IsIncludeCountry();
309  bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
310  const OUString rExcludeCountry = rConfigItem.GetExcludeCountry();
311  bool bSpecialReplacementForCountry = (!bIncludeCountry || !rExcludeCountry.isEmpty());
312 
313  const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
314  Sequence< OUString> aAssignment =
315  rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
316  const OUString* pAssignment = aAssignment.getConstArray();
317  const OUString sCountryColumn(
318  (aAssignment.getLength() > MM_PART_COUNTRY && !aAssignment[MM_PART_COUNTRY].isEmpty())
319  ? aAssignment[MM_PART_COUNTRY]
320  : rHeaders[MM_PART_COUNTRY].first);
321 
322  OUString sHideParagraphsExpression;
323  SwAddressIterator aIter(aBlocks[0]);
324  while(aIter.HasMore())
325  {
326  SwMergeAddressItem aItem = aIter.Next();
327  if(aItem.bIsColumn)
328  {
329  OUString sConvertedColumn = aItem.sText;
330  auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
331  static_cast<sal_uInt32>(aAssignment.getLength()));
332  for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
333  {
334  if (rHeaders[nColumn].first == aItem.sText &&
335  !pAssignment[nColumn].isEmpty())
336  {
337  sConvertedColumn = pAssignment[nColumn];
338  break;
339  }
340  }
341  const OUString sDB(sDBName + sConvertedColumn);
342 
343  if(!sHideParagraphsExpression.isEmpty())
344  sHideParagraphsExpression += " AND ";
345  sHideParagraphsExpression += "![" + sDatabaseConditionPrefix + sConvertedColumn + "]";
346 
347  if( bSpecialReplacementForCountry && sCountryColumn == sConvertedColumn )
348  {
349  // now insert a hidden paragraph field
350  if( !rExcludeCountry.isEmpty() )
351  {
352  const OUString sExpression("[" + sDatabaseConditionPrefix + sCountryColumn + "]");
354  sExpression + " != \"" + rExcludeCountry + "\"",
355  sExpression,
356  0, &rShell );
357  aFieldMgr.InsertField( aData );
358  }
359  else
360  {
362  aFieldMgr.InsertField( aData );
363  }
364  }
365  else
366  {
367  SwInsertField_Data aData(SwFieldTypesEnum::Database, 0, sDB, OUString(), 0, &rShell);
368  aFieldMgr.InsertField( aData );
369  }
370  }
371  else if(!aItem.bIsReturn)
372  {
373  rShell.Insert(aItem.sText);
374  }
375  else
376  {
377  if(bHideEmptyParagraphs)
378  {
379  SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
380  aFieldMgr.InsertField( aData );
381  }
382  sHideParagraphsExpression.clear();
383  //now add a new paragraph
384  rShell.SplitNode();
385  }
386  }
387  if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
388  {
389  SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
390  aFieldMgr.InsertField( aData );
391  }
392  }
393  return pRet;
394 }
395 
396 void SwMailMergeLayoutPage::InsertGreeting(SwWrtShell& rShell, SwMailMergeConfigItem const & rConfigItem, bool bExample)
397 {
398  //set the cursor to the desired position - if no text content is here then
399  //new paragraphs are inserted
400  const SwRect& rPageRect = rShell.GetAnyCurRect(CurRectType::Page);
401  const Point aGreetingPos( DEFAULT_LEFT_DISTANCE + rPageRect.Left(), GREETING_TOP_DISTANCE );
402 
403  const bool bRet = rShell.SetShadowCursorPos( aGreetingPos, FILL_TAB_SPACE );
404 
405  if(!bRet)
406  {
407  //there's already text at the desired position
408  //go to start of the doc, directly!
409  rShell.SttEndDoc(true);
410  //and go by paragraph until the position is reached
411  long nYPos = rShell.GetCharRect().Top();
412  while(nYPos < GREETING_TOP_DISTANCE)
413  {
414  if(!rShell.FwdPara())
415  break;
416  nYPos = rShell.GetCharRect().Top();
417  }
418  //text needs to be appended
419  while(nYPos < GREETING_TOP_DISTANCE)
420  {
421  if(!rShell.AppendTextNode())
422  break;
423  nYPos = rShell.GetCharRect().Top();
424  }
425  }
426  else
427  {
428  //we may end up inside of a paragraph if the left margin is not at DEFAULT_LEFT_DISTANCE
430  }
431  bool bSplitNode = !rShell.IsEndPara();
432  sal_Int32 nMoves = rConfigItem.GetGreetingMoves();
433  if( !bExample && 0 != nMoves )
434  {
435  if(nMoves < 0)
436  {
437  rShell.MoveParagraph( nMoves );
438  }
439  else
440  while(nMoves)
441  {
442  bool bMoved = rShell.MoveParagraph();
443  if(!bMoved)
444  {
445  //insert a new paragraph before the greeting line
446  rShell.SplitNode();
447  }
448  --nMoves;
449  }
450  }
451  //now insert the greeting text - if we have any?
452  const bool bIndividual = rConfigItem.IsIndividualGreeting(false);
453  if(bIndividual)
454  {
455  //lock expression fields - prevents hiding of the paragraph to insert into
456  rShell.LockExpFields();
457  if(bExample)
458  {
460  eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
461  {
463  rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
464  sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
465  if( nCurrent >= 0 && nCurrent < aEntries.getLength())
466  {
467  // Greeting
468  rShell.Insert(aEntries[nCurrent]);
469  break;
470  }
471  }
472  }
473  else
474  {
475  SwFieldMgr aFieldMgr(&rShell);
476  //three paragraphs, each with an appropriate hidden paragraph field
477  //are to be inserted
478 
479  //name of the gender column
480  const OUString sGenderColumn = rConfigItem.GetAssignedColumn(MM_PART_GENDER);
481  const OUString sNameColumn = rConfigItem.GetAssignedColumn(MM_PART_LASTNAME);
482 
483  const OUString& rFemaleGenderValue = rConfigItem.GetFemaleGenderValue();
484  bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
485  const SwDBData& rData = rConfigItem.GetCurrentDBData();
486  const OUString sCommonBase(rData.sDataSource + "." + rData.sCommand + ".");
487  const OUString sConditionBase("[" + sCommonBase + sGenderColumn + "]");
488  const OUString sNameColumnBase("[" + sCommonBase + sNameColumn + "]");
489 
490  const OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
491  + rData.sCommand + OUStringChar(DB_DELIM)
492  + OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM));
493 
494 // Female: [database.sGenderColumn] != "rFemaleGenderValue" && [database.NameColumn]
495 // Male: [database.sGenderColumn] == "rFemaleGenderValue" && [database.rGenderColumn]
496 // Neutral: [database.sNameColumn]
497  OSL_ENSURE(!sGenderColumn.isEmpty() && !rFemaleGenderValue.isEmpty(),
498  "gender settings not available - how to form the condition?");
499  //column used as lastname
501  eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
502  {
503  Sequence< OUString> aEntries = rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
504  sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
505  if( nCurrent >= 0 && nCurrent < aEntries.getLength())
506  {
507  const OUString sGreeting = aEntries[nCurrent];
508  OUString sCondition;
509  OUString sHideParagraphsExpression;
510  switch(eGender)
511  {
513  sCondition = sConditionBase + " != \"" + rFemaleGenderValue
514  + "\" OR NOT " + sNameColumnBase;
515  sHideParagraphsExpression = "!" + sNameColumnBase;
516  break;
518  sCondition = sConditionBase + " == \"" + rFemaleGenderValue
519  + "\" OR NOT " + sNameColumnBase;
520  break;
522  sCondition = sNameColumnBase;
523  break;
524  }
525 
526  if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
527  {
528  OUString sComplete = "(" + sCondition + ") OR (" + sHideParagraphsExpression + ")";
529  SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sComplete, OUString(), 0, &rShell);
530  aFieldMgr.InsertField( aData );
531  }
532  else
533  {
534  SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sCondition, OUString(), 0, &rShell);
535  aFieldMgr.InsertField( aData );
536  }
537  //now the text has to be inserted
538  const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
539  Sequence< OUString> aAssignment =
540  rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
541  const OUString* pAssignment = aAssignment.getConstArray();
542  SwAddressIterator aIter(sGreeting);
543  while(aIter.HasMore())
544  {
545  SwMergeAddressItem aItem = aIter.Next();
546  if(aItem.bIsColumn)
547  {
548  OUString sConvertedColumn = aItem.sText;
549  auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
550  static_cast<sal_uInt32>(aAssignment.getLength()));
551  for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
552  {
553  if (rHeaders[nColumn].first == aItem.sText &&
554  !pAssignment[nColumn].isEmpty())
555  {
556  sConvertedColumn = pAssignment[nColumn];
557  break;
558  }
559  }
561  sDBName + sConvertedColumn,
562  OUString(), 0, &rShell);
563  aFieldMgr.InsertField( aData );
564  }
565  else
566  {
567  rShell.Insert(aItem.sText);
568  }
569  }
570  //now add a new paragraph
571  rShell.SplitNode();
572  }
573  }
574 
575  }
576  rShell.UnlockExpFields();
577  }
578  else
579  {
581  sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(SwMailMergeConfigItem::NEUTRAL);
582  // Greeting
583  rShell.Insert(( nCurrent >= 0 && nCurrent < aEntries.getLength() )
584  ? aEntries[nCurrent] : OUString());
585  }
586  // now insert a new paragraph here if necessary
587  if(bSplitNode)
588  {
589  rShell.Push();
590  rShell.SplitNode();
592  }
593  //put the cursor to the start of the paragraph
594  rShell.SttPara();
595 
596  OSL_ENSURE(nullptr == rShell.GetTableFormat(), "What to do with a table here?");
597 }
598 
600 {
601  m_xExampleContainerWIN->show();
602 
603  Reference< XModel > & xModel = m_xExampleFrame->GetModel();
604  //now the ViewOptions should be set properly
605  Reference< XViewSettingsSupplier > xSettings(xModel->getCurrentController(), UNO_QUERY);
606  m_xViewProperties = xSettings->getViewSettings();
607  auto pXDoc = comphelper::getUnoTunnelImplementation<SwXTextDocument>(xModel);
608  SwDocShell* pDocShell = pXDoc->GetDocShell();
609  m_pExampleWrtShell = pDocShell->GetWrtShell();
610  OSL_ENSURE(m_pExampleWrtShell, "No SwWrtShell found!");
611  if(!m_pExampleWrtShell)
612  return;
613 
614  SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
615  if(rConfigItem.IsAddressBlock())
616  {
617  m_pAddressBlockFormat = InsertAddressFrame(
618  *m_pExampleWrtShell, rConfigItem,
620  m_xAlignToBodyCB->get_active(), true);
621  }
622  if(rConfigItem.IsGreetingLine(false))
623  {
624  InsertGreeting(*m_pExampleWrtShell, rConfigItem, true);
625  m_bIsGreetingInserted = true;
626  }
627 
628  ZoomHdl_Impl(*m_xZoomLB);
629 
630  const SwFormatFrameSize& rPageSize = m_pExampleWrtShell->GetPageDesc(
631  m_pExampleWrtShell->GetCurPageDesc()).GetMaster().GetFrameSize();
632  m_xLeftMF->set_max(rPageSize.GetWidth() - DEFAULT_LEFT_DISTANCE, FieldUnit::NONE);
633  m_xTopMF->set_max(rPageSize.GetHeight() - DEFAULT_TOP_DISTANCE, FieldUnit::NONE);
634 }
635 
636 IMPL_LINK(SwMailMergeLayoutPage, ZoomHdl_Impl, weld::ComboBox&, rBox, void)
637 {
638  if (m_pExampleWrtShell)
639  {
640  sal_Int16 eType = DocumentZoomType::BY_VALUE;
641  short nZoom = 50;
642  switch (rBox.get_active())
643  {
644  case 0 : eType = DocumentZoomType::ENTIRE_PAGE; break;
645  case 1 : nZoom = 50; break;
646  case 2 : nZoom = 75; break;
647  case 3 : nZoom = 100; break;
648  }
649  Any aZoom;
650  aZoom <<= eType;
651  m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_TYPE, aZoom);
652  aZoom <<= nZoom;
653  m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_VALUE, aZoom);
654 
655  m_xExampleFrame->Invalidate();
656  }
657 }
658 
660 {
661  if(m_pExampleWrtShell && m_pAddressBlockFormat)
662  {
663  long nLeft = static_cast< long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
664  long nTop = static_cast< long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
665 
666  SfxItemSet aSet(
667  m_pExampleWrtShell->GetAttrPool(),
669  if (m_xAlignToBodyCB->get_active())
670  aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
671  else
672  aSet.Put(SwFormatHoriOrient( nLeft, text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
673  aSet.Put(SwFormatVertOrient( nTop, text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
674  m_pExampleWrtShell->GetDoc()->SetFlyFrameAttr( *m_pAddressBlockFormat, aSet );
675  m_xExampleFrame->Invalidate();
676  }
677 }
678 
679 IMPL_LINK(SwMailMergeLayoutPage, GreetingsHdl_Impl, weld::Button&, rButton, void)
680 {
681  bool bDown = &rButton == m_xDownPB.get();
682  bool bMoved = m_pExampleWrtShell->MoveParagraph( bDown ? 1 : -1 );
683  if (bMoved || bDown)
684  m_pWizard->GetConfigItem().MoveGreeting(bDown ? 1 : -1 );
685  if(!bMoved && bDown)
686  {
687  //insert a new paragraph before the greeting line
688  m_pExampleWrtShell->SplitNode();
689  }
690  m_xExampleFrame->Invalidate();
691 }
692 
693 IMPL_LINK(SwMailMergeLayoutPage, AlignToTextHdl_Impl, weld::ToggleButton&, rBox, void)
694 {
695  bool bCheck = rBox.get_active() && rBox.get_sensitive();
696  m_xLeftFT->set_sensitive(!bCheck);
697  m_xLeftMF->set_sensitive(!bCheck);
698  ChangeAddressHdl_Impl( *m_xLeftMF );
699 }
700 
701 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
std::unique_ptr< weld::Button > m_xUpPB
SwMergeAddressItem Next()
bool HasMore() const
bool IsHideEmptyParagraphs() const
bool AppendTextNode()
Definition: editsh.cxx:210
bool SttPara(bool bSelect=false)
Definition: move.cxx:348
FieldUnit
#define RES_FRM_SIZE
Definition: hintids.hxx:196
sal_Int32 nCommandType
Definition: swdbdata.hxx:32
void SplitNode(bool bAutoFormat=false)
Definition: wrtsh1.cxx:999
bool IsAddressInserted() const
#define DEFAULT_LEFT_DISTANCE
bool DelFullPara()
Remove a complete paragraph.
Definition: eddel.cxx:337
#define UNO_NAME_ZOOM_VALUE
Definition: unoprnms.hxx:427
#define EX_SHOW_DEFAULT_PAGE
Definition: unotools.hxx:34
SwMailMergeConfigItem & GetConfigItem()
#define DEFAULT_ADDRESS_WIDTH
bool SttEndDoc(bool bStt)
Definition: crsrsh.cxx:562
fill with spaces and tabs
Definition: crstate.hxx:31
bool GoCurrPara(SwPaM &rPam, SwMoveFnCollection const &aPosPara)
Definition: pam.cxx:947
const LanguageTag & GetUILanguageTag() const
long Height() const
signed char sal_Int8
long GetWidth() const
const SwRect & GetAnyCurRect(CurRectType eType, const Point *pPt=nullptr, const css::uno::Reference< css::embed::XEmbeddedObject > &=css::uno::Reference< css::embed::XEmbeddedObject >()) const
Definition: fews.cxx:92
#define UNO_NAME_ZOOM_TYPE
Definition: unoprnms.hxx:428
OUString & GetExcludeCountry() const
OUString sDataSource
Definition: swdbdata.hxx:30
static const AllSettings & GetSettings()
#define RES_SURROUND
Definition: hintids.hxx:208
std::unique_ptr< SwOneExampleFrame > m_xExampleFrame
bool Pop(SwCursorShell::PopMode=SwCursorShell::PopMode::DeleteStack)
Definition: wrtsh1.cxx:1726
SwMoveFnCollection const & fnParaStart
Definition: paminit.cxx:46
SwUndoId EndUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Closes parenthesis of nUndoId, not used by UI.
Definition: edws.cxx:233
#define DEFAULT_ADDRESS_HEIGHT
const SwDBData & GetCurrentDBData() const
sal_Int32 GetGreetingMoves() const
std::unique_ptr< weld::Builder > m_xBuilder
css::uno::Reference< css::frame::XModel > GetModel() const
std::unique_ptr< weld::MetricSpinButton > m_xLeftMF
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
bool InsertField(const SwInsertField_Data &rData)
Definition: fldmgr.cxx:898
std::unique_ptr< weld::ComboBox > m_xZoomLB
bool DelRight()
Definition: delete.cxx:285
SwWrtShell & GetWrtShell() const
Definition: view.hxx:400
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
bool GotoFly(const OUString &rName, FlyCntType eType=FLYCNTTYPE_ALL, bool bSelFrame=true)
Definition: move.cxx:590
Value in Var-direction gives minimum (can be exceeded but not be less).
Definition: fmtfsize.hxx:39
void Top(const long nTop)
Definition: swrect.hxx:202
#define GREETING_TOP_DISTANCE
static SwFrameFormat * InsertAddressFrame(SwWrtShell &rShell, SwMailMergeConfigItem const &rConfigItem, const Point &rDestination, bool bAlignToBody, bool bExample)
css::uno::Sequence< OUString > GetAddressBlocks() const
static SwFrameFormat * InsertAddressAndGreeting(SwView const *pView, SwMailMergeConfigItem &rConfigItem, const Point &rAddressPos, bool bAlignToBody)
#define DEFAULT_TOP_DISTANCE
const OUString & GetName() const
Definition: format.hxx:111
SwMailMergeLayoutPage(weld::Container *pPage, SwMailMergeWizard *pWizard)
static void InsertGreeting(SwWrtShell &rShell, SwMailMergeConfigItem const &rConfigItem, bool bExample)
css::uno::Sequence< OUString > GetGreetings(Gender eType) const
void Insert(SwField const &, SwPaM *pAnnotationRange=nullptr)
Definition: wrtsh2.cxx:71
std::unique_ptr< weld::CustomWeld > m_xExampleContainerWIN
bool MovePara(SwWhichPara, SwMoveFnCollection const &)
Definition: crsrsh.cxx:683
bool IsGreetingInserted() const
const SfxItemPool & GetAttrPool() const
Definition: viewsh.hxx:614
IMPL_LINK(SwMailMergeLayoutPage, ZoomHdl_Impl, weld::ComboBox &, rBox, void)
const std::vector< std::pair< OUString, int > > & GetDefaultAddressHeaders() const
static SW_DLLPUBLIC std::shared_ptr< const SfxFilter > GetFilterOfFormat(const OUString &rFormat, const SfxFilterContainer *pCnt=nullptr)
find for an internal format name the corresponding filter entry
Definition: iodetect.cxx:68
bool SetShadowCursorPos(const Point &rPt, SwFillMode eFillMode)
Definition: crstrvl.cxx:2040
Style of a layout element.
Definition: frmfmt.hxx:57
#define RES_ANCHOR
Definition: hintids.hxx:211
SW_DLLPUBLIC FieldUnit GetDfltMetric(bool bWeb)
Definition: uitool.cxx:709
OUString const & GetURL() const
std::unique_ptr< weld::Button > m_xDownPB
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2221
std::unique_ptr< weld::Container > m_xPosition
FlyAnchors.
Definition: fmtanchr.hxx:34
virtual ~SwMailMergeLayoutPage() override
SwUndoId StartUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Undo: set up Undo parenthesis, return nUndoId of this parenthesis.
Definition: edws.cxx:222
OString stripStart(const OString &rIn, sal_Char c)
bool MoveParagraph(long nOffset=1)
Definition: ednumber.cxx:397
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:224
GUIDCNamePair const aData
DocumentType const eType
void UnlockExpFields()
Definition: edfld.cxx:360
std::unique_ptr< weld::CheckButton > m_xAlignToBodyCB
#define DB_DELIM
Definition: swtypes.hxx:141
long X() const
SwDocShell * GetDocShell()
Definition: view.cxx:1127
SwFrameFormat * GetTableFormat()
Definition: edws.cxx:182
css::uno::Sequence< OUString > GetColumnAssignment(const SwDBData &rDBData) const
Rect of current page.
sal_Int32 GetCurrentGreeting(Gender eType) const
std::unique_ptr< weld::MetricSpinButton > m_xTopMF
void Left(const long nLeft)
Definition: swrect.hxx:193
bool IsIndividualGreeting(bool bInEMail) const
SwMailMergeWizard * m_pWizard
const SwFrameFormat * NewFlyFrame(const SfxItemSet &rSet, bool bAnchValid=false, SwFrameFormat *pParent=nullptr)
Definition: fefly1.cxx:632
static OUString formatPercent(double dNumber, const LanguageTag &rLangTag)
#define FILTER_XML
XML filter.
Definition: iodetect.hxx:35
IMPL_LINK_NOARG(SwMailMergeLayoutPage, PreviewLoadedHdl_Impl, SwOneExampleFrame &, void)
OUString GetAssignedColumn(sal_uInt32 nColumn) const
virtual bool commitPage(::vcl::WizardTypes::CommitPageReason _eReason) override
const SwRect & GetCharRect() const
Definition: crsrsh.hxx:520
bool IsGreetingLine(bool bInEMail) const
void UnSelectFrame()
Definition: select.cxx:327
const OUString & GetFemaleGenderValue() const
bool IsIncludeCountry() const
std::unique_ptr< weld::Container > m_xGreetingLine
#define RES_BOX
Definition: hintids.hxx:213
void SAL_CALL first(const css::awt::SpinEvent &rEvent) override
virtual void Activate() override
ScXMLEditAttributeMap::Entry const aEntries[]
const SwFrameFormat * GetFlyFrameFormat() const
Get FlyFrameFormat; for UI macro linkage at Flys.
Definition: fefly1.cxx:1233
SwWrtShell * m_pExampleWrtShell
Reference< XModel > xModel
bool IsAddressBlock() const
bool IsEndPara() const
Definition: crsrsh.cxx:1101
long GetHeight() const
bool FwdPara()
Definition: wrtsh.hxx:227
OUString sCommand
Definition: swdbdata.hxx:31
void EnableKillingFile(bool bEnable=true)
void LockExpFields()
Definition: edfld.cxx:355
Definition: view.hxx:146
long Y() const
SwFrameFormat * m_pAddressBlockFormat