1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: crbm.cxx,v $
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"
38 #include <boost/bind.hpp>
43 #include <IDocumentMarkAccess.hxx>
44 #include <IDocumentSettingAccess.hxx>
50 struct CrsrStateHelper
52 CrsrStateHelper(SwCrsrShell
& rShell
)
54 , m_pCrsr(rShell
.GetSwCrsr())
55 , m_aSaveState(*m_pCrsr
)
58 void SetCrsrToMark(::sw::mark::IMark
const * const pMark
)
60 *(m_pCrsr
->GetPoint()) = pMark
->GetMarkStart();
61 if(pMark
->IsExpanded())
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();
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
;
100 ::sw::mark::IMark
* SwCrsrShell::SetBookmark(
101 const KeyCode
& rCode
,
102 const ::rtl::OUString
& rName
,
103 const ::rtl::OUString
& rShortName
,
104 IDocumentMarkAccess::MarkType eMark
)
107 ::sw::mark::IMark
* pMark
= getIDocumentMarkAccess()->makeMark(
111 ::sw::mark::IBookmark
* pBookmark
= dynamic_cast< ::sw::mark::IBookmark
* >(pMark
);
114 pBookmark
->SetKeyCode(rCode
);
115 pBookmark
->SetShortName(rShortName
);
120 // setzt CurCrsr.SPoint
122 bool SwCrsrShell::GotoMark(const ::sw::mark::IMark
* const pMark
, bool bAtStart
)
125 CrsrStateHelper
aCrsrSt(*this);
127 *(aCrsrSt
.m_pCrsr
)->GetPoint() = pMark
->GetMarkStart();
129 *(aCrsrSt
.m_pCrsr
)->GetPoint() = pMark
->GetMarkEnd();
130 if(aCrsrSt
.RollbackIfIllegal()) return false;
132 UpdateCrsr(SwCrsrShell::SCROLLWIN
|SwCrsrShell::CHKRANGE
|SwCrsrShell::READONLY
);
136 bool SwCrsrShell::GotoMark(const ::sw::mark::IMark
* const pMark
)
139 CrsrStateHelper
aCrsrSt(*this);
140 aCrsrSt
.SetCrsrToMark(pMark
);
142 if(aCrsrSt
.RollbackIfIllegal()) return false;
144 UpdateCrsr(SwCrsrShell::SCROLLWIN
|SwCrsrShell::CHKRANGE
|SwCrsrShell::READONLY
);
148 bool SwCrsrShell::GoNextBookmark()
150 IDocumentMarkAccess
* const pMarkAccess
= getIDocumentMarkAccess();
151 IDocumentMarkAccess::container_t vCandidates
;
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
);
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())
177 UpdateCrsr(SwCrsrShell::SCROLLWIN
|SwCrsrShell::CHKRANGE
|SwCrsrShell::READONLY
);
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
;
188 pMarkAccess
->getBookmarksBegin(),
190 pMarkAccess
->getBookmarksBegin(),
191 pMarkAccess
->getBookmarksEnd(),
192 *GetCrsr()->GetPoint(),
193 bind(&::sw::mark::IMark::StartsAfter
, _2
, _1
)),
194 back_inserter(vCandidates
),
195 &lcl_IsInvisibleBookmark
);
199 &lcl_ReverseMarkOrderingByEnd
);
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(..)
210 if(!(**ppMark
).EndsBefore(*GetCrsr()->GetPoint()))
212 aCrsrSt
.SetCrsrToMark(ppMark
->get());
213 if(!aCrsrSt
.RollbackIfIllegal())
214 break; // found legal move
216 if(ppMark
==vCandidates
.end())
222 UpdateCrsr(SwCrsrShell::SCROLLWIN
|SwCrsrShell::CHKRANGE
|SwCrsrShell::READONLY
);
226 bool SwCrsrShell::IsFormProtected()
228 return getIDocumentSettingAccess()->get(IDocumentSettingAccess::PROTECT_FORM
);
231 ::sw::mark::IFieldmark
* SwCrsrShell::GetCurrentFieldmark()
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;
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
);