xref: /trunk/main/sw/source/ui/shells/langhelper.cxx (revision 0cf2fd93dccc69f9c6d59dfb2dbcea73e82909ce)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_sw.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <string.h>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vcl/window.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <wrtsh.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sfx2/bindings.hxx>
32cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
33cdf0e10cSrcweir #include <sfx2/request.hxx>
34cdf0e10cSrcweir #include <editeng/eeitem.hxx>
35cdf0e10cSrcweir #include <editeng/editeng.hxx>
36cdf0e10cSrcweir #include <editeng/editdata.hxx>
37cdf0e10cSrcweir #include <editeng/outliner.hxx>
38cdf0e10cSrcweir #include <editeng/editview.hxx>
39cdf0e10cSrcweir #include <editeng/scripttypeitem.hxx>
40cdf0e10cSrcweir #include <editeng/langitem.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <svl/languageoptions.hxx>
43cdf0e10cSrcweir #include <svtools/langtab.hxx>
44cdf0e10cSrcweir #include <svl/slstitm.hxx>
45cdf0e10cSrcweir #include <svl/svstdarr.hxx>
46cdf0e10cSrcweir #include <svl/stritem.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <ndtxt.hxx>
49cdf0e10cSrcweir #include <pam.hxx>
50cdf0e10cSrcweir #include <view.hxx>
51cdf0e10cSrcweir #include <viewopt.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include "swabstdlg.hxx"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include <vcl/msgbox.hxx>
56cdf0e10cSrcweir 
57cdf0e10cSrcweir #include <langhelper.hxx>
58cdf0e10cSrcweir 
59cdf0e10cSrcweir using namespace ::com::sun::star;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir namespace SwLangHelper
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 
GetLanguageStatus(OutlinerView * pOLV,SfxItemSet & rSet)64cdf0e10cSrcweir     sal_uInt16 GetLanguageStatus( OutlinerView* pOLV, SfxItemSet& rSet )
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         ESelection aSelection = pOLV->GetSelection();
67cdf0e10cSrcweir         EditView& rEditView=pOLV->GetEditView();
68cdf0e10cSrcweir         EditEngine* pEditEngine=rEditView.GetEditEngine();
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         // the value of used script types
71cdf0e10cSrcweir         const sal_uInt16 nScriptType =pOLV->GetSelectedScriptType();
72cdf0e10cSrcweir         String aScriptTypesInUse( String::CreateFromInt32( nScriptType ) );//pEditEngine->GetScriptType(aSelection)
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         SvtLanguageTable aLangTable;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         // get keyboard language
77cdf0e10cSrcweir         String aKeyboardLang;
78cdf0e10cSrcweir         LanguageType nLang = LANGUAGE_DONTKNOW;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         Window* pWin = rEditView.GetWindow();
81cdf0e10cSrcweir         if(pWin)
82cdf0e10cSrcweir             nLang = pWin->GetInputLanguage();
83cdf0e10cSrcweir         if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_SYSTEM)
84cdf0e10cSrcweir             aKeyboardLang = aLangTable.GetString( nLang );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         // get the language that is in use
87cdf0e10cSrcweir         const String aMultipleLanguages = String::CreateFromAscii("*");
88cdf0e10cSrcweir         String aCurrentLang = aMultipleLanguages;
89cdf0e10cSrcweir         SfxItemSet aSet(pOLV->GetAttribs());
90cdf0e10cSrcweir         nLang = SwLangHelper::GetCurrentLanguage( aSet,nScriptType );
91cdf0e10cSrcweir         if (nLang != LANGUAGE_DONTKNOW)
92cdf0e10cSrcweir             aCurrentLang = aLangTable.GetString( nLang );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         // build sequence for status value
95cdf0e10cSrcweir         uno::Sequence< ::rtl::OUString > aSeq( 4 );
96cdf0e10cSrcweir         aSeq[0] = aCurrentLang;
97cdf0e10cSrcweir         aSeq[1] = aScriptTypesInUse;
98cdf0e10cSrcweir         aSeq[2] = aKeyboardLang;
99cdf0e10cSrcweir         aSeq[3] = SwLangHelper::GetTextForLanguageGuessing( pEditEngine, aSelection );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         // set sequence as status value
102cdf0e10cSrcweir         SfxStringListItem aItem( SID_LANGUAGE_STATUS );
103cdf0e10cSrcweir         aItem.SetStringList( aSeq );
104cdf0e10cSrcweir         rSet.Put( aItem, SID_LANGUAGE_STATUS );
105cdf0e10cSrcweir         return 0;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
SetLanguageStatus(OutlinerView * pOLV,SfxRequest & rReq,SwView & rView,SwWrtShell & rSh)108cdf0e10cSrcweir     bool SetLanguageStatus( OutlinerView* pOLV, SfxRequest &rReq, SwView &rView, SwWrtShell &rSh )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         bool bRestoreSelection = false;
111cdf0e10cSrcweir         SfxItemSet aEditAttr(pOLV->GetAttribs());
112cdf0e10cSrcweir         ESelection   aSelection  = pOLV->GetSelection();
113cdf0e10cSrcweir         EditView   & rEditView   = pOLV->GetEditView();
114cdf0e10cSrcweir         EditEngine * pEditEngine = rEditView.GetEditEngine();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         // get the language
117cdf0e10cSrcweir         String aNewLangTxt;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         SFX_REQUEST_ARG( rReq, pItem, SfxStringItem, SID_LANGUAGE_STATUS , sal_False );
120cdf0e10cSrcweir         if (pItem)
121cdf0e10cSrcweir             aNewLangTxt = pItem->GetValue();
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         //!! Remember the view frame right now...
124cdf0e10cSrcweir         //!! (call to GetView().GetViewFrame() will break if the
125cdf0e10cSrcweir         //!! SwTextShell got destroyed meanwhile.)
126cdf0e10cSrcweir         SfxViewFrame *pViewFrame = rView.GetViewFrame();
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         if (aNewLangTxt.EqualsAscii( "*" ))
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             // open the dialog "Tools/Options/Language Settings - Language"
131cdf0e10cSrcweir             SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
132cdf0e10cSrcweir             if (pFact)
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 VclAbstractDialog* pDlg = pFact->CreateVclDialog( rView.GetWindow(), SID_LANGUAGE_OPTIONS );
135cdf0e10cSrcweir                 pDlg->Execute();
136cdf0e10cSrcweir                 delete pDlg;
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         else
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             // setting the new language...
142cdf0e10cSrcweir             if (aNewLangTxt.Len() > 0)
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 const String aSelectionLangPrefix( String::CreateFromAscii("Current_") );
145cdf0e10cSrcweir                 const String aParagraphLangPrefix( String::CreateFromAscii("Paragraph_") );
146cdf0e10cSrcweir                 const String aDocumentLangPrefix( String::CreateFromAscii("Default_") );
147cdf0e10cSrcweir                 const String aStrNone( String::CreateFromAscii("LANGUAGE_NONE") );
148cdf0e10cSrcweir                 const String aStrResetLangs( String::CreateFromAscii("RESET_LANGUAGES") );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir                 xub_StrLen nPos = 0;
151cdf0e10cSrcweir                 bool bForSelection = true;
152cdf0e10cSrcweir                 bool bForParagraph = false;
153cdf0e10cSrcweir                 if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aSelectionLangPrefix, 0 )))
154cdf0e10cSrcweir                 {
155cdf0e10cSrcweir                     // ... for the current selection
156cdf0e10cSrcweir                     aNewLangTxt = aNewLangTxt.Erase( nPos, aSelectionLangPrefix.Len() );
157cdf0e10cSrcweir                     bForSelection = true;
158cdf0e10cSrcweir                 }
159cdf0e10cSrcweir                 else if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aParagraphLangPrefix , 0 )))
160cdf0e10cSrcweir                 {
161cdf0e10cSrcweir                     // ... for the current paragraph language
162cdf0e10cSrcweir                     aNewLangTxt = aNewLangTxt.Erase( nPos, aParagraphLangPrefix.Len() );
163cdf0e10cSrcweir                     bForSelection = true;
164cdf0e10cSrcweir                     bForParagraph = true;
165cdf0e10cSrcweir                 }
166cdf0e10cSrcweir                 else if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aDocumentLangPrefix , 0 )))
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     // ... as default document language
169cdf0e10cSrcweir                     aNewLangTxt = aNewLangTxt.Erase( nPos, aDocumentLangPrefix.Len() );
170cdf0e10cSrcweir                     bForSelection = false;
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir                 if (bForParagraph)
174cdf0e10cSrcweir                 {
175cdf0e10cSrcweir                     bRestoreSelection = true;
176cdf0e10cSrcweir                     SwLangHelper::SelectPara( rEditView, aSelection );
177cdf0e10cSrcweir                     aSelection = pOLV->GetSelection();
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir                 if (!bForSelection) // document language to be changed...
180cdf0e10cSrcweir                 {
181cdf0e10cSrcweir                     rSh.StartAction();
182cdf0e10cSrcweir                     rSh.LockView( sal_True );
183cdf0e10cSrcweir                     rSh.Push();
184cdf0e10cSrcweir 
185cdf0e10cSrcweir                     // prepare to apply new language to all text in document
186cdf0e10cSrcweir                     rSh.SelAll();
187cdf0e10cSrcweir                     rSh.ExtendedSelectAll();
188cdf0e10cSrcweir                 }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir                 if (aNewLangTxt == aStrNone)
191cdf0e10cSrcweir                     SwLangHelper::SetLanguage_None( rSh, pOLV, aSelection, bForSelection, aEditAttr );
192cdf0e10cSrcweir                 else if (aNewLangTxt == aStrResetLangs)
193cdf0e10cSrcweir                     SwLangHelper::ResetLanguages( rSh, pOLV, aSelection, bForSelection );
194cdf0e10cSrcweir                 else
195cdf0e10cSrcweir                     SwLangHelper::SetLanguage( rSh, pOLV, aSelection, aNewLangTxt, bForSelection, aEditAttr );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir                 // ugly hack, as it seems that EditView/EditEngine does not update their spellchecking marks
198cdf0e10cSrcweir                 // when setting a new language attribute
199cdf0e10cSrcweir                 if (bForSelection)
200cdf0e10cSrcweir                 {
201cdf0e10cSrcweir                     const SwViewOption* pVOpt = rView.GetWrtShellPtr()->GetViewOptions();
202cdf0e10cSrcweir                     sal_uLong nCntrl = pEditEngine->GetControlWord();
203cdf0e10cSrcweir                     // turn off
204cdf0e10cSrcweir                     if (!pVOpt->IsOnlineSpell())
205cdf0e10cSrcweir                         nCntrl &= ~EE_CNTRL_ONLINESPELLING;
206cdf0e10cSrcweir                     else
207cdf0e10cSrcweir                         nCntrl &= ~EE_CNTRL_ONLINESPELLING;
208cdf0e10cSrcweir                     pEditEngine->SetControlWord(nCntrl);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir                     //turn back on
211cdf0e10cSrcweir                     if (pVOpt->IsOnlineSpell())
212cdf0e10cSrcweir                         nCntrl |= EE_CNTRL_ONLINESPELLING;
213cdf0e10cSrcweir                     else
214cdf0e10cSrcweir                         nCntrl &= ~EE_CNTRL_ONLINESPELLING;
215cdf0e10cSrcweir                     pEditEngine->SetControlWord(nCntrl);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                     pEditEngine->CompleteOnlineSpelling();
218cdf0e10cSrcweir                     rEditView.Invalidate();
219cdf0e10cSrcweir                 }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir                 if (!bForSelection)
222cdf0e10cSrcweir                 {
223cdf0e10cSrcweir                     // need to release view and restore selection...
224cdf0e10cSrcweir                     rSh.Pop( sal_False );
225cdf0e10cSrcweir                     rSh.LockView( sal_False );
226cdf0e10cSrcweir                     rSh.EndAction();
227cdf0e10cSrcweir                 }
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         // invalidate slot to get the new language displayed
232cdf0e10cSrcweir         pViewFrame->GetBindings().Invalidate( rReq.GetSlot() );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         rReq.Done();
235cdf0e10cSrcweir         return bRestoreSelection;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
SetLanguage(SwWrtShell & rWrtSh,const String & rLangText,bool bIsForSelection,SfxItemSet & rCoreSet)239cdf0e10cSrcweir     void SetLanguage( SwWrtShell &rWrtSh, const String &rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         SetLanguage( rWrtSh, 0 , ESelection(), rLangText, bIsForSelection, rCoreSet );
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
SetLanguage(SwWrtShell & rWrtSh,OutlinerView * pOLV,ESelection aSelection,const String & rLangText,bool bIsForSelection,SfxItemSet & rCoreSet)244cdf0e10cSrcweir     void SetLanguage( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, const String &rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
245cdf0e10cSrcweir     {
246cdf0e10cSrcweir         const LanguageType nLang = SvtLanguageTable().GetType( rLangText );
247cdf0e10cSrcweir         if (nLang != LANGUAGE_DONTKNOW)
248cdf0e10cSrcweir         {
249cdf0e10cSrcweir             sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( nLang );
250cdf0e10cSrcweir 
251cdf0e10cSrcweir             EditEngine* pEditEngine = pOLV ? pOLV->GetEditView().GetEditEngine() : NULL;
252cdf0e10cSrcweir             DBG_ASSERT( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir             //get ScriptType
255cdf0e10cSrcweir             sal_uInt16 nLangWhichId = 0;
256cdf0e10cSrcweir             bool bIsSingleScriptType = true;
257cdf0e10cSrcweir             switch (nScriptType)
258cdf0e10cSrcweir             {
259cdf0e10cSrcweir                 case SCRIPTTYPE_LATIN :    nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE : RES_CHRATR_LANGUAGE; break;
260cdf0e10cSrcweir                 case SCRIPTTYPE_ASIAN :    nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CJK : RES_CHRATR_CJK_LANGUAGE; break;
261cdf0e10cSrcweir                 case SCRIPTTYPE_COMPLEX :  nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CTL : RES_CHRATR_CTL_LANGUAGE; break;
262cdf0e10cSrcweir                 default:
263cdf0e10cSrcweir                     bIsSingleScriptType = false;
264cdf0e10cSrcweir                     DBG_ERROR( "unexpected case" );
265cdf0e10cSrcweir             }
266cdf0e10cSrcweir             if (bIsSingleScriptType)
267cdf0e10cSrcweir             {
268cdf0e10cSrcweir                 // change language for selection or paragraph
269cdf0e10cSrcweir                 // (for paragraph is handled by previosuly having set the selection to the
270cdf0e10cSrcweir                 // whole paragraph)
271cdf0e10cSrcweir                 if (bIsForSelection)
272cdf0e10cSrcweir                 {
273cdf0e10cSrcweir                     // apply language to current selection
274cdf0e10cSrcweir                     if (pEditEngine)
275cdf0e10cSrcweir                     {
276cdf0e10cSrcweir                         rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
277cdf0e10cSrcweir                         pEditEngine->QuickSetAttribs( rCoreSet, aSelection);
278cdf0e10cSrcweir                     }
279cdf0e10cSrcweir                     else
280cdf0e10cSrcweir                     {
281cdf0e10cSrcweir                         rWrtSh.GetCurAttr( rCoreSet );
282cdf0e10cSrcweir                         rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
28369a74367SOliver-Rainer Wittmann                         rWrtSh.SetAttrSet( rCoreSet );
284cdf0e10cSrcweir                     }
285cdf0e10cSrcweir                 }
286cdf0e10cSrcweir                 else // change language for all text
287cdf0e10cSrcweir                 {
288cdf0e10cSrcweir                     // set document default language
289cdf0e10cSrcweir                     switch (nLangWhichId)
290cdf0e10cSrcweir                     {
291cdf0e10cSrcweir                          case EE_CHAR_LANGUAGE :      nLangWhichId = RES_CHRATR_LANGUAGE; break;
292cdf0e10cSrcweir                          case EE_CHAR_LANGUAGE_CJK :  nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
293cdf0e10cSrcweir                          case EE_CHAR_LANGUAGE_CTL :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
294cdf0e10cSrcweir                     }
295cdf0e10cSrcweir                     rWrtSh.SetDefault( SvxLanguageItem( nLang, nLangWhichId ) );
296cdf0e10cSrcweir 
297cdf0e10cSrcweir                     // #i102191: hard set respective language attribute in text document
298cdf0e10cSrcweir                     // (for all text in the document - which should be selected by now...)
29969a74367SOliver-Rainer Wittmann                     rWrtSh.SetAttrItem( SvxLanguageItem( nLang, nLangWhichId ) );
300cdf0e10cSrcweir                 }
301cdf0e10cSrcweir             }
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir 
SetLanguage_None(SwWrtShell & rWrtSh,bool bIsForSelection,SfxItemSet & rCoreSet)305cdf0e10cSrcweir     void SetLanguage_None( SwWrtShell &rWrtSh, bool bIsForSelection, SfxItemSet &rCoreSet )
306cdf0e10cSrcweir     {
307cdf0e10cSrcweir         SetLanguage_None( rWrtSh,0,ESelection(),bIsForSelection,rCoreSet );
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 
SetLanguage_None(SwWrtShell & rWrtSh,OutlinerView * pOLV,ESelection aSelection,bool bIsForSelection,SfxItemSet & rCoreSet)310cdf0e10cSrcweir     void SetLanguage_None( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, bool bIsForSelection, SfxItemSet &rCoreSet )
311cdf0e10cSrcweir     {
312cdf0e10cSrcweir         // EditEngine IDs
313cdf0e10cSrcweir         const sal_uInt16 aLangWhichId_EE[3] =
314cdf0e10cSrcweir         {
315cdf0e10cSrcweir             EE_CHAR_LANGUAGE,
316cdf0e10cSrcweir             EE_CHAR_LANGUAGE_CJK,
317cdf0e10cSrcweir             EE_CHAR_LANGUAGE_CTL
318cdf0e10cSrcweir         };
319cdf0e10cSrcweir 
320*0cf2fd93Smseidel         // Writer IDs
321cdf0e10cSrcweir         const sal_uInt16 aLangWhichId_Writer[3] =
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir             RES_CHRATR_LANGUAGE,
324cdf0e10cSrcweir             RES_CHRATR_CJK_LANGUAGE,
325cdf0e10cSrcweir             RES_CHRATR_CTL_LANGUAGE
326cdf0e10cSrcweir         };
327cdf0e10cSrcweir 
328cdf0e10cSrcweir         if (bIsForSelection)
329cdf0e10cSrcweir         {
330cdf0e10cSrcweir             // change language for selection or paragraph
331*0cf2fd93Smseidel             // (for paragraph is handled by previously having set the selection to the
332cdf0e10cSrcweir             // whole paragraph)
333cdf0e10cSrcweir 
334cdf0e10cSrcweir             EditEngine* pEditEngine = pOLV ? pOLV->GetEditView().GetEditEngine() : NULL;
335cdf0e10cSrcweir             DBG_ASSERT( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
336cdf0e10cSrcweir             if (pEditEngine)
337cdf0e10cSrcweir             {
338cdf0e10cSrcweir                 for (sal_uInt16 i = 0; i < 3; ++i)
339cdf0e10cSrcweir                     rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_EE[i] ));
340cdf0e10cSrcweir                 pEditEngine->QuickSetAttribs( rCoreSet, aSelection);
341cdf0e10cSrcweir             }
342cdf0e10cSrcweir             else
343cdf0e10cSrcweir             {
344cdf0e10cSrcweir                 rWrtSh.GetCurAttr( rCoreSet );
345cdf0e10cSrcweir                 for (sal_uInt16 i = 0; i < 3; ++i)
346cdf0e10cSrcweir                     rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_Writer[i] ));
34769a74367SOliver-Rainer Wittmann                 rWrtSh.SetAttrSet( rCoreSet );
348cdf0e10cSrcweir             }
349cdf0e10cSrcweir         }
350cdf0e10cSrcweir         else // change language for all text
351cdf0e10cSrcweir         {
352cdf0e10cSrcweir             SvUShortsSort aAttribs;
353cdf0e10cSrcweir             for (sal_uInt16 i = 0; i < 3; ++i)
354cdf0e10cSrcweir             {
355cdf0e10cSrcweir                 rWrtSh.SetDefault( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_Writer[i] ) );
356cdf0e10cSrcweir                 aAttribs.Insert( aLangWhichId_Writer[i] );
357cdf0e10cSrcweir             }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir             // set all language attributes to default
360cdf0e10cSrcweir             // (for all text in the document - which should be selected by now...)
361cdf0e10cSrcweir             rWrtSh.ResetAttr( &aAttribs );
362cdf0e10cSrcweir         }
363cdf0e10cSrcweir     }
364cdf0e10cSrcweir 
ResetLanguages(SwWrtShell & rWrtSh,bool bIsForSelection)365cdf0e10cSrcweir     void ResetLanguages( SwWrtShell &rWrtSh, bool bIsForSelection )
366cdf0e10cSrcweir     {
367cdf0e10cSrcweir         ResetLanguages( rWrtSh, 0 , ESelection(), bIsForSelection );
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir 
ResetLanguages(SwWrtShell & rWrtSh,OutlinerView * pOLV,ESelection aSelection,bool bIsForSelection)370cdf0e10cSrcweir     void ResetLanguages( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, bool bIsForSelection )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         (void) bIsForSelection;
373cdf0e10cSrcweir         (void) aSelection;
374cdf0e10cSrcweir 
375cdf0e10cSrcweir         // reset language for current selection.
376cdf0e10cSrcweir         // The selection should already have been expanded to the whole paragraph or
377cdf0e10cSrcweir         // to all text in the document if those are the ranges where to reset
378cdf0e10cSrcweir         // the language attributes
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         if (pOLV)
381cdf0e10cSrcweir         {
382cdf0e10cSrcweir             EditView &rEditView = pOLV->GetEditView();
383cdf0e10cSrcweir             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE );
384cdf0e10cSrcweir             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CJK );
385cdf0e10cSrcweir             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CTL );
386cdf0e10cSrcweir         }
387cdf0e10cSrcweir         else
388cdf0e10cSrcweir         {
389cdf0e10cSrcweir             SvUShortsSort aAttribs;
390cdf0e10cSrcweir             aAttribs.Insert( RES_CHRATR_LANGUAGE );
391cdf0e10cSrcweir             aAttribs.Insert( RES_CHRATR_CJK_LANGUAGE );
392cdf0e10cSrcweir             aAttribs.Insert( RES_CHRATR_CTL_LANGUAGE );
393cdf0e10cSrcweir             rWrtSh.ResetAttr( &aAttribs );
394cdf0e10cSrcweir         }
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 
398*0cf2fd93Smseidel     // @returns : the language for the selected text that is set for the
399*0cf2fd93Smseidel     //     specified attribute (script type).
400*0cf2fd93Smseidel     //     If there are more than one languages used LANGUAGE_DONTKNOW will be returned.
401*0cf2fd93Smseidel     // @param nLangWhichId : one of
402*0cf2fd93Smseidel     //     RES_CHRATR_LANGUAGE, RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CTL_LANGUAGE,
GetLanguage(SwWrtShell & rSh,sal_uInt16 nLangWhichId)403cdf0e10cSrcweir     LanguageType GetLanguage( SwWrtShell &rSh, sal_uInt16 nLangWhichId )
404cdf0e10cSrcweir     {
405cdf0e10cSrcweir         SfxItemSet aSet( rSh.GetAttrPool(), nLangWhichId, nLangWhichId );
406cdf0e10cSrcweir         rSh.GetCurAttr( aSet );
407cdf0e10cSrcweir 
408cdf0e10cSrcweir         return GetLanguage(aSet,nLangWhichId);
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir 
GetLanguage(SfxItemSet aSet,sal_uInt16 nLangWhichId)411cdf0e10cSrcweir     LanguageType GetLanguage( SfxItemSet aSet, sal_uInt16 nLangWhichId )
412cdf0e10cSrcweir     {
413cdf0e10cSrcweir 
414cdf0e10cSrcweir         LanguageType nLang = LANGUAGE_SYSTEM;
415cdf0e10cSrcweir 
416cdf0e10cSrcweir         const SfxPoolItem *pItem = 0;
417cdf0e10cSrcweir         SfxItemState nState = aSet.GetItemState( nLangWhichId, sal_True, &pItem );
418cdf0e10cSrcweir         if (nState > SFX_ITEM_DEFAULT && pItem)
419cdf0e10cSrcweir         {
420cdf0e10cSrcweir             // the item is set and can be used
421cdf0e10cSrcweir             nLang = (dynamic_cast< const SvxLanguageItem* >(pItem))->GetLanguage();
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir         else if (nState == SFX_ITEM_DEFAULT)
424cdf0e10cSrcweir         {
425cdf0e10cSrcweir             // since the attribute is not set: retrieve the default value
426cdf0e10cSrcweir             nLang = (dynamic_cast< const SvxLanguageItem& >(aSet.GetPool()->GetDefaultItem( nLangWhichId ))).GetLanguage();
427cdf0e10cSrcweir         }
428cdf0e10cSrcweir         else if (nState == SFX_ITEM_DONTCARE)
429cdf0e10cSrcweir         {
430cdf0e10cSrcweir             // there is more than one language...
431cdf0e10cSrcweir             nLang = LANGUAGE_DONTKNOW;
432cdf0e10cSrcweir         }
433cdf0e10cSrcweir         DBG_ASSERT( nLang != LANGUAGE_SYSTEM, "failed to get the language?" );
434cdf0e10cSrcweir 
435cdf0e10cSrcweir         return nLang;
436cdf0e10cSrcweir     }
437cdf0e10cSrcweir 
438*0cf2fd93Smseidel     // @returns: the language in use for the selected text.
439*0cf2fd93Smseidel     //     'In use' means the language(s) matching the script type(s) of the
440*0cf2fd93Smseidel     //     selected text. Or in other words, the language a spell checker would use.
441*0cf2fd93Smseidel     //     If there is more than one language LANGUAGE_DONTKNOW will be returned.
GetCurrentLanguage(SwWrtShell & rSh)442cdf0e10cSrcweir     LanguageType GetCurrentLanguage( SwWrtShell &rSh )
443cdf0e10cSrcweir     {
444cdf0e10cSrcweir         // get all script types used in current selection
445cdf0e10cSrcweir         const sal_uInt16 nScriptType = rSh.GetScriptType();
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         //set language attribute to use according to the script type
448cdf0e10cSrcweir         sal_uInt16 nLangWhichId = 0;
449cdf0e10cSrcweir         bool bIsSingleScriptType = true;
450cdf0e10cSrcweir         switch (nScriptType)
451cdf0e10cSrcweir         {
452cdf0e10cSrcweir              case SCRIPTTYPE_LATIN :    nLangWhichId = RES_CHRATR_LANGUAGE; break;
453cdf0e10cSrcweir              case SCRIPTTYPE_ASIAN :    nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
454cdf0e10cSrcweir              case SCRIPTTYPE_COMPLEX :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
455cdf0e10cSrcweir              default: bIsSingleScriptType = false; break;
456cdf0e10cSrcweir         }
457cdf0e10cSrcweir 
458cdf0e10cSrcweir         // get language according to the script type(s) in use
459cdf0e10cSrcweir         LanguageType nCurrentLang = LANGUAGE_SYSTEM;
460cdf0e10cSrcweir         if (bIsSingleScriptType)
461cdf0e10cSrcweir             nCurrentLang = GetLanguage( rSh, nLangWhichId );
462cdf0e10cSrcweir         else
463cdf0e10cSrcweir         {
464cdf0e10cSrcweir             // check if all script types are set to LANGUAGE_NONE and return
465cdf0e10cSrcweir             // that if this is the case. Otherwise, having multiple script types
466cdf0e10cSrcweir             // in use always means there are several languages in use...
467cdf0e10cSrcweir             const sal_uInt16 aScriptTypes[3] =
468cdf0e10cSrcweir             {
469cdf0e10cSrcweir                 RES_CHRATR_LANGUAGE,
470cdf0e10cSrcweir                 RES_CHRATR_CJK_LANGUAGE,
471cdf0e10cSrcweir                 RES_CHRATR_CTL_LANGUAGE
472cdf0e10cSrcweir             };
473cdf0e10cSrcweir             nCurrentLang = LANGUAGE_NONE;
474cdf0e10cSrcweir             for (sal_uInt16 i = 0; i < 3; ++i)
475cdf0e10cSrcweir             {
476cdf0e10cSrcweir                 LanguageType nTmpLang = GetLanguage( rSh, aScriptTypes[i] );
477cdf0e10cSrcweir                 if (nTmpLang != LANGUAGE_NONE)
478cdf0e10cSrcweir                 {
479cdf0e10cSrcweir                     nCurrentLang = LANGUAGE_DONTKNOW;
480cdf0e10cSrcweir                     break;
481cdf0e10cSrcweir                 }
482cdf0e10cSrcweir             }
483cdf0e10cSrcweir         }
484cdf0e10cSrcweir         DBG_ASSERT( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
485cdf0e10cSrcweir 
486cdf0e10cSrcweir         return nCurrentLang;
487cdf0e10cSrcweir     }
488cdf0e10cSrcweir 
489*0cf2fd93Smseidel     // @returns: the language in use for the selected text.
490*0cf2fd93Smseidel     //     'In use' means the language(s) matching the script type(s) of the
491*0cf2fd93Smseidel     //     selected text. Or in other words, the language a spell checker would use.
492*0cf2fd93Smseidel     //     If there is more than one language LANGUAGE_DONTKNOW will be returned.
GetCurrentLanguage(SfxItemSet aSet,sal_uInt16 nScriptType)493cdf0e10cSrcweir     LanguageType GetCurrentLanguage( SfxItemSet aSet, sal_uInt16 nScriptType )
494cdf0e10cSrcweir     {
495cdf0e10cSrcweir         //set language attribute to use according to the script type
496cdf0e10cSrcweir         sal_uInt16 nLangWhichId = 0;
497cdf0e10cSrcweir         bool bIsSingleScriptType = true;
498cdf0e10cSrcweir         switch (nScriptType)
499cdf0e10cSrcweir         {
500cdf0e10cSrcweir              case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE; break;
501cdf0e10cSrcweir              case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
502cdf0e10cSrcweir              case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
503cdf0e10cSrcweir              default: bIsSingleScriptType = false;
504cdf0e10cSrcweir         }
505cdf0e10cSrcweir 
506cdf0e10cSrcweir         // get language according to the script type(s) in use
507cdf0e10cSrcweir         LanguageType nCurrentLang = LANGUAGE_SYSTEM;
508cdf0e10cSrcweir         if (bIsSingleScriptType)
509cdf0e10cSrcweir             nCurrentLang = GetLanguage( aSet, nLangWhichId );
510cdf0e10cSrcweir         else
511cdf0e10cSrcweir         {
512cdf0e10cSrcweir             // check if all script types are set to LANGUAGE_NONE and return
513cdf0e10cSrcweir             // that if this is the case. Otherwise, having multiple script types
514cdf0e10cSrcweir             // in use always means there are several languages in use...
515cdf0e10cSrcweir             const sal_uInt16 aScriptTypes[3] =
516cdf0e10cSrcweir             {
517cdf0e10cSrcweir                 EE_CHAR_LANGUAGE,
518cdf0e10cSrcweir                 EE_CHAR_LANGUAGE_CJK,
519cdf0e10cSrcweir                 EE_CHAR_LANGUAGE_CTL
520cdf0e10cSrcweir             };
521cdf0e10cSrcweir             nCurrentLang = LANGUAGE_NONE;
522cdf0e10cSrcweir             for (sal_uInt16 i = 0; i < 3; ++i)
523cdf0e10cSrcweir             {
524cdf0e10cSrcweir                 LanguageType nTmpLang = GetLanguage( aSet, aScriptTypes[i] );
525cdf0e10cSrcweir                 if (nTmpLang != LANGUAGE_NONE)
526cdf0e10cSrcweir                 {
527cdf0e10cSrcweir                     nCurrentLang = LANGUAGE_DONTKNOW;
528cdf0e10cSrcweir                     break;
529cdf0e10cSrcweir                 }
530cdf0e10cSrcweir             }
531cdf0e10cSrcweir         }
532cdf0e10cSrcweir         DBG_ASSERT( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         return nCurrentLang;
535cdf0e10cSrcweir     }
536cdf0e10cSrcweir 
GetTextForLanguageGuessing(SwWrtShell & rSh)537cdf0e10cSrcweir     String GetTextForLanguageGuessing( SwWrtShell &rSh )
538cdf0e10cSrcweir     {
539cdf0e10cSrcweir         // string for guessing language
540cdf0e10cSrcweir         String aText;
541cdf0e10cSrcweir         SwPaM *pCrsr = rSh.GetCrsr();
542cdf0e10cSrcweir         SwTxtNode *pNode = pCrsr->GetNode()->GetTxtNode();
543cdf0e10cSrcweir         if (pNode)
544cdf0e10cSrcweir         {
545cdf0e10cSrcweir             aText = pNode->GetTxt();
546cdf0e10cSrcweir             if (aText.Len() > 0)
547cdf0e10cSrcweir             {
548cdf0e10cSrcweir                 xub_StrLen nStt = 0;
549cdf0e10cSrcweir                 xub_StrLen nEnd = pCrsr->GetPoint()->nContent.GetIndex();
550cdf0e10cSrcweir                 // at most 100 chars to the left...
551cdf0e10cSrcweir                 nStt = nEnd > 100 ? nEnd - 100 : 0;
552cdf0e10cSrcweir                 // ... and 100 to the right of the cursor position
553cdf0e10cSrcweir                 nEnd = aText.Len() - nEnd > 100 ? nEnd + 100 : aText.Len();
554cdf0e10cSrcweir                 aText = aText.Copy( nStt, nEnd - nStt );
555cdf0e10cSrcweir             }
556cdf0e10cSrcweir         }
557cdf0e10cSrcweir         return aText;
558cdf0e10cSrcweir     }
559cdf0e10cSrcweir 
GetTextForLanguageGuessing(EditEngine * rEditEngine,ESelection aDocSelection)560cdf0e10cSrcweir     String GetTextForLanguageGuessing( EditEngine* rEditEngine, ESelection aDocSelection )
561cdf0e10cSrcweir     {
562cdf0e10cSrcweir         // string for guessing language
563cdf0e10cSrcweir         String aText;
564cdf0e10cSrcweir 
565cdf0e10cSrcweir         aText = rEditEngine->GetText(aDocSelection);
566cdf0e10cSrcweir         if (aText.Len() > 0)
567cdf0e10cSrcweir         {
568cdf0e10cSrcweir             xub_StrLen nStt = 0;
569cdf0e10cSrcweir             xub_StrLen nEnd = aDocSelection.nEndPos;
570cdf0e10cSrcweir             // at most 100 chars to the left...
571cdf0e10cSrcweir             nStt = nEnd > 100 ? nEnd - 100 : 0;
572cdf0e10cSrcweir             // ... and 100 to the right of the cursor position
573cdf0e10cSrcweir             nEnd = aText.Len() - nEnd > 100 ? nEnd + 100 : aText.Len();
574cdf0e10cSrcweir             aText = aText.Copy( nStt, nEnd - nStt );
575cdf0e10cSrcweir         }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir         return aText;
578cdf0e10cSrcweir     }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 
SelectPara(EditView & rEditView,const ESelection & rCurSel)581cdf0e10cSrcweir     void SelectPara( EditView &rEditView, const ESelection &rCurSel )
582cdf0e10cSrcweir     {
583cdf0e10cSrcweir         ESelection aParaSel( rCurSel.nStartPara, 0, rCurSel.nStartPara, USHRT_MAX );
584cdf0e10cSrcweir         rEditView.SetSelection( aParaSel );
585cdf0e10cSrcweir     }
586cdf0e10cSrcweir 
SelectCurrentPara(SwWrtShell & rWrtSh)587cdf0e10cSrcweir     void SelectCurrentPara( SwWrtShell &rWrtSh )
588cdf0e10cSrcweir     {
589cdf0e10cSrcweir         // select current para
590cdf0e10cSrcweir         if (!rWrtSh.IsSttPara())
591cdf0e10cSrcweir             rWrtSh.MovePara( fnParaCurr, fnParaStart );
592cdf0e10cSrcweir         if (!rWrtSh.HasMark())
593cdf0e10cSrcweir             rWrtSh.SetMark();
594cdf0e10cSrcweir         rWrtSh.SwapPam();
595cdf0e10cSrcweir         if (!rWrtSh.IsEndPara())
596cdf0e10cSrcweir             rWrtSh.MovePara( fnParaCurr, fnParaEnd );
597cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 1
598cdf0e10cSrcweir         String aSelTxt;
599cdf0e10cSrcweir         rWrtSh.GetSelectedText( aSelTxt );
600cdf0e10cSrcweir         (void) aSelTxt;
601cdf0e10cSrcweir     #endif
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir }
604*0cf2fd93Smseidel 
605*0cf2fd93Smseidel /* vim: set noet sw=4 ts=4: */
606