1 | unit UTheme;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Graphics, ComCtrls, Controls, ExtCtrls, Menus, StdCtrls,
|
---|
7 | Spin, Forms, fgl, Grids;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TTheme = class
|
---|
11 | Name: string;
|
---|
12 | ColorWindow: TColor;
|
---|
13 | ColorWindowText: TColor;
|
---|
14 | ColorControl: TColor;
|
---|
15 | ColorControlText: TColor;
|
---|
16 | ColorControlSelected: TColor;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | { TThemes }
|
---|
20 |
|
---|
21 | TThemes = class(TFPGObjectList<TTheme>)
|
---|
22 | function AddNew(Name: string): TTheme;
|
---|
23 | function FindByName(Name: string): TTheme;
|
---|
24 | procedure LoadToStrings(Strings: TStrings);
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TThemeManager }
|
---|
28 |
|
---|
29 | TThemeManager = class(TComponent)
|
---|
30 | private
|
---|
31 | FTheme: TTheme;
|
---|
32 | procedure SetTheme(AValue: TTheme);
|
---|
33 | procedure SetThemeName(AValue: TTheme);
|
---|
34 | public
|
---|
35 | Used: Boolean;
|
---|
36 | Themes: TThemes;
|
---|
37 | procedure ApplyTheme(Component: TComponent);
|
---|
38 | constructor Create(AOwner: TComponent); override;
|
---|
39 | destructor Destroy; override;
|
---|
40 | procedure UseTheme(Form: TForm);
|
---|
41 | property Theme: TTheme read FTheme write SetTheme;
|
---|
42 | end;
|
---|
43 |
|
---|
44 | procedure Register;
|
---|
45 |
|
---|
46 | implementation
|
---|
47 |
|
---|
48 | { TThemes }
|
---|
49 |
|
---|
50 | procedure Register;
|
---|
51 | begin
|
---|
52 | RegisterComponents('Common', [TThemeManager]);
|
---|
53 | end;
|
---|
54 |
|
---|
55 | function TThemes.AddNew(Name: string): TTheme;
|
---|
56 | begin
|
---|
57 | Result := TTheme.Create;
|
---|
58 | Result.Name := Name;
|
---|
59 | Add(Result);
|
---|
60 | end;
|
---|
61 |
|
---|
62 | function TThemes.FindByName(Name: string): TTheme;
|
---|
63 | var
|
---|
64 | Theme: TTheme;
|
---|
65 | begin
|
---|
66 | Result := nil;
|
---|
67 | for Theme in Self do
|
---|
68 | if Theme.Name = Name then begin
|
---|
69 | Result := Theme;
|
---|
70 | Exit;
|
---|
71 | end;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TThemes.LoadToStrings(Strings: TStrings);
|
---|
75 | var
|
---|
76 | I: Integer;
|
---|
77 | begin
|
---|
78 | Strings.BeginUpdate;
|
---|
79 | try
|
---|
80 | while Strings.Count < Count do Strings.Add('');
|
---|
81 | while Strings.Count > Count do Strings.Delete(Strings.Count - 1);
|
---|
82 | for I := 0 to Count - 1 do begin
|
---|
83 | Strings[I] := Items[I].Name;
|
---|
84 | Strings.Objects[I] := Items[I];
|
---|
85 | end;
|
---|
86 | finally
|
---|
87 | Strings.EndUpdate;
|
---|
88 | end;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TThemeManager.SetThemeName(AValue: TTheme);
|
---|
92 | begin
|
---|
93 | if FTheme = AValue then Exit;
|
---|
94 | FTheme := AValue;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TThemeManager.SetTheme(AValue: TTheme);
|
---|
98 | begin
|
---|
99 | if FTheme = AValue then Exit;
|
---|
100 | FTheme := AValue;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | constructor TThemeManager.Create(AOwner: TComponent);
|
---|
104 | begin
|
---|
105 | inherited;
|
---|
106 | Themes := TThemes.Create;
|
---|
107 | with Themes.AddNew('System') do begin
|
---|
108 | ColorWindow := clWindow;
|
---|
109 | ColorWindowText := clWindowText;
|
---|
110 | ColorControl := clMenu;
|
---|
111 | ColorControlText := clWindowText;
|
---|
112 | ColorControlSelected := clWindow;
|
---|
113 | end;
|
---|
114 | Theme := TTheme(Themes.First);
|
---|
115 | with Themes.AddNew('Dark') do begin
|
---|
116 | ColorWindow := RGBToColor($20, $20, $20);
|
---|
117 | ColorWindowText := clWhite;
|
---|
118 | ColorControl := RGBToColor($40, $40, $40);
|
---|
119 | ColorControlText := clWhite;
|
---|
120 | ColorControlSelected := RGBToColor(96, 125, 155);
|
---|
121 | end;
|
---|
122 | with Themes.AddNew('Light') do begin
|
---|
123 | ColorWindow := clWhite;
|
---|
124 | ColorWindowText := clBlack;
|
---|
125 | ColorControl := RGBToColor($e0, $e0, $e0);
|
---|
126 | ColorControlText := clBlack;
|
---|
127 | ColorControlSelected := RGBToColor(196, 225, 255);
|
---|
128 | end;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | destructor TThemeManager.Destroy;
|
---|
132 | begin
|
---|
133 | FreeAndNil(Themes);
|
---|
134 | inherited;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TThemeManager.ApplyTheme(Component: TComponent);
|
---|
138 | var
|
---|
139 | Control: TControl;
|
---|
140 | I: Integer;
|
---|
141 | begin
|
---|
142 | if Component is TWinControl then begin
|
---|
143 | for I := 0 to TWinControl(Component).ControlCount - 1 do
|
---|
144 | ApplyTheme(TWinControl(Component).Controls[I]);
|
---|
145 | end;
|
---|
146 |
|
---|
147 | if Component is TControl then begin
|
---|
148 | Control := (Component as TControl);
|
---|
149 | if (Control is TEdit) or (Control is TSpinEdit) or (Control is TComboBox) and
|
---|
150 | (Control is TMemo) or (Control is TListView) or (Control is TCustomDrawGrid) or
|
---|
151 | (Control is TCheckBox) or (Control is TPageControl) or (Control is TRadioButton) then begin
|
---|
152 | Control.Color := FTheme.ColorWindow;
|
---|
153 | Control.Font.Color := FTheme.ColorWindowText;
|
---|
154 | end else begin
|
---|
155 | Control.Color := FTheme.ColorControl;
|
---|
156 | Control.Font.Color := FTheme.ColorControlText;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | if Control is TCustomDrawGrid then begin
|
---|
160 | (Control as TCustomDrawGrid).Editor.Color := FTheme.ColorWindow;
|
---|
161 | (Control as TCustomDrawGrid).Editor.Font.Color := FTheme.ColorWindowText;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | if Control is TPageControl then begin
|
---|
165 | for I := 0 to TPageControl(Component).PageCount - 1 do
|
---|
166 | ApplyTheme(TPageControl(Component).Pages[I]);
|
---|
167 | end;
|
---|
168 |
|
---|
169 | if Control is TCoolBar then begin
|
---|
170 | (Control as TCoolBar).Themed := False;
|
---|
171 | end;
|
---|
172 | end;
|
---|
173 | end;
|
---|
174 |
|
---|
175 | procedure TThemeManager.UseTheme(Form: TForm);
|
---|
176 | begin
|
---|
177 | if not Used and (FTheme.Name = 'System') then Exit;
|
---|
178 | ApplyTheme(Form);
|
---|
179 | Used := True;
|
---|
180 | end;
|
---|
181 |
|
---|
182 |
|
---|
183 | end.
|
---|