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 #include <rootfrm.hxx>
28 #include <viewsh.hxx>
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  */
42 {
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:
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 SwGrammarContact::SwGrammarContact() : mbFinished( false )
63 {
64  aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
65  aTimer.SetInvokeHandler( LINK(this, SwGrammarContact, TimerRepaint) );
66  aTimer.SetDebugName( "sw::SwGrammarContact TimerRepaint" );
67 }
68 
69 IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer, void )
70 {
71  if( pTimer )
72  {
73  pTimer->Stop();
74  if( GetRegisteredIn() )
75  { //Replace the old wrong list by the proxy list and repaint all frames
76  getMyTextNode()->SetGrammarCheck( mpProxyList.release() );
77  SwTextFrame::repaintTextFrames( *getMyTextNode() );
78  }
79  }
80 }
81 
82 /* I'm always a client of the current paragraph */
84 {
85  SwTextNode* pTextNode = rNewPos.nNode.GetNode().GetTextNode();
86  if( pTextNode != GetRegisteredIn() ) // paragraph has been changed
87  {
88  aTimer.Stop();
89  if( GetRegisteredIn() ) // My last paragraph has been left
90  {
91  if( mpProxyList )
92  { // replace old list by the proxy list and repaint
95  }
97  }
98  if( pTextNode )
99  pTextNode->Add( this ); // welcome new paragraph
100  }
101 }
102 
103 /* deliver a grammar check list for the given text node */
105 {
106  SwGrammarMarkUp *pRet = nullptr;
107  if( GetRegisteredIn() == &rTextNode ) // hey, that's my current paragraph!
108  { // so you will get a proxy list...
109  if( bCreate )
110  {
111  if( mbFinished )
112  {
113  mpProxyList.reset();
114  }
115  if( !mpProxyList )
116  {
117  if( rTextNode.GetGrammarCheck() )
118  mpProxyList.reset( static_cast<SwGrammarMarkUp*>(rTextNode.GetGrammarCheck()->Clone()) );
119  else
120  {
121  mpProxyList.reset( new SwGrammarMarkUp() );
122  mpProxyList->SetInvalid( 0, COMPLETE_STRING );
123  }
124  }
125  mbFinished = false;
126  }
127  pRet = mpProxyList.get();
128  }
129  else
130  {
131  pRet = rTextNode.GetGrammarCheck(); // do you have already a list?
132  if( bCreate && !pRet ) // do you want to create a list?
133  {
134  pRet = new SwGrammarMarkUp();
135  pRet->SetInvalid( 0, COMPLETE_STRING );
136  rTextNode.SetGrammarCheck( pRet );
137  rTextNode.SetGrammarCheckDirty( true );
138  }
139  }
140  return pRet;
141 }
142 
144 {
145  if( !pOld || pOld->Which() != RES_OBJECTDYING )
146  return;
147 
148  const SwPtrMsgPoolItem *pDead = static_cast<const SwPtrMsgPoolItem *>(pOld);
149  if( pDead->pObject == GetRegisteredIn() )
150  { // if my current paragraph dies, I throw the proxy list away
151  aTimer.Stop();
152  EndListeningAll();
153  mpProxyList.reset();
154  }
155 }
156 
158 {
159  if( &rTextNode != GetRegisteredIn() ) // not my paragraph
160  SwTextFrame::repaintTextFrames( rTextNode ); // can be repainted directly
161  else
162  {
163  if( mpProxyList )
164  {
165  mbFinished = true;
166  aTimer.Start(); // will replace old list and repaint with delay
167  }
168  else if( getMyTextNode()->GetGrammarCheck() )
169  { // all grammar problems seems to be gone, no delay needed
170  getMyTextNode()->SetGrammarCheck( nullptr );
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 ~SwGrammarContact() override
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 SwGrammarMarkUp * getGrammarCheck(SwTextNode &rTextNode, bool bCreate) override
getGrammarCheck checks if the given text node is blocked by the current cursor if not...
SwNodeIndex nNode
Definition: pam.hxx:37
static void repaintTextFrames(const SwTextNode &rNode)
Repaint all text frames of the given text node.
Definition: txtfrm.cxx:4006
SwNode & GetNode() const
Definition: ndindex.hxx:118
void SetGrammarCheckDirty(bool bNew) const
Definition: txtedt.cxx:2255
IGrammarContact * getGrammarContact(const SwTextNode &rTextNode)
getGrammarContact() delivers the grammar contact of the document (for a given textnode) ...
Definition: docnew.cxx:797
void finishGrammarCheck(SwTextNode &rTextNode)
finishGrammarCheck() calls the same function of the grammar contact of the document (for a given text...
void EndListeningAll()
Definition: calbck.cxx:124
SwTextNode * getMyTextNode()
SwGrammarMarkUp * GetGrammarCheck()
Definition: txtedt.cxx:2189
virtual void Start() override
DECL_LINK(TimerRepaint, Timer *, void)
virtual void updateCursorPosition(const SwPosition &rNewPos) override
Update cursor position reacts to a change of the current input cursor As long as the cursor in inside...
virtual SwWrongList * Clone() override
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetGrammarCheck(SwGrammarMarkUp *pNew, bool bDelete=true)
Definition: txtedt.cxx:2177
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
void Stop()
const SwModify * GetRegisteredIn() const
Definition: calbck.hxx:157
Organizer of the contact between SwTextNodes and grammar checker.
virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) override
void SetInvokeHandler(const Link< Timer *, void > &rLink)
IMPL_LINK(SwGrammarContact, TimerRepaint, Timer *, pTimer, void)
std::unique_ptr< SwGrammarMarkUp > mpProxyList
#define RES_OBJECTDYING
Definition: hintids.hxx:283
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
void SetInvalid(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:253
virtual void finishGrammarCheck(SwTextNode &rTextNode) override
finishGrammarCheck() has to be called if a grammar checking has been completed for a text node...
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
void SetDebugName(const sal_Char *pDebugName)