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 0 : SwTxtNode* getMyTxtNode() { return (SwTxtNode*)GetRegisteredIn(); }
49 : DECL_LINK( TimerRepaint, Timer * );
50 :
51 : public:
52 : SwGrammarContact();
53 1790 : ~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 898 : SwGrammarContact::SwGrammarContact() : mpProxyList(0), mbFinished( false )
65 : {
66 898 : aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
67 898 : aTimer.SetTimeoutHdl( LINK(this, SwGrammarContact, TimerRepaint) );
68 898 : }
69 :
70 0 : IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer )
71 : {
72 0 : if( pTimer )
73 : {
74 0 : pTimer->Stop();
75 0 : if( GetRegisteredIn() )
76 : { //Replace the old wrong list by the proxy list and repaint all frames
77 0 : getMyTxtNode()->SetGrammarCheck( mpProxyList, true );
78 0 : mpProxyList = 0;
79 0 : SwTxtFrm::repaintTextFrames( *getMyTxtNode() );
80 : }
81 : }
82 0 : return 0;
83 : }
84 :
85 : /* I'm always a client of the current paragraph */
86 18755 : void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
87 : {
88 18755 : SwTxtNode* pTxtNode = rNewPos.nNode.GetNode().GetTxtNode();
89 18755 : if( pTxtNode != GetRegisteredIn() ) // paragraph has been changed
90 : {
91 835 : aTimer.Stop();
92 835 : if( GetRegisteredIn() ) // My last paragraph has been left
93 : {
94 38 : if( mpProxyList )
95 : { // replace old list by the proxy list and repaint
96 0 : getMyTxtNode()->SetGrammarCheck( mpProxyList, true );
97 0 : SwTxtFrm::repaintTextFrames( *getMyTxtNode() );
98 : }
99 38 : GetRegisteredInNonConst()->Remove( this ); // good bye old paragraph
100 38 : mpProxyList = 0;
101 : }
102 835 : if( pTxtNode )
103 835 : pTxtNode->Add( this ); // welcome new paragraph
104 : }
105 18755 : }
106 :
107 : /* deliver a grammar check list for the given text node */
108 5258 : SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTxtNode& rTxtNode, bool bCreate )
109 : {
110 5258 : SwGrammarMarkUp *pRet = 0;
111 5258 : if( GetRegisteredIn() == &rTxtNode ) // hey, that's my current paragraph!
112 : { // so you will get a proxy list...
113 2428 : if( bCreate )
114 : {
115 0 : if( mbFinished )
116 : {
117 0 : delete mpProxyList;
118 0 : mpProxyList = 0;
119 : }
120 0 : if( !mpProxyList )
121 : {
122 0 : if( rTxtNode.GetGrammarCheck() )
123 0 : mpProxyList = (SwGrammarMarkUp*)rTxtNode.GetGrammarCheck()->Clone();
124 : else
125 : {
126 0 : mpProxyList = new SwGrammarMarkUp();
127 0 : mpProxyList->SetInvalid( 0, STRING_LEN );
128 : }
129 : }
130 0 : mbFinished = false;
131 : }
132 2428 : pRet = mpProxyList;
133 : }
134 : else
135 : {
136 2830 : pRet = rTxtNode.GetGrammarCheck(); // do you have already a list?
137 2830 : if( bCreate && !pRet ) // do you want to create a list?
138 : {
139 0 : pRet = new SwGrammarMarkUp();
140 0 : pRet->SetInvalid( 0, STRING_LEN );
141 0 : rTxtNode.SetGrammarCheck( pRet );
142 0 : rTxtNode.SetGrammarCheckDirty( true );
143 : }
144 : }
145 5258 : return pRet;
146 : }
147 :
148 3151 : void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * )
149 : {
150 3151 : if( !pOld || pOld->Which() != RES_OBJECTDYING )
151 6298 : return;
152 :
153 4 : SwPtrMsgPoolItem *pDead = (SwPtrMsgPoolItem *)pOld;
154 4 : if( pDead->pObject == GetRegisteredIn() )
155 : { // if my current paragraph dies, I throw the proxy list away
156 4 : aTimer.Stop();
157 4 : GetRegisteredInNonConst()->Remove( this );
158 4 : delete mpProxyList;
159 4 : mpProxyList = 0;
160 : }
161 : }
162 :
163 0 : void SwGrammarContact::finishGrammarCheck( SwTxtNode& rTxtNode )
164 : {
165 0 : if( &rTxtNode != GetRegisteredIn() ) // not my paragraph
166 0 : SwTxtFrm::repaintTextFrames( rTxtNode ); // can be repainted directly
167 : else
168 : {
169 0 : if( mpProxyList )
170 : {
171 0 : mbFinished = true;
172 0 : 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 0 : }
181 :
182 898 : IGrammarContact* createGrammarContact()
183 : {
184 898 : return new SwGrammarContact();
185 : }
186 :
187 0 : void finishGrammarCheck( SwTxtNode& rTxtNode )
188 : {
189 0 : IGrammarContact* pGrammarContact = getGrammarContact( rTxtNode );
190 0 : if( pGrammarContact )
191 0 : pGrammarContact->finishGrammarCheck( rTxtNode );
192 99 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|