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/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>
40 #include <ViewShellBase.hxx>
42 #include <sdresid.hxx>
43 #include <strings.hrc>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::uno
;
54 FuHangulHanjaConversion::FuHangulHanjaConversion (
58 SdDrawDocument
* pDocument
,
60 : FuPoor(pViewSh
, pWin
, pView
, pDocument
, rReq
),
64 if ( dynamic_cast< const DrawViewShell
*>( mpViewShell
) != nullptr )
67 pSdOutliner
= new SdOutliner( mpDoc
, OutlinerMode::TextObject
);
69 else if ( dynamic_cast< const OutlineViewShell
*>( mpViewShell
) != nullptr )
72 pSdOutliner
= mpDoc
->GetOutliner();
76 pSdOutliner
->PrepareSpelling();
79 FuHangulHanjaConversion::~FuHangulHanjaConversion()
82 pSdOutliner
->EndConversion();
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
) );
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();
109 if ( pSdOutliner
&& dynamic_cast< const DrawViewShell
*>( mpViewShell
) != nullptr && !bOwnOutliner
)
111 pSdOutliner
->EndConversion();
114 pSdOutliner
= new SdOutliner( mpDoc
, OutlinerMode::TextObject
);
115 pSdOutliner
->BeginConversion();
117 else if ( pSdOutliner
&& dynamic_cast< const OutlineViewShell
*>( mpViewShell
) != nullptr && bOwnOutliner
)
119 pSdOutliner
->EndConversion();
122 bOwnOutliner
= false;
123 pSdOutliner
= mpDoc
->GetOutliner();
124 pSdOutliner
->BeginConversion();
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();
145 if (mpView
!= nullptr)
149 void FuHangulHanjaConversion::ConvertStyles( LanguageType nTargetLanguage
, const vcl::Font
*pTargetFont
)
154 SfxStyleSheetBasePool
* pStyleSheetPool
= mpDoc
->GetStyleSheetPool();
155 if( !pStyleSheetPool
)
158 SfxStyleSheetBase
* pStyle
= pStyleSheetPool
->First();
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
) );
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
195 Reference
< lang::XMultiComponentFactory
> xMCF( xContext
->getServiceManager() );
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
);
206 Reference
< awt::XWindow
> xDialogParentWindow
;
207 Sequence
<Any
> aSeq(comphelper::InitAnyPropertySequence(
209 {"ParentWindow", uno::Any(xDialogParentWindow
)}
211 xInit
->initialize( aSeq
);
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
);
226 xProp
->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified
;
227 xProp
->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants
;
228 xProp
->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms
;
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;
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: */