Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/control/prgsbar.cxx
Line
Count
Source
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/salnativewidgets.hxx>
22
#include <vcl/status.hxx>
23
#include <vcl/toolkit/prgsbar.hxx>
24
#include <vcl/settings.hxx>
25
#include <sal/log.hxx>
26
#include <vcl/svapp.hxx>
27
#include <vcl/idle.hxx>
28
#include <tools/json_writer.hxx>
29
30
0
#define PROGRESSBAR_OFFSET          3
31
0
#define PROGRESSBAR_WIN_OFFSET      2
32
33
static WinBits clearProgressBarBorder( vcl::Window const * pParent, WinBits nOrgStyle, ProgressBar::BarStyle eBarStyle )
34
0
{
35
0
    WinBits nOutStyle = nOrgStyle;
36
0
    if( pParent && (nOrgStyle & WB_BORDER) != 0 )
37
0
    {
38
0
        if (pParent->IsNativeControlSupported(eBarStyle == ProgressBar::BarStyle::Progress
39
0
                                                  ? ControlType::Progress
40
0
                                                  : ControlType::LevelBar,
41
0
                                              ControlPart::Entire))
42
0
            nOutStyle &= WB_BORDER;
43
0
    }
44
0
    return nOutStyle;
45
0
}
46
47
Size ProgressBar::GetOptimalSize() const
48
0
{
49
0
    return meBarStyle == BarStyle::Progress ? Size(150, 20) : Size(150,10);
50
0
}
51
52
ProgressBar::ProgressBar( vcl::Window* pParent, WinBits nWinStyle, BarStyle eBarStyle ) :
53
0
        Window( pParent, clearProgressBarBorder( pParent, nWinStyle, eBarStyle ) ),
54
0
        meBarStyle(eBarStyle)
55
0
{
56
0
    SetOutputSizePixel( GetOptimalSize() );
57
58
0
    mnPrgsWidth = 0;
59
0
    mnPrgsHeight = 0;
60
0
    mnPercent = 0;
61
0
    mnPercentCount = 0;
62
0
    mbCalcNew = true;
63
0
    SetType(WindowType::PROGRESSBAR);
64
65
0
    ImplInitSettings(true, true, true);
66
0
}
Unexecuted instantiation: ProgressBar::ProgressBar(vcl::Window*, long, ProgressBar::BarStyle)
Unexecuted instantiation: ProgressBar::ProgressBar(vcl::Window*, long, ProgressBar::BarStyle)
67
68
void ProgressBar::ImplInitSettings( bool bFont,
69
                                    bool bForeground, bool bBackground )
70
0
{
71
0
    const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
72
73
/* FIXME: !!! We do not support text output at the moment
74
    if ( bFont )
75
        ApplyControlFont(*this, rStyleSettings.GetAppFont());
76
*/
77
78
0
    if ( bBackground )
79
0
    {
80
0
        if (!IsControlBackground()
81
0
            && IsNativeControlSupported(meBarStyle == BarStyle::Progress ? ControlType::Progress
82
0
                                                                         : ControlType::LevelBar,
83
0
                                        ControlPart::Entire))
84
0
        {
85
0
            if( GetStyle() & WB_BORDER )
86
0
                SetBorderStyle( WindowBorderStyle::REMOVEBORDER );
87
0
            EnableChildTransparentMode();
88
0
            SetPaintTransparent( true );
89
0
            SetBackground();
90
0
            SetParentClipMode( ParentClipMode::NoClip );
91
0
        }
92
0
        else
93
0
        {
94
0
            Color aColor;
95
0
            if ( IsControlBackground() )
96
0
                aColor = GetControlBackground();
97
0
            else
98
0
                aColor = rStyleSettings.GetFaceColor();
99
0
            SetBackground( aColor );
100
0
        }
101
0
    }
102
103
0
    if ( !(bForeground || bFont) )
104
0
        return;
105
106
0
    Color aColor = rStyleSettings.GetHighlightColor();
107
0
    if ( IsControlForeground() )
108
0
        aColor = GetControlForeground();
109
0
    if ( aColor.IsRGBEqual( GetBackground().GetColor() ) )
110
0
    {
111
0
        if ( aColor.GetLuminance() > 100 )
112
0
            aColor.DecreaseLuminance( 64 );
113
0
        else
114
0
            aColor.IncreaseLuminance( 64 );
115
0
    }
116
0
    GetOutDev()->SetLineColor();
117
0
    GetOutDev()->SetFillColor( aColor );
118
/* FIXME: !!! We do not support text output at the moment
119
    SetTextColor( aColor );
120
    SetTextFillColor();
121
*/
122
0
}
123
124
void ProgressBar::ImplDrawProgress(vcl::RenderContext& rRenderContext, sal_uInt16 nNewPerc)
125
0
{
126
0
    if (mbCalcNew)
127
0
    {
128
0
        mbCalcNew = false;
129
130
0
        Size aSize(GetOutputSizePixel());
131
0
        mnPrgsHeight = aSize.Height() - (PROGRESSBAR_WIN_OFFSET * 2);
132
0
        mnPrgsWidth = (mnPrgsHeight * 2) / 3;
133
0
        maPos.setY( PROGRESSBAR_WIN_OFFSET );
134
0
        tools::Long nMaxWidth = aSize.Width() - (PROGRESSBAR_WIN_OFFSET * 2) + PROGRESSBAR_OFFSET;
135
0
        sal_uInt16 nMaxCount = static_cast<sal_uInt16>(nMaxWidth / (mnPrgsWidth+PROGRESSBAR_OFFSET));
136
0
        if (nMaxCount <= 1)
137
0
        {
138
0
            nMaxCount = 1;
139
0
        }
140
0
        else
141
0
        {
142
0
            while (((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) > nMaxWidth)
143
0
            {
144
0
                nMaxCount--;
145
0
            }
146
0
        }
147
0
        mnPercentCount = 10000 / nMaxCount;
148
0
        nMaxWidth = ((10000 / (10000 / nMaxCount)) * (mnPrgsWidth + PROGRESSBAR_OFFSET)) - PROGRESSBAR_OFFSET;
149
0
        maPos.setX( (aSize.Width() - nMaxWidth) / 2 );
150
0
    }
151
152
0
    ::DrawProgress(
153
0
        this, rRenderContext, maPos, PROGRESSBAR_OFFSET, mnPrgsWidth, mnPrgsHeight,
154
0
        /*nPercent1=*/0, nNewPerc * 100, mnPercentCount, tools::Rectangle(Point(), GetSizePixel()),
155
0
        meBarStyle == BarStyle::Progress ? ControlType::Progress : ControlType::LevelBar);
156
0
}
157
158
void ProgressBar::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
159
0
{
160
0
    ImplDrawProgress(rRenderContext, mnPercent);
161
0
}
162
163
void ProgressBar::Resize()
164
0
{
165
0
    mbCalcNew = true;
166
0
    if ( IsReallyVisible() )
167
0
        Invalidate();
168
0
}
169
170
void ProgressBar::SetValue( sal_uInt16 nNewPercent )
171
0
{
172
0
    SAL_WARN_IF( nNewPercent > 100, "vcl", "StatusBar::SetProgressValue(): nPercent > 100" );
173
174
0
    if ( nNewPercent < mnPercent )
175
0
    {
176
0
        mbCalcNew = true;
177
0
        mnPercent = nNewPercent;
178
0
        if ( IsReallyVisible() )
179
0
        {
180
0
            Invalidate();
181
0
            PaintImmediately();
182
0
        }
183
0
    }
184
0
    else if ( mnPercent != nNewPercent )
185
0
    {
186
0
        mnPercent = nNewPercent;
187
0
        Invalidate();
188
189
        // Make sure the progressbar is actually painted even if the caller is busy with its task,
190
        // so the main loop would not be invoked.
191
0
        Idle aIdle("ProgressBar::SetValue aIdle");
192
0
        aIdle.SetPriority(TaskPriority::POST_PAINT);
193
0
        aIdle.Start();
194
0
        while (aIdle.IsActive() && !Application::IsQuit())
195
0
        {
196
0
            Application::Yield();
197
0
        }
198
0
    }
199
0
}
200
201
void ProgressBar::StateChanged( StateChangedType nType )
202
0
{
203
/* FIXME: !!! We do not support text output at the moment
204
    if ( (nType == StateChangedType::Zoom) ||
205
         (nType == StateChangedType::ControlFont) )
206
    {
207
        ImplInitSettings( true, false, false );
208
        Invalidate();
209
    }
210
    else
211
*/
212
0
    if ( nType == StateChangedType::ControlForeground )
213
0
    {
214
0
        ImplInitSettings( false, true, false );
215
0
        Invalidate();
216
0
    }
217
0
    else if ( nType == StateChangedType::ControlBackground )
218
0
    {
219
0
        ImplInitSettings( false, false, true );
220
0
        Invalidate();
221
0
    }
222
223
0
    Window::StateChanged( nType );
224
0
}
225
226
void ProgressBar::DataChanged( const DataChangedEvent& rDCEvt )
227
0
{
228
0
    if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
229
0
         (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
230
0
    {
231
0
        ImplInitSettings( true, true, true );
232
0
        Invalidate();
233
0
    }
234
235
0
    Window::DataChanged( rDCEvt );
236
0
}
237
238
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */