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 "SwRewriter.hxx"
21 : #include "chrdlg.hrc"
22 : #include "cmdid.h"
23 : #include "comcore.hrc"
24 : #include "crsskip.hxx"
25 : #include "doc.hxx"
26 : #include "docsh.hxx" //CheckSpellChanges
27 : #include "edtwin.hxx"
28 : #include "helpid.h"
29 : #include "hintids.hxx"
30 : #include "langhelper.hxx"
31 : #include "ndtxt.hxx"
32 : #include "olmenu.hrc"
33 : #include "olmenu.hxx"
34 : #include "swabstdlg.hxx"
35 : #include "swmodule.hxx"
36 : #include "swtypes.hxx"
37 : #include "swundo.hxx"
38 : #include "uitool.hxx"
39 : #include "unomid.h"
40 : #include "view.hxx"
41 : #include "viewopt.hxx"
42 : #include "wrtsh.hxx"
43 : #include "wview.hxx"
44 :
45 : #include <comphelper/anytostring.hxx>
46 : #include <comphelper/processfactory.hxx>
47 : #include <cppuhelper/exc_hlp.hxx>
48 : #include <editeng/acorrcfg.hxx>
49 : #include <editeng/svxacorr.hxx>
50 : #include <editeng/langitem.hxx>
51 : #include <editeng/splwrap.hxx>
52 : #include <editeng/brushitem.hxx>
53 : #include <editeng/unolingu.hxx>
54 : #include <editeng/editview.hxx>
55 : #include <i18nlangtag/mslangid.hxx>
56 : #include <i18nlangtag/languagetag.hxx>
57 : #include <linguistic/lngprops.hxx>
58 : #include <linguistic/misc.hxx>
59 : #include <osl/file.hxx>
60 : #include <rtl/string.hxx>
61 : #include <vcl/graphicfilter.hxx>
62 : #include <sfx2/dispatch.hxx>
63 : #include <sfx2/imagemgr.hxx>
64 : #include <sfx2/request.hxx>
65 : #include <sfx2/sfxdlg.hxx>
66 : #include <svl/itemset.hxx>
67 : #include <svl/languageoptions.hxx>
68 : #include <svl/stritem.hxx>
69 : #include <svtools/langtab.hxx>
70 : #include <svx/dlgutil.hxx>
71 : #include <unotools/lingucfg.hxx>
72 : #include <unotools/linguprops.hxx>
73 : #include <vcl/msgbox.hxx>
74 : #include <vcl/settings.hxx>
75 : #include <vcl/svapp.hxx>
76 : #include <sal/macros.h>
77 :
78 : #include <map>
79 :
80 : #include <com/sun/star/container/XIndexAccess.hpp>
81 : #include <com/sun/star/container/XNameAccess.hpp>
82 : #include <com/sun/star/document/XDocumentLanguages.hpp>
83 : #include <com/sun/star/frame/XModuleManager.hpp>
84 : #include <com/sun/star/frame/XStorable.hpp>
85 : #include <com/sun/star/i18n/ScriptType.hpp>
86 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
87 : #include <com/sun/star/lang/XServiceInfo.hpp>
88 : #include <com/sun/star/linguistic2/SingleProofreadingError.hpp>
89 : #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
90 : #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
91 : #include <com/sun/star/uno/Any.hxx>
92 : #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
93 : #include <com/sun/star/system/SystemShellExecute.hpp>
94 : #include <com/sun/star/frame/UICommandDescription.hpp>
95 :
96 :
97 : using namespace ::com::sun::star;
98 :
99 : extern void sw_CharDialog( SwWrtShell &rWrtSh, bool bUseDialog, sal_uInt16 nSlot,const SfxItemSet *pArgs, SfxRequest *pReq );
100 :
101 :
102 : /// @returns : the language for the selected text that is set for the
103 : /// specified attribute (script type).
104 : /// If there are more than one languages used LANGUAGE_DONTKNOW will be returned.
105 : /// @param nLangWhichId : one of
106 : /// RES_CHRATR_LANGUAGE, RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CTL_LANGUAGE,
107 : /// @returns: the language in use for the selected text.
108 : /// 'In use' means the language(s) matching the script type(s) of the
109 : /// selected text. Or in other words, the language a spell checker would use.
110 : /// If there is more than one language LANGUAGE_DONTKNOW will be returned.
111 : // check if nScriptType includes the script type associated to nLang
112 0 : static inline bool lcl_checkScriptType( sal_Int16 nScriptType, LanguageType nLang )
113 : {
114 0 : return 0 != (nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ));
115 : }
116 :
117 0 : void SwSpellPopup::fillLangPopupMenu(
118 : PopupMenu *pPopupMenu,
119 : sal_uInt16 nLangItemIdStart,
120 : uno::Sequence< OUString > aSeq,
121 : SwWrtShell* pWrtSh,
122 : std::map< sal_Int16, OUString > &rLangTable )
123 : {
124 0 : if (!pPopupMenu)
125 0 : return;
126 :
127 0 : SvtLanguageTable aLanguageTable;
128 :
129 : // set of languages to be displayed in the sub menus
130 0 : std::set< OUString > aLangItems;
131 :
132 0 : OUString aCurLang( aSeq[0] );
133 0 : sal_uInt16 nScriptType = static_cast< sal_Int16 >(aSeq[1].toInt32());
134 0 : OUString aKeyboardLang( aSeq[2] );
135 0 : OUString aGuessedTextLang( aSeq[3] );
136 :
137 0 : if (!aCurLang.isEmpty() &&
138 0 : LANGUAGE_DONTKNOW != aLanguageTable.GetType( aCurLang ))
139 0 : aLangItems.insert( aCurLang );
140 :
141 : //2--System
142 0 : const AllSettings& rAllSettings = Application::GetSettings();
143 0 : LanguageType rSystemLanguage = rAllSettings.GetLanguageTag().getLanguageType();
144 0 : if (rSystemLanguage != LANGUAGE_DONTKNOW)
145 : {
146 0 : if (lcl_checkScriptType( nScriptType, rSystemLanguage ))
147 0 : aLangItems.insert( aLanguageTable.GetString(rSystemLanguage) );
148 : }
149 :
150 : //3--UI
151 0 : LanguageType rUILanguage = rAllSettings.GetUILanguageTag().getLanguageType();
152 0 : if (rUILanguage != LANGUAGE_DONTKNOW)
153 : {
154 0 : if (lcl_checkScriptType(nScriptType, rUILanguage ))
155 0 : aLangItems.insert( aLanguageTable.GetString(rUILanguage) );
156 : }
157 :
158 : //4--guessed language
159 0 : if (!aGuessedTextLang.isEmpty())
160 : {
161 0 : if (lcl_checkScriptType(nScriptType, aLanguageTable.GetType(aGuessedTextLang)))
162 0 : aLangItems.insert( aGuessedTextLang );
163 : }
164 :
165 : //5--keyboard language
166 0 : if (!aKeyboardLang.isEmpty())
167 : {
168 0 : if (lcl_checkScriptType(nScriptType, aLanguageTable.GetType(aKeyboardLang)))
169 0 : aLangItems.insert( aKeyboardLang );
170 : }
171 :
172 : //6--all languages used in current document
173 0 : uno::Reference< com::sun::star::frame::XModel > xModel;
174 0 : uno::Reference< com::sun::star::frame::XController > xController( pWrtSh->GetView().GetViewFrame()->GetFrame().GetFrameInterface()->getController(), uno::UNO_QUERY );
175 0 : if ( xController.is() )
176 0 : xModel = xController->getModel();
177 0 : uno::Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, uno::UNO_QUERY );
178 : /*the description of nScriptType flags
179 : LATIN : 0x0001
180 : ASIAN : 0x0002
181 : COMPLEX: 0x0004
182 : */
183 0 : const sal_Int16 nMaxCount = 7;
184 0 : if (xDocumentLanguages.is())
185 : {
186 0 : uno::Sequence< lang::Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ) );
187 0 : if (rLocales.getLength() > 0)
188 : {
189 0 : for (sal_uInt16 i = 0; i < rLocales.getLength(); ++i)
190 : {
191 0 : if (aLangItems.size() == (size_t)nMaxCount)
192 0 : break;
193 0 : const lang::Locale& rLocale = rLocales[i];
194 0 : if (lcl_checkScriptType( nScriptType, aLanguageTable.GetType( rLocale.Language )))
195 0 : aLangItems.insert( rLocale.Language );
196 : }
197 0 : }
198 : }
199 :
200 :
201 0 : sal_uInt16 nItemId = nLangItemIdStart;
202 0 : std::set< OUString >::const_iterator it;
203 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
204 : {
205 0 : OUString aEntryTxt( *it );
206 0 : if (aEntryTxt != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) )&&
207 0 : aEntryTxt != "*" && // multiple languages in current selection
208 0 : !aEntryTxt.isEmpty()) // 'no language found' from language guessing
209 : {
210 : OSL_ENSURE( nLangItemIdStart <= nItemId && nItemId <= nLangItemIdStart + MN_MAX_NUM_LANG,
211 : "nItemId outside of expected range!" );
212 0 : pPopupMenu->InsertItem( nItemId, aEntryTxt, MIB_RADIOCHECK );
213 0 : if (aEntryTxt == aCurLang)
214 : {
215 : //make a check mark for the current language
216 0 : pPopupMenu->CheckItem( nItemId, sal_True );
217 : }
218 0 : rLangTable[ nItemId ] = aEntryTxt;
219 0 : ++nItemId;
220 : }
221 0 : }
222 :
223 0 : pPopupMenu->InsertItem( nLangItemIdStart + MN_NONE_OFFSET, String(SW_RES( STR_LANGSTATUS_NONE )), MIB_RADIOCHECK );
224 0 : if ( aLanguageTable.GetString( LANGUAGE_NONE ) == aCurLang )
225 0 : pPopupMenu->CheckItem( nLangItemIdStart + MN_NONE_OFFSET, sal_True );
226 :
227 0 : pPopupMenu->InsertItem( nLangItemIdStart + MN_RESET_OFFSET, String(SW_RES( STR_RESET_TO_DEFAULT_LANGUAGE )), 0 );
228 0 : pPopupMenu->InsertItem( nLangItemIdStart + MN_MORE_OFFSET, String(SW_RES( STR_LANGSTATUS_MORE )), 0 );
229 : }
230 :
231 :
232 0 : static Image lcl_GetImageFromPngUrl( const OUString &rFileUrl )
233 : {
234 0 : Image aRes;
235 0 : OUString aTmp;
236 0 : osl::FileBase::getSystemPathFromFileURL( rFileUrl, aTmp );
237 0 : Graphic aGraphic;
238 0 : const String aFilterName( IMP_PNG );
239 0 : if( GRFILTER_OK == GraphicFilter::LoadGraphic( aTmp, aFilterName, aGraphic ) )
240 : {
241 0 : aRes = Image( aGraphic.GetBitmapEx() );
242 : }
243 0 : return aRes;
244 : }
245 :
246 :
247 0 : OUString RetrieveLabelFromCommand( const OUString& aCmdURL )
248 : {
249 0 : OUString aLabel;
250 0 : if ( !aCmdURL.isEmpty() )
251 : {
252 : try
253 : {
254 : uno::Reference< container::XNameAccess > const xNameAccess(
255 : frame::UICommandDescription::create(
256 : ::comphelper::getProcessComponentContext() ),
257 0 : uno::UNO_QUERY_THROW );
258 0 : uno::Reference< container::XNameAccess > xUICommandLabels;
259 0 : uno::Any a = xNameAccess->getByName( "com.sun.star.text.TextDocument" );
260 0 : uno::Reference< container::XNameAccess > xUICommands;
261 0 : a >>= xUICommandLabels;
262 0 : OUString aStr;
263 0 : uno::Sequence< beans::PropertyValue > aPropSeq;
264 0 : a = xUICommandLabels->getByName( aCmdURL );
265 0 : if ( a >>= aPropSeq )
266 : {
267 0 : for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
268 : {
269 0 : if ( aPropSeq[i].Name == "Name" )
270 : {
271 0 : aPropSeq[i].Value >>= aStr;
272 0 : break;
273 : }
274 : }
275 : }
276 0 : aLabel = aStr;
277 : }
278 0 : catch (const uno::Exception&)
279 : {
280 : }
281 : }
282 :
283 0 : return aLabel;
284 : }
285 :
286 :
287 0 : SwSpellPopup::SwSpellPopup(
288 : SwWrtShell* pWrtSh,
289 : const uno::Reference< linguistic2::XSpellAlternatives > &xAlt,
290 : const String &rParaText
291 : ) : PopupMenu( SW_RES(MN_SPELL_POPUP) )
292 : , pSh( pWrtSh )
293 : , xSpellAlt(xAlt)
294 0 : , bGrammarResults(false)
295 : {
296 : OSL_ENSURE(xSpellAlt.is(), "no spelling alternatives available");
297 :
298 0 : SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
299 0 : bool bUseImagesInMenus = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
300 :
301 0 : nCheckedLanguage = LANGUAGE_NONE;
302 0 : if (xSpellAlt.is())
303 : {
304 0 : nCheckedLanguage = LanguageTag( xSpellAlt->getLocale() ).getLanguageType();
305 0 : aSuggestions = xSpellAlt->getAlternatives();
306 : }
307 0 : sal_Int16 nStringCount = static_cast< sal_Int16 >( aSuggestions.getLength() );
308 :
309 0 : SvtLinguConfig aCfg;
310 :
311 0 : PopupMenu *pMenu = GetPopupMenu(MN_AUTOCORR);
312 0 : pMenu->SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
313 0 : sal_Bool bEnable = sal_False;
314 0 : if( nStringCount )
315 : {
316 0 : Image aImage;
317 0 : OUString aSuggestionImageUrl;
318 :
319 0 : if (bUseImagesInMenus)
320 : {
321 0 : uno::Reference< container::XNamed > xNamed( xSpellAlt, uno::UNO_QUERY );
322 0 : if (xNamed.is())
323 : {
324 0 : aSuggestionImageUrl = aCfg.GetSpellAndGrammarContextSuggestionImage( xNamed->getName() );
325 0 : aImage = Image( lcl_GetImageFromPngUrl( aSuggestionImageUrl ) );
326 0 : }
327 : }
328 :
329 0 : InsertSeparator(OString(), 0);
330 0 : bEnable = sal_True;
331 0 : sal_uInt16 nAutoCorrItemId = MN_AUTOCORR_START;
332 0 : sal_uInt16 nItemId = MN_SUGGESTION_START;
333 0 : for (sal_uInt16 i = 0; i < nStringCount; ++i)
334 : {
335 0 : const OUString aEntry = aSuggestions[ i ];
336 0 : InsertItem(nItemId, aEntry, 0, OString(), i);
337 0 : SetHelpId( nItemId, HID_LINGU_REPLACE);
338 0 : if (!aSuggestionImageUrl.isEmpty())
339 0 : SetItemImage( nItemId, aImage );
340 :
341 0 : pMenu->InsertItem( nAutoCorrItemId, aEntry );
342 0 : pMenu->SetHelpId( nAutoCorrItemId, HID_LINGU_AUTOCORR);
343 :
344 0 : ++nAutoCorrItemId;
345 0 : ++nItemId;
346 0 : }
347 : }
348 :
349 0 : OUString aIgnoreSelection( String( SW_RES( STR_IGNORE_SELECTION ) ) );
350 0 : OUString aSpellingAndGrammar = RetrieveLabelFromCommand( ".uno:SpellingAndGrammarDialog" );
351 0 : SetItemText( MN_SPELLING_DLG, aSpellingAndGrammar );
352 0 : sal_uInt16 nItemPos = GetItemPos( MN_IGNORE_WORD );
353 0 : InsertItem(MN_IGNORE_SELECTION, aIgnoreSelection, 0, OString(), nItemPos);
354 0 : SetHelpId( MN_IGNORE_SELECTION, HID_LINGU_IGNORE_SELECTION);
355 :
356 0 : EnableItem( MN_AUTOCORR, bEnable );
357 :
358 0 : uno::Reference< linguistic2::XLanguageGuessing > xLG = SW_MOD()->GetLanguageGuesser();
359 0 : nGuessLangWord = LANGUAGE_NONE;
360 0 : nGuessLangPara = LANGUAGE_NONE;
361 0 : if (xSpellAlt.is() && xLG.is())
362 : {
363 0 : nGuessLangWord = EditView::CheckLanguage( xSpellAlt->getWord(), ::GetSpellChecker(), xLG, false );
364 0 : nGuessLangPara = EditView::CheckLanguage( rParaText, ::GetSpellChecker(), xLG, true );
365 : }
366 0 : if (nGuessLangWord != LANGUAGE_NONE || nGuessLangPara != LANGUAGE_NONE)
367 : {
368 : // make sure LANGUAGE_NONE gets not used as menu entry
369 0 : if (nGuessLangWord == LANGUAGE_NONE)
370 0 : nGuessLangWord = nGuessLangPara;
371 0 : if (nGuessLangPara == LANGUAGE_NONE)
372 0 : nGuessLangPara = nGuessLangWord;
373 : }
374 :
375 0 : pMenu = GetPopupMenu(MN_ADD_TO_DIC);
376 0 : pMenu->SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS); //! necessary to retrieve the correct dictionary name in 'Execute' below
377 0 : uno::Reference< linguistic2::XSearchableDictionaryList > xDicList( SvxGetDictionaryList() );
378 0 : sal_uInt16 nItemId = MN_DICTIONARIES_START;
379 0 : if (xDicList.is())
380 : {
381 : // add the default positive dictionary to dic-list (if not already done).
382 : // This is to ensure that there is at least one dictionary to which
383 : // words could be added.
384 0 : uno::Reference< linguistic2::XDictionary > xDic( SvxGetOrCreatePosDic( xDicList ) );
385 0 : if (xDic.is())
386 0 : xDic->setActive( sal_True );
387 :
388 0 : aDics = xDicList->getDictionaries();
389 0 : const uno::Reference< linguistic2::XDictionary > *pDic = aDics.getConstArray();
390 0 : sal_uInt16 nDicCount = static_cast< sal_uInt16 >(aDics.getLength());
391 :
392 0 : for( sal_uInt16 i = 0; i < nDicCount; i++ )
393 : {
394 0 : uno::Reference< linguistic2::XDictionary > xDicTmp( pDic[i], uno::UNO_QUERY );
395 0 : if (!xDicTmp.is() || SvxGetIgnoreAllList() == xDicTmp)
396 0 : continue;
397 :
398 0 : uno::Reference< frame::XStorable > xStor( xDicTmp, uno::UNO_QUERY );
399 0 : LanguageType nActLanguage = LanguageTag( xDicTmp->getLocale() ).getLanguageType();
400 0 : if( xDicTmp->isActive()
401 0 : && xDicTmp->getDictionaryType() != linguistic2::DictionaryType_NEGATIVE
402 0 : && (nCheckedLanguage == nActLanguage || LANGUAGE_NONE == nActLanguage )
403 0 : && (!xStor.is() || !xStor->isReadonly()) )
404 : {
405 : // the extra 1 is because of the (possible) external
406 : // linguistic entry above
407 0 : pMenu->InsertItem( nItemId, xDicTmp->getName() );
408 0 : aDicNameSingle = xDicTmp->getName();
409 :
410 0 : if (bUseImagesInMenus)
411 : {
412 0 : uno::Reference< lang::XServiceInfo > xSvcInfo( xDicTmp, uno::UNO_QUERY );
413 0 : if (xSvcInfo.is())
414 : {
415 : OUString aDictionaryImageUrl( aCfg.GetSpellAndGrammarContextDictionaryImage(
416 0 : xSvcInfo->getImplementationName() ) );
417 0 : if (!aDictionaryImageUrl.isEmpty())
418 : {
419 0 : Image aImage( lcl_GetImageFromPngUrl( aDictionaryImageUrl ) );
420 0 : pMenu->SetItemImage( nItemId, aImage );
421 0 : }
422 0 : }
423 : }
424 :
425 0 : ++nItemId;
426 : }
427 0 : }
428 : }
429 0 : EnableItem( MN_ADD_TO_DIC, ((nItemId - MN_DICTIONARIES_START) > 1)?sal_True:sal_False );
430 0 : EnableItem( MN_ADD_TO_DIC_SINGLE, ((nItemId - MN_DICTIONARIES_START) == 1)?sal_True:sal_False );
431 :
432 : //ADD NEW LANGUAGE MENU ITEM
433 : ///////////////////////////////////////////////////////////////////////////
434 0 : String aScriptTypesInUse( OUString::number( pWrtSh->GetScriptType() ) );
435 0 : SvtLanguageTable aLanguageTable;
436 :
437 : // get keyboard language
438 0 : String aKeyboardLang;
439 0 : SwEditWin& rEditWin = pWrtSh->GetView().GetEditWin();
440 0 : LanguageType nLang = rEditWin.GetInputLanguage();
441 0 : if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_SYSTEM)
442 0 : aKeyboardLang = aLanguageTable.GetString( nLang );
443 :
444 : // get the language that is in use
445 0 : String aCurrentLang = OUString("*");
446 0 : nLang = SwLangHelper::GetCurrentLanguage( *pWrtSh );
447 0 : if (nLang != LANGUAGE_DONTKNOW)
448 0 : aCurrentLang = aLanguageTable.GetString( nLang );
449 :
450 : // build sequence for status value
451 0 : uno::Sequence< OUString > aSeq( 4 );
452 0 : aSeq[0] = aCurrentLang;
453 0 : aSeq[1] = aScriptTypesInUse;
454 0 : aSeq[2] = aKeyboardLang;
455 0 : aSeq[3] = aLanguageTable.GetString(nGuessLangWord);
456 :
457 0 : pMenu = GetPopupMenu(MN_SET_LANGUAGE_SELECTION);
458 0 : fillLangPopupMenu( pMenu, MN_SET_LANGUAGE_SELECTION_START, aSeq, pWrtSh, aLangTable_Text );
459 0 : EnableItem( MN_SET_LANGUAGE_SELECTION, true );
460 :
461 0 : pMenu = GetPopupMenu(MN_SET_LANGUAGE_PARAGRAPH);
462 0 : fillLangPopupMenu( pMenu, MN_SET_LANGUAGE_PARAGRAPH_START, aSeq, pWrtSh, aLangTable_Paragraph );
463 0 : EnableItem( MN_SET_LANGUAGE_PARAGRAPH, true );
464 :
465 0 : if (bUseImagesInMenus)
466 : {
467 0 : uno::Reference< frame::XFrame > xFrame = pWrtSh->GetView().GetViewFrame()->GetFrame().GetFrameInterface();
468 0 : Image rImg = ::GetImage( xFrame, ".uno:SpellingAndGrammarDialog", sal_False );
469 0 : SetItemImage( MN_SPELLING_DLG, rImg );
470 : }
471 :
472 : //////////////////////////////////////////////////////////////////////////////////
473 :
474 0 : RemoveDisabledEntries( sal_True, sal_True );
475 0 : }
476 :
477 :
478 :
479 0 : SwSpellPopup::SwSpellPopup(
480 : SwWrtShell *pWrtSh,
481 : const linguistic2::ProofreadingResult &rResult,
482 : sal_Int32 nErrorInResult,
483 : const uno::Sequence< OUString > &rSuggestions,
484 : const String &rParaText ) :
485 : PopupMenu( SW_RES(MN_SPELL_POPUP) ),
486 : pSh( pWrtSh ),
487 : xGrammarResult( rResult ),
488 : aSuggestions( rSuggestions ),
489 : sExplanationLink( ),
490 : bGrammarResults( true ),
491 0 : aInfo16( SW_RES(IMG_INFO_16) )
492 : {
493 0 : nCheckedLanguage = LanguageTag( rResult.aLocale ).getLanguageType();
494 0 : nGrammarError = nErrorInResult;
495 0 : bool bUseImagesInMenus = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
496 :
497 0 : sal_uInt16 nPos = 0;
498 0 : OUString aMessageText( rResult.aErrors[ nErrorInResult ].aShortComment );
499 0 : InsertSeparator(OString(), nPos++);
500 0 : InsertItem(MN_SHORT_COMMENT, aMessageText, MIB_NOSELECT, OString(), nPos++);
501 0 : if (bUseImagesInMenus)
502 0 : SetItemImage( MN_SHORT_COMMENT, aInfo16 );
503 :
504 : // Add an item to show detailled infos if the FullCommentURL property is defined
505 0 : beans::PropertyValues aProperties = rResult.aErrors[ nErrorInResult ].aProperties;
506 : {
507 0 : sal_Int32 i = 0;
508 0 : while ( sExplanationLink.isEmpty() && i < aProperties.getLength() )
509 : {
510 0 : if ( aProperties[i].Name == "FullCommentURL" )
511 : {
512 0 : uno::Any aValue = aProperties[i].Value;
513 0 : aValue >>= sExplanationLink;
514 : }
515 0 : ++i;
516 : }
517 : }
518 :
519 0 : if ( !sExplanationLink.isEmpty( ) )
520 : {
521 0 : InsertItem(MN_EXPLANATION_LINK, SW_RESSTR(STR_EXPLANATION_LINK), MIB_TEXT | MIB_HELP, OString(), nPos++);
522 : }
523 :
524 0 : SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
525 :
526 0 : InsertSeparator(OString(), nPos++);
527 0 : sal_Int32 nStringCount = aSuggestions.getLength();
528 0 : if ( nStringCount ) // suggestions available...
529 : {
530 0 : Image aImage;
531 0 : OUString aSuggestionImageUrl;
532 :
533 0 : if (bUseImagesInMenus)
534 : {
535 0 : uno::Reference< lang::XServiceInfo > xInfo( rResult.xProofreader, uno::UNO_QUERY );
536 0 : if (xInfo.is())
537 : {
538 0 : aSuggestionImageUrl = SvtLinguConfig().GetSpellAndGrammarContextSuggestionImage( xInfo->getImplementationName() );
539 0 : aImage = Image( lcl_GetImageFromPngUrl( aSuggestionImageUrl ) );
540 0 : }
541 : }
542 :
543 0 : sal_uInt16 nItemId = MN_SUGGESTION_START;
544 0 : for (sal_uInt16 i = 0; i < nStringCount; ++i)
545 : {
546 0 : const OUString aEntry = aSuggestions[ i ];
547 0 : InsertItem(nItemId, aEntry, 0, OString(), nPos++);
548 0 : SetHelpId( nItemId, HID_LINGU_REPLACE );
549 0 : if (!aSuggestionImageUrl.isEmpty())
550 0 : SetItemImage( nItemId, aImage );
551 :
552 0 : ++nItemId;
553 0 : }
554 0 : InsertSeparator(OString(), nPos++);
555 : }
556 :
557 0 : OUString aIgnoreSelection( String( SW_RES( STR_IGNORE_SELECTION ) ) );
558 0 : OUString aSpellingAndGrammar = RetrieveLabelFromCommand( ".uno:SpellingAndGrammarDialog" );
559 0 : SetItemText( MN_SPELLING_DLG, aSpellingAndGrammar );
560 0 : sal_uInt16 nItemPos = GetItemPos( MN_IGNORE_WORD );
561 0 : InsertItem( MN_IGNORE_SELECTION, aIgnoreSelection, 0, OString(), nItemPos );
562 0 : SetHelpId( MN_IGNORE_SELECTION, HID_LINGU_IGNORE_SELECTION);
563 :
564 0 : EnableItem( MN_AUTOCORR, false );
565 :
566 0 : uno::Reference< linguistic2::XLanguageGuessing > xLG = SW_MOD()->GetLanguageGuesser();
567 0 : nGuessLangWord = LANGUAGE_NONE;
568 0 : nGuessLangPara = LANGUAGE_NONE;
569 0 : if (xLG.is())
570 : {
571 0 : nGuessLangPara = EditView::CheckLanguage( rParaText, ::GetSpellChecker(), xLG, true );
572 : }
573 0 : if (nGuessLangWord != LANGUAGE_NONE || nGuessLangPara != LANGUAGE_NONE)
574 : {
575 : // make sure LANGUAGE_NONE gets not used as menu entry
576 0 : if (nGuessLangWord == LANGUAGE_NONE)
577 0 : nGuessLangWord = nGuessLangPara;
578 0 : if (nGuessLangPara == LANGUAGE_NONE)
579 0 : nGuessLangPara = nGuessLangWord;
580 : }
581 :
582 0 : EnableItem( MN_IGNORE_WORD, false );
583 0 : EnableItem( MN_ADD_TO_DIC, false );
584 0 : EnableItem( MN_ADD_TO_DIC_SINGLE, false );
585 :
586 : //ADD NEW LANGUAGE MENU ITEM
587 : ///////////////////////////////////////////////////////////////////////////
588 0 : String aScriptTypesInUse( OUString::number( pWrtSh->GetScriptType() ) );
589 0 : SvtLanguageTable aLanguageTable;
590 :
591 : // get keyboard language
592 0 : String aKeyboardLang;
593 0 : SwEditWin& rEditWin = pWrtSh->GetView().GetEditWin();
594 0 : LanguageType nLang = rEditWin.GetInputLanguage();
595 0 : if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_SYSTEM)
596 0 : aKeyboardLang = aLanguageTable.GetString( nLang );
597 :
598 : // get the language that is in use
599 0 : String aCurrentLang = OUString("*");
600 0 : nLang = SwLangHelper::GetCurrentLanguage( *pWrtSh );
601 0 : if (nLang != LANGUAGE_DONTKNOW)
602 0 : aCurrentLang = aLanguageTable.GetString( nLang );
603 :
604 : // build sequence for status value
605 0 : uno::Sequence< OUString > aSeq( 4 );
606 0 : aSeq[0] = aCurrentLang;
607 0 : aSeq[1] = aScriptTypesInUse;
608 0 : aSeq[2] = aKeyboardLang;
609 0 : aSeq[3] = aLanguageTable.GetString(nGuessLangWord);
610 :
611 0 : PopupMenu *pMenu = GetPopupMenu(MN_SET_LANGUAGE_SELECTION);
612 0 : fillLangPopupMenu( pMenu, MN_SET_LANGUAGE_SELECTION_START, aSeq, pWrtSh, aLangTable_Text );
613 0 : EnableItem( MN_SET_LANGUAGE_SELECTION, true );
614 :
615 0 : pMenu = GetPopupMenu(MN_SET_LANGUAGE_PARAGRAPH);
616 0 : fillLangPopupMenu( pMenu, MN_SET_LANGUAGE_PARAGRAPH_START, aSeq, pWrtSh, aLangTable_Paragraph );
617 0 : EnableItem( MN_SET_LANGUAGE_PARAGRAPH, true );
618 :
619 0 : if (bUseImagesInMenus)
620 : {
621 0 : uno::Reference< frame::XFrame > xFrame = pWrtSh->GetView().GetViewFrame()->GetFrame().GetFrameInterface();
622 0 : Image rImg = ::GetImage( xFrame, ".uno:SpellingAndGrammarDialog", sal_False );
623 0 : SetItemImage( MN_SPELLING_DLG, rImg );
624 : }
625 :
626 : //////////////////////////////////////////////////////////////////////////////////
627 :
628 0 : RemoveDisabledEntries( sal_True, sal_True );
629 0 : }
630 :
631 :
632 0 : sal_uInt16 SwSpellPopup::Execute( const Rectangle& rWordPos, Window* pWin )
633 : {
634 0 : sal_uInt16 nRet = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos));
635 0 : Execute( nRet );
636 0 : return nRet;
637 : }
638 :
639 0 : void SwSpellPopup::Execute( sal_uInt16 nId )
640 : {
641 0 : if (nId == USHRT_MAX)
642 0 : return;
643 :
644 0 : if (/*bGrammarResults && */nId == MN_SHORT_COMMENT)
645 0 : return; // nothing to do since it is the error message (short comment)
646 :
647 0 : if ((MN_SUGGESTION_START <= nId && nId <= MN_SUGGESTION_END) ||
648 0 : (MN_AUTOCORR_START <= nId && nId <= MN_AUTOCORR_END))
649 : {
650 0 : sal_Int32 nAltIdx = (MN_SUGGESTION_START <= nId && nId <= MN_SUGGESTION_END) ?
651 0 : nId - MN_SUGGESTION_START : nId - MN_AUTOCORR_START;
652 : OSL_ENSURE( 0 <= nAltIdx && nAltIdx < aSuggestions.getLength(), "index out of range" );
653 0 : if (0 <= nAltIdx && nAltIdx < aSuggestions.getLength() && (bGrammarResults || xSpellAlt.is()))
654 : {
655 0 : sal_Bool bOldIns = pSh->IsInsMode();
656 0 : pSh->SetInsMode( sal_True );
657 :
658 0 : String aTmp( aSuggestions[ nAltIdx ] );
659 0 : String aOrig( bGrammarResults ? OUString() : xSpellAlt->getWord() );
660 :
661 : // if orginal word has a trailing . (likely the end of a sentence)
662 : // and the replacement text hasn't, then add it to the replacement
663 0 : if (aTmp.Len() && aOrig.Len() &&
664 0 : '.' == aOrig.GetChar( aOrig.Len() - 1) && /* !IsAlphaNumeric ??*/
665 0 : '.' != aTmp.GetChar( aTmp.Len() - 1))
666 : {
667 0 : aTmp += '.';
668 : }
669 :
670 : // #111827#
671 0 : SwRewriter aRewriter;
672 :
673 0 : aRewriter.AddRule(UndoArg1, pSh->GetCrsrDescr());
674 0 : aRewriter.AddRule(UndoArg2, String(SW_RES(STR_YIELDS)));
675 :
676 0 : String aTmpStr( SW_RES(STR_START_QUOTE) );
677 0 : aTmpStr += aTmp;
678 0 : aTmpStr += String(SW_RES(STR_END_QUOTE));
679 0 : aRewriter.AddRule(UndoArg3, aTmpStr);
680 :
681 0 : pSh->StartUndo(UNDO_UI_REPLACE, &aRewriter);
682 0 : pSh->StartAction();
683 0 : pSh->DelLeft();
684 :
685 0 : pSh->Insert( aTmp );
686 :
687 : /* #102505# EndAction/EndUndo moved down since insertion
688 : of temporary auto correction is now undoable two and
689 : must reside in the same undo group.*/
690 :
691 : // record only if it's NOT already present in autocorrection
692 0 : SvxAutoCorrect* pACorr = SvxAutoCorrCfg::Get().GetAutoCorrect();
693 :
694 0 : String aOrigWord( bGrammarResults ? OUString() : xSpellAlt->getWord() ) ;
695 0 : String aNewWord( aSuggestions[ nAltIdx ] );
696 0 : SvxPrepareAutoCorrect( aOrigWord, aNewWord );
697 :
698 0 : if (MN_AUTOCORR_START <= nId && nId <= MN_AUTOCORR_END)
699 0 : pACorr->PutText( aOrigWord, aNewWord, nCheckedLanguage );
700 :
701 : /* #102505# EndAction/EndUndo moved down since insertion
702 : of temporary auto correction is now undoable two and
703 : must reside in the same undo group.*/
704 0 : pSh->EndAction();
705 0 : pSh->EndUndo();
706 :
707 0 : pSh->SetInsMode( bOldIns );
708 0 : }
709 : }
710 0 : else if (nId == MN_SPELLING_DLG)
711 : {
712 0 : if (bGrammarResults)
713 : {
714 0 : SvtLinguConfig().SetProperty( UPN_IS_GRAMMAR_INTERACTIVE, uno::makeAny( sal_True ));
715 : }
716 0 : pSh->Left(CRSR_SKIP_CHARS, sal_False, 1, sal_False );
717 : {
718 0 : uno::Reference<linguistic2::XSearchableDictionaryList> xDictionaryList( SvxGetDictionaryList() );
719 0 : SvxDicListChgClamp aClamp( xDictionaryList );
720 0 : pSh->GetView().GetViewFrame()->GetDispatcher()->
721 0 : Execute( FN_SPELL_GRAMMAR_DIALOG, SFX_CALLMODE_ASYNCHRON );
722 : }
723 : }
724 0 : else if (nId == MN_IGNORE_SELECTION)
725 : {
726 0 : SwPaM *pPaM = pSh->GetCrsr();
727 0 : if (pPaM) {
728 0 : if (bGrammarResults) {
729 : try
730 : {
731 0 : xGrammarResult.xProofreader->ignoreRule(
732 0 : xGrammarResult.aErrors[ nGrammarError ].aRuleIdentifier,
733 0 : xGrammarResult.aLocale );
734 : }
735 0 : catch( const uno::Exception& )
736 : {
737 : }
738 : }
739 0 : pSh->IgnoreGrammarErrorAt( *pPaM );
740 : }
741 : }
742 0 : else if (nId == MN_IGNORE_WORD)
743 : {
744 0 : uno::Reference< linguistic2::XDictionary > xDictionary( SvxGetIgnoreAllList(), uno::UNO_QUERY );
745 : linguistic::AddEntryToDic( xDictionary,
746 0 : xSpellAlt->getWord(), sal_False, aEmptyStr, LANGUAGE_NONE );
747 : }
748 0 : else if ((MN_DICTIONARIES_START <= nId && nId <= MN_DICTIONARIES_END) || nId == MN_ADD_TO_DIC_SINGLE)
749 : {
750 0 : OUString aWord( xSpellAlt->getWord() );
751 0 : String aDicName;
752 :
753 0 : if (MN_DICTIONARIES_START <= nId && nId <= MN_DICTIONARIES_END)
754 : {
755 0 : PopupMenu *pMenu = GetPopupMenu(MN_ADD_TO_DIC);
756 0 : aDicName = pMenu->GetItemText(nId);
757 : }
758 : else
759 0 : aDicName = aDicNameSingle;
760 :
761 0 : uno::Reference< linguistic2::XDictionary > xDic;
762 0 : uno::Reference< linguistic2::XSearchableDictionaryList > xDicList( SvxGetDictionaryList() );
763 0 : if (xDicList.is())
764 0 : xDic = xDicList->getDictionaryByName( aDicName );
765 :
766 0 : if (xDic.is())
767 : {
768 0 : sal_Int16 nAddRes = linguistic::AddEntryToDic( xDic, aWord, sal_False, aEmptyStr, LANGUAGE_NONE );
769 : // save modified user-dictionary if it is persistent
770 0 : uno::Reference< frame::XStorable > xSavDic( xDic, uno::UNO_QUERY );
771 0 : if (xSavDic.is())
772 0 : xSavDic->store();
773 :
774 0 : if (DIC_ERR_NONE != nAddRes
775 0 : && !xDic->getEntry( aWord ).is())
776 : {
777 : SvxDicError(
778 0 : &pSh->GetView().GetViewFrame()->GetWindow(),
779 0 : nAddRes );
780 0 : }
781 0 : }
782 : }
783 0 : else if ( nId == MN_EXPLANATION_LINK && !sExplanationLink.isEmpty() )
784 : {
785 : try
786 : {
787 : uno::Reference< com::sun::star::system::XSystemShellExecute > xSystemShellExecute(
788 0 : com::sun::star::system::SystemShellExecute::create( ::comphelper::getProcessComponentContext() ) );
789 0 : xSystemShellExecute->execute( sExplanationLink, OUString(),
790 0 : com::sun::star::system::SystemShellExecuteFlags::URIS_ONLY );
791 : }
792 0 : catch (const uno::Exception&)
793 : {
794 0 : uno::Any exc( ::cppu::getCaughtException() );
795 0 : OUString msg( ::comphelper::anyToString( exc ) );
796 0 : const SolarMutexGuard guard;
797 0 : ErrorBox aErrorBox( NULL, WB_OK, msg );
798 0 : aErrorBox.SetText( "Explanations" );
799 0 : aErrorBox.Execute();
800 : }
801 : }
802 : else
803 : {
804 : // Set language for selection or for paragraph...
805 :
806 0 : SfxItemSet aCoreSet( pSh->GetView().GetPool(),
807 : RES_CHRATR_LANGUAGE, RES_CHRATR_LANGUAGE,
808 : RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CJK_LANGUAGE,
809 : RES_CHRATR_CTL_LANGUAGE, RES_CHRATR_CTL_LANGUAGE,
810 0 : 0 );
811 0 : String aNewLangTxt;
812 :
813 0 : if (MN_SET_LANGUAGE_SELECTION_START <= nId && nId <= MN_SET_LANGUAGE_SELECTION_END)
814 : {
815 : //Set language for current selection
816 0 : aNewLangTxt = aLangTable_Text[nId];
817 0 : SwLangHelper::SetLanguage( *pSh, aNewLangTxt, true, aCoreSet );
818 : }
819 0 : else if (nId == MN_SET_SELECTION_NONE)
820 : {
821 : //Set Language_None for current selection
822 0 : SwLangHelper::SetLanguage_None( *pSh, true, aCoreSet );
823 : }
824 0 : else if (nId == MN_SET_SELECTION_RESET)
825 : {
826 : //reset languages for current selection
827 0 : SwLangHelper::ResetLanguages( *pSh, true );
828 : }
829 0 : else if (nId == MN_SET_SELECTION_MORE)
830 : {
831 : //Open Format/Character Dialog
832 0 : sw_CharDialog( *pSh, true, nId, 0, 0 );
833 : }
834 0 : else if (MN_SET_LANGUAGE_PARAGRAPH_START <= nId && nId <= MN_SET_LANGUAGE_PARAGRAPH_END)
835 : {
836 : //Set language for current paragraph
837 0 : aNewLangTxt = aLangTable_Paragraph[nId];
838 0 : pSh->Push(); // save cursor
839 0 : SwLangHelper::SelectCurrentPara( *pSh );
840 0 : SwLangHelper::SetLanguage( *pSh, aNewLangTxt, true, aCoreSet );
841 0 : pSh->Pop( sal_False ); // restore cursor
842 : }
843 0 : else if (nId == MN_SET_PARA_NONE)
844 : {
845 : //Set Language_None for current paragraph
846 0 : pSh->Push(); // save cursor
847 0 : SwLangHelper::SelectCurrentPara( *pSh );
848 0 : SwLangHelper::SetLanguage_None( *pSh, true, aCoreSet );
849 0 : pSh->Pop( sal_False ); // restore cursor
850 : }
851 0 : else if (nId == MN_SET_PARA_RESET)
852 : {
853 : //reset languages for current paragraph
854 0 : pSh->Push(); // save cursor
855 0 : SwLangHelper::SelectCurrentPara( *pSh );
856 0 : SwLangHelper::ResetLanguages( *pSh, true );
857 0 : pSh->Pop( sal_False ); // restore cursor
858 : }
859 0 : else if (nId == MN_SET_PARA_MORE)
860 : {
861 0 : pSh->Push(); // save cursor
862 0 : SwLangHelper::SelectCurrentPara( *pSh );
863 : //Open Format/Character Dialog
864 0 : sw_CharDialog( *pSh, true, nId, 0, 0 );
865 0 : pSh->Pop( sal_False ); // restore cursor
866 0 : }
867 : }
868 :
869 0 : pSh->EnterStdMode();
870 99 : }
871 :
872 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|