source: tags/1.3.6/Settings.pas

Last change on this file was 570, checked in by chronos, 6 months ago
  • Fixed: Workaround for incorrectly styling TEdit controls under Gtk2.
File size: 12.2 KB
Line 
1unit Settings;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Dialogs, LCLProc, ScreenTools, Messg, ButtonA,
7 Directories, DrawDlg, ButtonC, KeyBindings, Languages, ListBoxEx,
8 {$IFDEF DPI}Dpi.Forms, Dpi.Controls, Dpi.Graphics, Dpi.StdCtrls, System.UITypes{$ELSE}
9 Forms, Controls, Graphics, StdCtrls{$ENDIF};
10
11type
12 { TSettingsDlg }
13
14 TSettingsDlg = class(TDrawDlg)
15 ButtonFullscreen: TButtonC;
16 ButtonCustomDpi: TButtonC;
17 ButtonGammaDown: TButtonC;
18 ButtonDpiDown: TButtonC;
19 EditShortCutPrimary: TEdit;
20 EditShortCutSecondary: TEdit;
21 ListLanguages: TListBoxEx;
22 ListKeyBindings: TListBoxEx;
23 ButtonOk: TButtonA;
24 ButtonCancel: TButtonA;
25 ButtonReset: TButtonA;
26 ButtonGammaUp: TButtonC;
27 ButtonDpiUp: TButtonC;
28 procedure ButtonCustomDpiClick(Sender: TObject);
29 procedure ButtonDpiDownClick(Sender: TObject);
30 procedure ButtonDpiUpClick(Sender: TObject);
31 procedure ButtonFullscreenClick(Sender: TObject);
32 procedure ButtonCancelClick(Sender: TObject);
33 procedure ButtonResetClick(Sender: TObject);
34 procedure ButtonGammaDownClick(Sender: TObject);
35 procedure EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
36 Shift: TShiftState);
37 procedure EditShortCutSecondaryKeyUp(Sender: TObject; var Key: Word;
38 Shift: TShiftState);
39 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
40 procedure FormCreate(Sender: TObject);
41 procedure FormDestroy(Sender: TObject);
42 procedure FormPaint(Sender: TObject);
43 procedure FormShow(Sender: TObject);
44 procedure ListKeyBindingsSelectionChange(Sender: TObject; User: Boolean);
45 procedure ButtonOkClick(Sender: TObject);
46 procedure ButtonGammaUpClick(Sender: TObject);
47 private
48 LocalGamma: Integer;
49 LocalKeyBindings: TKeyBindings;
50 CurrentKeyBinding: TKeyBinding;
51 LocalDpi: Integer;
52 procedure UpdateShortCutItem;
53 function GetLocalCustomDpiEnabled: Boolean;
54 procedure UpdateInterface;
55 public
56 procedure LoadData;
57 procedure SaveData;
58 end;
59
60var
61 SettingsDlg: TSettingsDlg;
62
63const
64 DpiMin = 100;
65 DpiMax = 500;
66 DpiStep = 25;
67
68
69implementation
70
71{$R *.lfm}
72
73uses
74 Start;
75
76var
77 SFullScreen, SGamma, SRestartMsg, SShortCutPrimary, SShortCutSecondary,
78 SLanguages, SKeyBindings, SCustomDpi, SDpi: string;
79
80procedure ReloadLanguages;
81begin
82 SFullScreen := Phrases.Lookup('SETTINGS', 0);
83 SGamma := Phrases.Lookup('SETTINGS', 1);
84 SRestartMsg := Phrases.Lookup('SETTINGS', 2);
85 SShortCutPrimary := Phrases.Lookup('SETTINGS', 3);
86 SShortCutSecondary := Phrases.Lookup('SETTINGS', 4);
87 SLanguages := Phrases.Lookup('SETTINGS', 5);
88 SKeyBindings := Phrases.Lookup('SETTINGS', 6);
89 SCustomDpi := Phrases.Lookup('SETTINGS', 7);
90 SDpi := Phrases.Lookup('SETTINGS', 8);
91end;
92
93{ TSettingsDlg }
94
95procedure TSettingsDlg.FormCreate(Sender: TObject);
96begin
97 Color := clBlack;
98 LocalKeyBindings := TKeyBindings.Create;
99
100 Canvas.Font.Assign(UniFont[ftNormal]);
101 Canvas.Brush.Style := TBrushStyle.bsClear;
102
103 ButtonOk.Caption := Phrases.Lookup('BTN_OK');
104 ButtonCancel.Caption := Phrases.Lookup('BTN_CANCEL');
105 ButtonReset.Caption := Phrases.Lookup('BTN_RESET');
106 InitButtons;
107end;
108
109procedure TSettingsDlg.ButtonCancelClick(Sender: TObject);
110begin
111 ModalResult := mrCancel;
112end;
113
114procedure TSettingsDlg.ButtonResetClick(Sender: TObject);
115begin
116 // TODO: Better to have default values on single place
117 ListLanguages.ItemIndex := 0;
118 ButtonFullscreen.ButtonIndex := 3;
119 ButtonCustomDpi.ButtonIndex := 2;
120 LocalDpi := 100;
121 LocalGamma := 100;
122 ListKeyBindings.ItemIndex := -1;
123 ListKeyBindingsSelectionChange(nil, False);
124 LocalKeyBindings.ResetToDefault;
125 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
126 UpdateInterface;
127 Repaint;
128end;
129
130procedure TSettingsDlg.ButtonGammaDownClick(Sender: TObject);
131begin
132 if LocalGamma > 50 then
133 begin
134 Dec(LocalGamma);
135 Invalidate;
136 end;
137end;
138
139procedure TSettingsDlg.EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
140 Shift: TShiftState);
141begin
142 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
143 CurrentKeyBinding.ShortCut := Key or
144 (scShift * Integer(ssShift in Shift)) or
145 (scCtrl * Integer(ssCtrl in Shift)) or
146 (scAlt * Integer(ssAlt in Shift));
147 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut);
148 Key := 0;
149 UpdateShortCutItem;
150 end;
151end;
152
153procedure TSettingsDlg.EditShortCutSecondaryKeyUp(Sender: TObject;
154 var Key: Word; Shift: TShiftState);
155begin
156 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
157 CurrentKeyBinding.ShortCut2 := Key or
158 (scShift * Integer(ssShift in Shift)) or
159 (scCtrl * Integer(ssCtrl in Shift)) or
160 (scAlt * Integer(ssAlt in Shift));
161 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2);
162 Key := 0;
163 UpdateShortCutItem;
164 end;
165end;
166
167procedure TSettingsDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction
168 );
169begin
170 ListKeyBindings.ItemIndex := -1;
171end;
172
173procedure TSettingsDlg.ButtonFullscreenClick(Sender: TObject);
174begin
175 ButtonFullscreen.ButtonIndex := ButtonFullscreen.ButtonIndex xor 1;
176end;
177
178procedure TSettingsDlg.ButtonDpiDownClick(Sender: TObject);
179begin
180 Dec(LocalDpi, DpiStep);
181 if LocalDpi < DpiMin then LocalDpi := DpiMin;
182 Invalidate;
183end;
184
185procedure TSettingsDlg.ButtonCustomDpiClick(Sender: TObject);
186begin
187 ButtonCustomDpi.ButtonIndex := ButtonCustomDpi.ButtonIndex xor 1;
188 UpdateInterface;
189end;
190
191procedure TSettingsDlg.ButtonDpiUpClick(Sender: TObject);
192begin
193 Inc(LocalDpi, DpiStep);
194 if LocalDpi > DpiMax then LocalDpi := DpiMax;
195 Invalidate;
196end;
197
198procedure TSettingsDlg.FormDestroy(Sender: TObject);
199begin
200 FreeAndNil(LocalKeyBindings);
201end;
202
203procedure TSettingsDlg.FormPaint(Sender: TObject);
204begin
205 PaintBackground(Canvas, 3, 3, ClientWidth - 6, ClientHeight - 6,
206 ClientWidth, ClientHeight);
207 Frame(Canvas, 0, 0, ClientWidth - 1, ClientHeight - 1, 0, 0);
208 Frame(Canvas, 1, 1, ClientWidth - 2, ClientHeight - 2,
209 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
210 Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
211 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
212 EditFrame(Canvas, EditShortCutPrimary.BoundsRect, MainTexture);
213 EditFrame(Canvas, EditShortCutSecondary.BoundsRect, MainTexture);
214 EditFrame(Canvas, ListLanguages.BoundsRect, MainTexture);
215 BtnFrame(Canvas, ButtonOk.BoundsRect, MainTexture);
216 BtnFrame(Canvas, ButtonCancel.BoundsRect, MainTexture);
217
218 RFrame(Canvas, ButtonFullscreen.Left - 1, ButtonFullscreen.Top - 1,
219 ButtonFullscreen.Left + 12, ButtonFullscreen.Top + 12, MainTexture.ColorBevelShade,
220 MainTexture.ColorBevelLight);
221 {$IFDEF DPI}
222 RFrame(Canvas, ButtonCustomDpi.Left - 1, ButtonCustomDpi.Top - 1,
223 ButtonCustomDpi.Left + 12, ButtonCustomDpi.Top + 12, MainTexture.ColorBevelShade,
224 MainTexture.ColorBevelLight);
225 LoweredTextOut(Canvas, -2, MainTexture, ButtonCustomDpi.Left + 32,
226 ButtonCustomDpi.Top - 4, SCustomDpi);
227 UnderlinedTitleValue(Canvas, SDpi, IntToStr(LocalDpi) + '%',
228 ButtonDpiUp.Left - 150 - 4, ButtonDpiUp.Top + 2, 150);
229 {$ENDIF}
230
231 LoweredTextOut(Canvas, -2, MainTexture, ListLanguages.Left,
232 ListLanguages.Top - 26, SLanguages);
233 LoweredTextOut(Canvas, -2, MainTexture, ListKeyBindings.Left,
234 ListKeyBindings.Top - 26, SKeyBindings);
235 LoweredTextOut(Canvas, -2, MainTexture, ButtonFullscreen.Left + 32,
236 ButtonFullscreen.Top - 4, SFullScreen);
237 UnderlinedTitleValue(Canvas, SGamma, IntToStr(LocalGamma) + '%',
238 ButtonGammaUp.Left - 150 - 4, ButtonGammaUp.Top + 2, 150);
239 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutPrimary.Left,
240 EditShortCutPrimary.Top - 26, SShortCutPrimary);
241 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutSecondary.Left,
242 EditShortCutSecondary.Top - 26, SShortCutSecondary);
243end;
244
245procedure TSettingsDlg.FormShow(Sender: TObject);
246begin
247 Caption := Phrases2.Lookup('ACTIONHEADER_CONFIG');
248 ReloadLanguages;
249 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items);
250 ListLanguages.Font.Color := MainTexture.ColorMark;
251 ListKeyBindings.Font.Color := MainTexture.ColorMark;
252 LoadData;
253 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
254 EditShortCutPrimary.Font.Color := MainTexture.ColorMark;
255 EditShortCutSecondary.Font.Color := MainTexture.ColorMark;
256 {$IFDEF DPI}
257 ButtonCustomDpi.Visible := True;
258 ButtonDpiDown.Visible := True;
259 ButtonDpiUp.Visible := True;
260 {$ELSE}
261 ButtonCustomDpi.Visible := False;
262 ButtonDpiDown.Visible := False;
263 ButtonDpiUp.Visible := False;
264 {$ENDIF}
265 UpdateInterface;
266 Gtk2DisableControlStyling(EditShortCutPrimary);
267 Gtk2DisableControlStyling(EditShortCutSecondary);
268end;
269
270procedure TSettingsDlg.ListKeyBindingsSelectionChange(Sender: TObject;
271 User: Boolean);
272begin
273 if Assigned(CurrentKeyBinding) then begin
274 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
275 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
276 end;
277
278 if ListKeyBindings.ItemIndex >= 0 then
279 CurrentKeyBinding := LocalKeyBindings[ListKeyBindings.ItemIndex]
280 else CurrentKeyBinding := nil;
281
282 if Assigned(CurrentKeyBinding) then begin
283 if CurrentKeyBinding.ShortCut <> 0 then
284 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut)
285 else EditShortCutPrimary.Text := '';
286 EditShortCutPrimary.Enabled := True;
287 if CurrentKeyBinding.ShortCut2 <> 0 then
288 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2)
289 else EditShortCutSecondary.Text := '';
290 EditShortCutSecondary.Enabled := True;
291 end else begin
292 EditShortCutPrimary.Text := '';
293 EditShortCutPrimary.Enabled := False;
294 EditShortCutSecondary.Text := '';
295 EditShortCutSecondary.Enabled := False;
296 end;
297end;
298
299procedure TSettingsDlg.ButtonOkClick(Sender: TObject);
300begin
301 SaveData;
302 ModalResult := mrOk;
303end;
304
305procedure TSettingsDlg.ButtonGammaUpClick(Sender: TObject);
306begin
307 if LocalGamma < 150 then begin
308 Inc(LocalGamma);
309 Invalidate;
310 end;
311end;
312
313procedure TSettingsDlg.UpdateShortCutItem;
314begin
315 if Assigned(CurrentKeyBinding) then begin
316 if CurrentKeyBinding.ShortCut > 0 then
317 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut);
318 if CurrentKeyBinding.ShortCut2 > 0 then
319 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut2);
320 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
321 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
322 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
323 end;
324end;
325
326function TSettingsDlg.GetLocalCustomDpiEnabled: Boolean;
327begin
328 Result := (ButtonCustomDpi.ButtonIndex and 1) = 1;
329end;
330
331procedure TSettingsDlg.UpdateInterface;
332begin
333 ButtonDpiUp.Enabled := GetLocalCustomDpiEnabled;
334 ButtonDpiDown.Enabled := GetLocalCustomDpiEnabled;
335end;
336
337procedure TSettingsDlg.LoadData;
338begin
339 StartDlg.Translator.Language := StartDlg.Translator.Languages.SearchByCode(LocaleCode);
340 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items, False);
341 ListLanguages.ItemIndex := ListLanguages.Items.IndexOfObject(StartDlg.Translator.Language);
342 if ListLanguages.ItemIndex = -1 then ListLanguages.ItemIndex := 0;
343 if FullScreen then ButtonFullscreen.ButtonIndex := 3
344 else ButtonFullscreen.ButtonIndex := 2;
345 LocalGamma := Gamma;
346 LocalKeyBindings.Assign(KeyBindings.KeyBindings);
347 {$IFDEF DPI}
348 if CustomDpiEnabled then ButtonCustomDpi.ButtonIndex := 3
349 else ButtonCustomDpi.ButtonIndex := 2;
350 LocalDpi := Round(CustomDpi * 100 / 96);
351 {$ENDIF}
352end;
353
354procedure TSettingsDlg.SaveData;
355var
356 NeedRestart: Boolean;
357begin
358 NeedRestart := (Gamma <> LocalGamma) or (CustomDpiEnabled <> GetLocalCustomDpiEnabled) or
359 ((GetLocalCustomDpiEnabled and (LocalDpi <> CustomDpi)));
360 if ListLanguages.ItemIndex <> -1 then begin
361 StartDlg.Translator.Language := TLanguage(ListLanguages.Items.Objects[ListLanguages.ItemIndex]);
362 LocaleCode := StartDlg.Translator.Language.Code;
363 end else begin
364 StartDlg.Translator.Language := nil;
365 LocaleCode := '';
366 end;
367 FullScreen := (ButtonFullscreen.ButtonIndex and 1) = 1;
368 Gamma := LocalGamma;
369 ScreenTools.CustomDpiEnabled := CustomDpiEnabled;
370 if NeedRestart then SimpleMessage(SRestartMsg);
371 KeyBindings.KeyBindings.Assign(LocalKeyBindings);
372 {$IFDEF DPI}
373 CustomDpiEnabled := GetLocalCustomDpiEnabled;
374 CustomDpi := Round(LocalDpi * 96 / 100);
375 {$ENDIF}
376end;
377
378end.
379
380
Note: See TracBrowser for help on using the repository browser.