xref: /trunk/main/sw/source/core/fields/ddetbl.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 #include <frmfmt.hxx>
33 #include <doc.hxx>
34 #include <index.hxx>
35 #include <ndtxt.hxx>
36 #include <swtable.hxx>
37 #include <swddetbl.hxx>
38 #include <ddefld.hxx>           // fuer den FieldType
39 #include <ndindex.hxx>
40 #include <fldupde.hxx>
41 #include <swtblfmt.hxx>
42 #include <fieldhint.hxx>
43 
44 TYPEINIT1( SwDDETable, SwTable );
45 
46     // Constructor movet alle Lines/Boxen aus der SwTable zu sich.
47     // Die SwTable ist danach Leer und muss geloescht werden.
48 SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType,
49                         sal_Bool bUpdate )
50     : SwTable( rTable ), aDepend( this, pDDEType )
51 {
52     // Kopiere/move die Daten der Tabelle
53     aSortCntBoxes.Insert( &rTable.GetTabSortBoxes(), 0,
54                           rTable.GetTabSortBoxes().Count()  ); // move die Inh. Boxen
55     rTable.GetTabSortBoxes().Remove( (sal_uInt16)0, rTable.GetTabSortBoxes().Count() );
56 
57     aLines.Insert( &rTable.GetTabLines(),0 );                       // move die Lines
58     rTable.GetTabLines().Remove( 0, rTable.GetTabLines().Count() );
59 
60     if( aLines.Count() )
61     {
62         const SwNode& rNd = *GetTabSortBoxes()[0]->GetSttNd();
63         if( rNd.GetNodes().IsDocNodes() )
64         {
65             // mba: swclient refactoring - this code shouldn't have done anything!
66             // the ModifyLock Flag is evaluated in SwModify only, though it was accessible via SwClient
67             // This has been fixed now
68 //          aDepend.LockModify();
69             pDDEType->IncRefCnt();
70 //          aDepend.UnlockModify();
71 
72             // Setzen der Werte in die einzelnen Boxen
73             // update box content only if update flag is set (false in import)
74             if (bUpdate)
75                 ChangeContent();
76         }
77     }
78 }
79 
80 __EXPORT SwDDETable::~SwDDETable()
81 {
82     SwDDEFieldType* pFldTyp = (SwDDEFieldType*)aDepend.GetRegisteredIn();
83     SwDoc* pDoc = GetFrmFmt()->GetDoc();
84     if( !pDoc->IsInDtor() && aLines.Count() &&
85         GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
86         pFldTyp->DecRefCnt();
87 
88     // sind wir der letzte Abhaengige vom "geloeschten Feld" dann loesche dieses
89     if( pFldTyp->IsDeleted() && pFldTyp->IsLastDepend() )
90     {
91         pFldTyp->Remove( &aDepend );
92         delete pFldTyp;
93     }
94 }
95 
96 void SwDDETable::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
97 {
98     if( pNew && RES_UPDATEDDETBL == pNew->Which() )
99         ChangeContent();
100     else
101         SwTable::Modify( pOld, pNew );
102 }
103 
104 void SwDDETable::SwClientNotify( const SwModify&, const SfxHint& rHint )
105 {
106     const SwFieldHint* pHint = dynamic_cast<const SwFieldHint*>( &rHint );
107     if ( pHint )
108         // replace DDETable by real table
109         NoDDETable();
110 }
111 
112 void SwDDETable::ChangeContent()
113 {
114     ASSERT( GetFrmFmt(), "Kein FrameFormat" );
115 
116     // Stehen wir im richtigen NodesArray (Wegen UNDO)
117     if( !aLines.Count() )
118         return;
119     ASSERT( GetTabSortBoxes().Count(), "Tabelle ohne Inhalt?" );
120     if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
121         return;
122 
123     // zugriff auf den DDEFldType
124     SwDDEFieldType* pDDEType = (SwDDEFieldType*)aDepend.GetRegisteredIn();
125 
126     String aExpand = pDDEType->GetExpansion();
127     aExpand.EraseAllChars( '\r' );
128 
129     for( sal_uInt16 n = 0; n < aLines.Count(); ++n )
130     {
131         String aLine = aExpand.GetToken( n, '\n' );
132         SwTableLine* pLine = aLines[ n ];
133         for( sal_uInt16 i = 0; i < pLine->GetTabBoxes().Count(); ++i )
134         {
135             SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
136             ASSERT( pBox->GetSttIdx(), "keine InhaltsBox" );
137             SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
138             SwTxtNode* pTxtNode = aNdIdx.GetNode().GetTxtNode();
139             ASSERT( pTxtNode, "Kein Node" );
140             SwIndex aCntIdx( pTxtNode, 0 );
141             pTxtNode->EraseText( aCntIdx );
142             pTxtNode->InsertText( aLine.GetToken( i, '\t' ), aCntIdx );
143 
144             SwTableBoxFmt* pBoxFmt = (SwTableBoxFmt*)pBox->GetFrmFmt();
145             pBoxFmt->LockModify();
146             pBoxFmt->ResetFmtAttr( RES_BOXATR_VALUE );
147             pBoxFmt->UnlockModify();
148         }
149     }
150 
151     const IDocumentSettingAccess* pIDSA = GetFrmFmt()->getIDocumentSettingAccess();
152     SwDoc* pDoc = GetFrmFmt()->GetDoc();
153     if( AUTOUPD_FIELD_AND_CHARTS == pIDSA->getFieldUpdateFlags(true) )
154         pDoc->SetFieldsDirty( true, NULL, 0 );
155 }
156 
157 SwDDEFieldType* SwDDETable::GetDDEFldType()
158 {
159     return (SwDDEFieldType*)aDepend.GetRegisteredIn();
160 }
161 
162 sal_Bool SwDDETable::NoDDETable()
163 {
164     // suche den TabellenNode
165     ASSERT( GetFrmFmt(), "Kein FrameFormat" );
166     SwDoc* pDoc = GetFrmFmt()->GetDoc();
167 
168     // Stehen wir im richtigen NodesArray (Wegen UNDO)
169     if( !aLines.Count() )
170         return sal_False;
171     ASSERT( GetTabSortBoxes().Count(), "Tabelle ohne Inhalt?" );
172     SwNode* pNd = (SwNode*)GetTabSortBoxes()[0]->GetSttNd();
173     if( !pNd->GetNodes().IsDocNodes() )
174         return sal_False;
175 
176     SwTableNode* pTblNd = pNd->FindTableNode();
177     ASSERT( pTblNd, "wo steht denn die Tabelle ?");
178 
179     SwTable* pNewTbl = new SwTable( *this );
180 
181     // Kopiere/move die Daten der Tabelle
182     pNewTbl->GetTabSortBoxes().Insert( &GetTabSortBoxes(), 0,
183                           GetTabSortBoxes().Count()  ); // move die Inh. Boxen
184     GetTabSortBoxes().Remove( (sal_uInt16)0, GetTabSortBoxes().Count() );
185 
186     pNewTbl->GetTabLines().Insert( &GetTabLines(),0 );                      // move die Lines
187     GetTabLines().Remove( 0, GetTabLines().Count() );
188 
189     if( pDoc->GetCurrentViewShell() )   //swmod 071108//swmod 071225
190         ((SwDDEFieldType*)aDepend.GetRegisteredIn())->DecRefCnt();
191 
192     pTblNd->SetNewTable( pNewTbl );       // setze die Tabelle
193 
194     return sal_True;
195 }
196 
197 
198