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