unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ToolWin, Menus, Buttons, ShellAPI,
  SharedData, About, DistrDis, TextReport, MiscRoutines,
  Plan, GraphS, Init, ExtCtrls, SharedCode, CheckPoint,
  Step1, MathDef, Setup, StdCtrls, Sounds;

type
  TMainForm = class(TForm)
    MainMenu: TMainMenu;
    FileMenu: TMenuItem;
    NewItem: TMenuItem;
    ExitItem: TMenuItem;
    ExperimentMenu: TMenuItem;
    ImportDataItem: TMenuItem;
    ExportDataItem: TMenuItem;
    AverageItem: TMenuItem;
    AnalysisItem: TMenuItem;
    ServiceMenu: TMenuItem;
    GraphMenu: TMenuItem;
    DiagramItem: TMenuItem;
    SurfaceItem: TMenuItem;
    ExtremumItem: TMenuItem;
    ShowPirsonItem: TMenuItem;
    ShowCohrenItem: TMenuItem;
    ShowStudentItem: TMenuItem;
    ShowFisherItem: TMenuItem;
    HelpMenu: TMenuItem;
    HelpWelcomeItem: TMenuItem;
    HelpAboutItem: TMenuItem;
    ViewMenu: TMenuItem;
    ShowToolBarItem: TMenuItem;
    ShowStatusBarItem: TMenuItem;
    StatusBar: TStatusBar;
    ViewPopup: TPopupMenu;
    ShowToolBarItemP: TMenuItem;
    ShowStatusBarItemP: TMenuItem;
    UnbreakBandsP: TMenuItem;
    ShowMenuItem: TMenuItem;
    FormMenu: TMenuItem;
    ImportCItem: TMenuItem;
    ExportCItem: TMenuItem;
    CloseItem: TMenuItem;
    ExportNItem: TMenuItem;
    ImportMLItem: TMenuItem;
    ImportDFItem: TMenuItem;
    ExportMLItem: TMenuItem;
    ExportDFItem: TMenuItem;
    UpdateReportItem: TMenuItem;
    NewDataItem: TMenuItem;
    ImportMenu: TMenuItem;
    ExportMenu: TMenuItem;
    ImportAllItem: TMenuItem;
    ExportAllItem: TMenuItem;
    ExportReportItem: TMenuItem;
    ILNormal: TImageList;
    ClockTimer: TTimer;
    HTMLHItem: TMenuItem;
    OpenItem: TMenuItem;
    SaveItem: TMenuItem;
    MinimizeItem: TMenuItem;
    ArrangeItem: TMenuItem;
    OptionsItem: TMenuItem;
    TranslateItem: TMenuItem;
    SaveAsItem: TMenuItem;
    ShowMenuItemP: TMenuItem;
    ShowItem: TMenuItem;
    KNamesItem: TMenuItem;
    CascadeItem: TMenuItem;
    TileItem: TMenuItem;
    ArrangeIconsItem: TMenuItem;
    FullScreenItem: TMenuItem;
    RegenerateCodeItem: TMenuItem;
    ToolPanel: TPanel;
    AnimatePanel: TPanel;
    CoolPanel: TPanel;
    CoolBar: TCoolBar;
    ATorus: TAnimate;
    AnalisysToolBar: TToolBar;
    AnalisysButton: TToolButton;
    StartToolBar: TToolBar;
    NewButton: TToolButton;
    ResultsToolBar: TToolBar;
    ExtremumButton: TToolButton;
    ILHot: TImageList;
    ILDisabled: TImageList;
    OpenButton: TToolButton;
    ImportAllButton: TToolButton;
    ReportButton: TToolButton;
    DiagramButton: TToolButton;
    SurfaceButton: TToolButton;
    ExportDiagramItem: TMenuItem;
    ExportSurfaceItem: TMenuItem;
    ILCoolBar: TImageList;
    UnbreakBands: TMenuItem;
    CheckPointItem: TMenuItem;
    HelpInterfaceItem: TMenuItem;
    PlugInsMenu: TMenuItem;
    HelpQuickStartItem: TMenuItem;
    HelpQuickPlanItem: TMenuItem;
    s1: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    procedure OnMenuOnOff(Sender: TObject);
    procedure OnToolBarOnOff(Sender: TObject);
    procedure OnStatusBarOnOff(Sender: TObject);
    procedure ExitItemClick(Sender: TObject);
    procedure ShowPirsonItemClick(Sender: TObject);
    procedure ShowCohrenItemClick(Sender: TObject);
    procedure ShowStudentItemClick(Sender: TObject);
    procedure ShowFisherItemClick(Sender: TObject);
    procedure NewItemClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure NMFormItemClick(Sender: TObject);
    procedure YFormItemClick(Sender: TObject);
    procedure MLFormItemClick(Sender: TObject);
    procedure CMFormItemClick(Sender: TObject);
    procedure DFFormItemClick(Sender: TObject);
    procedure ShowItemClick(Sender: TObject);
    procedure HelpAboutItemClick(Sender: TObject);
    procedure ImportCItemClick(Sender: TObject);
    procedure ExportCItemClick(Sender: TObject);
    procedure CloseItemClick(Sender: TObject);
    procedure ImportDataItemClick(Sender: TObject);
    procedure ExportNItemClick(Sender: TObject);
    procedure ImportDFItemClick(Sender: TObject);
    procedure ImportMLItemClick(Sender: TObject);
    procedure ExportMLItemClick(Sender: TObject);
    procedure ExportDFItemClick(Sender: TObject);
    procedure ExportDataItemClick(Sender: TObject);
    procedure ReportFormItemClick(Sender: TObject);
    procedure UpdateReportItemClick(Sender: TObject);
    procedure AverageItemClick(Sender: TObject);
    procedure AnalysisItemClick(Sender: TObject);
    procedure DiagramItemClick(Sender: TObject);
    procedure ExtremumItemClick(Sender: TObject);
    procedure SurfaceItemClick(Sender: TObject);
    procedure HelpWelcomeItemClick(Sender: TObject);
    procedure ExportGraphMenuClick(Sender: TObject);
    procedure NewDataItemClick(Sender: TObject);
    procedure ImportAllItemClick(Sender: TObject);
    procedure ExportAllItemClick(Sender: TObject);
    procedure ExportReportItemClick(Sender: TObject);
    procedure ClockTimerTimer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure HTMLHItemClick(Sender: TObject);
    procedure MinimizeItemClick(Sender: TObject);
    procedure ArrangeItemClick(Sender: TObject);
    procedure OptionsItemClick(Sender: TObject);
    procedure OpenItemClick(Sender: TObject);
    procedure SaveItemClick(Sender: TObject);
    procedure SaveAsItemClick(Sender: TObject);
    procedure TranslateItemClick(Sender: TObject);
    procedure KNamesItemClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure CascadeItemClick(Sender: TObject);
    procedure TileItemClick(Sender: TObject);
    procedure ArrangeIconsItemClick(Sender: TObject);
    procedure FullScreenItemClick(Sender: TObject);
    procedure RegenerateCodeItemClick(Sender: TObject);
    procedure FormMenuClick(Sender: TObject);
    procedure UnbreakBandsPClick(Sender: TObject);
    procedure ExportDiagramItemClick(Sender: TObject);
    procedure ExportSurfaceItemClick(Sender: TObject);
    procedure CheckPointItemClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure HelpInterfaceItemClick(Sender: TObject);
    procedure ATorusStop(Sender: TObject);
    procedure PlugInsMenuClick(Sender: TObject);
    procedure HelpQuickPlanItemClick(Sender: TObject);
    procedure HelpQuickStartItemClick(Sender: TObject);
    procedure FileMenuClick(Sender: TObject);
    procedure ExportMenuClick(Sender: TObject);
    procedure ImportMenuClick(Sender: TObject);
    procedure ExperimentMenuClick(Sender: TObject);
    procedure GraphMenuClick(Sender: TObject);
  private
    MainFormInitialized : Boolean;
    MustChangeWindowStateFromSetup : Boolean;
    // Время последнего обновления статусной строки
    LastStatusBarUpdated : integer;
    PluginsList : TList;
    procedure HideToolBar;
    procedure ShowToolBar;
    procedure HideStatusBar;
    procedure ShowStatusBar;
    procedure HideMenu;
    procedure ShowMenu;
    procedure FullScreenMode(FullScreen:Boolean);
    procedure Cool(CoolStuff:Boolean);
    procedure OnDisplayChange(var Msg: TMessage);
      message WM_DISPLAYCHANGE;
    procedure ControlsFromPlan;
    procedure WaveOutDone(var Msg: TMessage);
      message $3BD; // WOM_DONE
  public
    // == True если требуется увести фокус от MDIchild - окна на
    // другое окно
    MustBringToFront : Boolean;
    // Сервер математики
    MainPlan   : TMathPlan;
    procedure SynchronizeMatrices;
    procedure StateChanged;
    procedure CaptionChanged;
    procedure SetupChanged;
  end;

var
  MainForm   : TMainForm;

implementation

{$R *.DFM}
{$R 'Media\Splash\Torus.res'}
{$R 'Media\Splash\Grey.res'}

uses Extensions;

procedure TMainForm.OnDisplayChange;
var
  bpp,cx,cy : integer;
begin
  bpp:=Msg.wParam;
  cx:=LOWORD(Msg.lParam);
  cy:=HIWORD(Msg.lParam);
  if(MainPlan<>nil) then
    if(MainPlan.GetGServer<>nil) then
      MainPlan.GetGServer^.DisplayChange(cx,cy,bpp);
  if(bpp<=8) then
    Cool(False)
  else
    Cool(True);
  if(WindowState=wsMaximized) then
  begin
    Left:=0;
    Top:=0;
    Width:=cx;
    Height:=cy;
    FormResize(Self);
  end;
end;

procedure TMainForm.Cool;
begin
  AnimatePanel.Visible:=CoolStuff;
  if(CoolStuff) then
  begin
    if(CoolBar.Bitmap.Empty) then
      CoolBar.Bitmap.LoadFromResourceId(0,IDB_COOLBITMAP);
  end
  else
  begin
    CoolBar.Bitmap.Handle:=0;
  end;
end;

procedure TMainForm.HideMenu;
begin
  if(ToolPanel.Visible or StatusBar.Visible or
    AppSetup.GeneralFlags.CanHideAllControls) then
  begin
    Menu:=nil;
    ShowMenuItem.Checked:=False;
    ShowMenuItemP.Checked:=False;
  end;
end;

procedure TMainForm.ShowMenu;
begin
  Menu:=MainMenu;
  ShowMenuItem.Checked:=True;
  ShowMenuItemP.Checked:=True;
end;

procedure TMainForm.HideToolBar;
begin
  ShowToolBarItem.Checked:=False;
  ShowToolBarItemP.Checked:=False;
  ToolPanel.Visible:=False;
  if(not (ToolPanel.Visible or StatusBar.Visible or
    AppSetup.GeneralFlags.CanHideAllControls)) then
  begin
    ShowMenu;
    ShowMenuItem.Enabled:=False;
    ShowMenuItemP.Enabled:=False;
  end;
end;

procedure TMainForm.ShowToolBar;
begin
  ShowToolBarItem.Checked:=True;
  ShowToolBarItemP.Checked:=True;
  ToolPanel.Visible:=True;
  ShowMenuItem.Enabled:=True;
  ShowMenuItemP.Enabled:=True;
end;

procedure TMainForm.HideStatusBar;
begin
  ShowStatusBarItem.Checked:=False;
  ShowStatusBarItemP.Checked:=False;
  StatusBar.Visible:=False;
  if(not (ToolPanel.Visible or StatusBar.Visible or
    AppSetup.GeneralFlags.CanHideAllControls)) then
  begin
    ShowMenu;
    ShowMenuItem.Enabled:=False;
    ShowMenuItemP.Enabled:=False;
  end;
end;

procedure TMainForm.ShowStatusBar;
begin
  ShowStatusBarItem.Checked:=True;
  ShowStatusBarItemP.Checked:=True;
  StatusBar.Visible:=True;
  ShowMenuItem.Enabled:=True;
  ShowMenuItemP.Enabled:=True;
end;

procedure TMainForm.OnMenuOnOff(Sender: TObject);
begin
  if(Menu<>nil) then
    HideMenu
  else
    ShowMenu;
end;

procedure TMainForm.OnToolBarOnOff(Sender: TObject);
begin
  if ToolPanel.Visible then
    HideToolBar
  else
    ShowToolBar;
end;

procedure TMainForm.OnStatusBarOnOff(Sender: TObject);
begin
  if(StatusBar.Visible)then
    HideStatusBar
  else
    ShowStatusBar;
  if(StatusBar.Visible or ToolPanel.Visible) then
  begin
    ShowMenuItem.Enabled:=True;
    ShowMenuItemP.Enabled:=True;
  end
  else
  begin
    if(Menu=nil) then OnMenuOnOff(Self);
    ShowMenuItem.Enabled:=False;
    ShowMenuItemP.Enabled:=False;
  end;
end;

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

procedure TMainForm.ShowPirsonItemClick(Sender: TObject);
begin
  if(not QuantilsForm.Visible) then
    QuantilsForm.Show;
  QuantilsForm.BringToFront;
  QuantilsForm.ActiveControl:=QuantilsForm.PirsonF;
end;

procedure TMainForm.ShowCohrenItemClick(Sender: TObject);
begin
  if(not QuantilsForm.Visible) then
    QuantilsForm.Show;
  QuantilsForm.BringToFront;
  QuantilsForm.ActiveControl:=QuantilsForm.CohrenN;
end;

procedure TMainForm.ShowStudentItemClick(Sender: TObject);
begin
  if(not QuantilsForm.Visible) then
    QuantilsForm.Show;
  QuantilsForm.BringToFront;
  QuantilsForm.ActiveControl:=QuantilsForm.StudentF;
end;

procedure TMainForm.ShowFisherItemClick(Sender: TObject);
begin
  if(not QuantilsForm.Visible) then
    QuantilsForm.Show;
  QuantilsForm.BringToFront;
  QuantilsForm.ActiveControl:=QuantilsForm.FisherN;
end;

procedure TMainForm.NewItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acNew))) then
    MainPlan.New;
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // ВНИМАНИЕ !!! Конструкцию: CanClose:=MainPlan.CanDo(acClose)
  // использовать нельзя - она предназначена для управления меню.
  CanClose:=True;
  if((MainPlan<>nil)and(MainPlan.CanDo(acClose))) then
    CanClose:=MainPlan.Close;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  MainFormInitialized:=False;
  MustChangeWindowStateFromSetup:=False;
  Application.Title:=SharedData.StrAppName;
  SharedData.StrAppHomePath:=ExtractFilePath(Application.ExeName);
  MainPlan:=nil;
  MainPlan:=TMathPlan.Create;
  PluginsList:=TList.Create;
  PlugIns:=TPlugIns.Create;
  ExternalModules:=TMathDefinition.Create;
  // Выставляем положение часов на панели инструментов
  FormResize(Self);
  LastStatusBarUpdated:=-1;
  // 15.03.00
  SharedData.StrFileName:='[без имени]';
end;

procedure TMainForm.NMFormItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acShowNatural))) then
    MainPlan.ShowNaturalMatrix;
end;

procedure TMainForm.YFormItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acShowY)) then
      MainPlan.ShowYMatrix;
end;

procedure TMainForm.MLFormItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acShowML)) then
      MainPlan.ShowMLVector;
end;

procedure TMainForm.CMFormItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acShowCode)) then
      MainPlan.ShowMainMatrix;
end;

procedure TMainForm.DFFormItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acShowDF)) then
      MainPlan.ShowDFVector;
end;

procedure TMainForm.HelpAboutItemClick(Sender: TObject);
begin
  ATorus.Active:=True;
  AboutBox.ShowModal;
  ATorus.Active:=False;
end;

procedure TMainForm.ImportCItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acImportCode)) then exit;
  MainPlan.ImportMainMatrix;
end;

procedure TMainForm.ExportCItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acExportCode)) then exit;
  MainPlan.ExportMainMatrix;
end;

procedure TMainForm.ExportNItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acExportNatural)) then exit;
  MainPlan.ExportNaturalMatrix;
end;

procedure TMainForm.CloseItemClick(Sender: TObject);
var
  CanClose : Boolean;
begin
  FormCloseQuery(Self,CanClose);
  if(not CanClose) then exit;
end;

procedure TMainForm.ControlsFromPlan;
begin
  if(MainPlan=nil) then exit;
  // Обновляем состояние кнопок панели инструментов
  NewButton.Enabled:=MainPlan.CanDo(acNew);
  OpenButton.Enabled:=MainPlan.CanDo(acOpen);
  ImportAllButton.Enabled:=MainPlan.CanDo(acImportAll);
  AnalisysButton.Enabled:=MainPlan.CanDo(acAnalysis);
  ReportButton.Enabled:=MainPlan.CanDo(acCreateReport);
  ExtremumButton.Enabled:=MainPlan.CanDo(acExtremum);
  DiagramButton.Enabled:=MainPlan.CanDo(acNewGraph);
  SurfaceButton.Enabled:=MainPlan.CanDo(acNewSurface);
  // Обновняем статусную строку
  StatusBar.Panels[0].Text:=MainPlan.GetStateString;
  LastStatusBarUpdated:=GetTickCount;
  // Обновняем заголовок
  CaptionChanged;
end;

procedure TMainForm.CaptionChanged;
var
  DriveDir,Ext : string;
begin
  if((MainPlan=nil)or
     (not MainPlan.CanDo(acSaveAs))) then
  begin
    if(MainPlan.Modified) then
      Caption:='(план изменен) - '+StrAppName
    else
      Caption:=StrAppName;
  end
  else
  begin
    if(StrLen(MainPlan.FileName)>0) then
      SplitPath(MainPlan.FileName,DriveDir,SharedData.StrFileName,Ext)
    else
      SharedData.StrFileName:='[без имени]';
    if(MainPlan.Modified) then
      Caption:=SharedData.StrFileName+' (изменен) - '+StrAppName
    else
      Caption:=SharedData.StrFileName+' - '+StrAppName;
  end;
end;

procedure TMainForm.NewDataItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acCreateY)) then exit;
  MainPlan.NewData;
end;

procedure TMainForm.ImportDataItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acImportY)) then exit;
  MainPlan.ImportYMatrix;
end;

procedure TMainForm.ImportMLItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acImportML)) then exit;
  MainPlan.ImportMLVector;
end;

procedure TMainForm.ImportDFItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acImportDF)) then exit;
  MainPlan.ImportDFVector;
end;

procedure TMainForm.ExportMLItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acExportML)) then exit;
  MainPlan.ExportMLVector;
end;

procedure TMainForm.ExportDFItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acExportDF)) then exit;
  MainPlan.ExportDFVector;
end;

procedure TMainForm.ExportDataItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acExportY)) then exit;
  MainPlan.ExportYMatrix;
end;

procedure TMainForm.ReportFormItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acShowReport)) then
    MainPlan.ShowReport;
end;

procedure TMainForm.UpdateReportItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acCreateReport)) then
  begin
    MainPlan.CreateReport;
    if(not ReportForm.Visible) then
      MainPlan.ShowReport;
    if(ReportForm.WindowState=wsMinimized) then
      ReportForm.WindowState:=wsNormal;
    ReportForm.BringToFront;
  end;
end;

procedure TMainForm.AverageItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acAverageY))) then
    MainPlan.ShowAverageData;
end;

procedure TMainForm.AnalysisItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acAnalysis))) then
    MainPlan.FullAnalysis;
end;

procedure TMainForm.ExtremumItemClick(Sender: TObject);
begin
  if((MainPlan=nil) or
     (not MainPlan.CanDo(acExtremum))) then exit;
  MainPlan.FindExtremum;
end;

procedure TMainForm.DiagramItemClick(Sender: TObject);
begin
  if((MainPlan=nil) or
     (not MainPlan.CanDo(acNewGraph))) then exit;
  MainPlan.NewGraph;
end;

procedure TMainForm.SurfaceItemClick(Sender: TObject);
begin
  if((MainPlan=nil) or
     (not MainPlan.CanDo(acNewSurface))) then
    exit;
  MainPlan.NewSurface;
end;

procedure TMainForm.ExportGraphMenuClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acExportGraph)) then
    MainPlan.ExportGraph;
end;

procedure TMainForm.ImportAllItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acImportAll)) then
    MainPlan.ImportAll;
end;

procedure TMainForm.ExportAllItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acExportAll)) then
    MainPlan.ExportAll;
end;

procedure TMainForm.ExportReportItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(MainPlan.CanDo(acExportReport)) then
    MainPlan.ExportReport;
end;

procedure TMainForm.ClockTimerTimer(Sender: TObject);
const
  MessageDelayMS = 10000;
begin
  if(ClockTimer.Interval < 1000) then
    ClockTimer.Interval := 1000;
  if(MustChangeWindowStateFromSetup) then
  begin
    IniForm.Close;
    MustChangeWindowStateFromSetup:=False;
    WindowState:=AppSetup.GeneralFlags.WindowState;
    if(WindowState=wsNormal) then
    begin
      Width:=AppSetup.GeneralFlags.Width;
      Height:=AppSetup.GeneralFlags.Height;
      Left:=AppSetup.GeneralFlags.Left;
      Top:=AppSetup.GeneralFlags.Top;
    end;
    if(AppSetup.GeneralFlags.ToolBarVisible) then
      ShowToolBar
    else
      HideToolBar;
    if(AppSetup.GeneralFlags.StatusBarVisible) then
      ShowStatusBar
    else
      HideStatusBar;
    if(AppSetup.GeneralFlags.MenuBarVisible) then
      ShowMenu
    else
      HideMenu;
    FullScreenItem.Checked:=AppSetup.GeneralFlags.FullScreen;
    if(AppSetup.GeneralFlags.FullScreen) then FullScreenMode(True);
    // В конце инициализации приверяем, не передано ли чего в командной
    // строке.
    if(ParamCount=1) then
    begin
      if(FileExists(ParamStr(1))) then
        MainPlan.Open(PChar(ParamStr(1)))
      else
        Warning('Файл не "'+ParamStr(1)+'" найден.');
    end;
  end;
  if((LastStatusBarUpdated <> -1) and
     ((GetTickCount - LastStatusBarUpdated) > MessageDelayMS)) then
  begin
    if(MainPlan <> nil) then
      MainPlan.ClearStatus;
    StatusBar.Panels[0].Text:=MainPlan.GetStateString;
  end;
  if(MustBringToFront) then
  begin
    MustBringToFront:=False;
    if(CheckPointDlg.Visible) then
      CheckPointDlg.BringToFront;
  end;
  if(ATorus.Active) then
  begin
    ChooseAmbSound;
  end;
  StatusBar.Panels[1].Text:=GetSystemTimeStr;
  StatusBar.Panels[2].Text:=GetSystemDateStr;
end;

procedure TMainForm.HelpWelcomeItemClick(Sender: TObject);
begin
  Help(HELP_CONTENTS,0);
end;

procedure TMainForm.FormShow(Sender: TObject);
var
  Bpp : integer;
  i : integer;
begin
  if(MainFormInitialized) then exit;
  MainFormInitialized:=True;
  MustBringToFront:=False;
  Bpp:=GetDeviceCaps(IniForm.Canvas.Handle,BITSPIXEL);
  ATorus.ResId:=IDA_TORUS;
  Cool(Bpp>8);
  Sounds.Initialize;
  PlugIns.FindPlugIns(
    ExtractFilePath(Application.Exename)+StrPlugInsDir);
  if(PlugIns.Count>0) then
  begin
    for i:=0 to PlugIns.Count-1 do
    begin
      PluginsList.Add(TMenuItem.Create(Self));
      TMenuItem(PluginsList[i]).Caption:=PlugIns.Items[i]^.MenuName;
      TMenuItem(PluginsList[i]).OnClick:=PlugIns.Items[i]^.PluginMainThunk;
      TMenuItem(PluginsList[i]).Tag:=integer(PlugIns.Items[i]);
      PlugInsMenu.Add(PluginsList[i]);
    end;
  end
  else
  begin
    // 26.10.00
  	PlugInsMenu.Enabled:=False;
  	(*
    26.10.00
    PluginsList.Add(TMenuItem.Create(Self));
    TMenuItem(PluginsList[0]).Caption:='(дополнения не найдены)';
    TMenuItem(PluginsList[0]).Enabled:=False;
    TMenuItem(PluginsList[0]).Tag:=0;
    PlugInsMenu.Add(PluginsList[0]);
    *)
  end;
  ExternalModules.FindModules(
    ExtractFilePath(Application.Exename)+StrModulesDir);
  if(ExternalModules.ModuleCount=0) then
  begin
    Warning('Не найдено ни одного внешнего модуля.'+#13+
      'Вы сможете использовать только встроенные планы.');
      StepOneForm.RBFromGen.Enabled:=False;
      StepOneForm.RBCustom.Enabled:=False;
      StepOneForm.RBFromGen.Checked:=False;
      StepOneForm.RBCustom.Checked:=False;
      StepOneForm.RBOld.Checked:=True;
  end
  else
  begin
    if(ExternalModules.AddenCount=0) then
    begin
      Warning('Внешние модули не содержат слагаемых.'+#13+
        'Вы сможете использовать только встроенные планы.');
      StepOneForm.RBFromGen.Enabled:=False;
      StepOneForm.RBCustom.Enabled:=False;
      StepOneForm.RBFromGen.Checked:=False;
      StepOneForm.RBCustom.Checked:=False;
      StepOneForm.RBOld.Checked:=True;
    end
    else
    begin
      if(ExternalModules.GenCount=0) then
      begin
        // Нет генераторов.
        Warning('Внешние модули не содержат стандартных планов.'+#13+
          'Вам придется самостоятельно определять кодовую матрицу'+#13+
          'или использовать встроенные планы.');
        StepOneForm.RBFromGen.Enabled:=False;
      end;
    end;
  end;
  // Проверяем, какой из двух символов - точка или запятая - используется
  // для разделения целой и дробной частей числа.
  SharedData.DecimalSymbol:=FloatToStr(1.1)[2];
  StateChanged;
  // Устанавливаем размер окна
  MustChangeWindowStateFromSetup := True;
  // 25 nov 2011, kkatarn@sleepgate.ru
  // Меня добил этот тормозной таймер! Блин, 12 лет тормозов!
  ClockTimer.Interval := 10;
end;

procedure TMainForm.SynchronizeMatrices;
begin
  if(MainPlan<>nil) then
    MainPlan.SynchronizeMatrices(Self);
end;

procedure TMainForm.StateChanged;
begin
  if(MainPlan<>nil) then ControlsFromPlan;
end;

procedure TMainForm.SetupChanged;
begin
  CoolBar.ShowHint:=AppSetup.GeneralFlags.ShowHints;
  if(MainPlan<>nil) then MainPlan.OnChangeSetup;
end;

procedure TMainForm.HTMLHItemClick(Sender: TObject);
begin
  ShellExecute(Handle,'open',
    PChar(StrAppHomePath+StrAppHtmlFile),
    nil,nil,SW_SHOWNORMAL);
end;

procedure TMainForm.MinimizeItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acMinimizeForms)) then
      MainPlan.MinimizeForms;
end;

procedure TMainForm.ShowItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acShowForms)) then
      MainPlan.ShowForms;
end;

procedure TMainForm.ArrangeItemClick(Sender: TObject);
var
  Rc : TRect;
begin
  if(MainPlan<>nil) then
  begin
    if(MainPlan.CanDo(acArrangeForms)) then
    begin
      Rc:=ClientRect;
      Dec(Rc.right,4);
      Dec(Rc.bottom,4);
      if(ToolPanel.Visible) then
        Dec(Rc.bottom,ToolPanel.Height);
      if(StatusBar.Visible) then
        Dec(Rc.bottom,StatusBar.Height);
      Cascade;
      MainPlan.ArrangeForms(Rc);
    end;
  end;
end;

procedure TMainForm.OptionsItemClick(Sender: TObject);
begin
  OptionsForm.ShowModal;
end;

procedure TMainForm.OpenItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acOpen)) then exit;
  MainPlan.Open(nil);
end;

procedure TMainForm.SaveItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acSave)) then exit;
  MainPlan.Save;
end;

procedure TMainForm.SaveAsItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acSaveAs)) then exit;
  MainPlan.SaveAs;
end;

procedure TMainForm.TranslateItemClick(Sender: TObject);
begin
  if(MainPlan=nil) then exit;
  if(not MainPlan.CanDo(acTranslate)) then exit;
  MainPlan.Translate;
end;

procedure TMainForm.KNamesItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acKNames)) then
      MainPlan.SetKNames;
end;

procedure TMainForm.FormResize(Sender: TObject);
begin
  StatusBar.Panels[0].Width:=ClientWidth-120;
  StatusBar.Panels[1].Width:=60;
  StatusBar.Panels[1].Text:=GetSystemTimeStr;
  StatusBar.Panels[2].Text:=GetSystemDateStr;
end;

procedure TMainForm.CascadeItemClick(Sender: TObject);
begin
  Cascade;
end;

procedure TMainForm.TileItemClick(Sender: TObject);
begin
  Tile;
end;

procedure TMainForm.ArrangeIconsItemClick(Sender: TObject);
begin
  ArrangeIcons;
end;

procedure TMainForm.FullScreenMode(FullScreen:Boolean);
var
  c,n,m,d,y,r,g : Boolean;
  t,s : Boolean;
  procedure Hide;
  begin
    MainPlan.TemporaryClose(c,n,m,d,y,r,g);
    t:=ToolPanel.Visible;
    s:=StatusBar.Visible;
    if(t) then HideToolBar;
    if(s) then HideStatusBar;
  end;
  procedure Show;
  begin
    MainPlan.RestoreVisibleForms(c,n,m,d,y,r,g);
    if(t) then ShowToolBar;
    if(s) then ShowStatusBar;
  end;
begin
  if(FullScreen) then
  begin
    Hide;
    WindowState:=wsMaximized;
    BorderStyle:=bsNone;
    Show;
  end
  else
  begin
    Hide;
    BorderStyle:=bsSizeable;
    Show;
  end;
end;

procedure TMainForm.FullScreenItemClick(Sender: TObject);
begin
  if(BorderStyle=bsSizeable) then
    FullScreenMode(True)
  else
    FullScreenMode(False);
end;

procedure TMainForm.RegenerateCodeItemClick(Sender: TObject);
begin
  if(MainPlan<>nil) then
    if(MainPlan.CanDo(acRegenerateCode)) then
      MainPlan.RegenerateCode;
end;

procedure TMainForm.FormMenuClick(Sender: TObject);
begin
  ControlsFromPlan;
  // Окно
  MinimizeItem.Enabled:=MainPlan.CanDo(acMinimizeForms);
  ShowItem.Enabled:=MainPlan.CanDo(acShowForms);
  ArrangeItem.Enabled:=MainPlan.CanDo(acArrangeForms);
end;

procedure TMainForm.UnbreakBandsPClick(Sender: TObject);
var
  i : integer;
begin
  for i:=0 to 2 do
  begin
    CoolBar.Bands[i].Break:=False;
    CoolBar.Bands[i].Width:=CoolBar.Bands[i].MinWidth;
  end;
  CoolBar.Bands[0].Width:=219;
  CoolBar.Bands[1].Width:=151;
  CoolBar.Bands[2].Width:=314;
end;

procedure TMainForm.ExportDiagramItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acExportGraph))) then
    MainPlan.ExportGraph;
end;

procedure TMainForm.ExportSurfaceItemClick(Sender: TObject);
begin
  if((MainPlan<>nil)and(MainPlan.CanDo(acExportSurface))) then
    MainPlan.ExportSurface;
end;

procedure TMainForm.CheckPointItemClick(Sender: TObject);
begin
  if((MainPlan<>nil) and (MainPlan.CanDo(acCheckPoint))) then
    MainPlan.CheckPoint;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
var
  i,l : integer;
begin
  l:=PluginsList.Count;
  for i:=0 to l-1 do
  begin
    PlugInsMenu.Delete(0);
    TMenuItem(PluginsList[i]).Free;
  end;
  PluginsList.Free;
  PlugIns.Destroy;
  ExternalModules.Destroy;
  MainPlan.Destroy;
  Sounds.Finalize;
  MainPlan:=nil;
  Help(HELP_QUIT,0);
end;

procedure TMainForm.HelpInterfaceItemClick(Sender: TObject);
begin
  Help(HELP_CONTEXT,IDH_CONTENS);
end;

procedure TMainForm.ATorusStop(Sender: TObject);
begin
  Sounds.ClosePlayer;
end;

procedure TMainForm.WaveOutDone;
begin
  Sounds.ClosePlayer;
end;

procedure TMainForm.PlugInsMenuClick(Sender: TObject);
var
  i : integer;
begin
  for i:=0 to PluginsList.Count-1 do
  begin
    if(TMenuItem(PluginsList[i]).Tag<>0) then
    begin
      TMenuItem(PluginsList[i]).Enabled:=
        pIPlugIn(TMenuItem(PluginsList[i]).Tag).CanDo;
    end;
  end;
end;

procedure TMainForm.HelpQuickStartItemClick(Sender: TObject);
begin
  Help(HELP_CONTEXT,IDH_QUICKSTART);
end;

procedure TMainForm.HelpQuickPlanItemClick(Sender: TObject);
begin
  Help(HELP_CONTEXT,IDH_QUICKPLAN);
end;

procedure TMainForm.FileMenuClick(Sender: TObject);
begin
  NewItem.Enabled:=MainPlan.CanDo(acNew);
  OpenItem.Enabled:=MainPlan.CanDo(acOpen);
  SaveItem.Enabled:=MainPlan.CanDo(acSave);
  SaveAsItem.Enabled:=MainPlan.CanDo(acSaveAs);
  CloseItem.Enabled:=MainPlan.CanDo(acClose);
  UpdateReportItem.Enabled:=MainPlan.CanDo(acCreateReport);
end;

procedure TMainForm.ExportMenuClick(Sender: TObject);
begin
  // Экспорт
  ExportCItem.Enabled:=MainPlan.CanDo(acExportCode);
  ExportNItem.Enabled:=MainPlan.CanDo(acExportNatural);
  ExportMLItem.Enabled:=MainPlan.CanDo(acExportML);
  ExportDFItem.Enabled:=MainPlan.CanDo(acExportDF);
  ExportDataItem.Enabled:=MainPlan.CanDo(acExportY);
  ExportAllItem.Enabled:=MainPlan.CanDo(acExportAll);
  ExportReportItem.Enabled:=MainPlan.CanDo(acExportReport);
end;

procedure TMainForm.ImportMenuClick(Sender: TObject);
begin
  // Импорт
  ImportMLItem.Enabled:=MainPlan.CanDo(acImportML);
  ImportDFItem.Enabled:=MainPlan.CanDo(acImportDF);
  ImportDataItem.Enabled:=MainPlan.CanDo(acImportY);
  ImportAllItem.Enabled:=MainPlan.CanDo(acImportAll);
end;

procedure TMainForm.ExperimentMenuClick(Sender: TObject);
begin
  // Эксперимент
  KNamesItem.Enabled:=MainPlan.CanDo(acKNames);
  NewDataItem.Enabled:=MainPlan.CanDo(acCreateY);
  CheckPointItem.Enabled:=MainPlan.CanDo(acCheckPoint);
  AverageItem.Enabled:=MainPlan.CanDo(acAverageY);
  AnalysisItem.Enabled:=MainPlan.CanDo(acAnalysis);
  ExtremumItem.Enabled:=MainPlan.CanDo(acExtremum);
  TranslateItem.Enabled:=MainPlan.CanDo(acTranslate);
  RegenerateCodeItem.Enabled:=MainPlan.CanDo(acRegenerateCode);
end;

procedure TMainForm.GraphMenuClick(Sender: TObject);
begin
  // Диаграмма
  DiagramItem.Enabled:=MainPlan.CanDo(acNewGraph);
  SurfaceItem.Enabled:=MainPlan.CanDo(acNewSurface);
  ExportDiagramItem.Enabled:=MainPlan.CanDo(acExportGraph);
  ExportSurfaceItem.Enabled:=MainPlan.CanDo(acExportSurface);
end;

end.

