xref: /trunk/main/starmath/source/view.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_starmath.hxx"
30 
31 
32 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
33 #include <com/sun/star/accessibility/AccessibleEventObject.hpp>
34 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
35 #include <com/sun/star/accessibility/XAccessible.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/frame/XDesktop.hpp>
38 #include <com/sun/star/frame/XFramesSupplier.hpp>
39 #include <com/sun/star/container/XChild.hpp>
40 
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/storagehelper.hxx>
43 #include <rtl/logfile.hxx>
44 #include <sfx2/app.hxx>
45 #include <sfx2/dispatch.hxx>
46 #include <sfx2/docfile.hxx>
47 #include <sfx2/docfilt.hxx>
48 #include <sfx2/docinsert.hxx>
49 #include <sfx2/filedlghelper.hxx>
50 #include <sfx2/msg.hxx>
51 #include <sfx2/objface.hxx>
52 #include <sfx2/printer.hxx>
53 #include <sfx2/request.hxx>
54 #include <svl/eitem.hxx>
55 #include <svl/intitem.hxx>
56 #include <svl/itemset.hxx>
57 #include <svl/poolitem.hxx>
58 #include <svl/ptitem.hxx>
59 #include <svl/stritem.hxx>
60 #include <svtools/transfer.hxx>
61 #include <svl/undo.hxx>
62 #include <svl/whiter.hxx>
63 #include <svx/dialogs.hrc>
64 #include <editeng/editeng.hxx>
65 #include <svx/svxdlg.hxx>
66 #include <svx/zoomitem.hxx>
67 #include <vcl/decoview.hxx>
68 #include <vcl/menu.hxx>
69 #include <vcl/msgbox.hxx>
70 #include <vcl/wrkwin.hxx>
71 
72 #include "unomodel.hxx"
73 #include "view.hxx"
74 #include "config.hxx"
75 #include "dialog.hxx"
76 #include "document.hxx"
77 #include "starmath.hrc"
78 #include "toolbox.hxx"
79 #include "mathmlimport.hxx"
80 
81 
82 #define MINWIDTH        200
83 #define MINHEIGHT       200
84 #define MINSPLIT        40
85 #define SPLITTERWIDTH   2
86 
87 #define MINZOOM 25
88 #define MAXZOOM 800
89 
90 #define SmViewShell
91 #include "smslots.hxx"
92 
93 using namespace com::sun::star;
94 using namespace com::sun::star::accessibility;
95 using namespace com::sun::star::uno;
96 
97 //////////////////////////////////////////////////////////////////////
98 
99 SmGraphicWindow::SmGraphicWindow(SmViewShell* pShell):
100     ScrollableWindow(&pShell->GetViewFrame()->GetWindow(), 0),
101     pAccessible(0),
102     pViewShell(pShell),
103     nZoom(100),
104     bIsCursorVisible(sal_False)
105 {
106     // docking windows are usually hidden (often already done in the
107     // resource) and will be shown by the sfx framework.
108     Hide();
109 
110     const Fraction aFraction (1,1);
111     SetMapMode( MapMode(MAP_100TH_MM, Point(), aFraction, aFraction));
112 
113     ApplyColorConfigValues( SM_MOD()->GetColorConfig() );
114 
115     SetTotalSize();
116 
117     SetHelpId(HID_SMA_WIN_DOCUMENT);
118     SetUniqueId(HID_SMA_WIN_DOCUMENT);
119 }
120 
121 SmGraphicWindow::~SmGraphicWindow()
122 {
123     if (pAccessible)
124         pAccessible->ClearWin();    // make Accessible defunctional
125     // Note: memory for pAccessible will be freed when the reference
126     // xAccessible is released.
127 }
128 
129 void SmGraphicWindow::StateChanged( StateChangedType eType )
130 {
131     if ( eType == STATE_CHANGE_INITSHOW )
132         Show();
133     ScrollableWindow::StateChanged( eType );
134 }
135 
136 
137 void SmGraphicWindow::ApplyColorConfigValues( const svtools::ColorConfig &rColorCfg )
138 {
139     // Note: SetTextColor not necessary since the nodes that
140     // get painted have the color information.
141 #if OSL_DEBUG_LEVEL > 1
142 //   ColorData nVal = rColorCfg.GetColorValue(svtools::DOCCOLOR).nColor;
143 #endif
144     SetBackground( Color( (ColorData) rColorCfg.GetColorValue(svtools::DOCCOLOR).nColor ) );
145     Invalidate();
146 }
147 
148 
149 void SmGraphicWindow::DataChanged( const DataChangedEvent& rEvt )
150 {
151     ApplyColorConfigValues( SM_MOD()->GetColorConfig() );
152 
153     ScrollableWindow::DataChanged( rEvt );
154 }
155 
156 
157 void SmGraphicWindow::MouseButtonDown(const MouseEvent& rMEvt)
158 {
159     ScrollableWindow::MouseButtonDown(rMEvt);
160 
161     //
162     // set formula-cursor and selection of edit window according to the
163     // position clicked at
164     //
165     DBG_ASSERT(rMEvt.GetClicks() > 0, "Sm : 0 clicks");
166     if ( rMEvt.IsLeft() && pViewShell->GetEditWindow() )
167     {
168         const SmNode *pTree = pViewShell->GetDoc()->GetFormulaTree();
169         //! kann NULL sein! ZB wenn bereits beim laden des Dokuments (bevor der
170         //! Parser angeworfen wurde) ins Fenster geklickt wird.
171         if (!pTree)
172             return;
173 
174         // get click position relativ to formula
175         Point  aPos (PixelToLogic(rMEvt.GetPosPixel())
176                      - GetFormulaDrawPos());
177 
178         // if it was clicked inside the formula then get the appropriate node
179         const SmNode *pNode = 0;
180         if (pTree->OrientedDist(aPos) <= 0)
181             pNode = pTree->FindRectClosestTo(aPos);
182 
183         if (pNode)
184         {   SmEditWindow  *pEdit = pViewShell->GetEditWindow();
185             const SmToken  aToken (pNode->GetToken());
186 
187 #ifdef notnow
188             // include introducing symbols of special char and text
189             // (ie '%' and '"')
190             sal_uInt16  nExtra = (aToken.eType == TSPECIAL  ||  aToken.eType == TTEXT) ? 1 : 0;
191 
192             // set selection to the beginning of the token
193             ESelection  aSel (aToken.nRow - 1, aToken.nCol - 1 - nExtra);
194 
195             if (rMEvt.GetClicks() != 1)
196             {   // select whole token
197                 // for text include terminating symbol (ie '"')
198                 aSel.nEndPos += aToken.aText.Len() + nExtra
199                                 + (aToken.eType == TTEXT ? 1 : 0);
200             }
201 #endif
202             // set selection to the beginning of the token
203             ESelection  aSel (aToken.nRow - 1, aToken.nCol - 1);
204 
205             if (rMEvt.GetClicks() != 1 || aToken.eType == TPLACE)
206                 aSel.nEndPos = aSel.nEndPos + sal::static_int_cast< sal_uInt16 >(aToken.aText.Len());
207 
208             pEdit->SetSelection(aSel);
209             SetCursor(pNode);
210 
211             // allow for immediate editing and
212             //! implicitly synchronize the cursor position mark in this window
213             pEdit->GrabFocus();
214         }
215     }
216 }
217 
218 void SmGraphicWindow::GetFocus()
219 {
220 /*
221     if (xAccessible.is())
222     {
223         uno::Any aOldValue, aNewValue;
224         // aOldValue remains empty
225         aNewValue <<= AccessibleStateType::FOCUSED;
226         pAccessible->LaunchEvent( AccessibleEventId::STATE_CHANGED,
227                 aOldValue, aNewValue );
228     }
229 */
230 }
231 
232 void SmGraphicWindow::LoseFocus()
233 {
234     ScrollableWindow::LoseFocus();
235     if (xAccessible.is())
236     {
237         uno::Any aOldValue, aNewValue;
238         aOldValue <<= AccessibleStateType::FOCUSED;
239         // aNewValue remains empty
240         pAccessible->LaunchEvent( AccessibleEventId::STATE_CHANGED,
241                 aOldValue, aNewValue );
242     }
243 }
244 
245 void SmGraphicWindow::ShowCursor(sal_Bool bShow)
246     // shows or hides the formula-cursor depending on 'bShow' is sal_True or not
247 {
248     sal_Bool  bInvert = bShow != IsCursorVisible();
249 
250     if (bInvert)
251         InvertTracking(aCursorRect, SHOWTRACK_SMALL | SHOWTRACK_WINDOW);
252 
253     SetIsCursorVisible(bShow);
254 }
255 
256 
257 void SmGraphicWindow::SetCursor(const SmNode *pNode)
258 {
259     const SmNode *pTree = pViewShell->GetDoc()->GetFormulaTree();
260 
261     // get appropriate rectangle
262     Point aOffset (pNode->GetTopLeft() - pTree->GetTopLeft()),
263           aTLPos  (GetFormulaDrawPos() + aOffset);
264     aTLPos.X() -= pNode->GetItalicLeftSpace();
265     Size  aSize   (pNode->GetItalicSize());
266     Point aBRPos  (aTLPos.X() + aSize.Width(), aTLPos.Y() + aSize.Height());
267 
268     SetCursor(Rectangle(aTLPos, aSize));
269 }
270 
271 void SmGraphicWindow::SetCursor(const Rectangle &rRect)
272     // sets cursor to new position (rectangle) 'rRect'.
273     // The old cursor will be removed, and the new one will be shown if
274     // that is activated in the ConfigItem
275 {
276     SmModule *pp = SM_MOD();
277 
278     if (IsCursorVisible())
279         ShowCursor(sal_False);      // clean up remainings of old cursor
280     aCursorRect = rRect;
281     if (pp->GetConfig()->IsShowFormulaCursor())
282         ShowCursor(sal_True);       // draw new cursor
283 }
284 
285 const SmNode * SmGraphicWindow::SetCursorPos(sal_uInt16 nRow, sal_uInt16 nCol)
286     // looks for a VISIBLE node in the formula tree with it's token at
287     // (or around) the position 'nRow', 'nCol' in the edit window
288     // (row and column numbering starts with 1 there!).
289     // If there is such a node the formula-cursor is set to cover that nodes
290     // rectangle. If not the formula-cursor will be hidden.
291     // In any case the search result is being returned.
292 {
293     // find visible node with token at nRow, nCol
294     const SmNode *pTree = pViewShell->GetDoc()->GetFormulaTree(),
295                  *pNode = 0;
296     if (pTree)
297         pNode = pTree->FindTokenAt(nRow, nCol);
298 
299     if (pNode)
300         SetCursor(pNode);
301     else
302         ShowCursor(sal_False);
303 
304     return pNode;
305 }
306 
307 
308 void SmGraphicWindow::Paint(const Rectangle&)
309 {
310     DBG_ASSERT(pViewShell, "Sm : NULL pointer");
311 
312     SmDocShell &rDoc = *pViewShell->GetDoc();
313     Point aPoint;
314 
315     rDoc.Draw(*this, aPoint);   //! modifies aPoint to be the topleft
316                                 //! corner of the formula
317     SetFormulaDrawPos(aPoint);
318 
319     SetIsCursorVisible(sal_False);  // (old) cursor must be drawn again
320 
321     const SmEditWindow *pEdit = pViewShell->GetEditWindow();
322     if (pEdit)
323     {   // get new position for formula-cursor (for possible altered formula)
324         sal_uInt16  nRow, nCol;
325         SmGetLeftSelectionPart(pEdit->GetSelection(), nRow, nCol);
326         nRow++;
327         nCol++;
328         const SmNode *pFound = SetCursorPos(nRow, nCol);
329 
330         SmModule  *pp = SM_MOD();
331         if (pFound && pp->GetConfig()->IsShowFormulaCursor())
332             ShowCursor(sal_True);
333     }
334 }
335 
336 
337 void SmGraphicWindow::SetTotalSize ()
338 {
339     SmDocShell &rDoc = *pViewShell->GetDoc();
340     const Size aTmp( PixelToLogic( LogicToPixel( rDoc.GetSize() )));
341     if ( aTmp != ScrollableWindow::GetTotalSize() )
342         ScrollableWindow::SetTotalSize( aTmp );
343 }
344 
345 
346 void SmGraphicWindow::KeyInput(const KeyEvent& rKEvt)
347 {
348     if (! (GetView() && GetView()->KeyInput(rKEvt)) )
349         ScrollableWindow::KeyInput(rKEvt);
350 }
351 
352 
353 void SmGraphicWindow::Command(const CommandEvent& rCEvt)
354 {
355     sal_Bool bCallBase = sal_True;
356     if ( !pViewShell->GetViewFrame()->GetFrame().IsInPlace() )
357     {
358         switch ( rCEvt.GetCommand() )
359         {
360             case COMMAND_CONTEXTMENU:
361             {
362                 GetParent()->ToTop();
363                 SmResId aResId( RID_VIEWMENU );
364                 PopupMenu* pPopupMenu = new PopupMenu(aResId);
365                 pPopupMenu->SetSelectHdl(LINK(this, SmGraphicWindow, MenuSelectHdl));
366                 Point aPos(5, 5);
367                 if (rCEvt.IsMouseEvent())
368                     aPos = rCEvt.GetMousePosPixel();
369                 DBG_ASSERT( pViewShell, "view shell missing" );
370 
371                 // added for replaceability of context menus #96085, #93782
372                 pViewShell->GetViewFrame()->GetBindings().GetDispatcher()
373                         ->ExecutePopup( aResId, this, &aPos );
374                 //pPopupMenu->Execute( this, aPos );
375 
376                 delete pPopupMenu;
377                 bCallBase = sal_False;
378             }
379             break;
380 
381             case COMMAND_WHEEL:
382             {
383                 const CommandWheelData* pWData = rCEvt.GetWheelData();
384                 if  ( pWData && COMMAND_WHEEL_ZOOM == pWData->GetMode() )
385                 {
386                     sal_uInt16 nTmpZoom = GetZoom();
387                     if( 0L > pWData->GetDelta() )
388                         nTmpZoom -= 10;
389                     else
390                         nTmpZoom += 10;
391                     SetZoom( nTmpZoom );
392                     bCallBase = sal_False;
393                 }
394             }
395             break;
396         }
397     }
398     if ( bCallBase )
399         ScrollableWindow::Command (rCEvt);
400 }
401 
402 
403 IMPL_LINK_INLINE_START( SmGraphicWindow, MenuSelectHdl, Menu *, pMenu )
404 {
405     SmViewShell *pViewSh = GetView();
406     if (pViewSh)
407         pViewSh->GetViewFrame()->GetDispatcher()->Execute( pMenu->GetCurItemId() );
408     return 0;
409 }
410 IMPL_LINK_INLINE_END( SmGraphicWindow, MenuSelectHdl, Menu *, pMenu )
411 
412 
413 void SmGraphicWindow::SetZoom(sal_uInt16 Factor)
414 {
415     nZoom = Min(Max((sal_uInt16) Factor, (sal_uInt16) MINZOOM), (sal_uInt16) MAXZOOM);
416     Fraction   aFraction (nZoom, 100);
417     SetMapMode( MapMode(MAP_100TH_MM, Point(), aFraction, aFraction) );
418     SetTotalSize();
419     SmViewShell *pViewSh = GetView();
420     if (pViewSh)
421         pViewSh->GetViewFrame()->GetBindings().Invalidate(SID_ATTR_ZOOM);
422     Invalidate();
423 }
424 
425 
426 void SmGraphicWindow::ZoomToFitInWindow()
427 {
428     SmDocShell &rDoc = *pViewShell->GetDoc();
429 
430     // set defined mapmode before calling 'LogicToPixel' below
431     SetMapMode(MapMode(MAP_100TH_MM));
432 
433     Size       aSize (LogicToPixel(rDoc.GetSize()));
434     Size       aWindowSize (GetSizePixel());
435 
436     if (aSize.Width() > 0  &&  aSize.Height() > 0)
437     {
438         long nVal = Min ((85 * aWindowSize.Width())  / aSize.Width(),
439                       (85 * aWindowSize.Height()) / aSize.Height());
440         SetZoom ( sal::static_int_cast< sal_uInt16 >(nVal) );
441     }
442 }
443 
444 uno::Reference< XAccessible > SmGraphicWindow::CreateAccessible()
445 {
446     if (!pAccessible)
447     {
448         pAccessible = new SmGraphicAccessible( this );
449         xAccessible = pAccessible;
450     }
451     return xAccessible;
452 }
453 
454 /**************************************************************************/
455 
456 
457 SmGraphicController::SmGraphicController(SmGraphicWindow &rSmGraphic,
458                         sal_uInt16          nId_,
459                         SfxBindings     &rBindings) :
460     SfxControllerItem(nId_, rBindings),
461     rGraphic(rSmGraphic)
462 {
463 }
464 
465 
466 void SmGraphicController::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
467 {
468     rGraphic.SetTotalSize();
469     rGraphic.Invalidate();
470     SfxControllerItem::StateChanged (nSID, eState, pState);
471 }
472 
473 
474 /**************************************************************************/
475 
476 
477 SmEditController::SmEditController(SmEditWindow &rSmEdit,
478                      sal_uInt16       nId_,
479                      SfxBindings  &rBindings) :
480     SfxControllerItem(nId_, rBindings),
481     rEdit(rSmEdit)
482 {
483 }
484 
485 
486 #if OSL_DEBUG_LEVEL > 1
487 SmEditController::~SmEditController()
488 {
489 }
490 #endif
491 
492 
493 void SmEditController::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
494 {
495     const SfxStringItem *pItem = PTR_CAST(SfxStringItem, pState);
496 
497     if ((pItem != NULL) && (rEdit.GetText() != pItem->GetValue()))
498         rEdit.SetText(pItem->GetValue());
499     SfxControllerItem::StateChanged (nSID, eState, pState);
500 }
501 
502 
503 /**************************************************************************/
504 
505 SmCmdBoxWindow::SmCmdBoxWindow(SfxBindings *pBindings_, SfxChildWindow *pChildWindow,
506                                Window *pParent) :
507     SfxDockingWindow(pBindings_, pChildWindow, pParent, SmResId(RID_CMDBOXWINDOW)),
508     aEdit       (*this),
509     aController (aEdit, SID_TEXT, *pBindings_),
510     bExiting    (sal_False)
511 {
512     Hide ();
513 
514     aInitialFocusTimer.SetTimeoutHdl(LINK(this, SmCmdBoxWindow, InitialFocusTimerHdl));
515     aInitialFocusTimer.SetTimeout(100);
516 }
517 
518 
519 SmCmdBoxWindow::~SmCmdBoxWindow ()
520 {
521     aInitialFocusTimer.Stop();
522     bExiting = sal_True;
523 }
524 
525 
526 SmViewShell * SmCmdBoxWindow::GetView()
527 {
528     SfxViewShell *pView = GetBindings().GetDispatcher()->GetFrame()->GetViewShell();
529     return PTR_CAST(SmViewShell, pView);
530 }
531 
532 void SmCmdBoxWindow::Resize()
533 {
534     Rectangle aRect = Rectangle(Point(0, 0), GetOutputSizePixel());
535 
536     if (! IsFloatingMode())
537     {
538         switch (GetAlignment())
539         {
540             case SFX_ALIGN_TOP:     aRect.Bottom()--;   break;
541             case SFX_ALIGN_BOTTOM:  aRect.Top()++;      break;
542             case SFX_ALIGN_LEFT:    aRect.Right()--;    break;
543             case SFX_ALIGN_RIGHT:   aRect.Left()++;     break;
544             default:
545                 break;
546         }
547     }
548 
549     DecorationView aView(this);
550     aRect.Left() += 8; aRect.Top()   += 8;
551     aRect.Right()-= 8; aRect.Bottom()-= 8;
552     aRect = aView.DrawFrame( aRect, FRAME_DRAW_DOUBLEIN );
553 
554     aEdit.SetPosSizePixel(aRect.TopLeft(), aRect.GetSize());
555     SfxDockingWindow::Resize();
556     Invalidate();
557 }
558 
559 
560 void SmCmdBoxWindow::Paint(const Rectangle& /*rRect*/)
561 {
562     Rectangle aRect = Rectangle(Point(0, 0), GetOutputSizePixel());
563     DecorationView aView(this);
564 
565     if (! IsFloatingMode())
566     {
567         Point aFrom, aTo;
568         switch (GetAlignment())
569         {
570             case SFX_ALIGN_TOP:
571                 aFrom = aRect.BottomLeft(); aTo = aRect.BottomRight();
572                 aRect.Bottom()--;
573                 break;
574 
575             case SFX_ALIGN_BOTTOM:
576                 aFrom = aRect.TopLeft(); aTo = aRect.TopRight();
577                 aRect.Top()++;
578                 break;
579 
580             case SFX_ALIGN_LEFT:
581                 aFrom = aRect.TopRight(); aTo = aRect.BottomRight();
582                 aRect.Right()--;
583                 break;
584 
585             case SFX_ALIGN_RIGHT:
586                 aFrom = aRect.TopLeft(); aTo = aRect.BottomLeft();
587                 aRect.Left()++;
588                 break;
589 
590             default:
591                 break;
592         }
593         DrawLine( aFrom, aTo );
594         aView.DrawFrame(aRect, FRAME_DRAW_OUT);
595     }
596     aRect.Left() += 8; aRect.Top()   += 8;
597     aRect.Right()-= 8; aRect.Bottom()-= 8;
598     aRect = aView.DrawFrame( aRect, FRAME_DRAW_DOUBLEIN );
599 }
600 
601 
602 Size SmCmdBoxWindow::CalcDockingSize(SfxChildAlignment eAlign)
603 {
604     switch (eAlign)
605     {
606         case SFX_ALIGN_LEFT:
607         case SFX_ALIGN_RIGHT:
608             return Size();
609         default:
610             break;
611     }
612     return SfxDockingWindow::CalcDockingSize(eAlign);
613 }
614 
615 
616 SfxChildAlignment SmCmdBoxWindow::CheckAlignment(SfxChildAlignment eActual,
617                                              SfxChildAlignment eWish)
618 {
619     switch (eWish)
620     {
621         case SFX_ALIGN_TOP:
622         case SFX_ALIGN_BOTTOM:
623         case SFX_ALIGN_NOALIGNMENT:
624             return eWish;
625         default:
626             break;
627     }
628 
629     return eActual;
630 }
631 
632 
633 void SmCmdBoxWindow::StateChanged( StateChangedType nStateChange )
634 {
635     if (STATE_CHANGE_INITSHOW == nStateChange)
636     {
637         Resize();   // #98848# avoid SmEditWindow not being painted correctly
638 
639         // set initial position of window in floating mode
640         if (sal_True == IsFloatingMode())
641             AdjustPosition();   //! don't change pos in docking-mode !
642 
643 //        // make sure the formula can be edited right away
644 //        aEdit.GrabFocus();
645 
646         // grab focus as above does not work...
647         // Thus we implement a timer based solution to get the inital
648         // focus in the Edit window.
649         aInitialFocusTimer.Start();
650     }
651 
652     SfxDockingWindow::StateChanged( nStateChange );
653 }
654 
655 
656 IMPL_LINK( SmCmdBoxWindow, InitialFocusTimerHdl, Timer *, EMPTYARG /*pTimer*/ )
657 {
658     // We want to have the focus in the edit window once Math has been opened
659     // to allow for immediate typing.
660     // Problem: There is no proper way to do this
661     // Thus: this timer based soultion has been implemented (see GrabFocus below)
662     //
663     // Follow-up problem (#i114910): grabing the focus may bust the help system since
664     // it relies on getting the current frame which conflicts with grabbing the focus.
665     // Thus aside from the 'GrabFocus' call everything else is to get the
666     // help reliably working despite using 'GrabFocus'.
667 
668     try
669     {
670         uno::Reference< frame::XDesktop > xDesktop;
671         uno::Reference< lang::XMultiServiceFactory > xSMGR( comphelper::getProcessServiceFactory() );
672         if (xSMGR.is())
673         {
674             xDesktop = uno::Reference< frame::XDesktop >(
675                 xSMGR->createInstance( rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" )), uno::UNO_QUERY_THROW );
676         }
677 
678         aEdit.GrabFocus();
679 
680         if (xDesktop.is())
681         {
682             bool bInPlace = GetView()->GetViewFrame()->GetFrame().IsInPlace();
683             uno::Reference< frame::XFrame > xFrame( GetBindings().GetDispatcher()->GetFrame()->GetFrame().GetFrameInterface());
684             if ( bInPlace )
685             {
686                 uno::Reference< container::XChild > xModel( GetView()->GetDoc()->GetModel(), uno::UNO_QUERY_THROW );
687                 uno::Reference< frame::XModel > xParent( xModel->getParent(), uno::UNO_QUERY_THROW );
688                 uno::Reference< frame::XController > xParentCtrler( xParent->getCurrentController() );
689                 uno::Reference< frame::XFramesSupplier > xParentFrame( xParentCtrler->getFrame(), uno::UNO_QUERY_THROW );
690                 xParentFrame->setActiveFrame( xFrame );
691             }
692             else
693             {
694                 uno::Reference< frame::XFramesSupplier > xFramesSupplier( xDesktop, uno::UNO_QUERY );
695                 xFramesSupplier->setActiveFrame( xFrame );
696             }
697         }
698     }
699     catch (uno::Exception &)
700     {
701         DBG_ASSERT( 0, "failed to properly set initial focus to edit window" );
702     }
703     return 0;
704 }
705 
706 
707 void SmCmdBoxWindow::AdjustPosition()
708 {
709     Point aPt;
710     const Rectangle aRect( aPt, GetParent()->GetOutputSizePixel() );
711     Point aTopLeft( Point( aRect.Left(),
712                            aRect.Bottom() - GetSizePixel().Height() ) );
713     Point aPos( GetParent()->OutputToScreenPixel( aTopLeft ) );
714     if (aPos.X() < 0)
715         aPos.X() = 0;
716     if (aPos.Y() < 0)
717         aPos.Y() = 0;
718     SetPosPixel( aPos );
719 }
720 
721 
722 void SmCmdBoxWindow::ToggleFloatingMode()
723 {
724     SfxDockingWindow::ToggleFloatingMode();
725 
726     if (GetFloatingWindow())
727         GetFloatingWindow()->SetMinOutputSizePixel(Size (200, 50));
728 }
729 
730 
731 void SmCmdBoxWindow::GetFocus()
732 {
733     if (!bExiting)
734         aEdit.GrabFocus();
735 }
736 
737 /**************************************************************************/
738 
739 
740 SFX_IMPL_DOCKINGWINDOW(SmCmdBoxWrapper, SID_CMDBOXWINDOW);
741 
742 SmCmdBoxWrapper::SmCmdBoxWrapper(Window *pParentWindow, sal_uInt16 nId,
743                                  SfxBindings *pBindings,
744                                  SfxChildWinInfo *pInfo) :
745     SfxChildWindow(pParentWindow, nId)
746 {
747     pWindow = new SmCmdBoxWindow(pBindings, this, pParentWindow);
748 
749     // make window docked to the bottom initially (after first start)
750     eChildAlignment = SFX_ALIGN_BOTTOM;
751     ((SfxDockingWindow *)pWindow)->Initialize(pInfo);
752 }
753 
754 
755 #if OSL_DEBUG_LEVEL > 1
756 SmCmdBoxWrapper::~SmCmdBoxWrapper()
757 {
758 }
759 #endif
760 
761 
762 /**************************************************************************/
763 
764 struct SmViewShell_Impl
765 {
766     sfx2::DocumentInserter* pDocInserter;
767     SfxRequest*             pRequest;
768 
769     SmViewShell_Impl() :
770           pDocInserter( NULL )
771         , pRequest( NULL )
772     {}
773 
774     ~SmViewShell_Impl()
775     {
776         delete pDocInserter;
777         delete pRequest;
778     }
779 };
780 
781 TYPEINIT1( SmViewShell, SfxViewShell );
782 
783 SFX_IMPL_INTERFACE(SmViewShell, SfxViewShell, SmResId(0))
784 {
785     SFX_OBJECTBAR_REGISTRATION( SFX_OBJECTBAR_TOOLS | SFX_VISIBILITY_STANDARD |
786                                 SFX_VISIBILITY_FULLSCREEN | SFX_VISIBILITY_SERVER,
787                                 SmResId(RID_MATH_TOOLBOX ));
788 
789     SFX_CHILDWINDOW_REGISTRATION(SID_TASKPANE);
790     SFX_CHILDWINDOW_REGISTRATION(SmToolBoxWrapper::GetChildWindowId());
791     SFX_CHILDWINDOW_REGISTRATION(SmCmdBoxWrapper::GetChildWindowId());
792 }
793 
794 
795 SFX_IMPL_NAMED_VIEWFACTORY(SmViewShell, "Default")
796 {
797     SFX_VIEW_REGISTRATION(SmDocShell);
798 }
799 
800 
801 Size SmViewShell::GetOptimalSizePixel() const
802 {
803     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetOptimalSizePixel" );
804 
805     return aGraphic.LogicToPixel( ((SmViewShell*)this)->GetDoc()->GetSize() );
806 }
807 
808 
809 void SmViewShell::AdjustPosSizePixel(const Point &rPos, const Size &rSize)
810 {
811     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::AdjustPosSizePixel" );
812 
813     aGraphic.SetPosSizePixel(rPos, rSize);
814 }
815 
816 
817 void SmViewShell::InnerResizePixel(const Point &rOfs, const Size &rSize)
818 {
819     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::InnerResizePixel" );
820 
821     Size aObjSize = GetObjectShell()->GetVisArea().GetSize();
822     if ( aObjSize.Width() > 0 && aObjSize.Height() > 0 )
823     {
824         Size aProvidedSize = GetWindow()->PixelToLogic( rSize, MAP_100TH_MM );
825         SfxViewShell::SetZoomFactor( Fraction( aProvidedSize.Width(), aObjSize.Width() ),
826                         Fraction( aProvidedSize.Height(), aObjSize.Height() ) );
827     }
828 
829     SetBorderPixel( SvBorder() );
830     GetGraphicWindow().SetPosSizePixel(rOfs, rSize);
831     GetGraphicWindow().SetTotalSize();
832 }
833 
834 
835 void SmViewShell::OuterResizePixel(const Point &rOfs, const Size &rSize)
836 {
837     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::OuterResizePixel" );
838 
839     SmGraphicWindow &rWin = GetGraphicWindow();
840     rWin.SetPosSizePixel(rOfs, rSize);
841     if (GetDoc()->IsPreview())
842         rWin.ZoomToFitInWindow();
843     rWin.Update();
844 }
845 
846 
847 void SmViewShell::QueryObjAreaPixel( Rectangle& rRect ) const
848 {
849     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::QueryObjAreaPixel" );
850 
851     rRect.SetSize( GetGraphicWindow().GetSizePixel() );
852 }
853 
854 
855 void SmViewShell::SetZoomFactor( const Fraction &rX, const Fraction &rY )
856 {
857     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::SetZoomFactor" );
858 
859     const Fraction &rFrac = rX < rY ? rX : rY;
860     GetGraphicWindow().SetZoom( (sal_uInt16) long(rFrac * Fraction( 100, 1 )) );
861 
862     //Um Rundungsfehler zu minimieren lassen wir von der Basisklasse ggf.
863     //auch die krummen Werte einstellen
864     SfxViewShell::SetZoomFactor( rX, rY );
865 }
866 
867 
868 Size SmViewShell::GetTextLineSize(OutputDevice& rDevice, const String& rLine)
869 {
870     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetTextLineSize" );
871 
872     String aText;
873     Size   aSize(rDevice.GetTextWidth(rLine), rDevice.GetTextHeight());
874     sal_uInt16 nTabs = rLine.GetTokenCount('\t');
875 
876     if (nTabs > 0)
877     {
878         long TabPos = rDevice.GetTextWidth('n') * 8;
879 
880         aSize.Width() = 0;
881 
882         for (sal_uInt16 i = 0; i < nTabs; i++)
883         {
884             if (i > 0)
885                 aSize.Width() = ((aSize.Width() / TabPos) + 1) * TabPos;
886 
887             aText = rLine.GetToken(i, '\t');
888             aText.EraseLeadingChars('\t');
889             aText.EraseTrailingChars('\t');
890             aSize.Width() += rDevice.GetTextWidth(aText);
891         }
892     }
893 
894     return aSize;
895 }
896 
897 
898 Size SmViewShell::GetTextSize(OutputDevice& rDevice, const String& rText, long MaxWidth)
899 {
900     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetTextSize" );
901 
902     Size    aSize;
903     String  aLine;
904     Size    TextSize;
905     String  aText;
906     sal_uInt16  nLines = rText.GetTokenCount('\n');
907 
908     for (sal_uInt16 i = 0; i < nLines; i++)
909     {
910         aLine = rText.GetToken(i, '\n');
911         aLine.EraseAllChars('\r');
912         aLine.EraseLeadingChars('\n');
913         aLine.EraseTrailingChars('\n');
914 
915         aSize = GetTextLineSize(rDevice, aLine);
916 
917         if (aSize.Width() > MaxWidth)
918         {
919             do
920             {
921                 xub_StrLen m    = aLine.Len();
922                 xub_StrLen nLen = m;
923 
924                 for (xub_StrLen n = 0; n < nLen; n++)
925                 {
926                     sal_Unicode cLineChar = aLine.GetChar(n);
927                     if ((cLineChar == ' ') || (cLineChar == '\t'))
928                     {
929                         aText = aLine.Copy(0, n);
930                         if (GetTextLineSize(rDevice, aText).Width() < MaxWidth)
931                             m = n;
932                         else
933                             break;
934                     }
935                 }
936 
937                 aText = aLine.Copy(0, m);
938                 aLine.Erase(0, m);
939                 aSize = GetTextLineSize(rDevice, aText);
940                 TextSize.Height() += aSize.Height();
941                 TextSize.Width() = Max(TextSize.Width(), Min(aSize.Width(), MaxWidth));
942 
943                 aLine.EraseLeadingChars(' ');
944                 aLine.EraseLeadingChars('\t');
945                 aLine.EraseLeadingChars(' ');
946             }
947             while (aLine.Len() > 0);
948         }
949         else
950         {
951             TextSize.Height() += aSize.Height();
952             TextSize.Width() = Max(TextSize.Width(), aSize.Width());
953         }
954     }
955 
956     return TextSize;
957 }
958 
959 
960 void SmViewShell::DrawTextLine(OutputDevice& rDevice, const Point& rPosition, const String& rLine)
961 {
962     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::DrawTextLine" );
963 
964     String  aText;
965     Point   aPoint (rPosition);
966     sal_uInt16  nTabs = rLine.GetTokenCount('\t');
967 
968     if (nTabs > 0)
969     {
970         long TabPos = rDevice.GetTextWidth('n') * 8;
971 
972         for (sal_uInt16 i = 0; i < nTabs; i++)
973         {
974             if (i > 0)
975                 aPoint.X() = ((aPoint.X() / TabPos) + 1) * TabPos;
976 
977             aText = rLine.GetToken(i, '\t');
978             aText.EraseLeadingChars('\t');
979             aText.EraseTrailingChars('\t');
980             rDevice.DrawText(aPoint, aText);
981             aPoint.X() += rDevice.GetTextWidth(aText);
982         }
983     }
984     else
985         rDevice.DrawText(aPoint, rLine);
986 }
987 
988 
989 void SmViewShell::DrawText(OutputDevice& rDevice, const Point& rPosition, const String& rText, sal_uInt16 MaxWidth)
990 {
991     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::DrawText" );
992 
993     sal_uInt16  nLines = rText.GetTokenCount('\n');
994     Point   aPoint (rPosition);
995     Size    aSize;
996     String  aLine;
997     String  aText;
998 
999     for (sal_uInt16 i = 0; i < nLines; i++)
1000     {
1001         aLine = rText.GetToken(i, '\n');
1002         aLine.EraseAllChars('\r');
1003         aLine.EraseLeadingChars('\n');
1004         aLine.EraseTrailingChars('\n');
1005         aSize = GetTextLineSize(rDevice, aLine);
1006         if (aSize.Width() > MaxWidth)
1007         {
1008             do
1009             {
1010                 xub_StrLen m    = aLine.Len();
1011                 xub_StrLen nLen = m;
1012 
1013                 for (xub_StrLen n = 0; n < nLen; n++)
1014                 {
1015                     sal_Unicode cLineChar = aLine.GetChar(n);
1016                     if ((cLineChar == ' ') || (cLineChar == '\t'))
1017                     {
1018                         aText = aLine.Copy(0, n);
1019                         if (GetTextLineSize(rDevice, aText).Width() < MaxWidth)
1020                             m = n;
1021                         else
1022                             break;
1023                     }
1024                 }
1025                 aText = aLine.Copy(0, m);
1026                 aLine.Erase(0, m);
1027 
1028                 DrawTextLine(rDevice, aPoint, aText);
1029                 aPoint.Y() += aSize.Height();
1030 
1031                 aLine.EraseLeadingChars(' ');
1032                 aLine.EraseLeadingChars('\t');
1033                 aLine.EraseLeadingChars(' ');
1034             }
1035             while (GetTextLineSize(rDevice, aLine).Width() > MaxWidth);
1036 
1037             // print the remaining text
1038             if (aLine.Len() > 0)
1039             {
1040                 DrawTextLine(rDevice, aPoint, aLine);
1041                 aPoint.Y() += aSize.Height();
1042             }
1043         }
1044         else
1045         {
1046             DrawTextLine(rDevice, aPoint, aLine);
1047             aPoint.Y() += aSize.Height();
1048         }
1049     }
1050 }
1051 
1052 void SmViewShell::Impl_Print(
1053         OutputDevice &rOutDev,
1054         const SmPrintUIOptions &rPrintUIOptions,
1055         Rectangle aOutRect, Point aZeroPoint )
1056 {
1057     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Impl_Print" );
1058 
1059     const bool bIsPrintTitle = rPrintUIOptions.getBoolValue( PRTUIOPT_TITLE_ROW, sal_True );
1060     const bool bIsPrintFrame = rPrintUIOptions.getBoolValue( PRTUIOPT_BORDER, sal_True );
1061     const bool bIsPrintFormulaText = rPrintUIOptions.getBoolValue( PRTUIOPT_FORMULA_TEXT, sal_True );
1062     SmPrintSize ePrintSize( static_cast< SmPrintSize >( rPrintUIOptions.getIntValue( PRTUIOPT_PRINT_FORMAT, PRINT_SIZE_NORMAL ) ));
1063     const sal_uInt16 nZoomFactor = static_cast< sal_uInt16 >(rPrintUIOptions.getIntValue( PRTUIOPT_PRINT_SCALE, 100 ));
1064 // IsIgnoreSpacesRight is a parser option! Thus it does not get evaluated here anymore (too late).
1065 //    const bool bNoRightSpaces = rPrintUIOptions.getBoolValue( PRTUIOPT_NO_RIGHT_SPACE, sal_True );
1066 
1067     rOutDev.Push();
1068     rOutDev.SetLineColor( Color(COL_BLACK) );
1069 
1070     // output text on top
1071     if (bIsPrintTitle)
1072     {
1073         Size aSize600 (0, 600);
1074         Size aSize650 (0, 650);
1075         Font aFont(FAMILY_DONTKNOW, aSize600);
1076 
1077         aFont.SetAlign(ALIGN_TOP);
1078         aFont.SetWeight(WEIGHT_BOLD);
1079         aFont.SetSize(aSize650);
1080         aFont.SetColor( Color(COL_BLACK) );
1081         rOutDev.SetFont(aFont);
1082 
1083         Size aTitleSize (GetTextSize(rOutDev, GetDoc()->GetTitle(), aOutRect.GetWidth() - 200));
1084 
1085         aFont.SetWeight(WEIGHT_NORMAL);
1086         aFont.SetSize(aSize600);
1087         rOutDev.SetFont(aFont);
1088 
1089         Size aDescSize (GetTextSize(rOutDev, GetDoc()->GetComment(), aOutRect.GetWidth() - 200));
1090 
1091         if (bIsPrintFrame)
1092             rOutDev.DrawRect(Rectangle(aOutRect.TopLeft(),
1093                                Size(aOutRect.GetWidth(), 100 + aTitleSize.Height() + 200 + aDescSize.Height() + 100)));
1094         aOutRect.Top() += 200;
1095 
1096         // output title
1097         aFont.SetWeight(WEIGHT_BOLD);
1098         aFont.SetSize(aSize650);
1099         rOutDev.SetFont(aFont);
1100         Point aPoint(aOutRect.Left() + (aOutRect.GetWidth() - aTitleSize.Width())  / 2,
1101                      aOutRect.Top());
1102         DrawText(rOutDev, aPoint, GetDoc()->GetTitle(),
1103                  sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
1104         aOutRect.Top() += aTitleSize.Height() + 200;
1105 
1106         // output description
1107         aFont.SetWeight(WEIGHT_NORMAL);
1108         aFont.SetSize(aSize600);
1109         rOutDev.SetFont(aFont);
1110         aPoint.X() = aOutRect.Left() + (aOutRect.GetWidth()  - aDescSize.Width())  / 2;
1111         aPoint.Y() = aOutRect.Top();
1112         DrawText(rOutDev, aPoint, GetDoc()->GetComment(),
1113                  sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
1114         aOutRect.Top() += aDescSize.Height() + 300;
1115     }
1116 
1117     // output text on bottom
1118     if (bIsPrintFormulaText)
1119     {
1120         Font aFont(FAMILY_DONTKNOW, Size(0, 600));
1121         aFont.SetAlign(ALIGN_TOP);
1122         aFont.SetColor( Color(COL_BLACK) );
1123 
1124         // get size
1125         rOutDev.SetFont(aFont);
1126 
1127         Size aSize (GetTextSize(rOutDev, GetDoc()->GetText(), aOutRect.GetWidth() - 200));
1128 
1129         aOutRect.Bottom() -= aSize.Height() + 600;
1130 
1131         if (bIsPrintFrame)
1132             rOutDev.DrawRect(Rectangle(aOutRect.BottomLeft(),
1133                                Size(aOutRect.GetWidth(), 200 + aSize.Height() + 200)));
1134 
1135         Point aPoint (aOutRect.Left() + (aOutRect.GetWidth()  - aSize.Width())  / 2,
1136                       aOutRect.Bottom() + 300);
1137         DrawText(rOutDev, aPoint, GetDoc()->GetText(),
1138                  sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
1139         aOutRect.Bottom() -= 200;
1140     }
1141 
1142     if (bIsPrintFrame)
1143         rOutDev.DrawRect(aOutRect);
1144 
1145     aOutRect.Top()    += 100;
1146     aOutRect.Left()   += 100;
1147     aOutRect.Bottom() -= 100;
1148     aOutRect.Right()  -= 100;
1149 
1150     Size aSize (GetDoc()->GetSize());
1151 
1152     MapMode    OutputMapMode;
1153     // PDF export should always use PRINT_SIZE_NORMAL ...
1154     if (!rPrintUIOptions.getBoolValue( "IsPrinter", sal_False ) )
1155         ePrintSize = PRINT_SIZE_NORMAL;
1156     switch (ePrintSize)
1157     {
1158         case PRINT_SIZE_NORMAL:
1159             OutputMapMode = MapMode(MAP_100TH_MM);
1160             break;
1161 
1162         case PRINT_SIZE_SCALED:
1163             if ((aSize.Width() > 0) && (aSize.Height() > 0))
1164             {
1165                 Size     OutputSize (rOutDev.LogicToPixel(Size(aOutRect.GetWidth(),
1166                                                             aOutRect.GetHeight()), MapMode(MAP_100TH_MM)));
1167                 Size     GraphicSize (rOutDev.LogicToPixel(aSize, MapMode(MAP_100TH_MM)));
1168                 sal_uInt16   nZ = (sal_uInt16) Min((long)Fraction(OutputSize.Width()  * 100L, GraphicSize.Width()),
1169                                               (long)Fraction(OutputSize.Height() * 100L, GraphicSize.Height()));
1170                 Fraction aFraction ((sal_uInt16) Max ((sal_uInt16) MINZOOM, Min((sal_uInt16) MAXZOOM, (sal_uInt16) (nZ - 10))), (sal_uInt16) 100);
1171 
1172                 OutputMapMode = MapMode(MAP_100TH_MM, aZeroPoint, aFraction, aFraction);
1173             }
1174             else
1175                 OutputMapMode = MapMode(MAP_100TH_MM);
1176             break;
1177 
1178         case PRINT_SIZE_ZOOMED:
1179         {
1180             Fraction aFraction( nZoomFactor, 100 );
1181 
1182             OutputMapMode = MapMode(MAP_100TH_MM, aZeroPoint, aFraction, aFraction);
1183             break;
1184         }
1185     }
1186 
1187     aSize = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aSize, OutputMapMode),
1188                                    MapMode(MAP_100TH_MM));
1189 
1190     Point aPos (aOutRect.Left() + (aOutRect.GetWidth()  - aSize.Width())  / 2,
1191                 aOutRect.Top()  + (aOutRect.GetHeight() - aSize.Height()) / 2);
1192 
1193     aPos     = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aPos, MapMode(MAP_100TH_MM)),
1194                                           OutputMapMode);
1195     aOutRect   = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aOutRect, MapMode(MAP_100TH_MM)),
1196                                           OutputMapMode);
1197 
1198     rOutDev.SetMapMode(OutputMapMode);
1199     rOutDev.SetClipRegion(Region(aOutRect));
1200     GetDoc()->Draw(rOutDev, aPos);
1201     rOutDev.SetClipRegion();
1202 
1203     rOutDev.Pop();
1204 }
1205 
1206 sal_uInt16 SmViewShell::Print(SfxProgress & /*rProgress*/, sal_Bool /*bIsAPI*/)
1207 {
1208     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Print" );
1209     DBG_ASSERT( 0, "SmViewShell::Print: no longer used with new UI print dialog. Should be removed!!" );
1210     return 0;
1211 }
1212 
1213 
1214 SfxPrinter* SmViewShell::GetPrinter(sal_Bool bCreate)
1215 {
1216     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetPrinter" );
1217 
1218     SmDocShell *pDoc = GetDoc();
1219     if ( pDoc->HasPrinter() || bCreate )
1220         return pDoc->GetPrinter();
1221     return 0;
1222 }
1223 
1224 
1225 sal_uInt16 SmViewShell::SetPrinter(SfxPrinter *pNewPrinter, sal_uInt16 nDiffFlags, bool )
1226 {
1227     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::SetPrinter" );
1228     SfxPrinter *pOld = GetDoc()->GetPrinter();
1229     if ( pOld && pOld->IsPrinting() )
1230         return SFX_PRINTERROR_BUSY;
1231 
1232     if ((nDiffFlags & SFX_PRINTER_PRINTER) == SFX_PRINTER_PRINTER)
1233         GetDoc()->SetPrinter( pNewPrinter );
1234 
1235     if ((nDiffFlags & SFX_PRINTER_OPTIONS) == SFX_PRINTER_OPTIONS)
1236     {
1237         SmModule *pp = SM_MOD();
1238         pp->GetConfig()->ItemSetToConfig(pNewPrinter->GetOptions());
1239     }
1240     return 0;
1241 }
1242 
1243 
1244 SfxTabPage* SmViewShell::CreatePrintOptionsPage(Window *pParent,
1245                                                 const SfxItemSet &rOptions)
1246 {
1247     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::CreatePrintOptionsPage" );
1248 
1249     return SmPrintOptionsTabPage::Create(pParent, rOptions);
1250 }
1251 
1252 
1253 SmEditWindow *SmViewShell::GetEditWindow()
1254 {
1255     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetEditWindow" );
1256 
1257     SmCmdBoxWrapper *pWrapper = (SmCmdBoxWrapper *) GetViewFrame()->
1258             GetChildWindow( SmCmdBoxWrapper::GetChildWindowId() );
1259 
1260     if (pWrapper != NULL)
1261     {
1262         SmEditWindow *pEditWin  = pWrapper->GetEditWindow();
1263         DBG_ASSERT( pEditWin, "SmEditWindow missing" );
1264         return pEditWin;
1265     }
1266 
1267     return NULL;
1268 }
1269 
1270 
1271 void SmViewShell::SetStatusText(const String& Text)
1272 {
1273     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::SetStatusText" );
1274 
1275     StatusText = Text;
1276     GetViewFrame()->GetBindings().Invalidate(SID_TEXTSTATUS);
1277 }
1278 
1279 
1280 void SmViewShell::ShowError( const SmErrorDesc *pErrorDesc )
1281 {
1282     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::ShowError" );
1283 
1284     DBG_ASSERT(GetDoc(), "Sm : Document missing");
1285     if (pErrorDesc || 0 != (pErrorDesc = GetDoc()->GetParser().GetError(0)) )
1286     {
1287         SetStatusText( pErrorDesc->Text );
1288         GetEditWindow()->MarkError( Point( pErrorDesc->pNode->GetColumn(),
1289                                            pErrorDesc->pNode->GetRow()));
1290     }
1291 }
1292 
1293 
1294 void SmViewShell::NextError()
1295 {
1296     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::NextError" );
1297 
1298     DBG_ASSERT(GetDoc(), "Sm : Document missing");
1299     const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser().NextError();
1300 
1301     if (pErrorDesc)
1302         ShowError( pErrorDesc );
1303 }
1304 
1305 
1306 void SmViewShell::PrevError()
1307 {
1308     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::PrevError" );
1309 
1310     DBG_ASSERT(GetDoc(), "Sm : Document missing");
1311     const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser().PrevError();
1312 
1313     if (pErrorDesc)
1314         ShowError( pErrorDesc );
1315 }
1316 
1317 
1318 sal_Bool SmViewShell::Insert( SfxMedium& rMedium )
1319 {
1320     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Insert" );
1321 
1322     SmDocShell *pDoc = GetDoc();
1323     String aText( pDoc->GetText() );
1324     String aTemp = aText;
1325     sal_Bool bRet = sal_False, bChkOldVersion = sal_True;
1326 
1327     uno::Reference < embed::XStorage > xStorage = rMedium.GetStorage();
1328     uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
1329     if ( xNameAccess.is() && xNameAccess->getElementNames().getLength() )
1330     {
1331         if ( xNameAccess->hasByName( C2S( "content.xml" ) ) || xNameAccess->hasByName( C2S( "Content.xml" ) ))
1332         {
1333             bChkOldVersion = sal_False;
1334             // is this a fabulous math package ?
1335             Reference<com::sun::star::frame::XModel> xModel(pDoc->GetModel());
1336             SmXMLImportWrapper aEquation(xModel);    //!! modifies the result of pDoc->GetText() !!
1337             bRet = 0 == aEquation.Import(rMedium);
1338         }
1339     }
1340 
1341     if( bRet )
1342     {
1343         aText = pDoc->GetText();
1344         SmEditWindow *pEditWin = GetEditWindow();
1345         if (pEditWin)
1346             pEditWin->InsertText( aText );
1347         else
1348         {
1349             DBG_ERROR( "EditWindow missing" );
1350             aTemp += aText;
1351             aText  = aTemp;
1352         }
1353 
1354         pDoc->Parse();
1355         pDoc->SetModified(sal_True);
1356 
1357         SfxBindings &rBnd = GetViewFrame()->GetBindings();
1358         rBnd.Invalidate(SID_GAPHIC_SM);
1359         rBnd.Invalidate(SID_TEXT);
1360     }
1361     return bRet;
1362 }
1363 
1364 
1365 sal_Bool SmViewShell::InsertFrom(SfxMedium &rMedium)
1366 {
1367     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::InsertFrom" );
1368 
1369     sal_Bool        bSuccess = sal_False;
1370     SmDocShell *pDoc = GetDoc();
1371     SvStream   *pStream = rMedium.GetInStream();
1372     String      aText( pDoc->GetText() );
1373     String      aTemp = aText;
1374 
1375     if (pStream)
1376     {
1377         const String& rFltName = rMedium.GetFilter()->GetFilterName();
1378         if ( rFltName.EqualsAscii(MATHML_XML) )
1379         {
1380             Reference<com::sun::star::frame::XModel> xModel( pDoc->GetModel() );
1381             SmXMLImportWrapper aEquation(xModel);    //!! modifies the result of pDoc->GetText() !!
1382             bSuccess = 0 == aEquation.Import(rMedium);
1383         }
1384         else
1385         {
1386             //bSuccess = ImportSM20File( pStream );
1387         }
1388     }
1389 
1390     if( bSuccess )
1391     {
1392         aText = pDoc->GetText();
1393         SmEditWindow *pEditWin = GetEditWindow();
1394         if (pEditWin)
1395             pEditWin->InsertText( aText );
1396         else
1397         {
1398             DBG_ERROR( "EditWindow missing" );
1399             aTemp += aText;
1400             aText  = aTemp;
1401         }
1402 
1403         pDoc->Parse();
1404         pDoc->SetModified(sal_True);
1405 
1406         SfxBindings &rBnd = GetViewFrame()->GetBindings();
1407         rBnd.Invalidate(SID_GAPHIC_SM);
1408         rBnd.Invalidate(SID_TEXT);
1409     }
1410 
1411     return bSuccess;
1412 }
1413 
1414 
1415 void SmViewShell::Execute(SfxRequest& rReq)
1416 {
1417     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Execute" );
1418 
1419     SmEditWindow *pWin = GetEditWindow();
1420 
1421     switch (rReq.GetSlot())
1422     {
1423         case SID_FORMULACURSOR:
1424         {
1425             SmModule *pp = SM_MOD();
1426 
1427             const SfxItemSet  *pArgs = rReq.GetArgs();
1428             const SfxPoolItem *pItem;
1429 
1430             sal_Bool  bVal;
1431             if ( pArgs &&
1432                  SFX_ITEM_SET == pArgs->GetItemState( SID_FORMULACURSOR, sal_False, &pItem))
1433                 bVal = ((SfxBoolItem *) pItem)->GetValue();
1434             else
1435                 bVal = !pp->GetConfig()->IsShowFormulaCursor();
1436 
1437             pp->GetConfig()->SetShowFormulaCursor(bVal);
1438             GetGraphicWindow().ShowCursor(bVal);
1439             break;
1440         }
1441         case SID_DRAW:
1442             if (pWin)
1443             {
1444                 GetDoc()->SetText( pWin->GetText() );
1445                 SetStatusText(String());
1446                 ShowError( 0 );
1447                 GetDoc()->Repaint();
1448             }
1449             break;
1450 
1451         case SID_ADJUST:
1452         case SID_FITINWINDOW:
1453             aGraphic.ZoomToFitInWindow();
1454             break;
1455 
1456         case SID_VIEW050:
1457             aGraphic.SetZoom(50);
1458             break;
1459 
1460         case SID_VIEW100:
1461             aGraphic.SetZoom(100);
1462             break;
1463 
1464         case SID_VIEW200:
1465             aGraphic.SetZoom(200);
1466             break;
1467 
1468         case SID_ZOOMIN:
1469             aGraphic.SetZoom(aGraphic.GetZoom() + 25);
1470             break;
1471 
1472         case SID_ZOOMOUT:
1473             DBG_ASSERT(aGraphic.GetZoom() >= 25, "Sm: falsches sal_uInt16 Argument");
1474             aGraphic.SetZoom(aGraphic.GetZoom() - 25);
1475             break;
1476 
1477         case SID_COPYOBJECT:
1478         {
1479             //TODO/LATER: does not work because of UNO Tunneling - will be fixed later
1480             Reference< datatransfer::XTransferable > xTrans( GetDoc()->GetModel(), uno::UNO_QUERY );
1481             if( xTrans.is() )
1482             {
1483                 Reference< lang::XUnoTunnel> xTnnl( xTrans, uno::UNO_QUERY);
1484                 if( xTnnl.is() )
1485                 {
1486                     TransferableHelper* pTrans = reinterpret_cast< TransferableHelper * >(
1487                             sal::static_int_cast< sal_uIntPtr >(
1488                             xTnnl->getSomething( TransferableHelper::getUnoTunnelId() )));
1489                     if( pTrans )
1490                         pTrans->CopyToClipboard( this ? GetEditWindow() : 0 );
1491                 }
1492             }
1493         }
1494         break;
1495 
1496         case SID_PASTEOBJECT:
1497         {
1498             TransferableDataHelper aData( TransferableDataHelper::CreateFromSystemClipboard(this ? GetEditWindow(): 0) );
1499             uno::Reference < io::XInputStream > xStrm;
1500             SotFormatStringId nId;
1501             if( aData.GetTransferable().is() &&
1502                 ( aData.HasFormat( nId = SOT_FORMATSTR_ID_EMBEDDED_OBJ ) ||
1503                   (aData.HasFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR ) &&
1504                    aData.HasFormat( nId = SOT_FORMATSTR_ID_EMBED_SOURCE ))) &&
1505                 aData.GetInputStream( nId, xStrm ) && xStrm.is() )
1506             {
1507                 try
1508                 {
1509                     uno::Reference < embed::XStorage > xStorage =
1510                             ::comphelper::OStorageHelper::GetStorageFromInputStream( xStrm, ::comphelper::getProcessServiceFactory() );
1511                     uno::Reference < beans::XPropertySet > xProps( xStorage, uno::UNO_QUERY );
1512                     SfxMedium aMedium( xStorage, String() );
1513                     Insert( aMedium );
1514                     GetDoc()->UpdateText();
1515                 }
1516                 catch (uno::Exception &)
1517                 {
1518                     DBG_ERROR( "SmViewShell::Execute (SID_PASTEOBJECT): failed to get storage from input stream" );
1519                 }
1520             }
1521         }
1522         break;
1523 
1524 
1525         case SID_CUT:
1526             if (pWin)
1527                 pWin->Cut();
1528             break;
1529 
1530         case SID_COPY:
1531             if (pWin)
1532             {
1533                 if (pWin->IsAllSelected())
1534                 {
1535                     GetViewFrame()->GetDispatcher()->Execute(
1536                                 SID_COPYOBJECT, SFX_CALLMODE_STANDARD,
1537                                 new SfxVoidItem(SID_COPYOBJECT), 0L);
1538                 }
1539                 else
1540                     pWin->Copy();
1541             }
1542             break;
1543 
1544         case SID_PASTE:
1545             {
1546                 sal_Bool bCallExec = 0 == pWin;
1547                 if( !bCallExec )
1548                 {
1549                     TransferableDataHelper aDataHelper(
1550                         TransferableDataHelper::CreateFromSystemClipboard(
1551                                                     GetEditWindow()) );
1552 
1553                     if( aDataHelper.GetTransferable().is() &&
1554                         aDataHelper.HasFormat( FORMAT_STRING ))
1555                         pWin->Paste();
1556                     else
1557                         bCallExec = sal_True;
1558                 }
1559                 if( bCallExec )
1560                 {
1561                     GetViewFrame()->GetDispatcher()->Execute(
1562                             SID_PASTEOBJECT, SFX_CALLMODE_STANDARD,
1563                             new SfxVoidItem(SID_PASTEOBJECT), 0L);
1564                 }
1565             }
1566             break;
1567 
1568         case SID_DELETE:
1569             if (pWin)
1570                 pWin->Delete();
1571             break;
1572 
1573         case SID_SELECT:
1574             if (pWin)
1575                 pWin->SelectAll();
1576             break;
1577 
1578         case SID_INSERTCOMMAND:
1579         {
1580             const SfxInt16Item& rItem =
1581                 (const SfxInt16Item&)rReq.GetArgs()->Get(SID_INSERTCOMMAND);
1582 
1583             if (pWin)
1584                 pWin->InsertCommand(rItem.GetValue());
1585             break;
1586         }
1587 
1588         case SID_INSERTTEXT:
1589         {
1590             const SfxStringItem& rItem =
1591                     (const SfxStringItem&)rReq.GetArgs()->Get(SID_INSERTTEXT);
1592             if (pWin)
1593                 pWin->InsertText(rItem.GetValue());
1594             break;
1595         }
1596 
1597         case SID_IMPORT_FORMULA:
1598         {
1599             delete pImpl->pRequest;
1600             pImpl->pRequest = new SfxRequest( rReq );
1601             delete pImpl->pDocInserter;
1602             pImpl->pDocInserter =
1603                 new ::sfx2::DocumentInserter( 0, GetDoc()->GetFactory().GetFactoryName(), 0 );
1604             pImpl->pDocInserter->StartExecuteModal( LINK( this, SmViewShell, DialogClosedHdl ) );
1605             break;
1606         }
1607 
1608         case SID_NEXTERR:
1609             NextError();
1610             if (pWin)
1611                 pWin->GrabFocus();
1612             break;
1613 
1614         case SID_PREVERR:
1615             PrevError();
1616             if (pWin)
1617                 pWin->GrabFocus();
1618             break;
1619 
1620         case SID_NEXTMARK:
1621             if (pWin)
1622             {
1623                 pWin->SelNextMark();
1624                 pWin->GrabFocus();
1625             }
1626             break;
1627 
1628         case SID_PREVMARK:
1629             if (pWin)
1630             {
1631                 pWin->SelPrevMark();
1632                 pWin->GrabFocus();
1633             }
1634             break;
1635 
1636         case SID_TEXTSTATUS:
1637         {
1638             if (rReq.GetArgs() != NULL)
1639             {
1640                 const SfxStringItem& rItem =
1641                     (const SfxStringItem&)rReq.GetArgs()->Get(SID_TEXTSTATUS);
1642 
1643                 SetStatusText(rItem.GetValue());
1644             }
1645 
1646             break;
1647         }
1648 
1649         case SID_GETEDITTEXT:
1650             if (pWin)
1651                 if (pWin->GetText ().Len ()) GetDoc()->SetText( pWin->GetText() );
1652             break;
1653 
1654         case SID_ATTR_ZOOM:
1655         {
1656             if ( !GetViewFrame()->GetFrame().IsInPlace() )
1657             {
1658                 //CHINA001 SvxZoomDialog *pDlg = 0;
1659                 AbstractSvxZoomDialog *pDlg = 0;
1660                 const SfxItemSet *pSet = rReq.GetArgs();
1661                 if ( !pSet )
1662                 {
1663                     SfxItemSet aSet( GetDoc()->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
1664                     aSet.Put( SvxZoomItem( SVX_ZOOM_PERCENT, aGraphic.GetZoom()));
1665                     //CHINA001 pDlg = new SvxZoomDialog( &GetViewFrame()->GetWindow(), aSet);
1666                     SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
1667                     if(pFact)
1668                     {
1669                         pDlg = pFact->CreateSvxZoomDialog(&GetViewFrame()->GetWindow(), aSet);
1670                         DBG_ASSERT(pDlg, "Dialogdiet fail!");//CHINA001
1671                     }
1672                     pDlg->SetLimits( MINZOOM, MAXZOOM );
1673                     if( pDlg->Execute() != RET_CANCEL )
1674                         pSet = pDlg->GetOutputItemSet();
1675                 }
1676                 if ( pSet )
1677                 {
1678                     const SvxZoomItem &rZoom = (const SvxZoomItem &)pSet->Get(SID_ATTR_ZOOM);
1679                     switch( rZoom.GetType() )
1680                     {
1681                         case SVX_ZOOM_PERCENT:
1682                             aGraphic.SetZoom((sal_uInt16)rZoom.GetValue ());
1683                             break;
1684 
1685                         case SVX_ZOOM_OPTIMAL:
1686                             aGraphic.ZoomToFitInWindow();
1687                             break;
1688 
1689                         case SVX_ZOOM_PAGEWIDTH:
1690                         case SVX_ZOOM_WHOLEPAGE:
1691                         {
1692                             const MapMode aMap( MAP_100TH_MM );
1693                             SfxPrinter *pPrinter = GetPrinter( sal_True );
1694                             Point aPoint;
1695                             Rectangle  OutputRect(aPoint, pPrinter->GetOutputSize());
1696                             Size       OutputSize(pPrinter->LogicToPixel(Size(OutputRect.GetWidth(),
1697                                                                               OutputRect.GetHeight()), aMap));
1698                             Size       GraphicSize(pPrinter->LogicToPixel(GetDoc()->GetSize(), aMap));
1699                             sal_uInt16     nZ = (sal_uInt16) Min((long)Fraction(OutputSize.Width()  * 100L, GraphicSize.Width()),
1700                                                          (long)Fraction(OutputSize.Height() * 100L, GraphicSize.Height()));
1701                             aGraphic.SetZoom (nZ);
1702                             break;
1703                         }
1704                         default:
1705                             break;
1706                     }
1707                 }
1708                 delete pDlg;
1709             }
1710         }
1711         break;
1712 
1713         case SID_TOOLBOX:
1714         {
1715             GetViewFrame()->ToggleChildWindow( SmToolBoxWrapper::GetChildWindowId() );
1716         }
1717         break;
1718 
1719         case SID_SYMBOLS_CATALOGUE:
1720         {
1721 
1722             // get device used to retrieve the FontList
1723             SmDocShell *pDoc = GetDoc();
1724             OutputDevice *pDev = pDoc->GetPrinter();
1725             if (!pDev || pDev->GetDevFontCount() == 0)
1726                 pDev = &SM_MOD()->GetDefaultVirtualDev();
1727             DBG_ASSERT (pDev, "device for font list missing" );
1728 
1729             SmModule *pp = SM_MOD();
1730             SmSymbolDialog( NULL, pDev, pp->GetSymbolManager(), *this ).Execute();
1731         }
1732         break;
1733     }
1734     rReq.Done();
1735 }
1736 
1737 
1738 void SmViewShell::GetState(SfxItemSet &rSet)
1739 {
1740     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::GetState" );
1741 
1742     SfxWhichIter aIter(rSet);
1743 
1744     SmEditWindow *pEditWin = GetEditWindow();
1745     for (sal_uInt16 nWh = aIter.FirstWhich(); nWh != 0; nWh = aIter.NextWhich())
1746     {
1747         switch (nWh)
1748         {
1749         case SID_CUT:
1750         case SID_COPY:
1751         case SID_DELETE:
1752             if (! pEditWin || ! pEditWin->IsSelected())
1753                 rSet.DisableItem(nWh);
1754             break;
1755 
1756         case SID_PASTE:
1757             if( !xClipEvtLstnr.is()  &&  pEditWin)
1758             {
1759                 TransferableDataHelper aDataHelper(
1760                         TransferableDataHelper::CreateFromSystemClipboard(
1761                                                         pEditWin) );
1762 
1763                 bPasteState = aDataHelper.GetTransferable().is() &&
1764                  ( aDataHelper.HasFormat( FORMAT_STRING ) ||
1765                    aDataHelper.HasFormat( SOT_FORMATSTR_ID_EMBEDDED_OBJ ) ||
1766                    (aDataHelper.HasFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR )
1767                      && aDataHelper.HasFormat( SOT_FORMATSTR_ID_EMBED_SOURCE )));
1768             }
1769             if( !bPasteState )
1770                 rSet.DisableItem( nWh );
1771             break;
1772 
1773         case SID_ATTR_ZOOM:
1774             rSet.Put(SvxZoomItem( SVX_ZOOM_PERCENT, aGraphic.GetZoom()));
1775             /* no break here */
1776         case SID_VIEW050:
1777         case SID_VIEW100:
1778         case SID_VIEW200:
1779         case SID_ADJUST:
1780         case SID_ZOOMIN:
1781         case SID_ZOOMOUT:
1782         case SID_FITINWINDOW:
1783             if ( GetViewFrame()->GetFrame().IsInPlace() )
1784                 rSet.DisableItem( nWh );
1785             break;
1786 
1787         case SID_NEXTERR:
1788         case SID_PREVERR:
1789         case SID_NEXTMARK:
1790         case SID_PREVMARK:
1791         case SID_DRAW:
1792         case SID_SELECT:
1793             if (! pEditWin || pEditWin->IsEmpty())
1794                 rSet.DisableItem(nWh);
1795             break;
1796 
1797         case SID_TEXTSTATUS:
1798             {
1799                 rSet.Put(SfxStringItem(nWh, StatusText));
1800             }
1801             break;
1802 
1803         case SID_FORMULACURSOR:
1804             {
1805                 SmModule *pp = SM_MOD();
1806                 rSet.Put(SfxBoolItem(nWh, pp->GetConfig()->IsShowFormulaCursor()));
1807             }
1808             break;
1809 
1810         case SID_TOOLBOX:
1811             {
1812                 sal_Bool bState = sal_False;
1813                 SfxChildWindow *pChildWnd = GetViewFrame()->
1814                         GetChildWindow( SmToolBoxWrapper::GetChildWindowId() );
1815                 if (pChildWnd  &&  pChildWnd->GetWindow()->IsVisible())
1816                     bState = sal_True;
1817                 rSet.Put(SfxBoolItem(SID_TOOLBOX, bState));
1818             }
1819             break;
1820 
1821         }
1822     }
1823 }
1824 
1825 
1826 SmViewShell::SmViewShell(SfxViewFrame *pFrame_, SfxViewShell *):
1827     SfxViewShell(pFrame_, SFX_VIEW_HAS_PRINTOPTIONS | SFX_VIEW_CAN_PRINT),
1828     aGraphic(this),
1829     aGraphicController(aGraphic, SID_GAPHIC_SM, pFrame_->GetBindings()),
1830     pImpl( new SmViewShell_Impl )
1831 {
1832     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::SmViewShell" );
1833 
1834 //    pViewFrame = &pFrame_->GetWindow();
1835 
1836     SetStatusText(String());
1837     SetWindow(&aGraphic);
1838     SfxShell::SetName(C2S("SmView"));
1839     SfxShell::SetUndoManager( &GetDoc()->GetEditEngine().GetUndoManager() );
1840     SetHelpId( HID_SMA_VIEWSHELL_DOCUMENT );
1841 }
1842 
1843 
1844 SmViewShell::~SmViewShell()
1845 {
1846     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::~SmViewShell" );
1847 
1848     //!! this view shell is not active anymore !!
1849     // Thus 'SmGetActiveView' will give a 0 pointer.
1850     // Thus we need to supply this view as argument
1851     SmEditWindow *pEditWin = GetEditWindow();
1852     if (pEditWin)
1853         pEditWin->DeleteEditView( *this );
1854     delete pImpl;
1855 }
1856 
1857 void SmViewShell::Deactivate( sal_Bool bIsMDIActivate )
1858 {
1859     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Deactivate" );
1860 
1861     SmEditWindow *pEdit = GetEditWindow();
1862     if ( pEdit )
1863         pEdit->Flush();
1864 
1865     SfxViewShell::Deactivate( bIsMDIActivate );
1866 }
1867 
1868 
1869 void SmViewShell::Activate( sal_Bool bIsMDIActivate )
1870 {
1871     RTL_LOGFILE_CONTEXT( aLog, "starmath: SmViewShell::Activate" );
1872 
1873     SfxViewShell::Activate( bIsMDIActivate );
1874 
1875     SmEditWindow *pEdit = GetEditWindow();
1876     if ( pEdit )
1877     {
1878         //! Since there is no way to be informed if a "drag and drop"
1879         //! event has taken place, we call SetText here in order to
1880         //! syncronize the GraphicWindow display with the text in the
1881         //! EditEngine.
1882         SmDocShell *pDoc = GetDoc();
1883         pDoc->SetText( pDoc->GetEditEngine().GetText( LINEEND_LF ) );
1884 
1885         if ( bIsMDIActivate )
1886             pEdit->GrabFocus();
1887     }
1888 }
1889 
1890 //------------------------------------------------------------------
1891 
1892 IMPL_LINK( SmViewShell, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
1893 {
1894     DBG_ASSERT( _pFileDlg, "SmViewShell::DialogClosedHdl(): no file dialog" );
1895     DBG_ASSERT( pImpl->pDocInserter, "ScDocShell::DialogClosedHdl(): no document inserter" );
1896 
1897     if ( ERRCODE_NONE == _pFileDlg->GetError() )
1898     {
1899         //sal_uInt16 nSlot = pImpl->pRequest->GetSlot();
1900         SfxMedium* pMedium = pImpl->pDocInserter->CreateMedium();
1901 
1902         if ( pMedium != NULL )
1903         {
1904             if ( pMedium->IsStorage() )
1905                 Insert( *pMedium );
1906             else
1907                 InsertFrom( *pMedium );
1908             delete pMedium;
1909 
1910             SmDocShell* pDoc = GetDoc();
1911             pDoc->UpdateText();
1912             pDoc->ArrangeFormula();
1913             pDoc->Repaint();
1914             // adjust window, repaint, increment ModifyCount,...
1915             GetViewFrame()->GetBindings().Invalidate(SID_GAPHIC_SM);
1916         }
1917     }
1918 
1919     pImpl->pRequest->SetReturnValue( SfxBoolItem( pImpl->pRequest->GetSlot(), sal_True ) );
1920     pImpl->pRequest->Done();
1921     return 0;
1922 }
1923 
1924 void SmViewShell::Notify( SfxBroadcaster& , const SfxHint& rHint )
1925 {
1926     if ( rHint.IsA(TYPE(SfxSimpleHint)) )
1927     {
1928         switch( ( (SfxSimpleHint&) rHint ).GetId() )
1929         {
1930             case SFX_HINT_MODECHANGED:
1931             case SFX_HINT_DOCCHANGED:
1932                 GetViewFrame()->GetBindings().InvalidateAll(sal_False);
1933                 break;
1934             default:
1935                 break;
1936         }
1937     }
1938 }
1939 
1940