xref: /aoo4110/main/sw/source/core/crsr/unocrsr.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 #include <unocrsr.hxx>
29 #include <doc.hxx>
30 #include <swtable.hxx>
31 #include <docary.hxx>
32 #include <rootfrm.hxx>
33 
SV_IMPL_PTRARR(SwUnoCrsrTbl,SwUnoCrsrPtr)34 SV_IMPL_PTRARR( SwUnoCrsrTbl, SwUnoCrsrPtr )
35 
36 IMPL_FIXEDMEMPOOL_NEWDEL( SwUnoCrsr, 10, 10 )
37 
38 SwUnoCrsr::SwUnoCrsr( const SwPosition &rPos, SwPaM* pRing )
39 	: SwCursor( rPos, pRing, false ), SwModify( 0 ),
40 	bRemainInSection( sal_True ),
41 	bSkipOverHiddenSections( sal_False ),
42 	bSkipOverProtectSections( sal_False )
43 
44 {}
45 
~SwUnoCrsr()46 SwUnoCrsr::~SwUnoCrsr()
47 {
48 	SwDoc* pDoc = GetDoc();
49 	if( !pDoc->IsInDtor() )
50 	{
51 		// dann muss der Cursor aus dem Array ausgetragen werden
52 		SwUnoCrsrTbl& rTbl = (SwUnoCrsrTbl&)pDoc->GetUnoCrsrTbl();
53 		sal_uInt16 nDelPos = rTbl.GetPos( this );
54 
55 		if( USHRT_MAX != nDelPos )
56 			rTbl.Remove( nDelPos );
57 		else {
58 			ASSERT( !this, "UNO Cursor nicht mehr im Array" );
59         }
60 	}
61 
62 	// den gesamten Ring loeschen!
63 	while( GetNext() != this )
64 	{
65 		Ring* pNxt = GetNext();
66 		pNxt->MoveTo( 0 );		// ausketten
67 		delete pNxt;			// und loeschen
68 	}
69 }
70 
Clone() const71 SwUnoCrsr * SwUnoCrsr::Clone() const
72 {
73     SwUnoCrsr * pNewCrsr = GetDoc()->CreateUnoCrsr( *GetPoint() );
74     if (HasMark())
75     {
76         pNewCrsr->SetMark();
77         *pNewCrsr->GetMark() = *GetMark();
78     }
79     return pNewCrsr;
80 }
81 
Clone() const82 SwUnoTableCrsr * SwUnoTableCrsr::Clone() const
83 {
84     SwUnoTableCrsr * pNewCrsr = dynamic_cast<SwUnoTableCrsr*>(
85         GetDoc()->CreateUnoCrsr(
86             *GetPoint(), sal_True /* create SwUnoTableCrsr */ ) );
87     OSL_ENSURE(pNewCrsr, "Clone: cannot create SwUnoTableCrsr?");
88     if (HasMark())
89     {
90         pNewCrsr->SetMark();
91         *pNewCrsr->GetMark() = *GetMark();
92     }
93     return pNewCrsr;
94 }
95 
96 
IsReadOnlyAvailable() const97 bool SwUnoCrsr::IsReadOnlyAvailable() const
98 {
99     return true;
100 }
101 
102 const SwCntntFrm*
DoSetBidiLevelLeftRight(sal_Bool &,sal_Bool,sal_Bool)103 SwUnoCrsr::DoSetBidiLevelLeftRight( sal_Bool &, sal_Bool, sal_Bool )
104 {
105     return 0; // not for uno cursor
106 }
107 
DoSetBidiLevelUpDown()108 void SwUnoCrsr::DoSetBidiLevelUpDown()
109 {
110     return; // not for uno cursor
111 }
112 
IsSelOvr(int eFlags)113 sal_Bool SwUnoCrsr::IsSelOvr( int eFlags )
114 {
115 	if( bRemainInSection )
116 	{
117 		SwDoc* pDoc = GetDoc();
118 		SwNodeIndex aOldIdx( *pDoc->GetNodes()[ GetSavePos()->nNode ] );
119 		SwNodeIndex& rPtIdx = GetPoint()->nNode;
120         SwStartNode *pOldSttNd = aOldIdx.GetNode().StartOfSectionNode(),
121                     *pNewSttNd = rPtIdx.GetNode().StartOfSectionNode();
122 		if( pOldSttNd != pNewSttNd )
123 		{
124 			sal_Bool bMoveDown = GetSavePos()->nNode < rPtIdx.GetIndex();
125 			sal_Bool bValidPos = sal_False;
126 
127 			// search the correct surrounded start node - which the index
128 			// can't leave.
129 			while( pOldSttNd->IsSectionNode() )
130 				pOldSttNd = pOldSttNd->StartOfSectionNode();
131 
132 			// is the new index inside this surrounded section?
133 			if( rPtIdx > *pOldSttNd &&
134 				rPtIdx < pOldSttNd->EndOfSectionIndex() )
135 			{
136 				// check if it a valid move inside this section
137 				// (only over SwSection's !)
138 				const SwStartNode* pInvalidNode;
139 				do {
140 					pInvalidNode = 0;
141                     pNewSttNd = rPtIdx.GetNode().StartOfSectionNode();
142 
143 					const SwStartNode *pSttNd = pNewSttNd, *pEndNd = pOldSttNd;
144 					if( pSttNd->EndOfSectionIndex() >
145 						pEndNd->EndOfSectionIndex() )
146 					{
147 						pEndNd = pNewSttNd;
148 						pSttNd = pOldSttNd;
149 					}
150 
151 					while( pSttNd->GetIndex() > pEndNd->GetIndex() )
152 					{
153 						if( !pSttNd->IsSectionNode() )
154 							pInvalidNode = pSttNd;
155 						pSttNd = pSttNd->StartOfSectionNode();
156 					}
157 					if( pInvalidNode )
158 					{
159 						if( bMoveDown )
160 						{
161 							rPtIdx.Assign( *pInvalidNode->EndOfSectionNode(), 1 );
162 
163 							if( !rPtIdx.GetNode().IsCntntNode() &&
164 								( !pDoc->GetNodes().GoNextSection( &rPtIdx ) ||
165                                   rPtIdx > pOldSttNd->EndOfSectionIndex() ) )
166 								break;
167 						}
168 						else
169 						{
170 							rPtIdx.Assign( *pInvalidNode, -1 );
171 
172 							if( !rPtIdx.GetNode().IsCntntNode() &&
173 								( !pDoc->GetNodes().GoPrevSection( &rPtIdx ) ||
174                             	  rPtIdx < *pOldSttNd ) )
175 								break;
176 						}
177 					}
178 					else
179 						bValidPos = sal_True;
180 				} while ( pInvalidNode );
181 			}
182 
183 			if( bValidPos )
184 			{
185 				SwCntntNode* pCNd = GetCntntNode();
186 				sal_uInt16 nCnt = 0;
187 				if( pCNd && !bMoveDown )
188 					nCnt = pCNd->Len();
189 				GetPoint()->nContent.Assign( pCNd, nCnt );
190 			}
191 			else
192 			{
193 				rPtIdx = GetSavePos()->nNode;
194 				GetPoint()->nContent.Assign( GetCntntNode(), GetSavePos()->nCntnt );
195 				return sal_True;
196 			}
197 		}
198 	}
199 	return SwCursor::IsSelOvr( eFlags );
200 }
201 
202 
203 /*  */
204 
SwUnoTableCrsr(const SwPosition & rPos)205 SwUnoTableCrsr::SwUnoTableCrsr(const SwPosition& rPos)
206 	: SwCursor(rPos,0,false), SwUnoCrsr(rPos), SwTableCursor(rPos), aTblSel(rPos,0,false)
207 {
208 	SetRemainInSection(sal_False);
209 }
210 
~SwUnoTableCrsr()211 SwUnoTableCrsr::~SwUnoTableCrsr()
212 {
213 	while( aTblSel.GetNext() != &aTblSel )
214 		delete aTblSel.GetNext();			// und loeschen
215 }
216 
217 
218 /*
219 SwCursor* SwUnoTableCrsr::Create( SwPaM* pRing ) const
220 {
221 	return SwUnoCrsr::Create( pRing );
222 }
223 */
224 
IsSelOvr(int eFlags)225 sal_Bool SwUnoTableCrsr::IsSelOvr( int eFlags )
226 {
227 	sal_Bool bRet = SwUnoCrsr::IsSelOvr( eFlags );
228 	if( !bRet )
229 	{
230 		const SwTableNode* pTNd = GetPoint()->nNode.GetNode().FindTableNode();
231 		bRet = !(pTNd == GetDoc()->GetNodes()[ GetSavePos()->nNode ]->
232 				FindTableNode() && (!HasMark() ||
233 				pTNd == GetMark()->nNode.GetNode().FindTableNode() ));
234 	}
235 	return bRet;
236 }
237 
MakeBoxSels()238 void SwUnoTableCrsr::MakeBoxSels()
239 {
240 	const SwCntntNode* pCNd;
241     bool bMakeTblCrsrs = true;
242 	if( GetPoint()->nNode.GetIndex() && GetMark()->nNode.GetIndex() &&
243 			0 != ( pCNd = GetCntntNode() ) && pCNd->getLayoutFrm( pCNd->GetDoc()->GetCurrentLayout() ) &&
244 			0 != ( pCNd = GetCntntNode(sal_False) ) && pCNd->getLayoutFrm( pCNd->GetDoc()->GetCurrentLayout() ) )
245 		bMakeTblCrsrs = GetDoc()->GetCurrentLayout()->MakeTblCrsrs( *this );
246 
247     if ( !bMakeTblCrsrs )
248     {
249         SwSelBoxes& rTmpBoxes = (SwSelBoxes&)GetBoxes();
250         sal_uInt16 nCount = 0;
251         while( nCount < rTmpBoxes.Count() )
252             DeleteBox( nCount );
253     }
254 
255 	if( IsChgd() )
256 	{
257 		SwTableCursor::MakeBoxSels( &aTblSel );
258 		if( !GetBoxesCount() )
259 		{
260 			const SwTableBox* pBox;
261 			const SwNode* pBoxNd = GetPoint()->nNode.GetNode().FindTableBoxStartNode();
262 			const SwTableNode* pTblNd = pBoxNd ? pBoxNd->FindTableNode() : 0;
263 			if( pTblNd && 0 != ( pBox = pTblNd->GetTable().GetTblBox( pBoxNd->GetIndex() )) )
264 				InsertBox( *pBox );
265 		}
266 	}
267 }
268 
269