Update ooo320-m1
[ooovba.git] / sw / source / ui / app / applab.cxx
blob889b131c14eb573692079d85c3353ff2bf5f7103
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: applab.cxx,v $
10 * $Revision: 1.34.46.1 $
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"
35 #if STLPORT_VERSION>=321
36 #include <cstdarg>
37 #endif
40 #include <hintids.hxx>
42 #ifndef _APP_HXX //autogen
43 #include <vcl/svapp.hxx>
44 #endif
45 #ifndef _WRKWIN_HXX //autogen
46 #include <vcl/wrkwin.hxx>
47 #endif
48 #ifndef _MSGBOX_HXX //autogen
49 #include <vcl/msgbox.hxx>
50 #endif
51 #include <sfx2/app.hxx>
52 #include <sfx2/dispatch.hxx>
53 #include <sfx2/printer.hxx>
54 #include <sfx2/request.hxx>
55 #include <sfx2/linkmgr.hxx>
56 #include <svx/pbinitem.hxx>
57 #include <svx/ulspitem.hxx>
58 #include <svx/lrspitem.hxx>
59 #include <svx/boxitem.hxx>
60 #include <svx/paperinf.hxx>
61 #include <svx/protitem.hxx>
62 #include <com/sun/star/frame/XStorable.hpp>
63 #include <com/sun/star/frame/XModel.hpp>
64 #include <fmthdft.hxx>
65 #include <fmtanchr.hxx>
66 #include <fmtfsize.hxx>
67 #include <fmtornt.hxx>
68 #include <swwait.hxx>
69 #include <gloshdl.hxx>
70 #include <mdiexp.hxx>
71 #include <frmatr.hxx>
72 #include <paratr.hxx>
73 #include <swmodule.hxx>
74 #ifndef _VIEW_HXX
75 #include <view.hxx>
76 #endif
77 #ifndef _DOCSH_HXX
78 #include <docsh.hxx>
79 #endif
80 #include <fldbas.hxx>
81 #include <swundo.hxx>
82 #include <wrtsh.hxx>
83 #ifndef _CMDID_H
84 #include <cmdid.h>
85 #endif
86 #ifndef _DBMGR_HXX
87 #include <dbmgr.hxx>
88 #endif
89 #include <fmtcol.hxx>
90 #include <expfld.hxx>
91 #include <fldmgr.hxx>
92 #include <label.hxx>
93 #include <labimg.hxx>
94 #include <section.hxx>
95 #include <pagedesc.hxx>
96 #include <poolfmt.hxx>
98 #ifndef _APP_HRC
99 #include <app.hrc>
100 #endif
101 #ifndef _POOLFMT_HRC
102 #include <poolfmt.hrc>
103 #endif
104 #include "swabstdlg.hxx"
105 #include "envelp.hrc"
106 #include <misc.hrc>
108 #include <IDocumentDeviceAccess.hxx>
110 using namespace ::com::sun::star;
111 using ::rtl::OUString;
113 // steht im appenv.cxx
114 extern String InsertLabEnvText( SwWrtShell& , SwFldMgr& , const String& );
116 const char __FAR_DATA MASTER_LABEL[] = "MasterLabel";
118 // --------------------------------------------------------------------------
120 const SwFrmFmt *lcl_InsertBCText( SwWrtShell& rSh, const SwLabItem& rItem,
121 SwFrmFmt &rFmt,
122 sal_uInt16 nCol, sal_uInt16 nRow, sal_Bool bPage)
124 SfxItemSet aSet(rSh.GetAttrPool(), RES_ANCHOR, RES_ANCHOR,
125 RES_VERT_ORIENT, RES_VERT_ORIENT, RES_HORI_ORIENT, RES_HORI_ORIENT, 0 );
126 sal_uInt16 nPhyPageNum, nVirtPageNum;
127 rSh.GetPageNum( nPhyPageNum, nVirtPageNum );
129 aSet.Put(SwFmtAnchor(bPage ? FLY_IN_CNTNT : FLY_PAGE, nPhyPageNum));
130 if (!bPage)
132 aSet.Put(SwFmtHoriOrient(rItem.lLeft + nCol * rItem.lHDist,
133 text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
134 aSet.Put(SwFmtVertOrient(rItem.lUpper + nRow * rItem.lVDist,
135 text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
137 const SwFrmFmt *pFmt = rSh.NewFlyFrm(aSet, sal_True, &rFmt ); // Fly einfuegen
138 ASSERT( pFmt, "Fly not inserted" );
140 rSh.UnSelectFrm(); //Rahmen wurde automatisch selektiert
142 rSh.SetTxtFmtColl( rSh.GetTxtCollFromPool( RES_POOLCOLL_STANDARD ) );
145 if(!rItem.bSynchron || !(nCol|nRow))
147 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
148 DBG_ASSERT(pFact, "Dialogdiet fail!");
149 ::GlossarySetActGroup fnSetActGroup = pFact->SetGlossaryActGroupFunc( DLG_RENAME_GLOS );
150 if ( fnSetActGroup )
151 (*fnSetActGroup)( rItem.sGlossaryGroup );
152 SwGlossaryHdl* pGlosHdl = rSh.GetView().GetGlosHdl();
153 pGlosHdl->SetCurGroup(rItem.sGlossaryGroup, sal_True);
154 pGlosHdl->InsertGlossary( rItem.sGlossaryBlockName );
157 return pFmt;
160 const SwFrmFmt *lcl_InsertLabText( SwWrtShell& rSh, const SwLabItem& rItem,
161 SwFrmFmt &rFmt, SwFldMgr& rFldMgr,
162 sal_uInt16 nCol, sal_uInt16 nRow, sal_Bool bLast, sal_Bool bPage)
164 SfxItemSet aSet(rSh.GetAttrPool(), RES_ANCHOR, RES_ANCHOR,
165 RES_VERT_ORIENT, RES_VERT_ORIENT, RES_HORI_ORIENT, RES_HORI_ORIENT, 0 );
166 sal_uInt16 nPhyPageNum, nVirtPageNum;
167 rSh.GetPageNum( nPhyPageNum, nVirtPageNum );
169 aSet.Put(SwFmtAnchor(bPage ? FLY_IN_CNTNT : FLY_PAGE, nPhyPageNum));
170 if (!bPage)
172 aSet.Put(SwFmtHoriOrient(rItem.lLeft + nCol * rItem.lHDist,
173 text::HoriOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
174 aSet.Put(SwFmtVertOrient(rItem.lUpper + nRow * rItem.lVDist,
175 text::VertOrientation::NONE, text::RelOrientation::PAGE_FRAME ));
177 const SwFrmFmt *pFmt = rSh.NewFlyFrm(aSet, sal_True, &rFmt ); // Fly einfuegen
178 ASSERT( pFmt, "Fly not inserted" );
180 rSh.UnSelectFrm(); //Rahmen wurde automatisch selektiert
182 rSh.SetTxtFmtColl( rSh.GetTxtCollFromPool( RES_POOLCOLL_STANDARD ) );
184 // Ggf. "Naechster Datensatz"
185 String sDBName;
186 if( (!rItem.bSynchron || !(nCol|nRow)) && (sDBName = InsertLabEnvText( rSh, rFldMgr, rItem.aWriting )).Len() && !bLast )
188 sDBName.SetToken( 3, DB_DELIM, String::CreateFromAscii("True"));
189 SwInsertFld_Data aData(TYP_DBNEXTSETFLD, 0, sDBName, aEmptyStr, 0, &rSh );
190 rFldMgr.InsertFld( aData );
193 return pFmt;
196 // ----------------------------------------------------------------------------
199 void SwModule::InsertLab(SfxRequest& rReq, sal_Bool bLabel)
201 static sal_uInt16 nLabelTitleNo = 0;
202 static sal_uInt16 nBCTitleNo = 0;
204 // DB-Manager anlegen
205 SwNewDBMgr* pNewDBMgr = new SwNewDBMgr;
207 // SwLabItem aus Config lesen
208 SwLabCfgItem aLabCfg(bLabel);
210 // Dialog hochfahren
211 SfxItemSet aSet( GetPool(), FN_LABEL, FN_LABEL, 0 );
212 aSet.Put( aLabCfg.GetItem() );
214 SwAbstractDialogFactory* pDialogFactory = SwAbstractDialogFactory::Create();
215 DBG_ASSERT(pDialogFactory, "SwAbstractDialogFactory fail!");
217 AbstarctSwLabDlg* pDlg = pDialogFactory->CreateSwLabDlg( 0, aSet, pNewDBMgr, bLabel, DLG_LAB );
218 DBG_ASSERT(pDlg, "Dialogdiet fail!");
220 if ( RET_OK == pDlg->Execute() )
222 // Dialog auslesen, Item in Config speichern
223 const SwLabItem& rItem = (const SwLabItem&) pDlg->
224 GetOutputItemSet()->Get(FN_LABEL);
225 aLabCfg.GetItem() = rItem;
226 aLabCfg.Commit();
228 // Neues Dokument erzeugen.
229 SfxObjectShellRef xDocSh( new SwDocShell( SFX_CREATE_MODE_STANDARD));
230 xDocSh->DoInitNew( 0 );
232 // Drucker
233 Printer *pPrt = pDlg->GetPrt();
234 if (pPrt)
236 SwDocShell *pDocSh = (SwDocShell*)(&*xDocSh);
237 pDocSh->getIDocumentDeviceAccess()->setJobsetup(pPrt->GetJobSetup());
240 const SfxItemSet *pArgs = rReq.GetArgs();
241 DBG_ASSERT( pArgs, "no arguments in SfxRequest");
242 const SfxPoolItem* pFrameItem = 0;
243 if(pArgs)
244 pArgs->GetItemState(SID_DOCFRAME, FALSE, &pFrameItem);
246 SfxViewFrame* pFrame = 0;
247 if( pFrameItem )
249 SfxFrame* pFr = ((const SfxFrameItem*)pFrameItem)->GetFrame();
250 xDocSh->PutItem(SfxBoolItem(SID_HIDDEN, TRUE));
251 pFr->InsertDocument(xDocSh);
252 pFrame = pFr->GetCurrentViewFrame();
254 else
256 pFrame = SfxViewFrame::CreateViewFrame( *xDocSh, 0, TRUE );
258 SwView *pNewView = (SwView*) pFrame->GetViewShell();
259 pNewView->AttrChangedNotify( &pNewView->GetWrtShell() );//Damit SelectShell gerufen wird.
261 // Dokumenttitel setzen
262 String aTmp;
263 if(bLabel)
265 aTmp = String(SW_RES( STR_LAB_TITLE));
266 aTmp += String::CreateFromInt32(++nLabelTitleNo );
268 else
270 aTmp = pDlg->GetBusinessCardStr();
271 aTmp += String::CreateFromInt32( ++nBCTitleNo );
273 xDocSh->SetTitle( aTmp );
275 pFrame->GetFrame()->Appear();
277 // Shell ermitteln
278 SwWrtShell *pSh = pNewView->GetWrtShellPtr();
279 ASSERT( pSh, "missing WrtShell" );
281 { // block for locks the dispatcher!!
283 SwWait aWait( (SwDocShell&)*xDocSh, sal_True );
285 SET_CURR_SHELL(pSh);
286 pSh->SetLabelDoc(rItem.bSynchron);
287 pSh->DoUndo( sal_False );
288 pSh->StartAllAction();
290 pSh->SetNewDoc(); // Performanceprobleme vermeiden
292 SwPageDesc aDesc = pSh->GetPageDesc( 0 );
293 SwFrmFmt& rFmt = aDesc.GetMaster();
295 // Raender
296 SvxLRSpaceItem aLRMargin( RES_LR_SPACE );
297 SvxULSpaceItem aULMargin( RES_UL_SPACE );
298 aLRMargin.SetLeft ((sal_uInt16) rItem.lLeft );
299 aULMargin.SetUpper((sal_uInt16) rItem.lUpper);
300 aLRMargin.SetRight(MINLAY/2);
301 aULMargin.SetLower(MINLAY/2);
302 rFmt.SetFmtAttr(aLRMargin);
303 rFmt.SetFmtAttr(aULMargin);
305 // Kopf- und Fusszeilen
306 rFmt.SetFmtAttr(SwFmtHeader(sal_Bool(sal_False)));
307 aDesc.ChgHeaderShare(sal_False);
308 rFmt.SetFmtAttr(SwFmtFooter(sal_Bool(sal_False)));
309 aDesc.ChgFooterShare(sal_False);
312 aDesc.SetUseOn(nsUseOnPage::PD_ALL); // Seitennumerierung
314 // Einstellen der Seitengroesse
315 rFmt.SetFmtAttr(SwFmtFrmSize(ATT_FIX_SIZE,
316 rItem.lLeft + rItem.nCols * rItem.lHDist + MINLAY,
317 rItem.lUpper + rItem.nRows * rItem.lVDist + MINLAY));
319 // Numerierungsart
320 SvxNumberType aType;
321 aType.SetNumberingType(SVX_NUM_NUMBER_NONE);
322 aDesc.SetNumType( aType );
324 // Folgevorlage
325 const SwPageDesc &rFollow = pSh->GetPageDesc( pSh->GetCurPageDesc() );
326 aDesc.SetFollow( &rFollow );
328 pPrt = pSh->getIDocumentDeviceAccess()->getPrinter( true );
329 SvxPaperBinItem aItem( RES_PAPER_BIN );
330 aItem.SetValue((sal_Int8)pPrt->GetPaperBin());
331 rFmt.SetFmtAttr(aItem);
333 //determine orientation by calculating the width and height of the resulting page
334 const int nResultWidth = rItem.lHDist * (rItem.nCols - 1) + rItem.lWidth + rItem.lLeft;
335 const int nResultHeight = rItem.lVDist * (rItem.nRows - 1) + rItem.lHeight + rItem.lUpper;
336 aDesc.SetLandscape(nResultWidth > nResultHeight);
338 pSh->ChgPageDesc( 0, aDesc );
340 // Rahmen einfuegen
341 SwFldMgr* pFldMgr = new SwFldMgr;
342 pFldMgr->SetEvalExpFlds(sal_False);
344 //fix(24446): Damit der Text der Ettiketten nicht im unbedruckbaren
345 //Bereich landet stellen wir entsprechende Raender ein. Um das Handling
346 //so Optimal wie moeglich zu halten stellen wir zunaechst an der
347 //aktuellen Absatzvorlage keinen Rand als hartes Attribut ein (Damit die
348 //Formatierung wg. der Zeichengeb. Rahmen passt. Dann stellen wir die
349 //Standarabsatzvorlage anhand des unbedruckbaren Bereiches ein.
350 const long nMin = pPrt->GetPageOffset().X() - rItem.lLeft;
351 if ( nMin > 0 )
353 SvxLRSpaceItem aLR( RES_LR_SPACE );
354 pSh->SetAttr( aLR );
355 SwFmt *pStandard = pSh->GetTxtCollFromPool( RES_POOLCOLL_STANDARD );
356 aLR.SetLeft ( sal_uInt16(nMin) );
357 aLR.SetRight( sal_uInt16(nMin) );
358 pStandard->SetFmtAttr( aLR );
361 // Rahmenvorlage vorbereiten
362 SwFrmFmt* pFmt = pSh->GetFrmFmtFromPool( RES_POOLFRM_LABEL );
363 SwFmtFrmSize aFrmSize( ATT_FIX_SIZE,
364 rItem.lHDist - (rItem.lHDist-rItem.lWidth),
365 rItem.lVDist - (rItem.lVDist-rItem.lHeight));
366 pFmt->SetFmtAttr(aFrmSize);
368 SvxLRSpaceItem aFrmLRSpace( 0, (sal_uInt16)(rItem.lHDist - rItem.lWidth),
369 0, 0,
370 RES_LR_SPACE);
371 pFmt->SetFmtAttr(aFrmLRSpace);
373 SvxULSpaceItem aFrmULSpace( 0, (sal_uInt16)(rItem.lVDist - rItem.lHeight),
374 RES_UL_SPACE);
375 pFmt->SetFmtAttr(aFrmULSpace);
377 const SwFrmFmt *pFirstFlyFmt = 0;
378 if ( rItem.bPage )
380 SwFmtVertOrient aFrmVertOrient( pFmt->GetVertOrient() );
381 aFrmVertOrient.SetVertOrient( text::VertOrientation::TOP );
382 pFmt->SetFmtAttr(aFrmVertOrient);
384 for ( sal_uInt16 i = 0; i < rItem.nRows; ++i )
386 for ( sal_uInt16 j = 0; j < rItem.nCols; ++j )
388 pSh->Push();
389 const SwFrmFmt *pTmp =
390 bLabel ?
391 lcl_InsertLabText( *pSh, rItem, *pFmt, *pFldMgr, j, i,
392 i == rItem.nRows - 1 && j == rItem.nCols - 1,
393 sal_True ) :
394 lcl_InsertBCText(*pSh, rItem, *pFmt, j, i, sal_True);
395 if (!(i|j))
397 pFirstFlyFmt = pTmp;
399 if (rItem.bSynchron)
401 // if there is no content in the fly then
402 // dont leave the fly!!!
403 pSh->Push();
404 pSh->SttDoc();
405 sal_Bool bInFly = 0 != pSh->WizzardGetFly();
406 pSh->Pop( bInFly );
408 if( bInFly )
409 pSh->EndDoc(sal_True); // select all content
410 // in the fly
411 else
412 pSh->SetMark(); // set only the mark
414 SwSection aSect( CONTENT_SECTION,
415 String::CreateFromAscii(MASTER_LABEL));
416 pSh->InsertSection(aSect);
419 else if (rItem.bSynchron)
421 SwSection aSect(FILE_LINK_SECTION, pSh->GetUniqueSectionName());
422 String sLinkName(sfx2::cTokenSeperator);
423 sLinkName += sfx2::cTokenSeperator;
424 sLinkName += String::CreateFromAscii(MASTER_LABEL);
425 aSect.SetLinkFileName(sLinkName);
426 aSect.SetProtect();
427 pSh->Insert(aDotStr); // Dummytext zum Zuweisen der Section
428 pSh->SttDoc();
429 pSh->EndDoc(sal_True); // Alles im Rahmen selektieren
430 pSh->InsertSection(aSect);
432 pSh->Pop( sal_False );
434 if ( i + 1 != rItem.nRows )
435 pSh->SplitNode(); // Kleine Optimierung
438 else
440 pFirstFlyFmt = bLabel ?
441 lcl_InsertLabText( *pSh, rItem, *pFmt, *pFldMgr,
442 static_cast< sal_uInt16 >(rItem.nCol - 1),
443 static_cast< sal_uInt16 >(rItem.nRow - 1), sal_True, sal_False ) :
444 lcl_InsertBCText(*pSh, rItem, *pFmt,
445 static_cast< sal_uInt16 >(rItem.nCol - 1),
446 static_cast< sal_uInt16 >(rItem.nRow - 1), sal_False);
449 //fill the user fields
450 if(!bLabel)
452 uno::Reference< frame::XModel > xModel = pSh->GetView().GetDocShell()->GetBaseModel();
453 DBG_ASSERT(pDialogFactory, "SwAbstractDialogFactory fail!");
454 SwLabDlgMethod SwLabDlgUpdateFieldInformation = pDialogFactory->GetSwLabDlgStaticMethod ();
455 SwLabDlgUpdateFieldInformation(xModel, rItem);
458 pFldMgr->SetEvalExpFlds(sal_True);
459 pFldMgr->EvalExpFlds(pSh);
461 delete pFldMgr;
463 pSh->GotoFly(pFirstFlyFmt->GetName(), FLYCNTTYPE_ALL, sal_False);
465 pSh->EndAllAction();
466 pSh->DoUndo( sal_True );
469 if( rItem.aWriting.indexOf( '<' ) >= 0 )
471 // Datenbankbrowser mit zuletzt verwendeter Datenbank oeffnen
472 ShowDBObj( *pNewView, pSh->GetDBData() );
475 if( rItem.bSynchron )
477 SfxDispatcher* pDisp = pFrame->GetDispatcher();
478 ASSERT(pDisp, "Heute kein Dispatcher am Frame?");
479 pDisp->Execute(FN_SYNC_LABELS, SFX_CALLMODE_ASYNCHRON);
481 rReq.SetReturnValue(SfxVoidItem(bLabel ? FN_LABEL : FN_BUSINESS_CARD));
483 delete pDlg;
485 if( pNewDBMgr )
486 delete pNewDBMgr;