source: branches/generator/Packages/Common/UTheme.pas

Last change on this file was 167, checked in by chronos, 6 years ago
  • Modified: Update Common package.
  • Fixed: Remember forms dimensions after application restart.
File size: 4.1 KB
Line 
1unit UTheme;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics, ComCtrls, Controls, ExtCtrls, Menus, StdCtrls,
7 Spin, Forms, Contnrs, Grids;
8
9type
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(TObjectList)
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
44procedure Register;
45
46implementation
47
48{ TThemes }
49
50procedure Register;
51begin
52 RegisterComponents('Common', [TThemeManager]);
53end;
54
55function TThemes.AddNew(Name: string): TTheme;
56begin
57 Result := TTheme.Create;
58 Result.Name := Name;
59 Add(Result);
60end;
61
62function TThemes.FindByName(Name: string): TTheme;
63var
64 Theme: TTheme;
65begin
66 Result := nil;
67 for Theme in Self do
68 if Theme.Name = Name then begin
69 Result := Theme;
70 Exit;
71 end;
72end;
73
74procedure TThemes.LoadToStrings(Strings: TStrings);
75var
76 Theme: TTheme;
77begin
78 Strings.Clear;
79 for Theme in Self do
80 Strings.AddObject(Theme.Name, Theme);
81end;
82
83procedure TThemeManager.SetThemeName(AValue: TTheme);
84begin
85 if FTheme = AValue then Exit;
86 FTheme := AValue;
87end;
88
89procedure TThemeManager.SetTheme(AValue: TTheme);
90begin
91 if FTheme = AValue then Exit;
92 FTheme := AValue;
93end;
94
95constructor TThemeManager.Create(AOwner: TComponent);
96begin
97 inherited;
98 Themes := TThemes.Create;
99 with Themes.AddNew('System') do begin
100 ColorWindow := clWindow;
101 ColorWindowText := clWindowText;
102 ColorControl := clMenu;
103 ColorControlText := clWindowText;
104 ColorControlSelected := clWindow;
105 end;
106 Theme := TTheme(Themes.First);
107 with Themes.AddNew('Dark') do begin
108 ColorWindow := RGBToColor($20, $20, $20);
109 ColorWindowText := clWhite;
110 ColorControl := RGBToColor($40, $40, $40);
111 ColorControlText := clWhite;
112 ColorControlSelected := RGBToColor(96, 125, 155);
113 end;
114 with Themes.AddNew('Light') do begin
115 ColorWindow := clWhite;
116 ColorWindowText := clBlack;
117 ColorControl := RGBToColor($e0, $e0, $e0);
118 ColorControlText := clBlack;
119 ColorControlSelected := RGBToColor(196, 225, 255);
120 end;
121end;
122
123destructor TThemeManager.Destroy;
124begin
125 Themes.Free;
126 inherited Destroy;
127end;
128
129procedure TThemeManager.ApplyTheme(Component: TComponent);
130var
131 Control: TControl;
132 I: Integer;
133begin
134 if Component is TWinControl then begin
135 for I := 0 to TWinControl(Component).ControlCount - 1 do
136 ApplyTheme(TWinControl(Component).Controls[I]);
137 end;
138
139 if Component is TControl then begin
140 Control := (Component as TControl);
141 if (Control is TEdit) or (Control is TSpinEdit) or (Control is TComboBox) and
142 (Control is TMemo) or (Control is TListView) or (Control is TCustomDrawGrid) or
143 (Control is TCheckBox) or (Control is TPageControl) or (Control is TRadioButton) then begin
144 Control.Color := FTheme.ColorWindow;
145 Control.Font.Color := FTheme.ColorWindowText;
146 end else begin
147 Control.Color := FTheme.ColorControl;
148 Control.Font.Color := FTheme.ColorControlText;
149 end;
150
151 if Control is TCustomDrawGrid then begin
152 (Control as TCustomDrawGrid).Editor.Color := FTheme.ColorWindow;
153 (Control as TCustomDrawGrid).Editor.Font.Color := FTheme.ColorWindowText;
154 end;
155
156 if Control is TPageControl then begin
157 for I := 0 to TPageControl(Component).PageCount - 1 do
158 ApplyTheme(TPageControl(Component).Pages[I]);
159 end;
160
161 if Control is TCoolBar then begin
162 (Control as TCoolBar).Themed := False;
163 end;
164 end;
165end;
166
167procedure TThemeManager.UseTheme(Form: TForm);
168begin
169 if not Used and (FTheme.Name = 'System') then Exit;
170 ApplyTheme(Form);
171 Used := True;
172end;
173
174
175end.
Note: See TracBrowser for help on using the repository browser.