source: trunk/Packages/Common/UTheme.pas

Last change on this file was 215, checked in by chronos, 3 years ago
  • Modified: Build under Lazarus 2.2.0.
  • Modified: Updated Common package.
File size: 4.4 KB
Line 
1unit UTheme;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics, ComCtrls, Controls, ExtCtrls, Menus, StdCtrls,
7 Spin, Forms, fgl, 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(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
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 I: Integer;
77begin
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;
89end;
90
91procedure TThemeManager.SetThemeName(AValue: TTheme);
92begin
93 if FTheme = AValue then Exit;
94 FTheme := AValue;
95end;
96
97procedure TThemeManager.SetTheme(AValue: TTheme);
98begin
99 if FTheme = AValue then Exit;
100 FTheme := AValue;
101end;
102
103constructor TThemeManager.Create(AOwner: TComponent);
104begin
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;
129end;
130
131destructor TThemeManager.Destroy;
132begin
133 FreeAndNil(Themes);
134 inherited;
135end;
136
137procedure TThemeManager.ApplyTheme(Component: TComponent);
138var
139 Control: TControl;
140 I: Integer;
141begin
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;
173end;
174
175procedure TThemeManager.UseTheme(Form: TForm);
176begin
177 if not Used and (FTheme.Name = 'System') then Exit;
178 ApplyTheme(Form);
179 Used := True;
180end;
181
182
183end.
Note: See TracBrowser for help on using the repository browser.