LibreOffice Module sw (master)  1
crbm.cxx
Go to the documentation of this file.
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 <rootfrm.hxx>
23 #include <txtfrm.hxx>
24 #include <IMark.hxx>
25 #include <swcrsr.hxx>
26 #include <IDocumentMarkAccess.hxx>
28 
29 namespace
30 {
31  struct CursorStateHelper
32  {
33  explicit CursorStateHelper(SwCursorShell const & rShell)
34  : m_pCursor(rShell.GetCursor())
35  , m_aSaveState(*m_pCursor)
36  { }
37 
38  void SetCursorToMark(::sw::mark::IMark const * const pMark)
39  {
40  *(m_pCursor->GetPoint()) = pMark->GetMarkStart();
41  if(pMark->IsExpanded())
42  {
43  m_pCursor->SetMark();
44  *(m_pCursor->GetMark()) = pMark->GetMarkEnd();
45  }
46  }
47 
49  bool RollbackIfIllegal()
50  {
51  if(m_pCursor->IsSelOvr(SwCursorSelOverFlags::CheckNodeSection
53  {
54  m_pCursor->DeleteMark();
55  m_pCursor->RestoreSavePos();
56  return true;
57  }
58  return false;
59  }
60 
61  SwCursor* m_pCursor;
62  SwCursorSaveState m_aSaveState;
63  };
64 
65  bool lcl_ReverseMarkOrderingByEnd(const ::sw::mark::IMark* pFirst,
66  const ::sw::mark::IMark* pSecond)
67  {
68  return pFirst->GetMarkEnd() > pSecond->GetMarkEnd();
69  }
70 
71  bool lcl_IsInvisibleBookmark(const ::sw::mark::IMark* pMark)
72  {
74  }
75 }
76 
77 // at CurrentCursor.SPoint
79  const vcl::KeyCode& rCode,
80  const OUString& rName,
82 {
83  StartAction();
85  *GetCursor(),
86  rName,
88  ::sw::mark::IBookmark* pBookmark = dynamic_cast< ::sw::mark::IBookmark* >(pMark);
89  if(pBookmark)
90  {
91  pBookmark->SetKeyCode(rCode);
92  pBookmark->SetShortName(OUString());
93  }
94  EndAction();
95  return pMark;
96 }
97 // set CurrentCursor.SPoint
98 
99 // at CurrentCursor.SPoint
101  const vcl::KeyCode& rCode,
102  const OUString& rName,
103  bool bHide,
104  const OUString& rCondition)
105 {
106  StartAction();
108  *GetCursor(),
109  rName,
111  ::sw::mark::IBookmark* pBookmark = dynamic_cast< ::sw::mark::IBookmark* >(pMark);
112  if (pBookmark)
113  {
114  pBookmark->SetKeyCode(rCode);
115  pBookmark->SetShortName(OUString());
116  pBookmark->Hide(bHide);
117  pBookmark->SetHideCondition(rCondition);
118  }
119  EndAction();
120  return pMark;
121 }
122 
123 namespace sw {
124 
125 bool IsMarkHidden(SwRootFrame const& rLayout, ::sw::mark::IMark const& rMark)
126 {
127  if (!rLayout.HasMergedParas())
128  {
129  return false;
130  }
131  SwTextNode const& rNode(*rMark.GetMarkPos().nNode.GetNode().GetTextNode());
132  SwTextFrame const*const pFrame(static_cast<SwTextFrame const*>(
133  rNode.getLayoutFrame(&rLayout)));
134  if (!pFrame)
135  {
136  return true;
137  }
138  if (rMark.IsExpanded())
139  {
140  SwTextFrame const*const pOtherFrame(static_cast<SwTextFrame const*>(
141  rMark.GetOtherMarkPos().nNode.GetNode().GetTextNode()->getLayoutFrame(&rLayout)));
142  return pFrame == pOtherFrame
143  && pFrame->MapModelToViewPos(rMark.GetMarkPos())
144  == pFrame->MapModelToViewPos(rMark.GetOtherMarkPos());
145  }
146  else
147  {
148  if (rMark.GetMarkPos().nContent.GetIndex() == rNode.Len())
149  { // at end of node: never deleted (except if node deleted)
150  return rNode.GetRedlineMergeFlag() == SwNode::Merge::Hidden;
151  }
152  else
153  { // check character following mark pos
154  return pFrame->MapModelToViewPos(rMark.GetMarkPos())
155  == pFrame->MapModelToView(&rNode, rMark.GetMarkPos().nContent.GetIndex() + 1);
156  }
157  }
158 }
159 
160 } // namespace sw
161 
162 // set CurrentCursor.SPoint
163 bool SwCursorShell::GotoMark(const ::sw::mark::IMark* const pMark, bool bAtStart)
164 {
165  if (sw::IsMarkHidden(*GetLayout(), *pMark))
166  {
167  return false;
168  }
169  // watch Cursor-Moves
170  CursorStateHelper aCursorSt(*this);
171  if ( bAtStart )
172  *aCursorSt.m_pCursor->GetPoint() = pMark->GetMarkStart();
173  else
174  *aCursorSt.m_pCursor->GetPoint() = pMark->GetMarkEnd();
175 
176  if(aCursorSt.RollbackIfIllegal()) return false;
177 
179  return true;
180 }
181 
182 bool SwCursorShell::GotoMark(const ::sw::mark::IMark* const pMark)
183 {
184  if (sw::IsMarkHidden(*GetLayout(), *pMark))
185  {
186  return false;
187  }
188  // watch Cursor-Moves
189  CursorStateHelper aCursorSt(*this);
190  aCursorSt.SetCursorToMark(pMark);
191 
192  if(aCursorSt.RollbackIfIllegal()) return false;
193 
195  return true;
196 }
197 
199 {
201  std::vector<::sw::mark::IMark*> vCandidates;
202  remove_copy_if(
203  pMarkAccess->findFirstBookmarkStartsAfter(*GetCursor()->GetPoint()),
204  pMarkAccess->getBookmarksEnd(),
205  back_inserter(vCandidates),
206  &lcl_IsInvisibleBookmark);
207 
208  // watch Cursor-Moves
209  CursorStateHelper aCursorSt(*this);
210  auto ppMark = vCandidates.begin();
211  for(; ppMark!=vCandidates.end(); ++ppMark)
212  {
213  if (sw::IsMarkHidden(*GetLayout(), **ppMark))
214  {
215  continue;
216  }
217  aCursorSt.SetCursorToMark(*ppMark);
218  if(!aCursorSt.RollbackIfIllegal())
219  break; // found legal move
220  }
221  if(ppMark==vCandidates.end())
222  {
223  SttEndDoc(false);
224  return false;
225  }
226 
228  return true;
229 }
230 
232 {
234  // candidates from which to choose the mark before
235  // no need to consider marks starting after rPos
236  std::vector<::sw::mark::IMark*> vCandidates;
237  remove_copy_if(
238  pMarkAccess->getBookmarksBegin(),
239  pMarkAccess->findFirstBookmarkStartsAfter(*GetCursor()->GetPoint()),
240  back_inserter(vCandidates),
241  &lcl_IsInvisibleBookmark);
242  sort(
243  vCandidates.begin(),
244  vCandidates.end(),
245  &lcl_ReverseMarkOrderingByEnd);
246 
247  // watch Cursor-Moves
248  CursorStateHelper aCursorSt(*this);
249  auto ppMark = vCandidates.begin();
250  for(; ppMark!=vCandidates.end(); ++ppMark)
251  {
252  // ignoring those not ending before the Cursor
253  // (we were only able to eliminate those starting
254  // behind the Cursor by the upper_bound(..)
255  // above)
256  if(!((**ppMark).GetMarkEnd() < *GetCursor()->GetPoint()))
257  continue;
258  if (sw::IsMarkHidden(*GetLayout(), **ppMark))
259  {
260  continue;
261  }
262  aCursorSt.SetCursorToMark(*ppMark);
263  if(!aCursorSt.RollbackIfIllegal())
264  break; // found legal move
265  }
266  if(ppMark==vCandidates.end())
267  {
268  SttEndDoc(true);
269  return false;
270  }
271 
273  return true;
274 }
275 
277 {
279 }
280 
282 {
283  // TODO: Refactor
284  SwPosition pos(*GetCursor()->GetPoint());
286 }
287 
289 {
290  SwPosition pos(*GetCursor()->GetPoint());
292 }
293 
295 {
296  SwPosition pos(*GetCursor()->GetPoint());
298 }
299 
301 {
302  if(pMark==nullptr) return false;
303 
304  // watch Cursor-Moves
305  CursorStateHelper aCursorSt(*this);
306  aCursorSt.SetCursorToMark(pMark);
307  ++aCursorSt.m_pCursor->GetPoint()->nContent;
308  --aCursorSt.m_pCursor->GetMark()->nContent;
309 
310  if(aCursorSt.RollbackIfIllegal()) return false;
311 
313  return true;
314 }
315 
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:159
virtual ::sw::mark::IFieldmark * getFieldmarkAfter(const SwPosition &pos) const =0
SAL_DLLPRIVATE void UpdateCursor(sal_uInt16 eFlags=SwCursorShell::SCROLLWIN|SwCursorShell::CHKRANGE, bool bIdleEnd=false)
Definition: crsrsh.cxx:1574
Marks a position in the document model.
Definition: pam.hxx:36
bool SttEndDoc(bool bStt)
Definition: crsrsh.cxx:575
check overlapping PaMs
Definition: crsrsh.hxx:156
SwNodeIndex nNode
Definition: pam.hxx:38
static SW_DLLPUBLIC MarkType GetType(const ::sw::mark::IMark &rMark)
Returns the MarkType used to create the mark.
Definition: docbm.cxx:478
SwContentFrame * getLayoutFrame(const SwRootFrame *, const SwPosition *pPos=nullptr, std::pair< Point, bool > const *pViewPosAndCalcFrame=nullptr) const
Definition: node.cxx:1212
Provides access to the marks of a document.
virtual const_iterator_t findFirstBookmarkStartsAfter(const SwPosition &rPos) const =0
Finds the first mark that is starting after.
virtual const SwPosition & GetMarkPos() const =0
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document setting interface.
Definition: viewsh.cxx:2779
bool IsMarkHidden(SwRootFrame const &rLayout,::sw::mark::IMark const &rMark)
Definition: crbm.cxx:125
SwNode & GetNode() const
Definition: ndindex.hxx:121
Dialog to specify the properties of date form field.
bool GotoMark(const ::sw::mark::IMark *const pMark)
Definition: crbm.cxx:182
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
SwIndex nContent
Definition: pam.hxx:39
size_t pos
virtual ::sw::mark::IFieldmark * getFieldmarkFor(const SwPosition &pos) const =0
virtual ::sw::mark::IMark * makeMark(const SwPaM &rPaM, const OUString &rProposedName, MarkType eMark,::sw::mark::InsertMode eMode, SwPosition const *pSepPos=nullptr)=0
Generates a new mark in the document for a certain selection.
const IDocumentMarkAccess * getIDocumentMarkAccess() const
Provides access to the document bookmark interface.
Definition: viewsh.cxx:2783
virtual void SetKeyCode(const vcl::KeyCode &)=0
virtual void SetShortName(const OUString &)=0
bool IsFormProtected()
Definition: crbm.cxx:276
virtual const_iterator_t getBookmarksEnd() const =0
returns a STL-like random access iterator to the end of the sequence of IBookmarks.
virtual void Hide(bool hide)=0
A helper class to save cursor state (position).
Definition: swcrsr.hxx:232
TextFrameIndex MapModelToViewPos(SwPosition const &rPos) const
Definition: txtfrm.cxx:1270
const SwPosition * GetPoint() const
Definition: pam.hxx:208
::sw::mark::IMark * SetBookmark(const vcl::KeyCode &, const OUString &rName, IDocumentMarkAccess::MarkType eMark=IDocumentMarkAccess::MarkType::BOOKMARK)
Definition: crbm.cxx:78
bool GotoFieldmark(const ::sw::mark::IFieldmark *const pMark)
Definition: crbm.cxx:300
bool GoNextBookmark()
Definition: crbm.cxx:198
virtual void SetHideCondition(const OUString &)=0
void StartAction()
Definition: crsrsh.cxx:228
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:196
::sw::mark::IFieldmark * GetFieldmarkAfter()
Definition: crbm.cxx:288
::sw::mark::IMark * SetBookmark2(const vcl::KeyCode &, const OUString &rName, bool bHide, const OUString &rCondition)
Definition: crbm.cxx:100
virtual const_iterator_t getBookmarksBegin() const =0
returns a STL-like random access iterator to the begin of the sequence the IBookmarks.
sal_Int32 GetIndex() const
Definition: index.hxx:91
scroll window
Definition: crsrsh.hxx:155
virtual bool get(DocumentSettingId id) const =0
Return the specified document setting.
virtual const SwPosition & GetMarkEnd() const =0
bool GoPrevBookmark()
Definition: crbm.cxx:231
::sw::mark::IFieldmark * GetFieldmarkBefore()
Definition: crbm.cxx:294
make visible in spite of Readonly
Definition: crsrsh.hxx:157
bool HasMergedParas() const
Definition: rootfrm.hxx:426
virtual const SwPosition & GetMarkStart() const =0
virtual ::sw::mark::IFieldmark * getFieldmarkBefore(const SwPosition &pos) const =0
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2174
virtual bool IsExpanded() const =0
::sw::mark::IFieldmark * GetCurrentFieldmark()
Definition: crbm.cxx:281
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:856
virtual const SwPosition & GetOtherMarkPos() const =0
void EndAction(const bool bIdleEnd=false)
Definition: crsrsh.cxx:245