Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "crsrsh.hxx"
21 : #include "ndtxt.hxx"
22 : #include <docary.hxx>
23 : #include <boost/bind.hpp>
24 :
25 : #include "IMark.hxx"
26 : #include "callnk.hxx"
27 : #include "swcrsr.hxx"
28 : #include <IDocumentMarkAccess.hxx>
29 : #include <IDocumentSettingAccess.hxx>
30 :
31 : using namespace std;
32 :
33 : namespace
34 : {
35 0 : struct CrsrStateHelper
36 : {
37 0 : CrsrStateHelper(SwCrsrShell& rShell)
38 : : m_aLink(rShell)
39 0 : , m_pCrsr(rShell.GetSwCrsr())
40 0 : , m_aSaveState(*m_pCrsr)
41 0 : { }
42 :
43 0 : void SetCrsrToMark(::sw::mark::IMark const * const pMark)
44 : {
45 0 : *(m_pCrsr->GetPoint()) = pMark->GetMarkStart();
46 0 : if(pMark->IsExpanded())
47 : {
48 0 : m_pCrsr->SetMark();
49 0 : *(m_pCrsr->GetMark()) = pMark->GetMarkEnd();
50 : }
51 0 : }
52 :
53 : /// returns true if the Cursor had been rolled back
54 0 : bool RollbackIfIllegal()
55 : {
56 0 : if(m_pCrsr->IsSelOvr(nsSwCursorSelOverFlags::SELOVER_CHECKNODESSECTION
57 0 : | nsSwCursorSelOverFlags::SELOVER_TOGGLE))
58 : {
59 0 : m_pCrsr->DeleteMark();
60 0 : m_pCrsr->RestoreSavePos();
61 0 : return true;
62 : }
63 0 : return false;
64 : }
65 :
66 : SwCallLink m_aLink;
67 : SwCursor* m_pCrsr;
68 : SwCrsrSaveState m_aSaveState;
69 : };
70 :
71 :
72 0 : static bool lcl_ReverseMarkOrderingByEnd(const IDocumentMarkAccess::pMark_t& rpFirst,
73 : const IDocumentMarkAccess::pMark_t& rpSecond)
74 : {
75 0 : return rpFirst->GetMarkEnd() > rpSecond->GetMarkEnd();
76 : }
77 :
78 0 : static bool lcl_IsInvisibleBookmark(IDocumentMarkAccess::pMark_t pMark)
79 : {
80 0 : return IDocumentMarkAccess::GetType(*pMark) != IDocumentMarkAccess::BOOKMARK;
81 : }
82 : }
83 :
84 : // at CurCrsr.SPoint
85 0 : ::sw::mark::IMark* SwCrsrShell::SetBookmark(
86 : const KeyCode& rCode,
87 : const ::rtl::OUString& rName,
88 : const ::rtl::OUString& rShortName,
89 : IDocumentMarkAccess::MarkType eMark)
90 : {
91 0 : StartAction();
92 0 : ::sw::mark::IMark* pMark = getIDocumentMarkAccess()->makeMark(
93 0 : *GetCrsr(),
94 : rName,
95 0 : eMark);
96 0 : ::sw::mark::IBookmark* pBookmark = dynamic_cast< ::sw::mark::IBookmark* >(pMark);
97 0 : if(pBookmark)
98 : {
99 0 : pBookmark->SetKeyCode(rCode);
100 0 : pBookmark->SetShortName(rShortName);
101 : }
102 0 : EndAction();
103 0 : return pMark;
104 : }
105 : // set CurCrsr.SPoint
106 :
107 0 : bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark, bool bAtStart)
108 : {
109 : // watch Crsr-Moves
110 0 : CrsrStateHelper aCrsrSt(*this);
111 0 : if ( bAtStart )
112 0 : *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkStart();
113 : else
114 0 : *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkEnd();
115 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
116 :
117 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
118 0 : return true;
119 : }
120 :
121 0 : bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark)
122 : {
123 : // watch Crsr-Moves
124 0 : CrsrStateHelper aCrsrSt(*this);
125 0 : aCrsrSt.SetCrsrToMark(pMark);
126 :
127 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
128 :
129 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
130 0 : return true;
131 : }
132 :
133 0 : bool SwCrsrShell::GoNextBookmark()
134 : {
135 0 : IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
136 0 : IDocumentMarkAccess::container_t vCandidates;
137 : remove_copy_if(
138 : upper_bound(
139 0 : pMarkAccess->getBookmarksBegin(),
140 0 : pMarkAccess->getBookmarksEnd(),
141 0 : *GetCrsr()->GetPoint(),
142 : boost::bind(&::sw::mark::IMark::StartsAfter, _2, _1)), // finds the first that is starting after
143 0 : pMarkAccess->getBookmarksEnd(),
144 : back_inserter(vCandidates),
145 0 : &lcl_IsInvisibleBookmark);
146 :
147 : // watch Crsr-Moves
148 0 : CrsrStateHelper aCrsrSt(*this);
149 0 : IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
150 0 : for(; ppMark!=vCandidates.end(); ++ppMark)
151 : {
152 0 : aCrsrSt.SetCrsrToMark(ppMark->get());
153 0 : if(!aCrsrSt.RollbackIfIllegal())
154 0 : break; // found legal move
155 : }
156 0 : if(ppMark==vCandidates.end())
157 : {
158 0 : SttEndDoc(false);
159 0 : return false;
160 : }
161 :
162 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
163 0 : return true;
164 : }
165 :
166 0 : bool SwCrsrShell::GoPrevBookmark()
167 : {
168 0 : IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
169 : // candidates from which to choose the mark before
170 : // no need to consider marks starting after rPos
171 0 : IDocumentMarkAccess::container_t vCandidates;
172 : remove_copy_if(
173 0 : pMarkAccess->getBookmarksBegin(),
174 : upper_bound(
175 0 : pMarkAccess->getBookmarksBegin(),
176 0 : pMarkAccess->getBookmarksEnd(),
177 0 : *GetCrsr()->GetPoint(),
178 : boost::bind(&::sw::mark::IMark::StartsAfter, _2, _1)),
179 : back_inserter(vCandidates),
180 0 : &lcl_IsInvisibleBookmark);
181 : sort(
182 : vCandidates.begin(),
183 : vCandidates.end(),
184 0 : &lcl_ReverseMarkOrderingByEnd);
185 :
186 : // watch Crsr-Moves
187 0 : CrsrStateHelper aCrsrSt(*this);
188 0 : IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
189 0 : for(; ppMark!=vCandidates.end(); ++ppMark)
190 : {
191 : // ignoring those not ending before the Crsr
192 : // (we were only able to eliminate those starting
193 : // behind the Crsr by the upper_bound(..)
194 : // above)
195 0 : if(!(**ppMark).EndsBefore(*GetCrsr()->GetPoint()))
196 0 : continue;
197 0 : aCrsrSt.SetCrsrToMark(ppMark->get());
198 0 : if(!aCrsrSt.RollbackIfIllegal())
199 0 : break; // found legal move
200 : }
201 0 : if(ppMark==vCandidates.end())
202 : {
203 0 : SttEndDoc(true);
204 0 : return false;
205 : }
206 :
207 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
208 0 : return true;
209 : }
210 :
211 236 : bool SwCrsrShell::IsFormProtected()
212 : {
213 236 : return getIDocumentSettingAccess()->get(IDocumentSettingAccess::PROTECT_FORM);
214 : }
215 :
216 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetCurrentFieldmark()
217 : {
218 : // TODO: Refactor
219 0 : SwPosition pos(*GetCrsr()->GetPoint());
220 0 : return getIDocumentMarkAccess()->getFieldmarkFor(pos);
221 : }
222 :
223 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkAfter()
224 : {
225 0 : SwPosition pos(*GetCrsr()->GetPoint());
226 0 : return getIDocumentMarkAccess()->getFieldmarkAfter(pos);
227 : }
228 :
229 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkBefore()
230 : {
231 0 : SwPosition pos(*GetCrsr()->GetPoint());
232 0 : return getIDocumentMarkAccess()->getFieldmarkBefore(pos);
233 : }
234 :
235 0 : bool SwCrsrShell::GotoFieldmark(::sw::mark::IFieldmark const * const pMark)
236 : {
237 0 : if(pMark==NULL) return false;
238 :
239 : // watch Crsr-Moves
240 0 : CrsrStateHelper aCrsrSt(*this);
241 0 : aCrsrSt.SetCrsrToMark(pMark);
242 0 : aCrsrSt.m_pCrsr->GetPoint()->nContent++;
243 0 : aCrsrSt.m_pCrsr->GetMark()->nContent--;
244 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
245 :
246 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
247 0 : return true;
248 30 : }
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|