unit acMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ImgList, ecSyntAnal, ecPopupCtrl, ecSyntMemo,
  ecAutoReplace, ecKeyMap, ecSyntDlg;

type
  TForm4 = class(TForm)
    Panel1: TPanel;
    SyntaxMemo1: TSyntaxMemo;
    SyntAnalyzer1: TSyntAnalyzer;
    AutoCompletePopup1: TAutoCompletePopup;
    SyntStyles1: TSyntStyles;
    ImageList1: TImageList;
    Label1: TLabel;
    SyntaxMemo2: TSyntaxMemo;
    Splitter1: TSplitter;
    SyntaxMemo3: TSyntaxMemo;
    Label2: TLabel;
    Panel2: TPanel;
    Button1: TButton;
    Splitter2: TSplitter;
    Button2: TButton;
    Label3: TLabel;
    ComboBox1: TComboBox;
    CheckBox1: TCheckBox;
    SyntAutoReplace1: TSyntAutoReplace;
    SyntKeyMapping1: TSyntKeyMapping;
    ecSelCharPopup1: TecSelCharPopup;
    TemplatePopup1: TTemplatePopup;
    SyntFindDialog1: TSyntFindDialog;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Label4: TLabel;
    ComboBox2: TComboBox;
    CheckBox4: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure AutoCompletePopup1GetAutoCompleteList(Sender: TObject;
      Pos: TPoint; List, Display: TStrings);
    procedure AutoCompletePopup1BeforeComplete(Sender: TObject;
      var Item: String);
    procedure SyntAutoReplace1AutoReplace(APos: Integer; const AWord: string;
      var AReplace: String; var Accept: Boolean);
    procedure ecSelCharPopup1CloseUp(Sender: TObject; var Accept: Boolean);
    procedure AutoCompletePopup1Change(Sender: TObject);
    procedure SyntKeyMapping1ExecuteCommand(Sender: TObject;
      Command: Integer; Data: Pointer; var Handled: Boolean);
    procedure SyntaxMemo1CheckChar(Sender: TObject; C: Word;
      var IsWord: Boolean);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure ecSelCharPopup1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

uses ecCmdConst, ecStrUtils, Buttons, ComCtrls;

{$R *.dfm}

type
  TAccessToolButton = class(TToolButton);

procedure TForm4.FormShow(Sender: TObject);
begin
  Button1Click(nil);
  ComboBox1.ItemIndex := 0;
  ComboBox2.ItemIndex := 1;
end;

procedure TForm4.Button1Click(Sender: TObject);
//var i: integer;
begin
  AutoCompletePopup1.Items := SyntaxMemo2.Lines;
  AutoCompletePopup1.DisplayItems := SyntaxMemo3.Lines;
{  for i := 0 to 500 do
    begin
      AutoCompletePopup1.Items.AddStrings(SyntaxMemo2.Lines);
      AutoCompletePopup1.DisplayItems.AddStrings(SyntaxMemo3.Lines);
    end;}
end;

procedure TForm4.AutoCompletePopup1Change(Sender: TObject);
var Idx: integer;
begin
  with AutoCompletePopup1.ListBox do
    if ItemIndex = -1 then Idx := -1
      else Idx := AutoCompletePopup1.ListItemIndex(ItemIndex);
  if Idx <> -1 then
    AutoCompletePopup1.ToolHint.Text := AutoCompletePopup1.Items[Idx]
  else
    AutoCompletePopup1.ToolHint.Text := '';
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
  SyntStyles1.Customize;
end;

procedure TForm4.ComboBox1Change(Sender: TObject);
begin
  AutoCompletePopup1.SortType := TAutoCompleteSort(ComboBox1.ItemIndex);
end;

procedure TForm4.CheckBox1Click(Sender: TObject);
begin
  AutoCompletePopup1.AutoSize := CheckBox1.Checked;
end;

procedure TForm4.AutoCompletePopup1GetAutoCompleteList(Sender: TObject;
  Pos: TPoint; List, Display: TStrings);
begin
  AutoCompletePopup1.ListBox.MultiSelect := True;
end;

procedure TForm4.AutoCompletePopup1BeforeComplete(Sender: TObject;
  var Item: String);
var i: integer;
begin
  with AutoCompletePopup1.ListBox do
  if SelCount > 1 then
   begin
    Item := '';
    for i := 0 to Items.Count - 1 do
      if Selected[i] then
        begin
          if Item <> '' then Item := Item + ', ';
          Item := Item + AutoCompletePopup1.GetListItem(i);
        end;
   end;
end;

procedure TForm4.SyntAutoReplace1AutoReplace(APos: Integer; const AWord: string;
  var AReplace: String; var Accept: Boolean);
var idx: integer;
begin
  with SyntaxMemo1.SyntObj do
    begin
      idx := TokenAtPos(APos);
      Accept := (idx = -1) or (Tags[idx].TokenType <> 1{comment});
    end;
end;

procedure TForm4.ecSelCharPopup1CloseUp(Sender: TObject;
  var Accept: Boolean);
begin
  if Accept then
    begin
      ecSelCharPopup1.SyntMemo.InsertText(Format('chr(%d)', [ord(ecSelCharPopup1.SelChar)]));
      Accept := False;
    end;
end;

// Example of new command - duplicating line
procedure TForm4.SyntKeyMapping1ExecuteCommand(Sender: TObject;
  Command: Integer; Data: Pointer; var Handled: Boolean);
var p: TPoint;
begin
  if Command = 1001 then
    with Sender as TCustomSyntaxMemo do
      if CaretPos.Y < Lines.Count then
        begin
          BeginUpdate;
          try
            p := CaretPos;
            CaretPos := Point(Lines.LineLength(CaretPos.Y), CaretPos.Y);
            InsertText(sLineBreak + Lines[CaretPos.Y]);
            CaretPos := p;
          finally
            EndUpdate;
          end;
        end;
end;

// changing word characters for word functions (including picking word to find)
procedure TForm4.SyntaxMemo1CheckChar(Sender: TObject; C: Word;
  var IsWord: Boolean);
begin
  IsWord := IsWord or (C = Ord('-'));
end;

procedure TForm4.CheckBox2Click(Sender: TObject);
begin
  with AutoCompletePopup1 do
    if CheckBox2.Checked then
      Options := Options + [aoFormatted]
    else
      Options := Options - [aoFormatted];
end;

procedure TForm4.CheckBox3Click(Sender: TObject);
begin
  with AutoCompletePopup1 do
    if CheckBox3.Checked then
      Options := Options + [aoWithColumns]
    else
      Options := Options - [aoWithColumns];
end;

procedure TForm4.ComboBox2Change(Sender: TObject);
begin
  AutoCompletePopup1.FilterType := TAutoCompleteFilter(ComboBox2.ItemIndex);
end;

procedure TForm4.CheckBox4Click(Sender: TObject);
begin
  AutoCompletePopup1.FixedItemHeight := CheckBox4.Checked;
end;

procedure TForm4.ecSelCharPopup1Change(Sender: TObject);
begin
  with ecSelCharPopup1.ToolHint do
    Text := StringReplace(Text, '$','0x',[]);
end;

end.

