summaryrefslogtreecommitdiff
path: root/sw/source/ui/docvw/PageBreakWin.cxx
blob: b9ca0a3a9cc7775192a0cc69caccb4dfa5319433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * [ Copyright (C) 2011 SUSE <cbosdonnat@suse.com> (initial developer) ]
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */
#include <popup.hrc>
#include <utlui.hrc>

#include <DashedLine.hxx>
#include <edtwin.hxx>
#include <PageBreakWin.hxx>
#include <pagefrm.hxx>
#include <viewopt.hxx>

#include <basegfx/color/bcolortools.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/range/b2drectangle.hxx>
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
#include <svx/sdr/contact/objectcontacttools.hxx>
#include <vcl/decoview.hxx>
#include <vcl/svapp.hxx>

#define BUTTON_SIZE 30
#define ARROW_WIDTH 20

using namespace basegfx;
using namespace basegfx::tools;
using namespace drawinglayer::primitive2d;

namespace
{
    B2DPolygon lcl_CreatePolygon( B2DRectangle aBounds )
    {
        B2DPolygon aRetval;
        const double nRadius = 4;
        const double nKappa((M_SQRT2 - 1.0) * 4.0 / 3.0);

        // Create the top left corner
        {
            B2DPoint aTLCorner = aBounds.getMinimum();
            B2DPoint aStart( 0.0, nRadius );
            B2DPoint aEnd( nRadius, 0.0 );
            aRetval.append( aStart );
            aRetval.appendBezierSegment(
                    interpolate( aStart, aTLCorner, nKappa ),
                    interpolate( aEnd, aTLCorner, nKappa ),
                    aEnd );
        }

        // Create the top right angle
        {
            B2DPoint aTMCorner( aBounds.getWidth() - ARROW_WIDTH, 0.0 );
            B2DPoint aStart = aTMCorner + B2DVector( - nRadius, 0.0 );
            B2DVector aEndVect( double( ARROW_WIDTH ), aBounds.getHeight() / 2.0 );
            aEndVect.setLength( nRadius );
            B2DPoint aEnd = aTMCorner + aEndVect;
            aRetval.append( aStart );
            aRetval.appendBezierSegment(
                    interpolate( aStart, aTMCorner, nKappa ),
                    interpolate( aEnd, aTMCorner, nKappa ),
                    aEnd );
        }

        // Create the right corner
        {
            B2DPoint aMRCorner( aBounds.getWidth(), aBounds.getHeight() / 2.0 );
            B2DVector aStartVect( double( - ARROW_WIDTH ), - aBounds.getHeight() / 2.0 );
            aStartVect.setLength( nRadius );
            B2DPoint aStart = aMRCorner + aStartVect;
            B2DVector aEndVect( double( - ARROW_WIDTH ), aBounds.getHeight() / 2.0 );
            aEndVect.setLength( nRadius );
            B2DPoint aEnd = aMRCorner + aEndVect;
            aRetval.append( aStart );
            aRetval.appendBezierSegment(
                    interpolate( aStart, aMRCorner, nKappa ),
                    interpolate( aEnd, aMRCorner, nKappa ),
                    aEnd );
        }

        // Create the bottom right angle
        {
            B2DPoint aBMCorner( aBounds.getWidth() - ARROW_WIDTH, aBounds.getHeight() );
            B2DVector aStartVect( double( ARROW_WIDTH ), - aBounds.getHeight() / 2.0 );
            aStartVect.setLength( nRadius );
            B2DPoint aStart = aBMCorner + aStartVect;
            B2DPoint aEnd = aBMCorner + B2DVector( - nRadius, 0.0 );
            aRetval.append( aStart );
            aRetval.appendBezierSegment(
                    interpolate( aStart, aBMCorner, nKappa ),
                    interpolate( aEnd, aBMCorner, nKappa ),
                    aEnd );
        }

        // Create the bottom left corner
        {
            B2DPoint aBLCorner( aBounds.getMinX(), aBounds.getHeight() );
            B2DPoint aStart( nRadius, aBounds.getHeight() );
            B2DPoint aEnd( 0.0, aBounds.getHeight() - nRadius );
            aRetval.append( aStart );
            aRetval.appendBezierSegment(
                    interpolate( aStart, aBLCorner, nKappa ),
                    interpolate( aEnd, aBLCorner, nKappa ),
                    aEnd );
        }

        aRetval.setClosed( true );
        return aRetval;
    }
}

SwPageBreakWin::SwPageBreakWin( SwEditWin* pEditWin, const SwPageFrm* pPageFrm ) :
    MenuButton( pEditWin, WB_DIALOGCONTROL ),
    SwFrameControl( pEditWin, pPageFrm ),
    m_pPopupMenu( NULL ),
    m_pLine( NULL )
{
    // Use pixels for the rest of the drawing
    SetMapMode( MapMode ( MAP_PIXEL ) );

    // Create the line control
    BColor aColor = SwViewOption::GetPageBreakColor().getBColor();
    m_pLine = new SwDashedLine( GetEditWin(), aColor );

    // Create the popup menu
    m_pPopupMenu = new PopupMenu( SW_RES( MN_PAGEBREAK_BUTTON ) );
    SetPopupMenu( m_pPopupMenu );
}

SwPageBreakWin::~SwPageBreakWin( )
{
    delete m_pPopupMenu;
    delete m_pLine;
}

void SwPageBreakWin::Paint( const Rectangle& )
{
    const Rectangle aRect( Rectangle( Point( 0, 0 ), PixelToLogic( GetSizePixel() ) ) );

    // Properly paint the control
    BColor aColor = SwViewOption::GetPageBreakColor().getBColor();

    BColor aHslLine = rgb2hsl( aColor );
    double nLuminance = aHslLine.getZ();
    nLuminance += ( 1.0 - nLuminance ) * 0.75;
    if ( aHslLine.getZ() > 0.7 )
        nLuminance = aHslLine.getZ() * 0.7;
    aHslLine.setZ( nLuminance );
    BColor aOtherColor = hsl2rgb( aHslLine );

    const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    if ( rSettings.GetHighContrastMode( ) )
    {
        aColor = rSettings.GetDialogTextColor().getBColor();
        aOtherColor = rSettings.GetDialogColor( ).getBColor();
    }

    Primitive2DSequence aSeq( 2 );
    B2DRectangle aBRect( double( aRect.Left() ), double( aRect.Top( ) ),
           double( aRect.Right() ), double( aRect.Bottom( ) ) );
    B2DPolygon aPolygon = lcl_CreatePolygon( aBRect );

    // Create the polygon primitives
    aSeq[0] = Primitive2DReference( new PolyPolygonColorPrimitive2D(
            B2DPolyPolygon( aPolygon ), aOtherColor ) );
    aSeq[1] = Primitive2DReference( new PolygonHairlinePrimitive2D(
            aPolygon, aColor ) );

    // Create the processor and process the primitives
    const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
    drawinglayer::processor2d::BaseProcessor2D * pProcessor =
        sdr::contact::createBaseProcessor2DFromOutputDevice(
                    *this, aNewViewInfos );

    pProcessor->process( aSeq );

    // Paint the picture
    Image aImg( SW_RES( IMG_PAGE_BREAK ) );
    DrawImage( Point( 3, 3 ), aImg );

    // Paint the symbol if not readonly button
    if ( IsEnabled() )
    {
        Point aPicPos( aRect.getWidth() - ARROW_WIDTH, 0 );
        Size aPicSize( ARROW_WIDTH, aRect.getHeight() );
        Rectangle aSymbolRect( aPicPos, aPicSize );

        // 10% distance to the left
        const long nBorderDistanceLeft = ((aSymbolRect.GetWidth()*100)+500)/1000;
        aSymbolRect.Left()+=nBorderDistanceLeft;
        // 40% distance to the right
        const long nBorderDistanceRight = ((aSymbolRect.GetWidth()*400)+500)/1000;
        aSymbolRect.Right()-=nBorderDistanceRight;
        // 30% distance to the top button border
        const long nBorderDistanceTop = ((aSymbolRect.GetHeight()*300)+500)/1000;
        aSymbolRect.Top()+=nBorderDistanceTop;
        // 25% distance to the bottom button border
        const long nBorderDistanceBottom = ((aSymbolRect.GetHeight()*250)+500)/1000;
        aSymbolRect.Bottom()-=nBorderDistanceBottom;

        SymbolType nSymbol = SYMBOL_SPIN_DOWN;
        DecorationView aDecoView( this );
        aDecoView.DrawSymbol( aSymbolRect, nSymbol,
                              ( Application::GetSettings().GetStyleSettings().GetHighContrastMode()
                                ? Color( COL_WHITE )
                                : Color( COL_BLACK ) ) );
    }
}

void SwPageBreakWin::Select( )
{
    // TODO Menu item selected...
}

void SwPageBreakWin::UpdatePosition( )
{
    const SwPageFrm* pPrevPage = static_cast< const SwPageFrm* >( GetPageFrame()->GetPrev() );
    Rectangle aPrevFrmRect = GetEditWin()->LogicToPixel( pPrevPage->Frm().SVRect() );
    Rectangle aBoundRect = GetEditWin()->LogicToPixel( GetPageFrame()->GetBoundRect().SVRect() );
    Rectangle aFrmRect = GetEditWin()->LogicToPixel( GetPageFrame()->Frm().SVRect() );

    long nYLineOffset = ( aPrevFrmRect.Bottom() + aFrmRect.Top() ) / 2;
    if ( aFrmRect.Top() == aPrevFrmRect.Top() )
        nYLineOffset = ( aBoundRect.Top() + aFrmRect.Top() ) / 2;

    Size aBtnSize( BUTTON_SIZE + ARROW_WIDTH, BUTTON_SIZE );
    Point aBtnPos( aFrmRect.Left() - aBtnSize.Width() + ARROW_WIDTH / 2,
            nYLineOffset - aBtnSize.Height() / 2 );

    SetPosSizePixel( aBtnPos, aBtnSize );

    // Update the line position
    Point aLinePos( aFrmRect.Left() + ARROW_WIDTH / 2, nYLineOffset );
    Size aLineSize( aBoundRect.getWidth(), 1 );
    m_pLine->SetPosSizePixel( aLinePos, aLineSize );
}

void SwPageBreakWin::ShowAll( bool bShow )
{
    Show( bShow );
    m_pLine->Show( bShow );
}

const SwPageFrm* SwPageBreakWin::GetPageFrame( )
{
    return static_cast< const SwPageFrm * >( GetFrame( ) );
}

void SwPageBreakWin::SetReadonly( bool bReadonly )
{
    Enable( !bReadonly );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */