Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / dialog / pagectrl.cxx
blobc0f76c609ec9aab7c79f1dcc7a419929ff2869b3
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 <vcl/builder.hxx>
22 #include <vcl/settings.hxx>
23 #include <editeng/frmdiritem.hxx>
24 #include <svx/pageitem.hxx>
25 #include <svx/pagectrl.hxx>
26 #include <editeng/boxitem.hxx>
28 #include <algorithm>
30 // struct PageWindow_Impl ------------------------------------------------
32 struct PageWindow_Impl
34 SvxBoxItem* pBorder;
35 Bitmap aBitmap;
36 bool bBitmap;
37 bool bResetBackground;
38 bool bFrameDirection;
39 sal_Int32 nFrameDirection;
42 PageWindow_Impl() :
43 pBorder(0),
44 bBitmap(false),
45 bResetBackground(false),
46 bFrameDirection(false),
47 nFrameDirection(0) {}
49 void EnableFrameDirection(bool bEnable){bFrameDirection = bEnable;}
50 void SetFrameDirection(sal_Int32 nDirection){nFrameDirection = nDirection;}
54 // STATIC DATA -----------------------------------------------------------
56 #define CELL_WIDTH 1600L
57 #define CELL_HEIGHT 800L
59 // class SvxPageWindow ---------------------------------------------------
61 SvxPageWindow::SvxPageWindow( Window* pParent ) :
63 Window( pParent ),
65 nTop ( 0 ),
66 nBottom ( 0 ),
67 nLeft ( 0 ),
68 nRight ( 0 ),
69 aColor ( COL_TRANSPARENT ),
70 nHdLeft ( 0 ),
71 nHdRight ( 0 ),
72 nHdDist ( 0 ),
73 nHdHeight ( 0 ),
74 aHdColor ( COL_TRANSPARENT ),
75 pHdBorder ( 0 ),
76 nFtLeft ( 0 ),
77 nFtRight ( 0 ),
78 nFtDist ( 0 ),
79 nFtHeight ( 0 ),
80 aFtColor ( COL_TRANSPARENT ),
81 pFtBorder ( 0 ),
82 bFooter ( false ),
83 bHeader ( false ),
84 bTable ( false ),
85 bHorz ( false ),
86 bVert ( false ),
87 eUsage ( SVX_PAGE_ALL )
89 pImpl = new PageWindow_Impl;
91 // Count in Twips by default
92 SetMapMode( MapMode( MAP_TWIP ) );
93 aWinSize = GetOptimalSize();
94 aWinSize.Height() -= 4;
95 aWinSize.Width() -= 4;
97 aWinSize = PixelToLogic( aWinSize );
98 SetBackground();
103 SvxPageWindow::~SvxPageWindow()
105 delete pImpl;
106 delete pHdBorder;
107 delete pFtBorder;
110 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxPageWindow(Window *pParent, VclBuilder::stringmap &)
112 return new SvxPageWindow(pParent);
118 void SvxPageWindow::Paint( const Rectangle& )
120 Fraction aXScale( aWinSize.Width(), std::max( (long) (aSize.Width() * 2 + aSize.Width() / 8), 1L ) );
121 Fraction aYScale( aWinSize.Height(), std::max( aSize.Height(), 1L ) );
122 MapMode aMapMode( GetMapMode() );
124 if ( aYScale < aXScale )
126 aMapMode.SetScaleX( aYScale );
127 aMapMode.SetScaleY( aYScale );
129 else
131 aMapMode.SetScaleX( aXScale );
132 aMapMode.SetScaleY( aXScale );
134 SetMapMode( aMapMode );
135 Size aSz( PixelToLogic( GetSizePixel() ) );
136 long nYPos = ( aSz.Height() - aSize.Height() ) / 2;
138 if ( eUsage == SVX_PAGE_ALL )
140 // all pages are equal -> draw one page
141 if ( aSize.Width() > aSize.Height() )
143 // Draw Landscape page of the same size
144 Fraction aX = aMapMode.GetScaleX();
145 Fraction aY = aMapMode.GetScaleY();
146 Fraction a2( 1.5 );
147 aX *= a2;
148 aY *= a2;
149 aMapMode.SetScaleX( aX );
150 aMapMode.SetScaleY( aY );
151 SetMapMode( aMapMode );
152 aSz = PixelToLogic( GetSizePixel() );
153 nYPos = ( aSz.Height() - aSize.Height() ) / 2;
154 long nXPos = ( aSz.Width() - aSize.Width() ) / 2;
155 DrawPage( Point( nXPos, nYPos ), true, true );
157 else
158 // Portrait
159 DrawPage( Point( ( aSz.Width() - aSize.Width() ) / 2, nYPos ), true, true );
161 else
163 // Left and right page are different -> draw two pages if possible
164 DrawPage( Point( 0, nYPos ), false, eUsage & SVX_PAGE_LEFT );
165 DrawPage( Point( aSize.Width() + aSize.Width() / 8, nYPos ), true,
166 eUsage & SVX_PAGE_RIGHT );
171 void SvxPageWindow::DrawPage( const Point& rOrg, const bool bSecond, const bool bEnabled )
173 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
174 const Color& rFieldColor = rStyleSettings.GetFieldColor();
175 const Color& rFieldTextColor = rStyleSettings.GetFieldTextColor();
176 const Color& rDisableColor = rStyleSettings.GetDisableColor();
177 const Color& rDlgColor = rStyleSettings.GetDialogColor();
179 // background
180 if(!bSecond || pImpl->bResetBackground)
182 SetLineColor( Color(COL_TRANSPARENT) );
183 SetFillColor( rDlgColor );
184 Size winSize(GetOutputSize());
185 DrawRect( Rectangle( Point(0,0), winSize ) );
187 if ( pImpl->bResetBackground )
188 pImpl->bResetBackground = false;
190 SetLineColor( rFieldTextColor );
191 // Shadow
192 Size aTempSize = aSize;
193 // Page
194 if ( !bEnabled )
196 SetFillColor( rDisableColor );
197 DrawRect( Rectangle( rOrg, aTempSize ) );
198 return;
200 SetFillColor( rFieldColor );
201 DrawRect( Rectangle( rOrg, aTempSize ) );
203 long nL = nLeft;
204 long nR = nRight;
206 if ( eUsage == SVX_PAGE_MIRROR && !bSecond )
208 // turn for mirrored
209 nL = nRight;
210 nR = nLeft;
213 Rectangle aRect;
215 aRect.Left() = rOrg.X() + nL;
216 aRect.Right() = rOrg.X() + aTempSize.Width() - nR;
217 aRect.Top() = rOrg.Y() + nTop;
218 aRect.Bottom()= rOrg.Y() + aTempSize.Height() - nBottom;
220 Rectangle aHdRect( aRect );
221 Rectangle aFtRect( aRect );
223 if ( bHeader )
225 // show headers if possible
226 aHdRect.Left() += nHdLeft;
227 aHdRect.Right() -= nHdRight;
228 aHdRect.Bottom() = aRect.Top() + nHdHeight;
229 aRect.Top() += nHdHeight + nHdDist;
230 SetFillColor( aHdColor );
231 DrawRect( aHdRect );
234 if ( bFooter )
236 // show footer if possible
237 aFtRect.Left() += nFtLeft;
238 aFtRect.Right() -= nFtRight;
239 aFtRect.Top() = aRect.Bottom() - nFtHeight;
240 aRect.Bottom() -= nFtHeight + nFtDist;
241 SetFillColor( aFtColor );
242 DrawRect( aFtRect );
245 // Paint Body
246 SetFillColor( aColor );
247 if ( pImpl->bBitmap )
249 DrawRect( aRect );
250 Point aBmpPnt = aRect.TopLeft();
251 Size aBmpSiz = aRect.GetSize();
252 long nDeltaX = aBmpSiz.Width() / 15;
253 long nDeltaY = aBmpSiz.Height() / 15;
254 aBmpPnt.X() += nDeltaX;
255 aBmpPnt.Y() += nDeltaY;
256 aBmpSiz.Width() -= nDeltaX * 2;
257 aBmpSiz.Height() -= nDeltaY * 2;
258 DrawBitmap( aBmpPnt, aBmpSiz, pImpl->aBitmap );
260 else
261 DrawRect( aRect );
263 if(pImpl->bFrameDirection && !bTable)
265 Point aPos;
266 Font aFont(GetFont());
267 const Size aSaveSize = aFont.GetSize();
268 Size aDrawSize( 0, aRect.GetHeight() / 6);
269 aFont.SetSize(aDrawSize);
270 SetFont(aFont);
271 OUString sText("ABC");
272 Point aMove(1, GetTextHeight());
273 sal_Unicode cArrow = 0x2193;
274 long nAWidth = GetTextWidth(sText.copy(0,1));
275 switch(pImpl->nFrameDirection)
277 case FRMDIR_HORI_LEFT_TOP:
278 aPos = aRect.TopLeft();
279 aPos.X() += PixelToLogic(Point(1,1)).X();
280 aMove.Y() = 0;
281 cArrow = 0x2192;
282 break;
283 case FRMDIR_HORI_RIGHT_TOP:
284 aPos = aRect.TopRight();
285 aPos.X() -= nAWidth;
286 aMove.Y() = 0;
287 aMove.X() *= -1;
288 cArrow = 0x2190;
289 break;
290 case FRMDIR_VERT_TOP_LEFT:
291 aPos = aRect.TopLeft();
292 aPos.X() += PixelToLogic(Point(1,1)).X();
293 aMove.X() = 0;
294 break;
295 case FRMDIR_VERT_TOP_RIGHT:
296 aPos = aRect.TopRight();
297 aPos.X() -= nAWidth;
298 aMove.X() = 0;
299 break;
301 sText += OUString(cArrow);
302 for(sal_uInt16 i = 0; i < sText.getLength(); i++)
304 OUString sDraw(sText.copy(i,1));
305 long nHDiff = 0;
306 long nCharWidth = GetTextWidth(sDraw);
307 bool bHorizontal = 0 == aMove.Y();
308 if(!bHorizontal)
310 nHDiff = (nAWidth - nCharWidth)/2;
311 aPos.X() += nHDiff;
313 DrawText(aPos, sDraw);
314 if(bHorizontal)
316 aPos.X() += aMove.X() < 0 ? - nCharWidth : nCharWidth;
318 else
320 aPos.X() -= nHDiff;
321 aPos.Y() += aMove.Y();
324 aFont.SetSize(aSaveSize);
325 SetFont(aFont);
328 if ( bTable )
330 // Paint Table, if necessary center it
331 SetLineColor( Color(COL_LIGHTGRAY) );
333 long nW = aRect.GetWidth(), nH = aRect.GetHeight();
334 long nTW = CELL_WIDTH * 3, nTH = CELL_HEIGHT * 3;
335 long _nLeft = bHorz ? aRect.Left() + ((nW - nTW) / 2) : aRect.Left();
336 long _nTop = bVert ? aRect.Top() + ((nH - nTH) / 2) : aRect.Top();
337 Rectangle aCellRect( Point( _nLeft, _nTop ), Size( CELL_WIDTH, CELL_HEIGHT ) );
339 for ( sal_uInt16 i = 0; i < 3; ++i )
341 aCellRect.Left() = _nLeft;
342 aCellRect.Right() = _nLeft + CELL_WIDTH;
343 if ( i > 0 )
344 aCellRect.Move( 0, CELL_HEIGHT );
346 for ( sal_uInt16 j = 0; j < 3; ++j )
348 if ( j > 0 )
349 aCellRect.Move( CELL_WIDTH, 0 );
350 DrawRect( aCellRect );
358 void SvxPageWindow::SetBorder( const SvxBoxItem& rNew )
360 delete pImpl->pBorder;
361 pImpl->pBorder = new SvxBoxItem( rNew );
366 void SvxPageWindow::SetBitmap( Bitmap* pBmp )
368 if ( pBmp )
370 pImpl->aBitmap = *pBmp;
371 pImpl->bBitmap = true;
373 else
374 pImpl->bBitmap = false;
379 void SvxPageWindow::SetHdBorder( const SvxBoxItem& rNew )
381 delete pHdBorder;
382 pHdBorder = new SvxBoxItem( rNew );
386 void SvxPageWindow::SetFtBorder( const SvxBoxItem& rNew )
388 delete pFtBorder;
389 pFtBorder = new SvxBoxItem( rNew );
392 void SvxPageWindow::EnableFrameDirection(bool bEnable)
394 pImpl->EnableFrameDirection(bEnable);
397 void SvxPageWindow::SetFrameDirection(sal_Int32 nFrameDirection)
399 pImpl->SetFrameDirection(nFrameDirection);
402 void SvxPageWindow::ResetBackground()
404 pImpl->bResetBackground = true;
407 Size SvxPageWindow::GetOptimalSize() const
409 return LogicToPixel(Size(75, 46), MapMode(MAP_APPFONT));
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */