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

📄 textbox.cpp

📁 SimpleGraphicOperatingSystem 32位图形化操作系统 多进程 支持FAT32 详见www.sgos.net.cn
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				rect.right = right;
				Refresh(rect);
			}
		}
	}

	int TextBox::DrawTextBox(int type){
		switch( type ){
		case 0:
			if(!(style&TEXTBOX_NOBACKGROUND))
				Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.inactive.backColor );
			if(!(style&TEXTBOX_NOFRAME))
				//Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,winStyle.inactive.borderColor );

                                Bitmap::Line( bitmap, 1, 1, bitmap->Width-2, 1, Bitmap::MakeColor(150,150,150));//上
                                Bitmap::Line( bitmap, 1, 1, 1, bitmap->Height-1, Bitmap::MakeColor(150,150,150));//左
                                Bitmap::Line( bitmap, 1,0,bitmap->Width-1-1,0,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, 1,bitmap->Height-1,bitmap->Width-1-1,bitmap->Height-1,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, 0,1,0,bitmap->Height-1-1,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, bitmap->Width-1,1,bitmap->Width-1,bitmap->Height-1-1,winStyle.active.borderColor );
			break;
		case 1:
			if(!(style&TEXTBOX_NOBACKGROUND))
				Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.active.backColor );
			if(!(style&TEXTBOX_NOFRAME))
				//Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,winStyle.active.borderColor );

                                Bitmap::Line( bitmap, 1, 1, bitmap->Width-2, 1, Bitmap::MakeColor(150,150,150));//上
                                Bitmap::Line( bitmap, 1, 1, 1, bitmap->Height-1, Bitmap::MakeColor(150,150,150));//左
                                Bitmap::Line( bitmap, 1,0,bitmap->Width-1-1,0,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, 1,bitmap->Height-1,bitmap->Width-1-1,bitmap->Height-1,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, 0,1,0,bitmap->Height-1-1,winStyle.active.borderColor );
                                Bitmap::Line( bitmap, bitmap->Width-1,1,bitmap->Width-1,bitmap->Height-1-1,winStyle.active.borderColor );
			break;
		}

		if(!(style&TEXTBOX_NOVSCROLLBAR)){
			vScroll->OnPaint();
		}

		ReloadChars();
		DrawChars();
	}

	int TextBox::SetScrollBar(){
		if( !colChars ) return 0;
		int lines;
		lines = GetTextLines();
		int t = lines/colChars;
		//DPRINT("lines:%d  t:%d  lines-colChars:%d", lines, t, lines-colChars );
		if(t>=1)
			vScroll->Set(0,lines-colChars+1, 1, colChars);
		else
			vScroll->Set(0, 0, 0, 0);
		vScroll->SetValue( vY );
	}

	int TextBox::OnChange(){
	}

	int TextBox::SetText(string strText){
		strContent = strText;
		ReloadChars();
		SetSelStart(selStart);
		SetSelLength(0);
		DrawChars();
		if(!(style&TEXTBOX_NOVSCROLLBAR)){
			SetScrollBar();
		}
		OnChange();
	}

	int TextBox::DrawCursor(int x,int y){
		Bitmap::Line( bitmap, x, y, x, y+ch-2, Bitmap::BLACK);
	}

	int TextBox::OnPaint(){
		int i;
		for(i=0;i<rowChars*colChars;i++)
			chars[i].dirty=true;
		DrawTextBox(0);
		Refresh();
		BaseWindow::OnPaint();
	}

	int TextBox::OnMessage( Message* msg ){
		switch(msg->message){
		case WM_SETTEXT:
			strContent = (char*)msg->param[0];
			OnPaint();
		}
		BaseWindow::OnMessage(msg);
	}

	_Char* TextBox::CharFromPos(int x, int y ){
		int c=((short)x-left)/cw;
		int r=((short)y-top)/ch;
		int newVX=0,newVY=0;
		if(c<0){
			newVX=c-rowChars;
			c=0;
		}
		if(c>=rowChars){
			newVX=c-rowChars;
			c=rowChars-1;
		}
		if(r<0){
			//DPRINT("r=%d",r);
			newVY=r-colChars;
			r=0;
		}

		if(r>=colChars){
			newVY=r-colChars;
			r=colChars-1;
		}
		if(newVX||newVY)
			SetVisualPosition( vX+newVX, vY+newVY );
		_Char* chr=&chars[r*rowChars+c];
		if( !chr->value ){
			while( chr>=chars&&!chr->value ) chr--;
			chr++;
		}

		if(chr>chars&&(chr-1)->wordFlag)
			chr--;
		return chr;
	}

	int TextBox::DrawSingleChar(_Char* ch){
		if(ch)
			ch->dirty=true;
		DrawChars();
	}

	int TextBox::SetSelStart(int sel){
		if( sel<0 ) sel=0;
		if( sel>strContent.Length() ) sel=strContent.Length();
		int oldStart = selStart;
		selStart = sel;
		int i, found=0;
		for(i=0;i<rowChars*colChars;i++)
			if(chars[i].pos==oldStart||&chars[i]==selStartChar){
				chars[i].dirty=true;
				break;
			}
		for(i=0;i<rowChars*colChars;i++){
			if(chars[i].pos==selStart)
			{
				selStartChar=&chars[i];
				if(chars[i-1].wordFlag){
					selStart--;
					selStartChar=&chars[i-1];
				}
				selStartChar->dirty=true;
				//DPRINT("Set sel start at %d", selStart );
				found=true;
				break;
			}
		}
		if(!found){
			SetVisualPosition(0,GetRowFromPos(selStart)-colChars+1);
			//DPRINT("row GetRowFromPos(selStart):%d  colChars:%d  result:%d", GetRowFromPos(selStart),colChars,GetRowFromPos(selStart)-colChars+1);
		}
		DrawChars();
	}

	int TextBox::SetSelLength(int len){
		selLength=len;
		int sEnd = MAX( selStart, selStart+selLength );
		int sStart = MIN( selStart, selStart+selLength );
		int i;
		for(i=0;i<rowChars*colChars;i++){
			if(chars[i].value&&chars[i].pos>=sStart&&chars[i].pos<sEnd){
				if(!chars[i].selected){
					chars[i].selected=true;
					chars[i].dirty=true;
				}
			}else{
				if(chars[i].selected){
					chars[i].selected=false;
					chars[i].dirty=true;
				}
			}
		}

		DrawChars();
	}

	int TextBox::OnMouseDown( int button, Point* p ){
		if(button==1){
			if(bShift){
				_Char* chrEnd = CharFromPos(p->x,p->y);
				if(!chrEnd)return 0;
				if( selLength!=chrEnd->pos-selStart )
				{
					SetSelLength(chrEnd->pos-selStart);
				}
			}else{
				bCatched = true;
				SetSelLength(0);
				_Char* chrStart = CharFromPos(p->x,p->y);
				if(chrStart)
					SetSelStart(chrStart->pos);
			}
		}
		BaseWindow::OnMouseDown(button, p);
	}
	int TextBox::OnMouseUp( int button, Point* p ){
		if(button==1){
			bCatched=false;
		}
		BaseWindow::OnMouseUp(button, p);
	}
	int TextBox::OnMouseMove( Point* p ){
		if(bCatched){
			_Char* chrEnd = CharFromPos(p->x,p->y);
			if(!chrEnd)return 0;
			if( selLength!=chrEnd->pos-selStart )
			{
				SetSelLength(chrEnd->pos-selStart);
			}
			BaseWindow::OnMouseMove(p);
		}
	}

	int TextBox::SetSelText(string strText){
		int sEnd = MAX( selStart, selStart+selLength );
		int sStart = MIN( selStart, selStart+selLength );
		int newStart = sStart+strText.Length();
		SetText(strContent.Left(sStart)+strText+strContent.Right(strContent.Length()-sEnd));
		//ReloadChars();
		SetSelStart(newStart);

	}

	int TextBox::DelSelText()
	{
		int sEnd = MAX( selStart, selStart+selLength );
		int sStart = MIN( selStart, selStart+selLength );
		for(int i=0;i<rowChars*colChars;i++)
			if(chars[i].pos==selStart){
				chars[i].dirty=true;
				break;
			}
		if(sStart==sEnd){
			if(sStart>0){
				if( HasWordFlag(sStart-2) ){
					SetText(strContent.Left(sStart-2)+strContent.Right(strContent.Length()-sEnd));
					SetSelStart(sStart-2);
				}else if(strContent[sStart-1]=='\n'&&strContent[sStart-2]=='\r'){
					SetText(strContent.Left(sStart-2)+strContent.Right(strContent.Length()-sEnd));
					SetSelStart(sStart-2);
				}else{
					SetText(strContent.Left(sStart-1)+strContent.Right(strContent.Length()-sEnd));
					SetSelStart(sStart-1);
				}
			}
		}else{
			SetText(strContent.Left(sStart)+strContent.Right(strContent.Length()-sEnd));
			SetSelStart(sStart);
		}
	}

	int TextBox::OnKeyDown( int code, char ascii, int flag ){
		//DPRINT("%x", code );
		if(code==0x30){
			bShift=true;
			return 0;
		}
		char str[9]={0,};
		str[0]=ascii;
		str[1]=0;
		switch(ascii){
		case '\b':
			DelSelText();
			break;
		case '\n':
		case '\r':
			if(style&TEXTBOX_MUTILINE)
				SetSelText(string(str));
			else
				SetSelText(string(""));
			break;
		default:
			if(isprint(ascii))
				SetSelText(string(str));
		}
		BaseWindow::OnKeyDown(code,ascii,flag);
	}

	int TextBox::OnKeyUp( int code, char ascii, int flag ){
		if(code==0x30){
			bShift=false;
		}
	}

	int TextBox::SelStart(){
		return selStart;
	}

	int TextBox::SelLength(){
		return selLength;
	}

	string& TextBox::Text(){
		return strContent;
	}
}

⌨️ 快捷键说明

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