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

📄 editu2.pas

📁 一个mwEdit控件原码,比mwCuuEdit0.92a功能先进.
💻 PAS
📖 第 1 页 / 共 4 页
字号:
  HasHighlighter := FALSE;
  for i := 0 to fHighlighters.Count - 1 do begin
    if cbxHighlighterSelect.Text = fHighlighters[i] then begin
      SynEditor.Highlighter := fHighlighters.Objects[i] as TSynCustomHighlighter;
      HasHighlighter := TRUE;
      break;
    end;
  end;
  cbxExporterSelect.Enabled := HasHighlighter;
  UpdateCanExport;
  StatusBar.Panels[3].Text := '';
  // highlighter user settings
  cbxSettingsSelect.Items.Clear;
  if HasHighlighter and (hcUserSettings in SynEditor.Highlighter.Capabilities) then
    SynEditor.Highlighter.EnumUserSettings(cbxSettingsSelect.Items);
  cbxSettingsSelect.Enabled := cbxSettingsSelect.Items.Count > 0;

  btnSaveToReg.Enabled := HasHighlighter
    and (hcRegistry in SynEditor.Highlighter.Capabilities);
  btnLoadFromReg.Enabled := btnSaveToReg.Enabled;

  cbxAttrSelect.Enabled := HasHighlighter;
  cbxAttrForeground.Enabled := HasHighlighter;
  cbxAttrBackground.Enabled := HasHighlighter;
  grbAttrStyle.Enabled := HasHighlighter;
  if SynEditor.Highlighter is TSynGeneralSyn then begin
    grbAttrComments.Enabled := TRUE;
    with SynEditor.Highlighter as TSynGeneralSyn do begin
      cbCommentsAnsi.Checked := csAnsiStyle in Comments;
      cbCommentsPas.Checked := csPasStyle in Comments;
      cbCommentsC.Checked := csCStyle in Comments;
      cbCommentsAsm.Checked := csAsmStyle in Comments;
      cbCommentsBas.Checked := csBasStyle in Comments;
    end;
  end else
    grbAttrComments.Enabled := FALSE;
  if SynEditor.Highlighter is TSynPythonSyn then
    SynEditPythonBehaviour1.Editor := SynEditor
  else
    SynEditPythonBehaviour1.Editor := nil;
  btnKeywords.Enabled := grbAttrComments.Enabled;
  ReloadAttributes;

  SynEditor.SetFocus;
end;

procedure TDemoMainForm.cbxAttrSelectChange(Sender: TObject);
var
  Attr: TSynHighlighterAttributes;
begin
  Attr := TSynHighlighterAttributes.Create('');
  try
    if SynEditor.Highlighter <> nil then
      Attr.Assign(SynEditor.Highlighter.Attribute[cbxAttrSelect.ItemIndex]);
    cbxAttrForeground.ItemIndex := ColorToIndex(Attr.Foreground);
    cbxAttrBackground.ItemIndex := ColorToIndex(Attr.Background);
    cbStyleBold.Checked := (fsBold in Attr.Style);
    cbStyleItalic.Checked := (fsItalic in Attr.Style);
    cbStyleUnderLine.Checked := (fsUnderline in Attr.Style);
    cbStyleStrikeOut.Checked := (fsStrikeOut in Attr.Style);
  finally
    Attr.Free;
  end;
end;

procedure TDemoMainForm.cbxSettingsSelectChange(Sender: TObject);
begin
  ShowSuccess(SynEditor.Highlighter.UseUserSettings(cbxSettingsSelect.ItemIndex));
  ReloadAttributes;
  SynEditor.SetFocus;
end;

procedure TDemoMainForm.cbxAttrForegroundChange(Sender: TObject);
begin
  if SynEditor.Highlighter <> nil then begin
    SynEditor.Highlighter.Attribute[cbxAttrSelect.ItemIndex].Foreground :=
      IndexToColor(cbxAttrForeground.ItemIndex);
  end;
end;

