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 #ifndef RPTUI_VIEWSWINDOW_HXX
28 #define RPTUI_VIEWSWINDOW_HXX
29 
30 #include <com/sun/star/report/XSection.hpp>
31 #include <vcl/window.hxx>
32 #include <svtools/colorcfg.hxx>
33 #include "ReportDefines.hxx"
34 #include "ReportSection.hxx"
35 #include <comphelper/propmultiplex.hxx>
36 #include "cppuhelper/basemutex.hxx"
37 #include <svtools/colorcfg.hxx>
38 #include <com/sun/star/beans/NamedValue.hpp>
39 #include <svx/svdedtv.hxx>
40 #include <SectionView.hxx>
41 #include <unotools/options.hxx>
42 #include <list>
43 #include <vector>
44 #include <boost/shared_ptr.hpp>
45 
46 #include <MarkedSection.hxx>
47 #include <SectionWindow.hxx>
48 
49 class SdrHdl;
50 namespace rptui
51 {
52 	class OReportWindow;
53 	class ODesignView;
54 	class OEndMarker;
55 	class OReportSection;
56 	class OSectionView;
57 
58 
59     // -----------------------------------------------------------------------------
60     struct RectangleLess : public ::std::binary_function< Rectangle, Rectangle, bool>
61     {
62         enum CompareMode { POS_LEFT,POS_RIGHT,POS_UPPER,POS_DOWN,POS_CENTER_HORIZONTAL,POS_CENTER_VERTICAL };
63         CompareMode m_eCompareMode;
64         Point       m_aRefPoint;
65         RectangleLess(CompareMode _eCompareMode,const Point& _rRefPoint ) : m_eCompareMode(_eCompareMode),m_aRefPoint(_rRefPoint){}
66         bool operator() (const Rectangle& lhs, const Rectangle& rhs) const
67         {
68             switch(m_eCompareMode)
69             {
70             case POS_LEFT:
71                 return lhs.Left() < rhs.Left();
72             case POS_RIGHT:
73                 return lhs.Right() >= rhs.Right();
74             case POS_UPPER:
75                 return lhs.Top() < rhs.Top();
76             case POS_DOWN:
77                 return lhs.Bottom() >= rhs.Bottom();
78             case POS_CENTER_HORIZONTAL:
79                 return abs(m_aRefPoint.X() - lhs.Center().X()) < abs(m_aRefPoint.X() - rhs.Center().X());
80             case POS_CENTER_VERTICAL:
81                 return abs(lhs.Center().Y() - m_aRefPoint.Y()) < abs(rhs.Center().Y() - m_aRefPoint.Y());
82             }
83             return false;
84         }
85     };
86 
87     class OWindowPositionCorrector
88 	{
89 		::std::vector< ::std::pair<Window*,Point> > m_aChildren;
90 		long m_nDeltaX;
91 		long m_nDeltaY;
92 	public:
93 		OWindowPositionCorrector(Window* _pWindow,long _nDeltaX, long _nDeltaY) :m_nDeltaX(_nDeltaX), m_nDeltaY(_nDeltaY)
94 		{
95 			sal_uInt16 nCount = _pWindow->GetChildCount();
96 			m_aChildren.reserve(nCount);
97 			while( nCount )
98 			{
99 				Window* pChild = _pWindow->GetChild(--nCount);
100 				m_aChildren.push_back(::std::pair<Window*,Point>(pChild,pChild->GetPosPixel()));
101 			}
102 		}
103 		~OWindowPositionCorrector()
104 		{
105 			::std::vector< ::std::pair<Window*,Point> >::iterator aIter = m_aChildren.begin();
106 			::std::vector< ::std::pair<Window*,Point> >::iterator aEnd = m_aChildren.end();
107 			for (; aIter != aEnd; ++aIter)
108 			{
109 				const Point aPos = aIter->first->GetPosPixel();
110 				if ( aPos == aIter->second )
111 					aIter->first->SetPosPixel(Point(m_nDeltaX,m_nDeltaY) + aPos);
112 			}
113 		}
114 	};
115 
116     class OViewsWindow :	public Window
117 						,	public utl::ConfigurationListener
118                         ,   public IMarkedSection
119 	{
120         typedef ::std::multimap<Rectangle,::std::pair<SdrObject*,OSectionView*>,RectangleLess>      TRectangleMap;
121 	public:
122 		typedef ::std::vector< ::boost::shared_ptr<OSectionWindow> >								TSectionsMap;
123 
124 		struct TReportPairHelper : public ::std::unary_function< TSectionsMap::value_type, OReportSection >
125 		{
126 			OReportSection& operator() (const TSectionsMap::value_type& lhs) const
127 			{
128 				return lhs->getReportSection();
129 			}
130 		};
131         struct TStartMarkerHelper : public ::std::unary_function< TSectionsMap::value_type, OStartMarker >
132 		{
133 			OStartMarker& operator() (const TSectionsMap::value_type& lhs) const
134 			{
135 				return lhs->getStartMarker();
136 			}
137 		};
138 	private:
139 		TSectionsMap							m_aSections;
140 		svtools::ColorConfig					m_aColorConfig;
141 		OReportWindow*							m_pParent;
142         ::rtl::OUString                         m_sShapeType;
143 		sal_Bool								m_bInSplitHandler;
144 		sal_Bool								m_bInUnmark;
145 
146 		void ImplInitSettings();
147 		/** returns the iterator at pos _nPos or the end()
148 		*/
149 		TSectionsMap::iterator getIteratorAtPos(sal_uInt16 _nPos);
150         void collectRectangles(TRectangleMap& _rMap,bool _bBoundRects);
151         void collectBoundResizeRect(const TRectangleMap& _rSortRectangles,sal_Int32 _nControlModification,bool _bAlignAtSection,bool _bBoundRects,Rectangle& _rBound,Rectangle& _rResize);
152         void impl_resizeSectionWindow(OSectionWindow& _rSectionWindow,Point& _rStartPoint,bool _bSet);
153 
154         OViewsWindow(OViewsWindow&);
155         void operator =(OViewsWindow&);
156 	protected:
157 		virtual void DataChanged( const DataChangedEvent& rDCEvt );
158         // windows overload
159 		virtual void MouseButtonDown( const MouseEvent& rMEvt );
160 		virtual void MouseButtonUp( const MouseEvent& rMEvt );
161 
162         virtual void Paint( const Rectangle& rRect );
163 		virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 );
164 	public:
165 		OViewsWindow(
166             OReportWindow* _pReportWindow);
167 		virtual ~OViewsWindow();
168 
169         // windows overload
170 		virtual void Resize();
171 
172         void resize(const OSectionWindow& _rSectionWindow);
173 
174 		/** late ctor
175 		*/
176 		void initialize();
177 
178 		inline OReportWindow*		getView()			const { return m_pParent; }
179 
180 		/** removes the section at the given position.
181 		*
182 		* \param _nPosition Zero based.
183 		*/
184 		void			removeSection(sal_uInt16 _nPosition);
185 
186 		/** adds a new section at position _nPosition.
187 			If the section is <NULL/> nothing happens.
188 			If the position is grater than the current elements, the section will be appended.
189 		*/
190 		void			addSection(const ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection >& _xSection
191 									,const ::rtl::OUString& _sColorEntry
192 									,sal_uInt16 _nPosition = USHRT_MAX);
193 
194 		sal_uInt16			getSectionCount() const;
195         /** return the section at the given position
196         *
197         * \param _nPos
198         * \return the section at this pos or an empty section
199         */
200         ::boost::shared_ptr<OSectionWindow> getSectionWindow(const sal_uInt16 _nPos) const;
201 
202 		/** turns the grid on or off
203 		*
204 		* \param _bVisible
205 		*/
206 		void			toggleGrid(sal_Bool _bVisible);
207         void            setGridSnap(sal_Bool bOn);
208         void            setDragStripes(sal_Bool bOn);
209 
210 		/** returns the total accumulated height of all sections until _pSection is reached
211 		*/
212 		sal_Int32		getTotalHeight() const;
213 
214 		inline bool		empty() const { return m_aSections.empty(); }
215 		void 			SetMode( DlgEdMode m_eMode );
216 		void			SetInsertObj( sal_uInt16 eObj,const ::rtl::OUString& _sShapeType = ::rtl::OUString());
217 		rtl::OUString   GetInsertObjString() const;
218 		/** copies the current selection in this section
219 		*/
220 		void Copy();
221 
222 		/**	returns if paste is allowed
223 		*
224 		* \return <TRUE/> if paste is allowed
225 		*/
226 		sal_Bool IsPasteAllowed() const;
227 
228 		/** paste a new control in this section
229 		*/
230 		void Paste();
231 
232 		/** Deletes the current selection in this section
233 		*
234 		*/
235 		void Delete();
236 
237 		/** All objects will be marked.
238 		*/
239 		void SelectAll(const sal_uInt16 _nObjectType);
240 
241         /** returns <TRUE/> when a object is marked
242         */
243         sal_Bool HasSelection() const;
244 
245 		/** unmark all objects on the views without the given one.
246 		*
247 		* @param _pSectionView The view where the objects should not be unmarked.
248 		*/
249 		void			unmarkAllObjects(OSectionView* _pSectionView);
250 
251 		/** returns the report section window for the given xsection
252 			@param	_xSection	the section
253 		*/
254 		// ::boost::shared_ptr<OSectionWindow>	getReportSection(const ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection >& _xSection);
255 		::boost::shared_ptr<OSectionWindow> getSectionWindow(const ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection>& _xSection) const;
256 
257 		/** checks if the keycode is known by the child windows
258 			@param	_rCode	the keycode
259 			@return <TRUE/> if the keycode is handled otherwise <FALSE/>
260 		*/
261 		sal_Bool		handleKeyEvent(const KeyEvent& _rEvent);
262 
263 		/** the the section as marked or not marked
264 			@param	_pSectionView	the section where to set the marked flag
265 			@param	_bMark	the marked flag
266 		*/
267 		void			setMarked(OSectionView* _pSectionView,sal_Bool _bMark);
268         void			setMarked(const ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection>& _xSection,sal_Bool _bMark);
269         void			setMarked(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::report::XReportComponent> >& _xShape,sal_Bool _bMark);
270 
271 		// IMarkedSection
272 		::boost::shared_ptr<OSectionWindow> getMarkedSection(NearSectionAccess nsa = CURRENT) const;
273         virtual void markSection(const sal_uInt16 _nPos);
274 
275         /** align all marked objects in all sections
276         */
277         void alignMarkedObjects(sal_Int32 _nControlModification,bool _bAlignAtSection, bool bBoundRects = false);
278 
279         /** creates a default object
280         *
281         */
282         void createDefault();
283 
284         /** shows or hides the ruler.
285 		*/
286 		void showRuler(sal_Bool _bShow);
287 
288         /** returns the currently set shape type.
289         *
290         * \return \member m_sShapeType
291         */
292         inline ::rtl::OUString getShapeType() const { return m_sShapeType; }
293 
294 		/** returns the current position in the list
295 		*/
296 		sal_uInt16 getPosition(const OSectionWindow* _pSectionWindow = NULL) const;
297 
298         /** calls on every section BrkAction
299         *
300         */
301         void BrkAction();
302         void BegMarkObj(const Point& _aPnt,const OSectionView* _pSection);
303 
304     private:
305         void BegDragObj_createInvisibleObjectAtPosition(const Rectangle& _aRect, const OSectionView& _rSection);
306 		void EndDragObj_removeInvisibleObjects();
307         Point m_aDragDelta;
308         ::std::vector<SdrObject*> m_aBegDragTempList;
309         bool isObjectInMyTempList(SdrObject *);
310     public:
311         void BegDragObj(const Point& _aPnt, SdrHdl* _pHdl,const OSectionView* _pSection);
312         void EndDragObj(sal_Bool _bDragIntoNewSection,const OSectionView* _pSection,const Point& _aPnt);
313 
314 		void EndAction();
315         void ForceMarkedToAnotherPage();
316         sal_Bool IsAction() const;
317         sal_Bool IsDragObj() const;
318         void handleKey(const KeyCode& _rCode);
319         void stopScrollTimer();
320 
321         /** return the section at the given point which is relative to the given section
322         *
323         * \param _pSection the section which is used as reference point
324         * \param _rPnt the point, it will be changed that it is inside the section which will be returned
325         * \return the section
326         */
327         OSectionView* getSectionRelativeToPosition(const OSectionView* _pSection,Point& _rPnt);
328 
329         void MovAction(const Point& rPnt,const OSectionView* _pSection,bool _bMove /*= true */, bool _bControlKeySet);
330         // void MovAction2(const Point& rPnt,const OSectionView* _pSection);
331 
332         sal_uInt32 getMarkedObjectCount() const;
333 
334         /** fills the positions of all collapsed sections.
335         *
336         * \param _rCollapsedPositions Out parameter which holds afterwards all positions of the collapsed sections.
337         */
338         void fillCollapsedSections(::std::vector<sal_uInt16>& _rCollapsedPositions) const;
339 
340         /** collpase all sections given by their position
341         *
342         * \param _aCollpasedSections The position of the sections which should be collapsed.
343         */
344         void collapseSections(const com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& _aCollpasedSections);
345 
346         /** zoom the ruler and view windows
347         */
348         void zoom(const Fraction& _aZoom);
349 
350         void scrollChildren(const Point& _aThumbPos);
351 
352         /** fills the vector with all selected control models
353             /param  _rSelection The vector will be filled and will not be cleared before.
354         */
355         void fillControlModelSelection(::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > >& _rSelection) const;
356 	};
357 //==============================================================================
358 } // rptui
359 //==============================================================================
360 #endif // RPTUI_VIEWSWINDOW_HXX
361 
362