/* prompt.c:
This program demonstrates a standard GL prompt and a user-defined prompt. If you choose the user-defined prompt, mouse events are ignored until you press the Enter key.
Peter Broadwell & dave ratcliffe 1989 */
#include <stdio.h> #include <gl/gl.h> #include <gl/device.h>
#define PROMPT 1 #define EXIT 2
long menu; /* The user-defined prompt's identifier */ char aString[40];
main() { Device dev; short val; long menuval;
init(); /* process events forever */ while(TRUE) { dev=qread(&val); switch(dev) { case ESCKEY: exit(); break; case REDRAW: reshapeviewport(); color(BLUE); clear(); break; case RIGHTMOUSE: if(val) { menuval = dopup(menu); switch (menuval) { case PROMPT: /* prompt to get file name */ getUserString("File: ", aString, sizeof(aString)); printf("The user entered \"%s\"\n", aString); break; case EXIT: exit(); break; default: break; }
}
break; default: break; }
}
} init() /* do all the basic graphics setup */ { long sx, sy; ginit(); /* Open a full size window */ overlay(2); drawmode(OVERDRAW); mapcolor(BLACK,0,0,0); mapcolor(RED,255,0,0); drawmode(NORMALDRAW); gconfig(); qdevice(ESCKEY); qdevice(RIGHTMOUSE); qenter(REDRAW, 1); menu = defpup("GL-style prompt %t|My Prompt|Exit"); }
/* Clear prompt, move to start of prompt box, and output requested prompt */ getUserString(prompt,userStr,maxlen) /* get name of file */ char *prompt, *userStr; int maxlen; { /* lower left corner of prompt box */
#define FILEX 5
#define FILEY 15
#define FILEYHI (30+FILEY) /* 30 pixels hi */
#define TEXTX (FILEX+5)
#define TEXTY (FILEY+10)
#define clearprompt(aprmpt) \ color(RED); clear(); color(BLACK); linewidth(2);\ recti(FILEX+2, FILEY+2, wxsize-8, FILEYHI-1);\ linewidth(1); cmov2i(TEXTX, TEXTY); charstr(aprmpt);
int cur_str_len; short c; Device dev; long maxwidth, maxxval; /* max length of window's width in pixels */ char *str; char *prmpt = prompt, keyBoardWasQueued; long oldmode, xorig, yorig, wxsize, wysize; Screencoord mask1, mask2, mask3, mask4; /* Save old state to restore latter */ pushmatrix(); oldmode = getdrawmode(); getscrmask(&mask1, &mask2, &mask3, &mask4); keyBoardWasQueued = isqueued(KEYBD); drawmode(OVERDRAW); /* Enable overlay */ /* Set viewport to fill window */ getorigin(&xorig,&yorig); getsize(&wxsize,&wysize); ortho2(-0.5,(float)wxsize, -0.5, (float)wysize); maxxval = wxsize + xorig; userStr[0] = '\0'; maxwidth = (wxsize-11) - (FILEX + strwidth(prompt)); scrmask(FILEX, (Screencoord)(wxsize-6), FILEY, FILEYHI); cur_str_len = strlen(userStr); clearprompt(prmpt); /* Display my prompt */ qdevice(KEYBD); /* read till eof ('\n' or '\r') */ while(dev = qread(&c)) { if(dev != KEYBD) continue; /* don't care */ switch(c) { case '\027': /* ^W sets cursor back to start */ cur_str_len = 0; clearprompt(prmpt); break; case '\n': case '\r': goto done; case '\b': if(cur_str_len) { userStr[--cur_str_len] = '\0'; clearprompt(prmpt); /* display rightmost portion */ for(str=userStr; *str && strwidth(str) > maxwidth; str++); charstr(str); }
break; default: if(cur_str_len < (maxlen -1)) { str = &userStr[cur_str_len]; userStr[cur_str_len++] = c; userStr[cur_str_len] = '\0'; charstr(str); }
else { ringbell(); }
break; }
}
done: if(!keyBoardWasQueued) unqdevice(KEYBD); scrmask(mask1, mask2, mask3, mask4); /* restore old */ drawmode(OVERDRAW); color(0); clear(); drawmode(oldmode); popmatrix(); userStr[maxlen] = '\0'; }
The drawmode subroutine, getdrawmode subroutine, getscrmask subroutine, ginit subroutine, isqueued subroutine, linewidth subroutine, scrmask subroutine, strwidth subroutine, unqdevice subroutine.