1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fuhhconv.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
33 #include <com/sun/star/i18n/TextConversionOption.hpp>
35 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <cppuhelper/bootstrap.hxx>
40 #include <vcl/msgbox.hxx>
41 #include <svtools/style.hxx>
42 #include <svx/eeitem.hxx>
43 #include <svx/langitem.hxx>
44 #include <svx/fontitem.hxx>
46 #include <fuhhconv.hxx>
47 #include "drawdoc.hxx"
48 #include "Outliner.hxx"
49 #include "DrawViewShell.hxx"
50 #include "OutlineViewShell.hxx"
52 #include "ViewShellBase.hxx"
54 #include "sdresid.hxx"
55 #include "strings.hrc"
59 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
61 using namespace ::com::sun::star
;
62 using namespace ::com::sun::star::beans
;
63 using namespace ::com::sun::star::uno
;
69 TYPEINIT1( FuHangulHanjaConversion
, FuPoor
);
71 /*************************************************************************
75 \************************************************************************/
77 FuHangulHanjaConversion::FuHangulHanjaConversion (
81 SdDrawDocument
* pDocument
,
83 : FuPoor(pViewSh
, pWin
, pView
, pDocument
, rReq
),
87 if ( mpViewShell
->ISA(DrawViewShell
) )
90 pSdOutliner
= new Outliner( mpDoc
, OUTLINERMODE_TEXTOBJECT
);
92 else if ( mpViewShell
->ISA(OutlineViewShell
) )
95 pSdOutliner
= mpDoc
->GetOutliner();
99 pSdOutliner
->PrepareSpelling();
104 /*************************************************************************
108 \************************************************************************/
110 FuHangulHanjaConversion::~FuHangulHanjaConversion()
113 pSdOutliner
->EndConversion();
119 FunctionReference
FuHangulHanjaConversion::Create( ViewShell
* pViewSh
, ::sd::Window
* pWin
, ::sd::View
* pView
, SdDrawDocument
* pDoc
, SfxRequest
& rReq
)
121 FunctionReference
xFunc( new FuHangulHanjaConversion( pViewSh
, pWin
, pView
, pDoc
, rReq
) );
125 /*************************************************************************
129 \************************************************************************/
131 void FuHangulHanjaConversion::StartConversion( INT16 nSourceLanguage
, INT16 nTargetLanguage
,
132 const Font
*pTargetFont
, INT32 nOptions
, BOOL bIsInteractive
)
135 String
aString( SdResId(STR_UNDO_HANGULHANJACONVERSION
) );
136 mpView
->BegUndo( aString
);
138 ViewShellBase
* pBase
= PTR_CAST(ViewShellBase
, SfxViewShell::Current());
140 mpViewShell
= pBase
->GetMainViewShell().get();
144 if ( pSdOutliner
&& mpViewShell
->ISA(DrawViewShell
) && !bOwnOutliner
)
146 pSdOutliner
->EndConversion();
149 pSdOutliner
= new Outliner( mpDoc
, OUTLINERMODE_TEXTOBJECT
);
150 pSdOutliner
->BeginConversion();
152 else if ( pSdOutliner
&& mpViewShell
->ISA(OutlineViewShell
) && bOwnOutliner
)
154 pSdOutliner
->EndConversion();
157 bOwnOutliner
= FALSE
;
158 pSdOutliner
= mpDoc
->GetOutliner();
159 pSdOutliner
->BeginConversion();
163 pSdOutliner
->StartConversion(nSourceLanguage
, nTargetLanguage
, pTargetFont
, nOptions
, bIsInteractive
);
166 // Due to changing between edit mode, notes mode, and handout mode the
167 // view has most likely changed. Get the new one.
168 mpViewShell
= pBase
->GetMainViewShell().get();
169 if (mpViewShell
!= NULL
)
171 mpView
= mpViewShell
->GetView();
172 mpWindow
= mpViewShell
->GetActiveWindow();
185 void FuHangulHanjaConversion::ConvertStyles( INT16 nTargetLanguage
, const Font
*pTargetFont
)
190 SfxStyleSheetBasePool
* pStyleSheetPool
= mpDoc
->GetStyleSheetPool();
191 if( !pStyleSheetPool
)
194 SfxStyleSheetBase
* pStyle
= pStyleSheetPool
->First();
197 SfxItemSet
& rSet
= pStyle
->GetItemSet();
199 const bool bHasParent
= pStyle
->GetParent().Len() != 0;
201 if( !bHasParent
|| rSet
.GetItemState( EE_CHAR_LANGUAGE_CJK
, FALSE
) == SFX_ITEM_SET
)
202 rSet
.Put( SvxLanguageItem( nTargetLanguage
, EE_CHAR_LANGUAGE_CJK
) );
205 ( !bHasParent
|| rSet
.GetItemState( EE_CHAR_FONTINFO_CJK
, FALSE
) == SFX_ITEM_SET
) )
207 // set new font attribute
208 SvxFontItem
aFontItem( (SvxFontItem
&) rSet
.Get( EE_CHAR_FONTINFO_CJK
) );
209 aFontItem
.GetFamilyName() = pTargetFont
->GetName();
210 aFontItem
.GetFamily() = pTargetFont
->GetFamily();
211 aFontItem
.GetStyleName() = pTargetFont
->GetStyleName();
212 aFontItem
.GetPitch() = pTargetFont
->GetPitch();
213 aFontItem
.GetCharSet() = pTargetFont
->GetCharSet();
214 rSet
.Put( aFontItem
);
217 pStyle
= pStyleSheetPool
->Next();
220 mpDoc
->SetLanguage( EE_CHAR_LANGUAGE_CJK
, nTargetLanguage
);
223 void FuHangulHanjaConversion::StartChineseConversion()
225 //open ChineseTranslationDialog
226 Reference
< XComponentContext
> xContext(
227 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
230 Reference
< lang::XMultiComponentFactory
> xMCF( xContext
->getServiceManager() );
233 Reference
< ui::dialogs::XExecutableDialog
> xDialog(
234 xMCF
->createInstanceWithContext(
235 rtl::OUString::createFromAscii("com.sun.star.linguistic2.ChineseTranslationDialog")
236 , xContext
), UNO_QUERY
);
237 Reference
< lang::XInitialization
> xInit( xDialog
, UNO_QUERY
);
241 Reference
< awt::XWindow
> xDialogParentWindow(0);
242 Sequence
<Any
> aSeq(1);
243 Any
* pArray
= aSeq
.getArray();
244 PropertyValue aParam
;
245 aParam
.Name
= rtl::OUString::createFromAscii("ParentWindow");
246 aParam
.Value
<<= makeAny(xDialogParentWindow
);
247 pArray
[0] <<= makeAny(aParam
);
248 xInit
->initialize( aSeq
);
251 sal_Int16 nDialogRet
= xDialog
->execute();
252 if( RET_OK
== nDialogRet
)
254 //get some parameters from the dialog
255 sal_Bool bToSimplified
= sal_True
;
256 sal_Bool bUseVariants
= sal_True
;
257 sal_Bool bCommonTerms
= sal_True
;
258 Reference
< beans::XPropertySet
> xProp( xDialog
, UNO_QUERY
);
263 xProp
->getPropertyValue( C2U("IsDirectionToSimplified") ) >>= bToSimplified
;
264 xProp
->getPropertyValue( C2U("IsUseCharacterVariants") ) >>= bUseVariants
;
265 xProp
->getPropertyValue( C2U("IsTranslateCommonTerms") ) >>= bCommonTerms
;
272 //execute translation
273 sal_Int16 nSourceLang
= bToSimplified
? LANGUAGE_CHINESE_TRADITIONAL
: LANGUAGE_CHINESE_SIMPLIFIED
;
274 sal_Int16 nTargetLang
= bToSimplified
? LANGUAGE_CHINESE_SIMPLIFIED
: LANGUAGE_CHINESE_TRADITIONAL
;
275 sal_Int32 nOptions
= bUseVariants
? i18n::TextConversionOption::USE_CHARACTER_VARIANTS
: 0;
277 nOptions
= nOptions
| i18n::TextConversionOption::CHARACTER_BY_CHARACTER
;
279 Font aTargetFont
= mpWindow
->GetDefaultFont(
280 DEFAULTFONT_CJK_PRESENTATION
,
281 nTargetLang
, DEFAULTFONT_FLAGS_ONLYONE
);
283 StartConversion( nSourceLang
, nTargetLang
, &aTargetFont
, nOptions
, sal_False
);
284 ConvertStyles( nTargetLang
, &aTargetFont
);
287 Reference
< lang::XComponent
> xComponent( xDialog
, UNO_QUERY
);
288 if( xComponent
.is() )
289 xComponent
->dispose();
293 } // end of namespace