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