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 #include <svx/zoomsliderctrl.hxx>
33 #ifndef _STATUS_HXX //autogen
34 #include <vcl/status.hxx>
35 #endif
36 #ifndef _MENU_HXX //autogen
37 #include <vcl/menu.hxx>
38 #endif
39 #include <vcl/image.hxx>
40 #include <svx/zoomslideritem.hxx>
41 #include <svx/dialmgr.hxx>
42 #include <svx/dialogs.hrc>
43 
44 #include <set>
45 
46 // -----------------------------------------------------------------------
47 
48 SFX_IMPL_STATUSBAR_CONTROL( SvxZoomSliderControl, SvxZoomSliderItem );
49 
50 // -----------------------------------------------------------------------
51 
52 struct SvxZoomSliderControl::SvxZoomSliderControl_Impl
53 {
54     sal_uInt16                   mnCurrentZoom;
55     sal_uInt16                   mnMinZoom;
56     sal_uInt16                   mnMaxZoom;
57     sal_uInt16                   mnSliderCenter;
58     std::vector< long >      maSnappingPointOffsets;
59     std::vector< sal_uInt16 >    maSnappingPointZooms;
60     Image                    maSliderButton;
61     Image                    maIncreaseButton;
62     Image                    maDecreaseButton;
63     bool                     mbValuesSet;
64     bool                     mbOmitPaint;
65 
66     SvxZoomSliderControl_Impl() :
67         mnCurrentZoom( 0 ),
68         mnMinZoom( 0 ),
69         mnMaxZoom( 0 ),
70         mnSliderCenter( 0 ),
71         maSnappingPointOffsets(),
72         maSnappingPointZooms(),
73         maSliderButton(),
74         maIncreaseButton(),
75         maDecreaseButton(),
76         mbValuesSet( false ),
77         mbOmitPaint( false ) {}
78 };
79 
80 // -----------------------------------------------------------------------
81 
82 const long nButtonWidth   = 10;
83 const long nButtonHeight  = 10;
84 const long nIncDecWidth   = 11;
85 const long nIncDecHeight  = 11;
86 const long nSliderHeight  = 2;
87 const long nSnappingHeight = 4;
88 const long nSliderXOffset = 20;
89 const long nSnappingEpsilon = 5; // snapping epsilon in pixels
90 const long nSnappingPointsMinDist = nSnappingEpsilon; // minimum distance of two adjacent snapping points
91 
92 // -----------------------------------------------------------------------
93 
94 // nOffset referes to the origin of the control:
95 // + ----------- -
96 sal_uInt16 SvxZoomSliderControl::Offset2Zoom( long nOffset ) const
97 {
98     const long nControlWidth = getControlRect().GetWidth();
99     sal_uInt16 nRet = 0;
100 
101     if ( nOffset < nSliderXOffset )
102         return mpImpl->mnMinZoom;;
103 
104     if ( nOffset > nControlWidth - nSliderXOffset )
105         return mpImpl->mnMaxZoom;
106 
107     // check for snapping points:
108     sal_uInt16 nCount = 0;
109     std::vector< long >::iterator aSnappingPointIter;
110     for ( aSnappingPointIter = mpImpl->maSnappingPointOffsets.begin();
111           aSnappingPointIter != mpImpl->maSnappingPointOffsets.end();
112           ++aSnappingPointIter )
113     {
114         const long nCurrent = *aSnappingPointIter;
115         if ( Abs(nCurrent - nOffset) < nSnappingEpsilon )
116         {
117             nOffset = nCurrent;
118             nRet = mpImpl->maSnappingPointZooms[ nCount ];
119             break;
120         }
121         ++nCount;
122     }
123 
124     if ( 0 == nRet )
125     {
126         if ( nOffset < nControlWidth / 2 )
127         {
128             // first half of slider
129             const long nFirstHalfRange = mpImpl->mnSliderCenter - mpImpl->mnMinZoom;
130             const long nHalfSliderWidth = nControlWidth/2 - nSliderXOffset;
131             const long nZoomPerSliderPixel = (1000 * nFirstHalfRange) / nHalfSliderWidth;
132             const long nOffsetToSliderLeft = nOffset - nSliderXOffset;
133             nRet = mpImpl->mnMinZoom + sal_uInt16( nOffsetToSliderLeft * nZoomPerSliderPixel / 1000 );
134         }
135         else
136         {
137             // second half of slider
138             const long nSecondHalfRange = mpImpl->mnMaxZoom - mpImpl->mnSliderCenter;
139             const long nHalfSliderWidth = nControlWidth/2 - nSliderXOffset;
140             const long nZoomPerSliderPixel = 1000 * nSecondHalfRange / nHalfSliderWidth;
141             const long nOffsetToSliderCenter = nOffset - nControlWidth/2;
142             nRet = mpImpl->mnSliderCenter + sal_uInt16( nOffsetToSliderCenter * nZoomPerSliderPixel / 1000 );
143         }
144     }
145 
146     if ( nRet < mpImpl->mnMinZoom )
147         nRet = mpImpl->mnMinZoom;
148     else if ( nRet > mpImpl->mnMaxZoom )
149         nRet = mpImpl->mnMaxZoom;
150 
151     return nRet;
152 }
153 
154 // returns the offset to the left control border
155 long SvxZoomSliderControl::Zoom2Offset( sal_uInt16 nCurrentZoom ) const
156 {
157     const long nControlWidth = getControlRect().GetWidth();
158     long nRet = nSliderXOffset;
159 
160     const long nHalfSliderWidth = nControlWidth/2 - nSliderXOffset;
161 
162     if ( nCurrentZoom <= mpImpl->mnSliderCenter )
163     {
164         nCurrentZoom = nCurrentZoom - mpImpl->mnMinZoom;
165         const long nFirstHalfRange = mpImpl->mnSliderCenter - mpImpl->mnMinZoom;
166         const long nSliderPixelPerZoomPercent = 1000 * nHalfSliderWidth  / nFirstHalfRange;
167         const long nOffset = (nSliderPixelPerZoomPercent * nCurrentZoom) / 1000;
168         nRet += nOffset;
169     }
170     else
171     {
172         nCurrentZoom = nCurrentZoom - mpImpl->mnSliderCenter;
173         const long nSecondHalfRange = mpImpl->mnMaxZoom - mpImpl->mnSliderCenter;
174         const long nSliderPixelPerZoomPercent = 1000 * nHalfSliderWidth  / nSecondHalfRange;
175         const long nOffset = (nSliderPixelPerZoomPercent * nCurrentZoom) / 1000;
176         nRet += nHalfSliderWidth + nOffset;
177     }
178 
179     return nRet;
180 }
181 
182 // -----------------------------------------------------------------------
183 
184 SvxZoomSliderControl::SvxZoomSliderControl( sal_uInt16 _nSlotId,  sal_uInt16 _nId, StatusBar& _rStb ) :
185     SfxStatusBarControl( _nSlotId, _nId, _rStb ),
186     mpImpl( new SvxZoomSliderControl_Impl )
187 {
188     const sal_Bool bHC = GetStatusBar().GetSettings().GetStyleSettings().GetHighContrastMode();
189     mpImpl->maSliderButton   = Image( SVX_RES( bHC ? RID_SVXBMP_SLIDERBUTTON_HC : RID_SVXBMP_SLIDERBUTTON ) );
190     mpImpl->maIncreaseButton = Image( SVX_RES( bHC ? RID_SVXBMP_SLIDERINCREASE_HC : RID_SVXBMP_SLIDERINCREASE ) );
191     mpImpl->maDecreaseButton = Image( SVX_RES( bHC ? RID_SVXBMP_SLIDERDECREASE_HC : RID_SVXBMP_SLIDERDECREASE ) );
192 }
193 
194 // -----------------------------------------------------------------------
195 
196 SvxZoomSliderControl::~SvxZoomSliderControl()
197 {
198 	delete mpImpl;
199 }
200 
201 // -----------------------------------------------------------------------
202 
203 void SvxZoomSliderControl::StateChanged( sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
204 {
205     if ( (SFX_ITEM_AVAILABLE != eState) || pState->ISA( SfxVoidItem ) )
206     {
207         GetStatusBar().SetItemText( GetId(), String() );
208         mpImpl->mbValuesSet   = false;
209     }
210     else
211     {
212         OSL_ENSURE( pState->ISA( SvxZoomSliderItem ), "invalid item type: should be a SvxZoomSliderItem" );
213         mpImpl->mnCurrentZoom = static_cast<const SvxZoomSliderItem*>( pState )->GetValue();
214         mpImpl->mnMinZoom     = static_cast<const SvxZoomSliderItem*>( pState )->GetMinZoom();
215         mpImpl->mnMaxZoom     = static_cast<const SvxZoomSliderItem*>( pState )->GetMaxZoom();
216         mpImpl->mnSliderCenter= 100;
217         mpImpl->mbValuesSet   = true;
218 
219         if ( mpImpl->mnSliderCenter == mpImpl->mnMaxZoom )
220             mpImpl->mnSliderCenter = mpImpl->mnMinZoom + (sal_uInt16)((mpImpl->mnMaxZoom - mpImpl->mnMinZoom) * 0.5);
221 
222 
223         DBG_ASSERT( mpImpl->mnMinZoom <= mpImpl->mnCurrentZoom &&
224                     mpImpl->mnMinZoom <  mpImpl->mnSliderCenter &&
225                     mpImpl->mnMaxZoom >= mpImpl->mnCurrentZoom &&
226                     mpImpl->mnMaxZoom > mpImpl->mnSliderCenter,
227                     "Looks like the zoom slider item is corrupted" );
228 
229         const com::sun::star::uno::Sequence < sal_Int32 > rSnappingPoints = static_cast<const SvxZoomSliderItem*>( pState )->GetSnappingPoints();
230         mpImpl->maSnappingPointOffsets.clear();
231         mpImpl->maSnappingPointZooms.clear();
232 
233         // get all snapping points:
234         std::set< sal_uInt16 > aTmpSnappingPoints;
235         for ( sal_uInt16 j = 0; j < rSnappingPoints.getLength(); ++j )
236         {
237             const sal_Int32 nSnappingPoint = rSnappingPoints[j];
238             aTmpSnappingPoints.insert( (sal_uInt16)nSnappingPoint );
239         }
240 
241         // remove snapping points that are to close to each other:
242         std::set< sal_uInt16 >::iterator aSnappingPointIter;
243         long nLastOffset = 0;
244 
245         for ( aSnappingPointIter = aTmpSnappingPoints.begin(); aSnappingPointIter != aTmpSnappingPoints.end(); ++aSnappingPointIter )
246         {
247             const sal_uInt16 nCurrent = *aSnappingPointIter;
248             const long nCurrentOffset = Zoom2Offset( nCurrent );
249 
250             if ( nCurrentOffset - nLastOffset >= nSnappingPointsMinDist )
251             {
252                 mpImpl->maSnappingPointOffsets.push_back( nCurrentOffset );
253                 mpImpl->maSnappingPointZooms.push_back( nCurrent );
254                 nLastOffset = nCurrentOffset;
255             }
256         }
257     }
258 
259     if ( !mpImpl->mbOmitPaint && GetStatusBar().AreItemsVisible() )
260         GetStatusBar().SetItemData( GetId(), 0 );    // force repaint
261 }
262 
263 // -----------------------------------------------------------------------
264 
265 void SvxZoomSliderControl::Paint( const UserDrawEvent& rUsrEvt )
266 {
267     if ( !mpImpl->mbValuesSet || mpImpl->mbOmitPaint )
268         return;
269 
270     const Rectangle     aControlRect = getControlRect();
271     OutputDevice*       pDev =  rUsrEvt.GetDevice();
272     Rectangle           aRect = rUsrEvt.GetRect();
273     Rectangle           aSlider = aRect;
274 
275     aSlider.Top()   += (aControlRect.GetHeight() - nSliderHeight)/2 - 1;
276     aSlider.Bottom() = aSlider.Top() + nSliderHeight;
277     aSlider.Left()  += nSliderXOffset;
278     aSlider.Right() -= nSliderXOffset;
279 
280     Color               aOldLineColor = pDev->GetLineColor();
281     Color               aOldFillColor = pDev->GetFillColor();
282 
283     pDev->SetLineColor( Color( COL_GRAY ) );
284     pDev->SetFillColor( Color( COL_GRAY ) );
285 
286     // draw snapping points:
287     std::vector< long >::iterator aSnappingPointIter;
288     for ( aSnappingPointIter = mpImpl->maSnappingPointOffsets.begin();
289           aSnappingPointIter != mpImpl->maSnappingPointOffsets.end();
290           ++aSnappingPointIter )
291     {
292         Rectangle aSnapping( aRect );
293         aSnapping.Bottom()   = aSlider.Top();
294         aSnapping.Top() = aSnapping.Bottom() - nSnappingHeight;
295         aSnapping.Left() += *aSnappingPointIter;
296         aSnapping.Right() = aSnapping.Left();
297         pDev->DrawRect( aSnapping );
298 
299         aSnapping.Top() += nSnappingHeight + nSliderHeight;
300         aSnapping.Bottom() += nSnappingHeight + nSliderHeight;
301         pDev->DrawRect( aSnapping );
302     }
303 
304     // draw slider
305     Rectangle aFirstLine( aSlider );
306     aFirstLine.Bottom() = aFirstLine.Top();
307 
308     Rectangle aSecondLine( aSlider );
309     aSecondLine.Top() = aSecondLine.Bottom();
310 
311     Rectangle aLeft( aSlider );
312     aLeft.Right() = aLeft.Left();
313 
314     Rectangle aRight( aSlider );
315     aRight.Left() = aRight.Right();
316 
317     pDev->SetLineColor( Color ( COL_WHITE ) );
318     pDev->SetFillColor( Color ( COL_WHITE ) );
319     pDev->DrawRect( aSecondLine );
320     pDev->DrawRect( aRight );
321 
322     pDev->SetLineColor( Color( COL_GRAY ) );
323     pDev->SetFillColor( Color( COL_GRAY ) );
324     pDev->DrawRect( aFirstLine );
325     pDev->DrawRect( aLeft );
326 
327     // draw slider button
328     Point aImagePoint = aRect.TopLeft();
329     aImagePoint.X() += Zoom2Offset( mpImpl->mnCurrentZoom );
330     aImagePoint.X() -= nButtonWidth/2;
331     aImagePoint.Y() += (aControlRect.GetHeight() - nButtonHeight)/2;
332     pDev->DrawImage( aImagePoint, mpImpl->maSliderButton );
333 
334     // draw decrease button
335     aImagePoint = aRect.TopLeft();
336     aImagePoint.X() += (nSliderXOffset - nIncDecWidth)/2;
337     aImagePoint.Y() += (aControlRect.GetHeight() - nIncDecHeight)/2;
338     pDev->DrawImage( aImagePoint, mpImpl->maDecreaseButton );
339 
340     // draw increase button
341     aImagePoint.X() = aRect.TopLeft().X() + aControlRect.GetWidth() - nIncDecWidth - (nSliderXOffset - nIncDecWidth)/2;
342     pDev->DrawImage( aImagePoint, mpImpl->maIncreaseButton );
343 
344     pDev->SetLineColor( aOldLineColor );
345     pDev->SetFillColor( aOldFillColor );
346 }
347 
348 // -----------------------------------------------------------------------
349 
350 sal_Bool SvxZoomSliderControl::MouseButtonDown( const MouseEvent & rEvt )
351 {
352     if ( !mpImpl->mbValuesSet )
353         return sal_True;;
354 
355     const Rectangle aControlRect = getControlRect();
356     const Point aPoint = rEvt.GetPosPixel();
357     const sal_Int32 nXDiff = aPoint.X() - aControlRect.Left();
358 
359     const long nButtonLeftOffset = (nSliderXOffset - nIncDecWidth)/2;
360     const long nButtonRightOffset = (nSliderXOffset + nIncDecWidth)/2;
361 
362     const long nOldZoom = mpImpl->mnCurrentZoom;
363 
364     // click to - button
365     if ( nXDiff >= nButtonLeftOffset && nXDiff <= nButtonRightOffset )
366         mpImpl->mnCurrentZoom = mpImpl->mnCurrentZoom - 5;
367     // click to + button
368     else if ( nXDiff >= aControlRect.GetWidth() - nSliderXOffset + nButtonLeftOffset &&
369               nXDiff <= aControlRect.GetWidth() - nSliderXOffset + nButtonRightOffset )
370         mpImpl->mnCurrentZoom = mpImpl->mnCurrentZoom + 5;
371     // click to slider
372     else if( nXDiff >= nSliderXOffset && nXDiff <= aControlRect.GetWidth() - nSliderXOffset )
373         mpImpl->mnCurrentZoom = Offset2Zoom( nXDiff );
374 
375     if ( mpImpl->mnCurrentZoom < mpImpl->mnMinZoom )
376         mpImpl->mnCurrentZoom = mpImpl->mnMinZoom;
377     else if ( mpImpl->mnCurrentZoom > mpImpl->mnMaxZoom )
378         mpImpl->mnCurrentZoom = mpImpl->mnMaxZoom;
379 
380     if ( nOldZoom == mpImpl->mnCurrentZoom )
381         return sal_True;
382 
383     if ( GetStatusBar().AreItemsVisible() )
384         GetStatusBar().SetItemData( GetId(), 0 );    // force repaint
385 
386     mpImpl->mbOmitPaint = true; // optimization: paint before executing command,
387                                 // then omit painting which is triggered by the execute function
388 
389     SvxZoomSliderItem aZoomSliderItem( mpImpl->mnCurrentZoom );
390 
391     ::com::sun::star::uno::Any a;
392     aZoomSliderItem.QueryValue( a );
393 
394     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs( 1 );
395     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ZoomSlider" ));
396     aArgs[0].Value = a;
397 
398     execute( aArgs );
399 
400     mpImpl->mbOmitPaint = false;
401 
402     return sal_True;
403 }
404 
405 // -----------------------------------------------------------------------
406 
407 sal_Bool SvxZoomSliderControl::MouseMove( const MouseEvent & rEvt )
408 {
409     if ( !mpImpl->mbValuesSet )
410         return sal_True;;
411 
412     const short nButtons = rEvt.GetButtons();
413 
414     // check mouse move with button pressed
415     if ( 1 == nButtons )
416     {
417         const Rectangle aControlRect = getControlRect();
418         const Point aPoint = rEvt.GetPosPixel();
419         const sal_Int32 nXDiff = aPoint.X() - aControlRect.Left();
420 
421         if ( nXDiff >= nSliderXOffset && nXDiff <= aControlRect.GetWidth() - nSliderXOffset )
422         {
423             mpImpl->mnCurrentZoom = Offset2Zoom( nXDiff );
424 
425             if ( GetStatusBar().AreItemsVisible() )
426                 GetStatusBar().SetItemData( GetId(), 0 );    // force repaint
427 
428             mpImpl->mbOmitPaint = true; // optimization: paint before executing command,
429                                         // then omit painting which is triggered by the execute function
430 
431             // commit state change
432             SvxZoomSliderItem aZoomSliderItem( mpImpl->mnCurrentZoom );
433 
434             ::com::sun::star::uno::Any a;
435             aZoomSliderItem.QueryValue( a );
436 
437             ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs( 1 );
438             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ZoomSlider" ));
439             aArgs[0].Value = a;
440 
441             execute( aArgs );
442 
443             mpImpl->mbOmitPaint = false;
444         }
445     }
446 
447     return sal_True;
448 }
449