unit HighLightfrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SynEdit, SynEditHighlighter, SynHighlighterPas, StdCtrls,
  SynHighlighterJava, SynHighlighterCpp, SynHighlighterCS;

type
  PGlobalDLLData = ^TGlobalDLLData;
  TGlobalDLLData = record
    TheHighLighter: TSynCSSyn;
  end;

  TfrmHighLighter = class(TForm)
    SynCSSyn1: TSynCSSyn;
    btnClose: TButton;
    SynEdit1: TSynEdit;
    procedure btnCloseClick(Sender: TObject);
  private
    FDefaultHightLighter: string;
    { Private declarations }
  public
    property DefaultHightLighter: string read FDefaultHightLighter;
    { Public declarations }
  end;

var
  frmHighLighter: TfrmHighLighter;

  procedure OpenSharedData;
  procedure CloseSharedData;
  procedure GetDLLData(var AGlobalData: PGlobalDLLData); export; StdCall;
  procedure Initial(AHighLighters: String); export; stdcall;
  function GetHighLighters(ADefault: Boolean = False): string; export; stdcall;
  function GetExample: string; export; stdcall;

const
  cMMFileName: PChar = 'SharedMapData';

implementation

{$R *.dfm}

var
  GlobalData : PGlobalDLLData;
  MapHandle  : THandle;
  
procedure Initial(AHighLighters: String);
var
  I: Integer;
  sHighLigher: string;
  lstHighLights: TStringList;
  procedure ConfigHighLight(AHighLighter: string; Attributes: TSynHighlighterAttributes);
  var
    lstHighLight: TStringList;
  begin
    lstHighLight := TStringList.Create;
    lstHighLight.CommaText := AHighLighter;
    if lstHighLight.Count > 6 then
    begin
      Attributes.Foreground := StrToIntDef(lstHighLight.Strings[1], 0);
      Attributes.Background := StrToIntDef(lstHighLight.Strings[2], 0);
      Attributes.Style := [];
      if SameText(lstHighLight.Strings[3], '1')  then
      begin
        Attributes.Style := Attributes.Style + [fsBold];
      end;
      if SameText(lstHighLight.Strings[4], '1')  then
      begin
        Attributes.Style := Attributes.Style + [fsItalic];
      end;
      if SameText(lstHighLight.Strings[5], '1')  then
      begin
        Attributes.Style := Attributes.Style + [fsUnderline];
      end;
      if SameText(lstHighLight.Strings[6], '1')  then
      begin
        Attributes.Style := Attributes.Style + [fsStrikeOut];
      end;
    end;
    lstHighLight.Free;
  end;
begin
  if not SameText(AHighLighters, '') then
  begin
    lstHighLights := TStringList.Create;
    lstHighLights.Delimiter := ';';
    lstHighLights.DelimitedText := AHighLighters;
    for I := 1 to lstHighLights.Count - 1 do
    begin
      sHighLigher := lstHighLights.Strings[I];
      Case I of
        1: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.AsmAttri);
        2: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.CommentAttri);
        3: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.DirecAttri);
        4: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.IdentifierAttri);
        5: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.InvalidAttri);
        6: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.KeyAttri);
        7: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.NumberAttri);
        8: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.SpaceAttri);
        9: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.StringAttri);
        10: ConfigHighLight(sHighLigher, frmHighLighter.SynCSSyn1.SymbolAttri);
      end;
    end;
    lstHighLights.Free;
  end;
end;

function GetExample: string;
begin
  Result := frmHighLighter.SynEdit1.Lines.Text;
end;  

function GetHighLighters(ADefault: Boolean = False): string;
const
  arrStyle: array[Boolean] of string = ('0', '1');
var
  lstHighLights: TStringList;
  function GetHighLight(ATitle: string; Attributes: TSynHighlighterAttributes): string;
  var
    lstHighLight: TStringList;
  begin
    lstHighLight := TStringList.Create;
    lstHighLight.Add(ATitle);
    lstHighLight.Add(IntToStr(Attributes.Foreground));
    lstHighLight.Add(IntToStr(Attributes.Background));
    lstHighLight.Add(arrStyle[(Attributes.Style * [fsBold]) = [fsBold]]);
    lstHighLight.Add(arrStyle[(Attributes.Style * [fsItalic]) = [fsItalic]]);
    lstHighLight.Add(arrStyle[(Attributes.Style * [fsUnderline]) = [fsUnderline]]);
    lstHighLight.Add(arrStyle[(Attributes.Style * [fsStrikeOut]) = [fsStrikeOut]]);
    Result := lstHighLight.CommaText;
    lstHighLight.Free;
  end;
begin

  if ADefault then
  begin
    Result := frmHighLighter.DefaultHightLighter;
  end
  else begin
    lstHighLights := TStringList.Create;
    lstHighLights.Add('背景色,' + IntToStr(frmHighLighter.SynEdit1.ActiveLine.Background) + ','
      + IntToStr(frmHighLighter.SynEdit1.Color) + ',' + '0,0,0,0');
    lstHighLights.Add(GetHighLight('汇编', frmHighLighter.SynCSSyn1.AsmAttri));
    lstHighLights.Add(GetHighLight('注释', frmHighLighter.SynCSSyn1.CommentAttri));
    lstHighLights.Add(GetHighLight('指示', frmHighLighter.SynCSSyn1.DirecAttri));
    lstHighLights.Add(GetHighLight('标识符', frmHighLighter.SynCSSyn1.IdentifierAttri));
    lstHighLights.Add(GetHighLight('无效部分', frmHighLighter.SynCSSyn1.InvalidAttri));
    lstHighLights.Add(GetHighLight('关键字', frmHighLighter.SynCSSyn1.KeyAttri));
    lstHighLights.Add(GetHighLight('数字', frmHighLighter.SynCSSyn1.NumberAttri));
    lstHighLights.Add(GetHighLight('Space', frmHighLighter.SynCSSyn1.SpaceAttri));
    lstHighLights.Add(GetHighLight('字符串', frmHighLighter.SynCSSyn1.StringAttri));
    lstHighLights.Add(GetHighLight('符号', frmHighLighter.SynCSSyn1.SymbolAttri));

    lstHighLights.Delimiter := ';';
    Result := lstHighLights.DelimitedText;
    lstHighLights.Free;
  end;
end;

procedure GetDLLData(var AGlobalData: PGlobalDLLData); StdCall;
begin
  AGlobalData := GlobalData;
end;

procedure OpenSharedData;
var
   Size: Integer;
begin
  Size := SizeOf(TGlobalDLLData);

  MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, Size, cMMFileName);

  if MapHandle > 0 then
  begin
    GlobalData := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, Size);
    GlobalData^.TheHighLighter := frmHighLighter.SynCSSyn1;
    if GlobalData = nil then
    begin
      CloseHandle(MapHandle);
    end;
  end;
end;

procedure CloseSharedData;
begin
  UnmapViewOfFile(GlobalData);
  CloseHandle(MapHandle);
end;


procedure TfrmHighLighter.btnCloseClick(Sender: TObject);
begin
  Close;
end;

initialization
  frmHighLighter := TfrmHighLighter.Create(Application);
  frmHighLighter.FDefaultHightLighter := GetHighLighters;

finalization
  frmHighLighter.Free;

end.

