//----------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//----------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
bool OpenTag = false;
bool GoBack = false;
int Color = 0;
bool GetNextChar = true;
bool NeedMoreChar = false;
int start;
int end;
char PreviousChar;
bool WaitForGreater = false;
bool GotGreater = false;
int SlashCount = 0;
int PreviousStart = 0;
//----------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------
void ColorChange()
{
if(GoBack == true)
{
Form1->RichEdit1->SelStart = start;
Form1->RichEdit1->SelLength = end - start;
Form1->RichEdit1->SelLength++;
Form1->RichEdit1->SelAttributes->Style = Form1->RichEdit1->DefAttributes->Style;
switch(Color)
{
case 0:
Form1->RichEdit1->SelAttributes->Color = clBlack;
break;
case 1:
Form1->RichEdit1->SelAttributes->Color = clBlue;
break;
case 2:
Form1->RichEdit1->SelAttributes->Color = clRed;
break;
case 3:
Form1->RichEdit1->SelAttributes->Color = clPurple;
break;
case 4:
Form1->RichEdit1->SelAttributes->Color = clGreen;
break;
case 5:
Form1->RichEdit1->SelAttributes->Color = clTeal;
break;
case 6:
Form1->RichEdit1->SelAttributes->Color = clOlive;
break;
case 7:
Form1->RichEdit1->SelAttributes->Color = clDkGray;
break;
case 8:
Form1->RichEdit1->SelAttributes->Color = clLime;
break;
case 9:
Form1->RichEdit1->SelAttributes->Color = clSkyBlue;
break;
default:
break;
}
Form1->RichEdit1->SelStart = end + 1;
Form1->RichEdit1->SelAttributes->Color = clBlack;
Color = 0;
GoBack = false;
}
}
//----------------------------------------------------------
void TForm1::ColorizeTags()
{
int OldStart = RichEdit1->SelStart;
WaitForGreater = false;
int Counter = 0;
char Key;
int Length = Form1->RichEdit1->Text.Length();
AnsiString Char = " ";
Screen->Cursor = crHourGlass;
while(Counter < Length)
{
Form1->RichEdit1->SelStart = Counter;
Form1->RichEdit1->SelLength = 1;
Char = Form1->RichEdit1->SelText;
if(Char != "")
{
Key = Char[1];
Form1->RichEdit2KeyPress(Form1,Key);
Form1->RichEdit2Change(Form1);
}
Counter++;
}
RichEdit1->SelStart = OldStart;
Screen->Cursor = crDefault;
}
//----------------------------------------------------------
void __fastcall TForm1::RichEdit2KeyPress(TObject *Sender, char &Key)
{
if(OpenTag == false)
{
if(Key == '<')
{
start = RichEdit1->SelStart;
OpenTag = true;
GetNextChar = true;
SlashCount = 0;
}
if(Key == '>')
{
GotGreater = true;
}
}
else if(OpenTag == true)
{
if(NeedMoreChar == true)
{
if((PreviousChar == 'a')||(PreviousChar == 'A'))
{
if((Key == 'p')||(Key == 'P'))
{//<applet>
Color = 2;
}
if((Key == 'r')||(Key == 'R'))
{//<area>
Color = 8;
}
if((Key == '>')||(Key == ' '))
{//<a>
Color = 6;
}
}
if((PreviousChar == 'b')||(PreviousChar == 'B'))
{
if((Key == 'o')||(Key == 'O'))
{//<body>
Color = 1;
}
if((Key == 'r')||(Key == 'R'))
{//<br>
Color = 3;
}
if((Key == '>'))
{//<b>
Color = 3;
}
}
if((PreviousChar == 'c')||(PreviousChar == 'C'))
{
if((Key == 'e')||(Key == 'E'))
{//<center>
Color = 3;
}
if((Key == 'o')||(Key == 'O'))
{//<code>
Color = 0;
}
}
if((PreviousChar == 'h')||(PreviousChar == 'H'))
{
if((Key == '1')||(Key == '2')||(Key == '3')||(Key == '4')||(Key == '5')||(Key == '6'))
{//<H#>
Color = 3;
}
if((Key == 'e')||(Key == 'E'))
{//<head>
Color = 1;
}
if((Key == 'r')||(Key == 'R'))
{//<hr>
Color = 3;
}
if((Key == 't')||(Key == 'T'))
{//<html>
Color = 1;
}
}
if((PreviousChar == 'i')||(PreviousChar == 'I'))
{
if((Key == 'm')||(Key == 'M'))
{//<img>
Color = 4;
}
if((Key == 'n')||(Key == 'N'))
{//<input>
Color = 8;
}
if((Key == '>'))
{//<i>
Color = 3;
}
}
if((PreviousChar == 'm')||(PreviousChar == 'M'))
{
if((Key == 'a')||(Key == 'A'))
{//<map>
Color = 0;
}
if((Key == 'e')||(Key == 'E'))
{//<meta>
Color = 0;
}
}
if((PreviousChar == 'p')||(PreviousChar == 'P'))
{
if((Key == 'a')||(Key == 'A'))
{//<Param>
Color = 2;
}
if((Key == '>'))
{//<p>
Color = 3;
}
}
if((PreviousChar == 's')||(PreviousChar == 'S'))
{
if((Key == 'c')||(Key == 'C'))
{//<script>
Color = 2;
}
if((Key == 't')||(Key == 'T'))
{//<style>
Color = 9;
}
}
if((PreviousChar == 't')||(PreviousChar == 'T'))
{
if((Key == 'a')||(Key == 'A'))
{//<table>
Color = 5;
}
if((Key == 'd')||(Key == 'D'))
{//<td>
Color = 5;
}
if((Key == 'e')||(Key == 'E'))
{//<textarea>
Color = 8;
}
if((Key == 'i')||(Key == 'I'))
{//<title>
Color = 7;
}
if((Key == 'r')||(Key == 'R'))
{//<tr>
Color = 5;
}
}
NeedMoreChar = false;
}
if(GetNextChar == true)
{
if(Key == ' ')
{
GetNextChar = true;
}
else if(Key == '/')
{
if(SlashCount < 1)
{
SlashCount++;
GetNextChar = true;
}
else
{
GetNextChar = false;
Color = 10;
}
}
else
{
//--- Tags that only need one character to define
if((Key == 'D')||(Key == 'd'))
{
Color = 8;
}
if((Key == 'F')||(Key == 'f'))
{
Color = 3;
}
if((Key == 'U')||(Key == 'u'))
{
Color = 3;
}
if((Key == 'V')||(Key == 'v'))
{
Color = 2;
}
if((Key == 'E')||(Key == 'e'))
{
Color = 0;
}
//--- Tags that need more characters to define
if((Key == 'a')||(Key == 'A')||(Key == 'B')||(Key == 'b')
||(Key == 'c')||(Key == 'C')||(Key == 'h')||(Key == 'H')
||(Key == 'i')||(Key == 'I')||(Key == 'm')||(Key == 'M')
||(Key == 'p')||(Key == 'P')||(Key == 's')||(Key == 'S')
||(Key == 't')||(Key == 'T'))
{
NeedMoreChar = true;
PreviousChar = Key;
}
GetNextChar = false;
}
}
if(Key == '>')
{
end = RichEdit1->SelStart;
RichEdit1->SelLength = 1;
GoBack = true;
OpenTag = false;
if(WaitForGreater == true)
{
GotGreater = true;
}
}
}
}
//----------------------------------------------------------
void __fastcall TForm1::RichEdit2Change(TObject *Sender)
{
PreviousStart = RichEdit1->SelStart;
if(GoBack == true)
{
Form1->RichEdit1->SelStart = start;
Form1->RichEdit1->SelLength = end - start;
Form1->RichEdit1->SelLength++;
Form1->RichEdit1->SelAttributes->Style = Form1->RichEdit1->DefAttributes->Style;
switch(Color)
{
case 0:
Form1->RichEdit1->SelAttributes->Color = clBlack;
break;
case 1:
Form1->RichEdit1->SelAttributes->Color = clBlue;
break;
case 2:
Form1->RichEdit1->SelAttributes->Color = clRed;
break;
case 3:
Form1->RichEdit1->SelAttributes->Color = clPurple;
break;
case 4:
Form1->RichEdit1->SelAttributes->Color = clGreen;
break;
case 5:
Form1->RichEdit1->SelAttributes->Color = clTeal;
break;
case 6:
Form1->RichEdit1->SelAttributes->Color = clOlive;
break;
case 7:
Form1->RichEdit1->SelAttributes->Color = clDkGray;
break;
case 8:
Form1->RichEdit1->SelAttributes->Color = clLime;
break;
case 9:
Form1->RichEdit1->SelAttributes->Color = clSkyBlue;
break;
case 10:
Form1->RichEdit1->SelAttributes->Color = clNavy;
break;
default:
break;
}
Form1->RichEdit1->SelStart = end + 1;
Form1->RichEdit1->SelAttributes->Color = clBlack;
Color = 0;
GoBack = false;
}
else
{
Form1->StatusBar1->SimpleText = "";
}
if((WaitForGreater == true)&&(GotGreater == true)&&(OpenTag == false))
{
RichEdit1->Lines->BeginUpdate();
ColorizeTags();
RichEdit1->Lines->EndUpdate();
RichEdit1->SelLength = 0;
RichEdit1->SelAttributes->Color = clBlack;
GotGreater = false;
WaitForGreater = false;
}
RichEdit1->SelStart = PreviousStart;
}
//----------------------------------------------------------
void __fastcall TForm1::SaveWebPage1Click(TObject *Sender)
{
if((RichEdit1->Modified == true)&&(SaveBox->FileName != ""))
{
RichEdit1->PlainText = true;
RichEdit1->Lines->SaveToFile(SaveBox->FileName);
RichEdit1->PlainText = false;
}
else if((RichEdit1->Modified == true)&&(SaveBox->Execute()))
{
RichEdit1->PlainText = true;
RichEdit1->Lines->SaveToFile(SaveBox->FileName);
Form1->Caption = "HTML Editor- " + SaveBox->FileName;
RichEdit1->PlainText = false;
}
}
//----------------------------------------------------------
void __fastcall TForm1::LoadWebPage1Click(TObject *Sender)
{
int selection;
if(RichEdit1->Modified)
{
selection = Application->MessageBoxA("File has been modified... Save Changes?","Save Changes?",MB_YESNO);
if(selection == 6)
{
Form1->SaveWebPage1Click(Form1);
}
}
if(OpenBox->Execute())
{
RichEdit1->Lines->Clear();
RichEdit1->SelAttributes->Color = clBlack;
RichEdit1->Lines->LoadFromFile(OpenBox->FileName);
Form1->Caption = "HTML Editor- " + OpenBox->FileName;
SaveBox->FileName = OpenBox->FileName;
ColorizeTags();
}
}
//----------------------------------------------------------void __fastcall TForm1::NewWebPage1Click(TObject *Sender)
{
int selection;
if(RichEdit1->Modified)
{
selection = Application->MessageBoxA("File has been modified... Save Changes?","Save Changes?",MB_YESNO);
if(selection == 6)
{
Form1->SaveWebPage1Click(Form1);
}
}
RichEdit1->Lines->Clear();
SaveBox->FileName = "";
OpenBox->FileName = "";
}
//---------------------------------------------------------
void __fastcall TForm1::RichEdit2KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if((Key == VK_DELETE)||(Key == VK_BACK))
{
WaitForGreater = true;
}
}
//----------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
int selection;
if(RichEdit1->Modified == true)
{
selection = Application->MessageBoxA("File has been modified... Save Changes?","Save Changes?",MB_YESNO);
if(selection == 6)
{
Form1->SaveWebPage1Click(Form1);
}
}
}
//----------------------------------------------------------
void __fastcall TForm1::PageControl1Change(TObject *Sender)
{
Form1->SaveWebPage1Click(Form1);
CppWebBrowser1->Navigate((WideString)SaveBox->FileName);
}
//----------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Form1->Close();
}
//----------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->PageControl1->TabIndex = 0;
}
//----------------------------------------------------------