procedure TDemoMainForm.cbxAttrBackgroundChange(Sender: TObject);
begin
  if SynEditor.Highlighter <> nil then begin
    SynEditor.Highlighter.Attribute[cbxAttrSelect.ItemIndex].Background :=
      IndexToColor(cbxAttrBackground.ItemIndex);
  end;
end;

procedure TDemoMainForm.cbFontStyleClick(Sender: TObject);
var
  Style: TFontStyles;
begin
  if SynEditor.Highlighter <> nil then begin
    Style := [];
    if cbStyleBold.Checked then
      Include(Style, fsBold);
    if cbStyleItalic.Checked then
      Include(Style, fsItalic);
    if cbStyleUnderLine.Checked then
      Include(Style, fsUnderline);
    if cbStyleStrikeOut.Checked then
      Include(Style, fsStrikeOut);
    SynEditor.Highlighter.Attribute[cbxAttrSelect.ItemIndex].Style := Style;
  end;
end;

const
  csRegKeyRoot = 'Software\SynEdit\Highlighters\';

procedure TDemoMainForm.btnSaveToRegClick(Sender: TObject);
begin
  ShowSuccess(SynEditor.Highlighter.SaveToRegistry(HKEY_CURRENT_USER,
    csRegKeyRoot + cbxHighlighterSelect.Text));
end;

procedure TDemoMainForm.btnLoadFromRegClick(Sender: TObject);
begin
  if SynEditor.Highlighter.LoadFromRegistry(HKEY_CURRENT_USER,
    csRegKeyRoot + cbxHighlighterSelect.Text)
  then begin
    ShowSuccess(TRUE);
    cbxAttrSelectChange(Self);
  end else
    ShowSuccess(FALSE);
end;

procedure TDemoMainForm.btnKeywordsClick(Sender: TObject);
var
  Highlighter: TSynGeneralSyn;
begin
  Highlighter := SynEditor.HighLighter as TSynGeneralSyn;

  Form2 := TForm2.Create(Self);
  try
    Form2.lbKeywords.Items.Assign(Highlighter.Keywords);
    if Form2.ShowModal = mrOk then
      Highlighter.Keywords := Form2.lbKeywords.Items;
  finally
    Form2.Free;
    Form2 := nil;
  end;
end;

procedure TDemoMainForm.cbCommentsClick(Sender: TObject);
var
  CmntSet: TCommentStyles;
begin
  CmntSet := [];
  if cbCommentsAnsi.Checked then
    Include(CmntSet, csAnsiStyle);
  if cbCommentsPas.Checked then
    Include(CmntSet, csPasStyle);
  if cbCommentsC.Checked then
    Include(CmntSet, csCStyle);
  if cbCommentsAsm.Checked then
    Include(CmntSet, csAsmStyle);
  if cbCommentsBas.Checked then
    Include(CmntSet, csBasStyle);
  (SynEditor.Highlighter as TSynGeneralSyn).Comments := CmntSet;
end;

procedure TDemoMainForm.btnEditClick(Sender: TObject);
var
  Dlg: TSynEditKeystrokesEditorForm;
begin
  Dlg := TSynEditKeystrokesEditorForm.Create(Self);
  try
    Dlg.Caption := 'SynEdit Demo Keystroke Editor';
    Dlg.Keystrokes := SynEditor.Keystrokes;
    if Dlg.ShowModal = mrOk then begin
      SynEditor.Keystrokes := Dlg.Keystrokes;
      UpdateKeystrokesList;
    end;
  finally
    Dlg.Free;
  end;
end;

procedure TDemoMainForm.SpeedButtonClick(Sender: TObject);
var
  p: TBufferCoord;
  Mark: TSynEditMark;
begin
  if not fDisableMarkButtons then with SynEditor do begin
    p := CaretXY;
    Marks.ClearLine(p.Line);
    if (Sender as TSpeedButton).Down then begin
      Mark := TSynEditMark.Create(SynEditor);
      with Mark do begin
        Line := p.Line;
        Char := p.Char;
        ImageIndex := (Sender as TSpeedButton).Tag;
        Visible := TRUE;
        InternalImage := BookMarkOptions.BookMarkImages = nil;
      end;
      Marks.Place(Mark);
    end;
  end;
end;

procedure TDemoMainForm.btnSearchClick(Sender: TObject);
begin
  FindDialog1.Execute;
  btnSearchNext.Enabled := TRUE;
  btnSearchPrev.Enabled := TRUE;
end;

procedure TDemoMainForm.btnSearchNextPrevClick(Sender: TObject);
begin
  if (Sender = btnSearchNext) then
    FindDialog1.Options := FindDialog1.Options + [frDown]
  else if (Sender = btnSearchPrev) then
    FindDialog1.Options := FindDialog1.Options - [frDown];
  DoFindText(Sender);
  SynEditor.SetFocus;
end;

procedure TDemoMainForm.DoFindText(Sender: TObject);
var
  rOptions: TSynSearchOptions;
  dlg: TFindDialog;
  sSearch: string;
begin
  if Sender = ReplaceDialog1 then
    dlg := ReplaceDialog1
  else
    dlg := FindDialog1;
  sSearch := dlg.FindText;
  if Length(sSearch) = 0 then begin
    Beep;
    lblSearchResult.Caption := 'Can''t search for empty text!';
    lblSearchResult.Visible := TRUE;
  end else begin
    rOptions := [];
    if not (frDown in dlg.Options) then
      Include(rOptions, ssoBackwards);
    if frMatchCase in dlg.Options then
      Include(rOptions, ssoMatchCase);
    if frWholeWord in dlg.Options then
      Include(rOptions, ssoWholeWord);
    if SynEditor.SearchReplace(sSearch, '', rOptions) = 0 then begin
      Beep;
      lblSearchResult.Caption := 'SearchText ''' + sSearch + ''' not found!';
      lblSearchResult.Visible := TRUE;
    end else
      lblSearchResult.Visible := FALSE;
  end;
end;

procedure TDemoMainForm.DoReplaceText(Sender: TObject);
var
  rOptions: TSynSearchOptions;
  sSearch: string;
begin
  sSearch := ReplaceDialog1.FindText;
  if Length(sSearch) = 0 then begin
    Beep;
    lblSearchResult.Caption := 'Can''t replace an empty text!';
    lblSearchResult.Visible := TRUE;
  end else begin
    rOptions := [ssoReplace];
    if frMatchCase in ReplaceDialog1.Options then
      Include(rOptions, ssoMatchCase);
    if frWholeWord in ReplaceDialog1.Options then
      Include(rOptions, ssoWholeWord);
    if frReplaceAll in ReplaceDialog1.Options then
      Include(rOptions, ssoReplaceAll);
    if SynEditor.SearchReplace(sSearch, ReplaceDialog1.ReplaceText, rOptions) = 0
    then begin
      Beep;
      lblSearchResult.Caption := 'SearchText ''' + sSearch +
        ''' could not be replaced!';
      lblSearchResult.Visible := TRUE;
    end else
      lblSearchResult.Visible := FALSE;
  end;
end;

procedure TDemoMainForm.btnReplaceClick(Sender: TObject);
begin
  ReplaceDialog1.Execute;
end;

procedure TDemoMainForm.btnExportToFileClick(Sender: TObject);
var
  Exporter: TSynCustomExporter;
begin
  Exporter := GetSelectedExporter;
  // can't export to file in several formats at the same time...
  if Assigned(Exporter) then
    with SaveDialog1 do begin
      Filter := Exporter.DefaultFilter;
      if Execute then with Exporter do begin
        ExportAsText := TRUE;
        Highlighter := SynEditor.Highlighter;
        if fFileName <> '' then
          Title := '"' + fFileName + '" exported as ' + FormatName;
        if cbExportSelected.Checked and SynEditor.SelAvail then
          ExportRange(SynEditor.Lines, SynEditor.BlockBegin, SynEditor.BlockEnd)
        else
          ExportAll(SynEditor.Lines);
        SaveToFile(FileName);
      end;
    end;
  SynEditor.SetFocus;
end;

procedure TDemoMainForm.btnExportToClipboardClick(Sender: TObject);
var
  Exporter: TSynCustomExporter;
  SelectedOnly: boolean;
  i: integer;

  procedure DoExport;
  begin
    with Exporter do begin
      ExportAsText := FALSE;
      Highlighter := SynEditor.Highlighter;
      if SelectedOnly then
        ExportRange(SynEditor.Lines, SynEditor.BlockBegin, SynEditor.BlockEnd)
      else
        ExportAll(SynEditor.Lines);
      CopyToClipboard;
      Highlighter := nil;
    end;
  end;

begin
  Clipboard.Open;
  try
    SelectedOnly := cbExportSelected.Checked and SynEditor.SelAvail;
    if SelectedOnly then
      Clipboard.AsText := SynEditor.SelText
    else
      Clipboard.AsText := SynEditor.Lines.Text;
    // is exporter selected?
    Exporter := GetSelectedExporter;
    if Exporter <> nil then
      DoExport
    else for i := 0 to ComponentCount - 1 do begin
      if not (Components[i] is TSynCustomExporter) then
        continue;
      Exporter := TSynCustomExporter(Components[i]);
      DoExport;
    end;
  finally
    Clipboard.Close;
  end;
end;

procedure TDemoMainForm.SynEditorContextHelp (Sender: TObject; word: String);
var
  hlpmsg : array[0..255] of char;
begin
  StrFmt (hlpmsg,'Need help for %s?',[word]);
  with Application do
    MessageBox (hlpmsg,PChar (Title),mb_ok or mb_iconquestion);
end;

procedure TDemoMainForm.cbShrinkListClick(Sender: TObject);
var
  iOptions: TSynCompletionOptions;
begin
  iOptions := SynCompletionProposal1.Options;
  if cbShrinkList.Checked then
    Include( iOptions, scoLimitToMatchedText )
  else
    Exclude( iOptions, scoLimitToMatchedText );
  SynCompletionProposal1.Options := iOptions;
end;

procedure TDemoMainForm.cbCompletionAttrChange(Sender: TObject);
var
  idx : Integer;
begin
  idx := -1;
  case cbCompletionAttr.ItemIndex of
    0 : idx := ColorToIndex(SynCompletionProposal1.ClBackground);
    1 : idx := ColorToIndex(SynCompletionProposal1.Font.Color);
    2 : idx := ColorToIndex(SynCompletionProposal1.ClSelect);
    3 : idx := ColorToIndex(SynCompletionProposal1.ClSelectedText);
  end;
  cbxCompletionColor.ItemIndex := idx;
end;

procedure TDemoMainForm.cbxCompletionColorChange(Sender: TObject);
begin
  case cbCompletionAttr.ItemIndex of
    0 : SynCompletionProposal1.ClBackground := IndexToColor(cbxCompletionColor.ItemIndex);
    1 : SynCompletionProposal1.Font.Color := IndexToColor(cbxCompletionColor.ItemIndex);
    2 : SynCompletionProposal1.ClSelect := IndexToColor(cbxCompletionColor.ItemIndex);
    3 : SynCompletionProposal1.ClSelectedText := IndexToColor(cbxCompletionColor.ItemIndex);
  end;
end;

procedure TDemoMainForm.SynEditorGutterClick(Sender: TObject;
  Button: TMouseButton; X, Y, Line: Integer; Mark: TSynEditMark);
begin
  if cbOther.Checked then
    LogEvent('OnGutterClick');
  SynEditor.CaretY := Line;
  if not assigned(mark) then begin // place first mark
    SpeedButton1.Down := true;
    SpeedButton1.Click;
  end else
  if (not mark.IsBookmark) and (mark.ImageIndex >= SpeedButton1.Tag) then begin
    if mark.ImageIndex = SpeedButton5.Tag then begin // remove mark
      SpeedButton5.Down := false;
      SpeedButton5.Click;
    end else
      mark.ImageIndex := mark.ImageIndex + 1;
  end;
  ResetMarkButtons;

end;

end.

⌨️ 快捷键说明

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