source: tags/1.4.0/UCore.pas

Last change on this file was 151, checked in by chronos, 7 years ago
  • Added: Option to disable auto reopen last file on start.
File size: 6.7 KB
Line 
1unit UCore;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, UAcronym, UCoolTranslator, UPersistentForm,
9 UJobProgressView, UScaleDPI, Forms, Controls, ExtCtrls, Menus, LazFileUtils,
10 URegistry, UApplicationInfo, Registry;
11
12type
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
56var
57 Core: TCore;
58
59const
60 DefaultRegKey = '\Software\Chronosoft\Acronym Decoder';
61
62
63implementation
64
65uses
66 UFormMain;
67
68const
69 ExampleFile = 'Example acronyms.adp';
70
71
72{$R *.lfm}
73
74procedure TCore.DataModuleCreate(Sender: TObject);
75begin
76 AcronymDb := nil;
77 InitializeStarted := False;
78 InitializeFinished := False;
79 StoredDimension := TControlDimension.Create;
80end;
81
82procedure TCore.DataModuleDestroy(Sender: TObject);
83begin
84 FreeAndNil(StoredDimension);
85 FreeAndNil(AcronymDb);
86end;
87
88procedure TCore.TrayIcon1Click(Sender: TObject);
89begin
90 if not FormMain.Visible then FormMain.AShow.Execute
91 else FormMain.Hide;
92end;
93
94procedure TCore.CoolTranslator1Translate(Sender: TObject);
95begin
96 UAcronym.Translate;
97end;
98
99procedure TCore.SetAlwaysOnTop(AValue: Boolean);
100begin
101 if FAlwaysOnTop = AValue then Exit;
102 FAlwaysOnTop := AValue;
103 if FAlwaysOnTop then FormMain.FormStyle := fsSystemStayOnTop
104 else FormMain.FormStyle := fsNormal;
105end;
106
107procedure TCore.Initialize;
108var
109 FileNameOption: string;
110 ExampleFileName: string;
111begin
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;
162end;
163
164procedure TCore.LoadConfig;
165begin
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;
183end;
184
185procedure TCore.SaveConfig;
186begin
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;
205end;
206
207procedure TCore.ScaleDPI;
208begin
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}
216end;
217
218function TCore.FindFirstNonOption: string;
219var
220 S: string;
221 I: Integer;
222begin
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;
230end;
231
232
233
234end.
235
Note: See TracBrowser for help on using the repository browser.