1 | unit UCore;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, UAcronym, UCoolTranslator, UPersistentForm,
|
---|
9 | UJobProgressView, UScaleDPI, Forms, Controls, ExtCtrls, Menus, LazFileUtils,
|
---|
10 | URegistry, UApplicationInfo, Registry;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TCore }
|
---|
15 |
|
---|
16 | TCore = class(TDataModule)
|
---|
17 | ApplicationInfo1: TApplicationInfo;
|
---|
18 | CoolTranslator1: TCoolTranslator;
|
---|
19 | ImageList1: TImageList;
|
---|
20 | ImageListLarge: TImageList;
|
---|
21 | JobProgressView1: TJobProgressView;
|
---|
22 | MenuItem1: TMenuItem;
|
---|
23 | MenuItem19: TMenuItem;
|
---|
24 | MenuItem2: TMenuItem;
|
---|
25 | MenuItem26: TMenuItem;
|
---|
26 | MenuItem27: TMenuItem;
|
---|
27 | MenuItem28: TMenuItem;
|
---|
28 | MenuItem3: TMenuItem;
|
---|
29 | PersistentForm1: TPersistentForm;
|
---|
30 | PopupMenuTrayIcon: TPopupMenu;
|
---|
31 | ScaleDPI1: TScaleDPI;
|
---|
32 | TrayIcon1: TTrayIcon;
|
---|
33 | procedure CoolTranslator1Translate(Sender: TObject);
|
---|
34 | procedure DataModuleCreate(Sender: TObject);
|
---|
35 | procedure DataModuleDestroy(Sender: TObject);
|
---|
36 | procedure TrayIcon1Click(Sender: TObject);
|
---|
37 | private
|
---|
38 | FAlwaysOnTop: Boolean;
|
---|
39 | StoredDimension: TControlDimension;
|
---|
40 | procedure SetAlwaysOnTop(AValue: Boolean);
|
---|
41 | function FindFirstNonOption: string;
|
---|
42 | public
|
---|
43 | AcronymDb: TAcronymDb;
|
---|
44 | StartOnLogon: Boolean;
|
---|
45 | StartMinimizedToTray: Boolean;
|
---|
46 | ReopenLastFileOnStart: Boolean;
|
---|
47 | InitializeStarted: Boolean;
|
---|
48 | InitializeFinished: Boolean;
|
---|
49 | procedure Initialize;
|
---|
50 | procedure LoadConfig;
|
---|
51 | procedure SaveConfig;
|
---|
52 | procedure ScaleDPI;
|
---|
53 | property AlwaysOnTop: Boolean read FAlwaysOnTop write SetAlwaysOnTop;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | var
|
---|
57 | Core: TCore;
|
---|
58 |
|
---|
59 | const
|
---|
60 | DefaultRegKey = '\Software\Chronosoft\Acronym Decoder';
|
---|
61 |
|
---|
62 |
|
---|
63 | implementation
|
---|
64 |
|
---|
65 | uses
|
---|
66 | UFormMain;
|
---|
67 |
|
---|
68 | const
|
---|
69 | ExampleFile = 'Example acronyms.adp';
|
---|
70 |
|
---|
71 |
|
---|
72 | {$R *.lfm}
|
---|
73 |
|
---|
74 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
75 | begin
|
---|
76 | AcronymDb := nil;
|
---|
77 | InitializeStarted := False;
|
---|
78 | InitializeFinished := False;
|
---|
79 | StoredDimension := TControlDimension.Create;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
83 | begin
|
---|
84 | FreeAndNil(StoredDimension);
|
---|
85 | FreeAndNil(AcronymDb);
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TCore.TrayIcon1Click(Sender: TObject);
|
---|
89 | begin
|
---|
90 | if not FormMain.Visible then FormMain.AShow.Execute
|
---|
91 | else FormMain.Hide;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TCore.CoolTranslator1Translate(Sender: TObject);
|
---|
95 | begin
|
---|
96 | UAcronym.Translate;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TCore.SetAlwaysOnTop(AValue: Boolean);
|
---|
100 | begin
|
---|
101 | if FAlwaysOnTop = AValue then Exit;
|
---|
102 | FAlwaysOnTop := AValue;
|
---|
103 | if FAlwaysOnTop then FormMain.FormStyle := fsSystemStayOnTop
|
---|
104 | else FormMain.FormStyle := fsNormal;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | procedure TCore.Initialize;
|
---|
108 | var
|
---|
109 | FileNameOption: string;
|
---|
110 | ExampleFileName: string;
|
---|
111 | begin
|
---|
112 | if not InitializeStarted then begin
|
---|
113 | InitializeStarted := True;
|
---|
114 | LoadConfig;
|
---|
115 |
|
---|
116 | {$IFDEF 0}
|
---|
117 | if Application.HasOption('h', 'help') then begin
|
---|
118 | WriteLn('AcronymDecoder <project file>');
|
---|
119 | WriteLn(' -t --tray Start minimized in system tray');
|
---|
120 | WriteLn(' -h --help Show this help');
|
---|
121 | Application.Terminate;
|
---|
122 | Exit;
|
---|
123 | end;
|
---|
124 | {$ENDIF}
|
---|
125 |
|
---|
126 | if Application.HasOption('t', 'tray') then begin
|
---|
127 | FormMain.Visible := False;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | ExampleFileName := ExtractFileDir(Application.ExeName) + DirectorySeparator + ExampleFile;
|
---|
131 | FileNameOption := FindFirstNonOption;
|
---|
132 | if FileNameOption <> '' then begin
|
---|
133 | // Open file specified as command line parameter
|
---|
134 | FormMain.ProjectOpen(FileNameOption);
|
---|
135 | end else
|
---|
136 | if ReopenLastFileOnStart and (FormMain.LastOpenedList1.Items.Count > 0) and
|
---|
137 | FileExists(FormMain.LastOpenedList1.Items[0]) then begin
|
---|
138 | // Open last opened file
|
---|
139 | FormMain.ProjectOpen(FormMain.LastOpenedList1.Items[0])
|
---|
140 | end else
|
---|
141 | if FileExists(ExampleFileName) then begin
|
---|
142 | // Open default database with examples if no item is in recent openned history
|
---|
143 | FileNameOption := ExtractFileDir(Application.ExeName) + DirectorySeparator + ExampleFile;
|
---|
144 | {$IFDEF Linux}
|
---|
145 | // If installed in Linux system then use installation directory for po files
|
---|
146 | if Application.ExeName = '/usr/bin/' + ExtractFileNameOnly(Application.ExeName) then
|
---|
147 | FileNameOption := '/usr/share/' + ExtractFileNameOnly(Application.ExeName) + '/Examples/' + ExampleFile;
|
---|
148 | {$ENDIF}
|
---|
149 | FormMain.ProjectOpen(FileNameOption);
|
---|
150 | end else begin
|
---|
151 | // Create empty file
|
---|
152 | FormMain.AFileNew.Execute;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | //ImageList1.Assign(ImageListLarge);
|
---|
156 |
|
---|
157 | ScaleDPI;
|
---|
158 | FormMain.UpdateAcronymsList;
|
---|
159 | FormMain.ListViewFilter1.UpdateFromListView(FormMain.ListViewAcronyms);
|
---|
160 | InitializeFinished := True;
|
---|
161 | end;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TCore.LoadConfig;
|
---|
165 | begin
|
---|
166 | FormMain.LoadConfig;
|
---|
167 |
|
---|
168 | with TRegistryEx.Create do
|
---|
169 | try
|
---|
170 | RootKey := HKEY_CURRENT_USER;
|
---|
171 | OpenKey(DefaultRegKey, True);
|
---|
172 | ScaleDPI1.DPI := Point(ReadIntegerWithDefault('DPIX', 96), ReadIntegerWithDefault('DPIY', 96));
|
---|
173 | ScaleDPI1.AutoDetect := ReadBoolWithDefault('DPIAuto', True);
|
---|
174 | if ValueExists('LanguageCode') then
|
---|
175 | CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
|
---|
176 | else CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode('');
|
---|
177 | AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
|
---|
178 | StartMinimizedToTray := ReadBoolWithDefault('StartMinimizedToTray', False);
|
---|
179 | ReopenLastFileOnStart := ReadBoolWithDefault('ReopenLastFileOnStart', True);
|
---|
180 | finally
|
---|
181 | Free;
|
---|
182 | end;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TCore.SaveConfig;
|
---|
186 | begin
|
---|
187 | FormMain.SaveConfig;
|
---|
188 |
|
---|
189 | with TRegistryEx.Create do
|
---|
190 | try
|
---|
191 | RootKey := HKEY_CURRENT_USER;
|
---|
192 | OpenKey(DefaultRegKey, True);
|
---|
193 | WriteInteger('DPIX', ScaleDPI1.DPI.X);
|
---|
194 | WriteInteger('DPIY', ScaleDPI1.DPI.Y);
|
---|
195 | WriteBool('DPIAuto', ScaleDPI1.AutoDetect);
|
---|
196 | if Assigned(CoolTranslator1.Language) and (CoolTranslator1.Language.Code <> '') then
|
---|
197 | WriteString('LanguageCode', CoolTranslator1.Language.Code)
|
---|
198 | else DeleteValue('LanguageCode');
|
---|
199 | WriteBool('AlwaysOnTop', AlwaysOnTop);
|
---|
200 | WriteBool('StartMinimizedToTray', StartMinimizedToTray);
|
---|
201 | WriteBool('ReopenLastFileOnStart', ReopenLastFileOnStart);
|
---|
202 | finally
|
---|
203 | Free;
|
---|
204 | end;
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TCore.ScaleDPI;
|
---|
208 | begin
|
---|
209 | // TODO: Transparent image scaling not working properly under linux Gtk2
|
---|
210 | // Also screen DPI is not correctly detected under linux Gtk2
|
---|
211 | //Core.ScaleDPI1.DPI := Point(200, 200);
|
---|
212 | {$IFDEF WINDOWS}
|
---|
213 | Core.ScaleDPI1.ScaleControl(FormMain.CoolBar1, Core.ScaleDPI1.DesignDPI);
|
---|
214 | Core.ScaleDPI1.ScaleImageList(ImageList1, Core.ScaleDPI1.DesignDPI);
|
---|
215 | {$ENDIF}
|
---|
216 | end;
|
---|
217 |
|
---|
218 | function TCore.FindFirstNonOption: string;
|
---|
219 | var
|
---|
220 | S: string;
|
---|
221 | I: Integer;
|
---|
222 | begin
|
---|
223 | Result := '';
|
---|
224 | for I := 1 to Application.ParamCount do begin
|
---|
225 | S := Application.Params[I];
|
---|
226 | if S[1] = Application.OptionChar then Continue;
|
---|
227 | Result := S;
|
---|
228 | Break;
|
---|
229 | end;
|
---|
230 | end;
|
---|
231 |
|
---|
232 |
|
---|
233 |
|
---|
234 | end.
|
---|
235 |
|
---|