xref: /trunk/main/svx/source/dialog/pagectrl.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 // include ---------------------------------------------------------------
32 
33 
34 #include <vcl/bitmap.hxx>
35 #include <editeng/frmdiritem.hxx>
36 #include <svx/pageitem.hxx>
37 #include <svx/pagectrl.hxx>
38 #include <editeng/boxitem.hxx>
39 
40 #include <algorithm>
41 
42 // struct PageWindow_Impl ------------------------------------------------
43 
44 struct PageWindow_Impl
45 {
46     SvxBoxItem*     pBorder;
47     Bitmap          aBitmap;
48     FASTBOOL        bBitmap;
49     sal_Bool        bResetBackground;
50     sal_Bool        bFrameDirection;
51     sal_Int32       nFrameDirection;
52 
53 
54     PageWindow_Impl() :
55         pBorder(0),
56         bBitmap(sal_False),
57         bResetBackground(sal_False),
58         bFrameDirection(sal_False),
59         nFrameDirection(0) {}
60 
61     void        EnableFrameDirection(sal_Bool bEnable){bFrameDirection = bEnable;}
62     void        SetFrameDirection(sal_Int32 nDirection){nFrameDirection = nDirection;}
63 
64 };
65 
66 // STATIC DATA -----------------------------------------------------------
67 
68 #define CELL_WIDTH      1600L
69 #define CELL_HEIGHT      800L
70 
71 // class SvxPageWindow ---------------------------------------------------
72 
73 SvxPageWindow::SvxPageWindow( Window* pParent, const ResId& rId ) :
74 
75     Window( pParent, rId ),
76 
77     nTop        ( 0 ),
78     nBottom     ( 0 ),
79     nLeft       ( 0 ),
80     nRight      ( 0 ),
81     aColor      ( COL_TRANSPARENT ),
82     nHdLeft     ( 0 ),
83     nHdRight    ( 0 ),
84     nHdDist     ( 0 ),
85     nHdHeight   ( 0 ),
86     aHdColor    ( COL_TRANSPARENT ),
87     pHdBorder   ( 0 ),
88     nFtLeft     ( 0 ),
89     nFtRight    ( 0 ),
90     nFtDist     ( 0 ),
91     nFtHeight   ( 0 ),
92     aFtColor    ( COL_TRANSPARENT ),
93     pFtBorder   ( 0 ),
94     bFooter     ( sal_False ),
95     bHeader     ( sal_False ),
96     bTable      ( sal_False ),
97     bHorz       ( sal_False ),
98     bVert       ( sal_False ),
99     eUsage      ( SVX_PAGE_ALL )
100 
101 {
102     pImpl = new PageWindow_Impl;
103 
104     // defaultmaessing in Twips rechnen
105     SetMapMode( MapMode( MAP_TWIP ) );
106     aWinSize = GetOutputSizePixel();
107     aWinSize.Height() -= 4;
108     aWinSize.Width() -= 4;
109 
110     aWinSize = PixelToLogic( aWinSize );
111     SetBackground();
112 }
113 
114 // -----------------------------------------------------------------------
115 
116 SvxPageWindow::~SvxPageWindow()
117 {
118     delete pImpl;
119     delete pHdBorder;
120     delete pFtBorder;
121 }
122 
123 // -----------------------------------------------------------------------
124 
125 void __EXPORT SvxPageWindow::Paint( const Rectangle& )
126 {
127     Fraction aXScale( aWinSize.Width(), std::max( (long) (aSize.Width() * 2 + aSize.Width() / 8), 1L ) );
128     Fraction aYScale( aWinSize.Height(), std::max( aSize.Height(), 1L ) );
129     MapMode aMapMode( GetMapMode() );
130 
131     if ( aYScale < aXScale )
132     {
133         aMapMode.SetScaleX( aYScale );
134         aMapMode.SetScaleY( aYScale );
135     }
136     else
137     {
138         aMapMode.SetScaleX( aXScale );
139         aMapMode.SetScaleY( aXScale );
140     }
141     SetMapMode( aMapMode );
142     Size aSz( PixelToLogic( GetSizePixel() ) );
143     long nYPos = ( aSz.Height() - aSize.Height() ) / 2;
144 
145     if ( eUsage == SVX_PAGE_ALL )
146     {
147         // alle Seiten gleich -> eine Seite malen
148         if ( aSize.Width() > aSize.Height() )
149         {
150             // Querformat in gleicher Gr"osse zeichnen
151             Fraction aX = aMapMode.GetScaleX();
152             Fraction aY = aMapMode.GetScaleY();
153             Fraction a2( 1.5 );
154             aX *= a2;
155             aY *= a2;
156             aMapMode.SetScaleX( aX );
157             aMapMode.SetScaleY( aY );
158             SetMapMode( aMapMode );
159             aSz = PixelToLogic( GetSizePixel() );
160             nYPos = ( aSz.Height() - aSize.Height() ) / 2;
161             long nXPos = ( aSz.Width() - aSize.Width() ) / 2;
162             DrawPage( Point( nXPos, nYPos ), sal_True, sal_True );
163         }
164         else
165             // Hochformat
166             DrawPage( Point( ( aSz.Width() - aSize.Width() ) / 2, nYPos ), sal_True, sal_True );
167     }
168     else
169     {
170         // Linke und rechte Seite unterschiedlich -> ggf. zwei Seiten malen
171         DrawPage( Point( 0, nYPos ), sal_False, (sal_Bool)( eUsage & SVX_PAGE_LEFT ) );
172         DrawPage( Point( aSize.Width() + aSize.Width() / 8, nYPos ), sal_True,
173                   (sal_Bool)( eUsage & SVX_PAGE_RIGHT ) );
174     }
175 }
176 
177 // -----------------------------------------------------------------------
178 void SvxPageWindow::DrawPage( const Point& rOrg, const sal_Bool bSecond, const sal_Bool bEnabled )
179 {
180     const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
181     const Color& rFieldColor = rStyleSettings.GetFieldColor();
182     const Color& rFieldTextColor = rStyleSettings.GetFieldTextColor();
183     const Color& rDisableColor = rStyleSettings.GetDisableColor();
184     const Color& rDlgColor = rStyleSettings.GetDialogColor();
185 
186     // background
187     if(!bSecond || pImpl->bResetBackground)
188     {
189         SetLineColor( Color(COL_TRANSPARENT) );
190         SetFillColor( rDlgColor );
191         Size winSize(GetOutputSize());
192         DrawRect( Rectangle( Point(0,0), winSize ) );
193 
194         if ( pImpl->bResetBackground )
195             pImpl->bResetBackground = sal_False;
196     }
197     SetLineColor( rFieldTextColor );
198     // Schatten
199     Size aTempSize = aSize;
200     // Seite
201     if ( !bEnabled )
202     {
203         SetFillColor( rDisableColor );
204         DrawRect( Rectangle( rOrg, aTempSize ) );
205         return;
206     }
207     SetFillColor( rFieldColor );
208     DrawRect( Rectangle( rOrg, aTempSize ) );
209 
210     // Border Top Bottom Left Right
211     Point aBegin( rOrg );
212     Point aEnd( rOrg );
213 
214     long nL = nLeft;
215     long nR = nRight;
216 
217     if ( eUsage == SVX_PAGE_MIRROR && !bSecond )
218     {
219         // f"ur gespiegelt drehen
220         nL = nRight;
221         nR = nLeft;
222     }
223 
224     Rectangle aRect;
225 
226     aRect.Left()  = rOrg.X() + nL;
227     aRect.Right() = rOrg.X() + aTempSize.Width() - nR;
228     aRect.Top()   = rOrg.Y() + nTop;
229     aRect.Bottom()= rOrg.Y() + aTempSize.Height() - nBottom;
230 
231     Rectangle aHdRect( aRect );
232     Rectangle aFtRect( aRect );
233 
234     if ( bHeader )
235     {
236         // ggf. Header anzeigen
237         aHdRect.Left() += nHdLeft;
238         aHdRect.Right() -= nHdRight;
239         aHdRect.Bottom() = aRect.Top() + nHdHeight;
240         aRect.Top() += nHdHeight + nHdDist;
241         SetFillColor( aHdColor );
242         DrawRect( aHdRect );
243     }
244 
245     if ( bFooter )
246     {
247         // ggf. Footer anzeigen
248         aFtRect.Left() += nFtLeft;
249         aFtRect.Right() -= nFtRight;
250         aFtRect.Top() = aRect.Bottom() - nFtHeight;
251         aRect.Bottom() -= nFtHeight + nFtDist;
252         SetFillColor( aFtColor );
253         DrawRect( aFtRect );
254     }
255 
256     // Body malen
257     SetFillColor( aColor );
258     if ( pImpl->bBitmap )
259     {
260         DrawRect( aRect );
261         Point aBmpPnt = aRect.TopLeft();
262         Size aBmpSiz = aRect.GetSize();
263         long nDeltaX = aBmpSiz.Width() / 15;
264         long nDeltaY = aBmpSiz.Height() / 15;
265         aBmpPnt.X() += nDeltaX;
266         aBmpPnt.Y() += nDeltaY;
267         aBmpSiz.Width() -= nDeltaX * 2;
268         aBmpSiz.Height() -= nDeltaY * 2;
269         DrawBitmap( aBmpPnt, aBmpSiz, pImpl->aBitmap );
270     }
271     else
272         DrawRect( aRect );
273 
274     if(pImpl->bFrameDirection && !bTable)
275     {
276        //pImpl->nFrameDirection
277         Point aPos;
278         Font aFont(GetFont());
279         const Size aSaveSize = aFont.GetSize();
280         Size aDrawSize( 0, aRect.GetHeight() / 6);
281         aFont.SetSize(aDrawSize);
282         SetFont(aFont);
283         String sText(String::CreateFromAscii("ABC"));
284         Point aMove(1, GetTextHeight());
285         sal_Unicode cArrow = 0x2193;
286         long nAWidth = GetTextWidth(String(sText.GetChar(0)));
287         switch(pImpl->nFrameDirection)
288         {
289             case FRMDIR_HORI_LEFT_TOP:
290                 aPos = aRect.TopLeft();
291                 aPos.X() += PixelToLogic(Point(1,1)).X();
292                 aMove.Y() = 0;
293                 cArrow = 0x2192;
294             break;
295             case FRMDIR_HORI_RIGHT_TOP:
296                 aPos = aRect.TopRight();
297                 aPos.X() -= nAWidth;
298                 aMove.Y() = 0;
299                 aMove.X() *= -1;
300                 cArrow = 0x2190;
301             break;
302             case FRMDIR_VERT_TOP_LEFT:
303                 aPos = aRect.TopLeft();
304                 aPos.X() += PixelToLogic(Point(1,1)).X();
305                 aMove.X() = 0;
306             break;
307             case FRMDIR_VERT_TOP_RIGHT:
308                 aPos = aRect.TopRight();
309                 aPos.X() -= nAWidth;
310                 aMove.X() = 0;
311             break;
312         }
313         sText.Append(cArrow);
314         for(sal_uInt16 i = 0; i < sText.Len(); i++)
315         {
316             String sDraw(sText.GetChar(i));
317             long nHDiff = 0;
318             long nCharWidth = GetTextWidth(sDraw);
319             sal_Bool bHorizontal = 0 == aMove.Y();
320             if(!bHorizontal)
321             {
322                 nHDiff = (nAWidth - nCharWidth)/2;
323                 aPos.X() += nHDiff;
324             }
325             DrawText(aPos, sDraw);
326             if(bHorizontal)
327             {
328                 aPos.X() += aMove.X() < 0 ? - nCharWidth : nCharWidth;
329             }
330             else
331             {
332                 aPos.X() -= nHDiff;
333                 aPos.Y() += aMove.Y();
334             }
335         }
336         aFont.SetSize(aSaveSize);
337         SetFont(aFont);
338 
339     }
340     if ( bTable )
341     {
342         // Tabelle malen, ggf. zentrieren
343         SetLineColor( Color(COL_LIGHTGRAY) );
344 
345         long nW = aRect.GetWidth(), nH = aRect.GetHeight();
346         long nTW = CELL_WIDTH * 3, nTH = CELL_HEIGHT * 3;
347         long _nLeft = bHorz ? aRect.Left() + ((nW - nTW) / 2) : aRect.Left();
348         long _nTop = bVert ? aRect.Top() + ((nH - nTH) / 2) : aRect.Top();
349         Rectangle aCellRect( Point( _nLeft, _nTop ), Size( CELL_WIDTH, CELL_HEIGHT ) );
350 
351         for ( sal_uInt16 i = 0; i < 3; ++i )
352         {
353             aCellRect.Left() = _nLeft;
354             aCellRect.Right() = _nLeft + CELL_WIDTH;
355             if ( i > 0 )
356                 aCellRect.Move( 0, CELL_HEIGHT );
357 
358             for ( sal_uInt16 j = 0; j < 3; ++j )
359             {
360                 if ( j > 0 )
361                     aCellRect.Move( CELL_WIDTH, 0 );
362                 DrawRect( aCellRect );
363             }
364         }
365     }
366 }
367 
368 // -----------------------------------------------------------------------
369 
370 void SvxPageWindow::SetBorder( const SvxBoxItem& rNew )
371 {
372     delete pImpl->pBorder;
373     pImpl->pBorder = new SvxBoxItem( rNew );
374 }
375 
376 // -----------------------------------------------------------------------
377 
378 void SvxPageWindow::SetBitmap( Bitmap* pBmp )
379 {
380     if ( pBmp )
381     {
382         pImpl->aBitmap = *pBmp;
383         pImpl->bBitmap = sal_True;
384     }
385     else
386         pImpl->bBitmap = sal_False;
387 }
388 
389 // -----------------------------------------------------------------------
390 
391 void SvxPageWindow::SetHdBorder( const SvxBoxItem& rNew )
392 {
393     delete pHdBorder;
394     pHdBorder = new SvxBoxItem( rNew );
395 }
396 // -----------------------------------------------------------------------
397 
398 void SvxPageWindow::SetFtBorder( const SvxBoxItem& rNew )
399 {
400     delete pFtBorder;
401     pFtBorder = new SvxBoxItem( rNew );
402 }
403 /* -----------------------------13.06.2002 16:16------------------------------
404 
405  ---------------------------------------------------------------------------*/
406 void  SvxPageWindow::EnableFrameDirection(sal_Bool bEnable)
407 {
408     pImpl->EnableFrameDirection(bEnable);
409 }
410 /* -----------------------------13.06.2002 16:16------------------------------
411 
412  ---------------------------------------------------------------------------*/
413 void  SvxPageWindow::SetFrameDirection(sal_Int32 nFrameDirection)
414 {
415     pImpl->SetFrameDirection(nFrameDirection);
416 }
417 
418 void SvxPageWindow::ResetBackground()
419 {
420     pImpl->bResetBackground = sal_True;
421 }
422