home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / SNAV0111.ZIP / SNAV2.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-16  |  17.1 KB  |  485 lines

  1. //          ┌───────┐
  2. //    ─────────>│ AVNER │
  3. //    ─────────>│  BEN  │──────> Software Engineering Method
  4. //          └───────┘
  5. //    10 Dov-Hoz st. Tel-Aviv 63416 Israel tel. 972-3-221535
  6.  
  7. // The Screen NAVigator, ver 1.10 April 1990
  8. // Copyright (c) 1989 by Avner Ben
  9. // Snav is not, and never was, free software.
  10. // for conditions for use refer to file "copyrigh.txt"
  11.  
  12. // The Screen Navigator is an object-oriented device-independent
  13. // character-graphics driver package, written in the C++ language,
  14. // distributed in the form of C++ source code.
  15. // For further information refer to the documentation files.
  16.  
  17. // The user may not omit this text, from the beginning of the file, to
  18. // the line of asterisks below.
  19.  
  20. /***************************************************************************/
  21.  
  22. // package nucleus part 3 - headers.
  23. // This file defines the generic "panel" (formatted output medium),
  24. // and the generic "shape" (character-graphic element to be charted
  25. // on the former). Also defined is the archetypal shape "oblong". For
  26. // example of more specific shapes, refer to demo-3.
  27.  
  28. // History:
  29. // 25.10.89 avner ben coded.
  30. /////// snav v1.1
  31. // 22.1.90-11.4.90 avner ben:
  32.  
  33. // C++    v2.0  upgrade  *  added  to  class panel language indicator, color,
  34. // window * added  to panel data  redundancy with fix  mechanism, color and
  35. // cursor  processing  *  added  class    square_pos  *  renamed    class point
  36. // point_pos * renamed class curve stroke * * changed some  query-functions
  37. // to return by value * added default increment/decrement to class point  *
  38. // changed  some  point  functions  to    return    void  * defined transparent
  39. // screen-driver allocator * removed  some inline code to  implementation *
  40. // in  class  curve,  replaces    offset    with  pendn toggle * moved iterator
  41. // methods from curve to shape * added last-curve pointer to shape *  added
  42. // offset from base to class curve, for use by shape iterator * moved  some
  43. // function from implementation screen driver * removed specific  shape  to
  44. // snav3
  45.  
  46. // Site history (of this copy):
  47. // __.__.__ ____________ : __________________.
  48.  
  49. #ifndef SNAV2_H
  50. #define SNAV2_H
  51.  
  52. #include <string.h>
  53. #ifndef SNAV1_H
  54. #include "snav1.hpp"
  55. #endif
  56. #ifndef NULL
  57. #include <stdlib.h>
  58. #endif
  59.  
  60. class point_pos {
  61. // position on bi-dimensional panel
  62.  
  63.     friend class square_pos;
  64.     friend class panel;
  65.  
  66.     private :
  67.         int ypos, xpos,                     //position
  68.             yofst, xofst;        // ofst for the increment operator
  69.  
  70.     public :
  71.         point_pos(int y=1, int x=1)
  72.         { ypos=y; xpos=x; yofst=0; xofst=1; }
  73.         point_pos(int y, int x, const direction &dir);
  74.         point_pos(const point_pos &other)
  75.         {    ypos=other.ypos; xpos=other.xpos;
  76.             yofst=other.yofst; xofst=other.xofst; }
  77.         point_pos &operator=(const point_pos &other)
  78.         {    ypos=other.ypos; xpos=other.xpos;
  79.             yofst=other.yofst; xofst=other.xofst;
  80.             return *this; }
  81.         point_pos(const direction &dir, const point_pos &other);
  82.         void move(const direction &dir, int len=1);
  83.         // increment/decrement y/x positions as implied by direction
  84.         void chg_dir(const direction &dir, int increment=1);
  85.         // modify internal offset for ++ operator
  86.         void chg_dir(int yoffset, int xoffset)
  87.         // modify internal offset for ++ operator
  88.         { yofst=yoffset; xofst=xoffset; }
  89.         boolean operator==(const point_pos &other)
  90.         { return(ypos==other.ypos && xpos==other.xpos); }
  91.         boolean operator!=(const point_pos &other)
  92.         { return(ypos!=other.ypos || xpos!=other.xpos); }
  93.         int y(void) { return(ypos); }
  94.         int x(void) { return(xpos); }
  95.         void sety(int y) { ypos=y; }
  96.         void setx(int x) { xpos=x; }
  97.         point_pos &operator+=(const point_pos &offset);
  98.         // shift by user-specified offset
  99.         point_pos &operator-=(const point_pos &shifted);
  100.         // shift conter to user-specified offset
  101.         point_pos &operator++(void)
  102.         // shift by internally-held offset
  103.         { ypos+=yofst; xpos+=xofst; return(*this); }
  104.         point_pos &operator--(void)
  105.         // shift counter to internally-held offset
  106.         { ypos-=yofst; xpos-=xofst; return(*this); }
  107.         char *name(void);
  108.         int how_far(const direction &dir);
  109. };
  110.  
  111. class square_pos
  112. { // position measured by pair of extreme corners.
  113.  
  114.     private :
  115.         point_pos top,        // up-left corner
  116.               bot;        // right-down corner
  117.         int curlen, curwd;    // current dimensions
  118.  
  119.     public :
  120.         square_pos(const point_pos &start, const point_pos &end) {
  121.             top=start; bot=end;
  122.             curlen=bot.ypos-top.ypos+1;
  123.             curwd=bot.xpos-top.xpos+1;
  124.         }
  125.         void set_start(const point_pos &start);
  126.         void set_end(const point_pos &end);
  127.         void set_end_y(int len);
  128.         void set_end_x(int wd);
  129.         void move_limit(const direction &dir, int ofst=1);
  130.         void set_limit(const direction &dir, int pos);
  131.         void move(const direction &dir, int len=1);
  132.         int len(const axis &dim);
  133.         point_pos start(void) { return top; } // (temporary result)
  134.         point_pos end(void) { return bot; } // (temporary result)
  135.         int sty(void) { return top.ypos; }
  136.         int eny(void) { return bot.ypos; }
  137.         int stx(void) { return top.xpos; }
  138.         int enx(void) { return bot.xpos; }
  139.         char *name(void);
  140. };
  141.  
  142. enum vd_attr {
  143.     VD_OFF=0,                      //  "void" - leave alone
  144.     VD_HI=1,                  // to be ORed int an integer-set
  145.     VD_UNDLN=2,
  146.     VD_BLNK=4,
  147.     VD_REV=8,
  148.     VD_HID=16
  149. };
  150.  
  151. enum vd_clr { // pc oriented ordering
  152.     VD_BLK,
  153.     VD_BLU,
  154.     VD_GRN,
  155.     VD_CYN,
  156.     VD_RED,
  157.     VD_PNK,
  158.     VD_YLW,
  159.     VD_WHT
  160. };
  161.  
  162. struct color_ind
  163. { // visual attribute configuartion - ansi style
  164.  
  165.     int attr;                         // ORed combi
  166.     vd_clr backgnd, forgnd;
  167.     color_ind(int attrs=0, vd_clr for_color=VD_WHT, vd_clr back_color=VD_BLK)
  168.     { attr=attrs; backgnd=back_color; forgnd=for_color; }
  169.     void set_attr(int attrs) { attr=attrs; }
  170.     void attr_on(vd_attr at) { attr|=at; }
  171.     void attr_off(vd_attr at) { attr^=(attr&at); }
  172.     boolean ask_attr(vd_attr at) { return attr&at; }
  173.     void toggle_attr(vd_attr at);
  174.     void set_for_clr(vd_clr color);
  175.     void set_back_clr(vd_clr color);
  176.     vd_clr ask_for_clr(void);
  177.     vd_clr ask_back_clr(void);
  178.     void normalize(void);
  179.     boolean operator()(void);
  180.     boolean operator==(const color_ind &other);
  181.     boolean operator!=(const color_ind &other);
  182. };
  183.  
  184. class panel {
  185. // the generic screen object
  186.  
  187.     // abstract class for specific driver(s) to be implemented by user,
  188.     // using late binding.
  189.     // important: assumed are only console facilities - no keyboard input.
  190.     // 'get' and 'put' apply to the screen matrix storage!
  191.  
  192.     private :
  193.         boolean takes_graph;
  194.         boolean wrap_around;
  195.  
  196.     protected :
  197.         square_pos win;
  198.         point_pos cursor;
  199.         color_ind color, def_color;
  200.         int lang;          // pending language. user-managed values
  201.         direction curdir;           // current cursor direction
  202.         point_pos top, bot;               // added for efficiency
  203.         int hlen, vlen;                // added for efficiency
  204.  
  205.     public :
  206.         // note: arguments defaulting to NULL are requests from the
  207.         // driver to use (and advance) the built-in cursor.
  208.         // otherwise (arguments supplied) "direct cursor adressing"
  209.         // is requested (and built-in cursor is not affected).
  210.  
  211.         panel(square_pos *wind,color_ind *default_clr, direction *dir=NULL,
  212.          boolean ingraph=TRUE, boolean inwrap=FALSE);
  213.         ~panel(void);        // restores if needed, does not clear!
  214.  
  215.         // the following methods must be redefined by the user
  216.         // to suit the specific device:
  217.         virtual char get_c(point_pos *pt=NULL)=0;
  218.         // get char in specified/cursor position
  219.         virtual void put_c(char c, point_pos *pt=NULL, direction *dir=NULL)=0;
  220.         // put char in specified/cursor position,
  221.         // using pending color.
  222.         // if cursor position, cursor is advanced one step
  223.         // in specified/cursor direction.
  224.         // wrap-around practiced, if enabled
  225.         virtual void put_color(const color_ind &clr, point_pos *pt=NULL)=0;
  226.         // set color in specified/cursor position
  227.         virtual void put_attr(vd_attr at, point_pos *pt=NULL)=0;
  228.         // toggle attribute in specified/cursor position
  229.         virtual void put_background(vd_clr colornum, point_pos *pt=NULL)=0;
  230.         // set background color in specified/cursor position
  231.         virtual void put_forground(vd_clr colornum, point_pos *pt=NULL)=0;
  232.         // set forground color in specified/cursor position
  233.         virtual color_ind get_color(point_pos *pt=NULL)=0;
  234.         // retrieve color in specified/cursor position
  235.         virtual boolean get_attr(vd_attr at, point_pos *pt=NULL)=0;
  236.         // toggle attribute in specified/cursor position
  237.         virtual void posit(point_pos *pt=NULL)=0;
  238.         // reposition to specified/internal-cursor position
  239.         virtual void save(void)=0;
  240.         // save whole window content (not stackable!)
  241.         virtual void restore(void)=0;
  242.         // restore window to state of last save operation, delete buff
  243.         virtual unsigned int ask_cursor(void)=0;
  244.         // user-managed values
  245.         virtual void set_cursor(unsigned int cursor_type)=0;
  246.         // user-managed values
  247.         virtual unsigned int hide_cursor(void)=0;
  248.         // retruns current shape
  249.         virtual void fix(void);
  250.         // fix controlled data redundancy
  251.  
  252.         // the following are "macros" based on the above having been
  253.         // defined in the working object:
  254.         boolean ask_legal(point_pos *pt);
  255.         // tell if point_pos is within window range
  256.         // (default implementation supplied)
  257.         boolean next(direction *dir=NULL, point_pos *pt=NULL);
  258.         // move cursor/specified-point_pos one position
  259.         // in pending/specified direction, if movement possible
  260.         boolean wpnext(direction *dir=NULL, point_pos *pt=NULL);
  261.         // move cursor/specified-point_pos one position
  262.         // in pending/specified direction. wrap-around, if movement
  263.         // impossible (if enabled in panel setup)
  264.         char next_c(direction *dir=NULL, point_pos *pt=NULL);
  265.         // get neighbour of cursor/specified char,
  266.         // in pending/specified direction, no movement.
  267.         void clr_eol(point_pos *pt=NULL);
  268.         // clear from specified/cursor position to end-of-line
  269.         void clear(void);
  270.         // clear whole screen and posit home
  271.         void put_s(char *s, point_pos *pt=NULL, direction *dir=NULL);
  272.         // write a string on screen starting at cursor/specified point.
  273.         // if cursor, it  is advanced, with wrap-around if enabled.
  274.         // not cursor, wrap around not practiced.
  275.         void home(void);
  276.         // position to start corner of screen, depending on
  277.         // pending cursor direction.
  278.         int ask_limit(const direction &dir);
  279.         // beginning/end of window - height/breadth
  280.         void set_limit(const direction &dir, int pos);
  281.         // modify window dimensions
  282.         void move_limit(const direction &dir, int ofst=1);
  283.         // modify window dimensions, relative
  284.  
  285.         // full screen operations and navigation
  286.         void arclist(point_pos *pt, int len, const direction &dir,
  287.          const weight_d &wgt, boolean tipped=FALSE, color_ind *clr=NULL);
  288.         // the graphic primitive (simmilar to shape "arc").
  289.         // draw straight line from y:x in direction and weight
  290.         // specified, possibly tipped by arrowhead.
  291.         // semigraphics intersected are "welded", where possible.
  292.         // for "welding" logic, refer to computer_alphabet::carrw()
  293.         // in snav-1.
  294.         // warning: arc listing ignores cursor, and does not wrap!
  295.         void arcclr(point_pos *pt, int len, const direction &dir);
  296.         // clear space formerly occupied by arc
  297.         boolean twg_c(point_pos *pt);
  298.         // translate one typewriter graphic char to 1h1v semigraphic
  299.         // ("full screen" algorithm)
  300.         int twg(void);
  301.         // translate panel from typewriter graphics to 1h1v semigraphics
  302.         char *env(point_pos *pt=NULL);
  303.         // returns string made of current char, followed
  304.         // by the four sorrounding chars, in direction++ order.
  305.         intersection gowhere(point_pos *junction, intersection *result=NULL);
  306.         // directions of arcs available from specified point on screen
  307.         // (if point is currently occupied by a semigraphic). if result
  308.         // is null, one is allocated. based upon env().
  309.         // for association logic see alph::ccont() in snav-1.
  310.  
  311.         // management
  312.         void set_color(const color_ind &new_color);
  313.         color_ind ask_color(void);
  314.         color_ind ask_def_color(void);
  315.         virtual void toggle_attr(vd_attr at);
  316.         boolean ask_attr(vd_attr at);
  317.         void reset_color(void);
  318.         void set_def_color(const color_ind &clr);
  319.         void set_def_attr(vd_attr at);
  320.         void set_def_background(vd_clr color);
  321.         void set_def_forground(vd_clr color);
  322.         void paint_background(vd_clr colornum);
  323.         point_pos ask_where(void) { return cursor; }
  324.         boolean ask_grph(void);
  325.         boolean ask_wrap(void);
  326.         direction ask_dir(void) { return curdir; }
  327.         void set_dir(const direction &dir);
  328.         void turn(void);
  329.         int ask_len(const axis &dim);
  330.         int ask_lang(void);
  331.         void set_lang(int inlang);
  332.         point_pos ask_corner(const direction &dir);
  333.         // current screen window corner - either top-left or bot-right
  334.         square_pos ask_window(void);
  335.  
  336.         // misc
  337.         boolean enframe(const weight_d &wgt=h1v1, char *title="\0",
  338.          boolean centered=FALSE);
  339.         // draw oblong around panel and contract
  340.         void deframe(void);
  341.         // expand (checks nothing)
  342. } ;
  343.  
  344. class screen_driver_manager
  345. { // run-time panel-implementation selector
  346.  
  347.     private:
  348.         int default_screen;
  349.     public:
  350.         screen_driver_manager(int def=0);
  351.         int ask_default(void);
  352.         void set_default(int def);
  353.         panel *allocate(const square_pos &window, int typ=0);
  354.         // to be implemented by user, reflecting the local driver repertory.
  355.         // for example, see file "demo0.hpp"
  356. };
  357.  
  358. class arc
  359. { // the straight line shape - the basic graphic primitive
  360.  
  361.     protected :
  362.         int len;
  363.         direction dir;
  364.         weight_d wgt;        // only relevant axis considered
  365.         boolean tipped;     // tipped with arrowhead
  366.         color_ind *color;
  367.  
  368.     public    :
  369.         arc(int inlen, const direction &indir, const weight_d &inwgt=h1v1,
  370.          boolean intip=FALSE, color_ind *incolor=NULL)
  371.         { len=inlen; dir=indir; wgt=inwgt; tipped=intip; color=incolor; }
  372.         void list(panel *scr, point_pos *pt);
  373.         void clear(panel *scr, point_pos *pt);
  374.  
  375.         // info
  376.         int ask_len(void) { return len; }
  377.         direction ask_dir(void) { return dir; } // (temporary result)
  378.         weight_d ask_wgt(void) { return wgt; } // (temporary result)
  379.         boolean ask_tip(void) { return tipped; }
  380.         color_ind *ask_color(void) { return color; }
  381.  
  382.         // management
  383.         void contract(void);
  384.         void expand(void);
  385.         void turn(void);
  386.         void invert(void);
  387.         void toggle_tip(void);
  388.         void set_len(int inlen);
  389.         void set_dir(const direction &indir);
  390.         void set_wgt(const weight_d &inwgt);
  391.         void mask(const weight_d &other_wgt);
  392. } ;
  393.  
  394. class stroke : public arc
  395. { // arc as list member
  396.  
  397.     friend class shape;
  398.  
  399.     private :
  400.         stroke *next;
  401.         boolean pendn;
  402.         point_pos offset;               // used by "shape" iterator
  403.  
  404.     public :
  405.         stroke(arc *src, boolean pen_down=TRUE);
  406.         stroke(int inlen, const direction &indir, const weight_d &inwgt=h1v1,
  407.          boolean intip=FALSE, color_ind *incolor=NULL, boolean pendn=TRUE);
  408.         stroke *get_next(void) { return next; }
  409.         void toggle_pen(void);
  410.         boolean pen_is_down(void);
  411. } ;
  412.  
  413. class shape
  414. { // the generic shape
  415.   // not an "abstract class" (may also be defined and used as is)
  416.  
  417.     protected :
  418.         stroke *stsk, *ensk;                      // curve
  419.         char *sname;                      // name for info
  420.  
  421.     public:
  422.         shape(stroke *sk=NULL, char *inname="amorph");
  423.         virtual void append_stroke(stroke *sk);
  424.         void append_stroke(const point_pos &offset);
  425.         ~shape(void);
  426.         virtual void list(panel *scr, point_pos *pt, boolean stay=TRUE);
  427.         // warning: the generic shape lists only arcs pre-allocated!
  428.         // in a "regular" shape, the user should supply specific listing.
  429.         virtual void clear(panel *scr, point_pos *pt, boolean stay=TRUE);
  430.         // warning: the generic shape clears only arcs pre-allocated!
  431.         // in a "regular" shape, the user should supply specific clearing.
  432.         char *name(void) { return(sname); }
  433.         stroke *ask_start(void) { return stsk; }
  434. } ;
  435.  
  436. class tracing : public shape
  437. { // shape copied from the real world, stored as unnormalized sequential tracing
  438.   // a "procedural" class, implementing a shape + its imitializing process.
  439.   // once the constructor returns, the user has nothing to look for in the
  440.   // private members.
  441.  
  442.     private :
  443.         point_pos start;            // scan-start address
  444.         struct landmark {        // list of turn points (fifo)
  445.             point_pos pos;        // node address
  446.             intersection dirstart;    // all available directions
  447.             intersection dirleft;    // directions left unexplored
  448.             landmark *next;
  449.             landmark(point_pos *pt, intersection *avdir);
  450.             void proceed(point_pos *start, intersection *avdir,
  451.              direction *curdir);
  452.             // recommend next dirction left to go
  453.         } *stmark;
  454.  
  455.     public:
  456.         tracing(panel *scr, point_pos *cursor);
  457.         // fully automatic "probe constructor". if the like of a shape
  458.         // exists in the real world (screen), in the specified
  459.         // coordinates (cursor), then do your best to copy it.
  460.         // ambiguities are solved automatically, using direction++ order.
  461.         ~tracing(void);
  462.         landmark *append_stroke(panel *scr, point_pos *cursor, direction *curdir);
  463.         // one phase of the probe-constructor. read arc. return
  464.         // available directions at exit point_pos. cursor set to exit point.
  465.         landmark *mark(point_pos *cursor, intersection *avdir);
  466.         // locate node entry for cursor, append if not in list
  467. };
  468.  
  469. class oblong : public shape
  470. { // sample "regular shape"
  471.  
  472.     private :
  473.         int wd, hgt;
  474.         weight_d wgt;
  475.  
  476.     public :
  477.         oblong(int width, int height, const weight_d &wgt=h1v1);
  478.         void list (panel *scr, point_pos *pt);
  479.         // spcified is left up corner
  480.         void clear (panel *scr, point_pos *pt);
  481.         // spcified is left up corner
  482. } ;
  483.  
  484. #endif
  485.