LCOV - code coverage report
Current view: top level - sw/source/core/txtnode - SwGrammarContact.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 36 74 48.6 %
Date: 2014-11-03 Functions: 9 14 64.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10