xref: /trunk/main/sc/source/ui/view/preview.cxx (revision b3f79822)
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_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 #include <tools/pstm.hxx>
31 #include "scitems.hxx"
32 #include <editeng/eeitem.hxx>
33 
34 
35 #include <svtools/colorcfg.hxx>
36 #include <svx/fmview.hxx>
37 #include <editeng/sizeitem.hxx>
38 #include <svx/svdpagv.hxx>
39 #include <sfx2/bindings.hxx>
40 #include <sfx2/viewfrm.hxx>
41 #include <sfx2/dispatch.hxx>
42 #include <svtools/accessibilityoptions.hxx>
43 #include <svl/itemset.hxx>
44 #include <tools/multisel.hxx>
45 #include <vcl/waitobj.hxx>
46 #include <vcl/sound.hxx>
47 
48 #include "preview.hxx"
49 #include "prevwsh.hxx"
50 #include "prevloc.hxx"
51 #include "docsh.hxx"
52 #include "docfunc.hxx"
53 #include "printfun.hxx"
54 #include "printopt.hxx"
55 #include "stlpool.hxx"
56 #include "undostyl.hxx"
57 #include "drwlayer.hxx"
58 #include "scmod.hxx"
59 #include "globstr.hrc"
60 #include "sc.hrc"			// fuer ShellInvalidate
61 #include "AccessibleDocumentPagePreview.hxx"
62 #include <vcl/lineinfo.hxx>
63 #include <svx/algitem.hxx>
64 #include <editeng/lrspitem.hxx>
65 #include <editeng/ulspitem.hxx>
66 #include <editeng/sizeitem.hxx>
67 #include "attrib.hxx"
68 #include "pagepar.hxx"
69 #include <com/sun/star/accessibility/XAccessible.hpp>
70 #include "AccessibilityHints.hxx"
71 #include <vcl/svapp.hxx>
72 #include "viewutil.hxx"
73 
74 // STATIC DATA -----------------------------------------------------------
75 
76 //==================================================================
77 
78 #define SC_PREVIEW_SHADOWSIZE	2
79 
80 long lcl_GetDisplayStart( SCTAB nTab, ScDocument* pDoc, long* pPages )
81 {
82 	long nDisplayStart = 0;
83 	for (SCTAB i=0; i<nTab; i++)
84 	{
85 		if ( pDoc->NeedPageResetAfterTab(i) )
86 			nDisplayStart = 0;
87 		else
88 			nDisplayStart += pPages[i];
89 	}
90 	return nDisplayStart;
91 }
92 
93 
94 ScPreview::ScPreview( Window* pParent, ScDocShell* pDocSh, ScPreviewShell* pViewSh ) :
95 	Window( pParent ),
96 	nPageNo( 0 ),
97 	nZoom( 100 ),
98 	bValid( sal_False ),
99 	nTabsTested( 0 ),
100 	nTab( 0 ),
101 	nTabStart( 0 ),
102 	nDisplayStart( 0 ),
103 	nTotalPages( 0 ),
104 	bStateValid( sal_False ),
105 	bLocationValid( sal_False ),
106 	pLocationData( NULL ),
107 	pDrawView( NULL ),
108     bInPaint( false ),
109     bInSetZoom( false ),
110 	bInGetState( sal_False ),
111 	pDocShell( pDocSh ),
112     pViewShell( pViewSh ),
113     bLeftRulerMove( sal_False ),
114     bRightRulerMove( sal_False ),
115     bTopRulerMove( sal_False ),
116     bBottomRulerMove( sal_False ),
117     bHeaderRulerMove( sal_False ),
118     bFooterRulerMove( sal_False ),
119     bLeftRulerChange( sal_False ),
120     bRightRulerChange( sal_False ),
121     bTopRulerChange( sal_False ),
122     bBottomRulerChange( sal_False ),
123     bHeaderRulerChange( sal_False ),
124     bFooterRulerChange( sal_False ),
125     bPageMargin ( sal_False ),
126     bColRulerMove( sal_False ),
127     mnScale( 0 ),
128     nColNumberButttonDown( 0 ),
129     nHeaderHeight ( 0 ),
130     nFooterHeight ( 0 )
131 {
132     SetOutDevViewType( OUTDEV_VIEWTYPE_PRINTPREVIEW ); //#106611#
133 	SetBackground();
134 
135 	SetHelpId( HID_SC_WIN_PREVIEW );
136 	SetUniqueId( HID_SC_WIN_PREVIEW );
137 
138 	SetDigitLanguage( SC_MOD()->GetOptDigitLanguage() );
139 
140     for (SCCOL i=0; i<=MAXCOL; i++)
141         nRight[i] = 0;                  // initialized with actual positions when markers are drawn
142 }
143 
144 
145 __EXPORT ScPreview::~ScPreview()
146 {
147 	delete pDrawView;
148 	delete pLocationData;
149 }
150 
151 void ScPreview::UpdateDrawView()		// nTab muss richtig sein
152 {
153 	ScDocument* pDoc = pDocShell->GetDocument();
154 	ScDrawLayer* pModel = pDoc->GetDrawLayer();		// ist nicht 0
155 
156 	// #114135#
157 	if ( pModel )
158 	{
159         SdrPage* pPage = pModel->GetPage(nTab);
160         if ( pDrawView && ( !pDrawView->GetSdrPageView() || pDrawView->GetSdrPageView()->GetPage() != pPage ) )
161 		{
162 			//	die angezeigte Page der DrawView umzustellen (s.u.) funktioniert nicht ?!?
163 			delete pDrawView;
164 			pDrawView = NULL;
165 		}
166 
167 		if ( !pDrawView )									// neu anlegen?
168 		{
169 			pDrawView = new FmFormView( pModel, this );
170 			// #55259# die DrawView uebernimmt den Design-Modus vom Model
171 			// (Einstellung "Im Entwurfsmodus oeffnen"), darum hier zuruecksetzen
172 			pDrawView->SetDesignMode( sal_True );
173 			pDrawView->SetPrintPreview( sal_True );
174             pDrawView->ShowSdrPage(pPage);
175 		}
176 #if 0
177 		else if ( !pDrawView->GetSdrPageView())		// angezeigte Page umstellen
178 		{
179 			pDrawView->HideSdrPage();
180 			pDrawView->ShowSdrPage(pDrawView->GetModel()->GetPage(nTab));
181 		}
182 #endif
183 	}
184 	else if ( pDrawView )
185 	{
186 		delete pDrawView;			// fuer diese Tabelle nicht gebraucht
187 		pDrawView = NULL;
188 	}
189 }
190 
191 
192 void ScPreview::TestLastPage()
193 {
194 	if (nPageNo >= nTotalPages)
195 	{
196 		if (nTotalPages)
197 		{
198 			nPageNo = nTotalPages - 1;
199 			nTab = nTabCount - 1;
200 			while (nTab > 0 && !nPages[nTab])		// letzte nicht leere Tabelle
201 				--nTab;
202 			DBG_ASSERT(nPages[nTab],"alle Tabellen leer?");
203 			nTabPage = nPages[nTab] - 1;
204 			nTabStart = 0;
205 			for (sal_uInt16 i=0; i<nTab; i++)
206 				nTabStart += nPages[i];
207 
208 			ScDocument* pDoc = pDocShell->GetDocument();
209 			nDisplayStart = lcl_GetDisplayStart( nTab, pDoc, nPages );
210 		}
211 		else		// leeres Dokument
212 		{
213 			nTab = 0;
214 			nPageNo = nTabPage = nTabStart = nDisplayStart = 0;
215 			aState.nPrintTab = 0;
216                         aState.nStartCol = aState.nEndCol = 0;
217                         aState.nStartRow = aState.nEndRow = 0;
218                         aState.nZoom = 0;
219 			aState.nPagesX = aState.nPagesY = 0;
220 			aState.nTabPages = aState.nTotalPages =
221 			aState.nPageStart = aState.nDocPages = 0;
222 		}
223 	}
224 }
225 
226 
227 void ScPreview::CalcPages( SCTAB /*nToWhichTab*/ )
228 {
229 	WaitObject( this );
230 
231 	ScDocument* pDoc = pDocShell->GetDocument();
232 	nTabCount = pDoc->GetTableCount();
233 
234 	//SCTAB nAnz = Min( nTabCount, SCTAB(nToWhichTab+1) );
235     SCTAB nAnz = nTabCount;
236 	SCTAB nStart = nTabsTested;
237 	if (!bValid)
238 	{
239 		nStart = 0;
240 		nTotalPages = 0;
241 		nTabsTested = 0;
242 	}
243 
244     // update all pending row heights with a single progress bar,
245     // instead of a separate progress for each sheet from ScPrintFunc
246     pDocShell->UpdatePendingRowHeights( nAnz-1, true );
247 
248 	//	PrintOptions is passed to PrintFunc for SkipEmpty flag,
249 	//	but always all sheets are used (there is no selected sheet)
250 	ScPrintOptions aOptions = SC_MOD()->GetPrintOptions();
251 
252 	for (SCTAB i=nStart; i<nAnz; i++)
253 	{
254 		long nAttrPage = i > 0 ? nFirstAttr[i-1] : 1;
255 
256 		long nThisStart = nTotalPages;
257 		ScPrintFunc aPrintFunc( this, pDocShell, i, nAttrPage, 0, NULL, &aOptions );
258 		long nThisTab = aPrintFunc.GetTotalPages();
259 		nPages[i] = nThisTab;
260 		nTotalPages += nThisTab;
261 		nFirstAttr[i] = aPrintFunc.GetFirstPageNo();	// behalten oder aus Vorlage
262 
263 		if (nPageNo>=nThisStart && nPageNo<nTotalPages)
264 		{
265 			nTab = i;
266 			nTabPage = nPageNo - nThisStart;
267 			nTabStart = nThisStart;
268 
269 			aPrintFunc.GetPrintState( aState );
270 			aPageSize = aPrintFunc.GetPageSize();
271 		}
272 	}
273 
274 	nDisplayStart = lcl_GetDisplayStart( nTab, pDoc, nPages );
275 
276 	if (nAnz > nTabsTested)
277 		nTabsTested = nAnz;
278 
279 	//	testen, ob hinter letzter Seite
280 
281 	if ( nTabsTested >= nTabCount )
282 		TestLastPage();
283 
284 	aState.nDocPages = nTotalPages;
285 
286 	bValid = sal_True;
287 	bStateValid = sal_True;
288 	DoInvalidate();
289 }
290 
291 
292 void ScPreview::RecalcPages()					// nur nPageNo geaendert
293 {
294 	if (!bValid)
295 		return;							// dann wird CalcPages aufgerufen
296 
297 	SCTAB nOldTab = nTab;
298 
299 	sal_Bool bDone = sal_False;
300 	while (nPageNo >= nTotalPages && nTabsTested < nTabCount)
301 	{
302 		CalcPages( nTabsTested );
303 		bDone = sal_True;
304 	}
305 
306 	if (!bDone)
307 	{
308 		long nPartPages = 0;
309 		for (SCTAB i=0; i<nTabsTested; i++)
310 		{
311 			long nThisStart = nPartPages;
312 			nPartPages += nPages[i];
313 
314 			if (nPageNo>=nThisStart && nPageNo<nPartPages)
315 			{
316 				nTab = i;
317 				nTabPage = nPageNo - nThisStart;
318 				nTabStart = nThisStart;
319 
320 //				aPageSize = aPrintFunc.GetPageSize();
321 			}
322 		}
323 
324 		ScDocument* pDoc = pDocShell->GetDocument();
325 		nDisplayStart = lcl_GetDisplayStart( nTab, pDoc, nPages );
326 	}
327 
328 	TestLastPage();			// testen, ob hinter letzter Seite
329 
330 	if ( nTab != nOldTab )
331 		bStateValid = sal_False;
332 
333 	DoInvalidate();
334 }
335 
336 
337 void ScPreview::DoPrint( ScPreviewLocationData* pFillLocation )
338 {
339 	if (!bValid)
340 	{
341 		CalcPages(0);
342 		RecalcPages();
343 		UpdateDrawView();		// Tabelle evtl. geaendert
344 	}
345 
346 	Fraction aPreviewZoom( nZoom, 100 );
347 	Fraction aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
348 	MapMode aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
349 
350 	sal_Bool bDoPrint = ( pFillLocation == NULL );
351 	sal_Bool bValidPage = ( nPageNo < nTotalPages );
352 
353 	ScModule* pScMod = SC_MOD();
354     const svtools::ColorConfig& rColorCfg = pScMod->GetColorConfig();
355     Color aBackColor( rColorCfg.GetColorValue(svtools::APPBACKGROUND).nColor );
356 
357 	if ( bDoPrint && ( aOffset.X() < 0 || aOffset.Y() < 0 ) && bValidPage )
358 	{
359 		SetMapMode( aMMMode );
360 		SetLineColor();
361 		SetFillColor(aBackColor);
362 
363 		Size aWinSize = GetOutputSize();
364 		if ( aOffset.X() < 0 )
365 			DrawRect(Rectangle( 0, 0, -aOffset.X(), aWinSize.Height() ));
366 		if ( aOffset.Y() < 0 )
367 			DrawRect(Rectangle( 0, 0, aWinSize.Width(), -aOffset.Y() ));
368 	}
369 
370     long   nLeftMargin = 0;
371     long   nRightMargin = 0;
372     long   nTopMargin = 0;
373     long   nBottomMargin = 0;
374     sal_Bool bHeaderOn = sal_False;
375     sal_Bool bFooterOn = sal_False;
376 
377     ScDocument* pDoc = pDocShell->GetDocument();
378     sal_Bool bLayoutRTL = pDoc->IsLayoutRTL( nTab );
379 
380 	Size aLocalPageSize;
381 	if ( bValidPage )
382 	{
383 		ScPrintOptions aOptions = pScMod->GetPrintOptions();
384 
385 		ScPrintFunc* pPrintFunc;
386 		if (bStateValid)
387 			pPrintFunc = new ScPrintFunc( this, pDocShell, aState, &aOptions );
388 		else
389 			pPrintFunc = new ScPrintFunc( this, pDocShell, nTab, nFirstAttr[nTab], nTotalPages, NULL, &aOptions );
390 
391 		pPrintFunc->SetOffset(aOffset);
392 		pPrintFunc->SetManualZoom(nZoom);
393 		pPrintFunc->SetDateTime(aDate,aTime);
394 		pPrintFunc->SetClearFlag(sal_True);
395 		pPrintFunc->SetUseStyleColor( pScMod->GetAccessOptions().GetIsForPagePreviews() );
396 
397 		pPrintFunc->SetDrawView( pDrawView );
398 
399 		// MultiSelection fuer die eine Seite muss etwas umstaendlich erzeugt werden...
400 		Range aPageRange( nPageNo+1, nPageNo+1 );
401 		MultiSelection aPage( aPageRange );
402 		aPage.SetTotalRange( Range(0,RANGE_MAX) );
403 		aPage.Select( aPageRange );
404 
405         long nPrinted = pPrintFunc->DoPrint( aPage, nTabStart, nDisplayStart, bDoPrint, pFillLocation );
406 		DBG_ASSERT(nPrinted<=1, "was'n nu los?");
407 
408 		SetMapMode(aMMMode);
409 
410         //init nLeftMargin ... in the ScPrintFunc::InitParam!!!
411         nLeftMargin = pPrintFunc->GetLeftMargin();
412         nRightMargin = pPrintFunc->GetRightMargin();
413         nTopMargin = pPrintFunc->GetTopMargin();
414         nBottomMargin = pPrintFunc->GetBottomMargin();
415         nHeaderHeight = pPrintFunc->GetHeader().nHeight;
416         nFooterHeight = pPrintFunc->GetFooter().nHeight;
417         bHeaderOn = pPrintFunc->GetHeader().bEnable;
418         bFooterOn = pPrintFunc->GetFooter().bEnable;
419         mnScale = pPrintFunc->GetZoom();
420 
421         if ( bDoPrint && bPageMargin && pLocationData )     // don't make use of pLocationData while filling it
422         {
423             Rectangle aPixRect;
424             Rectangle aRectCellPosition;
425             Rectangle aRectPosition;
426             pLocationData->GetMainCellRange( aPageArea, aPixRect );
427             if( !bLayoutRTL )
428             {
429                 pLocationData->GetCellPosition( aPageArea.aStart, aRectPosition );
430                 nLeftPosition = aRectPosition.Left();
431                 for( SCCOL i = aPageArea.aStart.Col(); i <= aPageArea.aEnd.Col(); i++ )
432                 {
433                     pLocationData->GetCellPosition( ScAddress( i,aPageArea.aStart.Row(),aPageArea.aStart.Tab()),aRectCellPosition );
434                     nRight[i] = aRectCellPosition.Right();
435                 }
436             }
437             else
438             {
439                 pLocationData->GetCellPosition( aPageArea.aEnd, aRectPosition );
440                 nLeftPosition = aRectPosition.Right()+1;
441 
442                 pLocationData->GetCellPosition( aPageArea.aStart,aRectCellPosition );
443                 nRight[ aPageArea.aEnd.Col() ] = aRectCellPosition.Left();
444                 for( SCCOL i = aPageArea.aEnd.Col(); i > aPageArea.aStart.Col(); i-- )
445                 {
446                     pLocationData->GetCellPosition( ScAddress( i,aPageArea.aEnd.Row(),aPageArea.aEnd.Tab()),aRectCellPosition );
447                     nRight[ i-1 ] = nRight[ i ] + aRectCellPosition.Right() - aRectCellPosition.Left() + 1;
448                 }
449             }
450         }
451 
452 		if (nPrinted)	// wenn nichts, alles grau zeichnen
453 		{
454 			aLocalPageSize = pPrintFunc->GetPageSize();
455 			aLocalPageSize.Width()  = (long) (aLocalPageSize.Width()  * HMM_PER_TWIPS );
456 			aLocalPageSize.Height() = (long) (aLocalPageSize.Height() * HMM_PER_TWIPS );
457 
458             nLeftMargin = (long) ( nLeftMargin * HMM_PER_TWIPS );
459             nRightMargin = (long) ( nRightMargin * HMM_PER_TWIPS );
460             nTopMargin = (long) ( nTopMargin * HMM_PER_TWIPS );
461             nBottomMargin = (long) ( nBottomMargin * HMM_PER_TWIPS );
462             nHeaderHeight = (long) ( nHeaderHeight * HMM_PER_TWIPS * mnScale / 100 + nTopMargin );
463             nFooterHeight = (long) ( nFooterHeight * HMM_PER_TWIPS * mnScale / 100 + nBottomMargin );
464 		}
465 
466 		if (!bStateValid)
467 		{
468 			pPrintFunc->GetPrintState( aState );
469 			aState.nDocPages = nTotalPages;
470 			bStateValid = sal_True;
471 		}
472 		delete pPrintFunc;
473 	}
474 
475 	if ( bDoPrint )
476 	{
477 		long nPageEndX = aLocalPageSize.Width()  - aOffset.X();
478 		long nPageEndY = aLocalPageSize.Height() - aOffset.Y();
479 		if ( !bValidPage )
480 			nPageEndX = nPageEndY = 0;
481 
482 		Size aWinSize = GetOutputSize();
483 		Point aWinEnd( aWinSize.Width(), aWinSize.Height() );
484 		sal_Bool bRight  = nPageEndX <= aWinEnd.X();
485 		sal_Bool bBottom = nPageEndY <= aWinEnd.Y();
486 
487         if( bPageMargin && bValidPage )
488         {
489             SetMapMode(aMMMode);
490             SetLineColor( COL_BLACK );
491             DrawInvert( (long)( nTopMargin - aOffset.Y() ), POINTER_VSIZEBAR );
492             DrawInvert( (long)(nPageEndY - nBottomMargin ), POINTER_VSIZEBAR );
493             DrawInvert( (long)( nLeftMargin - aOffset.X() ), POINTER_HSIZEBAR );
494             DrawInvert( (long)( nPageEndX - nRightMargin ) , POINTER_HSIZEBAR );
495             if( bHeaderOn )
496             {
497                 DrawInvert( nHeaderHeight - aOffset.Y(), POINTER_VSIZEBAR );
498             }
499             if( bFooterOn )
500             {
501                 DrawInvert( nPageEndY - nFooterHeight, POINTER_VSIZEBAR );
502             }
503 
504             SetMapMode( MapMode( MAP_PIXEL ) );
505             for( int i= aPageArea.aStart.Col(); i<= aPageArea.aEnd.Col(); i++ )
506             {
507                 Point aColumnTop = LogicToPixel( Point( 0, -aOffset.Y() ) ,aMMMode );
508                 SetLineColor( COL_BLACK );
509                 SetFillColor( COL_BLACK );
510                 DrawRect( Rectangle( Point( nRight[i] - 2, aColumnTop.Y() ),Point( nRight[i] + 2 , 4 + aColumnTop.Y()) ));
511                 DrawLine( Point( nRight[i], aColumnTop.Y() ), Point( nRight[i],  10 + aColumnTop.Y()) );
512             }
513             SetMapMode( aMMMode );
514         }
515 
516 		if (bRight || bBottom)
517 		{
518             SetMapMode(aMMMode);
519 			SetLineColor();
520 			SetFillColor(aBackColor);
521 			if (bRight)
522 				DrawRect(Rectangle(nPageEndX,0, aWinEnd.X(),aWinEnd.Y()));
523 			if (bBottom)
524 			{
525 				if (bRight)
526 					DrawRect(Rectangle(0,nPageEndY, nPageEndX,aWinEnd.Y()));	// Ecke nicht doppelt
527 				else
528 					DrawRect(Rectangle(0,nPageEndY, aWinEnd.X(),aWinEnd.Y()));
529 			}
530 		}
531 
532 		if ( bValidPage )
533 		{
534             Color aBorderColor( SC_MOD()->GetColorConfig().GetColorValue(svtools::FONTCOLOR).nColor );
535 
536 			//	draw border
537 
538 			if ( aOffset.X() <= 0 || aOffset.Y() <= 0 || bRight || bBottom )
539 			{
540 				SetLineColor( aBorderColor );
541 				SetFillColor();
542 
543 				Rectangle aPixel( LogicToPixel( Rectangle( -aOffset.X(), -aOffset.Y(), nPageEndX, nPageEndY ) ) );
544 				--aPixel.Right();
545 				--aPixel.Bottom();
546 				DrawRect( PixelToLogic( aPixel ) );
547 			}
548 
549 			//	draw shadow
550 
551 			SetLineColor();
552 			SetFillColor( aBorderColor );
553 
554 			Rectangle aPixel;
555 
556 			aPixel = LogicToPixel( Rectangle( nPageEndX, -aOffset.Y(), nPageEndX, nPageEndY ) );
557 			aPixel.Top() += SC_PREVIEW_SHADOWSIZE;
558 			aPixel.Right() += SC_PREVIEW_SHADOWSIZE - 1;
559 			aPixel.Bottom() += SC_PREVIEW_SHADOWSIZE - 1;
560 			DrawRect( PixelToLogic( aPixel ) );
561 
562 			aPixel = LogicToPixel( Rectangle( -aOffset.X(), nPageEndY, nPageEndX, nPageEndY ) );
563 			aPixel.Left() += SC_PREVIEW_SHADOWSIZE;
564 			aPixel.Right() += SC_PREVIEW_SHADOWSIZE - 1;
565 			aPixel.Bottom() += SC_PREVIEW_SHADOWSIZE - 1;
566 			DrawRect( PixelToLogic( aPixel ) );
567 		}
568 	}
569 }
570 
571 //Issue51656 Add resizeable margin on page preview from maoyg
572 void __EXPORT ScPreview::Paint( const Rectangle& /* rRect */ )
573 {
574     bool bWasInPaint = bInPaint;        // nested calls shouldn't be necessary, but allow for now
575     bInPaint = true;
576 
577     if (bPageMargin)
578         GetLocationData();              // fill location data for column positions
579     DoPrint( NULL );
580 	pViewShell->UpdateScrollBars();
581 
582     bInPaint = bWasInPaint;
583 }
584 //Issue51656 Add resizeable margin on page preview from maoyg
585 
586 void __EXPORT ScPreview::Command( const CommandEvent& rCEvt )
587 {
588 	sal_uInt16 nCmd = rCEvt.GetCommand();
589 	if ( nCmd == COMMAND_WHEEL || nCmd == COMMAND_STARTAUTOSCROLL || nCmd == COMMAND_AUTOSCROLL )
590 	{
591 		sal_Bool bDone = pViewShell->ScrollCommand( rCEvt );
592 		if (!bDone)
593 			Window::Command(rCEvt);
594 	}
595 	else if ( nCmd == COMMAND_CONTEXTMENU )
596 		SfxDispatcher::ExecutePopup();
597 	else
598 		Window::Command( rCEvt );
599 }
600 
601 
602 void __EXPORT ScPreview::KeyInput( const KeyEvent& rKEvt )
603 {
604     //  The + and - keys can't be configured as accelerator entries, so they must be handled directly
605     //  (in ScPreview, not ScPreviewShell -> only if the preview window has the focus)
606 
607     const KeyCode& rKeyCode = rKEvt.GetKeyCode();
608     sal_uInt16 nKey = rKeyCode.GetCode();
609     sal_Bool bHandled = sal_False;
610     if(!rKeyCode.GetModifier())
611     {
612         sal_uInt16 nSlot = 0;
613         switch(nKey)
614         {
615             case KEY_ADD:      nSlot = SID_PREVIEW_ZOOMIN;  break;
616             case KEY_ESCAPE:   nSlot = ScViewUtil::IsFullScreen( *pViewShell ) ? SID_CANCEL : SID_PREVIEW_CLOSE; break;
617             case KEY_SUBTRACT: nSlot = SID_PREVIEW_ZOOMOUT; break;
618         }
619         if(nSlot)
620         {
621             bHandled = sal_True;
622             pViewShell->GetViewFrame()->GetDispatcher()->Execute( nSlot, SFX_CALLMODE_ASYNCHRON );
623         }
624     }
625 
626     if ( !bHandled && !pViewShell->KeyInput(rKEvt) )
627         Window::KeyInput(rKEvt);
628 }
629 
630 
631 const ScPreviewLocationData& ScPreview::GetLocationData()
632 {
633 	if ( !pLocationData )
634 	{
635 		pLocationData = new ScPreviewLocationData( pDocShell->GetDocument(), this );
636 		bLocationValid = sal_False;
637 	}
638 	if ( !bLocationValid )
639 	{
640 		pLocationData->Clear();
641 		DoPrint( pLocationData );
642 		bLocationValid = sal_True;
643 	}
644 	return *pLocationData;
645 }
646 
647 
648 void ScPreview::DataChanged(sal_Bool bNewTime)
649 {
650 	if (bNewTime)
651 	{
652 		aDate = Date();
653 		aTime = Time();
654 	}
655 
656 	bValid = sal_False;
657 	InvalidateLocationData( SC_HINT_DATACHANGED );
658 	Invalidate();
659 }
660 
661 
662 String ScPreview::GetPosString()
663 {
664 	if (!bValid)
665 	{
666 		CalcPages(nTab);
667 		UpdateDrawView();		// Tabelle evtl. geaendert
668 	}
669 
670 	String aString( ScGlobal::GetRscString( STR_PAGE ) );
671 	aString += ' ';
672 	aString += String::CreateFromInt32(nPageNo+1);
673 
674 	if (nTabsTested >= nTabCount)
675 	{
676 		aString.AppendAscii(RTL_CONSTASCII_STRINGPARAM( " / " ));
677 		aString += String::CreateFromInt32(nTotalPages);
678 	}
679 
680 	return aString;
681 }
682 
683 
684 void ScPreview::SetZoom(sal_uInt16 nNewZoom)
685 {
686 	if (nNewZoom < 20)
687 		nNewZoom = 20;
688 	if (nNewZoom > 400)
689 		nNewZoom = 400;
690 	if (nNewZoom != nZoom)
691 	{
692 		nZoom = nNewZoom;
693 
694 		//	apply new MapMode and call UpdateScrollBars to update aOffset
695 
696 		Fraction aPreviewZoom( nZoom, 100 );
697 		Fraction aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
698 		MapMode aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
699 		SetMapMode( aMMMode );
700 
701         bInSetZoom = true;              // don't scroll during SetYOffset in UpdateScrollBars
702 		pViewShell->UpdateScrollBars();
703         bInSetZoom = false;
704 
705 		bStateValid = sal_False;
706 		InvalidateLocationData( SC_HINT_ACC_VISAREACHANGED );
707 		DoInvalidate();
708 		Invalidate();
709 	}
710 }
711 
712 
713 void ScPreview::SetPageNo( long nPage )
714 {
715 	nPageNo = nPage;
716 	RecalcPages();
717 	UpdateDrawView();		// Tabelle evtl. geaendert
718 	InvalidateLocationData( SC_HINT_DATACHANGED );
719 	Invalidate();
720 }
721 
722 
723 long ScPreview::GetFirstPage(SCTAB nTabP)
724 {
725 	SCTAB nDocTabCount = pDocShell->GetDocument()->GetTableCount();
726 	if (nTabP >= nDocTabCount)
727 		nTabP = nDocTabCount-1;
728 
729 	long nPage = 0;
730 	if (nTabP>0)
731 	{
732 		CalcPages( nTabP );
733 		UpdateDrawView();		// Tabelle evtl. geaendert
734 
735 		for (SCTAB i=0; i<nTabP; i++)
736 			nPage += nPages[i];
737 
738 		// bei leerer Tabelle vorhergehende Seite
739 
740 		if ( nPages[nTabP]==0 && nPage > 0 )
741 			--nPage;
742 	}
743 
744 	return nPage;
745 }
746 
747 
748 Size lcl_GetDocPageSize( ScDocument* pDoc, SCTAB nTab )
749 {
750 	String aName = pDoc->GetPageStyle( nTab );
751 	ScStyleSheetPool* pStylePool = pDoc->GetStyleSheetPool();
752 	SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aName, SFX_STYLE_FAMILY_PAGE );
753 	if ( pStyleSheet )
754 	{
755 		SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
756 		return ((const SvxSizeItem&) rStyleSet.Get(ATTR_PAGE_SIZE)).GetSize();
757 	}
758 	else
759 	{
760 		DBG_ERROR( "PageStyle not found" );
761 		return Size();
762 	}
763 }
764 
765 
766 sal_uInt16 ScPreview::GetOptimalZoom(sal_Bool bWidthOnly)
767 {
768 	double nWinScaleX = ScGlobal::nScreenPPTX / pDocShell->GetOutputFactor();
769 	double nWinScaleY = ScGlobal::nScreenPPTY;
770 	Size aWinSize = GetOutputSizePixel();
771 
772 	//	desired margin is 0.25cm in default MapMode (like Writer),
773 	//	but some additional margin is introduced by integer scale values
774 	//	-> add only 0.10cm, so there is some margin in all cases.
775 	Size aMarginSize( LogicToPixel( Size( 100, 100 ), MAP_100TH_MM ) );
776 	aWinSize.Width()  -= 2 * aMarginSize.Width();
777 	aWinSize.Height() -= 2 * aMarginSize.Height();
778 
779 	Size aLocalPageSize = lcl_GetDocPageSize( pDocShell->GetDocument(), nTab );
780 	if ( aLocalPageSize.Width() && aLocalPageSize.Height() )
781 	{
782 		long nZoomX = (long) ( aWinSize.Width() * 100  / ( aLocalPageSize.Width() * nWinScaleX ));
783 		long nZoomY = (long) ( aWinSize.Height() * 100 / ( aLocalPageSize.Height() * nWinScaleY ));
784 
785 		long nOptimal = nZoomX;
786 		if (!bWidthOnly && nZoomY<nOptimal)
787 			nOptimal = nZoomY;
788 
789 		if (nOptimal<20)
790 			nOptimal = 20;
791 		if (nOptimal>400)
792 			nOptimal = 400;
793 
794 		return (sal_uInt16) nOptimal;
795 	}
796 	else
797 		return nZoom;
798 }
799 
800 
801 void ScPreview::SetXOffset( long nX )
802 {
803 	if ( aOffset.X() == nX )
804 		return;
805 
806 	if (bValid)
807 	{
808 		long nDif = LogicToPixel(aOffset).X() - LogicToPixel(Point(nX,0)).X();
809 		aOffset.X() = nX;
810         if (nDif && !bInSetZoom)
811 		{
812 			MapMode aOldMode = GetMapMode(); SetMapMode(MAP_PIXEL);
813 			Scroll( nDif, 0 );
814 			SetMapMode(aOldMode);
815 		}
816 	}
817 	else
818 	{
819 		aOffset.X() = nX;
820         if (!bInSetZoom)
821 			Invalidate();
822 	}
823 	InvalidateLocationData( SC_HINT_ACC_VISAREACHANGED );
824     Paint(Rectangle());
825 }
826 
827 
828 void ScPreview::SetYOffset( long nY )
829 {
830 	if ( aOffset.Y() == nY )
831 		return;
832 
833 	if (bValid)
834 	{
835 		long nDif = LogicToPixel(aOffset).Y() - LogicToPixel(Point(0,nY)).Y();
836 		aOffset.Y() = nY;
837         if (nDif && !bInSetZoom)
838 		{
839 			MapMode aOldMode = GetMapMode(); SetMapMode(MAP_PIXEL);
840 			Scroll( 0, nDif );
841 			SetMapMode(aOldMode);
842 		}
843 	}
844 	else
845 	{
846 		aOffset.Y() = nY;
847         if (!bInSetZoom)
848 			Invalidate();
849 	}
850 	InvalidateLocationData( SC_HINT_ACC_VISAREACHANGED );
851     Paint(Rectangle());
852 }
853 
854 
855 void ScPreview::DoInvalidate()
856 {
857 	//	Wenn das ganze aus dem GetState der Shell gerufen wird,
858 	//	muss das Invalidate hinterher asynchron kommen...
859 
860 	if (bInGetState)
861 		Application::PostUserEvent( STATIC_LINK( this, ScPreview, InvalidateHdl ) );
862 	else
863 		StaticInvalidate();		// sofort
864 }
865 
866 void ScPreview::StaticInvalidate()
867 {
868 	//	static method, because it's called asynchronously
869 	//	-> must use current viewframe
870 
871 	SfxViewFrame* pViewFrm = SfxViewFrame::Current();
872 	if (!pViewFrm)
873 		return;
874 
875 	SfxBindings& rBindings = pViewFrm->GetBindings();
876 	rBindings.Invalidate(SID_STATUS_DOCPOS);
877 	rBindings.Invalidate(SID_STATUS_PAGESTYLE);
878 	rBindings.Invalidate(SID_PREVIEW_PREVIOUS);
879 	rBindings.Invalidate(SID_PREVIEW_NEXT);
880 	rBindings.Invalidate(SID_PREVIEW_FIRST);
881 	rBindings.Invalidate(SID_PREVIEW_LAST);
882 	rBindings.Invalidate(SID_ATTR_ZOOM);
883 	rBindings.Invalidate(SID_PREVIEW_ZOOMIN);
884 	rBindings.Invalidate(SID_PREVIEW_ZOOMOUT);
885     rBindings.Invalidate(SID_PREVIEW_SCALINGFACTOR);
886     rBindings.Invalidate(SID_ATTR_ZOOMSLIDER);
887 }
888 
889 IMPL_STATIC_LINK( ScPreview, InvalidateHdl, void*, EMPTYARG )
890 {
891     (void)pThis;    // avoid warning
892 
893 	StaticInvalidate();
894 	return 0;
895 }
896 
897 void ScPreview::DataChanged( const DataChangedEvent& rDCEvt )
898 {
899 	Window::DataChanged(rDCEvt);
900 
901 	if ( (rDCEvt.GetType() == DATACHANGED_PRINTER) ||
902 		 (rDCEvt.GetType() == DATACHANGED_DISPLAY) ||
903 		 (rDCEvt.GetType() == DATACHANGED_FONTS) ||
904 		 (rDCEvt.GetType() == DATACHANGED_FONTSUBSTITUTION) ||
905 		 ((rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
906 		  (rDCEvt.GetFlags() & SETTINGS_STYLE)) )
907 	{
908 		if ( rDCEvt.GetType() == DATACHANGED_FONTS )
909 			pDocShell->UpdateFontList();
910 
911         // #i114518# Paint of form controls may modify the window's settings.
912         // Ignore the event if it is called from within Paint.
913         if ( !bInPaint )
914         {
915             if ( rDCEvt.GetType() == DATACHANGED_SETTINGS &&
916                   (rDCEvt.GetFlags() & SETTINGS_STYLE) )
917             {
918                 //  scroll bar size may have changed
919                 pViewShell->InvalidateBorder();     // calls OuterResizePixel
920             }
921 
922             Invalidate();
923             InvalidateLocationData( SC_HINT_DATACHANGED );
924         }
925 	}
926 }
927 
928 //Issue51656 Add resizeable margin on page preview from maoyg
929 void __EXPORT ScPreview::MouseButtonDown( const MouseEvent& rMEvt )
930 {
931     Fraction  aPreviewZoom( nZoom, 100 );
932     Fraction  aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
933     MapMode   aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
934 
935     aButtonDownChangePoint = PixelToLogic( rMEvt.GetPosPixel(),aMMMode );
936     aButtonDownPt = PixelToLogic( rMEvt.GetPosPixel(),aMMMode );
937 
938     CaptureMouse();
939 
940     if( rMEvt.IsLeft() && GetPointer() == POINTER_HSIZEBAR )
941     {
942         SetMapMode( aMMMode );
943         if( bLeftRulerChange )
944         {
945            DrawInvert( aButtonDownChangePoint.X(), POINTER_HSIZEBAR );
946            bLeftRulerMove = sal_True;
947            bRightRulerMove = sal_False;
948         }
949         else if( bRightRulerChange )
950         {
951            DrawInvert( aButtonDownChangePoint.X(), POINTER_HSIZEBAR );
952            bLeftRulerMove = sal_False;
953            bRightRulerMove = sal_True;
954         }
955     }
956 
957     if( rMEvt.IsLeft() && GetPointer() == POINTER_VSIZEBAR )
958     {
959         SetMapMode( aMMMode );
960         if( bTopRulerChange )
961         {
962             DrawInvert( aButtonDownChangePoint.Y(), POINTER_VSIZEBAR );
963             bTopRulerMove = sal_True;
964             bBottomRulerMove = sal_False;
965         }
966         else if( bBottomRulerChange )
967         {
968             DrawInvert( aButtonDownChangePoint.Y(), POINTER_VSIZEBAR );
969             bTopRulerMove = sal_False;
970             bBottomRulerMove = sal_True;
971         }
972         else if( bHeaderRulerChange )
973         {
974             DrawInvert( aButtonDownChangePoint.Y(), POINTER_VSIZEBAR );
975             bHeaderRulerMove = sal_True;
976             bFooterRulerMove = sal_False;
977         }
978         else if( bFooterRulerChange )
979         {
980             DrawInvert( aButtonDownChangePoint.Y(), POINTER_VSIZEBAR );
981             bHeaderRulerMove = sal_False;
982             bFooterRulerMove = sal_True;
983         }
984     }
985 
986     if( rMEvt.IsLeft() && GetPointer() == POINTER_HSPLIT )
987     {
988         Point  aNowPt = rMEvt.GetPosPixel();
989         SCCOL i = 0;
990         for( i = aPageArea.aStart.Col(); i<= aPageArea.aEnd.Col(); i++ )
991         {
992             if( aNowPt.X() < nRight[i] + 2 && aNowPt.X() > nRight[i] - 2 )
993             {
994                 nColNumberButttonDown = i;
995                 break;
996             }
997         }
998         if( i == aPageArea.aEnd.Col()+1 )
999             return;
1000 
1001         SetMapMode( aMMMode );
1002         if( nColNumberButttonDown == aPageArea.aStart.Col() )
1003             DrawInvert( PixelToLogic( Point( nLeftPosition, 0 ),aMMMode ).X() ,POINTER_HSPLIT );
1004         else
1005             DrawInvert( PixelToLogic( Point( nRight[ nColNumberButttonDown-1 ], 0 ),aMMMode ).X() ,POINTER_HSPLIT );
1006 
1007         DrawInvert( aButtonDownChangePoint.X(), POINTER_HSPLIT );
1008         bColRulerMove = sal_True;
1009     }
1010 }
1011 
1012 void __EXPORT ScPreview::MouseButtonUp( const MouseEvent& rMEvt )
1013 {
1014         Fraction  aPreviewZoom( nZoom, 100 );
1015         Fraction  aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
1016         MapMode   aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
1017 
1018         aButtonUpPt = PixelToLogic( rMEvt.GetPosPixel(),aMMMode );
1019 
1020         long  nWidth = (long) lcl_GetDocPageSize(pDocShell->GetDocument(), nTab).Width();
1021         long  nHeight = (long) lcl_GetDocPageSize(pDocShell->GetDocument(), nTab).Height();
1022 
1023         if( rMEvt.IsLeft() && GetPointer() == POINTER_HSIZEBAR )
1024         {
1025             SetPointer( Pointer( POINTER_ARROW ) );
1026 
1027             sal_Bool bMoveRulerAction= sal_True;
1028 
1029             ScDocument * pDoc = pDocShell->GetDocument();
1030             String   aOldName = pDoc->GetPageStyle( nTab );
1031             sal_Bool  bUndo( pDoc->IsUndoEnabled() );
1032             ScStyleSheetPool* pStylePool = pDoc->GetStyleSheetPool();
1033             SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aOldName, SFX_STYLE_FAMILY_PAGE );
1034 
1035             if ( pStyleSheet )
1036             {
1037                 ScStyleSaveData aOldData;
1038                 if( bUndo )
1039                     aOldData.InitFromStyle( pStyleSheet );
1040 
1041                 SfxItemSet&  rStyleSet = pStyleSheet->GetItemSet();
1042 
1043                 SvxLRSpaceItem aLRItem = ( const SvxLRSpaceItem& ) rStyleSet.Get( ATTR_LRSPACE );
1044 
1045                 if(( bLeftRulerChange || bRightRulerChange ) && ( aButtonUpPt.X() <= ( 0 - aOffset.X() ) || aButtonUpPt.X() > nWidth * HMM_PER_TWIPS - aOffset.X() ) )
1046                 {
1047                     bMoveRulerAction = sal_False;
1048                     Paint(Rectangle(0,0,10000,10000));
1049                 }
1050                 else if( bLeftRulerChange && ( aButtonUpPt.X() / HMM_PER_TWIPS > nWidth - aLRItem.GetRight() - aOffset.X() / HMM_PER_TWIPS ) )
1051                 {
1052                     bMoveRulerAction = sal_False;
1053                     Paint(Rectangle(0,0,10000,10000));
1054                 }
1055                 else if( bRightRulerChange && ( aButtonUpPt.X() / HMM_PER_TWIPS < aLRItem.GetLeft() - aOffset.X() / HMM_PER_TWIPS ) )
1056                 {
1057                     bMoveRulerAction = sal_False;
1058                     Paint(Rectangle(0,0,10000,10000));
1059                 }
1060                 else if( aButtonDownPt.X() == aButtonUpPt.X() )
1061                 {
1062                     bMoveRulerAction = sal_False;
1063                     DrawInvert( aButtonUpPt.X(), POINTER_HSIZEBAR );
1064                 }
1065                 if( bMoveRulerAction )
1066                 {
1067                     ScDocShellModificator aModificator( *pDocShell );
1068                     if( bLeftRulerChange && bLeftRulerMove )
1069                     {
1070                        aLRItem.SetLeft( (long)( aButtonUpPt.X() / HMM_PER_TWIPS + aOffset.X() / HMM_PER_TWIPS ));
1071                        rStyleSet.Put( aLRItem );
1072                     }
1073                     else if( bRightRulerChange && bRightRulerMove )
1074                     {
1075                         aLRItem.SetRight( (long)( nWidth - aButtonUpPt.X() / HMM_PER_TWIPS - aOffset.X() / HMM_PER_TWIPS ));
1076                         rStyleSet.Put( aLRItem );
1077                     }
1078 
1079                     ScStyleSaveData aNewData;
1080                     aNewData.InitFromStyle( pStyleSheet );
1081                     if( bUndo )
1082                     {
1083                         pDocShell->GetUndoManager()->AddUndoAction(
1084                             new ScUndoModifyStyle( pDocShell, SFX_STYLE_FAMILY_PAGE,
1085                             aOldData, aNewData ) );
1086                     }
1087 
1088                     if ( ValidTab( nTab ) )
1089                     {
1090                         ScPrintFunc aPrintFunc( this, pDocShell, nTab );
1091                         aPrintFunc.UpdatePages();
1092                     }
1093 
1094                     Rectangle aRect(0,0,10000,10000);
1095                     Paint( aRect );
1096                     aModificator.SetDocumentModified();
1097                     bLeftRulerChange = sal_False;
1098                     bRightRulerChange = sal_False;
1099                 }
1100             }
1101             bLeftRulerMove = sal_False;
1102             bRightRulerMove = sal_False;
1103         }
1104 
1105         if( rMEvt.IsLeft() && GetPointer() == POINTER_VSIZEBAR )
1106         {
1107             SetPointer( POINTER_ARROW );
1108 
1109             sal_Bool  bMoveRulerAction = sal_True;
1110             if( ( bTopRulerChange || bBottomRulerChange || bHeaderRulerChange || bFooterRulerChange ) && ( aButtonUpPt.Y() <= ( 0 - aOffset.Y() ) || aButtonUpPt.Y() > nHeight * HMM_PER_TWIPS -aOffset.Y() ) )
1111             {
1112                 bMoveRulerAction = sal_False;
1113                 Paint( Rectangle(0,0,10000,10000) );
1114             }
1115             else if( aButtonDownPt.Y() == aButtonUpPt.Y() )
1116             {
1117                 bMoveRulerAction = sal_False;
1118                 DrawInvert( aButtonUpPt.Y(), POINTER_VSIZEBAR );
1119             }
1120             if( bMoveRulerAction )
1121             {
1122                 ScDocument * pDoc = pDocShell->GetDocument();
1123                 sal_Bool  bUndo( pDoc->IsUndoEnabled() );
1124                 ScStyleSheetPool* pStylePool = pDoc->GetStyleSheetPool();
1125                 SfxStyleSheetBase* pStyleSheet = pStylePool->Find( pDoc->GetPageStyle( nTab ), SFX_STYLE_FAMILY_PAGE );
1126                 DBG_ASSERT( pStyleSheet, "PageStyle not found" );
1127                 if ( pStyleSheet )
1128                 {
1129                     ScDocShellModificator aModificator( *pDocShell );
1130                     ScStyleSaveData aOldData;
1131                     if( bUndo )
1132                         aOldData.InitFromStyle( pStyleSheet );
1133 
1134                     SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
1135 
1136                     SvxULSpaceItem aULItem = ( const SvxULSpaceItem&)rStyleSet.Get( ATTR_ULSPACE );
1137 
1138                     if( bTopRulerMove && bTopRulerChange )
1139                     {
1140                         aULItem.SetUpperValue( (sal_uInt16)( aButtonUpPt.Y() / HMM_PER_TWIPS + aOffset.Y() / HMM_PER_TWIPS ) );
1141                         rStyleSet.Put( aULItem );
1142                     }
1143                     else if( bBottomRulerMove && bBottomRulerChange )
1144                     {
1145                         aULItem.SetLowerValue( (sal_uInt16)( nHeight - aButtonUpPt.Y() / HMM_PER_TWIPS - aOffset.Y() / HMM_PER_TWIPS ) );
1146                         rStyleSet.Put( aULItem );
1147                     }
1148                     else if( bHeaderRulerMove && bHeaderRulerChange )
1149                     {
1150                         const SfxPoolItem* pItem = NULL;
1151                         if ( rStyleSet.GetItemState( ATTR_PAGE_HEADERSET, sal_False, &pItem ) == SFX_ITEM_SET )
1152                         {
1153                             SfxItemSet& pHeaderSet = ((SvxSetItem*)pItem)->GetItemSet();
1154                             Size  aHeaderSize = ((const SvxSizeItem&)pHeaderSet.Get(ATTR_PAGE_SIZE)).GetSize();
1155                             aHeaderSize.Height() = (long)( aButtonUpPt.Y() / HMM_PER_TWIPS + aOffset.Y() / HMM_PER_TWIPS - aULItem.GetUpper());
1156                             aHeaderSize.Height() = aHeaderSize.Height() * 100 / mnScale;
1157                             SvxSetItem  aNewHeader( (const SvxSetItem&)rStyleSet.Get(ATTR_PAGE_HEADERSET) );
1158                             aNewHeader.GetItemSet().Put( SvxSizeItem( ATTR_PAGE_SIZE, aHeaderSize ) );
1159                             rStyleSet.Put( aNewHeader );
1160                         }
1161                     }
1162                     else if( bFooterRulerMove && bFooterRulerChange )
1163                     {
1164                         const SfxPoolItem* pItem = NULL;
1165                         if( rStyleSet.GetItemState( ATTR_PAGE_FOOTERSET, sal_False, &pItem ) == SFX_ITEM_SET )
1166                         {
1167                             SfxItemSet& pFooterSet = ((SvxSetItem*)pItem)->GetItemSet();
1168                             Size aFooterSize = ((const SvxSizeItem&)pFooterSet.Get(ATTR_PAGE_SIZE)).GetSize();
1169                             aFooterSize.Height() = (long)( nHeight - aButtonUpPt.Y() / HMM_PER_TWIPS - aOffset.Y() / HMM_PER_TWIPS - aULItem.GetLower() );
1170                             aFooterSize.Height() = aFooterSize.Height() * 100 / mnScale;
1171                             SvxSetItem  aNewFooter( (const SvxSetItem&)rStyleSet.Get(ATTR_PAGE_FOOTERSET) );
1172                             aNewFooter.GetItemSet().Put( SvxSizeItem( ATTR_PAGE_SIZE, aFooterSize ) );
1173                             rStyleSet.Put( aNewFooter );
1174                         }
1175                     }
1176 
1177                     ScStyleSaveData aNewData;
1178                     aNewData.InitFromStyle( pStyleSheet );
1179                     if( bUndo )
1180                     {
1181                         pDocShell->GetUndoManager()->AddUndoAction(
1182                             new ScUndoModifyStyle( pDocShell, SFX_STYLE_FAMILY_PAGE,
1183                             aOldData, aNewData ) );
1184                     }
1185 
1186                     if ( ValidTab( nTab ) )
1187                     {
1188                         ScPrintFunc aPrintFunc( this, pDocShell, nTab );
1189                         aPrintFunc.UpdatePages();
1190                     }
1191 
1192                     Rectangle  aRect(0,0,10000,10000);
1193                     Paint( aRect );
1194                     aModificator.SetDocumentModified();
1195                     bTopRulerChange = sal_False;
1196                     bBottomRulerChange = sal_False;
1197                     bHeaderRulerChange = sal_False;
1198                     bFooterRulerChange = sal_False;
1199                  }
1200               }
1201               bTopRulerMove = sal_False;
1202               bBottomRulerMove = sal_False;
1203               bHeaderRulerMove = sal_False;
1204               bFooterRulerMove = sal_False;
1205         }
1206         if( rMEvt.IsLeft() && GetPointer() == POINTER_HSPLIT )
1207         {
1208             SetPointer(POINTER_ARROW);
1209             ScDocument* pDoc = pDocShell->GetDocument();
1210             sal_Bool  bLayoutRTL = pDoc->IsLayoutRTL( nTab );
1211             sal_Bool  bMoveRulerAction = sal_True;
1212             if( aButtonDownPt.X() == aButtonUpPt.X() )
1213             {
1214                 bMoveRulerAction = sal_False;
1215                 if( nColNumberButttonDown == aPageArea.aStart.Col() )
1216                     DrawInvert( PixelToLogic( Point( nLeftPosition, 0 ),aMMMode ).X() ,POINTER_HSPLIT );
1217                 else
1218                     DrawInvert( PixelToLogic( Point( nRight[ nColNumberButttonDown-1 ], 0 ),aMMMode ).X() ,POINTER_HSPLIT );
1219                 DrawInvert( aButtonUpPt.X(), POINTER_HSPLIT );
1220             }
1221             if( bMoveRulerAction )
1222             {
1223                 long  nNewColWidth = 0;
1224                 ScDocFunc aFunc(*pDocShell);
1225                 SCCOLROW nCols[2] = { nColNumberButttonDown, nColNumberButttonDown };
1226 
1227                 if( !bLayoutRTL )
1228                 {
1229                     nNewColWidth = (long) ( PixelToLogic( Point( rMEvt.GetPosPixel().X() - nRight[ nColNumberButttonDown ], 0), aMMMode ).X() / HMM_PER_TWIPS ) * 100 / mnScale;
1230                     nNewColWidth += pDocShell->GetDocument()->GetColWidth( nColNumberButttonDown, nTab );
1231                 }
1232                 else
1233                 {
1234 
1235                     nNewColWidth = (long) ( PixelToLogic( Point( nRight[ nColNumberButttonDown ] - rMEvt.GetPosPixel().X(), 0), aMMMode ).X() / HMM_PER_TWIPS ) * 100 / mnScale;
1236                     nNewColWidth += pDocShell->GetDocument()->GetColWidth( nColNumberButttonDown, nTab );
1237                 }
1238 
1239                 if( nNewColWidth >= 0 )
1240                 {
1241                     aFunc.SetWidthOrHeight( sal_True, 1,nCols, nTab, SC_SIZE_DIRECT, (sal_uInt16)nNewColWidth, sal_True, sal_True);
1242                 }
1243                 if ( ValidTab( nTab ) )
1244                 {
1245                     ScPrintFunc aPrintFunc( this, pDocShell, nTab );
1246                     aPrintFunc.UpdatePages();
1247                 }
1248                 Rectangle  nRect(0,0,10000,10000);
1249                 Paint( nRect );
1250             }
1251             bColRulerMove = sal_False;
1252         }
1253         ReleaseMouse();
1254 }
1255 
1256 void __EXPORT ScPreview::MouseMove( const MouseEvent& rMEvt )
1257 {
1258     Fraction aPreviewZoom( nZoom, 100 );
1259     Fraction aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
1260     MapMode  aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
1261     Point    aMouseMovePoint = PixelToLogic( rMEvt.GetPosPixel(), aMMMode );
1262 
1263     long    nLeftMargin = 0;
1264     long    nRightMargin = 0;
1265     long    nTopMargin = 0;
1266     long    nBottomMargin = 0;
1267     Size    PageSize;
1268 
1269     long    nWidth = (long) lcl_GetDocPageSize(pDocShell->GetDocument(), nTab).Width();
1270     long    nHeight = (long) lcl_GetDocPageSize(pDocShell->GetDocument(), nTab).Height();
1271 
1272     if ( nPageNo < nTotalPages )
1273     {
1274         ScPrintOptions aOptions = SC_MOD()->GetPrintOptions();
1275 
1276         ScPrintFunc* pPrintFunc;
1277 
1278         if (bStateValid)
1279             pPrintFunc = new ScPrintFunc( this, pDocShell, aState, &aOptions );
1280         else
1281             pPrintFunc = new ScPrintFunc( this, pDocShell, nTab, nFirstAttr[nTab], nTotalPages, NULL, &aOptions );
1282 
1283         nLeftMargin = (long)( pPrintFunc->GetLeftMargin() * HMM_PER_TWIPS - aOffset.X() );
1284         nRightMargin = (long)( pPrintFunc->GetRightMargin() * HMM_PER_TWIPS );
1285         nRightMargin = (long)( nWidth * HMM_PER_TWIPS - nRightMargin - aOffset.X() );
1286         nTopMargin = (long)( pPrintFunc->GetTopMargin() * HMM_PER_TWIPS - aOffset.Y() );
1287         nBottomMargin = (long)( pPrintFunc->GetBottomMargin() * HMM_PER_TWIPS );
1288         nBottomMargin = (long)( nHeight * HMM_PER_TWIPS - nBottomMargin - aOffset.Y() );
1289         if( mnScale > 0 )
1290         {
1291             nHeaderHeight = (long)( nTopMargin + pPrintFunc->GetHeader().nHeight * HMM_PER_TWIPS * mnScale / 100 );
1292             nFooterHeight = (long)( nBottomMargin - pPrintFunc->GetFooter().nHeight * HMM_PER_TWIPS * mnScale / 100 );
1293         }
1294         else
1295         {
1296             nHeaderHeight = (long)( nTopMargin + pPrintFunc->GetHeader().nHeight * HMM_PER_TWIPS );
1297             nFooterHeight = (long)( nBottomMargin - pPrintFunc->GetFooter().nHeight * HMM_PER_TWIPS );
1298         }
1299         delete pPrintFunc;
1300     }
1301 
1302     Point   aPixPt( rMEvt.GetPosPixel() );
1303     Point   aLeftTop = LogicToPixel( Point( nLeftMargin, -aOffset.Y() ) , aMMMode );
1304     Point   aLeftBottom = LogicToPixel( Point( nLeftMargin ,(long)(nHeight * HMM_PER_TWIPS - aOffset.Y()) ), aMMMode );
1305     Point   aRightTop = LogicToPixel( Point( nRightMargin, -aOffset.Y() ), aMMMode );
1306     Point   aTopLeft = LogicToPixel( Point( -aOffset.X(), nTopMargin ), aMMMode );
1307     Point   aTopRight = LogicToPixel( Point( (long)(nWidth * HMM_PER_TWIPS - aOffset.X()), nTopMargin ), aMMMode );
1308     Point   aBottomLeft = LogicToPixel( Point( -aOffset.X(), nBottomMargin ), aMMMode );
1309     Point   aHeaderLeft = LogicToPixel( Point(  -aOffset.X(), nHeaderHeight ), aMMMode );
1310     Point   aFooderLeft = LogicToPixel( Point( -aOffset.X(), nFooterHeight ), aMMMode );
1311 
1312     sal_Bool   bOnColRulerChange = sal_False;
1313 
1314     for( SCCOL i=aPageArea.aStart.Col(); i<= aPageArea.aEnd.Col(); i++ )
1315     {
1316         Point   aColumnTop = LogicToPixel( Point( 0, -aOffset.Y() ) ,aMMMode );
1317         Point   aColumnBottom = LogicToPixel( Point( 0, (long)( nHeight * HMM_PER_TWIPS - aOffset.Y()) ), aMMMode );
1318         if( aPixPt.X() < ( nRight[i] + 2 ) && ( aPixPt.X() > ( nRight[i] - 2 ) ) && ( aPixPt.X() < aRightTop.X() ) && ( aPixPt.X() > aLeftTop.X() )
1319             && ( aPixPt.Y() > aColumnTop.Y() ) && ( aPixPt.Y() < aColumnBottom.Y() ) && !bLeftRulerMove && !bRightRulerMove
1320             && !bTopRulerMove && !bBottomRulerMove && !bHeaderRulerMove && !bFooterRulerMove )
1321         {
1322             bOnColRulerChange = sal_True;
1323             if( !rMEvt.GetButtons() && GetPointer() == POINTER_HSPLIT )
1324                 nColNumberButttonDown = i;
1325             break;
1326         }
1327     }
1328 
1329     if( aPixPt.X() < ( aLeftTop.X() + 2 ) && aPixPt.X() > ( aLeftTop.X() - 2 ) && !bRightRulerMove )
1330     {
1331         bLeftRulerChange = sal_True;
1332         bRightRulerChange = sal_False;
1333     }
1334     else if( aPixPt.X() < ( aRightTop.X() + 2 ) && aPixPt.X() > ( aRightTop.X() - 2 ) && !bLeftRulerMove )
1335     {
1336         bLeftRulerChange = sal_False;
1337         bRightRulerChange = sal_True;
1338     }
1339     else if( aPixPt.Y() < ( aTopLeft.Y() + 2 ) && aPixPt.Y() > ( aTopLeft.Y() - 2 ) && !bBottomRulerMove && !bHeaderRulerMove && !bFooterRulerMove )
1340     {
1341         bTopRulerChange = sal_True;
1342         bBottomRulerChange = sal_False;
1343         bHeaderRulerChange = sal_False;
1344         bFooterRulerChange = sal_False;
1345     }
1346     else if( aPixPt.Y() < ( aBottomLeft.Y() + 2 ) && aPixPt.Y() > ( aBottomLeft.Y() - 2 ) && !bTopRulerMove && !bHeaderRulerMove && !bFooterRulerMove )
1347     {
1348         bTopRulerChange = sal_False;
1349         bBottomRulerChange = sal_True;
1350         bHeaderRulerChange = sal_False;
1351         bFooterRulerChange = sal_False;
1352     }
1353     else if( aPixPt.Y() < ( aHeaderLeft.Y() + 2 ) && aPixPt.Y() > ( aHeaderLeft.Y() - 2 ) && !bTopRulerMove && !bBottomRulerMove && !bFooterRulerMove )
1354     {
1355         bTopRulerChange = sal_False;
1356         bBottomRulerChange = sal_False;
1357         bHeaderRulerChange = sal_True;
1358         bFooterRulerChange = sal_False;
1359     }
1360     else if( aPixPt.Y() < ( aFooderLeft.Y() + 2 ) && aPixPt.Y() > ( aFooderLeft.Y() - 2 ) && !bTopRulerMove && !bBottomRulerMove && !bHeaderRulerMove )
1361     {
1362         bTopRulerChange = sal_False;
1363         bBottomRulerChange = sal_False;
1364         bHeaderRulerChange = sal_False;
1365         bFooterRulerChange = sal_True;
1366     }
1367 
1368     if( bPageMargin )
1369     {
1370         if(( (aPixPt.X() < ( aLeftTop.X() + 2 ) && aPixPt.X() > ( aLeftTop.X() - 2 )) || bLeftRulerMove ||
1371             ( aPixPt.X() < ( aRightTop.X() + 2 ) && aPixPt.X() > ( aRightTop.X() - 2 ) ) || bRightRulerMove || bOnColRulerChange || bColRulerMove )
1372             && aPixPt.Y() > aLeftTop.Y() && aPixPt.Y() < aLeftBottom.Y() )
1373         {
1374             if( bOnColRulerChange || bColRulerMove )
1375             {
1376                 SetPointer( Pointer( POINTER_HSPLIT ) );
1377                 if( bColRulerMove )
1378                 {
1379                     if( aMouseMovePoint.X() > -aOffset.X() && aMouseMovePoint.X() < nWidth * HMM_PER_TWIPS - aOffset.X() )
1380                        DragMove( aMouseMovePoint.X(), POINTER_HSPLIT );
1381                 }
1382             }
1383             else
1384             {
1385                 if( bLeftRulerChange && !bTopRulerMove && !bBottomRulerMove && !bHeaderRulerMove && !bFooterRulerMove )
1386                 {
1387                     SetPointer( Pointer( POINTER_HSIZEBAR ) );
1388                     if( bLeftRulerMove )
1389                     {
1390                        if( aMouseMovePoint.X() > -aOffset.X() && aMouseMovePoint.X() < nWidth * HMM_PER_TWIPS - aOffset.X() )
1391                            DragMove( aMouseMovePoint.X(), POINTER_HSIZEBAR );
1392                     }
1393                 }
1394                 else if( bRightRulerChange && !bTopRulerMove && !bBottomRulerMove && !bHeaderRulerMove && !bFooterRulerMove )
1395                 {
1396                     SetPointer( Pointer( POINTER_HSIZEBAR ) );
1397                     if( bRightRulerMove )
1398                     {
1399                        if( aMouseMovePoint.X() > -aOffset.X() && aMouseMovePoint.X() < nWidth * HMM_PER_TWIPS - aOffset.X() )
1400                            DragMove( aMouseMovePoint.X(), POINTER_HSIZEBAR );
1401                     }
1402                 }
1403             }
1404         }
1405         else
1406         {
1407             if( ( ( aPixPt.Y() < ( aTopLeft.Y() + 2 ) && aPixPt.Y() > ( aTopLeft.Y() - 2 ) ) || bTopRulerMove ||
1408                 ( aPixPt.Y() < ( aBottomLeft.Y() + 2 ) && aPixPt.Y() > ( aBottomLeft.Y() - 2 ) ) || bBottomRulerMove ||
1409                 ( aPixPt.Y() < ( aHeaderLeft.Y() + 2 ) && aPixPt.Y() > ( aHeaderLeft.Y() - 2 ) ) || bHeaderRulerMove ||
1410                 ( aPixPt.Y() < ( aFooderLeft.Y() + 2 ) && aPixPt.Y() > ( aFooderLeft.Y() - 2 ) ) || bFooterRulerMove )
1411                 && aPixPt.X() > aTopLeft.X() && aPixPt.X() < aTopRight.X() )
1412             {
1413                 if( bTopRulerChange )
1414                 {
1415                     SetPointer( Pointer( POINTER_VSIZEBAR ) );
1416                     if( bTopRulerMove )
1417                     {
1418                         if( aMouseMovePoint.Y() > -aOffset.Y() && aMouseMovePoint.Y() < nHeight * HMM_PER_TWIPS - aOffset.Y() )
1419                             DragMove( aMouseMovePoint.Y(), POINTER_VSIZEBAR );
1420                     }
1421                 }
1422                 else if( bBottomRulerChange )
1423                 {
1424                     SetPointer( Pointer( POINTER_VSIZEBAR ) );
1425                     if( bBottomRulerMove )
1426                     {
1427                         if( aMouseMovePoint.Y() > -aOffset.Y() && aMouseMovePoint.Y() < nHeight * HMM_PER_TWIPS - aOffset.Y() )
1428                             DragMove( aMouseMovePoint.Y(), POINTER_VSIZEBAR );
1429                     }
1430                 }
1431                 else if( bHeaderRulerChange )
1432                 {
1433                     SetPointer( Pointer( POINTER_VSIZEBAR ) );
1434                     if( bHeaderRulerMove )
1435                     {
1436                         if( aMouseMovePoint.Y() > -aOffset.Y() && aMouseMovePoint.Y() < nHeight * HMM_PER_TWIPS - aOffset.Y() )
1437                             DragMove( aMouseMovePoint.Y(), POINTER_VSIZEBAR );
1438                     }
1439                 }
1440                 else if( bFooterRulerChange )
1441                 {
1442                     SetPointer( Pointer( POINTER_VSIZEBAR ) );
1443                     if( bFooterRulerMove )
1444                     {
1445                         if( aMouseMovePoint.Y() > -aOffset.Y() && aMouseMovePoint.Y() < nHeight * HMM_PER_TWIPS - aOffset.Y() )
1446                             DragMove( aMouseMovePoint.Y(), POINTER_VSIZEBAR );
1447                     }
1448                 }
1449             }
1450             else
1451                 SetPointer( Pointer( POINTER_ARROW ) );
1452         }
1453     }
1454 }
1455 //Issue51656 Add resizeable margin on page preview from maoyg
1456 void ScPreview::InvalidateLocationData(sal_uLong nId)
1457 {
1458 	bLocationValid = sal_False;
1459     if (pViewShell->HasAccessibilityObjects())
1460     	pViewShell->BroadcastAccessibility( SfxSimpleHint( nId ) );
1461 }
1462 
1463 void ScPreview::GetFocus()
1464 {
1465     if (pViewShell->HasAccessibilityObjects())
1466         pViewShell->BroadcastAccessibility( ScAccWinFocusGotHint(GetAccessible()) );
1467 }
1468 
1469 void ScPreview::LoseFocus()
1470 {
1471     if (pViewShell->HasAccessibilityObjects())
1472         pViewShell->BroadcastAccessibility( ScAccWinFocusLostHint(GetAccessible()) );
1473 }
1474 
1475 com::sun::star::uno::Reference<com::sun::star::accessibility::XAccessible> ScPreview::CreateAccessible()
1476 {
1477 	ScAccessibleDocumentPagePreview* pAccessible =
1478 		new ScAccessibleDocumentPagePreview( GetAccessibleParentWindow()->GetAccessible(), pViewShell );
1479 	com::sun::star::uno::Reference < com::sun::star::accessibility::XAccessible > xAccessible = pAccessible;
1480 	pAccessible->Init();
1481 	return xAccessible;
1482 }
1483 
1484 //Issue51656 Add resizeable margin on page preview from maoyg
1485 void ScPreview::DragMove( long nDragMovePos, sal_uInt16 nFlags )
1486 {
1487     Fraction aPreviewZoom( nZoom, 100 );
1488     Fraction aHorPrevZoom( (long)( 100 * nZoom / pDocShell->GetOutputFactor() ), 10000 );
1489     MapMode  aMMMode( MAP_100TH_MM, Point(), aHorPrevZoom, aPreviewZoom );
1490     SetMapMode( aMMMode );
1491     long  nPos = nDragMovePos;
1492     if( nFlags == POINTER_HSIZEBAR || nFlags == POINTER_HSPLIT )
1493     {
1494         if( nDragMovePos != aButtonDownChangePoint.X() )
1495         {
1496             DrawInvert( aButtonDownChangePoint.X(), nFlags );
1497             aButtonDownChangePoint.X() = nPos;
1498             DrawInvert( aButtonDownChangePoint.X(), nFlags );
1499         }
1500     }
1501     else if( nFlags == POINTER_VSIZEBAR )
1502     {
1503         if( nDragMovePos != aButtonDownChangePoint.Y() )
1504         {
1505             DrawInvert( aButtonDownChangePoint.Y(), nFlags );
1506             aButtonDownChangePoint.Y() = nPos;
1507             DrawInvert( aButtonDownChangePoint.Y(), nFlags );
1508         }
1509     }
1510 }
1511 
1512 void ScPreview::DrawInvert( long nDragPos, sal_uInt16 nFlags )
1513 {
1514     long  nHeight = (long) lcl_GetDocPageSize( pDocShell->GetDocument(), nTab ).Height();
1515     long  nWidth = (long) lcl_GetDocPageSize( pDocShell->GetDocument(), nTab ).Width();
1516     if( nFlags == POINTER_HSIZEBAR || nFlags == POINTER_HSPLIT )
1517     {
1518         Rectangle aRect( nDragPos, -aOffset.Y(), nDragPos + 1,(long)( ( nHeight * HMM_PER_TWIPS ) - aOffset.Y()));
1519         Invert( aRect,INVERT_50 );
1520     }
1521     else if( nFlags == POINTER_VSIZEBAR )
1522     {
1523         Rectangle aRect( -aOffset.X(), nDragPos,(long)( ( nWidth * HMM_PER_TWIPS ) - aOffset.X() ), nDragPos + 1 );
1524         Invert( aRect,INVERT_50 );
1525     }
1526 }
1527 //Issue51656 Add resizeable margin on page preview from maoyg
1528