-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtils.pas
More file actions
258 lines (224 loc) · 9.37 KB
/
Copy pathUtils.pas
File metadata and controls
258 lines (224 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{****************************************************************************************}
{ PowerOff }
{ ErrorSoft(c) 2016 }
{ }
{ Utility was written, to check the suitability of the FireMonkey technology to develop- }
{ in general - yes, at the moment, under Windows (and to simulate the interface UWP), }
{ it is a workable technology. }
{ }
{ This can be useful utilite or some of the sources, such as FontSizeForBox function. }
{ }
{ PowerOff - Very simple application for auto shutdown the computer. }
{ It is useful app, if you before sleep like watch videos on }
{ YouTube/music/TV shows/movies, but they continue playing all night, causing a headache }
{ in the morning ... }
{ }
{ This project uses MIT license: }
{ -------------------------------------------------------------------------------------- }
{ Copyright (c) 2016 errorsoft }
{ Permission is hereby granted, free of charge, to any person obtaining a copy of this }
{ software and associated documentation files (the "Software"), to deal in the Software }
{ without restriction, including without limitation the rights to use, copy, modify, }
{ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to }
{ permit persons to whom the Software is furnished to do so, subject to the following }
{ conditions: }
{ The above copyright notice and this permission notice shall be included in all copies }
{ or substantial portions of the Software. }
{ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, }
{ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR }
{ A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT }
{ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF }
{ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE }
{ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }
{****************************************************************************************}
unit Utils;
interface
uses
System.Types, FMX.Types, FMX.Graphics, FMX.TextLayout, System.Math, System.SysUtils, FMX.Controls, System.UITypes,
FMX.StdCtrls;
const
cMaxFontSize = 512;
type
TLineKind = (Up, Down, Left, Right, Both);
function IsRu: Boolean;
function CalcTextSize(Text: string; Font: TFont; Size: Single = 0): TSizeF;
function FontSizeForBox(Text: string; Font: TFont; Width, Height: Single; MaxFontSize: Single = cMaxFontSize): Integer;
function SecondsToString(Seconds: Integer): string;
procedure DrawTicks(Control: TTrackBar; Offset: Single; PageSize: Single; DrawBounds: Boolean;
LineKind: TLineKind; LineWidth, LineSpace: Single; Color: TAlphaColor);
implementation
function IsRu: Boolean;
begin
Result := PreferredUILanguages.ToLower.IndexOf('ru') <> -1;
end;
function cHour: Char;
begin
if IsRu then Result := '÷' else Result := 'h';
end;
function cMinute: Char;
begin
if IsRu then Result := 'ì' else Result := 'm';
end;
function cSecond: Char;
begin
if IsRu then Result := 'ñ' else Result := 's';
end;
function CalcTextSize(Text: string; Font: TFont; Size: Single = 0): TSizeF;
var
TextLayout: TTextLayout;
begin
TextLayout := TTextLayoutManager.DefaultTextLayout.Create;
try
TextLayout.BeginUpdate;
try
TextLayout.Text := Text;
TextLayout.MaxSize := TPointF.Create(9999, 9999);
TextLayout.Font.Assign(Font);
if not SameValue(0, Size) then
begin
TextLayout.Font.Size := Size;
end;
TextLayout.WordWrap := False;
TextLayout.Trimming := TTextTrimming.None;
TextLayout.HorizontalAlign := TTextAlign.Leading;
TextLayout.VerticalAlign := TTextAlign.Leading;
finally
TextLayout.EndUpdate;
end;
Result.Width := TextLayout.Width;
Result.Height := TextLayout.Height;
finally
TextLayout.Free;
end;
end;
function FontSizeForBox(Text: string; Font: TFont; Width, Height: Single; MaxFontSize: Single = cMaxFontSize): Integer;
var
Size, Max, Min, MaxIterations: Integer;
Current: TSizeF;
begin
Max := Trunc(MaxFontSize);
Min := 0;
MaxIterations := 20;
repeat
Size := (Max + Min) div 2;
Current := CalcTextSize(Text, Font, Size);
if ((Abs(Width - Current.Width) < 1) and (Width >= Current.Width)) and
((Abs(Height - Current.Height) < 1) and (Height >= Current.Height)) then
break
else
if (Width < Current.Width) or (Height < Current.Height) then
Max := Size
else
Min := Size;
Dec(MaxIterations);
until MaxIterations = 0;
Result := Size;
end;
function SecondsToString(Seconds: Integer): string;
var
n: Integer;
begin
Result := '';
if Seconds >= 60 * 60 then
begin
n := Seconds div (60 * 60);
Result := Result + n.toString + cHour + ' ';
end;
if Seconds >= 60 then
begin
n := (Seconds div 60) mod 60;
if n <= 9 then
Result := Result + '0';
Result := Result + n.toString + cMinute + ' ';
end;
n := Seconds mod 60;
if n <= 9 then
Result := Result + '0';
Result := Result + n.toString + cSecond + ' ';
end;
type
THTrackBar = class(TTrackBar) end;
procedure DrawTicks(Control: TTrackBar; Offset: Single; PageSize: Single; DrawBounds: Boolean;
LineKind: TLineKind; LineWidth, LineSpace: Single; Color: TAlphaColor);
var
Obj: TFmxObject;
Cnt: TControl;
L: TPointF;
Coord, RealCoord: Single;
function GetCoord(Value: Single): Single;
begin
if Control.Orientation = TOrientation.Horizontal then
Result := Ceil(THTrackBar(Control).GetThumbRect(Value).CenterPoint.X)// + Crutch
else
Result := Ceil(THTrackBar(Control).GetThumbRect(Value).CenterPoint.Y);// + Crutch;
end;
procedure DrawLine(Coord: Single);
begin
if Control.Orientation = TOrientation.Horizontal then
begin
if (SameValue(LineSpace, 0)) and (LineKind = TLineKind.Both) then
begin
Control.Canvas.DrawLine(
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) - LineWidth + 0.5),
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) + LineWidth - 0.5), 1)
end else
begin
if (LineKind = TLineKind.Down) or (LineKind = TLineKind.Both) then
Control.Canvas.DrawLine(
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) + LineSpace + 0.5),
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) + LineSpace + LineWidth - 0.5), 1);
if (LineKind = TLineKind.Up) or (LineKind = TLineKind.Both) then
Control.Canvas.DrawLine(
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) - LineSpace - 0.5),
PointF(Coord + 0.5, L.Y + Trunc(Cnt.Height / 2) - LineSpace - LineWidth + 0.5), 1);
end;
end else
begin
if (SameValue(LineSpace, 0)) and (LineKind = TLineKind.Both) then
begin
Control.Canvas.DrawLine(
PointF(L.X + Trunc(Cnt.Width / 2) - LineWidth + 0.5, Coord + 0.5),
PointF(L.X + Trunc(Cnt.Width / 2) + LineWidth - 0.5, Coord + 0.5), 1)
end else
begin
if (LineKind = TLineKind.Right) or (LineKind = TLineKind.Both) then
Control.Canvas.DrawLine(
PointF(L.X + Trunc(Cnt.Width / 2) + LineWidth + 0.5, Coord + 0.5),
PointF(L.X + Trunc(Cnt.Width / 2) + LineWidth + LineWidth - 0.5, Coord + 0.5), 1);
if (LineKind = TLineKind.Left) or (LineKind = TLineKind.Both) then
Control.Canvas.DrawLine(
PointF(L.X + Trunc(Cnt.Width / 2) - LineWidth - 0.5, Coord + 0.5),
PointF(L.X + Trunc(Cnt.Width / 2) - LineWidth - LineWidth + 0.5, Coord + 0.5), 1);
end;
end;
end;
begin
if Control.Orientation = TOrientation.Horizontal then
Obj := Control.FindStyleResource('htrack')
else
Obj := Control.FindStyleResource('vtrack');
if Obj = nil then
Exit;
Cnt := Obj.FindStyleResource('background') as TControl;
if Cnt = nil then
Exit;
Control.Canvas.Stroke.Thickness := 1;
Control.Canvas.Stroke.Kind := TBrushKind.Solid;
Control.Canvas.Stroke.Color := Color;
L := Cnt.LocalToAbsolute(PointF(0, 0)) - Control.LocalToAbsolute(PointF(0, 0));
if DrawBounds and not SameValue(Offset, 0.0) then
DrawLine(GetCoord(Control.Min));
Coord := Offset + Control.Min;
while Coord <= Control.Max - Control.Min do
begin
if (Coord >= Control.Min) and (Coord <= Control.Max) then
begin
RealCoord := GetCoord(Coord);
DrawLine(RealCoord);
end;
Coord := Coord + PageSize;
end;
if DrawBounds and not SameValue(GetCoord(Control.Max), GetCoord(Coord - PageSize)) then
DrawLine(GetCoord(Control.Max));
end;
end.