LibreOffice Module vcl (master) 1
prgsbar.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <vcl/event.hxx>
21#include <vcl/status.hxx>
23#include <vcl/settings.hxx>
24#include <sal/log.hxx>
25#include <vcl/svapp.hxx>
26#include <vcl/idle.hxx>
27
28#define PROGRESSBAR_OFFSET 3
29#define PROGRESSBAR_WIN_OFFSET 2
30
31void ProgressBar::ImplInit()
32{
33 mnPrgsWidth = 0;
34 mnPrgsHeight = 0;
35 mnPercent = 0;
36 mnPercentCount = 0;
37 mbCalcNew = true;
38
39 ImplInitSettings( true, true, true );
40}
41
42static WinBits clearProgressBarBorder( vcl::Window const * pParent, WinBits nOrgStyle )
43{
44 WinBits nOutStyle = nOrgStyle;
45 if( pParent && (nOrgStyle & WB_BORDER) != 0 )
46 {
48 nOutStyle &= WB_BORDER;
49 }
50 return nOutStyle;
51}
52
53Size ProgressBar::GetOptimalSize() const
54{
55 return Size(150, 20);
56}
57
58ProgressBar::ProgressBar( vcl::Window* pParent, WinBits nWinStyle ) :
59 Window( pParent, clearProgressBarBorder( pParent, nWinStyle ) )
60{
61 SetOutputSizePixel( GetOptimalSize() );
62 ImplInit();
63}
64
65void ProgressBar::ImplInitSettings( bool bFont,
66 bool bForeground, bool bBackground )
67{
68 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
69
70/* FIXME: !!! We do not support text output at the moment
71 if ( bFont )
72 ApplyControlFont(*this, rStyleSettings.GetAppFont());
73*/
74
75 if ( bBackground )
76 {
77 if( !IsControlBackground() &&
78 IsNativeControlSupported( ControlType::Progress, ControlPart::Entire ) )
79 {
80 if( GetStyle() & WB_BORDER )
81 SetBorderStyle( WindowBorderStyle::REMOVEBORDER );
82 EnableChildTransparentMode();
83 SetPaintTransparent( true );
84 SetBackground();
85 SetParentClipMode( ParentClipMode::NoClip );
86 }
87 else
88 {
89 Color aColor;
90 if ( IsControlBackground() )
91 aColor = GetControlBackground();
92 else
93 aColor = rStyleSettings.GetFaceColor();
94 SetBackground( aColor );
95 }
96 }
97
98 if ( !(bForeground || bFont) )
99 return;
100
101 Color aColor = rStyleSettings.GetHighlightColor();
102 if ( IsControlForeground() )
103 aColor = GetControlForeground();
104 if ( aColor.IsRGBEqual( GetBackground().GetColor() ) )
105 {
106 if ( aColor.GetLuminance() > 100 )
107 aColor.DecreaseLuminance( 64 );
108 else
109 aColor.IncreaseLuminance( 64 );
110 }
111 GetOutDev()->SetLineColor();
112 GetOutDev()->SetFillColor( aColor );
113/* FIXME: !!! We do not support text output at the moment
114 SetTextColor( aColor );
115 SetTextFillColor();
116*/
117}
118
119void ProgressBar::ImplDrawProgress(vcl::RenderContext& rRenderContext, sal_uInt16 nNewPerc)
120{
121 if (mbCalcNew)
122 {
123 mbCalcNew = false;
124
125 Size aSize(GetOutputSizePixel());
126 mnPrgsHeight = aSize.Height() - (PROGRESSBAR_WIN_OFFSET * 2);
127 mnPrgsWidth = (mnPrgsHeight * 2) / 3;
128 maPos.setY( PROGRESSBAR_WIN_OFFSET );
129 tools::Long nMaxWidth = aSize.Width() - (PROGRESSBAR_WIN_OFFSET * 2) + PROGRESSBAR_OFFSET;
130 sal_uInt16 nMaxCount = static_cast<sal_uInt16>(nMaxWidth / (mnPrgsWidth+PROGRESSBAR_OFFSET));
131 if (nMaxCount <= 1)
132 {
133 nMaxCount = 1;
134 }
135 else
136 {
137 while (((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) > nMaxWidth)
138 {
139 nMaxCount--;
140 }
141 }
142 mnPercentCount = 10000 / nMaxCount;
143 nMaxWidth = ((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) - PROGRESSBAR_OFFSET;
144 maPos.setX( (aSize.Width() - nMaxWidth) / 2 );
145 }
146
147 ::DrawProgress(this, rRenderContext, maPos, PROGRESSBAR_OFFSET, mnPrgsWidth, mnPrgsHeight,
148 /*nPercent1=*/0, nNewPerc * 100, mnPercentCount,
149 tools::Rectangle(Point(), GetSizePixel()));
150}
151
152void ProgressBar::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
153{
154 ImplDrawProgress(rRenderContext, mnPercent);
155}
156
157void ProgressBar::Resize()
158{
159 mbCalcNew = true;
160 if ( IsReallyVisible() )
161 Invalidate();
162}
163
164void ProgressBar::SetValue( sal_uInt16 nNewPercent )
165{
166 SAL_WARN_IF( nNewPercent > 100, "vcl", "StatusBar::SetProgressValue(): nPercent > 100" );
167
168 if ( nNewPercent < mnPercent )
169 {
170 mbCalcNew = true;
171 mnPercent = nNewPercent;
172 if ( IsReallyVisible() )
173 {
174 Invalidate();
175 PaintImmediately();
176 }
177 }
178 else if ( mnPercent != nNewPercent )
179 {
180 mnPercent = nNewPercent;
181 Invalidate();
182
183 // Make sure the progressbar is actually painted even if the caller is busy with its task,
184 // so the main loop would not be invoked.
185 Idle aIdle("ProgressBar::SetValue aIdle");
186 aIdle.SetPriority(TaskPriority::POST_PAINT);
187 aIdle.Start();
188 while (aIdle.IsActive() && !Application::IsQuit())
189 {
191 }
192 }
193}
194
195void ProgressBar::StateChanged( StateChangedType nType )
196{
197/* FIXME: !!! We do not support text output at the moment
198 if ( (nType == StateChangedType::Zoom) ||
199 (nType == StateChangedType::ControlFont) )
200 {
201 ImplInitSettings( true, false, false );
202 Invalidate();
203 }
204 else
205*/
207 {
208 ImplInitSettings( false, true, false );
209 Invalidate();
210 }
211 else if ( nType == StateChangedType::ControlBackground )
212 {
213 ImplInitSettings( false, false, true );
214 Invalidate();
215 }
216
217 Window::StateChanged( nType );
218}
219
220void ProgressBar::DataChanged( const DataChangedEvent& rDCEvt )
221{
222 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
223 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
224 {
225 ImplInitSettings( true, true, true );
226 Invalidate();
227 }
228
229 Window::DataChanged( rDCEvt );
230}
231
232/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void Yield()
Process the next event.
Definition: svapp.cxx:551
static bool IsQuit()
Has Quit() been called?
Definition: svapp.cxx:608
sal_uInt8 GetLuminance() const
void DecreaseLuminance(sal_uInt8 cLumDec)
void IncreaseLuminance(sal_uInt8 cLumInc)
bool IsRGBEqual(const Color &rColor) const
DataChangedEventType GetType() const
Definition: event.hxx:362
AllSettingsFlags GetFlags() const
Definition: event.hxx:363
An idle is a timer to be scheduled immediately.
Definition: idle.hxx:35
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:171
const Color & GetHighlightColor() const
const Color & GetFaceColor() const
bool IsNativeControlSupported(ControlType nType, ControlPart nPart) const
Query the platform layer for control support.
Definition: window3.cxx:74
#define SAL_WARN_IF(condition, area, stream)
long Long
static WinBits clearProgressBarBorder(vcl::Window const *pParent, WinBits nOrgStyle)
Definition: prgsbar.cxx:42
#define PROGRESSBAR_WIN_OFFSET
Definition: prgsbar.cxx:29
#define PROGRESSBAR_OFFSET
Definition: prgsbar.cxx:28
void DrawProgress(vcl::Window *pWindow, vcl::RenderContext &rRenderContext, const Point &rPos, tools::Long nOffset, tools::Long nPrgsWidth, tools::Long nPrgsHeight, sal_uInt16 nPercent1, sal_uInt16 nPercent2, sal_uInt16 nPercentCount, const tools::Rectangle &rFramePosSize)
Definition: status.cxx:472
@ POST_PAINT
Everything running directly after painting.
StateChangedType
Definition: window.hxx:291
sal_Int64 WinBits
Definition: wintypes.hxx:109
WinBits const WB_BORDER
Definition: wintypes.hxx:115