1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_starmath.hxx"
32 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
33 #include <com/sun/star/lang/Locale.hpp>
34 #include <com/sun/star/uno/Any.h>
36 #include <comphelper/accessibletexthelper.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <comphelper/storagehelper.hxx>
39 #include <rtl/logfile.hxx>
40 #include <rtl/ustring.hxx>
41 #include <sfx2/app.hxx>
42 #include <sfx2/dispatch.hxx>
43 #include <sfx2/docfile.hxx>
44 #include <sfx2/docfilt.hxx>
45 #include <sfx2/fcontnr.hxx>
46 #include <sfx2/msg.hxx>
47 #include <sfx2/objface.hxx>
48 #include <sfx2/printer.hxx>
49 #include <sfx2/request.hxx>
50 #include <sfx2/viewfrm.hxx>
51 #include <sot/clsids.hxx>
52 #include <sot/exchange.hxx>
53 #include <sot/formats.hxx>
54 #include <sot/storage.hxx>
55 #include <svl/eitem.hxx>
56 #include <svl/fstathelper.hxx>
57 #include <svl/intitem.hxx>
58 #include <svl/itempool.hxx>
59 #include <unotools/lingucfg.hxx>
60 #include <unotools/linguprops.hxx>
61 #include <unotools/pathoptions.hxx>
62 #include <svl/ptitem.hxx>
63 #include <svtools/sfxecode.hxx>
64 #include <svl/slstitm.hxx>
65 #include <svl/smplhint.hxx>
66 #include <svl/stritem.hxx>
67 #include <svtools/transfer.hxx>
68 #include <svl/undo.hxx>
69 #include <svl/urihelper.hxx>
70 #include <svl/whiter.hxx>
71 #include <editeng/editeng.hxx>
72 #include <editeng/editstat.hxx>
73 #include <editeng/eeitem.hxx>
74 #include <editeng/fhgtitem.hxx>
75 #include <editeng/fontitem.hxx>
76 #include <editeng/unolingu.hxx>
77 #include <ucbhelper/content.hxx>
78 #include <vcl/mapmod.hxx>
79 #include <vcl/mapunit.hxx>
80 #include <vcl/msgbox.hxx>
81 #include <sfx2/sfx.hrc>
82 #include <document.hxx>
88 #include <starmath.hrc>
90 #include <toolbox.hxx>
91 #include <unomodel.hxx>
92 #include <utility.hxx>
94 #include "mathtype.hxx"
95 #include "mathmlimport.hxx"
96 #include "mathmlexport.hxx"
97 #include <sfx2/sfxsids.hrc>
98 #include <svx/svxids.hrc>
100 using namespace ::com::sun::star
;
101 using namespace ::com::sun::star::accessibility
;
102 using namespace ::com::sun::star::lang
;
103 using namespace ::com::sun::star::ucb
;
104 using namespace ::com::sun::star::uno
;
107 #define DOCUMENT_BUFFER_SIZE (USHORT)32768
109 static const char __FAR_DATA pStarMathDoc
[] = "StarMathDocument";
112 #include "smslots.hxx"
114 ////////////////////////////////////////////////////////////
117 TYPEINIT1( SmDocShell
, SfxObjectShell
);
119 SFX_IMPL_INTERFACE(SmDocShell
, SfxObjectShell
, SmResId(0))
121 SFX_POPUPMENU_REGISTRATION(SmResId(RID_VIEWMENU
));
122 SFX_POPUPMENU_REGISTRATION(SmResId(RID_COMMANDMENU
));
125 SFX_IMPL_OBJECTFACTORY(SmDocShell
, SvGlobalName(SO3_SM_CLASSID
), SFXOBJECTSHELL_STD_NORMAL
, "smath" )
127 void SmDocShell::SFX_NOTIFY(SfxBroadcaster
&, const TypeId
&,
128 const SfxHint
& rHint
, const TypeId
&)
130 switch (((SfxSimpleHint
&)rHint
).GetId())
132 case HINT_FORMATCHANGED
:
133 SetFormulaArranged(FALSE
);
135 nModifyCount
++; //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
142 void SmDocShell::LoadSymbols()
144 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::LoadSymbols" );
146 SmModule
*pp
= SM_MOD();
147 pp
->GetSymbolManager().Load();
151 const String
SmDocShell::GetComment() const
153 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetComment" );
154 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(
155 const_cast<SmDocShell
*>(this)->GetModel(), uno::UNO_QUERY_THROW
);
156 uno::Reference
<document::XDocumentProperties
> xDocProps(
157 xDPS
->getDocumentProperties());
158 return xDocProps
->getDescription();
162 void SmDocShell::SetText(const String
& rBuffer
)
164 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetText" );
166 if (rBuffer
!= aText
)
168 BOOL bIsEnabled
= IsEnableSetModified();
170 EnableSetModified( FALSE
);
173 SetFormulaArranged( FALSE
);
178 SmViewShell
*pViewSh
= SmGetActiveView();
181 pViewSh
->GetViewFrame()->GetBindings().Invalidate(SID_TEXT
);
182 if ( SFX_CREATE_MODE_EMBEDDED
== GetCreateMode() )
185 pViewSh
->GetGraphicWindow().Invalidate();
189 EnableSetModified( bIsEnabled
);
192 // launch accessible event if necessary
193 SmGraphicAccessible
*pAcc
= pViewSh
? pViewSh
->GetGraphicWindow().GetAccessible_Impl() : 0;
196 Any aOldValue
, aNewValue
;
197 if ( comphelper::OCommonAccessibleText::implInitTextChangedEvent( aText
, rBuffer
, aOldValue
, aNewValue
) )
199 pAcc
->LaunchEvent( AccessibleEventId::TEXT_CHANGED
,
200 aOldValue
, aNewValue
);
204 if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED
)
205 OnDocumentPrinterChanged(0);
209 void SmDocShell::SetFormat(SmFormat
& rFormat
)
211 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetFormat" );
214 SetFormulaArranged( FALSE
);
217 nModifyCount
++; //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
219 // don't use SmGetActiveView since the view shell might not be active (0 pointer)
220 // if for example the Basic Macro dialog currently has the focus. Thus:
221 SfxViewFrame
* pFrm
= SfxViewFrame::GetFirst( this );
224 pFrm
->GetBindings().Invalidate(SID_GAPHIC_SM
);
225 pFrm
= SfxViewFrame::GetNext( *pFrm
, this );
229 String
SmDocShell::GetAccessibleText()
231 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetAccessibleText" );
233 if (!IsFormulaArranged())
235 if (0 == aAccText
.Len())
237 DBG_ASSERT( pTree
, "Tree missing" );
239 pTree
->GetAccessibleText( aAccText
);
244 void SmDocShell::Parse()
246 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Parse" );
251 pTree
= aInterpreter
.Parse(aText
);
252 nModifyCount
++; //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
253 SetFormulaArranged( FALSE
);
257 void SmDocShell::ArrangeFormula()
259 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::ArrangeFormula" );
261 if (IsFormulaArranged())
264 //! Nur f�r die Dauer der Existenz dieses Objekts sind am Drucker die
265 //! richtigen Einstellungen garantiert.
266 SmPrinterAccess
aPrtAcc(*this);
267 // OutputDevice *pOutDev = aPrtAcc.GetPrinter();
268 OutputDevice
* pOutDev
= aPrtAcc
.GetRefDev();
272 #if OSL_DEBUG_LEVEL > 1
273 DBG_ERROR("!! SmDocShell::ArrangeFormula: reference device missing !!");
277 // falls n�tig ein anderes OutputDevice holen f�r das formatiert wird
280 SmViewShell
*pView
= SmGetActiveView();
282 pOutDev
= &pView
->GetGraphicWindow();
285 pOutDev
= &SM_MOD()->GetDefaultVirtualDev();
286 pOutDev
->SetMapMode( MapMode(MAP_100TH_MM
) );
289 DBG_ASSERT(pOutDev
->GetMapMode().GetMapUnit() == MAP_100TH_MM
,
290 "Sm : falscher MapMode");
292 const SmFormat
&rFormat
= GetFormat();
293 pTree
->Prepare(rFormat
, *this);
295 // format/draw formulas always from left to right,
296 // and numbers should not be converted
297 ULONG nLayoutMode
= pOutDev
->GetLayoutMode();
298 pOutDev
->SetLayoutMode( TEXT_LAYOUT_BIDI_LTR
);
299 INT16 nDigitLang
= pOutDev
->GetDigitLanguage();
300 pOutDev
->SetDigitLanguage( LANGUAGE_ENGLISH
);
302 pTree
->Arrange(*pOutDev
, rFormat
);
304 pOutDev
->SetLayoutMode( nLayoutMode
);
305 pOutDev
->SetDigitLanguage( nDigitLang
);
307 SetFormulaArranged(TRUE
);
309 // invalidate accessible text
314 void SetEditEngineDefaultFonts(
315 EditEngine
&/*rEditEngine*/,
316 SfxItemPool
&rEditEngineItemPool
)
319 // set fonts to be used
321 SvtLinguOptions aOpt
;
322 SvtLinguConfig().GetOptions( aOpt
);
331 // info to get western font to be used
332 { LANGUAGE_ENGLISH_US
, LANGUAGE_NONE
,
333 DEFAULTFONT_FIXED
, EE_CHAR_FONTINFO
},
334 // info to get CJK font to be used
335 { LANGUAGE_JAPANESE
, LANGUAGE_NONE
,
336 DEFAULTFONT_CJK_TEXT
, EE_CHAR_FONTINFO_CJK
},
337 // info to get CTL font to be used
338 { LANGUAGE_ARABIC_SAUDI_ARABIA
, LANGUAGE_NONE
,
339 DEFAULTFONT_CTL_TEXT
, EE_CHAR_FONTINFO_CTL
}
341 aTable
[0].nLang
= aOpt
.nDefaultLanguage
;
342 aTable
[1].nLang
= aOpt
.nDefaultLanguage_CJK
;
343 aTable
[2].nLang
= aOpt
.nDefaultLanguage_CTL
;
345 for (int i
= 0; i
< 3; ++i
)
347 const FontDta
&rFntDta
= aTable
[i
];
348 LanguageType nLang
= (LANGUAGE_NONE
== rFntDta
.nLang
) ?
349 rFntDta
.nFallbackLang
: rFntDta
.nLang
;
350 Font aFont
= Application::GetDefaultDevice()->GetDefaultFont(
351 rFntDta
.nFontType
, nLang
, DEFAULTFONT_FLAGS_ONLYONE
);
353 ByteString
aFntName( aFont
.GetName(), 1 );
354 int eFntFamily
= aFont
.GetFamily();
355 ByteString
aFntStyleName( aFont
.GetStyleName(), 1 );
356 int ePitch
= aFont
.GetPitch();
357 int eCharSet
= aFont
.GetCharSet();
358 fprintf(stderr
, "\nFontName %s \n", aFntName
.GetBuffer() );
359 fprintf(stderr
, "StyleName %s \n", aFntStyleName
.GetBuffer() );
360 fprintf(stderr
, "eFntFamily %d \n", eFntFamily
);
361 fprintf(stderr
, "ePitch %d \n", ePitch
);
362 fprintf(stderr
, "eCharSet %d \n", eCharSet
);
364 rEditEngineItemPool
.SetPoolDefaultItem(
365 SvxFontItem( aFont
.GetFamily(), aFont
.GetName(),
366 aFont
.GetStyleName(), aFont
.GetPitch(), aFont
.GetCharSet(),
367 rFntDta
.nFontInfoId
) );
371 SvxFontHeightItem
aFontHeigt(
372 Application::GetDefaultDevice()->LogicToPixel(
373 Size( 0, 11 ), MapMode( MAP_POINT
) ).Height(), 100,
374 EE_CHAR_FONTHEIGHT
);
375 rEditEngineItemPool
.SetPoolDefaultItem( aFontHeigt
);
376 aFontHeigt
.SetWhich( EE_CHAR_FONTHEIGHT_CJK
);
377 rEditEngineItemPool
.SetPoolDefaultItem( aFontHeigt
);
378 aFontHeigt
.SetWhich( EE_CHAR_FONTHEIGHT_CTL
);
379 rEditEngineItemPool
.SetPoolDefaultItem( aFontHeigt
);
383 EditEngine
& SmDocShell::GetEditEngine()
385 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetEditEngine" );
390 //! see also SmEditWindow::DataChanged !
393 pEditEngineItemPool
= EditEngine::CreatePool();
395 SetEditEngineDefaultFonts( *pEditEngine
, *pEditEngineItemPool
);
397 pEditEngine
= new EditEngine( pEditEngineItemPool
);
399 pEditEngine
->EnableUndo( TRUE
);
400 pEditEngine
->SetDefTab( USHORT(
401 Application::GetDefaultDevice()->GetTextWidth( C2S("XXXX") ) ) );
403 pEditEngine
->SetControlWord(
404 (pEditEngine
->GetControlWord() | EE_CNTRL_AUTOINDENTING
) &
405 (~EE_CNTRL_UNDOATTRIBS
) &
406 (~EE_CNTRL_PASTESPECIAL
) );
408 pEditEngine
->SetWordDelimiters( C2S(" .=+-*/(){}[];\"" ) );
409 pEditEngine
->SetRefMapMode( MAP_PIXEL
);
411 pEditEngine
->SetPaperSize( Size( 800, 0 ) );
413 pEditEngine
->EraseVirtualDevice();
415 // set initial text if the document already has some...
416 // (may be the case when reloading a doc)
417 String
aTxt( GetText() );
419 pEditEngine
->SetText( aTxt
);
421 pEditEngine
->ClearModifyFlag();
423 // forces new settings to be used if the itempool was modified
424 // after cthe creation of the EditEngine
425 //pEditEngine->Clear(); //#77957 incorrect font size
431 SfxItemPool
& SmDocShell::GetEditEngineItemPool()
433 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetEditEngineItemPool" );
435 if (!pEditEngineItemPool
)
437 DBG_ASSERT( pEditEngineItemPool
, "EditEngineItemPool missing" );
438 return *pEditEngineItemPool
;
442 void SmDocShell::Draw(OutputDevice
&rDev
, Point
&rPosition
)
444 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Draw" );
448 DBG_ASSERT(pTree
, "Sm : NULL pointer");
450 if (!IsFormulaArranged())
453 //Problem: Was passiert mit dem WYSIWYG? Wir haben waehrend wir inplace aktiv
454 //sind kein Referenzdevice und sind auch nicht darauf ausgerichtet. Es kann
455 //also jetzt eine Differenz zwischen der VisArea (spricht die Groesse im Client)
456 //und der jetzt vorliegenden Groese geben.
457 //Idee: Die Differenz koennte, zumindest behelfsmaessig, mit SmNod::SetSize
460 rPosition
.X() += aFormat
.GetDistance( DIS_LEFTSPACE
);
461 rPosition
.Y() += aFormat
.GetDistance( DIS_TOPSPACE
);
463 //! in case of high contrast-mode (accessibility option!)
464 //! the draw mode needs to be set to default, because when imbedding
465 //! Math for example in Calc in "a over b" the fraction bar may not
466 //! be visible else. More generally: the FillColor may have been changed.
467 ULONG nOldDrawMode
= DRAWMODE_DEFAULT
;
468 BOOL bRestoreDrawMode
= FALSE
;
469 if (OUTDEV_WINDOW
== rDev
.GetOutDevType() &&
470 ((Window
&) rDev
).GetSettings().GetStyleSettings().GetHighContrastMode())
472 nOldDrawMode
= rDev
.GetDrawMode();
473 rDev
.SetDrawMode( DRAWMODE_DEFAULT
);
474 bRestoreDrawMode
= TRUE
;
477 // format/draw formulas always from left to right
478 // and numbers should not be converted
479 ULONG nLayoutMode
= rDev
.GetLayoutMode();
480 rDev
.SetLayoutMode( TEXT_LAYOUT_BIDI_LTR
);
481 INT16 nDigitLang
= rDev
.GetDigitLanguage();
482 rDev
.SetDigitLanguage( LANGUAGE_ENGLISH
);
484 pTree
->Draw(rDev
, rPosition
);
486 rDev
.SetLayoutMode( nLayoutMode
);
487 rDev
.SetDigitLanguage( nDigitLang
);
489 if (bRestoreDrawMode
)
490 rDev
.SetDrawMode( nOldDrawMode
);
495 Size
SmDocShell::GetSize()
497 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetSize" );
506 if (!IsFormulaArranged())
508 aRet
= pTree
->GetSize();
513 aRet
.Width() += aFormat
.GetDistance( DIS_LEFTSPACE
) +
514 aFormat
.GetDistance( DIS_RIGHTSPACE
);
515 if ( !aRet
.Height() )
516 aRet
.Height() = 1000;
518 aRet
.Height() += aFormat
.GetDistance( DIS_TOPSPACE
) +
519 aFormat
.GetDistance( DIS_BOTTOMSPACE
);
525 ////////////////////////////////////////
527 SmPrinterAccess::SmPrinterAccess( SmDocShell
&rDocShell
)
529 if ( 0 != (pPrinter
= rDocShell
.GetPrt()) )
531 pPrinter
->Push( PUSH_MAPMODE
);
532 if ( SFX_CREATE_MODE_EMBEDDED
== rDocShell
.GetCreateMode() )
534 // if it is an embedded object (without it's own printer)
535 // we change the MapMode temporarily.
536 //!If it is a document with it's own printer the MapMode should
537 //!be set correct (once) elsewhere(!), in order to avoid numerous
538 //!superfluous pushing and poping of the MapMode when using
541 const MapUnit eOld
= pPrinter
->GetMapMode().GetMapUnit();
542 if ( MAP_100TH_MM
!= eOld
)
544 MapMode
aMap( pPrinter
->GetMapMode() );
545 aMap
.SetMapUnit( MAP_100TH_MM
);
546 Point
aTmp( aMap
.GetOrigin() );
547 aTmp
.X() = OutputDevice::LogicToLogic( aTmp
.X(), eOld
, MAP_100TH_MM
);
548 aTmp
.Y() = OutputDevice::LogicToLogic( aTmp
.Y(), eOld
, MAP_100TH_MM
);
549 aMap
.SetOrigin( aTmp
);
550 pPrinter
->SetMapMode( aMap
);
554 if ( 0 != (pRefDev
= rDocShell
.GetRefDev()) && pPrinter
!= pRefDev
)
556 pRefDev
->Push( PUSH_MAPMODE
);
557 if ( SFX_CREATE_MODE_EMBEDDED
== rDocShell
.GetCreateMode() )
559 // if it is an embedded object (without it's own printer)
560 // we change the MapMode temporarily.
561 //!If it is a document with it's own printer the MapMode should
562 //!be set correct (once) elsewhere(!), in order to avoid numerous
563 //!superfluous pushing and poping of the MapMode when using
566 const MapUnit eOld
= pRefDev
->GetMapMode().GetMapUnit();
567 if ( MAP_100TH_MM
!= eOld
)
569 MapMode
aMap( pRefDev
->GetMapMode() );
570 aMap
.SetMapUnit( MAP_100TH_MM
);
571 Point
aTmp( aMap
.GetOrigin() );
572 aTmp
.X() = OutputDevice::LogicToLogic( aTmp
.X(), eOld
, MAP_100TH_MM
);
573 aTmp
.Y() = OutputDevice::LogicToLogic( aTmp
.Y(), eOld
, MAP_100TH_MM
);
574 aMap
.SetOrigin( aTmp
);
575 pRefDev
->SetMapMode( aMap
);
581 SmPrinterAccess::~SmPrinterAccess()
585 if ( pRefDev
&& pRefDev
!= pPrinter
)
589 ////////////////////////////////////////
591 Printer
* SmDocShell::GetPrt()
593 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetPrt" );
595 if ( SFX_CREATE_MODE_EMBEDDED
== GetCreateMode() )
597 //Normalerweise wird der Printer vom Server besorgt. Wenn dieser aber
598 //keinen liefert (weil etwa noch keine connection da ist), kann es
599 //dennoch sein, dass wir den Printer kennen, denn dieser wird in
600 //OnDocumentPrinterChanged vom Server durchgereicht und dann temporaer
602 Printer
*pPrt
= GetDocumentPrinter();
603 if ( !pPrt
&& pTmpPrinter
)
607 else if ( !pPrinter
)
609 SfxItemSet
*pOptions
=
610 new SfxItemSet(GetPool(),
611 SID_PRINTSIZE
, SID_PRINTSIZE
,
612 SID_PRINTZOOM
, SID_PRINTZOOM
,
613 SID_PRINTTITLE
, SID_PRINTTITLE
,
614 SID_PRINTTEXT
, SID_PRINTTEXT
,
615 SID_PRINTFRAME
, SID_PRINTFRAME
,
616 SID_NO_RIGHT_SPACES
, SID_NO_RIGHT_SPACES
,
619 SmModule
*pp
= SM_MOD();
620 pp
->GetConfig()->ConfigToItemSet(*pOptions
);
621 pPrinter
= new SfxPrinter(pOptions
);
622 pPrinter
->SetMapMode( MapMode(MAP_100TH_MM
) );
627 OutputDevice
* SmDocShell::GetRefDev()
629 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetRefDev" );
631 if ( SFX_CREATE_MODE_EMBEDDED
== GetCreateMode() )
633 OutputDevice
* pOutDev
= GetDocumentRefDev();
642 void SmDocShell::SetPrinter( SfxPrinter
*pNew
)
644 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetPrinter" );
647 pPrinter
= pNew
; //Eigentumsuebergang!
648 pPrinter
->SetMapMode( MapMode(MAP_100TH_MM
) );
649 SetFormulaArranged(FALSE
);
653 void SmDocShell::OnDocumentPrinterChanged( Printer
*pPrt
)
655 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::OnDocumentPrinterChanged" );
658 SetFormulaArranged(FALSE
);
659 Size aOldSize
= GetVisArea().GetSize();
661 if( aOldSize
!= GetVisArea().GetSize() && aText
.Len() )
666 void SmDocShell::Repaint()
668 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Repaint" );
670 BOOL bIsEnabled
= IsEnableSetModified();
672 EnableSetModified( FALSE
);
674 SetFormulaArranged( FALSE
);
676 Size aVisSize
= GetSize();
677 SetVisAreaSize( aVisSize
);
678 SmViewShell
*pViewSh
= SmGetActiveView();
680 pViewSh
->GetGraphicWindow().Invalidate();
683 EnableSetModified( bIsEnabled
);
687 SmDocShell::SmDocShell( const sal_uInt64 i_nSfxCreationFlags
) :
688 SfxObjectShell( i_nSfxCreationFlags
),
690 pEditEngineItemPool ( 0 ),
695 bIsFormulaArranged ( FALSE
)
697 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SmDocShell" );
699 SetPool(&SFX_APP()->GetPool());
701 SmModule
*pp
= SM_MOD();
702 aFormat
= pp
->GetConfig()->GetStandardFormat();
704 StartListening(aFormat
);
705 StartListening(*pp
->GetConfig());
707 SetBaseModel( new SmModel(this) );
712 SmDocShell::~SmDocShell()
714 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::~SmDocShell" );
716 SmModule
*pp
= SM_MOD();
718 EndListening(aFormat
);
719 EndListening(*pp
->GetConfig());
722 SfxItemPool::Free(pEditEngineItemPool
);
728 BOOL
SmDocShell::SetData( const String
& rData
)
730 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetData" );
737 BOOL
SmDocShell::ConvertFrom(SfxMedium
&rMedium
)
739 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::ConvertFrom" );
741 BOOL bSuccess
= FALSE
;
742 const String
& rFltName
= rMedium
.GetFilter()->GetFilterName();
744 DBG_ASSERT( !rFltName
.EqualsAscii( STAROFFICE_XML
), "Wrong filter!");
746 if ( rFltName
.EqualsAscii( MATHML_XML
) )
753 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
754 SmXMLImportWrapper
aEquation(xModel
);
755 bSuccess
= 0 == aEquation
.Import(rMedium
);
759 SvStream
*pStream
= rMedium
.GetInStream();
762 if ( SotStorage::IsStorageFile( pStream
) )
764 SvStorageRef aStorage
= new SotStorage( pStream
, FALSE
);
765 if ( aStorage
->IsStream( C2S( "Equation Native" ) ) )
767 // is this a MathType Storage?
768 MathType
aEquation( aText
);
769 if ( TRUE
== (bSuccess
= (1 == aEquation
.Parse( aStorage
)) ))
775 //bSuccess = ImportSM20File( pStream );
780 if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED
)
782 //???OnDocumentPrinterChanged(0);
783 SetFormulaArranged( FALSE
);
787 FinishedLoading( SFX_LOADED_ALL
);
792 BOOL
SmDocShell::InitNew( const uno::Reference
< embed::XStorage
>& xStorage
)
794 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::InitNew" );
797 if ( SfxObjectShell::InitNew( xStorage
) )
800 SetVisArea(Rectangle(Point(0, 0), Size(2000, 1000)));
806 BOOL
SmDocShell::Load( SfxMedium
& rMedium
)
808 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Load" );
811 if( SfxObjectShell::Load( rMedium
))
813 uno::Reference
< embed::XStorage
> xStorage
= GetMedium()->GetStorage();
814 uno::Reference
< container::XNameAccess
> xAccess (xStorage
, uno::UNO_QUERY
);
817 xAccess
->hasByName( C2S( "content.xml" ) ) &&
818 xStorage
->isStreamElement( C2S( "content.xml" ) )
821 xAccess
->hasByName( C2S( "Content.xml" ) ) &&
822 xStorage
->isStreamElement( C2S( "Content.xml" ) )
826 // is this a fabulous math package ?
827 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
828 SmXMLImportWrapper
aEquation(xModel
);
829 ULONG nError
= aEquation
.Import(rMedium
);
831 SetError( nError
, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX
) ) );
835 if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED
)
837 //???OnDocumentPrinterChanged(0);
838 SetFormulaArranged( FALSE
);
842 FinishedLoading( SFX_LOADED_ALL
);
846 //------------------------------------------------------------------
848 BOOL
SmDocShell::Save()
850 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Save" );
852 //! apply latest changes if necessary
855 if ( SfxObjectShell::Save() )
859 if( pTree
&& !IsFormulaArranged() )
862 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
863 SmXMLExportWrapper
aEquation(xModel
);
864 aEquation
.SetFlat(sal_False
);
865 return aEquation
.Export(*GetMedium());
872 * replace bad characters that can not be saved. (#i74144)
874 sal_Bool
SmDocShell::ReplaceBadChars()
876 sal_Bool bReplace
= sal_False
;
879 String
aEngTxt( pEditEngine
->GetText( LINEEND_LF
) );
880 const sal_Unicode
*pEngTxt
= aEngTxt
.GetBuffer();
881 xub_StrLen nLen
= aEngTxt
.Len();
882 for (xub_StrLen i
= 0; i
< nLen
&& !bReplace
; ++i
)
884 const sal_Unicode c
= *pEngTxt
++;
885 if (c
< ' ' && c
!= '\r' && c
!= '\n' && c
!= '\t')
890 sal_Unicode
*pChgTxt
= aEngTxt
.GetBufferAccess();
891 for (xub_StrLen i
= 0; i
< nLen
; ++i
)
893 sal_Unicode
&rc
= *pChgTxt
++;
894 if (rc
< ' ' && rc
!= '\r' && rc
!= '\n' && rc
!= '\t')
897 aEngTxt
.ReleaseBufferAccess( nLen
);
906 void SmDocShell::UpdateText()
908 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::UpdateText" );
910 if (pEditEngine
&& pEditEngine
->IsModified())
912 String
aEngTxt( pEditEngine
->GetText( LINEEND_LF
) );
913 if (GetText() != aEngTxt
)
919 BOOL
SmDocShell::SaveAs( SfxMedium
& rMedium
)
921 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SaveAs" );
925 //! apply latest changes if necessary
928 if ( SfxObjectShell::SaveAs( rMedium
) )
932 if( pTree
&& !IsFormulaArranged() )
935 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
936 SmXMLExportWrapper
aEquation(xModel
);
937 aEquation
.SetFlat(sal_False
);
938 bRet
= aEquation
.Export(rMedium
);
943 BOOL
SmDocShell::ConvertTo( SfxMedium
&rMedium
)
945 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::ConvertTo" );
948 const SfxFilter
* pFlt
= rMedium
.GetFilter();
953 if( pTree
&& !IsFormulaArranged() )
956 const String
& rFltName
= pFlt
->GetFilterName();
957 if(rFltName
.EqualsAscii( STAROFFICE_XML
))
959 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
960 SmXMLExportWrapper
aEquation(xModel
);
961 aEquation
.SetFlat(sal_False
);
962 bRet
= aEquation
.Export(rMedium
);
964 else if(rFltName
.EqualsAscii( MATHML_XML
))
966 Reference
<com::sun::star::frame::XModel
> xModel(GetModel());
967 SmXMLExportWrapper
aEquation(xModel
);
968 aEquation
.SetFlat(sal_True
);
969 bRet
= aEquation
.Export(rMedium
);
971 else if( pFlt
->GetFilterName().EqualsAscii("MathType 3.x"))
972 bRet
= WriteAsMathType3( rMedium
);
977 BOOL
SmDocShell::SaveCompleted( const ::com::sun::star::uno::Reference
< ::com::sun::star::embed::XStorage
>& xStorage
)
979 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SaveCompleted" );
981 if( SfxObjectShell::SaveCompleted( xStorage
))
988 void SmDocShell::Execute(SfxRequest
& rReq
)
990 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Execute" );
992 switch (rReq
.GetSlot())
996 SmFormat aOldFormat
= GetFormat();
997 SmFormat
aNewFormat( aOldFormat
);
998 aNewFormat
.SetTextmode(!aOldFormat
.IsTextmode());
1000 SfxUndoManager
*pTmpUndoMgr
= GetUndoManager();
1002 pTmpUndoMgr
->AddUndoAction(
1003 new SmFormatAction(this, aOldFormat
, aNewFormat
));
1005 SetFormat( aNewFormat
);
1010 case SID_AUTO_REDRAW
:
1012 SmModule
*pp
= SM_MOD();
1013 BOOL bRedraw
= pp
->GetConfig()->IsAutoRedraw();
1014 pp
->GetConfig()->SetAutoRedraw(!bRedraw
);
1018 case SID_LOADSYMBOLS
:
1022 case SID_SAVESYMBOLS
:
1028 // get device used to retrieve the FontList
1029 OutputDevice
*pDev
= GetPrinter();
1030 if (!pDev
|| pDev
->GetDevFontCount() == 0)
1031 pDev
= &SM_MOD()->GetDefaultVirtualDev();
1032 DBG_ASSERT (pDev
, "device for font list missing" );
1034 SmFontTypeDialog
*pFontTypeDialog
= new SmFontTypeDialog( NULL
, pDev
);
1036 SmFormat aOldFormat
= GetFormat();
1037 pFontTypeDialog
->ReadFrom( aOldFormat
);
1038 if (pFontTypeDialog
->Execute() == RET_OK
)
1040 SmFormat
aNewFormat( aOldFormat
);
1042 pFontTypeDialog
->WriteTo(aNewFormat
);
1043 SfxUndoManager
*pTmpUndoMgr
= GetUndoManager();
1045 pTmpUndoMgr
->AddUndoAction(
1046 new SmFormatAction(this, aOldFormat
, aNewFormat
));
1048 SetFormat( aNewFormat
);
1051 delete pFontTypeDialog
;
1057 SmFontSizeDialog
*pFontSizeDialog
= new SmFontSizeDialog(NULL
);
1059 SmFormat aOldFormat
= GetFormat();
1060 pFontSizeDialog
->ReadFrom( aOldFormat
);
1061 if (pFontSizeDialog
->Execute() == RET_OK
)
1063 SmFormat
aNewFormat( aOldFormat
);
1065 pFontSizeDialog
->WriteTo(aNewFormat
);
1067 SfxUndoManager
*pTmpUndoMgr
= GetUndoManager();
1069 pTmpUndoMgr
->AddUndoAction(
1070 new SmFormatAction(this, aOldFormat
, aNewFormat
));
1072 SetFormat( aNewFormat
);
1075 delete pFontSizeDialog
;
1081 SmDistanceDialog
*pDistanceDialog
= new SmDistanceDialog(NULL
);
1083 SmFormat aOldFormat
= GetFormat();
1084 pDistanceDialog
->ReadFrom( aOldFormat
);
1085 if (pDistanceDialog
->Execute() == RET_OK
)
1087 SmFormat
aNewFormat( aOldFormat
);
1089 pDistanceDialog
->WriteTo(aNewFormat
);
1091 SfxUndoManager
*pTmpUndoMgr
= GetUndoManager();
1093 pTmpUndoMgr
->AddUndoAction(
1094 new SmFormatAction(this, aOldFormat
, aNewFormat
));
1096 SetFormat( aNewFormat
);
1099 delete pDistanceDialog
;
1105 SmAlignDialog
*pAlignDialog
= new SmAlignDialog(NULL
);
1107 SmFormat aOldFormat
= GetFormat();
1108 pAlignDialog
->ReadFrom( aOldFormat
);
1109 if (pAlignDialog
->Execute() == RET_OK
)
1111 SmFormat
aNewFormat( aOldFormat
);
1113 pAlignDialog
->WriteTo(aNewFormat
);
1115 SmModule
*pp
= SM_MOD();
1116 SmFormat
aFmt( pp
->GetConfig()->GetStandardFormat() );
1117 pAlignDialog
->WriteTo( aFmt
);
1118 pp
->GetConfig()->SetStandardFormat( aFmt
);
1120 SfxUndoManager
*pTmpUndoMgr
= GetUndoManager();
1122 pTmpUndoMgr
->AddUndoAction(
1123 new SmFormatAction(this, aOldFormat
, aNewFormat
));
1125 SetFormat( aNewFormat
);
1128 delete pAlignDialog
;
1134 const SfxStringItem
& rItem
= (const SfxStringItem
&)rReq
.GetArgs()->Get(SID_TEXT
);
1135 if (GetText() != rItem
.GetValue())
1136 SetText(rItem
.GetValue());
1143 SfxUndoManager
* pTmpUndoMgr
= GetUndoManager();
1146 USHORT nId
= rReq
.GetSlot(), nCnt
= 1;
1147 const SfxItemSet
* pArgs
= rReq
.GetArgs();
1148 const SfxPoolItem
* pItem
;
1149 if( pArgs
&& SFX_ITEM_SET
== pArgs
->GetItemState( nId
, FALSE
, &pItem
))
1150 nCnt
= ((SfxUInt16Item
*)pItem
)->GetValue();
1152 BOOL (SfxUndoManager:: *fnDo
)( USHORT
);
1155 if( SID_UNDO
== rReq
.GetSlot() )
1157 nCount
= pTmpUndoMgr
->GetUndoActionCount();
1158 fnDo
= &SfxUndoManager::Undo
;
1162 nCount
= pTmpUndoMgr
->GetRedoActionCount();
1163 fnDo
= &SfxUndoManager::Redo
;
1166 for( ; nCnt
&& nCount
; --nCnt
, --nCount
)
1167 (pTmpUndoMgr
->*fnDo
)( 0 );
1170 SfxViewFrame
* pFrm
= SfxViewFrame::GetFirst( this );
1173 SfxBindings
& rBind
= pFrm
->GetBindings();
1174 rBind
.Invalidate(SID_UNDO
);
1175 rBind
.Invalidate(SID_REDO
);
1176 rBind
.Invalidate(SID_REPEAT
);
1177 rBind
.Invalidate(SID_CLEARHISTORY
);
1178 pFrm
= SfxViewFrame::GetNext( *pFrm
, this );
1188 void SmDocShell::GetState(SfxItemSet
&rSet
)
1190 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetState" );
1192 SfxWhichIter
aIter(rSet
);
1194 for (USHORT nWh
= aIter
.FirstWhich(); 0 != nWh
; nWh
= aIter
.NextWhich())
1199 rSet
.Put(SfxBoolItem(SID_TEXTMODE
, GetFormat().IsTextmode()));
1202 case SID_DOCTEMPLATE
:
1203 rSet
.DisableItem(SID_DOCTEMPLATE
);
1206 case SID_AUTO_REDRAW
:
1208 SmModule
*pp
= SM_MOD();
1209 BOOL bRedraw
= pp
->GetConfig()->IsAutoRedraw();
1211 rSet
.Put(SfxBoolItem(SID_AUTO_REDRAW
, bRedraw
));
1215 case SID_MODIFYSTATUS
:
1217 sal_Unicode cMod
= ' ';
1220 rSet
.Put(SfxStringItem(SID_MODIFYSTATUS
, String(cMod
)));
1225 rSet
.Put(SfxStringItem(SID_TEXT
, GetText()));
1229 //! very old (pre UNO) and ugly hack to invalidate the SmGraphicWindow.
1230 //! If nModifyCount gets changed then the call below will implicitly notify
1231 //! SmGraphicController::StateChanged and there the window gets invalidated.
1232 //! Thus all the 'nModifyCount++' before invalidating this slot.
1233 rSet
.Put(SfxInt16Item(SID_GAPHIC_SM
, nModifyCount
));
1239 SfxViewFrame
* pFrm
= SfxViewFrame::GetFirst( this );
1241 pFrm
->GetSlotState( nWh
, NULL
, &rSet
);
1243 rSet
.DisableItem( nWh
);
1247 case SID_GETUNDOSTRINGS
:
1248 case SID_GETREDOSTRINGS
:
1250 SfxUndoManager
* pTmpUndoMgr
= GetUndoManager();
1253 UniString(SfxUndoManager:: *fnGetComment
)( USHORT
) const;
1256 if( SID_GETUNDOSTRINGS
== nWh
)
1258 nCount
= pTmpUndoMgr
->GetUndoActionCount();
1259 fnGetComment
= &SfxUndoManager::GetUndoActionComment
;
1263 nCount
= pTmpUndoMgr
->GetRedoActionCount();
1264 fnGetComment
= &SfxUndoManager::GetRedoActionComment
;
1269 for( sal_uInt16 n
= 0; n
< nCount
; ++n
)
1270 ( sList
+= (pTmpUndoMgr
->*fnGetComment
)( n
) )
1273 SfxStringListItem
aItem( nWh
);
1274 aItem
.SetString( sList
);
1279 rSet
.DisableItem( nWh
);
1287 SfxUndoManager
*SmDocShell::GetUndoManager()
1289 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::GetUndoManager" );
1293 return &pEditEngine
->GetUndoManager();
1297 void SmDocShell::SaveSymbols()
1299 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SaveSymbols" );
1301 SmModule
*pp
= SM_MOD();
1302 pp
->GetSymbolManager().Save();
1306 void SmDocShell::Draw(OutputDevice
*pDevice
,
1310 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::Draw" );
1312 pDevice
->IntersectClipRegion(GetVisArea());
1314 Draw(*pDevice
, atmppoint
);
1317 SfxItemPool
& SmDocShell::GetPool() const
1319 return SFX_APP()->GetPool();
1322 void SmDocShell::SetVisArea(const Rectangle
& rVisArea
)
1324 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetVisArea" );
1326 Rectangle
aNewRect(rVisArea
);
1328 aNewRect
.SetPos(Point());
1330 if (! aNewRect
.Right()) aNewRect
.Right() = 2000;
1331 if (! aNewRect
.Bottom()) aNewRect
.Bottom() = 1000;
1333 BOOL bIsEnabled
= IsEnableSetModified();
1335 EnableSetModified( FALSE
);
1337 //TODO/LATER: it's unclear how this interacts with the SFX code
1338 // If outplace editing, then dont resize the OutplaceWindow. But the
1339 // ObjectShell has to resize. Bug 56470
1341 if( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED
&& !IsInPlaceActive() && GetFrame() )
1343 GetFrame()->LockAdjustPosSizePixel();
1344 bUnLockFrame
= TRUE
;
1347 bUnLockFrame
= FALSE
;
1349 SfxObjectShell::SetVisArea( aNewRect
);
1352 GetFrame()->UnlockAdjustPosSizePixel();
1355 EnableSetModified( bIsEnabled
);
1359 void SmDocShell::FillClass(SvGlobalName
* pClassName
,
1360 sal_uInt32
* pFormat
,
1361 String
* /*pAppName*/,
1362 String
* pFullTypeName
,
1363 String
* pShortTypeName
,
1364 sal_Int32 nFileFormat
,
1365 sal_Bool bTemplate
/* = sal_False */) const
1367 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::FillClass" );
1369 if (nFileFormat
== SOFFICE_FILEFORMAT_60
)
1371 *pClassName
= SvGlobalName(SO3_SM_CLASSID_60
);
1372 *pFormat
= SOT_FORMATSTR_ID_STARMATH_60
;
1373 *pFullTypeName
= String(SmResId(STR_MATH_DOCUMENT_FULLTYPE_CURRENT
));
1374 *pShortTypeName
= String(SmResId(RID_DOCUMENTSTR
));
1376 else if (nFileFormat
== SOFFICE_FILEFORMAT_8
)
1378 *pClassName
= SvGlobalName(SO3_SM_CLASSID_60
);
1379 *pFormat
= bTemplate
? SOT_FORMATSTR_ID_STARMATH_8_TEMPLATE
: SOT_FORMATSTR_ID_STARMATH_8
;
1380 *pFullTypeName
= String(SmResId(STR_MATH_DOCUMENT_FULLTYPE_CURRENT
));
1381 *pShortTypeName
= String(SmResId(RID_DOCUMENTSTR
));
1385 ULONG
SmDocShell::GetMiscStatus() const
1387 return SfxObjectShell::GetMiscStatus() | SVOBJ_MISCSTATUS_NOTRESIZEABLE
1388 | SVOBJ_MISCSTATUS_RESIZEONPRINTERCHANGE
;
1391 void SmDocShell::SetModified(BOOL bModified
)
1393 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::SetModified" );
1395 if( IsEnableSetModified() )
1397 SfxObjectShell::SetModified( bModified
);
1398 Broadcast(SfxSimpleHint(SFX_HINT_DOCCHANGED
));
1402 BOOL
SmDocShell::WriteAsMathType3( SfxMedium
& rMedium
)
1404 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmDocShell::WriteAsMathType3" );
1406 MathType
aEquation( aText
, pTree
);
1408 BOOL bRet
= 0 != aEquation
.ConvertFromStarMath( rMedium
);