source: ProjectTemplates/FileMenuProject/Packages/Common/UTheme.pas

Last change on this file was 507, checked in by chronos, 7 years ago
  • Modified: Update Common package.
  • Added: Support for color theme.
File size: 3.8 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 for I := 0 to Component.ComponentCount - 1 do
135 ApplyTheme(Component.Components[I]);
136
137 if Component is TControl then begin
138 Control := (Component as TControl);
139 if (Control is TEdit) or (Control is TSpinEdit) or (Control is TComboBox) and
140 (Control is TMemo) or (Control is TListView) or (Control is TCustomDrawGrid) or
141 (Control is TCheckBox) then begin
142 Control.Color := FTheme.ColorWindow;
143 Control.Font.Color := FTheme.ColorWindowText;
144 end else begin
145 Control.Color := FTheme.ColorControl;
146 Control.Font.Color := FTheme.ColorControlText;
147 end;
148
149 if Control is TCustomDrawGrid then begin
150 (Control as TCustomDrawGrid).Editor.Color := FTheme.ColorWindow;
151 (Control as TCustomDrawGrid).Editor.Font.Color := FTheme.ColorWindowText;
152 end;
153 end;
154end;
155
156procedure TThemeManager.UseTheme(Form: TForm);
157begin
158 if not Used and (FTheme.Name = 'System') then Exit;
159 ApplyTheme(Form);
160 Used := True;
161end;
162
163
164end.
Note: See TracBrowser for help on using the repository browser.