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