bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / func / fuhhconv.cxx
bloba8331942f3c972ef0069204e275818cad0a13947
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/i18n/TextConversionOption.hpp>
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>
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"
41 #include "sdresid.hxx"
42 #include "strings.hrc"
44 class SfxRequest;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::uno;
50 namespace sd {
52 class ViewShell;
54 TYPEINIT1( FuHangulHanjaConversion, FuPoor );
56 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 bOwnOutliner(false)
66 if ( mpViewShell->ISA(DrawViewShell) )
68 bOwnOutliner = true;
69 pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
71 else if ( mpViewShell->ISA(OutlineViewShell) )
73 bOwnOutliner = false;
74 pSdOutliner = mpDoc->GetOutliner();
77 if (pSdOutliner)
78 pSdOutliner->PrepareSpelling();
81 FuHangulHanjaConversion::~FuHangulHanjaConversion()
83 if (pSdOutliner)
84 pSdOutliner->EndConversion();
86 if (bOwnOutliner)
87 delete pSdOutliner;
90 rtl::Reference<FuPoor> FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
92 rtl::Reference<FuPoor> xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
93 return xFunc;
96 /**
97 * Search and replace
99 void FuHangulHanjaConversion::StartConversion( sal_Int16 nSourceLanguage, sal_Int16 nTargetLanguage,
100 const vcl::Font *pTargetFont, sal_Int32 nOptions, bool bIsInteractive )
103 mpView->BegUndo(SD_RESSTR(STR_UNDO_HANGULHANJACONVERSION));
105 ViewShellBase* pBase = PTR_CAST(ViewShellBase, SfxViewShell::Current());
106 if (pBase != NULL)
107 mpViewShell = pBase->GetMainViewShell().get();
109 if( mpViewShell )
111 if ( pSdOutliner && mpViewShell->ISA(DrawViewShell) && !bOwnOutliner )
113 pSdOutliner->EndConversion();
115 bOwnOutliner = true;
116 pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
117 pSdOutliner->BeginConversion();
119 else if ( pSdOutliner && mpViewShell->ISA(OutlineViewShell) && bOwnOutliner )
121 pSdOutliner->EndConversion();
122 delete pSdOutliner;
124 bOwnOutliner = false;
125 pSdOutliner = mpDoc->GetOutliner();
126 pSdOutliner->BeginConversion();
129 if (pSdOutliner)
130 pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
133 // Due to changing between edit mode, notes mode, and handout mode the
134 // view has most likely changed. Get the new one.
135 mpViewShell = pBase ? pBase->GetMainViewShell().get() : NULL;
136 if (mpViewShell != NULL)
138 mpView = mpViewShell->GetView();
139 mpWindow = mpViewShell->GetActiveWindow();
141 else
143 mpView = 0;
144 mpWindow = NULL;
147 if (mpView != NULL)
148 mpView->EndUndo();
151 void FuHangulHanjaConversion::ConvertStyles( sal_Int16 nTargetLanguage, const vcl::Font *pTargetFont )
153 if( !mpDoc )
154 return;
156 SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
157 if( !pStyleSheetPool )
158 return;
160 SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
161 while( pStyle )
163 SfxItemSet& rSet = pStyle->GetItemSet();
165 const bool bHasParent = !pStyle->GetParent().isEmpty();
167 if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, false ) == SfxItemState::SET )
168 rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
170 if( pTargetFont &&
171 ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, false ) == SfxItemState::SET ) )
173 // set new font attribute
174 SvxFontItem aFontItem( static_cast<const SvxFontItem&>( rSet.Get( EE_CHAR_FONTINFO_CJK ) ) );
175 aFontItem.SetFamilyName( pTargetFont->GetName());
176 aFontItem.SetFamily( pTargetFont->GetFamily());
177 aFontItem.SetStyleName( pTargetFont->GetStyleName());
178 aFontItem.SetPitch( pTargetFont->GetPitch());
179 aFontItem.SetCharSet( pTargetFont->GetCharSet());
180 rSet.Put( aFontItem );
183 pStyle = pStyleSheetPool->Next();
186 mpDoc->SetLanguage( EE_CHAR_LANGUAGE_CJK, nTargetLanguage );
189 void FuHangulHanjaConversion::StartChineseConversion()
191 //open ChineseTranslationDialog
192 Reference< XComponentContext > xContext(
193 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
194 if(xContext.is())
196 Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
197 if(xMCF.is())
199 Reference< ui::dialogs::XExecutableDialog > xDialog(
200 xMCF->createInstanceWithContext("com.sun.star.linguistic2.ChineseTranslationDialog"
201 , xContext), UNO_QUERY);
202 Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
203 if( xInit.is() )
205 // initialize dialog
206 Reference< awt::XWindow > xDialogParentWindow(0);
207 Sequence<Any> aSeq(1);
208 Any* pArray = aSeq.getArray();
209 PropertyValue aParam;
210 aParam.Name = "ParentWindow";
211 aParam.Value <<= makeAny(xDialogParentWindow);
212 pArray[0] <<= makeAny(aParam);
213 xInit->initialize( aSeq );
215 //execute dialog
216 sal_Int16 nDialogRet = xDialog->execute();
217 if( RET_OK == nDialogRet )
219 //get some parameters from the dialog
220 bool bToSimplified = true;
221 bool bUseVariants = true;
222 bool bCommonTerms = true;
223 Reference< beans::XPropertySet > xProp( xDialog, UNO_QUERY );
224 if( xProp.is() )
228 xProp->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified;
229 xProp->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants;
230 xProp->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms;
232 catch( Exception& )
237 //execute translation
238 sal_Int16 nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
239 sal_Int16 nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
240 sal_Int32 nOptions = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
241 if( !bCommonTerms )
242 nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
244 vcl::Font aTargetFont = OutputDevice::GetDefaultFont(
245 DefaultFontType::CJK_PRESENTATION,
246 nTargetLang, GetDefaultFontFlags::OnlyOne );
248 StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, false );
249 ConvertStyles( nTargetLang, &aTargetFont );
252 Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
253 if( xComponent.is() )
254 xComponent->dispose();
258 } // end of namespace
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */