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