xref: /trunk/main/sw/source/core/crsr/bookmrk.cxx (revision b0b7a757)
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         , mbInDestruction( false )
191     { }
192 
193     void DdeBookmark::SetRefObject(SwServerObject* pObj)
194     {
195         m_aRefObj = pObj;
196     }
197 
198     const ::rtl::OUString DdeBookmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__DdeLink__");
199 
200     void DdeBookmark::DeregisterFromDoc(SwDoc* const pDoc)
201     {
202         if(m_aRefObj.Is())
203             pDoc->GetLinkManager().RemoveServer(m_aRefObj);
204     }
205 
206     DdeBookmark::~DdeBookmark()
207     {
208         mbInDestruction = true;
209         if( m_aRefObj.Is() )
210         {
211             if(m_aRefObj->HasDataLinks())
212             {
213                 ::sfx2::SvLinkSource* p = &m_aRefObj;
214                 p->SendDataChanged();
215             }
216             m_aRefObj->SetNoServer();
217         }
218     }
219 
220     Bookmark::Bookmark(const SwPaM& aPaM,
221         const KeyCode& rCode,
222         const ::rtl::OUString& rName,
223         const ::rtl::OUString& rShortName)
224         : DdeBookmark(aPaM)
225         , ::sfx2::Metadatable()
226         , m_aCode(rCode)
227         , m_sShortName(rShortName)
228     {
229         m_aName = rName;
230     }
231 
232     void Bookmark::InitDoc(SwDoc* const io_pDoc)
233     {
234         if (io_pDoc->GetIDocumentUndoRedo().DoesUndo())
235         {
236             io_pDoc->GetIDocumentUndoRedo().AppendUndo(
237                     new SwUndoInsBookmark(*this));
238         }
239         io_pDoc->SetModified();
240     }
241 
242     // ::sfx2::Metadatable
243     ::sfx2::IXmlIdRegistry& Bookmark::GetRegistry()
244     {
245         SwDoc *const pDoc( GetMarkPos().GetDoc() );
246         OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?");
247         return pDoc->GetXmlIdRegistry();
248     }
249 
250     bool Bookmark::IsInClipboard() const
251     {
252         SwDoc *const pDoc( GetMarkPos().GetDoc() );
253         OSL_ENSURE(pDoc, "Bookmark::IsInClipboard: no doc?");
254         return pDoc->IsClipBoard();
255     }
256 
257     bool Bookmark::IsInUndo() const
258     {
259         return false;
260     }
261 
262     bool Bookmark::IsInContent() const
263     {
264         SwDoc *const pDoc( GetMarkPos().GetDoc() );
265         OSL_ENSURE(pDoc, "Bookmark::IsInContent: no doc?");
266         return !pDoc->IsInHeaderFooter( SwNodeIndex(GetMarkPos().nNode) );
267     }
268 
269     uno::Reference< rdf::XMetadatable > Bookmark::MakeUnoObject()
270     {
271         // create new SwXBookmark
272         SwDoc *const pDoc( GetMarkPos().GetDoc() );
273         OSL_ENSURE(pDoc, "Bookmark::MakeUnoObject: no doc?");
274         const uno::Reference< rdf::XMetadatable> xMeta(
275                 SwXBookmark::CreateXBookmark(*pDoc, *this), uno::UNO_QUERY);
276         return xMeta;
277     }
278 
279 
280     Fieldmark::Fieldmark(const SwPaM& rPaM)
281         : MarkBase(rPaM, MarkBase::GenerateNewName(our_sNamePrefix))
282     {
283         if(!IsExpanded())
284             SetOtherMarkPos(GetMarkPos());
285     }
286 
287     rtl::OUString Fieldmark::ToString( ) const
288     {
289         rtl::OUStringBuffer buf;
290         buf.appendAscii( "Fieldmark: ( Name, Type, [ Nd1, Id1 ], [ Nd2, Id2 ] ): ( " );
291         buf.append( m_aName ).appendAscii( ", " );
292         buf.append( m_aFieldname ).appendAscii( ", [ " );
293         buf.append( sal_Int32( GetMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " );
294         buf.append( sal_Int32( GetMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ], [" );
295         buf.append( sal_Int32( GetOtherMarkPos().nNode.GetIndex( ) ) ).appendAscii( ", " );
296         buf.append( sal_Int32( GetOtherMarkPos( ).nContent.GetIndex( ) ) ).appendAscii( " ] ) " );
297 
298         return buf.makeStringAndClear( );
299     }
300 
301     void Fieldmark::Invalidate( )
302     {
303         // @TODO: Does exist a better solution to trigger a format of the
304         //        fieldmark portion? If yes, please use it.
305         SwPaM aPaM( this->GetMarkPos(), this->GetOtherMarkPos() );
306         aPaM.InvalidatePaM();
307     }
308 
309     const ::rtl::OUString Fieldmark::our_sNamePrefix = ::rtl::OUString::createFromAscii("__Fieldmark__");
310 
311     TextFieldmark::TextFieldmark(const SwPaM& rPaM)
312         : Fieldmark(rPaM)
313     { }
314 
315     void TextFieldmark::InitDoc(SwDoc* const io_pDoc)
316     {
317         lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDEND);
318     }
319 
320     CheckboxFieldmark::CheckboxFieldmark(const SwPaM& rPaM)
321         : Fieldmark(rPaM)
322     { }
323 
324     void CheckboxFieldmark::InitDoc(SwDoc* const io_pDoc)
325     {
326         lcl_AssureFieldMarksSet(this, io_pDoc, CH_TXT_ATR_FORMELEMENT, CH_TXT_ATR_FIELDEND);
327 
328         // For some reason the end mark is moved from 1 by the Insert: we don't
329         // want this for checkboxes
330         this->GetMarkEnd( ).nContent--;
331     }
332     void CheckboxFieldmark::SetChecked(bool checked)
333     {
334         (*GetParameters())[::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT)] = makeAny(checked);
335     }
336 
337     bool CheckboxFieldmark::IsChecked() const
338     {
339         bool bResult = false;
340         parameter_map_t::const_iterator pResult = GetParameters()->find(::rtl::OUString::createFromAscii(ODF_FORMCHECKBOX_RESULT));
341         if(pResult != GetParameters()->end())
342             pResult->second >>= bResult;
343         return bResult;
344     }
345 
346 }}
347