⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainedit.pas

📁 大家是不是为不知道如何在VB学到绝地程序
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    Procedure EditExpandAllExecute(Sender: TObject);
    Procedure MacroPlayExecute(Sender: TObject);
    Procedure MacroPauseExecute(Sender: TObject);
    Procedure MacroStopExecute(Sender: TObject);
    Procedure MacroRecordExecute(Sender: TObject);
    Procedure ViewMacrobarExecute(Sender: TObject);
    Procedure ViewMacrobarUpdate(Sender: TObject);
    procedure ActionListUpdate(Action: TBasicAction; var Handled: Boolean);
  Private
    { Private declarations }
    SearchOptions: TSynSearchOptions;
    strExports: TStringList;
    FAllowDocking: Boolean;
    FAllowDroping: Boolean;
    tran: TvgTranslator;
    mwPrintOptions: TSynEditPrint;

    Function GetEditor(Index: Integer): TMySynEdit;
    Function GetEditorCount: Integer;
    Procedure SetAllowDocking(Const Value: Boolean);
    Procedure SetAllowDroping(Const Value: Boolean);

    {Navigate Keys Commands:}
    Procedure JumpUpProc(Editor: TMySynEdit);
    //(PASCAL)jump to the upper procedure before, Default Key: Ctrl+PGUP
    Procedure JumpDnProc(Editor: TMySynEdit);
    //(PASCAL)jump to the lower procedure after, Default Key: Ctrl+PGDN
  Protected
    Procedure WMDropFiles(Var Msg: TWMDropFiles); Message WM_DROPFILES;
    Procedure WMRestoreApp(Var Msg: TMessage); Message WM_RESTOREAPP;
    Procedure WMNotExists(Var Msg: TMessage); Message WM_NOTEXISTS;
    Procedure ShowApplication;
    Function PromptSave: Boolean;
    // Add Current file to Recent File Menu
    Procedure AddFileToMRL(Const FileName: String);
    Procedure AppHint(Sender: TObject);
    Procedure AutoMakeProc(Editor: TMySynEdit; Var Key: Word);
    //(PASCAL)AutoComplete Proc Protocol, Default Key: Ctrl+Shift+C
  Public
    Procedure StatusChangeEvent(Sender: TObject; Changes: TSynStatusChanges);
    Procedure SpecialLineColorsEvent(Sender: TObject; Line: Integer; Var Special: Boolean; Var FG, BG: TColor);
    Procedure ReplaceTextEvent(Sender: TObject; Const ASearch, AReplace: String; Line, Column: Integer; Var Action: TSynReplaceAction);
    Procedure MouseDownEvent(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    Procedure PrintStatusEvent(Sender: TObject; Status: TSynPrintStatus; PageNumber: Integer; Var Abort: Boolean);
    Procedure DropFilesEvent(Sender: TObject; X, Y: Integer; Files: TStrings);
    Procedure KeyDownEvent(Sender: TObject; Var Key: Word; Shift: TShiftState);
    Procedure ProcessUserCommandEvent(Sender: TObject; Var Command: TSynEditorCommand; Var AChar: Char; Data: Pointer);
    Procedure RecorderStateChangeEvent(NewState: TRecorderState);
    Procedure HandleMessage(Var Msg: TMsg; Var Handled: Boolean);
    Procedure HexEditor1StateChanged(Sender: TObject);
    Procedure WindowClick(Sender: TObject);
    Procedure ToolsMenuClick(Sender: TObject);
    Procedure SelectLanMenuClick(Sender: TObject);
    Procedure DefaultHandler(Var Message); Override;
    Procedure BuildToolsMenu;
    Procedure BuildLanguagesMenu;
    Function OpenFile(AFileName: String; NeedSetFocus: Boolean = True): TMySynEdit;
    Procedure OpenFilePos(AFileName: String; X, Y: Integer; Const Bookmarks: TBookMarksSet; bkPos: String; NeedSetFocus: Boolean = True);
    Function FileAlreadyOpen(Const AFileName: String): Boolean;
    Function SearchStr(Const ASearch: String; AOptions: TSynSearchOptions; Var P: TPoint): Integer; //if found then result >0 and result in P, else result = 0;
    Function CloseFile(Editor: TMySynEdit): TModalResult;
    Procedure SaveFile(Editor: TMySynEdit);
    Procedure SaveAsFile(Editor: TMySynEdit);
    Function CurrentEditor: TMySynEdit;
    Property Editor[Index: Integer]: TMySynEdit Read GetEditor;
    Property EditorCount: Integer Read GetEditorCount;
    Property AllowDroping: Boolean Read FAllowDroping Write SetAllowDroping;
    Property AllowDocking: Boolean Read FAllowDocking Write SetAllowDocking;
  End;

Var
  frmJediEdit: TfrmJediEdit;
  DFMWasText: Boolean; //Delphi Form File was Text
  {Editor Var}
  {optAutoIndent: Boolean = True;
  optDragDropEditing: Boolean = False;
  optDropFiles: Boolean = True;
  optHalfPageScroll: Boolean = False;
  optScrollPastEol: Boolean = False;
  optTabsToSpaces: Boolean = True;
  optSmartTabs: Boolean = True;
  optUndoAfterSave: Boolean = False;
  optAllowDocking: Boolean = False;
  optAutoSave: Boolean = False;}

  {Hex Search}
  FindPos: Integer = -1;
  FindBuf: PChar = Nil;
  FindLen: Integer = 0;
  FindHexStr: String = '';
  FindICase: Boolean = False;

  CurrentLan: String = ''; //Current Language Default

Const
  stLinePanel = 0;
  stModifiedPanel = 1;
  stTotalLinePanel = 2;
  stInsertPanel = 3;
  stSelectionPanel = 4;
  stDatePanel = 5;
  sthintPanel = 6;

  // Check up to the first 8k to see if there are any nonprintable chars
Function IsFileHex(Const AFileName: String): Boolean;

Implementation

Uses
  IniFiles,
  AppUtils,
  AboutDlg,
  ReplaceDialog,
  ReplacePromptDialog,
  FindDialog,
  EditModule,
  RxShell,
  PrintStatusDialog,
  GrepResultsDlg;

{$R *.DFM}

Const
  // Print Margin, unit: millimeters
  // 2 centimeter margins
  LeftMargin = 20;
  RightMargin = 20;
  TopMargin = 20;
  BottomMargin = 20;

  MyToolsCount: Integer = 1; //don't bother , it auto-maintained.
  sProductName: String = 'Jedi Program Editor';
  sProductVer: String = '';

Procedure TfrmJediEdit.FormShow(Sender: TObject);
Begin
  If ParamCount > 0 Then OpenFile(ParamStr(1))
  Else If EditorCount <= 0 Then FileNew.Execute
  Else CurrentEditor.SetFocus;
End;

Function TfrmJediEdit.OpenFile(AFileName: String; NeedSetFocus: Boolean = True): TMySynEdit;
Var
  Page: TTabSheet;
  Edit: TMySynEdit;
  BinFile: Boolean;
  Procedure LoadFile(Const FileName: String);
  Var
    Ext: String;
    OldCursor: TCursor;
  Begin
    OldCursor := Cursor;
    Try
      Cursor := CrHourGlass;
      Windows.SetCursor(Screen.Cursors[CrHourGlass]);
      Ext := UpperCase(ExtractFileExt(FileName));
      Edit.HighLighter := GetExtHighLighter(Ext);
      If Ext = '.DFM' Then BinFile := False
      Else BinFile := IsFileHex(AFileName);
      If BinFile Then Edit.HexEdit.LoadFromFile(AFileName)
      else if ext = '.DFM' then
      begin
        LoadDFMFile2Strings(aFileName, Edit.Lines, DFMWasText);
        //Edit.HighLighter := EditorDataModule.SynHighlighterDfm1;
      end else begin
        Edit.Lines.LoadFromFile(AFileName);
        Edit.LoadCollaspeList;
      End;
      If Edit.HighLighter = EditorDataModule.PythonSyn1 Then EditorDataModule.PythonBehaviour1.Editor := Edit
      Else EditorDataModule.PythonBehaviour1.Editor := Nil;
    Finally
      Cursor := OldCursor;
    End;
  End;

Var
  Key1: Word;
  Shift1: TShiftState;
  s: string;
Begin
  If FileAlreadyOpen(AFileName) Then Begin
    Result := CurrentEditor;
    Exit;
  End;
  Page := TTabSheet.Create(PageControl);
  Page.PageControl := PageControl;
  Edit := TMySynEdit.Create(Page);
  Edit.Visible := False;
  Edit.Parent := Page;
  Edit.Align := AlClient;
  Edit.Width := ClientWidth;
  Edit.Height := PageControl.Height;
  Edit.NeedSetFocus := NeedSetFocus;
  Perform(WM_SETREDRAW, 0, 0); //Lock window
  Try
    Edit.WantTabs := True;
    Edit.BookMarkOptions.BookMarkImages := EditorDataModule.ilBookMarks;
    With Edit Do Begin
      HexEdit.Visible := False;
      HexEdit.Parent := Page;
      HexEdit.Align := AlClient;
      HexEdit.WantTabs := False;
      HexEdit.BytesPerColumn := 1;
      HexEdit.OnStateChanged := HexEditor1StateChanged;
    End;

    If FAllowDocking Then Begin
      Edit.DragKind := dkDock;
      Edit.DragMode := DmAutomatic;
    End Else Begin
      Edit.DragKind := DkDrag;
      Edit.DragMode := DmManual;
    End;

    If (AFileName <> '') Then Begin
      If (PageControl.ActivePage.Caption = (tran.TMsg(DefaultFileName) + '1')) And (Not CurrentEditor.Modified) Then PageControl.ActivePage.Free;
      Page.Caption := ExtractFileName(AFileName);
      Edit.FileName := AFileName;
      If Not FileExists(AFileName) Then ShowMessage(Format(tran.TMsg(sNoFileExist), [AFileName]))
      Else LoadFile(AFileName);
    End Else Begin
      Page.Caption := tran.TMsg(DefaultFileName) + IntToStr(DefaultFileNameCount);
      Inc(DefaultFileNameCount);
    End;
    Edit.Modified := False;
    Edit.Caption := Page.Caption;

    dlgOption.AssignOptions(Edit);

    Edit.OnStatusChange := StatusChangeEvent;
    Edit.OnDropFiles := DropFilesEvent;
    Edit.OnReplaceText := ReplaceTextEvent;
    Edit.OnMouseDown := MouseDownEvent;
    Edit.OnProcessUserCommand := ProcessUserCommandEvent;
    Edit.OnSpecialLineColors := SpecialLineColorsEvent;
    Edit.PopupMenu := EditorPopup;

    Edit.Keystrokes.ResetDefaults;

    //Add New User Editor Commands
    ShortcutToKey(scAutoCompletion, Key1, Shift1);
    Edit.AddKey(ecAutoCompletion, Key1, Shift1, 0, []);
    ShortcutToKey(nkcJumpUpPro, Key1, Shift1);
    Edit.AddKey(ecJumpUpPro, Key1, Shift1, 0, []);
    ShortcutToKey(nkcJumpDnPro, Key1, Shift1);
    Edit.AddKey(ecJumpDnPro, Key1, Shift1, 0, []);
    ShortcutToKey(nkcCollaspeNext, Key1, Shift1);
    Edit.AddKey(ecCollaspeNext, Key1, Shift1, 0, []);
    ShortcutToKey(nkcCollaspePrev, Key1, Shift1);
    Edit.AddKey(ecCollaspePrev, Key1, Shift1, 0, []);

    Edit.ClearUndo;
    PageControl.ActivePage := Page;
    Edit.HexView := BinFile;

    //Load AutoComplete List
    if Edit.HighLighter <> nil then
    begin
      s := GetTemplateFileName(Edit.HighLighter.LanguageName);
      If FileExists(s) Then begin
        EditorDataModule.AutoComplete1.AutoCompleteList.LoadFromFile(s);
      end;
    end;

    PageControlChange(Self);
    StatusChangeEvent(Edit, []);
  Finally
    Perform(WM_SETREDRAW, 1, 0); //UnLock window
  End;
  RedrawWindow(Handle, Nil, 0, RDW_FRAME + RDW_INVALIDATE +
    RDW_ALLCHILDREN + RDW_NOINTERNALPAINT);
  AddFileToMRL(AFileName); //MRL means Most Recently file List
  cbxHighlighter.Enabled := Not BinFile;
  tbHighlighter.Enabled := Not BinFile;
  Result := Edit;
  {$IFDEF debug}
  SendDebug('OpenFile OK!');
  {$ENDIF}
End;

Procedure TfrmJediEdit.FileNewExecute(Sender: TObject);
Begin
  OpenFile('');
End;

Procedure TfrmJediEdit.FormCreate(Sender: TObject);
Var
  s, bkPos: String;
  I, x, y: Integer;
  NewItem: TMenuItem;
  Icon: TIcon;
  BookMarks: TBookMarksSet;
Begin
  tran := TvgTranslator.Create(Self);
  Application.OnMessage := HandleMessage;
  TheRecorder.OnStateChange := RecorderStateChangeEvent;
  RecorderStateChangeEvent(rsIdle);
  FormStorage1.IniFileName := ExtractFilePath(Application.ExeName) + GetDefaultIniName;
  CurrentLan := FormStorage1.ReadString('DefaultLanguage', CurrentLan);
  BuildLanguagesMenu;
  tran.LanguageFile := CurrentLan;
  tran.Translate;

  With AppVerInfo Do Begin
    Try
      sProductName := ProductName;
      sProductVer := FileVersion;
    Finally
      Free;
    End;
  End;
  Application.Title := tran.TMsg(sProductName) + ' ' + sProductVer;
  mwPrintOptions := TSynEditPrint.Create(Self);
  mwPrintOptions.Wrap := False;
  mwPrintOptions.Header.Add(tran.TMsg(sTitle) + ': $title$', Self.Font, taCenter, 0);
  mwPrintOptions.Header.Add(tran.TMsg(sPrintDateTime) + ': $time$ $date$', Self.Font, taLeftJustify, 0);
  mwPrintOptions.Header.Add(tran.TMsg(sPrintBy) + ' ' + tran.TMsg(sProductName), Self.Font, taRightJustify, 0);
  mwPrintOptions.Header.Add('', Self.Font, taRightJustify, 0);

  mwPrintOptions.Colors := False;
  mwPrintOptions.Margins.UnitSystem := usMM;
  mwPrintOptions.Margins.Left := LeftMargin;
  mwPrintOptions.Margins.Right := RightMargin;
  mwPrintOptions.Margins.Top := TopMargin;
  mwPrintOptions.Margins.Bottom := BottomMargin;

  mwPrintOptions.Footer.Add('', Self.Font, taRightJustify, 0);
  mwPrintOptions.Footer.Add(tran.TMsg(sPageNumber) + ': $pagenum$ ' + sPrintDateTime + ': $datetime$', Self.Font, taRightJustify, 0);

  MyToolsCount := mmiTools.Count;
  If Not Assigned(strExports) Then strExports := TStringList.Create;
  With EditorDataModule Do EnumerateHighlighters(cbxHighlighter.Items, s);
  OpenFileDialog.Filter := s;
  SaveDialog.Filter := s;
  With EditorDataModule Do EnumerateExporters(strExports, s);
  ExportDialog.Filter := s;
  cbxHighlighter.ItemIndex := 0;
  Application.OnHint := AppHint;
  mmiWindows.Add(TMenuItem.Create(mmiWindows));

  If InternalToolsList.Count > 0 Then With mmiTools Do Begin
      NewBottomLine;
      MyToolsCount := mmiTools.Count + InternalToolsList.Count;
      For I := 0 To InternalToolsList.Count - 1 Do Begin
        NewItem := TMenuItem.Create(mmiTools);
        NewItem.Caption := InternalToolsList.Names[I];
        NewItem.Hint := InternalToolsList.Values[NewItem.Caption];
        Icon := IconExtract(NewItem.Hint, 0);
        If Icon <> Nil Then Try
          NewItem.Bitmap.Width := Icon.Width;
          NewItem.Bitmap.Height := Icon.Height;
          NewItem.Bitmap.Canvas.Draw(0, 0, Icon);
        Finally
          Icon.Free;
        End;
        NewItem.OnClick := ToolsMenuClick;
        Add(NewItem);
      End;
    End;

  If ToolsList.Count > 0 Then BuildToolsMenu;

  dlgOption := TdlgOption.Create(Self);

  s := ExtractFilePath(Application.ExeName) + GetDefaultIniName;
  EditorDataModule.LoadSettings(s);

  CanUndoAfterSave := dlgOption.chkUndoAfterSave.Checked;
  BackupFile := dlgOption.chkBackupFile.Checked;
  OverwriteBackup := dlgOption.chkOverwriteBackup.Checked;
  KeepLastOpen := dlgOption.chkLastOpen.Checked;
  sDateTimeFormat := dlgOption.cboDateTimeFormat.Text;
  AllowDroping := dlgOption.cbDropFiles.Checked;
  AllowDocking := dlgOption.chkAllowDocking.Checked;
  StatusBar.Panels[stDatePanel].Width :=
    Canvas.TextWidth(FormatDateTime(sDateTimeFormat, Now)) + 8;

  //Open Last_open Files
  If dlgOption.chkLastOpen.Checked Then With TIniFile.Create(s) Do Try
      I := ReadInteger(secLastOpen, 'FileOpenNumber', 0) - 1;
      While I >= 0 Do Begin
        s := ReadString(secLastOpen, 'LastFile_' + IntToStr(I), '');
        X := ReadInteger(secLastOpen, 'LastFile_' + IntToStr(I) + 'XPos', 1);
        Y := ReadInteger(secLastOpen, 'LastFile_' + IntToStr(I) + 'YPos', 1);
        Word(BookMarks) := ReadInteger(secLastOpen, 'LastFile_' + IntToStr(I) + 'Marks', 0);
        bkPos := ReadString(secLastOpen, 'LastFile_' + IntToStr(I) + 'MarksPos', '');
        //bookmark pos by order,: 20,23,33,22
        OpenFilePos(s, x, y, BookMarks, bkPos, False);
        //ShowMessage(s);
        Dec(I);
      End;
    Finally
      Free;
    End;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -