Update ooo320-m1
[ooovba.git] / sw / source / core / unocore / unobkm.cxx
blob61d1557d3c831adfc29ded11f0c5136649c22948
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: unobkm.cxx,v $
10 * $Revision: 1.16 $
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 <unoprnms.hxx>
40 #include <IMark.hxx>
41 #include <crossrefbookmark.hxx>
42 #include <doc.hxx>
43 #include <docary.hxx>
44 #include <swundo.hxx>
45 #ifndef _COMCORE_HRC
46 #include <comcore.hrc>
47 #endif
48 #include <undobj.hxx>
49 #include <docsh.hxx>
52 using namespace ::sw::mark;
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::text;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::beans;
59 using ::rtl::OUString;
60 using ::rtl::OUStringBuffer;
62 namespace
64 static OUString lcl_QuoteName(const OUString& rName)
66 static const OUString sStart = OUString(String(SW_RES(STR_START_QUOTE)));
67 static const OUString sEnd = OUString(String(SW_RES(STR_END_QUOTE)));
68 OUStringBuffer sBuf(64);
69 return sBuf.append(sStart).append(rName).append(sEnd).makeStringAndClear();
73 /******************************************************************
74 * SwXBookmark
75 ******************************************************************/
76 TYPEINIT1(SwXBookmark, SwClient)
78 const uno::Sequence< sal_Int8 > & SwXBookmark::getUnoTunnelId()
80 static uno::Sequence< sal_Int8 > aSeq = ::CreateUnoTunnelId();
81 return aSeq;
84 sal_Int64 SAL_CALL SwXBookmark::getSomething( const uno::Sequence< sal_Int8 >& rId )
85 throw(uno::RuntimeException)
87 if( rId.getLength() == 16
88 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ) )
90 return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >(this) );
92 return 0;
95 SwXBookmark::SwXBookmark(::sw::mark::IMark* pBkmk, SwDoc* pDoc)
96 : m_aLstnrCntnr((text::XTextContent*)this)
97 , m_pDoc(pDoc)
98 , m_pRegisteredBookmark(NULL)
100 registerInMark(pBkmk);
103 SwXBookmark::~SwXBookmark()
106 void SwXBookmark::attachToRangeEx(
107 const uno::Reference< text::XTextRange > & xTextRange,
108 IDocumentMarkAccess::MarkType eType)
109 throw(lang::IllegalArgumentException, uno::RuntimeException)
111 if(m_pRegisteredBookmark)
112 throw uno::RuntimeException();
114 uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
115 SwXTextRange* pRange = 0;
116 OTextCursorHelper* pCursor = 0;
117 if(xRangeTunnel.is())
120 pRange = reinterpret_cast< SwXTextRange * >(
121 sal::static_int_cast< sal_IntPtr >( xRangeTunnel->getSomething( SwXTextRange::getUnoTunnelId() )));
122 pCursor = reinterpret_cast< OTextCursorHelper * >(
123 sal::static_int_cast< sal_IntPtr >( xRangeTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
126 SwDoc* pDc = pRange ? (SwDoc*)pRange->GetDoc() : pCursor ?
127 (SwDoc*)pCursor->GetDoc() : 0;
128 if(pDc)
130 m_pDoc = pDc;
131 SwUnoInternalPaM aPam(*m_pDoc);
132 SwXTextRange::XTextRangeToSwPaM(aPam, xTextRange);
133 UnoActionContext aCont(m_pDoc);
134 if(!m_aName.Len())
135 m_aName = OUString::createFromAscii("Bookmark");
136 if(eType == IDocumentMarkAccess::BOOKMARK && ::sw::mark::CrossRefNumItemBookmark::IsLegalName(m_aName))
137 eType = IDocumentMarkAccess::CROSSREF_NUMITEM_BOOKMARK;
138 else if(eType == IDocumentMarkAccess::BOOKMARK && ::sw::mark::CrossRefHeadingBookmark::IsLegalName(m_aName))
139 eType = IDocumentMarkAccess::CROSSREF_HEADING_BOOKMARK;
140 registerInMark(m_pDoc->getIDocumentMarkAccess()->makeMark(aPam, m_aName, eType));
141 // --> OD 2007-10-23 #i81002#
142 // Check, if bookmark has been created.
143 // E.g., the creation of a cross-reference bookmark is suppress,
144 // if the PaM isn't a valid one for cross-reference bookmarks.
145 if(!m_pRegisteredBookmark)
147 OSL_ENSURE(false,
148 "<SwXBookmark::attachToRange(..)>"
149 " - could not create Mark.");
150 throw lang::IllegalArgumentException();
152 // <--
154 else
155 throw lang::IllegalArgumentException();
158 void SwXBookmark::attachToRange(const uno::Reference< text::XTextRange > & xTextRange)
159 throw( lang::IllegalArgumentException, uno::RuntimeException )
161 attachToRangeEx(xTextRange, IDocumentMarkAccess::BOOKMARK);
164 void SwXBookmark::attach(const uno::Reference< text::XTextRange > & xTextRange)
165 throw( lang::IllegalArgumentException, uno::RuntimeException )
167 vos::OGuard aGuard(Application::GetSolarMutex());
168 attachToRange( xTextRange );
171 uno::Reference< text::XTextRange > SwXBookmark::getAnchor(void)
172 throw( uno::RuntimeException )
174 vos::OGuard aGuard(Application::GetSolarMutex());
175 uno::Reference< text::XTextRange > aRet;
176 if(m_pRegisteredBookmark)
177 aRet = SwXTextRange::CreateTextRangeFromPosition(
178 m_pDoc,
179 m_pRegisteredBookmark->GetMarkPos(),
180 m_pRegisteredBookmark->IsExpanded() ? &m_pRegisteredBookmark->GetOtherMarkPos() : NULL);
181 else
182 throw uno::RuntimeException();
183 return aRet;
186 void SwXBookmark::dispose(void)
187 throw( uno::RuntimeException )
189 vos::OGuard aGuard(Application::GetSolarMutex());
190 if(m_pRegisteredBookmark)
191 GetDoc()->getIDocumentMarkAccess()->deleteMark(m_pRegisteredBookmark);
192 else
193 throw uno::RuntimeException();
196 void SwXBookmark::addEventListener(const uno::Reference< lang::XEventListener > & aListener)
197 throw( uno::RuntimeException )
199 if(!m_pRegisteredBookmark)
200 throw uno::RuntimeException();
201 m_aLstnrCntnr.AddListener(aListener);
204 void SwXBookmark::removeEventListener(const uno::Reference< lang::XEventListener > & aListener)
205 throw( uno::RuntimeException )
207 if(!m_pRegisteredBookmark || !m_aLstnrCntnr.RemoveListener(aListener))
208 throw uno::RuntimeException();
211 OUString SwXBookmark::getName(void)
212 throw(uno::RuntimeException)
214 vos::OGuard aGuard(Application::GetSolarMutex());
215 OUString sRet;
216 if(m_pRegisteredBookmark)
217 sRet = m_pRegisteredBookmark->GetName();
218 else
219 sRet = m_aName;
220 return sRet;
223 void SwXBookmark::setName(const OUString& rName)
224 throw(uno::RuntimeException)
226 vos::OGuard aGuard(Application::GetSolarMutex());
227 if(!m_pRegisteredBookmark)
228 m_aName = rName;
229 if(!m_pRegisteredBookmark || getName() == rName)
230 return;
231 IDocumentMarkAccess* const pMarkAccess = m_pDoc->getIDocumentMarkAccess();
232 if(pMarkAccess->findMark(rName) != pMarkAccess->getMarksEnd())
233 throw uno::RuntimeException();
235 SwPaM aPam(m_pRegisteredBookmark->GetMarkPos());
236 if(m_pRegisteredBookmark->IsExpanded())
238 aPam.SetMark();
239 *aPam.GetMark() = m_pRegisteredBookmark->GetOtherMarkPos();
242 SwRewriter aRewriter;
243 aRewriter.AddRule(UNDO_ARG1, lcl_QuoteName(getName()));
244 aRewriter.AddRule(UNDO_ARG2, SW_RES(STR_YIELDS));
245 aRewriter.AddRule(UNDO_ARG3, lcl_QuoteName(rName));
247 m_pDoc->StartUndo(UNDO_BOOKMARK_RENAME, &aRewriter);
248 pMarkAccess->renameMark(m_pRegisteredBookmark, rName);
249 m_pDoc->EndUndo(UNDO_BOOKMARK_RENAME, NULL);
252 OUString SwXBookmark::getImplementationName(void) throw( uno::RuntimeException )
254 return OUString::createFromAscii("SwXBookmark");
257 sal_Bool SwXBookmark::supportsService(const OUString& rServiceName)
258 throw( uno::RuntimeException )
260 return !rServiceName.compareToAscii("com.sun.star.text.Bookmark") ||
261 !rServiceName.compareToAscii("com.sun.star.document.LinkTarget") ||
262 !rServiceName.compareToAscii("com.sun.star.text.TextContent");
265 uno::Sequence< OUString > SwXBookmark::getSupportedServiceNames(void)
266 throw( uno::RuntimeException )
268 uno::Sequence< OUString > aRet(3);
269 aRet[0] = OUString::createFromAscii("com.sun.star.text.Bookmark");
270 aRet[1] = OUString::createFromAscii("com.sun.star.document.LinkTarget");
271 aRet[2] = OUString::createFromAscii("com.sun.star.text.TextContent");
272 return aRet;
275 void SwXBookmark::Modify(SfxPoolItem *pOld, SfxPoolItem *pNew)
277 ClientModify(this, pOld, pNew);
278 if(!GetRegisteredIn())
280 m_pRegisteredBookmark = NULL;
281 m_pDoc = NULL;
282 m_aLstnrCntnr.Disposing();
286 // MetadatableMixin
287 ::sfx2::Metadatable* SwXBookmark::GetCoreObject()
289 return dynamic_cast< ::sfx2::Metadatable* >( GetBookmark() );
292 uno::Reference<frame::XModel> SwXBookmark::GetModel()
294 if (GetDoc())
296 SwDocShell const * const pShell( GetDoc()->GetDocShell() );
297 return (pShell) ? pShell->GetModel() : 0;
299 return 0;
303 uno::Reference< beans::XPropertySetInfo > SwXBookmark::getPropertySetInfo(void)
304 throw( uno::RuntimeException )
306 static uno::Reference< beans::XPropertySetInfo > aRef;
307 if(!aRef.is())
309 aRef = aSwMapProvider.GetPropertySet(PROPERTY_MAP_BOOKMARK)->getPropertySetInfo();
311 return aRef;
314 void SwXBookmark::setPropertyValue(const OUString& PropertyName, const uno::Any& /*aValue*/)
315 throw( beans::UnknownPropertyException,
316 beans::PropertyVetoException,
317 lang::IllegalArgumentException,
318 lang::WrappedTargetException,
319 uno::RuntimeException )
321 // nothing to set here
322 throw IllegalArgumentException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Property is read-only: " ) ) + PropertyName, static_cast < cppu::OWeakObject * > ( this ), 0 );
325 uno::Any SwXBookmark::getPropertyValue(const OUString& rPropertyName)
326 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
328 uno::Any aRet;
329 if(!SwXParagraph::getDefaultTextContentValue(aRet, rPropertyName))
331 if(rPropertyName.equalsAsciiL( SW_PROP_NAME(UNO_LINK_DISPLAY_NAME)))
332 aRet <<= getName();
334 return aRet;
337 void SwXBookmark::addPropertyChangeListener(const OUString& /*PropertyName*/,
338 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
339 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
342 void SwXBookmark::removePropertyChangeListener(const OUString& /*PropertyName*/,
343 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
344 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
347 void SwXBookmark::addVetoableChangeListener(const OUString& /*PropertyName*/,
348 const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
349 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
352 void SwXBookmark::removeVetoableChangeListener(const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
353 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
356 SwXFieldmark::SwXFieldmark(bool _isReplacementObject, ::sw::mark::IMark* pBkm, SwDoc* pDc)
357 : SwXFieldmark_BASE(pBkm, pDc)
358 , isReplacementObject(_isReplacementObject)
361 void SwXFieldmark::attachToRange( const uno::Reference < text::XTextRange >& xTextRange )
362 throw( lang::IllegalArgumentException, uno::RuntimeException )
364 attachToRangeEx( xTextRange,
365 ( isReplacementObject ? IDocumentMarkAccess::CHECKBOX_FIELDMARK : IDocumentMarkAccess::TEXT_FIELDMARK ) );
368 ::rtl::OUString SwXFieldmark::getFieldType( void )
369 throw( ::com::sun::star::uno::RuntimeException )
371 vos::OGuard aGuard( Application::GetSolarMutex( ) );
372 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
373 IFieldmark *pBkm = dynamic_cast< IFieldmark* > ( pMark );
374 if ( pBkm )
375 return pBkm->GetFieldname( );
376 else
377 throw uno::RuntimeException( );
380 void SwXFieldmark::setFieldType( const::rtl::OUString & fieldType )
381 throw( ::com::sun::star::uno::RuntimeException )
383 vos::OGuard aGuard( Application::GetSolarMutex( ) );
384 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
385 IFieldmark *pBkm = dynamic_cast< IFieldmark* >( pMark );
386 if ( pBkm )
387 pBkm->SetFieldname( fieldType );
388 else
389 throw uno::RuntimeException( );
392 sal_Int16 SwXFieldmark::getParamCount( )
393 throw( ::com::sun::star::uno::RuntimeException )
395 vos::OGuard aGuard( Application::GetSolarMutex( ) );
396 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
397 IFieldmark *pBkm = dynamic_cast< IFieldmark* >( pMark );
398 if ( pBkm )
399 return pBkm->getNumOfParams( );
400 else
401 throw uno::RuntimeException( );
404 rtl::OUString SwXFieldmark::getParamName( sal_Int16 i )
405 throw( ::com::sun::star::uno::RuntimeException )
407 vos::OGuard aGuard( Application::GetSolarMutex( ) );
408 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
409 IFieldmark *pBkm = dynamic_cast< IFieldmark* >( pMark );
410 if ( pBkm )
411 return pBkm->getParam( i ).first;
412 else
413 throw uno::RuntimeException( );
416 ::rtl::OUString SwXFieldmark::getParamValue( ::sal_Int16 i )
417 throw( ::com::sun::star::uno::RuntimeException )
419 vos::OGuard aGuard( Application::GetSolarMutex( ) );
420 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
421 IFieldmark *pBkm = dynamic_cast< IFieldmark* >( pMark );
422 if ( pBkm )
423 return pBkm->getParam( i ).second;
424 else
425 throw uno::RuntimeException( );
428 void SwXFieldmark::addParam( const ::rtl::OUString & name,
429 const ::rtl::OUString & value,
430 sal_Bool replaceExisting )
431 throw( ::com::sun::star::uno::RuntimeException )
433 vos::OGuard aGuard( Application::GetSolarMutex( ) );
434 IMark* pMark = const_cast< IMark* >( GetBookmark( ) );
435 IFieldmark *pBkm = dynamic_cast< IFieldmark* >( pMark );
436 if ( pBkm )
437 pBkm->addParam( const_cast< rtl::OUString& >( name ),
438 const_cast< rtl::OUString& >( value ),
439 replaceExisting );
440 else
441 throw uno::RuntimeException( );