nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / ui / dbui / mmlayoutpage.cxx
blob2793ddfe0a28dd6c5a29d8d68e360694ab74277f
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 <i18nutil/unicode.hxx>
28 #include <unotools/tempfile.hxx>
29 #include <uitool.hxx>
30 #include <view.hxx>
31 #include <swundo.hxx>
32 #include <sfx2/docfilt.hxx>
33 #include <sfx2/fcontnr.hxx>
34 #include <svtools/unitconv.hxx>
35 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
36 #include <com/sun/star/view/DocumentZoomType.hpp>
37 #include <fldmgr.hxx>
38 #include <fldbas.hxx>
39 #include <unotxdoc.hxx>
40 #include <docsh.hxx>
41 #include <doc.hxx>
42 #include <wrtsh.hxx>
43 #include <fmtsrnd.hxx>
44 #include <pagedesc.hxx>
45 #include <fmtanchr.hxx>
46 #include <fmtornt.hxx>
47 #include <fmtfsize.hxx>
48 #include <editeng/boxitem.hxx>
49 #include <osl/file.hxx>
50 #include <vcl/settings.hxx>
51 #include <unoprnms.hxx>
52 #include <iodetect.hxx>
54 #include <dbui.hrc>
56 using namespace osl;
57 using namespace svt;
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::text;
61 using namespace ::com::sun::star::frame;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::view;
65 #define DEFAULT_LEFT_DISTANCE (MM50*5) // 2,5 cm
66 #define DEFAULT_TOP_DISTANCE (MM50*11) // 5,5 cm
67 #define GREETING_TOP_DISTANCE (MM50*25) //12,5 cm
68 #define DEFAULT_ADDRESS_WIDTH (MM50*15)// 7,5 cm
69 #define DEFAULT_ADDRESS_HEIGHT (MM50*7) // 3,5cm
71 SwMailMergeLayoutPage::SwMailMergeLayoutPage(weld::Container* pPage, SwMailMergeWizard* pWizard)
72 : vcl::OWizardPage(pPage, pWizard, "modules/swriter/ui/mmlayoutpage.ui", "MMLayoutPage")
73 , m_pExampleWrtShell(nullptr)
74 , m_pAddressBlockFormat(nullptr)
75 , m_bIsGreetingInserted(false)
76 , m_pWizard(pWizard)
77 , m_xPosition(m_xBuilder->weld_container("addresspos"))
78 , m_xAlignToBodyCB(m_xBuilder->weld_check_button("align"))
79 , m_xLeftFT(m_xBuilder->weld_label("leftft"))
80 , m_xLeftMF(m_xBuilder->weld_metric_spin_button("left", FieldUnit::CM))
81 , m_xTopMF(m_xBuilder->weld_metric_spin_button("top", FieldUnit::CM))
82 , m_xGreetingLine(m_xBuilder->weld_container("greetingspos"))
83 , m_xUpPB(m_xBuilder->weld_button("up"))
84 , m_xDownPB(m_xBuilder->weld_button("down"))
85 , m_xZoomLB(m_xBuilder->weld_combo_box("zoom"))
87 std::shared_ptr<const SfxFilter> pSfxFlt =
88 SwDocShell::Factory().GetFilterContainer()->
89 GetFilter4FilterName("writer8", SfxFilterFlags::EXPORT);
91 //save the current document into a temporary file
93 //temp file needs its own block
94 //creating with extension is not supported by a static method :-(
95 OUString const sExt(
96 comphelper::string::stripStart(pSfxFlt->GetDefaultExtension(),'*'));
97 utl::TempFile aTempFile( OUString(), true, &sExt );
98 m_sExampleURL = aTempFile.GetURL();
99 aTempFile.EnableKillingFile();
101 SwView* pView = m_pWizard->GetSwView();
102 uno::Sequence< beans::PropertyValue > aValues(2);
103 beans::PropertyValue* pValues = aValues.getArray();
104 pValues[0].Name = "FilterName";
105 pValues[0].Value <<= pSfxFlt->GetFilterName();
106 // Don't save embedded data set! It would steal it from current document.
107 pValues[1].Name = "NoEmbDataSet";
108 pValues[1].Value <<= 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 SfxItemSet aSet(
260 rShell.GetAttrPool(),
261 svl::Items<
262 RES_FRM_SIZE, RES_FRM_SIZE,
263 RES_SURROUND, RES_ANCHOR,
264 RES_BOX, RES_BOX>{} );
265 aSet.Put(SwFormatAnchor(RndStdIds::FLY_AT_PAGE, 1));
266 if(bAlignLeft)
267 aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
268 else
269 aSet.Put(SwFormatHoriOrient( rDestination.X(), text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
270 aSet.Put(SwFormatVertOrient( rDestination.Y(), text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
271 aSet.Put(SwFormatFrameSize( SwFrameSize::Minimum, DEFAULT_ADDRESS_WIDTH, DEFAULT_ADDRESS_HEIGHT ));
272 // the example gets a border around the frame, the real document doesn't get one
273 if(!bExample)
274 aSet.Put(SvxBoxItem( RES_BOX ));
275 aSet.Put(SwFormatSurround( css::text::WrapTextMode_NONE ));
277 rShell.NewFlyFrame(aSet, true );
278 SwFrameFormat* pRet = rShell.GetFlyFrameFormat();
279 OSL_ENSURE( pRet, "Fly not inserted" );
281 rShell.UnSelectFrame();
282 const Sequence< OUString> aBlocks = rConfigItem.GetAddressBlocks();
283 if(bExample)
285 rShell.Insert(aBlocks[0]);
287 else
289 //the placeholders should be replaced by the appropriate fields
290 SwFieldMgr aFieldMgr(&rShell);
291 //create a database string source.command.commandtype.column
292 const SwDBData& rData = rConfigItem.GetCurrentDBData();
293 OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
294 + rData.sCommand + OUStringChar(DB_DELIM));
295 const OUString sDatabaseConditionPrefix(sDBName.replace(DB_DELIM, '.'));
296 sDBName += OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM);
298 // if only the country is in an address line the
299 // paragraph has to be hidden depending on the
300 // IsIncludeCountry()/GetExcludeCountry() settings
302 bool bIncludeCountry = rConfigItem.IsIncludeCountry();
303 bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
304 const OUString rExcludeCountry = rConfigItem.GetExcludeCountry();
305 bool bSpecialReplacementForCountry = (!bIncludeCountry || !rExcludeCountry.isEmpty());
307 const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
308 Sequence< OUString> aAssignment =
309 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
310 const OUString* pAssignment = aAssignment.getConstArray();
311 const OUString sCountryColumn(
312 (aAssignment.getLength() > MM_PART_COUNTRY && !aAssignment[MM_PART_COUNTRY].isEmpty())
313 ? aAssignment[MM_PART_COUNTRY]
314 : rHeaders[MM_PART_COUNTRY].first);
316 OUString sHideParagraphsExpression;
317 SwAddressIterator aIter(aBlocks[0]);
318 while(aIter.HasMore())
320 SwMergeAddressItem aItem = aIter.Next();
321 if(aItem.bIsColumn)
323 OUString sConvertedColumn = aItem.sText;
324 auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
325 static_cast<sal_uInt32>(aAssignment.getLength()));
326 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
328 if (rHeaders[nColumn].first == aItem.sText &&
329 !pAssignment[nColumn].isEmpty())
331 sConvertedColumn = pAssignment[nColumn];
332 break;
335 const OUString sDB(sDBName + sConvertedColumn);
337 if(!sHideParagraphsExpression.isEmpty())
338 sHideParagraphsExpression += " AND ";
339 sHideParagraphsExpression += "![" + sDatabaseConditionPrefix + sConvertedColumn + "]";
341 if( bSpecialReplacementForCountry && sCountryColumn == sConvertedColumn )
343 // now insert a hidden paragraph field
344 if( !rExcludeCountry.isEmpty() )
346 const OUString sExpression("[" + sDatabaseConditionPrefix + sCountryColumn + "]");
347 SwInsertField_Data aData(SwFieldTypesEnum::ConditionalText, 0,
348 sExpression + " != \"" + rExcludeCountry + "\"",
349 sExpression,
350 0, &rShell );
351 aFieldMgr.InsertField( aData );
353 else
355 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, "", "", 0, &rShell );
356 aFieldMgr.InsertField( aData );
359 else
361 SwInsertField_Data aData(SwFieldTypesEnum::Database, 0, sDB, OUString(), 0, &rShell);
362 aFieldMgr.InsertField( aData );
365 else if(!aItem.bIsReturn)
367 rShell.Insert(aItem.sText);
369 else
371 if(bHideEmptyParagraphs)
373 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
374 aFieldMgr.InsertField( aData );
376 sHideParagraphsExpression.clear();
377 //now add a new paragraph
378 rShell.SplitNode();
381 if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
383 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sHideParagraphsExpression, OUString(), 0, &rShell);
384 aFieldMgr.InsertField( aData );
387 return pRet;
390 void SwMailMergeLayoutPage::InsertGreeting(SwWrtShell& rShell, SwMailMergeConfigItem const & rConfigItem, bool bExample)
392 //set the cursor to the desired position - if no text content is here then
393 //new paragraphs are inserted
394 const SwRect& rPageRect = rShell.GetAnyCurRect(CurRectType::Page);
395 const Point aGreetingPos( DEFAULT_LEFT_DISTANCE + rPageRect.Left(), GREETING_TOP_DISTANCE );
397 const bool bRet = rShell.SetShadowCursorPos( aGreetingPos, SwFillMode::TabSpace );
399 if(!bRet)
401 //there's already text at the desired position
402 //go to start of the doc, directly!
403 rShell.SttEndDoc(true);
404 //and go by paragraph until the position is reached
405 tools::Long nYPos = rShell.GetCharRect().Top();
406 while(nYPos < GREETING_TOP_DISTANCE)
408 if(!rShell.FwdPara())
409 break;
410 nYPos = rShell.GetCharRect().Top();
412 //text needs to be appended
413 while(nYPos < GREETING_TOP_DISTANCE)
415 if(!rShell.AppendTextNode())
416 break;
417 nYPos = rShell.GetCharRect().Top();
420 else
422 //we may end up inside of a paragraph if the left margin is not at DEFAULT_LEFT_DISTANCE
423 rShell.MovePara(GoCurrPara, fnParaStart);
425 bool bSplitNode = !rShell.IsEndPara();
426 sal_Int32 nMoves = rConfigItem.GetGreetingMoves();
427 if( !bExample && 0 != nMoves )
429 if(nMoves < 0)
431 rShell.MoveParagraph( nMoves );
433 else
434 while(nMoves)
436 bool bMoved = rShell.MoveParagraph();
437 if(!bMoved)
439 //insert a new paragraph before the greeting line
440 rShell.SplitNode();
442 --nMoves;
445 //now insert the greeting text - if we have any?
446 const bool bIndividual = rConfigItem.IsIndividualGreeting(false);
447 if(bIndividual)
449 //lock expression fields - prevents hiding of the paragraph to insert into
450 rShell.LockExpFields();
451 if(bExample)
453 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
454 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
456 Sequence< OUString > aEntries =
457 rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
458 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
459 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
461 // Greeting
462 rShell.Insert(aEntries[nCurrent]);
463 break;
467 else
469 SwFieldMgr aFieldMgr(&rShell);
470 //three paragraphs, each with an appropriate hidden paragraph field
471 //are to be inserted
473 //name of the gender column
474 const OUString sGenderColumn = rConfigItem.GetAssignedColumn(MM_PART_GENDER);
475 const OUString sNameColumn = rConfigItem.GetAssignedColumn(MM_PART_LASTNAME);
477 const OUString& rFemaleGenderValue = rConfigItem.GetFemaleGenderValue();
478 bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
479 const SwDBData& rData = rConfigItem.GetCurrentDBData();
480 const OUString sCommonBase(rData.sDataSource + "." + rData.sCommand + ".");
481 const OUString sConditionBase("[" + sCommonBase + sGenderColumn + "]");
482 const OUString sNameColumnBase("[" + sCommonBase + sNameColumn + "]");
484 const OUString sDBName(rData.sDataSource + OUStringChar(DB_DELIM)
485 + rData.sCommand + OUStringChar(DB_DELIM)
486 + OUString::number(rData.nCommandType) + OUStringChar(DB_DELIM));
488 // Female: [database.sGenderColumn] != "rFemaleGenderValue" && [database.NameColumn]
489 // Male: [database.sGenderColumn] == "rFemaleGenderValue" && [database.rGenderColumn]
490 // Neutral: [database.sNameColumn]
491 OSL_ENSURE(!sGenderColumn.isEmpty() && !rFemaleGenderValue.isEmpty(),
492 "gender settings not available - how to form the condition?");
493 //column used as lastname
494 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
495 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
497 Sequence< OUString> aEntries = rConfigItem.GetGreetings(static_cast<SwMailMergeConfigItem::Gender>(eGender));
498 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(static_cast<SwMailMergeConfigItem::Gender>(eGender));
499 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
501 const OUString sGreeting = aEntries[nCurrent];
502 OUString sCondition;
503 OUString sHideParagraphsExpression;
504 switch(eGender)
506 case SwMailMergeConfigItem::FEMALE:
507 sCondition = sConditionBase + " != \"" + rFemaleGenderValue
508 + "\" OR NOT " + sNameColumnBase;
509 sHideParagraphsExpression = "!" + sNameColumnBase;
510 break;
511 case SwMailMergeConfigItem::MALE:
512 sCondition = sConditionBase + " == \"" + rFemaleGenderValue
513 + "\" OR NOT " + sNameColumnBase;
514 break;
515 case SwMailMergeConfigItem::NEUTRAL:
516 sCondition = sNameColumnBase;
517 break;
520 if(bHideEmptyParagraphs && !sHideParagraphsExpression.isEmpty())
522 OUString sComplete = "(" + sCondition + ") OR (" + sHideParagraphsExpression + ")";
523 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sComplete, OUString(), 0, &rShell);
524 aFieldMgr.InsertField( aData );
526 else
528 SwInsertField_Data aData(SwFieldTypesEnum::HiddenParagraph, 0, sCondition, OUString(), 0, &rShell);
529 aFieldMgr.InsertField( aData );
531 //now the text has to be inserted
532 const std::vector<std::pair<OUString, int>>& rHeaders = rConfigItem.GetDefaultAddressHeaders();
533 Sequence< OUString> aAssignment =
534 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
535 const OUString* pAssignment = aAssignment.getConstArray();
536 SwAddressIterator aIter(sGreeting);
537 while(aIter.HasMore())
539 SwMergeAddressItem aItem = aIter.Next();
540 if(aItem.bIsColumn)
542 OUString sConvertedColumn = aItem.sText;
543 auto nSize = std::min(static_cast<sal_uInt32>(rHeaders.size()),
544 static_cast<sal_uInt32>(aAssignment.getLength()));
545 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
547 if (rHeaders[nColumn].first == aItem.sText &&
548 !pAssignment[nColumn].isEmpty())
550 sConvertedColumn = pAssignment[nColumn];
551 break;
554 SwInsertField_Data aData(SwFieldTypesEnum::Database, 0,
555 sDBName + sConvertedColumn,
556 OUString(), 0, &rShell);
557 aFieldMgr.InsertField( aData );
559 else
561 rShell.Insert(aItem.sText);
564 //now add a new paragraph
565 rShell.SplitNode();
570 rShell.UnlockExpFields();
572 else
574 Sequence< OUString> aEntries = rConfigItem.GetGreetings(SwMailMergeConfigItem::NEUTRAL);
575 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(SwMailMergeConfigItem::NEUTRAL);
576 // Greeting
577 rShell.Insert(( nCurrent >= 0 && nCurrent < aEntries.getLength() )
578 ? aEntries[nCurrent] : OUString());
580 // now insert a new paragraph here if necessary
581 if(bSplitNode)
583 rShell.Push();
584 rShell.SplitNode();
585 rShell.Pop(SwCursorShell::PopMode::DeleteCurrent);
587 //put the cursor to the start of the paragraph
588 rShell.SttPara();
590 OSL_ENSURE(nullptr == rShell.GetTableFormat(), "What to do with a table here?");
593 IMPL_LINK_NOARG(SwMailMergeLayoutPage, PreviewLoadedHdl_Impl, SwOneExampleFrame&, void)
595 m_xExampleContainerWIN->show();
597 Reference< XModel > & xModel = m_xExampleFrame->GetModel();
598 //now the ViewOptions should be set properly
599 Reference< XViewSettingsSupplier > xSettings(xModel->getCurrentController(), UNO_QUERY);
600 m_xViewProperties = xSettings->getViewSettings();
601 auto pXDoc = comphelper::getUnoTunnelImplementation<SwXTextDocument>(xModel);
602 SwDocShell* pDocShell = pXDoc->GetDocShell();
603 m_pExampleWrtShell = pDocShell->GetWrtShell();
604 OSL_ENSURE(m_pExampleWrtShell, "No SwWrtShell found!");
605 if(!m_pExampleWrtShell)
606 return;
608 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
609 if(rConfigItem.IsAddressBlock())
611 m_pAddressBlockFormat = InsertAddressFrame(
612 *m_pExampleWrtShell, rConfigItem,
613 Point(DEFAULT_LEFT_DISTANCE, DEFAULT_TOP_DISTANCE),
614 m_xAlignToBodyCB->get_active(), true);
616 if(rConfigItem.IsGreetingLine(false))
618 InsertGreeting(*m_pExampleWrtShell, rConfigItem, true);
619 m_bIsGreetingInserted = true;
622 ZoomHdl_Impl(*m_xZoomLB);
624 const SwFormatFrameSize& rPageSize = m_pExampleWrtShell->GetPageDesc(
625 m_pExampleWrtShell->GetCurPageDesc()).GetMaster().GetFrameSize();
626 m_xLeftMF->set_max(rPageSize.GetWidth() - DEFAULT_LEFT_DISTANCE, FieldUnit::NONE);
627 m_xTopMF->set_max(rPageSize.GetHeight() - DEFAULT_TOP_DISTANCE, FieldUnit::NONE);
630 IMPL_LINK(SwMailMergeLayoutPage, ZoomHdl_Impl, weld::ComboBox&, rBox, void)
632 if (!m_pExampleWrtShell)
633 return;
635 sal_Int16 eType = DocumentZoomType::BY_VALUE;
636 short nZoom = 50;
637 switch (rBox.get_active())
639 case 0 : eType = DocumentZoomType::ENTIRE_PAGE; break;
640 case 1 : nZoom = 50; break;
641 case 2 : nZoom = 75; break;
642 case 3 : nZoom = 100; break;
644 Any aZoom;
645 aZoom <<= eType;
646 m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_TYPE, aZoom);
647 aZoom <<= nZoom;
648 m_xViewProperties->setPropertyValue(UNO_NAME_ZOOM_VALUE, aZoom);
650 m_xExampleFrame->Invalidate();
653 IMPL_LINK_NOARG(SwMailMergeLayoutPage, ChangeAddressHdl_Impl, weld::MetricSpinButton&, void)
655 if(!(m_pExampleWrtShell && m_pAddressBlockFormat))
656 return;
658 tools::Long nLeft = static_cast< tools::Long >(m_xLeftMF->denormalize(m_xLeftMF->get_value(FieldUnit::TWIP)));
659 tools::Long nTop = static_cast< tools::Long >(m_xTopMF->denormalize(m_xTopMF->get_value(FieldUnit::TWIP)));
661 SfxItemSet aSet(
662 m_pExampleWrtShell->GetAttrPool(),
663 svl::Items<RES_VERT_ORIENT, RES_ANCHOR>{});
664 if (m_xAlignToBodyCB->get_active())
665 aSet.Put(SwFormatHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
666 else
667 aSet.Put(SwFormatHoriOrient( nLeft, text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
668 aSet.Put(SwFormatVertOrient( nTop, text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
669 m_pExampleWrtShell->GetDoc()->SetFlyFrameAttr( *m_pAddressBlockFormat, aSet );
670 m_xExampleFrame->Invalidate();
673 IMPL_LINK(SwMailMergeLayoutPage, GreetingsHdl_Impl, weld::Button&, rButton, void)
675 bool bDown = &rButton == m_xDownPB.get();
676 bool bMoved = m_pExampleWrtShell->MoveParagraph( bDown ? 1 : -1 );
677 if (bMoved || bDown)
678 m_pWizard->GetConfigItem().MoveGreeting(bDown ? 1 : -1 );
679 if(!bMoved && bDown)
681 //insert a new paragraph before the greeting line
682 m_pExampleWrtShell->SplitNode();
684 m_xExampleFrame->Invalidate();
687 IMPL_LINK(SwMailMergeLayoutPage, AlignToTextHdl_Impl, weld::ToggleButton&, rBox, void)
689 bool bCheck = rBox.get_active() && rBox.get_sensitive();
690 m_xLeftFT->set_sensitive(!bCheck);
691 m_xLeftMF->set_sensitive(!bCheck);
692 ChangeAddressHdl_Impl( *m_xLeftMF );
695 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */