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