bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / dialog / pagectrl.cxx
blobe4469cdf58e74dbabc5a2d3888d97cd200d58433
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <vcl/bitmap.hxx>
21 #include <editeng/frmdiritem.hxx>
22 #include <svx/pageitem.hxx>
23 #include <svx/pagectrl.hxx>
24 #include <editeng/boxitem.hxx>
26 #include <algorithm>
28 // struct PageWindow_Impl ------------------------------------------------
30 struct PageWindow_Impl
32 SvxBoxItem* pBorder;
33 Bitmap aBitmap;
34 bool bBitmap;
35 bool bResetBackground;
36 sal_Bool bFrameDirection;
37 sal_Int32 nFrameDirection;
40 PageWindow_Impl() :
41 pBorder(0),
42 bBitmap(false),
43 bResetBackground(false),
44 bFrameDirection(sal_False),
45 nFrameDirection(0) {}
47 void EnableFrameDirection(sal_Bool bEnable){bFrameDirection = bEnable;}
48 void SetFrameDirection(sal_Int32 nDirection){nFrameDirection = nDirection;}
52 // STATIC DATA -----------------------------------------------------------
54 #define CELL_WIDTH 1600L
55 #define CELL_HEIGHT 800L
57 // class SvxPageWindow ---------------------------------------------------
59 SvxPageWindow::SvxPageWindow( Window* pParent, const ResId& rId ) :
61 Window( pParent, rId ),
63 nTop ( 0 ),
64 nBottom ( 0 ),
65 nLeft ( 0 ),
66 nRight ( 0 ),
67 aColor ( COL_TRANSPARENT ),
68 nHdLeft ( 0 ),
69 nHdRight ( 0 ),
70 nHdDist ( 0 ),
71 nHdHeight ( 0 ),
72 aHdColor ( COL_TRANSPARENT ),
73 pHdBorder ( 0 ),
74 nFtLeft ( 0 ),
75 nFtRight ( 0 ),
76 nFtDist ( 0 ),
77 nFtHeight ( 0 ),
78 aFtColor ( COL_TRANSPARENT ),
79 pFtBorder ( 0 ),
80 bFooter ( sal_False ),
81 bHeader ( sal_False ),
82 bTable ( sal_False ),
83 bHorz ( sal_False ),
84 bVert ( sal_False ),
85 eUsage ( SVX_PAGE_ALL )
88 pImpl = new PageWindow_Impl;
90 // Count in Twips by default
91 SetMapMode( MapMode( MAP_TWIP ) );
92 aWinSize = GetOutputSizePixel();
93 aWinSize.Height() -= 4;
94 aWinSize.Width() -= 4;
96 aWinSize = PixelToLogic( aWinSize );
97 SetBackground();
100 SvxPageWindow::SvxPageWindow( Window* pParent ) :
102 Window( pParent ),
104 nTop ( 0 ),
105 nBottom ( 0 ),
106 nLeft ( 0 ),
107 nRight ( 0 ),
108 aColor ( COL_TRANSPARENT ),
109 nHdLeft ( 0 ),
110 nHdRight ( 0 ),
111 nHdDist ( 0 ),
112 nHdHeight ( 0 ),
113 aHdColor ( COL_TRANSPARENT ),
114 pHdBorder ( 0 ),
115 nFtLeft ( 0 ),
116 nFtRight ( 0 ),
117 nFtDist ( 0 ),
118 nFtHeight ( 0 ),
119 aFtColor ( COL_TRANSPARENT ),
120 pFtBorder ( 0 ),
121 bFooter ( sal_False ),
122 bHeader ( sal_False ),
123 bTable ( sal_False ),
124 bHorz ( sal_False ),
125 bVert ( sal_False ),
126 eUsage ( SVX_PAGE_ALL )
128 pImpl = new PageWindow_Impl;
130 // Count in Twips by default
131 SetMapMode( MapMode( MAP_TWIP ) );
132 aWinSize = GetOptimalSize();
133 aWinSize.Height() -= 4;
134 aWinSize.Width() -= 4;
136 aWinSize = PixelToLogic( aWinSize );
137 SetBackground();
140 // -----------------------------------------------------------------------
142 SvxPageWindow::~SvxPageWindow()
144 delete pImpl;
145 delete pHdBorder;
146 delete pFtBorder;
149 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxPageWindow(Window *pParent)
151 return new SvxPageWindow(pParent);
155 // -----------------------------------------------------------------------
157 void SvxPageWindow::Paint( const Rectangle& )
159 Fraction aXScale( aWinSize.Width(), std::max( (long) (aSize.Width() * 2 + aSize.Width() / 8), 1L ) );
160 Fraction aYScale( aWinSize.Height(), std::max( aSize.Height(), 1L ) );
161 MapMode aMapMode( GetMapMode() );
163 if ( aYScale < aXScale )
165 aMapMode.SetScaleX( aYScale );
166 aMapMode.SetScaleY( aYScale );
168 else
170 aMapMode.SetScaleX( aXScale );
171 aMapMode.SetScaleY( aXScale );
173 SetMapMode( aMapMode );
174 Size aSz( PixelToLogic( GetSizePixel() ) );
175 long nYPos = ( aSz.Height() - aSize.Height() ) / 2;
177 if ( eUsage == SVX_PAGE_ALL )
179 // all pages are equal -> draw one page
180 if ( aSize.Width() > aSize.Height() )
182 // Draw Landscape page of the same size
183 Fraction aX = aMapMode.GetScaleX();
184 Fraction aY = aMapMode.GetScaleY();
185 Fraction a2( 1.5 );
186 aX *= a2;
187 aY *= a2;
188 aMapMode.SetScaleX( aX );
189 aMapMode.SetScaleY( aY );
190 SetMapMode( aMapMode );
191 aSz = PixelToLogic( GetSizePixel() );
192 nYPos = ( aSz.Height() - aSize.Height() ) / 2;
193 long nXPos = ( aSz.Width() - aSize.Width() ) / 2;
194 DrawPage( Point( nXPos, nYPos ), sal_True, sal_True );
196 else
197 // Portrait
198 DrawPage( Point( ( aSz.Width() - aSize.Width() ) / 2, nYPos ), sal_True, sal_True );
200 else
202 // Left and right page are different -> draw two pages if possible
203 DrawPage( Point( 0, nYPos ), sal_False, (sal_Bool)( eUsage & SVX_PAGE_LEFT ) );
204 DrawPage( Point( aSize.Width() + aSize.Width() / 8, nYPos ), sal_True,
205 (sal_Bool)( eUsage & SVX_PAGE_RIGHT ) );
209 // -----------------------------------------------------------------------
210 void SvxPageWindow::DrawPage( const Point& rOrg, const sal_Bool bSecond, const sal_Bool bEnabled )
212 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
213 const Color& rFieldColor = rStyleSettings.GetFieldColor();
214 const Color& rFieldTextColor = rStyleSettings.GetFieldTextColor();
215 const Color& rDisableColor = rStyleSettings.GetDisableColor();
216 const Color& rDlgColor = rStyleSettings.GetDialogColor();
218 // background
219 if(!bSecond || pImpl->bResetBackground)
221 SetLineColor( Color(COL_TRANSPARENT) );
222 SetFillColor( rDlgColor );
223 Size winSize(GetOutputSize());
224 DrawRect( Rectangle( Point(0,0), winSize ) );
226 if ( pImpl->bResetBackground )
227 pImpl->bResetBackground = false;
229 SetLineColor( rFieldTextColor );
230 // Shadow
231 Size aTempSize = aSize;
232 // Page
233 if ( !bEnabled )
235 SetFillColor( rDisableColor );
236 DrawRect( Rectangle( rOrg, aTempSize ) );
237 return;
239 SetFillColor( rFieldColor );
240 DrawRect( Rectangle( rOrg, aTempSize ) );
242 long nL = nLeft;
243 long nR = nRight;
245 if ( eUsage == SVX_PAGE_MIRROR && !bSecond )
247 // turn for mirrored
248 nL = nRight;
249 nR = nLeft;
252 Rectangle aRect;
254 aRect.Left() = rOrg.X() + nL;
255 aRect.Right() = rOrg.X() + aTempSize.Width() - nR;
256 aRect.Top() = rOrg.Y() + nTop;
257 aRect.Bottom()= rOrg.Y() + aTempSize.Height() - nBottom;
259 Rectangle aHdRect( aRect );
260 Rectangle aFtRect( aRect );
262 if ( bHeader )
264 // show headers if possible
265 aHdRect.Left() += nHdLeft;
266 aHdRect.Right() -= nHdRight;
267 aHdRect.Bottom() = aRect.Top() + nHdHeight;
268 aRect.Top() += nHdHeight + nHdDist;
269 SetFillColor( aHdColor );
270 DrawRect( aHdRect );
273 if ( bFooter )
275 // show footer if possible
276 aFtRect.Left() += nFtLeft;
277 aFtRect.Right() -= nFtRight;
278 aFtRect.Top() = aRect.Bottom() - nFtHeight;
279 aRect.Bottom() -= nFtHeight + nFtDist;
280 SetFillColor( aFtColor );
281 DrawRect( aFtRect );
284 // Paint Body
285 SetFillColor( aColor );
286 if ( pImpl->bBitmap )
288 DrawRect( aRect );
289 Point aBmpPnt = aRect.TopLeft();
290 Size aBmpSiz = aRect.GetSize();
291 long nDeltaX = aBmpSiz.Width() / 15;
292 long nDeltaY = aBmpSiz.Height() / 15;
293 aBmpPnt.X() += nDeltaX;
294 aBmpPnt.Y() += nDeltaY;
295 aBmpSiz.Width() -= nDeltaX * 2;
296 aBmpSiz.Height() -= nDeltaY * 2;
297 DrawBitmap( aBmpPnt, aBmpSiz, pImpl->aBitmap );
299 else
300 DrawRect( aRect );
302 if(pImpl->bFrameDirection && !bTable)
304 Point aPos;
305 Font aFont(GetFont());
306 const Size aSaveSize = aFont.GetSize();
307 Size aDrawSize( 0, aRect.GetHeight() / 6);
308 aFont.SetSize(aDrawSize);
309 SetFont(aFont);
310 String sText(OUString("ABC"));
311 Point aMove(1, GetTextHeight());
312 sal_Unicode cArrow = 0x2193;
313 long nAWidth = GetTextWidth(OUString(sText.GetChar(0)));
314 switch(pImpl->nFrameDirection)
316 case FRMDIR_HORI_LEFT_TOP:
317 aPos = aRect.TopLeft();
318 aPos.X() += PixelToLogic(Point(1,1)).X();
319 aMove.Y() = 0;
320 cArrow = 0x2192;
321 break;
322 case FRMDIR_HORI_RIGHT_TOP:
323 aPos = aRect.TopRight();
324 aPos.X() -= nAWidth;
325 aMove.Y() = 0;
326 aMove.X() *= -1;
327 cArrow = 0x2190;
328 break;
329 case FRMDIR_VERT_TOP_LEFT:
330 aPos = aRect.TopLeft();
331 aPos.X() += PixelToLogic(Point(1,1)).X();
332 aMove.X() = 0;
333 break;
334 case FRMDIR_VERT_TOP_RIGHT:
335 aPos = aRect.TopRight();
336 aPos.X() -= nAWidth;
337 aMove.X() = 0;
338 break;
340 sText.Append(cArrow);
341 for(sal_uInt16 i = 0; i < sText.Len(); i++)
343 OUString sDraw(sText.GetChar(i));
344 long nHDiff = 0;
345 long nCharWidth = GetTextWidth(sDraw);
346 bool bHorizontal = 0 == aMove.Y();
347 if(!bHorizontal)
349 nHDiff = (nAWidth - nCharWidth)/2;
350 aPos.X() += nHDiff;
352 DrawText(aPos, sDraw);
353 if(bHorizontal)
355 aPos.X() += aMove.X() < 0 ? - nCharWidth : nCharWidth;
357 else
359 aPos.X() -= nHDiff;
360 aPos.Y() += aMove.Y();
363 aFont.SetSize(aSaveSize);
364 SetFont(aFont);
367 if ( bTable )
369 // Paint Table, if necessary center it
370 SetLineColor( Color(COL_LIGHTGRAY) );
372 long nW = aRect.GetWidth(), nH = aRect.GetHeight();
373 long nTW = CELL_WIDTH * 3, nTH = CELL_HEIGHT * 3;
374 long _nLeft = bHorz ? aRect.Left() + ((nW - nTW) / 2) : aRect.Left();
375 long _nTop = bVert ? aRect.Top() + ((nH - nTH) / 2) : aRect.Top();
376 Rectangle aCellRect( Point( _nLeft, _nTop ), Size( CELL_WIDTH, CELL_HEIGHT ) );
378 for ( sal_uInt16 i = 0; i < 3; ++i )
380 aCellRect.Left() = _nLeft;
381 aCellRect.Right() = _nLeft + CELL_WIDTH;
382 if ( i > 0 )
383 aCellRect.Move( 0, CELL_HEIGHT );
385 for ( sal_uInt16 j = 0; j < 3; ++j )
387 if ( j > 0 )
388 aCellRect.Move( CELL_WIDTH, 0 );
389 DrawRect( aCellRect );
395 // -----------------------------------------------------------------------
397 void SvxPageWindow::SetBorder( const SvxBoxItem& rNew )
399 delete pImpl->pBorder;
400 pImpl->pBorder = new SvxBoxItem( rNew );
403 // -----------------------------------------------------------------------
405 void SvxPageWindow::SetBitmap( Bitmap* pBmp )
407 if ( pBmp )
409 pImpl->aBitmap = *pBmp;
410 pImpl->bBitmap = true;
412 else
413 pImpl->bBitmap = false;
416 // -----------------------------------------------------------------------
418 void SvxPageWindow::SetHdBorder( const SvxBoxItem& rNew )
420 delete pHdBorder;
421 pHdBorder = new SvxBoxItem( rNew );
423 // -----------------------------------------------------------------------
425 void SvxPageWindow::SetFtBorder( const SvxBoxItem& rNew )
427 delete pFtBorder;
428 pFtBorder = new SvxBoxItem( rNew );
431 void SvxPageWindow::EnableFrameDirection(sal_Bool bEnable)
433 pImpl->EnableFrameDirection(bEnable);
436 void SvxPageWindow::SetFrameDirection(sal_Int32 nFrameDirection)
438 pImpl->SetFrameDirection(nFrameDirection);
441 void SvxPageWindow::ResetBackground()
443 pImpl->bResetBackground = true;
446 Size SvxPageWindow::GetOptimalSize() const
448 return LogicToPixel(Size(75, 46), MapMode(MAP_APPFONT));
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */