//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "lebcb_1a.h"
#include "lebcb_1b.h"
#include "lebcb_1c.h"
#include "lebcb_1d.h"
#include "lebcb_1e.h"
#include "lebcb_1f.h"
#include "lebcb_1z.h"
//---------------------------------------------------------------------------
#pragma link "LEDlp32"
#pragma resource "*.dfm"
TLEditForm *LEditForm;
//---------------------------------------------------------------------------
__fastcall TLEditForm::TLEditForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::LEdit1Change(TObject *Sender)
{
  // Set the status bar counts
  TotalLineCount->Caption = IntToStr(LEdit1->LineCount);
  LEdit1->GetSelection();
  LineNumber->Caption = IntToStr(LEdit1->EndLine);
  ColNumber->Caption = IntToStr(LEdit1->EndPos);
  UpdateMenuItems(true);
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::UpdateMenuItems(bool Value)
{
  SaveMenuItem->Enabled = Value;
  SaveAsMenuItem->Enabled = Value;
  ReloadFileMenuItem->Enabled = Value;
  CutMenuItem->Enabled = Value;
  CopyMenuItem->Enabled = Value;
  PasteMenuItem->Enabled = Value;
  DeleteMenuItem->Enabled = Value;
  SelectAllMenuItem->Enabled = Value;
  ClearMenuItem->Enabled = Value;
  ClearAllMenuItem->Enabled = Value;
  FindMenuItem->Enabled = Value;
  FindNextMenuItem->Enabled = Value;
  FindBraceMenuItem->Enabled = Value;
  ReplaceMenuItem->Enabled = Value;
  GotoLineMenuItem->Enabled = Value;
  SetBookmarkMenuItem->Enabled = Value;
  FindBookmarkMenuItem->Enabled = Value;
  SaveBtn->Enabled = Value;
  SaveAsBtn->Enabled = Value;
  ReloadBtn->Enabled = Value;
  CutBtn->Enabled = Value;
  CopyBtn->Enabled = Value;
  PasteBtn->Enabled = Value;
  FindBtn->Enabled = Value;
  ReplaceBtn->Enabled = Value;
  GotoBtn->Enabled = Value;
  SetBookmarkBtn->Enabled = Value;
  FindBookmarkBtn->Enabled = Value;
  // Set the undo menu item status
  if (LEdit1->CanUndo) {
    UndoMenuItem->Enabled = true;
    UndoBtn->Enabled = true;
  }
  else {
    UndoMenuItem->Enabled = false;
    UndoBtn->Enabled = false;
  }
  // Set the redo menu item status
  if (LEdit1->CanRedo) {
    RedoMenuItem->Enabled = true;
    RedoBtn->Enabled = true;
  }
  else {
    RedoMenuItem->Enabled = false;
    RedoBtn->Enabled = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ExitMenuItemClick(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::NewMenuItemClick(TObject *Sender)
{
  // Start a new file in LEdit
  LEdit1->NewFile();
  LEdit1->FileName = "";
  LEditForm->Caption = "";
  UpdateMenuItems(false);
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::OpenMenuItemClick(TObject *Sender)
{
  // Load a file into LEdit
  if (OpenDialog1->Execute()) {
    LEdit1->LoadFromFile(OpenDialog1->FileName);
    LEdit1->FileName = OpenDialog1->FileName;
    LEditForm->Caption = OpenDialog1->FileName;
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SaveMenuItemClick(TObject *Sender)
{
  // Save the current LEdit contents to a file
  LEdit1->SaveFile();
  LEditForm->Caption = LEdit1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SaveAsMenuItemClick(TObject *Sender)
{
  // Save the current LEdit contents to a new file
  LEdit1->SaveFileAs();
  LEditForm->Caption = LEdit1->FileName;
  /* The alternate method below can also be used if you do not
    want to use the built-in dialogs */
  /* if (SaveDialog1->Execute()) {
    LEdit1->SaveToFile(SaveDialog1->Filename);
    LEditForm->Caption = SaveDialog1->Filename;
  } */
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ReloadFileMenuItemClick(TObject *Sender)
{
  // Reload the current file into LEdit
  LEdit1->ReloadFile();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::PrinterSetupMenuItemClick(TObject *Sender)
{
  LEdit1->PrinterSetup();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::PrintMenuItemClick(TObject *Sender)
{
  LEdit1->Print();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::UndoMenuItemClick(TObject *Sender)
{
  // Undo the last action
  LEdit1->Undo();
  if (LEdit1->CanRedo) {
    RedoMenuItem->Enabled = true;
    RedoBtn->Enabled = true;
  }
  else {
    RedoMenuItem->Enabled = false;
    RedoBtn->Enabled = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::RedoMenuItemClick(TObject *Sender)
{
  // Redo the last undo
  LEdit1->Redo();
  if (LEdit1->CanUndo) {
    UndoMenuItem->Enabled = true;
    UndoBtn->Enabled = true;
  }
  else {
    UndoMenuItem->Enabled = false;
    UndoBtn->Enabled = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ClearUndoMenuItemClick(TObject *Sender)
{
  // Clear the undo buffer
  LEdit1->EmptyUndoBuffer();
  // Set the undo menu item status
  if (LEdit1->CanUndo) {
    UndoMenuItem->Enabled = true;
    UndoBtn->Enabled = true;
  }
  else {
    UndoMenuItem->Enabled = false;
    UndoBtn->Enabled = false;
  }
  // Set the redo menu item status
  if (LEdit1->CanRedo) {
    RedoMenuItem->Enabled = true;
    RedoBtn->Enabled = true;
  }
  else {
    RedoMenuItem->Enabled = false;
    RedoBtn->Enabled = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::CutMenuItemClick(TObject *Sender)
{
  // Cut the selection and place it on the windows clipboard
  LEdit1->CutToClipboard();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::CopyMenuItemClick(TObject *Sender)
{
  // Copy the selection to the windows clipboard but do not cut it
  LEdit1->CopyToClipboard();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::PasteMenuItemClick(TObject *Sender)
{
  // Paste the contents of the windows clipboard into LEdit
  LEdit1->PasteFromClipboard();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::DeleteMenuItemClick(TObject *Sender)
{
  // Delete the selected text
  LEdit1->ClearSelection();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SelectAllMenuItemClick(TObject *Sender)
{
  // Select all the text in LEdit
  LEdit1->SelectAll();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ClearMenuItemClick(TObject *Sender)
{
  // Clear the selected text from LEdit
  LEdit1->ClearSelection();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ClearAllMenuItemClick(TObject *Sender)
{
  // Clear all the text from LEdit
  LEdit1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FindMenuItemClick(TObject *Sender)
{
  // Show the Find dialog to perform a search
  LEdit1->ShowFindDialog();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FindNextMenuItemClick(TObject *Sender)
{
  // Find the next occurance of the search
  LEdit1->FindNext();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FindBraceMenuItemClick(TObject *Sender)
{
  // Find the next brace
  LEdit1->FindBrace();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ReplaceMenuItemClick(TObject *Sender)
{
  // Show the Replace dialog to replace text searched for
  LEdit1->ShowReplaceDialog();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::GotoLineMenuItemClick(TObject *Sender)
{
  // Show the Goto Line dialog to go to a specific line
  LEdit1->ShowGotoLineDialog();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ForeColorMenuItemClick(TObject *Sender)
{
  // Display a color dialog and set the foreground (text) color of LEdit
  if (ColorDialog->Execute())
    LEdit1->Font->Color = ColorDialog->Color;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::BackgroundColor1Click(TObject *Sender)
{
  // Display a color dialog and set the background color of LEdit
  if (ColorDialog->Execute())
    LEdit1->Color = ColorDialog->Color;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SelectForeColorMenuItemClick(TObject *Sender)
{
  // Display a color dialog and set the selection foreground color of LEdit
  if (ColorDialog->Execute())
    LEdit1->ForeColorSelected = ColorDialog->Color;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SelectBackColorMenuItemClick(TObject *Sender)
{
  // Display a color dialog and set the selection background color of LEdit
  if (ColorDialog->Execute())
    LEdit1->BackColorSelected = ColorDialog->Color;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::HeaderFooter1Click(TObject *Sender)
{
  HeaderFooterForm->Edit1->Text = FHeader;
  HeaderFooterForm->Edit2->Text = FFooter;
  HeaderFooterForm->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FormCreate(TObject *Sender)
{
  // Set up the application
  Application->OnHint = DisplayHint;
  UpdateMenuItems(false);
  Application->HintColor = clAqua;
  Application->HintPause = 0;
  FHeader = "";
  FFooter = "";
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FormClose(TObject *Sender, TCloseAction &Action)
{
  if (LEdit1->Modified) {
    if (MessageDlg("The text has been modified.  Save?", mtInformation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
          LEdit1->SaveFile();
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FormShow(TObject *Sender)
{
  if (ParamStr(1) != "") {
    LEdit1->LoadFromFile(ParamStr(1));
    LEditForm->Caption = ParamStr(1);
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FontMenuItemClick(TObject *Sender)
{
  // Show the Font dialog to set the font
  LEdit1->ShowFontDialog();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::ReadOnlyMenuItemClick(TObject *Sender)
{
  // Toggle LEdit to Read only or not Read Only mode
  ReadOnlyMenuItem->Checked = !(ReadOnlyMenuItem->Checked);
  LEdit1->ReadOnly = !(LEdit1->ReadOnly);
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::CaretWidthMenuItemClick(TObject *Sender)
{
  CaretWidthForm->CaretWidthEdit->Text = IntToStr(LEdit1->CaretWidth);
  if (CaretWidthForm->ShowModal() == mrOk)
    LEdit1->CaretWidth = FCaretWidth;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::InsertModeMenuItemClick(TObject *Sender)
{
  // Toggle LEdit to Insert or Overwrite mode
  InsertModeMenuItem->Checked = !(InsertModeMenuItem->Checked);
  LEdit1->InsertMode = !(LEdit1->InsertMode);
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::WordWrapMenuItemClick(TObject *Sender)
{
  long SL;
  long EL;
  int SP;
  int EP;

  // Get the current location
  LEdit1->GetSelection();
  SL = LEdit1->StartLine;
  EL = LEdit1->EndLine;
  SP = LEdit1->StartPos;
  EP = LEdit1->EndPos;
  // Toggle LEdit to Word Wrap mode
  WordWrapMenuItem->Checked = !(WordWrapMenuItem->Checked);
  LEdit1->WordWrap = !(LEdit1->WordWrap);
  // Set it back to the same position
  LEdit1->StartLine = SL;
  LEdit1->EndLine = EL;
  LEdit1->StartPos = SP;
  LEdit1->EndPos = EP;
  LEdit1->SetSelection();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::UnixStyleSaveMenuItemClick(TObject *Sender)
{
  // Turn off Mac Style Save if it is on
  if (MacStyleSaveMenuItem->Checked) {
    MacStyleSaveMenuItem->Checked = !(MacStyleSaveMenuItem->Checked);
    LEdit1->MacStyleSave = MacStyleSaveMenuItem->Checked;
  }
  // Toggle LEdit to Unix Style Save on or off
  UnixStyleSaveMenuItem->Checked = !(UnixStyleSaveMenuItem->Checked);
  LEdit1->UnixStyleSave = UnixStyleSaveMenuItem->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::MacStyleSaveMenuItemClick(TObject *Sender)
{
  // Turn off Unix Style Save if it is on
  if (UnixStyleSaveMenuItem->Checked) {
    UnixStyleSaveMenuItem->Checked = !(UnixStyleSaveMenuItem->Checked);
    LEdit1->UnixStyleSave = UnixStyleSaveMenuItem->Checked;
  }
  // Toggle LEdit to Mac Style Save on or off
  MacStyleSaveMenuItem->Checked = !(MacStyleSaveMenuItem->Checked);
  LEdit1->MacStyleSave = MacStyleSaveMenuItem->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::WWWidthMenuItemClick(TObject *Sender)
{
  WWWidthForm->WWWidthEdit->Text = IntToStr(LEdit1->WordWrapWidth);
  if (WWWidthForm->ShowModal() == mrOk)
    LEdit1->WordWrapWidth = WWWidth;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::WWRMarginMenuItemClick(TObject *Sender)
{
  WWRMarginForm->WWRMarginEdit->Text = IntToStr(LEdit1->WordWrapRMargin);
  if (WWRMarginForm->ShowModal() == mrOk)
    LEdit1->WordWrapRMargin = WWRMargin;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SetBookmarkMenuItemClick(TObject *Sender)
{
  // Set a bookmark
  LEdit1->SetBookmark();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::SetBookmarkNumberMenuItemClick(TObject *Sender)
{
  BookmarkNumberForm->BookmarkNumberEdit->Text = IntToStr(LEdit1->BookmarkNumber);
  if (BookmarkNumberForm->ShowModal() == mrOk) {
    LEdit1->BookmarkNumber = FBmNum;
    // Set a bookmark
    LEdit1->SetBookmark();
  }
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::FindBookmarkMenuItemClick(TObject *Sender)
{
  // Go to the bookmark
  LEdit1->FindBookmark();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::AboutMenuItemClick(TObject *Sender)
{
  // Show about box
  LEExAboutBox->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::LEdit1PrintPage(TObject *Sender, TCanvas *PrintDC,
	WORD VerticalSize, WORD HorizontalSize, WORD &HeaderSize, WORD &FooterSize)
{
  TRect HRect;
  TRect FRect;

  // Set Header Rectangle
  HRect.Left = 0;
  HRect.Right = HorizontalSize;
  HRect.Top = 10;
  HRect.Bottom = HRect.Top + 20;
  // Set Footer Rectangle
  FRect.Left = 0;
  FRect.Right = HorizontalSize;
  FRect.Top = VerticalSize - 50;
  FRect.Bottom = FRect.Top + 20;
  // Write the Header
  PrintDC->TextRect(HRect, HRect.Left + 5, HRect.Top + 2, FHeader);
  // Write the Footer
  PrintDC->TextRect(FRect, FRect.Left + 5, FRect.Top + 2, FFooter);
  // Set the return values
  HeaderSize = 40;
  FooterSize = 50;
}
//---------------------------------------------------------------------------
void __fastcall TLEditForm::DisplayHint(TObject *Sender)
{
  StatusLine->Caption = Application->Hint;
}
//---------------------------------------------------------------------------
