xref: /aoo42x/main/sd/source/ui/func/fuhhconv.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 #include <com/sun/star/i18n/TextConversionOption.hpp>
31 
32 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
33 #include <com/sun/star/lang/XInitialization.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <cppuhelper/bootstrap.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <svl/style.hxx>
39 #include <editeng/eeitem.hxx>
40 #include <editeng/langitem.hxx>
41 #include <editeng/fontitem.hxx>
42 
43 #include <fuhhconv.hxx>
44 #include "drawdoc.hxx"
45 #include "Outliner.hxx"
46 #include "DrawViewShell.hxx"
47 #include "OutlineViewShell.hxx"
48 #include "Window.hxx"
49 #include "ViewShellBase.hxx"
50 
51 #include "sdresid.hxx"
52 #include "strings.hrc"
53 
54 class SfxRequest;
55 
56 #define C2U(cChar)  rtl::OUString::createFromAscii(cChar)
57 
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::uno;
61 
62 namespace sd {
63 
64 class ViewShell;
65 
66 TYPEINIT1( FuHangulHanjaConversion, FuPoor );
67 
68 /*************************************************************************
69 |*
70 |* Konstruktor
71 |*
72 \************************************************************************/
73 
74 FuHangulHanjaConversion::FuHangulHanjaConversion (
75     ViewShell* pViewSh,
76     ::sd::Window* pWin,
77     ::sd::View* pView,
78     SdDrawDocument* pDocument,
79     SfxRequest& rReq )
80 	   : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
81 	pSdOutliner(NULL),
82 	bOwnOutliner(sal_False)
83 {
84 	if ( mpViewShell->ISA(DrawViewShell) )
85 	{
86 		bOwnOutliner = sal_True;
87 		pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
88 	}
89 	else if ( mpViewShell->ISA(OutlineViewShell) )
90 	{
91 		bOwnOutliner = sal_False;
92 		pSdOutliner = mpDoc->GetOutliner();
93 	}
94 
95 	if (pSdOutliner)
96 	   pSdOutliner->PrepareSpelling();
97 }
98 
99 
100 
101 /*************************************************************************
102 |*
103 |* Destruktor
104 |*
105 \************************************************************************/
106 
107 FuHangulHanjaConversion::~FuHangulHanjaConversion()
108 {
109 	if (pSdOutliner)
110 		pSdOutliner->EndConversion();
111 
112 	if (bOwnOutliner)
113 		delete pSdOutliner;
114 }
115 
116 FunctionReference FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
117 {
118 	FunctionReference xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
119 	return xFunc;
120 }
121 
122 /*************************************************************************
123 |*
124 |* Suchen&Ersetzen
125 |*
126 \************************************************************************/
127 
128 void FuHangulHanjaConversion::StartConversion( sal_Int16 nSourceLanguage, sal_Int16 nTargetLanguage,
129         const Font *pTargetFont, sal_Int32 nOptions, sal_Bool bIsInteractive )
130 {
131 
132 	String aString( SdResId(STR_UNDO_HANGULHANJACONVERSION) );
133 	mpView->BegUndo( aString );
134 
135     ViewShellBase* pBase = PTR_CAST(ViewShellBase, SfxViewShell::Current());
136     if (pBase != NULL)
137         mpViewShell = pBase->GetMainViewShell().get();
138 
139 	if( mpViewShell )
140 	{
141 		if ( pSdOutliner && mpViewShell->ISA(DrawViewShell) && !bOwnOutliner )
142 		{
143 			pSdOutliner->EndConversion();
144 
145 			bOwnOutliner = sal_True;
146 			pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
147 			pSdOutliner->BeginConversion();
148 		}
149 		else if ( pSdOutliner && mpViewShell->ISA(OutlineViewShell) && bOwnOutliner )
150 		{
151 			pSdOutliner->EndConversion();
152 			delete pSdOutliner;
153 
154 			bOwnOutliner = sal_False;
155 			pSdOutliner = mpDoc->GetOutliner();
156 			pSdOutliner->BeginConversion();
157 		}
158 
159 		if (pSdOutliner)
160             pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
161 	}
162 
163     // Due to changing between edit mode, notes mode, and handout mode the
164     // view has most likely changed.  Get the new one.
165     mpViewShell = pBase->GetMainViewShell().get();
166     if (mpViewShell != NULL)
167     {
168         mpView = mpViewShell->GetView();
169         mpWindow = mpViewShell->GetActiveWindow();
170     }
171     else
172     {
173         mpView = 0;
174         mpWindow = NULL;
175     }
176 
177     if (mpView != NULL)
178         mpView->EndUndo();
179 }
180 
181 
182 void FuHangulHanjaConversion::ConvertStyles( sal_Int16 nTargetLanguage, const Font *pTargetFont )
183 {
184 	if( !mpDoc )
185 		return;
186 
187 	SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
188 	if( !pStyleSheetPool )
189 		return;
190 
191 	SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
192 	while( pStyle )
193 	{
194 		SfxItemSet& rSet = pStyle->GetItemSet();
195 
196 		const bool bHasParent = pStyle->GetParent().Len() != 0;
197 
198 		if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, sal_False ) == SFX_ITEM_SET )
199 			rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
200 
201 		if( pTargetFont &&
202 			( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, sal_False ) == SFX_ITEM_SET ) )
203 		{
204 			// set new font attribute
205 			SvxFontItem aFontItem( (SvxFontItem&) rSet.Get( EE_CHAR_FONTINFO_CJK ) );
206             aFontItem.SetFamilyName(   pTargetFont->GetName());
207             aFontItem.SetFamily(       pTargetFont->GetFamily());
208             aFontItem.SetStyleName(    pTargetFont->GetStyleName());
209             aFontItem.SetPitch(        pTargetFont->GetPitch());
210             aFontItem.SetCharSet(      pTargetFont->GetCharSet());
211 			rSet.Put( aFontItem );
212 		}
213 
214 		pStyle = pStyleSheetPool->Next();
215 	}
216 
217 	mpDoc->SetLanguage( EE_CHAR_LANGUAGE_CJK, nTargetLanguage );
218 }
219 
220 void FuHangulHanjaConversion::StartChineseConversion()
221 {
222     //open ChineseTranslationDialog
223     Reference< XComponentContext > xContext(
224         ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
225     if(xContext.is())
226     {
227         Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
228         if(xMCF.is())
229         {
230             Reference< ui::dialogs::XExecutableDialog > xDialog(
231                     xMCF->createInstanceWithContext(
232                         rtl::OUString::createFromAscii("com.sun.star.linguistic2.ChineseTranslationDialog")
233                         , xContext), UNO_QUERY);
234             Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
235             if( xInit.is() )
236             {
237                 //  initialize dialog
238                 Reference< awt::XWindow > xDialogParentWindow(0);
239                 Sequence<Any> aSeq(1);
240                 Any* pArray = aSeq.getArray();
241                 PropertyValue aParam;
242                 aParam.Name = rtl::OUString::createFromAscii("ParentWindow");
243                 aParam.Value <<= makeAny(xDialogParentWindow);
244                 pArray[0] <<= makeAny(aParam);
245                 xInit->initialize( aSeq );
246 
247                 //execute dialog
248                 sal_Int16 nDialogRet = xDialog->execute();
249                 if( RET_OK == nDialogRet )
250                 {
251                     //get some parameters from the dialog
252                     sal_Bool bToSimplified = sal_True;
253                     sal_Bool bUseVariants = sal_True;
254                     sal_Bool bCommonTerms = sal_True;
255                     Reference< beans::XPropertySet >  xProp( xDialog, UNO_QUERY );
256                     if( xProp.is() )
257                     {
258                         try
259                         {
260                             xProp->getPropertyValue( C2U("IsDirectionToSimplified") ) >>= bToSimplified;
261                             xProp->getPropertyValue( C2U("IsUseCharacterVariants") ) >>= bUseVariants;
262                             xProp->getPropertyValue( C2U("IsTranslateCommonTerms") ) >>= bCommonTerms;
263                         }
264                         catch( Exception& )
265                         {
266                         }
267                     }
268 
269                     //execute translation
270                     sal_Int16 nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
271                     sal_Int16 nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
272                     sal_Int32 nOptions    = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
273                     if( !bCommonTerms )
274                         nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
275 
276                     Font aTargetFont = mpWindow->GetDefaultFont(
277                                         DEFAULTFONT_CJK_PRESENTATION,
278                                         nTargetLang, DEFAULTFONT_FLAGS_ONLYONE );
279 
280                     StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, sal_False );
281 					ConvertStyles( nTargetLang, &aTargetFont );
282                 }
283             }
284             Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
285             if( xComponent.is() )
286                 xComponent->dispose();
287         }
288     }
289 }
290 } // end of namespace
291