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 <bookmrk.hxx> 29 #include <IDocumentMarkAccess.hxx> 30 #include <IDocumentUndoRedo.hxx> 31 #include <doc.hxx> 32 #include <errhdl.hxx> 33 #include <ndtxt.hxx> 34 #include <pam.hxx> 35 #include <swserv.hxx> 36 #include <sfx2/linkmgr.hxx> 37 #include <swtypes.hxx> 38 #include <UndoBookmark.hxx> 39 #include <unobookmark.hxx> 40 #include <rtl/random.h> 41 #include <xmloff/odffields.hxx> 42 43 44 SV_IMPL_REF( SwServerObject ) 45 46 using namespace ::sw::mark; 47 using namespace ::com::sun::star; 48 using namespace ::com::sun::star::uno; 49 50 namespace 51 { 52 static void lcl_FixPosition(SwPosition& rPos) 53 { 54 // make sure the position has 1) the proper node, and 2) a proper index 55 SwTxtNode* pTxtNode = rPos.nNode.GetNode().GetTxtNode(); 56 if(pTxtNode == NULL && rPos.nContent.GetIndex() > 0) 57 { 58 OSL_TRACE( 59 "bookmrk.cxx::lcl_FixPosition" 60 " - illegal position: %d without proper TxtNode", rPos.nContent.GetIndex()); 61 rPos.nContent.Assign(NULL, 0); 62 } 63 else if(pTxtNode != NULL && rPos.nContent.GetIndex() > pTxtNode->Len()) 64 { 65 OSL_TRACE( 66 "bookmrk.cxx::lcl_FixPosition" 67 " - illegal position: %d is beyond %d", rPos.nContent.GetIndex(), pTxtNode->Len()); 68 rPos.nContent.Assign(pTxtNode, pTxtNode->Len()); 69 } 70 }; 71 72 static void lcl_AssureFieldMarksSet(Fieldmark* const pField, 73 SwDoc* const io_pDoc, 74 const sal_Unicode aStartMark, 75 const sal_Unicode aEndMark) 76 { 77 SwPosition& rStart = pField->GetMarkStart(); 78 SwPosition& rEnd = pField->GetMarkEnd(); 79 SwTxtNode const*const pStartTxtNode = 80 rStart.nNode.GetNode().GetTxtNode(); 81 SwTxtNode const*const pEndTxtNode = rEnd.nNode.GetNode().GetTxtNode(); 82 const sal_Unicode ch_start=pStartTxtNode->GetTxt().GetChar(rStart.nContent.GetIndex()); 83 const sal_Unicode ch_end=pEndTxtNode->GetTxt().GetChar(rEnd.nContent.GetIndex()-1); 84 SwPaM aStartPaM(rStart); 85 SwPaM aEndPaM(rEnd); 86 io_pDoc->GetIDocumentUndoRedo().StartUndo(UNDO_UI_REPLACE, NULL); 87 if(ch_start != aStartMark) 88 { 89 io_pDoc->InsertString(aStartPaM, aStartMark); 90 } 91 if ( aEndMark && ( ch_end != aEndMark ) && ( rStart != rEnd ) ) 92 { 93 io_pDoc->InsertString(aEndPaM, aEndMark); 94 } 95 io_pDoc->GetIDocumentUndoRedo().EndUndo(UNDO_UI_REPLACE, NULL); 96 }; 97 } 98 99 namespace sw { namespace mark 100 { 101 MarkBase::MarkBase(const SwPaM& aPaM, 102 const ::rtl::OUString& rName) 103 : SwModify(0) 104 , m_pPos1(new SwPosition(*(aPaM.GetPoint()))) 105 , m_aName(rName) 106 { 107 lcl_FixPosition(*m_pPos1); 108 if (aPaM.HasMark() && (*aPaM.GetMark() != *aPaM.GetPoint())) 109 { 110 MarkBase::SetOtherMarkPos(*(aPaM.GetMark())); 111 lcl_FixPosition(*m_pPos2); 112 } 113 } 114 115 bool MarkBase::IsCoveringPosition(const SwPosition& rPos) const 116 { 117 return GetMarkStart() <= rPos && rPos <= GetMarkEnd(); 118 } 119 120 void MarkBase::SetMarkPos(const SwPosition& rNewPos) 121 { 122 ::boost::scoped_ptr<SwPosition>(new SwPosition(rNewPos)).swap(m_pPos1); 123 //lcl_FixPosition(*m_pPos1); 124 } 125 126 void MarkBase::SetOtherMarkPos(const SwPosition& rNewPos) 127 { 128 ::boost::scoped_ptr<SwPosition>(new SwPosition(rNewPos)).swap(m_pPos2); 129 //lcl_FixPosition(*m_pPos2); 130 } 131 132 rtl::OUString MarkBase::ToString( ) const 133 { 134 rtl::OUStringBuffer buf; 135 buf.appendAscii( "Mark: ( Name, [ Node1, Index1 ] ): ( " ); 136 buf.append( m_aName ).appendAscii( ", [ " ); 137 buf.append( sal_Int32( GetMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " ); 138 buf.append( sal_Int32( GetMarkPos().nContent.GetIndex( ) ) ).appendAscii( " ] )" ); 139 140 return buf.makeStringAndClear( ); 141 } 142 143 MarkBase::~MarkBase() 144 { } 145 146 ::rtl::OUString MarkBase::GenerateNewName(const ::rtl::OUString& rPrefix) 147 { 148 static rtlRandomPool aPool = rtl_random_createPool(); 149 static ::rtl::OUString sUniquePostfix; 150 static sal_Int32 nCount = SAL_MAX_INT32; 151 ::rtl::OUStringBuffer aResult(rPrefix); 152 if(nCount == SAL_MAX_INT32) 153 { 154 sal_Int32 nRandom; 155 ::rtl::OUStringBuffer sUniquePostfixBuffer; 156 rtl_random_getBytes(aPool, &nRandom, sizeof(nRandom)); 157 sUniquePostfix = ::rtl::OUStringBuffer(13).appendAscii("_").append(static_cast<sal_Int32>(abs(nRandom))).makeStringAndClear(); 158 nCount = 0; 159 } 160 // putting the counter in front of the random parts will speed up string comparisons 161 return aResult.append(nCount++).append(sUniquePostfix).makeStringAndClear(); 162 } 163 164 165 void MarkBase::Modify( const SfxPoolItem *pOld, const SfxPoolItem *pNew ) 166 { 167 NotifyClients(pOld, pNew); 168 if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) 169 { // invalidate cached uno object 170 SetXBookmark(uno::Reference<text::XTextContent>(0)); 171 } 172 } 173 174 175 NavigatorReminder::NavigatorReminder(const SwPaM& rPaM) 176 : MarkBase(rPaM, our_sNamePrefix) 177 { } 178 179 const ::rtl::OUString NavigatorReminder::our_sNamePrefix = ::rtl::OUString::createFromAscii("__NavigatorReminder__"); 180 181 UnoMark::UnoMark(const SwPaM& aPaM) 182 : MarkBase(aPaM, MarkBase::GenerateNewName(our_sNamePrefix)) 183 { } 184 185 const ::rtl::OUString UnoMark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__UnoMark__"); 186 187 DdeBookmark::DdeBookmark(const SwPaM& aPaM) 188 : MarkBase(aPaM, MarkBase::GenerateNewName(our_sNamePrefix)) 189 , m_aRefObj(NULL) 190 { } 191 192 void DdeBookmark::SetRefObject(SwServerObject* pObj) 193 { 194 m_aRefObj = pObj; 195 } 196 197 const ::rtl::OUString DdeBookmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__DdeLink__"); 198 199 void DdeBookmark::DeregisterFromDoc(SwDoc* const pDoc) 200 { 201 if(m_aRefObj.Is()) 202 pDoc->GetLinkManager().RemoveServer(m_aRefObj); 203 } 204 205 DdeBookmark::~DdeBookmark() 206 { 207 if( m_aRefObj.Is() ) 208 { 209 if(m_aRefObj->HasDataLinks()) 210 { 211 ::sfx2::SvLinkSource* p = &m_aRefObj; 212 p->SendDataChanged(); 213 } 214 m_aRefObj->SetNoServer(); 215 } 216 } 217 218 Bookmark::Bookmark(const SwPaM& aPaM, 219 const KeyCode& rCode, 220 const ::rtl::OUString& rName, 221 const ::rtl::OUString& rShortName) 222 : DdeBookmark(aPaM) 223 , ::sfx2::Metadatable() 224 , m_aCode(rCode) 225 , m_sShortName(rShortName) 226 { 227 m_aName = rName; 228 } 229 230 void Bookmark::InitDoc(SwDoc* const io_pDoc) 231 { 232 if (io_pDoc->GetIDocumentUndoRedo().DoesUndo()) 233 { 234 io_pDoc->GetIDocumentUndoRedo().AppendUndo( 235 new SwUndoInsBookmark(*this)); 236 } 237 io_pDoc->SetModified(); 238 } 239 240 // ::sfx2::Metadatable 241 ::sfx2::IXmlIdRegistry& Bookmark::GetRegistry() 242 { 243 SwDoc *const pDoc( GetMarkPos().GetDoc() ); 244 OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?"); 245 return pDoc->GetXmlIdRegistry(); 246 } 247 248 bool Bookmark::IsInClipboard() const 249 { 250 SwDoc *const pDoc( GetMarkPos().GetDoc() ); 251 OSL_ENSURE(pDoc, "Bookmark::IsInClipboard: no doc?"); 252 return pDoc->IsClipBoard(); 253 } 254 255 bool Bookmark::IsInUndo() const 256 { 257 return false; 258 } 259 260 bool Bookmark::IsInContent() const 261 { 262 SwDoc *const pDoc( GetMarkPos().GetDoc() ); 263 OSL_ENSURE(pDoc, "Bookmark::IsInContent: no doc?"); 264 return !pDoc->IsInHeaderFooter( SwNodeIndex(GetMarkPos().nNode) ); 265 } 266 267 uno::Reference< rdf::XMetadatable > Bookmark::MakeUnoObject() 268 { 269 // create new SwXBookmark 270 SwDoc *const pDoc( GetMarkPos().GetDoc() ); 271 OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?"); 272 const uno::Reference< rdf::XMetadatable> xMeta( 273 SwXBookmark::CreateXBookmark(*pDoc, *this), uno::UNO_QUERY); 274 return xMeta; 275 } 276 277 278 Fieldmark::Fieldmark(const SwPaM& rPaM) 279 : MarkBase(rPaM, MarkBase::GenerateNewName(our_sNamePrefix)) 280 { 281 if(!IsExpanded()) 282 SetOtherMarkPos(GetMarkPos()); 283 } 284 285 rtl::OUString Fieldmark::ToString( ) const 286 { 287 rtl::OUStringBuffer buf; 288 buf.appendAscii( "Fieldmark: ( Name, Type, [ Nd1, Id1 ], [ Nd2, Id2 ] ): ( " ); 289 buf.append( m_aName ).appendAscii( ", " ); 290 buf.append( m_aFieldname ).appendAscii( ", [ " ); 291 buf.append( sal_Int32( GetMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " ); 292 buf.append( sal_Int32( GetMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ], [" ); 293 buf.append( sal_Int32( GetOtherMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " ); 294 buf.append( sal_Int32( GetOtherMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ] ) " ); 295 296 return buf.makeStringAndClear( ); 297 } 298 299 void Fieldmark::Invalidate( ) 300 { 301 // @TODO: Does exist a better solution to trigger a format of the 302 // fieldmark portion? If yes, please use it. 303 SwPaM aPaM( this->GetMarkPos(), this->GetOtherMarkPos() ); 304 aPaM.InvalidatePaM(); 305 } 306 307 const ::rtl::OUString Fieldmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__Fieldmark__"); 308 309 TextFieldmark::TextFieldmark(const SwPaM& rPaM) 310 : Fieldmark(rPaM) 311 { } 312 313 void TextFieldmark::InitDoc(SwDoc* const io_pDoc) 314 { 315 lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDEND); 316 } 317 318 CheckboxFieldmark::CheckboxFieldmark(const SwPaM& rPaM) 319 : Fieldmark(rPaM) 320 { } 321 322 void CheckboxFieldmark::InitDoc(SwDoc* const io_pDoc) 323 { 324 lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FORMELEMENT, CH_TXT_ATR_FIELDEND); 325 326 // For some reason the end mark is moved from 1 by the Insert: we don't 327 // want this for checkboxes 328 this->GetMarkEnd( ).nContent--; 329 } 330 void CheckboxFieldmark::SetChecked(bool checked) 331 { 332 (*GetParameters())[::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT)] = makeAny(checked); 333 } 334 335 bool CheckboxFieldmark::IsChecked() const 336 { 337 bool bResult = false; 338 parameter_map_t::const_iterator pResult = GetParameters()->find(::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT)); 339 if(pResult != GetParameters()->end()) 340 pResult->second >>= bResult; 341 return bResult; 342 } 343 344 }} 345