Update ooo320-m1
[ooovba.git] / sw / source / core / crsr / crbm.cxx
blob0d6a63e251342bca7f1874ca4894c4e841afaf8e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: crbm.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include "crsrsh.hxx"
36 #include "ndtxt.hxx"
37 #include <docary.hxx>
38 #include <boost/bind.hpp>
40 #include "IMark.hxx"
41 #include "callnk.hxx"
42 #include "swcrsr.hxx"
43 #include <IDocumentMarkAccess.hxx>
44 #include <IDocumentSettingAccess.hxx>
46 using namespace std;
48 namespace
50 struct CrsrStateHelper
52 CrsrStateHelper(SwCrsrShell& rShell)
53 : m_aLink(rShell)
54 , m_pCrsr(rShell.GetSwCrsr())
55 , m_aSaveState(*m_pCrsr)
56 { }
58 void SetCrsrToMark(::sw::mark::IMark const * const pMark)
60 *(m_pCrsr->GetPoint()) = pMark->GetMarkStart();
61 if(pMark->IsExpanded())
63 m_pCrsr->SetMark();
64 *(m_pCrsr->GetMark()) = pMark->GetMarkEnd();
68 // returns true if the Cursor had been rolled back
69 bool RollbackIfIllegal()
71 if(m_pCrsr->IsSelOvr(nsSwCursorSelOverFlags::SELOVER_CHECKNODESSECTION
72 | nsSwCursorSelOverFlags::SELOVER_TOGGLE))
74 m_pCrsr->DeleteMark();
75 m_pCrsr->RestoreSavePos();
76 return true;
78 return false;
81 SwCallLink m_aLink;
82 SwCursor* m_pCrsr;
83 SwCrsrSaveState m_aSaveState;
87 static bool lcl_ReverseMarkOrderingByEnd(const IDocumentMarkAccess::pMark_t& rpFirst,
88 const IDocumentMarkAccess::pMark_t& rpSecond)
90 return rpFirst->GetMarkEnd() > rpSecond->GetMarkEnd();
93 static bool lcl_IsInvisibleBookmark(IDocumentMarkAccess::pMark_t pMark)
95 return IDocumentMarkAccess::GetType(*pMark) != IDocumentMarkAccess::BOOKMARK;
99 // at CurCrsr.SPoint
100 ::sw::mark::IMark* SwCrsrShell::SetBookmark(
101 const KeyCode& rCode,
102 const ::rtl::OUString& rName,
103 const ::rtl::OUString& rShortName,
104 IDocumentMarkAccess::MarkType eMark)
106 StartAction();
107 ::sw::mark::IMark* pMark = getIDocumentMarkAccess()->makeMark(
108 *GetCrsr(),
109 rName,
110 eMark);
111 ::sw::mark::IBookmark* pBookmark = dynamic_cast< ::sw::mark::IBookmark* >(pMark);
112 if(pBookmark)
114 pBookmark->SetKeyCode(rCode);
115 pBookmark->SetShortName(rShortName);
117 EndAction();
118 return pMark;
120 // setzt CurCrsr.SPoint
122 bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark, bool bAtStart)
124 // watch Crsr-Moves
125 CrsrStateHelper aCrsrSt(*this);
126 if ( bAtStart )
127 *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkStart();
128 else
129 *(aCrsrSt.m_pCrsr)->GetPoint() = pMark->GetMarkEnd();
130 if(aCrsrSt.RollbackIfIllegal()) return false;
132 UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
133 return true;
136 bool SwCrsrShell::GotoMark(const ::sw::mark::IMark* const pMark)
138 // watch Crsr-Moves
139 CrsrStateHelper aCrsrSt(*this);
140 aCrsrSt.SetCrsrToMark(pMark);
142 if(aCrsrSt.RollbackIfIllegal()) return false;
144 UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
145 return true;
148 bool SwCrsrShell::GoNextBookmark()
150 IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
151 IDocumentMarkAccess::container_t vCandidates;
152 remove_copy_if(
153 upper_bound(
154 pMarkAccess->getBookmarksBegin(),
155 pMarkAccess->getBookmarksEnd(),
156 *GetCrsr()->GetPoint(),
157 bind(&::sw::mark::IMark::StartsAfter, _2, _1)), // finds the first that is starting after
158 pMarkAccess->getBookmarksEnd(),
159 back_inserter(vCandidates),
160 &lcl_IsInvisibleBookmark);
162 // watch Crsr-Moves
163 CrsrStateHelper aCrsrSt(*this);
164 IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
165 for(; ppMark!=vCandidates.end(); ++ppMark)
167 aCrsrSt.SetCrsrToMark(ppMark->get());
168 if(!aCrsrSt.RollbackIfIllegal())
169 break; // found legal move
171 if(ppMark==vCandidates.end())
173 SttEndDoc(false);
174 return false;
177 UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
178 return true;
181 bool SwCrsrShell::GoPrevBookmark()
183 IDocumentMarkAccess* const pMarkAccess = getIDocumentMarkAccess();
184 // candidates from which to choose the mark before
185 // no need to consider marks starting after rPos
186 IDocumentMarkAccess::container_t vCandidates;
187 remove_copy_if(
188 pMarkAccess->getBookmarksBegin(),
189 upper_bound(
190 pMarkAccess->getBookmarksBegin(),
191 pMarkAccess->getBookmarksEnd(),
192 *GetCrsr()->GetPoint(),
193 bind(&::sw::mark::IMark::StartsAfter, _2, _1)),
194 back_inserter(vCandidates),
195 &lcl_IsInvisibleBookmark);
196 sort(
197 vCandidates.begin(),
198 vCandidates.end(),
199 &lcl_ReverseMarkOrderingByEnd);
201 // watch Crsr-Moves
202 CrsrStateHelper aCrsrSt(*this);
203 IDocumentMarkAccess::const_iterator_t ppMark = vCandidates.begin();
204 for(; ppMark!=vCandidates.end(); ++ppMark)
206 // ignoring those not ending before the Crsr
207 // (we were only able to eliminate those starting
208 // behind the Crsr by the upper_bound(..)
209 // above)
210 if(!(**ppMark).EndsBefore(*GetCrsr()->GetPoint()))
211 continue;
212 aCrsrSt.SetCrsrToMark(ppMark->get());
213 if(!aCrsrSt.RollbackIfIllegal())
214 break; // found legal move
216 if(ppMark==vCandidates.end())
218 SttEndDoc(true);
219 return false;
222 UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
223 return true;
226 bool SwCrsrShell::IsFormProtected()
228 return getIDocumentSettingAccess()->get(IDocumentSettingAccess::PROTECT_FORM);
231 ::sw::mark::IFieldmark* SwCrsrShell::GetCurrentFieldmark()
233 // TODO: Refactor
234 SwPosition pos(*GetCrsr()->GetPoint());
235 return getIDocumentMarkAccess()->getFieldmarkFor(pos);
238 ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkAfter()
240 SwPosition pos(*GetCrsr()->GetPoint());
241 return getIDocumentMarkAccess()->getFieldmarkAfter(pos);
244 ::sw::mark::IFieldmark* SwCrsrShell::GetFieldmarkBefore()
246 SwPosition pos(*GetCrsr()->GetPoint());
247 return getIDocumentMarkAccess()->getFieldmarkBefore(pos);
250 bool SwCrsrShell::GotoFieldmark(::sw::mark::IFieldmark const * const pMark)
252 if(pMark==NULL) return false;
254 // watch Crsr-Moves
255 CrsrStateHelper aCrsrSt(*this);
256 aCrsrSt.SetCrsrToMark(pMark);
257 //aCrsrSt.m_pCrsr->GetPoint()->nContent--;
258 //aCrsrSt.m_pCrsr->GetMark()->nContent++;
259 if(aCrsrSt.RollbackIfIllegal()) return false;
261 UpdateCrsr(SwCrsrShell::SCROLLWIN|SwCrsrShell::CHKRANGE|SwCrsrShell::READONLY);
262 return true;