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 :
24 : #include "IMark.hxx"
25 : #include "callnk.hxx"
26 : #include "swcrsr.hxx"
27 : #include <IDocumentMarkAccess.hxx>
28 : #include <IDocumentSettingAccess.hxx>
29 :
30 : using namespace std;
31 :
32 : namespace
33 : {
34 0 : struct CrsrStateHelper
35 : {
36 0 : CrsrStateHelper(SwCrsrShell& rShell)
37 : : m_aLink(rShell)
38 0 : , m_pCrsr(rShell.GetSwCrsr())
39 0 : , m_aSaveState(*m_pCrsr)
40 0 : { }
41 :
42 0 : void SetCrsrToMark(::sw::mark::IMark const * const pMark)
43 : {
44 0 : *(m_pCrsr->GetPoint()) = pMark->GetMarkStart();
45 0 : if(pMark->IsExpanded())
46 : {
47 0 : m_pCrsr->SetMark();
48 0 : *(m_pCrsr->GetMark()) = pMark->GetMarkEnd();
49 : }
50 0 : }
51 :
52 : /// returns true if the Cursor had been rolled back
53 0 : bool RollbackIfIllegal()
54 : {
55 0 : if(m_pCrsr->IsSelOvr(nsSwCursorSelOverFlags::SELOVER_CHECKNODESSECTION
56 0 : | nsSwCursorSelOverFlags::SELOVER_TOGGLE))
57 : {
58 0 : m_pCrsr->DeleteMark();
59 0 : m_pCrsr->RestoreSavePos();
60 0 : return true;
61 : }
62 0 : return false;
63 : }
64 :
65 : SwCallLink m_aLink;
66 : SwCursor* m_pCrsr;
67 : SwCrsrSaveState m_aSaveState;
68 : };
69 :
70 :
71 0 : static bool lcl_ReverseMarkOrderingByEnd(const IDocumentMarkAccess::pMark_t& rpFirst,
72 : const IDocumentMarkAccess::pMark_t& rpSecond)
73 : {
74 0 : return rpFirst->GetMarkEnd() > rpSecond->GetMarkEnd();
75 : }
76 :
77 0 : static bool lcl_IsInvisibleBookmark(IDocumentMarkAccess::pMark_t pMark)
78 : {
79 0 : return IDocumentMarkAccess::GetType(*pMark) != IDocumentMarkAccess::BOOKMARK;
80 : }
81 : }
82 :
83 : // at CurCrsr.SPoint
84 0 : ::sw::mark::IMark* SwCrsrShell::SetBookmark(
85 : const KeyCode& rCode,
86 : const OUString& rName,
87 : const OUString& rShortName,
88 : IDocumentMarkAccess::MarkType eMark)
89 : {
90 0 : StartAction();
91 0 : ::sw::mark::IMark* pMark = getIDocumentMarkAccess()->makeMark(
92 0 : *GetCrsr(),
93 : rName,
94 0 : eMark);
95 0 : ::sw::mark::IBookmark* pBookmark = dynamic_cast< ::sw::mark::IBookmark* >(pMark);
96 0 : if(pBookmark)
97 : {
98 0 : pBookmark->SetKeyCode(rCode);
99 0 : pBookmark->SetShortName(rShortName);
100 : }
101 0 : EndAction();
102 0 : return pMark;
103 : }
104 : // set CurCrsr.SPoint
105 :
106 0 : bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark, bool bAtStart)
107 : {
108 : // watch Crsr-Moves
109 0 : CrsrStateHelper aCrsrSt(*this);
110 0 : if ( bAtStart )
111 0 : *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkStart();
112 : else
113 0 : *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkEnd();
114 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
115 :
116 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
117 0 : return true;
118 : }
119 :
120 0 : bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark)
121 : {
122 : // watch Crsr-Moves
123 0 : CrsrStateHelper aCrsrSt(*this);
124 0 : aCrsrSt.SetCrsrToMark(pMark);
125 :
126 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
127 :
128 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
129 0 : return true;
130 : }
131 :
132 0 : bool SwCrsrShell::GoNextBookmark()
133 : {
134 0 : IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
135 0 : IDocumentMarkAccess::container_t vCandidates;
136 : remove_copy_if(
137 : upper_bound( // finds the first that is starting after
138 0 : pMarkAccess->getBookmarksBegin(),
139 0 : pMarkAccess->getBookmarksEnd(),
140 0 : *GetCrsr()->GetPoint(),
141 : sw::mark::CompareIMarkStartsAfter()),
142 0 : pMarkAccess->getBookmarksEnd(),
143 : back_inserter(vCandidates),
144 0 : &lcl_IsInvisibleBookmark);
145 :
146 : // watch Crsr-Moves
147 0 : CrsrStateHelper aCrsrSt(*this);
148 0 : IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
149 0 : for(; ppMark!=vCandidates.end(); ++ppMark)
150 : {
151 0 : aCrsrSt.SetCrsrToMark(ppMark->get());
152 0 : if(!aCrsrSt.RollbackIfIllegal())
153 0 : break; // found legal move
154 : }
155 0 : if(ppMark==vCandidates.end())
156 : {
157 0 : SttEndDoc(false);
158 0 : return false;
159 : }
160 :
161 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
162 0 : return true;
163 : }
164 :
165 0 : bool SwCrsrShell::GoPrevBookmark()
166 : {
167 0 : IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
168 : // candidates from which to choose the mark before
169 : // no need to consider marks starting after rPos
170 0 : IDocumentMarkAccess::container_t vCandidates;
171 : remove_copy_if(
172 0 : pMarkAccess->getBookmarksBegin(),
173 : upper_bound(
174 0 : pMarkAccess->getBookmarksBegin(),
175 0 : pMarkAccess->getBookmarksEnd(),
176 0 : *GetCrsr()->GetPoint(),
177 : sw::mark::CompareIMarkStartsAfter()),
178 : back_inserter(vCandidates),
179 0 : &lcl_IsInvisibleBookmark);
180 : sort(
181 : vCandidates.begin(),
182 : vCandidates.end(),
183 0 : &lcl_ReverseMarkOrderingByEnd);
184 :
185 : // watch Crsr-Moves
186 0 : CrsrStateHelper aCrsrSt(*this);
187 0 : IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
188 0 : for(; ppMark!=vCandidates.end(); ++ppMark)
189 : {
190 : // ignoring those not ending before the Crsr
191 : // (we were only able to eliminate those starting
192 : // behind the Crsr by the upper_bound(..)
193 : // above)
194 0 : if(!(**ppMark).EndsBefore(*GetCrsr()->GetPoint()))
195 0 : continue;
196 0 : aCrsrSt.SetCrsrToMark(ppMark->get());
197 0 : if(!aCrsrSt.RollbackIfIllegal())
198 0 : break; // found legal move
199 : }
200 0 : if(ppMark==vCandidates.end())
201 : {
202 0 : SttEndDoc(true);
203 0 : return false;
204 : }
205 :
206 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
207 0 : return true;
208 : }
209 :
210 793 : bool SwCrsrShell::IsFormProtected()
211 : {
212 793 : return getIDocumentSettingAccess()->get(IDocumentSettingAccess::PROTECT_FORM);
213 : }
214 :
215 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetCurrentFieldmark()
216 : {
217 : // TODO: Refactor
218 0 : SwPosition pos(*GetCrsr()->GetPoint());
219 0 : return getIDocumentMarkAccess()->getFieldmarkFor(pos);
220 : }
221 :
222 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkAfter()
223 : {
224 0 : SwPosition pos(*GetCrsr()->GetPoint());
225 0 : return getIDocumentMarkAccess()->getFieldmarkAfter(pos);
226 : }
227 :
228 0 : ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkBefore()
229 : {
230 0 : SwPosition pos(*GetCrsr()->GetPoint());
231 0 : return getIDocumentMarkAccess()->getFieldmarkBefore(pos);
232 : }
233 :
234 0 : bool SwCrsrShell::GotoFieldmark(::sw::mark::IFieldmark const * const pMark)
235 : {
236 0 : if(pMark==NULL) return false;
237 :
238 : // watch Crsr-Moves
239 0 : CrsrStateHelper aCrsrSt(*this);
240 0 : aCrsrSt.SetCrsrToMark(pMark);
241 0 : aCrsrSt.m_pCrsr->GetPoint()->nContent++;
242 0 : aCrsrSt.m_pCrsr->GetMark()->nContent--;
243 0 : if(aCrsrSt.RollbackIfIllegal()) return false;
244 :
245 0 : UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
246 0 : return true;
247 99 : }
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|