| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Editor.h"
- #include "WinInterface.h"
- #include "TextsWin.h"
- #include "Tools.h"
- #include <Common.h>
- #include <ScpMain.h>
- #include "VCLCommon.h"
- #include "WinConfiguration.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- //---------------------------------------------------------------------------
- void __fastcall DoEditorForm(const AnsiString FileName, TCustomForm * ParentForm,
- TNotifyEvent OnFileChanged, const AnsiString Caption)
- {
- TEditorForm * Dialog = new TEditorForm(Application);
- try
- {
- Dialog->FileName = FileName;
- Dialog->ParentForm = ParentForm;
- Dialog->Caption = Caption.IsEmpty() ? FileName : Caption;
- Dialog->OnFileChanged = OnFileChanged;
- Dialog->Execute();
- }
- __finally
- {
- delete Dialog;
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TEditorForm::TEditorForm(TComponent* Owner)
- : TForm(Owner)
- {
- FParentForm = NULL;
- FCaretPos.x = -1;
- FCaretPos.y = -1;
- FLastFindDialog = NULL;
- ApplyConfiguration();
- FindDialog->FindText = WinConfiguration->Editor.FindText;
- TFindOptions Options = FindDialog->Options;
- if (WinConfiguration->Editor.FindMatchCase)
- {
- Options << frMatchCase;
- }
- if (WinConfiguration->Editor.FindWholeWord)
- {
- Options << frWholeWord;
- }
- FindDialog->Options = Options;
- ReplaceDialog->FindText = FindDialog->FindText;
- ReplaceDialog->Options = FindDialog->Options;
- ReplaceDialog->ReplaceText = WinConfiguration->Editor.ReplaceText;
- UseSystemFont(this);
- }
- //---------------------------------------------------------------------------
- __fastcall TEditorForm::~TEditorForm()
- {
- if (FLastFindDialog)
- {
- WinConfiguration->Editor.FindText = FLastFindDialog->FindText;
- WinConfiguration->Editor.ReplaceText = ReplaceDialog->ReplaceText;
- WinConfiguration->Editor.FindMatchCase = FLastFindDialog->Options.Contains(frMatchCase);
- WinConfiguration->Editor.FindWholeWord = FLastFindDialog->Options.Contains(frWholeWord);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TEditorForm::Execute()
- {
- ShowModal();
- return true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::SetFileName(const AnsiString value)
- {
- if (value != FFileName)
- {
- FFileName = value;
- if (Visible)
- {
- LoadFile();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::SetParentForm(TCustomForm * value)
- {
- if (FParentForm != value)
- {
- FParentForm = value;
- if (value)
- {
- BoundsRect = value->BoundsRect;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::EditorActionsUpdate(TBasicAction *Action,
- bool &Handled)
- {
- Handled = true;
- if (Action == SaveAction)
- {
- SaveAction->Enabled = EditorMemo->Modified;
- }
- else if (Action == FindNextAction)
- {
- FindNextAction->Enabled =
- FLastFindDialog != NULL || !FindDialog->FindText.IsEmpty();
- }
- else if (Action == PreferencesAction || Action == CloseAction ||
- Action == FindAction || Action == ReplaceAction || Action == GoToLineAction)
- {
- ((TAction *)Action)->Enabled = true;
- }
- else
- {
- Handled = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::EditorActionsExecute(TBasicAction *Action,
- bool &Handled)
- {
- Handled = true;
- if (Action == SaveAction)
- {
- assert(!FFileName.IsEmpty());
- EditorMemo->Lines->SaveToFile(FFileName);
- if (FOnFileChanged)
- {
- FOnFileChanged(this);
- }
- EditorMemo->Modified = false;
- UpdateControls();
- }
- else if (Action == PreferencesAction)
- {
- if (DoPreferencesDialog(pmEditor))
- {
- ApplyConfiguration();
- }
- }
- else if (Action == CloseAction)
- {
- Close();
- }
- else if (Action == FindAction || Action == ReplaceAction)
- {
- StartFind(Action == FindAction);
- }
- else if (Action == FindNextAction)
- {
- if (!FLastFindDialog)
- {
- FLastFindDialog = FindDialog;
- }
- Find();
- }
- else if (Action == GoToLineAction)
- {
- GoToLine();
- }
- else if (Action == EditPaste)
- {
- // original source: http://home.att.net/~robertdunn/FAQs/Faqs.html
- // tell the Rich Edit control to insert unformatted text (CF_TEXT)
- REPASTESPECIAL RepasteSpecial = { 0, 0 };
- SendMessage(EditorMemo->Handle, EM_PASTESPECIAL, CF_TEXT,
- (LPARAM)&RepasteSpecial);
- }
- else
- {
- Handled = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::FormCloseQuery(TObject * /*Sender*/,
- bool &CanClose)
- {
- if (EditorMemo->Modified)
- {
- int Answer = MessageDialog(LoadStr(SAVE_CHANGES), qtConfirmation,
- qaYes | qaNo | qaCancel);
- CanClose = (Answer != qaCancel);
- if (Answer == qaYes)
- {
- SaveAction->Execute();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::ApplyConfiguration()
- {
- bool PrevModified = EditorMemo->Modified;
- assert(Configuration);
- EditorMemo->Font->Name = WinConfiguration->Editor.FontName;
- EditorMemo->Font->Height = WinConfiguration->Editor.FontHeight;
- EditorMemo->Font->Charset = (TFontCharset)WinConfiguration->Editor.FontCharset;
- EditorMemo->Font->Style = IntToFontStyles(WinConfiguration->Editor.FontStyle);
- EditorMemo->DefAttributes->Assign(EditorMemo->Font);
- if (EditorMemo->WordWrap != WinConfiguration->Editor.WordWrap)
- {
- if (Visible)
- {
- TStrings * Content = new TStringList();
- try
- {
- Content->Assign(EditorMemo->Lines);
- EditorMemo->WordWrap = WinConfiguration->Editor.WordWrap;
- EditorMemo->Lines = Content;
- EditorMemo->CaretPos = TPoint(0, 0);
- }
- __finally
- {
- delete Content;
- }
- }
- else
- {
- EditorMemo->WordWrap = WinConfiguration->Editor.WordWrap;
- }
- }
- EditorMemo->Modified = PrevModified;
- EditorMemo->ClearUndo();
- UpdateControls();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::UpdateControls()
- {
- TPoint ACaretPos = EditorMemo->CaretPos;
- if (ACaretPos.x != FCaretPos.x || ACaretPos.y != FCaretPos.y)
- {
- FCaretPos = ACaretPos;
- int Count = EditorMemo->Lines->Count;
- StatusBar->Panels->Items[0]->Text = FMTLOAD(EDITOR_LINE_STATUS,
- ((int)FCaretPos.y+1, Count));
- StatusBar->Panels->Items[1]->Text = FMTLOAD(EDITOR_COLUMN_STATUS,
- ((int)FCaretPos.x+1));
- AnsiString Character;
- if (FCaretPos.y >= 0 && FCaretPos.y < EditorMemo->Lines->Count)
- {
- AnsiString Line = EditorMemo->Lines->Strings[FCaretPos.y];
- if (FCaretPos.x+1 <= Line.Length())
- {
- Character = FMTLOAD(EDITOR_CHARACTER_STATUS,
- (int((unsigned char)Line[FCaretPos.x+1]), int((unsigned char)Line[FCaretPos.x+1])));
- }
- }
- StatusBar->Panels->Items[2]->Text = Character;
- }
- StatusBar->Panels->Items[3]->Text =
- (EditorMemo->Modified ? LoadStr(EDITOR_MODIFIED) : AnsiString(""));
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::EditorMemoMouseUp(TObject * /*Sender*/,
- TMouseButton /*Button*/, TShiftState /*Shift*/, int /*X*/, int /*Y*/)
- {
- UpdateControls();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::EditorMemoKeyUp(TObject * /*Sender*/,
- WORD & /*Key*/, TShiftState /*Shift*/)
- {
- UpdateControls();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::EditorMemoChange(TObject * /*Sender*/)
- {
- UpdateControls();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::FindDialogFind(TObject * /*Sender*/)
- {
- Find();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::Find()
- {
- int NewPos;
- int Replacements = 0;
- do
- {
- assert(FLastFindDialog);
- TSearchTypes SearchTypes;
- // length condition is there to improve performance when large
- // block is selected in editor
- if (FLastFindDialog == ReplaceDialog &&
- (ReplaceDialog->Options.Contains(frReplace) ||
- ReplaceDialog->Options.Contains(frReplaceAll)) &&
- ReplaceDialog->FindText.Length() == EditorMemo->SelLength &&
- AnsiSameText(ReplaceDialog->FindText, EditorMemo->SelText))
- {
- EditorMemo->SelText = ReplaceDialog->ReplaceText;
- Replacements++;
- }
- if (FLastFindDialog->Options.Contains(frMatchCase))
- {
- SearchTypes << stMatchCase;
- }
- if (FLastFindDialog->Options.Contains(frWholeWord))
- {
- SearchTypes << stWholeWord;
- }
- NewPos = EditorMemo->FindText(FLastFindDialog->FindText,
- EditorMemo->SelLength ? EditorMemo->SelStart+1 : EditorMemo->SelStart,
- EditorMemo->Text.Length(), SearchTypes);
- if (NewPos >= 0)
- {
- EditorMemo->SelStart = NewPos;
- EditorMemo->SelLength = FLastFindDialog->FindText.Length();
- }
- if (FLastFindDialog->Handle)
- {
- PositionFindDialog(true);
- }
- if (NewPos < 0)
- {
- if (!Replacements)
- {
- MessageDialog(FMTLOAD(EDITOR_FIND_END, (FLastFindDialog->FindText)), qtInformation, qaOK, 0);
- }
- else
- {
- MessageDialog(FMTLOAD(EDITOR_REPLACE_END, (Replacements)), qtInformation, qaOK, 0);
- }
- }
- }
- while (NewPos >= 0 && FLastFindDialog == ReplaceDialog &&
- ReplaceDialog->Options.Contains(frReplaceAll));
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::FormShow(TObject * /*Sender*/)
- {
- LoadFile();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::LoadFile()
- {
- EditorMemo->Lines->LoadFromFile(FFileName);
- EditorMemo->Modified = false;
- FCaretPos.x = -1;
- ApplyConfiguration();
- }
- //---------------------------------------------------------------------------
- bool __fastcall TEditorForm::CursorInUpperPart()
- {
- HFONT OldFont;
- void *DC;
- TTextMetric TM;
- TRect Rect;
- DC = GetDC(EditorMemo->Handle);
- OldFont = SelectObject(DC, EditorMemo->Font->Handle);
- try
- {
- GetTextMetrics(DC, &TM);
- EditorMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
- }
- __finally
- {
- SelectObject(DC, OldFont);
- ReleaseDC(EditorMemo->Handle, DC);
- }
- int VisibleLines = (Rect.Bottom - Rect.Top) / (TM.tmHeight + TM.tmExternalLeading);
- int FirstLine = SendMessage(EditorMemo->Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
- TPoint CaretPos = EditorMemo->CaretPos;
- return (CaretPos.y - FirstLine) < VisibleLines / 2;
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::PositionFindDialog(bool VerticalOnly)
- {
- assert(FLastFindDialog);
- if (!VerticalOnly)
- {
- FLastFindDialog->Left = EditorMemo->Left + EditorMemo->Width / 2 - 100;
- }
- FLastFindDialog->Top = EditorMemo->Top + (EditorMemo->Height / 4) +
- (CursorInUpperPart() ? (EditorMemo->Height / 2) : 0) - 30;
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::StartFind(bool Find)
- {
- AnsiString Text = EditorMemo->SelText;
- TFindOptions Options;
- if (FLastFindDialog)
- {
- Options = FLastFindDialog->Options;
- if (Text.IsEmpty())
- {
- Text = FLastFindDialog->FindText;
- }
- }
- TFindDialog * Dialog = Find ? FindDialog : ReplaceDialog;
- if (FLastFindDialog && Dialog != FLastFindDialog && FLastFindDialog->Handle)
- {
- FLastFindDialog->CloseDialog();
- }
- FLastFindDialog = Dialog;
- if (!Text.IsEmpty())
- {
- FLastFindDialog->FindText = Text;
- }
- if (!Options.Empty())
- {
- FLastFindDialog->Options = Options;
- }
- if (!FLastFindDialog->Handle)
- {
- PositionFindDialog(false);
- }
- FLastFindDialog->Execute();
- }
- //---------------------------------------------------------------------------
- void __fastcall TEditorForm::GoToLine()
- {
- AnsiString Str;
- if (InputDialog(LoadStr(EDITOR_GO_TO_LINE), LoadStr(EDITOR_LINE_NUMBER), Str))
- {
- int Line = StrToIntDef(Str, -1);
- if (Line <= 0 || Line > EditorMemo->Lines->Count)
- {
- throw Exception(LoadStr(EDITOR_INVALID_LINE));
- }
- else
- {
- EditorMemo->CaretPos = TPoint(0, Line-1);
- }
- }
- }
- //---------------------------------------------------------------------------
|