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