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.

  THighLightInfo = class
  private
    FStrikeOutStyle: Boolean;
    FItalicStyle: Boolean;
    FUnderlineStyle: Boolean;
    FBoldStyle: Boolean;
    FForeGround: TColor;
    FBackGround: TColor;
    FTitle: string;
  public
    property Title: string read FTitle write FTitle;
    property ForeGround: TColor read FForeGround write FForeGround;
    property BackGround: TColor read FBackGround write FBackGround;
    property BoldStyle: Boolean read FBoldStyle write FBoldStyle;
    property ItalicStyle: Boolean read FItalicStyle write FItalicStyle;
    property UnderlineStyle: Boolean read FUnderlineStyle write FUnderlineStyle;
    property StrikeOutStyle: Boolean read FStrikeOutStyle write FStrikeOutStyle;
  end;

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

var
  frmHighLighter: TfrmHighLighter;

  procedure Initial(LstHightLighter: TStrings); export; stdcall;
  procedure HighLightContent(AEditor: TSynEdit; AEditorBackGroundColor: TColor); export; stdcall;
  function GetHightLighterList(ADefault: Boolean = False): TStrings; export; stdcall;
  function GetExample: string; export; stdcall;

implementation

{$R *.dfm}

procedure Initial(LstHightLighter: TStrings);
var
  I: Integer;
  objHighLighter: THighLightInfo;
  procedure ConfigHighLight(AHighLighter: THighLightInfo; Attributes: TSynHighlighterAttributes);
  begin
    Attributes.Foreground := AHighLighter.ForeGround;
    Attributes.Background := AHighLighter.BackGround;
    Attributes.Style := [];
    if AHighLighter.BoldStyle then
    begin
      Attributes.Style := Attributes.Style + [fsBold];
    end;
    if AHighLighter.ItalicStyle then
    begin
      Attributes.Style := Attributes.Style + [fsItalic];
    end;  
    if AHighLighter.UnderlineStyle then
    begin
      Attributes.Style := Attributes.Style + [fsUnderline];
    end;  
    if AHighLighter.StrikeOutStyle then
    begin
      Attributes.Style := Attributes.Style + [fsStrikeOut];
    end;  
  end;
begin
  if Assigned(LstHightLighter) then
  begin
    for I := 1 to LstHightLighter.Count - 1 do
    begin
      objHighLighter := THighLightInfo(LstHightLighter.Objects[I]);
      Case I of
        1: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.AsmAttri);
        2: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.CommentAttri);
        3: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.DirecAttri);
        4: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.IdentifierAttri);
        5: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.InvalidAttri);
        6: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.KeyAttri);
        7: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.NumberAttri);
        8: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.SpaceAttri);
        9: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.StringAttri);
        10: ConfigHighLight(objHighLighter, frmHighLighter.SynCSSyn1.SymbolAttri);
      end;
    end;
  end;  
end;

procedure HighLightContent(AEditor: TSynEdit; AEditorBackGroundColor: TColor); export; stdcall;
begin
  AEditor.Highlighter := frmHighLighter.SynCSSyn1;
  AEditor.Color := AEditorBackGroundColor;
  AEditor.ActiveLine.Background := AEditorBackGroundColor;
end;

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

function GetHightLighterList(ADefault: Boolean = False): TStrings;
var
  objHighLighter: THighLightInfo;
  I: Integer;
  function GetHighLight(ATitle: string; Attributes: TSynHighlighterAttributes): THighLightInfo;
  begin
    Result := THighLightInfo.Create;
    Result.Title := ATitle;
    Result.ForeGround := Attributes.Foreground;
    Result.BackGround := Attributes.Background;
    Result.BoldStyle := (Attributes.Style * [fsBold]) = [fsBold];
    Result.ItalicStyle := (Attributes.Style * [fsItalic]) = [fsItalic];
    Result.UnderlineStyle := (Attributes.Style * [fsUnderline]) = [fsUnderline];
    Result.StrikeOutStyle := (Attributes.Style * [fsStrikeOut]) = [fsStrikeOut];
  end;
  function Clone(AHighLighter: THighLightInfo): THighLightInfo;
  begin
    Result := THighLightInfo.Create;
    Result.Title := AHighLighter.Title;
    Result.ForeGround := AHighLighter.ForeGround;
    Result.BackGround := AHighLighter.BackGround;
    Result.BoldStyle := AHighLighter.BoldStyle;
    Result.ItalicStyle := AHighLighter.ItalicStyle;
    Result.UnderlineStyle := AHighLighter.UnderlineStyle;
    Result.StrikeOutStyle := AHighLighter.StrikeOutStyle;
  end;
begin
  Result := TStringList.Create;

  if ADefault then
  begin
    for I := 0 to frmHighLighter.LstDefaultHightLighter.Count - 1 do
    begin
      Result.AddObject('', Clone(THighLightInfo(frmHighLighter.LstDefaultHightLighter.Objects[I])));
    end;
  end
  else begin
    objHighLighter := THighLightInfo.Create;
    objHighLighter.Title := '背景色';
    objHighLighter.BackGround := frmHighLighter.SynEdit1.Color;
    Result.AddObject('',objHighLighter);
    Result.AddObject('',GetHighLight('汇编', frmHighLighter.SynCSSyn1.AsmAttri));
    Result.AddObject('',GetHighLight('注释', frmHighLighter.SynCSSyn1.CommentAttri));
    Result.AddObject('',GetHighLight('指示', frmHighLighter.SynCSSyn1.DirecAttri));
    Result.AddObject('',GetHighLight('标识符', frmHighLighter.SynCSSyn1.IdentifierAttri));
    Result.AddObject('',GetHighLight('无效部分', frmHighLighter.SynCSSyn1.InvalidAttri));
    Result.AddObject('',GetHighLight('关键字', frmHighLighter.SynCSSyn1.KeyAttri));
    Result.AddObject('',GetHighLight('数字', frmHighLighter.SynCSSyn1.NumberAttri));
    Result.AddObject('',GetHighLight('Space', frmHighLighter.SynCSSyn1.SpaceAttri));
    Result.AddObject('',GetHighLight('字符串', frmHighLighter.SynCSSyn1.StringAttri));
    Result.AddObject('',GetHighLight('符号', frmHighLighter.SynCSSyn1.SymbolAttri));
  end;
end;

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

procedure TfrmHighLighter.FormDestroy(Sender: TObject);
var
  I: Integer;
begin
  if Assigned(FLstDefaultHightLighter) then
  begin
    for I := 0 to FLstDefaultHightLighter.Count - 1 do
    begin
      THighLightInfo(FLstDefaultHightLighter.Objects[I]).Free;
    end;
    FLstDefaultHightLighter.Free;
  end;
end;

initialization
  frmHighLighter := TfrmHighLighter.Create(Application);
  frmHighLighter.FLstDefaultHightLighter := GetHightLighterList;

finalization
  frmHighLighter.Free;

end.
 
