android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / txtnode / GrammarContact.cxx
blob47e2fac264c02e6386ba2cf5d37221931a311bd4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <GrammarContact.hxx>
21 #include <doc.hxx>
22 #include <pam.hxx>
23 #include <ndtxt.hxx>
24 #include <txtfrm.hxx>
26 namespace sw
29 GrammarContact::GrammarContact()
30 : m_aTimer( "sw::SwGrammarContact TimerRepaint" ),
31 m_isFinished( false ),
32 m_pTextNode(nullptr)
34 m_aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked'
35 m_aTimer.SetInvokeHandler( LINK(this, GrammarContact, TimerRepaint) );
38 void GrammarContact::CheckBroadcaster()
40 if (HasBroadcaster())
41 return;
42 m_pTextNode = nullptr;
43 m_pProxyList.reset();
46 IMPL_LINK( GrammarContact, TimerRepaint, Timer *, pTimer, void )
48 CheckBroadcaster();
49 if( pTimer )
51 pTimer->Stop();
52 if( m_pTextNode )
53 { //Replace the old wrong list by the proxy list and repaint all frames
54 m_pTextNode->SetGrammarCheck( std::move(m_pProxyList) );
55 SwTextFrame::repaintTextFrames( *m_pTextNode );
60 /* I'm always a client of the current paragraph */
61 void GrammarContact::updateCursorPosition( const SwPosition& rNewPos )
63 CheckBroadcaster();
64 SwTextNode* pTextNode = rNewPos.GetNode().GetTextNode();
65 if( pTextNode == m_pTextNode ) // paragraph has been changed
66 return;
68 m_aTimer.Stop();
69 if( m_pTextNode ) // My last paragraph has been left
71 if( m_pProxyList )
72 { // replace old list by the proxy list and repaint
73 m_pTextNode->SetGrammarCheck( std::move(m_pProxyList) );
74 SwTextFrame::repaintTextFrames( *m_pTextNode );
76 EndListeningAll();
78 if( pTextNode )
80 m_pTextNode = pTextNode;
81 EndListeningAll();
82 StartListening(pTextNode->GetNotifier()); // welcome new paragraph
86 /* deliver a grammar check list for the given text node */
87 SwGrammarMarkUp* GrammarContact::getGrammarCheck( SwTextNode& rTextNode, bool bCreate )
89 SwGrammarMarkUp *pRet = nullptr;
90 CheckBroadcaster();
91 if( m_pTextNode == &rTextNode ) // hey, that's my current paragraph!
92 { // so you will get a proxy list...
93 if( bCreate )
95 if( m_isFinished )
97 m_pProxyList.reset();
99 if( !m_pProxyList )
101 if( rTextNode.GetGrammarCheck() )
102 m_pProxyList.reset( static_cast<SwGrammarMarkUp*>(rTextNode.GetGrammarCheck()->Clone()) );
103 else
105 m_pProxyList.reset( new SwGrammarMarkUp() );
106 m_pProxyList->SetInvalid( 0, COMPLETE_STRING );
109 m_isFinished = false;
111 pRet = m_pProxyList.get();
113 else
115 pRet = rTextNode.GetGrammarCheck(); // do you have already a list?
116 if( bCreate && !pRet ) // do you want to create a list?
118 pRet = new SwGrammarMarkUp();
119 pRet->SetInvalid( 0, COMPLETE_STRING );
120 rTextNode.SetGrammarCheck( std::unique_ptr<SwGrammarMarkUp>(pRet) );
121 rTextNode.SetGrammarCheckDirty( true );
124 return pRet;
127 void GrammarContact::finishGrammarCheck( SwTextNode& rTextNode )
129 CheckBroadcaster();
130 if( &rTextNode != m_pTextNode ) // not my paragraph
131 SwTextFrame::repaintTextFrames( rTextNode ); // can be repainted directly
132 else
134 if( m_pProxyList )
136 m_isFinished = true;
137 m_aTimer.Start(); // will replace old list and repaint with delay
139 else if( m_pTextNode->GetGrammarCheck() )
140 { // all grammar problems seems to be gone, no delay needed
141 m_pTextNode->ClearGrammarCheck();
142 SwTextFrame::repaintTextFrames( *m_pTextNode );
147 sw::GrammarContact* getGrammarContactFor(const SwTextNode& rTextNode)
149 const SwDoc& rDoc = rTextNode.GetDoc();
150 if (rDoc.IsInDtor())
151 return nullptr;
152 return rDoc.getGrammarContact().get();
155 void finishGrammarCheckFor(SwTextNode& rTextNode)
157 sw::GrammarContact* pGrammarContact = getGrammarContactFor(rTextNode);
158 if (pGrammarContact)
160 pGrammarContact->finishGrammarCheck(rTextNode);
164 } // end sw
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */