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 <cppuhelper/bootstrap.hxx>
27 #include <vcl/msgbox.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
;
54 TYPEINIT1( FuHangulHanjaConversion
, FuPoor
);
56 FuHangulHanjaConversion::FuHangulHanjaConversion (
60 SdDrawDocument
* pDocument
,
62 : FuPoor(pViewSh
, pWin
, pView
, pDocument
, rReq
),
66 if ( mpViewShell
->ISA(DrawViewShell
) )
69 pSdOutliner
= new Outliner( mpDoc
, OUTLINERMODE_TEXTOBJECT
);
71 else if ( mpViewShell
->ISA(OutlineViewShell
) )
74 pSdOutliner
= mpDoc
->GetOutliner();
78 pSdOutliner
->PrepareSpelling();
81 FuHangulHanjaConversion::~FuHangulHanjaConversion()
84 pSdOutliner
->EndConversion();
90 rtl::Reference
<FuPoor
> FuHangulHanjaConversion::Create( ViewShell
* pViewSh
, ::sd::Window
* pWin
, ::sd::View
* pView
, SdDrawDocument
* pDoc
, SfxRequest
& rReq
)
92 rtl::Reference
<FuPoor
> xFunc( new FuHangulHanjaConversion( pViewSh
, pWin
, pView
, pDoc
, rReq
) );
99 void FuHangulHanjaConversion::StartConversion( sal_Int16 nSourceLanguage
, sal_Int16 nTargetLanguage
,
100 const vcl::Font
*pTargetFont
, sal_Int32 nOptions
, bool bIsInteractive
)
103 mpView
->BegUndo(SD_RESSTR(STR_UNDO_HANGULHANJACONVERSION
));
105 ViewShellBase
* pBase
= PTR_CAST(ViewShellBase
, SfxViewShell::Current());
107 mpViewShell
= pBase
->GetMainViewShell().get();
111 if ( pSdOutliner
&& mpViewShell
->ISA(DrawViewShell
) && !bOwnOutliner
)
113 pSdOutliner
->EndConversion();
116 pSdOutliner
= new Outliner( mpDoc
, OUTLINERMODE_TEXTOBJECT
);
117 pSdOutliner
->BeginConversion();
119 else if ( pSdOutliner
&& mpViewShell
->ISA(OutlineViewShell
) && bOwnOutliner
)
121 pSdOutliner
->EndConversion();
124 bOwnOutliner
= false;
125 pSdOutliner
= mpDoc
->GetOutliner();
126 pSdOutliner
->BeginConversion();
130 pSdOutliner
->StartConversion(nSourceLanguage
, nTargetLanguage
, pTargetFont
, nOptions
, bIsInteractive
);
133 // Due to changing between edit mode, notes mode, and handout mode the
134 // view has most likely changed. Get the new one.
135 mpViewShell
= pBase
? pBase
->GetMainViewShell().get() : NULL
;
136 if (mpViewShell
!= NULL
)
138 mpView
= mpViewShell
->GetView();
139 mpWindow
= mpViewShell
->GetActiveWindow();
151 void FuHangulHanjaConversion::ConvertStyles( sal_Int16 nTargetLanguage
, const vcl::Font
*pTargetFont
)
156 SfxStyleSheetBasePool
* pStyleSheetPool
= mpDoc
->GetStyleSheetPool();
157 if( !pStyleSheetPool
)
160 SfxStyleSheetBase
* pStyle
= pStyleSheetPool
->First();
163 SfxItemSet
& rSet
= pStyle
->GetItemSet();
165 const bool bHasParent
= !pStyle
->GetParent().isEmpty();
167 if( !bHasParent
|| rSet
.GetItemState( EE_CHAR_LANGUAGE_CJK
, false ) == SfxItemState::SET
)
168 rSet
.Put( SvxLanguageItem( nTargetLanguage
, EE_CHAR_LANGUAGE_CJK
) );
171 ( !bHasParent
|| rSet
.GetItemState( EE_CHAR_FONTINFO_CJK
, false ) == SfxItemState::SET
) )
173 // set new font attribute
174 SvxFontItem
aFontItem( static_cast<const SvxFontItem
&>( rSet
.Get( EE_CHAR_FONTINFO_CJK
) ) );
175 aFontItem
.SetFamilyName( pTargetFont
->GetName());
176 aFontItem
.SetFamily( pTargetFont
->GetFamily());
177 aFontItem
.SetStyleName( pTargetFont
->GetStyleName());
178 aFontItem
.SetPitch( pTargetFont
->GetPitch());
179 aFontItem
.SetCharSet( pTargetFont
->GetCharSet());
180 rSet
.Put( aFontItem
);
183 pStyle
= pStyleSheetPool
->Next();
186 mpDoc
->SetLanguage( EE_CHAR_LANGUAGE_CJK
, nTargetLanguage
);
189 void FuHangulHanjaConversion::StartChineseConversion()
191 //open ChineseTranslationDialog
192 Reference
< XComponentContext
> xContext(
193 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
196 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(0);
207 Sequence
<Any
> aSeq(1);
208 Any
* pArray
= aSeq
.getArray();
209 PropertyValue aParam
;
210 aParam
.Name
= "ParentWindow";
211 aParam
.Value
<<= makeAny(xDialogParentWindow
);
212 pArray
[0] <<= makeAny(aParam
);
213 xInit
->initialize( aSeq
);
216 sal_Int16 nDialogRet
= xDialog
->execute();
217 if( RET_OK
== nDialogRet
)
219 //get some parameters from the dialog
220 bool bToSimplified
= true;
221 bool bUseVariants
= true;
222 bool bCommonTerms
= true;
223 Reference
< beans::XPropertySet
> xProp( xDialog
, UNO_QUERY
);
228 xProp
->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified
;
229 xProp
->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants
;
230 xProp
->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms
;
237 //execute translation
238 sal_Int16 nSourceLang
= bToSimplified
? LANGUAGE_CHINESE_TRADITIONAL
: LANGUAGE_CHINESE_SIMPLIFIED
;
239 sal_Int16 nTargetLang
= bToSimplified
? LANGUAGE_CHINESE_SIMPLIFIED
: LANGUAGE_CHINESE_TRADITIONAL
;
240 sal_Int32 nOptions
= bUseVariants
? i18n::TextConversionOption::USE_CHARACTER_VARIANTS
: 0;
242 nOptions
= nOptions
| i18n::TextConversionOption::CHARACTER_BY_CHARACTER
;
244 vcl::Font aTargetFont
= OutputDevice::GetDefaultFont(
245 DefaultFontType::CJK_PRESENTATION
,
246 nTargetLang
, GetDefaultFontFlags::OnlyOne
);
248 StartConversion( nSourceLang
, nTargetLang
, &aTargetFont
, nOptions
, false );
249 ConvertStyles( nTargetLang
, &aTargetFont
);
252 Reference
< lang::XComponent
> xComponent( xDialog
, UNO_QUERY
);
253 if( xComponent
.is() )
254 xComponent
->dispose();
258 } // end of namespace
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */