source: trunk/StudioPackage/UDataModule.pas

Last change on this file was 16, checked in by chronos, 12 years ago
  • Modified: Definition of text source file moved to new package Basic.
File size: 7.6 KB
Line 
1unit UDataModule;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, ActnList, Controls, SpecializedList,
9 USource, UModularSystem, UCoolTranslator, ULDStudioAPI, Menus, ComCtrls,
10 Registry, URegistry, UApplicationInfo;
11
12type
13 { TOpenedFiles }
14
15 TOpenedFiles = class(TListObject)
16 private
17 FOnChange: TNotifyEvent;
18 public
19 Selected: TSource;
20 function FindByFileName(Value: string): TSource;
21 procedure CloseFile(Source: TSource);
22 procedure OpenFileName(FileName: string);
23 procedure OpenFile(Source: TSource);
24 property OnChange: TNotifyEvent read FOnChange write FOnChange;
25 end;
26
27 { TDataModule1 }
28
29 TDataModule1 = class(TDataModule)
30 published
31 ActionListRegistred: TActionList;
32 ApplicationInfo1: TApplicationInfo;
33 CoolTranslator1: TCoolTranslator;
34 ImageListMain: TImageList;
35 API: TLDStudioAPI;
36 procedure APIDockForm(Form: TForm; Target: TDockTarget);
37 function APIGetRegistryContext: TRegistryContext;
38 procedure APIRegisterFileTemplate(Template: TFileTemplate);
39 procedure APIRegisterFileType(FileType: TSourceType);
40 procedure APIRegisterForm(Form: TForm);
41 procedure APIRegisterMainMenuItem(MenuItem: TMenuItem;
42 ParentItem: TMenuItem; Index: Integer);
43 procedure APIUnregisterAction(Action: TAction);
44 procedure APIUnregisterFileTemplate(Template: TFileTemplate);
45 procedure APIUnregisterFileType(FileType: TSourceType);
46 procedure APIUnregisterForm(Form: TForm);
47 procedure APIUnregisterMainMenuItem(MenuItem: TMenuItem);
48 procedure APIOpenFile(FileName: string);
49 procedure APIRegisterAction(Action: TAction);
50 procedure DataModuleCreate(Sender: TObject);
51 procedure DataModuleDestroy(Sender: TObject);
52 private
53 procedure AutoOpen;
54 procedure DockToPageControl(PageControl: TPageControl; Form: TForm);
55 public
56 ModuleManager: TModuleManager;
57 FileTypes: TFileTypes;
58 FileTemplates: TFileTemplates;
59 OpenedFiles: TOpenedFiles;
60 procedure LoadFromRegistry(Context: TRegistryContext);
61 procedure SaveToRegistry(Context: TRegistryContext);
62 end;
63
64
65var
66 DataModule1: TDataModule1;
67
68
69implementation
70
71{$R *.lfm}
72
73uses
74 UFormMain, UFormSettings, UFormModuleList, UFormNewFile, ULDModuleBasic;
75
76resourcestring
77 SNewFile = 'New file';
78
79{ TOpenedFiles }
80
81function TOpenedFiles.FindByFileName(Value: string): TSource;
82var
83 I: Integer;
84begin
85 I := 0;
86 while (I < Count) and (TSource(Items[I]).Name <> Value) do Inc(I);
87 if I < Count then Result := TSource(Items[I])
88 else Result := nil;
89end;
90
91procedure TOpenedFiles.CloseFile(Source: TSource);
92begin
93 FreeAndNil(Source.Form);
94 Remove(Source);
95 if Assigned(FOnChange) then FOnChange(Self);
96end;
97
98procedure TOpenedFiles.OpenFileName(FileName: string);
99var
100 NewFile: TSource;
101 FileType: TSourceType;
102begin
103 FileType := DataModule1.FileTypes.FindByExt(ExtractFileExt(FileName));
104 if Assigned(FileType) then begin
105 NewFile := FileType.SourceClass.Create;
106 NewFile.Name := FileName;
107 OpenFile(NewFile);
108 end;
109end;
110
111procedure TOpenedFiles.OpenFile(Source: TSource);
112begin
113 AddNew(Source);
114 Source.Form := Source.FileType.Form.Create(nil);
115 Source.Form.Caption := ExtractFileName(Source.Name);
116 Source.Form.Visible := True;
117 Selected := Source;
118 if Assigned(FOnChange) then FOnChange(Self);
119end;
120
121{ TDataModule1 }
122
123procedure TDataModule1.DataModuleCreate(Sender: TObject);
124begin
125 FileTemplates := TFileTemplates.Create;
126 FileTemplates.OwnsObjects := False;
127 FileTypes := TFileTypes.Create;
128 FileTypes.OwnsObjects := False;
129 OpenedFiles := TOpenedFiles.Create;
130 ModuleManager := TModuleManager.Create(nil);
131 ModuleManager.API := API;
132
133 FormMain := TFormMain.Create(nil);
134 Application.UpdateMainForm(FormMain);
135 OpenedFiles.OnChange := FormMain.OpenedFileChange;
136 FormSettings := TFormSettings.Create(nil);
137 FormModuleList := TFormModuleList.Create(nil);
138 FormNewFile := TFormNewFile.Create(nil);
139
140 LoadFromRegistry(RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey));
141
142 DataModule1 := Self;
143 FormMain.Show;
144 AutoOpen;
145end;
146
147procedure TDataModule1.APIUnregisterMainMenuItem(MenuItem: TMenuItem);
148begin
149 MenuItem.Parent.Delete(MenuItem.Parent.IndexOf(MenuItem));
150end;
151
152procedure TDataModule1.APIRegisterMainMenuItem(MenuItem: TMenuItem;
153 ParentItem: TMenuItem; Index: Integer);
154begin
155 if Assigned(ParentItem) then ParentItem.Insert(Index, MenuItem)
156 else FormMain.MainMenu.Items.Insert(Index, MenuItem);
157end;
158
159procedure TDataModule1.APIRegisterForm(Form: TForm);
160begin
161 Form.Parent := FormMain;
162end;
163
164procedure TDataModule1.APIRegisterFileType(FileType: TSourceType);
165begin
166 FileTypes.Add(FileType);
167end;
168
169procedure TDataModule1.DockToPageControl(PageControl: TPageControl; Form: TForm
170 );
171var
172 NewTabSheet: TTabSheet;
173begin
174 NewTabSheet := TTabSheet.Create(PageControl);
175 NewTabSheet.Parent := PageControl;
176 NewTabSheet.Visible := True;
177 NewTabSheet.Caption := Form.Caption;
178 Form.Align := alClient;
179 Form.ManualDock(NewTabSheet, nil, alClient);
180 Form.Visible := True;
181end;
182
183procedure TDataModule1.LoadFromRegistry(Context: TRegistryContext);
184begin
185 with TRegistryEx.Create do
186 try
187 RootKey := Context.RootKey;
188 OpenKey(Context.Key, True);
189
190 finally
191 Free;
192 end;
193 FormMain.LoadFromRegistry(Context);
194end;
195
196procedure TDataModule1.SaveToRegistry(Context: TRegistryContext);
197begin
198 with TRegistryEx.Create do
199 try
200 RootKey := Context.RootKey;
201 OpenKey(Context.Key, True);
202
203 finally
204 Free;
205 end;
206 FormMain.SaveToRegistry(Context);
207end;
208
209procedure TDataModule1.APIDockForm(Form: TForm; Target: TDockTarget);
210begin
211 with FormMain do
212 case Target of
213 dtCenter: DockToPageControl(PageControlCenter, Form);
214// dtTop: DockToPageControl(PageControlTop, Form);
215 dtLeft: DockToPageControl(PageControlLeft, Form);
216 dtRight: DockToPageControl(PageControlRight, Form);
217 dtBottom: DockToPageControl(PageControlBottom, Form);
218 end;
219end;
220
221function TDataModule1.APIGetRegistryContext: TRegistryContext;
222begin
223 Result := RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey);
224end;
225
226procedure TDataModule1.APIRegisterFileTemplate(Template: TFileTemplate);
227begin
228 FileTemplates.Add(Template);
229end;
230
231procedure TDataModule1.APIUnregisterAction(Action: TAction);
232begin
233 Action.ActionList := nil;
234end;
235
236procedure TDataModule1.APIUnregisterFileTemplate(Template: TFileTemplate);
237begin
238 FileTemplates.Remove(Template);
239end;
240
241procedure TDataModule1.APIUnregisterFileType(FileType: TSourceType);
242begin
243 FileTypes.Remove(FileType);
244end;
245
246procedure TDataModule1.APIUnregisterForm(Form: TForm);
247begin
248 Form.Parent := nil;
249end;
250
251procedure TDataModule1.DataModuleDestroy(Sender: TObject);
252begin
253 SaveToRegistry(RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey));
254 FreeAndNil(ModuleManager);
255 FreeAndNil(FormNewFile);
256 FreeAndNil(FormMain);
257 FreeAndNil(FormSettings);
258 FreeAndNil(FormModuleList);
259 FreeAndNil(OpenedFiles);
260 FreeAndNil(FileTypes);
261 FreeAndNil(FileTemplates);
262end;
263
264procedure TDataModule1.AutoOpen;
265var
266 FileName: string;
267begin
268 if (ParamCount > 0) then begin
269 FileName := UTF8Encode(ParamStr(1));
270 if FileExistsUTF8(FileName) then begin
271 OpenedFiles.OpenFileName(FileName);
272 end;
273 end;
274end;
275
276procedure TDataModule1.APIOpenFile(FileName: string);
277begin
278 OpenedFiles.OpenFileName(FileName);
279end;
280
281procedure TDataModule1.APIRegisterAction(Action: TAction);
282begin
283 Action.ActionList := ActionListRegistred;
284end;
285
286end.
287
Note: See TracBrowser for help on using the repository browser.