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 #include "precompiled_reportdesign.hxx"
28 #include "ReportWindow.hxx"
29 #include "ReportSection.hxx"
30 #include "SectionView.hxx"
31 #include "ViewsWindow.hxx"
32 #include "ReportRuler.hxx"
33 #include "DesignView.hxx"
34 #include "UITools.hxx"
35 
36 #include <tools/debug.hxx>
37 #include <svtools/colorcfg.hxx>
38 #include <svl/itempool.hxx>
39 #include <unotools/syslocale.hxx>
40 
41 #include "RptDef.hxx"
42 #include "dlgedfunc.hxx"
43 #include "RptModel.hxx"
44 #include "uistrings.hrc"
45 #include "RptPage.hxx"
46 #include "ReportController.hxx"
47 #include "EndMarker.hxx"
48 #include "ColorChanger.hxx"
49 #include "ScrollHelper.hxx"
50 
51 #include "helpids.hrc"
52 #include "dlgedfac.hxx"
53 #include <boost/bind.hpp>
54 #include <toolkit/helper/vclunohelper.hxx>
55 
56 
57 #define SECTION_OFFSET	3
58 namespace rptui
59 {
60 using namespace ::com::sun::star;
61 using namespace ::comphelper;
62 //==================================================================
63 // class OReportWindow
64 //==================================================================
65 DBG_NAME( rpt_OReportWindow )
66 //------------------------------------------------------------------------------
67 OReportWindow::OReportWindow(OScrollWindowHelper* _pParent,ODesignView* _pView)
68 : Window(_pParent,WB_DIALOGCONTROL)
69 , ::comphelper::OPropertyChangeListener(m_aMutex)
70 ,m_aHRuler(this)
71 ,m_pView(_pView)
72 ,m_pParent(_pParent)
73 ,m_aViewsWindow(this)
74 ,m_pObjFac( new DlgEdFactory() )
75 {
76 	DBG_CTOR( rpt_OReportWindow,NULL);
77 	SetHelpId(UID_RPT_REPORTWINDOW);
78 	SetMapMode( MapMode( MAP_100TH_MM ) );
79 
80 	m_aViewsWindow.Show();
81 
82 	m_aHRuler.Show();
83 	m_aHRuler.Activate();
84 	m_aHRuler.SetPagePos(0);
85 	m_aHRuler.SetBorders();
86 	m_aHRuler.SetIndents();
87 	m_aHRuler.SetMargin1();
88 	m_aHRuler.SetMargin2();
89     const MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
90     m_aHRuler.SetUnit(MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH);
91 
92 	ImplInitSettings();
93     m_pReportListener = addStyleListener(_pView->getController().getReportDefinition(),this);
94 }
95 //------------------------------------------------------------------------------
96 OReportWindow::~OReportWindow()
97 {
98 	DBG_DTOR( rpt_OReportWindow,NULL);
99     if ( m_pReportListener.is() )
100 		m_pReportListener->dispose();
101 }
102 // -----------------------------------------------------------------------------
103 void OReportWindow::initialize()
104 {
105 	m_aViewsWindow.initialize();
106 }
107 //----------------------------------------------------------------------------
108 void OReportWindow::SetInsertObj( sal_uInt16 eObj,const ::rtl::OUString& _sShapeType )
109 {
110 	m_aViewsWindow.SetInsertObj( eObj,_sShapeType);
111 }
112 
113 //----------------------------------------------------------------------------
114 rtl::OUString OReportWindow::GetInsertObjString() const
115 {
116 	return m_aViewsWindow.GetInsertObjString();
117 }
118 //------------------------------------------------------------------------------
119 void OReportWindow::SetMode( DlgEdMode eNewMode )
120 {
121 	m_aViewsWindow.SetMode(eNewMode);
122 }
123 //----------------------------------------------------------------------------
124 void OReportWindow::removeSection(sal_uInt16 _nPosition)
125 {
126 	m_aViewsWindow.removeSection(_nPosition);
127 	m_pParent->setTotalSize(GetTotalWidth(),GetTotalHeight());
128     m_aViewsWindow.Invalidate(INVALIDATE_TRANSPARENT);
129 	//Resize();
130 }
131 //----------------------------------------------------------------------------
132 void OReportWindow::addSection(const uno::Reference< report::XSection >& _xSection,const ::rtl::OUString& _sColorEntry,sal_uInt16 _nPosition)
133 {
134 	if ( !_xSection.is() )
135 		return;
136 
137 	m_aViewsWindow.addSection(_xSection,_sColorEntry,_nPosition);
138 
139 	m_pParent->setTotalSize(GetTotalWidth(),GetTotalHeight());
140 }
141 //------------------------------------------------------------------------------
142 void OReportWindow::toggleGrid(sal_Bool _bVisible)
143 {
144 	m_aViewsWindow.toggleGrid(_bVisible);
145 }
146 //----------------------------------------------------------------------------
147 void OReportWindow::showRuler(sal_Bool _bShow)
148 {
149 	m_aHRuler.Show(_bShow);
150 
151 	m_aViewsWindow.showRuler(_bShow);
152 }
153 //------------------------------------------------------------------------------
154 sal_Int32 OReportWindow::getMaxMarkerWidth(sal_Bool _bWithEnd) const
155 {
156     Fraction aStartWidth(long(REPORT_STARTMARKER_WIDTH));
157     aStartWidth *= m_aViewsWindow.GetMapMode().GetScaleX();
158     if ( _bWithEnd )
159         aStartWidth += Fraction(long(REPORT_ENDMARKER_WIDTH));
160 	return sal_Int32((long)aStartWidth);
161 }
162 //------------------------------------------------------------------------------
163 sal_Int32 OReportWindow::GetTotalWidth() const
164 {
165 	sal_Int32 nWidth = 0;
166 	if ( !m_aViewsWindow.empty() )
167 	{
168         Fraction aStartWidth(long(REPORT_ENDMARKER_WIDTH + REPORT_STARTMARKER_WIDTH ));
169         const Fraction aZoom(m_pView->getController().getZoomValue(),100);
170         aStartWidth *= aZoom; // m_aViewsWindow.GetMapMode().GetScaleX();;
171 		const sal_Int32 nPaperWidth = getStyleProperty<awt::Size>(m_pView->getController().getReportDefinition(),PROPERTY_PAPERSIZE).Width;
172         Fraction aPaperWidth(nPaperWidth,1);
173         aPaperWidth *= aZoom;
174 		const Size aPageSize = LogicToPixel(Size(aPaperWidth,0));
175 		nWidth = aPageSize.Width() + long(aStartWidth);
176 	}
177 	return nWidth;
178 }
179 //------------------------------------------------------------------------------
180 void OReportWindow::Resize()
181 {
182 	Window::Resize();
183 	if ( !m_aViewsWindow.empty() )
184 	{
185 		const Size aTotalOutputSize = GetOutputSizePixel();
186         Fraction aStartWidth(long(REPORT_STARTMARKER_WIDTH)*m_pView->getController().getZoomValue(),100);
187 
188 		const Point aOffset = LogicToPixel( Point( SECTION_OFFSET, 0 ), MAP_APPFONT );
189 		Point aStartPoint((long)aStartWidth + aOffset.X(),0);
190 		uno::Reference<report::XReportDefinition> xReportDefinition = getReportView()->getController().getReportDefinition();
191 		const sal_Int32 nPaperWidth = getStyleProperty<awt::Size>(xReportDefinition,PROPERTY_PAPERSIZE).Width;
192 		sal_Int32 nLeftMargin = getStyleProperty<sal_Int32>(xReportDefinition,PROPERTY_LEFTMARGIN);
193 		sal_Int32 nRightMargin = getStyleProperty<sal_Int32>(xReportDefinition,PROPERTY_RIGHTMARGIN);
194 		Size aPageSize  = m_aViewsWindow.LogicToPixel(Size(nPaperWidth ,0));
195 		nLeftMargin     = m_aViewsWindow.LogicToPixel(Size(nLeftMargin,0)).Width();
196 		nRightMargin    = m_aViewsWindow.LogicToPixel(Size(nRightMargin,0)).Width();
197 
198 		aPageSize.Height() = m_aHRuler.GetSizePixel().Height();
199 
200         const long nTermp(m_aViewsWindow.getTotalHeight() + aPageSize.Height());
201 		long nSectionsHeight = ::std::max<long>(nTermp,aTotalOutputSize.Height());
202 
203 		m_aHRuler.SetPosSizePixel(aStartPoint,aPageSize);
204 		m_aHRuler.SetNullOffset(nLeftMargin);
205 		m_aHRuler.SetMargin1(0);
206 		m_aHRuler.SetMargin2(aPageSize.Width() - nLeftMargin - nRightMargin);
207 
208 		aStartPoint.Y() += aPageSize.Height();
209 		nSectionsHeight -= aStartPoint.Y();
210 
211         aStartPoint.X() = aOffset.X();
212 
213         m_aViewsWindow.SetPosSizePixel(aStartPoint,Size(aTotalOutputSize.Width(),nSectionsHeight));
214 	}
215 }
216 //------------------------------------------------------------------------------
217 Point OReportWindow::getThumbPos() const
218 {
219 	return m_pParent->getThumbPos();
220 }
221 //------------------------------------------------------------------------------
222 void OReportWindow::ImplInitSettings()
223 {
224 	SetBackground( );
225 }
226 //-----------------------------------------------------------------------------
227 void OReportWindow::DataChanged( const DataChangedEvent& rDCEvt )
228 {
229 	Window::DataChanged( rDCEvt );
230 
231 	if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
232 		 (rDCEvt.GetFlags() & SETTINGS_STYLE) )
233 	{
234 		ImplInitSettings();
235         Invalidate();
236 	}
237 }
238 //------------------------------------------------------------------------------
239 sal_Int32 OReportWindow::GetTotalHeight() const
240 {
241 	return m_aViewsWindow.getTotalHeight();
242 }
243 //------------------------------------------------------------------------------
244 void OReportWindow::ScrollChildren(const Point& _aThumbPos)
245 {
246     MapMode aMap = m_aHRuler.GetMapMode();
247     Point aOrg( aMap.GetOrigin() );
248     if ( aOrg.X() != (-_aThumbPos.X()) )
249     {
250         aMap.SetOrigin( Point(- _aThumbPos.X(), aOrg.Y()));
251         m_aHRuler.SetMapMode( aMap );
252         m_aHRuler.Scroll(-(aOrg.X() + _aThumbPos.X()),0);
253     }
254 
255     /*const Point aPos(PixelToLogic(_aThumbPos));*/
256     m_aViewsWindow.scrollChildren(_aThumbPos);
257 }
258 //----------------------------------------------------------------------------
259 sal_uInt16 OReportWindow::getSectionCount() const
260 {
261 	return m_aViewsWindow.getSectionCount();
262 }
263 //----------------------------------------------------------------------------
264 void OReportWindow::notifySizeChanged()
265 {
266 	m_pParent->setTotalSize(GetTotalWidth(),GetTotalHeight());
267 }
268 //----------------------------------------------------------------------------
269 sal_Bool OReportWindow::HasSelection() const
270 {
271 	return m_aViewsWindow.HasSelection();
272 }
273 //----------------------------------------------------------------------------
274 void OReportWindow::Delete()
275 {
276 
277 	m_aViewsWindow.Delete();
278 }
279 //----------------------------------------------------------------------------
280 void OReportWindow::Copy()
281 {
282 
283 	m_aViewsWindow.Copy();
284 }
285 //----------------------------------------------------------------------------
286 void OReportWindow::Paste()
287 {
288 
289 	m_aViewsWindow.Paste();
290 }
291 //----------------------------------------------------------------------------
292 sal_Bool OReportWindow::IsPasteAllowed() const
293 {
294     return m_aViewsWindow.IsPasteAllowed();
295 }
296 //-----------------------------------------------------------------------------
297 void OReportWindow::SelectAll(const sal_uInt16 _nObjectType)
298 {
299 
300 	m_aViewsWindow.SelectAll(_nObjectType);
301 }
302 //-----------------------------------------------------------------------------
303 void OReportWindow::unmarkAllObjects(OSectionView* _pSectionView)
304 {
305 
306 	m_aViewsWindow.unmarkAllObjects(_pSectionView);
307 }
308 //-----------------------------------------------------------------------------
309 void OReportWindow::showProperties(const uno::Reference< report::XSection>& _xReportComponent)
310 {
311 	::boost::shared_ptr<OSectionWindow> pSectionWindow = m_aViewsWindow.getSectionWindow( _xReportComponent );
312     m_pView->UpdatePropertyBrowserDelayed(pSectionWindow->getReportSection().getSectionView());
313 }
314 //------------------------------------------------------------------------------
315 sal_Bool OReportWindow::handleKeyEvent(const KeyEvent& _rEvent)
316 {
317 	return m_aViewsWindow.handleKeyEvent(_rEvent);
318 }
319 //------------------------------------------------------------------------
320 void OReportWindow::setMarked(OSectionView* _pSectionView,sal_Bool _bMark)
321 {
322 	if ( _pSectionView )
323 		m_aViewsWindow.setMarked(_pSectionView,_bMark);
324 }
325 //------------------------------------------------------------------------
326 void OReportWindow::setMarked(const uno::Reference< report::XSection>& _xSection,sal_Bool _bMark)
327 {
328 
329 	m_aViewsWindow.setMarked(_xSection,_bMark);
330 }
331 //------------------------------------------------------------------------
332 void OReportWindow::setMarked(const uno::Sequence< uno::Reference< report::XReportComponent> >& _xShape,sal_Bool _bMark)
333 {
334 
335 	m_aViewsWindow.setMarked(_xShape,_bMark);
336 }
337 //-----------------------------------------------------------------------------
338 ::boost::shared_ptr<OSectionWindow> OReportWindow::getMarkedSection(NearSectionAccess nsa) const
339 {
340 	return  m_aViewsWindow.getMarkedSection(nsa);
341 }
342 //-----------------------------------------------------------------------------
343 ::boost::shared_ptr<OSectionWindow> OReportWindow::getSectionWindow(const ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection>& _xSection) const
344 {
345 	return  m_aViewsWindow.getSectionWindow(_xSection);
346 }
347 // -------------------------------------------------------------------------
348 void OReportWindow::markSection(const sal_uInt16 _nPos)
349 {
350 
351     m_aViewsWindow.markSection(_nPos);
352 }
353 // -----------------------------------------------------------------------------
354 void OReportWindow::fillCollapsedSections(::std::vector<sal_uInt16>& _rCollapsedPositions) const
355 {
356 
357     m_aViewsWindow.fillCollapsedSections(_rCollapsedPositions);
358 }
359 // -----------------------------------------------------------------------------
360 void OReportWindow::collapseSections(const uno::Sequence< ::com::sun::star::beans::PropertyValue>& _aCollpasedSections)
361 {
362 
363     m_aViewsWindow.collapseSections(_aCollpasedSections);
364 }
365 // -----------------------------------------------------------------------------
366 void OReportWindow::alignMarkedObjects(sal_Int32 _nControlModification,bool _bAlignAtSection, bool bBoundRects)
367 {
368 
369     m_aViewsWindow.alignMarkedObjects(_nControlModification, _bAlignAtSection, bBoundRects);
370 }
371 // -----------------------------------------------------------------------------
372 void OReportWindow::setGridSnap(sal_Bool bOn)
373 {
374 
375     m_aViewsWindow.setGridSnap(bOn);
376 }
377 // -----------------------------------------------------------------------------
378 void OReportWindow::setDragStripes(sal_Bool bOn)
379 {
380     m_aViewsWindow.setDragStripes(bOn);
381 }
382 // -----------------------------------------------------------------------------
383 sal_uInt32 OReportWindow::getMarkedObjectCount() const
384 {
385     return m_aViewsWindow.getMarkedObjectCount();
386 }
387 // -----------------------------------------------------------------------------
388 void OReportWindow::zoom(const Fraction& _aZoom)
389 {
390     m_aHRuler.SetZoom(_aZoom);
391     m_aHRuler.Invalidate();
392 
393     //setZoomFactor(_aZoom,*this); // if this will be include the H - ruler has the wrong size
394     m_aViewsWindow.zoom(_aZoom);
395 
396     notifySizeChanged();
397     const Point aNewThumbPos( m_pParent->getThumbPos() );
398 
399     ScrollChildren( aNewThumbPos );
400     Resize();
401 
402     Invalidate(INVALIDATE_NOERASE | INVALIDATE_NOCHILDREN | INVALIDATE_TRANSPARENT);
403 }
404 // -----------------------------------------------------------------------------
405 void OReportWindow::fillControlModelSelection(::std::vector< uno::Reference< uno::XInterface > >& _rSelection) const
406 {
407     m_aViewsWindow.fillControlModelSelection(_rSelection);
408 }
409 // -----------------------------------------------------------------------------
410 sal_Int32 OReportWindow::impl_getRealPixelWidth() const
411 {
412     const sal_Int32 nPaperWidth = getStyleProperty<awt::Size>(m_pView->getController().getReportDefinition(),PROPERTY_PAPERSIZE).Width;
413     MapMode aMap( MAP_100TH_MM );
414     const Size aPageSize = LogicToPixel(Size(nPaperWidth,0),aMap);
415     return aPageSize.Width() + REPORT_ENDMARKER_WIDTH + REPORT_STARTMARKER_WIDTH + SECTION_OFFSET;
416 }
417 // -----------------------------------------------------------------------------
418 sal_uInt16 OReportWindow::getZoomFactor(SvxZoomType _eType) const
419 {
420     sal_uInt16 nZoom(100);
421     const Size aSize( GetSizePixel() );
422     switch( _eType)
423     {
424         case SVX_ZOOM_PERCENT:
425             nZoom = m_pView->getController().getZoomValue();
426             break;
427         case SVX_ZOOM_OPTIMAL:
428             break;
429         case SVX_ZOOM_WHOLEPAGE:
430             {
431                 nZoom = (sal_uInt16)(long)Fraction(aSize.Width()*100,impl_getRealPixelWidth());
432                 MapMode aMap( MAP_100TH_MM );
433                 const Size aHeight = m_aViewsWindow.LogicToPixel(m_aViewsWindow.PixelToLogic(Size(0,GetTotalHeight() + m_aHRuler.GetSizePixel().Height())),aMap);
434                 nZoom = ::std::min(nZoom,(sal_uInt16)(long)Fraction(aSize.Height()*100,aHeight.Height()));
435             }
436             break;
437         case SVX_ZOOM_PAGEWIDTH:
438             nZoom = (sal_uInt16)(long)Fraction(aSize.Width()*100,impl_getRealPixelWidth());
439             break;
440         default:
441             break;
442     }
443 
444     return nZoom;
445 }
446 // -----------------------------------------------------------------------------
447 void OReportWindow::_propertyChanged(const beans::PropertyChangeEvent& _rEvent) throw( uno::RuntimeException)
448 {
449     (void)_rEvent;
450     Resize();
451     m_aViewsWindow.Resize();
452     static sal_Int32 nIn = INVALIDATE_TRANSPARENT;
453     Invalidate(nIn);
454 }
455 //==================================================================
456 }	//rptui
457 //==================================================================
458 
459