Line data Source code
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 : */
41 : class SwGrammarContact : public IGrammarContact, public SwClient
42 : {
43 : Timer aTimer;
44 : SwGrammarMarkUp *mpProxyList;
45 : bool mbFinished;
46 0 : SwTextNode* getMyTextNode() { return static_cast<SwTextNode*>(GetRegisteredIn()); }
47 : DECL_LINK_TYPED( TimerRepaint, Timer *, void );
48 :
49 : public:
50 : SwGrammarContact();
51 5898 : virtual ~SwGrammarContact() { aTimer.Stop(); delete mpProxyList; }
52 :
53 : // (pure) virtual functions of IGrammarContact
54 : virtual void updateCursorPosition( const SwPosition& rNewPos ) SAL_OVERRIDE;
55 : virtual SwGrammarMarkUp* getGrammarCheck( SwTextNode& rTextNode, bool bCreate ) SAL_OVERRIDE;
56 : virtual void finishGrammarCheck( SwTextNode& rTextNode ) SAL_OVERRIDE;
57 : protected:
58 : // virtual function of SwClient
59 : virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew) SAL_OVERRIDE;
60 : };
61 :
62 2958 : SwGrammarContact::SwGrammarContact() : mpProxyList(0), mbFinished( false )
63 : {
64 2958 : aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
65 2958 : aTimer.SetTimeoutHdl( LINK(this, SwGrammarContact, TimerRepaint) );
66 2958 : }
67 :
68 0 : IMPL_LINK_TYPED( SwGrammarContact, TimerRepaint, Timer *, pTimer, void )
69 : {
70 0 : if( pTimer )
71 : {
72 0 : pTimer->Stop();
73 0 : if( GetRegisteredIn() )
74 : { //Replace the old wrong list by the proxy list and repaint all frames
75 0 : getMyTextNode()->SetGrammarCheck( mpProxyList, true );
76 0 : mpProxyList = 0;
77 0 : SwTextFrm::repaintTextFrames( *getMyTextNode() );
78 : }
79 : }
80 0 : }
81 :
82 : /* I'm always a client of the current paragraph */
83 43508 : void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
84 : {
85 43508 : SwTextNode* pTextNode = rNewPos.nNode.GetNode().GetTextNode();
86 43508 : if( pTextNode != GetRegisteredIn() ) // paragraph has been changed
87 : {
88 2983 : aTimer.Stop();
89 2983 : if( GetRegisteredIn() ) // My last paragraph has been left
90 : {
91 174 : if( mpProxyList )
92 : { // replace old list by the proxy list and repaint
93 0 : getMyTextNode()->SetGrammarCheck( mpProxyList, true );
94 0 : SwTextFrm::repaintTextFrames( *getMyTextNode() );
95 : }
96 174 : GetRegisteredInNonConst()->Remove( this ); // good bye old paragraph
97 174 : mpProxyList = 0;
98 : }
99 2983 : if( pTextNode )
100 2983 : pTextNode->Add( this ); // welcome new paragraph
101 : }
102 43508 : }
103 :
104 : /* deliver a grammar check list for the given text node */
105 12239 : SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTextNode& rTextNode, bool bCreate )
106 : {
107 12239 : SwGrammarMarkUp *pRet = 0;
108 12239 : if( GetRegisteredIn() == &rTextNode ) // hey, that's my current paragraph!
109 : { // so you will get a proxy list...
110 2392 : if( bCreate )
111 : {
112 0 : if( mbFinished )
113 : {
114 0 : delete mpProxyList;
115 0 : mpProxyList = 0;
116 : }
117 0 : if( !mpProxyList )
118 : {
119 0 : if( rTextNode.GetGrammarCheck() )
120 0 : mpProxyList = static_cast<SwGrammarMarkUp*>(rTextNode.GetGrammarCheck()->Clone());
121 : else
122 : {
123 0 : mpProxyList = new SwGrammarMarkUp();
124 0 : mpProxyList->SetInvalid( 0, COMPLETE_STRING );
125 : }
126 : }
127 0 : mbFinished = false;
128 : }
129 2392 : pRet = mpProxyList;
130 : }
131 : else
132 : {
133 9847 : pRet = rTextNode.GetGrammarCheck(); // do you have already a list?
134 9847 : if( bCreate && !pRet ) // do you want to create a list?
135 : {
136 0 : pRet = new SwGrammarMarkUp();
137 0 : pRet->SetInvalid( 0, COMPLETE_STRING );
138 0 : rTextNode.SetGrammarCheck( pRet );
139 0 : rTextNode.SetGrammarCheckDirty( true );
140 : }
141 : }
142 12239 : return pRet;
143 : }
144 :
145 3547 : void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * )
146 : {
147 3547 : if( !pOld || pOld->Which() != RES_OBJECTDYING )
148 7046 : return;
149 :
150 48 : const SwPtrMsgPoolItem *pDead = static_cast<const SwPtrMsgPoolItem *>(pOld);
151 48 : if( pDead->pObject == GetRegisteredIn() )
152 : { // if my current paragraph dies, I throw the proxy list away
153 48 : aTimer.Stop();
154 48 : GetRegisteredInNonConst()->Remove( this );
155 48 : delete mpProxyList;
156 48 : mpProxyList = 0;
157 : }
158 : }
159 :
160 0 : void SwGrammarContact::finishGrammarCheck( SwTextNode& rTextNode )
161 : {
162 0 : if( &rTextNode != GetRegisteredIn() ) // not my paragraph
163 0 : SwTextFrm::repaintTextFrames( rTextNode ); // can be repainted directly
164 : else
165 : {
166 0 : if( mpProxyList )
167 : {
168 0 : mbFinished = true;
169 0 : aTimer.Start(); // will replace old list and repaint with delay
170 : }
171 0 : else if( getMyTextNode()->GetGrammarCheck() )
172 : { // all grammar problems seems to be gone, no delay needed
173 0 : getMyTextNode()->SetGrammarCheck( 0, true );
174 0 : SwTextFrm::repaintTextFrames( *getMyTextNode() );
175 : }
176 : }
177 0 : }
178 :
179 2958 : IGrammarContact* createGrammarContact()
180 : {
181 2958 : return new SwGrammarContact();
182 : }
183 :
184 0 : void finishGrammarCheck( SwTextNode& rTextNode )
185 : {
186 0 : IGrammarContact* pGrammarContact = getGrammarContact( rTextNode );
187 0 : if( pGrammarContact )
188 0 : pGrammarContact->finishGrammarCheck( rTextNode );
189 177 : }
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|