{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecCustDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
{$IFNDEF EC_VCL6_UP} ecColCombo,{$ENDIF}
  Dialogs, StdCtrls, ExtCtrls, ecSyntAnal, ecSyntMemo, ecsnStyleEdit;

type
  TCustomSyntDlg = class(TForm)
    LB: TListBox;
    Label1: TLabel;
    Button2: TButton;
    Button3: TButton;
    FD: TFontDialog;
    SyntaxMemo1: TSyntaxMemo;
    SyntStyleFrame1: TSyntStyleFrame;
    FSynt: TSyntAnalyzer;
    procedure LBClick(Sender: TObject);
    procedure SyntaxMemo1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormShow(Sender: TObject);
  public
  end;

function CustomizeHighlightingStyles(Synt: TSyntAnalyzer): Boolean;

implementation

{$R *.dfm}

function CustomizeHighlightingStyles(Synt: TSyntAnalyzer): Boolean;
begin
  with TCustomSyntDlg.Create(nil) do
  try
    FSynt.Assign(Synt);
    Result := ShowModal = mrOk;
    if Result then
     Synt.Assign(FSynt);
  finally
    Free;
  end;
end;

{ TCustomSyntDlg }

procedure TCustomSyntDlg.FormShow(Sender: TObject);
var i: integer;
begin
  SyntStyleFrame1.DisableStyle := True;
  SyntaxMemo1.Lines.Assign(FSynt.SampleText);
  for i := 0 to FSynt.Formats.Count - 1 do
    LB.Items.AddObject(FSynt.Formats[i].DisplayName, FSynt.Formats[i]);
  if LB.Items.Count > 0 then
    LB.ItemIndex := 0;
  LBClick(LB);
end;

procedure TCustomSyntDlg.LBClick(Sender: TObject);
begin
  if LB.ItemIndex >= 0 then
    SyntStyleFrame1.Style := TSyntaxFormat(LB.Items.Objects[LB.ItemIndex])
  else
    SyntStyleFrame1.Style := nil;
end;

procedure TCustomSyntDlg.SyntaxMemo1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var p: integer;
    F: TSyntaxFormat;
begin
  with SyntaxMemo1 do
   begin
    F := FormatAtPos(CaretPosToStrPos(MouseToCaret(X, Y)));
    if F <> nil then
     begin
       p := LB.Items.IndexOfObject(F);
       if p <> -1 then
        begin
          LB.ItemIndex := p;
          LBClick(LB);
        end;
     end;
   end;
  Abort;
end;

end.
