Debian Codegraph @

components/KASToolBar/kasprogressbar.pas

raw
1 {
2 Double Commander Components
3 -------------------------------------------------------------------------
4 Extended ProgressBar class
5
6 Copyright (C) 2010 Przemyslaw Nagay ([email protected])
7 Copyright (C) 2011-2018 Alexander Koblov ([email protected])
8
9 Windows 7 implementation based on "Windows 7 Component Library"
10 by Daniel Wischnewski (http://www.gumpi.com/blog)
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 }
25
26 unit KASProgressBar;
27
28 {$mode objfpc}{$H+}
29
30 interface
31
32 uses
33 LCLType, Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ComCtrls
34 {$IFDEF LCLWIN32}
35 , InterfaceBase, ComObj, LMessages, Windows, Themes, dwTaskbarList
36 {$ENDIF}
37 {$IFDEF LCLGTK2}
38 , Gtk2
39 {$ENDIF}
40 {$IFDEF LCLQT}
41 , qt4, qtwidgets
42 {$ENDIF}
43 {$IFDEF LCLQT5}
44 , qt5, qtwidgets
45 {$ENDIF}
46 {$IFDEF LCLQT6}
47 , qt6, qtwidgets
48 {$ENDIF}
49 ;
50
51 type
52
53 { TKASProgressBar }
54
55 TKASProgressBar = class(TProgressBar)
56 private
57 FShowInTaskbar: Boolean;
58 {$IFDEF LCLWIN32}
59 FBarText: String;
60 FTaskBarEntryHandle: HWND;
61 FTaskbarList: ITaskbarList;
62 FTaskbarList3: ITaskbarList3;
63 {$ENDIF}
64 protected
65 {$IFDEF LCLWIN32}
66 procedure InitializeWnd; override;
67 procedure WMPaint(var Msg: TLMPaint); message LM_PAINT;
68 {$ENDIF}
69 procedure DoOnResize; override;
70 public
71 constructor Create(AOwner: TComponent); override;
72
73 procedure SetProgress(CurrentValue: Int64; MaxValue: Int64; BarText: String = '');
74 published
75 property ShowInTaskbar: Boolean read FShowInTaskbar write FShowInTaskbar default False;
76 end;
77
78 procedure Register;
79
80 implementation
81
82 procedure Register;
83 begin
84 RegisterComponents('KASComponents',[TKASProgressBar]);
85 end;
86
87 { TKASProgressBar }
88
89 {$IFDEF LCLWIN32}
90 procedure TKASProgressBar.InitializeWnd;
91 var
92 aOwnerForm: TWinControl;
93 begin
94 inherited InitializeWnd;
95 if CheckWin32Version(6, 1) then
96 begin
97 aOwnerForm:= GetParentForm(Self);
98 if Assigned(aOwnerForm) and (aOwnerForm <> Application.MainForm) then
99 FTaskBarEntryHandle := aOwnerForm.Handle
100 else
101 FTaskBarEntryHandle := Widgetset.AppHandle;
102 end;
103 BarShowText:= BarShowText and CheckWin32Version(8);
104 end;
105
106 procedure TKASProgressBar.WMPaint(var Msg: TLMPaint);
107 var
108 OldFont: HFONT;
109 OldBkMode: Integer;
110 Details: TThemedElementDetails;
111 begin
112 inherited WMPaint(Msg);
113
114 if BarShowText and ThemeServices.ThemesEnabled then
115 begin
116 OldBkMode:= SetBkMode(Msg.DC, TRANSPARENT);
117 Details:= ThemeServices.GetElementDetails(tpBar);
118 OldFont:= SelectObject(Msg.DC, Font.Reference.Handle);
119 ThemeServices.DrawText(Msg.DC, Details, FBarText, Msg.PaintStruct^.rcPaint, DT_SINGLELINE or DT_CENTER or DT_VCENTER, 0);
120 SelectObject(Msg.DC, OldFont);
121 SetBkMode(Msg.DC, OldBkMode);
122 end;
123 end;
124 {$ENDIF}
125
126 procedure TKASProgressBar.DoOnResize;
127 begin
128 inherited;
129 Max := Width;
130 end;
131
132 constructor TKASProgressBar.Create(AOwner: TComponent);
133 begin
134 inherited Create(AOwner);
135
136 {$IFDEF LCLWIN32}
137 FTaskbarList3 := nil;
138 FTaskBarEntryHandle := INVALID_HANDLE_VALUE;
139 // Works only under Windows 7 and higher
140 if CheckWin32Version(6, 1) then
141 try
142 FTaskbarList := ITaskbarList(CreateComObject(CLSID_TaskbarList));
143 FTaskbarList.HrInit;
144 FTaskbarList.QueryInterface(CLSID_TaskbarList3, FTaskbarList3);
145 except
146 FTaskbarList3 := nil;
147 end;
148 {$ENDIF}
149
150 {$IFDEF LCLGTK2}
151 // Have to disable LCLGTK2 default progress bar text
152 // set in TGtk2WSProgressBar.UpdateProgressBarText.
153 BarShowText := False;
154 {$ENDIF}
155 end;
156
157 procedure TKASProgressBar.SetProgress(CurrentValue: Int64; MaxValue: Int64;
158 BarText: String);
159 {$IFDEF LCLGTK2}
160 var
161 wText: String;
162 {$ENDIF}
163 {$IF DEFINED(LCLQT) OR DEFINED(LCLQT5) OR DEFINED(LCLQT6)}
164 var
165 wText: WideString;
166 {$ENDIF}
167 begin
168 if MaxValue <> 0 then
169 Position := Round(CurrentValue * Max / MaxValue)
170 else
171 Position := 0;
172
173 {$IFDEF LCLWIN32}
174 if BarShowText then
175 begin
176 if MaxValue = 0 then
177 FBarText := BarText
178 else if BarText = '' then
179 FBarText := FloatToStrF((CurrentValue / MaxValue) * 100, ffFixed, 0, 0) + '%'
180 else
181 FBarText := BarText + ' (' + FloatToStrF((CurrentValue / MaxValue) * 100, ffFixed, 0, 0) + '%)';
182 end;
183
184 if FShowInTaskbar and (FTaskBarEntryHandle <> INVALID_HANDLE_VALUE) and Assigned(FTaskbarList3) then
185 begin
186 FTaskbarList3.SetProgressValue(FTaskBarEntryHandle, Position, Max);
187 end;
188 {$ENDIF}
189
190 {$IFDEF LCLGTK2}
191 {
192 %v - the current progress value.
193 %l - the lower bound for the progress value.
194 %u - the upper bound for the progress value.
195 %p - the current progress percentage.
196 }
197 if BarText <> '' then
198 wText := BarText + ' (%p%%)'
199 else
200 wText := '%p%%';
201 gtk_progress_set_format_string(PGtkProgress(Self.Handle), PChar(wText));
202 // Have to reset 'show_text' every time because LCLGTK2 will set it according to BarShowText.
203 gtk_progress_set_show_text(PGtkProgress(Self.Handle), True);
204 {$ENDIF}
205 {$IF DEFINED(LCLQT) OR DEFINED(LCLQT5) OR DEFINED(LCLQT6)}
206 {
207 %p - is replaced by the percentage completed.
208 %v - is replaced by the current value.
209 %m - is replaced by the total number of steps.
210 }
211 if BarText <> '' then
212 wText := WideString(BarText) + ' (%p%)'
213 else
214 wText := '%p%';
215 QProgressBar_setFormat(QProgressBarH(TQtProgressBar(Self.Handle).Widget), @wText);
216 //QProgressBar_setTextVisible(QProgressBarH(TQtProgressBar(Self.Handle).Widget), True);
217 {$ENDIF}
218 end;
219
220 end.