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