Use o3tl::convert in Math
[LibreOffice.git] / reportdesign / source / ui / report / StartMarker.cxx
blob150f7aff51562d160dba21ee46fe8154ed4eadbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <StartMarker.hxx>
20 #include <UITools.hxx>
21 #include <vcl/image.hxx>
22 #include <vcl/svapp.hxx>
23 #include <bitmaps.hlst>
24 #include <ColorChanger.hxx>
25 #include <ReportDefines.hxx>
26 #include <SectionWindow.hxx>
27 #include <helpids.h>
28 #include <vcl/event.hxx>
29 #include <vcl/help.hxx>
30 #include <vcl/gradient.hxx>
31 #include <vcl/lineinfo.hxx>
32 #include <vcl/settings.hxx>
34 #include <unotools/syslocale.hxx>
35 #include <unotools/localedatawrapper.hxx>
37 #define CORNER_SPACE 5
40 namespace rptui
44 Image* OStartMarker::s_pDefCollapsed = nullptr;
45 Image* OStartMarker::s_pDefExpanded = nullptr;
46 oslInterlockedCount OStartMarker::s_nImageRefCount = 0;
49 OStartMarker::OStartMarker(OSectionWindow* _pParent,const OUString& _sColorEntry)
50 : OColorListener(_pParent,_sColorEntry)
51 , m_aVRuler(VclPtr<Ruler>::Create(this,WB_VERT))
52 , m_pParent(_pParent)
53 , m_bShowRuler(true)
55 osl_atomic_increment(&s_nImageRefCount);
56 initDefaultNodeImages();
57 ImplInitSettings();
58 m_aVRuler->Show();
59 m_aVRuler->Activate();
60 m_aVRuler->SetPagePos();
61 m_aVRuler->SetBorders();
62 m_aVRuler->SetIndents();
63 m_aVRuler->SetMargin1();
64 m_aVRuler->SetMargin2();
65 const MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
66 m_aVRuler->SetUnit(MeasurementSystem::Metric == eSystem ? FieldUnit::CM : FieldUnit::INCH);
67 EnableChildTransparentMode();
68 SetParentClipMode( ParentClipMode::NoClip );
69 SetPaintTransparent( true );
72 OStartMarker::~OStartMarker()
74 disposeOnce();
77 void OStartMarker::dispose()
79 if ( osl_atomic_decrement(&s_nImageRefCount) == 0 )
81 delete s_pDefCollapsed;
82 s_pDefCollapsed = nullptr;
83 delete s_pDefExpanded;
84 s_pDefExpanded = nullptr;
86 m_aVRuler.disposeAndClear();
87 m_pParent.clear();
88 OColorListener::dispose();
91 sal_Int32 OStartMarker::getMinHeight() const
93 Fraction aExtraWidth(tools::Long(2 * REPORT_EXTRA_SPACE));
94 aExtraWidth *= GetMapMode().GetScaleX();
95 return LogicToPixel(Size(0, GetTextHeight())).Height() + tools::Long(aExtraWidth);
98 void OStartMarker::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
100 rRenderContext.Push(vcl::PushFlags::TEXTCOLOR);
102 Size aSize(GetOutputSizePixel());
103 const tools::Long nCornerWidth = tools::Long(CORNER_SPACE * double(GetMapMode().GetScaleX()));
105 if (isCollapsed())
107 rRenderContext.SetClipRegion();
109 else
111 const tools::Long nVRulerWidth = m_aVRuler->GetSizePixel().Width();
112 tools::Long nSize = aSize.Width() - nVRulerWidth;
113 aSize.AdjustWidth(nCornerWidth );
114 rRenderContext.SetClipRegion(vcl::Region(rRenderContext.PixelToLogic(tools::Rectangle(Point(),
115 Size(nSize, aSize.Height())))));
118 tools::Rectangle aWholeRect(Point(), aSize);
120 const ColorChanger aColors(&rRenderContext, m_nTextBoundaries, m_nColor);
121 tools::PolyPolygon aPoly;
122 aPoly.Insert( tools::Polygon(aWholeRect,nCornerWidth,nCornerWidth));
124 Color aStartColor(m_nColor);
125 aStartColor.IncreaseLuminance(10);
126 sal_uInt16 nHue = 0;
127 sal_uInt16 nSat = 0;
128 sal_uInt16 nBri = 0;
129 aStartColor.RGBtoHSB(nHue, nSat, nBri);
130 nSat += 40;
131 Color aEndColor(Color::HSBtoRGB(nHue, nSat, nBri));
132 Gradient aGradient(GradientStyle::Linear,aStartColor,aEndColor);
133 aGradient.SetSteps(static_cast<sal_uInt16>(aSize.Height()));
135 rRenderContext.DrawGradient(PixelToLogic(aPoly) ,aGradient);
138 rRenderContext.Push(vcl::PushFlags::MAPMODE);
139 rRenderContext.SetMapMode();
141 rRenderContext.DrawImage(m_aImageRect.TopLeft(), m_aImageRect.GetSize(), m_aImage);
143 const Color aColor(m_nColor);
144 Color aTextColor = GetTextColor();
145 if (aColor.GetLuminance() < 128)
146 aTextColor = COL_WHITE;
147 rRenderContext.SetTextColor(aTextColor);
149 rRenderContext.DrawText(m_aTextRect, m_aText, DrawTextFlags::MultiLine | DrawTextFlags::WordBreakHyphenation);
151 rRenderContext.Pop();
153 if (m_bMarked)
155 const tools::Long nCornerHeight = tools::Long(CORNER_SPACE * double(GetMapMode().GetScaleY()));
156 tools::Rectangle aRect(Point(nCornerWidth, nCornerHeight),
157 Size(aSize.Width() - nCornerWidth - nCornerWidth,
158 aSize.Height() - nCornerHeight - nCornerHeight));
159 ColorChanger aColors(&rRenderContext, COL_WHITE, COL_WHITE);
160 rRenderContext.DrawPolyLine( tools::Polygon(rRenderContext.PixelToLogic(aRect)),
161 LineInfo(LineStyle::Solid, 2));
164 rRenderContext.Pop();
167 void OStartMarker::MouseButtonUp( const MouseEvent& rMEvt )
169 if ( !rMEvt.IsLeft() )
170 return;
172 Point aPos( rMEvt.GetPosPixel());
174 const Size aOutputSize = GetOutputSizePixel();
175 if( aPos.X() > aOutputSize.Width() || aPos.Y() > aOutputSize.Height() )
176 return;
177 if ( rMEvt.GetClicks() == 2 || m_aImageRect.Contains( aPos ) )
179 m_bCollapsed = !m_bCollapsed;
181 changeImage();
183 m_aVRuler->Show(!m_bCollapsed && m_bShowRuler);
184 m_aCollapsedLink.Call(*this);
187 m_pParent->showProperties();
190 void OStartMarker::changeImage()
192 m_aImage = m_bCollapsed ? *s_pDefCollapsed : *s_pDefExpanded;
195 void OStartMarker::initDefaultNodeImages()
197 if ( !s_pDefCollapsed )
199 s_pDefCollapsed = new Image(StockImage::Yes, RID_BMP_TREENODE_COLLAPSED);
200 s_pDefExpanded = new Image(StockImage::Yes, RID_BMP_TREENODE_EXPANDED);
203 m_aImage = m_bCollapsed ? *s_pDefCollapsed : *s_pDefExpanded;
206 void OStartMarker::ApplySettings(vcl::RenderContext& rRenderContext)
208 rRenderContext.SetBackground();
209 rRenderContext.SetFillColor(Application::GetSettings().GetStyleSettings().GetDialogColor());
212 void OStartMarker::ImplInitSettings()
214 ApplySettings(*GetOutDev());
217 void OStartMarker::Resize()
219 const Size aOutputSize( GetOutputSizePixel() );
220 const tools::Long nOutputWidth = aOutputSize.Width();
221 const tools::Long nOutputHeight = aOutputSize.Height();
223 const tools::Long nVRulerWidth = m_aVRuler->GetSizePixel().Width();
224 const Point aRulerPos(nOutputWidth - nVRulerWidth,0);
225 m_aVRuler->SetPosSizePixel(aRulerPos,Size(nVRulerWidth,nOutputHeight));
227 Size aImageSize = m_aImage.GetSizePixel();
228 const MapMode& rMapMode = GetMapMode();
229 aImageSize.setWidth( tools::Long(aImageSize.Width() * static_cast<double>(rMapMode.GetScaleX())) );
230 aImageSize.setHeight( tools::Long(aImageSize.Height() * static_cast<double>(rMapMode.GetScaleY())) );
232 tools::Long nExtraWidth = tools::Long(REPORT_EXTRA_SPACE * rMapMode.GetScaleX());
234 Point aPos(aImageSize.Width() + (nExtraWidth * 2), nExtraWidth);
235 const tools::Long nHeight = ::std::max<sal_Int32>(nOutputHeight - 2*aPos.Y(),LogicToPixel(Size(0, GetTextHeight())).Height());
236 m_aTextRect = tools::Rectangle(aPos, Size(aRulerPos.X() - aPos.X(),nHeight));
238 aPos.setX( nExtraWidth );
239 aPos.AdjustY(static_cast<sal_Int32>((LogicToPixel(Size(0, GetTextHeight())).Height() - aImageSize.Height()) * 0.5) ) ;
240 m_aImageRect = tools::Rectangle(aPos, aImageSize);
242 OColorListener::Resize();
245 void OStartMarker::setTitle(const OUString& rTitle)
247 if (m_aText != rTitle)
249 m_aText = rTitle;
250 Invalidate();
254 void OStartMarker::Notify(SfxBroadcaster & rBc, SfxHint const & rHint)
256 OColorListener::Notify(rBc, rHint);
257 if (rHint.GetId() == SfxHintId::ColorsChanged)
259 Invalidate(InvalidateFlags::Children);
263 void OStartMarker::showRuler(bool _bShow)
265 m_bShowRuler = _bShow;
266 m_aVRuler->Show(!m_bCollapsed && m_bShowRuler);
269 void OStartMarker::RequestHelp( const HelpEvent& rHEvt )
271 if (m_aText.isEmpty())
272 return;
274 // show help
275 tools::Rectangle aItemRect(rHEvt.GetMousePosPixel(),Size(GetSizePixel().Width(),getMinHeight()));
276 Point aPt = OutputToScreenPixel( aItemRect.TopLeft() );
277 aItemRect.SetLeft( aPt.X() );
278 aItemRect.SetTop( aPt.Y() );
279 aPt = OutputToScreenPixel( aItemRect.BottomRight() );
280 aItemRect.SetRight( aPt.X() );
281 aItemRect.SetBottom( aPt.Y() );
282 if( rHEvt.GetMode() == HelpEventMode::BALLOON )
283 Help::ShowBalloon( this, aItemRect.Center(), aItemRect, m_aText);
284 else
285 Help::ShowQuickHelp( this, aItemRect, m_aText );
288 void OStartMarker::setCollapsed(bool _bCollapsed)
290 OColorListener::setCollapsed(_bCollapsed);
291 showRuler(_bCollapsed);
292 changeImage();
295 void OStartMarker::zoom(const Fraction& _aZoom)
297 setZoomFactor(_aZoom, *this);
298 m_aVRuler->SetZoom(_aZoom);
299 Resize();
300 Invalidate();
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */