📄 gettext.c
字号:
#ifndef __GETTEXT_C
#define __GETTEXT_C
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <graphics.h>
#include "constants.h"
#include "dspstr.h"
#include "draw3d.h"
//-----------------------------
//显示错误信息提示:
// 在文本方式下显示屏幕提示信息!
//-----------------------------
void TMShowErrMsg( char* ErrMsg )
{
char *pte, *pte1, *buffer;
int rowcou, maxrowlen;
int x1,y1, x2,y2, x, y;
struct text_info ti;
pte = ErrMsg;
pte1 = ErrMsg;
//计算待显示文本的最大行宽及行数.
rowcou=1; maxrowlen=0;
while( *pte ){
if( *pte=='\n' ){
rowcou++; *pte=0;
if( maxrowlen < strlen(pte1) ) maxrowlen = strlen(pte1);
pte1=pte+1; *pte='\n';
}
pte++;
}
//缺省处理.
if( maxrowlen ==0 ) maxrowlen = strlen (ErrMsg );
if( maxrowlen <26 ) maxrowlen = 26;
gettextinfo ( &ti );//保存屏幕参数.
//确定显示信息窗的屏幕座标位置.
x1 = ( ti.screenwidth-maxrowlen-4 )/2; x2 = x1+maxrowlen+4;
y1 = ( ti.screenheight-rowcou-4 )/2; y2 = y1+rowcou+3;
buffer = ( char *)( malloc( (x2-x1+1) * (y2-y1+1) ) );//分配内存.
gettext (x1, y1, x2, y2, buffer);//保存显示区的原始信息.
//显示信息窗的框架.
textattr( 0x70 );
window( x1, y1, x2, y1); clrscr();
window( x1, y2, x2, y2); clrscr();
textattr( 0x40);
window( x1, y1+1, x2, y2-1); clrscr();
window(1,1,ti.screenwidth, ti.screenheight);
textattr( 0x70);
gotoxy( x1+(x2-x1)/2-4, y1); cprintf("Warning!");
gotoxy( x1+2, y2); cprintf("Press any to continue...");
//显示提示信息.
textattr(0x40);
pte=ErrMsg;
x= x1+2; y=y1+2;
while(*pte){
if( *pte != '\n' ) { gotoxy(x,y); putch( *pte ); x++;}
else { x=x1+2; y++;}
pte++;
}
sound(2000);delay(50);nosound();//发报警声.
getch();//等待按键.
puttext (x1, y1, x2, y2, buffer);//恢复原始信息.
free(buffer);//释放所占内存.
//恢复屏幕参数.
textattr(ti.attribute);
gotoxy(ti.curx, ti.cury);
}
//-----------------------------
//设置文本模式下光标的形状
// i=0:插入式条形光标
// =1:改写式块形光标.
//-----------------------------
void SetTextCursor(int i)
{
union REGS regs;
regs.h.ah=1;
if(i==0)regs.x.cx=0x80f;
else regs.x.cx=0xe0f;
int86(0x10, ®s, ®s);
}
//--------------------------------
//从文本屏幕接收一行可滚动字符.
//--------------------------------
void TMGetRollStr(int x, int y, int winwid, char *pstr)
{
struct text_info ti;
unsigned char *strbeg, *strend, *winbeg, *winend, *strcur;
unsigned char ckey,str[128];
unsigned int len, MAXLEN=127;
int i, ins_key, ins_f=0,exit_f;
int ikey;
//检查参数的合法性
len=strlen(pstr); if(len>MAXLEN){ pstr[MAXLEN]=0; len=MAXLEN; }
if(winwid<=3)return;//窗口宽度太小, 返回!
//给七个重要的指针分配初始值.
strbeg=pstr; strend=strbeg+len;
if(len>winwid-3)winbeg=strend-winwid+3;
else winbeg=strbeg;
winend=winbeg+winwid-3;
strcur=strend;
//开始处理键值!
gettextinfo(&ti);//保存前一窗口的参数.
exit_f=0;
ins_key= !( bioskey(2)&0x80 );
SetTextCursor(ins_key);
while( !exit_f ){
//容错冗余部分.
if(winbeg<strbeg){ winbeg=strbeg; winend=winbeg+winwid-3; }
if(strcur<winbeg)strcur=winbeg;
if(strend>pstr+MAXLEN)strend=pstr+MAXLEN;
if(strcur>strend)strcur=strend;
//显示变量fname中的值.
//clear display zone
textattr(0x1f);
gotoxy(x,y); for(i=0; i<winwid; i++)putch(' ');
//Display chars.
strcpy(str,winbeg);
str[winend-winbeg+1]=0;
gotoxy(x+1,y); cprintf("%s",str);
//Display scroll sign.
if(winbeg>strbeg){ gotoxy(x,y); putch(17); }//指示可左滚屏
if(winend<strend-1){ gotoxy(x+winwid-1,y); putch(16); }//指示可右滚屏
//Move text cursor.
gotoxy(x+(strcur-winbeg)+1,y);
while( !bioskey(1) ){//检测Ins键状态
ikey=!( bioskey(2)&0x80 );
if(ikey != ins_key){ ins_key=ikey; ins_f=1; break; }
}
if( ins_f ){
SetTextCursor(ins_key); ins_f=0;
continue;
}
ikey=bioskey(0);
ckey=ikey;
switch(ikey){
case LARROW:
if(strcur>strbeg)strcur--;
if(strcur<winbeg){ winbeg--; winend--; }
break;
case RARROW:
if(strcur<strend)strcur++;
if(strcur>winend){ winbeg++; winend++; }
break;
case HOME:
strcur=strbeg; winbeg=strbeg; winend=winbeg+winwid-3;
break;
case END:
strcur=strend;
if(strcur>winend){ winend=strend; winbeg=winend-winwid+3; }
break;
case DEL:
if(strcur==strend)break;
memmove(strcur, strcur+1, strend-strcur+1);
strend--; *strend=0;
break;
case TAB: break;
case ENTER:
// pte=strbeg;
// while( pte<strend && *pte!='*' && *pte!='?' )pte++;
// if(pte==strend){ *strend='\0'; exit_f=1; }
// else ShowErrMsg("Filename should NOT include \n wildcards \"*\" and \"?\"!");
*strend='\0'; exit_f=1;
break;
case BKSP:
if(strcur>strbeg){
memmove(strcur-1, strcur, strend-strcur);//(dest , src, lenght);
strcur--; strend--; *strend=0;
if(winbeg>strbeg){winbeg--; winend--;}
}
break;
default:
ckey=ikey&0xff;
if(ckey<' ')continue;
if( ins_key && strend>=pstr+MAXLEN && strend==strcur){ sound(2000);delay(50);nosound(); continue; }//串太长!
if( ins_key){
memmove(strcur+1, strcur, strend-strcur);
*strcur=ckey; strcur++;
if(strend<pstr+MAXLEN){ strend++; *strend=0; }
}
else{
*strcur=ckey; strcur++;
if(strcur==strend+1 && strend<pstr+MAXLEN){ strend++; *strend=0;}
if(strcur>strend)strcur=strend;
}
if(strcur>winend){ winend++; winbeg++; }
break;
}
// while( bioskey(1) ) bioskey(0);
}
*strend='\0';
SetTextCursor( 1 );//将光标设为插入式条形光标.
textattr(ti.attribute);
gotoxy(x,y);
cprintf(" ");
gotoxy(x,y);
cprintf("%s",pstr);
}
//--------------------------
//在图形方式下显示光标
//--------------------------
// cur_no: 0 显示下划线光标
// 1 显示块形光标
void DispGMCursor(int x, int y, int col, int cur_no)
{
char cur_dot[4][16]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0xff,0xff,0,
0,0,0,0, 0,0,0,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0,
0,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0,
0x18,0x14,0x12,0x29, 0x45,0x43,0x63,0x54, 0x6c,0x50,0xa0,0xc0, 0,0xff,0xff,0};
int x1,y1;
char dot;
for(y1=y; y1<y+16; y1++){
dot=cur_dot[cur_no][y1-y];
for(x1=x; x1<x+8; x1++){
if( dot&0x80 )putpixel(x1,y1,col); dot<<=1;
}
}
}
//--------------------------------
//从图形屏幕读取可滚动的字符串.
//--------------------------------
void GMGetRollStr(int x, int y, int winwid, int col,int bkcol,
unsigned char *pstr)
//注意:pstr的长度必须大于127!!!
//Caution!! The Lenght of "pstr" must be over 127.
{ unsigned len, MAXLEN=127;
unsigned char cur_char[2]=" \0";
unsigned char *strbeg, *strend, *winbeg, *winend, *strcur;
unsigned char str[128];
int i, cur_x, cur_f, cur_t, ins_key, ins_f=0;
int exit_f;
int ikey,winbytes;
unsigned char ckey;
//检查参数的合法性
winwid=winwid/8*8;
winbytes=winwid/8-2;
if(winbytes<=1)return;//窗口宽度太小, 返回!
len=strlen(pstr); if(len>MAXLEN){ pstr[MAXLEN]=0; len=MAXLEN; }
//给七个重要的指针分配初始值.
strbeg=pstr; strend=strbeg+len;
if(len>winbytes-1)winbeg=strend-winbytes+1;
else winbeg=strbeg;
winend=winbeg+winbytes-1;
strcur=strend;
//开始处理键值!
exit_f=0;
ins_key=!( bioskey(2) &0x80 );//Ins键未按下时, 为插入状态!
while( !exit_f ){
//容错冗余部分.
if(winbeg<strbeg){ winbeg=strbeg; winend=winbeg+winbytes-1; }
if(strcur<winbeg)strcur=winbeg;
if(strend>pstr+MAXLEN){ strend=pstr+MAXLEN; *strend=0; }
if(strcur>strend)strcur=strend;
//显示变量fname中的值.
setfillstyle(SOLID_FILL,bkcol);
bar( x, y, x+winwid-1,y+15);
strcpy(str,winbeg);
str[winend-winbeg+1]=0;
out16textxy(x+8,y,str,col,-1);//图形光标被移动
//
if(strcur==strend){
cur_x=x+8+strlen(str)*8;
cur_char[0]=' '; }
else { //strcur<strend
cur_x=x+8+(strcur-winbeg)*8;
cur_char[0]=str[strcur-winbeg]; }
if(winbeg>strbeg)out16textxy(x,y, "\021\0",col,-1);//指示可左滚屏
if(winend<strend-1)out16textxy(x+winwid-8,y,"\020\0",col,-1);//指示可右滚屏
cur_f=1; cur_t=0;
DispGMCursor(cur_x, y, col, !ins_key);//显示光标.
while( !bioskey(1) ){//使光标闪烁!
ikey=!( bioskey(2)&0x80 );
if( ikey!=ins_key ){//检查Ins键是否被按下!
ins_key=ikey; ins_f=1;
break; }
if(cur_f && cur_t==40){//显示光标
cur_t=0; cur_f=0;
DispGMCursor(cur_x, y, col, !ins_key);
}
delay(2); cur_t++;
if(!cur_f && cur_t==80){//关闭光标
cur_t=0; cur_f=1;
setfillstyle(SOLID_FILL,bkcol);
bar( cur_x, y, cur_x+7,y+15);
out16textxy(cur_x, y, cur_char, col,-1);
}
}
if(ins_f){ ins_f=0; continue; }//Ins键被按下!
ikey= bioskey(0);
switch(ikey){
case LARROW:
if(strcur==strbeg){ sound(1000);delay(20);nosound(); break; }
if(strcur>strbeg)strcur--;
while(strcur<winbeg){ winbeg--; winend--; }
break;
case RARROW:
if(strcur==strend){ sound(1000);delay(20);nosound(); break; }
if(strcur<strend)strcur++;
if(strcur>winend){ winbeg++; winend++; }
break;
case HOME:
if(strcur==strbeg){ sound(1000);delay(20);nosound(); break; }
strcur=strbeg; winbeg=strbeg; winend=winbeg+winbytes-1;
break;
case END:
if(strcur==strend){ sound(1000);delay(20);nosound(); break; }
strcur=strend;
if(strcur>winend){
winend=strend; winbeg=winend-winbytes+1;
}
break;
case DEL:
if(strcur==strend){ sound(1000);delay(20);nosound(); break; }
memmove(strcur, strcur+1, strend-strcur+1);
strend--; *strend=0;
break;
case TAB: break;
case ENTER:
case ESC: *strbeg=0; exit_f=1; break;
case BKSP:
if(strcur>strbeg){
memmove(strcur-1, strcur, strend-strcur);//(dest , src, lenght);
strcur--; strend--; *strend=0;
if(winbeg>strbeg){winbeg--; winend--;}
}
else sound(1000);delay(20);nosound();
break;
default://Some ASCII characters inputed.OK.
ckey=ikey&0xff;
// if(!ckey)break;//The sentence is important!!!!
if(ckey<' ')break;
if( ins_key && strend>=pstr+MAXLEN && strend==strcur){ sound(1000);delay(20);nosound(); continue; }//串太长!
if( ins_key&&(strcur!=strend)){
memmove(strcur+1, strcur, strend-strcur);
*strcur=ckey; strcur++;
if(strend<pstr+MAXLEN){ strend++; *strend=0; }
}
else{
*strcur=ckey; strcur++;
if(strcur==strend+1 && strend<pstr+MAXLEN){ strend++; *strend=0;}
if(strcur>strend)strcur=strend;
}
if(strcur>winend){ winend++; winbeg++; }
break;
}
}
*strend='\0';
}
//--------------------------------
//从图形屏幕接收字符(不能滚动).
//--------------------------------
//--------------------------------
int GMGetStr(int x, int y, int winwid, unsigned char *textstr,
unsigned char F_Flash,unsigned char F_Disp,unsigned char F_Down)
//if(F_Flash) textstr[0]=0 first;
//if(!F_Disp) content in textstr will not be appear.
//if(F_Down) UP_ARROW,DOWN_ARROW,TAB,LEFTARROW.etc will same as ENTER
{ unsigned char cte, str[41], *pstr1,*pstr2;
int kte, exit_f,col=WHITE,bkcol=BLUE,bkcol1=LIGHTGRAY;
unsigned cur_f,cur_t,cur_x,i;
if(winwid/8<1 || winwid/8 >40)return 0;
else winwid=winwid/8*8;
DrawFrame2(x-1,y-1,x+winwid,y+16,1);
setwritemode(COPY_PUT);
setfillstyle(SOLID_FILL,bkcol);
bar(x,y,x+winwid-1,y+15);
pstr1=textstr;
pstr2=str; *pstr2=0;
if(F_Flash)*pstr1=0;
else {
*(pstr1+winwid/8)=0;
//Clear ' ' in textstr;
pstr1=textstr;
while(*pstr1==' ')pstr1++;
if(pstr1!=textstr){
i=strlen(pstr1);
memmove(textstr,pstr1,i); *(textstr+i)=0;
}
pstr1=textstr+strlen(textstr);
while(*(pstr1-1)==' '){ *(pstr1-1)=0; pstr1--; }
//Display it.
out16textxy(x,y,textstr,col,-1);
//ready str.
for(i=0; i<strlen(textstr); ++i)str[i]='*';str[i]=0;
pstr2=str+strlen(textstr);
}
exit_f=0;
while(!exit_f){
cur_f=1; cur_t=0;
cur_x=x+strlen(textstr)*8;
if(strlen(textstr)<winwid/8){
DispGMCursor(cur_x, y, col, 0);//显示光标.
while( !bioskey(1) ){//使光标闪烁!
if(cur_f && cur_t==40){//显示光标
cur_t=0; cur_f=0;
DispGMCursor(cur_x, y, col, 0);
}
delay(2); cur_t++;
if(!cur_f && cur_t==80){//关闭光标
cur_t=0; cur_f=1;
bar( cur_x, y, cur_x+7,y+14);
}
}
}
//开始处理键值!
kte=bioskey(0);
switch(kte){
case LARROW:
case RARROW:
case DARROW:
case UARROW:
case TAB:
if(!F_Down) break;
case ENTER:
setfillstyle(SOLID_FILL,bkcol1);
bar(x,y,x+winwid-1,y+15);
if(F_Disp)out16textxy(x,y,textstr,col,bkcol1);
else out16textxy(x,y,str,col,bkcol1);
exit_f=1; break;
case BKSP:
if(pstr1>textstr){ *(--pstr1)=0; *(--pstr2)=0; }
else { sound(2000); delay(50); nosound(); continue;}
bar(x,y,x+winwid-1,y+15);
if(F_Disp)out16textxy(x,y,textstr,col,bkcol);
else out16textxy(x,y,str,col,bkcol);
break;
default://Some ASCII characters inputed.OK.
if(pstr1-textstr>=winwid/8){
sound(2000); delay(50); nosound(); continue;}
cte=(char)kte;
if(cte<' ' || cte>0x7e){
sound(2000); delay(50); nosound(); continue;}
*pstr1=cte; pstr1++; *pstr1=0;
*pstr2='*'; pstr2++; *pstr2=0;
if(F_Disp)out16textxy(x,y,textstr,col,bkcol);
else out16textxy(x,y,str,col,bkcol);
break;
}
}
return kte;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -