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

📄 javaedit.cpp

📁 jedit 一个小型java编译器的源码
💻 CPP
字号:
#include"javaedit.h"
COLORREF JavaEdit::CommentColor=RGB(0,128,0);
COLORREF JavaEdit::KeywordColor=RGB(0,0,255);
void JavaEdit::OnDraw(HDC hdc)
{
	RECT rect;
	rect.left=rect.top=0;
	rect.right=Width;
	rect.bottom=Height;
	SelectObject(m_hdcBack,m_BmpBack);
	SelectObject(m_hdcBack,(HFONT)GetStockObject(SYSTEM_FIXED_FONT));
	HBRUSH hBgBrush=CreateSolidBrush(hBgColor);
	FillRect(m_hdcBack,&rect,hBgBrush);
	SetBackGround();
	SetBkMode(m_hdcBack,TRANSPARENT);

	int TotalLine;
	int i,j;
	char c[2];
	bool SelFlag=false;
	bool StrFlag=false;
	bool KeyWordFlag=false;
	int CommentFlag=NCOMMENT;

	TotalLine=Str.GetTotalLine();
	SetBkColor(m_hdcBack,hBgColor);
	SetTextColor(m_hdcBack,hTextColor);
	for(i=1;i<=TotalLine;i++)
	{
		if(Y+i*cHeight<5||Y+i*cHeight>Height)
			continue;
		for(j=1;j<Str.GetLineWidth(i);j++)
		{
			c[0]=Str.GetChar(i,j);
			if(c[0]==0x0d) continue;
			if(c[0]==0x09) c[0]=0x20;
			if(Str.IsInSelection(i,j))
			{
				if(!SelFlag)
				{
					SelFlag=true;
					SetBkMode(m_hdcBack,OPAQUE);
					SetBkColor(m_hdcBack,hTextColor);
					SetTextColor(m_hdcBack,hBgColor);
				}
			}
			else
			{
				if(SelFlag)
				{
					SelFlag=false;
					SetBkMode(m_hdcBack,TRANSPARENT);
					SetBkColor(m_hdcBack,hBgColor);
					SetTextColor(m_hdcBack,hTextColor);
				}
			}
			if(c[0]=='\"')
				StrFlag=!StrFlag;
			if(!SelFlag)
			{
				Hstring Sel(Str.GetSelectWord(i,j));
				if(!StrFlag)
				{
					for(int k=0;k<KeyWord.length();k++)
					{
						if(KeyWord[k]==Sel)
						{
							if(!KeyWordFlag&&!CommentFlag)
							{
								KeyWordFlag=true;
								SetBkColor(m_hdcBack,hBgColor);
								SetTextColor(m_hdcBack,KeywordColor);
							}
							break;
						}
					}
				
					if(k==KeyWord.length()&&KeyWordFlag)
					{
						KeyWordFlag=false;
						SetBkColor(m_hdcBack,hBgColor);
						SetTextColor(m_hdcBack,hTextColor);
					}
				}
				int Comment=IsComment(i,j);
				switch(Comment)
				{
				case COMMENT1:
				case COMMENT2:
					CommentFlag=Comment;
					SetBkColor(m_hdcBack,hBgColor);
					SetTextColor(m_hdcBack,CommentColor);
					break;
				case COMMENT1END:
					if(CommentFlag==COMMENT1)
					{
						CommentFlag=NCOMMENT;
						SetBkColor(m_hdcBack,hBgColor);
						SetTextColor(m_hdcBack,hTextColor);
					}
					break;
				case COMMENT2END:
					if(CommentFlag==COMMENT2)
					{
						CommentFlag=NCOMMENT;
						SetBkColor(m_hdcBack,hBgColor);
						SetTextColor(m_hdcBack,hTextColor);
					}
					break;
				}
			}
			if(c[0]==0x0a) 
			{
				StrFlag=false;
				continue;
			}
			if(c[0]>=-95&&c[0]<=-2)
			{
				c[1]=Str.GetChar(i,++j);
				if(PosInRect(X+(j-2)*cWidth,Y+(i-1)*cHeight))
					TextOut(m_hdcBack,X+(j-2)*cWidth,Y+(i-1)*cHeight,c,2);
			}
			else
				if(PosInRect(X+(j-1)*cWidth,Y+(i-1)*cHeight))
					TextOut(m_hdcBack,X+(j-1)*cWidth,Y+(i-1)*cHeight,c,1);
		}
	}
	DeleteObject(hBgBrush);
	BitBlt(hdc,0,0,Width,Height,m_hdcBack,0,0,SRCCOPY);
}
void JavaEdit::OnLCChanged()
{
	LineStart=0;
	for(int i=0;i<Str.GetCur();i++)
	{
		if(Str.GetChar(i)=='{')
			LineStart+=4;
		if(Str.GetChar(i)=='}')
			LineStart-=4;
	}
	SetMenu();
}
int JavaEdit::IsComment(int line,int column)
{
	char c[4];
	int Pos=Str.GetPos(line,column);
	c[0]=Str.GetChar(Pos-2);
	c[1]=Str.GetChar(Pos-1);
	c[2]=Str.GetChar(Pos);
	if(Pos==Str.GetLength())
		c[3]=0;
	else
		c[3]=Str.GetChar(Pos+1);

	if(c[2]=='/'&&c[3]=='*')
		return COMMENT1;
	if(c[2]=='/'&&c[3]=='/')
		return COMMENT2;
	if(c[0]=='*'&&c[1]=='/')
		return COMMENT1END;

	if(c[2]==0x0a) 
		return COMMENT2END;

	return NCOMMENT;
}

void JavaEdit::OnChar(WPARAM wParam,LPARAM lParam)
{
	int i;
	int Line,Column;
	switch(wParam)
	{
	case 0x1b:
		return;
	case '}':
		Column=Str.GetColumn()-1;
		Line=Str.GetLine();
		LineStart-=4;      
		while(Column>LineStart)
		{
			Column--;
			if(Str.GetChar(Line,Column+1)==0x20)
			{
				Str.DeletePrev();
			}
			else
				break;
		}
		Str.Insert('}');
		break;
	case 0x0d:
		Str.DeleteSel();
		Str.Insert(0x0a);
		for(i=0;i<LineStart;i++)
			Str.Insert(0x20);
		break;
	case 0x09:
		Str.DeleteSel();
		if((Str.GetPos()-1)%4!=0)
			while((Str.GetPos()-1)%4!=0)
				Str.Insert(0x20);
		else
			for(int i=0;i<4;i++)
				Str.Insert(0x20);
		break;
	case 0x08:
		if(!Str.IsSelEmpty())
			Str.DeleteSel();
		else
			Str.DeletePrev();
		break;
	default:
		Str.DeleteSel();
		Str.Insert((char)wParam);
	}
	AdjustCaretPos();
	SetScrollBar(m_hWnd);
	InvalidateRect(m_hWnd,NULL,false);
}

⌨️ 快捷键说明

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