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 <Window.hxx>
21 #include <DrawDocShell.hxx>
22 
23 #include <svx/svxids.hrc>
24 
25 #include <svx/ofaitem.hxx>
26 #include <svl/stritem.hxx>
27 #include <svl/srchitem.hxx>
28 #include <svl/languageoptions.hxx>
29 #include <svtools/langtab.hxx>
30 #include <sfx2/request.hxx>
31 #include <sfx2/sfxdlg.hxx>
32 #include <sfx2/viewfrm.hxx>
33 #include <vcl/abstdlg.hxx>
34 #include <svx/drawitem.hxx>
35 #include <editeng/langitem.hxx>
36 #include <editeng/eeitem.hxx>
37 #include <editeng/outlobj.hxx>
38 #include <editeng/editobj.hxx>
39 #include <com/sun/star/i18n/TextConversionOption.hpp>
40 #include <sfx2/notebookbar/SfxNotebookBar.hxx>
41 
42 #include <sdmod.hxx>
43 #include <drawdoc.hxx>
44 #include <fusearch.hxx>
45 #include <ViewShell.hxx>
46 #include <slideshow.hxx>
47 #include <fuhhconv.hxx>
48 #include <memory>
49 
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::uno;
53 
54 namespace sd {
55 
lcl_setLanguageForObj(SdrObject * pObj,LanguageType nLang,bool bLanguageNone)56 static void lcl_setLanguageForObj( SdrObject *pObj, LanguageType nLang, bool bLanguageNone )
57 {
58     const sal_uInt16 aLangWhichId_EE[3] =
59     {
60         EE_CHAR_LANGUAGE,
61         EE_CHAR_LANGUAGE_CJK,
62         EE_CHAR_LANGUAGE_CTL
63     };
64 
65     if( bLanguageNone )
66         nLang = LANGUAGE_NONE;
67 
68     if( nLang != LANGUAGE_DONTKNOW )
69     {
70         if( nLang == LANGUAGE_NONE )
71         {
72             for(sal_uInt16 n : aLangWhichId_EE)
73                 pObj->SetMergedItem( SvxLanguageItem( nLang, n ) );
74         }
75         else
76         {
77             sal_uInt16 nLangWhichId = 0;
78             SvtScriptType nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( nLang );
79             switch (nScriptType)
80             {
81                 case SvtScriptType::LATIN :    nLangWhichId = EE_CHAR_LANGUAGE; break;
82                 case SvtScriptType::ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
83                 case SvtScriptType::COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
84                 default:
85                     OSL_FAIL("unexpected case" );
86                     return;
87             }
88             pObj->SetMergedItem( SvxLanguageItem( nLang, nLangWhichId ) );
89 
90             // Reset shape text language to default, so it inherits the shape language set above.
91             OutlinerParaObject* pOutliner = pObj->GetOutlinerParaObject();
92             if (pOutliner)
93             {
94                 EditTextObject& rEditTextObject
95                     = const_cast<EditTextObject&>(pOutliner->GetTextObject());
96                 for (sal_uInt16 n : aLangWhichId_EE)
97                 {
98                     rEditTextObject.RemoveCharAttribs(n);
99                 }
100             }
101         }
102     }
103     else    // Reset to default
104     {
105         for(sal_uInt16 n : aLangWhichId_EE)
106             pObj->ClearMergedItem( n );
107     }
108 }
109 
lcl_setLanguage(const SdDrawDocument * pDoc,const OUString & rLanguage,bool bLanguageNone=false)110 static void lcl_setLanguage( const SdDrawDocument *pDoc, const OUString &rLanguage, bool bLanguageNone = false )
111 {
112     LanguageType nLang = SvtLanguageTable::GetLanguageType( rLanguage );
113 
114     // Do it for SdDrawDocument->SetLanguage as well?
115 
116     sal_uInt16 nPageCount = pDoc->GetPageCount();   // Pick All Pages
117     for( sal_uInt16 nPage = 0; nPage < nPageCount; nPage++ )
118     {
119         const SdrPage *pPage = pDoc->GetPage( nPage );
120         const size_t nObjCount = pPage->GetObjCount();
121         for( size_t nObj = 0; nObj < nObjCount; ++nObj )
122         {
123             SdrObject *pObj = pPage->GetObj( nObj );
124             if (pObj->GetObjIdentifier() != OBJ_PAGE)
125                 lcl_setLanguageForObj( pObj, nLang, bLanguageNone );
126         }
127     }
128 }
129 
130 /**
131  * Handles SFX-Requests
132  */
Execute(SfxRequest & rReq)133 void DrawDocShell::Execute( SfxRequest& rReq )
134 {
135     if(mpViewShell && SlideShow::IsRunning( mpViewShell->GetViewShellBase() ))
136     {
137         // during a running presentation no slot will be executed
138         return;
139     }
140 
141     switch ( rReq.GetSlot() )
142     {
143         case SID_SEARCH_ITEM:
144         {
145             const SfxItemSet* pReqArgs = rReq.GetArgs();
146 
147             if (pReqArgs)
148             {
149                 const SvxSearchItem & rSearchItem = pReqArgs->Get(SID_SEARCH_ITEM);
150 
151                 SD_MOD()->SetSearchItem(std::unique_ptr<SvxSearchItem>(static_cast<SvxSearchItem*>(rSearchItem.Clone())));
152             }
153 
154             rReq.Done();
155         }
156         break;
157 
158         case FID_SEARCH_ON:
159         {
160             // no action needed
161             rReq.Done();
162         }
163         break;
164 
165         case FID_SEARCH_OFF:
166         {
167             if( dynamic_cast< FuSearch* >(mxDocShellFunction.get()) )
168             {
169                 // End Search&Replace in all docshells
170                 SfxObjectShell* pFirstShell = SfxObjectShell::GetFirst();
171                 SfxObjectShell* pShell = pFirstShell;
172 
173                 while (pShell)
174                 {
175                     if( dynamic_cast< const DrawDocShell *>( pShell ) !=  nullptr)
176                     {
177                         static_cast<DrawDocShell*>(pShell)->CancelSearching();
178                     }
179 
180                     pShell = SfxObjectShell::GetNext(*pShell);
181 
182                     if (pShell == pFirstShell)
183                     {
184                         pShell = nullptr;
185                     }
186                 }
187 
188                 SetDocShellFunction(nullptr);
189                 Invalidate();
190                 rReq.Done();
191             }
192         }
193         break;
194 
195         case FID_SEARCH_NOW:
196         {
197             const SfxItemSet* pReqArgs = rReq.GetArgs();
198 
199             if ( pReqArgs )
200             {
201                 rtl::Reference< FuSearch > xFuSearch( dynamic_cast< FuSearch* >( GetDocShellFunction().get() ) );
202 
203                 if( !xFuSearch.is() && mpViewShell )
204                 {
205                     ::sd::View* pView = mpViewShell->GetView();
206                     SetDocShellFunction( FuSearch::Create( mpViewShell, mpViewShell->GetActiveWindow(), pView, mpDoc, rReq ) );
207                     xFuSearch.set( dynamic_cast< FuSearch* >( GetDocShellFunction().get() ) );
208                 }
209 
210                 if( xFuSearch.is() )
211                 {
212                     const SvxSearchItem& rSearchItem = pReqArgs->Get(SID_SEARCH_ITEM);
213 
214                     SD_MOD()->SetSearchItem(std::unique_ptr<SvxSearchItem>(static_cast<SvxSearchItem*>( rSearchItem.Clone() )));
215                     xFuSearch->SearchAndReplace(&rSearchItem);
216                 }
217             }
218 
219             rReq.Done();
220         }
221         break;
222 
223         case SID_CLOSEDOC:
224         {
225             ExecuteSlot(rReq, SfxObjectShell::GetStaticInterface());
226         }
227         break;
228 
229         case SID_GET_COLORLIST:
230         {
231             const SvxColorListItem* pColItem = GetItem( SID_COLOR_TABLE );
232             const XColorListRef& pList = pColItem->GetColorList();
233             rReq.SetReturnValue( OfaRefItem<XColorList>( SID_GET_COLORLIST, pList ) );
234         }
235         break;
236 
237         case SID_VERSION:
238         {
239             ExecuteSlot( rReq, SfxObjectShell::GetStaticInterface() );
240         }
241         break;
242 
243         case SID_HANGUL_HANJA_CONVERSION:
244         {
245             if( mpViewShell )
246             {
247                 rtl::Reference<FuPoor> aFunc( FuHangulHanjaConversion::Create( mpViewShell, mpViewShell->GetActiveWindow(), mpViewShell->GetView(), mpDoc, rReq ) );
248                 static_cast< FuHangulHanjaConversion* >( aFunc.get() )->StartConversion( LANGUAGE_KOREAN, LANGUAGE_KOREAN, nullptr, i18n::TextConversionOption::CHARACTER_BY_CHARACTER, true );
249             }
250         }
251         break;
252 
253         case SID_CHINESE_CONVERSION:
254         {
255             if( mpViewShell )
256             {
257                 rtl::Reference<FuPoor> aFunc( FuHangulHanjaConversion::Create( mpViewShell, mpViewShell->GetActiveWindow(), mpViewShell->GetView(), mpDoc, rReq ) );
258                 static_cast< FuHangulHanjaConversion* >( aFunc.get() )->StartChineseConversion();
259             }
260         }
261         break;
262         case SID_LANGUAGE_STATUS:
263         {
264             OUString aNewLangTxt;
265             const SfxStringItem* pItem = rReq.GetArg<SfxStringItem>(SID_LANGUAGE_STATUS);
266             if (pItem)
267                 aNewLangTxt = pItem->GetValue();
268             if (aNewLangTxt == "*" )
269             {
270                 // open the dialog "Tools/Options/Language Settings - Language"
271                 if (mpViewShell)
272                 {
273                     SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
274                     ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateVclDialog( mpViewShell->GetFrameWeld(), SID_LANGUAGE_OPTIONS ));
275                     pDlg->Execute();
276                 }
277             }
278             else
279             {
280                 if( mpViewShell )
281                 {
282                     // setting the new language...
283                     if (!aNewLangTxt.isEmpty())
284                     {
285                         const OUString aDocumentLangPrefix("Default_");
286                         const OUString aStrNone("LANGUAGE_NONE");
287                         const OUString aStrResetLangs("RESET_LANGUAGES");
288                         SdDrawDocument* pDoc = mpViewShell->GetDoc();
289                         sal_Int32 nPos = -1;
290                         if (-1 != (nPos = aNewLangTxt.indexOf( aDocumentLangPrefix )))
291                         {
292                             aNewLangTxt = aNewLangTxt.replaceAt( nPos, aDocumentLangPrefix.getLength(), "" );
293                         }
294                         else
295                         {
296                             break;
297                         }
298                         if (aNewLangTxt == aStrNone)
299                             lcl_setLanguage( pDoc, OUString(), true );
300                         else if (aNewLangTxt == aStrResetLangs)
301                             lcl_setLanguage( pDoc, OUString() );
302                         else
303                             lcl_setLanguage( pDoc, aNewLangTxt );
304 
305                         if ( pDoc->GetOnlineSpell() )
306                         {
307                             pDoc->StartOnlineSpelling();
308                         }
309                     }
310                 }
311             }
312             Broadcast(SfxHint(SfxHintId::LanguageChanged));
313         }
314         break;
315 
316         case SID_NOTEBOOKBAR:
317         {
318             const SfxStringItem* pFile = rReq.GetArg<SfxStringItem>( SID_NOTEBOOKBAR );
319 
320             if ( mpViewShell )
321             {
322                 SfxBindings& rBindings( mpViewShell->GetFrame()->GetBindings() );
323 
324                 if ( sfx2::SfxNotebookBar::IsActive() )
325                     sfx2::SfxNotebookBar::ExecMethod( rBindings, pFile ? pFile->GetValue() : "" );
326                 else
327                     sfx2::SfxNotebookBar::CloseMethod( rBindings );
328             }
329         }
330         break;
331 
332         default:
333         break;
334     }
335 }
336 
SetDocShellFunction(const rtl::Reference<FuPoor> & xFunction)337 void DrawDocShell::SetDocShellFunction( const rtl::Reference<FuPoor>& xFunction )
338 {
339     if( mxDocShellFunction.is() )
340         mxDocShellFunction->Dispose();
341 
342     mxDocShellFunction = xFunction;
343 }
344 
345 } // end of namespace sd
346 
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
348