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

unit ecLinesEditor;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ecSyntMemo, ecSyntAnal, ImgList,
  ComCtrls, ActnList, Buttons, ToolWin, Menus, StdActns,ecSyntDlg,
{$IFDEF EC_DOTNET}
  Variants,
{$ENDIF}
  ShellApi, ecKeyMap, ecActns, ecStylesCombo;

type

  TSyntEditMain = class(TForm)
    SyntaxMemo1: TSyntaxMemo;
    ActionList1: TActionList;
    SB: TStatusBar;
    SyntFindDialog1: TSyntFindDialog;
    actOpen: TAction;
    OD: TOpenDialog;
    SD: TSaveDialog;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
   ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    ToolButton13: TToolButton;
    ToolButton14: TToolButton;
    ToolButton15: TToolButton;
    SyntReplaceDialog1: TSyntReplaceDialog;
    ToolButton16: TToolButton;
    MainMenu1: TMainMenu;
    actNewFile: TAction;
    actSaveAs: TAction;
    File1: TMenuItem;
    Edit1: TMenuItem;
    New1: TMenuItem;
    Open1: TMenuItem;
    Saveas1: TMenuItem;
    Copy1: TMenuItem;
    Cut1: TMenuItem;
    Paste1: TMenuItem;
    Delete1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    Undo1: TMenuItem;
    Redo1: TMenuItem;
    Findnext1: TMenuItem;
    Replace1: TMenuItem;
    Options1: TMenuItem;
    N4: TMenuItem;
    SelectAll1: TMenuItem;
    PopupMenu1: TPopupMenu;
    Copy2: TMenuItem;
    Cut2: TMenuItem;
    Paste2: TMenuItem;
    Delete2: TMenuItem;
    N7: TMenuItem;
    ogglebookmarks1: TMenuItem;
    Gotobookmarks1: TMenuItem;
    Bookmark01: TMenuItem;
    Bookmark11: TMenuItem;
    Bookmark21: TMenuItem;
    Bookmark31: TMenuItem;
    Bookmark41: TMenuItem;
    Bookmark51: TMenuItem;
    Bookmark61: TMenuItem;
    Bookmark71: TMenuItem;
    Bookmark81: TMenuItem;
    Bookmark91: TMenuItem;
    Bookmark02: TMenuItem;
    Bookmark12: TMenuItem;
    Bookmark22: TMenuItem;
    Bookmark32: TMenuItem;
    Bookmark42: TMenuItem;
    Bookmark52: TMenuItem;
    Bookmark62: TMenuItem;
    Bookmark72: TMenuItem;
    Bookmark82: TMenuItem;
    Bookmark92: TMenuItem;
    ToolButton17: TToolButton;
    SyntaxHighlignting2: TMenuItem;
    View1: TMenuItem;
    FullExpand1: TMenuItem;
    FullCollapse1: TMenuItem;
    EditPaste1: TEditPaste;
    EditSelectAll1: TEditSelectAll;
    EditUndo1: TEditUndo;
    EditDelete1: TEditDelete;
    actShowNonPrinted1: TMenuItem;
    EditCopy1: TEditCopy;
    EditCut1: TEditCut;
    N10: TMenuItem;
    ToolButton19: TToolButton;
    ToolButton20: TToolButton;
    ToolButton21: TToolButton;
    ImageList1: TImageList;
    KeyMapping1: TMenuItem;
    Action11: TMenuItem;
    ecCommandRedo: TecCommandAction;
    ecCommandAction1: TecCommandAction;
    ecCommandAction2: TecCommandAction;
    ecFullCollapse1: TecFullCollapse;
    ecFullExpand1: TecFullExpand;
    ecCommandAction3: TecCommandAction;
    CollapseSelection1: TMenuItem;
    ecSearch1: TecSearch;
    ecSearchAgain1: TecSearchAgain;
    ecReplace1: TecReplace;
    ToolButton24: TToolButton;
    ToolButton25: TToolButton;
    ToolButton26: TToolButton;
    ToolButton27: TToolButton;
    ToolButton28: TToolButton;
    ToolButton29: TToolButton;
    ToolButton30: TToolButton;
    ToolButton31: TToolButton;
    Userstyles1: TMenuItem;
    ecUserRangeDelete1: TecUserRangeDelete;
    ecUserRangeCollapsable1: TecUserRangeCollapsable;
    ecUserRangeTopLine1: TecUserRangeTopLine;
    ecUserRangeBottomLine1: TecUserRangeBottomLine;
    ecUserRangeLineHighlight1: TecUserRangeLineHighlight;
    Userranges1: TMenuItem;
    Makecollapsible1: TMenuItem;
    Rangetopline1: TMenuItem;
    Rangebottomline1: TMenuItem;
    Highlightlines1: TMenuItem;
    Deleterange1: TMenuItem;
    N1: TMenuItem;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    ecToggleNonPrinted1: TecToggleNonPrinted;
    ecToggleWordWrap1: TecToggleWordWrap;
    Wordwrap1: TMenuItem;
    ToolButton18: TToolButton;
    ecCustomizeLexerPropertiesAction1: TecCustomizeLexerPropertiesAction;
    ecCustomizeUserStylesAction1: TecCustomizeUserStylesAction;
    ecCustomizeKeyMappingAction1: TecCustomizeKeyMappingAction;
    UserStylesCombo1: TUserStylesCombo;
    procedure SyntaxMemo1CaretPosChanged(Sender: TObject);
    procedure SyntaxMemo1Change(Sender: TObject);
    procedure actOpenExecute(Sender: TObject);
    procedure actNewFileExecute(Sender: TObject);
    procedure actSaveAsExecute(Sender: TObject);
    procedure Bookmark91Click(Sender: TObject);
    procedure Bookmark92Click(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
    procedure SyntaxMemo1SetBookmark(Snder: TObject; Bookmark: TBookmark; var Accept: Boolean);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    FFileName: string;
    procedure OpenFile(AFileName: string);
  end;

implementation

{$R *.dfm}

procedure TSyntEditMain.SyntaxMemo1SetBookmark(Snder: TObject;
  Bookmark: TBookmark; var Accept: Boolean);
begin
  with Bookmark do
   if (BmIndex >= 0) and (BmIndex < 10) then
    begin
     ImageIndex := 13 + BmIndex;
     Hint := 'Bookmark ' + IntToStr(BmIndex);
    end;
end;

procedure TSyntEditMain.SyntaxMemo1CaretPosChanged(Sender: TObject);
begin
  with SyntaxMemo1.CaretPos do
    SB.Panels[0].Text := Format('  %d: %d', [X, Y]);
end;

procedure TSyntEditMain.SyntaxMemo1Change(Sender: TObject);
begin
  if SyntaxMemo1.Modified then  SB.Panels[1].Text := 'Modified'
                          else  SB.Panels[1].Text := '';
  if SyntaxMemo1.ReplaceMode then SB.Panels[2].Text := 'Overwrite'
                             else SB.Panels[2].Text := 'Insert';
end;

procedure TSyntEditMain.OpenFile(AFileName: string);
begin
  SyntaxMemo1.LoadFromFile(AFileName);
  Caption := 'EControl syntax editor';
end;

procedure TSyntEditMain.actOpenExecute(Sender: TObject);
begin
  if OD.Execute then
   OpenFile(OD.FileName);
end;

procedure TSyntEditMain.actSaveAsExecute(Sender: TObject);
begin
  SD.FileName := FFileName;
  if SD.Execute then
   begin
    SyntaxMemo1.SaveToFile(SD.FileName);
    FFileName := SD.FileName;
   end;
end;

procedure TSyntEditMain.actNewFileExecute(Sender: TObject);
begin
  SyntaxMemo1.Lines.Clear;
  SyntaxMemo1.Modified := False;
  FFileName := '';
end;

procedure TSyntEditMain.Bookmark91Click(Sender: TObject);
begin
  SyntaxMemo1.ToggleBookmark((Sender as TComponent).Tag);
end;

procedure TSyntEditMain.Bookmark92Click(Sender: TObject);
begin
  SyntaxMemo1.GotoBookmark((Sender as TComponent).Tag);
end;

procedure TSyntEditMain.PopupMenu1Popup(Sender: TObject);
var i, j: integer;
begin
  for i := 0 to 9 do
   for j := 1 to 2 do
    (FindComponent('Bookmark'+IntToStr(i)+IntToStr(j)) as TMenuItem).Checked :=
      SyntaxMemo1.Bookmarks[i] <> -1;
end;

procedure TSyntEditMain.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if SyntaxMemo1.Modified and (ModalResult = mrCancel) then
   case MessageBox(Handle, 'Do you want to save changes?', 'Confirmation', MB_ICONQUESTION or MB_YESNOCANCEL) of
     IDYes: ModalResult := mrOk;
     IDCancel: CanClose := False;
   end;
end;

end.
