LibreOffice Module sw (master)  1
SwGrammarContact.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 <vcl/timer.hxx>
21 #include <IGrammarContact.hxx>
22 #include <pam.hxx>
23 #include <ndtxt.hxx>
24 #include <SwGrammarMarkUp.hxx>
25 #include <txtfrm.hxx>
26 #include <svl/listener.hxx>
27 
28 namespace {
29 
30 /*
31  * This class is responsible for the delayed display of grammar checks when a paragraph is edited
32  * It's a client of the paragraph the cursor points to.
33  * If the cursor position changes, updateCursorPosition has to be called
34  * If the grammar checker wants to set a grammar marker at a paragraph, he has to request
35  * the grammar list from this class. If the requested paragraph is not edited, it returns
36  * the normal grammar list. But if the paragraph is the active one, a proxy list will be returned and
37  * all changes are set in this proxy list. If the cursor leaves the paragraph the proxy list
38  * will replace the old list. If the grammar checker has completed the paragraph ('setChecked')
39  * then a timer is setup which replaces the old list as well.
40  */
41 class SwGrammarContact final : public IGrammarContact, public SvtListener
42 {
43  Timer m_aTimer;
44  std::unique_ptr<SwGrammarMarkUp> m_pProxyList;
45  bool m_isFinished;
46  SwTextNode* m_pTextNode;
47  DECL_LINK( TimerRepaint, Timer *, void );
48 
49 public:
50  SwGrammarContact();
51  virtual ~SwGrammarContact() override { m_aTimer.Stop(); }
52 
53  // (pure) virtual functions of IGrammarContact
54  virtual void updateCursorPosition( const SwPosition& rNewPos ) override;
55  virtual SwGrammarMarkUp* getGrammarCheck( SwTextNode& rTextNode, bool bCreate ) override;
56  virtual void finishGrammarCheck( SwTextNode& rTextNode ) override;
57  void CheckBroadcaster()
58  {
59  if(HasBroadcaster())
60  return;
61  m_pTextNode = nullptr;
62  m_pProxyList.reset();
63  }
64 };
65 
66 }
67 
68 SwGrammarContact::SwGrammarContact() : m_isFinished( false ), m_pTextNode(nullptr)
69 {
70  m_aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
71  m_aTimer.SetInvokeHandler( LINK(this, SwGrammarContact, TimerRepaint) );
72  m_aTimer.SetDebugName( "sw::SwGrammarContact TimerRepaint" );
73 }
74 
75 IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer, void )
76 {
77  CheckBroadcaster();
78  if( pTimer )
79  {
80  pTimer->Stop();
81  if( m_pTextNode )
82  { //Replace the old wrong list by the proxy list and repaint all frames
83  m_pTextNode->SetGrammarCheck( std::move(m_pProxyList) );
84  SwTextFrame::repaintTextFrames( *m_pTextNode );
85  }
86  }
87 }
88 
89 /* I'm always a client of the current paragraph */
90 void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
91 {
92  CheckBroadcaster();
93  SwTextNode* pTextNode = rNewPos.nNode.GetNode().GetTextNode();
94  if( pTextNode == m_pTextNode ) // paragraph has been changed
95  return;
96 
97  m_aTimer.Stop();
98  if( m_pTextNode ) // My last paragraph has been left
99  {
100  if( m_pProxyList )
101  { // replace old list by the proxy list and repaint
102  m_pTextNode->SetGrammarCheck( std::move(m_pProxyList) );
103  SwTextFrame::repaintTextFrames( *m_pTextNode );
104  }
105  EndListeningAll();
106  }
107  if( pTextNode )
108  {
109  m_pTextNode = pTextNode;
110  EndListeningAll();
111  StartListening(pTextNode->GetNotifier()); // welcome new paragraph
112  }
113 }
114 
115 /* deliver a grammar check list for the given text node */
116 SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTextNode& rTextNode, bool bCreate )
117 {
118  SwGrammarMarkUp *pRet = nullptr;
119  CheckBroadcaster();
120  if( m_pTextNode == &rTextNode ) // hey, that's my current paragraph!
121  { // so you will get a proxy list...
122  if( bCreate )
123  {
124  if( m_isFinished )
125  {
126  m_pProxyList.reset();
127  }
128  if( !m_pProxyList )
129  {
130  if( rTextNode.GetGrammarCheck() )
131  m_pProxyList.reset( static_cast<SwGrammarMarkUp*>(rTextNode.GetGrammarCheck()->Clone()) );
132  else
133  {
134  m_pProxyList.reset( new SwGrammarMarkUp() );
135  m_pProxyList->SetInvalid( 0, COMPLETE_STRING );
136  }
137  }
138  m_isFinished = false;
139  }
140  pRet = m_pProxyList.get();
141  }
142  else
143  {
144  pRet = rTextNode.GetGrammarCheck(); // do you have already a list?
145  if( bCreate && !pRet ) // do you want to create a list?
146  {
147  pRet = new SwGrammarMarkUp();
148  pRet->SetInvalid( 0, COMPLETE_STRING );
149  rTextNode.SetGrammarCheck( std::unique_ptr<SwGrammarMarkUp>(pRet) );
150  rTextNode.SetGrammarCheckDirty( true );
151  }
152  }
153  return pRet;
154 }
155 
157 {
158  CheckBroadcaster();
159  if( &rTextNode != m_pTextNode ) // not my paragraph
160  SwTextFrame::repaintTextFrames( rTextNode ); // can be repainted directly
161  else
162  {
163  if( m_pProxyList )
164  {
165  m_isFinished = true;
166  m_aTimer.Start(); // will replace old list and repaint with delay
167  }
168  else if( m_pTextNode->GetGrammarCheck() )
169  { // all grammar problems seems to be gone, no delay needed
170  m_pTextNode->ClearGrammarCheck();
171  SwTextFrame::repaintTextFrames( *m_pTextNode );
172  }
173  }
174 }
175 
177 {
178  return new SwGrammarContact();
179 }
180 
181 void finishGrammarCheck( SwTextNode& rTextNode )
182 {
183  IGrammarContact* pGrammarContact = getGrammarContact( rTextNode );
184  if( pGrammarContact )
185  pGrammarContact->finishGrammarCheck( rTextNode );
186 }
187 
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void finishGrammarCheck(SwTextNode &rTextNode)=0
finishGrammarCheck() has to be called if a grammar checking has been completed for a text node...
Marks a position in the document model.
Definition: pam.hxx:35
virtual void updateCursorPosition(const SwPosition &rNewPos)=0
Update cursor position reacts to a change of the current input cursor As long as the cursor in inside...
SwNodeIndex nNode
Definition: pam.hxx:37
virtual SwGrammarMarkUp * getGrammarCheck(SwTextNode &rTextNode, bool bCreate)=0
getGrammarCheck checks if the given text node is blocked by the current cursor if not...
static void repaintTextFrames(const SwTextNode &rNode)
Repaint all text frames of the given text node.
Definition: txtfrm.cxx:4025
void ClearGrammarCheck()
Definition: txtedt.cxx:2221
SwNode & GetNode() const
Definition: ndindex.hxx:119
void SetGrammarCheckDirty(bool bNew) const
Definition: txtedt.cxx:2303
IGrammarContact * getGrammarContact(const SwTextNode &rTextNode)
getGrammarContact() delivers the grammar contact of the document (for a given textnode) ...
Definition: docnew.cxx:804
void finishGrammarCheck(SwTextNode &rTextNode)
finishGrammarCheck() calls the same function of the grammar contact of the document (for a given text...
void SetGrammarCheck(std::unique_ptr< SwGrammarMarkUp > pNew)
Definition: txtedt.cxx:2215
SwGrammarMarkUp * GetGrammarCheck()
Definition: txtedt.cxx:2232
virtual SwWrongList * Clone() override
bool HasBroadcaster() const
SvtBroadcaster & GetNotifier()
Definition: calbck.hxx:102
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
Organizer of the contact between SwTextNodes and grammar checker.
IMPL_LINK(SwGrammarContact, TimerRepaint, Timer *, pTimer, void)
constexpr sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:58
IGrammarContact * createGrammarContact()
Factory for a grammar contact.
void SetInvalid(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:253
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:850