1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
39 #include <ViewShellBase.hxx>
41 #include <sdresid.hxx>
42 #include <strings.hrc>
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::uno
;
55 FuHangulHanjaConversion::FuHangulHanjaConversion (
59 SdDrawDocument
* pDocument
,
61 : FuPoor(pViewSh
, pWin
, pView
, pDocument
, rReq
),
65 if ( dynamic_cast< const DrawViewShell
*>( mpViewShell
) != nullptr )
68 pSdOutliner
= new SdOutliner( mpDoc
, OutlinerMode::TextObject
);
70 else if ( dynamic_cast< const OutlineViewShell
*>( mpViewShell
) != nullptr )
73 pSdOutliner
= mpDoc
->GetOutliner();
77 pSdOutliner
->PrepareSpelling();
80 FuHangulHanjaConversion::~FuHangulHanjaConversion()
83 pSdOutliner
->EndConversion();
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
) );
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();
110 if ( pSdOutliner
&& dynamic_cast< const DrawViewShell
*>( mpViewShell
) != nullptr && !bOwnOutliner
)
112 pSdOutliner
->EndConversion();
115 pSdOutliner
= new SdOutliner( mpDoc
, OutlinerMode::TextObject
);
116 pSdOutliner
->BeginConversion();
118 else if ( pSdOutliner
&& dynamic_cast< const OutlineViewShell
*>( mpViewShell
) != nullptr && bOwnOutliner
)
120 pSdOutliner
->EndConversion();
123 bOwnOutliner
= false;
124 pSdOutliner
= mpDoc
->GetOutliner();
125 pSdOutliner
->BeginConversion();
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();
146 if (mpView
!= nullptr)
150 void FuHangulHanjaConversion::ConvertStyles( LanguageType nTargetLanguage
, const vcl::Font
*pTargetFont
)
155 SfxStyleSheetBasePool
* pStyleSheetPool
= mpDoc
->GetStyleSheetPool();
156 if( !pStyleSheetPool
)
159 SfxStyleSheetBase
* pStyle
= pStyleSheetPool
->First();
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
) );
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
195 Reference
< lang::XMultiComponentFactory
> xMCF( xContext
->getServiceManager() );
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
);
205 Reference
< awt::XWindow
> xDialogParentWindow(nullptr);
206 Sequence
<Any
> aSeq(comphelper::InitAnyPropertySequence(
208 {"ParentWindow", uno::Any(xDialogParentWindow
)}
210 xInit
->initialize( aSeq
);
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
);
225 xProp
->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified
;
226 xProp
->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants
;
227 xProp
->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms
;
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;
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: */