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