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