Ignore:
Timestamp:
Oct 18, 2010, 10:00:00 AM (14 years ago)
Author:
george
Message:
  • Added: Storing application setting in system registry.
  • Added: Loading project files.
  • Added: Remembering last opened files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Transpascal/Forms/UMainForm.pas

    r64 r66  
    77uses
    88  SysUtils, Variants, Classes, Graphics, Controls, Forms,
    9   Dialogs, StdCtrls, UCompiler, UProducerAsm8051,
     9  Dialogs, StdCtrls, UCompiler, UProducerAsm8051, Registry,
    1010  UProducerC, ComCtrls, ExtCtrls, SynEdit, SynHighlighterPas, UProducerTreeView,
    1111  UProducerPascal, Contnrs, UProject, FileUtil, Menus, ActnList, UCoolDocking,
    12   UCompiledForm, UCodeTreeForm;
     12  UCompiledForm, UCodeTreeForm, URegistry, ULastOpenedList, UApplicationInfo;
    1313
    1414const
    15   ProjectDir = '../../Project/';
     15  RegistryRootKey = HKEY_CURRENT_USER;
     16  RegistryKey = 'Software\Transpascal';
    1617
    1718type
     
    4041    MenuItem8: TMenuItem;
    4142    DockPanel: TPanel;
     43    MenuItemOpenRecent: TMenuItem;
     44    OpenDialog1: TOpenDialog;
     45    SaveDialog1: TSaveDialog;
    4246    Splitter3: TSplitter;
    4347    ToolBar1: TToolBar;
     48    procedure AProjectOpenExecute(Sender: TObject);
     49    procedure AProjectSaveAsExecute(Sender: TObject);
    4450    procedure ComboBox1Change(Sender: TObject);
    4551    procedure ComboBoxTargetSelectionChange(Sender: TObject);
     
    5056    procedure FormDestroy(Sender: TObject);
    5157  private
     58    LastOpenedFiles: TLastOpenedList;
     59    ReopenLastOpenedFile: Boolean;
     60    procedure OpenRecentClick(Sender: TObject);
    5261    procedure LoadErrorMessages;
    53     procedure UpdateProjectTree;
    54     procedure UpdateProjectGroup(Node: TTreeNode; Group: TProjectGroup);
    5562    procedure DockInit;
     63    procedure LoadFromRegistry;
     64    procedure SaveToRegistry;
     65    procedure OpenProject(FileName: string);
    5666  public
    5767    Project: TProject;
     
    96106  Compiler.Init;
    97107  Compiler.Parser.OnGetSource := GetSource;
     108  if Project.Items.Count > 0 then
    98109  with TProjectFile(Project.Items[0]) do begin
    99110    Compiler.Compile(Parent.GetDir + Name, Source);
     
    117128end;
    118129
    119 procedure TMainForm.UpdateProjectTree;
    120 var
    121   NewNode: TTreeNode;
    122 begin
    123   with ProjectManager, TreeViewProject, Items do begin
    124     BeginUpdate;
    125     Clear;
    126     NewNode := AddChild(nil, Project.Name);
    127     UpdateProjectGroup(NewNode, Project);
    128     NewNode.Expand(True);
    129     EndUpdate;
    130     TreeViewProject.TopItem.Items[0].Selected := True;
    131   end;
    132 end;
    133 
    134 procedure TMainForm.UpdateProjectGroup(Node: TTreeNode; Group: TProjectGroup);
    135 var
    136   I: Integer;
    137   NewNode: TTreeNode;
    138 begin
    139   with ProjectManager do
    140   for I := 0 to Group.Items.Count - 1 do begin
    141     NewNode := TreeViewProject.Items.AddChild(Node,
    142       TProjectNode(Group.Items[I]).Name);
    143     NewNode.Data := Pointer(Group.Items[I]);
    144     if TProjectNode(Group.Items[I]) is TProjectGroup then
    145       UpdateProjectGroup(NewNode, TProjectGroup(Group.Items[I]));
    146   end;
    147 end;
    148 
    149130function TMainForm.GetSource(Name: string; Source: TStringList): Boolean;
    150131var
     
    195176end;
    196177
     178procedure TMainForm.LoadFromRegistry;
     179begin
     180  with TRegistryEx.Create do
     181    try
     182      RootKey := HKEY_CURRENT_USER;
     183      OpenKey(RegistryKey, True);
     184      ReopenLastOpenedFile := ReadBoolWithDefault('ReopenLastOpenedFile', True);
     185    finally
     186      Free;
     187    end;
     188  LastOpenedFiles.LoadFromRegistry(RegistryRootKey, RegistryKey);
     189end;
     190
     191procedure TMainForm.SaveToRegistry;
     192begin
     193  with TRegistryEx.Create do
     194    try
     195      RootKey := RegistryRootKey;
     196      OpenKey(RegistryKey, True);
     197      WriteBool('ReopenLastOpenedFile', ReopenLastOpenedFile);
     198    finally
     199      Free;
     200    end;
     201  LastOpenedFiles.SaveToRegistry(RegistryRootKey, RegistryKey);
     202  LastOpenedFiles.ReloadMenu;
     203end;
     204
     205procedure TMainForm.OpenProject(FileName: string);
     206begin
     207  Project.LoadFromFile(FileName);
     208  LastOpenedFiles.Add(FileName);
     209  LastOpenedFiles.ReloadMenu;
     210  ProjectManager.UpdateProjectTree;
     211  Caption := ApplicationInfo.Name + ' - ' + Project.Name;
     212end;
     213
    197214procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
    198215begin
    199216  Project.Save;
     217  SaveToRegistry;
    200218end;
    201219
     
    204222  Compiler := TCompiler.Create;
    205223  Project := TProject.Create;
     224  LastOpenedFiles := TLastOpenedList.Create;
     225  LastOpenedFiles.MenuItem := MenuItemOpenRecent;
     226  LastOpenedFiles.ClickAction := OpenRecentClick;
    206227end;
    207228
    208229procedure TMainForm.FormDestroy(Sender: TObject);
    209230begin
     231  LastOpenedFiles.Free;
    210232  Project.Free;
    211233  Compiler.Free;
    212234end;
    213235
     236procedure TMainForm.OpenRecentClick(Sender: TObject);
     237begin
     238  if Sender is TMenuItem then
     239     OpenProject(StringReplace(TMenuItem(Sender).Caption, '&', '', [rfReplaceAll]));
     240end;
     241
    214242procedure TMainForm.FormShow(Sender: TObject);
    215243var
    216244  NewProjectFile: TProjectFile;
    217245begin
     246  LoadFromRegistry;
    218247  DockInit;
    219248
    220   Project.RootDir := '../../Project/';
    221   Project.Name := 'Project';
    222 
    223   NewProjectFile := TProjectFile.Create;
    224   NewProjectFile.Parent := Project;
    225   NewProjectFile.Name := 'Project.pas';
    226   Project.Items.Add(NewProjectFile);
    227 
    228   NewProjectFile := TProjectFile.Create;
    229   NewProjectFile.Parent := Project;
    230   NewProjectFile.Name := 'System.pas';
    231   Project.Items.Add(NewProjectFile);
    232 
    233   NewProjectFile := TProjectFile.Create;
    234   NewProjectFile.Parent := Project;
    235   NewProjectFile.Name := 'MemoryManager.pas';
    236   Project.Items.Add(NewProjectFile);
    237 
    238   Project.Load;
    239   UpdateProjectTree;
     249  if ReopenLastOpenedFile and (LastOpenedFiles.Count > 0) then begin
     250    OpenProject(LastOpenedFiles[0]);
     251  end;
    240252
    241253  WindowState := wsMaximized;
     254end;
     255
     256procedure TMainForm.ComboBox1Change(Sender: TObject);
     257begin
    242258  ButtonCompileClick(Self);
    243259end;
    244260
    245 procedure TMainForm.ComboBox1Change(Sender: TObject);
     261procedure TMainForm.AProjectOpenExecute(Sender: TObject);
     262begin
     263  if OpenDialog1.Execute then begin
     264    OpenProject(OpenDialog1.FileName);
     265  end;
     266end;
     267
     268procedure TMainForm.AProjectSaveAsExecute(Sender: TObject);
     269begin
     270  if SaveDialog1.Execute then begin
     271    Project.SaveToFile(SaveDialog1.FileName);
     272  end;
     273end;
     274
     275procedure TMainForm.ComboBoxTargetSelectionChange(Sender: TObject);
    246276begin
    247277  ButtonCompileClick(Self);
    248278end;
    249279
    250 procedure TMainForm.ComboBoxTargetSelectionChange(Sender: TObject);
    251 begin
    252   ButtonCompileClick(Self);
    253 end;
    254 
    255280end.
Note: See TracChangeset for help on using the changeset viewer.