Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <comphelper/string.hxx>
21 : #include <frmfmt.hxx>
22 : #include <doc.hxx>
23 : #include <index.hxx>
24 : #include <ndtxt.hxx>
25 : #include <swtable.hxx>
26 : #include <swddetbl.hxx>
27 : #include <ddefld.hxx>
28 : #include <ndindex.hxx>
29 : #include <fldupde.hxx>
30 : #include <swtblfmt.hxx>
31 : #include <fieldhint.hxx>
32 :
33 0 : TYPEINIT1( SwDDETable, SwTable );
34 :
35 : /// Ctor moves all lines/boxes from a SwTable into itself.
36 : /// Afterwards the SwTable is empty and must be deleted.
37 0 : SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType, sal_Bool bUpdate )
38 0 : : SwTable( rTable ), aDepend( this, pDDEType )
39 : {
40 : // copy the table data
41 0 : m_TabSortContentBoxes.insert(rTable.GetTabSortBoxes());
42 0 : rTable.GetTabSortBoxes().clear();
43 :
44 : aLines.insert( aLines.begin(),
45 0 : rTable.GetTabLines().begin(), rTable.GetTabLines().end() ); // move lines
46 0 : rTable.GetTabLines().clear();
47 :
48 0 : if( !aLines.empty() )
49 : {
50 0 : const SwNode& rNd = *GetTabSortBoxes()[0]->GetSttNd();
51 0 : if( rNd.GetNodes().IsDocNodes() )
52 : {
53 0 : pDDEType->IncRefCnt();
54 :
55 : // update box content only if update flag is set (false in import)
56 0 : if (bUpdate)
57 0 : ChangeContent();
58 : }
59 : }
60 0 : }
61 :
62 0 : SwDDETable::~SwDDETable()
63 : {
64 0 : SwDDEFieldType* pFldTyp = (SwDDEFieldType*)aDepend.GetRegisteredIn();
65 0 : SwDoc* pDoc = GetFrmFmt()->GetDoc();
66 0 : if( !pDoc->IsInDtor() && !aLines.empty() &&
67 0 : GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
68 0 : pFldTyp->DecRefCnt();
69 :
70 : // If it is the last dependent of the "deleted field" than delete it finally
71 0 : if( pFldTyp->IsDeleted() && pFldTyp->IsLastDepend() )
72 : {
73 0 : pFldTyp->Remove( &aDepend );
74 0 : delete pFldTyp;
75 : }
76 0 : }
77 :
78 0 : void SwDDETable::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
79 : {
80 0 : if( pNew && RES_UPDATEDDETBL == pNew->Which() )
81 0 : ChangeContent();
82 : else
83 0 : SwTable::Modify( pOld, pNew );
84 0 : }
85 :
86 0 : void SwDDETable::SwClientNotify( const SwModify&, const SfxHint& rHint )
87 : {
88 0 : const SwFieldHint* pHint = dynamic_cast<const SwFieldHint*>( &rHint );
89 0 : if ( pHint )
90 : // replace DDETable by real table
91 0 : NoDDETable();
92 0 : }
93 :
94 0 : void SwDDETable::ChangeContent()
95 : {
96 : OSL_ENSURE( GetFrmFmt(), "No FrameFormat" );
97 :
98 : // Is this the correct NodesArray? (because of UNDO)
99 0 : if( aLines.empty() )
100 0 : return;
101 : OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
102 0 : if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
103 0 : return;
104 :
105 : // access to DDEFldType
106 0 : SwDDEFieldType* pDDEType = (SwDDEFieldType*)aDepend.GetRegisteredIn();
107 :
108 0 : OUString aExpand = comphelper::string::remove(pDDEType->GetExpansion(), '\r');
109 :
110 0 : for( sal_uInt16 n = 0; n < aLines.size(); ++n )
111 : {
112 0 : OUString aLine = aExpand.getToken( n, '\n' );
113 0 : SwTableLine* pLine = aLines[ n ];
114 0 : for( sal_uInt16 i = 0; i < pLine->GetTabBoxes().size(); ++i )
115 : {
116 0 : SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
117 : OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
118 0 : SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
119 0 : SwTxtNode* pTxtNode = aNdIdx.GetNode().GetTxtNode();
120 : OSL_ENSURE( pTxtNode, "No Node" );
121 0 : SwIndex aCntIdx( pTxtNode, 0 );
122 0 : pTxtNode->EraseText( aCntIdx );
123 0 : pTxtNode->InsertText( aLine.getToken( i, '\t' ), aCntIdx );
124 :
125 0 : SwTableBoxFmt* pBoxFmt = (SwTableBoxFmt*)pBox->GetFrmFmt();
126 0 : pBoxFmt->LockModify();
127 0 : pBoxFmt->ResetFmtAttr( RES_BOXATR_VALUE );
128 0 : pBoxFmt->UnlockModify();
129 0 : }
130 0 : }
131 :
132 0 : const IDocumentSettingAccess* pIDSA = GetFrmFmt()->getIDocumentSettingAccess();
133 0 : SwDoc* pDoc = GetFrmFmt()->GetDoc();
134 0 : if( AUTOUPD_FIELD_AND_CHARTS == pIDSA->getFieldUpdateFlags(true) )
135 0 : pDoc->SetFieldsDirty( true, NULL, 0 );
136 : }
137 :
138 0 : SwDDEFieldType* SwDDETable::GetDDEFldType()
139 : {
140 0 : return (SwDDEFieldType*)aDepend.GetRegisteredIn();
141 : }
142 :
143 0 : sal_Bool SwDDETable::NoDDETable()
144 : {
145 : // search table node
146 : OSL_ENSURE( GetFrmFmt(), "No FrameFormat" );
147 0 : SwDoc* pDoc = GetFrmFmt()->GetDoc();
148 :
149 : // Is this the correct NodesArray? (because of UNDO)
150 0 : if( aLines.empty() )
151 0 : return sal_False;
152 : OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
153 0 : SwNode* pNd = (SwNode*)GetTabSortBoxes()[0]->GetSttNd();
154 0 : if( !pNd->GetNodes().IsDocNodes() )
155 0 : return sal_False;
156 :
157 0 : SwTableNode* pTblNd = pNd->FindTableNode();
158 : OSL_ENSURE( pTblNd, "Where is the table?");
159 :
160 0 : SwTable* pNewTbl = new SwTable( *this );
161 :
162 : // copy the table data
163 0 : pNewTbl->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
164 0 : GetTabSortBoxes().clear();
165 :
166 0 : pNewTbl->GetTabLines().insert( pNewTbl->GetTabLines().begin(),
167 0 : GetTabLines().begin(), GetTabLines().end() ); // move lines
168 0 : GetTabLines().clear();
169 :
170 0 : if( pDoc->GetCurrentViewShell() )
171 0 : ((SwDDEFieldType*)aDepend.GetRegisteredIn())->DecRefCnt();
172 :
173 0 : pTblNd->SetNewTable( pNewTbl ); // replace table
174 :
175 0 : return sal_True;
176 : }
177 :
178 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|