| 1 | unit UDataModule; | 
|---|
| 2 |  | 
|---|
| 3 | {$mode delphi}{$H+} | 
|---|
| 4 |  | 
|---|
| 5 | interface | 
|---|
| 6 |  | 
|---|
| 7 | uses | 
|---|
| 8 | Classes, SysUtils, FileUtil, Forms, ActnList, Controls, SpecializedList, | 
|---|
| 9 | USource, UModularSystem, UCoolTranslator, ULDStudioAPI, Menus, ComCtrls, | 
|---|
| 10 | Registry, URegistry, UApplicationInfo; | 
|---|
| 11 |  | 
|---|
| 12 | type | 
|---|
| 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 |  | 
|---|
| 65 | var | 
|---|
| 66 | DataModule1: TDataModule1; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | implementation | 
|---|
| 70 |  | 
|---|
| 71 | {$R *.lfm} | 
|---|
| 72 |  | 
|---|
| 73 | uses | 
|---|
| 74 | UFormMain, UFormSettings, UFormModuleList, UFormNewFile, ULDModuleBasic; | 
|---|
| 75 |  | 
|---|
| 76 | resourcestring | 
|---|
| 77 | SNewFile = 'New file'; | 
|---|
| 78 |  | 
|---|
| 79 | { TOpenedFiles } | 
|---|
| 80 |  | 
|---|
| 81 | function TOpenedFiles.FindByFileName(Value: string): TSource; | 
|---|
| 82 | var | 
|---|
| 83 | I: Integer; | 
|---|
| 84 | begin | 
|---|
| 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; | 
|---|
| 89 | end; | 
|---|
| 90 |  | 
|---|
| 91 | procedure TOpenedFiles.CloseFile(Source: TSource); | 
|---|
| 92 | begin | 
|---|
| 93 | FreeAndNil(Source.Form); | 
|---|
| 94 | Remove(Source); | 
|---|
| 95 | if Assigned(FOnChange) then FOnChange(Self); | 
|---|
| 96 | end; | 
|---|
| 97 |  | 
|---|
| 98 | procedure TOpenedFiles.OpenFileName(FileName: string); | 
|---|
| 99 | var | 
|---|
| 100 | NewFile: TSource; | 
|---|
| 101 | FileType: TSourceType; | 
|---|
| 102 | begin | 
|---|
| 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; | 
|---|
| 109 | end; | 
|---|
| 110 |  | 
|---|
| 111 | procedure TOpenedFiles.OpenFile(Source: TSource); | 
|---|
| 112 | begin | 
|---|
| 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); | 
|---|
| 119 | end; | 
|---|
| 120 |  | 
|---|
| 121 | { TDataModule1 } | 
|---|
| 122 |  | 
|---|
| 123 | procedure TDataModule1.DataModuleCreate(Sender: TObject); | 
|---|
| 124 | begin | 
|---|
| 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; | 
|---|
| 145 | end; | 
|---|
| 146 |  | 
|---|
| 147 | procedure TDataModule1.APIUnregisterMainMenuItem(MenuItem: TMenuItem); | 
|---|
| 148 | begin | 
|---|
| 149 | MenuItem.Parent.Delete(MenuItem.Parent.IndexOf(MenuItem)); | 
|---|
| 150 | end; | 
|---|
| 151 |  | 
|---|
| 152 | procedure TDataModule1.APIRegisterMainMenuItem(MenuItem: TMenuItem; | 
|---|
| 153 | ParentItem: TMenuItem; Index: Integer); | 
|---|
| 154 | begin | 
|---|
| 155 | if Assigned(ParentItem) then ParentItem.Insert(Index, MenuItem) | 
|---|
| 156 | else FormMain.MainMenu.Items.Insert(Index, MenuItem); | 
|---|
| 157 | end; | 
|---|
| 158 |  | 
|---|
| 159 | procedure TDataModule1.APIRegisterForm(Form: TForm); | 
|---|
| 160 | begin | 
|---|
| 161 | Form.Parent := FormMain; | 
|---|
| 162 | end; | 
|---|
| 163 |  | 
|---|
| 164 | procedure TDataModule1.APIRegisterFileType(FileType: TSourceType); | 
|---|
| 165 | begin | 
|---|
| 166 | FileTypes.Add(FileType); | 
|---|
| 167 | end; | 
|---|
| 168 |  | 
|---|
| 169 | procedure TDataModule1.DockToPageControl(PageControl: TPageControl; Form: TForm | 
|---|
| 170 | ); | 
|---|
| 171 | var | 
|---|
| 172 | NewTabSheet: TTabSheet; | 
|---|
| 173 | begin | 
|---|
| 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; | 
|---|
| 181 | end; | 
|---|
| 182 |  | 
|---|
| 183 | procedure TDataModule1.LoadFromRegistry(Context: TRegistryContext); | 
|---|
| 184 | begin | 
|---|
| 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); | 
|---|
| 194 | end; | 
|---|
| 195 |  | 
|---|
| 196 | procedure TDataModule1.SaveToRegistry(Context: TRegistryContext); | 
|---|
| 197 | begin | 
|---|
| 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); | 
|---|
| 207 | end; | 
|---|
| 208 |  | 
|---|
| 209 | procedure TDataModule1.APIDockForm(Form: TForm; Target: TDockTarget); | 
|---|
| 210 | begin | 
|---|
| 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; | 
|---|
| 219 | end; | 
|---|
| 220 |  | 
|---|
| 221 | function TDataModule1.APIGetRegistryContext: TRegistryContext; | 
|---|
| 222 | begin | 
|---|
| 223 | Result := RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey); | 
|---|
| 224 | end; | 
|---|
| 225 |  | 
|---|
| 226 | procedure TDataModule1.APIRegisterFileTemplate(Template: TFileTemplate); | 
|---|
| 227 | begin | 
|---|
| 228 | FileTemplates.Add(Template); | 
|---|
| 229 | end; | 
|---|
| 230 |  | 
|---|
| 231 | procedure TDataModule1.APIUnregisterAction(Action: TAction); | 
|---|
| 232 | begin | 
|---|
| 233 | Action.ActionList := nil; | 
|---|
| 234 | end; | 
|---|
| 235 |  | 
|---|
| 236 | procedure TDataModule1.APIUnregisterFileTemplate(Template: TFileTemplate); | 
|---|
| 237 | begin | 
|---|
| 238 | FileTemplates.Remove(Template); | 
|---|
| 239 | end; | 
|---|
| 240 |  | 
|---|
| 241 | procedure TDataModule1.APIUnregisterFileType(FileType: TSourceType); | 
|---|
| 242 | begin | 
|---|
| 243 | FileTypes.Remove(FileType); | 
|---|
| 244 | end; | 
|---|
| 245 |  | 
|---|
| 246 | procedure TDataModule1.APIUnregisterForm(Form: TForm); | 
|---|
| 247 | begin | 
|---|
| 248 | Form.Parent := nil; | 
|---|
| 249 | end; | 
|---|
| 250 |  | 
|---|
| 251 | procedure TDataModule1.DataModuleDestroy(Sender: TObject); | 
|---|
| 252 | begin | 
|---|
| 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); | 
|---|
| 262 | end; | 
|---|
| 263 |  | 
|---|
| 264 | procedure TDataModule1.AutoOpen; | 
|---|
| 265 | var | 
|---|
| 266 | FileName: string; | 
|---|
| 267 | begin | 
|---|
| 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; | 
|---|
| 274 | end; | 
|---|
| 275 |  | 
|---|
| 276 | procedure TDataModule1.APIOpenFile(FileName: string); | 
|---|
| 277 | begin | 
|---|
| 278 | OpenedFiles.OpenFileName(FileName); | 
|---|
| 279 | end; | 
|---|
| 280 |  | 
|---|
| 281 | procedure TDataModule1.APIRegisterAction(Action: TAction); | 
|---|
| 282 | begin | 
|---|
| 283 | Action.ActionList := ActionListRegistred; | 
|---|
| 284 | end; | 
|---|
| 285 |  | 
|---|
| 286 | end. | 
|---|
| 287 |  | 
|---|