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 300 : ~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 558 : SwGrammarContact::SwGrammarContact() : mpProxyList(0), mbFinished( false )
65 : {
66 558 : aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
67 558 : aTimer.SetTimeoutHdl( LINK(this, SwGrammarContact, TimerRepaint) );
68 558 : }
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 3038 : void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos )
87 : {
88 3038 : SwTxtNode* pTxtNode = rNewPos.nNode.GetNode().GetTxtNode();
89 3038 : if( pTxtNode != GetRegisteredIn() ) // paragraph has been changed
90 : {
91 514 : aTimer.Stop();
92 514 : if( GetRegisteredIn() ) // My last paragraph has been left
93 : {
94 36 : 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 36 : GetRegisteredInNonConst()->Remove( this ); // good bye old paragraph
100 36 : mpProxyList = 0;
101 : }
102 514 : if( pTxtNode )
103 514 : pTxtNode->Add( this ); // welcome new paragraph
104 : }
105 3038 : }
106 :
107 : /* deliver a grammar check list for the given text node */
108 29 : SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTxtNode& rTxtNode, bool bCreate )
109 : {
110 29 : SwGrammarMarkUp *pRet = 0;
111 29 : if( GetRegisteredIn() == &rTxtNode ) // hey, that's my current paragraph!
112 : { // so you will get a proxy list...
113 1 : if( bCreate )
114 : {
115 1 : if( mbFinished )
116 : {
117 0 : delete mpProxyList;
118 0 : mpProxyList = 0;
119 : }
120 1 : if( !mpProxyList )
121 : {
122 1 : if( rTxtNode.GetGrammarCheck() )
123 0 : mpProxyList = (SwGrammarMarkUp*)rTxtNode.GetGrammarCheck()->Clone();
124 : else
125 : {
126 1 : mpProxyList = new SwGrammarMarkUp();
127 1 : mpProxyList->SetInvalid( 0, STRING_LEN );
128 : }
129 : }
130 1 : mbFinished = false;
131 : }
132 1 : pRet = mpProxyList;
133 : }
134 : else
135 : {
136 28 : pRet = rTxtNode.GetGrammarCheck(); // do you have already a list?
137 28 : if( bCreate && !pRet ) // do you want to create a list?
138 : {
139 26 : pRet = new SwGrammarMarkUp();
140 26 : pRet->SetInvalid( 0, STRING_LEN );
141 26 : rTxtNode.SetGrammarCheck( pRet );
142 26 : rTxtNode.SetGrammarCheckDirty( true );
143 : }
144 : }
145 29 : return pRet;
146 : }
147 :
148 0 : void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * )
149 : {
150 0 : if( !pOld || pOld->Which() != RES_OBJECTDYING )
151 0 : return;
152 :
153 0 : SwPtrMsgPoolItem *pDead = (SwPtrMsgPoolItem *)pOld;
154 0 : if( pDead->pObject == GetRegisteredIn() )
155 : { // if my current paragraph dies, I throw the proxy list away
156 0 : aTimer.Stop();
157 0 : GetRegisteredInNonConst()->Remove( this );
158 0 : delete mpProxyList;
159 0 : mpProxyList = 0;
160 : }
161 : }
162 :
163 52 : void SwGrammarContact::finishGrammarCheck( SwTxtNode& rTxtNode )
164 : {
165 52 : if( &rTxtNode != GetRegisteredIn() ) // not my paragraph
166 51 : SwTxtFrm::repaintTextFrames( rTxtNode ); // can be repainted directly
167 : else
168 : {
169 1 : if( mpProxyList )
170 : {
171 1 : mbFinished = true;
172 1 : 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 52 : }
181 :
182 558 : IGrammarContact* createGrammarContact()
183 : {
184 558 : return new SwGrammarContact();
185 : }
186 :
187 52 : void finishGrammarCheck( SwTxtNode& rTxtNode )
188 : {
189 52 : IGrammarContact* pGrammarContact = getGrammarContact( rTxtNode );
190 52 : if( pGrammarContact )
191 52 : pGrammarContact->finishGrammarCheck( rTxtNode );
192 52 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|