home *** CD-ROM | disk | FTP | other *** search
- // ┌───────┐
- // ─────────>│ AVNER │
- // ─────────>│ BEN │──────> Software Engineering Method
- // └───────┘
- // 10 Dov-Hoz st. Tel-Aviv 63416 Israel tel. 972-3-221535
-
- // The Screen NAVigator, ver 1.10 April 1990
- // Copyright (c) 1989 by Avner Ben
- // Snav is not, and never was, free software.
- // for conditions for use refer to file "copyrigh.txt"
-
- // The Screen Navigator is an object-oriented device-independent
- // character-graphics driver package, written in the C++ language,
- // distributed in the form of C++ source code.
- // For further information refer to the documentation files.
-
- // this simple example is intended as a template to be extended and modified
- // by the user, provided the above title and copyright notice are unchanged
- // and are not ommitted.
-
- /***************************************************************************/
-
- // demonstration part 2 - source code.
- // a simplistic formatted-input driver, implemented with the screen driver
- // of demo part 1. Device-specific assumptions made about function key
- // values. Implemented are only the methods required by the main demo program.
-
- // 28.10.89 avner ben coded.
- /////// snav v1.1
- // 8.3.90-7.4.90 avner ben:
- // * C++ v2.0 upgrade * modified some functions to return by value *
- // simplified class char_input using inheritance * default screen-driver
- // allocation * made class character_input generic * removed char-set
- // dependant tests to driver * optimized box redraw in text-window *
-
- // site history (this copy):
- // __.__.__, _____________: _______________________________.
-
- #include <conio.h>
- #include <ctype.h>
- #ifndef SNAV2_H
- #include "snav2.hpp"
- #endif
- #include "demo2.hpp"
-
- extern screen_driver_manager screen_driver;
- extern keyboard_driver_manager keyboard_driver;
-
- character_input :: character_input(panel *inscr)
- {
- private_screen=FALSE;
- scr=inscr;
- cc=' '; fkey=K_C;
- }
-
- character_input :: character_input(const square_pos &window, int driver_num)
- {
- private_screen=TRUE;
- scr=screen_driver.allocate(window,driver_num);
- cc=' '; fkey=K_C;
- }
-
- character_input :: ~character_input()
- {
- if (private_screen) delete scr;
- }
-
- boolean character_input :: get_position(char * prompt, panel *drawbd, boolean hilite)
- {
- if (*prompt)
- { clear(); scr->put_s(prompt); }
- drawbd->posit();
- while (get_c()!='\r') {
- if (cc=='\033')
- { clear(); return(FALSE); }
- if (fkey) switch (fkey) {
- case K_RT : drawbd->next(&(direction)rt); break;
- case K_UP : drawbd->next(&(direction)up); break;
- case K_LT : drawbd->next(&(direction)lt); break;
- case K_DN : drawbd->next(&(direction)dn); break;
- }
- }
- if (hilite) drawbd->put_attr(VD_REV);
- clear();
- return(TRUE);
- }
-
- int character_input :: get_number(char *prompt, int imax)
- {
- scr->put_s(prompt); next();
- int i=0; char c;
- while (isdigit(get_c()) || i==0) {
- if (cc=='\033') return(0);
- if (isdigit(cc) && i*10+charnum(cc)<=imax) {
- i=i*10+charnum(cc);
- scr->put_c(cc);
- if (i*10>imax) break;
- }
- }
- return(i);
- }
-
- direction character_input :: get_dir()
- {
- scr->put_s("direction (arrw):"); next();
- direction dir(NODIR);
- while (!dir()) {
- get_c();
- switch (fkey) {
- case K_RT : dir=rt; break;
- case K_UP : dir=up; break;
- case K_LT : dir=lt; break;
- case K_DN : dir=dn; break;
- }
- }
- scr->put_s(dir.name());
- return dir;
- }
-
- weight_d character_input :: get_wgt()
- {
- scr->put_s("weight (h:v):"); next();
- int i[2];
- for (int j=0; j<=1; j++) {
- put_c_stay('1');
- while (get_c()!='\r')
- if (cc>='0' && cc<='3') break;
- if (cc=='\r') cc='1';
- i[j]=charnum(cc);
- scr->put_c(cc);
- if (j<1) scr->put_c(':');
- }
- weight_d result(i[0],i[1]);
- return result;
- }
-
- boolean character_input :: get_boolean(char *prompt)
- {
- scr->put_s(prompt); next();
- put_c_stay('Y');
- for (get_c(); ; get_c())
- if (cc=='Y' || cc=='y' || cc=='\r') {
- scr->put_c('Y'); return(TRUE);
- } else if (cc=='N' || cc=='n') {
- scr->put_c('N'); return(FALSE);
- }
- }
-
- void character_input :: msg(char *prompt)
- {
- clear();
- scr->put_s(prompt); get_c();
- clear();
- }
-
- character_input *keyboard_driver_manager :: allocate(const square_pos &window,
- int scr_num, int kbd_num)
- {
- if (!kbd_num) kbd_num=default_kbd;
-
- switch (kbd_num) {
- case 1 : return new pc_keyboard((square_pos)window, scr_num);
- // insert here other drivers you may have...
- default: return new pc_keyboard((square_pos)window, scr_num);
- // installation default
- }
- }
-
- character_input *keyboard_driver_manager :: allocate(panel *scr, int kbd_num)
- {
- if (!kbd_num) kbd_num=default_kbd;
-
- switch (kbd_num) {
- case 1 : return new pc_keyboard(scr);
- // insert here other drivers you may have...
- default: return new pc_keyboard(scr);
- // installation default
- }
- }
-
- char pc_keyboard :: get_c()
- { // device-specific primitive
-
- if (!(cc=getch())) { // pc-specific scan-codes
- cc=' ';
- switch ((cc=getch())) {
- case 'M' : fkey=K_RT; break;
- case 'H' : fkey=K_UP; break;
- case 'K' : fkey=K_LT; break;
- case 'P' : fkey=K_DN; break;
- }
- } else fkey=K_C;
- return cc;
- }
-
-
- slide_option :: slide_option(char *text, int posopt, char *help)
- {
- s=new char[strlen(text)+1]; strcpy(s,text);
- hlp=new char[strlen(help)+1]; strcpy(hlp,help);
- // convert all but option char to lower-case
- if (posopt<1 || posopt>strlen(text)) posopt=1; posopt-=1;
- letter=s+posopt;
- for (int i=strlen(text)-1; i>=0; i--)
- if (i==posopt) s[i]=toupper(s[i]);
- else s[i]=tolower(s[i]);
- num=0;
- next=last=NULL;
- }
-
- slide_menu :: slide_menu(panel *parent, panel *help)
- { // chops top line off parent screen!
- scr=screen_driver.allocate(parent->ask_window());
- scr->set_limit(up,parent->ask_limit(up));
- parent->move_limit(up,-1);
- explain=help;
- stopt=enopt=cursor=NULL;
- scr->clear();
- }
-
- slide_menu :: ~slide_menu(void)
- {
- delete(scr);
- if (stopt) {
- slide_option *next=stopt->next;
- for (; stopt; stopt=next) {
- next=stopt->next;
- delete(stopt);
- }
- }
- }
-
- void slide_menu :: append_opt(char *text, int posopt, char *help)
- {
- if (!*text) return;
- enopt=new slide_option(text, posopt, help);
- if (!stopt) {
- cursor=stopt=enopt;
- enopt->pos=scr->ask_corner(up);
- scr->put_c('│',&(enopt->pos)); (enopt->pos).move(rt);
- enopt->num=1;
- } else {
- for (slide_option *opt0=stopt; opt0->next; opt0=opt0->next) ;
- opt0->next=enopt; enopt->last=opt0;
- enopt->num=opt0->num+1;
- enopt->pos=opt0->pos;
- (enopt->pos).move(rt,strlen(opt0->s)+1);
- }
- enopt->on(scr);
- point_pos pt=enopt->pos;
- pt.move(rt,strlen(enopt->s));
- scr->put_c('│',&pt);
- pt.move(rt);
- if (!scr->get_attr(VD_REV,&pt))
- while (scr->ask_legal(&pt)) {
- scr->put_attr(VD_REV,&pt); pt.move(rt);
- }
- }
-
- void slide_menu :: next_opt()
- {
- if (!stopt) return;
- cursor->on(scr);
- if (cursor->next) cursor=cursor->next;
- else cursor=stopt;
- cursor->off(scr); cursor->put_help(explain);
- }
-
- void slide_menu :: prev_opt()
- {
- if (!stopt) return;
- cursor->on(scr);
- if (cursor->last) cursor=cursor->last;
- else cursor=enopt;
- cursor->off(scr); cursor->put_help(explain);
- }
-
- int slide_menu :: select()
- {
- if (!stopt) return 0;
- if (!stopt->next) return 1;
- cursor->off(scr); cursor->put_help(explain);
- char c;
- while ((c=getch())!='\r') {
- if (c=='\033') return 0;
- else if (c=='\0') {
- c=getch();
- if (c=='M') next_opt(); // PC-specific fkey values
- else if (c=='K') prev_opt();
- } else {
- c=toupper(c);
- for (slide_option *opt=stopt; opt; opt=opt->next)
- if (*(opt->letter)==c) {
- cursor->on(scr);
- cursor=opt; cursor->off(scr);
- cursor->put_help(explain);
- break;
- }
- if (c==*(cursor->letter)) break; // auto select
- }
- }
- explain->clear();
- return cursor->num;
- }
-
- #include <stdio.h>
- text_window :: text_window(panel *parent, point_pos *pt0, char *title, boolean centered, char *inpline[])
- {
- point_pos start;
- if (pt0 && parent->ask_legal(pt0)) start=*pt0;
- else {
- start.sety(parent->ask_limit(up));
- start.setx(parent->ask_limit(lt));
- }
- int len=0;
- for (int numlns=0; inpline[numlns][0]; numlns++)
- if (strlen(inpline[numlns])>len)
- len=strlen(inpline[numlns]);
- if (len+start.x()-1+2>parent->ask_limit(rt))
- len=parent->ask_limit(rt)-start.x()+1-2;
- if (numlns+start.y()-1+2>parent->ask_limit(dn))
- len=parent->ask_limit(dn)-start.y()+1-2;
- box=screen_driver.allocate(square_pos(start,
- point_pos(start.y()+numlns-1+2,start.x()+len-1+2)));
- box->save();
- box->toggle_attr(VD_HI);
- box->enframe(h2v1,title,centered);
- box->toggle_attr(VD_HI);
- point_pos pos=box->ask_corner(up);
- for (int i=0; i<numlns; i++) {
- box->posit(&pos);
- box->put_s(inpline[i]);
- pos.move(dn);
- }
- }
-
- void text_window :: pause(char *prompt)
- {
- if (*prompt) {
- box->move_limit(dn,1);
- point_pos stpos(box->ask_limit(dn),box->ask_limit(lt));
- weight_d wgt=alph->cweight(box->get_c(&stpos));
- int col=box->ask_limit(rt)-strlen(prompt)+1;
- box->reset_color();
- if (col>=box->ask_limit(lt))
- box->put_s(prompt,&point_pos(box->ask_limit(dn),col));
- else box->put_s(prompt,&stpos);
- if (!getch()) getch();
- box->toggle_attr(VD_HI);
- box->arclist(&stpos,box->ask_len(hdim),rt,wgt);
- box->move_limit(dn,-1);
- box->toggle_attr(VD_HI);
- } else if (!getch()) getch();
- }
-
- void text_window :: put_page(char *inpline[])
- {
- box->clear();
- for (int numlns=0; inpline[numlns][0]; numlns++) ;
- point_pos pos(box->ask_limit(up),box->ask_limit(lt));
- for (int i=0; i<numlns; i++) {
- box->posit(&pos);
- box->put_s(inpline[i]);
- pos.move(dn);
- }
- }
-