Update ooo320-m1
[ooovba.git] / sw / source / ui / dbui / mmlayoutpage.cxx
blobea475f8bb66c56ea3188fd5c3b67402f12aabea8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mmlayoutpage.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
35 #endif
36 #include <swtypes.hxx>
37 #include <mmlayoutpage.hxx>
38 #include <mailmergewizard.hxx>
39 #include <mmconfigitem.hxx>
40 #include <mailmergehelper.hxx>
41 #include <unotools.hxx>
42 #include <unotools/tempfile.hxx>
43 #include <uitool.hxx>
44 #include <svx/dlgutil.hxx>
45 #ifndef _VIEW_HXX
46 #include <view.hxx>
47 #endif
48 #include <swundo.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include <svtools/stritem.hxx>
51 #include <sfx2/docfilt.hxx>
52 #include <com/sun/star/text/XParagraphCursor.hpp>
53 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
54 #include <com/sun/star/view/DocumentZoomType.hpp>
55 #include <fldmgr.hxx>
56 #include <fldbas.hxx>
57 #include <poolfmt.hxx>
58 #include <unotxdoc.hxx>
59 #ifndef _DOCSH_HXX
60 #include <docsh.hxx>
61 #endif
62 #include <doc.hxx>
63 #include <wrtsh.hxx>
64 #include <fmtsrnd.hxx>
65 #include <pagedesc.hxx>
66 #include <fmtanchr.hxx>
67 #include <fmtornt.hxx>
68 #include <fmtfsize.hxx>
69 #include <svx/boxitem.hxx>
70 #include <svtools/urihelper.hxx>
71 #include <shellio.hxx>
72 #include <osl/file.hxx>
73 #include <unoprnms.hxx>
75 #include <mmlayoutpage.hrc>
76 #include <dbui.hrc>
77 #include <unomid.h>
79 using namespace osl;
80 using namespace svt;
81 using namespace ::com::sun::star;
82 using namespace ::com::sun::star::uno;
83 using namespace ::com::sun::star::text;
84 using namespace ::com::sun::star::frame;
85 using namespace ::com::sun::star::lang;
86 using namespace ::com::sun::star::view;
88 #define DEFAULT_LEFT_DISTANCE (MM50*5) // 2,5 cm
89 #define DEFAULT_TOP_DISTANCE (MM50*11) // 5,5 cm
90 #define GREETING_TOP_DISTANCE (MM50*25) //12,5 cm
91 #define DEFAULT_ADDRESS_WIDTH (MM50*15)// 7,5 cm
92 #define DEFAULT_ADDRESS_HEIGHT (MM50*7) // 3,5cm
94 /*-- 15.04.2004 08:16:35---------------------------------------------------
96 -----------------------------------------------------------------------*/
97 SwMailMergeLayoutPage::SwMailMergeLayoutPage( SwMailMergeWizard* _pParent) :
98 svt::OWizardPage( _pParent, SW_RES(DLG_MM_LAYOUT_PAGE)),
99 #ifdef MSC
100 #pragma warning (disable : 4355)
101 #endif
102 m_aHeaderFI( this, SW_RES( FI_HEADER )),
103 m_aPositionFL( this, SW_RES( FL_POSITION )),
104 m_aAlignToBodyCB( this, SW_RES( CB_ALIGN )),
105 m_aLeftFT( this, SW_RES( FT_LEFT )),
106 m_aLeftMF( this, SW_RES( MF_LEFT )),
107 m_aTopFT( this, SW_RES( FT_TOP )),
108 m_aTopMF( this, SW_RES( MF_TOP )),
109 m_aGreetingLineFL( this, SW_RES( FL_GREETINGLINE )),
110 m_aUpFT( this, SW_RES( FT_UP )),
111 m_aUpPB( this, SW_RES( MF_UP )),
112 m_aDownFT( this, SW_RES( FT_DOWN )),
113 m_aDownPB( this, SW_RES( PB_DOWN )),
114 m_aExampleContainerWIN( this, SW_RES( WIN_EXAMPLECONTAINER )),
115 m_aExampleWIN( this, 0 ),
116 m_aZoomFT( this, SW_RES( FT_ZOOM )),
117 m_aZoomLB( this, SW_RES( LB_ZOOM )),
118 #ifdef MSC
119 #pragma warning (default : 4355)
120 #endif
121 m_pExampleFrame(0),
122 m_pExampleWrtShell(0),
123 m_pAddressBlockFormat(0),
124 m_bIsGreetingInserted(false),
125 m_pWizard(_pParent)
127 FreeResource();
128 m_aExampleWIN.SetPosSizePixel(m_aExampleContainerWIN.GetPosPixel(),
129 m_aExampleContainerWIN.GetSizePixel());
132 const SfxFilter *pSfxFlt = SwIoSystem::GetFilterOfFormat(
133 String::CreateFromAscii( FILTER_XML ),
134 SwDocShell::Factory().GetFilterContainer() );
135 //save the current document into a temporary file
137 //temp file needs it's own block
138 //creating with extension is not supported by a static method :-(
139 String sLeading;
140 String sExt(pSfxFlt->GetDefaultExtension());
141 sExt.EraseLeadingChars('*');
142 utl::TempFile aTempFile( sLeading, &sExt );
143 m_sExampleURL = aTempFile.GetURL();
144 aTempFile.EnableKillingFile();
146 SwView* pView = m_pWizard->GetSwView();
147 uno::Sequence< beans::PropertyValue > aValues(1);
148 beans::PropertyValue* pValues = aValues.getArray();
149 pValues[0].Name = C2U("FilterName");
150 pValues[0].Value <<= ::rtl::OUString(pSfxFlt->GetFilterName());
152 uno::Reference< frame::XStorable > xStore( pView->GetDocShell()->GetModel(), uno::UNO_QUERY);
153 xStore->storeToURL( m_sExampleURL, aValues );
155 Link aLink(LINK(this, SwMailMergeLayoutPage, PreviewLoadedHdl_Impl));
156 m_pExampleFrame = new SwOneExampleFrame( m_aExampleWIN,
157 EX_SHOW_DEFAULT_PAGE, &aLink, &m_sExampleURL );
159 m_aExampleWIN.Show( FALSE );
160 m_aExampleContainerWIN.Show(TRUE);
162 m_aLeftMF.SetValue(m_aLeftMF.Normalize(DEFAULT_LEFT_DISTANCE), FUNIT_TWIP);
163 m_aTopMF.SetValue(m_aTopMF.Normalize(DEFAULT_TOP_DISTANCE), FUNIT_TWIP);
165 m_aZoomLB.InsertEntry(String::CreateFromAscii("50 %"), 1);
166 m_aZoomLB.InsertEntry(String::CreateFromAscii("75 %"), 2);
167 m_aZoomLB.InsertEntry(String::CreateFromAscii("100 %"), 3);
168 m_aZoomLB.SelectEntryPos(0); //page size
169 m_aZoomLB.SetSelectHdl(LINK(this, SwMailMergeLayoutPage, ZoomHdl_Impl));
171 Link aFrameHdl = LINK(this, SwMailMergeLayoutPage, ChangeAddressHdl_Impl);
172 m_aLeftMF.SetUpHdl(aFrameHdl);
173 m_aLeftMF.SetDownHdl(aFrameHdl);
174 m_aLeftMF.SetLoseFocusHdl(aFrameHdl);
175 m_aTopMF.SetUpHdl(aFrameHdl);
176 m_aTopMF.SetDownHdl(aFrameHdl);
177 m_aTopMF.SetLoseFocusHdl(aFrameHdl);
179 FieldUnit eFieldUnit = ::GetDfltMetric(sal_False);
180 ::SetFieldUnit( m_aLeftMF, eFieldUnit );
181 ::SetFieldUnit( m_aTopMF, eFieldUnit );
183 Link aUpDownHdl = LINK(this, SwMailMergeLayoutPage, GreetingsHdl_Impl );
184 m_aUpPB.SetClickHdl(aUpDownHdl);
185 m_aDownPB.SetClickHdl(aUpDownHdl);
186 m_aAlignToBodyCB.SetClickHdl(LINK(this, SwMailMergeLayoutPage, AlignToTextHdl_Impl));
187 m_aAlignToBodyCB.Check();
189 /*-- 15.04.2004 08:17:11---------------------------------------------------
191 -----------------------------------------------------------------------*/
192 SwMailMergeLayoutPage::~SwMailMergeLayoutPage()
194 delete m_pExampleFrame;
195 File::remove( m_sExampleURL );
198 /*-- 27.05.2004 13:41:04---------------------------------------------------
200 -----------------------------------------------------------------------*/
201 void SwMailMergeLayoutPage::ActivatePage()
203 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
204 sal_Bool bGreetingLine = rConfigItem.IsGreetingLine(sal_False) && !rConfigItem.IsGreetingInserted();
205 sal_Bool bAddressBlock = rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted();
207 m_aPositionFL.Enable(bAddressBlock);
208 m_aLeftFT.Enable(bAddressBlock);
209 m_aTopFT.Enable(bAddressBlock);
210 m_aLeftMF.Enable(bAddressBlock);
211 m_aTopMF.Enable(bAddressBlock);
212 AlignToTextHdl_Impl( &m_aAlignToBodyCB );
214 m_aGreetingLineFL.Enable(bGreetingLine);
215 m_aUpPB.Enable(bGreetingLine);
216 m_aDownPB.Enable(bGreetingLine);
217 m_aUpFT.Enable(bGreetingLine);
218 m_aDownFT.Enable(bGreetingLine);
220 //check if greeting and/or address frame have to be inserted/removed
221 if(m_pExampleWrtShell) // initially there's nothing to check
223 if(!rConfigItem.IsGreetingInserted() &&
224 m_bIsGreetingInserted != (0 != bGreetingLine) )
226 if( m_bIsGreetingInserted )
228 m_pExampleWrtShell->DelFullPara();
229 m_bIsGreetingInserted = false;
231 else
233 InsertGreeting(*m_pExampleWrtShell, m_pWizard->GetConfigItem(), true);
234 m_bIsGreetingInserted = true;
237 if(!rConfigItem.IsAddressInserted() &&
238 rConfigItem.IsAddressBlock() != ( 0 != m_pAddressBlockFormat ))
240 if( m_pAddressBlockFormat )
242 m_pExampleWrtShell->Push();
243 m_pExampleWrtShell->GotoFly( m_pAddressBlockFormat->GetName() );
244 m_pExampleWrtShell->DelRight();
245 m_pAddressBlockFormat = 0;
246 m_pExampleWrtShell->Pop(FALSE);
248 else
250 long nLeft = static_cast< long >(m_aLeftMF.Denormalize(m_aLeftMF.GetValue(FUNIT_TWIP)));
251 long nTop = static_cast< long >(m_aTopMF.Denormalize(m_aTopMF.GetValue(FUNIT_TWIP)));
252 m_pAddressBlockFormat = InsertAddressFrame(
253 *m_pExampleWrtShell, m_pWizard->GetConfigItem(),
254 Point(nLeft, nTop),
255 m_aAlignToBodyCB.IsChecked(), true);
261 /*-- 11.05.2004 10:41:26---------------------------------------------------
263 -----------------------------------------------------------------------*/
264 sal_Bool SwMailMergeLayoutPage::commitPage( CommitPageReason _eReason )
266 //now insert the frame and the greeting
267 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
268 if(eTravelForward == _eReason)
270 long nLeft = static_cast< long >(m_aLeftMF.Denormalize(m_aLeftMF.GetValue(FUNIT_TWIP)));
271 long nTop = static_cast< long >(m_aTopMF.Denormalize(m_aTopMF.GetValue(FUNIT_TWIP)));
272 InsertAddressAndGreeting(
273 m_pWizard->GetSwView(),
274 rConfigItem,
275 Point(nLeft, nTop),
276 m_aAlignToBodyCB.IsChecked());
278 return sal_True;
280 /*-- 24.06.2004 09:50:26---------------------------------------------------
282 -----------------------------------------------------------------------*/
283 SwFrmFmt* SwMailMergeLayoutPage::InsertAddressAndGreeting(SwView* pView,
284 SwMailMergeConfigItem& rConfigItem,
285 const Point& rAddressPosition,
286 bool bAlignToBody)
288 SwFrmFmt* pAddressBlockFormat = 0;
289 pView->GetWrtShell().StartUndo(UNDO_INSERT);
290 if(rConfigItem.IsAddressBlock() && !rConfigItem.IsAddressInserted())
292 //insert the frame
293 Point aAddressPosition(DEFAULT_LEFT_DISTANCE, DEFAULT_TOP_DISTANCE);
294 if(rAddressPosition.X() > 0 && rAddressPosition.Y() > 0)
295 aAddressPosition = rAddressPosition;
296 pAddressBlockFormat = InsertAddressFrame( pView->GetWrtShell(),
297 rConfigItem,
298 aAddressPosition, bAlignToBody, false);
299 rConfigItem.SetAddressInserted(pAddressBlockFormat->GetName());
301 //now the greeting
302 if(rConfigItem.IsGreetingLine(sal_False) && !rConfigItem.IsGreetingInserted())
304 InsertGreeting( pView->GetWrtShell(), rConfigItem, false);
305 rConfigItem.SetGreetingInserted();
307 pView->GetWrtShell().EndUndo(UNDO_INSERT);
308 return pAddressBlockFormat;
310 /*-- 11.05.2004 12:49:04---------------------------------------------------
312 -----------------------------------------------------------------------*/
313 SwFrmFmt* SwMailMergeLayoutPage::InsertAddressFrame(
314 SwWrtShell& rShell,
315 SwMailMergeConfigItem& rConfigItem,
316 const Point& rDestination,
317 bool bAlignLeft,
318 bool bExample)
320 // insert the address block and the greeting line
321 SfxItemSet aSet(rShell.GetAttrPool(), RES_ANCHOR, RES_ANCHOR,
322 RES_VERT_ORIENT, RES_VERT_ORIENT,
323 RES_HORI_ORIENT, RES_HORI_ORIENT,
324 RES_BOX, RES_BOX,
325 RES_FRM_SIZE, RES_FRM_SIZE,
326 RES_SURROUND, RES_SURROUND,
327 0 );
328 aSet.Put(SwFmtAnchor(FLY_PAGE, 1));
329 if(bAlignLeft)
330 aSet.Put(SwFmtHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
331 else
332 aSet.Put(SwFmtHoriOrient( rDestination.X(), text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
333 aSet.Put(SwFmtVertOrient( rDestination.Y(), text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
334 aSet.Put(SwFmtFrmSize( ATT_MIN_SIZE, DEFAULT_ADDRESS_WIDTH, DEFAULT_ADDRESS_HEIGHT ));
335 // the example gets a border around the frame, the real document doesn't get one
336 if(!bExample)
337 aSet.Put(SvxBoxItem( RES_BOX ));
338 aSet.Put(SwFmtSurround( SURROUND_NONE ));
340 rShell.NewFlyFrm(aSet, sal_True );
341 SwFrmFmt* pRet = rShell.GetFlyFrmFmt();
342 ASSERT( pRet, "Fly not inserted" );
344 rShell.UnSelectFrm();
345 const Sequence< ::rtl::OUString> aBlocks = rConfigItem.GetAddressBlocks();
346 if(bExample)
348 rShell.Insert(aBlocks[0]);
350 else
352 //the placeholders should be replaced by the appropriate fields
353 SwFldMgr aFldMgr(&rShell);
354 //create a database string source.command.commandtype.column
355 const SwDBData& rData = rConfigItem.GetCurrentDBData();
356 String sDBName(rData.sDataSource);
357 sDBName += DB_DELIM;
358 sDBName += String(rData.sCommand);
359 sDBName += DB_DELIM;
360 String sDatabaseConditionPrefix(sDBName);
361 sDatabaseConditionPrefix.SearchAndReplaceAll(DB_DELIM, '.');
362 sDBName += String::CreateFromInt32(rData.nCommandType);
363 sDBName += DB_DELIM;
365 // if only the country is in an address line the
366 // paragraph has to be hidden depending on the
367 // IsIncludeCountry()/GetExcludeCountry() settings
369 sal_Bool bIncludeCountry = rConfigItem.IsIncludeCountry();
370 sal_Bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
371 const ::rtl::OUString rExcludeCountry = rConfigItem.GetExcludeCountry();
372 bool bSpecialReplacementForCountry = (!bIncludeCountry || rExcludeCountry.getLength());
374 const ResStringArray& rHeaders = rConfigItem.GetDefaultAddressHeaders();
375 String sCountryColumn = rHeaders.GetString(MM_PART_COUNTRY);
376 Sequence< ::rtl::OUString> aAssignment =
377 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
378 const ::rtl::OUString* pAssignment = aAssignment.getConstArray();
379 if(aAssignment.getLength() > MM_PART_COUNTRY && aAssignment[MM_PART_COUNTRY].getLength())
380 sCountryColumn = aAssignment[MM_PART_COUNTRY];
382 String sHideParagraphsExpression;
383 SwAddressIterator aIter(aBlocks[0]);
384 while(aIter.HasMore())
386 SwMergeAddressItem aItem = aIter.Next();
387 if(aItem.bIsColumn)
389 String sConvertedColumn = aItem.sText;
390 for(USHORT nColumn = 0;
391 nColumn < rHeaders.Count() && nColumn < aAssignment.getLength();
392 ++nColumn)
394 if(rHeaders.GetString(nColumn) == aItem.sText &&
395 pAssignment[nColumn].getLength())
397 sConvertedColumn = pAssignment[nColumn];
398 break;
401 String sDB(sDBName);
402 sDB += sConvertedColumn;
404 if(sHideParagraphsExpression.Len())
405 sHideParagraphsExpression.AppendAscii(" AND ");
406 sHideParagraphsExpression += '!';
407 sHideParagraphsExpression += '[';
408 sHideParagraphsExpression += sDatabaseConditionPrefix;
409 sHideParagraphsExpression += sConvertedColumn;
410 sHideParagraphsExpression += ']';
412 if( bSpecialReplacementForCountry && sCountryColumn == sConvertedColumn )
414 // now insert a hidden paragraph field
415 String sExpression;
416 if( rExcludeCountry.getLength() )
418 sExpression = sDatabaseConditionPrefix;
419 sExpression.Insert('[', 0);
420 sExpression += sCountryColumn;
421 sExpression.AppendAscii("]");
423 String sCondition(sExpression);
424 sCondition.AppendAscii(" != \"");
425 sCondition += String(rExcludeCountry);
426 sCondition += '\"';
428 SwInsertFld_Data aData(TYP_CONDTXTFLD, 0, sCondition, sExpression, 0, &rShell );
429 aFldMgr.InsertFld( aData );
431 else
433 SwInsertFld_Data aData(TYP_HIDDENPARAFLD, 0, sExpression, aEmptyStr, 0, &rShell );
434 aFldMgr.InsertFld( aData );
437 else
439 SwInsertFld_Data aData(TYP_DBFLD, 0, sDB, aEmptyStr, 0, &rShell );
440 aFldMgr.InsertFld( aData );
443 else if(!aItem.bIsReturn)
445 rShell.Insert(aItem.sText);
447 else
449 if(bHideEmptyParagraphs)
451 SwInsertFld_Data aData(TYP_HIDDENPARAFLD, 0, sHideParagraphsExpression, aEmptyStr, 0, &rShell );
452 aFldMgr.InsertFld( aData );
454 sHideParagraphsExpression.Erase();
455 //now add a new paragraph
456 rShell.SplitNode();
459 if(bHideEmptyParagraphs && sHideParagraphsExpression.Len())
461 SwInsertFld_Data aData(TYP_HIDDENPARAFLD, 0, sHideParagraphsExpression, aEmptyStr, 0, &rShell );
462 aFldMgr.InsertFld( aData );
465 return pRet;
468 /*-- 12.05.2004 12:20:19---------------------------------------------------
470 -----------------------------------------------------------------------*/
471 void SwMailMergeLayoutPage::InsertGreeting(SwWrtShell& rShell, SwMailMergeConfigItem& rConfigItem, bool bExample)
473 //set the cursor to the desired position - if no text content is here then
474 //new paragraphs are inserted
475 const SwRect& rPageRect = rShell.GetAnyCurRect(RECT_PAGE);
476 const Point aGreetingPos( DEFAULT_LEFT_DISTANCE + rPageRect.Left(), GREETING_TOP_DISTANCE );
478 const sal_Bool bRet = rShell.SetShadowCrsrPos( aGreetingPos, FILL_SPACE );
480 if(!bRet)
482 //there's already text at the desired position
483 //go to start of the doc, directly!
484 rShell.SttEndDoc(TRUE);
485 //and go by paragraph until the position is reached
486 long nYPos = rShell.GetCharRect().Top();
487 while(nYPos < GREETING_TOP_DISTANCE)
489 if(!rShell.FwdPara())
490 break;
491 nYPos = rShell.GetCharRect().Top();
493 //text needs to be appended
494 while(nYPos < GREETING_TOP_DISTANCE)
496 if(!rShell.AppendTxtNode())
497 break;
498 nYPos = rShell.GetCharRect().Top();
501 else
503 //we may end up inside of a paragraph if the left margin is not at DEFAULT_LEFT_DISTANCE
504 rShell.MovePara(GetfnParaCurr(), GetfnParaStart());
506 bool bSplitNode = rShell.GetText().Len() > 0;
507 // rShell.SetTxtFmtColl( rShell.GetTxtCollFromPool( RES_POOLCOLL_GREETING ) );
508 sal_Int32 nMoves = rConfigItem.GetGreetingMoves();
509 if( !bExample && 0 != nMoves )
511 if(nMoves < 0)
513 rShell.MoveParagraph( nMoves );
515 else
516 while(nMoves)
518 sal_Bool bMoved = rShell.MoveParagraph( 1 );
519 if(!bMoved)
521 //insert a new paragraph before the greeting line
522 rShell.SplitNode();
524 --nMoves;
527 //now insert the greeting text - if we have any?
528 const sal_Bool bIndividual = rConfigItem.IsIndividualGreeting(sal_False);
529 String sGreeting;
530 if(bIndividual)
532 //lock expression fields - prevents hiding of the paragraph to insert into
533 rShell.LockExpFlds();
534 if(bExample)
536 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
537 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
539 Sequence< ::rtl::OUString > aEntries =
540 rConfigItem.GetGreetings((SwMailMergeConfigItem::Gender)eGender);
541 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting((SwMailMergeConfigItem::Gender)eGender);
542 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
544 sGreeting = aEntries[nCurrent];
545 rShell.Insert(sGreeting);
546 break;
550 else
552 SwFldMgr aFldMgr(&rShell);
553 //three paragraphs, each with an appropriate hidden paragraph field
554 //are to be inserted
556 //name of the gender column
557 String sGenderColumn = rConfigItem.GetAssignedColumn(MM_PART_GENDER);
558 String sNameColumn = rConfigItem.GetAssignedColumn(MM_PART_LASTNAME);
560 const ::rtl::OUString& rFemaleGenderValue = rConfigItem.GetFemaleGenderValue();
561 sal_Bool bHideEmptyParagraphs = rConfigItem.IsHideEmptyParagraphs();
562 const SwDBData& rData = rConfigItem.GetCurrentDBData();
563 String sConditionBase(rData.sDataSource);
564 sConditionBase += '.';
565 sConditionBase += String(rData.sCommand);
566 sConditionBase += '.';
567 //split the name column from here
568 String sNameColumnBase(sConditionBase);
570 sConditionBase += String(sGenderColumn);
571 sConditionBase += ']';
572 sConditionBase.Insert('[', 0);
574 sNameColumnBase += String(sNameColumn);
575 sNameColumnBase += ']';
576 sNameColumnBase.Insert('[', 0);
578 String sDBName(rData.sDataSource);
579 sDBName += DB_DELIM;
580 sDBName += String(rData.sCommand);
581 sDBName += DB_DELIM;
582 sDBName += String::CreateFromInt32(rData.nCommandType);
583 sDBName += DB_DELIM;
585 // Female: [database.sGenderColumn] != "rFemaleGenderValue" && [database.NameColumn]
586 // Male: [database.sGenderColumn] == "rFemaleGenderValue" && [database.rGenderColumn]
587 // Neutral: [database.sNameColumn]
588 DBG_ASSERT(sGenderColumn.Len() && rFemaleGenderValue.getLength(),
589 "gender settings not available - how to form the condition?");
590 //column used as lastname
591 for(sal_Int8 eGender = SwMailMergeConfigItem::FEMALE;
592 eGender <= SwMailMergeConfigItem::NEUTRAL; ++eGender)
594 Sequence< ::rtl::OUString> aEntries = rConfigItem.GetGreetings((SwMailMergeConfigItem::Gender)eGender);
595 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting((SwMailMergeConfigItem::Gender)eGender);
596 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
598 sGreeting = aEntries[nCurrent];
599 String sCondition(sConditionBase);
600 String sHideParagraphsExpression;
601 switch(eGender)
603 case SwMailMergeConfigItem::FEMALE:
604 sCondition.AppendAscii(" != \"");
605 sCondition += String(rFemaleGenderValue);
606 sCondition.AppendAscii("\" OR NOT ");
607 sCondition += String(sNameColumnBase);
609 sHideParagraphsExpression += '!';
610 sHideParagraphsExpression += sNameColumnBase;
611 break;
612 case SwMailMergeConfigItem::MALE:
613 sCondition.AppendAscii(" == \"");
614 sCondition += String(rFemaleGenderValue);
615 sCondition.AppendAscii("\" OR NOT ");
616 sCondition += String(sNameColumnBase);
617 break;
618 case SwMailMergeConfigItem::NEUTRAL:
619 sCondition = sNameColumnBase;
620 break;
623 if(bHideEmptyParagraphs && sHideParagraphsExpression.Len())
625 String sComplete( sCondition );
626 sComplete.Insert('(', 0);
627 sComplete.AppendAscii( ") OR (");
628 sComplete += sHideParagraphsExpression;
629 sComplete += ')';
630 SwInsertFld_Data aData(TYP_HIDDENPARAFLD, 0, sComplete, aEmptyStr, 0, &rShell );
631 aFldMgr.InsertFld( aData );
633 else
635 SwInsertFld_Data aData(TYP_HIDDENPARAFLD, 0, sCondition, aEmptyStr, 0, &rShell );
636 aFldMgr.InsertFld( aData );
638 //now the text has to be inserted
639 const ResStringArray& rHeaders = rConfigItem.GetDefaultAddressHeaders();
640 Sequence< ::rtl::OUString> aAssignment =
641 rConfigItem.GetColumnAssignment( rConfigItem.GetCurrentDBData() );
642 const ::rtl::OUString* pAssignment = aAssignment.getConstArray();
643 SwAddressIterator aIter(sGreeting);
644 while(aIter.HasMore())
646 SwMergeAddressItem aItem = aIter.Next();
647 if(aItem.bIsColumn)
649 String sDB(sDBName);
650 String sConvertedColumn = aItem.sText;
651 for(USHORT nColumn = 0;
652 nColumn < rHeaders.Count() && nColumn < aAssignment.getLength();
653 ++nColumn)
655 if(rHeaders.GetString(nColumn) == aItem.sText &&
656 pAssignment[nColumn].getLength())
658 sConvertedColumn = pAssignment[nColumn];
659 break;
662 sDB += sConvertedColumn;
663 SwInsertFld_Data aData(TYP_DBFLD, 0, sDB, aEmptyStr, 0, &rShell );
664 aFldMgr.InsertFld( aData );
666 else
668 rShell.Insert(aItem.sText);
671 //now add a new paragraph
672 rShell.SplitNode();
677 rShell.UnlockExpFlds();
679 else
681 Sequence< ::rtl::OUString> aEntries = rConfigItem.GetGreetings(SwMailMergeConfigItem::NEUTRAL);
682 sal_Int32 nCurrent = rConfigItem.GetCurrentGreeting(SwMailMergeConfigItem::NEUTRAL);
683 if( nCurrent >= 0 && nCurrent < aEntries.getLength())
684 sGreeting = aEntries[nCurrent];
685 rShell.Insert(sGreeting);
687 // now insert a new paragraph here if necessary
688 if(bSplitNode)
690 rShell.Push();
691 rShell.SplitNode();
692 rShell.Pop(FALSE);
694 //put the cursor to the start of the paragraph
695 rShell.SttPara();
697 DBG_ASSERT(0 == rShell.GetTableFmt(), "What to do with a table here?");
699 /*-- 10.05.2004 09:34:25---------------------------------------------------
701 -----------------------------------------------------------------------*/
702 IMPL_LINK(SwMailMergeLayoutPage, PreviewLoadedHdl_Impl, void*, EMPTYARG)
704 m_aExampleWIN.Show( TRUE );
705 m_aExampleContainerWIN.Show(FALSE);
707 Reference< XModel > & xModel = m_pExampleFrame->GetModel();
708 //now the ViewOptions should be set properly
709 Reference< XViewSettingsSupplier > xSettings(xModel->getCurrentController(), UNO_QUERY);
710 m_xViewProperties = xSettings->getViewSettings();
711 Reference< XUnoTunnel > xDocTunnel(xModel, UNO_QUERY);
712 SwXTextDocument* pXDoc = reinterpret_cast<SwXTextDocument*>(xDocTunnel->getSomething(SwXTextDocument::getUnoTunnelId()));
713 SwDocShell* pDocShell = pXDoc->GetDocShell();
714 m_pExampleWrtShell = pDocShell->GetWrtShell();
715 DBG_ASSERT(m_pExampleWrtShell, "No SwWrtShell found!");
716 if(!m_pExampleWrtShell)
717 return 0;
719 SwMailMergeConfigItem& rConfigItem = m_pWizard->GetConfigItem();
720 if(rConfigItem.IsAddressBlock())
722 m_pAddressBlockFormat = InsertAddressFrame(
723 *m_pExampleWrtShell, rConfigItem,
724 Point(DEFAULT_LEFT_DISTANCE, DEFAULT_TOP_DISTANCE),
725 m_aAlignToBodyCB.IsChecked(), true);
727 if(rConfigItem.IsGreetingLine(sal_False))
729 InsertGreeting(*m_pExampleWrtShell, rConfigItem, true);
730 m_bIsGreetingInserted = true;
733 Any aZoom;
734 aZoom <<= (sal_Int16)DocumentZoomType::ENTIRE_PAGE;
735 m_xViewProperties->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE)), aZoom);
738 // m_pExampleWrtShell->SetTxtFmtColl( rSh.GetTxtCollFromPool( RES_POOLCOLL_STANDARD ) );
739 const SwFmtFrmSize& rPageSize = m_pExampleWrtShell->GetPageDesc(
740 m_pExampleWrtShell->GetCurPageDesc()).GetMaster().GetFrmSize();
741 m_aLeftMF.SetMax(rPageSize.GetWidth() - DEFAULT_LEFT_DISTANCE);
742 m_aTopMF.SetMax(rPageSize.GetHeight() - DEFAULT_TOP_DISTANCE);
743 return 0;
745 /*-- 10.05.2004 14:05:24---------------------------------------------------
747 -----------------------------------------------------------------------*/
748 IMPL_LINK(SwMailMergeLayoutPage, ZoomHdl_Impl, ListBox*, pBox)
750 if(m_pExampleWrtShell)
752 sal_Int16 eType = DocumentZoomType::BY_VALUE;
753 short nZoom = 50;
754 switch(pBox->GetSelectEntryPos())
756 case 0 : eType = DocumentZoomType::ENTIRE_PAGE; break;
757 case 1 : nZoom = 50; break;
758 case 2 : nZoom = 75; break;
759 case 3 : nZoom = 100; break;
761 Any aZoom;
762 aZoom <<= eType;
763 m_xViewProperties->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE)), aZoom);
764 aZoom <<= nZoom;
765 m_xViewProperties->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE)), aZoom);
768 return 0;
772 /*-- 10.05.2004 15:56:51---------------------------------------------------
774 -----------------------------------------------------------------------*/
775 IMPL_LINK(SwMailMergeLayoutPage, ChangeAddressHdl_Impl, MetricField*, EMPTYARG)
777 if(m_pExampleWrtShell && m_pAddressBlockFormat)
779 long nLeft = static_cast< long >(m_aLeftMF.Denormalize(m_aLeftMF.GetValue(FUNIT_TWIP)));
780 long nTop = static_cast< long >(m_aTopMF.Denormalize(m_aTopMF.GetValue(FUNIT_TWIP)));
782 SfxItemSet aSet(m_pExampleWrtShell->GetAttrPool(), RES_ANCHOR, RES_ANCHOR,
783 RES_VERT_ORIENT, RES_VERT_ORIENT,
784 RES_HORI_ORIENT, RES_HORI_ORIENT,
785 0 );
786 if(m_aAlignToBodyCB.IsChecked())
787 aSet.Put(SwFmtHoriOrient( 0, text::HoriOrientation::NONE, text::RelOrientation::PAGE_PRINT_AREA ));
788 else
789 aSet.Put(SwFmtHoriOrient( nLeft, text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
790 aSet.Put(SwFmtVertOrient( nTop, text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
791 m_pExampleWrtShell->GetDoc()->SetFlyFrmAttr( *m_pAddressBlockFormat, aSet );
793 return 0;
796 /*-- 10.05.2004 16:13:36---------------------------------------------------
798 -----------------------------------------------------------------------*/
799 IMPL_LINK(SwMailMergeLayoutPage, GreetingsHdl_Impl, PushButton*, pButton)
801 bool bDown = pButton == &m_aDownPB;
802 BOOL bMoved = m_pExampleWrtShell->MoveParagraph( bDown ? 1 : -1 );
803 if (bMoved || bDown)
804 m_pWizard->GetConfigItem().MoveGreeting(bDown ? 1 : -1 );
805 if(!bMoved && bDown)
807 //insert a new paragraph before the greeting line
808 m_pExampleWrtShell->SplitNode();
811 return 0;
813 /*-- 15.07.2004 16:05:30---------------------------------------------------
815 -----------------------------------------------------------------------*/
816 IMPL_LINK(SwMailMergeLayoutPage, AlignToTextHdl_Impl, CheckBox*, pBox)
818 sal_Bool bCheck = pBox->IsChecked() && pBox->IsEnabled();
819 m_aLeftFT.Enable(!bCheck);
820 m_aLeftMF.Enable(!bCheck);
821 ChangeAddressHdl_Impl( 0 );
822 return 0;