update dev300-m58
[ooovba.git] / sw / source / core / unocore / unorefmk.cxx
blob336ba97e68a3efb9040a0c7da0399572b2fae971
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: unorefmk.cxx,v $
10 * $Revision: 1.17 $
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 #include <vos/mutex.hxx>
36 #include <vcl/svapp.hxx>
37 #include <unoobj.hxx>
38 #include <unomap.hxx>
39 #include <unocrsr.hxx>
40 #include <doc.hxx>
41 #include <ndtxt.hxx>
42 #include <fmtrfmrk.hxx>
43 #include <txtrfmrk.hxx>
44 #include <hints.hxx>
46 using namespace ::com::sun::star;
47 using ::rtl::OUString;
49 /******************************************************************
50 * SwXReferenceMark
51 ******************************************************************/
53 /* -----------------------------13.03.00 12:15--------------------------------
55 ---------------------------------------------------------------------------*/
56 const uno::Sequence< sal_Int8 > & SwXReferenceMark::getUnoTunnelId()
58 static uno::Sequence< sal_Int8 > aSeq = ::CreateUnoTunnelId();
59 return aSeq;
61 /* -----------------------------10.03.00 18:04--------------------------------
63 ---------------------------------------------------------------------------*/
64 sal_Int64 SAL_CALL SwXReferenceMark::getSomething( const uno::Sequence< sal_Int8 >& rId )
65 throw(uno::RuntimeException)
67 if( rId.getLength() == 16
68 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
69 rId.getConstArray(), 16 ) )
71 return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >(this) );
73 return 0;
75 /* -----------------------------06.04.00 16:41--------------------------------
77 ---------------------------------------------------------------------------*/
78 OUString SwXReferenceMark::getImplementationName(void) throw( uno::RuntimeException )
80 return C2U("SwXReferenceMark");
82 /* -----------------------------06.04.00 16:41--------------------------------
84 ---------------------------------------------------------------------------*/
85 BOOL SwXReferenceMark::supportsService(const OUString& rServiceName) throw( uno::RuntimeException )
87 return !rServiceName.compareToAscii("com.sun.star.text.ReferenceMark")||
88 !rServiceName.compareToAscii("com.sun.star.text.TextContent");
90 /* -----------------------------06.04.00 16:41--------------------------------
92 ---------------------------------------------------------------------------*/
93 uno::Sequence< OUString > SwXReferenceMark::getSupportedServiceNames(void) throw( uno::RuntimeException )
95 uno::Sequence< OUString > aRet(2);
96 OUString* pArray = aRet.getArray();
97 pArray[0] = C2U("com.sun.star.text.ReferenceMark");
98 pArray[1] = C2U("com.sun.star.text.TextContent");
99 return aRet;
101 /*-- 11.12.98 10:28:32---------------------------------------------------
103 -----------------------------------------------------------------------*/
104 TYPEINIT1(SwXReferenceMark, SwClient);
106 SwXReferenceMark::SwXReferenceMark(SwDoc* pDc, const SwFmtRefMark* pRefMark) :
107 aLstnrCntnr( (text::XTextContent*)this),
108 pDoc(pDc),
109 pMark(pRefMark),
110 m_bIsDescriptor(0 == pRefMark)
112 if(pRefMark)
113 sMarkName = pRefMark->GetRefName();
114 if(pDoc)
115 pDoc->GetUnoCallBack()->Add(this);
117 /*-- 11.12.98 10:28:33---------------------------------------------------
119 -----------------------------------------------------------------------*/
120 SwXReferenceMark::~SwXReferenceMark()
124 /* -----------------03.11.99 14:14-------------------
126 --------------------------------------------------*/
127 void SwXReferenceMark::InsertRefMark( SwPaM& rPam )
129 //! in some cases when this function is called the pDoc pointer member may have become
130 //! invalid/deleted thus we obtain the document pointer from rPaM where it should always
131 //! be valid.
132 SwDoc *pDoc2 = rPam.GetDoc();
134 UnoActionContext aCont(pDoc2);
135 SwTxtAttr* pTxtAttr = 0;
136 SwFmtRefMark aRefMark(sMarkName);
137 SfxItemSet aSet(pDoc2->GetAttrPool(), RES_TXTATR_REFMARK, RES_TXTATR_REFMARK, 0L);
138 aSet.Put(aRefMark);
139 sal_Bool bMark = *rPam.GetPoint() != *rPam.GetMark();
140 SwXTextCursor::SetCrsrAttr(rPam, aSet, 0);
142 if( bMark && *rPam.GetPoint() > *rPam.GetMark())
143 rPam.Exchange();
145 if( bMark )
146 pTxtAttr = rPam.GetNode()->GetTxtNode()->GetTxtAttr(
147 rPam.GetPoint()->nContent, RES_TXTATR_REFMARK );
148 else
149 pTxtAttr = rPam.GetNode()->GetTxtNode()->GetTxtAttr(
150 rPam.GetPoint()->nContent.GetIndex()-1, RES_TXTATR_REFMARK );
152 if(pTxtAttr)
153 pMark = &pTxtAttr->GetRefMark();
155 pDoc2->GetUnoCallBack()->Add(this);
158 /* -----------------18.02.99 13:33-------------------
160 * --------------------------------------------------*/
161 void SwXReferenceMark::attachToRange(const uno::Reference< text::XTextRange > & xTextRange)
162 throw( lang::IllegalArgumentException, uno::RuntimeException )
164 if(!m_bIsDescriptor)
165 throw uno::RuntimeException();
166 uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
167 SwXTextRange* pRange = 0;
168 OTextCursorHelper* pCursor = 0;
169 if(xRangeTunnel.is())
171 pRange = reinterpret_cast< SwXTextRange * >(
172 sal::static_int_cast< sal_IntPtr >( xRangeTunnel->getSomething( SwXTextRange::getUnoTunnelId()) ));
173 pCursor = reinterpret_cast< OTextCursorHelper * >(
174 sal::static_int_cast< sal_IntPtr >( xRangeTunnel->getSomething( OTextCursorHelper::getUnoTunnelId()) ));
176 SwDoc* pDocument = pRange ? (SwDoc*)pRange->GetDoc() : pCursor ? (SwDoc*)pCursor->GetDoc() : 0;
177 if(pDocument)
179 SwUnoInternalPaM aPam(*pDocument);
180 //das muss jetzt sal_True liefern
181 SwXTextRange::XTextRangeToSwPaM(aPam, xTextRange);
182 InsertRefMark(aPam);
183 m_bIsDescriptor = sal_False;
184 pDoc = pDocument;
185 pDoc->GetUnoCallBack()->Add(this);
187 else
188 throw lang::IllegalArgumentException();
190 /*-- 11.12.98 10:28:34---------------------------------------------------
192 -----------------------------------------------------------------------*/
193 void SwXReferenceMark::attach(const uno::Reference< text::XTextRange > & xTextRange)
194 throw( lang::IllegalArgumentException, uno::RuntimeException )
196 vos::OGuard aGuard(Application::GetSolarMutex());
197 attachToRange( xTextRange );
199 /*-- 11.12.98 10:28:34---------------------------------------------------
201 -----------------------------------------------------------------------*/
202 uno::Reference< text::XTextRange > SwXReferenceMark::getAnchor(void) throw( uno::RuntimeException )
204 vos::OGuard aGuard(Application::GetSolarMutex());
205 uno::Reference< text::XTextRange > xRet;
206 if(IsValid())
208 const SwFmtRefMark* pNewMark = pDoc->GetRefMark(sMarkName);
209 if(pNewMark && pNewMark == pMark)
211 const SwTxtRefMark* pTxtMark = pMark->GetTxtRefMark();
212 if(pTxtMark &&
213 &pTxtMark->GetTxtNode().GetNodes() == &pDoc->GetNodes())
215 SwTxtNode& rTxtNode = (SwTxtNode&)pTxtMark->GetTxtNode();
216 SwPaM* pPam = pTxtMark->GetEnd() ?
217 new SwPaM( rTxtNode, *pTxtMark->GetEnd(),
218 rTxtNode, *pTxtMark->GetStart()) :
219 new SwPaM( rTxtNode, *pTxtMark->GetStart());
222 xRet = SwXTextRange::CreateTextRangeFromPosition(pDoc,
223 *pPam->Start(), pPam->End());
224 delete pPam;
228 return xRet;
230 /*-- 11.12.98 10:28:35---------------------------------------------------
232 -----------------------------------------------------------------------*/
233 void SwXReferenceMark::dispose(void) throw( uno::RuntimeException )
235 vos::OGuard aGuard(Application::GetSolarMutex());
236 if(IsValid())
238 const SwFmtRefMark* pNewMark = pDoc->GetRefMark(sMarkName);
239 if(pNewMark && pNewMark == pMark)
241 const SwTxtRefMark* pTxtMark = pMark->GetTxtRefMark();
242 if(pTxtMark &&
243 &pTxtMark->GetTxtNode().GetNodes() == &pDoc->GetNodes())
245 SwTxtNode& rTxtNode = (SwTxtNode&)pTxtMark->GetTxtNode();
246 xub_StrLen nStt = *pTxtMark->GetStart(),
247 nEnd = pTxtMark->GetEnd() ? *pTxtMark->GetEnd()
248 : nStt + 1;
250 SwPaM aPam( rTxtNode, nStt, rTxtNode, nEnd );
251 pDoc->DeleteAndJoin( aPam );
255 else
256 throw uno::RuntimeException();
258 /*-- 11.12.98 10:28:35---------------------------------------------------
260 -----------------------------------------------------------------------*/
261 void SwXReferenceMark::addEventListener(const uno::Reference< lang::XEventListener > & aListener) throw( uno::RuntimeException )
263 if(!GetRegisteredIn())
264 throw uno::RuntimeException();
265 aLstnrCntnr.AddListener(aListener);
267 /*-- 11.12.98 10:28:35---------------------------------------------------
269 -----------------------------------------------------------------------*/
270 void SwXReferenceMark::removeEventListener(const uno::Reference< lang::XEventListener > & aListener) throw( uno::RuntimeException )
272 if(!GetRegisteredIn() || !aLstnrCntnr.RemoveListener(aListener))
273 throw uno::RuntimeException();
275 /*-- 11.12.98 10:28:36---------------------------------------------------
277 -----------------------------------------------------------------------*/
278 OUString SwXReferenceMark::getName(void) throw( uno::RuntimeException )
280 vos::OGuard aGuard(Application::GetSolarMutex());
281 if(!IsValid() || !pDoc->GetRefMark(sMarkName))
283 throw uno::RuntimeException();
285 return sMarkName;
287 /*-- 11.12.98 10:28:36---------------------------------------------------
289 -----------------------------------------------------------------------*/
290 void SwXReferenceMark::setName(const OUString& Name_) throw( uno::RuntimeException )
292 vos::OGuard aGuard(Application::GetSolarMutex());
293 if(m_bIsDescriptor)
294 sMarkName = String(Name_);
295 else
297 String sNewName(Name_);
298 if(!IsValid() || !pDoc->GetRefMark(sMarkName) || pDoc->GetRefMark(sNewName))
300 throw uno::RuntimeException();
302 const SwFmtRefMark* pCurMark = pDoc->GetRefMark(sMarkName);
303 if(sNewName != sMarkName && pCurMark && pCurMark == pMark)
305 UnoActionContext aCont(pDoc);
306 const SwTxtRefMark* pTxtMark = pMark->GetTxtRefMark();
307 if(pTxtMark &&
308 &pTxtMark->GetTxtNode().GetNodes() == &pDoc->GetNodes())
310 SwTxtNode& rTxtNode = (SwTxtNode&)pTxtMark->GetTxtNode();
311 xub_StrLen nStt = *pTxtMark->GetStart(),
312 nEnd = pTxtMark->GetEnd() ? *pTxtMark->GetEnd()
313 : nStt + 1;
315 SwPaM aPam( rTxtNode, nStt, rTxtNode, nEnd );
316 pDoc->DeleteAndJoin( aPam ); //! deletes the pDoc member in the SwXReferenceMark
317 //! The aPam will keep the correct and functional doc though
319 sMarkName = sNewName;
320 //create a new one
321 InsertRefMark( aPam );
322 pDoc = aPam.GetDoc();
327 /* -----------------------------07.01.00 12:51--------------------------------
329 ---------------------------------------------------------------------------*/
330 void SwXReferenceMark::Invalidate()
332 if(GetRegisteredIn())
334 ((SwModify*)GetRegisteredIn())->Remove(this);
335 aLstnrCntnr.Disposing();
336 pDoc = 0;
337 pMark = 0;
340 /*-- 11.12.98 10:28:37---------------------------------------------------
342 -----------------------------------------------------------------------*/
343 void SwXReferenceMark::Modify( SfxPoolItem *pOld, SfxPoolItem *pNew)
345 switch( pOld ? pOld->Which() : 0 )
347 case RES_REMOVE_UNO_OBJECT:
348 case RES_OBJECTDYING:
349 if( (void*)GetRegisteredIn() == ((SwPtrMsgPoolItem *)pOld)->pObject )
350 Invalidate();
351 break;
352 case RES_FMT_CHG:
353 // wurden wir an das neue umgehaengt und wird das alte geloscht?
354 if( ((SwFmtChg*)pNew)->pChangedFmt == GetRegisteredIn() &&
355 ((SwFmtChg*)pOld)->pChangedFmt->IsFmtInDTOR() )
356 Invalidate();
357 break;
358 case RES_REFMARK_DELETED:
359 if( (void*)pMark == ((SwPtrMsgPoolItem *)pOld)->pObject )
360 Invalidate();
361 break;
365 /*-- 12.09.00 12:58:20---------------------------------------------------
367 -----------------------------------------------------------------------*/
368 uno::Reference< beans::XPropertySetInfo > SwXReferenceMark::getPropertySetInfo( ) throw(uno::RuntimeException)
370 static uno::Reference< beans::XPropertySetInfo > xRef =
371 aSwMapProvider.GetPropertySet(PROPERTY_MAP_PARAGRAPH_EXTENSIONS)->getPropertySetInfo();
372 return xRef;
374 /*-- 12.09.00 12:58:20---------------------------------------------------
376 -----------------------------------------------------------------------*/
377 void SwXReferenceMark::setPropertyValue(
378 const OUString& /*rPropertyName*/, const uno::Any& /*rValue*/ )
379 throw(beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
381 throw lang::IllegalArgumentException();
383 /*-- 12.09.00 12:58:20---------------------------------------------------
385 -----------------------------------------------------------------------*/
386 uno::Any SwXReferenceMark::getPropertyValue( const OUString& rPropertyName )
387 throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
389 uno::Any aRet;
390 if(!SwXParagraph::getDefaultTextContentValue(aRet, rPropertyName))
391 throw beans::UnknownPropertyException();
392 return aRet;
394 /*-- 12.09.00 12:58:20---------------------------------------------------
396 -----------------------------------------------------------------------*/
397 void SwXReferenceMark::addPropertyChangeListener(
398 const OUString& /*rPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
399 throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
402 /*-- 12.09.00 12:58:20---------------------------------------------------
404 -----------------------------------------------------------------------*/
405 void SwXReferenceMark::removePropertyChangeListener(
406 const OUString& /*rPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
407 throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
410 /*-- 12.09.00 12:58:20---------------------------------------------------
412 -----------------------------------------------------------------------*/
413 void SwXReferenceMark::addVetoableChangeListener( const OUString& /*rPropertyName*/,
414 const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
415 throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
418 /*-- 12.09.00 12:58:21---------------------------------------------------
420 -----------------------------------------------------------------------*/
421 void SwXReferenceMark::removeVetoableChangeListener(
422 const OUString& /*rPropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
423 throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)