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 <hints.hxx>
22 #include <IGrammarContact.hxx>
23 #include <pam.hxx>
24 #include <ndtxt.hxx>
25 #include <SwGrammarMarkUp.hxx>
26 #include <txtfrm.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 : public IGrammarContact, public SwClient
42 {
43  Timer aTimer;
44  std::unique_ptr<SwGrammarMarkUp> mpProxyList;
45  bool mbFinished;
46  SwTextNode* getMyTextNode() { return static_cast<SwTextNode*>(GetRegisteredIn()); }
47  DECL_LINK( TimerRepaint, Timer *, void );
48 
49 public:
50  SwGrammarContact();
51  virtual ~SwGrammarContact() override { 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 protected:
58  // virtual function of SwClient
59  virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew) override;
60 };
61 
62 }
63 
64 SwGrammarContact::SwGrammarContact() : mbFinished( false )
65 {
66  aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
67  aTimer.SetInvokeHandler( LINK(this, SwGrammarContact, TimerRepaint) );
68  aTimer.SetDebugName( "sw::SwGrammarContact TimerRepaint" );
69 }
70 
71 IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer, void )
72 {
73  if( pTimer )
74  {
75  pTimer->Stop();
76  if( GetRegisteredIn() )
77  { //Replace the old wrong list by the proxy list and repaint all frames
78  getMyTextNode()->SetGrammarCheck( mpProxyList.release() );
79  SwTextFrame::repaintTextFrames( *getMyTextNode() );
80  }
81  }
82 }
83 
84 /* I'm always a client of the current paragraph */
85 void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
86 {
87  SwTextNode* pTextNode = rNewPos.nNode.GetNode().GetTextNode();
88  if( pTextNode != GetRegisteredIn() ) // paragraph has been changed
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 
105 /* deliver a grammar check list for the given text node */
106 SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTextNode& rTextNode, bool bCreate )
107 {
108  SwGrammarMarkUp *pRet = nullptr;
109  if( GetRegisteredIn() == &rTextNode ) // hey, that's my current paragraph!
110  { // so you will get a proxy list...
111  if( bCreate )
112  {
113  if( mbFinished )
114  {
115  mpProxyList.reset();
116  }
117  if( !mpProxyList )
118  {
119  if( rTextNode.GetGrammarCheck() )
120  mpProxyList.reset( static_cast<SwGrammarMarkUp*>(rTextNode.GetGrammarCheck()->Clone()) );
121  else
122  {
123  mpProxyList.reset( new SwGrammarMarkUp() );
124  mpProxyList->SetInvalid( 0, COMPLETE_STRING );
125  }
126  }
127  mbFinished = false;
128  }
129  pRet = mpProxyList.get();
130  }
131  else
132  {
133  pRet = rTextNode.GetGrammarCheck(); // do you have already a list?
134  if( bCreate && !pRet ) // do you want to create a list?
135  {
136  pRet = new SwGrammarMarkUp();
137  pRet->SetInvalid( 0, COMPLETE_STRING );
138  rTextNode.SetGrammarCheck( pRet );
139  rTextNode.SetGrammarCheckDirty( true );
140  }
141  }
142  return pRet;
143 }
144 
145 void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * )
146 {
147  if( !pOld || pOld->Which() != RES_OBJECTDYING )
148  return;
149 
150  const SwPtrMsgPoolItem *pDead = static_cast<const SwPtrMsgPoolItem *>(pOld);
151  if( pDead->pObject == GetRegisteredIn() )
152  { // if my current paragraph dies, I throw the proxy list away
153  aTimer.Stop();
154  EndListeningAll();
155  mpProxyList.reset();
156  }
157 }
158 
160 {
161  if( &rTextNode != GetRegisteredIn() ) // not my paragraph
162  SwTextFrame::repaintTextFrames( rTextNode ); // can be repainted directly
163  else
164  {
165  if( mpProxyList )
166  {
167  mbFinished = true;
168  aTimer.Start(); // will replace old list and repaint with delay
169  }
170  else if( getMyTextNode()->GetGrammarCheck() )
171  { // all grammar problems seems to be gone, no delay needed
172  getMyTextNode()->SetGrammarCheck( nullptr );
173  SwTextFrame::repaintTextFrames( *getMyTextNode() );
174  }
175  }
176 }
177 
179 {
180  return new SwGrammarContact();
181 }
182 
183 void finishGrammarCheck( SwTextNode& rTextNode )
184 {
185  IGrammarContact* pGrammarContact = getGrammarContact( rTextNode );
186  if( pGrammarContact )
187  pGrammarContact->finishGrammarCheck( rTextNode );
188 }
189 
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Add(SwClient *pDepend)
Definition: calbck.cxx:217
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:4009
SwNode & GetNode() const
Definition: ndindex.hxx:119
void SetGrammarCheckDirty(bool bNew) const
Definition: txtedt.cxx:2247
IGrammarContact * getGrammarContact(const SwTextNode &rTextNode)
getGrammarContact() delivers the grammar contact of the document (for a given textnode) ...
Definition: docnew.cxx:778
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:2181
virtual SwWrongList * Clone() override
void SetGrammarCheck(SwGrammarMarkUp *pNew, bool bDelete=true)
Definition: txtedt.cxx:2169
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
void Stop()
const SwModify * GetRegisteredIn() const
Definition: calbck.hxx:157
Organizer of the contact between SwTextNodes and grammar checker.
IMPL_LINK(SwGrammarContact, TimerRepaint, Timer *, pTimer, void)
void * pObject
Definition: hints.hxx:53
IGrammarContact * createGrammarContact()
Factory for a grammar contact.
const sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:61
sal_uInt16 Which() const
virtual void Modify(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue)
Definition: calbck.cxx:130
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:836