unit Main;

interface

uses WinTypes, WinProcs, SysUtils, Classes, Graphics, Forms, Controls, Menus,
  StdCtrls, Dialogs, Buttons, Messages, ExtCtrls,
  {$IFDEF WIN32}
  commint, commfrm,
  {$ELSE}
  comm,
  {$ENDIF}
  TVType, cmgh, logu,
  mytype, apextype, ulantype, UlanGlob, IniObj, ULObju,
  ULFObju, ComCtrls, ULMaGlob, Modulu, FileMenuHdl,
  WinUtl, Fileu, BomeOneInstance{, HJHelp{, HtmlHelp}
  {v0.13}, FileNewDlg, ExtComDl{/v0.13}{v0.14},Channelsu, DevMode{/v0.14}
  {v0.18},ExpImpu{/v0.18};

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    Panel1: TPanel;
    StatusLine: TPanel;
    File_Item: TMenuItem;
    FileNew_Item: TMenuItem;
    FileOpen_Item: TMenuItem;
    Panel2: TPanel;
    FileClose_Item: TMenuItem;
    Window_Item: TMenuItem;
    Help_Item: TMenuItem;
    N1: TMenuItem;
    FileExit_Item: TMenuItem;
    WindowCascade_Item: TMenuItem;
    WindowTile_Item: TMenuItem;
    WindowArrangeIcons_Item: TMenuItem;
    HelpAbout_Item: TMenuItem;
    OpenDialog: TOpenDialog;
    FileSave_Item: TMenuItem;
    FileSaveAs_Item: TMenuItem;
    Edit_Item: TMenuItem;
    EditCut_Item: TMenuItem;
    EditCopy_Item: TMenuItem;
    EditPaste_Item: TMenuItem;
    WindowMinimizeAll_Item: TMenuItem;
    SpeedPanel: TPanel;
    FileOpen_Btn: TSpeedButton;
    FileSave_Btn: TSpeedButton;
    EditCut_Btn: TSpeedButton;
    EditCopy_Btn: TSpeedButton;
    EditPaste_Btn: TSpeedButton;
    FileExit_Btn: TSpeedButton;
    Chromatogram_Btn: TSpeedButton;
    FileNew_Btn: TSpeedButton;
    Tools_Item: TMenuItem;
    ToolsDebug_Item: TMenuItem;
    ToolsComWindow_Item: TMenuItem;
    ToolsSetup_Btn: TSpeedButton;
    IniObject1: TIniObject;
    ToolsTest_Item: TMenuItem;
    Timer: TTimer;
    ToolsSetup_Item: TMenuItem;
    HelpOnHelp_Item: TMenuItem;
    HelpContents_Item: TMenuItem;
    HelpSearch_Item: TMenuItem;
    FMH: TFileMenuHandler;
    OneInstance: TOneInstance;
    ESaveDialog: TESaveDialog;
    Setup_Item: TMenuItem;
    Channels_Item: TMenuItem;
    DevicesAutodetect_Item: TMenuItem;
    DevicesBrowse_Item: TMenuItem;
    Autodetect1: TMenuItem;
    SetupOptions_Item: TMenuItem;
    SetupExpImp_Item: TMenuItem;
    ParentFileOpenDialog: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FileNew_ItemClick(Sender: TObject);
    procedure WindowCascade_ItemClick(Sender: TObject);
    procedure UpdateMenuItems(Sender: TObject);
    procedure WindowTile_ItemClick(Sender: TObject);
    procedure WindowArrangeIcons_ItemClick(Sender: TObject);
    procedure FileClose_ItemClick(Sender: TObject);
    procedure FileOpen_ItemClick(Sender: TObject);
    procedure FileExit_ItemClick(Sender: TObject);
    procedure FileSave_ItemClick(Sender: TObject);
    procedure FileSaveAs_ItemClick(Sender: TObject);
    procedure EditCut_ItemClick(Sender: TObject);
    procedure EditCopy_ItemClick(Sender: TObject);
    procedure EditPaste_ItemClick(Sender: TObject);
    procedure WindowMinimizeAll_ItemClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FileNew_BtnClick(Sender: TObject);
    procedure Chromatogram_BtnClick(Sender: TObject);
    procedure EditCut_BtnClick(Sender: TObject);
    procedure EditCopy_BtnClick(Sender: TObject);
    procedure EditPaste_BtnClick(Sender: TObject);
    procedure HelpAbout_ItemClick(Sender: TObject);
    procedure ToolsDebug_ItemClick(Sender: TObject);
    procedure ToolsComWindow_ItemClick(Sender: TObject);
    procedure ToolsSetup_BtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolsTest_ItemClick(Sender: TObject);
    procedure DevicesAutodetect_ItemClick(Sender: TObject);
    procedure DevicesBrowse_ItemClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure ToolsSetup_ItemClick(Sender: TObject);
    procedure HelpOnHelp_ItemClick(Sender: TObject);
    procedure HelpContents_ItemClick(Sender: TObject);
    procedure HelpSearch_ItemClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    function FMHOpen(var Filename: String): Boolean;
    function FMHNew(var Filename: String): Boolean;
    function FMHSave(var Filename: String): Boolean;
    procedure OneInstanceInstanceStarted(Sender: TObject;
      params: TStringList);
    function FMHBeforeSave(AIsNew: boolean; var AFilename: String): Boolean;
    procedure SaveDialogTypeChange(Sender: TObject);
    procedure Channels_ItemClick(Sender: TObject);
    procedure ChannelsSetDefault_ItemClick(Sender: TObject);
    procedure SetupOptions_ItemClick(Sender: TObject);
    procedure SetupExpImp_ItemClick(Sender: TObject);
  private
    {HHlp:THtmlHelp;}
    { Private declarations }
    {procedure CreateSpectrumForm(const Name: string; AMode:word);}
    procedure ShowHint(Sender: TObject);
    {procedure SetPortDefaults;}
  public
    { Public declarations }
    ComPort: TComm;
    procedure WMAppMessage(var Msg:TMessage);message WM_APPMESSAGE;{in globals}
    {procedure PeaksDefineCreateForm(AConnectedForm:TForm);}
    function AppOnHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
    {function AppOnHelp(Command: Word; Data: Longint): Boolean;}
    procedure WMHelp(var Msg:TMessage); message WM_HELP;
    procedure LoadCursors;
    {v0.09}
    function FileOpen(var AFileName: string): boolean;
    procedure WMInitMenu(var Msg:TMessage);message WM_INITMENU;
    {/0.09}
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}
{$R IMAGES.RES}

uses SpecForm, globals, conftype, confu,
  {pkdeffrm,} aboutfrm, debugfrm, devsetfr, pumpfrm;

procedure ReadIni;
begin
{  ConfigReadWriteValue(rwRead, EnvSectionName, 'Com', @ComPortNumber, ptInteger);}
  ConfigReadWriteValue(rwRead, EnvSectionName, 'ToolsAllowed', @ToolsAllowed, ptByte);
  ConfigReadWriteValue(rwRead, EnvSectionName, 'DebugToolsAllowed', @DebugToolsAllowed, ptByte);
  ConfigReadWriteValue(rwRead, EnvSectionName, 'TestAllowed', @TestAllowed, ptByte);
  UlanConfigReadWrite(rwRead);{ulanglob}
end;

procedure WriteIni;
begin
{  ConfigReadWriteValue(rwWrite, EnvSectionName, 'Com', @ComPortNumber, ptInteger);}
  ConfigReadWriteValue(rwWrite, EnvSectionName, 'ToolsAllowed', @ToolsAllowed, ptByte);
  ConfigReadWriteValue(rwWrite, EnvSectionName, 'DebugToolsAllowed', @DebugToolsAllowed, ptByte);
  ConfigReadWriteValue(rwWrite, EnvSectionName, 'TestAllowed', @TestAllowed, ptByte);
  UlanConfigReadWrite(rwWrite);
end;
(*
procedure TMainForm.SetPortDefaults;
begin
  {$IFDEF WIN32}
  {commint}
  with ComPort do begin
    AfterOpenState := aoSpecified;
    BaudRate := br9600;
    DataBits := da8;
    DeviceName := 'Com2';
    FlowControl := fcDefault;
    MonitorEvents := [evRxChar];
    Options := [];
    Parity := paSpace;
    ReadBufSize := 4096;
    ReadTimeout := 1000;
    StopBits := sb10;
    WriteBufSize := 2048;
    WriteTimeout := 1000;
  end;
  {$ELSE}
  {comm}
  with ComPort do begin
    BaudRate := tbr9600;
    DataBits := tdbEight;
    Parity := tpSpace;
    ReadBufferSize := 2048;
    Stopbits := tsbOne;
    TxLowCount := 1024;
    WriteBufferSize := 1024;
    Events := [tceRxChar];
  end;
  {$ENDIF}
end;
*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  {v0.10}
  if not OneInstance.IsFirstInstance then
    exit;
  {/v0.10}
  Caption := 'ChromuLan v' + uLanVersion;
  HelpContext := 0{1000};
{  HHlp := THtmlHelp.Create(Self);}

  Application.OnHint := ShowHint;
  Application.OnHelp := AppOnHelp;{winhelp windows}
  Screen.OnActiveFormChange := UpdateMenuItems;
  ComPort := nil;
  {v0.19}
  ulMInit(false);
  {/v0.19
  LogRegister;
  ConfigInit(Paramstr(0));}
  ReadIni;
  Tools_Item.Visible := ToolsAllowed;
  ToolsDebug_Item.Visible := DebugToolsAllowed;
  ToolsTest_Item.Visible := TestAllowed;
  {v0.19 moved above}
  {/v0.19 ulMInit(false);}
{comm}
  LoadCursors;
  {v0.13}
  Application.UpdateFormatSettings := false;
    { ignore WM_INICHANGED, so that DecimalSeparator (set in UlanGlob to '.')
      value won't get changed }
  {/v0.13}
  {v0.14}
  FMH.SaveDialog := TSaveDialog(ESaveDialog);
end;

procedure TMainForm.LoadCursors;
begin
  Screen.Cursors[crThinCross] := LoadCursor(HInstance, 'THINCROSS');
  Screen.Cursors[crZoomIn] := LoadCursor(HInstance, 'ZOOMIN');
  Screen.Cursors[crThinCrossUp] := LoadCursor(HInstance, 'THINCROSSUP');
end;

procedure TMainForm.ShowHint(Sender: TObject);
begin
  StatusLine.Caption := Application.Hint;
end;

procedure TMainForm.FileNew_ItemClick(Sender: TObject);
begin
{v0.09}
  FMH.New;
{  CreateSpectrumForm('', omCreate);}
end;

procedure TMainForm.FileOpen_ItemClick(Sender: TObject);
{v0.09}
begin
  FMH.Open;
end;
{/v0.09
var
  fn: string;
  m: TOpenMode;
begin
  if OpenDialog.Execute then begin
    FileOpen(OpenDialog.FileName);
  end;
end;
}

function TMainForm.FileOpen(var AFileName: string): boolean;
{v0.09}
var
  fn, ext: string;
{/v0.09}
var
  m: TOpenMode;
begin
  if ofReadOnly in OpenDialog.Options then
    m := omReadOnly
  else
    m := omRead;
  fn := AFileName;
  {v0.09}
  ext := GetExtFromFileDialogFilter(OpenDialog.Filter, OpenDialog.FilterIndex);
  fn := ReplaceExt(fn, ext, false);
  AFileName := fn;
  {/v0.09}
  CreateSpectrumForm(fn, m);
  Result := true;
end;

procedure TMainForm.FileClose_ItemClick(Sender: TObject);
begin
  if ActiveMDIChild <> nil then
    ActiveMDIChild.Close;
end;

procedure TMainForm.FileSave_ItemClick(Sender: TObject);
begin
  { save current file (ActiveMDIChild points to the window) }
{  FileSaveAs_ItemClick(Sender);}
  FMH.Save;
end;

procedure TMainForm.FileSaveAs_ItemClick(Sender: TObject);
begin
  {v0.09}
  FMH.SaveAs;
  {/v0.09
  if Screen.ActiveForm is TSpectrumForm then begin
    with Screen.ActiveForm as TSpectrumForm  do begin
      DoSaveTo;
    end;
  end;
  }
end;

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

procedure TMainForm.EditCut_ItemClick(Sender: TObject);
begin
  {cut selection to clipboard}
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipCut, 0);
    SendMessage(ActiveMDIChild.Handle, WM_CUT, 0, 0);
  end;
end;

procedure TMainForm.EditCopy_ItemClick(Sender: TObject);
begin
  {copy selection to clipboard}
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipCopy, 0);
    SendMessage(ActiveMDIChild.Handle, WM_COPY, 0, 0);
  end;
end;

procedure TMainForm.EditPaste_ItemClick(Sender: TObject);
begin
  {paste from clipboard}
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipPaste, 0);
    {SendMessage(ActiveMDIChild.Handle, WM_PASTE, 0, 0);}
  end;
end;

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

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

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

procedure TMainForm.WindowMinimizeAll_ItemClick(Sender: TObject);
var
  I: Integer;
begin
  { Must be done backwards through the MDIChildren array }
  for I := MDIChildCount - 1 downto 0 do
    MDIChildren[I].WindowState := wsMinimized;
end;

procedure TMainForm.UpdateMenuItems(Sender: TObject);
var f:TForm;
begin
  FileClose_Item.Enabled := MDIChildCount > 0;
  {v0.09}{/v0.09
  FileSave_Item.Enabled := MDIChildCount > 0;}
  FileSaveAs_Item.Enabled := MDIChildCount > 0;
  EditCut_Item.Enabled := MDIChildCount > 0;
  EditCopy_Item.Enabled := MDIChildCount > 0;
  EditPaste_Item.Enabled := MDIChildCount > 0;
  FileSave_Btn.Enabled := MDIChildCount > 0;
  EditCut_Btn.Enabled := MDIChildCount > 0;
  EditCopy_Btn.Enabled := MDIChildCount > 0;
  EditPaste_Btn.Enabled := MDIChildCount > 0;
  WindowCascade_Item.Enabled := MDIChildCount > 0;
  WindowTile_Item.Enabled := MDIChildCount > 0;
  WindowArrangeIcons_Item.Enabled := MDIChildCount > 0;
  WindowMinimizeAll_Item.Enabled := MDIChildCount > 0;
  {v0.09}
  f := ActiveMDIChild;
  if (f <> nil) then begin
    if (f is TSpectrumForm) then with f as TSpectrumForm do begin
      FileSave_Item.Enabled := (Data <> nil) and Data.ULF.Modified;
    end else begin
      FileSave_Item.Enabled := false;
    end;
  end else begin
    FileSave_Item.Enabled := false;
  end;
  {v0.09}
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  {v0.10}
  if not OneInstance.IsFirstInstance then
    exit;
  {v0.10}
  Screen.OnActiveFormChange := nil;
  {HHlp.Free;}
  {ConfigInit(Paramstr(0));}
  Application.OnHelp := nil;
  Application.HelpCommand(HELP_QUIT, 0);
  {v0.14}
  FFileNewDialog.Free;
  {/v0.14}
  WriteIni;
  ConfigDone;
  ulMDone;
end;

procedure TMainForm.WMHelp(var Msg:TMessage);
(*
var
  i: integer;
  hi: PHelpInfo;

  tagHELPINFO = packed record
    iContextType: Integer; { Either HELPINFO_WINDOW or HELPINFO_MENUITEM }
    iCtrlId: Integer;      { Control Id or a Menu item Id. }
    hItemHandle: THandle;  { hWnd of control or hMenu.     }
    dwContextId: DWORD;    { Context Id associated with this item }
    MousePos: TPoint;      { Mouse Position in screen co-ordinates }
  end; iditem windows messages wm_menuselect

  f: TComponent;*)

begin
(*  i := 0;{cm_invokehelp messages windows helpinfo uleditfr}
  hi := PHelpInfo(Msg.lParam);
  f := nil;

  with hi^ do begin
    for i := 0 to ComponentCount - 1 do begin
      c := Components[i];
      case iContextType of
        HELPINFO_WINDOW: begin
          if c is TWinControl then with c as TWinControl do begin
            if Handle = hItemHandle then begin
              f := c;
              break;
            end;
          end;
        end;
        HELPINFO_MENUITEM: begin
          if c is TMenuItem then with c as TMenuItem do begin
            {if c.Command =}
          end;
        end;
      end;
    end;
  end;
  *)
  inherited;
end;

procedure TMainForm.WMAppMessage(var Msg:TMessage);
var CurChild:integer;

  function ForwardMessage(var Msg:TMessage):longint;
  var i:integer;l:longint;
  begin{tform}
    ForwardMessage:= 0;
    for i := 0 to MDIChildCount - 1 do begin
      l := SendMessage(MDIChildren[i].Handle, Msg.Msg, Msg.WParam, Msg.LParam);
      if l > 0 then begin
        ForwardMessage := l;
        CurChild := i;
        exit;
      end;
    end;
  end;

begin
  case Msg.WParam of
    cmQueryRunning, cmULObjBeforeEdit,
    cmULEditFormCloseQuery, cmULObjAfterEdit, cmULObjAfterBrowseEdit:  begin
      if ForwardMessage(Msg) = 1 then begin
        Msg.Result := 1;
        exit;
      end;
    end;

  end;
  inherited;
end;

procedure TMainForm.FileNew_BtnClick(Sender: TObject);
begin
  FileNew_ItemClick(Sender);
end;

procedure TMainForm.Chromatogram_BtnClick(Sender: TObject);
begin
  PostMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmLocalMenu, 0);
end;

procedure TMainForm.EditCut_BtnClick(Sender: TObject);
begin
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipCut, 0);
    SendMessage(ActiveMDIChild.Handle, WM_CUT, 0, 0);
  end;
end;

procedure TMainForm.EditCopy_BtnClick(Sender: TObject);
begin
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipCopy, 0);
    SendMessage(ActiveMDIChild.Handle, WM_COPY, 0, 0);
  end;
end;

procedure TMainForm.EditPaste_BtnClick(Sender: TObject);
begin
  if ActiveMDIChild <>  nil then begin
    SendMessage(ActiveMDIChild.Handle, WM_APPMESSAGE, cmClipPaste, 0);
    {SendMessage(ActiveMDIChild.Handle, WM_PASTE, 0, 0);}
  end;
end;

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

procedure TMainForm.ToolsDebug_ItemClick(Sender: TObject);
begin
  if DebugForm = nil then begin
    DebugForm := TDebugForm.Create(Application);
  end;
  if not ToolsDebug_Item.Checked then begin
    ToolsDebug_Item.Checked := true;
    DebugForm.Show;
  end else begin
    ToolsDebug_Item.Checked := false;
    DebugForm.Hide;
  end;
end;

procedure TMainForm.ToolsComWindow_ItemClick(Sender: TObject);
{$IFDEF WIN32}
var f:TCommForm;
{$ENDIF}
begin
{$IFDEF WIN32}
  f := TCommForm.Create(Application);
  f.ShowModal;
  f.Free;
{$ENDIF}
end;

procedure TMainForm.ToolsSetup_BtnClick(Sender: TObject);
begin
  DevSetupForm.ShowModal;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{  IniObject1.Write;}
end;

procedure TMainForm.ToolsTest_ItemClick(Sender: TObject);
begin
  ULFObju.Test;
end;

procedure TMainForm.DevicesAutodetect_ItemClick(Sender: TObject);
{v0.14}
var
  dm: TDeviceMode;
{/v0.14}
begin
  {v0.14}
  dm := dmUlan;
  if not DeviceModeCheck(dm) then begin
    exit
  end else begin
    if Modules.DeviceMode <> dmUlan then begin
      Modules.DeviceMode := dmUlan;
    end;
    Modules.ULDrvFailed := false;
  end;
  {/v0.14}
  if Modules.ScanForModules > 0 then begin
    Modules.ULD.Browse
  end else
    ShowMessage('No devices detected.');
end;

procedure TMainForm.DevicesBrowse_ItemClick(Sender: TObject);
begin
  Modules.ULD.Browse;
end;

procedure TMainForm.TimerTimer(Sender: TObject);
begin
  if csDestroying in ComponentState then
    exit;
  ULMDoTimer;{Modules.DoTimer;}
end;

procedure TMainForm.ToolsSetup_ItemClick(Sender: TObject);
begin
  DevSetupForm.ShowModal;
end;

procedure TMainForm.HelpOnHelp_ItemClick(Sender: TObject);
begin
  WinHelp(Handle, 'WINHELP.HLP', HELP_HELPONHELP, 0);
end;

function TMainForm.AppOnHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
begin
  {v0.11}
  Result := true;
  {/v0.11}
  CallHelp := true;{false; tapplication windows}
  if Command = HELP_CONTEXTPOPUP then begin
    CallHelp := false;
    Application.HelpContext(Data);
  end;
{  WinHelp(Handle, '',}
end;

procedure TMainForm.HelpContents_ItemClick(Sender: TObject);
begin
  Application.HelpCommand(Help_Contents, 0);
end;

procedure TMainForm.HelpSearch_ItemClick(Sender: TObject);
var
 P: PChar;
begin
{ Application.HelpFile := 'c:\delphi\bin\delphi.hlp';}
  P := StrNew('');
  Application.HelpCommand(Help_PartialKey, longint(P));
  StrDispose(P);
end;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key <> vk_Return then begin
    Key := vk_Return;
  end;
end;

{v0.09}
function TMainForm.FMHOpen(var Filename: String): Boolean;
begin
  Result := FileOpen(FileName);
end;

function TMainForm.FMHNew(var Filename: String): Boolean;
{v0.13}
var
  s: string;
  li: TListItem;
{/v0.13}
begin
  {v0.13}
  if FileNewDialog.ShowModal = mrOK then begin
    s := FileNewDialog.ActivePageBaseName;
    if s = 'Anal' then begin
      li := FileNewDialog.ActiveListViewItem;
      if li <> nil then
        s := {v0.15}TemplateDir + {/v0.15} li.Caption
      else
        s := '';
      CreateSpectrumForm(s, omCreate);
    end;
  end;
  {/v0.13
  Result := true;
  CreateSpectrumForm('', omCreate);}
end;

function TMainForm.FMHSave(var Filename: String): Boolean;
var fn, ext:string;
begin
  Result := false;
  fn := FileName;
  if Screen.ActiveForm is TSpectrumForm then begin
    with Screen.ActiveForm as TSpectrumForm  do begin{winutl}
      ext := GetExtFromFileDialogFilter({v0.14}FMH.{/v0.14}SaveDialog.Filter, FMH.SaveDialog.FilterIndex);
      fn := ReplaceExt(fn, ext, {v0.13}true{/v0.13 false});
      SaveTo(fn);
      Result := true;
    end;
  end;
  FileName := fn;
end;

procedure TMainForm.WMInitMenu(var Msg:TMessage);
begin
  UpdateMenuItems(Self);
  Msg.Result := 0;
end;
{/v0.09}

{v0.10}
procedure TMainForm.OneInstanceInstanceStarted(Sender: TObject;
  params: TStringList);
var
  i: integer;
  fn: string;
begin
  for i := 0 to params.Count - 1 do begin
    fn := params[i];
    FileOpen(fn);
  end;
end;
{/v0.10}

{v0.12}
function TMainForm.FMHBeforeSave(AIsNew: boolean; var AFilename: String): Boolean;
{v0.16}
var i: integer;
{/v0.16}
begin
  Result := false;
  if Screen.ActiveForm is TSpectrumForm then begin
    with Screen.ActiveForm as TSpectrumForm  do begin
      if Data <> nil then begin
        if AIsNew then begin
          AFileName := '';
          {v0.16}
          FMH.SaveDialog.FilterIndex := 1;
          {/v0.16}
        end else begin
          AFileName := Data.ULF.FileName;
          {v0.16}
          if not GetDialogFilterIndexFromExt(FMH.SaveDialog.Filter, ExtractFileExt(AFileName), i) then
            i := 1;
          FMH.SaveDialog.FilterIndex := i;
          {/v0.16}
        end;
        Result := true;
      end;
    end;
  end;
end;
{/v0.12}

procedure TMainForm.SaveDialogTypeChange(Sender: TObject);
begin
end;
{uLan Analysis File (*.ULF)|*.ULF|Raw Data (*.DAT)|*.DAT|Raw ASCII (*.TXT)|*.TXT|uLan Analysis ASCII (*.ASC)|*.ASC|uLan Analysis Template File (*.ULT)|*.ULT}
{[ofOverwritePrompt,ofHideReadOnly,ofEnableSizing]}
procedure TMainForm.Channels_ItemClick(Sender: TObject);
begin
  Channels.ULL.Browse;
end;

procedure TMainForm.ChannelsSetDefault_ItemClick(Sender: TObject);
begin
  Channels.ULL.Edit;
end;

procedure TMainForm.SetupOptions_ItemClick(Sender: TObject);
begin
  DevSetupForm.ShowModal;
end;

procedure TMainForm.SetupExpImp_ItemClick(Sender: TObject);
begin
  {v0.18}
  ExpImp.Browse;
  {/v0.18}
end;

end.
