forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewTabSet.pas
374 lines (334 loc) · 9.56 KB
/
NewTabSet.pas
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
unit NewTabSet;
{
Inno Setup
Copyright (C) 1997-2020 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
TNewTabSet - modern VS-style tabs with theme support
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ModernColors;
type
TTabPosition = (tpTop, tpBottom);
TNewTabSet = class(TCustomControl)
private
FHints: TStrings;
FTabs: TStrings;
FTabIndex: Integer;
FTabPosition: TTabPosition;
FTheme: TTheme;
function GetTabRect(Index: Integer): TRect;
procedure InvalidateTab(Index: Integer);
procedure TabsListChanged(Sender: TObject);
procedure SetTabs(Value: TStrings);
procedure SetTabIndex(Value: Integer);
procedure SetTabPosition(Value: TTabPosition);
procedure SetTheme(Value: TTheme);
procedure SetHints(const Value: TStrings);
protected
procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
procedure CreateParams(var Params: TCreateParams); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Theme: TTheme read FTheme write SetTheme;
published
property Align;
property Font;
property Hints: TStrings read FHints write SetHints;
property ParentFont;
property TabIndex: Integer read FTabIndex write SetTabIndex;
property Tabs: TStrings read FTabs write SetTabs;
property TabPosition: TTabPosition read FTabPosition write SetTabPosition default tpBottom;
property OnClick;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JR', [TNewTabSet]);
end;
procedure RGBToHSV(const R, G, B: Integer; var H, S: Double; var V: Integer);
var
Max, Min, C: Integer;
begin
Max := R;
if G > Max then Max := G;
if B > Max then Max := B;
Min := R;
if G < Min then Min := G;
if B < Min then Min := B;
C := Max - Min;
if C = 0 then begin
H := 0;
S := 0;
end
else begin
if Max = R then
H := (60 * (G - B)) / C
else if Max = G then
H := (60 * (B - R)) / C + 120
else if Max = B then
H := (60 * (R - G)) / C + 240;
if H < 0 then
H := H + 360;
S := C / Max;
end;
V := Max;
end;
procedure HSVtoRGB(const H, S: Double; const V: Integer; var R, G, B: Integer);
var
I, P, Q, T: Integer;
F: Double;
begin
I := Trunc(H / 60);
F := Frac(H / 60);
P := Round(V * (1.0 - S));
Q := Round(V * (1.0 - S * F));
T := Round(V * (1.0 - S * (1.0 - F)));
case I of
0: begin R := V; G := t; B := p; end;
1: begin R := q; G := V; B := p; end;
2: begin R := p; G := V; B := t; end;
3: begin R := p; G := q; B := V; end;
4: begin R := t; G := p; B := V; end;
5: begin R := V; G := p; B := q; end;
else
{ Should only get here with bogus input }
R := 0; G := 0; B := 0;
end;
end;
function LightenColor(const Color: TColorRef; const Amount: Integer): TColorRef;
var
H, S: Double;
V, R, G, B: Integer;
begin
RGBtoHSV(Byte(Color), Byte(Color shr 8), Byte(Color shr 16), H, S, V);
Inc(V, Amount);
if V > 255 then
V := 255;
if V < 0 then
V := 0;
HSVtoRGB(H, S, V, R, G, B);
Result := R or (G shl 8) or (B shl 16);
end;
{ TNewTabSet }
const
TabPaddingX = 5;
TabPaddingY = 3;
TabSpacing = 1;
constructor TNewTabSet.Create(AOwner: TComponent);
begin
inherited;
FTabs := TStringList.Create;
TStringList(FTabs).OnChange := TabsListChanged;
FTabPosition := tpBottom;
FHints := TStringList.Create;
ControlStyle := ControlStyle + [csOpaque];
Width := 129;
Height := 21;
end;
procedure TNewTabSet.CreateParams(var Params: TCreateParams);
begin
inherited;
with Params.WindowClass do
style := style and not (CS_HREDRAW or CS_VREDRAW);
end;
destructor TNewTabSet.Destroy;
begin
FTabs.Free;
inherited;
end;
procedure TNewTabSet.CMHintShow(var Message: TCMHintShow);
var
I: Integer;
R: TRect;
begin
inherited;
if Message.HintInfo.HintControl = Self then begin
for I := 0 to FTabs.Count-1 do begin
if I >= FHints.Count then
Break;
R := GetTabRect(I);
if PtInRect(R, Message.HintInfo.CursorPos) then begin
Message.HintInfo.HintStr := FHints[I];
Message.HintInfo.CursorRect := R;
Break;
end;
end;
end;
end;
function TNewTabSet.GetTabRect(Index: Integer): TRect;
var
CR: TRect;
I, SizeX, SizeY: Integer;
Size: TSize;
begin
CR := ClientRect;
Canvas.Font.Assign(Font);
if FTabPosition = tpBottom then
Result.Top := 0;
Result.Right := 4;
for I := 0 to FTabs.Count-1 do begin
Size := Canvas.TextExtent(FTabs[I]);
SizeX := Size.cx + (TabPaddingX * 2) + TabSpacing;
SizeY := Size.cy + (TabPaddingY * 2);
if FTabPosition = tpTop then
Result.Top := CR.Bottom - SizeY;
Result := Bounds(Result.Right, Result.Top, SizeX, SizeY);
if Index = I then
Exit;
end;
SetRectEmpty(Result);
end;
procedure TNewTabSet.InvalidateTab(Index: Integer);
var
R: TRect;
begin
if HandleAllocated and (Index >= 0) and (Index < FTabs.Count) then begin
R := GetTabRect(Index);
{ Inc R.Right since the trailing separator of a tab overwrites the first
pixel of the next tab }
Inc(R.Right);
InvalidateRect(Handle, @R, False);
end;
end;
procedure TNewTabSet.TabsListChanged(Sender: TObject);
begin
Invalidate;
end;
procedure TNewTabSet.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
var
I: Integer;
R: TRect;
begin
if Button = mbLeft then begin
for I := 0 to FTabs.Count-1 do begin
R := GetTabRect(I);
if (X >= R.Left) and (X < R.Right) then begin
TabIndex := I;
Break;
end;
end;
end;
end;
procedure TNewTabSet.Paint;
var
HighColorMode: Boolean;
procedure DrawTabs(const SelectedTab: Boolean);
var
I: Integer;
R: TRect;
begin
for I := 0 to FTabs.Count-1 do begin
R := GetTabRect(I);
if SelectedTab and (FTabIndex = I) then begin
Dec(R.Right, TabSpacing);
if FTheme <> nil then
Canvas.Brush.Color := FTheme.Colors[tcBack]
else
Canvas.Brush.Color := clBtnFace;
Canvas.FillRect(R);
if FTheme <> nil then
Canvas.Font.Color := FTheme.Colors[tcFore]
else
Canvas.Font.Color := clBtnText;
Canvas.TextOut(R.Left + TabPaddingX, R.Top + TabPaddingY, FTabs[I]);
ExcludeClipRect(Canvas.Handle, R.Left, R.Top, R.Right, R.Bottom);
Break;
end;
if not SelectedTab and (FTabIndex <> I) then begin
if FTheme <> nil then
Canvas.Font.Color := FTheme.Colors[tcMarginFore]
else if HighColorMode and (ColorToRGB(clBtnFace) <> clBlack) then
Canvas.Font.Color := LightenColor(ColorToRGB(clBtnShadow), -43)
else begin
{ If the button face color is black, or if running in low color mode,
use plain clBtnHighlight as the text color }
Canvas.Font.Color := clBtnHighlight;
end;
Canvas.TextOut(R.Left + TabPaddingX, R.Top + TabPaddingY, FTabs[I]);
end;
end;
end;
var
CR: TRect;
begin
Canvas.Font.Assign(Font);
HighColorMode := (GetDeviceCaps(Canvas.Handle, BITSPIXEL) *
GetDeviceCaps(Canvas.Handle, PLANES)) >= 15;
CR := ClientRect;
{ Work around an apparent NT 4.0/2000/??? bug. If the width of the DC is
greater than the width of the screen, then any call to ExcludeClipRect
inexplicably shrinks the DC's clipping rectangle to the screen width.
Calling IntersectClipRect first with the entire client area as the
rectangle solves this (don't ask me why). }
IntersectClipRect(Canvas.Handle, CR.Left, CR.Top, CR.Right, CR.Bottom);
{ Selected tab }
DrawTabs(True);
{ Top or bottom line }
if FTheme <> nil then
Canvas.Pen.Color := FTheme.Colors[tcBack]
else
Canvas.Pen.Color := clBtnFace;
if FTabPosition = tpBottom then begin
Canvas.MoveTo(0, 0);
Canvas.LineTo(CR.Right, 0);
end else begin
Canvas.MoveTo(0, CR.Bottom-1);
Canvas.LineTo(CR.Right, CR.Bottom-1);
end;
{ Background fill }
if FTheme <> nil then
Canvas.Brush.Color := FTheme.Colors[tcMarginBack]
else if HighColorMode then
Canvas.Brush.Color := LightenColor(ColorToRGB(clBtnFace), 35)
else
Canvas.Brush.Color := clBtnShadow;
if FTabPosition = tpBottom then
Inc(CR.Top)
else
Dec(CR.Bottom);
Canvas.FillRect(CR);
{ Non-selected tabs }
DrawTabs(False);
end;
procedure TNewTabSet.SetHints(const Value: TStrings);
begin
FHints.Assign(Value);
ShowHint := FHints.Count > 0;
end;
procedure TNewTabSet.SetTabIndex(Value: Integer);
begin
if FTabIndex <> Value then begin
InvalidateTab(FTabIndex);
FTabIndex := Value;
InvalidateTab(Value);
Click;
end;
end;
procedure TNewTabSet.SetTabPosition(Value: TTabPosition);
begin
if FTabPosition <> Value then begin
FTabPosition := Value;
Invalidate;
end;
end;
procedure TNewTabSet.SetTabs(Value: TStrings);
begin
FTabs.Assign(Value);
if FTabIndex >= FTabs.Count then
SetTabIndex(FTabs.Count-1);
end;
procedure TNewTabSet.SetTheme(Value: TTheme);
begin
if FTheme <> Value then begin
FTheme := Value;
Invalidate;
end;
end;
end.