#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "ChildFrm.h"
#include "aboutDlg.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "ecActns"
#pragma link "ecAutoReplace"
#pragma link "ecKeyMap"
#pragma link "ecPopupCtrl"
#pragma link "ecPrint"
#pragma link "ecPropManager"
#pragma link "ecSpell"
#pragma link "ecStylesCombo"
#pragma link "ecSyntAnal"
#pragma link "ecSyntDlg"
#pragma link "ecSyntMemo"
#pragma link "ecSyntTree"
TSyntEditMain *SyntEditMain;
//---------------------------------------------------------------------------
__fastcall TSyntEditMain::TSyntEditMain(TComponent* Owner)
: TForm(Owner)
{
// TControlHack(Splitter1)->OnMouseDown = SplitterMouseDown;
FHyperlinkHlt = True;
#ifdef USE_RX_LIB
FormPlacement = new TFormPlacement(this);
FormPlacement->Name = "FormPlacement";
FormPlacement->IniFileName = ChangeFileExt(Application->ExeName, "->ini");
FormPlacement->IniSection = "MainForm";
MRU = new TMRUManager(this);
MRU->Name = "MRU";
MRU->AccelDelimiter = adSpace;
MRU->Capacity = 9;
MRU->RecentMenu = Reopen1;
MRU->OnClick = MRUClick;
MRU->IniStorage = FormPlacement;
Reopen1->Visible = True;
FormPlacement->RestoreFormPlacement;
#endif
}
//---------------------------------------------------------------------------
void __fastcall TSyntEditMain::LoadTreeImages()
{
// Loading tree images
AnsiString s = ExtractFilePath(Application->ExeName) + "ecTreeImg->bmp";
if( FileExists(s) )
try
{
Graphics::TBitmap *bmp = new Graphics::TBitmap;
try
{
bmp->LoadFromFile(s);
}
catch(...)
{
delete bmp;
return;
}
if(bmp->Empty) return;
TColor c = bmp->Canvas->Pixels[0][0];
delete bmp;
if(TreeImageList->GetInstRes(0, rtBitmap, s, 16, (TLoadResources() << lrFromFile), c))
SyntaxTreeView1->Images = TreeImageList;
}
catch(...)
{
}
}
//---------------------------------------------------------------------------
void __fastcall LoadFile(TLoadableComponent *Comp, const AnsiString AFileName)
{
if(FileExists(AFileName))
try
{
Comp->LoadFromFile(AFileName);
}
catch(...)
{
Application->HandleException(SyntEditMain);
}
}
void __fastcall TSyntEditMain::FormShow(TObject *Sender)
{
DragAcceptFiles(Handle, True);
Caption = AnsiString("EControl syntax") +
#ifdef EC_UNICODE
" (unicode)" +
#endif
" editor";
LoadLanguages();
PropsManager1->LoadProps();
PropsManager1->UpdateAll();
PropsManager2->LoadProps();
LoadFile(SyntaxManager1, ExtractFilePath(Application->ExeName) + "LexLib.LXL");
LoadFile(SyntStyles1, ChangeFileExt(Application->ExeName, ".ecs"));
LoadTreeImages();
// LoadFile(SharedStyles, ChangeFileExt(Application->ExeName, "->ess"));
AnsiString s = ChangeFileExt(Application->ExeName, ".ari");
if(FileExists(s))
SyntAutoReplace1->Items->LoadFromFile(s);
FReadOnly = (ParamCount() > 1) && SameText(ParamStr(2), "r");
if(FReadOnly) FileExitAction->ShortCut = VK_ESCAPE;
ecDictionary1->LoadCfg(ChangeFileExt(Application->ExeName, ".ini"));
if(ParamCount() > 0) OpenFile(ParamStr(1));
else CreateFrame("");
}
//---------------------------------------------------------------------------
void __fastcall TSyntEditMain::FormClose(TObject *Sender, TCloseAction &Action)
{
// CloseAll(False);
CurrentEditor = 0;
#ifdef USE_RX_LIB
FormPlacement->SaveFormPlacement();
#endif
PropsManager1->SaveProps();
PropsManager2->SaveProps();
SyntStyles1->SaveToFile(ChangeFileExt(Application->ExeName, ".ecs"));
// SharedStyles->SaveToFile(ChangeFileExt(Application->ExeName, "->ess"));
SyntAutoReplace1->Items->SaveToFile(ChangeFileExt(Application->ExeName, ".ari"));
ecDictionary1->SaveCfg(ChangeFileExt(Application->ExeName, ".ini"));
}
//---------------------------------------------------------------------------
TEditorFrame* __fastcall TSyntEditMain::OpenFile(const AnsiString AFileName)
{
// Check, if the file is opened
for(int i = 0; i < FrameCount; i++)
if(Frames[i]->IsTheFile(AFileName))
{
CurrentFrame = Frames[i];
return Frames[i];
}
// Create new frame and load file
TEditorFrame* Result;
if(FrameCount == 1 && Frames[0]->IsNewFile() && !Frames[0]->Modified)
Result = Frames[0];
else
Result = CreateFrame(AFileName);
Result->LoadFile(AFileName);
Result->TextSource->SyntaxAnalyzer = SyntaxManager1->AnalyzerForFile(AFileName);
SyntaxManager1->CurrentLexer = Result->TextSource->SyntaxAnalyzer;
FrameChanged();
// Result->ecSpellChecker1->Active = True;
if( !Result->EditorMaster->Focused())
Result->EditorMaster->SetFocus();
#ifdef USE_RX_LIB
MRU->Add(AFileName, 0);
#endif
return Result;
}
//---------------------------------------------------------------------------
// Save specified frame
void __fastcall TSyntEditMain::SaveFrame(TEditorFrame* Frame, bool PromtDialog)
{
if(Frame == 0) return;
if(!PromtDialog)
PromtDialog = Frame->IsNewFile();
if( PromtDialog )
{
SD->Filter = SyntaxManager1->GetFilesFilter();
SD->FileName = Frame->FileName;
if( SD->Execute())
{
Frame->SaveFile(SD->FileName);
#ifdef USE_RX_LIB
if( !Frame->IsTheFile(SD->FileName))
MRU->Add(SD->FileName, 0);
#endif
}
}
else Frame->SaveFile(Frame->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TSyntEditMain::FormCloseQuery(TObject *Sender,
bool& CanClose)
{
int ModifiedCount = GetModifiedCount();
if(ModifiedCount > 0)
{
int res = IDYES;
for(int i = 0; i < FrameCount; i++)
if( Frames[i]->Modified )
{
if(res != mrAll)
res = PromtToSave(Frames[i], ModifiedCount > 1);
switch( res )
{
case mrYes:
case mrAll: SaveFrame(Frames[i], False);
break;
case mrCancel:
CanClose = False;
return;
}
ModifiedCount--;
}
}
if(SyntaxManager1->Modified)
switch( MessageDlg("Save changes to LexLib.lxl?", mtConfirmation, mbYesNoCancel, 0))
{
case mrYes: SyntaxManager1->SaveToFile(ExtractFilePath(Application->ExeName) + "LexLib.LXL");
break;
case mrCancel:
CanClose = False;
return;
}
}
//---------------------------------------------------------------------------
TModalResult __fastcall TSyntEditMain::PromtToSave(TEditorFrame* Frame, bool AllowAll)
{
TMsgDlgButtons Buttons = mbYesNoCancel;
if(AllowAll)
Buttons = Buttons << mbAll;
return MessageDlg("Save changes to " + ExtractFileName(Frame->FileName) + "?",
mtConfirmation, Buttons, 0);
}
//---------------------------------------------------------------------------
int __fastcall TSyntEditMain::GetModifiedCount()
{
int Result = 0;
for(int i = 0; i < FrameCount; i++)
if(Frames[i]->Modified)
Result++;
return Result;
}
//---------------------------------------------------------------------------
#ifdef USE_RX_LIB
void __fastcall TSyntEditMain::MRUClick(TObject *Sender, const AnsiString RecentName,
const AnsiString Caption, int UserData)
{
OpenFile(RecentName);
}
#endif
//---------------------------------------------------------------------------
void __fastcall TSyntEditMain::actSaveUpdate(TObject *Sender)
{
actSave->Enabled = CurrentFrame && CurrentFrame->Modified;
}
//---------------------------------------------------------------------------
void __fastcall TSyntEditMain::actNewFileExecute(TObject *Se