1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SDEXT_PRESENTER_PRESENTER_SCROLL_BAR_HXX
25 #define SDEXT_PRESENTER_PRESENTER_SCROLL_BAR_HXX
26 
27 #include "PresenterBitmapContainer.hxx"
28 #include <com/sun/star/awt/Point.hpp>
29 #include <com/sun/star/awt/XWindow.hpp>
30 #include <com/sun/star/drawing/XPresenterHelper.hpp>
31 #include <com/sun/star/rendering/XCanvas.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <cppuhelper/basemutex.hxx>
34 #include <cppuhelper/compbase4.hxx>
35 #include <boost/function.hpp>
36 #include <boost/noncopyable.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 namespace css = ::com::sun::star;
40 
41 namespace sdext { namespace presenter {
42 
43 class PresenterCanvasHelper;
44 class PresenterPaintManager;
45 
46 namespace {
47     typedef ::cppu::WeakComponentImplHelper4 <
48         css::awt::XWindowListener,
49         css::awt::XPaintListener,
50         css::awt::XMouseListener,
51         css::awt::XMouseMotionListener
52     > PresenterScrollBarInterfaceBase;
53 }
54 
55 /** Base class of horizontal and vertical scroll bars.
56 */
57 class PresenterScrollBar
58     : private ::boost::noncopyable,
59       private ::cppu::BaseMutex,
60       public PresenterScrollBarInterfaceBase
61 {
62 public:
63     typedef ::boost::function<void(double)> ThumbMotionListener;
64     virtual ~PresenterScrollBar (void);
65 
66     virtual void SAL_CALL disposing (void);
67 
68     void SetVisible (const bool bIsVisible);
69 
70     /** Set the bounding box of the scroll bar.
71     */
72     void SetPosSize (const css::geometry::RealRectangle2D& rBox);
73 
74     /** Set the position of the movable thumb.
75         @param nPosition
76             A value between 0 and the last value given to SetTotalSize()
77             minus the last value given to SetThumbSize().
78     */
79     void SetThumbPosition (
80         double nPosition,
81         const bool bAsynchronousRepaint);
82 
83     double GetThumbPosition (void) const;
84 
85     /** Set the upper border of the slider range.
86     */
87     void SetTotalSize (const double nTotalSize);
88 
89     double GetTotalSize (void) const;
90 
91     /** Set the size of the movable thumb.
92         @param nThumbSize
93             A value not larger than the last value given to SetTotalSize().
94     */
95     void SetThumbSize (const double nThumbSize);
96     double GetThumbSize (void) const;
97 
98     void SetLineHeight (const double nLineHeight);
99     double GetLineHeight (void) const;
100 
101     /** Set the canvas that is used for painting the scroll bar.
102     */
103     void SetCanvas (const css::uno::Reference<css::rendering::XCanvas>& rxCanvas);
104 
105     void SetBackground (const SharedBitmapDescriptor& rpBackgroundBitmap);
106 
107     /** Call this after changing total size or thumb position or size to
108         move the thumb to a valid position.
109     */
110     void CheckValues (void);
111 
112     /** On some occasions it is necessary to trigger the painting of a
113         scrollbar from the outside.
114     */
115     virtual void Paint (
116         const css::awt::Rectangle& rUpdateBox,
117         bool bNoClip = false);
118 
119     virtual sal_Int32 GetSize (void) const = 0;
120 
121     // XWindowListener
122 
123     virtual void SAL_CALL windowResized (const css::awt::WindowEvent& rEvent)
124         throw (css::uno::RuntimeException);
125 
126     virtual void SAL_CALL windowMoved (const css::awt::WindowEvent& rEvent)
127         throw (css::uno::RuntimeException);
128 
129     virtual void SAL_CALL windowShown (const css::lang::EventObject& rEvent)
130         throw (css::uno::RuntimeException);
131 
132     virtual void SAL_CALL windowHidden (const css::lang::EventObject& rEvent)
133         throw (css::uno::RuntimeException);
134 
135 
136     // XPaintListener
137 
138     virtual void SAL_CALL windowPaint (const css::awt::PaintEvent& rEvent)
139         throw (css::uno::RuntimeException);
140 
141 
142     // XMouseListener
143 
144     virtual void SAL_CALL mousePressed (const css::awt::MouseEvent& rEvent)
145         throw(css::uno::RuntimeException);
146 
147     virtual void SAL_CALL mouseReleased (const css::awt::MouseEvent& rEvent)
148         throw(css::uno::RuntimeException);
149 
150     virtual void SAL_CALL mouseEntered (const css::awt::MouseEvent& rEvent)
151         throw(css::uno::RuntimeException);
152 
153     virtual void SAL_CALL mouseExited (const css::awt::MouseEvent& rEvent)
154         throw(css::uno::RuntimeException);
155 
156 
157     // XMouseMotionListener
158 
159     virtual void SAL_CALL mouseMoved (const css::awt::MouseEvent& rEvent)
160         throw (css::uno::RuntimeException);
161 
162     virtual void SAL_CALL mouseDragged (const css::awt::MouseEvent& rEvent)
163         throw (css::uno::RuntimeException);
164 
165 
166     // lang::XEventListener
167     virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
168         throw (css::uno::RuntimeException);
169 
170 
171     enum Area { Total, Pager, Thumb, PagerUp, PagerDown, PrevButton, NextButton, None,
172                 __AreaCount__ = None };
173 
174 protected:
175     css::uno::Reference<css::uno::XComponentContext> mxComponentContext;
176     css::uno::Reference<css::awt::XWindow> mxParentWindow;
177     css::uno::Reference<css::awt::XWindow> mxWindow;
178     css::uno::Reference<css::rendering::XCanvas> mxCanvas;
179     css::uno::Reference<css::drawing::XPresenterHelper> mxPresenterHelper;
180     ::boost::shared_ptr<PresenterPaintManager> mpPaintManager;
181     double mnThumbPosition;
182     double mnTotalSize;
183     double mnThumbSize;
184     double mnLineHeight;
185     css::geometry::RealPoint2D maDragAnchor;
186     ::boost::function<void(double)> maThumbMotionListener;
187     Area meButtonDownArea;
188     Area meMouseMoveArea;
189     css::geometry::RealRectangle2D maBox[__AreaCount__];
190     bool mbIsNotificationActive;
191     static boost::weak_ptr<PresenterBitmapContainer> mpSharedBitmaps;
192     boost::shared_ptr<PresenterBitmapContainer> mpBitmaps;
193     SharedBitmapDescriptor mpPrevButtonDescriptor;
194     SharedBitmapDescriptor mpNextButtonDescriptor;
195     SharedBitmapDescriptor mpPagerStartDescriptor;
196     SharedBitmapDescriptor mpPagerCenterDescriptor;
197     SharedBitmapDescriptor mpPagerEndDescriptor;
198     SharedBitmapDescriptor mpThumbStartDescriptor;
199     SharedBitmapDescriptor mpThumbCenterDescriptor;
200     SharedBitmapDescriptor mpThumbEndDescriptor;
201     bool maEnabledState[__AreaCount__];
202 
203     virtual css::geometry::RealRectangle2D GetRectangle (const Area eArea) const;
204     virtual double GetDragDistance (const sal_Int32 nX, const sal_Int32 nY) const = 0;
205     virtual void UpdateDragAnchor (const double nDragDistance) = 0;
206     virtual css::geometry::RealPoint2D GetPoint (const double nMajor, const double nMinor) const = 0;
207     virtual double GetMajor (const double nX, const double nY) const = 0;
208     virtual double GetMinor (const double nX, const double nY) const = 0;
209     virtual void UpdateBorders (void) = 0;
210     virtual void UpdateBitmaps (void) = 0;
211     virtual void PaintComposite(
212         const css::awt::Rectangle& rRepaintBox,
213         const Area eArea,
214         const SharedBitmapDescriptor& rpStartBitmaps,
215         const SharedBitmapDescriptor& rpCenterBitmaps,
216         const SharedBitmapDescriptor& rpEndBitmaps) = 0;
217 
218     PresenterScrollBar (
219         const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
220         const css::uno::Reference<css::awt::XWindow>& rxParentWindow,
221         const ::boost::shared_ptr<PresenterPaintManager>& rpPaintManager,
222         const ::boost::function<void(double)>& rThumbMotionListener);
223 
224     void Repaint (
225         const css::geometry::RealRectangle2D aBox,
226         const bool bAsynchronous);
227     void PaintBackground (
228         const css::awt::Rectangle& rRepaintBox);
229     void PaintBitmap(
230         const css::awt::Rectangle& rRepaintBox,
231         const Area eArea,
232         const SharedBitmapDescriptor& rpBitmaps);
233     void NotifyThumbPositionChange (void);
234     void UpdateWidthOrHeight (sal_Int32& rSize,
235         const SharedBitmapDescriptor& rpDescriptor);
236     css::uno::Reference<css::rendering::XBitmap> GetBitmap (
237         const Area eArea,
238         const SharedBitmapDescriptor& rpBitmaps) const;
239     PresenterBitmapContainer::BitmapDescriptor::Mode GetBitmapMode (
240         const Area eArea) const;
241     bool IsDisabled (const Area eArea) const;
242     double ValidateThumbPosition (double nPosition);
243     void SetThumbPosition (
244         double nPosition,
245         const bool bAsynchronousRepaint,
246         const bool bValidate,
247         const bool bNotify);
248 
249 private:
250     class MousePressRepeater;
251     ::boost::shared_ptr<MousePressRepeater> mpMousePressRepeater;
252     SharedBitmapDescriptor mpBackgroundBitmap;
253     ::boost::scoped_ptr<PresenterCanvasHelper> mpCanvasHelper;
254 
255     Area GetArea (const double nX, const double nY) const;
256 };
257 
258 
259 
260 
261 /** A vertical scroll bar.
262 */
263 class PresenterVerticalScrollBar : public PresenterScrollBar
264 {
265 public:
266     PresenterVerticalScrollBar (
267         const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
268         const css::uno::Reference<css::awt::XWindow>& rxParentWindow,
269         const ::boost::shared_ptr<PresenterPaintManager>& rpPaintManager,
270         const ::boost::function<void(double)>& rThumbMotionListener);
271     virtual ~PresenterVerticalScrollBar (void);
272     virtual sal_Int32 GetSize (void) const;
273 
274 protected:
275     virtual double GetDragDistance (const sal_Int32 nX, const sal_Int32 nY) const;
276     virtual void UpdateDragAnchor (const double nDragDistance);
277     virtual css::geometry::RealPoint2D GetPoint (const double nMajor, const double nMinor) const;
278     virtual double GetMinor (const double nX, const double nY) const;
279     virtual double GetMajor (const double nX, const double nY) const;
280     virtual void UpdateBorders (void);
281     virtual void UpdateBitmaps (void);
282     virtual void PaintComposite(
283         const css::awt::Rectangle& rRepaintBox,
284         const Area eArea,
285         const SharedBitmapDescriptor& rpStartBitmaps,
286         const SharedBitmapDescriptor& rpCenterBitmaps,
287         const SharedBitmapDescriptor& rpEndBitmaps);
288 
289 private:
290     sal_Int32 mnScrollBarWidth;
291 };
292 
293 
294 
295 
296 /** A horizontal scroll bar.
297 */
298 class PresenterHorizontalScrollBar : public PresenterScrollBar
299 {
300 public:
301     PresenterHorizontalScrollBar (
302         const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
303         const css::uno::Reference<css::awt::XWindow>& rxParentWindow,
304         const ::boost::shared_ptr<PresenterPaintManager>& rpPaintManager,
305         const ::boost::function<void(double)>& rThumbMotionListener);
306     virtual ~PresenterHorizontalScrollBar (void);
307     virtual sal_Int32 GetSize (void) const;
308 
309 protected:
310     virtual double GetDragDistance (const sal_Int32 nX, const sal_Int32 nY) const;
311     virtual void UpdateDragAnchor (const double nDragDistance);
312     virtual css::geometry::RealPoint2D GetPoint (const double nMajor, const double nMinor) const;
313     virtual double GetMinor (const double nX, const double nY) const;
314     virtual double GetMajor (const double nX, const double nY) const;
315     virtual void UpdateBorders (void);
316     virtual void UpdateBitmaps (void);
317     virtual void PaintComposite(
318         const css::awt::Rectangle& rRepaintBox,
319         const Area eArea,
320         const SharedBitmapDescriptor& rpStartBitmaps,
321         const SharedBitmapDescriptor& rpCenterBitmaps,
322         const SharedBitmapDescriptor& rpEndBitmaps);
323 
324 private:
325     sal_Int32 mnScrollBarHeight;
326 };
327 
328 
329 
330 
331 } } // end of namespace ::sdext::presenter
332 
333 #endif
334