unit Main;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, Menus,
  StdCtrls, Dialogs, Buttons, Messages, ExtCtrls, ComCtrls, StdActns,
  ActnList, ToolWin, ImgList,Powercad,pcGui,pcTypesUtils, PcPluginDlg,
  PCBlockDlg, PCLayerDlg, DlgBase, PCMacroDlg,PCFileDlgs, PCFillDlg;

type
  TMainForm = class(TForm)
    OpenDialog: TOpenDialog;
    GUI: TCadInterface;
    PCMacroDialog1: TPCMacroDialog;
    PCLayerDlg1: TPCLayerDlg;
    PCBlockDlg1: TPCBlockDlg;
    PCPluginDlg1: TPCPluginDlg;
    PCFillDlg1: TPCFillDlg;
    procedure FileOpen1Execute(Sender: TObject);
    procedure HelpAbout1Execute(Sender: TObject);
    procedure FileExit1Execute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GUIMDINewFile(Sender: TObject);
    procedure GUIMDIOpenFile(Sender: TObject; FileName: String);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    procedure CreateMDIChild(const Name: string);

    procedure OnCustomized(Sender: TObject);
    procedure WMenuClicked(Sender: TObject);
  public
    { Public declarations }
    WMenu: TMenuItem;
    Procedure CreateWindowMenu;
  end;

var
  MainForm: TMainForm;


implementation

{$R *.DFM}

uses ChildWin, About;

procedure TMainForm.CreateMDIChild(const Name: string);
var
  Child: TMDIChild;
begin
  { create a new MDI child window }
  Child := TMDIChild.Create(Application);

  if (name <> '') and (FileExists(Name)) then begin
    Child.Powercad1.Updated := False;
    Child.Powercad1.cOpenDrawingFile(Name);
  end;
  Child.Caption := Child.Powercad1.ActiveFile;
end;

procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
  if OpenDialog.Execute then
    CreateMDIChild(OpenDialog.FileName);
end;

procedure TMainForm.HelpAbout1Execute(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

procedure TMainForm.FileExit1Execute(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.FormCreate(Sender: TObject);
var PlgDir,MacroDir,BlockDir: String;
var xItem: TmenuItem;
begin
  PlgDir := '';

  RegRead('PlgDir',PlgDir);
  RegRead('MacroDir',MacroDir);
  RegRead('BlockDir',BlockDir);

  if (BlockDir = '') then
  begin
    if (not DirectoryExists(extractfilepath(application.ExeName)+'blocks')) then
      CreateDir(extractfilepath(application.ExeName)+'blocks');
    BlockDir := extractfilepath(application.ExeName)+'blocks'
  end;

  if (plgDir = '') then
  begin
    if (not DirectoryExists(extractfilepath(application.ExeName)+'plugins')) then
      CreateDir(extractfilepath(application.ExeName)+'plugins');
    PlgDir := extractfilepath(application.ExeName)+'plugins';
  end;

  if (MacroDir = '')  then
  begin
    if (not DirectoryExists(extractfilepath(application.ExeName)+'macros')) then
      CreateDir(extractfilepath(application.ExeName)+'macros');
    MacroDir := extractfilepath(application.ExeName)+'macros';
  end;
  PCPluginDlg1.PluginsDirectory := plgDir;
  PCBlockDlg1.BlockDirectory := BlockDir;
  PCMacroDialog1.MacroDirectory  := MacroDir;
  Gui.CadControl := nil;
  Gui.LoadFromFile(ExtractFilePath(application.ExeName)+'\Normal.dat' );
  GUi.OnCusotmized := OnCustomized;
  CreateWindowMenu;
    GUI.CadControl := nil;
    PCMacroDialog1.CadControl := nil;
    PCBlockDlg1.CadControl := nil;
    PCLayerDlg1.CadControl := nil;
    PCPluginDlg1.CadControl := nil;
    PCFillDlg1.CadControl := nil;
end;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if assigned(ActiveMDIChild) then begin
      TMDIChild(ActiveMDIChild).Powercad1.DoKeyStroke(Key,DelphiSetToOleShift(Shift));
  end;
end;

procedure TMainForm.GUIMDINewFile(Sender: TObject);
begin
  CreateMDIChild('');
  CreateWindowMenu;
end;

procedure TMainForm.GUIMDIOpenFile(Sender: TObject; FileName: String);
var Dlg: TPCOpenDialog;
begin
  dlg := TPCOpenDialog.Create(self);
  if dlg.Execute then begin
    CreateMDIChild(dlg.FileName);
  end;
  dlg.free;
  CreateWindowMenu;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Gui.SaveToFile(ExtractFilePath(application.ExeName)+'\Normal.dat');
end;

procedure TMainForm.CreateWindowMenu;
var i: Integer;
    xForm: TForm;
    xItem: TMenuItem;
begin
  if Menu.Items.IndexOf(WMenu) > -1 then begin
    Menu.Items.Remove(WMenu);
  end;
  WMenu := TMenuItem.Create(Menu);
  WMenu.Caption := 'Windows';
  Menu.Items.Add(WMenu);
  for i := 0 to Self.MDIChildCount -1 do
  begin
    xForm := Self.MDIChildren[i];
    xItem := TmenuItem.Create(WMenu);
    xItem.Caption := xForm.Caption;
    xItem.OnClick := WMenuClicked;
    xItem.Tag := Integer(xForm);
    Wmenu.Add(xItem);
  end;
end;

procedure TMainForm.OnCustomized(Sender: TObject);
begin
  CreateWindowMenu;
end;

procedure TMainForm.WMenuClicked(Sender: TObject);
var xForm: TForm;
begin
  xForm := TForm((Sender as TmenuItem).Tag);
  xForm.BringToFront;
end;

end.
