android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / dbui / mmlayoutpage.cxx
blob2e01e49ec90a7cf4f7ba79d852cd76bddcebcb8d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <comphelper/propertyvalue.hxx>
28 #include <comphelper/servicehelper.hxx>
29 #include <i18nutil/unicode.hxx>
30 #include <unotools/tempfile.hxx>
31 #include <uitool.hxx>
32 #include <view.hxx>
33 #include <swundo.hxx>
34 #include <sfx2/docfilt.hxx>
35 #include <sfx2/fcontnr.hxx>
36 #include <svtools/unitconv.hxx>
37 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
38 #include <com/sun/star/view/DocumentZoomType.hpp>
39 #include <fldmgr.hxx>
40 #include <fldbas.hxx>
41 #include <unotxdoc.hxx>
42 #include <docsh.hxx>
43 #include <doc.hxx>
44 #include <wrtsh.hxx>
45 #include <fmtsrnd.hxx>
46 #include <pagedesc.hxx>
47 #include <fmtanchr.hxx>
48 #include <fmtornt.hxx>
49 #include <fmtfsize.hxx>
50 #include <editeng/boxitem.hxx>
51 #include <osl/file.hxx>
52 #include <vcl/settings.hxx>
53 #include <unoprnms.hxx>
55 #include <dbui.hrc>
57 using namespace osl;
58 using namespace svt;
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::text;
62 using namespace ::com::sun::star::frame;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::view;
66 constexpr tools::Long DEFAULT_LEFT_DISTANCE = o3tl::toTwips(25, o3tl::Length::mm); // 2,5 cm
67 constexpr tools::Long DEFAULT_TOP_DISTANCE = o3tl::toTwips(55, o3tl::Length::mm); // 5,5 cm
68 constexpr tools::Long GREETING_TOP_DISTANCE = o3tl::toTwips(125, o3tl::Length::mm); //12,5 cm
69 constexpr tools::Long DEFAULT_ADDRESS_WIDTH = o3tl::toTwips(75, o3tl::Length::mm); // 7,5 cm
70 constexpr tools::Long DEFAULT_ADDRESS_HEIGHT = o3tl::toTwips(35, o3tl::Length::mm); // 3,5cm
72 SwMailMergeLayoutPage::SwMailMergeLayoutPage(weld::Container* pPage, SwMailMergeWizard* pWizard)
73 : vcl::OWizardPage(pPage, pWizard, "modules/swriter/ui/mmlayoutpage.ui", "MMLayoutPage")
74 , m_pExampleWrtShell(nullptr)
75 , m_pAddressBlockFormat(nullptr)
76 , m_bIsGreetingInserted(false)
77 , m_pWizard(pWizard)
78 , m_xPosition(m_xBuilder->weld_container("addresspos"))
79 , m_xAlignToBodyCB(m_xBuilder->weld_check_button("align"))
80 , m_xLeftFT(m_xBuilder->weld_label("leftft"))
81 , m_xLeftMF(m_xBuilder->weld_metric_spin_button("left", FieldUnit::CM))
82 , m_xTopMF(m_xBuilder->weld_metric_spin_button("top", FieldUnit::CM))
83 , m_xGreetingLine(m_xBuilder->weld_container("greetingspos"))
84 , m_xUpPB(m_xBuilder->weld_button("up"))
85 , m_xDownPB(m_xBuilder->weld_button("down"))
86 , m_xZoomLB(m_xBuilder->weld_combo_box("zoom"))
88 std::shared_ptr<const SfxFilter> pSfxFlt =
89 SwDocShell::Factory().GetFilterContainer()->
90 GetFilter4FilterName("writer8", SfxFilterFlags::EXPORT);
92 //save the current document into a temporary file
94 //temp file needs its own block
95 //creating with extension is not supported by a static method :-(
96 OUString const sExt(
97 comphelper::string::stripStart(pSfxFlt->GetDefaultExtension(),'*'));
98 utl::TempFileNamed aTempFile( u"", true, sExt );
99 aTempFile.EnableKillingFile();
100 m_sExampleURL = aTempFile.GetURL();
102 SwView* pView = m_pWizard->GetSwView();
103 // Don't save embedded data set! It would steal it from current document.
104 uno::Sequence< beans::PropertyValue > aValues =
106 comphelper::makePropertyValue("FilterName", pSfxFlt->GetFilterName()),
107 comphelper::makePropertyValue("NoEmbDataSet", true)
110 uno::Reference< frame::XStorable > xStore( pView->GetDocShell()->GetModel(), uno::UNO_QUERY);
111 xStore->storeToURL( m_sExampleURL, aValues );
113 Link<SwOneExampleFrame&,void> aLink(LINK(this, SwMailMergeLayoutPage, PreviewLoadedHdl_Impl));
114 m_xExampleFrame.reset(new SwOneExampleFrame(EX_SHOW_DEFAULT_PAGE, &aLink, &m_sExampleURL));
115 m_xExampleContainerWIN.reset(new weld::CustomWeld(*m_xBuilder, "example", *m_xExampleFrame));
117 Size aSize = m_xExampleFrame->GetDrawingArea()->get_ref_device().LogicToPixel(
118 Size(124, 159), MapMode(MapUnit::MapAppFont));
119 m_xExampleFrame->set_size_request(aSize.Width(), aSize.Height());
121 m_xExampleContainerWIN->hide();
123 m_xLeftMF->set_value(m_xLeftMF->normalize(DEFAULT_LEFT_DISTANCE), FieldUnit::TWIP);
124 m_xTopMF->set_value(m_xTopMF->normalize(DEFAULT_TOP_DISTANCE), FieldUnit::TWIP);
126 const LanguageTag& rLang = Application::GetSettings().GetUILanguageTag();
127 m_xZoomLB->append_text(unicode::formatPercent(50, rLang));
128 m_xZoomLB->append_text(unicode::formatPercent(75, rLang));
129 m_xZoomLB->append_text(unicode::formatPercent(100, rLang));
130 m_xZoomLB->set_active(0); //page size
131 m_xZoomLB->connect_changed(LINK(this, SwMailMergeLayoutPage, ZoomHdl_Impl));
133 Link<weld::MetricSpinButton&,void> aFrameHdl = LINK(this, SwMailMergeLayoutPage, ChangeAddressHdl_Impl);
134 m_xLeftMF->connect_value_changed(aFrameHdl);
135 m_xTopMF->connect_value_changed(aFrameHdl);
137 FieldUnit eFieldUnit = ::GetDfltMetric(false);
138 ::SetFieldUnit( *m_xLeftMF, eFieldUnit );
139 ::SetFieldUnit( *m_xTopMF, eFieldUnit );
141 Link<weld::Button&,void> aUpDownHdl = LINK(this, SwMailMergeLayoutPage, GreetingsHdl_Impl );
142 m_xUpPB->connect_clicked(aUpDownHdl);
143 m_xDownPB->connect_clicked(aUpDownHdl);
144 m_xAlignToBodyCB->connect_toggled(LINK(this, SwMailMergeLayoutPage, AlignToTextHdl_Impl));
145 m_xAlignToBodyCB->set_active(true);
148 SwMailMergeLayoutPage::~SwMailMergeLayoutPage()
150 File::remove( m_sExampleURL );
153 void SwMailMergeLayoutPage::Activate()
155 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
156 bool bGreetingLine = rConfigItem.IsGreetingLine(false) && !rConfigItem.IsGreetingInserted();
157 bool bAddressBlock = rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted();
159 m_xPosition->set_sensitive(bAddressBlock);
160 AlignToTextHdl_Impl(*m_xAlignToBodyCB);
162 m_xGreetingLine->set_sensitive(bGreetingLine);
164 //check if greeting and/or address frame have to be inserted/removed
165 if(!m_pExampleWrtShell) // initially there's nothing to check
166 return;
168 if(!rConfigItem.IsGreetingInserted() &&
169 m_bIsGreetingInserted != bGreetingLine )
171 if( m_bIsGreetingInserted )
173 m_pExampleWrtShell->DelFullPara();
174 m_bIsGreetingInserted = false;
176 else
178 InsertGreeting(*m_pExampleWrtShell, m_pWizard->GetConfigItem(), true);
179 m_bIsGreetingInserted = true;
182 if(!rConfigItem.IsAddressInserted() &&
183 rConfigItem.IsAddressBlock() != ( nullptr != m_pAddressBlockFormat ))
185 if( m_pAddressBlockFormat )
187 m_pExampleWrtShell->Push();
188 m_pExampleWrtShell->GotoFly( m_pAddressBlockFormat->GetName() );
189 m_pExampleWrtShell->DelRight();
190 m_pAddressBlockFormat = nullptr;
191 m_pExampleWrtShell->Pop(SwCursorShell::PopMode::DeleteCurrent);
193 else
195 tools::Long nLeft = static_cast< tools::Long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
196 tools::Long nTop = static_cast< tools::Long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
197 m_pAddressBlockFormat = InsertAddressFrame(
198 *m_pExampleWrtShell, m_pWizard->GetConfigItem(),
199 Point(nLeft, nTop),
200 m_xAlignToBodyCB->get_active(), true);
203 m_xExampleFrame->Invalidate();
206 bool SwMailMergeLayoutPage::commitPage(::vcl::WizardTypes::CommitPageReason eReason)
208 //now insert the frame and the greeting
209 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
210 if (eReason == ::vcl::WizardTypes::eTravelForward || eReason == ::vcl::WizardTypes::eFinish)
212 tools::Long nLeft = static_cast< tools::Long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
213 tools::Long nTop = static_cast< tools::Long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
214 InsertAddressAndGreeting(
215 m_pWizard->GetSwView(),
216 rConfigItem,
217 Point(nLeft, nTop),
218 m_xAlignToBodyCB->get_active());
220 return true;
223 SwFrameFormat* SwMailMergeLayoutPage::InsertAddressAndGreeting(SwView const * pView,
224 SwMailMergeConfigItem& rConfigItem,
225 const Point& rAddressPosition,
226 bool bAlignToBody)
228 SwFrameFormat* pAddressBlockFormat = nullptr;
229 pView->GetWrtShell().StartUndo(SwUndoId::INSERT);
230 if(rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted())
232 //insert the frame
233 Point aAddressPosition(DEFAULT_LEFT_DISTANCE, DEFAULT_TOP_DISTANCE);
234 if(rAddressPosition.X() > 0 && rAddressPosition.Y() > 0)
235 aAddressPosition = rAddressPosition;
236 pAddressBlockFormat = InsertAddressFrame( pView->GetWrtShell(),
237 rConfigItem,
238 aAddressPosition, bAlignToBody, false);
239 rConfigItem.SetAddressInserted();
241 //now the greeting
242 if(rConfigItem.IsGreetingLine(false) && !rConfigItem.IsGreetingInserted())
244 InsertGreeting( pView->GetWrtShell(), rConfigItem, false);
245 rConfigItem.SetGreetingInserted();
247 pView->GetWrtShell().EndUndo(SwUndoId::INSERT);
248 return pAddressBlockFormat;
251 SwFrameFormat* SwMailMergeLayoutPage::InsertAddressFrame(
252 SwWrtShell& rShell,
253 SwMailMergeConfigItem const & rConfigItem,
254 const Point& rDestination,
255 bool bAlignLeft,
256 bool bExample)
258 // insert the address block and the greeting line
259 SfxItemSetFixed<
260 RES_FRM_SIZE, RES_FRM_SIZE,
261 RES_SURROUND, RES_ANCHOR,
262 RES_BOX, RES_BOX> aSet( rShell.GetAttrPool() );
263 aSet.Put(SwFormatAnchor(RndStdIds::FLY_AT_PAGE, 1));
264 if(bAlignLeft)
265 aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
266 else
267 aSet.Put(SwFormatHoriOrient( rDestination.X(), text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
268 aSet.Put(SwFormatVertOrient( rDestination.Y(), text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
269 aSet.Put(SwFormatFrameSize( SwFrameSize::Minimum, DEFAULT_ADDRESS_WIDTH, DEFAULT_ADDRESS_HEIGHT ));
270 // the example gets a border around the frame, the real document doesn't get one
271 if(!bExample)
272 aSet.Put(SvxBoxItem( RES_BOX ));
273 aSet.Put(SwFormatSurround( css::text::WrapTextMode_NONE ));
275 rShell.NewFlyFrame(aSet, true );
276 SwFrameFormat* pRet = rShell.GetFlyFrameFormat();
277 OSL_ENSURE( pRet, "Fly not inserted" );
279 rShell.UnSelectFrame();
280 const Sequence< OUString> aBlocks = rConfigItem.GetAddressBlocks();
281 if(bExample)
283 rShell.Insert(aBlocks[0]);
285 else
287 //the placeholders should be replaced by the appropriate fields
288 SwFieldMgr aFieldMgr(&rShell);
289 //create a database string source.command.commandtype.column
290 const SwDBData& rData = rConfigItem.GetCurrentDBData();
291 OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
292 + rData.sCommand + OUStringChar(DB_DELIM));
293 const OUString sDatabaseConditionPrefix(sDBName.replace(DB_DELIM, '.'));
294 sDBName += OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM);
296 // if only the country is in an address line the
297 // paragraph has to be hidden depending on the
298 // IsIncludeCountry()/GetExcludeCountry() settings
300 bool bIncludeCountry = rConfigItem.IsIncludeCountry();
301 bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
302 const OUString rExcludeCountry = rConfigItem.GetExcludeCountry();
303 bool bSpecialReplacementForCountry = (!bIncludeCountry || !rExcludeCountry.isEmpty());
305 const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
306 Sequence< OUString> aAssignment =
307 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
308 const OUString* pAssignment = aAssignment.getConstArray();
309 const OUString sCountryColumn(
310 (aAssignment.getLength() > MM_PART_COUNTRY && !aAssignment[MM_PART_COUNTRY].isEmpty())
311 ? aAssignment[MM_PART_COUNTRY]
312 : rHeaders[MM_PART_COUNTRY].first);
314 OUString sHideParagraphsExpression;
315 SwAddressIterator aIter(aBlocks[0]);
316 while(aIter.HasMore())
318 SwMergeAddressItem aItem = aIter.Next();
319 if(aItem.bIsColumn)
321 OUString sConvertedColumn = aItem.sText;
322 auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
323 static_cast<sal_uInt32>(aAssignment.getLength()));
324 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
326 if (rHeaders[nColumn].first == aItem.sText &&
327 !pAssignment[nColumn].isEmpty())
329 sConvertedColumn = pAssignment[nColumn];
330 break;
333 const OUString sDB(sDBName + sConvertedColumn);
335 if(!sHideParagraphsExpression.isEmpty())
336 sHideParagraphsExpression += " AND ";
337 sHideParagraphsExpression += "![" + sDatabaseConditionPrefix + sConvertedColumn + "]";
339 if( bSpecialReplacementForCountry && sCountryColumn == sConvertedColumn )
341 // now insert a hidden paragraph field
342 if( !rExcludeCountry.isEmpty() )
344 const OUString sExpression("[" + sDatabaseConditionPrefix + sCountryColumn + "]");
345 SwInsertField_Data aData(SwFieldTypesEnum::ConditionalText, 0,
346 sExpression + " != \"" + rExcludeCountry + "\"",
347 sExpression,
348 0, &rShell );
349 aFieldMgr.InsertField( aData );
351 else
353 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, "", "", 0, &rShell );
354 aFieldMgr.InsertField( aData );
357 else
359 SwInsertField_Data aData(SwFieldTypesEnum::Database, 0, sDB, OUString(), 0, &rShell);
360 aFieldMgr.InsertField( aData );
363 else if(!aItem.bIsReturn)
365 rShell.Insert(aItem.sText);
367 else
369 if(bHideEmptyParagraphs)
371 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
372 aFieldMgr.InsertField( aData );
374 sHideParagraphsExpression.clear();
375 //now add a new paragraph
376 rShell.SplitNode();
379 if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
381 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
382 aFieldMgr.InsertField( aData );
385 return pRet;
388 void SwMailMergeLayoutPage::InsertGreeting(SwWrtShell& rShell, SwMailMergeConfigItem const & rConfigItem, bool bExample)
390 //set the cursor to the desired position - if no text content is here then
391 //new paragraphs are inserted
392 const SwRect& rPageRect = rShell.GetAnyCurRect(CurRectType::Page);
393 const Point aGreetingPos( DEFAULT_LEFT_DISTANCE + rPageRect.Left(), GREETING_TOP_DISTANCE );
395 const bool bRet = rShell.SetShadowCursorPos( aGreetingPos, SwFillMode::TabSpace );
397 if(!bRet)
399 //there's already text at the desired position
400 //go to start of the doc, directly!
401 rShell.SttEndDoc(true);
402 //and go by paragraph until the position is reached
403 tools::Long nYPos = rShell.GetCharRect().Top();
404 while(nYPos < GREETING_TOP_DISTANCE)
406 if(!rShell.FwdPara())
407 break;
408 nYPos = rShell.GetCharRect().Top();
410 //text needs to be appended
411 while(nYPos < GREETING_TOP_DISTANCE)
413 if(!rShell.AppendTextNode())
414 break;
415 nYPos = rShell.GetCharRect().Top();
418 else
420 //we may end up inside of a paragraph if the left margin is not at DEFAULT_LEFT_DISTANCE
421 rShell.MovePara(GoCurrPara, fnParaStart);
423 bool bSplitNode = !rShell.IsEndPara();
424 SwNodeOffset nMoves(rConfigItem.GetGreetingMoves());
425 if( !bExample && SwNodeOffset(0) != nMoves )
427 if(nMoves < SwNodeOffset(0))
429 rShell.MoveParagraph( nMoves );
431 else
432 while(nMoves)
434 bool bMoved = rShell.MoveParagraph();
435 if(!bMoved)
437 //insert a new paragraph before the greeting line
438 rShell.SplitNode();
440 --nMoves;
443 //now insert the greeting text - if we have any?
444 const bool bIndividual = rConfigItem.IsIndividualGreeting(false);
445 if(bIndividual)
447 //lock expression fields - prevents hiding of the paragraph to insert into
448 rShell.LockExpFields();
449 if(bExample)
451 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
452 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
454 Sequence< OUString > aEntries =
455 rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
456 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
457 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
459 // Greeting
460 rShell.Insert(aEntries[nCurrent]);
461 break;
465 else
467 SwFieldMgr aFieldMgr(&rShell);
468 //three paragraphs, each with an appropriate hidden paragraph field
469 //are to be inserted
471 //name of the gender column
472 const OUString sGenderColumn = rConfigItem.GetAssignedColumn(MM_PART_GENDER);
473 const OUString sNameColumn = rConfigItem.GetAssignedColumn(MM_PART_LASTNAME);
475 const OUString& rFemaleGenderValue = rConfigItem.GetFemaleGenderValue();
476 bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
477 const SwDBData& rData = rConfigItem.GetCurrentDBData();
478 const OUString sCommonBase(rData.sDataSource + "." + rData.sCommand + ".");
479 const OUString sConditionBase("[" + sCommonBase + sGenderColumn + "]");
480 const OUString sNameColumnBase("[" + sCommonBase + sNameColumn + "]");
482 const OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
483 + rData.sCommand + OUStringChar(DB_DELIM)
484 + OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM));
486 // Female: [database.sGenderColumn] != "rFemaleGenderValue" && [database.NameColumn]
487 // Male: [database.sGenderColumn] == "rFemaleGenderValue" && [database.rGenderColumn]
488 // Neutral: [database.sNameColumn]
489 OSL_ENSURE(!sGenderColumn.isEmpty() && !rFemaleGenderValue.isEmpty(),
490 "gender settings not available - how to form the condition?");
491 //column used as lastname
492 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
493 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
495 Sequence< OUString> aEntries = rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
496 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
497 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
499 const OUString sGreeting = aEntries[nCurrent];
500 OUString sCondition;
501 OUString sHideParagraphsExpression;
502 switch(eGender)
504 case SwMailMergeConfigItem::FEMALE:
505 sCondition = sConditionBase + " != \"" + rFemaleGenderValue
506 + "\" OR NOT " + sNameColumnBase;
507 sHideParagraphsExpression = "!" + sNameColumnBase;
508 break;
509 case SwMailMergeConfigItem::MALE:
510 sCondition = sConditionBase + " == \"" + rFemaleGenderValue
511 + "\" OR NOT " + sNameColumnBase;
512 break;
513 case SwMailMergeConfigItem::NEUTRAL:
514 sCondition = sNameColumnBase;
515 break;
518 if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
520 OUString sComplete = "(" + sCondition + ") OR (" + sHideParagraphsExpression + ")";
521 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sComplete, OUString(), 0, &rShell);
522 aFieldMgr.InsertField( aData );
524 else
526 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sCondition, OUString(), 0, &rShell);
527 aFieldMgr.InsertField( aData );
529 //now the text has to be inserted
530 const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
531 Sequence< OUString> aAssignment =
532 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
533 const OUString* pAssignment = aAssignment.getConstArray();
534 SwAddressIterator aIter(sGreeting);
535 while(aIter.HasMore())
537 SwMergeAddressItem aItem = aIter.Next();
538 if(aItem.bIsColumn)
540 OUString sConvertedColumn = aItem.sText;
541 auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
542 static_cast<sal_uInt32>(aAssignment.getLength()));
543 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
545 if (rHeaders[nColumn].first == aItem.sText &&
546 !pAssignment[nColumn].isEmpty())
548 sConvertedColumn = pAssignment[nColumn];
549 break;
552 SwInsertField_Data aData(SwFieldTypesEnum::Database, 0,
553 sDBName + sConvertedColumn,
554 OUString(), 0, &rShell);
555 aFieldMgr.InsertField( aData );
557 else
559 rShell.Insert(aItem.sText);
562 //now add a new paragraph
563 rShell.SplitNode();
568 rShell.UnlockExpFields();
570 else
572 Sequence< OUString> aEntries = rConfigItem.GetGreetings(SwMailMergeConfigItem::NEUTRAL);
573 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(SwMailMergeConfigItem::NEUTRAL);
574 // Greeting
575 rShell.Insert(( nCurrent >= 0 && nCurrent < aEntries.getLength() )
576 ? aEntries[nCurrent] : OUString());
578 // now insert a new paragraph here if necessary
579 if(bSplitNode)
581 rShell.Push();
582 rShell.SplitNode();
583 rShell.Pop(SwCursorShell::PopMode::DeleteCurrent);
585 //put the cursor to the start of the paragraph
586 rShell.SttPara();
588 OSL_ENSURE(nullptr == rShell.GetTableFormat(), "What to do with a table here?");
591 IMPL_LINK_NOARG(SwMailMergeLayoutPage, PreviewLoadedHdl_Impl, SwOneExampleFrame&, void)
593 m_xExampleContainerWIN->show();
595 Reference< XModel > & xModel = m_xExampleFrame->GetModel();
596 //now the ViewOptions should be set properly
597 Reference< XViewSettingsSupplier > xSettings(xModel->getCurrentController(), UNO_QUERY);
598 m_xViewProperties = xSettings->getViewSettings();
599 auto pXDoc = comphelper::getFromUnoTunnel<SwXTextDocument>(xModel);
600 SwDocShell* pDocShell = pXDoc->GetDocShell();
601 m_pExampleWrtShell = pDocShell->GetWrtShell();
602 OSL_ENSURE(m_pExampleWrtShell, "No SwWrtShell found!");
603 if(!m_pExampleWrtShell)
604 return;
606 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
607 if(rConfigItem.IsAddressBlock())
609 m_pAddressBlockFormat = InsertAddressFrame(
610 *m_pExampleWrtShell, rConfigItem,
611 Point(DEFAULT_LEFT_DISTANCE, DEFAULT_TOP_DISTANCE),
612 m_xAlignToBodyCB->get_active(), true);
614 if(rConfigItem.IsGreetingLine(false))
616 InsertGreeting(*m_pExampleWrtShell, rConfigItem, true);
617 m_bIsGreetingInserted = true;
620 ZoomHdl_Impl(*m_xZoomLB);
622 const SwFormatFrameSize& rPageSize = m_pExampleWrtShell->GetPageDesc(
623 m_pExampleWrtShell->GetCurPageDesc()).GetMaster().GetFrameSize();
624 m_xLeftMF->set_max(rPageSize.GetWidth() - DEFAULT_LEFT_DISTANCE, FieldUnit::NONE);
625 m_xTopMF->set_max(rPageSize.GetHeight() - DEFAULT_TOP_DISTANCE, FieldUnit::NONE);
628 IMPL_LINK(SwMailMergeLayoutPage, ZoomHdl_Impl, weld::ComboBox&, rBox, void)
630 if (!m_pExampleWrtShell)
631 return;
633 sal_Int16 eType = DocumentZoomType::BY_VALUE;
634 short nZoom = 50;
635 switch (rBox.get_active())
637 case 0 : eType = DocumentZoomType::ENTIRE_PAGE; break;
638 case 1 : nZoom = 50; break;
639 case 2 : nZoom = 75; break;
640 case 3 : nZoom = 100; break;
642 Any aZoom;
643 aZoom <<= eType;
644 m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_TYPE, aZoom);
645 aZoom <<= nZoom;
646 m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_VALUE, aZoom);
648 m_xExampleFrame->Invalidate();
651 IMPL_LINK_NOARG(SwMailMergeLayoutPage, ChangeAddressHdl_Impl, weld::MetricSpinButton&, void)
653 if(!(m_pExampleWrtShell && m_pAddressBlockFormat))
654 return;
656 tools::Long nLeft = static_cast< tools::Long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
657 tools::Long nTop = static_cast< tools::Long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
659 SfxItemSetFixed<RES_VERT_ORIENT, RES_ANCHOR> aSet(
660 m_pExampleWrtShell->GetAttrPool());
661 if (m_xAlignToBodyCB->get_active())
662 aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
663 else
664 aSet.Put(SwFormatHoriOrient( nLeft, text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
665 aSet.Put(SwFormatVertOrient( nTop, text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
666 m_pExampleWrtShell->GetDoc()->SetFlyFrameAttr( *m_pAddressBlockFormat, aSet );
667 m_xExampleFrame->Invalidate();
670 IMPL_LINK(SwMailMergeLayoutPage, GreetingsHdl_Impl, weld::Button&, rButton, void)
672 bool bDown = &rButton == m_xDownPB.get();
673 bool bMoved = m_pExampleWrtShell->MoveParagraph( SwNodeOffset(bDown ? 1 : -1) );
674 if (bMoved || bDown)
675 m_pWizard->GetConfigItem().MoveGreeting(bDown ? 1 : -1 );
676 if(!bMoved && bDown)
678 //insert a new paragraph before the greeting line
679 m_pExampleWrtShell->SplitNode();
681 m_xExampleFrame->Invalidate();
684 IMPL_LINK(SwMailMergeLayoutPage, AlignToTextHdl_Impl, weld::Toggleable&, rBox, void)
686 bool bCheck = rBox.get_active() && rBox.get_sensitive();
687 m_xLeftFT->set_sensitive(!bCheck);
688 m_xLeftMF->set_sensitive(!bCheck);
689 ChangeAddressHdl_Impl( *m_xLeftMF );
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */