unit Preferences;
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNIT_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, Grids, ValEdit, ObjectInspector;

type
  TPrefDlg = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    Label2: TLabel;
    TabSheet3: TTabSheet;
    Panel4: TPanel;
    Label10: TLabel;
    GroupBox1: TGroupBox;
    indentsize: TComboBox;
    groupbox3: TGroupBox;
    recentlength: TComboBox;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    ColorDialog1: TColorDialog;
    TabSheet4: TTabSheet;
    Label8: TLabel;
    ColorDialog2: TColorDialog;
    TabSheet5: TTabSheet;
    Panel3: TPanel;
    Panel5: TPanel;
    Label18: TLabel;
    GroupBox7: TGroupBox;
    oireqbackground: TShape;
    oireqfont: TLabel;
    GroupBox8: TGroupBox;
    oibackground: TShape;
    oifont: TLabel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    TabSheet6: TTabSheet;
    Panel6: TPanel;
    Label19: TLabel;
    GroupBox2: TGroupBox;
    Label4: TLabel;
    gridcolor: TShape;
    Label5: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    showgrid: TCheckBox;
    Snaptogrid: TCheckBox;
    ShowDots: TComboBox;
    BGX: TEdit;
    BGY: TEdit;
    GroupBox4: TGroupBox;
    Label6: TLabel;
    gridcolor2: TShape;
    Label7: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    showgrid2: TCheckBox;
    snaptogrid2: TCheckBox;
    showdots2: TComboBox;
    FGX: TEdit;
    FGY: TEdit;
    TabSheet7: TTabSheet;
    Panel7: TPanel;
    Label20: TLabel;
    GroupBox6: TGroupBox;
    AutoRollUp: TCheckBox;
    Label21: TLabel;
    exoeInspector: TCheckBox;
    exoeplugin: TCheckBox;
    exsnipwin: TCheckBox;
    exVariableWin: TCheckBox;
    GroupBox9: TGroupBox;
    Label22: TLabel;
    wsSnipWin: TCheckBox;
    wsMain: TCheckBox;
    wsOI: TCheckBox;
    wsPalette: TCheckBox;
    wsVarWin: TCheckBox;
    GroupBox5: TGroupBox;
    SpeedButton1: TSpeedButton;
    Edit1: TEdit;
    dvSheet: TTabSheet;
    Panel8: TPanel;
    Label23: TLabel;
    GroupBox10: TGroupBox;
    listProps: TValueListEditor;
    Label24: TLabel;
    ComboBox2: TComboBox;
    Label25: TLabel;
    Label3: TLabel;
    GroupBox11: TGroupBox;
    oiNonbackground: TShape;
    oinonfont: TLabel;
    Button6: TButton;
    Button7: TButton;
    FontDialog1: TFontDialog;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure gridcolorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure gridcolor2MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FGYChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure AutoRollUpClick(Sender: TObject);
    procedure exsnipwinClick(Sender: TObject);
    procedure listPropsDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure listPropsEditButtonClick(Sender: TObject);
    procedure FontDialog1Show(Sender: TObject);
    procedure listPropsValidate(Sender: TObject; ACol, ARow: Integer;
      const KeyName, KeyValue: String);
    procedure listPropsKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ComboBox2Change(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    VisIndex: Integer;
    GenIndex: Integer;
    SensorIndex: Integer;
    SupportList: TVisPlugEditorSupportList;
    dlgName: String;
  public
    Function Execute: Boolean;
    Procedure LoadDefaultPages;
    Procedure LoadVisDV(VisName: String);
    Procedure LoadGenDV(GenName: String);
    Procedure LoadFormDV;
    Procedure LoadOI;
    procedure SetOIFields(Number: Integer);
  end;

var
  PrefDlg: TPrefDlg;

implementation

uses Main, ProcsAndtypes, filectrl, lpl_Sysutils, pluginPalette, oi,
  VarWin, codesnippet, CM2EditorSupportInterface, Filter, Objbrowsewin;

{$R *.dfm}

function TPrefDlg.Execute: Boolean;
Var
 S: String;
 I: Integer;
begin
 indentsize.ItemIndex := Length(Options.Files.CMLIndent) - 1;
 recentlength.ItemIndex := Options.Files.RecentLength;
 If Options.Files.ShowFullPathInCaption then Radiogroup2.ItemIndex := 1
  else RadioGroup2.ItemIndex := 0;
 If Options.Files.LoadLastOnStartup Then RadioGroup1.ItemIndex := 1
  else Radiogroup1.ItemIndex := 0;
 Showgrid.checked := options.visual.Showgrid;
 Snaptogrid.checked := options.visual.SnapToGrid;
 BGX.Text := IntTostR(Options.Visual.GridSpacingX);
 BGY.Text := IntToStr(Options.Visual.GridSpacingY);
 If Options.Visual.ShowDot then Showdots.ITemIndex := 0
  else Showdots.ItemIndex := 1;
 GridColor.Brush.Color := Options.Visual.GridColor;
 Showgrid2.checked := options.visual.FormPanelOptions.Showgrid;
 Snaptogrid2.checked := options.visual.FormPanelOptions.SnapToGrid;
 FGX.Text := IntToStr(Options.Visual.FormPanelOptions.GridSpacingX);
 FGY.Text := IntToStr(Options.Visual.FormPanelOptions.GridSpacingY);
 If Options.Visual.FormPanelOptions.ShowDot then Showdots2.ITemIndex := 0
  else Showdots2.ItemIndex := 1;
 GridColor2.Brush.Color := Options.Visual.FormPanelOptions.GridColor;

 Edit1.Text := Options.Files.DefaultFolder;
 oireqfont.Font.color := Options.OI.RequiredFontColor;
 oireqBackground.Brush.Color := Options.OI.RequiredBackgroundColor;
 oifont.Font.color := options.OI.FontColor;
 oibackground.Brush.Color := options.OI.BackgroundColor;
 oinonfont.Font.Color := Options.OI.NonFontColor;
 oinonBackground.Brush.Color := options.OI.NonBackgroundColor;
 AutoRollUp.Checked := Options.ToolWindows.AutoRollUp;
 exSnipWin.Checked := Options.ToolWindows.ARUExcept.SnipWin;
 exoeInspector.Checked := Options.ToolWindows.ARUExcept.OI;
 exoeplugin.Checked := Options.ToolWindows.ARUExcept.Palette;
 exVariableWin.Checked := Options.ToolWindows.ARUExcept.VarWin;
 wsMain.checked := Options.ToolWindows.Snapping.Main;
 wsSnipWin.Checked := Options.ToolWindows.Snapping.SnipWin;
 wsOI.Checked := Options.ToolWindows.Snapping.OI;
 wsPalette.Checked := Options.ToolWindows.Snapping.Palette;
 wsVarWin.Checked := Options.ToolWindows.Snapping.VarWin;

 PageControl1.ActivePageIndex := 0;
 Result := Showmodal = mrOk;


 S := '';
 For I := 1 to IndentSize.itemIndex + 1 do S:= S + ' ';
 Options.Files.CMLIndent := S;
 Options.Files.RecentLength := RecentLength.ItemIndex;
 oeMain.RecentList.MaxLimit := Options.Files.RecentLength;
 Options.Files.ShowFullPathInCaption := Radiogroup2.ItemIndex = 1;
 Options.Files.LoadLastOnStartup := Radiogroup1.ItemIndex = 1;
 Options.Files.DefaultFolder := pathtook(Edit1.text);
 Options.Visual.ShowGrid := showgrid.checked;
 Options.Visual.SnapToGrid := snaptogrid.checked;
 Options.Visual.GridSpacingX := StrToInt(BGX.Text);
 Options.Visual.GridSpacingY := StrToInt(BGY.Text);
 Options.Visual.ShowDot := showdots.ItemIndex = 0;
 Options.Visual.GridColor := gridcolor.brush.Color;
 Options.Visual.FormPanelOptions.ShowGrid := showgrid2.checked;
 Options.Visual.FormPanelOptions.SnapToGrid := snaptogrid2.checked;
 Options.Visual.FormPanelOptions.GridSpacingX := StrToInt(FGX.Text);
 Options.Visual.FormPanelOptions.GridSpacingY := StrToInt(FGY.Text);
 Options.Visual.FormPanelOptions.ShowDot := showdots2.ItemIndex = 0;
 Options.Visual.FormPanelOptions.GridColor := gridcolor2.brush.Color;
 oeMain.DesktopImage1.ShowGrid := false;
 oeMain.DesktopImage1.GridLineColor := gridcolor.Brush.color;
 oeMain.DesktopImage1.GridSpaceX := Options.Visual.GridSpacingX;
 oeMain.DesktopImage1.GridSpaceY := Options.Visual.GridSpacingY;
 oeMain.DesktopImage1.ShowDot := showdots.ItemIndex = 0;
 oeMain.DesktopImage1.ShowGrid := ShowGrid.checked;

 Options.OI.RequiredFontColor := oireqfont.Font.color;
 Options.OI.RequiredBackgroundColor := oireqbackground.Brush.Color;
 Options.OI.FontColor := oifont.font.color;
 Options.OI.BackgroundColor := oibackground.Brush.Color;
 Options.OI.NonFontColor := oinonfont.Font.Color;
 Options.OI.NonBackgroundColor := oiNonbackground.Brush.Color;
 Options.ToolWindows.AutoRollUp := AutoRollUp.Checked;
 Options.ToolWindows.ARUExcept.SnipWin := exSnipWin.Checked;
 Options.Toolwindows.ARUExcept.OI := exoeInspector.checked;
 Options.ToolWindows.ARUExcept.Palette := exoePlugin.Checked;
 Options.ToolWindows.ARUExcept.VarWin := exVariableWin.Checked;
 Options.ToolWindows.Snapping.Main := wsMain.Checked;
 Options.ToolWindows.Snapping.SnipWin := wsSnipWin.Checked;
 Options.ToolWindows.Snapping.OI := wsOI.Checked;
 Options.ToolWindows.Snapping.Palette := wsPalette.checked;
 Options.ToolWindows.Snapping.VarWin := wsVarWin.Checked;

 oeMain.SetSnapping;
 If Options.ToolWindows.AutoRollUp then begin
  If not Options.ToolWindows.ARUExcept.SnipWin then begin
   SnipWin.SetRoll(false);
  end
   else
  begin
   SnipWin.SetRoll(true);
  end;
  If not Options.ToolWindows.ARUExcept.OI then begin
   oeInspector.SetRoll(false);
  end
   else
  begin
   oeInspector.SetRoll(true);
  end;
  If not Options.ToolWindows.ARUExcept.Palette then begin
   oePlugin.SetRoll(false);
  end
   else
  begin
   oePlugin.SetRoll(true);
  end;
  If not Options.ToolWindows.ARUExcept.VarWin then begin
   VariableWin.SetRoll(false);
  end
   else
  begin
   VariableWin.SetRoll(true);
  end;
  If not Options.ToolWindows.ARUExcept.ObjBrowser then begin
   ObjBrowser.SetRoll(false);
  end
   else
  begin
   ObjBrowser.SetRoll(true);
  end;
 end
  else
 begin
  oePlugin.SetRoll(not Options.ToolWindows.AutoRollUp);
  oeInspector.SetRoll(not Options.ToolWindows.AutoRollUp);
  VariableWin.SetRoll(not Options.ToolWindows.AutoRollUp);
  SnipWin.SetRoll(not Options.ToolWindows.AutoRollUp);
  ObjBrowser.SetRoll(not Options.Toolwindows.AutoRollUp);
 end;

 SaveOptions;
 oeMain.GESL.SaveToFile(PathToOk(Extractfilepath(ParamStr(0))) + 'DVGeneral.xml');
 oeMain.VESL.SaveToFile(PathToOk(Extractfilepath(ParamStr(0))) + 'DVVisual.xml');
 oeMain.FESL.SaveToFile(PathToOk(ExtractFilePath(ParamStr(0))) + 'DVForm.xml');
 oeMain.UpdateCaption;
 oeInspector.listProps.Repaint;
 For I := 0 to oeMain.FormMgr.Count - 1 do begin
  If assigned(oeMain.FormMgr[I]) then begin
   oeMain.FormMgr[I].Paint;
  end;
 end;
end;

procedure TPrefDlg.FormCreate(Sender: TObject);
begin
 treeview1.FullExpand;
end;

procedure TPrefDlg.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
 If node = nil then exit;
 PageControl1.ActivePageIndex := Node.SelectedIndex;
 If Treeview1.Selected = nil then exit;
 If treeview1.Selected.Parent = nil then exit;
 Label23.Caption := Node.Text + ' [' +Treeview1.Selected.Parent.Text + ']';
 If uppercase(Treeview1.Selected.Parent.Text) = 'VISUAL' then LoadVisDV(Node.Text);
 If uppercase(Treeview1.Selected.Parent.Text) = 'GENERAL' then LoadGenDV(Node.Text);
 If uppercase(Treeview1.Selected.Text) = 'FORM' then LoadFormDV;
end;

procedure TPrefDlg.gridcolorMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if button = mbLeft then begin
  colordialog1.Color := gridcolor.Brush.color;
  If not colordialog1.Execute then exit;
  gridcolor.Brush.color := colordialog1.color;
 end;
end;

procedure TPrefDlg.gridcolor2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if button = mbLeft then begin
  colordialog1.Color := gridcolor2.Brush.color;
  If not colordialog1.Execute then exit;
  gridcolor2.Brush.color := colordialog1.color;
 end;
end;

procedure TPrefDlg.SpeedButton1Click(Sender: TObject);
Var
 Dir: String;
begin
 Dir := Edit1.text;
 If not SelectDirectory('Select Default Folder', '', Dir) then exit;
 Edit1.Text := Dir;
end;

procedure TPrefDlg.FGYChange(Sender: TObject);
begin
 Try
  StrToInt((sender as TEdit).Text);
 except
  on EConvertError do begin
   (Sender as TEdit).Text := '10';
   (sender as Tedit).SelectAll;
   (Sender as TEdit).SetFocus;
  end;
 end;
end;

procedure TPrefDlg.Button2Click(Sender: TObject);
begin
 Colordialog1.Color := oireqfont.Font.Color;
 If not colordialog1.execute then exit;
 oireqfont.Font.Color := colordialog1.color;
end;

procedure TPrefDlg.Button4Click(Sender: TObject);
begin
 Colordialog1.Color := oireqbackground.Brush.Color;
 If not colordialog1.execute then exit;
 oireqbackground.Brush.Color := colordialog1.color;
end;

procedure TPrefDlg.Button3Click(Sender: TObject);
begin
 Colordialog1.color := oifont.Font.color;
 If not colordialog1.execute then exit;
 oifont.Font.Color := Colordialog1.color;
end;

procedure TPrefDlg.Button5Click(Sender: TObject);
begin
 Colordialog1.color := OIbackground.Brush.Color;
 If not colordialog1.Execute then exit;
 OIBackground.Brush.Color := Colordialog1.color;
end;

procedure TPrefDlg.AutoRollUpClick(Sender: TObject);
begin
 exSnipWin.Enabled := AutoRollUp.Checked;
 exoeInspector.Enabled := AutoRollUp.Checked;
 exoeplugin.Enabled := AutoRollUp.Checked;
 exVariableWin.Enabled := AutoRollUp.Checked;
end;

procedure TPrefDlg.exsnipwinClick(Sender: TObject);
begin
 If exSnipWin.Checked and
    exoeInspector.Checked and
    exoeplugin.Checked and
    exVariableWin.Checked then begin
  exSnipWin.Checked := false;
  exoeInspector.checked := false;
  exoeplugin.Checked := false;
  exVariableWin.Checked := false;
  AutoRollUp.Checked := false;
 end;
end;

procedure TPrefDlg.LoadDefaultPages;
Var
 GNode, VNode, Node: TTreeNode;
 I: Integer;
 List: TStringList;
begin
 Node := nil; GNode := nil; VNode := nil;
 For I := 0 to Treeview1.Items.Count - 1 do begin
  If uppercase(Treeview1.items[I].Text) = 'DEFAULT VALUES' then begin
   Node := TreeView1.Items[I];
   break;
  end;
 end;
 If node = nil then begin
  MessageDlg('Could not find "default values" in the tree on the preferences dialog', mtError, [mbOK], 0);
  exit;
 end;
 For I := 0 to Node.Count - 1 do begin
  If uppercase(Node.Item[I].Text) = 'GENERAL' then begin
   GNode := Node.Item[I];
  end;
  If uppercase(Node.Item[I].Text) = 'VISUAL' then begin
   VNode := Node.Item[I];
  end;
 end;
 If GNode = nil then begin
  MessageDlg('Could not find "General" node in treeview on preference dialog', mtError, [mbOK], 0);
  exit;
 end;
 If VNode = nil then begin
  MessageDlg('Could not find "Visual" node in the treeview on the preference dialog', mtError, [mbOK], 0);
  exit;
 end;
 List := TStringList.Create;
 For I := 0 to oeMain.gpl.Count - 1 do begin
  List.Add(oeMain.gpl.GenPlugIn[I].name);
 end;
 List.Sort;
 For I := 0 to List.Count -1 do begin
  Treeview1.Items.AddChild(GNode, List[I]).SelectedIndex := 7;
 end;
 List.Clear;
 For I := 0 to oeMain.VESL.count - 1 do begin
  If oeMain.VESL[I].Count > 0 then begin
   List.Add(oeMain.VESL[I].Name);
  end;
 end;
 List.Sort;
 For I := 0 to List.count - 1 do begin
  Treeview1.items.AddChild(VNode, List[I]).SelectedIndex := 7;
 end;
end;

procedure TPrefDlg.SetOIFields(Number: Integer);
Var
 I: Integer;
begin
 // Ensure we have to correct amount of rows
 If listprops.RowCount > Number then begin
  for I := listprops.RowCount - 1 downto Number do begin // ORiginal Value was 1 to ...
   listprops.DeleteRow(0);
  end;
 end;
 if listprops.RowCount < Number then begin
  For I := (listprops.RowCount - 1) to Number - 2 do begin
   If (listprops.InsertRow('', '', true) < 1) then MessageDlg('Failure to add Row', mtWarning, [mbOK], 0);
  end;
 end;
 For I := 0 to number - 1 do begin
  listprops.ItemProps[I].ReadOnly := False;
  listprops.ItemProps[I].EditStyle := esSimple;
  listprops.Cells[0, I] := '';
  listprops.Cells[1, I] := '';
 End;
end;

procedure TPrefDlg.listPropsDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
Var
 KeyName: String;
 I: Integer;
 Req: Boolean;
begin
 If not assigned(SupportList) then exit;
 Req := True;
 KeyName := listprops.Cells[0, Arow];
 For I := 0 to SupportList.Count -1 do begin
  If uppercase(SupportList[I].Name) = uppercase(Keyname) then begin
   Req := SupportList[I].Required;
   break;
  end;
 end;
 If not Req then begin
  If (uppercase(KeyName) = 'LEFT') or (Uppercase(Keyname) = 'TOP') or
   (Uppercase(KeyName) = 'HEIGHT') or (Uppercase(KeyName) = 'WIDTH') then begin
   Req := True;
  end;
 end;
 If Req then begin
  listprops.Canvas.Brush.Color := Options.OI.RequiredBackgroundColor;
  listprops.Canvas.Pen.Color := Options.OI.RequiredBackgroundColor;
  Listprops.Canvas.Font.Color := Options.OI.RequiredFontColor;
 End
  else
 begin
  listprops.Canvas.Brush.Color := Options.OI.BackgroundColor;
  listprops.Canvas.Pen.Color := Options.OI.BackgroundColor;
  Listprops.Canvas.Font.Color := Options.OI.FontColor;
 end;
 listprops.Canvas.Rectangle(Rect);
 listprops.Canvas.TextOut(Rect.Left + 1, Rect.Top + 1, Listprops.Cells[acol, arow]);
end;

procedure TPrefDlg.LoadOI;
Var
 I, II: Integer;
 S: String;
begin
 If SupportList.Count = 0 then begin
  // Not needed, if supportlist is zero the plugin is never created on the node list
  exit;
 end;
 SetOIFields(SupportList.Count);
 For I := 0 to SupportList.Count - 1 do begin
  listprops.Cells[0,I] := SupportList[I].Name;
  ListProps.Cells[1,I] := SupportList[I].Value;

  Case SupportList[I].ValueType of
    vtUnknown: listProps.ItemProps[I].EditStyle := esSimple;
    vtFixed: Begin
              listProps.ItemProps[I].EditStyle := esPickList;
              listProps.ItemProps[I].PickList.Clear;
              S := SupportList[I].ValueRange;
              For II := 1 to CountFilt(S, '|') do begin
               listProps.ItemProps[I].PickList.Add(GetFilt(S, II, '|'));
              end;
             End;
    vtColor: Begin
              listProps.ItemProps[I].EditStyle := esEllipsis;
              listProps.ItemProps[I].PickList.Clear;
             end;
    vtFile: Begin
              listProps.ItemProps[I].EditStyle := esEllipsis;
              listProps.ItemProps[I].PickList.Clear;
             End;
    vtFontEx: Begin
               listProps.ItemProps[I].EditStyle := esEllipsis;
               listProps.ItemProps[I].PickList.Clear;
               listProps.ItemProps[I].ReadOnly := true;
              End;



  end;
 end;
 listprops.Visible := true;
 listprops.Col := 1;
 listprops.Row := 0;
end;

procedure TPrefDlg.FontDialog1Show(Sender: TObject);
begin
 ShowWindow(GetDlgItem(fontDialog1.Handle, 1091), SW_HIDE); //label Color:
 ShowWindow(GetDlgItem(fontDialog1.Handle, 1139), SW_HIDE); //ColorBox
 ShowWindow(GetDlgItem(fontdialog1.Handle, 1094), SW_HIDE); //label Script
 ShowWindow(GetDlgItem(fontdialog1.Handle, 1140), SW_HIDE); //Script
 SetWindowText(fontdialog1.Handle, PAnsiChar(dlgName));
end;

procedure TPrefDlg.listPropsEditButtonClick(Sender: TObject);
Var
 II: Integer;
 S, S1, S2, KeyName, KeyValue: String;
 FName, FSize, FStyle: String;
 Strikeout, italic, bold, underline: boolean;
 FS: TFontStyles;
begin
 KeyValue := listProps.Cells[1,ListProps.Row];
 KeyName := listProps.Cells[0,ListProps.Row];
 Case SupportList[listprops.Row].ValueType of
  vtColor: begin
            Colordialog1.Color := RGBToColor(KeyValue);
            If not ColorDialog1.Execute then exit;
            KeyValue := ColorToRGB(ColorDialog1.Color);
           end;
  vtFile: begin
           Opendialog1.Title := 'Assign File to "' + KeyName + '" Property';
           S := SupportList[listProps.Row].ValueRange;
           S1 := ''; S2 := '';
           For II := 1 to CountFilt(S, '|') do begin
            S1 := S1 + ';*.' + GetFilt(S, II, '|');
            S2 := S2 + ';*.' + GetFilt(S, II, '|');
           end;
           S1 := Copy(S1, 2, length(S1));
           S2 := Copy(S2, 2, Length(S2));
           Opendialog1.Filter := 'All Supported Types (' + S1 + ')|' + S2;
           Opendialog1.FileName := KeyValue;
           If not opendialog1.Execute then exit;
           KeyValue := Opendialog1.Filename;
          end;
  vtFontEx: Begin
             FName := GetFilt(KeyValue, 1, ',');
             FSize := GetFilt(KeyValue, 2, ',');
             FStyle := GetFilt(KeyValue, 3, ',');
             If Length(FStyle) <> 4 then FStyle := '0000';
             fontdialog1.Font.Name := FName;
             try
              fontdialog1.Font.Size := StrToInt(FSize);
             except
              FontDialog1.Font.Size := 8;
             end;
             If FStyle[1] = '1' then Bold := true
              else Bold := false;
             If FStyle[2] = '1' then Italic := true
              else Italic := false;
             If FStyle[3] = '1' then Underline := true
              else Underline := false;
             If FStyle[4] = '1' then Strikeout := true
              else Strikeout := false;
             Fs := [];
             If Bold then FS := FS + [fsBold];
             If Italic then FS := FS + [fsItalic];
             If Underline Then FS := FS + [fsUnderline];
             If StrikeOut then FS := FS + [fsStrikeOut];
             fontdialog1.Font.Style := FS;
             dlgName := SupportList[listprops.row].Name;
             If fontDialog1.Execute then begin
              KeyValue := Fontdialog1.Font.Name + ',' + IntToStr(Fontdialog1.Font.Size) + ',';
              If fsbold in Fontdialog1.Font.Style then KeyValue := KeyValue + '1'
               else KeyValue := KeyValue + '0';
              If fsItalic in Fontdialog1.Font.Style then KeyValue := KeyValue + '1'
               else KeyValue := KeyValue + '0';
              If fsUnderline in Fontdialog1.Font.Style then KeyValue := KeyValue + '1'
               else KeyValue := KeyValue + '0';
              If fsStrikeout in Fontdialog1.Font.Style then KeyValue := KeyValue + '1'
               else KeyValue := KeyValue + '0';
             end;
            end;
 end;
 listProps.Cells[1, ListProps.Row] := KeyValue;
 listPropsValidate(self, ListProps.Col, ListProps.Row, KeyName, KeyValue);
end;

procedure TPrefDlg.listPropsValidate(Sender: TObject; ACol, ARow: Integer;
  const KeyName, KeyValue: String);
Var
 Min, Max: Integer;
 Current: Integer;
begin
 If SupportList[aRow].ValueType = vtRange then begin
  Min := StrToInt(GetFilt(SupportList[aRow].ValueRange, 1, '|'));
  Max := StrToInt(GetFilt(SupportList[aRow].ValueRange, 2, '|'));
  Try
   Current := StrToInt(KeyValue);
   If Current < Min then begin
    Current := Min;
    listProps.Cells[ACol, ARow] := IntToStr(Current);
   end;
   If Current > Max then begin
    Current := Max;
    listProps.Cells[ACol, ARow] := IntToStr(Current);
   end;
  except
   on EConvertError do begin
    listProps.Cells[ACol, ARow] := IntToStr(Min);
   end;
  end;
 end;
 // Save code goes here
 If VisIndex > -1 then begin
  oeMain.VESL[VisIndex].ChangeValue(KeyName, listProps.Cells[ACol, ARow]);
 end;
 If GenIndex > -1 Then begin
  oeMain.GESL[GenIndex][SensorIndex].ChangeValue(KeyName, listProps.Cells[ACol, ARow]);
 end;
 If (VisIndex = -1) and (GenIndex = -1) then begin
  oeMain.FESL.ChangeValue(KeyName, listProps.Cells[ACol, ARow]);
 end;
end;

procedure TPrefDlg.listPropsKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
Begin
 if key = vk_return then begin
  listPropsValidate(self, listProps.Col, listProps.Row, listProps.Cells[0, listProps.Row], listProps.Cells[1, listProps.Row]);
 end;
end;

procedure TPrefDlg.LoadFormDV;
Var
 I, II: Integer;
 List: TStringlist;
begin
 Combobox2.Enabled := false;
 Combobox2.Clear;
 Label25.Enabled := false;
 Groupbox10.Caption := 'Set Default Values For Forms';
 GenIndex := -1;
 VisIndex := -1;
 List := TStringList.Create;
 For I := 0 to oeMain.FESL.Count - 1 do begin
  List.Add(oeMain.FESL[I].Name);
 end;
 List.Sort;
 If assigned(SupportList) then begin
  FreeAndNil(SupportList);
 end;
 SupportList := TVisPlugEditorSupportList.Create;
 For I := 0 to list.count - 1 do begin
  For II := 0 to oeMain.FESL.Count - 1 do begin
   If uppercase(List[I]) = uppercase(oeMain.FESL[II].Name) then begin
    SupportList.Add(
     oeMain.FESL[II].Name,
     oeMain.FESL[II].Value,
     oeMain.FESL[II].Description,
     oeMain.FESL[II].Required,
     oeMain.FESL[II].ValueType,
     oeMain.FESL[II].ValueRange,
     oeMain.FESL[II].CanContainSensor
    );
   end;
  end;
 end;
 FreeAndNil(List);
 LoadOI;
end;

procedure TPrefDlg.LoadVisDV(VisName: String);
Var
 I, II: Integer;
 List: TStringlist;
begin
 Combobox2.Enabled := false;
 Combobox2.Clear;
 Label25.Enabled := false;
 Groupbox10.Caption := 'Set Default Values For This Plugin';
 GenIndex := -1;
 VisIndex := -1;
 For I := 0 to oeMain.VESL.Count - 1 do begin
  If uppercase(VisName) = uppercase(oeMain.VESL[I].Name) then begin
   VisIndex := I;
   break;
  end;
 end;
 If VisIndex = -1 then begin
  MessageDlg('Unable to load defaults, the plugin could not be found in VESL.', mtError, [mbOK], 0);
  exit;
 end;
 List := TStringList.Create;
 For I := 0 to oeMain.VESL[VisIndex].Count - 1 do begin
  List.Add(oeMain.VESL[VisIndex][I].Name);
 end;
 List.Sort;
 If assigned(SupportList) then begin
  FreeAndNil(SupportList);
 end;
 SupportList := TVisPlugEditorSupportList.Create;
 For I := 0 to list.count - 1 do begin
  For II := 0 to oeMain.VESL[VisIndex].Count - 1 do begin
   If uppercase(List[I]) = uppercase(oeMain.VESL[VisIndex][II].Name) then begin
    SupportList.Add(
     oeMain.VESL[VisIndex][II].Name,
     oeMain.VESL[VisIndex][II].Value,
     oeMain.VESL[VisIndex][II].Description,
     oeMain.VESL[VisIndex][II].Required,
     oeMain.VESL[VisIndex][II].ValueType,
     oeMain.VESL[VisIndex][II].ValueRange,
     oeMain.VESL[VisIndex][II].CanContainSensor
    );
   end;
  end;
 end;
 FreeAndNil(List);
 LoadOI;
end;

procedure TPrefDlg.LoadGenDV(GenName: String);
Var
 I: Integer;
begin
 VisIndex := -1;
 GenIndex := -1;
 Combobox2.Enabled := true;
 Combobox2.Clear;
 Label25.Enabled := true;
 Groupbox10.Caption := 'Set Default Values For This Sensor';
 For I := 0 to oeMain.GESL.Count -1 do begin
  If uppercase(GenName) = uppercase(oeMain.GESL[I].Name) then begin
   GenIndex := I;
   Break;
  end;
 end;
 For I := 0 to oeMain.GESL[GenIndex].Count - 1 do begin
  If oeMain.GESL[GenIndex][I].Count > 0 then begin
   Combobox2.Items.add(oeMain.GESL[GenIndex][I].SensorName);
  end;
 end;
 Combobox2.Sorted := true;
 If Combobox2.Items.count = 0 then begin
  Combobox2.enabled := false;
  listprops.Enabled := false;
  exit;
 end;
 Combobox2.Enabled := true;
 listprops.Enabled := true;
 Combobox2.ItemIndex := 0;
 Combobox2Change(self);
end;

procedure TPrefDlg.ComboBox2Change(Sender: TObject);
Var
 PI, SI, I, II: Integer;
 List: TStringlist;
 N: String;
begin
 N := Treeview1.Selected.Text + '.' + Combobox2.Text;
 If not oeMain.GESL.GetIndexByName(N, PI, SI) then begin
  MessageDlg('Unable to find specific sensor in plugin.' + #13#10 + N, mtError, [mbOK], 0);
  exit;
 end;
 List := TStringList.Create;
 For I := 0 to oeMain.GESL[PI][SI].Count - 1 do begin
  List.Add(oeMain.GESL[PI][SI][I].Name);
 end;
 List.Sort;
 If assigned(SupportList) then begin
  FreeAndNil(SupportList);
 end;
 SupportList := TVisPlugEditorSupportList.Create;
 For I := 0 to list.count - 1 do begin
  For II := 0 to oeMain.GESL[PI][SI].Count - 1 do begin
   If uppercase(List[I]) = uppercase(oeMain.GESL[PI][SI][II].Name) then begin
    SupportList.Add(
     oeMain.GESL[PI][SI][II].Name,
     oeMain.GESL[PI][SI][II].Value,
     oeMain.GESL[PI][SI][II].Description,
     oeMain.GESL[PI][SI][II].Required,
     oeMain.GESL[PI][SI][II].ValueType,
     oeMain.GESL[PI][SI][II].ValueRange,
     oeMain.GESL[PI][SI][II].CanContainSensor
    );
   end;
  end;
 end;
 SensorIndex := SI;
 FreeAndNil(List);
 LoadOI;
end;

procedure TPrefDlg.Button6Click(Sender: TObject);
begin
 Colordialog1.Color := oinonfont.Font.Color;
 If not colordialog1.execute then exit;
 oinonfont.Font.Color := colordialog1.color;
end;

procedure TPrefDlg.Button7Click(Sender: TObject);
begin
 Colordialog1.Color := oinonbackground.Brush.Color;
 If not colordialog1.execute then exit;
 oinonbackground.Brush.Color := colordialog1.color;
end;

end.
