LibreOffice Module sw (master)  1
callnk.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 <com/sun/star/i18n/XBreakIterator.hpp>
21 #include <fmtcntnt.hxx>
22 #include <txatbase.hxx>
23 #include "callnk.hxx"
24 #include <crsrsh.hxx>
25 #include <doc.hxx>
26 #include <frmfmt.hxx>
27 #include <txtfrm.hxx>
28 #include <rowfrm.hxx>
29 #include <fmtfsize.hxx>
30 #include <ndtxt.hxx>
31 #include <flyfrm.hxx>
32 #include <breakit.hxx>
33 
35  : m_rShell( rSh )
36 {
37  // remember SPoint-values of current cursor
39  SwNode& rNd = pCursor->GetPoint()->nNode.GetNode();
40  m_nNode = rNd.GetIndex();
41  m_nContent = pCursor->GetPoint()->nContent.GetIndex();
42  m_nNodeType = rNd.GetNodeType();
43  m_bHasSelection = ( *pCursor->GetPoint() != *pCursor->GetMark() );
44 
45  if( rNd.IsTextNode() )
47  !m_rShell.ActionPend() );
48  else
49  {
50  m_nLeftFramePos = 0;
51 
52  // A special treatment for SwFeShell:
53  // When deleting the header/footer, footnotes SwFeShell sets the
54  // Cursor to NULL (Node + Content).
55  // If the Cursor is not on a ContentNode (ContentNode) this fact gets
56  // saved in nNdType.
58  m_nNodeType = SwNodeType::NONE;
59  }
60 }
61 
62 static void lcl_notifyRow(const SwContentNode* pNode, SwCursorShell const & rShell)
63 {
64  if ( !pNode )
65  return;
66 
67  SwFrame *const pMyFrame = pNode->getLayoutFrame( rShell.GetLayout() );
68  if ( !pMyFrame )
69  return;
70 
71  // We need to emulated a change of the row height in order
72  // to have the complete row redrawn
73  SwRowFrame *const pRow = pMyFrame->FindRowFrame();
74  if ( !pRow )
75  return;
76 
77  const SwTableLine* pLine = pRow->GetTabLine( );
78 
79  if (rShell.IsTableMode())
80  {
81  // If we have a table selection, then avoid the notification: it's not necessary (the text
82  // cursor needs no updating) and the notification may kill the selection overlay, leading to
83  // flicker.
84  return;
85  }
86 
87  SwFormatFrameSize aSize = pLine->GetFrameFormat()->GetFrameSize();
88  pRow->ModifyNotification(nullptr, &aSize);
89 }
90 
91 SwCallLink::~SwCallLink() COVERITY_NOEXCEPT_FALSE
92 {
93  if( m_nNodeType == SwNodeType::NONE || !m_rShell.m_bCallChgLnk ) // see ctor
94  return ;
95 
96  // If travelling over Nodes check formats and register them anew at the
97  // new Node.
98  SwPaM* pCurrentCursor = m_rShell.IsTableMode() ? m_rShell.GetTableCrs() : m_rShell.GetCursor();
99  SwContentNode * pCNd = pCurrentCursor->GetContentNode();
100  if( !pCNd )
101  return;
102 
103  lcl_notifyRow(pCNd, m_rShell);
104 
105  const SwDoc *pDoc=m_rShell.GetDoc();
106  const SwContentNode *pNode = nullptr;
107  if ( pDoc && m_nNode < pDoc->GetNodes( ).Count( ) )
108  {
109  pNode = pDoc->GetNodes()[m_nNode]->GetContentNode();
110  }
111  lcl_notifyRow(pNode, m_rShell);
112 
113  sal_Int32 nCmp, nCurrentContent = pCurrentCursor->GetPoint()->nContent.GetIndex();
114  SwNodeType nNdWhich = pCNd->GetNodeType();
115  sal_uLong nCurrentNode = pCurrentCursor->GetPoint()->nNode.GetIndex();
116 
117  // Register the Shell as dependent at the current Node. By doing this all
118  // attribute changes can be signaled over the link.
119  pCNd->Add( &m_rShell );
120 
121  const bool bCurrentHasSelection = (*pCurrentCursor->GetPoint() != *pCurrentCursor->GetMark());
122 
123  if( m_nNodeType != nNdWhich || m_nNode != nCurrentNode )
124  {
125  // Every time a switch between nodes occurs, there is a chance that
126  // new attributes do apply - meaning text-attributes.
127  // So the currently applying attributes would have to be determined.
128  // That can be done in one go by the handler.
130  }
131  else if (m_bHasSelection != bCurrentHasSelection)
132  {
133  // always call change link when selection changes
135  }
136  else if( m_rShell.m_aChgLnk.IsSet() && SwNodeType::Text == nNdWhich &&
137  m_nContent != nCurrentContent )
138  {
139  // If travelling with left/right only and the frame is
140  // unchanged (columns!) then check text hints.
141  if( m_nLeftFramePos == SwCallLink::getLayoutFrame( m_rShell.GetLayout(), *pCNd->GetTextNode(), nCurrentContent,
142  !m_rShell.ActionPend() ) &&
143  (( nCmp = m_nContent ) + 1 == nCurrentContent || // Right
144  m_nContent -1 == ( nCmp = nCurrentContent )) ) // Left
145  {
146  if( nCmp == nCurrentContent && pCurrentCursor->HasMark() ) // left & select
147  ++nCmp;
148 
149  if ( pCNd->GetTextNode()->HasHints() )
150  {
151  const SwpHints &rHts = pCNd->GetTextNode()->GetSwpHints();
152 
153  for( size_t n = 0; n < rHts.Count(); ++n )
154  {
155  const SwTextAttr* pHt = rHts.Get( n );
156  const sal_Int32 *pEnd = pHt->End();
157  const sal_Int32 nStart = pHt->GetStart();
158 
159  // If "only start" or "start and end equal" then call on
160  // every overflow of start.
161  if( ( !pEnd || ( nStart == *pEnd ) ) &&
162  ( nStart == m_nContent || nStart == nCurrentContent) )
163  {
165  return;
166  }
167 
168  // If the attribute has an area and that area is not empty ...
169  else if( pEnd && nStart < *pEnd &&
170  // ... then test if travelling occurred via start/end.
171  ( nStart == nCmp ||
172  ( pHt->DontExpand() ? nCmp == *pEnd-1
173  : nCmp == *pEnd ) ))
174  {
176  return;
177  }
178  }
179  }
180 
181  assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is());
182  const OUString rText = pCNd->GetTextNode()->GetText();
183  if( !nCmp ||
184  g_pBreakIt->GetBreakIter()->getScriptType( rText, m_nContent )
185  != g_pBreakIt->GetBreakIter()->getScriptType(rText, nCurrentContent))
186  {
188  return;
189  }
190  }
191  else
192  // If travelling more than one character with home/end/.. then
193  // always call ChgLnk, because it can not be determined here what
194  // has changed. Something may have changed.
196  }
197 
198  const SwFrame* pFrame;
199  const SwFlyFrame *pFlyFrame;
200  if (m_rShell.ActionPend())
201  return;
202  pFrame = pCNd->getLayoutFrame(m_rShell.GetLayout(), nullptr, nullptr);
203  if (!pFrame)
204  return;
205  pFlyFrame = pFrame->FindFlyFrame();
206  if ( pFlyFrame && !m_rShell.IsTableMode() )
207  {
208  const SwNodeIndex* pIndex = pFlyFrame->GetFormat()->GetContent().GetContentIdx();
209  OSL_ENSURE( pIndex, "Fly without Content" );
210 
211  if (!pIndex)
212  return;
213 
214  const SwNode& rStNd = pIndex->GetNode();
215 
216  if( rStNd.EndOfSectionNode()->StartOfSectionIndex() > m_nNode ||
217  m_nNode > rStNd.EndOfSectionIndex() )
218  m_rShell.GetFlyMacroLnk().Call( pFlyFrame->GetFormat() );
219  }
220 }
221 
223  SwTextNode const & rNd, sal_Int32 nCntPos, bool /*bCalcFrame*/)
224 {
225  SwTextFrame* pFrame = static_cast<SwTextFrame*>(rNd.getLayoutFrame(pRoot, nullptr, nullptr));
226  SwTextFrame* pNext;
227  if ( pFrame && !pFrame->IsHiddenNow() )
228  {
229  if( pFrame->HasFollow() )
230  {
231  TextFrameIndex const nPos(pFrame->MapModelToView(&rNd, nCntPos));
232  for (;;)
233  {
234  pNext = pFrame->GetFollow();
235  if(!pNext || nPos < pNext->GetOffset())
236  break;
237  pFrame = pNext;
238  }
239  }
240 
241  return pFrame->getFrameArea().Left();
242  }
243  return 0;
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:682
Base class of the Writer layout elements.
Definition: frame.hxx:297
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:151
sal_uLong GetIndex() const
Definition: node.hxx:282
void Add(SwClient *pDepend)
Definition: calbck.cxx:217
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2800
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:188
sal_uLong StartOfSectionIndex() const
Definition: node.hxx:673
const OUString & GetText() const
Definition: ndtxt.hxx:211
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:647
sal_uIntPtr sal_uLong
const SwPosition * GetMark() const
Definition: pam.hxx:209
SwContentFrame * getLayoutFrame(const SwRootFrame *, const SwPosition *pPos=nullptr, std::pair< Point, bool > const *pViewPosAndCalcFrame=nullptr) const
Definition: node.cxx:1148
sal_Int64 n
Definition: doc.hxx:186
bool HasFollow() const
Definition: flowfrm.hxx:165
SwTableLine is one table row in the document model.
Definition: swtable.hxx:344
SwNode & GetNode() const
Definition: ndindex.hxx:119
static void lcl_notifyRow(const SwContentNode *pNode, SwCursorShell const &rShell)
Definition: callnk.cxx:62
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
SwContentNode * GetContentNode(bool bPoint=true) const
Definition: pam.hxx:229
bool IsHiddenNow() const
Hidden.
Definition: txtfrm.cxx:1347
SwNodeType GetNodeType() const
Definition: node.hxx:144
SwIndex nContent
Definition: pam.hxx:38
const SwRect & getFrameArea() const
Definition: frame.hxx:177
SwBreakIt * g_pBreakIt
Definition: breakit.cxx:33
sal_uLong GetIndex() const
Definition: ndindex.hxx:152
sal_Int32 GetStart() const
Definition: txatbase.hxx:82
SwRowFrame * FindRowFrame()
Definition: findfrm.cxx:538
SwDoc * GetDoc() const
Definition: viewsh.hxx:284
SwFlyFrame * FindFlyFrame()
Definition: frame.hxx:1089
bool m_bCallChgLnk
flag for derived classes
Definition: crsrsh.hxx:217
const Link< const SwFlyFrameFormat *, void > & GetFlyMacroLnk() const
Definition: crsrsh.hxx:484
SwTextFrame * GetFollow()
Definition: txtfrm.hxx:850
virtual void ModifyNotification(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue)
Definition: calbck.hxx:154
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
size_t Count() const
Definition: ndhints.hxx:142
SwNodeType
Definition: ndtyp.hxx:28
SwTextAttr * Get(size_t nPos) const
Definition: ndhints.hxx:144
const SwPaM * GetTableCrs() const
Definition: crsrsh.hxx:908
const SwPosition * GetPoint() const
Definition: pam.hxx:207
Count
bool ActionPend() const
Definition: viewsh.hxx:199
bool HasMark() const
A PaM marks a selection if Point and Mark are distinct positions.
Definition: pam.hxx:205
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:366
Marks a node in the document model.
Definition: ndindex.hxx:31
SwpHints & GetSwpHints()
getters for SwpHints
Definition: ndtxt.hxx:802
const SwNodeIndex * GetContentIdx() const
Definition: fmtcntnt.hxx:46
css::uno::Reference< css::i18n::XBreakIterator > const & GetBreakIter() const
Definition: breakit.hxx:62
void CallChgLnk()
Definition: crsrsh.cxx:2506
void Left(const long nLeft)
Definition: swrect.hxx:195
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:677
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
An SwTextAttr container, stores all directly formatted text portions for a text node.
Definition: ndhints.hxx:67
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
TextFrameIndex MapModelToView(SwTextNode const *pNode, sal_Int32 nIndex) const
Definition: txtfrm.cxx:1241
sal_Int32 GetIndex() const
Definition: index.hxx:95
SwNodes & GetNodes()
Definition: doc.hxx:405
const sal_Int32 * End() const
Definition: txatbase.hxx:148
const SwFormatFrameSize & GetFrameSize(bool=true) const
Definition: fmtfsize.hxx:104
const SwFormatContent & GetContent(bool=true) const
Definition: fmtcntnt.hxx:55
bool DontExpand() const
Definition: txatbase.hxx:92
Link< LinkParamNone *, void > m_aChgLnk
link will be called by every attribute/ format changes at cursor position.
Definition: crsrsh.hxx:175
const SwTableLine * GetTabLine() const
Definition: rowfrm.hxx:69
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2069
bool IsTextNode() const
Definition: node.hxx:636
sal_uInt16 nPos
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:836
bool HasHints() const
Definition: ndtxt.hxx:221
SwRowFrame is one table row in the document layout.
Definition: rowfrm.hxx:28
Base class of the Writer document model elements.
Definition: node.hxx:79