bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / func / fuhhconv.cxx
blobeea58f039c93dbd69f918bec6b399e4bc7244e34
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/XMultiComponentFactory.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <com/sun/star/awt/XWindow.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <cppuhelper/bootstrap.hxx>
29 #include <svl/style.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <editeng/langitem.hxx>
32 #include <editeng/fontitem.hxx>
34 #include <fuhhconv.hxx>
35 #include <drawdoc.hxx>
36 #include <Outliner.hxx>
37 #include <DrawViewShell.hxx>
38 #include <OutlineViewShell.hxx>
39 #include <Window.hxx>
40 #include <ViewShellBase.hxx>
42 #include <sdresid.hxx>
43 #include <strings.hrc>
45 class SfxRequest;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::uno;
51 namespace sd {
54 FuHangulHanjaConversion::FuHangulHanjaConversion (
55 ViewShell* pViewSh,
56 ::sd::Window* pWin,
57 ::sd::View* pView,
58 SdDrawDocument* pDocument,
59 SfxRequest& rReq )
60 : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
61 pSdOutliner(nullptr),
62 bOwnOutliner(false)
64 if ( dynamic_cast< const DrawViewShell *>( mpViewShell ) != nullptr )
66 bOwnOutliner = true;
67 pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
69 else if ( dynamic_cast< const OutlineViewShell *>( mpViewShell ) != nullptr )
71 bOwnOutliner = false;
72 pSdOutliner = mpDoc->GetOutliner();
75 if (pSdOutliner)
76 pSdOutliner->PrepareSpelling();
79 FuHangulHanjaConversion::~FuHangulHanjaConversion()
81 if (pSdOutliner)
82 pSdOutliner->EndConversion();
84 if (bOwnOutliner)
85 delete pSdOutliner;
88 rtl::Reference<FuPoor> FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
90 rtl::Reference<FuPoor> xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
91 return xFunc;
94 /**
95 * Search and replace
97 void FuHangulHanjaConversion::StartConversion( LanguageType nSourceLanguage, LanguageType nTargetLanguage,
98 const vcl::Font *pTargetFont, sal_Int32 nOptions, bool bIsInteractive )
101 mpView->BegUndo(SdResId(STR_UNDO_HANGULHANJACONVERSION));
103 ViewShellBase* pBase = dynamic_cast<ViewShellBase*>( SfxViewShell::Current() );
104 if (pBase != nullptr)
105 mpViewShell = pBase->GetMainViewShell().get();
107 if( mpViewShell )
109 if ( pSdOutliner && dynamic_cast< const DrawViewShell *>( mpViewShell ) != nullptr && !bOwnOutliner )
111 pSdOutliner->EndConversion();
113 bOwnOutliner = true;
114 pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
115 pSdOutliner->BeginConversion();
117 else if ( pSdOutliner && dynamic_cast< const OutlineViewShell *>( mpViewShell ) != nullptr && bOwnOutliner )
119 pSdOutliner->EndConversion();
120 delete pSdOutliner;
122 bOwnOutliner = false;
123 pSdOutliner = mpDoc->GetOutliner();
124 pSdOutliner->BeginConversion();
127 if (pSdOutliner)
128 pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
131 // Due to changing between edit mode, notes mode, and handout mode the
132 // view has most likely changed. Get the new one.
133 mpViewShell = pBase ? pBase->GetMainViewShell().get() : nullptr;
134 if (mpViewShell != nullptr)
136 mpView = mpViewShell->GetView();
137 mpWindow = mpViewShell->GetActiveWindow();
139 else
141 mpView = nullptr;
142 mpWindow = nullptr;
145 if (mpView != nullptr)
146 mpView->EndUndo();
149 void FuHangulHanjaConversion::ConvertStyles( LanguageType nTargetLanguage, const vcl::Font *pTargetFont )
151 if( !mpDoc )
152 return;
154 SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
155 if( !pStyleSheetPool )
156 return;
158 SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
159 while( pStyle )
161 SfxItemSet& rSet = pStyle->GetItemSet();
163 const bool bHasParent = !pStyle->GetParent().isEmpty();
165 if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, false ) == SfxItemState::SET )
166 rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
168 if( pTargetFont &&
169 ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, false ) == SfxItemState::SET ) )
171 // set new font attribute
172 SvxFontItem aFontItem( rSet.Get( EE_CHAR_FONTINFO_CJK ) );
173 aFontItem.SetFamilyName( pTargetFont->GetFamilyName());
174 aFontItem.SetFamily( pTargetFont->GetFamilyType());
175 aFontItem.SetStyleName( pTargetFont->GetStyleName());
176 aFontItem.SetPitch( pTargetFont->GetPitch());
177 aFontItem.SetCharSet( pTargetFont->GetCharSet());
178 rSet.Put( aFontItem );
181 pStyle = pStyleSheetPool->Next();
184 mpDoc->SetLanguage( nTargetLanguage, EE_CHAR_LANGUAGE_CJK );
187 void FuHangulHanjaConversion::StartChineseConversion()
189 //open ChineseTranslationDialog
190 Reference< XComponentContext > xContext(
191 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
192 if(!xContext.is())
193 return;
195 Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
196 if(!xMCF.is())
197 return;
199 Reference< ui::dialogs::XExecutableDialog > xDialog(
200 xMCF->createInstanceWithContext("com.sun.star.linguistic2.ChineseTranslationDialog"
201 , xContext), UNO_QUERY);
202 Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
203 if( xInit.is() )
205 // initialize dialog
206 Reference< awt::XWindow > xDialogParentWindow;
207 Sequence<Any> aSeq(comphelper::InitAnyPropertySequence(
209 {"ParentWindow", uno::Any(xDialogParentWindow)}
210 }));
211 xInit->initialize( aSeq );
213 //execute dialog
214 sal_Int16 nDialogRet = xDialog->execute();
215 if( RET_OK == nDialogRet )
217 //get some parameters from the dialog
218 bool bToSimplified = true;
219 bool bUseVariants = true;
220 bool bCommonTerms = true;
221 Reference< beans::XPropertySet > xProp( xDialog, UNO_QUERY );
222 if( xProp.is() )
226 xProp->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified;
227 xProp->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants;
228 xProp->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms;
230 catch( Exception& )
235 //execute translation
236 LanguageType nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
237 LanguageType nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
238 sal_Int32 nOptions = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
239 if( !bCommonTerms )
240 nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
242 vcl::Font aTargetFont = OutputDevice::GetDefaultFont(
243 DefaultFontType::CJK_PRESENTATION,
244 nTargetLang, GetDefaultFontFlags::OnlyOne );
246 StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, false );
247 ConvertStyles( nTargetLang, &aTargetFont );
250 Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
251 if( xComponent.is() )
252 xComponent->dispose();
254 } // end of namespace
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */