Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / func / fuhhconv.cxx
blobe3516b2e1bee02e34b9ad807fc6c0a0783f60af1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/i18n/TextConversionOption.hpp>
22 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <comphelper/propertysequence.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <svl/style.hxx>
29 #include <editeng/eeitem.hxx>
30 #include <editeng/langitem.hxx>
31 #include <editeng/fontitem.hxx>
33 #include <fuhhconv.hxx>
34 #include <drawdoc.hxx>
35 #include <Outliner.hxx>
36 #include <DrawViewShell.hxx>
37 #include <OutlineViewShell.hxx>
38 #include <Window.hxx>
39 #include <ViewShellBase.hxx>
41 #include <sdresid.hxx>
42 #include <strings.hrc>
44 class SfxRequest;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::uno;
50 namespace sd {
52 class ViewShell;
55 FuHangulHanjaConversion::FuHangulHanjaConversion (
56 ViewShell* pViewSh,
57 ::sd::Window* pWin,
58 ::sd::View* pView,
59 SdDrawDocument* pDocument,
60 SfxRequest& rReq )
61 : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
62 pSdOutliner(nullptr),
63 bOwnOutliner(false)
65 if ( dynamic_cast< const DrawViewShell *>( mpViewShell ) != nullptr )
67 bOwnOutliner = true;
68 pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
70 else if ( dynamic_cast< const OutlineViewShell *>( mpViewShell ) != nullptr )
72 bOwnOutliner = false;
73 pSdOutliner = mpDoc->GetOutliner();
76 if (pSdOutliner)
77 pSdOutliner->PrepareSpelling();
80 FuHangulHanjaConversion::~FuHangulHanjaConversion()
82 if (pSdOutliner)
83 pSdOutliner->EndConversion();
85 if (bOwnOutliner)
86 delete pSdOutliner;
89 rtl::Reference<FuPoor> FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
91 rtl::Reference<FuPoor> xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
92 return xFunc;
95 /**
96 * Search and replace
98 void FuHangulHanjaConversion::StartConversion( LanguageType nSourceLanguage, LanguageType nTargetLanguage,
99 const vcl::Font *pTargetFont, sal_Int32 nOptions, bool bIsInteractive )
102 mpView->BegUndo(SdResId(STR_UNDO_HANGULHANJACONVERSION));
104 ViewShellBase* pBase = dynamic_cast<ViewShellBase*>( SfxViewShell::Current() );
105 if (pBase != nullptr)
106 mpViewShell = pBase->GetMainViewShell().get();
108 if( mpViewShell )
110 if ( pSdOutliner && dynamic_cast< const DrawViewShell *>( mpViewShell ) != nullptr && !bOwnOutliner )
112 pSdOutliner->EndConversion();
114 bOwnOutliner = true;
115 pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
116 pSdOutliner->BeginConversion();
118 else if ( pSdOutliner && dynamic_cast< const OutlineViewShell *>( mpViewShell ) != nullptr && bOwnOutliner )
120 pSdOutliner->EndConversion();
121 delete pSdOutliner;
123 bOwnOutliner = false;
124 pSdOutliner = mpDoc->GetOutliner();
125 pSdOutliner->BeginConversion();
128 if (pSdOutliner)
129 pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
132 // Due to changing between edit mode, notes mode, and handout mode the
133 // view has most likely changed. Get the new one.
134 mpViewShell = pBase ? pBase->GetMainViewShell().get() : nullptr;
135 if (mpViewShell != nullptr)
137 mpView = mpViewShell->GetView();
138 mpWindow = mpViewShell->GetActiveWindow();
140 else
142 mpView = nullptr;
143 mpWindow = nullptr;
146 if (mpView != nullptr)
147 mpView->EndUndo();
150 void FuHangulHanjaConversion::ConvertStyles( LanguageType nTargetLanguage, const vcl::Font *pTargetFont )
152 if( !mpDoc )
153 return;
155 SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
156 if( !pStyleSheetPool )
157 return;
159 SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
160 while( pStyle )
162 SfxItemSet& rSet = pStyle->GetItemSet();
164 const bool bHasParent = !pStyle->GetParent().isEmpty();
166 if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, false ) == SfxItemState::SET )
167 rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
169 if( pTargetFont &&
170 ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, false ) == SfxItemState::SET ) )
172 // set new font attribute
173 SvxFontItem aFontItem( rSet.Get( EE_CHAR_FONTINFO_CJK ) );
174 aFontItem.SetFamilyName( pTargetFont->GetFamilyName());
175 aFontItem.SetFamily( pTargetFont->GetFamilyType());
176 aFontItem.SetStyleName( pTargetFont->GetStyleName());
177 aFontItem.SetPitch( pTargetFont->GetPitch());
178 aFontItem.SetCharSet( pTargetFont->GetCharSet());
179 rSet.Put( aFontItem );
182 pStyle = pStyleSheetPool->Next();
185 mpDoc->SetLanguage( nTargetLanguage, EE_CHAR_LANGUAGE_CJK );
188 void FuHangulHanjaConversion::StartChineseConversion()
190 //open ChineseTranslationDialog
191 Reference< XComponentContext > xContext(
192 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
193 if(xContext.is())
195 Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
196 if(xMCF.is())
198 Reference< ui::dialogs::XExecutableDialog > xDialog(
199 xMCF->createInstanceWithContext("com.sun.star.linguistic2.ChineseTranslationDialog"
200 , xContext), UNO_QUERY);
201 Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
202 if( xInit.is() )
204 // initialize dialog
205 Reference< awt::XWindow > xDialogParentWindow(nullptr);
206 Sequence<Any> aSeq(comphelper::InitAnyPropertySequence(
208 {"ParentWindow", uno::Any(xDialogParentWindow)}
209 }));
210 xInit->initialize( aSeq );
212 //execute dialog
213 sal_Int16 nDialogRet = xDialog->execute();
214 if( RET_OK == nDialogRet )
216 //get some parameters from the dialog
217 bool bToSimplified = true;
218 bool bUseVariants = true;
219 bool bCommonTerms = true;
220 Reference< beans::XPropertySet > xProp( xDialog, UNO_QUERY );
221 if( xProp.is() )
225 xProp->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified;
226 xProp->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants;
227 xProp->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms;
229 catch( Exception& )
234 //execute translation
235 LanguageType nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
236 LanguageType nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
237 sal_Int32 nOptions = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
238 if( !bCommonTerms )
239 nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
241 vcl::Font aTargetFont = OutputDevice::GetDefaultFont(
242 DefaultFontType::CJK_PRESENTATION,
243 nTargetLang, GetDefaultFontFlags::OnlyOne );
245 StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, false );
246 ConvertStyles( nTargetLang, &aTargetFont );
249 Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
250 if( xComponent.is() )
251 xComponent->dispose();
255 } // end of namespace
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */