xref: /aoo4110/main/starmath/source/document.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_starmath.hxx"
26 
27 
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/uno/Any.h>
31 
32 #include <comphelper/accessibletexthelper.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/storagehelper.hxx>
35 #include <rtl/logfile.hxx>
36 #include <rtl/ustring.hxx>
37 #include <unotools/eventcfg.hxx>
38 #include <sfx2/event.hxx>
39 #include <sfx2/app.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <sfx2/docfile.hxx>
42 #include <sfx2/docfilt.hxx>
43 #include <sfx2/fcontnr.hxx>
44 #include <sfx2/msg.hxx>
45 #include <sfx2/objface.hxx>
46 #include <sfx2/printer.hxx>
47 #include <sfx2/request.hxx>
48 #include <sfx2/viewfrm.hxx>
49 #include <sot/clsids.hxx>
50 #include <sot/exchange.hxx>
51 #include <sot/formats.hxx>
52 #include <sot/storage.hxx>
53 #include <svl/eitem.hxx>
54 #include <svl/fstathelper.hxx>
55 #include <svl/intitem.hxx>
56 #include <svl/itempool.hxx>
57 #include <unotools/lingucfg.hxx>
58 #include <unotools/linguprops.hxx>
59 #include <unotools/pathoptions.hxx>
60 #include <svl/ptitem.hxx>
61 #include <svtools/sfxecode.hxx>
62 #include <svl/slstitm.hxx>
63 #include <svl/smplhint.hxx>
64 #include <svl/stritem.hxx>
65 #include <svtools/transfer.hxx>
66 #include <svl/undo.hxx>
67 #include <svl/urihelper.hxx>
68 #include <svl/whiter.hxx>
69 #include <editeng/editeng.hxx>
70 #include <editeng/editstat.hxx>
71 #include <editeng/eeitem.hxx>
72 #include <editeng/fhgtitem.hxx>
73 #include <editeng/fontitem.hxx>
74 #include <editeng/unolingu.hxx>
75 #include <ucbhelper/content.hxx>
76 #include <vcl/mapmod.hxx>
77 #include <tools/mapunit.hxx>
78 #include <vcl/msgbox.hxx>
79 #include <sfx2/sfx.hrc>
80 #include <document.hxx>
81 #include <action.hxx>
82 #include <config.hxx>
83 #include <dialog.hxx>
84 #include <format.hxx>
85 #include <smdll.hxx>
86 #include <starmath.hrc>
87 #include <symbol.hxx>
88 #include <toolbox.hxx>
89 #include <unomodel.hxx>
90 #include <utility.hxx>
91 #include <view.hxx>
92 #include "mathtype.hxx"
93 #include "mathmlimport.hxx"
94 #include "mathmlexport.hxx"
95 #include <sfx2/sfxsids.hrc>
96 #include <svx/svxids.hrc>
97 #include <tools/diagnose_ex.h>
98 
99 using namespace ::com::sun::star;
100 using namespace ::com::sun::star::accessibility;
101 using namespace ::com::sun::star::lang;
102 using namespace ::com::sun::star::ucb;
103 using namespace ::com::sun::star::uno;
104 
105 
106 #define DOCUMENT_BUFFER_SIZE	(sal_uInt16)32768
107 
108 static const char __FAR_DATA pStarMathDoc[] = "StarMathDocument";
109 
110 #define SmDocShell
111 #include "smslots.hxx"
112 
113 ////////////////////////////////////////////////////////////
114 
115 
116 TYPEINIT1( SmDocShell, SfxObjectShell );
117 
118 SFX_IMPL_INTERFACE(SmDocShell, SfxObjectShell, SmResId(0))
119 {
120 	SFX_POPUPMENU_REGISTRATION(SmResId(RID_VIEWMENU));
121 	SFX_POPUPMENU_REGISTRATION(SmResId(RID_COMMANDMENU));
122 }
123 
124 SFX_IMPL_OBJECTFACTORY(SmDocShell, SvGlobalName(SO3_SM_CLASSID), SFXOBJECTSHELL_STD_NORMAL, "smath" )
125 
SFX_NOTIFY(SfxBroadcaster &,const TypeId &,const SfxHint & rHint,const TypeId &)126 void SmDocShell::SFX_NOTIFY(SfxBroadcaster&, const TypeId&,
127 					const SfxHint& rHint, const TypeId&)
128 {
129 	switch (((SfxSimpleHint&)rHint).GetId())
130 	{
131 		case HINT_FORMATCHANGED:
132 			SetFormulaArranged(sal_False);
133 
134             nModifyCount++;     //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
135 
136             Repaint();
137 			break;
138 	}
139 }
140 
LoadSymbols()141 void SmDocShell::LoadSymbols()
142 {
143     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::LoadSymbols" );
144 
145     SmModule *pp = SM_MOD();
146     pp->GetSymbolManager().Load();
147 }
148 
149 
GetComment() const150 const String SmDocShell::GetComment() const
151 {
152     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetComment" );
153     uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
154         const_cast<SmDocShell*>(this)->GetModel(), uno::UNO_QUERY_THROW);
155     uno::Reference<document::XDocumentProperties> xDocProps(
156         xDPS->getDocumentProperties());
157     return xDocProps->getDescription();
158 }
159 
160 
SetText(const String & rBuffer)161 void SmDocShell::SetText(const String& rBuffer)
162 {
163     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetText" );
164 
165 	if (rBuffer != aText)
166 	{
167 		sal_Bool bIsEnabled = IsEnableSetModified();
168 		if( bIsEnabled )
169 			EnableSetModified( sal_False );
170 
171 		aText = rBuffer;
172         SetFormulaArranged( sal_False );
173 
174         Parse();
175         //Repaint();
176 
177         SmViewShell *pViewSh = SmGetActiveView();
178 		if( pViewSh )
179         {
180 			pViewSh->GetViewFrame()->GetBindings().Invalidate(SID_TEXT);
181             if ( SFX_CREATE_MODE_EMBEDDED == GetCreateMode() )
182             {
183                 // have SwOleClient::FormatChanged() to align the modified formula properly
184                 // even if the vis area does not change (e.g. when formula text changes from
185                 // "{a over b + c} over d" to "d over {a over b + c}"
186                 SFX_APP()->NotifyEvent(SfxEventHint( SFX_EVENT_VISAREACHANGED, GlobalEventConfig::GetEventName(STR_EVENT_VISAREACHANGED), this));
187 
188                 Repaint();
189             }
190             else
191                 pViewSh->GetGraphicWindow().Invalidate();
192         }
193 
194 		if ( bIsEnabled )
195 			EnableSetModified( bIsEnabled );
196 		SetModified(sal_True);
197 
198         // launch accessible event if necessary
199         SmGraphicAccessible *pAcc = pViewSh ? pViewSh->GetGraphicWindow().GetAccessible_Impl() : 0;
200         if (pAcc)
201         {
202 			Any aOldValue, aNewValue;
203 			if ( comphelper::OCommonAccessibleText::implInitTextChangedEvent( aText, rBuffer, aOldValue, aNewValue ) )
204 			{
205 				pAcc->LaunchEvent( AccessibleEventId::TEXT_CHANGED,
206 						aOldValue, aNewValue );
207 			}
208         }
209 
210         if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
211             OnDocumentPrinterChanged(0);
212 	}
213 }
214 
SetFormat(SmFormat & rFormat)215 void SmDocShell::SetFormat(SmFormat& rFormat)
216 {
217     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetFormat" );
218 
219 	aFormat = rFormat;
220 	SetFormulaArranged( sal_False );
221 	SetModified( sal_True );
222 
223     nModifyCount++;     //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
224 
225     // don't use SmGetActiveView since the view shell might not be active (0 pointer)
226     // if for example the Basic Macro dialog currently has the focus. Thus:
227 	SfxViewFrame* pFrm = SfxViewFrame::GetFirst( this );
228 	while (pFrm)
229 	{
230 		pFrm->GetBindings().Invalidate(SID_GAPHIC_SM);
231 		pFrm = SfxViewFrame::GetNext( *pFrm, this );
232 	}
233 }
234 
GetAccessibleText()235 String SmDocShell::GetAccessibleText()
236 {
237     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetAccessibleText" );
238 
239     if (!IsFormulaArranged())
240         ArrangeFormula();
241     if (0 == aAccText.Len())
242     {
243         DBG_ASSERT( pTree, "Tree missing" );
244         if (pTree)
245             pTree->GetAccessibleText( aAccText );
246     }
247     return aAccText;
248 }
249 
Parse()250 void SmDocShell::Parse()
251 {
252     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Parse" );
253 
254 	if (pTree)
255 		delete pTree;
256     ReplaceBadChars();
257 	pTree = aInterpreter.Parse(aText);
258 	nModifyCount++;     //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
259     SetFormulaArranged( sal_False );
260 
261     aUsedSymbols = aInterpreter.GetUsedSymbols();
262 }
263 
264 
ArrangeFormula()265 void SmDocShell::ArrangeFormula()
266 {
267     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::ArrangeFormula" );
268 
269 	if (IsFormulaArranged())
270 		return;
271 
272 	//! Nur f�r die Dauer der Existenz dieses Objekts sind am Drucker die
273 	//! richtigen Einstellungen garantiert.
274     SmPrinterAccess  aPrtAcc(*this);
275 //	OutputDevice	*pOutDev = aPrtAcc.GetPrinter();
276     OutputDevice* pOutDev = aPrtAcc.GetRefDev();
277 
278 	if (!pOutDev)
279     {
280 #if OSL_DEBUG_LEVEL > 1
281 		DBG_ERROR("!! SmDocShell::ArrangeFormula: reference device missing !!");
282 #endif
283     }
284 
285     // falls n�tig ein anderes OutputDevice holen f�r das formatiert wird
286 	if (!pOutDev)
287 	{
288 		SmViewShell *pView = SmGetActiveView();
289 		if (pView)
290 			pOutDev = &pView->GetGraphicWindow();
291 		else
292         {
293             pOutDev = &SM_MOD()->GetDefaultVirtualDev();
294 			pOutDev->SetMapMode( MapMode(MAP_100TH_MM) );
295 		}
296 	}
297 	DBG_ASSERT(pOutDev->GetMapMode().GetMapUnit() == MAP_100TH_MM,
298 			   "Sm : falscher MapMode");
299 
300 	const SmFormat &rFormat = GetFormat();
301 	pTree->Prepare(rFormat, *this);
302 
303     // format/draw formulas always from left to right,
304     // and numbers should not be converted
305     sal_uLong nLayoutMode = pOutDev->GetLayoutMode();
306     pOutDev->SetLayoutMode( TEXT_LAYOUT_BIDI_LTR );
307     sal_Int16 nDigitLang = pOutDev->GetDigitLanguage();
308     pOutDev->SetDigitLanguage( LANGUAGE_ENGLISH );
309     //
310     pTree->Arrange(*pOutDev, rFormat);
311     //
312     pOutDev->SetLayoutMode( nLayoutMode );
313     pOutDev->SetDigitLanguage( nDigitLang );
314 
315 	SetFormulaArranged(sal_True);
316 
317     // invalidate accessible text
318     aAccText = String();
319 }
320 
321 
SetEditEngineDefaultFonts(EditEngine &,SfxItemPool & rEditEngineItemPool)322 void SetEditEngineDefaultFonts(
323         EditEngine &/*rEditEngine*/,
324         SfxItemPool &rEditEngineItemPool )
325 {
326         //
327         // set fonts to be used
328         //
329         SvtLinguOptions aOpt;
330         SvtLinguConfig().GetOptions( aOpt );
331         //
332         struct FontDta {
333             sal_Int16       nFallbackLang;
334             sal_Int16       nLang;
335             sal_uInt16      nFontType;
336             sal_uInt16      nFontInfoId;
337             } aTable[3] =
338         {
339             // info to get western font to be used
340             {   LANGUAGE_ENGLISH_US,    LANGUAGE_NONE,
341                 DEFAULTFONT_FIXED,      EE_CHAR_FONTINFO },
342             // info to get CJK font to be used
343             {   LANGUAGE_JAPANESE,      LANGUAGE_NONE,
344                 DEFAULTFONT_CJK_TEXT,   EE_CHAR_FONTINFO_CJK },
345             // info to get CTL font to be used
346             {   LANGUAGE_ARABIC_SAUDI_ARABIA,  LANGUAGE_NONE,
347                 DEFAULTFONT_CTL_TEXT,   EE_CHAR_FONTINFO_CTL }
348         };
349         aTable[0].nLang = aOpt.nDefaultLanguage;
350         aTable[1].nLang = aOpt.nDefaultLanguage_CJK;
351         aTable[2].nLang = aOpt.nDefaultLanguage_CTL;
352         //
353         for (int i = 0;  i < 3;  ++i)
354         {
355             const FontDta &rFntDta = aTable[i];
356             LanguageType nLang = (LANGUAGE_NONE == rFntDta.nLang) ?
357                     rFntDta.nFallbackLang : rFntDta.nLang;
358             Font aFont = Application::GetDefaultDevice()->GetDefaultFont(
359                         rFntDta.nFontType, nLang, DEFAULTFONT_FLAGS_ONLYONE );
360 #ifdef DEBUG_TL
361             ByteString aFntName( aFont.GetName(), 1 );
362             int eFntFamily = aFont.GetFamily();
363             ByteString aFntStyleName( aFont.GetStyleName(), 1 );
364             int ePitch = aFont.GetPitch();
365             int eCharSet = aFont.GetCharSet();
366             fprintf(stderr, "\nFontName %s \n", aFntName.GetBuffer() );
367             fprintf(stderr, "StyleName %s \n", aFntStyleName.GetBuffer() );
368             fprintf(stderr, "eFntFamily %d \n", eFntFamily );
369             fprintf(stderr, "ePitch %d \n", ePitch );
370             fprintf(stderr, "eCharSet %d \n", eCharSet );
371 #endif
372             rEditEngineItemPool.SetPoolDefaultItem(
373                     SvxFontItem( aFont.GetFamily(), aFont.GetName(),
374                         aFont.GetStyleName(), aFont.GetPitch(), aFont.GetCharSet(),
375                         rFntDta.nFontInfoId ) );
376         }
377 
378         // set font heights
379         SvxFontHeightItem aFontHeigt(
380                         Application::GetDefaultDevice()->LogicToPixel(
381                         Size( 0, 11 ), MapMode( MAP_POINT ) ).Height(), 100,
382                         EE_CHAR_FONTHEIGHT );
383         rEditEngineItemPool.SetPoolDefaultItem( aFontHeigt );
384         aFontHeigt.SetWhich( EE_CHAR_FONTHEIGHT_CJK );
385         rEditEngineItemPool.SetPoolDefaultItem( aFontHeigt );
386         aFontHeigt.SetWhich( EE_CHAR_FONTHEIGHT_CTL );
387         rEditEngineItemPool.SetPoolDefaultItem( aFontHeigt );
388 }
389 
390 
GetEditEngine()391 EditEngine& SmDocShell::GetEditEngine()
392 {
393     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetEditEngine" );
394 
395 	if (!pEditEngine)
396 	{
397         //!
398         //! see also SmEditWindow::DataChanged !
399         //!
400 
401 		pEditEngineItemPool = EditEngine::CreatePool();
402 
403         SetEditEngineDefaultFonts( *pEditEngine, *pEditEngineItemPool );
404 
405 		pEditEngine = new EditEngine( pEditEngineItemPool );
406 
407 		pEditEngine->EnableUndo( sal_True );
408 		pEditEngine->SetDefTab( sal_uInt16(
409 			Application::GetDefaultDevice()->GetTextWidth( C2S("XXXX") ) ) );
410 
411 		pEditEngine->SetControlWord(
412 				(pEditEngine->GetControlWord() | EE_CNTRL_AUTOINDENTING) &
413 				(~EE_CNTRL_UNDOATTRIBS) &
414 				(~EE_CNTRL_PASTESPECIAL) );
415 
416 		pEditEngine->SetWordDelimiters( C2S(" .=+-*/(){}[];\"" ) );
417 		pEditEngine->SetRefMapMode( MAP_PIXEL );
418 
419 		pEditEngine->SetPaperSize( Size( 800, 0 ) );
420 
421 		pEditEngine->EraseVirtualDevice();
422 
423         // set initial text if the document already has some...
424         // (may be the case when reloading a doc)
425         String aTxt( GetText() );
426         if (aTxt.Len())
427             pEditEngine->SetText( aTxt );
428 
429 		pEditEngine->ClearModifyFlag();
430 
431 		// forces new settings to be used if the itempool was modified
432 		// after cthe creation of the EditEngine
433 		//pEditEngine->Clear();	//#77957 incorrect font size
434 	}
435 	return *pEditEngine;
436 }
437 
438 
GetEditEngineItemPool()439 SfxItemPool& SmDocShell::GetEditEngineItemPool()
440 {
441     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetEditEngineItemPool" );
442 
443 	if (!pEditEngineItemPool)
444 		GetEditEngine();
445 	DBG_ASSERT( pEditEngineItemPool, "EditEngineItemPool missing" );
446 	return *pEditEngineItemPool;
447 }
448 
449 
Draw(OutputDevice & rDev,Point & rPosition)450 void SmDocShell::Draw(OutputDevice &rDev, Point &rPosition)
451 {
452     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Draw" );
453 
454     if (!pTree)
455 		Parse();
456 	DBG_ASSERT(pTree, "Sm : NULL pointer");
457 
458 	if (!IsFormulaArranged())
459 		ArrangeFormula();
460 
461 	//Problem: Was passiert mit dem WYSIWYG? Wir haben waehrend wir inplace aktiv
462 	//sind kein Referenzdevice und sind auch nicht darauf ausgerichtet. Es kann
463 	//also jetzt eine Differenz zwischen der VisArea (spricht die Groesse im Client)
464 	//und der jetzt vorliegenden Groese geben.
465 	//Idee: Die Differenz koennte, zumindest behelfsmaessig, mit SmNod::SetSize
466 	//angepasst werden.
467 
468 	rPosition.X() += aFormat.GetDistance( DIS_LEFTSPACE );
469 	rPosition.Y() += aFormat.GetDistance( DIS_TOPSPACE	);
470 
471     //! in case of high contrast-mode (accessibility option!)
472     //! the draw mode needs to be set to default, because when imbedding
473     //! Math for example in Calc in "a over b" the fraction bar may not
474     //! be visible else. More generally: the FillColor may have been changed.
475     sal_uLong nOldDrawMode = DRAWMODE_DEFAULT;
476     sal_Bool bRestoreDrawMode = sal_False;
477     if (OUTDEV_WINDOW == rDev.GetOutDevType() &&
478         ((Window &) rDev).GetSettings().GetStyleSettings().GetHighContrastMode())
479     {
480         nOldDrawMode = rDev.GetDrawMode();
481         rDev.SetDrawMode( DRAWMODE_DEFAULT );
482         bRestoreDrawMode = sal_True;
483     }
484 
485     // format/draw formulas always from left to right
486     // and numbers should not be converted
487     sal_uLong nLayoutMode = rDev.GetLayoutMode();
488     rDev.SetLayoutMode( TEXT_LAYOUT_BIDI_LTR );
489     sal_Int16 nDigitLang = rDev.GetDigitLanguage();
490     rDev.SetDigitLanguage( LANGUAGE_ENGLISH );
491     //
492     pTree->Draw(rDev, rPosition);
493     //
494     rDev.SetLayoutMode( nLayoutMode );
495     rDev.SetDigitLanguage( nDigitLang );
496 
497     if (bRestoreDrawMode)
498         rDev.SetDrawMode( nOldDrawMode );
499 }
500 
501 
502 
GetSize()503 Size SmDocShell::GetSize()
504 {
505     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetSize" );
506 
507 	Size aRet;
508 
509 	if (!pTree)
510 		Parse();
511 
512 	if (pTree)
513 	{
514 		if (!IsFormulaArranged())
515 			ArrangeFormula();
516 		aRet = pTree->GetSize();
517 
518 		if ( !aRet.Width() )
519 			aRet.Width() = 2000;
520 		else
521 			aRet.Width()  += aFormat.GetDistance( DIS_LEFTSPACE ) +
522 							 aFormat.GetDistance( DIS_RIGHTSPACE );
523 		if ( !aRet.Height() )
524 			aRet.Height() = 1000;
525 		else
526 			aRet.Height() += aFormat.GetDistance( DIS_TOPSPACE ) +
527 							 aFormat.GetDistance( DIS_BOTTOMSPACE );
528 	}
529 
530 	return aRet;
531 }
532 
533 ////////////////////////////////////////
534 
SmPrinterAccess(SmDocShell & rDocShell)535 SmPrinterAccess::SmPrinterAccess( SmDocShell &rDocShell )
536 {
537 	if ( 0 != (pPrinter = rDocShell.GetPrt()) )
538 	{
539 		pPrinter->Push( PUSH_MAPMODE );
540         if ( SFX_CREATE_MODE_EMBEDDED == rDocShell.GetCreateMode() )
541 		{
542 			// if it is an embedded object (without it's own printer)
543 			// we change the MapMode temporarily.
544 			//!If it is a document with it's own printer the MapMode should
545 			//!be set correct (once) elsewhere(!), in order to avoid numerous
546 			//!superfluous pushing and poping of the MapMode when using
547 			//!this class.
548 
549 			const MapUnit eOld = pPrinter->GetMapMode().GetMapUnit();
550 			if ( MAP_100TH_MM != eOld )
551 			{
552 				MapMode aMap( pPrinter->GetMapMode() );
553 				aMap.SetMapUnit( MAP_100TH_MM );
554 				Point aTmp( aMap.GetOrigin() );
555 				aTmp.X() = OutputDevice::LogicToLogic( aTmp.X(), eOld, MAP_100TH_MM );
556 				aTmp.Y() = OutputDevice::LogicToLogic( aTmp.Y(), eOld, MAP_100TH_MM );
557 				aMap.SetOrigin( aTmp );
558 				pPrinter->SetMapMode( aMap );
559 			}
560 		}
561 	}
562     if ( 0 != (pRefDev = rDocShell.GetRefDev()) && pPrinter != pRefDev )
563     {
564         pRefDev->Push( PUSH_MAPMODE );
565         if ( SFX_CREATE_MODE_EMBEDDED == rDocShell.GetCreateMode() )
566         {
567             // if it is an embedded object (without it's own printer)
568             // we change the MapMode temporarily.
569             //!If it is a document with it's own printer the MapMode should
570             //!be set correct (once) elsewhere(!), in order to avoid numerous
571             //!superfluous pushing and poping of the MapMode when using
572             //!this class.
573 
574             const MapUnit eOld = pRefDev->GetMapMode().GetMapUnit();
575             if ( MAP_100TH_MM != eOld )
576             {
577                 MapMode aMap( pRefDev->GetMapMode() );
578                 aMap.SetMapUnit( MAP_100TH_MM );
579                 Point aTmp( aMap.GetOrigin() );
580                 aTmp.X() = OutputDevice::LogicToLogic( aTmp.X(), eOld, MAP_100TH_MM );
581                 aTmp.Y() = OutputDevice::LogicToLogic( aTmp.Y(), eOld, MAP_100TH_MM );
582                 aMap.SetOrigin( aTmp );
583                 pRefDev->SetMapMode( aMap );
584             }
585         }
586     }
587 }
588 
~SmPrinterAccess()589 SmPrinterAccess::~SmPrinterAccess()
590 {
591 	if ( pPrinter )
592 		pPrinter->Pop();
593     if ( pRefDev && pRefDev != pPrinter )
594         pRefDev->Pop();
595 }
596 
597 ////////////////////////////////////////
598 
GetPrt()599 Printer* SmDocShell::GetPrt()
600 {
601     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetPrt" );
602 
603     if ( SFX_CREATE_MODE_EMBEDDED == GetCreateMode() )
604 	{
605 		//Normalerweise wird der Printer vom Server besorgt. Wenn dieser aber
606 		//keinen liefert (weil etwa noch keine connection da ist), kann es
607 		//dennoch sein, dass wir den Printer kennen, denn dieser wird in
608 		//OnDocumentPrinterChanged vom Server durchgereicht und dann temporaer
609 		//festgehalten.
610         Printer *pPrt = GetDocumentPrinter();
611         if ( !pPrt && pTmpPrinter )
612             pPrt = pTmpPrinter;
613         return pPrt;
614 	}
615 	else if ( !pPrinter )
616 	{
617 		SfxItemSet *pOptions =
618 			new SfxItemSet(GetPool(),
619 						   SID_PRINTSIZE,		SID_PRINTSIZE,
620 						   SID_PRINTZOOM,		SID_PRINTZOOM,
621 						   SID_PRINTTITLE,		SID_PRINTTITLE,
622 						   SID_PRINTTEXT,		SID_PRINTTEXT,
623 						   SID_PRINTFRAME,		SID_PRINTFRAME,
624 						   SID_NO_RIGHT_SPACES, SID_NO_RIGHT_SPACES,
625                            SID_SAVE_ONLY_USED_SYMBOLS, SID_SAVE_ONLY_USED_SYMBOLS,
626 						   0);
627 
628         SmModule *pp = SM_MOD();
629 		pp->GetConfig()->ConfigToItemSet(*pOptions);
630 		pPrinter = new SfxPrinter(pOptions);
631 		pPrinter->SetMapMode( MapMode(MAP_100TH_MM) );
632 	}
633 	return pPrinter;
634 }
635 
GetRefDev()636 OutputDevice* SmDocShell::GetRefDev()
637 {
638     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetRefDev" );
639 
640     if ( SFX_CREATE_MODE_EMBEDDED == GetCreateMode() )
641     {
642         OutputDevice* pOutDev = GetDocumentRefDev();
643         if ( pOutDev )
644             return pOutDev;
645     }
646 
647     return GetPrt();
648 }
649 
650 
SetPrinter(SfxPrinter * pNew)651 void SmDocShell::SetPrinter( SfxPrinter *pNew )
652 {
653     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetPrinter" );
654 
655 	delete pPrinter;
656 	pPrinter = pNew;	//Eigentumsuebergang!
657 	pPrinter->SetMapMode( MapMode(MAP_100TH_MM) );
658 	SetFormulaArranged(sal_False);
659     Repaint();
660 }
661 
OnDocumentPrinterChanged(Printer * pPrt)662 void SmDocShell::OnDocumentPrinterChanged( Printer *pPrt )
663 {
664     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::OnDocumentPrinterChanged" );
665 
666     pTmpPrinter = pPrt;
667 	SetFormulaArranged(sal_False);
668 	Size aOldSize = GetVisArea().GetSize();
669     Repaint();
670 	if( aOldSize != GetVisArea().GetSize() && aText.Len() )
671 		SetModified( sal_True );
672 	pTmpPrinter = 0;
673 }
674 
Repaint()675 void SmDocShell::Repaint()
676 {
677     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Repaint" );
678 
679 	sal_Bool bIsEnabled = IsEnableSetModified();
680 	if ( bIsEnabled )
681 		EnableSetModified( sal_False );
682 
683     SetFormulaArranged( sal_False );
684 
685 	Size aVisSize = GetSize();
686 	SetVisAreaSize( aVisSize );
687 	SmViewShell *pViewSh = SmGetActiveView();
688 	if (pViewSh)
689         pViewSh->GetGraphicWindow().Invalidate();
690 
691 	if ( bIsEnabled )
692 		EnableSetModified( bIsEnabled );
693 }
694 
695 
SmDocShell(const sal_uInt64 i_nSfxCreationFlags)696 SmDocShell::SmDocShell( const sal_uInt64 i_nSfxCreationFlags ) :
697 	SfxObjectShell( i_nSfxCreationFlags ),
698 	pTree				( 0 ),
699     pEditEngineItemPool ( 0 ),
700     pEditEngine         ( 0 ),
701 	pPrinter			( 0 ),
702     pTmpPrinter         ( 0 ),
703 	nModifyCount		( 0 ),
704 	bIsFormulaArranged	( sal_False )
705 {
706     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SmDocShell" );
707 
708 	SetPool(&SFX_APP()->GetPool());
709 
710     SmModule *pp = SM_MOD();
711     aFormat = pp->GetConfig()->GetStandardFormat();
712 
713 	StartListening(aFormat);
714 	StartListening(*pp->GetConfig());
715 
716 	SetBaseModel( new SmModel(this) );
717 }
718 
719 
720 
~SmDocShell()721 SmDocShell::~SmDocShell()
722 {
723     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::~SmDocShell" );
724 
725     SmModule *pp = SM_MOD();
726 
727 	EndListening(aFormat);
728 	EndListening(*pp->GetConfig());
729 
730 	delete pEditEngine;
731 	SfxItemPool::Free(pEditEngineItemPool);
732 	delete pTree;
733 	delete pPrinter;
734 }
735 
736 
SetData(const String & rData)737 sal_Bool SmDocShell::SetData( const String& rData )
738 {
739     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetData" );
740 
741 	SetText( rData );
742 	return sal_True;
743 }
744 
745 
ConvertFrom(SfxMedium & rMedium)746 sal_Bool SmDocShell::ConvertFrom(SfxMedium &rMedium)
747 {
748     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::ConvertFrom" );
749 
750 	sal_Bool	 bSuccess = sal_False;
751 	const String& rFltName = rMedium.GetFilter()->GetFilterName();
752 
753     DBG_ASSERT( !rFltName.EqualsAscii( STAROFFICE_XML ), "Wrong filter!");
754 
755     if ( rFltName.EqualsAscii( MATHML_XML ) )
756 	{
757 		if (pTree)
758 		{
759 			delete pTree;
760 			pTree = 0;
761 		}
762         Reference<com::sun::star::frame::XModel> xModel(GetModel());
763         SmXMLImportWrapper aEquation(xModel);
764         bSuccess = 0 == aEquation.Import(rMedium);
765 	}
766 	else
767 	{
768 		SvStream *pStream = rMedium.GetInStream();
769         if ( pStream )
770 		{
771             if ( SotStorage::IsStorageFile( pStream ) )
772             {
773                 SvStorageRef aStorage = new SotStorage( pStream, sal_False );
774                 if ( aStorage->IsStream( C2S( "Equation Native" ) ) )
775                 {
776                     // is this a MathType Storage?
777                     MathType aEquation( aText );
778                     if ( sal_True == (bSuccess = (1 == aEquation.Parse( aStorage )) ))
779                         Parse();
780                 }
781             }
782             else
783             {
784                 //bSuccess = ImportSM20File( pStream );
785             }
786 		}
787 	}
788 
789     if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
790     {
791         //???OnDocumentPrinterChanged(0);
792         SetFormulaArranged( sal_False );
793         Repaint();
794     }
795 
796 	FinishedLoading( SFX_LOADED_ALL );
797 	return bSuccess;
798 }
799 
800 
InitNew(const uno::Reference<embed::XStorage> & xStorage)801 sal_Bool SmDocShell::InitNew( const uno::Reference < embed::XStorage >& xStorage )
802 {
803     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::InitNew" );
804 
805 	sal_Bool bRet = sal_False;
806     if ( SfxObjectShell::InitNew( xStorage ) )
807 	{
808 		bRet = sal_True;
809 		SetVisArea(Rectangle(Point(0, 0), Size(2000, 1000)));
810 	}
811 	return bRet;
812 }
813 
814 
Load(SfxMedium & rMedium)815 sal_Bool SmDocShell::Load( SfxMedium& rMedium )
816 {
817     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Load" );
818 
819 	sal_Bool bRet = sal_False;
820     if( SfxObjectShell::Load( rMedium ))
821 	{
822         uno::Reference < embed::XStorage > xStorage = GetMedium()->GetStorage();
823         uno::Reference < container::XNameAccess > xAccess (xStorage, uno::UNO_QUERY);
824         if (
825             (
826              xAccess->hasByName( C2S( "content.xml" ) ) &&
827              xStorage->isStreamElement( C2S( "content.xml" ) )
828             ) ||
829             (
830              xAccess->hasByName( C2S( "Content.xml" ) ) &&
831              xStorage->isStreamElement( C2S( "Content.xml" ) )
832             )
833            )
834         {
835             // is this a fabulous math package ?
836             Reference<com::sun::star::frame::XModel> xModel(GetModel());
837             SmXMLImportWrapper aEquation(xModel);
838             sal_uLong nError = aEquation.Import(rMedium);
839             bRet = 0 == nError;
840             SetError( nError, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
841         }
842 	}
843 
844     if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
845     {
846         //???OnDocumentPrinterChanged(0);
847 		SetFormulaArranged( sal_False );
848         Repaint();
849     }
850 
851 	FinishedLoading( SFX_LOADED_ALL );
852 	return bRet;
853 }
854 
855 //------------------------------------------------------------------
856 
Save()857 sal_Bool SmDocShell::Save()
858 {
859     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Save" );
860 
861     //! apply latest changes if necessary
862     UpdateText();
863 
864     if ( SfxObjectShell::Save() )
865 	{
866 		if (!pTree)
867 			Parse();
868 		if( pTree && !IsFormulaArranged() )
869 			ArrangeFormula();
870 
871         Reference<com::sun::star::frame::XModel> xModel(GetModel());
872         SmXMLExportWrapper aEquation(xModel);
873         aEquation.SetFlat(sal_False);
874         return aEquation.Export(*GetMedium());
875 	}
876 
877 	return sal_False;
878 }
879 
880 /*
881  * replace bad characters that can not be saved. (#i74144)
882  * */
ReplaceBadChars()883 sal_Bool SmDocShell::ReplaceBadChars()
884 {
885 	sal_Bool bReplace = sal_False;
886 	if (pEditEngine)
887 	{
888 		String aEngTxt( pEditEngine->GetText( LINEEND_LF ) );
889 		const sal_Unicode *pEngTxt = aEngTxt.GetBuffer();
890 		xub_StrLen nLen = aEngTxt.Len();
891 		for (xub_StrLen i = 0;  i < nLen && !bReplace;  ++i)
892 		{
893 			const sal_Unicode c = *pEngTxt++;
894 			if (c < ' ' && c != '\r' && c != '\n' && c != '\t')
895 				bReplace = sal_True;
896 		}
897 		if (bReplace)
898 		{
899 			sal_Unicode *pChgTxt = aEngTxt.GetBufferAccess();
900 			for (xub_StrLen i = 0;  i < nLen;  ++i)
901 			{
902 				sal_Unicode &rc = *pChgTxt++;
903 				if (rc < ' ' && rc != '\r' && rc != '\n' && rc != '\t')
904 					rc = ' ';
905 			}
906 			aEngTxt.ReleaseBufferAccess( nLen );
907 
908 			aText = aEngTxt;
909 		}
910 	}
911 	return bReplace;
912 }
913 
914 
UpdateText()915 void SmDocShell::UpdateText()
916 {
917     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::UpdateText" );
918 
919     if (pEditEngine && pEditEngine->IsModified())
920     {
921         String aEngTxt( pEditEngine->GetText( LINEEND_LF ) );
922         if (GetText() != aEngTxt)
923             SetText( aEngTxt );
924     }
925 }
926 
927 
SaveAs(SfxMedium & rMedium)928 sal_Bool SmDocShell::SaveAs( SfxMedium& rMedium )
929 {
930     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SaveAs" );
931 
932 	sal_Bool bRet = sal_False;
933 
934     //! apply latest changes if necessary
935     UpdateText();
936 
937 	if ( SfxObjectShell::SaveAs( rMedium ) )
938 	{
939 		if (!pTree)
940 			Parse();
941 		if( pTree && !IsFormulaArranged() )
942 			ArrangeFormula();
943 
944         Reference<com::sun::star::frame::XModel> xModel(GetModel());
945         SmXMLExportWrapper aEquation(xModel);
946         aEquation.SetFlat(sal_False);
947         bRet = aEquation.Export(rMedium);
948 	}
949 	return bRet;
950 }
951 
ConvertTo(SfxMedium & rMedium)952 sal_Bool SmDocShell::ConvertTo( SfxMedium &rMedium )
953 {
954     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::ConvertTo" );
955 
956 	sal_Bool bRet = sal_False;
957 	const SfxFilter* pFlt = rMedium.GetFilter();
958 	if( pFlt )
959 	{
960 		if( !pTree )
961 			Parse();
962 		if( pTree && !IsFormulaArranged() )
963 			ArrangeFormula();
964 
965 		const String& rFltName = pFlt->GetFilterName();
966 		if(rFltName.EqualsAscii( STAROFFICE_XML ))
967 		{
968             Reference<com::sun::star::frame::XModel> xModel(GetModel());
969             SmXMLExportWrapper aEquation(xModel);
970 			aEquation.SetFlat(sal_False);
971 			bRet = aEquation.Export(rMedium);
972 		}
973 		else if(rFltName.EqualsAscii( MATHML_XML ))
974 		{
975             Reference<com::sun::star::frame::XModel> xModel(GetModel());
976             SmXMLExportWrapper aEquation(xModel);
977 			aEquation.SetFlat(sal_True);
978 			bRet = aEquation.Export(rMedium);
979 		}
980         else if( pFlt->GetFilterName().EqualsAscii("MathType 3.x"))
981             bRet = WriteAsMathType3( rMedium );
982 	}
983 	return bRet;
984 }
985 
SaveCompleted(const::com::sun::star::uno::Reference<::com::sun::star::embed::XStorage> & xStorage)986 sal_Bool SmDocShell::SaveCompleted( const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage )
987 {
988     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SaveCompleted" );
989 
990     if( SfxObjectShell::SaveCompleted( xStorage ))
991 		return sal_True;
992 
993 	return sal_False;
994 }
995 
996 
Execute(SfxRequest & rReq)997 void SmDocShell::Execute(SfxRequest& rReq)
998 {
999     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Execute" );
1000 
1001 	switch (rReq.GetSlot())
1002 	{
1003 		case SID_TEXTMODE:
1004 		{
1005             SmFormat aOldFormat  = GetFormat();
1006             SmFormat aNewFormat( aOldFormat );
1007             aNewFormat.SetTextmode(!aOldFormat.IsTextmode());
1008 
1009             ::svl::IUndoManager *pTmpUndoMgr = GetUndoManager();
1010             if (pTmpUndoMgr)
1011                 pTmpUndoMgr->AddUndoAction(
1012                     new SmFormatAction(this, aOldFormat, aNewFormat));
1013 
1014             SetFormat( aNewFormat );
1015             Repaint();
1016 		}
1017 		break;
1018 
1019 		case SID_AUTO_REDRAW :
1020 		{
1021             SmModule *pp = SM_MOD();
1022 			sal_Bool bRedraw = pp->GetConfig()->IsAutoRedraw();
1023 			pp->GetConfig()->SetAutoRedraw(!bRedraw);
1024 		}
1025 		break;
1026 
1027 		case SID_LOADSYMBOLS:
1028             LoadSymbols();
1029 		break;
1030 
1031 		case SID_SAVESYMBOLS:
1032             SaveSymbols();
1033 		break;
1034 
1035 		case SID_FONT:
1036 		{
1037             // get device used to retrieve the FontList
1038             OutputDevice *pDev = GetPrinter();
1039             if (!pDev || pDev->GetDevFontCount() == 0)
1040                 pDev = &SM_MOD()->GetDefaultVirtualDev();
1041             DBG_ASSERT (pDev, "device for font list missing" );
1042 
1043             SmFontTypeDialog *pFontTypeDialog = new SmFontTypeDialog( NULL, pDev );
1044 
1045             SmFormat aOldFormat  = GetFormat();
1046             pFontTypeDialog->ReadFrom( aOldFormat );
1047 			if (pFontTypeDialog->Execute() == RET_OK)
1048 			{
1049                 SmFormat aNewFormat( aOldFormat );
1050 
1051 				pFontTypeDialog->WriteTo(aNewFormat);
1052                 ::svl::IUndoManager *pTmpUndoMgr = GetUndoManager();
1053                 if (pTmpUndoMgr)
1054                     pTmpUndoMgr->AddUndoAction(
1055 						new SmFormatAction(this, aOldFormat, aNewFormat));
1056 
1057 				SetFormat( aNewFormat );
1058                 Repaint();
1059 			}
1060 			delete pFontTypeDialog;
1061 		}
1062 		break;
1063 
1064 		case SID_FONTSIZE:
1065 		{
1066 			SmFontSizeDialog *pFontSizeDialog = new SmFontSizeDialog(NULL);
1067 
1068             SmFormat aOldFormat  = GetFormat();
1069             pFontSizeDialog->ReadFrom( aOldFormat );
1070 			if (pFontSizeDialog->Execute() == RET_OK)
1071 			{
1072                 SmFormat aNewFormat( aOldFormat );
1073 
1074 				pFontSizeDialog->WriteTo(aNewFormat);
1075 
1076                 ::svl::IUndoManager *pTmpUndoMgr = GetUndoManager();
1077                 if (pTmpUndoMgr)
1078                     pTmpUndoMgr->AddUndoAction(
1079 						new SmFormatAction(this, aOldFormat, aNewFormat));
1080 
1081 				SetFormat( aNewFormat );
1082                 Repaint();
1083 			}
1084 			delete pFontSizeDialog;
1085 		}
1086 		break;
1087 
1088 		case SID_DISTANCE:
1089 		{
1090 			SmDistanceDialog *pDistanceDialog = new SmDistanceDialog(NULL);
1091 
1092             SmFormat aOldFormat  = GetFormat();
1093             pDistanceDialog->ReadFrom( aOldFormat );
1094 			if (pDistanceDialog->Execute() == RET_OK)
1095 			{
1096                 SmFormat aNewFormat( aOldFormat );
1097 
1098 				pDistanceDialog->WriteTo(aNewFormat);
1099 
1100                 ::svl::IUndoManager *pTmpUndoMgr = GetUndoManager();
1101                 if (pTmpUndoMgr)
1102                     pTmpUndoMgr->AddUndoAction(
1103 						new SmFormatAction(this, aOldFormat, aNewFormat));
1104 
1105 				SetFormat( aNewFormat );
1106                 Repaint();
1107 			}
1108 			delete pDistanceDialog;
1109 		}
1110 		break;
1111 
1112 		case SID_ALIGN:
1113 		{
1114 			SmAlignDialog *pAlignDialog = new SmAlignDialog(NULL);
1115 
1116             SmFormat aOldFormat  = GetFormat();
1117             pAlignDialog->ReadFrom( aOldFormat );
1118 			if (pAlignDialog->Execute() == RET_OK)
1119 			{
1120                 SmFormat aNewFormat( aOldFormat );
1121 
1122 				pAlignDialog->WriteTo(aNewFormat);
1123 
1124                 SmModule *pp = SM_MOD();
1125                 SmFormat aFmt( pp->GetConfig()->GetStandardFormat() );
1126                 pAlignDialog->WriteTo( aFmt );
1127                 pp->GetConfig()->SetStandardFormat( aFmt );
1128 
1129                 ::svl::IUndoManager *pTmpUndoMgr = GetUndoManager();
1130                 if (pTmpUndoMgr)
1131                     pTmpUndoMgr->AddUndoAction(
1132                         new SmFormatAction(this, aOldFormat, aNewFormat));
1133 
1134 				SetFormat( aNewFormat );
1135                 Repaint();
1136 			}
1137 			delete pAlignDialog;
1138 		}
1139 		break;
1140 
1141 		case SID_TEXT:
1142 		{
1143 			const SfxStringItem& rItem = (const SfxStringItem&)rReq.GetArgs()->Get(SID_TEXT);
1144 			if (GetText() != rItem.GetValue())
1145 				SetText(rItem.GetValue());
1146 		}
1147 		break;
1148 
1149 		case SID_UNDO:
1150 		case SID_REDO:
1151 		{
1152             ::svl::IUndoManager* pTmpUndoMgr = GetUndoManager();
1153             if( pTmpUndoMgr )
1154 			{
1155 				sal_uInt16 nId = rReq.GetSlot(), nCnt = 1;
1156 				const SfxItemSet* pArgs = rReq.GetArgs();
1157 				const SfxPoolItem* pItem;
1158 				if( pArgs && SFX_ITEM_SET == pArgs->GetItemState( nId, sal_False, &pItem ))
1159 					nCnt = ((SfxUInt16Item*)pItem)->GetValue();
1160 
1161 				sal_Bool (::svl::IUndoManager:: *fnDo)();
1162 
1163 				sal_uInt16 nCount;
1164 				if( SID_UNDO == rReq.GetSlot() )
1165 				{
1166                     nCount = pTmpUndoMgr->GetUndoActionCount();
1167 					fnDo = &::svl::IUndoManager::Undo;
1168 				}
1169 				else
1170 				{
1171                     nCount = pTmpUndoMgr->GetRedoActionCount();
1172 					fnDo = &::svl::IUndoManager::Redo;
1173 				}
1174 
1175                 try
1176                 {
1177 				    for( ; nCnt && nCount; --nCnt, --nCount )
1178                         (pTmpUndoMgr->*fnDo)();
1179                 }
1180                 catch( const Exception& e )
1181                 {
1182                     DBG_UNHANDLED_EXCEPTION();
1183                 }
1184 			}
1185 
1186             SmModule  *pModule = SM_MOD();
1187             if ( pModule && pModule->GetConfig()->IsAutoRedraw() )
1188                 UpdateText();
1189             Repaint();
1190 			SfxViewFrame* pFrm = SfxViewFrame::GetFirst( this );
1191 			while( pFrm )
1192 			{
1193 				SfxBindings& rBind = pFrm->GetBindings();
1194 				rBind.Invalidate(SID_UNDO);
1195 				rBind.Invalidate(SID_REDO);
1196 				rBind.Invalidate(SID_REPEAT);
1197 				rBind.Invalidate(SID_CLEARHISTORY);
1198 				pFrm = SfxViewFrame::GetNext( *pFrm, this );
1199 			}
1200 		}
1201 		break;
1202 	}
1203 
1204 	rReq.Done();
1205 }
1206 
1207 
GetState(SfxItemSet & rSet)1208 void SmDocShell::GetState(SfxItemSet &rSet)
1209 {
1210     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetState" );
1211 
1212 	SfxWhichIter aIter(rSet);
1213 
1214 	for (sal_uInt16 nWh = aIter.FirstWhich();  0 != nWh;  nWh = aIter.NextWhich())
1215 	{
1216 		switch (nWh)
1217 		{
1218 		case SID_TEXTMODE:
1219 			rSet.Put(SfxBoolItem(SID_TEXTMODE, GetFormat().IsTextmode()));
1220 			break;
1221 
1222 		case SID_DOCTEMPLATE :
1223 			rSet.DisableItem(SID_DOCTEMPLATE);
1224 			break;
1225 
1226 		case SID_AUTO_REDRAW :
1227 			{
1228                 SmModule  *pp = SM_MOD();
1229 				sal_Bool	   bRedraw = pp->GetConfig()->IsAutoRedraw();
1230 
1231 				rSet.Put(SfxBoolItem(SID_AUTO_REDRAW, bRedraw));
1232 			}
1233 			break;
1234 
1235 		case SID_MODIFYSTATUS:
1236 			{
1237 				sal_Unicode cMod = ' ';
1238 				if (IsModified())
1239 					cMod = '*';
1240 				rSet.Put(SfxStringItem(SID_MODIFYSTATUS, String(cMod)));
1241 			}
1242 			break;
1243 
1244 		case SID_TEXT:
1245 			rSet.Put(SfxStringItem(SID_TEXT, GetText()));
1246 			break;
1247 
1248         case SID_GAPHIC_SM:
1249             //! very old (pre UNO) and ugly hack to invalidate the SmGraphicWindow.
1250             //! If nModifyCount gets changed then the call below will implicitly notify
1251             //! SmGraphicController::StateChanged and there the window gets invalidated.
1252             //! Thus all the 'nModifyCount++' before invalidating this slot.
1253             rSet.Put(SfxInt16Item(SID_GAPHIC_SM, nModifyCount));
1254 			break;
1255 
1256 		case SID_UNDO:
1257 		case SID_REDO:
1258 			{
1259 				SfxViewFrame* pFrm = SfxViewFrame::GetFirst( this );
1260 				if( pFrm )
1261 					pFrm->GetSlotState( nWh, NULL, &rSet );
1262 				else
1263 					rSet.DisableItem( nWh );
1264 			}
1265 			break;
1266 
1267 		case SID_GETUNDOSTRINGS:
1268 		case SID_GETREDOSTRINGS:
1269 			{
1270                 ::svl::IUndoManager* pTmpUndoMgr = GetUndoManager();
1271                 if( pTmpUndoMgr )
1272 				{
1273 					UniString(::svl::IUndoManager:: *fnGetComment)( size_t, bool const ) const;
1274 
1275 					sal_uInt16 nCount;
1276 					if( SID_GETUNDOSTRINGS == nWh )
1277 					{
1278                         nCount = pTmpUndoMgr->GetUndoActionCount();
1279 						fnGetComment = &::svl::IUndoManager::GetUndoActionComment;
1280 					}
1281 					else
1282 					{
1283                         nCount = pTmpUndoMgr->GetRedoActionCount();
1284 						fnGetComment = &::svl::IUndoManager::GetRedoActionComment;
1285 					}
1286 					if( nCount )
1287 					{
1288 						String sList;
1289 						for( sal_uInt16 n = 0; n < nCount; ++n )
1290                             ( sList += (pTmpUndoMgr->*fnGetComment)( n, ::svl::IUndoManager::TopLevel ) )
1291 									+= '\n';
1292 
1293 						SfxStringListItem aItem( nWh );
1294 						aItem.SetString( sList );
1295 						rSet.Put( aItem );
1296 					}
1297 				}
1298 				else
1299 					rSet.DisableItem( nWh );
1300 			}
1301 			break;
1302 		}
1303 	}
1304 }
1305 
1306 
GetUndoManager()1307 ::svl::IUndoManager *SmDocShell::GetUndoManager()
1308 {
1309     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::GetUndoManager" );
1310 
1311 	if (!pEditEngine)
1312 		GetEditEngine();
1313 	return &pEditEngine->GetUndoManager();
1314 }
1315 
1316 
SaveSymbols()1317 void SmDocShell::SaveSymbols()
1318 {
1319     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SaveSymbols" );
1320 
1321     SmModule *pp = SM_MOD();
1322     pp->GetSymbolManager().Save();
1323 }
1324 
1325 
Draw(OutputDevice * pDevice,const JobSetup &,sal_uInt16)1326 void SmDocShell::Draw(OutputDevice *pDevice,
1327 					  const JobSetup &,
1328                       sal_uInt16 /*nAspect*/)
1329 {
1330     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::Draw" );
1331 
1332 	pDevice->IntersectClipRegion(GetVisArea());
1333 	Point atmppoint;
1334 	Draw(*pDevice, atmppoint);
1335 }
1336 
GetPool() const1337 SfxItemPool& SmDocShell::GetPool() const
1338 {
1339 	return SFX_APP()->GetPool();
1340 }
1341 
SetVisArea(const Rectangle & rVisArea)1342 void SmDocShell::SetVisArea(const Rectangle & rVisArea)
1343 {
1344     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetVisArea" );
1345 
1346 	Rectangle aNewRect(rVisArea);
1347 
1348 	aNewRect.SetPos(Point());
1349 
1350 	if (! aNewRect.Right()) aNewRect.Right() = 2000;
1351 	if (! aNewRect.Bottom()) aNewRect.Bottom() = 1000;
1352 
1353 	sal_Bool bIsEnabled = IsEnableSetModified();
1354 	if ( bIsEnabled )
1355 		EnableSetModified( sal_False );
1356 
1357     //TODO/LATER: it's unclear how this interacts with the SFX code
1358     // If outplace editing, then dont resize the OutplaceWindow. But the
1359 	// ObjectShell has to resize. Bug 56470
1360 	sal_Bool bUnLockFrame;
1361     if( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED && !IsInPlaceActive() && GetFrame() )
1362 	{
1363 		GetFrame()->LockAdjustPosSizePixel();
1364 		bUnLockFrame = sal_True;
1365 	}
1366 	else
1367 		bUnLockFrame = sal_False;
1368 
1369     SfxObjectShell::SetVisArea( aNewRect );
1370 
1371 	if( bUnLockFrame )
1372 		GetFrame()->UnlockAdjustPosSizePixel();
1373 
1374 	if ( bIsEnabled )
1375 		EnableSetModified( bIsEnabled );
1376 }
1377 
1378 
FillClass(SvGlobalName * pClassName,sal_uInt32 * pFormat,String *,String * pFullTypeName,String * pShortTypeName,sal_Int32 nFileFormat,sal_Bool bTemplate) const1379 void SmDocShell::FillClass(SvGlobalName* pClassName,
1380 						   sal_uInt32*  pFormat,
1381                            String* /*pAppName*/,
1382 						   String* pFullTypeName,
1383                            String* pShortTypeName,
1384                            sal_Int32 nFileFormat,
1385                            sal_Bool bTemplate /* = sal_False */) const
1386 {
1387     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::FillClass" );
1388 
1389 	if (nFileFormat == SOFFICE_FILEFORMAT_60 )
1390 	{
1391 		*pClassName 	= SvGlobalName(SO3_SM_CLASSID_60);
1392 		*pFormat		= SOT_FORMATSTR_ID_STARMATH_60;
1393         *pFullTypeName  = String(SmResId(STR_MATH_DOCUMENT_FULLTYPE_CURRENT));
1394 		*pShortTypeName = String(SmResId(RID_DOCUMENTSTR));
1395 	}
1396 	else if (nFileFormat == SOFFICE_FILEFORMAT_8 )
1397 	{
1398 		*pClassName 	= SvGlobalName(SO3_SM_CLASSID_60);
1399         *pFormat		= bTemplate ? SOT_FORMATSTR_ID_STARMATH_8_TEMPLATE : SOT_FORMATSTR_ID_STARMATH_8;
1400         *pFullTypeName  = String(SmResId(STR_MATH_DOCUMENT_FULLTYPE_CURRENT));
1401 		*pShortTypeName = String(SmResId(RID_DOCUMENTSTR));
1402 	}
1403 }
1404 
GetMiscStatus() const1405 sal_uLong SmDocShell::GetMiscStatus() const
1406 {
1407     return SfxObjectShell::GetMiscStatus() | SVOBJ_MISCSTATUS_NOTRESIZEABLE
1408 											 | SVOBJ_MISCSTATUS_RESIZEONPRINTERCHANGE;
1409 }
1410 
SetModified(sal_Bool bModified)1411 void SmDocShell::SetModified(sal_Bool bModified)
1412 {
1413     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::SetModified" );
1414 
1415 	if( IsEnableSetModified() )
1416 	{
1417 		SfxObjectShell::SetModified( bModified );
1418 		Broadcast(SfxSimpleHint(SFX_HINT_DOCCHANGED));
1419 	}
1420 }
1421 
WriteAsMathType3(SfxMedium & rMedium)1422 sal_Bool SmDocShell::WriteAsMathType3( SfxMedium& rMedium )
1423 {
1424     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmDocShell::WriteAsMathType3" );
1425 
1426     MathType aEquation( aText, pTree );
1427 
1428     sal_Bool bRet = 0 != aEquation.ConvertFromStarMath( rMedium );
1429     return bRet;
1430 }
1431 
1432 
1433