sdext: adapt xpdfwrapper to poppler 24.12
[LibreOffice.git] / vcl / source / control / prgsbar.cxx
blobf4727b97ad6e42360aa0f02fa80f72af343183c1
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 .
20 #include <vcl/event.hxx>
21 #include <vcl/status.hxx>
22 #include <vcl/toolkit/prgsbar.hxx>
23 #include <vcl/settings.hxx>
24 #include <sal/log.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/idle.hxx>
27 #include <tools/json_writer.hxx>
29 #define PROGRESSBAR_OFFSET 3
30 #define PROGRESSBAR_WIN_OFFSET 2
32 void ProgressBar::ImplInit()
34 mnPrgsWidth = 0;
35 mnPrgsHeight = 0;
36 mnPercent = 0;
37 mnPercentCount = 0;
38 mbCalcNew = true;
39 SetType(WindowType::PROGRESSBAR);
41 ImplInitSettings( true, true, true );
44 static WinBits clearProgressBarBorder( vcl::Window const * pParent, WinBits nOrgStyle, ProgressBar::BarStyle eBarStyle )
46 WinBits nOutStyle = nOrgStyle;
47 if( pParent && (nOrgStyle & WB_BORDER) != 0 )
49 if (pParent->IsNativeControlSupported(eBarStyle == ProgressBar::BarStyle::Progress
50 ? ControlType::Progress
51 : ControlType::LevelBar,
52 ControlPart::Entire))
53 nOutStyle &= WB_BORDER;
55 return nOutStyle;
58 Size ProgressBar::GetOptimalSize() const
60 return meBarStyle == BarStyle::Progress ? Size(150, 20) : Size(150,10);
63 ProgressBar::ProgressBar( vcl::Window* pParent, WinBits nWinStyle, BarStyle eBarStyle ) :
64 Window( pParent, clearProgressBarBorder( pParent, nWinStyle, eBarStyle ) ),
65 meBarStyle(eBarStyle)
67 SetOutputSizePixel( GetOptimalSize() );
68 ImplInit();
71 void ProgressBar::ImplInitSettings( bool bFont,
72 bool bForeground, bool bBackground )
74 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
76 /* FIXME: !!! We do not support text output at the moment
77 if ( bFont )
78 ApplyControlFont(*this, rStyleSettings.GetAppFont());
81 if ( bBackground )
83 if (!IsControlBackground()
84 && IsNativeControlSupported(meBarStyle == BarStyle::Progress ? ControlType::Progress
85 : ControlType::LevelBar,
86 ControlPart::Entire))
88 if( GetStyle() & WB_BORDER )
89 SetBorderStyle( WindowBorderStyle::REMOVEBORDER );
90 EnableChildTransparentMode();
91 SetPaintTransparent( true );
92 SetBackground();
93 SetParentClipMode( ParentClipMode::NoClip );
95 else
97 Color aColor;
98 if ( IsControlBackground() )
99 aColor = GetControlBackground();
100 else
101 aColor = rStyleSettings.GetFaceColor();
102 SetBackground( aColor );
106 if ( !(bForeground || bFont) )
107 return;
109 Color aColor = rStyleSettings.GetHighlightColor();
110 if ( IsControlForeground() )
111 aColor = GetControlForeground();
112 if ( aColor.IsRGBEqual( GetBackground().GetColor() ) )
114 if ( aColor.GetLuminance() > 100 )
115 aColor.DecreaseLuminance( 64 );
116 else
117 aColor.IncreaseLuminance( 64 );
119 GetOutDev()->SetLineColor();
120 GetOutDev()->SetFillColor( aColor );
121 /* FIXME: !!! We do not support text output at the moment
122 SetTextColor( aColor );
123 SetTextFillColor();
127 void ProgressBar::ImplDrawProgress(vcl::RenderContext& rRenderContext, sal_uInt16 nNewPerc)
129 if (mbCalcNew)
131 mbCalcNew = false;
133 Size aSize(GetOutputSizePixel());
134 mnPrgsHeight = aSize.Height() - (PROGRESSBAR_WIN_OFFSET * 2);
135 mnPrgsWidth = (mnPrgsHeight * 2) / 3;
136 maPos.setY( PROGRESSBAR_WIN_OFFSET );
137 tools::Long nMaxWidth = aSize.Width() - (PROGRESSBAR_WIN_OFFSET * 2) + PROGRESSBAR_OFFSET;
138 sal_uInt16 nMaxCount = static_cast<sal_uInt16>(nMaxWidth / (mnPrgsWidth+PROGRESSBAR_OFFSET));
139 if (nMaxCount <= 1)
141 nMaxCount = 1;
143 else
145 while (((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) > nMaxWidth)
147 nMaxCount--;
150 mnPercentCount = 10000 / nMaxCount;
151 nMaxWidth = ((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) - PROGRESSBAR_OFFSET;
152 maPos.setX( (aSize.Width() - nMaxWidth) / 2 );
155 ::DrawProgress(
156 this, rRenderContext, maPos, PROGRESSBAR_OFFSET, mnPrgsWidth, mnPrgsHeight,
157 /*nPercent1=*/0, nNewPerc * 100, mnPercentCount, tools::Rectangle(Point(), GetSizePixel()),
158 meBarStyle == BarStyle::Progress ? ControlType::Progress : ControlType::LevelBar);
161 void ProgressBar::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
163 ImplDrawProgress(rRenderContext, mnPercent);
166 void ProgressBar::Resize()
168 mbCalcNew = true;
169 if ( IsReallyVisible() )
170 Invalidate();
173 void ProgressBar::SetValue( sal_uInt16 nNewPercent )
175 SAL_WARN_IF( nNewPercent > 100, "vcl", "StatusBar::SetProgressValue(): nPercent > 100" );
177 if ( nNewPercent < mnPercent )
179 mbCalcNew = true;
180 mnPercent = nNewPercent;
181 if ( IsReallyVisible() )
183 Invalidate();
184 PaintImmediately();
187 else if ( mnPercent != nNewPercent )
189 mnPercent = nNewPercent;
190 Invalidate();
192 // Make sure the progressbar is actually painted even if the caller is busy with its task,
193 // so the main loop would not be invoked.
194 Idle aIdle("ProgressBar::SetValue aIdle");
195 aIdle.SetPriority(TaskPriority::POST_PAINT);
196 aIdle.Start();
197 while (aIdle.IsActive() && !Application::IsQuit())
199 Application::Yield();
204 void ProgressBar::StateChanged( StateChangedType nType )
206 /* FIXME: !!! We do not support text output at the moment
207 if ( (nType == StateChangedType::Zoom) ||
208 (nType == StateChangedType::ControlFont) )
210 ImplInitSettings( true, false, false );
211 Invalidate();
213 else
215 if ( nType == StateChangedType::ControlForeground )
217 ImplInitSettings( false, true, false );
218 Invalidate();
220 else if ( nType == StateChangedType::ControlBackground )
222 ImplInitSettings( false, false, true );
223 Invalidate();
226 Window::StateChanged( nType );
229 void ProgressBar::DataChanged( const DataChangedEvent& rDCEvt )
231 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
232 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
234 ImplInitSettings( true, true, true );
235 Invalidate();
238 Window::DataChanged( rDCEvt );
241 void ProgressBar::DumpAsPropertyTree(tools::JsonWriter& rJsonWriter)
243 vcl::Window::DumpAsPropertyTree(rJsonWriter);
244 rJsonWriter.put("value", mnPercent);
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */