Branch data 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 : :
21 : : #include <vcl/timer.hxx>
22 : : #include <hints.hxx>
23 : : #include <IGrammarContact.hxx>
24 : : #include <pam.hxx>
25 : : #include <ndtxt.hxx>
26 : : #include <SwGrammarMarkUp.hxx>
27 : : #include <txtfrm.hxx>
28 : : #include <rootfrm.hxx>
29 : : #include <viewsh.hxx>
30 : :
31 : : /* SwGrammarContact
32 : : This class is responsible for the delayed display of grammar checks when a paragraph is edited
33 : : It's a client of the paragraph the cursor points to.
34 : : If the cursor position changes, updateCursorPosition has to be called
35 : : If the grammar checker wants to set a grammar marker at a paragraph, he has to request
36 : : the grammar list from this class. If the requested paragraph is not edited, it returns
37 : : the normal grammar list. But if the paragraph is the active one, a proxy list will be returned and
38 : : all changes are set in this proxy list. If the cursor leaves the paragraph the proxy list
39 : : will replace the old list. If the grammar checker has completed the paragraph ('setChecked')
40 : : then a timer is setup which replaces the old list as well.
41 : : */
42 : :
43 : : class SwGrammarContact : public IGrammarContact, public SwClient
44 : : {
45 : : Timer aTimer;
46 : : SwGrammarMarkUp *mpProxyList;
47 : : bool mbFinished;
48 : 658 : SwTxtNode* getMyTxtNode() { return (SwTxtNode*)GetRegisteredIn(); }
49 : : DECL_LINK( TimerRepaint, Timer * );
50 : :
51 : : public:
52 : : SwGrammarContact();
53 [ + - ][ + + ]: 2916 : ~SwGrammarContact() { aTimer.Stop(); delete mpProxyList; }
[ + - ][ + - ]
[ + - ][ - + ]
54 : :
55 : : // (pure) virtual functions of IGrammarContact
56 : : virtual void updateCursorPosition( const SwPosition& rNewPos );
57 : : virtual SwGrammarMarkUp* getGrammarCheck( SwTxtNode& rTxtNode, bool bCreate );
58 : : virtual void finishGrammarCheck( SwTxtNode& rTxtNode );
59 : : protected:
60 : : // virtual function of SwClient
61 : : virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew);
62 : : };
63 : :
64 [ + - ]: 1549 : SwGrammarContact::SwGrammarContact() : mpProxyList(0), mbFinished( false )
65 : : {
66 [ + - ]: 1549 : aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
67 [ + - ]: 1549 : aTimer.SetTimeoutHdl( LINK(this, SwGrammarContact, TimerRepaint) );
68 : 1549 : }
69 : :
70 : 326 : IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer )
71 : : {
72 [ + - ]: 326 : if( pTimer )
73 : : {
74 : 326 : pTimer->Stop();
75 [ + - ]: 326 : if( GetRegisteredIn() )
76 : : { //Replace the old wrong list by the proxy list and repaint all frames
77 : 326 : getMyTxtNode()->SetGrammarCheck( mpProxyList, true );
78 : 326 : mpProxyList = 0;
79 : 326 : SwTxtFrm::repaintTextFrames( *getMyTxtNode() );
80 : : }
81 : : }
82 : 326 : return 0;
83 : : }
84 : :
85 : : /* I'm always a client of the current paragraph */
86 : 35537 : void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
87 : : {
88 : 35537 : SwTxtNode* pTxtNode = rNewPos.nNode.GetNode().GetTxtNode();
89 [ + + ]: 35537 : if( pTxtNode != GetRegisteredIn() ) // paragraph has been changed
90 : : {
91 : 1366 : aTimer.Stop();
92 [ + + ]: 1366 : if( GetRegisteredIn() ) // My last paragraph has been left
93 : : {
94 [ + + ]: 40 : if( mpProxyList )
95 : : { // replace old list by the proxy list and repaint
96 : 3 : getMyTxtNode()->SetGrammarCheck( mpProxyList, true );
97 : 3 : SwTxtFrm::repaintTextFrames( *getMyTxtNode() );
98 : : }
99 : 40 : GetRegisteredInNonConst()->Remove( this ); // good bye old paragraph
100 : 40 : mpProxyList = 0;
101 : : }
102 [ + - ]: 1366 : if( pTxtNode )
103 : 1366 : pTxtNode->Add( this ); // welcome new paragraph
104 : : }
105 : 35537 : }
106 : :
107 : : /* deliver a grammar check list for the given text node */
108 : 13350 : SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTxtNode& rTxtNode, bool bCreate )
109 : : {
110 : 13350 : SwGrammarMarkUp *pRet = 0;
111 [ + + ]: 13350 : if( GetRegisteredIn() == &rTxtNode ) // hey, that's my current paragraph!
112 : : { // so you will get a proxy list...
113 [ + + ]: 6227 : if( bCreate )
114 : : {
115 [ + + ]: 1393 : if( mbFinished )
116 : : {
117 [ + + ]: 702 : delete mpProxyList;
118 : 702 : mpProxyList = 0;
119 : : }
120 [ + + ]: 1393 : if( !mpProxyList )
121 : : {
122 [ + + ]: 1386 : if( rTxtNode.GetGrammarCheck() )
123 : 235 : mpProxyList = (SwGrammarMarkUp*)rTxtNode.GetGrammarCheck()->Clone();
124 : : else
125 : : {
126 [ + - ]: 1151 : mpProxyList = new SwGrammarMarkUp();
127 : 1151 : mpProxyList->SetInvalid( 0, STRING_LEN );
128 : : }
129 : : }
130 : 1393 : mbFinished = false;
131 : : }
132 : 6227 : pRet = mpProxyList;
133 : : }
134 : : else
135 : : {
136 : 7123 : pRet = rTxtNode.GetGrammarCheck(); // do you have already a list?
137 [ + + ][ + + ]: 7123 : if( bCreate && !pRet ) // do you want to create a list?
138 : : {
139 [ + - ]: 989 : pRet = new SwGrammarMarkUp();
140 : 989 : pRet->SetInvalid( 0, STRING_LEN );
141 : 989 : rTxtNode.SetGrammarCheck( pRet );
142 : 989 : rTxtNode.SetGrammarCheckDirty( true );
143 : : }
144 : : }
145 : 13350 : return pRet;
146 : : }
147 : :
148 : 6202 : void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * )
149 : : {
150 [ + + ][ + + ]: 6202 : if( !pOld || pOld->Which() != RES_OBJECTDYING )
[ + + ]
151 : 6202 : return;
152 : :
153 : 8 : SwPtrMsgPoolItem *pDead = (SwPtrMsgPoolItem *)pOld;
154 [ + - ]: 8 : if( pDead->pObject == GetRegisteredIn() )
155 : : { // if my current paragraph dies, I throw the proxy list away
156 : 8 : aTimer.Stop();
157 : 8 : GetRegisteredInNonConst()->Remove( this );
158 [ + + ]: 8 : delete mpProxyList;
159 : 8 : mpProxyList = 0;
160 : : }
161 : : }
162 : :
163 : 3685 : void SwGrammarContact::finishGrammarCheck( SwTxtNode& rTxtNode )
164 : : {
165 [ + + ]: 3685 : if( &rTxtNode != GetRegisteredIn() ) // not my paragraph
166 : 2299 : SwTxtFrm::repaintTextFrames( rTxtNode ); // can be repainted directly
167 : : else
168 : : {
169 [ + - ]: 1386 : if( mpProxyList )
170 : : {
171 : 1386 : mbFinished = true;
172 : 1386 : aTimer.Start(); // will replace old list and repaint with delay
173 : : }
174 [ # # ]: 0 : else if( getMyTxtNode()->GetGrammarCheck() )
175 : : { // all grammar problems seems to be gone, no delay needed
176 : 0 : getMyTxtNode()->SetGrammarCheck( 0, true );
177 : 0 : SwTxtFrm::repaintTextFrames( *getMyTxtNode() );
178 : : }
179 : : }
180 : 3685 : }
181 : :
182 : 1549 : IGrammarContact* createGrammarContact()
183 : : {
184 [ + - ]: 1549 : return new SwGrammarContact();
185 : : }
186 : :
187 : 3685 : void finishGrammarCheck( SwTxtNode& rTxtNode )
188 : : {
189 : 3685 : IGrammarContact* pGrammarContact = getGrammarContact( rTxtNode );
190 [ + - ]: 3685 : if( pGrammarContact )
191 : 3685 : pGrammarContact->finishGrammarCheck( rTxtNode );
192 : 3685 : }
193 : :
194 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|