xref: /aoo41x/main/sc/source/ui/view/spelldialog.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir #include "spelldialog.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <sfx2/app.hxx>
29cdf0e10cSrcweir #include <sfx2/bindings.hxx>
30cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
31cdf0e10cSrcweir #include <svx/svxids.hrc>
32cdf0e10cSrcweir #include <editeng/editstat.hxx>
33cdf0e10cSrcweir #include <editeng/editview.hxx>
34cdf0e10cSrcweir #include <editeng/unolingu.hxx>
35cdf0e10cSrcweir #include "selectionstate.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "spelleng.hxx"
38cdf0e10cSrcweir #include "tabvwsh.hxx"
39cdf0e10cSrcweir #include "docsh.hxx"
40cdf0e10cSrcweir #include "scmod.hxx"
41cdf0e10cSrcweir #include "editable.hxx"
42cdf0e10cSrcweir #include "undoblk.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ============================================================================
45cdf0e10cSrcweir 
SFX_IMPL_CHILDWINDOW(ScSpellDialogChildWindow,SID_SPELL_DIALOG)46cdf0e10cSrcweir SFX_IMPL_CHILDWINDOW( ScSpellDialogChildWindow, SID_SPELL_DIALOG )
47cdf0e10cSrcweir 
48cdf0e10cSrcweir ScSpellDialogChildWindow::ScSpellDialogChildWindow( Window* pParentP, sal_uInt16 nId,
49cdf0e10cSrcweir         SfxBindings* pBindings, SfxChildWinInfo* pInfo ) :
50cdf0e10cSrcweir     ::svx::SpellDialogChildWindow( pParentP, nId, pBindings, pInfo ),
51cdf0e10cSrcweir     mpViewShell( 0 ),
52cdf0e10cSrcweir     mpViewData( 0 ),
53cdf0e10cSrcweir     mpDocShell( 0 ),
54cdf0e10cSrcweir     mpDoc( 0 ),
55cdf0e10cSrcweir     mbNeedNextObj( false ),
56cdf0e10cSrcweir     mbOldIdleDisabled( false )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     Init();
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
~ScSpellDialogChildWindow()61cdf0e10cSrcweir ScSpellDialogChildWindow::~ScSpellDialogChildWindow()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     Reset();
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
GetInfo() const66cdf0e10cSrcweir SfxChildWinInfo ScSpellDialogChildWindow::GetInfo() const
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 	return ::svx::SpellDialogChildWindow::GetInfo();
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
InvalidateSpellDialog()71cdf0e10cSrcweir void ScSpellDialogChildWindow::InvalidateSpellDialog()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     ::svx::SpellDialogChildWindow::InvalidateSpellDialog();
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir // protected ------------------------------------------------------------------
77cdf0e10cSrcweir 
GetNextWrongSentence(bool)78cdf0e10cSrcweir ::svx::SpellPortions ScSpellDialogChildWindow::GetNextWrongSentence( bool /*bRecheck*/ )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     ::svx::SpellPortions aPortions;
81cdf0e10cSrcweir     if( mxEngine.get() && mpViewData )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         if( EditView* pEditView = mpViewData->GetSpellingView() )
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             // edit engine handles cell iteration internally
86cdf0e10cSrcweir             do
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 if( mbNeedNextObj )
89cdf0e10cSrcweir                     mxEngine->SpellNextDocument();
90cdf0e10cSrcweir                 mbNeedNextObj = !mxEngine->IsFinished() && !mxEngine->SpellSentence( *pEditView, aPortions, false );
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir             while( mbNeedNextObj );
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // finished? - close the spelling dialog
96cdf0e10cSrcweir         if( mxEngine->IsFinished() )
97cdf0e10cSrcweir             GetBindings().GetDispatcher()->Execute( SID_SPELL_DIALOG, SFX_CALLMODE_ASYNCHRON );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir     return aPortions;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
ApplyChangedSentence(const::svx::SpellPortions & rChanged,bool bRecheck)102cdf0e10cSrcweir void ScSpellDialogChildWindow::ApplyChangedSentence( const ::svx::SpellPortions& rChanged, bool bRecheck )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     if( mxEngine.get() && mpViewData )
105cdf0e10cSrcweir         if( EditView* pEditView = mpViewData->GetSpellingView() )
106cdf0e10cSrcweir             mxEngine->ApplyChangedSentence( *pEditView, rChanged, bRecheck );
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
GetFocus()109cdf0e10cSrcweir void ScSpellDialogChildWindow::GetFocus()
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     if( IsSelectionChanged() )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         Reset();
114cdf0e10cSrcweir         InvalidateSpellDialog();
115cdf0e10cSrcweir         Init();
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
LoseFocus()119cdf0e10cSrcweir void ScSpellDialogChildWindow::LoseFocus()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // private --------------------------------------------------------------------
124cdf0e10cSrcweir 
Reset()125cdf0e10cSrcweir void ScSpellDialogChildWindow::Reset()
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     if( mpViewShell && (mpViewShell == PTR_CAST( ScTabViewShell, SfxViewShell::Current() )) )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         if( mxEngine.get() && mxEngine->IsAnyModified() )
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             const ScAddress& rCursor = mxOldSel->GetCellCursor();
132cdf0e10cSrcweir             SCTAB nTab = rCursor.Tab();
133cdf0e10cSrcweir             SCCOL nOldCol = rCursor.Col();
134cdf0e10cSrcweir             SCROW nOldRow = rCursor.Row();
135cdf0e10cSrcweir             SCCOL nNewCol = mpViewData->GetCurX();
136cdf0e10cSrcweir             SCROW nNewRow = mpViewData->GetCurY();
137cdf0e10cSrcweir             mpDocShell->GetUndoManager()->AddUndoAction( new ScUndoConversion(
138cdf0e10cSrcweir                 mpDocShell, mpViewData->GetMarkData(),
139cdf0e10cSrcweir                 nOldCol, nOldRow, nTab, mxUndoDoc.release(),
140cdf0e10cSrcweir                 nNewCol, nNewRow, nTab, mxRedoDoc.release(),
141cdf0e10cSrcweir                 ScConversionParam( SC_CONVERSION_SPELLCHECK ) ) );
142cdf0e10cSrcweir             mpDoc->SetDirty();
143cdf0e10cSrcweir             mpDocShell->SetDocumentModified();
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         mpViewData->SetSpellingView( 0 );
147cdf0e10cSrcweir         mpViewShell->KillEditView( sal_True );
148cdf0e10cSrcweir         mpDocShell->PostPaintGridAll();
149cdf0e10cSrcweir         mpViewShell->UpdateInputHandler();
150cdf0e10cSrcweir         mpDoc->DisableIdle( mbOldIdleDisabled );
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     mxEngine.reset();
153cdf0e10cSrcweir     mxUndoDoc.reset();
154cdf0e10cSrcweir     mxRedoDoc.reset();
155cdf0e10cSrcweir     mxOldSel.reset();
156cdf0e10cSrcweir     mpViewShell = 0;
157cdf0e10cSrcweir     mpViewData = 0;
158cdf0e10cSrcweir     mpDocShell = 0;
159cdf0e10cSrcweir     mpDoc = 0;
160cdf0e10cSrcweir     mbNeedNextObj = false;
161cdf0e10cSrcweir     mbOldIdleDisabled = false;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
Init()164cdf0e10cSrcweir void ScSpellDialogChildWindow::Init()
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     if( mpViewShell )
167cdf0e10cSrcweir         return;
168cdf0e10cSrcweir     if( (mpViewShell = PTR_CAST( ScTabViewShell, SfxViewShell::Current() )) == 0 )
169cdf0e10cSrcweir         return;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     mpViewData = mpViewShell->GetViewData();
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     // exit edit mode - TODO support spelling in edit mode
174cdf0e10cSrcweir     if( mpViewData->HasEditView( mpViewData->GetActivePart() ) )
175cdf0e10cSrcweir         SC_MOD()->InputEnterHandler();
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     mxOldSel.reset( new ScSelectionState( *mpViewData ) );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     mpDocShell = mpViewData->GetDocShell();
180cdf0e10cSrcweir     mpDoc = mpDocShell->GetDocument();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     const ScAddress& rCursor = mxOldSel->GetCellCursor();
183cdf0e10cSrcweir     SCCOL nCol = rCursor.Col();
184cdf0e10cSrcweir     SCROW nRow = rCursor.Row();
185cdf0e10cSrcweir     SCTAB nTab = rCursor.Tab();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     ScMarkData& rMarkData = mpViewData->GetMarkData();
188cdf0e10cSrcweir     rMarkData.MarkToMulti();
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     switch( mxOldSel->GetSelectionType() )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         case SC_SELECTTYPE_NONE:
193cdf0e10cSrcweir         case SC_SELECTTYPE_SHEET:
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             // test if there is something editable
196cdf0e10cSrcweir             ScEditableTester aTester( mpDoc, rMarkData );
197cdf0e10cSrcweir             if( !aTester.IsEditable() )
198cdf0e10cSrcweir             {
199cdf0e10cSrcweir                 // #i85751# Don't show a ErrorMessage here, because the vcl
200cdf0e10cSrcweir                 // parent of the InfoBox is not fully initialized yet.
201cdf0e10cSrcweir                 // This leads to problems in the modality behaviour of the
202cdf0e10cSrcweir                 // ScSpellDialogChildWindow.
203cdf0e10cSrcweir 
204cdf0e10cSrcweir                 //mpViewShell->ErrorMessage( aTester.GetMessageId() );
205cdf0e10cSrcweir                 return;
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir         break;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         // edit mode exited, see TODO above
211cdf0e10cSrcweir //        case SC_SELECTTYPE_EDITCELL:
212cdf0e10cSrcweir //        break;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         default:
215cdf0e10cSrcweir             DBG_ERRORFILE( "ScSpellDialogChildWindow::Init - unknown selection type" );
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     mbOldIdleDisabled = mpDoc->IsIdleDisabled();
219cdf0e10cSrcweir     mpDoc->DisableIdle( sal_True );   // #42726# stop online spelling
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     // *** create Undo/Redo documents *** -------------------------------------
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     mxUndoDoc.reset( new ScDocument( SCDOCMODE_UNDO ) );
224cdf0e10cSrcweir     mxUndoDoc->InitUndo( mpDoc, nTab, nTab );
225cdf0e10cSrcweir     mxRedoDoc.reset( new ScDocument( SCDOCMODE_UNDO ) );
226cdf0e10cSrcweir     mxRedoDoc->InitUndo( mpDoc, nTab, nTab );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     if ( rMarkData.GetSelectCount() > 1 )
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         SCTAB nTabCount = mpDoc->GetTableCount();
231cdf0e10cSrcweir         for( SCTAB nOtherTab = 0; nOtherTab < nTabCount; ++nOtherTab )
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir             if( rMarkData.GetTableSelect( nOtherTab ) && (nOtherTab != nTab) )
234cdf0e10cSrcweir             {
235cdf0e10cSrcweir                 mxUndoDoc->AddUndoTab( nOtherTab, nOtherTab );
236cdf0e10cSrcweir                 mxRedoDoc->AddUndoTab( nOtherTab, nOtherTab );
237cdf0e10cSrcweir             }
238cdf0e10cSrcweir         }
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     // *** create and init the edit engine *** --------------------------------
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     mxEngine.reset( new ScSpellingEngine(
244cdf0e10cSrcweir         mpDoc->GetEnginePool(), *mpViewData, mxUndoDoc.get(), mxRedoDoc.get(), LinguMgr::GetSpellChecker() ) );
245cdf0e10cSrcweir     mxEngine->SetRefDevice( mpViewData->GetActiveWin() );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     mpViewShell->MakeEditView( mxEngine.get(), nCol, nRow );
248cdf0e10cSrcweir     EditView* pEditView = mpViewData->GetEditView( mpViewData->GetActivePart() );
249cdf0e10cSrcweir     mpViewData->SetSpellingView( pEditView );
250cdf0e10cSrcweir     Rectangle aRect( Point( 0, 0 ), Point( 0, 0 ) );
251cdf0e10cSrcweir     pEditView->SetOutputArea( aRect );
252cdf0e10cSrcweir     mxEngine->SetControlWord( EE_CNTRL_USECHARATTRIBS );
253cdf0e10cSrcweir     mxEngine->EnableUndo( sal_False );
254cdf0e10cSrcweir     mxEngine->SetPaperSize( aRect.GetSize() );
255cdf0e10cSrcweir     mxEngine->SetText( EMPTY_STRING );
256cdf0e10cSrcweir     mxEngine->ClearModifyFlag();
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     mbNeedNextObj = true;
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
IsSelectionChanged()261cdf0e10cSrcweir bool ScSpellDialogChildWindow::IsSelectionChanged()
262cdf0e10cSrcweir {
263cdf0e10cSrcweir     if( !mxOldSel.get() || !mpViewShell || (mpViewShell != PTR_CAST( ScTabViewShell, SfxViewShell::Current() )) )
264cdf0e10cSrcweir         return true;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     if( EditView* pEditView = mpViewData->GetSpellingView() )
267cdf0e10cSrcweir         if( pEditView->GetEditEngine() != mxEngine.get() )
268cdf0e10cSrcweir             return true;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     ScSelectionState aNewSel( *mpViewData );
271cdf0e10cSrcweir     return mxOldSel->GetSheetSelection() != aNewSel.GetSheetSelection();
272cdf0e10cSrcweir }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir // ============================================================================
275cdf0e10cSrcweir 
276