Ignore:
Timestamp:
Aug 6, 2012, 2:35:22 PM (12 years ago)
Author:
chronos
Message:
  • Added: Registration of new file templates for modules.
  • Added: Program stores last opened file list in registry.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/StudioPackage/UDataModule.pas

    r14 r15  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, ActnList, Controls, SpecializedList,
    9   USource, UModularSystem, ULDStudioAPI, Menus, ComCtrls;
     9  USource, UModularSystem, UCoolTranslator, ULDStudioAPI, Menus, ComCtrls,
     10  Registry, URegistry, UApplicationInfo;
    1011
    1112type
    1213
    13   { TOpenedFile }
    14 
    15   TOpenedFile = class
    16   private
    17     FFileName: string;
    18     procedure SetFileName(AValue: string);
    19   public
    20     Modified: Boolean;
    21     Form: TForm;
    22     property FileName: string read FFileName write SetFileName;
     14  { TTextTemplate }
     15
     16  TTextTemplate = class(TFileTemplate)
     17    constructor Create; override;
     18  end;
     19
     20  { TTextFileType }
     21
     22  TTextFileType = class(TSourceType)
     23    constructor Create; override;
    2324  end;
    2425
     
    2930    FOnChange: TNotifyEvent;
    3031  public
    31     Selected: TOpenedFile;
    32     function FindByFileName(Value: string): TOpenedFile;
    33     procedure CloseFile(FileName: string);
    34     procedure OpenFile(FileName: string);
     32    Selected: TSource;
     33    function FindByFileName(Value: string): TSource;
     34    procedure CloseFile(Source: TSource);
     35    procedure OpenFileName(FileName: string);
     36    procedure OpenFile(Source: TSource);
    3537    procedure NewFile;
    3638    property OnChange: TNotifyEvent read FOnChange write FOnChange;
     
    4244  published
    4345    ActionListRegistred: TActionList;
     46    ApplicationInfo1: TApplicationInfo;
     47    CoolTranslator1: TCoolTranslator;
    4448    ImageListMain: TImageList;
    4549    API: TLDStudioAPI;
    4650    procedure APIDockForm(Form: TForm; Target: TDockTarget);
     51    function APIGetRegistryContext: TRegistryContext;
     52    procedure APIRegisterFileTemplate(Template: TFileTemplate);
    4753    procedure APIRegisterFileType(FileType: TSourceType);
    4854    procedure APIRegisterForm(Form: TForm);
     
    5056      ParentItem: TMenuItem; Index: Integer);
    5157    procedure APIUnregisterAction(Action: TAction);
     58    procedure APIUnregisterFileTemplate(Template: TFileTemplate);
    5259    procedure APIUnregisterFileType(FileType: TSourceType);
    5360    procedure APIUnregisterForm(Form: TForm);
    5461    procedure APIUnregisterMainMenuItem(MenuItem: TMenuItem);
     62    procedure APIOpenFile(FileName: string);
     63    procedure APIRegisterAction(Action: TAction);
    5564    procedure DataModuleCreate(Sender: TObject);
    5665    procedure DataModuleDestroy(Sender: TObject);
    57     procedure APIOpenFile(FileName: string);
    58     procedure APIRegisterAction(Action: TAction);
    5966  private
     67    TextFileType: TSourceType;
     68    TextFileTemplate: TTextTemplate;
     69    procedure AutoOpen;
    6070    procedure DockToPageControl(PageControl: TPageControl; Form: TForm);
    6171  public
    6272    ModuleManager: TModuleManager;
    6373    FileTypes: TFileTypes;
     74    FileTemplates: TFileTemplates;
    6475    OpenedFiles: TOpenedFiles;
    65   end;
     76    procedure LoadFromRegistry(Context: TRegistryContext);
     77    procedure SaveToRegistry(Context: TRegistryContext);
     78  end;
     79
    6680
    6781var
     
    7488
    7589uses
    76   UFormSourceCode, UFormMain, UFormSettings, UFormModuleList;
     90  UFormSourceCode, UFormMain, UFormSettings, UFormModuleList, UFormNewFile;
    7791
    7892resourcestring
    7993  SNewFile = 'New file';
    80 
    81 procedure TOpenedFile.SetFileName(AValue: string);
    82 begin
    83   if FFileName = AValue then Exit;
    84   FFileName := AValue;
    85   if Assigned(Form) then
    86     Form.Caption := ExtractFileName(FFileName);
    87 end;
     94  STextFile = 'Text file';
     95
     96{ TTextFileType }
     97
     98constructor TTextFileType.Create;
     99begin
     100  inherited Create;
     101  Extension := '.txt';
     102  Title := 'Text file';;
     103end;
     104
     105{ TTextTemplate }
     106
     107constructor TTextTemplate.Create;
     108begin
     109  inherited Create;
     110  Name := STextFile;
     111end;
     112
    88113
    89114{ TOpenedFiles }
    90115
    91 function TOpenedFiles.FindByFileName(Value: string): TOpenedFile;
     116function TOpenedFiles.FindByFileName(Value: string): TSource;
    92117var
    93118  I: Integer;
    94119begin
    95120  I := 0;
    96   while (I < Count) and (TOpenedFile(Items[I]).FileName <> Value) do Inc(I);
    97   if I < Count then Result := TOpenedFile(Items[I])
     121  while (I < Count) and (TSource(Items[I]).Name <> Value) do Inc(I);
     122  if I < Count then Result := TSource(Items[I])
    98123    else Result := nil;
    99124end;
    100125
    101 procedure TOpenedFiles.CloseFile(FileName: string);
    102 var
    103   OpenedFile: TOpenedFile;
    104 begin
    105   OpenedFile := FindByFileName(FileName);
    106   FreeAndNil(OpenedFile.Form);
    107   Remove(OpenedFile);
     126procedure TOpenedFiles.CloseFile(Source: TSource);
     127begin
     128  FreeAndNil(Source.Form);
     129  Remove(Source);
    108130  if Assigned(FOnChange) then FOnChange(Self);
    109131end;
    110132
    111 procedure TOpenedFiles.OpenFile(FileName: string);
    112 var
    113   NewFile: TOpenedFile;
    114 begin
    115   NewFile := TOpenedFile(AddNew(TOpenedFile.Create));
    116   NewFile.FileName := FileName;
     133procedure TOpenedFiles.OpenFileName(FileName: string);
     134var
     135  NewFile: TSourceText;
     136begin
     137  NewFile := TSourceText.Create;
     138  NewFile.Name := FileName;
     139  OpenFile(NewFile);
     140end;
     141
     142procedure TOpenedFiles.OpenFile(Source: TSource);
     143begin
     144  AddNew(Source);
     145  Source.Form := TFormSourceCode.Create(nil);
     146  Source.Form.Caption := ExtractFileName(Source.Name);
     147  Source.Form.Visible := True;
     148  Selected := Source;
     149  if Assigned(FOnChange) then FOnChange(Self);
     150end;
     151
     152procedure TOpenedFiles.NewFile;
     153var
     154  NewFile: TSource;
     155begin
     156  NewFile := TSourceText(AddNew(TSource.Create));
     157  NewFile.Name := SNewFile;
    117158  NewFile.Form := TFormSourceCode.Create(nil);
    118   NewFile.Form.Caption := ExtractFileName(NewFile.FileName);
     159  NewFile.Form.Caption := ExtractFileName(NewFile.Name);
    119160  NewFile.Form.Visible := True;
    120161  Selected := NewFile;
     
    122163end;
    123164
    124 procedure TOpenedFiles.NewFile;
    125 var
    126   NewFile: TOpenedFile;
    127 begin
    128   NewFile := TOpenedFile(AddNew(TOpenedFile.Create));
    129   NewFile.FileName := SNewFile;
    130   NewFile.Form := TFormSourceCode.Create(nil);
    131   NewFile.Form.Caption := ExtractFileName(NewFile.FileName);
    132   NewFile.Form.Visible := True;
    133   Selected := NewFile;
    134   if Assigned(FOnChange) then FOnChange(Self);
    135 end;
    136 
    137165{ TDataModule1 }
    138166
    139167procedure TDataModule1.DataModuleCreate(Sender: TObject);
    140168begin
     169  FileTemplates := TFileTemplates.Create;
     170  FileTemplates.OwnsObjects := False;
    141171  FileTypes := TFileTypes.Create;
    142172  FileTypes.OwnsObjects := False;
     
    144174  ModuleManager := TModuleManager.Create(nil);
    145175  ModuleManager.API := API;
     176
     177  TextFileType := TTextFileType.Create;
     178  API.RegisterFileType(TextFileType);
     179  TextFileTemplate := TTextTemplate.Create;
     180  API.RegisterFileTemplate(TextFileTemplate);
    146181
    147182  FormMain := TFormMain.Create(nil);
     
    151186  FormSettings := TFormSettings.Create(nil);
    152187  FormModuleList := TFormModuleList.Create(nil);
    153 
     188  FormNewFile := TFormNewFile.Create(nil);
     189
     190  LoadFromRegistry(RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey));
     191
     192  DataModule1 := Self;
    154193  FormMain.Show;
     194  AutoOpen;
    155195end;
    156196
     
    191231end;
    192232
     233procedure TDataModule1.LoadFromRegistry(Context: TRegistryContext);
     234begin
     235  with TRegistryEx.Create do
     236  try
     237    RootKey := Context.RootKey;
     238    OpenKey(Context.Key, True);
     239
     240  finally
     241    Free;
     242  end;
     243  FormMain.LoadFromRegistry(Context);
     244end;
     245
     246procedure TDataModule1.SaveToRegistry(Context: TRegistryContext);
     247begin
     248  with TRegistryEx.Create do
     249  try
     250    RootKey := Context.RootKey;
     251    OpenKey(Context.Key, True);
     252
     253  finally
     254    Free;
     255  end;
     256  FormMain.SaveToRegistry(Context);
     257end;
     258
    193259procedure TDataModule1.APIDockForm(Form: TForm; Target: TDockTarget);
    194260begin
     
    203269end;
    204270
     271function TDataModule1.APIGetRegistryContext: TRegistryContext;
     272begin
     273  Result := RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey);
     274end;
     275
     276procedure TDataModule1.APIRegisterFileTemplate(Template: TFileTemplate);
     277begin
     278  FileTemplates.Add(Template);
     279end;
     280
    205281procedure TDataModule1.APIUnregisterAction(Action: TAction);
    206282begin
     
    208284end;
    209285
     286procedure TDataModule1.APIUnregisterFileTemplate(Template: TFileTemplate);
     287begin
     288  FileTemplates.Remove(Template);
     289end;
     290
    210291procedure TDataModule1.APIUnregisterFileType(FileType: TSourceType);
    211292begin
     
    220301procedure TDataModule1.DataModuleDestroy(Sender: TObject);
    221302begin
     303  API.UnregisterFileType(TextFileType);
     304  FreeAndNil(TextFileType);
     305  API.UnregisterFileTemplate(TextFileTemplate);
     306  FreeAndNil(TextFileTemplate);
     307
     308  SaveToRegistry(RegContext(HKEY(ApplicationInfo1.RegistryRoot), ApplicationInfo1.RegistryKey));
    222309  FreeAndNil(ModuleManager);
     310  FreeAndNil(FormNewFile);
    223311  FreeAndNil(FormMain);
    224312  FreeAndNil(FormSourceCode);
     
    227315  FreeAndNil(OpenedFiles);
    228316  FreeAndNil(FileTypes);
     317  FreeAndNil(FileTemplates);
     318end;
     319
     320procedure TDataModule1.AutoOpen;
     321var
     322  FileName: string;
     323begin
     324  if (ParamCount > 0) then begin
     325    FileName := UTF8Encode(ParamStr(1));
     326    if FileExistsUTF8(FileName) then begin
     327      OpenedFiles.OpenFileName(FileName);
     328    end;
     329  end;
    229330end;
    230331
    231332procedure TDataModule1.APIOpenFile(FileName: string);
    232333begin
    233   OpenedFiles.OpenFile(FileName);
     334  OpenedFiles.OpenFileName(FileName);
    234335end;
    235336
Note: See TracChangeset for help on using the changeset viewer.