merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / func / fuhhconv.cxx
blob5005aac455becd81d4836cb1f4285fbccb820019
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fuhhconv.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
33 #include <com/sun/star/i18n/TextConversionOption.hpp>
35 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <cppuhelper/bootstrap.hxx>
40 #include <vcl/msgbox.hxx>
41 #include <svtools/style.hxx>
42 #include <svx/eeitem.hxx>
43 #include <svx/langitem.hxx>
44 #include <svx/fontitem.hxx>
46 #include <fuhhconv.hxx>
47 #include "drawdoc.hxx"
48 #include "Outliner.hxx"
49 #include "DrawViewShell.hxx"
50 #include "OutlineViewShell.hxx"
51 #include "Window.hxx"
52 #include "ViewShellBase.hxx"
54 #include "sdresid.hxx"
55 #include "strings.hrc"
57 class SfxRequest;
59 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
61 using namespace ::com::sun::star;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::uno;
65 namespace sd {
67 class ViewShell;
69 TYPEINIT1( FuHangulHanjaConversion, FuPoor );
71 /*************************************************************************
73 |* Konstruktor
75 \************************************************************************/
77 FuHangulHanjaConversion::FuHangulHanjaConversion (
78 ViewShell* pViewSh,
79 ::sd::Window* pWin,
80 ::sd::View* pView,
81 SdDrawDocument* pDocument,
82 SfxRequest& rReq )
83 : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
84 pSdOutliner(NULL),
85 bOwnOutliner(FALSE)
87 if ( mpViewShell->ISA(DrawViewShell) )
89 bOwnOutliner = TRUE;
90 pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
92 else if ( mpViewShell->ISA(OutlineViewShell) )
94 bOwnOutliner = FALSE;
95 pSdOutliner = mpDoc->GetOutliner();
98 if (pSdOutliner)
99 pSdOutliner->PrepareSpelling();
104 /*************************************************************************
106 |* Destruktor
108 \************************************************************************/
110 FuHangulHanjaConversion::~FuHangulHanjaConversion()
112 if (pSdOutliner)
113 pSdOutliner->EndConversion();
115 if (bOwnOutliner)
116 delete pSdOutliner;
119 FunctionReference FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
121 FunctionReference xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
122 return xFunc;
125 /*************************************************************************
127 |* Suchen&Ersetzen
129 \************************************************************************/
131 void FuHangulHanjaConversion::StartConversion( INT16 nSourceLanguage, INT16 nTargetLanguage,
132 const Font *pTargetFont, INT32 nOptions, BOOL bIsInteractive )
135 String aString( SdResId(STR_UNDO_HANGULHANJACONVERSION) );
136 mpView->BegUndo( aString );
138 ViewShellBase* pBase = PTR_CAST(ViewShellBase, SfxViewShell::Current());
139 if (pBase != NULL)
140 mpViewShell = pBase->GetMainViewShell().get();
142 if( mpViewShell )
144 if ( pSdOutliner && mpViewShell->ISA(DrawViewShell) && !bOwnOutliner )
146 pSdOutliner->EndConversion();
148 bOwnOutliner = TRUE;
149 pSdOutliner = new Outliner( mpDoc, OUTLINERMODE_TEXTOBJECT );
150 pSdOutliner->BeginConversion();
152 else if ( pSdOutliner && mpViewShell->ISA(OutlineViewShell) && bOwnOutliner )
154 pSdOutliner->EndConversion();
155 delete pSdOutliner;
157 bOwnOutliner = FALSE;
158 pSdOutliner = mpDoc->GetOutliner();
159 pSdOutliner->BeginConversion();
162 if (pSdOutliner)
163 pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
166 // Due to changing between edit mode, notes mode, and handout mode the
167 // view has most likely changed. Get the new one.
168 mpViewShell = pBase->GetMainViewShell().get();
169 if (mpViewShell != NULL)
171 mpView = mpViewShell->GetView();
172 mpWindow = mpViewShell->GetActiveWindow();
174 else
176 mpView = 0;
177 mpWindow = NULL;
180 if (mpView != NULL)
181 mpView->EndUndo();
185 void FuHangulHanjaConversion::ConvertStyles( INT16 nTargetLanguage, const Font *pTargetFont )
187 if( !mpDoc )
188 return;
190 SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
191 if( !pStyleSheetPool )
192 return;
194 SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
195 while( pStyle )
197 SfxItemSet& rSet = pStyle->GetItemSet();
199 const bool bHasParent = pStyle->GetParent().Len() != 0;
201 if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, FALSE ) == SFX_ITEM_SET )
202 rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
204 if( pTargetFont &&
205 ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, FALSE ) == SFX_ITEM_SET ) )
207 // set new font attribute
208 SvxFontItem aFontItem( (SvxFontItem&) rSet.Get( EE_CHAR_FONTINFO_CJK ) );
209 aFontItem.GetFamilyName() = pTargetFont->GetName();
210 aFontItem.GetFamily() = pTargetFont->GetFamily();
211 aFontItem.GetStyleName() = pTargetFont->GetStyleName();
212 aFontItem.GetPitch() = pTargetFont->GetPitch();
213 aFontItem.GetCharSet() = pTargetFont->GetCharSet();
214 rSet.Put( aFontItem );
217 pStyle = pStyleSheetPool->Next();
220 mpDoc->SetLanguage( EE_CHAR_LANGUAGE_CJK, nTargetLanguage );
223 void FuHangulHanjaConversion::StartChineseConversion()
225 //open ChineseTranslationDialog
226 Reference< XComponentContext > xContext(
227 ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
228 if(xContext.is())
230 Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
231 if(xMCF.is())
233 Reference< ui::dialogs::XExecutableDialog > xDialog(
234 xMCF->createInstanceWithContext(
235 rtl::OUString::createFromAscii("com.sun.star.linguistic2.ChineseTranslationDialog")
236 , xContext), UNO_QUERY);
237 Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
238 if( xInit.is() )
240 // initialize dialog
241 Reference< awt::XWindow > xDialogParentWindow(0);
242 Sequence<Any> aSeq(1);
243 Any* pArray = aSeq.getArray();
244 PropertyValue aParam;
245 aParam.Name = rtl::OUString::createFromAscii("ParentWindow");
246 aParam.Value <<= makeAny(xDialogParentWindow);
247 pArray[0] <<= makeAny(aParam);
248 xInit->initialize( aSeq );
250 //execute dialog
251 sal_Int16 nDialogRet = xDialog->execute();
252 if( RET_OK == nDialogRet )
254 //get some parameters from the dialog
255 sal_Bool bToSimplified = sal_True;
256 sal_Bool bUseVariants = sal_True;
257 sal_Bool bCommonTerms = sal_True;
258 Reference< beans::XPropertySet > xProp( xDialog, UNO_QUERY );
259 if( xProp.is() )
263 xProp->getPropertyValue( C2U("IsDirectionToSimplified") ) >>= bToSimplified;
264 xProp->getPropertyValue( C2U("IsUseCharacterVariants") ) >>= bUseVariants;
265 xProp->getPropertyValue( C2U("IsTranslateCommonTerms") ) >>= bCommonTerms;
267 catch( Exception& )
272 //execute translation
273 sal_Int16 nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
274 sal_Int16 nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
275 sal_Int32 nOptions = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
276 if( !bCommonTerms )
277 nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
279 Font aTargetFont = mpWindow->GetDefaultFont(
280 DEFAULTFONT_CJK_PRESENTATION,
281 nTargetLang, DEFAULTFONT_FLAGS_ONLYONE );
283 StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, sal_False );
284 ConvertStyles( nTargetLang, &aTargetFont );
287 Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
288 if( xComponent.is() )
289 xComponent->dispose();
293 } // end of namespace