xref: /aoo42x/main/sw/source/core/crsr/bookmrk.cxx (revision 3b32dd21)
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     static void lcl_RemoveFieldMarks(Fieldmark* const pField,
99         SwDoc* const io_pDoc,
100         const sal_Unicode aStartMark,
101         const sal_Unicode aEndMark)
102     {
103         SwPosition& rStart = pField->GetMarkStart();
104         SwPosition& rEnd = pField->GetMarkEnd();
105         SwTxtNode const*const pStartTxtNode = rStart.nNode.GetNode().GetTxtNode();
106         SwTxtNode const*const pEndTxtNode = rEnd.nNode.GetNode().GetTxtNode();
107         const sal_Unicode ch_start=pStartTxtNode->GetTxt().GetChar(rStart.nContent.GetIndex());
108         xub_StrLen nEndPos = ( rEnd == rStart ||  rEnd.nContent.GetIndex() == 0 ) ?
109             rEnd.nContent.GetIndex() : rEnd.nContent.GetIndex() - 1;
110         const sal_Unicode ch_end=pEndTxtNode->GetTxt().GetChar( nEndPos );
111         SwPaM aStartPaM(rStart);
112         SwPaM aEndPaM(rEnd);
113         io_pDoc->GetIDocumentUndoRedo().StartUndo(UNDO_UI_REPLACE, NULL);
114         if( ch_start == aStartMark )
115         {
116             SwPaM aStart(rStart, rStart);
117             aStart.End()->nContent++;
118             io_pDoc->DeleteRange(aStart);
119         }
120         if ( ch_end == aEndMark )
121         {
122             SwPaM aEnd(rEnd, rEnd);
123             aEnd.Start()->nContent--;
124             io_pDoc->DeleteRange(aEnd);
125         }
126         io_pDoc->GetIDocumentUndoRedo().EndUndo(UNDO_UI_REPLACE, NULL);
127     };
128 }
129 
130 namespace sw { namespace mark
131 {
132     MarkBase::MarkBase(const SwPaM& aPaM,
133         const ::rtl::OUString& rName)
134         : SwModify(0)
135         , m_pPos1(new SwPosition(*(aPaM.GetPoint())))
136         , m_aName(rName)
137     {
138         lcl_FixPosition(*m_pPos1);
139         if (aPaM.HasMark() && (*aPaM.GetMark() != *aPaM.GetPoint()))
140         {
141             MarkBase::SetOtherMarkPos(*(aPaM.GetMark()));
142             lcl_FixPosition(*m_pPos2);
143         }
144     }
145 
146     bool MarkBase::IsCoveringPosition(const SwPosition& rPos) const
147     {
148         return GetMarkStart() <= rPos && rPos <= GetMarkEnd();
149     }
150 
151     void MarkBase::SetMarkPos(const SwPosition& rNewPos)
152     {
153         ::boost::scoped_ptr<SwPosition>(new SwPosition(rNewPos)).swap(m_pPos1);
154         //lcl_FixPosition(*m_pPos1);
155     }
156 
157     void MarkBase::SetOtherMarkPos(const SwPosition& rNewPos)
158     {
159         ::boost::scoped_ptr<SwPosition>(new SwPosition(rNewPos)).swap(m_pPos2);
160         //lcl_FixPosition(*m_pPos2);
161     }
162 
163     rtl::OUString MarkBase::ToString( ) const
164     {
165         rtl::OUStringBuffer buf;
166         buf.appendAscii( "Mark: ( Name, [ Node1, Index1 ] ): ( " );
167         buf.append( m_aName ).appendAscii( ", [ " );
168         buf.append( sal_Int32( GetMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " );
169         buf.append( sal_Int32( GetMarkPos().nContent.GetIndex( ) ) ).appendAscii( " ] )" );
170 
171         return buf.makeStringAndClear( );
172     }
173 
174     MarkBase::~MarkBase()
175     { }
176 
177     ::rtl::OUString MarkBase::GenerateNewName(const ::rtl::OUString& rPrefix)
178     {
179         static rtlRandomPool aPool = rtl_random_createPool();
180         static ::rtl::OUString sUniquePostfix;
181         static sal_Int32 nCount = SAL_MAX_INT32;
182         ::rtl::OUStringBuffer aResult(rPrefix);
183         if(nCount == SAL_MAX_INT32)
184         {
185             sal_Int32 nRandom;
186             ::rtl::OUStringBuffer sUniquePostfixBuffer;
187             rtl_random_getBytes(aPool, &nRandom, sizeof(nRandom));
188             sUniquePostfix = ::rtl::OUStringBuffer(13).appendAscii("_").append(static_cast<sal_Int32>(abs(nRandom))).makeStringAndClear();
189             nCount = 0;
190         }
191         // putting the counter in front of the random parts will speed up string comparisons
192         return aResult.append(nCount++).append(sUniquePostfix).makeStringAndClear();
193     }
194 
195 
196     void MarkBase::Modify( const SfxPoolItem *pOld, const SfxPoolItem *pNew )
197     {
198         NotifyClients(pOld, pNew);
199         if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which()))
200         {   // invalidate cached uno object
201             SetXBookmark(uno::Reference<text::XTextContent>(0));
202         }
203     }
204 
205 
206     NavigatorReminder::NavigatorReminder(const SwPaM& rPaM)
207         : MarkBase(rPaM, our_sNamePrefix)
208     { }
209 
210     const ::rtl::OUString NavigatorReminder::our_sNamePrefix = ::rtl::OUString::createFromAscii("__NavigatorReminder__");
211 
212     UnoMark::UnoMark(const SwPaM& aPaM)
213         : MarkBase(aPaM, MarkBase::GenerateNewName(our_sNamePrefix))
214     { }
215 
216     const ::rtl::OUString UnoMark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__UnoMark__");
217 
218     DdeBookmark::DdeBookmark(const SwPaM& aPaM)
219         : MarkBase(aPaM, MarkBase::GenerateNewName(our_sNamePrefix))
220         , m_aRefObj(NULL)
221         , mbInDestruction( false )
222     { }
223 
224     void DdeBookmark::SetRefObject(SwServerObject* pObj)
225     {
226         m_aRefObj = pObj;
227     }
228 
229     const ::rtl::OUString DdeBookmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__DdeLink__");
230 
231     void DdeBookmark::DeregisterFromDoc(SwDoc* const pDoc)
232     {
233         if(m_aRefObj.Is())
234             pDoc->GetLinkManager().RemoveServer(m_aRefObj);
235     }
236 
237     DdeBookmark::~DdeBookmark()
238     {
239         mbInDestruction = true;
240         if( m_aRefObj.Is() )
241         {
242             if(m_aRefObj->HasDataLinks())
243             {
244                 ::sfx2::SvLinkSource* p = &m_aRefObj;
245                 p->SendDataChanged();
246             }
247             m_aRefObj->SetNoServer();
248         }
249     }
250 
251     Bookmark::Bookmark(const SwPaM& aPaM,
252         const KeyCode& rCode,
253         const ::rtl::OUString& rName,
254         const ::rtl::OUString& rShortName)
255         : DdeBookmark(aPaM)
256         , ::sfx2::Metadatable()
257         , m_aCode(rCode)
258         , m_sShortName(rShortName)
259     {
260         m_aName = rName;
261     }
262 
263     void Bookmark::InitDoc(SwDoc* const io_pDoc)
264     {
265         if (io_pDoc->GetIDocumentUndoRedo().DoesUndo())
266         {
267             io_pDoc->GetIDocumentUndoRedo().AppendUndo(
268                     new SwUndoInsBookmark(*this));
269         }
270         io_pDoc->SetModified();
271     }
272 
273     // ::sfx2::Metadatable
274     ::sfx2::IXmlIdRegistry& Bookmark::GetRegistry()
275     {
276         SwDoc *const pDoc( GetMarkPos().GetDoc() );
277         OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?");
278         return pDoc->GetXmlIdRegistry();
279     }
280 
281     bool Bookmark::IsInClipboard() const
282     {
283         SwDoc *const pDoc( GetMarkPos().GetDoc() );
284         OSL_ENSURE(pDoc, "Bookmark::IsInClipboard: no doc?");
285         return pDoc->IsClipBoard();
286     }
287 
288     bool Bookmark::IsInUndo() const
289     {
290         return false;
291     }
292 
293     bool Bookmark::IsInContent() const
294     {
295         SwDoc *const pDoc( GetMarkPos().GetDoc() );
296         OSL_ENSURE(pDoc, "Bookmark::IsInContent: no doc?");
297         return !pDoc->IsInHeaderFooter( SwNodeIndex(GetMarkPos().nNode) );
298     }
299 
300     uno::Reference< rdf::XMetadatable > Bookmark::MakeUnoObject()
301     {
302         // create new SwXBookmark
303         SwDoc *const pDoc( GetMarkPos().GetDoc() );
304         OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?");
305         const uno::Reference< rdf::XMetadatable> xMeta(
306                 SwXBookmark::CreateXBookmark(*pDoc, *this), uno::UNO_QUERY);
307         return xMeta;
308     }
309 
310 
311     Fieldmark::Fieldmark(const SwPaM& rPaM)
312         : MarkBase(rPaM, MarkBase::GenerateNewName(our_sNamePrefix))
313     {
314         if(!IsExpanded())
315             SetOtherMarkPos(GetMarkPos());
316     }
317 
318     rtl::OUString Fieldmark::ToString( ) const
319     {
320         rtl::OUStringBuffer buf;
321         buf.appendAscii( "Fieldmark: ( Name, Type, [ Nd1, Id1 ], [ Nd2, Id2 ] ): ( " );
322         buf.append( m_aName ).appendAscii( ", " );
323         buf.append( m_aFieldname ).appendAscii( ", [ " );
324         buf.append( sal_Int32( GetMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " );
325         buf.append( sal_Int32( GetMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ], [" );
326         buf.append( sal_Int32( GetOtherMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " );
327         buf.append( sal_Int32( GetOtherMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ] ) " );
328 
329         return buf.makeStringAndClear( );
330     }
331 
332     void Fieldmark::Invalidate( )
333     {
334         // @TODO: Does exist a better solution to trigger a format of the
335         //        fieldmark portion? If yes, please use it.
336         SwPaM aPaM( this->GetMarkPos(), this->GetOtherMarkPos() );
337         aPaM.InvalidatePaM();
338     }
339 
340     const ::rtl::OUString Fieldmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__Fieldmark__");
341 
342     TextFieldmark::TextFieldmark(const SwPaM& rPaM)
343         : Fieldmark(rPaM)
344     { }
345 
346     void TextFieldmark::InitDoc(SwDoc* const io_pDoc)
347     {
348         lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDEND);
349     }
350 
351     void TextFieldmark::ReleaseDoc(SwDoc* const pDoc)
352     {
353         lcl_RemoveFieldMarks(this, pDoc, CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDEND);
354     }
355 
356     CheckboxFieldmark::CheckboxFieldmark(const SwPaM& rPaM)
357         : Fieldmark(rPaM)
358     { }
359 
360     void CheckboxFieldmark::InitDoc(SwDoc* const io_pDoc)
361     {
362         lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FORMELEMENT, CH_TXT_ATR_FIELDEND);
363 
364         // For some reason the end mark is moved from 1 by the Insert: we don't
365         // want this for checkboxes
366         this->GetMarkEnd( ).nContent--;
367     }
368     void CheckboxFieldmark::SetChecked(bool checked)
369     {
370         (*GetParameters())[::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT)] = makeAny(checked);
371     }
372 
373     bool CheckboxFieldmark::IsChecked() const
374     {
375         bool bResult = false;
376         parameter_map_t::const_iterator pResult = GetParameters()->find(::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT));
377         if(pResult != GetParameters()->end())
378             pResult->second >>= bResult;
379         return bResult;
380     }
381 
382 }}
383