Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/i18n/TextConversionOption.hpp>
21 :
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>
32 :
33 : #include <fuhhconv.hxx>
34 : #include "drawdoc.hxx"
35 : #include "Outliner.hxx"
36 : #include "DrawViewShell.hxx"
37 : #include "OutlineViewShell.hxx"
38 : #include "Window.hxx"
39 : #include "ViewShellBase.hxx"
40 :
41 : #include "sdresid.hxx"
42 : #include "strings.hrc"
43 :
44 : class SfxRequest;
45 :
46 :
47 : using namespace ::com::sun::star;
48 : using namespace ::com::sun::star::beans;
49 : using namespace ::com::sun::star::uno;
50 :
51 : namespace sd {
52 :
53 : class ViewShell;
54 :
55 0 : TYPEINIT1( FuHangulHanjaConversion, FuPoor );
56 :
57 : /*************************************************************************
58 : |*
59 : |* Konstruktor
60 : |*
61 : \************************************************************************/
62 :
63 0 : FuHangulHanjaConversion::FuHangulHanjaConversion (
64 : ViewShell* pViewSh,
65 : ::sd::Window* pWin,
66 : ::sd::View* pView,
67 : SdDrawDocument* pDocument,
68 : SfxRequest& rReq )
69 : : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
70 : pSdOutliner(NULL),
71 0 : bOwnOutliner(sal_False)
72 : {
73 0 : if ( mpViewShell->ISA(DrawViewShell) )
74 : {
75 0 : bOwnOutliner = sal_True;
76 0 : pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
77 : }
78 0 : else if ( mpViewShell->ISA(OutlineViewShell) )
79 : {
80 0 : bOwnOutliner = sal_False;
81 0 : pSdOutliner = mpDoc->GetOutliner();
82 : }
83 :
84 0 : if (pSdOutliner)
85 0 : pSdOutliner->PrepareSpelling();
86 0 : }
87 :
88 :
89 :
90 : /*************************************************************************
91 : |*
92 : |* Destruktor
93 : |*
94 : \************************************************************************/
95 :
96 0 : FuHangulHanjaConversion::~FuHangulHanjaConversion()
97 : {
98 0 : if (pSdOutliner)
99 0 : pSdOutliner->EndConversion();
100 :
101 0 : if (bOwnOutliner)
102 0 : delete pSdOutliner;
103 0 : }
104 :
105 0 : FunctionReference FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
106 : {
107 0 : FunctionReference xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
108 0 : return xFunc;
109 : }
110 :
111 : /*************************************************************************
112 : |*
113 : |* Suchen&Ersetzen
114 : |*
115 : \************************************************************************/
116 :
117 0 : void FuHangulHanjaConversion::StartConversion( sal_Int16 nSourceLanguage, sal_Int16 nTargetLanguage,
118 : const Font *pTargetFont, sal_Int32 nOptions, sal_Bool bIsInteractive )
119 : {
120 :
121 0 : String aString( SdResId(STR_UNDO_HANGULHANJACONVERSION) );
122 0 : mpView->BegUndo( aString );
123 :
124 0 : ViewShellBase* pBase = PTR_CAST(ViewShellBase, SfxViewShell::Current());
125 0 : if (pBase != NULL)
126 0 : mpViewShell = pBase->GetMainViewShell().get();
127 :
128 0 : if( mpViewShell )
129 : {
130 0 : if ( pSdOutliner && mpViewShell->ISA(DrawViewShell) && !bOwnOutliner )
131 : {
132 0 : pSdOutliner->EndConversion();
133 :
134 0 : bOwnOutliner = sal_True;
135 0 : pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
136 0 : pSdOutliner->BeginConversion();
137 : }
138 0 : else if ( pSdOutliner && mpViewShell->ISA(OutlineViewShell) && bOwnOutliner )
139 : {
140 0 : pSdOutliner->EndConversion();
141 0 : delete pSdOutliner;
142 :
143 0 : bOwnOutliner = sal_False;
144 0 : pSdOutliner = mpDoc->GetOutliner();
145 0 : pSdOutliner->BeginConversion();
146 : }
147 :
148 0 : if (pSdOutliner)
149 0 : pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
150 : }
151 :
152 : // Due to changing between edit mode, notes mode, and handout mode the
153 : // view has most likely changed. Get the new one.
154 0 : mpViewShell = pBase->GetMainViewShell().get();
155 0 : if (mpViewShell != NULL)
156 : {
157 0 : mpView = mpViewShell->GetView();
158 0 : mpWindow = mpViewShell->GetActiveWindow();
159 : }
160 : else
161 : {
162 0 : mpView = 0;
163 0 : mpWindow = NULL;
164 : }
165 :
166 0 : if (mpView != NULL)
167 0 : mpView->EndUndo();
168 0 : }
169 :
170 :
171 0 : void FuHangulHanjaConversion::ConvertStyles( sal_Int16 nTargetLanguage, const Font *pTargetFont )
172 : {
173 0 : if( !mpDoc )
174 0 : return;
175 :
176 0 : SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
177 0 : if( !pStyleSheetPool )
178 0 : return;
179 :
180 0 : SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
181 0 : while( pStyle )
182 : {
183 0 : SfxItemSet& rSet = pStyle->GetItemSet();
184 :
185 0 : const bool bHasParent = pStyle->GetParent().Len() != 0;
186 :
187 0 : if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, sal_False ) == SFX_ITEM_SET )
188 0 : rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
189 :
190 0 : if( pTargetFont &&
191 0 : ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, sal_False ) == SFX_ITEM_SET ) )
192 : {
193 : // set new font attribute
194 0 : SvxFontItem aFontItem( (SvxFontItem&) rSet.Get( EE_CHAR_FONTINFO_CJK ) );
195 0 : aFontItem.SetFamilyName( pTargetFont->GetName());
196 0 : aFontItem.SetFamily( pTargetFont->GetFamily());
197 0 : aFontItem.SetStyleName( pTargetFont->GetStyleName());
198 0 : aFontItem.SetPitch( pTargetFont->GetPitch());
199 0 : aFontItem.SetCharSet( pTargetFont->GetCharSet());
200 0 : rSet.Put( aFontItem );
201 : }
202 :
203 0 : pStyle = pStyleSheetPool->Next();
204 : }
205 :
206 0 : mpDoc->SetLanguage( EE_CHAR_LANGUAGE_CJK, nTargetLanguage );
207 : }
208 :
209 0 : void FuHangulHanjaConversion::StartChineseConversion()
210 : {
211 : //open ChineseTranslationDialog
212 : Reference< XComponentContext > xContext(
213 0 : ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
214 0 : if(xContext.is())
215 : {
216 0 : Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
217 0 : if(xMCF.is())
218 : {
219 : Reference< ui::dialogs::XExecutableDialog > xDialog(
220 0 : xMCF->createInstanceWithContext("com.sun.star.linguistic2.ChineseTranslationDialog"
221 0 : , xContext), UNO_QUERY);
222 0 : Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
223 0 : if( xInit.is() )
224 : {
225 : // initialize dialog
226 0 : Reference< awt::XWindow > xDialogParentWindow(0);
227 0 : Sequence<Any> aSeq(1);
228 0 : Any* pArray = aSeq.getArray();
229 0 : PropertyValue aParam;
230 0 : aParam.Name = "ParentWindow";
231 0 : aParam.Value <<= makeAny(xDialogParentWindow);
232 0 : pArray[0] <<= makeAny(aParam);
233 0 : xInit->initialize( aSeq );
234 :
235 : //execute dialog
236 0 : sal_Int16 nDialogRet = xDialog->execute();
237 0 : if( RET_OK == nDialogRet )
238 : {
239 : //get some parameters from the dialog
240 0 : sal_Bool bToSimplified = sal_True;
241 0 : sal_Bool bUseVariants = sal_True;
242 0 : sal_Bool bCommonTerms = sal_True;
243 0 : Reference< beans::XPropertySet > xProp( xDialog, UNO_QUERY );
244 0 : if( xProp.is() )
245 : {
246 : try
247 : {
248 0 : xProp->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified;
249 0 : xProp->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants;
250 0 : xProp->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms;
251 : }
252 0 : catch( Exception& )
253 : {
254 : }
255 : }
256 :
257 : //execute translation
258 0 : sal_Int16 nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
259 0 : sal_Int16 nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
260 0 : sal_Int32 nOptions = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
261 0 : if( !bCommonTerms )
262 0 : nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
263 :
264 : Font aTargetFont = mpWindow->GetDefaultFont(
265 : DEFAULTFONT_CJK_PRESENTATION,
266 0 : nTargetLang, DEFAULTFONT_FLAGS_ONLYONE );
267 :
268 0 : StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, sal_False );
269 0 : ConvertStyles( nTargetLang, &aTargetFont );
270 0 : }
271 : }
272 0 : Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
273 0 : if( xComponent.is() )
274 0 : xComponent->dispose();
275 0 : }
276 0 : }
277 0 : }
278 : } // end of namespace
279 :
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|