home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / hp / 15253 < prev    next >
Encoding:
Text File  |  1993-01-24  |  136.9 KB  |  4,908 lines

  1. Newsgroups: comp.sys.hp
  2. Path: sparky!uunet!cs.utexas.edu!torn!watserv2.uwaterloo.ca!maxwell.uwaterloo.ca!gordon
  3. From: gordon@maxwell.uwaterloo.ca (Gordon R. Strachan)
  4. Subject: XControl program and terminal emulator 1/4
  5. Message-ID: <C1CAL4.FJA@watserv2.uwaterloo.ca>
  6. Sender: news@watserv2.uwaterloo.ca
  7. Reply-To: gordon@maxwell.uwaterloo.ca (Gordon R. Strachan)
  8. Organization: University of Waterloo
  9. Date: Sun, 24 Jan 1993 03:48:40 GMT
  10. Lines: 4896
  11.  
  12.  
  13. # This is a shell archive.  Remove anything before this line,
  14. # then unpack it by saving it in a file and typing "sh file".
  15. #
  16. # Wrapped by Gordon R. Strachan <gordon@maxwell> on Sat Jan 23 22:18:42 1993
  17. #
  18. # This archive contains:
  19. #    GenTerm            Makefile        
  20. #    README            xcontrol        
  21. #    GenTerm/GenTerm.c    GenTerm/GenTerm.h    
  22. #    GenTerm/GenTermEm.h    GenTerm/GenTermEmP.h    
  23. #    GenTerm/GenTermP.h    
  24. #
  25. # Error checking via wc(1) will be performed.
  26.  
  27. LANG=""; export LANG
  28. PATH=/bin:/usr/bin:$PATH; export PATH
  29.  
  30. echo mkdir - GenTerm
  31. mkdir GenTerm
  32.  
  33. chmod 755 GenTerm
  34.  
  35. echo x - Makefile
  36. cat >Makefile <<'@EOF'
  37. #  Makefile for whole package
  38.  
  39. #Directory you want the parser files saved
  40. PARSEDIR=/usr/local/lib/GenTerm
  41.  
  42. #Directory you want library to be put in
  43. LIBDIR=/usr/local/lib
  44.  
  45. #Directory you want man pages put in
  46. MANDIR=/usr/local/man
  47.  
  48. #Directory to install include files
  49. INCDIR=/usr/local/include
  50.  
  51. #Directory to put executable
  52. BINDIR=/usr/local/bin
  53.  
  54. DIRS=PARSEDIR=$(PARSEDIR) LIBDIR=$(LIBDIR) MANDIR=$(MANDIR) INCDIR=$(INCDIR) BINDIR=$(BINDIR)
  55.  
  56. all :
  57.     (cd GenTerm; make $(DIRS) lib)
  58.     (cd xcontrol; make $(DIRS))
  59.  
  60. install:
  61.     (cd GenTerm; make $(DIRS) install)
  62.     (cd xcontrol; make $(DIRS) install)
  63.  
  64. installall:
  65.     (cd GenTerm; make $(DIRS) installall)
  66.     (cd xcontrol; make $(DIRS) install)
  67.  
  68. clean:
  69.     (cd GenTerm; make clean)
  70.     (cd xcontrol; make clean)
  71. @EOF
  72. set `wc -lwc <Makefile`
  73. if test $1$2$3 != 3488741
  74. then
  75.     echo ERROR: wc results of Makefile are $* should be 34 88 741
  76. fi
  77.  
  78. chmod 644 Makefile
  79.  
  80. echo x - README
  81. cat >README <<'@EOF'
  82. This package contains all the code necessary to build the XControl program.
  83. XControl is program which provides X window services to programs which where
  84. not written to interact with the X environment.  It provides a terminal
  85. emulator, much like an xterm or hpterm and will also inform the client
  86. program of other X events and allow it to take appropriate actions.  An
  87. example is when the user closes the window, the child program should not simply
  88. be killed (as hpterm would do) but instead should be told to orderly shut
  89. down and save itself.
  90.  
  91. An example program, called XVi in included.  It provides a simple X interface
  92. to the vi program.  Also, an action database is included to allow the vue
  93. filemanager to call this program whenever the edit action is requested.
  94.  
  95. The directory xcontrol contains the source code for the XControl program.
  96.  
  97. The directory GenTerm contains the code for the terminal emulation widget.
  98. This is a stand alone widget which provides a complete terminal emulator
  99. similar to the xterm and hpterm programs.  It can be installed seperately
  100. and used in other programs.
  101.  
  102. BUILDING THE PROGRAM
  103.  
  104. First:
  105.     Edit the Makefile in this directory to set the path names of where you
  106. want everything to be put.
  107.  
  108. Second:
  109.     Type "make"  is this directory to build the widget library and the program.
  110.  
  111. Third:
  112.     Type "make install" to install the XControl and XVi programs.  All required
  113. programs libraries and application default files will be installed.
  114.  
  115. Fourth:
  116.     If you want to install the terminal emulator widget library then type
  117. "make installall".
  118.  
  119.  
  120. Send any bugs reports or comments to gordon@maxwell.uwaterloo.ca
  121. @EOF
  122. set `wc -lwc <README`
  123. if test $1$2$3 != 392651641
  124. then
  125.     echo ERROR: wc results of README are $* should be 39 265 1641
  126. fi
  127.  
  128. chmod 644 README
  129.  
  130. echo mkdir - xcontrol
  131. mkdir xcontrol
  132.  
  133. chmod 755 xcontrol
  134.  
  135. echo x - GenTerm/GenTerm.c
  136. cat >GenTerm/GenTerm.c <<'@EOF'
  137. /* GenTerm -- A generalized terminal widget */
  138. /* History:                                 */
  139. /*         Written by G. R. Strachan 1992 */
  140.  
  141. /*  Copyright Gordon R. Strachan 1992 */
  142. /*  This code is provided as is, neither the University of Waterloo nor */
  143. /*  the author is liable for any damage caused by the use or misuse of this */
  144. /*  code.  */
  145.  
  146. /* Permission is granted to copy, use and modify this code provided it is */
  147. /* not sold for profit and the above copyright remains intact. */
  148.  
  149. #include <stdio.h>
  150. #include <ctype.h>
  151. #include <X11/X.h>
  152. #include <X11/Intrinsic.h>
  153. #include <X11/StringDefs.h>
  154. #include <X11/IntrinsicP.h>
  155. #include <X11/CoreP.h>
  156. #include <X11/Core.h>
  157. #include <X11/CompositeP.h>
  158. #include <X11/Composite.h>
  159. #include <X11/Shell.h>
  160. #include <X11/Xatom.h>
  161. /* sigh... HP doesn't distribute this include file so use it locally */
  162. #include "Xmu.h"
  163. /*#include <X11/Xmu/Xmu.h>*/
  164. #include "GenTermP.h"
  165. #include "GenTerm.h"
  166.  
  167. #define MIN(a,b) ((a)<(b)?(a):(b))
  168.  
  169. static XtResource resources[] = {
  170.  {XtNforeground,XtCForeground,XtRPixel,sizeof(Pixel),
  171.    XtOffset(GenTermWidget,genTerm.NormalForeground),XtRString,
  172.    XtDefaultForeground},
  173.  {XtNbackground,XtCBackground,XtRPixel,sizeof(Pixel),
  174.    XtOffset(GenTermWidget,genTerm.NormalBackground),XtRString,
  175.    XtDefaultBackground},
  176.  {XtNkbdCallback,XtCKbdCallback,XtRCallback,sizeof(caddr_t),
  177.    XtOffset(GenTermWidget,genTerm.kbdcallback),XtRCallback,NULL},
  178.  {XtNtopCallback,XtCTopCallback,XtRCallback,sizeof(caddr_t),
  179.    XtOffset(GenTermWidget,genTerm.topcallback),XtRCallback,NULL},
  180.  {XtNresizeCallback,XtCResizeCallback,XtRCallback,sizeof(caddr_t),
  181.    XtOffset(GenTermWidget,genTerm.resizecallback),XtRCallback,NULL},
  182.  {XtNlockCallback,XtCLockCallback,XtRCallback,sizeof(caddr_t),
  183.    XtOffset(GenTermWidget,genTerm.lockcallback),XtRCallback,NULL},
  184.  {XtNwidth,XtCWidth,XtRDimension,sizeof(Dimension),
  185.    XtOffset(GenTermWidget,core.width),XtRImmediate,(caddr_t)(7*80)},
  186.  {XtNheight,XtCHeight,XtRDimension,sizeof(Dimension),
  187.    XtOffset(GenTermWidget,core.height),XtRImmediate,(caddr_t)(13*24)},
  188.  {XtNbottomMargin,XtCBottomMargin,XtRDimension,sizeof(Dimension),
  189.    XtOffset(GenTermWidget,genTerm.BottomMargin),XtRImmediate,(caddr_t)0},
  190.  {XtNrows,XtCRows,XtRInt,sizeof(int),XtOffset(GenTermWidget,genTerm.rows),
  191.    XtRImmediate,(caddr_t)24},
  192.  {XtNcolumns,XtCColumns,XtRInt,sizeof(int),
  193.    XtOffset(GenTermWidget,genTerm.columns),XtRImmediate,(caddr_t)80},
  194.  {XtNbaseFont,XtCBaseFont,XtRString,sizeof(char *),
  195.    XtOffset(GenTermWidget,genTerm.Font),XtRString,"7x13"},
  196.  {XtNalternateFont,XtCAlternateFont,XtRString,sizeof(char *),
  197.    XtOffset(GenTermWidget,genTerm.AlternateFont),XtRString,"7x13bold"},
  198.  {XtNcharacterWidth,XtCCharacterWidth,XtRInt,sizeof(int),
  199.    XtOffset(GenTermWidget,genTerm.CharWidth),XtRImmediate,(caddr_t)7},
  200.  {XtNcharacterHeight,XtCCharacterHeight,XtRInt,sizeof(int),
  201.    XtOffset(GenTermWidget,genTerm.CharHeight),XtRImmediate,(caddr_t)13},
  202.  {XtNautoLineWrap,XtCAutoLineWrap,XtRBoolean,sizeof(Boolean),
  203.    XtOffset(GenTermWidget,genTerm.AutoWrap),XtRImmediate,FALSE},
  204.  {XtNliteralMode,XtCLiteralMode,XtRBoolean,sizeof(Boolean),
  205.    XtOffset(GenTermWidget,genTerm.LiteralMode),XtRImmediate,(caddr_t)FALSE},
  206.  {XtNcursorColor,XtCCursorColor,XtRPixel,sizeof(Pixel),
  207.    XtOffset(GenTermWidget,genTerm.CursorColour),XtRString,"orange"},
  208.  {XtNshowCursor,XtCShowCursor,XtRBoolean,sizeof(Boolean),
  209.    XtOffset(GenTermWidget,genTerm.ShowCursor),XtRImmediate,(caddr_t)True},
  210.  {XtNsaveLines,XtCSaveLines,XtRInt,sizeof(int),
  211.    XtOffset(GenTermWidget,genTerm.SaveLines),XtRImmediate,(caddr_t)512},
  212.  {XtNcursorFloats,XtCCursorFloats,XtRBoolean,sizeof(Boolean),
  213.    XtOffset(GenTermWidget,genTerm.CursorFloats),XtRImmediate,(caddr_t)TRUE},
  214.  {XtNfieldAttributes,XtCFieldAttributes,XtRBoolean,sizeof(Boolean),
  215.    XtOffset(GenTermWidget,genTerm.FieldAttributes),XtRImmediate,
  216.    (caddr_t)FALSE},
  217.  {XtNscrollDownClear,XtCScrollDownClear,XtRBoolean,sizeof(Boolean),
  218.    XtOffset(GenTermWidget,genTerm.ScrollDownClear),XtRImmediate,
  219.    (caddr_t)FALSE},
  220.  {XtNfullScroll,XtCFullScroll,XtRBoolean,sizeof(Boolean),
  221.    XtOffset(GenTermWidget,genTerm.FullScroll),XtRImmediate,(caddr_t)FALSE},
  222.  {XtNinsertMode,XtCInsertMode,XtRBoolean,sizeof(Boolean),
  223.    XtOffset(GenTermWidget,genTerm.InsertMode),XtRImmediate,(caddr_t)FALSE},
  224.  {XtNsaveScrollRegion,XtCSaveScrollRegion,XtRBoolean,sizeof(Boolean),
  225.    XtOffset(GenTermWidget,genTerm.SaveScrollRegion),XtRImmediate,
  226.    (caddr_t)FALSE},
  227.  {XtNblinkRate,XtCBlinkRate,XtRInt,sizeof(int),
  228.    XtOffset(GenTermWidget,genTerm.BlinkRate),XtRImmediate,(caddr_t)500},
  229.  {XtNnumberColors,XtCNumberColors,XtRInt,sizeof(int),
  230.    XtOffset(GenTermWidget,genTerm.NumberColours),XtRImmediate,(caddr_t)8},
  231.  {XtNpenColors,XtCPenColors,XtRString,sizeof(char *),
  232.    XtOffset(GenTermWidget,genTerm.PenColours),XtRString,"Default:Default"},
  233.  {XtNdefineColor,XtCDefineColor,XtRInt,sizeof(int),
  234.    XtOffset(GenTermWidget,genTerm.DefineColour),XtRImmediate,(caddr_t)GTRGB},
  235.  {XtNscrollOnOutput,XtCScrollOnOutput,XtRBoolean,sizeof(Boolean),
  236.    XtOffset(GenTermWidget,genTerm.ScrollOnOutput),XtRImmediate,(caddr_t)FALSE},
  237.  {XtNcursorKeyScrollRegion,XtCCursorKeyScrollRegion,XtRBoolean,sizeof(Boolean),
  238.    XtOffset(GenTermWidget,genTerm.CursorKeyScrollRegion),XtRImmediate,
  239.    (caddr_t)FALSE},
  240.  {XtNmarginBell,XtCMarginBell,XtRBoolean,sizeof(Boolean),
  241.    XtOffset(GenTermWidget,genTerm.MarginBell),XtRImmediate,(caddr_t)False},
  242.  {XtNmargin,XtCMargin,XtRInt,sizeof(int),
  243.    XtOffset(GenTermWidget,genTerm.Margin),XtRImmediate,(caddr_t)8},
  244.  {XtNeolStick,XtCEolStick,XtRBoolean,sizeof(Boolean),
  245.    XtOffset(GenTermWidget,genTerm.EOLStick),XtRImmediate,(caddr_t)False},
  246.  {XtNlockScreen,XtCLockScreen,XtRBoolean,sizeof(Boolean),
  247.    XtOffset(GenTermWidget,genTerm.Locked),XtRImmediate,(caddr_t)False},
  248.  {XtNignoreNull,XtCIgnoreNull,XtRBoolean,sizeof(Boolean),
  249.    XtOffset(GenTermWidget,genTerm.IgnoreNull),XtRImmediate,(caddr_t)False},
  250. };
  251.  
  252. static void GtInput();
  253. int GtOutput();
  254. static void GtOutputAction();
  255. static void GtExpose();
  256. static void GtNoExpose();
  257. static void GtInitialize();
  258. static void GtRealize();
  259. static void GtDestroy();
  260. static void GtResize();
  261. static void GtRedisplay();
  262. static void GtUpdateRegion();
  263. static Boolean GtSetValues();
  264. static void GtAllocateScreen();
  265. static void GtStoreString();
  266. static void GtShowLine();
  267. static void GtShowCursor();
  268. static void GtUnShowCursor();
  269. static void GtCheckPosition();
  270. static void GtNextLine();
  271. static void GtScrollScreen();
  272. static void GtDoTopCallback();
  273. static void GtSetFonts();
  274. static void GtCreatePens();
  275. static void GtSetupPenColours();
  276. static void GtMoveScreen();
  277. static int GtCheckValidPosition();
  278. static int GtScrollRegionScroll();
  279. static int GtScrollRegionScrollScreen();
  280. static void GtBlinkCallBack();
  281. static void GtDefinePen();
  282. static void GtDefineHalfBrightPen();
  283. static void GtHSLtoRGB();
  284. static void GtRGBtoHSL();
  285. static void GtReallocScreen();
  286. static void GtStartSelection();
  287. static void GtExtendSelection();
  288. static void GtMakeSelection();
  289. static void GtSortStartEnd();
  290. static char *GtCopySection();
  291. static Boolean GtConvertProc();
  292. static void GtLoseSelection();
  293. static void GtInsertSelection();
  294. static void GtInsertCallback();
  295. static void GtHighLightSection();
  296. static void GtDoTopCallback();
  297. static void GtDoLockCallback();
  298. static void GtUnHighLightSection();
  299. static void GtChangeFonts();
  300. static void GtChangeCursorColour();
  301. static void GtChangeColour();
  302. static void GtChangePen();
  303. static void GtDestroyPenColours();
  304.  
  305. static XtActionsRec actions[] = {
  306.  {"input",GtInput},
  307.  {"expose",GtExpose},
  308.  {"noexpose",GtNoExpose},
  309.  {"output",GtOutputAction},
  310.  {"StartSelection",GtStartSelection},
  311.  {"ExtendSelection",GtExtendSelection},
  312.  {"MakeSelection",GtMakeSelection},
  313.  {"InsertSelection",GtInsertSelection},
  314. };
  315.  
  316. static char translations[] =
  317. "\
  318. <KeyPress>:input()\n\
  319. <Expose>:expose()\n\
  320. <GraphicsExpose>:expose()\n\
  321. <NoExpose>:noexpose()\n\
  322. <Btn1Down>:StartSelection()\n\
  323. <Btn1Motion>:ExtendSelection()\n\
  324. <Btn1Up>:MakeSelection(PRIMARY,CUT_BUFFER0)\n\
  325. <Btn2Up>:InsertSelection(PRIMARY)\n\
  326. ";
  327.  
  328. GenTermClassRec genTermClassRec =
  329. {
  330. {  /* core fields */
  331. /* superclass               */      (WidgetClass) &compositeClassRec,
  332. /* class_name               */      "GenTerm",
  333. /* widget_size              */      sizeof(GenTermRec),
  334. /* class_initialize         */      NULL,
  335. /* class_part_initialize    */      NULL,
  336. /* class_inited             */      FALSE,
  337. /* initialize               */      GtInitialize,
  338. /* initialize_hook          */      NULL,
  339. /* realize                  */      GtRealize,
  340. /* actions                  */      actions,
  341. /* num_actions              */      XtNumber(actions),
  342. /* resources                */      resources,
  343. /* num_resources            */      XtNumber(resources),
  344. /* xrm_class                */      NULLQUARK,
  345. /* compress_motion          */      TRUE,
  346. /* compress_exposure        */      FALSE,
  347. /* compress_enterleave      */      TRUE,
  348. /* visible_interest         */      FALSE,
  349. /* destroy                  */      GtDestroy,
  350. /* resize                   */      GtResize,
  351. /* expose                   */      GtRedisplay,
  352. /* set_values               */      GtSetValues,
  353. /* set_values_hook          */      NULL,
  354. /* set_values_almost        */      XtInheritSetValuesAlmost,
  355. /* get_values_hook          */      NULL,
  356. /* accept_focus             */      XtInheritAcceptFocus,
  357. /* version                  */      XtVersion,
  358. /* callback_private         */      NULL,
  359. /* tm_table                 */      translations,
  360. /* query_geometry           */      XtInheritQueryGeometry,
  361. /* display_accelerator      */      XtInheritDisplayAccelerator,
  362. /* extension                */      NULL
  363. },
  364. { /* composite_class fields */
  365. /* geometry_manager   */   XtInheritGeometryManager,
  366. /* change_managed     */   XtInheritChangeManaged,
  367. /* insert_child       */   XtInheritInsertChild,
  368. /* delete_child       */   XtInheritDeleteChild,
  369. /* extension          */   NULL
  370. },
  371. { 0}
  372. };
  373.  
  374.  
  375. WidgetClass genTermWidgetClass = (WidgetClass) &genTermClassRec;
  376.  
  377. /* GtInitialize:  This is the initialization method for the widget instances */
  378. /* it basically just loads the fonts.  Most of the real work is done in the */
  379. /* realize method    */
  380.  
  381. static void GtInitialize(request,new)
  382.  
  383. GenTermWidget request,new;
  384.  
  385. {
  386.  int val;
  387.  
  388.  val = GtLoadFonts(new);
  389.  if(val == 0)
  390.   XtAppError(XtWidgetToApplicationContext(new),
  391.          "GenTerm Widget: Can't load fonts!");
  392.  else if(val == 2)
  393.   XtAppError(XtWidgetToApplicationContext(new),
  394.          "GenTerm Widget: Alternate font is different size than base font");
  395.  
  396.  if(new->genTerm.SaveLines < new->genTerm.rows)
  397.   XtAppError(XtWidgetToApplicationContext(new),
  398.          "GenTerm Widget: Savelines can not be less than rows");
  399.  new->genTerm.BlinkOn = -1;
  400.  new->genTerm.ColourMap = DefaultColormap(XtDisplay(new),
  401.                       DefaultScreen(XtDisplay(new)));
  402.  new->genTerm.ExposureCount = 0;
  403.  new->genTerm.Locked = False;
  404.  XmuInternAtom(XtDisplay(new),XmuMakeAtom("NULL"));
  405. }
  406.  
  407. /* GtLoadFonts:  This function loads the required fonts for the widget */
  408.  
  409. static int GtLoadFonts(new)
  410.  
  411. GenTermWidget new;
  412.  
  413.  if((new->genTerm.CurrentFont = XLoadQueryFont(XtDisplay(new),
  414.                            new->genTerm.Font)) != NULL)
  415.   {
  416.    new->genTerm.CharHeight = new->genTerm.CurrentFont->ascent +
  417.     new->genTerm.CurrentFont->descent;
  418.    new->genTerm.CharWidth = XTextWidth(new->genTerm.CurrentFont,"M",1);
  419.   }
  420.  else
  421.   return(0);
  422.  if((new->genTerm.AltFont = XLoadQueryFont(XtDisplay(new),
  423.                        new->genTerm.AlternateFont)) != NULL)
  424.   {
  425.    if((new->genTerm.AltFont->ascent + new->genTerm.AltFont->descent 
  426.        != new->genTerm.CharHeight) ||
  427.       (XTextWidth(new->genTerm.AltFont,"M",1) != new->genTerm.CharWidth))
  428.     return(2);
  429.   }
  430.  else
  431.   return(0);
  432.  return(1);
  433. }
  434.  
  435. /* GtRealize:  This is the realization method for the widget instance. */
  436. /*   It calculates the screen size, initializes the screen data structure */
  437. /*   sets up the default attributes and initializes all private variables */
  438.  
  439. static void GtRealize(w,valueMask,attributes)
  440.  
  441. GenTermWidget w;
  442. XtValueMask *valueMask;
  443. XSetWindowAttributes *attributes;
  444.  
  445. {
  446.  Arg args[20];
  447.  int n = 0;
  448.  WidgetClass superclass;
  449.  int npens;
  450.  int i;
  451.  
  452.  superclass = (WidgetClass) NULL;
  453.  superclass = (WidgetClass)compositeWidgetClass;
  454.  (*superclass->core_class.realize)(w,valueMask,attributes);
  455.  
  456.  n = 0;
  457.  XtSetArg(args[n],XtNwidthInc,w->genTerm.CharWidth); n++;
  458.  XtSetArg(args[n],XtNheightInc,w->genTerm.CharHeight); n++;
  459.  XtSetValues(w,args,n);
  460.  
  461.  GtSetupPenColours(w);
  462.  GtDefinePen(w,0);
  463.  
  464.  w->genTerm.foreground = w->genTerm.CurrentForeground =
  465.   w->genTerm.Colours[0].Foreground;
  466.  w->genTerm.background = w->genTerm.CurrentBackground =
  467.   w->genTerm.Colours[0].Background;
  468.  w->genTerm.DefaultSettings.Font = w->genTerm.CurrentFont;
  469.  w->genTerm.DefaultSettings.type = 0;
  470.  memcpy(&(w->genTerm.CurrentSettings),&(w->genTerm.DefaultSettings),
  471.     sizeof(struct Attribute));
  472.  
  473.  npens = w->genTerm.NumberColours << NUMATTRIBUTES;
  474.  
  475.  w->genTerm.Pens = (struct GraphicsContexts **) 
  476.   XtMalloc(sizeof(struct GraphicsContexts *) * npens);
  477.  for(i = 0; i < npens; i++)
  478.   w->genTerm.Pens[i] = NULL;
  479.  
  480.  GtCreatePens(w,0);
  481.  GtAllocateScreen(w);
  482.  
  483.  w->genTerm.CursorOn = FALSE;
  484.  
  485.  w->genTerm.CurrentRow = 0;
  486.  w->genTerm.CurrentColumn = 0;
  487.  w->genTerm.ScreenTop = 0;
  488.  w->genTerm.VScreenTop = 0;
  489.  w->genTerm.VirtualTop = 0;
  490.  w->genTerm.MaxVirtualTop = 0;
  491.  w->genTerm.CursorOffset = 0;
  492.  w->genTerm.CurrentNumRows = w->genTerm.rows;
  493.  w->genTerm.CurrentNumColumns = w->genTerm.columns;
  494.  w->genTerm.CurrentSaveLines = w->genTerm.SaveLines;
  495.  w->genTerm.CursorIsStuck = False;
  496.  w->genTerm.SelectionStarted = False;
  497.  w->genTerm.CurrentSelection = NULL;
  498.  
  499.  /* setup the default mapping table */
  500.  
  501.  for(i = 0; i < ' '; i++)
  502.   w->genTerm.MapTable[i] = ' ';
  503.  for(i = ' ' + 1; i < 128; i++)
  504.   w->genTerm.MapTable[i] = i;
  505.  for(i = 128; i < NMAP; i++)
  506.   w->genTerm.MapTable[i] = ' ';
  507. }
  508.  
  509. /* GtCreatePens:  This function builds the graphics contexts needed to */
  510. /*   implement the current attributes.  The graphics contexts are stored */
  511. /*   in the lookup table at position num */
  512.  
  513. static void GtCreatePens(w,num)
  514.  
  515. GenTermWidget w;
  516. int num;
  517.  
  518. {
  519.  XGCValues values;
  520.  int HighLight = num | HIGHLIGHT;
  521.  
  522.  w->genTerm.Pens[num] = (struct GraphicsContexts *)
  523.   XtMalloc(sizeof(struct GraphicsContexts));
  524.  
  525.  w->genTerm.Pens[HighLight] = (struct GraphicsContexts *)
  526.   XtMalloc(sizeof(struct GraphicsContexts));
  527.  
  528.  values.graphics_exposures = TRUE;
  529.  values.foreground = w->genTerm.foreground;
  530.  values.background = w->genTerm.background;
  531.  w->genTerm.Pens[num]->draw_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  532.                        GCBackground | GCForeground | 
  533.                        GCGraphicsExposures,&values);
  534.  XSetFont(XtDisplay(w),w->genTerm.Pens[num]->draw_gc,
  535.       w->genTerm.CurrentSettings.Font->fid);
  536.  
  537.  values.graphics_exposures = TRUE;
  538.  values.foreground = w->genTerm.background;
  539.  values.background = w->genTerm.foreground;
  540.  w->genTerm.Pens[HighLight]->draw_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  541.                          GCBackground | GCForeground |
  542.                          GCGraphicsExposures,&values);
  543.  XSetFont(XtDisplay(w),w->genTerm.Pens[HighLight]->draw_gc,
  544.       w->genTerm.CurrentSettings.Font->fid);
  545.  
  546.  values.graphics_exposures = FALSE;
  547.  values.foreground = w->genTerm.foreground;
  548.  values.background = w->genTerm.CursorColour;
  549.  w->genTerm.Pens[num]->cursor_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  550.                          GCBackground | GCForeground |
  551.                          GCGraphicsExposures,&values);
  552.  XSetFont(XtDisplay(w),w->genTerm.Pens[num]->cursor_gc,
  553.       w->genTerm.CurrentSettings.Font->fid);
  554.  
  555.  values.graphics_exposures = FALSE;
  556.  values.foreground = w->genTerm.background;
  557.  values.background = w->genTerm.CursorColour;
  558.  w->genTerm.Pens[HighLight]->cursor_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  559.                            GCBackground | GCForeground |
  560.                            GCGraphicsExposures,&values);
  561.  XSetFont(XtDisplay(w),w->genTerm.Pens[HighLight]->cursor_gc,
  562.       w->genTerm.CurrentSettings.Font->fid);
  563.  
  564.  values.graphics_exposures = FALSE;
  565.  values.foreground = w->genTerm.background;
  566.  values.background = w->genTerm.background;
  567.  w->genTerm.Pens[num]->blink_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  568.                         GCBackground | GCForeground |
  569.                         GCGraphicsExposures,&values);
  570.  XSetFont(XtDisplay(w),w->genTerm.Pens[num]->blink_gc,
  571.       w->genTerm.CurrentSettings.Font->fid);
  572.  
  573.  values.graphics_exposures = FALSE;
  574.  values.foreground = w->genTerm.foreground;
  575.  values.background = w->genTerm.background;
  576.  w->genTerm.Pens[HighLight]->blink_gc = XCreateGC(XtDisplay(w),XtWindow(w),
  577.                           GCBackground | GCForeground |
  578.                           GCGraphicsExposures,&values);
  579.  XSetFont(XtDisplay(w),w->genTerm.Pens[HighLight]->blink_gc,
  580.       w->genTerm.CurrentSettings.Font->fid);
  581. }
  582.  
  583. /* GtSetupPenColours(w):  This function allocates and initializes the pen */
  584. /*  colours structure.  */
  585.  
  586. static void GtSetupPenColours(w)
  587.  
  588. GenTermWidget w;
  589.  
  590. {
  591.  int i,j,k;
  592.  char *Colours = w->genTerm.PenColours;
  593.  char ColourName[100];
  594.  
  595.  w->genTerm.Colours = (struct PenColour *) XtMalloc(sizeof(struct PenColour) *
  596.                             w->genTerm.NumberColours);
  597.  
  598.  for(i = 0; i < w->genTerm.NumberColours; i++)
  599.   {
  600.    w->genTerm.Colours[i].defined = 0;
  601.    w->genTerm.Colours[i].ForegroundName = XtMalloc(sizeof(char) * 8);
  602.    w->genTerm.Colours[i].BackgroundName = XtMalloc(sizeof(char) * 8);
  603.    strcpy(w->genTerm.Colours[i].ForegroundName,"Default");
  604.    strcpy(w->genTerm.Colours[i].BackgroundName,"Default");
  605.   }
  606.  
  607.  /* we now parse the colour list we were given */
  608.  
  609.  i = j = 0;
  610.  while(*Colours != NULL)
  611.   {
  612.    if(i >= w->genTerm.NumberColours)
  613.     break;
  614.    k = 0;
  615.    while((Colours[k] != ':') && (Colours[k] != NULL))
  616.     ColourName[k++] = Colours[k];
  617.    ColourName[k] = NULL;
  618.    if(j == 0)
  619.     {
  620.      XtFree(w->genTerm.Colours[i].ForegroundName);
  621.      w->genTerm.Colours[i].ForegroundName = XtMalloc(sizeof(char) * 
  622.                              strlen(ColourName) + 1);
  623.      strcpy(w->genTerm.Colours[i].ForegroundName,ColourName);
  624.      j = 1;
  625.     }
  626.    else
  627.     {
  628.      XtFree(w->genTerm.Colours[i].BackgroundName);
  629.      w->genTerm.Colours[i].BackgroundName = XtMalloc(sizeof(char) *
  630.                              strlen(ColourName) + 1);
  631.      strcpy(w->genTerm.Colours[i].BackgroundName,ColourName);
  632.      j = 0;
  633.      i++;
  634.     }
  635.    if(Colours[k] != NULL)
  636.     Colours = &(Colours[++k]);
  637.   }
  638. }
  639.  
  640. /* GtDestroyPenColours:  This function deallocates the Pen colour structure */
  641.  
  642. static void GtDestroyPenColours(w)
  643.  
  644. GenTermWidget w;
  645.  
  646. {
  647.  int i;
  648.  
  649.  for(i = 0; i < w->genTerm.NumberColours; i++)
  650.   {
  651.    XtFree(w->genTerm.Colours[i].ForegroundName);
  652.    XtFree(w->genTerm.Colours[i].BackgroundName);
  653.   }
  654.  XtFree(w->genTerm.Colours);
  655. }
  656.  
  657. /* GtDefinePen:  This function defines the colour information for the given */
  658. /* pen number. */
  659.  
  660. static void GtDefinePen(w,pen)
  661.  
  662. GenTermWidget w;
  663. int pen;
  664.  
  665. {
  666.  XColor Colour;
  667.  char string[1024];
  668.  
  669.  if(w->genTerm.Colours[pen].defined == 0)
  670.   {
  671.    if(strcmp(w->genTerm.Colours[pen].ForegroundName,"Default") == 0)
  672.     w->genTerm.Colours[pen].Foreground = w->genTerm.NormalForeground;
  673.    else
  674.     {
  675.      if(!XParseColor(XtDisplay(w),
  676.              w->genTerm.ColourMap,
  677.              w->genTerm.Colours[pen].ForegroundName,&Colour))
  678.       {
  679.        sprintf(string,"Foreground color %s can't be located for pen %d",
  680.            w->genTerm.Colours[pen].ForegroundName,pen);
  681.        XtAppWarning(XtWidgetToApplicationContext(w),string);
  682.        w->genTerm.Colours[pen].Foreground = w->genTerm.NormalForeground;
  683.       }
  684.      else
  685.       {
  686.        if(!XAllocColor(XtDisplay(w),w->genTerm.ColourMap,&Colour))
  687.     {
  688.      sprintf(string,"Foreground color %s can not be allocated for pen %d",
  689.          w->genTerm.Colours[pen].ForegroundName,pen);
  690.      XtAppWarning(XtWidgetToApplicationContext(w),string);
  691.      w->genTerm.Colours[pen].Foreground = w->genTerm.NormalForeground;
  692.     }
  693.        else
  694.     w->genTerm.Colours[pen].Foreground = Colour.pixel;
  695.       }
  696.     }
  697.    if(strcmp(w->genTerm.Colours[pen].BackgroundName,"Default") == 0)
  698.     w->genTerm.Colours[pen].Background = w->genTerm.NormalBackground;
  699.    else
  700.     {
  701.      if(!XParseColor(XtDisplay(w),
  702.              w->genTerm.ColourMap,w->genTerm.Colours[pen].BackgroundName,
  703.              &Colour))
  704.       {
  705.        sprintf(string,"Background color %s can't be located for pen %d",
  706.            w->genTerm.Colours[pen].BackgroundName,pen);
  707.        XtAppWarning(XtWidgetToApplicationContext(w),string);
  708.        w->genTerm.Colours[pen].Background = w->genTerm.NormalBackground;
  709.       }
  710.      else
  711.       {
  712.        if(!XAllocColor(XtDisplay(w),w->genTerm.ColourMap,&Colour))
  713.     {
  714.      sprintf(string,"Background color %s can not be allocated for pen %d",
  715.          w->genTerm.Colours[pen].BackgroundName,pen);
  716.      XtAppWarning(XtWidgetToApplicationContext(w),string);
  717.      w->genTerm.Colours[pen].Background = w->genTerm.NormalBackground;
  718.     }
  719.        else
  720.     w->genTerm.Colours[pen].Background = Colour.pixel;
  721.       }
  722.     }
  723.    w->genTerm.Colours[pen].defined = 1;
  724.   }
  725. }
  726.  
  727. /* GtDefinePenColour:  This function allows a pen colour to be defined */
  728. /*  by either the rgb or hsl values of the foreground and the background. */
  729. /*  This routine should probably try to free the previous colours used for */
  730. /*  this pen but I have no way of telling if the colours are used in another */
  731. /*  pen so I might accidently free a colour I still need. */
  732.  
  733. GtDefinePenColour(w,pen,fr,fg,fb,br,bg,bb)
  734.  
  735. GenTermWidget w;
  736. int pen;
  737. float fr,fg,fb;
  738. float br,bg,bb;
  739.  
  740. {
  741.  int Fr,Fg,Fb;
  742.  int Br,Bg,Bb;
  743.  int red,green,blue;
  744.  XColor Foreground;
  745.  XColor Background;
  746.  int GotForeground = 0;
  747.  
  748. #define GtFixColour(a)  ((a)<0.0?0.0:((a)>1.0?1.0:(a)))
  749.  
  750.  /* first make sure we received sane values */
  751.  
  752.  if(pen < 0)
  753.   pen = 0;
  754.  if(pen >= w->genTerm.NumberColours)
  755.   pen = w->genTerm.NumberColours - 1;
  756.  
  757.  Fr = GtFixColour(fr) * MAXCOLOUR;
  758.  Fg = GtFixColour(fg) * MAXCOLOUR;
  759.  Fb = GtFixColour(fb) * MAXCOLOUR;
  760.  Br = GtFixColour(br) * MAXCOLOUR;
  761.  Bg = GtFixColour(bg) * MAXCOLOUR;
  762.  Bb = GtFixColour(bb) * MAXCOLOUR;
  763.  
  764.  if(w->genTerm.DefineColour == GTRGB)
  765.   {
  766.    Foreground.red = Fr;
  767.    Foreground.green = Fg;
  768.    Foreground.blue = Fb;
  769.  
  770.    Background.red = Br;
  771.    Background.green = Bg;
  772.    Background.blue = Bb;
  773.   }
  774.  else
  775.   {
  776.    GtHSLtoRGB(&red,&green,&blue,Fr,Fg,Fb);
  777.    Foreground.red = red;
  778.    Foreground.green = green;
  779.    Foreground.blue = blue;
  780.    GtHSLtoRGB(&red,&green,&blue,Br,Bg,Bb);
  781.    Background.red = red;
  782.    Background.green = green;
  783.    Background.blue = blue;
  784.   }
  785.  if(XAllocColor(XtDisplay(w),w->genTerm.ColourMap,&Foreground))
  786.   {
  787.    w->genTerm.Colours[pen].Foreground = Foreground.pixel;
  788.    GotForeground = 1;
  789.   }
  790.  if(XAllocColor(XtDisplay(w),w->genTerm.ColourMap,&Background))
  791.   w->genTerm.Colours[pen].Background = Background.pixel;
  792.  else
  793.   if((w->genTerm.Colours[pen].defined == 0) && (GotForeground == 1))
  794.    {                /* managed to allocate foreground colour only, fall back on default */
  795.     w->genTerm.Colours[pen].Background = w->genTerm.NormalBackground;
  796.    }
  797.  
  798.  w->genTerm.Colours[pen].defined = 1;
  799.  return(GTGOOD);
  800. }
  801.  
  802. /* GtDefineHalfBrightPen : This function defines the halfbright colour for */
  803. /*  the given pen. The algorithm first calculates the rgb values of the */
  804. /*  foreground and background colours of the pen.  These are then converted */
  805. /*  to hue saturation and luminance.  The difference in the luminance vales */
  806. /*  is calculated.  A new colour is then built such that it has the same hue */
  807. /*  and saturation as the foreground pen.  The luminance is calculated as */
  808. /*  the luminance of the foreground colour minus 3/4 of the luminance */
  809. /*  difference.  The 3/4 factor was determined empiracally. */
  810.  
  811. static void GtDefineHalfBrightPen(w,pen)
  812.  
  813. GenTermWidget w;
  814. int pen;
  815.  
  816. {
  817.  int r,g,b,h,s,l;
  818.  int l1,ldiff;
  819.  XColor ForegroundColour;
  820.  XColor BackgroundColour;
  821.  XrmValue from,to;
  822.  
  823.  switch(w->genTerm.Colours[pen].defined)
  824.   {
  825.  case 0:
  826.    GtDefinePen(w,pen);
  827.  case 1:
  828.    from.size = sizeof(Pixel);
  829.    from.addr = (caddr_t) &(w->genTerm.Colours[pen].Background);
  830.    to.size = sizeof(XColor);
  831.    to.addr = (caddr_t) &(BackgroundColour);
  832.    XtConvertAndStore(w,XtRPixel,&from,XtRColor,&to);
  833.  
  834.    from.size = sizeof(Pixel);
  835.    from.addr = (caddr_t) &(w->genTerm.Colours[pen].Foreground);
  836.    to.size = sizeof(XColor);
  837.    to.addr = (caddr_t) &ForegroundColour;
  838.    XtConvertAndStore(w,XtRPixel,&from,XtRColor,&to);
  839.  
  840.    r = BackgroundColour.red;
  841.    g = BackgroundColour.green;
  842.    b = BackgroundColour.blue;
  843.    GtRGBtoHSL(r,g,b,&h,&s,&l);
  844.  
  845.    l1 = l;
  846.  
  847.    r = ForegroundColour.red;
  848.    g = ForegroundColour.green;
  849.    b = ForegroundColour.blue;
  850.    GtRGBtoHSL(r,g,b,&h,&s,&l);
  851.  
  852.    ldiff = l - l1;
  853.    l = l - ldiff * 0.75;
  854.    GtHSLtoRGB(&r,&g,&b,h,s,l);
  855.  
  856.    ForegroundColour.red = r;
  857.    ForegroundColour.green = g;
  858.    ForegroundColour.blue = b;
  859.  
  860.    if(!XAllocColor(XtDisplay(w),w->genTerm.ColourMap,&ForegroundColour))
  861.     {
  862.      w->genTerm.Colours[pen].HalfBright = w->genTerm.Colours[pen].Foreground;
  863.      w->genTerm.Colours[pen].defined = 2;
  864.      return;
  865.     }
  866.    w->genTerm.Colours[pen].HalfBright = ForegroundColour.pixel;
  867.    w->genTerm.Colours[pen].defined = 2;
  868.    break;
  869.  case 2:
  870.    break;
  871.   }
  872. }
  873. /* GtDestroy:  This is the destroy method for the widget instance.  It frees */
  874. /*   up all allocated memory */
  875.  
  876. static void GtDestroy(w)
  877.  
  878. GenTermWidget w;
  879.  
  880. {
  881.  int i;
  882.  int npen;
  883.  
  884.  /* first delete all the graphics contexts */
  885.  
  886.  npen = w->genTerm.NumberColours <<NUMATTRIBUTES;
  887.  for(i = 0; i < npen; i++)
  888.   if(w->genTerm.Pens[i] != NULL)
  889.    {
  890.     XFreeGC(XtDisplay(w),w->genTerm.Pens[i]->draw_gc);
  891.     XFreeGC(XtDisplay(w),w->genTerm.Pens[i]->cursor_gc);
  892.     XFreeGC(XtDisplay(w),w->genTerm.Pens[i]->blink_gc);
  893.     XtFree(w->genTerm.Pens[i]);
  894.    }
  895.  XtFree(w->genTerm.Pens);
  896.  
  897.  /* now free our colours */
  898.  
  899.  for(i = 0; i < w->genTerm.NumberColours; i++)
  900.   if(w->genTerm.Colours[i].defined != 0)
  901.    {
  902.     if(strcmp(w->genTerm.Colours[i].ForegroundName,"Default") != 0)
  903.      XFreeColors(XtDisplay(w),w->genTerm.ColourMap,
  904.          &(w->genTerm.Colours[i].Foreground),1,0);
  905.     if(strcmp(w->genTerm.Colours[i].BackgroundName,"Default") != 0)
  906.      XFreeColors(XtDisplay(w),w->genTerm.ColourMap,
  907.          &(w->genTerm.Colours[i].Background),1,0);
  908.     if(w->genTerm.Colours[i].defined == 2)
  909.      XFreeColors(XtDisplay(w),w->genTerm.ColourMap,
  910.          &(w->genTerm.Colours[i].HalfBright),1,0);
  911.    }
  912.  GtDestroyPenColours(w);
  913.  
  914.  if(w->genTerm.CurrentSelection != NULL)
  915.   XtFree(w->genTerm.CurrentSelection);
  916.  
  917.  for(i = 0; i < w->genTerm.SaveLines; i++)
  918.   {
  919.    XtFree(w->genTerm.VScreen[i]->Characters);
  920.    XtFree(w->genTerm.VScreen[i]->Attributes);
  921.    XtFree(w->genTerm.VScreen[i]->Fields);
  922.    XtFree(w->genTerm.VScreen[i]);
  923.   }
  924.  XtFree(w->genTerm.VScreen);
  925.  XtFree(w->genTerm.Screen);
  926. }
  927.  
  928. /* GtBlinkCallBack:  This is the timer callback which is used to implement */
  929. /*  blinking text.  Everytime it is called it checks to see which state it */
  930. /*  in, whether the blinking text is shown or not.  It then searches the */
  931. /*  screen for text with the blinking mode inabled and redraws it with the */
  932. /*  appropriate graphics context.  If, it doesn't find any text on the */
  933. /*  screen which has a blinking attribute then it searches the save memory */
  934. /*  to see if there is any there.  If it doesn't find end then it does not */
  935. /*  reset the timeout callback. */
  936.  
  937. static void GtBlinkCallBack(client_data,Id)
  938.  
  939. XtPointer client_data;
  940. XtIntervalId *Id;
  941.  
  942. {
  943.  GenTermWidget w = (GenTermWidget) client_data;
  944.  int i,j,k;
  945.  GC gc;
  946.  int found = 0;
  947.  int start;
  948.  int xpos,ypos;
  949.  int len;
  950.  int type;
  951.  
  952.  for(i = 0; i < w->genTerm.rows; i++)
  953.   {
  954.    j = 0;
  955.    while(j < w->genTerm.columns)
  956.     {
  957.      if(w->genTerm.Screen[i]->Attributes[j].type & SETBLINK)
  958.       {
  959.        type = w->genTerm.Screen[i]->Attributes[j].type;
  960.        start = j;
  961.        found = 1;
  962.        while((++j < w->genTerm.columns) && 
  963.          (w->genTerm.Screen[i]->Attributes[j].type == type));
  964.  
  965.        len = j - start;
  966.        if(j == w->genTerm.columns)
  967.     {        /* don't blink the tail end of field (looks better) */
  968.      k = j - 1;
  969.      while((k >= start) && (w->genTerm.Screen[i]->Characters[k] == ' '))
  970.       {
  971.        k--;
  972.        len--;
  973.       }
  974.     }
  975.        if(w->genTerm.BlinkOn == 0)
  976.     gc = w->genTerm.Pens[type]->blink_gc;
  977.        else
  978.     gc = w->genTerm.Pens[type]->draw_gc;
  979.  
  980.        ypos = i * w->genTerm.CharHeight + w->genTerm.CurrentFont->ascent;
  981.        xpos = start * w->genTerm.CharWidth;
  982.        XDrawImageString(XtDisplay(w),XtWindow(w),gc,xpos,ypos,
  983.             &(w->genTerm.Screen[i]->Characters[start]),len);
  984.        if(type & SETUNDERLINE)    /* is this an underline font? */
  985.     {
  986.      ypos += w->genTerm.CurrentFont->descent - 1;
  987.      XDrawLine(XtDisplay(w),XtWindow(w),gc,xpos,ypos,
  988.            xpos + len * w->genTerm.CharWidth,ypos);
  989.     }
  990.       }
  991.      else
  992.       j++;
  993.     }
  994.   }
  995.  
  996.  /* check if we destroyed the cursor and fix it if we did */
  997.  if(w->genTerm.Screen[w->genTerm.CurrentRow]->
  998.     Attributes[w->genTerm.CurrentColumn].type & SETBLINK)
  999.   GtShowCursor(w);
  1000.  w->genTerm.BlinkOn = (w->genTerm.BlinkOn + 1) % 2;
  1001.  
  1002.  /* if we are still in a blink mode keep the  callback alive since we may get */
  1003.  /* more output to blink. */
  1004.  
  1005.  if(w->genTerm.CurrentSettings.type & SETBLINK)
  1006.   found = 1;
  1007.  
  1008.  if(found == 0)        /* if didn't find a blink on screen, check memory */
  1009.   {
  1010.    for(i = 0; i < w->genTerm.SaveLines; i++)
  1011.     for(j = 0; j < w->genTerm.columns; j++)
  1012.      if(w->genTerm.VScreen[i]->Attributes[j].type & SETBLINK)
  1013.       {
  1014.        found = 1;
  1015.        break;
  1016.       }
  1017.   }
  1018.  if(found == 0)
  1019.   w->genTerm.BlinkOn = -1;
  1020.  else
  1021.   w->genTerm.BlinkTimeOut =
  1022.    XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  1023.            (unsigned long) w->genTerm.BlinkRate,GtBlinkCallBack,w);
  1024. }
  1025.  
  1026. /* GtInput:  This is the input action callback.  It is called anytime an */
  1027. /*   input action occures.  It simply passes the X event on to the input */
  1028. /*   callback routine */
  1029.  
  1030. static void GtInput(w,event,x,y)
  1031.  
  1032. GenTermWidget w;
  1033. XEvent *event;
  1034. String *x;
  1035. Cardinal *y;
  1036.  
  1037. {
  1038.  GenTermCallback reason;
  1039.  
  1040.  GtMarginBell(w);
  1041.  
  1042.  reason.reason = GT_INPUT;
  1043.  reason.event = event;
  1044.  XtCallCallbacks(w,XtNkbdCallback,(caddr_t)&reason);
  1045. }
  1046.  
  1047. /* GtMarginBell:  This function tests whether the margin bell should be */
  1048. /*  sounded or not and sounds it if it should.  */
  1049.  
  1050. int GtMarginBell(w)
  1051.  
  1052. GenTermWidget w;
  1053.  
  1054. {
  1055.  if((w->genTerm.MarginBell) && 
  1056.     (w->genTerm.CurrentColumn == (w->genTerm.columns - w->genTerm.Margin)))
  1057.   XBell(XtDisplay(w),50);
  1058.  return(GTGOOD);
  1059. }
  1060.  
  1061. /* GtOutputAction:  This is the output action handler for the widget.  It */
  1062. /*  will send the given strings to the widget to be displayed on the screen */
  1063.  
  1064. static void GtOutputAction(w,event,strings,num)
  1065.  
  1066. GenTermWidget w;
  1067. XEvent *event;
  1068. char *strings[];
  1069. Cardinal *num;
  1070.  
  1071. {
  1072.  int i;
  1073.  
  1074.  for(i = 0; i < *num; i++)
  1075.   {
  1076.    GtOutput(w,strings[i],strlen(strings[i]));
  1077.   }
  1078. }
  1079.  
  1080. /* GtOutput:  This is the main user input routine.  The calling process */
  1081. /*   passes the function a character string and the length of the string. */
  1082. /*   The string is first filtered to make sure all the characters are */
  1083. /*   displayable.  If the LiteralMode resource is set, control characters */
  1084. /*   are proceeded with a ^ sign and mapped down into the legal range. The */
  1085. /*   string is stored in the screen array at the current cursor position */
  1086. /*   which is then updated.  */
  1087.  
  1088. int GtOutput(w,string,len)
  1089.  
  1090. GenTermWidget w;
  1091. char *string;
  1092. int len;
  1093.  
  1094. {
  1095.  char Buffer[1024];
  1096.  int BufferSize = 1024;
  1097.  int i,j;
  1098.  
  1099.  if(w->genTerm.Locked)
  1100.   return(GTLOCKED);
  1101.  
  1102.  /* first filter input to ensure there is nothing nasty in it */
  1103.  
  1104.  for(i = 0, j = 0; i < len; i++)
  1105.   {
  1106.    if(j >= BufferSize-3)
  1107.     {
  1108.      GtStoreString(w,Buffer,j);
  1109.      j = 0;
  1110.     }
  1111.    if(w->genTerm.LiteralMode == TRUE)
  1112.     {
  1113.      if(!iscntrl(string[i]))
  1114.       Buffer[j++] = string[i];
  1115.      else
  1116.       {
  1117.        Buffer[j++] = '^';
  1118.        Buffer[j++] = 0100 | string[i];
  1119.       }
  1120.     }
  1121.    else
  1122.     if((w->genTerm.IgnoreNull == False) || (string[i] != NULL))
  1123.      Buffer[j++] = w->genTerm.MapTable[string[i]];
  1124.   }
  1125.  len = j;
  1126.  
  1127.  GtStoreString(w,Buffer,len);    /* now output the string */
  1128.  return(GTGOOD);
  1129. }
  1130.  
  1131. /* GtRedisplay:  This is the Redisplay method for the widget instance.  It */
  1132. /*   determines the size of the screen that must be redrawn and calls the */
  1133. /*   appropriate function to do it. */
  1134.  
  1135. static void GtRedisplay(w,event)
  1136.  
  1137. GenTermWidget w;
  1138. XExposeEvent *event;
  1139.  
  1140. {
  1141.  int StartRow,EndRow;
  1142.  int StartColumn,EndColumn;
  1143.  int temp;
  1144.  
  1145.  GtPositionToRowColumn(w,event->x,event->y,&StartRow,&StartColumn);
  1146.  GtPositionToRowColumn(w,event->x + event->width,event->y + event->height,
  1147.                &EndRow,&EndColumn);
  1148.  if(EndRow >= w->genTerm.rows)
  1149.   EndRow = w->genTerm.rows - 1;
  1150.  if(EndColumn >= w->genTerm.columns)
  1151.   EndColumn = w->genTerm.columns - 1;
  1152.  
  1153.  GtUpdateRegion(w,StartRow,StartColumn,EndRow,EndColumn,TRUE);
  1154. }
  1155.  
  1156. /* GtUpdateRegion:  This function redraws the rectangular section of the */
  1157. /*   screen specified by the starting and ending row-column position. */
  1158. /*   If the cursor is within this region, the function will also redraw */
  1159. /*   the cursor.  */
  1160.  
  1161. static void GtUpdateRegion(w,StartRow,StartColumn,EndRow,EndColumn,CursorFlag)
  1162.  
  1163. GenTermWidget w;
  1164. int StartRow,StartColumn;
  1165. int EndRow,EndColumn;
  1166. Boolean CursorFlag;
  1167.  
  1168. {
  1169.  int i,len;
  1170.  
  1171.  if(EndRow >= w->genTerm.rows)
  1172.   EndRow = w->genTerm.rows - 1;
  1173.  
  1174.  for(i = StartRow; i <= EndRow; i++)
  1175.   {
  1176.    if(w->genTerm.Screen[i]->NumChars < EndColumn)
  1177.     len = w->genTerm.Screen[i]->NumChars - StartColumn;
  1178.    else
  1179.     len = EndColumn - StartColumn;
  1180.    if(len == 0)
  1181.     len = 1;
  1182.    GtShowLine(w,i,StartColumn,len);
  1183.   }
  1184.  
  1185.  if((StartRow <= w->genTerm.CurrentRow) && (w->genTerm.CurrentRow <= EndRow + 1)
  1186.     && (StartColumn <= w->genTerm.CurrentColumn) && 
  1187.     (w->genTerm.CurrentColumn <= EndColumn + 1) && (CursorFlag == TRUE))
  1188.   GtShowCursor(w);
  1189. }
  1190.  
  1191. /* GtPositionToRowColumn: This function takes a window coordinate and */
  1192. /*   converts it into a row and column number */
  1193.  
  1194. int GtPositionToRowColumn(w,x,y,row,column)
  1195.  
  1196. GenTermWidget w;
  1197. Dimension x,y;
  1198. int *row,*column;
  1199.  
  1200. {
  1201.  *row = y / w->genTerm.CharHeight;
  1202.  *column = x / w->genTerm.CharWidth;
  1203.  
  1204.  if(*row > w->genTerm.rows)
  1205.   *row = w->genTerm.rows;
  1206.  if(*column > w->genTerm.columns)
  1207.   *column = w->genTerm.columns;
  1208.  return(GTGOOD);
  1209. }
  1210.  
  1211. /* GtExpose:  This is the expose method for the widget instance. */
  1212.  
  1213. static void GtExpose(w,event)
  1214.  
  1215. GenTermWidget w;
  1216. XEvent *event;
  1217.  
  1218. {
  1219.  if(event->type == GraphicsExpose)
  1220.   {
  1221.    w->genTerm.ExposureCount--;
  1222.    if(w->genTerm.ExposureCount < 0) /* sanity check, should never happen */
  1223.     w->genTerm.ExposureCount = 0;
  1224.   }
  1225.  if(!XtIsRealized(w))
  1226.   return;
  1227.  GtRedisplay(w,event);
  1228. }
  1229.  
  1230. /* GtNoExpose: This is the noexpose event handler for the widget */
  1231.  
  1232. static void GtNoExpose(w,event)
  1233.  
  1234. GenTermWidget w;
  1235. XEvent *event;
  1236. {
  1237. w->genTerm.ExposureCount--;
  1238. if(w->genTerm.ExposureCount < 0)
  1239. w->genTerm.ExposureCount = 0;
  1240. }
  1241.  
  1242. /* GtResize:  This is the resize method for the widget instance. */
  1243.  
  1244. static void GtResize(w)
  1245.  
  1246. GenTermWidget w;
  1247.  
  1248. {
  1249.  GenTermCallback reason;
  1250.  
  1251.  w->genTerm.rows = (w->core.height - w->genTerm.BottomMargin) /
  1252.   w->genTerm.CharHeight;
  1253.  w->genTerm.columns = w->core.width / w->genTerm.CharWidth;
  1254.  if((w->genTerm.CurrentNumRows != w->genTerm.rows) || 
  1255.     (w->genTerm.CurrentNumColumns != w->genTerm.columns) ||
  1256.     (w->genTerm.SaveLines != w->genTerm.CurrentSaveLines))
  1257.   {
  1258.    if(w->genTerm.rows >= w->genTerm.SaveLines)
  1259.     w->genTerm.SaveLines = w->genTerm.rows * 2;
  1260.    GtReallocScreen(w);
  1261.    if(w->genTerm.CurrentRow >= w->genTerm.rows)
  1262.     w->genTerm.CurrentRow = w->genTerm.rows - 1;
  1263.    reason.reason = GT_RESIZE;
  1264.    reason.event = NULL;
  1265.    reason.Rows = w->genTerm.rows;
  1266.    reason.Columns = w->genTerm.columns;
  1267.    reason.Save = w->genTerm.SaveLines;
  1268.    XtCallCallbacks(w,XtNresizeCallback,(caddr_t)&reason);
  1269.    GtCheckPosition(w);
  1270.    GtUpdateRegion(w,0,0,w->genTerm.rows-1,w->genTerm.columns-1,True);
  1271.   }
  1272. }
  1273.  
  1274. /* GtReallocScreen:  This function reallocates the internal screen structure.*/
  1275. /*  It is called when ever the widget is resized. */
  1276.  
  1277. static void GtReallocScreen(w)
  1278.  
  1279. GenTermWidget w;
  1280.  
  1281. {
  1282.  struct Line **OldScreen = w->genTerm.VScreen;
  1283.  int i,j,k;
  1284.  int Row,Column,Save;
  1285.  int NewScreenTop;
  1286.  
  1287.  XtFree(w->genTerm.Screen);
  1288.  GtAllocateScreen(w);
  1289.  Row = MIN(w->genTerm.CurrentNumRows,w->genTerm.rows);
  1290.  Column = MIN(w->genTerm.CurrentNumColumns,w->genTerm.columns);
  1291.  Save = MIN(w->genTerm.SaveLines,w->genTerm.CurrentSaveLines);
  1292.  for(i = 0; i < w->genTerm.CurrentSaveLines; i++)
  1293.   {
  1294.    k = i % w->genTerm.SaveLines;
  1295.    for(j = 0; j < Column; j++)
  1296.     {
  1297.      w->genTerm.VScreen[k]->Characters[j] = OldScreen[i]->Characters[j];
  1298.      memcpy(&(w->genTerm.VScreen[k]->Attributes[j]),
  1299.         &(OldScreen[i]->Attributes[j]),sizeof(struct Attribute));
  1300.     }
  1301.    XtFree(w->genTerm.VScreen[k]->Fields);
  1302.    w->genTerm.VScreen[k]->Fields = (struct Field *)
  1303.     XtMalloc(sizeof(struct Field) * OldScreen[i]->NumFields);
  1304.    memcpy(w->genTerm.VScreen[k]->Fields,OldScreen[i]->Fields,
  1305.       sizeof(struct Field) * OldScreen[i]->NumFields);
  1306.    w->genTerm.VScreen[k]->ScrollRegion = OldScreen[i]->ScrollRegion;
  1307.    w->genTerm.VScreen[k]->NumChars = MIN(Column,OldScreen[i]->NumChars);
  1308.    w->genTerm.VScreen[k]->NumFields = OldScreen[i]->NumFields;
  1309.    if((w->genTerm.ScreenTop % w->genTerm.CurrentSaveLines) == i)
  1310.     NewScreenTop = k;
  1311.   }
  1312.  
  1313.  for(i = 0; i < w->genTerm.rows; i++)
  1314.   w->genTerm.Screen[i] = w->genTerm.VScreen[(NewScreenTop + i) %
  1315.                         w->genTerm.SaveLines];
  1316.  
  1317.  w->genTerm.ScreenTop = NewScreenTop;
  1318.  for(i = 0; i < w->genTerm.CurrentSaveLines; i++)
  1319.   {
  1320.    XtFree(OldScreen[i]->Characters);
  1321.    XtFree(OldScreen[i]->Attributes);
  1322.    XtFree(OldScreen[i]->Fields);
  1323.    XtFree(OldScreen[i]);
  1324.   }
  1325.  XtFree(OldScreen);
  1326.  
  1327.  w->genTerm.CurrentNumRows = w->genTerm.rows;
  1328.  w->genTerm.CurrentNumColumns = w->genTerm.columns;
  1329.  w->genTerm.CurrentSaveLines = w->genTerm.SaveLines;
  1330. }
  1331.  
  1332. /* GtSetValues:  This is the setvalue method for the widget instance. */
  1333.  
  1334. static Boolean GtSetValues(current,request,new)
  1335.  
  1336. GenTermWidget current,request,new;
  1337.  
  1338. {
  1339.  Arg args[10];
  1340.  int n = 0;
  1341.  int Redraw = False;
  1342.  int npen;
  1343.  
  1344.  if((current->genTerm.ShowCursor != new->genTerm.ShowCursor) && 
  1345.     (XtIsRealized(new)))
  1346.   {
  1347.    if((new->genTerm.ShowCursor == True) && (current->genTerm.CursorOn == False))
  1348.     GtShowCursor(new);
  1349.    if((new->genTerm.ShowCursor == False) && (current->genTerm.CursorOn == True))
  1350.     GtUnShowCursor(new);
  1351.   }
  1352.  if(current->genTerm.columns != new->genTerm.columns)
  1353.   {
  1354.    XtSetArg(args[n],XtNwidth,new->genTerm.columns * new->genTerm.CharWidth);n++;
  1355.   }
  1356.  if(current->genTerm.rows != new->genTerm.rows)
  1357.   {
  1358.    if(new->genTerm.SaveLines < new->genTerm.rows)
  1359.     {
  1360.      XtAppError(XtWidgetToApplicationContext(new),
  1361.         "GenTerm Widget: Savelines can not be less than rows");
  1362.      new->genTerm.rows = current->genTerm.rows;
  1363.     }
  1364.    else
  1365.     {
  1366.      XtSetArg(args[n],XtNheight,
  1367.           new->genTerm.BottomMargin + new->genTerm.rows *
  1368.           new->genTerm.CharHeight);n++;
  1369.     }
  1370.   }
  1371.  if(current->genTerm.BottomMargin != new->genTerm.BottomMargin)
  1372.   {
  1373.    XtSetArg(args[n],XtNheight,
  1374.         new->genTerm.BottomMargin + new->genTerm.rows *
  1375.         new->genTerm.CharHeight); n++;
  1376.   }
  1377.  if(current->genTerm.SaveLines != new->genTerm.SaveLines)
  1378.   { 
  1379.    if(new->genTerm.SaveLines < new->genTerm.rows)
  1380.     {
  1381.      XtAppError(XtWidgetToApplicationContext(new),
  1382.         "GenTerm Widget: Savelines can not be less than rows");
  1383.      new->genTerm.SaveLines = current->genTerm.SaveLines;
  1384.     }
  1385.    else
  1386.     if(XtIsRealized(new))
  1387.      GtResize(new);
  1388.   }
  1389.  if(current->genTerm.Locked != new->genTerm.Locked)
  1390.   {
  1391.    GtDoLockCallback(new);
  1392.   }
  1393.  
  1394.  if(current->genTerm.CharHeight != new->genTerm.CharHeight)
  1395.   new->genTerm.CharHeight = current->genTerm.CharHeight;
  1396.  if(current->genTerm.CharWidth != new->genTerm.CharWidth)
  1397.   new->genTerm.CharWidth = current->genTerm.CharWidth;
  1398.  
  1399.  if((current->genTerm.NumberColours != new->genTerm.NumberColours) &&
  1400.     (XtIsRealized(new)))
  1401.   {
  1402.    XtAppWarning(XtWidgetToApplicationContext(new),
  1403.         "GenTerm Widget: Resource Number Colours can only be set prior to realization");
  1404.    new->genTerm.NumberColours = current->genTerm.NumberColours;
  1405.   }
  1406.  
  1407.  if((current->genTerm.PenColours != new->genTerm.PenColours) &&
  1408.     (strcmp(current->genTerm.PenColours,new->genTerm.PenColours) != 0) &&
  1409.     (XtIsRealized(new)))
  1410.   {
  1411.    Redraw = True;
  1412.    GtChangePen(new,current);
  1413.   }
  1414.  
  1415.  if(((current->genTerm.Font != new->genTerm.Font) && 
  1416.      (strcmp(current->genTerm.Font,new->genTerm.Font) != 0)) ||
  1417.     ((current->genTerm.AlternateFont != new->genTerm.AlternateFont) &&
  1418.      (strcmp(current->genTerm.AlternateFont,new->genTerm.AlternateFont) != 0)))
  1419.   {
  1420.    if(GtLoadFonts(new) != 1)
  1421.     {
  1422.      new->genTerm.CurrentFont = current->genTerm.CurrentFont;
  1423.      new->genTerm.AltFont = current->genTerm.AltFont;
  1424.     }
  1425.    if(XtIsRealized(new))
  1426.     GtChangeFonts(new);
  1427.    XtSetArg(args[n],XtNwidthInc,new->genTerm.CharWidth); n++;
  1428.    XtSetArg(args[n],XtNheightInc,new->genTerm.CharHeight); n++;
  1429.    XtSetArg(args[n],XtNheight,        
  1430.         new->genTerm.BottomMargin + new->genTerm.rows *
  1431.         new->genTerm.CharHeight); n++;
  1432.    XtSetArg(args[n],XtNwidth,new->genTerm.columns * new->genTerm.CharWidth);n++;
  1433.   }
  1434.  
  1435.  if((current->genTerm.CursorColour != new->genTerm.CursorColour) &&
  1436.     (XtIsRealized(new)))
  1437.   {
  1438.    GtChangeCursorColour(new);
  1439.    Redraw = True;
  1440.   }
  1441.  
  1442.  if((current->genTerm.NormalForeground != new->genTerm.NormalForeground) &&
  1443.     XtIsRealized(new))
  1444.   {
  1445.    Redraw = True;
  1446.    npen = new->genTerm.NumberColours << NUMATTRIBUTES;
  1447.    GtChangeColour(new,new->genTerm.NormalForeground,
  1448.           current->genTerm.NormalForeground,0,npen);
  1449.    if(new->genTerm.foreground == current->genTerm.NormalForeground)
  1450.     new->genTerm.foreground = new->genTerm.NormalForeground;
  1451.    if(new->genTerm.CurrentForeground = current->genTerm.NormalForeground)
  1452.     new->genTerm.CurrentForeground = new->genTerm.NormalForeground;
  1453.    if(new->genTerm.background == current->genTerm.NormalForeground)
  1454.     new->genTerm.background = new->genTerm.NormalForeground;
  1455.    if(new->genTerm.CurrentBackground == current->genTerm.NormalForeground)
  1456.     new->genTerm.CurrentBackground = new->genTerm.NormalForeground;
  1457.   }
  1458.  
  1459.  if((current->genTerm.NormalBackground != new->genTerm.NormalBackground) &&
  1460.     XtIsRealized(new))
  1461.   {
  1462.    Redraw = True;
  1463.    npen = new->genTerm.NumberColours << NUMATTRIBUTES;
  1464.    GtChangeColour(new,new->genTerm.NormalBackground,
  1465.           current->genTerm.NormalBackground,0,npen);
  1466.    if(new->genTerm.foreground == current->genTerm.NormalBackground)
  1467.     new->genTerm.foreground = new->genTerm.NormalBackground;
  1468.    if(new->genTerm.CurrentForeground == current->genTerm.NormalBackground)
  1469.     new->genTerm.CurrentForeground = new->genTerm.NormalBackground;
  1470.    if(new->genTerm.background == current->genTerm.NormalBackground)
  1471.     new->genTerm.background = new->genTerm.NormalBackground;
  1472.    if(new->genTerm.CurrentBackground == current->genTerm.NormalBackground)
  1473.     new->genTerm.CurrentBackground = new->genTerm.NormalBackground;
  1474.   }
  1475.  
  1476.  if(n != 0)
  1477.   XtSetValues(new,args,n);
  1478.  return(Redraw);
  1479. }
  1480.  
  1481. /* GtAllocateScreen:  This function allocates the datastructures for the */
  1482. /*  terminal screen.  It allocates both the screen and the virtual screen.  */
  1483. /*  After allocating the memory, it initializes all the fields. */
  1484.  
  1485. static void GtAllocateScreen(widget)
  1486.  
  1487. GenTermWidget widget;
  1488.  
  1489. {
  1490.  int i,j;
  1491.  int size = widget->genTerm.columns;
  1492.  widget->genTerm.VScreen = (struct Line**) XtMalloc(sizeof(struct Line *) *
  1493.                             widget->genTerm.SaveLines);
  1494.  widget->genTerm.Screen = (struct Line **) XtMalloc(sizeof(struct Line *) *
  1495.                             widget->genTerm.rows);
  1496.  for(i = 0; i < widget->genTerm.SaveLines; i++)
  1497.   {
  1498.    widget->genTerm.VScreen[i] = (struct Line *) XtMalloc(sizeof(struct Line));
  1499.    widget->genTerm.VScreen[i]->Characters = (char *) XtMalloc(sizeof(char) *
  1500.                                   size);
  1501.    widget->genTerm.VScreen[i]->Attributes = (struct Attribute *) 
  1502.     XtMalloc(sizeof(struct Attribute) * size); 
  1503.    for(j = 0; j < widget->genTerm.columns; j++)
  1504.     {
  1505.      widget->genTerm.VScreen[i]->Characters[j] = ' ';
  1506.     }
  1507.    memrep(widget->genTerm.VScreen[i]->Attributes,
  1508.       &(widget->genTerm.DefaultSettings),sizeof(struct Attribute),
  1509.       widget->genTerm.columns);
  1510.  
  1511.    widget->genTerm.VScreen[i]->Fields = (struct Field *)
  1512.     XtMalloc(sizeof(struct Field));
  1513.    widget->genTerm.VScreen[i]->Fields[0].Start = 0;
  1514.    widget->genTerm.VScreen[i]->Fields[0].End = widget->genTerm.columns - 1;
  1515.  
  1516.    widget->genTerm.VScreen[i]->ScrollRegion = 0;
  1517.    widget->genTerm.VScreen[i]->NumChars = 0;
  1518.    widget->genTerm.VScreen[i]->NumFields = 1;
  1519.   }
  1520.  for(i = 0; i < widget->genTerm.rows; i++)
  1521.   widget->genTerm.Screen[i] = widget->genTerm.VScreen[i];
  1522. }
  1523.  
  1524. /* GtStoreString:  This function stores a string in the screen data */
  1525. /*  structure. It is the main input routine.  It simply puts the string into */
  1526. /*  the screen structure at the current cursor position.  If there isn't */
  1527. /*  enough room on the line it will either truncate the string or wrap to */
  1528. /*  the next line depending on the resource settings.  If the data wraps to */
  1529. /*  the next line, this function may force a scroll to ensure there is */
  1530. /*  enough room on the screen to display the string.  After storing the data,*/
  1531. /*  Showline is called to display it and the current cursor position is */
  1532. /*  updated. */
  1533.  
  1534. static void GtStoreString(w,buffer,len)
  1535.  
  1536. GenTermWidget w;
  1537. char buffer[];
  1538. int len;
  1539.  
  1540. {
  1541.  int newlen;
  1542.  int OldColumn = w->genTerm.CurrentColumn;
  1543.  int Column = w->genTerm.CurrentColumn;
  1544.  int Row;
  1545.  int i,end,to;
  1546.  Boolean Stuck = w->genTerm.CursorIsStuck;
  1547.  
  1548.  GtCheckPosition(w);
  1549.  Row = w->genTerm.CurrentRow;
  1550.  GtUnShowCursor(w);
  1551.  
  1552.  if(Stuck)            /* if cursor is currently stuck move it */
  1553.   {
  1554.    GtNextLine(w);
  1555.    Row = w->genTerm.CurrentRow;
  1556.    Column = w->genTerm.CurrentColumn;
  1557.    OldColumn = Column;
  1558.    w->genTerm.CursorIsStuck = False;
  1559.   }
  1560.  
  1561.  if(w->genTerm.InsertMode == True) /* if in insert mode move chars first */
  1562.   {
  1563.    end = w->genTerm.Screen[Row]->NumChars + len;
  1564.    if(end > w->genTerm.columns)
  1565.     end = w->genTerm.columns;
  1566.    for(i = end - 1; i >= Column; i--)
  1567.     {
  1568.      to = i + len;
  1569.      if(to >= w->genTerm.columns)
  1570.       to = w->genTerm.columns - 1;
  1571.      w->genTerm.Screen[Row]->Characters[to] = 
  1572.       w->genTerm.Screen[Row]->Characters[i];
  1573.      memcpy(&(w->genTerm.Screen[Row]->Attributes[to]),
  1574.         &(w->genTerm.Screen[Row]->Attributes[i]),sizeof(struct Attribute));
  1575.     }
  1576.    w->genTerm.Screen[Row]->NumChars = end;
  1577.    newlen = end - Column - len;
  1578.    GtShowLine(w,Row,Column + len,newlen);
  1579.   }
  1580.  if((len + OldColumn) < w->genTerm.columns) /* enough space on the line? */
  1581.   {
  1582.    memcpy(&(w->genTerm.Screen[Row]->Characters[Column]),buffer,len);
  1583.    if(w->genTerm.FieldAttributes == FALSE)
  1584.     memrep(&(w->genTerm.Screen[Row]->Attributes[Column]),
  1585.        &(w->genTerm.CurrentSettings),sizeof(struct Attribute),len);
  1586.  
  1587.    GtShowLine(w,w->genTerm.CurrentRow,OldColumn,len);
  1588.    w->genTerm.CurrentColumn += len;
  1589.    if(w->genTerm.Screen[Row]->NumChars < w->genTerm.CurrentColumn)
  1590.     w->genTerm.Screen[Row]->NumChars = w->genTerm.CurrentColumn;
  1591.   }
  1592.  else if(((len + OldColumn) == w->genTerm.columns) && (w->genTerm.EOLStick))
  1593.   {
  1594.    memcpy(&(w->genTerm.Screen[Row]->Characters[Column]),buffer,len);
  1595.    if(w->genTerm.FieldAttributes == FALSE)
  1596.     memrep(&(w->genTerm.Screen[Row]->Attributes[Column]),
  1597.        &(w->genTerm.CurrentSettings),sizeof(struct Attribute),len);
  1598.  
  1599.    GtShowLine(w,w->genTerm.CurrentRow,OldColumn,len);
  1600.    w->genTerm.CurrentColumn = w->genTerm.columns - 1;
  1601.    w->genTerm.CursorIsStuck = True;
  1602.    if(w->genTerm.Screen[Row]->NumChars < w->genTerm.CurrentColumn)
  1603.     w->genTerm.Screen[Row]->NumChars = w->genTerm.CurrentColumn;
  1604.   }
  1605.  else if(w->genTerm.AutoWrap == FALSE)
  1606.   {
  1607.    newlen = w->genTerm.columns - OldColumn;
  1608.    buffer[newlen-1] = buffer[len-1]; /* simulate cusor stuck at end of screen */
  1609.    memcpy(&(w->genTerm.Screen[Row]->Characters[Column]),buffer,newlen);
  1610.    if(w->genTerm.FieldAttributes == FALSE)
  1611.     memrep(&(w->genTerm.Screen[Row]->Attributes[Column]),
  1612.        &(w->genTerm.CurrentSettings),sizeof(struct Attribute),newlen);
  1613.    GtShowLine(w,w->genTerm.CurrentRow,OldColumn,newlen);
  1614.    w->genTerm.CurrentColumn += newlen - 1;
  1615.    if(w->genTerm.Screen[Row]->NumChars < w->genTerm.CurrentColumn)
  1616.     w->genTerm.Screen[Row]->NumChars = w->genTerm.CurrentColumn;
  1617.   }
  1618.  else                /* must do a line wrap possibly scrolling up */
  1619.   {
  1620.    newlen = w->genTerm.columns - OldColumn;
  1621.    memcpy(&(w->genTerm.Screen[Row]->Characters[Column]),buffer,newlen);
  1622.    if(w->genTerm.FieldAttributes == FALSE)
  1623.     memrep(&(w->genTerm.Screen[Row]->Attributes[Column]),
  1624.        &(w->genTerm.CurrentSettings),sizeof(struct Attribute),newlen);
  1625.    GtShowLine(w,w->genTerm.CurrentRow,OldColumn,newlen);
  1626.    if(w->genTerm.Screen[Row]->NumChars < (w->genTerm.CurrentColumn + newlen))
  1627.     w->genTerm.Screen[Row]->NumChars = w->genTerm.CurrentColumn + newlen;
  1628.    GtNextLine(w);        /* goto next line */
  1629.    GtStoreString(w,&(buffer[newlen]),len - newlen); /* put out rest of line */
  1630.   }
  1631.  
  1632.  GtShowCursor(w);
  1633.  
  1634. }
  1635.  
  1636. /* GtShowLine:  This is the display routine for the widget.  It displays the */
  1637. /*  next len characters starting at position row,col.  Each character is */
  1638. /*  displayed according to the attributes assigned to it. */
  1639.  
  1640. static void GtShowLine(w,row,col,len)
  1641.  
  1642. GenTermWidget w;
  1643. int row;
  1644. int col;
  1645. int len;
  1646.  
  1647. {
  1648.  int xpos,ypos;
  1649.  int i,end,npen;
  1650.  
  1651.  if((row < 0) || (row >= w->genTerm.rows) || (col < 0) || 
  1652.     (col >= w->genTerm.columns))
  1653.   return;
  1654.  
  1655.  ypos = row * w->genTerm.CharHeight + w->genTerm.CurrentFont->ascent;
  1656.  xpos = col * w->genTerm.CharWidth;
  1657.  end = col + len;
  1658.  
  1659.  while(col < end)
  1660.   {
  1661.    for(i = col; i < end; i++)
  1662.     if(w->genTerm.Screen[row]->Attributes[i].type != 
  1663.        w->genTerm.Screen[row]->Attributes[col].type)
  1664.      break;
  1665.    npen = w->genTerm.Screen[row]->Attributes[col].type;
  1666.    xpos = col * w->genTerm.CharWidth;
  1667.    len = i-col;
  1668.    XDrawImageString(XtDisplay(w),XtWindow(w),w->genTerm.Pens[npen]->draw_gc,
  1669.             xpos,ypos,&(w->genTerm.Screen[row]->Characters[col]),len);
  1670.    if(npen & SETUNDERLINE)    /* is this an underline font? */
  1671.     {
  1672.      ypos += w->genTerm.CurrentFont->descent - 1;
  1673.      XDrawLine(XtDisplay(w),XtWindow(w),w->genTerm.Pens[npen]->draw_gc,xpos,
  1674.            ypos,xpos + len * w->genTerm.CharWidth,ypos);
  1675.     }
  1676.    col = i;
  1677.   }
  1678. }
  1679.  
  1680. /* GtShowCursor:  This function will display the cursor at the current cursor*/
  1681. /*  position if the cursor is being displayed and is currently on the screen */
  1682.  
  1683. static void GtShowCursor(w)
  1684.  
  1685. GenTermWidget w;
  1686.  
  1687. {
  1688.  int row = w->genTerm.CurrentRow;
  1689.  int col = w->genTerm.CurrentColumn;
  1690.  int ypos = w->genTerm.CharHeight * row + w->genTerm.CurrentFont->ascent;
  1691.  int xpos = w->genTerm.CharWidth * col;
  1692.  int npen;
  1693.  
  1694.  if((w->genTerm.ShowCursor) && (row >= 0) && ( row < w->genTerm.rows))
  1695.   {
  1696.    npen = w->genTerm.Screen[row]->Attributes[col].type;
  1697.    XDrawImageString(XtDisplay(w),XtWindow(w),w->genTerm.Pens[npen]->cursor_gc,
  1698.             xpos,ypos,&(w->genTerm.Screen[row]->Characters[col]),1);
  1699.    w->genTerm.CursorOn = TRUE;
  1700.   }
  1701. }
  1702.  
  1703. /* GtUnShowCursor:  This function turns off the cursor */
  1704.  
  1705. static void GtUnShowCursor(w)
  1706.  
  1707. GenTermWidget w;
  1708.  
  1709. {
  1710.  int row = w->genTerm.CurrentRow;
  1711.  int col = w->genTerm.CurrentColumn;
  1712.  int ypos = w->genTerm.CharHeight * row + w->genTerm.CurrentFont->ascent;
  1713.  int xpos = w->genTerm.CharWidth * col;
  1714.  int npen;
  1715.  
  1716.  if((w->genTerm.CursorOn == TRUE) && (row >= 0) && (row < w->genTerm.rows))
  1717.   {
  1718.    npen = w->genTerm.Screen[row]->Attributes[col].type;
  1719.    XDrawImageString(XtDisplay(w),XtWindow(w),w->genTerm.Pens[npen]->draw_gc,
  1720.             xpos,ypos,&(w->genTerm.Screen[row]->Characters[col]),1);
  1721.    if(npen & SETUNDERLINE)
  1722.     {
  1723.      ypos += w->genTerm.CurrentFont->descent - 1;
  1724.      XDrawLine(XtDisplay(w),XtWindow(w),w->genTerm.Pens[npen]->draw_gc,xpos,
  1725.            ypos,xpos + w->genTerm.CharWidth,ypos);
  1726.     }
  1727.    w->genTerm.CursorOn = FALSE;
  1728.   }
  1729. }
  1730.  
  1731. /* GtCheckPosition:  This function checks to make sure the current cursor */
  1732. /*  position is within the visible screen boundaries.  If it isn't, it sets */
  1733. /*  the top of screen to make sure that it is. In addition if the */
  1734. /*  ScrollOnOutput resource is true then the function makes sure the screen */
  1735. /*  is at the bottom of the scroll bar. */
  1736.  
  1737. static void GtCheckPosition(w)
  1738.  
  1739. GenTermWidget w;
  1740.  
  1741. {
  1742.  if((w->genTerm.ScrollOnOutput == TRUE) &&
  1743.     (w->genTerm.VirtualTop < w->genTerm.MaxVirtualTop))
  1744.   GtSetTopOfScreen(w,w->genTerm.MaxVirtualTop);
  1745.  
  1746.  /* if the cursor can roll of the screen and it is not visible, move screen */
  1747.  /* so that it becomes visible */
  1748.  
  1749.  if((w->genTerm.CursorFloats == FALSE) &&
  1750.     ((w->genTerm.CurrentRow < 0) || (w->genTerm.CurrentRow >= w->genTerm.rows)))
  1751.   GtSetTopOfScreen(w,w->genTerm.VirtualTop - w->genTerm.CursorOffset);
  1752.  w->genTerm.CursorIsStuck = False; /* convenient place to reset this flag */
  1753. }
  1754.  
  1755. /* GtNextLine:  This function moves the cursor to the next line.  It is */
  1756. /* called by StoreString when it needs to wrap to the next line */
  1757.  
  1758. static void GtNextLine(w)
  1759.  
  1760. GenTermWidget w;
  1761.  
  1762. {
  1763.  if(w->genTerm.CurrentRow >= w->genTerm.rows-1)
  1764.   {
  1765.    GtScrollUp(w,1);
  1766.    w->genTerm.CurrentColumn = 0;
  1767.   }
  1768.  else if((w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion !=
  1769.       w->genTerm.Screen[w->genTerm.CurrentRow+1]->ScrollRegion) &&
  1770.      (w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion != 0))
  1771.   {
  1772.    GtScrollUp(w,1);
  1773.    w->genTerm.CurrentColumn = 0;
  1774.    w->genTerm.CurrentRow += 1;
  1775.   }
  1776.  else
  1777.   {
  1778.    w->genTerm.CurrentRow += 1;
  1779.    w->genTerm.CurrentColumn = 0;
  1780.   }
  1781. }
  1782.  
  1783. /* GtNewLine:  This function implements a newline function.  The current */
  1784. /*   position is moved dist lines down from the current line.  If this would */
  1785. /*   move the cursor off the bottom of the screen, a scroll up is performed. */
  1786.  
  1787. int GtNewLine(w,dist)
  1788.  
  1789. GenTermWidget w;
  1790. int dist;
  1791.  
  1792. {
  1793.  int NewRow = w->genTerm.CurrentRow + dist;
  1794.  int ScrollDist;
  1795.  int i;
  1796.  
  1797.  if(w->genTerm.Locked)
  1798.   return(GTLOCKED);
  1799.  
  1800.  GtCheckPosition(w);
  1801.  GtUnShowCursor(w);
  1802.  if((NewRow >= w->genTerm.rows) ||
  1803.     ((w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion !=
  1804.       w->genTerm.Screen[NewRow]->ScrollRegion) &&
  1805.      (w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion != 0)))
  1806.   {
  1807.    for(i = w->genTerm.CurrentRow; i < w->genTerm.rows; i++)
  1808.     if(w->genTerm.Screen[i]->ScrollRegion != 
  1809.        w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1810.      break;
  1811.    ScrollDist = dist - (i - w->genTerm.CurrentRow) + 1;
  1812.    GtScrollUp(w,ScrollDist);
  1813.    w->genTerm.CurrentRow = w->genTerm.rows - 1;
  1814.   }
  1815.  else
  1816.   w->genTerm.CurrentRow += dist;
  1817.  if(w->genTerm.CurrentRow >= w->genTerm.rows)
  1818.   w->genTerm.CurrentRow = w->genTerm.rows - 1;
  1819.  
  1820.  GtShowCursor(w);
  1821.  return(GTGOOD);
  1822. }
  1823.  
  1824. /* GtCursorDown:  This function moves the cursor down dist lines.  If this */
  1825. /*  would move the cursor outside of the screen and wrap=0 then the screen */
  1826. /*  is rolled up but the required number of lines.  If wrap=1, then the */
  1827. /*  cursor wraps around to the top of the screen.  If wrap=2, then the cursor*/
  1828. /*  stays at the bottom of the screen */
  1829.  
  1830. int GtCursorDown(w,dist,wrap)
  1831.  
  1832. GenTermWidget w;
  1833. int dist;
  1834. int wrap;
  1835.  
  1836. {
  1837.  int NewRow = w->genTerm.CurrentRow + dist;
  1838.  int move,i;
  1839.  int TopOfScroll, ScrollSize;
  1840.  
  1841.  if(w->genTerm.Locked)
  1842.   return(GTLOCKED);
  1843.  
  1844.  GtCheckPosition(w);
  1845.  GtUnShowCursor(w);
  1846.  if((NewRow >= w->genTerm.rows) && (wrap == 0))
  1847.   {
  1848.    move = w->genTerm.rows - NewRow + 1;
  1849.    GtScrollScreen(w,move);
  1850.    w->genTerm.CurrentRow = w->genTerm.rows - 1;
  1851.   }
  1852.  else if((NewRow >= w->genTerm.rows) && (wrap == 1))
  1853.   {
  1854.    while(NewRow >= w->genTerm.rows)
  1855.     NewRow -= w->genTerm.rows;
  1856.    w->genTerm.CurrentRow = NewRow;
  1857.   }
  1858.  else if((NewRow >= w->genTerm.rows) && (wrap == 2))
  1859.   w->genTerm.CurrentRow = w->genTerm.rows - 1;
  1860.  else if((w->genTerm.CursorKeyScrollRegion) &&
  1861.      ((w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion !=
  1862.        w->genTerm.Screen[NewRow]->ScrollRegion) &&
  1863.       (w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion != 0)))
  1864.   {
  1865.    if(wrap == 0)
  1866.     {
  1867.      for(i = w->genTerm.CurrentRow; i < w->genTerm.rows; i++)
  1868.       if(w->genTerm.Screen[i]->ScrollRegion != 
  1869.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1870.        break;
  1871.      move = dist - (i - w->genTerm.CurrentRow) + 1;
  1872.      GtScrollUp(w,move);
  1873.     }
  1874.    else if (wrap == 1)
  1875.     {
  1876.      TopOfScroll = -1;
  1877.      ScrollSize = 0;
  1878.      for(i = 0; i < w->genTerm.rows; i++)
  1879.       if(w->genTerm.Screen[i]->ScrollRegion == 
  1880.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1881.        {
  1882.     if(TopOfScroll == -1)
  1883.      TopOfScroll = i;
  1884.     ScrollSize++;
  1885.        }
  1886.      while(w->genTerm.Screen[NewRow]->ScrollRegion !=
  1887.        w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1888.       {
  1889.        NewRow = NewRow - ScrollSize;
  1890.       }
  1891.      w->genTerm.CurrentRow = NewRow;
  1892.     }
  1893.    else if (wrap == 2)
  1894.     {
  1895.      for(i = w->genTerm.CurrentRow; i < w->genTerm.rows; i++)
  1896.       if(w->genTerm.Screen[i]->ScrollRegion !=
  1897.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1898.        {
  1899.     i--;
  1900.     break;
  1901.        }
  1902.      w->genTerm.CurrentRow = i;
  1903.     }   
  1904.   }
  1905.  else
  1906.   w->genTerm.CurrentRow = NewRow;
  1907.  
  1908.  GtShowCursor(w);
  1909.  return(GTGOOD);
  1910. }
  1911.  
  1912. /* GtCursorUp: This function moves the current position up dist lines.  If */
  1913. /*  this would move the cursor off of the screen and wrap=0, then the screen */
  1914. /*  is rolled down the required number of lines and the cursor is placed on */
  1915. /*  the top line.  If wrap=1 then the cursor wraps around to the bottom of */
  1916. /*  the screen.  If wrap=2 then the cursor sticks at the top of the screen. */
  1917.  
  1918. int GtCursorUp(w,dist,wrap)
  1919.  
  1920. GenTermWidget w;
  1921. int dist;
  1922. int wrap;
  1923.  
  1924. {
  1925.  int NewRow = w->genTerm.CurrentRow - dist;
  1926.  int move,i;
  1927.  int TopOfScroll;
  1928.  int ScrollSize;
  1929.  
  1930.  if(w->genTerm.Locked)
  1931.   return(GTLOCKED);
  1932.  
  1933.  GtCheckPosition(w);
  1934.  GtUnShowCursor(w);
  1935.  if((NewRow < 0) && (wrap == 0))
  1936.   {
  1937.    move = NewRow;
  1938.    GtScrollScreen(w,move);
  1939.    w->genTerm.CurrentRow = 0;
  1940.   }
  1941.  else if((NewRow < 0) && (wrap == 1))
  1942.   {
  1943.    while(NewRow < 0)
  1944.     NewRow += w->genTerm.rows;
  1945.    w->genTerm.CurrentRow = NewRow;
  1946.   }
  1947.  else if((NewRow < 0) && (wrap == 2))
  1948.   w->genTerm.CurrentRow = 0;
  1949.  else if((w->genTerm.CursorKeyScrollRegion) &&
  1950.      ((w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion !=
  1951.        w->genTerm.Screen[NewRow]->ScrollRegion) &&
  1952.       (w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion != 0)))
  1953.   {
  1954.    if(wrap == 0)
  1955.     {
  1956.      for(i = w->genTerm.CurrentRow; i >= 0; i--)
  1957.       if(w->genTerm.Screen[i]->ScrollRegion != 
  1958.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1959.        break;
  1960.      move = dist - (w->genTerm.CurrentRow - i) + 1;
  1961.      move = -move;
  1962.      GtScrollUp(w,move);
  1963.     }
  1964.    else if (wrap == 1)
  1965.     {
  1966.      TopOfScroll = -1;
  1967.      ScrollSize = 0;
  1968.      for(i = 0; i < w->genTerm.rows; i++)
  1969.       if(w->genTerm.Screen[i]->ScrollRegion == 
  1970.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1971.        {
  1972.     if(TopOfScroll == -1)
  1973.      TopOfScroll = i;
  1974.     ScrollSize++;
  1975.        }
  1976.      while(w->genTerm.Screen[NewRow]->ScrollRegion !=
  1977.        w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1978.       {
  1979.        NewRow = NewRow + ScrollSize;
  1980.       }
  1981.      w->genTerm.CurrentRow = NewRow;
  1982.     }
  1983.    else if (wrap == 2)
  1984.     {
  1985.      for(i = w->genTerm.CurrentRow; i >= 0; i--)
  1986.       if(w->genTerm.Screen[i]->ScrollRegion !=
  1987.      w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion)
  1988.        {
  1989.     i++;
  1990.     break;
  1991.        }
  1992.      w->genTerm.CurrentRow = i;
  1993.     }  
  1994.   }
  1995.  else
  1996.   w->genTerm.CurrentRow = NewRow;
  1997.  
  1998.  GtShowCursor(w);
  1999.  return(GTGOOD);
  2000. }
  2001.  
  2002. /* GtCursorRight:  This function moves the cursor dist columns to the left. */
  2003. /*  If wrap=0, then the cursor will stick at the rightmost column, otherwise */
  2004. /*  it will wrap around to the left side.  If roll=0 then the cursor will */
  2005. /*  wrap from the bottom right corner to the top left otherwise the screen */
  2006. /*  will be rolled up */
  2007.  
  2008. int GtCursorRight(w,dist,wrap,roll)
  2009.  
  2010. GenTermWidget w;
  2011. int dist;
  2012. int wrap;
  2013. int roll;
  2014.  
  2015. {
  2016.  int NewColumn = w->genTerm.CurrentColumn + dist;
  2017.  
  2018.  if(w->genTerm.Locked)
  2019.   return(GTLOCKED);
  2020.  
  2021.  GtCheckPosition(w);
  2022.  GtUnShowCursor(w);
  2023.  if(NewColumn >= w->genTerm.columns)
  2024.   {
  2025.    if(wrap == 0)
  2026.     w->genTerm.CurrentColumn = w->genTerm.columns - 1;
  2027.    else
  2028.     {
  2029.      while(NewColumn >= w->genTerm.columns)
  2030.       {
  2031.        w->genTerm.CurrentRow += 1;
  2032.        if(w->genTerm.CurrentRow >= w->genTerm.rows)
  2033.     {
  2034.      if(roll == 0)
  2035.       w->genTerm.CurrentRow = 0;
  2036.      else
  2037.       {
  2038.        w->genTerm.CurrentRow = w->genTerm.rows - 1;
  2039.        GtScrollScreen(w,1);
  2040.       }
  2041.     }
  2042.        NewColumn = NewColumn - w->genTerm.columns;
  2043.       }
  2044.      w->genTerm.CurrentColumn = NewColumn;
  2045.     }
  2046.   }
  2047.  else
  2048.   {
  2049.    w->genTerm.CurrentColumn = NewColumn;
  2050.   }
  2051.  
  2052.  GtShowCursor(w);
  2053.  return(GTGOOD);
  2054. }
  2055.  
  2056. /* GtCursorLeft:  This function moves the current position dist columns to */
  2057. /*  the left.  If wrap==0 then the cursor will stick at the leftmost column */
  2058. /*  otherwise it will wrap around to the right. If roll==0 then at the top */
  2059. /*  left corner the cursor will wrap down to the bottom right, otherwise the */
  2060. /*  screen is rolled up if possible. */
  2061.  
  2062. int GtCursorLeft(w,dist,wrap,roll)
  2063.  
  2064. GenTermWidget w;
  2065. int dist;
  2066. int wrap;
  2067. int roll;
  2068.  
  2069. {
  2070.  int NewColumn = w->genTerm.CurrentColumn - dist;
  2071.  
  2072.  if(w->genTerm.Locked)
  2073.   return(GTLOCKED);
  2074.  
  2075.  GtCheckPosition(w);
  2076.  GtUnShowCursor(w);
  2077.  if(NewColumn < 0)
  2078.   {
  2079.    if(wrap == 0)
  2080.     NewColumn = 0;
  2081.    else
  2082.     {
  2083.      while(NewColumn < 0)
  2084.       {
  2085.        NewColumn = w->genTerm.columns + NewColumn;
  2086.        w->genTerm.CurrentRow -= 1;
  2087.        if(w->genTerm.CurrentRow < 0)
  2088.     if(roll == 0)
  2089.      w->genTerm.CurrentRow = w->genTerm.rows - 1;
  2090.     else
  2091.      {
  2092.       w->genTerm.CurrentRow = 0;
  2093.       if(w->genTerm.VirtualTop == 0)
  2094.        NewColumn = 0;
  2095.       else
  2096.        GtScrollScreen(w,-1);
  2097.      }
  2098.       }
  2099.     }
  2100.   }      
  2101.  w->genTerm.CurrentColumn = NewColumn;
  2102.  GtShowCursor(w);
  2103.  return(GTGOOD);
  2104. }
  2105.  
  2106. /* GtGetCursorPosition:  This function returns the row and column of the */
  2107. /*  current position */
  2108.  
  2109. int GtGetCursorPosition(w,r,c)
  2110.  
  2111. GenTermWidget w;
  2112. int *c;
  2113. int *r;
  2114.  
  2115. {
  2116.  *r = w->genTerm.CurrentRow;
  2117.  *c = w->genTerm.CurrentColumn;
  2118.  return(GTGOOD);
  2119. }
  2120.  
  2121. /* GtCursorReturn:  This function moves the current position to the leftmost */
  2122. /*  column on the current row */ 
  2123.  
  2124. int GtCursorReturn(w)
  2125.  
  2126. GenTermWidget w;
  2127.  
  2128. {
  2129.  if(w->genTerm.Locked)
  2130.   return(GTLOCKED);
  2131.  
  2132.  GtCheckPosition(w);
  2133.  GtUnShowCursor(w);
  2134.  w->genTerm.CurrentColumn = 0;
  2135.  GtShowCursor(w);
  2136.  return(GTGOOD);
  2137. }
  2138.  
  2139. /* GtSetCursorPosition:  This function moves the current position to the */
  2140. /*  specified row and column.  If the given position is outside of the */
  2141. /*  screen, the current position is set to the closest point that is still */
  2142. /*  within the screen. */
  2143.  
  2144. int GtSetCursorPosition(w,row,column)
  2145.  
  2146. GenTermWidget w;
  2147. int row;
  2148. int column;
  2149.  
  2150. {
  2151.  if(w->genTerm.Locked)
  2152.   return(GTLOCKED);
  2153.  
  2154.  GtCheckPosition(w);
  2155.  GtUnShowCursor(w);
  2156.  GtCheckValidPosition(w,&row,&column,1);
  2157.  
  2158.  w->genTerm.CurrentRow = row;
  2159.  w->genTerm.CurrentColumn = column;
  2160.  
  2161.  GtShowCursor(w);
  2162.  return(GTGOOD);
  2163. }
  2164.  
  2165. /* GtScrollUp:  This function is called when there is not enough space in */
  2166. /*  the current scroll region to output a new line.  It will scroll the */
  2167. /*  region up by the required amount and move the current position to */
  2168. /*  correspond to the new screen. */
  2169.  
  2170. int GtScrollUp(w,dist)
  2171.  
  2172. GenTermWidget w;
  2173. int dist;
  2174.  
  2175. {
  2176.  int Top;
  2177.  int NewTop;
  2178.  int NewCurrent;
  2179.  int OldCurrent;
  2180.  int CurrentRegion = w->genTerm.Screen[w->genTerm.CurrentRow]->ScrollRegion;
  2181.  int i = w->genTerm.CurrentRow;
  2182.  int DestY;
  2183.  int SrcY;
  2184.  int Height;
  2185.  int Width;
  2186.  
  2187.  if(w->genTerm.Locked)
  2188.   return(GTLOCKED);
  2189.  
  2190.  /* first go find top of scrolling region */
  2191.  
  2192.  while((i >= 0) && (w->genTerm.Screen[i]->ScrollRegion == CurrentRegion))
  2193.   i--;
  2194.  i++;
  2195.  
  2196.  if(i < 0)
  2197.   i = 0;
  2198.  Top = i;
  2199.  NewTop = Top + dist;
  2200.  if(NewTop < w->genTerm.CurrentRow) /* make sure scroll region large enough */
  2201.   {
  2202.    DestY = Top * w->genTerm.CharHeight;
  2203.    SrcY = NewTop * w->genTerm.CharHeight;
  2204.    Width = w->genTerm.columns * w->genTerm.CharWidth;
  2205.    Height = (w->genTerm.CurrentRow - NewTop + 1) * w->genTerm.CharHeight;
  2206.  
  2207.    XCopyArea(XtDisplay(w),XtWindow(w),XtWindow(w),w->genTerm.Pens[0]->draw_gc,
  2208.          0,SrcY,Width,Height,0,DestY);
  2209.    w->genTerm.ExposureCount++;
  2210.  
  2211.    Height = dist * w->genTerm.CharHeight;
  2212.    /*SrcY = w->genTerm.CurrentRow * w->genTerm.CharHeight;*/
  2213.    SrcY = (w->genTerm.CurrentRow - dist + 1) * w->genTerm.CharHeight;
  2214.    XClearArea(XtDisplay(w),XtWindow(w),0,SrcY,Width,Height,False);
  2215.  
  2216.    if((Top == 0) && (w->genTerm.CurrentRow + dist >= w->genTerm.rows))
  2217.     {
  2218.      if(w->genTerm.VirtualTop < (w->genTerm.SaveLines - w->genTerm.rows))
  2219.       GtMoveScreen(w,dist,w->genTerm.ScrollDownClear);
  2220.      else
  2221.       GtMoveScreen(w,dist,True);
  2222.      w->genTerm.CurrentRow -= (dist - 1);
  2223.      for(i = 0; i < dist; i++)
  2224.       w->genTerm.Screen[i + w->genTerm.CurrentRow]->ScrollRegion = CurrentRegion;
  2225.     }
  2226.    else
  2227.     {        /* perform an internal screen scroll inside a scroll region */
  2228.      GtScrollRegionScroll(w,dist,Top,CurrentRegion);
  2229.     }
  2230.    GtUpdateRegion(w,w->genTerm.CurrentRow,0,w->genTerm.CurrentRow+dist,
  2231.           w->genTerm.columns,FALSE);
  2232.   }
  2233.  else                /* our new top of screen is below our current line */
  2234.   {
  2235.    SrcY = Top * w->genTerm.CharHeight;   
  2236.    Height = w->genTerm.CharHeight * w->genTerm.rows;
  2237.    Width = w->genTerm.CharWidth * w->genTerm.columns;
  2238.    XClearArea(XtDisplay(w),XtWindow(w),0,0,Width,Height,False);
  2239.  
  2240.    if((Top == 0) && (w->genTerm.CurrentRow + dist >= w->genTerm.rows))
  2241.     GtMoveScreen(w,dist,FALSE);
  2242.    else
  2243.     GtScrollRegionScroll(w,dist,Top,CurrentRegion);
  2244.    GtUpdateRegion(w,0,0,w->genTerm.rows,w->genTerm.columns,FALSE);
  2245.   }
  2246.  return(GTGOOD);
  2247. }
  2248.  
  2249. /* GtScrollRegionScroll:  This function handles the scrolling from within */
  2250. /*  a scroll region */
  2251.  
  2252. static int GtScrollRegionScroll(w,dist,Top,CurrentRegion)
  2253.  
  2254. GenTermWidget w;
  2255. int dist;
  2256. int Top;
  2257. int CurrentRegion;
  2258.  
  2259. {
  2260.  struct Line **Store;
  2261.  int i;
  2262.  int NewCurrent;
  2263.  
  2264.  Store = (struct Line **) XtMalloc(sizeof(struct Line *) * dist);
  2265.  for(i = 0; i < dist; i++)
  2266.   {
  2267.    Store[i] = w->genTerm.Screen[Top + i];
  2268.   }
  2269.  if(w->genTerm.SaveScrollRegion == True)
  2270.   {
  2271.    for(i = Top - 1; i >= 0; i--)
  2272.     w->genTerm.Screen[i + dist] = w->genTerm.Screen[i];
  2273.  
  2274.    for(i = 0; i < dist; i++)
  2275.     w->genTerm.Screen[i] = Store[i];
  2276.  
  2277.    for(i = 0; i < w->genTerm.rows; i++)
  2278.     w->genTerm.VScreen[(w->genTerm.ScreenTop + i) % w->genTerm.SaveLines]
  2279.      = w->genTerm.Screen[i];
  2280.  
  2281.    for(i = 0; i < dist; i++)
  2282.     Store[i] = w->genTerm.VScreen[(w->genTerm.ScreenTop + w->genTerm.rows + i)
  2283.                   % w->genTerm.SaveLines];
  2284.  
  2285.    for(i = w->genTerm.rows; i > w->genTerm.CurrentRow + dist; i--)
  2286.     w->genTerm.VScreen[(w->genTerm.ScreenTop + i) % w->genTerm.SaveLines] =
  2287.      w->genTerm.VScreen[(w->genTerm.ScreenTop + i - dist) % w->genTerm.SaveLines];
  2288.  
  2289.    for(i = 0; i < dist; i++)
  2290.     {
  2291.      w->genTerm.VScreen[(w->genTerm.ScreenTop + w->genTerm.CurrentRow + 1 +
  2292.              i) % w->genTerm.SaveLines] = Store[i];
  2293.      if((w->genTerm.VirtualTop >= (w->genTerm.SaveLines - w->genTerm.rows)) ||
  2294.     (w->genTerm.ScrollDownClear == 0))
  2295.       GtClearLine(w,Store[i]);
  2296.     }  
  2297.  
  2298.    if(w->genTerm.VirtualTop < (w->genTerm.SaveLines - w->genTerm.rows))
  2299.     GtMoveScreen(w,dist,w->genTerm.ScrollDownClear);
  2300.    else
  2301.     GtMoveScreen(w,dist,True);
  2302.    NewCurrent = w->genTerm.CurrentRow - dist + 1;
  2303.    for(i = 0; i < dist; i++)
  2304.     w->genTerm.Screen[i + NewCurrent]->ScrollRegion = CurrentRegion;
  2305.   }
  2306.  else
  2307.   {
  2308.    NewCurrent = w->genTerm.CurrentRow - dist + 1;
  2309.    for(i = Top; i < NewCurrent; i++)
  2310.     w->genTerm.Screen[i] = w->genTerm.Screen[i+dist];
  2311.    for(i = 0; i < dist; i++)
  2312.     {
  2313.      w->genTerm.Screen[NewCurrent + i] = Store[i];
  2314.      GtClearLine(w,w->genTerm.Screen[NewCurrent + i]);
  2315.      w->genTerm.Screen[NewCurrent + i]->ScrollRegion = CurrentRegion;
  2316.     }
  2317.   }
  2318.  w->genTerm.CurrentRow = w->genTerm.CurrentRow - dist + 1;
  2319.  XtFree(Store);
  2320. }
  2321.  
  2322. /* GtClearLine:  This function removes all data and attributes from the */
  2323. /*  current line. */
  2324.  
  2325. static int GtClearLine(w,row)
  2326.  
  2327. GenTermWidget w;
  2328. struct Line *row;
  2329.  
  2330. {
  2331.  int i;
  2332.  
  2333.  for(i = 0; i < w->genTerm.columns; i++)
  2334.   {
  2335.    row->Characters[i] = ' ';
  2336.    memcpy(&(row->Attributes[i]),&(w->genTerm.DefaultSettings),
  2337.       sizeof(struct Attribute));
  2338.   }
  2339.  
  2340.  row->NumChars = 0;
  2341.  row->NumFields = 1;
  2342.  row->Fields[0].Start = 0;
  2343.  row->Fields[0].End = w->genTerm.columns - 1;
  2344. }
  2345.  
  2346. /* GtMoveScreen: This function moves the physical screen data by a distance  */
  2347. /*  dist within the virtual screen.  If clear=TRUE, the new lines that are */
  2348. /*  moved onto the screen are cleared.  This function will cause TopCallbacks*/
  2349. /*  to  be executed.  */
  2350.  
  2351. static void GtMoveScreen(w,dist,clear)
  2352.  
  2353. GenTermWidget w;
  2354. int dist;
  2355. int clear;
  2356.  
  2357. {
  2358.  int NewTop = w->genTerm.ScreenTop + dist;
  2359.  int i;
  2360.  
  2361.  for(i = 0; i < w->genTerm.rows; i++)
  2362.   {
  2363.    w->genTerm.Screen[i] = w->genTerm.VScreen[(NewTop + i) % w->genTerm.SaveLines];
  2364.    if((i >= w->genTerm.rows - dist) && (clear == TRUE))
  2365.     {
  2366.      GtClearLine(w,w->genTerm.Screen[i]);
  2367.     }
  2368.   }
  2369.  w->genTerm.ScreenTop = NewTop;
  2370.  if(w->genTerm.ScreenTop > w->genTerm.VScreenTop)
  2371.   w->genTerm.VScreenTop += dist;
  2372.  w->genTerm.VirtualTop = w->genTerm.VirtualTop + dist;
  2373.  if(w->genTerm.VirtualTop > w->genTerm.SaveLines - w->genTerm.rows)
  2374.   w->genTerm.VirtualTop = w->genTerm.SaveLines - w->genTerm.rows;
  2375.  if(w->genTerm.VirtualTop > w->genTerm.MaxVirtualTop)
  2376.   w->genTerm.MaxVirtualTop = w->genTerm.VirtualTop;
  2377.  GtDoTopCallback(w);
  2378. }
  2379.  
  2380. /* DoLockCallback:  This function will execute the LockCallbacks that are */
  2381. /* defined for the widget instance */
  2382.  
  2383. static void GtDoLockCallback(w)
  2384.  
  2385. GenTermWidget w;
  2386.  
  2387. {
  2388.  GenTermCallback reason;
  2389.  
  2390.  reason.reason = GT_LOCK;
  2391.  reason.event = NULL;
  2392.  reason.Value = w->genTerm.Locked;
  2393.  XtCallCallbacks(w,XtNlockCallback,(caddr_t)&reason);
  2394. }
  2395.  
  2396. /* GtDoTopCallback:  This function will execute the TopCallbacks that are */
  2397. /*  defined for the widget instance */
  2398.  
  2399. static void GtDoTopCallback(w)
  2400.  
  2401. GenTermWidget w;
  2402.  
  2403. {
  2404.  GenTermCallback reason;
  2405.  
  2406.  reason.reason = GT_TOP;
  2407.  reason.event = NULL;
  2408.  reason.TopLine = w->genTerm.VirtualTop;
  2409.  XtCallCallbacks(w,XtNtopCallback,(caddr_t)&reason);
  2410. }
  2411.  
  2412. /* GtSetTopOfScreen:  This function allows the caller to set the top line of */
  2413. /*  the physical screen to a certain point within the virtual screen. */
  2414.  
  2415. int GtSetTopOfScreen(w,line)
  2416.  
  2417. GenTermWidget w;
  2418. int line;
  2419.  
  2420. {
  2421.  int dist;
  2422.  
  2423.  if(w->genTerm.Locked)
  2424.   return(GTLOCKED);
  2425.  if (line < 0)
  2426.   line = 0;
  2427.  if(w->genTerm.FullScroll == True)
  2428.   {
  2429.    if(line > (w->genTerm.SaveLines - w->genTerm.rows))
  2430.     line = w->genTerm.SaveLines - w->genTerm.rows;
  2431.   }
  2432.  else
  2433.   {
  2434.    if(line > w->genTerm.MaxVirtualTop)
  2435.     line = w->genTerm.MaxVirtualTop;
  2436.   }
  2437.  
  2438.  GtUnShowCursor(w);
  2439.  dist = line - w->genTerm.VirtualTop;
  2440.  GtScrollScreen(w,dist);
  2441.  if(dist == 0)            /* if dist is zero we won't scroll but do callback anyway */
  2442.   {                /* incase user is moving a scroll bar */
  2443.    GtDoTopCallback(w);
  2444.   }
  2445.  GtShowCursor(w);
  2446.  return(GTGOOD);
  2447. }
  2448.  
  2449. /* GtScrollScreen:  This function moves the screen up or down in the virtual */
  2450. /*  screen by a distance dist.  It copies the data that is on the screen */
  2451. /*  to its new location, clears the screen that will be updated, calls */
  2452. /*  MoveScreen to move the data around and finally calls update region to */
  2453. /*  output the new information.  */
  2454.  
  2455. static void GtScrollScreen(w,dist)
  2456.  
  2457. GenTermWidget w;
  2458. int dist;
  2459.  
  2460. {
  2461.  int DestY,SrcY,Width,Height;
  2462.  int NewTop = dist + w->genTerm.VirtualTop;
  2463.  int offset;
  2464.  int i;
  2465.  int FromScroll = 0;
  2466.  int maxdist = w->genTerm.rows;
  2467.  
  2468.  if(dist == 0)
  2469.   return;
  2470.  
  2471.  offset = w->genTerm.MaxVirtualTop - NewTop;
  2472.  if(NewTop < 0)            /* make sure we can scroll to that point */
  2473.   dist -= NewTop;
  2474.  else if(offset > w->genTerm.SaveLines - w->genTerm.rows)
  2475.   {
  2476.    dist = w->genTerm.MaxVirtualTop - w->genTerm.VirtualTop - 
  2477.     w->genTerm.SaveLines + w->genTerm.rows;
  2478.   }
  2479.  if(w->genTerm.SaveScrollRegion == True)
  2480.   {
  2481.    for(i = 0; i < w->genTerm.rows; i++)
  2482.     if(w->genTerm.Screen[i]->ScrollRegion != 0)
  2483.      FromScroll = 1;
  2484.   }
  2485.  
  2486.  /* if we are going to scroll the screen with a scroll region then perform */
  2487.  /* the required row swapping in the virtual screen before we update the */
  2488.  /* screen. */
  2489.  
  2490.  if(FromScroll == 1)
  2491.   maxdist = GtScrollRegionScrollScreen(w,dist);
  2492.  else
  2493.   {
  2494.    if((dist > 0) && ( dist < maxdist))
  2495.     {
  2496.      DestY = 0;
  2497.      SrcY = dist * w->genTerm.CharHeight;
  2498.      Width = w->genTerm.columns * w->genTerm.CharWidth;
  2499.      Height = (w->genTerm.rows - dist) * w->genTerm.CharHeight;
  2500.  
  2501.      XCopyArea(XtDisplay(w),XtWindow(w),XtWindow(w),w->genTerm.Pens[0]->draw_gc,
  2502.            0,SrcY,Width,Height,0,DestY);
  2503.      w->genTerm.ExposureCount++;
  2504.  
  2505.      Height = dist * w->genTerm.CharHeight;
  2506.      SrcY = (w->genTerm.rows - dist) * w->genTerm.CharHeight;
  2507.      XClearArea(XtDisplay(w),XtWindow(w),0,SrcY,Width,Height,False);
  2508.  
  2509.      GtMoveScreen(w,dist,FALSE);
  2510.      GtUpdateRegion(w,w->genTerm.rows - dist,0,w->genTerm.rows,w->genTerm.columns,
  2511.             FALSE);
  2512.     }
  2513.    else if((dist < 0) && (-dist < maxdist))
  2514.     {
  2515.      DestY =- dist * w->genTerm.CharHeight;
  2516.      SrcY = 0;
  2517.      Width = w->genTerm.columns * w->genTerm.CharWidth;
  2518.      Height = (w->genTerm.rows + dist) * w->genTerm.CharHeight;
  2519.  
  2520.      XCopyArea(XtDisplay(w),XtWindow(w),XtWindow(w),w->genTerm.Pens[0]->draw_gc,
  2521.            0,SrcY,Width,Height,0,DestY);
  2522.      w->genTerm.ExposureCount++;
  2523.  
  2524.      Height = -dist * w->genTerm.CharHeight;
  2525.      SrcY = 0;
  2526.      XClearArea(XtDisplay(w),XtWindow(w),0,SrcY,Width,Height,False);
  2527.  
  2528.      GtMoveScreen(w,dist,FALSE);
  2529.      GtUpdateRegion(w,0,0,-dist,w->genTerm.columns,FALSE);
  2530.     }
  2531.    else
  2532.     {
  2533.      Width = w->genTerm.columns * w->genTerm.CharWidth;
  2534.      Height = w->genTerm.rows * w->genTerm.CharHeight;
  2535.      XClearArea(XtDisplay(w),XtWindow(w),0,0,Width,Height,False);
  2536.      GtMoveScreen(w,dist,FALSE);
  2537.      GtUpdateRegion(w,0,0,w->genTerm.rows,w->genTerm.columns,FALSE);
  2538.     }
  2539.   }
  2540.  if(w->genTerm.CursorFloats == FALSE)
  2541.   {
  2542.    w->genTerm.CursorOffset += dist;
  2543.    w->genTerm.CurrentRow -= dist;
  2544.   }
  2545. }
  2546.  
  2547. /* GtGetTopOfScreen:  This function returns to the calling program the */
  2548. /*  current position of the screen within the virtual screen. */
  2549.  
  2550. int GtGetTopOfScreen(w)
  2551.  
  2552. GenTermWidget w;
  2553.  
  2554. {
  2555.  return(w->genTerm.VirtualTop);
  2556. }
  2557.  
  2558. /* GtScrollRegionScrollScreen:  This function handles the row movement that */
  2559. /*  is required to perform a screen scroll when scroll regions are involved */
  2560.  
  2561. static int GtScrollRegionScrollScreen(w,dist)
  2562.  
  2563. GenTermWidget w;
  2564. int dist;
  2565.  
  2566. {
  2567.  int Top;
  2568.  int Bottom;
  2569.  int maxdist;
  2570.  int i;
  2571.  int ScreenTop = w->genTerm.ScreenTop;
  2572.  int Size = w->genTerm.SaveLines;
  2573.  int CurrentRegion;
  2574.  int Ypos,Width,Height;
  2575.  struct Line **Store;
  2576.  
  2577.  Store = (struct Line **) XtMalloc(sizeof(struct Line *) * abs(dist));
  2578.  
  2579.  if(dist > 0)
  2580.   {
  2581.    for(i = 0; i < w->genTerm.rows; i++)
  2582.     if(w->genTerm.Screen[i]->ScrollRegion != 0)
  2583.      {
  2584.       Top = i;
  2585.       break;
  2586.      }
  2587.    CurrentRegion = w->genTerm.Screen[Top]->ScrollRegion;
  2588.    for(i = Top; i < w->genTerm.rows; i++)
  2589.     if(w->genTerm.Screen[i]->ScrollRegion!=w->genTerm.Screen[Top]->ScrollRegion)
  2590.      break;
  2591.    Bottom = i - 1;
  2592.    if(Bottom >= w->genTerm.rows)
  2593.     Bottom = w->genTerm.rows - 1;
  2594.    maxdist = Bottom - Top;
  2595.  
  2596.    for(i = 0; i < dist; i++)
  2597.     Store[i] = w->genTerm.VScreen[(Top + i + ScreenTop) % Size];
  2598.  
  2599.    for(i = Top - 1; i >= 0; i--)
  2600.     w->genTerm.VScreen[(ScreenTop + i + dist) % Size] =
  2601.      w->genTerm.VScreen[(ScreenTop + i) % Size];
  2602.  
  2603.    for(i = 0; i < dist; i++)
  2604.     w->genTerm.VScreen[(ScreenTop + i) % Size] = Store[i];
  2605.  
  2606.    for(i = 0; i < dist; i++)
  2607.     Store[i] = w->genTerm.VScreen[(ScreenTop + w->genTerm.rows + i) % Size];
  2608.  
  2609.    for(i = w->genTerm.rows; i >= Bottom + dist; i--)
  2610.     w->genTerm.VScreen[(ScreenTop + i) % Size] =
  2611.      w->genTerm.VScreen[(ScreenTop + i - dist ) % Size];
  2612.  
  2613.    for(i = 0; i < dist; i++)
  2614.     {
  2615.      w->genTerm.VScreen[(ScreenTop + Bottom + 1 + i) % Size] =
  2616.       Store[i];
  2617.      w->genTerm.VScreen[(ScreenTop + Bottom + 1 + i) % 
  2618.             Size]->ScrollRegion = 
  2619.              w->genTerm.Screen[Top]->ScrollRegion;
  2620.     }
  2621.   }
  2622.  else
  2623.   {
  2624.    dist = abs(dist);
  2625.    for(i = w->genTerm.rows - 1; i >= 0; i--)
  2626.     if(w->genTerm.Screen[i]->ScrollRegion != 0)
  2627.      {
  2628.       Bottom = i;
  2629.       break;
  2630.      }
  2631.    CurrentRegion = w->genTerm.Screen[Bottom]->ScrollRegion;
  2632.    for(i = Bottom; i >= 0; i--)
  2633.     if(w->genTerm.Screen[i]->ScrollRegion != CurrentRegion)
  2634.      break;
  2635.    Top = i + 1;
  2636.    maxdist = Bottom - Top;
  2637.  
  2638.    for(i = 0; i < dist; i++)
  2639.     Store[i] = w->genTerm.VScreen[(ScreenTop - dist + i) % Size];
  2640.  
  2641.    for(i = 0; i < Top; i++)
  2642.     {
  2643.      w->genTerm.VScreen[(ScreenTop + i - dist) % Size] =
  2644.       w->genTerm.VScreen[(ScreenTop + i) % Size];
  2645.     }
  2646.  
  2647.    for(i = 0; i < dist; i++)
  2648.     {
  2649.      w->genTerm.VScreen[(ScreenTop + i + Top - dist) % Size] = Store[i];
  2650.      w->genTerm.VScreen[(ScreenTop + i + Top - dist) % Size]->ScrollRegion = 
  2651.       CurrentRegion;
  2652.     }
  2653.    if(Bottom < w->genTerm.rows - 1)
  2654.     {
  2655.      for(i = 0; i < dist; i++)
  2656.       Store[i] = w->genTerm.VScreen[(ScreenTop + Bottom - dist + 1 + i) % Size];
  2657.  
  2658.      for(i = Bottom; i < w->genTerm.rows; i++)
  2659.       w->genTerm.VScreen[(ScreenTop + 1 + i - dist) % Size] =
  2660.        w->genTerm.VScreen[(ScreenTop + 1 + i) % Size];
  2661.  
  2662.      for(i = 0; i < dist; i++)
  2663.       w->genTerm.VScreen[(ScreenTop + w->genTerm.rows + 1 + i) % Size] = Store[i];
  2664.     }
  2665.    dist = -dist;
  2666.   }
  2667.  Ypos = Top * w->genTerm.CharHeight;
  2668.  Width = w->genTerm.columns * w->genTerm.CharWidth;
  2669.  Height = (Bottom - Top + 1) * w->genTerm.CharHeight;
  2670.  XClearArea(XtDisplay(w),XtWindow(w),0,Ypos,Width,Height,False);
  2671.  GtMoveScreen(w,dist,False);
  2672.  GtUpdateRegion(w,Top,0,Bottom,w->genTerm.columns,False);
  2673.  XtFree(Store);
  2674.  return(maxdist);
  2675. }
  2676.  
  2677. /* GtGetLine:  This funtion returns to the calling program the data on the */
  2678. /*  current line.  The calling function must ensure that there is enough */
  2679. /*  space in string to store the data and the string is not null terminated. */
  2680.  
  2681. int GtGetLine(w,row,string,len)
  2682.  
  2683. GenTermWidget w;
  2684. int row;
  2685. char *string;
  2686. int *len;
  2687.  
  2688. {
  2689.  *len = 0;
  2690.  if((row >= 0) && (row < w->genTerm.rows))
  2691.   {
  2692.    *len = w->genTerm.Screen[row]->NumChars;
  2693.    strncpy(string,w->genTerm.Screen[row]->Characters,*len);
  2694.    return(GTGOOD);
  2695.   }
  2696.  return(GTBAD);
  2697. }
  2698.  
  2699. /* GtSetAttribute:  This function sets the attribute specified by which to */
  2700. /*  the value specified by flag. */
  2701.  
  2702. int GtSetAttribute(w,which,flag)
  2703.  
  2704. GenTermWidget w;
  2705. int which;
  2706. int flag;
  2707.  
  2708. {
  2709.  int row,col;
  2710.  int pen;
  2711.  
  2712.  /* a bit a kludge here.  GtLoadAttribute can be used to reset the current */
  2713.  /* settings on us.  Before we go and change attributes we check here if */
  2714.  /* the attributes were changed and if so make foreground and background */
  2715.  /* colors agree */
  2716.  
  2717.  if(w->genTerm.CurrentSettings.type == 0)
  2718.   {
  2719.    w->genTerm.foreground = w->genTerm.CurrentForeground = 
  2720.     w->genTerm.Colours[0].Foreground;
  2721.    w->genTerm.background = w->genTerm.CurrentBackground =
  2722.     w->genTerm.Colours[0].Background;
  2723.   }
  2724.  
  2725.  switch(which)
  2726.   {
  2727.  case GTFONT:
  2728.    if(flag == GTBASE)
  2729.     {
  2730.      w->genTerm.CurrentSettings.Font = w->genTerm.CurrentFont;
  2731.      w->genTerm.CurrentSettings.type &= ~SETALTFONT;
  2732.     }
  2733.    else if(flag == GTALTERNATE)
  2734.     {
  2735.      w->genTerm.CurrentSettings.Font = w->genTerm.AltFont;
  2736.      w->genTerm.CurrentSettings.type |= SETALTFONT;
  2737.     }
  2738.    else
  2739.     XtAppWarning(XtWidgetToApplicationContext(w),
  2740.          "Invalid argument for setting font");
  2741.    break;
  2742.  case GTINVERSEVIDEO:
  2743.    if(flag == GTNORMAL)
  2744.     {
  2745.      w->genTerm.foreground = w->genTerm.CurrentForeground;
  2746.      w->genTerm.background = w->genTerm.CurrentBackground;
  2747.      w->genTerm.CurrentSettings.type &= ~SETINVERSE;
  2748.     }
  2749.    else if(flag == GTINVERSE)
  2750.     {
  2751.      w->genTerm.foreground = w->genTerm.CurrentBackground;
  2752.      w->genTerm.background = w->genTerm.CurrentForeground;
  2753.      w->genTerm.CurrentSettings.type |= SETINVERSE;
  2754.     }
  2755.    else
  2756.     XtAppWarning(XtWidgetToApplicationContext(w),
  2757.          "Invalid argument for setting inverse video in GtSetAttribute");
  2758.    break;
  2759.  case GTUNDERLINE:
  2760.    if(flag == GTON)
  2761.     w->genTerm.CurrentSettings.type |= SETUNDERLINE;
  2762.    else if(flag == GTOFF)
  2763.     w->genTerm.CurrentSettings.type &= ~SETUNDERLINE;
  2764.    else
  2765.     XtAppWarning(XtWidgetToApplicationContext(w),
  2766.          "Invalid argument for setting underline mode in GtSetAttribute");
  2767.    break;
  2768.  case GTHALFBRIGHT:
  2769.    pen = w->genTerm.CurrentSettings.type >> NUMATTRIBUTES;
  2770.    if(flag == GTON)
  2771.     {
  2772.      GtDefineHalfBrightPen(w,pen);
  2773.      w->genTerm.foreground = w->genTerm.CurrentForeground =
  2774.       w->genTerm.Colours[pen].HalfBright;
  2775.      w->genTerm.CurrentSettings.type |= SETHALFBRIGHT;
  2776.     }
  2777.    else if(flag == GTOFF)
  2778.     {
  2779.      w->genTerm.foreground = w->genTerm.CurrentForeground =
  2780.       w->genTerm.Colours[pen].Foreground;
  2781.      w->genTerm.CurrentSettings.type &= ~SETHALFBRIGHT;
  2782.     }
  2783.    else
  2784.     XtAppWarning(XtWidgetToApplicationContext(w),
  2785.          "Invalid argument for setting halfbright mode in GtSetAttribute");
  2786.    break;
  2787.  case GTBLINKMODE:
  2788.    if(flag == GTON)
  2789.     {
  2790.      w->genTerm.CurrentSettings.type |= SETBLINK;
  2791.      if(w->genTerm.BlinkOn == -1) /* if first time set the timeout */
  2792.       {
  2793.        w->genTerm.BlinkTimeOut = 
  2794.     XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  2795.             (unsigned long)w->genTerm.BlinkRate,GtBlinkCallBack,w);
  2796.        w->genTerm.BlinkOn = 0;
  2797.       }
  2798.     }
  2799.    else if(flag == GTOFF)
  2800.     w->genTerm.CurrentSettings.type &= ~SETBLINK;
  2801.    else
  2802.     XtAppWarning(XtWidgetToApplicationContext(w),
  2803.          "Invalid argument for setting blink mode in GtSetAttribute");
  2804.    break;
  2805.  case GTPEN:
  2806.    if((flag < 0 ) || (flag >= w->genTerm.NumberColours))
  2807.     XtAppWarning(XtWidgetToApplicationContext(w),
  2808.          "Invalid pen number in GtSetAttribute");
  2809.    else
  2810.     {
  2811.      GtDefinePen(w,flag);
  2812.      w->genTerm.foreground = w->genTerm.CurrentForeground = 
  2813.       w->genTerm.Colours[flag].Foreground;
  2814.      w->genTerm.background = w->genTerm.CurrentBackground = 
  2815.       w->genTerm.Colours[flag].Background;
  2816.  
  2817.      /* must now check other attributes to make sure we put the colour in the */
  2818.      /* correct spot */
  2819.  
  2820.      if(w->genTerm.CurrentSettings.type & SETHALFBRIGHT)
  2821.       {
  2822.        GtDefineHalfBrightPen(w,flag);
  2823.        w->genTerm.foreground = w->genTerm.CurrentForeground =
  2824.     w->genTerm.Colours[flag].HalfBright;
  2825.       }
  2826.      if(w->genTerm.CurrentSettings.type & SETINVERSE)
  2827.       {
  2828.        w->genTerm.foreground = w->genTerm.CurrentBackground;
  2829.        w->genTerm.background = w->genTerm.CurrentForeground;
  2830.       }
  2831.      w->genTerm.CurrentSettings.type &= BASEATTRIBUTES;
  2832.      w->genTerm.CurrentSettings.type |= (flag << NUMATTRIBUTES);
  2833.     }
  2834.    break;
  2835.  default:
  2836.    XtAppWarning(XtWidgetToApplicationContext(w),
  2837.         "Invalid attribute specified in GtSetAttribute");
  2838.   }
  2839.  if(w->genTerm.Pens[w->genTerm.CurrentSettings.type] == NULL)
  2840.   GtCreatePens(w,w->genTerm.CurrentSettings.type);
  2841.  return(GTGOOD);
  2842. }
  2843.  
  2844. /* GtGetAttribute: This function returns the current value of the requested */
  2845. /*  attribute */
  2846.  
  2847. int GtGetAttribute(w,which,flag)
  2848.  
  2849. GenTermWidget w;
  2850. int which;
  2851. int *flag;
  2852.  
  2853. {
  2854.  switch(which)
  2855.   {
  2856.  case GTFONT:
  2857.    if(w->genTerm.CurrentSettings.type & SETALTFONT)
  2858.     *flag = 1;
  2859.    else
  2860.     *flag = 0;
  2861.    break;
  2862.  case GTINVERSEVIDEO:
  2863.    if(w->genTerm.CurrentSettings.type & SETINVERSE)
  2864.     *flag = 1;
  2865.    else
  2866.     *flag = 0;
  2867.    break;
  2868.  case GTUNDERLINE:
  2869.    if(w->genTerm.CurrentSettings.type & SETUNDERLINE)
  2870.     *flag = 1;
  2871.    else
  2872.     *flag = 0;
  2873.    break;
  2874.  case GTHALFBRIGHT:
  2875.    if(w->genTerm.CurrentSettings.type & SETHALFBRIGHT)
  2876.     *flag = 1;
  2877.    else
  2878.     *flag = 0;
  2879.    break;
  2880.  case GTBLINKMODE:
  2881.    if(w->genTerm.CurrentSettings.type & SETBLINK)
  2882.     *flag = 1;
  2883.    else
  2884.     *flag = 0;
  2885.    break;
  2886.  case GTPEN:
  2887.    *flag = w->genTerm.CurrentSettings.type >> NUMATTRIBUTES;
  2888.    break;
  2889.  default:
  2890.    XtAppWarning(XtWidgetToApplicationContext(w),
  2891.         "Invalid attribute specified in GtGetAttribute");
  2892.   }
  2893.  return(GTGOOD);
  2894. }
  2895.  
  2896. /* GtSetFieldAttribute:  This function copies the current attribute through */
  2897. /*  out the current field.  The end of the field is defined by the type */
  2898. /*  Argument.  If type = 0 then the field extends to the end of the line. */
  2899. /*  If type=1 then the field extends to the end of the line or the next */
  2900. /*  non blank character.  If type=2 then the field extends to the end of the */
  2901. /*  line or the next non-blank character which does not have a field */
  2902. /*  attribute set. If type=3 then the field extends to the end of the line */
  2903. /*  or until the next charater which does not have a field attribute set. */
  2904.  
  2905. int GtSetFieldAttribute(w,type)
  2906.  
  2907. GenTermWidget w;
  2908. int type;
  2909.  
  2910. {
  2911.  int row,col;
  2912.  int i,j;
  2913.  int start,end;
  2914.  struct Field *NewFields;
  2915.  
  2916.  if(w->genTerm.Locked)
  2917.   return(GTLOCKED);
  2918.  
  2919.  if(w->genTerm.FieldAttributes == TRUE)    /* is nop if not in field mode */
  2920.   {
  2921.    GtCheckPosition(w);
  2922.    row = w->genTerm.CurrentRow;
  2923.    i = col = w->genTerm.CurrentColumn;
  2924.    for(i = 0; i < w->genTerm.Screen[row]->NumFields; i++)
  2925.     if((w->genTerm.Screen[row]->Fields[i].Start <= col) &&
  2926.        (w->genTerm.Screen[row]->Fields[i].End >= col))
  2927.      {
  2928.       if(col == w->genTerm.Screen[row]->Fields[i].Start)
  2929.        {
  2930.     start = w->genTerm.Screen[row]->Fields[i].Start;
  2931.     end = w->genTerm.Screen[row]->Fields[i].End;
  2932.        }
  2933.       else
  2934.        {
  2935.     NewFields = (struct Field *) XtMalloc(sizeof(struct Field) * 
  2936.                           (w->genTerm.Screen[row]->NumFields
  2937.                            + 1));
  2938.     for(j = 0; j <= i; j++)
  2939.      {
  2940.       NewFields[j].Start = w->genTerm.Screen[row]->Fields[j].Start;
  2941.       NewFields[j].End = w->genTerm.Screen[row]->Fields[j].End;
  2942.      }
  2943.     start = col;
  2944.     end = NewFields[i].End;
  2945.     NewFields[i].End = start - 1;
  2946.     NewFields[i+1].Start = start;
  2947.     NewFields[i+1].End = end;
  2948.     for(j = i+1; j < w->genTerm.Screen[row]->NumFields; j++)
  2949.      {
  2950.       NewFields[j+1].Start = w->genTerm.Screen[row]->Fields[j].Start;
  2951.       NewFields[j+1].End = w->genTerm.Screen[row]->Fields[j].End;
  2952.      }
  2953.     XtFree(w->genTerm.Screen[row]->Fields);
  2954.     w->genTerm.Screen[row]->Fields = NewFields;
  2955.     w->genTerm.Screen[row]->NumFields += 1;
  2956.        }
  2957.       break;
  2958.      }
  2959.    if(end >= w->genTerm.columns)
  2960.     end = w->genTerm.columns - 1;
  2961.  
  2962.    for(i = start; i <= end; i++)
  2963.     {
  2964.      memcpy(&(w->genTerm.Screen[row]->Attributes[i]),
  2965.         &(w->genTerm.CurrentSettings),sizeof(struct Attribute));
  2966.     }
  2967.    w->genTerm.Screen[row]->NumChars = w->genTerm.columns;
  2968.    GtUpdateRegion(w,row,col,row,w->genTerm.columns,True);
  2969.   }
  2970.  else
  2971.   return(GTBAD);
  2972.  return(GTGOOD);
  2973. }
  2974.  
  2975. /* GtLoadAttribute:  This function sets the current attribute to be the */
  2976. /*  attribute of the character at position row,col.  If that position is */
  2977. /*  invalid, the default attribute is loaded. */
  2978.  
  2979. int GtLoadAttribute(w,row,col)
  2980.  
  2981. GenTermWidget w;
  2982. int row,col;
  2983.  
  2984. {
  2985.  if(GtCheckValidPosition(w,&row,&col,0) == 0)
  2986.   memcpy(&(w->genTerm.CurrentSettings),&(w->genTerm.DefaultSettings),
  2987.      sizeof(struct Attribute));
  2988.  else
  2989.   memcpy(&(w->genTerm.CurrentSettings),
  2990.      &(w->genTerm.Screen[row]->Attributes[col]),sizeof(struct Attribute));
  2991.  return(GTGOOD);
  2992. }
  2993.  
  2994. /* GtClearRegion:  This function clears the region of the screen specified */
  2995. /*  by the starting and ending row,column pair. */
  2996.  
  2997. int GtClearRegion(w,startrow,startcolumn,endrow,endcolumn)
  2998.  
  2999. GenTermWidget w;
  3000. int startrow,startcolumn,endrow,endcolumn;
  3001.  
  3002. {
  3003.  int i,j,k;
  3004.  int end;
  3005.  int xpos,ypos,height,width;
  3006.  int type;
  3007.  
  3008.  if(w->genTerm.Locked)
  3009.   return(GTLOCKED);
  3010.  
  3011.  GtCheckPosition(w);
  3012.  GtUnShowCursor(w);
  3013.  
  3014.  GtCheckValidPosition(w,&startrow,&startcolumn,1);
  3015.  GtCheckValidPosition(w,&endrow,&endcolumn,1);
  3016.  
  3017.  for(i = startrow; i <= endrow; i++)
  3018.   {
  3019.    end = endcolumn;
  3020.    if(w->genTerm.Screen[i]->NumChars < end)
  3021.     end = w->genTerm.Screen[i]->NumChars;
  3022.    for(j = startcolumn; j <= end; j++)
  3023.     {
  3024.      w->genTerm.Screen[i]->Characters[j] = ' ';
  3025.      memcpy(&(w->genTerm.Screen[i]->Attributes[j]),&(w->genTerm.DefaultSettings),
  3026.         sizeof(struct Attribute));
  3027.     }
  3028.  
  3029.    /* we now have to update the attribute fields, if they are being used */
  3030.    if(w->genTerm.FieldAttributes)
  3031.     {
  3032.      k = 0;
  3033.      w->genTerm.Screen[i]->Fields[k].Start = 0;
  3034.      type = w->genTerm.Screen[i]->Attributes[0].type;
  3035.      for(j = 0; j < end; j++)
  3036.       if(type != w->genTerm.Screen[i]->Attributes[j].type)
  3037.        {
  3038.     w->genTerm.Screen[i]->Fields[k++].End = j - 1;
  3039.     w->genTerm.Screen[i]->Fields[k].Start = j;
  3040.     type = w->genTerm.Screen[i]->Attributes[j].type;
  3041.        }
  3042.      w->genTerm.Screen[i]->Fields[k].End = w->genTerm.columns - 1;
  3043.      w->genTerm.Screen[i]->NumFields = k + 1;
  3044.     }
  3045.    /*   w->genTerm.Screen[i]->NumFields = 1;
  3046.     w->genTerm.Screen[i]->Fields[0].Start = 0;
  3047.     w->genTerm.Screen[i]->Fields[0].End = w->genTerm.columns - 1;*/
  3048.    if(w->genTerm.Screen[i]->NumChars > startcolumn)
  3049.     w->genTerm.Screen[i]->NumChars = startcolumn;
  3050.   }
  3051.  xpos = startcolumn * w->genTerm.CharWidth;
  3052.  ypos = startrow * w->genTerm.CharHeight;
  3053.  height = (endrow - startrow + 1) * w->genTerm.CharHeight;
  3054.  width = (endcolumn - startcolumn + 1) * w->genTerm.CharWidth;
  3055.  XClearArea(XtDisplay(w),XtWindow(w),xpos,ypos,width,height,False);
  3056.  
  3057.  GtShowCursor(w);
  3058.  return(GTGOOD);
  3059. }
  3060.  
  3061. /* GtCheckValidPosition:  This function takes a row,column pair at checks to */
  3062. /*  see if it is valid.  If it is invalid and flag=0 then an error is */
  3063. /*  returned.  If flag=1, the position is corrected. */
  3064.  
  3065. static int GtCheckValidPosition(w,row,col,flag)
  3066.  
  3067. GenTermWidget w;
  3068. int *row,*col;
  3069. int flag;
  3070.  
  3071. {
  3072.  if((*row >= 0) && (*row < w->genTerm.rows) && (*col >= 0) && 
  3073.     (*col < w->genTerm.columns))
  3074.   return(1);
  3075.  else
  3076.   {
  3077.    if(flag == 0)
  3078.     return(0);
  3079.    else
  3080.     {        /* we have an error and caller wants us to correct it */
  3081.      if(*row < 0)
  3082.       *row = 0;
  3083.      if(*row >= w->genTerm.rows)
  3084.       *row = w->genTerm.rows - 1;
  3085.      if(*col < 0)
  3086.       *col = 0;
  3087.      if(*col >= w->genTerm.columns)
  3088.       *col = w->genTerm.columns - 1;
  3089.      return(1);
  3090.     }
  3091.   }
  3092. }
  3093.  
  3094. /* GtClearMemory:  This function clears the data in the virtual screen. If */
  3095. /*  flag=0, the entire virtual screen is cleared.  If flag=1, the portion */
  3096. /*  of the virtual screen, currently below the screen is cleared.  If */
  3097. /*  flag=2 then the portion of the virtual screen currently above the screen */
  3098. /*  is cleared */
  3099.  
  3100. int GtClearMemory(w,flag)
  3101.  
  3102. GenTermWidget w;
  3103. int flag;
  3104.  
  3105. {
  3106.  int StartRow,EndRow;
  3107.  int i,j;
  3108.  int Width,Height;
  3109.  
  3110.  if(w->genTerm.Locked)
  3111.   return(GTLOCKED);
  3112.  switch(flag)
  3113.   {
  3114.  case 0:
  3115.    StartRow = 0;
  3116.    EndRow = w->genTerm.SaveLines;
  3117.    break;
  3118.  case 1:
  3119.    StartRow = w->genTerm.VirtualTop + w->genTerm.rows;
  3120.    EndRow = w->genTerm.MaxVirtualTop + w->genTerm.rows;
  3121.    break;
  3122.  case 2:
  3123.    StartRow = 0;
  3124.    EndRow = w->genTerm.VirtualTop;
  3125.    break;
  3126.  default:
  3127.    XtAppWarning(XtWidgetToApplicationContext(w),
  3128.         "Bad value to GtClearMemory");
  3129.    return(0);
  3130.   }
  3131.  for(i = StartRow; i < EndRow; i++)
  3132.   {
  3133.    for(j = 0; j < w->genTerm.VScreen[i]->NumChars; j++)
  3134.     {
  3135.      w->genTerm.VScreen[i]->Characters[j] = ' ';
  3136.      memcpy(&(w->genTerm.VScreen[i]->Attributes[j]),
  3137.         &(w->genTerm.DefaultSettings),sizeof(struct Attribute));
  3138.     }
  3139.    w->genTerm.VScreen[i]->NumChars = 0;
  3140.    w->genTerm.VScreen[i]->NumFields = 1;
  3141.    w->genTerm.VScreen[i]->Fields[0].Start = 0;
  3142.    w->genTerm.VScreen[i]->Fields[0].End = w->genTerm.columns - 1;
  3143.   }
  3144.  if(flag == 0)            /* we also cleared the screen so blank it */
  3145.   {
  3146.    Width = w->genTerm.columns * w->genTerm.CharWidth;
  3147.    Height = w->genTerm.rows * w->genTerm.CharHeight;
  3148.    XClearArea(XtDisplay(w),XtWindow(w),0,0,Width,Height,False);
  3149.   }  
  3150.  return(GTGOOD);
  3151. }
  3152.  
  3153. /* GtInsertLines:  This function inserts num rows at position row.  The  */
  3154. /*  insertion is performed by moving and clearing the last num lines in the */
  3155. /*  virtual screen to the current row. */
  3156.  
  3157. int GtInsertLines(w,row,num)
  3158.  
  3159. GenTermWidget w;
  3160. int row;
  3161. int num;
  3162.  
  3163. {
  3164.  int From,To,i;
  3165.  int Xpos,Ypos, Hght,Wid;
  3166.  int NewYpos,CpyHght;
  3167.  struct Line **Hold;
  3168.  
  3169.  if((row < 0) || (row >= w->genTerm.rows))
  3170.   return(GTBAD);
  3171.  
  3172.  if(w->genTerm.Locked)
  3173.   return(GTLOCKED);
  3174.  
  3175.  GtCheckPosition(w);
  3176.  GtUnShowCursor(w);
  3177.  
  3178.  /* first calculate where in the virtual screen we are to put the new lines */
  3179.  
  3180.  To = w->genTerm.ScreenTop + row;
  3181.  From = w->genTerm.VScreenTop + w->genTerm.rows + 1  - num;
  3182.  Hold = (struct Line **) XtMalloc(sizeof(struct Line *) * num);
  3183.  for(i = 0; i < num; i++)
  3184.   {
  3185.    Hold[i] = w->genTerm.VScreen[(From + i) % w->genTerm.SaveLines];
  3186.   }
  3187.  for(i = From; i >= (To + num); i--)
  3188.   {
  3189.    w->genTerm.VScreen[i % w->genTerm.SaveLines] =
  3190.     w->genTerm.VScreen[(i - num) % w->genTerm.SaveLines];
  3191.   }
  3192.  for(i = 0; i < num; i++)
  3193.   {
  3194.    w->genTerm.VScreen[(To + i) % w->genTerm.SaveLines] = Hold[i];
  3195.    GtClearLine(w,w->genTerm.VScreen[(To + i) % w->genTerm.SaveLines]);
  3196.   }
  3197.  for(i = 0; i < w->genTerm.rows; i++)
  3198.   w->genTerm.Screen[i] =
  3199.    w->genTerm.VScreen[(w->genTerm.ScreenTop + i) % w->genTerm.SaveLines];
  3200.  
  3201.  Xpos = 0;
  3202.  Ypos = row * w->genTerm.CharHeight;
  3203.  Hght = num * w->genTerm.CharHeight;
  3204.  Wid = w->genTerm.columns * w->genTerm.CharWidth;
  3205.  if((row + num) < w->genTerm.rows)
  3206.   {
  3207.    NewYpos = (row + num) * w->genTerm.CharHeight;
  3208.    CpyHght = (w->genTerm.rows - row - num) * w->genTerm.CharHeight;
  3209.    XCopyArea(XtDisplay(w),XtWindow(w),XtWindow(w),w->genTerm.Pens[0]->draw_gc,
  3210.          Xpos,Ypos,Wid,CpyHght,Xpos,NewYpos);
  3211.    w->genTerm.ExposureCount++;
  3212.   }
  3213.  XClearArea(XtDisplay(w),XtWindow(w),Xpos,Ypos,Wid,Hght,False);
  3214.  XtFree(Hold);
  3215.  
  3216.  /* since we might have scrolled down in memory we increase MaxVirtualTop */
  3217.  /* to allow us to get there */
  3218.  
  3219.  if(w->genTerm.ScrollOnOutput == False)
  3220.   {
  3221.    w->genTerm.MaxVirtualTop += num;
  3222.    if(w->genTerm.MaxVirtualTop > w->genTerm.SaveLines - w->genTerm.rows)
  3223.     w->genTerm.MaxVirtualTop = w->genTerm.SaveLines - w->genTerm.rows;
  3224.    if(w->genTerm.MaxVirtualTop >= w->genTerm.SaveLines - w->genTerm.rows)
  3225.     {
  3226.      w->genTerm.VirtualTop -= num;
  3227.      if(w->genTerm.VirtualTop < 0)
  3228.       w->genTerm.VirtualTop = 0;
  3229.      GtDoTopCallback(w);
  3230.     }
  3231.   }
  3232.  GtShowCursor(w);
  3233.  return(GTGOOD);
  3234. }
  3235.  
  3236. /* GtDeleteLines:  This function deletes num rows starting at the current */
  3237. /*  postion.  The deletion is performed by moving and clearing the rows to */
  3238. /*  the last num lines in the virtual screen to the current row. */
  3239.  
  3240. int GtDeleteLines(w,num)
  3241.  
  3242. GenTermWidget w;
  3243. int num;
  3244.  
  3245. {
  3246.  int From,To,i;
  3247.  int Xpos,Ypos, Hght,Wid;
  3248.  int NewYpos,CpyHght;
  3249.  struct Line **Hold;
  3250.  
  3251.  if(w->genTerm.Locked)
  3252.   return(GTLOCKED);
  3253.  
  3254.  GtCheckPosition(w);
  3255.  GtUnShowCursor(w);
  3256.  
  3257.  /* first calculate where in the virtual screen we are to put the new lines */
  3258.  
  3259.  From = w->genTerm.ScreenTop + w->genTerm.CurrentRow;
  3260.  To = w->genTerm.VScreenTop + w->genTerm.rows + 1  - num;
  3261.  Hold = (struct Line **) XtMalloc(sizeof(struct Line *) * num);
  3262.  for(i = 0; i < num; i++)
  3263.   {
  3264.    Hold[i] = w->genTerm.VScreen[(From + i) % w->genTerm.SaveLines];
  3265.   }
  3266.  for(i = From; i < (To + num); i++)
  3267.   {
  3268.    w->genTerm.VScreen[i % w->genTerm.SaveLines] =
  3269.     w->genTerm.VScreen[(i + num) % w->genTerm.SaveLines];
  3270.   }
  3271.  for(i = 0; i < num; i++)
  3272.   {
  3273.    w->genTerm.VScreen[(To + i) % w->genTerm.SaveLines] = Hold[i];
  3274.    GtClearLine(w,w->genTerm.VScreen[(To + i) % w->genTerm.SaveLines]);
  3275.   }
  3276.  for(i = 0; i < w->genTerm.rows; i++)
  3277.   w->genTerm.Screen[i] =
  3278.    w->genTerm.VScreen[(w->genTerm.ScreenTop + i) % w->genTerm.SaveLines];
  3279.  
  3280.  Xpos = 0;
  3281.  Ypos = (w->genTerm.CurrentRow + num) * w->genTerm.CharHeight;
  3282.  Hght = num * w->genTerm.CharHeight;
  3283.  Wid = w->genTerm.columns * w->genTerm.CharWidth;
  3284.  if((w->genTerm.CurrentRow + num) < w->genTerm.rows)
  3285.   {
  3286.    NewYpos = (w->genTerm.CurrentRow) * w->genTerm.CharHeight;
  3287.    CpyHght = (w->genTerm.rows - w->genTerm.CurrentRow - num) * 
  3288.     w->genTerm.CharHeight;
  3289.    XCopyArea(XtDisplay(w),XtWindow(w),XtWindow(w),w->genTerm.Pens[0]->draw_gc,
  3290.          Xpos,Ypos,Wid,CpyHght,Xpos,NewYpos);
  3291.    w->genTerm.ExposureCount++;
  3292.   }
  3293.  Ypos = (w->genTerm.rows - num) * w->genTerm.CharHeight;
  3294.  XClearArea(XtDisplay(w),XtWindow(w),Xpos,Ypos,Wid,Hght,False);
  3295.  GtUpdateRegion(w,w->genTerm.rows - num,0,w->genTerm.rows,w->genTerm.columns,
  3296.         False);
  3297.  XtFree(Hold);
  3298.  
  3299.  GtShowCursor(w);
  3300.  return(GTGOOD);
  3301. }
  3302.  
  3303. /* GtDeleteCharacters:  This function deletes num characters from the current*/
  3304. /*  line starting at the current position. */
  3305.  
  3306. int GtDeleteCharacters(w,num)
  3307.  
  3308. GenTermWidget w;
  3309. int num;
  3310.  
  3311. {
  3312.  int row;
  3313.  int max;
  3314.  int i;
  3315.  int Xpos,Ypos,Width,Height;
  3316.  
  3317.  if(w->genTerm.Locked)
  3318.   return(GTLOCKED);
  3319.  
  3320.  GtCheckPosition(w);
  3321.  GtUnShowCursor(w);
  3322.  
  3323.  row = w->genTerm.CurrentRow;
  3324.  max = w->genTerm.Screen[row]->NumChars;
  3325.  
  3326.  for(i = w->genTerm.CurrentColumn + num; i < max; i++)
  3327.   {
  3328.    w->genTerm.Screen[row]->Characters[i - num] = 
  3329.     w->genTerm.Screen[row]->Characters[i];
  3330.    memcpy(&(w->genTerm.Screen[row]->Attributes[i - num]),
  3331.       &(w->genTerm.Screen[row]->Attributes[i]),sizeof(struct Attribute));
  3332.   }
  3333.  for(i = max - num; i < max; i++)
  3334.   {
  3335.    w->genTerm.Screen[row]->Characters[i] = ' ';
  3336.    memcpy(&(w->genTerm.Screen[row]->Attributes[i]),&(w->genTerm.DefaultSettings),
  3337.       sizeof(struct Attribute));
  3338.   }
  3339.  w->genTerm.Screen[row]->NumChars -= num;
  3340.  if(w->genTerm.Screen[row]->NumChars < w->genTerm.CurrentColumn)
  3341.   w->genTerm.Screen[row]->NumChars = w->genTerm.CurrentColumn;
  3342.  
  3343.  Xpos = w->genTerm.Screen[row]->NumChars * w->genTerm.CharWidth;
  3344.  Ypos = row * w->genTerm.CharHeight;
  3345.  Width = (w->genTerm.rows - w->genTerm.Screen[row]->NumChars) * 
  3346.   w->genTerm.CharWidth;
  3347.  Height = w->genTerm.CharHeight;
  3348.  XClearArea(XtDisplay(w),XtWindow(w),Xpos,Ypos,Width,Height,False);
  3349.  GtUpdateRegion(w,row,w->genTerm.CurrentColumn,row,
  3350.         w->genTerm.Screen[row]->NumChars,False);
  3351.  
  3352.  GtShowCursor(w);
  3353.  return(GTGOOD);
  3354. }
  3355.  
  3356. /* GtSetScrollRegion:  This function sets a scroll region between the given */
  3357. /*  starting and ending rows. */
  3358.  
  3359. int GtSetScrollRegion(w,start,end,num)
  3360.  
  3361. GenTermWidget w;
  3362. int start;
  3363. int end;
  3364. int num;
  3365.  
  3366. {
  3367.  int i;
  3368.  
  3369.  if((start < 0) || (start >= w->genTerm.rows) || (end < 0) || 
  3370.     (end >= w->genTerm.rows))
  3371.   {
  3372.    XtAppWarning(XtWidgetToApplicationContext(w),
  3373.         "Invalid starting or ending row in GtSetScrollRegion");
  3374.    return(GTBAD);
  3375.   }
  3376.  else
  3377.   {
  3378.    for(i = start; i <= end; i++)
  3379.     w->genTerm.Screen[i]->ScrollRegion = num;
  3380.   }
  3381.  return(GTGOOD);
  3382. }
  3383.  
  3384. /* GtGetScrollRegion: This function returns the bounds on a given scroll */
  3385. /*  region. */
  3386.  
  3387. int GtGetScrollRegion(w,num,start,end)
  3388.  
  3389. GenTermWidget w;
  3390. int num;
  3391. int *start,*end;
  3392.  
  3393. {
  3394.  int i;
  3395.  
  3396.  *start = -1;
  3397.  *end = w->genTerm.rows;
  3398.  
  3399.  for(i = 0; i < w->genTerm.rows; i++)
  3400.   if(w->genTerm.Screen[i]->ScrollRegion == num)
  3401.    {
  3402.     *start = i;
  3403.     break;
  3404.    }
  3405.  if(*start != -1)
  3406.   {
  3407.    *end = w->genTerm.rows - 1;
  3408.    for(i = *start; i < w->genTerm.rows; i++)
  3409.     if(w->genTerm.Screen[i]->ScrollRegion != num)
  3410.      {
  3411.       *end = i - 1;
  3412.       break;
  3413.      }
  3414.   }
  3415.  return(GTGOOD);
  3416. }
  3417.  
  3418. /* GtGetMapTable: This function returns a copy of the current output mapping */
  3419. /*  table. */
  3420.  
  3421. int GtGetMapTable(w,Table)
  3422.  
  3423. GenTermWidget w;
  3424. char Table[NMAP];
  3425.  
  3426. {
  3427.  int i;
  3428.  
  3429.  for(i = 0; i < NMAP; i++)
  3430.   Table[i] = w->genTerm.MapTable[i];
  3431.  return(GTGOOD);
  3432. }
  3433.  
  3434. /* GtSetMapTable:  This function set the values for the current output */
  3435. /*  mapping table. */
  3436.  
  3437. int GtSetMapTable(w,Table)
  3438.  
  3439. GenTermWidget w;
  3440. char Table[NMAP];
  3441.  
  3442. {
  3443.  int i;
  3444.  
  3445.  for(i = 0; i < NMAP; i++)
  3446.   w->genTerm.MapTable[i] = Table[i];
  3447.  return(GTGOOD);
  3448. }
  3449.  
  3450. static void GtStartSelection(w,event,names,num)
  3451.  
  3452. GenTermWidget w;
  3453. XEvent *event;
  3454. char *names[];
  3455. Cardinal *num;
  3456.  
  3457. {
  3458.  int Row,Col;
  3459.  
  3460.  GtUnHighLightSection(w);
  3461.  if(w->genTerm.CurrentSelection != NULL)
  3462.   {
  3463.    XtFree(w->genTerm.CurrentSelection);
  3464.    w->genTerm.CurrentSelection = NULL;
  3465.   }
  3466.  w->genTerm.SelectionStarted = True;
  3467.  GtPositionToRowColumn(w,event->xbutton.x,event->xbutton.y,&Row,&Col);
  3468.  w->genTerm.SelectionStartRow = Row;
  3469.  w->genTerm.SelectionStartCol = Col;
  3470.  w->genTerm.SelectionEndRow = Row;
  3471.  w->genTerm.SelectionEndCol = Col;
  3472.  w->genTerm.Locked = True;
  3473.  GtDoLockCallback(w);
  3474. }
  3475.  
  3476. static void GtExtendSelection(w,event,names,num)
  3477.  
  3478. GenTermWidget w;
  3479. XEvent *event;
  3480. char *names[];
  3481. Cardinal *num;
  3482.  
  3483. {
  3484.  int Row,Col;
  3485.  int OldEndRow;
  3486.  
  3487.  if(w->genTerm.SelectionStarted == False)
  3488.   {
  3489.    XtAppWarning(XtWidgetToApplicationContext(w),
  3490.         "GenTerm widget gets extend selection action before start selection!");
  3491.   }
  3492.  else
  3493.   {
  3494.    GtPositionToRowColumn(w,event->xbutton.x,event->xbutton.y,&Row,&Col);
  3495.    OldEndRow = w->genTerm.SelectionEndRow;
  3496.    w->genTerm.SelectionEndRow = Row;
  3497.    w->genTerm.SelectionEndCol = Col;
  3498.    GtHighLightSection(w,OldEndRow);
  3499.   }
  3500. }
  3501.  
  3502. static void GtHighLightSection(w,OldRow)
  3503.  
  3504. GenTermWidget w;
  3505. int OldRow;
  3506.  
  3507. {
  3508.  int StartRow = w->genTerm.SelectionStartRow;
  3509.  int EndRow = w->genTerm.SelectionEndRow;
  3510.  int StartCol = w->genTerm.SelectionStartCol;
  3511.  int EndCol = w->genTerm.SelectionEndCol;
  3512.  int EndLine;
  3513.  int i,j;
  3514.  int First,Last;
  3515.  
  3516.  /* first erase old highlight section */
  3517.  
  3518.  if(StartRow < OldRow)
  3519.   {
  3520.    First = StartRow;
  3521.    Last = OldRow;
  3522.   }
  3523.  else
  3524.   {
  3525.    First = OldRow;
  3526.    Last = StartRow;
  3527.   }
  3528.  for(i = First; i <= Last; i++)
  3529.   {
  3530.    EndLine = MIN(w->genTerm.Screen[i]->NumChars,w->genTerm.columns-1);
  3531.    for(j = 0; j <= EndLine; j++)
  3532.     w->genTerm.Screen[i]->Attributes[j].type &= ~HIGHLIGHT;
  3533.   }
  3534.  
  3535.  GtSortStartEnd(&StartRow,&StartCol,&EndRow,&EndCol);
  3536.  
  3537.  EndLine = MIN(w->genTerm.Screen[StartRow]->NumChars,w->genTerm.columns-1);
  3538.  if(EndRow == StartRow)
  3539.   EndLine = MIN(EndLine,EndCol);
  3540.  for(i = StartCol; i <= EndLine; i++)
  3541.   w->genTerm.Screen[StartRow]->Attributes[i].type |= HIGHLIGHT;
  3542.  
  3543.  if(EndRow != StartRow)
  3544.   {
  3545.    for(i = StartRow + 1; i < EndRow; i++)
  3546.     {
  3547.      EndLine = MIN(w->genTerm.Screen[i]->NumChars,w->genTerm.columns-1);
  3548.      for(j = 0; j <= EndLine; j++)
  3549.       w->genTerm.Screen[i]->Attributes[j].type |= HIGHLIGHT;
  3550.     }
  3551.    EndLine = MIN(w->genTerm.Screen[i]->NumChars,EndCol);
  3552.    for(i = 0; i <= EndLine; i++)
  3553.     w->genTerm.Screen[EndRow]->Attributes[i].type |= HIGHLIGHT;
  3554.   }
  3555.  GtUpdateRegion(w,First,0,Last,w->genTerm.columns,True);
  3556.  GtUpdateRegion(w,StartRow,0,EndRow,w->genTerm.columns,True);
  3557. }
  3558.  
  3559. /* MyUnHightLightSection:  This function searches throught the entire */
  3560. /*  screen searching for characters with the highlight bit set.  If it finds */
  3561. /*  any it clears the bit and, if the character was visible, it calls */
  3562. /*  GtUpdateRegion to redraw that section of the screen.  Note that we can't */
  3563. /*  use the selection row column pointers because they are not valid after */
  3564. /*  the selection is completed because they aren't updated during screen */
  3565. /*  scrolling.  */
  3566.  
  3567. static void GtUnHighLightSection(w)
  3568.  
  3569. GenTermWidget w;
  3570.  
  3571. {
  3572.  int i,j;
  3573.  int StartRow = -1;
  3574.  int EndRow;
  3575.  
  3576.  /* first look in the visible screen */
  3577.  
  3578.  for(i = 0; i < w->genTerm.rows; i++)
  3579.   {
  3580.    for(j = 0; j < w->genTerm.columns; j++)
  3581.     if(w->genTerm.Screen[i]->Attributes[j].type & HIGHLIGHT)
  3582.      {
  3583.       w->genTerm.Screen[i]->Attributes[j].type &= ~HIGHLIGHT;
  3584.       if(StartRow == -1)
  3585.        StartRow = i;
  3586.       EndRow = i;
  3587.      }
  3588.   }
  3589.  /* now update any area that needs doing */
  3590.  
  3591.  if(StartRow != -1)
  3592.   GtUpdateRegion(w,StartRow,0,EndRow,w->genTerm.columns,True);
  3593.  
  3594.  /* finally, look for things that might have scrolled off the screen */
  3595.  
  3596.  for(i = 0; i < w->genTerm.SaveLines; i++)
  3597.   for(j = 0; j < w->genTerm.columns; j++)
  3598.    if(w->genTerm.VScreen[i]->Attributes[j].type & HIGHLIGHT)
  3599.     w->genTerm.VScreen[i]->Attributes[j].type &= ~HIGHLIGHT;
  3600.  
  3601. }
  3602.  
  3603. static void GtMakeSelection(w,event,names,num)
  3604.  
  3605. GenTermWidget w;
  3606. XEvent *event;
  3607. char *names[];
  3608. Cardinal *num;
  3609.  
  3610. {
  3611.  char *selection;
  3612.  Atom atom;
  3613.  int which = 0;
  3614.  int buffer = -1;
  3615.  int i;
  3616.  
  3617.  for(i = 0; i < *num; i++)
  3618.   {
  3619.    if(strcmp(names[i],"PRIMARY") == 0)
  3620.     which = 0;
  3621.    else if(strcmp(names[i],"SECONDARY") == 0)
  3622.     which = 1;
  3623.    else if(strncmp(names[i],"CUT_BUFFER",strlen("CUT_BUFFER")) == 0)
  3624.     {
  3625.      buffer = names[i][strlen("CUT_BUFFER")] - '0';
  3626.     }
  3627.   }
  3628.  
  3629.  if(w->genTerm.SelectionStarted == False)
  3630.   {
  3631.    XtAppWarning(XtWidgetToApplicationContext(w),
  3632.         "GenTerm widget gets make selection action before start selection!");
  3633.   }
  3634.  else
  3635.   {
  3636.    GtSortStartEnd(&w->genTerm.SelectionStartRow,&w->genTerm.SelectionStartCol,
  3637.           &w->genTerm.SelectionEndRow,&w->genTerm.SelectionEndCol);
  3638.    w->genTerm.SelectionStarted = False;
  3639.    if((w->genTerm.SelectionStartRow == w->genTerm.SelectionEndRow) &&
  3640.       (w->genTerm.SelectionStartCol == w->genTerm.SelectionEndCol))
  3641.     {
  3642.      GtUnHighLightSection(w);
  3643.      w->genTerm.Locked = False;
  3644.      GtDoLockCallback(w);
  3645.      return;
  3646.     }
  3647.  
  3648.    selection = GtCopySection(w,w->genTerm.SelectionStartRow,
  3649.                  w->genTerm.SelectionStartCol,
  3650.                  w->genTerm.SelectionEndRow,
  3651.                  w->genTerm.SelectionEndCol);
  3652.    w->genTerm.CurrentSelection = selection;
  3653.  
  3654.    if(which == 0)
  3655.     atom = XA_PRIMARY;
  3656.    else
  3657.     atom = XA_SECONDARY;
  3658.    if(XtOwnSelection(w,atom,event->xbutton.time,GtConvertProc,GtLoseSelection,
  3659.              NULL) == False)
  3660.     {
  3661.      XtAppWarning(XtWidgetToApplicationContext(w),
  3662.           "GenTerm Widget failed to own selection");
  3663.     }
  3664.    if(buffer >= 0)
  3665.     XStoreBuffer(XtDisplay(w),selection,strlen(selection),buffer);
  3666.   }
  3667.  w->genTerm.Locked = False;
  3668.  GtDoLockCallback(w);
  3669. }
  3670.  
  3671. /* GtConvertProc:  This function is the convert procedure for the selections*/
  3672. /*  It is copied from page 300 of the X Toolkit Intrinsics Programming */
  3673. /*  manual Volume 4. */
  3674.  
  3675. static Boolean GtConvertProc(w,selection,target,type_return,value_return,
  3676.              length_return,format_return)
  3677.  
  3678. GenTermWidget w;
  3679. Atom *selection;
  3680. Atom *target;
  3681. Atom *type_return;
  3682. caddr_t *value_return;
  3683. unsigned long *length_return;
  3684. int *format_return;
  3685.  
  3686. {
  3687.  if( *target == XA_TARGETS(XtDisplay(w)))
  3688.   {
  3689.    Atom *targetP;
  3690.    Atom *std_targets;
  3691.    unsigned long std_length;
  3692.  
  3693.    XmuConvertStandardSelection(w,CurrentTime,selection,target,type_return,
  3694.                    (caddr_t *)&std_targets,&std_length,
  3695.                    format_return);
  3696.    *value_return = XtMalloc(sizeof(Atom)*(std_length + 1));
  3697.    targetP = *(Atom **) value_return;
  3698.    *length_return = std_length + 1;
  3699.    *targetP++ = XA_STRING;
  3700.    bcopy((char *)std_targets,(char *)targetP,sizeof(Atom)*std_length);
  3701.    XtFree((char *)std_targets);
  3702.    *type_return = XA_ATOM;
  3703.    *format_return = sizeof(Atom) * 8;
  3704.    return(True);
  3705.   }
  3706.  else if (*target == XA_STRING)
  3707.   {
  3708.    *type_return = XA_STRING;
  3709.    *value_return = (XtPointer) XtNewString(w->genTerm.CurrentSelection);
  3710.    *length_return = strlen(*value_return);
  3711.    *format_return = 8;
  3712.    return(True);
  3713.   }
  3714.  else
  3715.   {
  3716.    if(XmuConvertStandardSelection(w,CurrentTime,selection,target,type_return,
  3717.                   value_return,length_return,format_return))
  3718.     return(True);
  3719.    else
  3720.     return(False);
  3721.   }
  3722. }
  3723.  
  3724. static void GtLoseSelection(w,selection)
  3725.  
  3726. GenTermWidget w;
  3727. Atom *selection;
  3728.  
  3729. {
  3730.  if(w->genTerm.CurrentSelection != NULL)
  3731.   XtFree(w->genTerm.CurrentSelection);
  3732.  w->genTerm.CurrentSelection = NULL;
  3733.  GtUnHighLightSection(w);
  3734. }
  3735.  
  3736. /* GtSortStartEnd:  This function takes two row column pairs and sorts them */
  3737. /*  to determine which comes before the other.  The position which comes */
  3738. /*  first is defined to be the one which you would encounter first when */
  3739. /*  traversing the screen left to row and top to bottom.  */
  3740.  
  3741. static void GtSortStartEnd(StartRow,StartCol,EndRow,EndCol)
  3742.  
  3743. int *StartRow;
  3744. int *StartCol;
  3745. int *EndRow;
  3746. int *EndCol;
  3747.  
  3748. {
  3749.  int row,col;
  3750.  Boolean Flip = False;
  3751.  
  3752.  if(*EndRow < *StartRow)
  3753.   Flip = True;
  3754.  else if((*EndRow == *StartRow) && (*EndCol < *StartCol))
  3755.   Flip = True;
  3756.  
  3757.  if(Flip == True)
  3758.   {
  3759.    row = *StartRow;
  3760.    col = *StartCol;
  3761.    *StartRow = *EndRow;
  3762.    *StartCol = *EndCol;
  3763.    *EndRow = row;
  3764.    *EndCol = col;
  3765.   }
  3766. }
  3767.  
  3768. /* GtCopySection:  This function copies a section of the screen */
  3769.  
  3770. static char *GtCopySection(w,StartRow,StartCol,EndRow,EndCol)
  3771.  
  3772. GenTermWidget w;
  3773. int StartRow,StartCol;
  3774. int EndRow,EndCol;
  3775.  
  3776. {
  3777.  char *store;
  3778.  int i,j,k = 0;
  3779.  int l;
  3780.  int size;
  3781.  
  3782.  size = (w->genTerm.columns + 1)* (EndRow - StartRow + 1); /* over est. size */
  3783.  store = XtMalloc(sizeof(char) * (size + 1));
  3784.  
  3785.  i = MIN(w->genTerm.Screen[StartRow]->NumChars,w->genTerm.columns-1);
  3786.  if(StartRow == EndRow)
  3787.   i = MIN(i,EndCol);
  3788.  while((i >= 0) && (w->genTerm.Screen[StartRow]->Characters[i--] == ' '));
  3789.  i++;
  3790.  for(j = StartCol; j <= i; j++)
  3791.   store[k++] = w->genTerm.Screen[StartRow]->Characters[j];
  3792.  
  3793.  if(StartRow == EndRow)
  3794.   {
  3795.    store[k] = NULL;
  3796.    return(store);
  3797.   }
  3798.  else
  3799.   store[k++] = '\n';
  3800.  
  3801.  for(l = StartRow + 1; l < EndRow; l++)
  3802.   {
  3803.    i = MIN(w->genTerm.Screen[l]->NumChars,w->genTerm.columns-1);
  3804.    while((i >= 0) && (w->genTerm.Screen[l]->Characters[i--] == ' '));
  3805.    i++;
  3806.    for(j = 0; j <= i; j++)
  3807.     store[k++] = w->genTerm.Screen[l]->Characters[j];
  3808.    store[k++] = '\n';
  3809.   }
  3810.  
  3811.  i = MIN(w->genTerm.Screen[EndRow]->NumChars,w->genTerm.columns-1);
  3812.  if(EndCol < i)
  3813.   i = EndCol;
  3814.  while((i >= 0) && (w->genTerm.Screen[EndRow]->Characters[i--] == ' '));
  3815.  i++;
  3816.  for(j = 0; j <= i; j++)
  3817.   store[k++] = w->genTerm.Screen[EndRow]->Characters[j];
  3818.  /*store[k++] = '\n';*/
  3819.  
  3820.  store[k] = NULL;
  3821.  return(store);
  3822. }
  3823.  
  3824. static void GtInsertSelection(w,event,names,num)
  3825.  
  3826. GenTermWidget w;
  3827. XEvent *event;
  3828. char *names[];
  3829. Cardinal *num;
  3830.  
  3831. {
  3832.  Atom which = XA_PRIMARY;
  3833.  int i;
  3834.  
  3835.  w->genTerm.CutBuffer = -1;
  3836.  
  3837.  for(i = 0; i < *num; i++)
  3838.   {
  3839.    if(strcmp(names[i],"SECONDARY") == 0)
  3840.     which = XA_SECONDARY;
  3841.    else if(strncmp(names[i],"CUT_BUFFER",strlen("CUT_BUFFER")) == 0)
  3842.     {
  3843.      w->genTerm.CutBuffer = names[i][strlen("CUT_BUFFER")] - '0';
  3844.     }
  3845.   }
  3846.  
  3847.  XtGetSelectionValue(w,which,XA_STRING,GtInsertCallback,NULL,
  3848.              event->xbutton.time);
  3849. }
  3850.  
  3851. static void GtInsertCallback(w,client_data,selection,type,value,length,format)
  3852.  
  3853. GenTermWidget w;
  3854. XtPointer client_data;
  3855. Atom *selection;
  3856. Atom *type;
  3857. XtPointer *value;
  3858. unsigned long *length;
  3859. int *format;
  3860.  
  3861. {
  3862.  GenTermCallback reason;
  3863.  int len;
  3864.  
  3865.  if((value == NULL) || ((*value == NULL) && (*length == 0)))
  3866.   {
  3867.    if(w->genTerm.CutBuffer >= 0)
  3868.     {
  3869.      reason.Selection = XFetchBuffer(XtDisplay(w),&(len),0);
  3870.      if(len > 0)
  3871.       {
  3872.        reason.reason = GT_PASTE;
  3873.        reason.event = NULL;
  3874.        XtCallCallbacks(w,XtNkbdCallback,(caddr_t)&reason);
  3875.       }
  3876.     }
  3877.   }
  3878.  else
  3879.   {
  3880.    reason.reason = GT_PASTE;
  3881.    reason.event = NULL;
  3882.    reason.Selection = value;
  3883.    XtCallCallbacks(w,XtNkbdCallback,(caddr_t)&reason);
  3884.   }
  3885. }
  3886.  
  3887. /* GtGetScreenSize:  This function returns the current size of the screen */
  3888. /*  to the calling program. */
  3889.  
  3890. int GtGetScreenSize(w,rows,cols)
  3891.  
  3892. GenTermWidget w;
  3893. int *rows;
  3894. int *cols;
  3895.  
  3896. {
  3897.  *rows = w->genTerm.rows;
  3898.  *cols = w->genTerm.columns;
  3899.  
  3900.  return(GTGOOD);
  3901. }
  3902.  
  3903. /* GtChangeFonts:  This function is called when the user has requested that */
  3904. /*  fonts be changed.  The new fonts have already been loaded.  Here we walk */
  3905. /*  through all the graphics contexts and attributes and change the */
  3906. /*  appropriate font */
  3907.  
  3908. static void GtChangeFonts(w)
  3909.  
  3910. GenTermWidget w;
  3911.  
  3912. {
  3913.  int i,j;
  3914.  int npen;
  3915.  Font font;
  3916.  XFontStruct *Temp;
  3917.  
  3918.  npen = w->genTerm.NumberColours << NUMATTRIBUTES;
  3919.  
  3920.  /* first walk each pen */
  3921.  
  3922.  for(i = 0; i < npen; i++)
  3923.   if(w->genTerm.Pens[i] != NULL)
  3924.    {
  3925.     if(i & SETALTFONT)
  3926.      font = w->genTerm.AltFont->fid;
  3927.     else
  3928.      font = w->genTerm.CurrentFont->fid;
  3929.     XSetFont(XtDisplay(w),w->genTerm.Pens[i]->draw_gc,font);
  3930.     XSetFont(XtDisplay(w),w->genTerm.Pens[i]->cursor_gc,font);
  3931.     XSetFont(XtDisplay(w),w->genTerm.Pens[i]->blink_gc,font);
  3932.    }
  3933.  /* now look at all attribute fields */
  3934.  for(i = 0; i < w->genTerm.SaveLines;i++)
  3935.   for(j = 0; j < w->genTerm.columns; j++)
  3936.    {
  3937.     if(w->genTerm.VScreen[i]->Attributes[j].type & SETALTFONT)
  3938.      w->genTerm.VScreen[i]->Attributes[j].Font = w->genTerm.AltFont;
  3939.     else
  3940.      w->genTerm.VScreen[i]->Attributes[j].Font = w->genTerm.CurrentFont;
  3941.    }
  3942.  
  3943.  if(w->genTerm.CurrentSettings.type & SETALTFONT)
  3944.   w->genTerm.CurrentSettings.Font = w->genTerm.AltFont;
  3945. }
  3946.  
  3947. /* GtChangeCursorColour:  This function is called whent he user has requested*/
  3948. /*  to change the cursor colour.  It goes through all the currently defined */
  3949. /*  graphics contexts and updates the cursor colour setting. */
  3950.  
  3951. static void GtChangeCursorColour(w)
  3952.  
  3953. GenTermWidget w;
  3954.  
  3955. {
  3956.  int i;
  3957.  int npen;
  3958.  XGCValues values;
  3959.  
  3960.  npen = w->genTerm.NumberColours << NUMATTRIBUTES;
  3961.  
  3962.  for(i = 0; i < npen; i++)
  3963.   if(w->genTerm.Pens[i] != NULL)
  3964.    {
  3965.     values.background = w->genTerm.CursorColour;
  3966.     XChangeGC(XtDisplay(w),w->genTerm.Pens[i]->cursor_gc,GCBackground,
  3967.           &values);
  3968.    }
  3969. }
  3970.  
  3971. /* GtChangeColour:  This function walks through all the graphics contexts */
  3972. /*  and changes any reference to the old colour to new colour. */
  3973.  
  3974. static void GtChangeColour(w,NewColour,OldColour,start,end)
  3975.  
  3976. GenTermWidget w;
  3977. Pixel OldColour;
  3978. Pixel NewColour;
  3979. int start,end;
  3980.  
  3981. {
  3982.  int i,j;
  3983.  XGCValues values;
  3984.  GC gc;
  3985.  
  3986.  for(i = start; i < end; i++)
  3987.   if(w->genTerm.Pens[i] != NULL)
  3988.    {
  3989.     for(j = 0; j < 3; j++)
  3990.      {
  3991.       switch(j)
  3992.        {
  3993.       case 0:
  3994.     gc = w->genTerm.Pens[i]->draw_gc;
  3995.     break;
  3996.       case 1:
  3997.     gc = w->genTerm.Pens[i]->cursor_gc;
  3998.     break;
  3999.       case 2:
  4000.     gc = w->genTerm.Pens[i]->blink_gc;
  4001.     break;
  4002.        }
  4003.  
  4004.       XGetGCValues(XtDisplay(w),gc,GCBackground | GCForeground,&values);
  4005.       if(values.foreground == OldColour)
  4006.        values.foreground = NewColour;
  4007.       if(values.background == OldColour)
  4008.        values.background = NewColour;
  4009.       XChangeGC(XtDisplay(w),gc,GCBackground | GCForeground,&values);
  4010.      }
  4011.    }
  4012. }
  4013.  
  4014. /* GtChangePen: This function is called when the user changes one of the */
  4015. /*  pen colours.  It rebuilts the pen colour structure and then if a colour */
  4016. /*  has been changed, it goes and updates all the graphics contexts that */
  4017. /*  might use that colour */
  4018.  
  4019. static void GtChangePen(new,old)
  4020.  
  4021. GenTermWidget new;
  4022. GenTermWidget old;
  4023.  
  4024. {
  4025.  int i;
  4026.  int start;
  4027.  
  4028.  GtSetupPenColours(new);    /* first build the new pen table */
  4029.  
  4030.  for(i = 0; i < new->genTerm.NumberColours; i++)
  4031.   {                /* now compare old and new tables to find pens that changed */
  4032.    if(old->genTerm.Colours[i].defined != 0) /* only care about ones we used */
  4033.     {
  4034.      if((strcmp(new->genTerm.Colours[i].ForegroundName,
  4035.         old->genTerm.Colours[i].ForegroundName) != 0) ||
  4036.     (strcmp(new->genTerm.Colours[i].BackgroundName,
  4037.         old->genTerm.Colours[i].BackgroundName) != 0))
  4038.       {
  4039.        GtDefinePen(new,i);
  4040.        start = i << NUMATTRIBUTES;
  4041.        GtChangeColour(new,new->genTerm.Colours[i].Foreground,
  4042.               old->genTerm.Colours[i].Foreground,start,
  4043.               start+BASEATTRIBUTES);
  4044.        GtChangeColour(new,new->genTerm.Colours[i].Background,
  4045.               old->genTerm.Colours[i].Background,start,
  4046.               start + BASEATTRIBUTES);
  4047.        if(old->genTerm.Colours[i].defined == 2)
  4048.     {
  4049.      GtDefineHalfBrightPen(new,i);
  4050.      GtChangeColour(new,new->genTerm.Colours[i].HalfBright,
  4051.             old->genTerm.Colours[i].HalfBright,start,
  4052.             start + BASEATTRIBUTES);
  4053.     }
  4054.        if((new->genTerm.CurrentSettings.type >> NUMATTRIBUTES) == i)
  4055.     {    /* pen is currently in use so update current colours */
  4056.      if(old->genTerm.foreground == old->genTerm.Colours[i].Foreground)
  4057.       new->genTerm.foreground = new->genTerm.Colours[i].Foreground;
  4058.      if(old->genTerm.foreground == old->genTerm.Colours[i].Background)
  4059.       new->genTerm.foreground = new->genTerm.Colours[i].Background;
  4060.      if(old->genTerm.foreground = old->genTerm.Colours[i].HalfBright)
  4061.       new->genTerm.foreground = new->genTerm.Colours[i].HalfBright;
  4062.      if(old->genTerm.CurrentForeground ==
  4063.         old->genTerm.Colours[i].Foreground)
  4064.       new->genTerm.CurrentForeground = new->genTerm.Colours[i].Foreground;
  4065.      if(old->genTerm.CurrentForeground ==
  4066.         old->genTerm.Colours[i].Background)
  4067.       new->genTerm.CurrentForeground = new->genTerm.Colours[i].Background;
  4068.      if(old->genTerm.CurrentForeground =
  4069.         old->genTerm.Colours[i].HalfBright)
  4070.       new->genTerm.CurrentForeground = new->genTerm.Colours[i].HalfBright;
  4071.      if(old->genTerm.background == old->genTerm.Colours[i].Foreground)
  4072.       new->genTerm.background = new->genTerm.Colours[i].Foreground;
  4073.      if(old->genTerm.background == old->genTerm.Colours[i].Background)
  4074.       new->genTerm.background = new->genTerm.Colours[i].Background;
  4075.      if(old->genTerm.background = old->genTerm.Colours[i].HalfBright)
  4076.       new->genTerm.background = new->genTerm.Colours[i].HalfBright;
  4077.      if(old->genTerm.CurrentBackground ==
  4078.         old->genTerm.Colours[i].Foreground)
  4079.       new->genTerm.CurrentBackground = new->genTerm.Colours[i].Foreground;
  4080.      if(old->genTerm.CurrentBackground ==
  4081.         old->genTerm.Colours[i].Background)
  4082.       new->genTerm.CurrentBackground = new->genTerm.Colours[i].Background;
  4083.      if(old->genTerm.CurrentBackground = old->genTerm.Colours[i].HalfBright)
  4084.       new->genTerm.CurrentBackground = new->genTerm.Colours[i].HalfBright;
  4085.     }
  4086.       }
  4087.      else
  4088.       {
  4089.        new->genTerm.Colours[i].Foreground = old->genTerm.Colours[i].Foreground;
  4090.        new->genTerm.Colours[i].Background = old->genTerm.Colours[i].Background;
  4091.        new->genTerm.Colours[i].HalfBright = old->genTerm.Colours[i].HalfBright;
  4092.        new->genTerm.Colours[i].defined = old->genTerm.Colours[i].defined;
  4093.       }
  4094.     }
  4095.   }
  4096.  GtDestroyPenColours(old);
  4097. }
  4098.  
  4099. /* GtHSLtoRGB: This utility function converts a colour from RGB to HSL */
  4100. /*  Stolen from "Starbase Graphics Techniques, HP-UX Concepts and */
  4101. /*  Tutorials" March 1988 Edition 1 */
  4102.  
  4103. static void GtHSLtoRGB(r,g,b,h,s,l)
  4104.  
  4105. int *r,*g,*b;
  4106. int h,s,l;
  4107.  
  4108. {
  4109.  float red,green,blue;
  4110.  float hue,sat,lum;
  4111.  float frac,lx,ly,lz;
  4112.  float minval;
  4113.  
  4114.  hue = ((float)h)/MAXCOLOUR;
  4115.  sat = ((float)s)/MAXCOLOUR;
  4116.  lum = ((float)l)/MAXCOLOUR;
  4117.  
  4118.  hue = 6.0 * hue;
  4119.  frac = hue - (int) hue;
  4120.  lx = lum * (1.0 - sat);
  4121.  ly = lum * (1.0 - sat * frac);
  4122.  lz = lum * (1.0 - sat * (1.0 - frac));
  4123.  
  4124.  switch((int)hue)
  4125.   {
  4126.  case 0:
  4127.  case 6:
  4128.    red = lum;
  4129.    green = lz;
  4130.    blue = lx;
  4131.    break;
  4132.  case 1:
  4133.    red = ly;
  4134.    green = lum;
  4135.    blue = lx;
  4136.    break;
  4137.  case 2:
  4138.    red = lx;
  4139.    green = lum;
  4140.    blue = lz;
  4141.    break;
  4142.  case 3:
  4143.    red = lx;
  4144.    green = ly;
  4145.    blue = lum;
  4146.    break;
  4147.  case 4:
  4148.    red = lz;
  4149.    green = lx;
  4150.    blue = lum;
  4151.    break;
  4152.  case 5:
  4153.    red = lum;
  4154.    green = lx;
  4155.    blue = ly;
  4156.    break;
  4157.   }
  4158.  
  4159.  
  4160.  red *= MAXCOLOUR;
  4161.  green *= MAXCOLOUR;
  4162.  blue *= MAXCOLOUR;
  4163.  
  4164.  *r = (int)red;
  4165.  *g = (int)green;
  4166.  *b = (int)blue;
  4167. }
  4168.  
  4169. static void GtRGBtoHSL(r,g,b,h,s,l)
  4170.  
  4171. int r,g,b;
  4172. int *h,*s,*l;
  4173.  
  4174. {
  4175.  float red,green,blue;
  4176.  float hue,sat,lum;
  4177.  float x,tempr,tempg,tempb;
  4178.  
  4179. #define MAXCOL(a,b,c) ((a>b?a:b)>c?(a>b?a:b):c)
  4180. #define MINCOL(a,b,c) ((a<b?a:b)<c?(a<b?a:b):c)
  4181.  
  4182.  red = ((float)r)/MAXCOLOUR;
  4183.  green = ((float)g)/MAXCOLOUR;
  4184.  blue = ((float)b)/MAXCOLOUR;
  4185.  
  4186.  hue = 0.0;
  4187.  sat = 1.0;
  4188.  lum = MAXCOL(red,green,blue);
  4189.  x = MINCOL(red,green,blue);
  4190.  if(lum != 0.0)
  4191.   {
  4192.    sat = (lum - x) / lum;
  4193.    if(sat != 0.0)
  4194.     {
  4195.      tempr = (lum - red) / (lum - x);
  4196.      tempg = (lum - green) / (lum - x);
  4197.      tempb = (lum - blue) / (lum - x);
  4198.      if(red == lum)
  4199.       hue = (green == x ? 5.0 + tempb : 1.0 - tempg);
  4200.      else if(green = lum)
  4201.       hue = (blue == x ? 1.0 + tempr : 3.0 - tempb);
  4202.      else
  4203.       hue = (red == x ? 3.0 + tempg : 5.0 - tempr);
  4204.      
  4205.      hue /= 6.0;
  4206.     }
  4207.   }
  4208.  
  4209.  *h = (int)(hue * MAXCOLOUR);
  4210.  *s = (int)(sat * MAXCOLOUR);
  4211.  *l = (int)(lum * MAXCOLOUR);
  4212. }
  4213.  
  4214. memrep(s1,s2,size,n)
  4215.  
  4216. char *s1;
  4217. char *s2;
  4218. int size;
  4219. int n;
  4220.  
  4221. {
  4222.  int i;
  4223.  
  4224.  for(i = 0; i < n; i++)
  4225.   {
  4226.    memcpy(s1,s2,size);
  4227.    s1 += size;
  4228.   }
  4229. }
  4230.  
  4231. @EOF
  4232. set `wc -lwc <GenTerm/GenTerm.c`
  4233. if test $1$2$3 != 409511685117264
  4234. then
  4235.     echo ERROR: wc results of GenTerm/GenTerm.c are $* should be 4095 11685 117264
  4236. fi
  4237.  
  4238. chmod 644 GenTerm/GenTerm.c
  4239.  
  4240. echo x - GenTerm/GenTerm.h
  4241. cat >GenTerm/GenTerm.h <<'@EOF'
  4242. #ifndef _READGenTermh
  4243. #define _READGenTermh
  4244. /* History:                                 */
  4245. /*         Written by G. R. Strachan 1992 */
  4246.  
  4247. /*  Copyright Gordon R. Strachan 1992 */
  4248. /*  This code is provided as is, neither the University of Waterloo nor */
  4249. /*  the author is liable for any damage caused by the use or misuse of this */
  4250. /*  code.  */
  4251.  
  4252. /* Permission is granted to copy, use and modify this code provided it is */
  4253. /* not sold for profit and the above copyright remains intact. */
  4254.  
  4255. #include <X11/Core.h>
  4256. #include <X11/Composite.h>
  4257.  
  4258. extern WidgetClass genTermWidgetClass;
  4259.  
  4260. typedef struct _GenTermClassRec *GenTermWidgetClass;
  4261. typedef struct _GenTermRec *GenTermWidget;
  4262.  
  4263. #define XtNrows                  "rows"
  4264. #define XtNcolumns               "columns"
  4265. #define XtNbottomMargin          "bottomMargin"
  4266. #define XtNbaseFont              "baseFont"
  4267. #define XtNalternateFont         "alternateFont"
  4268. #define XtNsaveLines             "saveLines"
  4269. #define XtNautoLineWrap          "autoLineWrap"
  4270. #define XtNliteralMode           "literalMode"
  4271. #define XtNcursorColor           "cursorColor"
  4272. #define XtNshowCursor            "showCursor"
  4273. #define XtNkbdCallback           "kbdCallback"
  4274. #define XtNtopCallback           "topCallback"
  4275. #define XtNresizeCallback        "resizeCallback"
  4276. #define XtNlockCallback          "lockCallback"
  4277. #define XtNcursorFloats          "cursorFloats"
  4278. #define XtNfieldAttributes       "fieldAttributes"
  4279. #define XtNscrollDownClear       "scrollDownClear"
  4280. #define XtNfullScroll            "fullScroll"
  4281. #define XtNinsertMode            "insertMode"
  4282. #define XtNsaveScrollRegion      "saveScrollRegion"
  4283. #define XtNcharacterHeight       "characterHeight"
  4284. #define XtNcharacterWidth        "characterWidth"
  4285. #define XtNblinkRate             "blinkRate"
  4286. #define XtNnumberColors          "numberColors"
  4287. #define XtNpenColors             "penColors"
  4288. #define XtNdefineColor           "defineColor"
  4289. #define XtNscrollOnOutput        "scrollOnOutput"
  4290. #define XtNcursorKeyScrollRegion "cursorKeyScrollRegion"
  4291. #define XtNmarginBell            "marginBell"
  4292. #define XtNmargin                "margin"
  4293. #define XtNeolStick              "eolStick"
  4294. #define XtNlockScreen            "lockScreen"
  4295. #define XtNignoreNull            "ignoreNull"
  4296.  
  4297. #define XtCRows                  "Rows"
  4298. #define XtCColumns               "Columns"
  4299. #define XtCBottomMargin          "BottomMargin"
  4300. #define XtCBaseFont              "BaseFont"
  4301. #define XtCAlternateFont         "AlternateFont"
  4302. #define XtCSaveLines             "SaveLines"
  4303. #define XtCAutoLineWrap          "AutoLineWrap"
  4304. #define XtCLiteralMode           "LiteralMode"
  4305. #define XtCCursorColor           "CursorColor"
  4306. #define XtCKbdCallback           "KbdCallback"
  4307. #define XtCTopCallback           "TopCallback"
  4308. #define XtCResizeCallback        "ResizeCallback"
  4309. #define XtCLockCallback          "LockCallback"
  4310. #define XtCCursorFloats          "CursorFloats"
  4311. #define XtCShowCursor            "ShowCursor"
  4312. #define XtCFieldAttributes       "Fieldattributes"
  4313. #define XtCScrollDownClear       "ScrollDownClear"
  4314. #define XtCFullScroll            "FullScroll"
  4315. #define XtCInsertMode            "InsertMode"
  4316. #define XtCSaveScrollRegion      "SaveScrollRegion"
  4317. #define XtCCharacterHeight       "CharacterHeight"
  4318. #define XtCCharacterWidth        "CharacterWidth"
  4319. #define XtCBlinkRate             "BlinkRate"
  4320. #define XtCNumberColors          "NumberColors"
  4321. #define XtCPenColors             "PenColors"
  4322. #define XtCDefineColor           "DefineColor"
  4323. #define XtCScrollOnOutput        "ScrollOnOutput"
  4324. #define XtCCursorKeyScrollRegion "CursorKeyScrollRegion"
  4325. #define XtCMarginBell            "MarginBell"
  4326. #define XtCMargin                "Margin"
  4327. #define XtCEolStick              "EolStick"
  4328. #define XtCLockScreen            "LockScreen"
  4329. #define XtCIgnoreNull            "IgnoreNull"
  4330.  
  4331. #define GTFONT 1
  4332. #define GTBASE 0
  4333. #define GTALTERNATE    1
  4334. #define GTINVERSEVIDEO 2
  4335. #define GTUNDERLINE    3
  4336. #define GTHALFBRIGHT   4
  4337. #define GTBLINKMODE    5
  4338. #define GTPEN          6
  4339.  
  4340. #define GTOFF 0
  4341. #define GTON 1
  4342. #define GTNORMAL 0
  4343. #define GTINVERSE 1
  4344.  
  4345. #define GT_INPUT 1
  4346. #define GT_TOP 2
  4347. #define GT_RESIZE 3
  4348. #define GT_PASTE 4
  4349. #define GT_LOCK 5
  4350.  
  4351. #define GTRGB 0
  4352. #define GTHSL 1
  4353.  
  4354. #define MAXCOLOUR 65535.0
  4355.  
  4356. #define NMAP 255
  4357.  
  4358. #define GTGOOD 1
  4359. #define GTBAD 0
  4360. #define GTLOCKED 2
  4361.  
  4362. typedef struct mytermCallback
  4363. {
  4364.  int reason;
  4365.  XEvent *event;
  4366.  int TopLine;
  4367.  int Rows;
  4368.  int Columns;
  4369.  int Save;
  4370.  char *Selection;
  4371.  int Value;
  4372. } GenTermCallback;
  4373.  
  4374. #endif _READGenTermh
  4375.  
  4376. @EOF
  4377. set `wc -lwc <GenTerm/GenTerm.h`
  4378. if test $1$2$3 != 1343924464
  4379. then
  4380.     echo ERROR: wc results of GenTerm/GenTerm.h are $* should be 134 392 4464
  4381. fi
  4382.  
  4383. chmod 644 GenTerm/GenTerm.h
  4384.  
  4385. echo x - GenTerm/GenTermEm.h
  4386. cat >GenTerm/GenTermEm.h <<'@EOF'
  4387. #ifndef _READGenTermEmh
  4388. #define _READGenTermEmh
  4389. /* History:                                 */
  4390. /*         Written by G. R. Strachan 1992 */
  4391.  
  4392. /*  Copyright Gordon R. Strachan 1992 */
  4393. /*  This code is provided as is, neither the University of Waterloo nor */
  4394. /*  the author is liable for any damage caused by the use or misuse of this */
  4395. /*  code.  */
  4396.  
  4397. /* Permission is granted to copy, use and modify this code provided it is */
  4398. /* not sold for profit and the above copyright remains intact. */
  4399.  
  4400. #include <X11/Core.h>
  4401. #include "GenTerm.h"
  4402.  
  4403. extern WidgetClass genTermEmWidgetClass;
  4404.  
  4405. typedef struct _GenTermEmClassRec *GenTermEmWidgetClass;
  4406. typedef struct _GenTermEmRec *GenTermEmWidget;
  4407.  
  4408. #define XtNname                   "name"
  4409. #define XtNshowFunction           "showFunction"
  4410. #define XtNfunctionSize           "functionSize"
  4411. #define XtNfunctionLabelWidth     "functionLabelWidth"
  4412. #define XtNnumberFunctionKeys     "numberFunctionKeys"
  4413. #define XtNoutputFile             "outputFile"
  4414. #define XtNlogging                "logging"
  4415. #define XtNlogFile                "logFile"
  4416. #define XtNdestructiveTab         "destructiveTab"
  4417. #define XtNparseFile              "parseFile"
  4418. #define XtNescCallback            "escCallback"
  4419. #define XtNwakeUpCallback         "wakeUpCallback"
  4420. #define XtNflowCallback           "flowCallback"
  4421. #define XtNnumberMapTables        "numberMapTables"
  4422. #define XtNnumberStorageArguments "numberStorageArguments"
  4423. #define XtNpreScroll              "preScroll"
  4424. #define XtNscrollChar             "scrollChar"
  4425. #define XtNscrollAbortChar        "scrollAbortChar"
  4426.  
  4427. #define XtCName                   "Name"
  4428. #define XtCShowFunction           "ShowFunction"
  4429. #define XtCFunctionSize           "FunctionSize"
  4430. #define XtCFunctionLabelWidth     "FunctionLabelWidth"
  4431. #define XtCNumberFunctionKeys     "NumberFunctionKeys"
  4432. #define XtCOutputFile             "OutputFile"
  4433. #define XtCLogging                "Logging"
  4434. #define XtCLogFile                "LogFile"
  4435. #define XtCDestructiveTab         "DestructiveTab"
  4436. #define XtCParseFile              "ParseFile"
  4437. #define XtCEscCallback            "EscCallback"
  4438. #define XtCWakeUpCallback         "WakeUpCallback"
  4439. #define XtCFlowCallback           "FlowCallback"
  4440. #define XtCNumberMapTables        "NumberMapTables"
  4441. #define XtCNumberStorageArguments "NumberStorageArguments"
  4442. #define XtCPreScroll              "PreScroll"
  4443. #define XtCScrollChar             "ScrollChar"
  4444. #define XtCScrollAbortChar        "ScrollAbortChar"
  4445.  
  4446. #define GTE_INPUT  1
  4447. #define GTE_ESCAPE 2
  4448. #define GTE_WAKEUP 3
  4449. #define GTE_XOFF   4
  4450. #define GTE_XON    5
  4451.  
  4452. #define GTE_DONE 1
  4453. #define GTE_WAIT 2
  4454.  
  4455. typedef struct mytermemCallback
  4456. {
  4457.  int reason;
  4458.  XEvent *event;
  4459.  char *string;
  4460.  int len;
  4461.  char **escapes;
  4462. } GenTermEmCallback;
  4463.  
  4464. #endif _READGenTermEmh
  4465. @EOF
  4466. set `wc -lwc <GenTerm/GenTermEm.h`
  4467. if test $1$2$3 != 782452753
  4468. then
  4469.     echo ERROR: wc results of GenTerm/GenTermEm.h are $* should be 78 245 2753
  4470. fi
  4471.  
  4472. chmod 644 GenTerm/GenTermEm.h
  4473.  
  4474. echo x - GenTerm/GenTermEmP.h
  4475. cat >GenTerm/GenTermEmP.h <<'@EOF'
  4476. /* GenTermP.h -- Private definitions for GenTerm widget */
  4477. /* History:                                 */
  4478. /*         Written by G. R. Strachan 1992 */
  4479.  
  4480. /*  Copyright Gordon R. Strachan 1992 */
  4481. /*  This code is provided as is, neither the University of Waterloo nor */
  4482. /*  the author is liable for any damage caused by the use or misuse of this */
  4483. /*  code.  */
  4484.  
  4485. /* Permission is granted to copy, use and modify this code provided it is */
  4486. /* not sold for profit and the above copyright remains intact. */
  4487.  
  4488. #ifndef _ReadGenTermEmP
  4489. #define _ReadGenTermEmP
  4490.  
  4491. #include <X11/X.h>
  4492. #include <X11/CoreP.h>
  4493. #include <X11/CompositeP.h>
  4494. #include <Xm/Label.h>
  4495.  
  4496. #include "GenTermEm.h"
  4497. #include "GenTermP.h"
  4498.  
  4499. #define NTABS 20
  4500. #define NUMARGS 20
  4501.  
  4502. #define NKEYS 8
  4503.  
  4504. #define ADEFINED  1
  4505. #define AMALLOCED (1 << 1)
  4506.  
  4507. /* our internal private data structures */
  4508.  
  4509. struct ParseItem
  4510. {
  4511.  short empty;
  4512.  char c;
  4513.  struct ParseItem *next;
  4514.  struct ParseItem *child;
  4515.  struct SpecialItem *special;
  4516.  struct Translator *translators;
  4517. };
  4518.  
  4519. struct InputArgument
  4520. {
  4521.  char *string;
  4522.  int length;
  4523.  int valid;
  4524. };
  4525.  
  4526. struct Arguments
  4527. {
  4528.  char *string;
  4529.  int type;
  4530.  int n;
  4531. };
  4532.  
  4533. struct ArgumentList
  4534. {
  4535.  int NumArgs;
  4536.  struct Arguments *args;
  4537. };
  4538.  
  4539. struct SpecialItem
  4540. {
  4541.  int position;
  4542.  struct ArgumentList *arglist;
  4543.  int (*func)();
  4544. };
  4545.  
  4546. struct Translator
  4547. {
  4548.  struct ArgumentList *arglist;
  4549.  int (*func)();
  4550.  int label;
  4551.  struct Translator *next;
  4552. };
  4553.  
  4554. struct FuncDefs
  4555. {
  4556.  char *name;
  4557.  int (*func)();
  4558. };
  4559.  
  4560. struct KeyTranslations
  4561. {
  4562.  KeySym sym;
  4563.  int state;
  4564.  char type;
  4565.  char *string;
  4566.  int len;
  4567. };
  4568.  
  4569. struct GtEList
  4570. {
  4571.  char Name[80];
  4572.  union
  4573.   {
  4574.    struct KeyTranslations *KeyTable;
  4575.    struct ParseItem *ParseTable;
  4576.   }Point;
  4577.  int Num;
  4578.  struct GtEList *Next;
  4579. };
  4580.  
  4581. struct GtEParseFile
  4582.  
  4583. {
  4584.  char *Name;
  4585.  struct GtEList *ParseTables;
  4586.  struct GtEList *KeyTables;
  4587.  struct GtEParseFile *Next;
  4588. };
  4589.  
  4590. struct StateTable
  4591. {
  4592.  char *Name;
  4593.  int Mask;
  4594. };
  4595.  
  4596. struct SpecialVariables    /* storage for static variable for special parsers*/
  4597. {
  4598.  int Started;
  4599.  union
  4600.   {
  4601.    int NumToGo;
  4602.    struct {
  4603.     int IntNumToGo;
  4604.     int FracNumToGo;
  4605.     int SeenDot;
  4606.    } FloatVars;
  4607.    struct {
  4608.     int NumToGo;
  4609.     char Sep[1];
  4610.     int Store;
  4611.    } DigitsVars;
  4612.   } store;
  4613. };
  4614.  
  4615. struct FunctionKey
  4616. {
  4617.  Widget FuncKey;
  4618.  char *Label;
  4619.  char *String;
  4620.  int Type;
  4621. };
  4622.  
  4623. /* Additions to widget class record */
  4624.  
  4625. typedef struct {
  4626.  struct GtEParseFile *ParseFiles;     /* our loaded parse files */
  4627. } GenTermEmClassPart;
  4628.  
  4629. /* our full class declaration */
  4630.  
  4631. typedef struct _GenTermEmClassRec {
  4632.  CoreClassPart core_class;
  4633.  CompositeClassPart composite_class;
  4634.  GenTermClassPart genTerm_class;
  4635.  GenTermEmClassPart genTermEm_class;
  4636. } GenTermEmClassRec;
  4637.  
  4638. /* new field for our widget record */
  4639.  
  4640. typedef struct {
  4641.  /* resources */
  4642.  char *name;
  4643.  char *ParseFile;
  4644.  Dimension FuncKeyHeight;
  4645.  int FuncLabelWidth;
  4646.  int NumberFunctionKeys;
  4647.  Boolean ShowKey;
  4648.  int OutputDescriptor;
  4649.  Boolean Logging;
  4650.  int LogFileDescriptor;
  4651.  Boolean DestructiveTab;
  4652.  int NumMapTables;
  4653.  int NumStoreArgs;
  4654.  Boolean PreScroll;
  4655.  unsigned char ScrollChar;
  4656.  unsigned char ScrollAbortChar;
  4657.  XtCallbackList KbdCallbacks;
  4658.  XtCallbackList EscCallbacks;
  4659.  XtCallbackList WakeUpCallbacks;
  4660.  XtCallbackList FlowCallbacks;
  4661.  
  4662. /* private data parts */
  4663.  
  4664.  Widget Menu;
  4665.  struct FunctionKey *FKeys;
  4666.  Boolean FunctionRealized;
  4667.  XtInputId OutputId;
  4668.  struct ParseItem *ParseTable;
  4669.  struct ParseItem *CurrentPoint;
  4670.  struct KeyTranslations *Keys;
  4671.  struct GtEList *KeyList;
  4672.  struct GtEList *ParseList;
  4673.  struct StateTable *Mods;
  4674.  int NumKeys;
  4675.  char InputStack[1024];
  4676.  int TopStack;
  4677.  int TabStops[NTABS];
  4678.  char **MapTables;
  4679.  XComposeStatus ComposeStatus;
  4680.  struct SpecialVariables SpecialVars;
  4681.  struct InputArgument *InputArgs;
  4682.  struct InputArgument *StoreArgs;
  4683.  XEvent LastEvent;
  4684.  char *Storage;
  4685.  int NumInStorage;
  4686.  int StorageSize;
  4687.  Boolean InputBlocked;
  4688.  int CutBuffer;
  4689.  Boolean WidgetLocked;
  4690.  Boolean NeedWakeUp;
  4691. } GenTermEmPart;
  4692.  
  4693. /* our full instance record */
  4694.  
  4695. typedef struct _GenTermEmRec
  4696. {
  4697.  CorePart core;
  4698.  CompositePart composite;
  4699.  GenTermPart genTerm;
  4700.  GenTermEmPart genTermEm;
  4701. } GenTermEmRec;
  4702.  
  4703. #endif _ReadGenTermEmP
  4704. @EOF
  4705. set `wc -lwc <GenTerm/GenTermEmP.h`
  4706. if test $1$2$3 != 2285074033
  4707. then
  4708.     echo ERROR: wc results of GenTerm/GenTermEmP.h are $* should be 228 507 4033
  4709. fi
  4710.  
  4711. chmod 644 GenTerm/GenTermEmP.h
  4712.  
  4713. echo x - GenTerm/GenTermP.h
  4714. cat >GenTerm/GenTermP.h <<'@EOF'
  4715. /* GenTermP.h -- Private definitions for GenTerm widget */
  4716. /* History:                                 */
  4717. /*         Written by G. R. Strachan 1992 */
  4718.  
  4719. /*  Copyright Gordon R. Strachan 1992 */
  4720. /*  This code is provided as is, neither the University of Waterloo nor */
  4721. /*  the author is liable for any damage caused by the use or misuse of this */
  4722. /*  code.  */
  4723.  
  4724. /* Permission is granted to copy, use and modify this code provided it is */
  4725. /* not sold for profit and the above copyright remains intact. */
  4726.  
  4727. #ifndef _ReadGenTermP
  4728. #define _ReadGenTermP
  4729.  
  4730. #include <X11/X.h>
  4731. #include <X11/IntrinsicP.h>
  4732. #include <X11/Core.h>
  4733. #include <X11/CoreP.h>
  4734. #include <X11/Composite.h>
  4735. #include <X11/CompositeP.h>
  4736.  
  4737. #include "GenTerm.h"
  4738.  
  4739. #define NPEN 512
  4740.  
  4741. #define SETALTFONT     1
  4742. #define SETINVERSE     2
  4743. #define SETUNDERLINE   4
  4744. #define SETHALFBRIGHT  8
  4745. #define SETBLINK       16
  4746. #define HIGHLIGHT      32
  4747. #define BASEATTRIBUTES 63
  4748. #define NUMATTRIBUTES  6
  4749.  
  4750. struct Attribute
  4751. {
  4752.  int type;
  4753.  XFontStruct *Font;
  4754. };
  4755.  
  4756. struct GraphicsContexts
  4757. {
  4758.  GC draw_gc;
  4759.  GC cursor_gc;
  4760.  GC blink_gc;
  4761. };
  4762.  
  4763. struct PenColour
  4764. {
  4765.  char *ForegroundName;
  4766.  char *BackgroundName;
  4767.  Pixel Foreground;
  4768.  Pixel Background;
  4769.  Pixel HalfBright;
  4770.  int defined;
  4771. };
  4772.  
  4773. struct Field
  4774. {
  4775.  int Start;
  4776.  int End;
  4777. };
  4778.  
  4779. struct Line
  4780. {
  4781.  char *Characters;
  4782.  struct Attribute *Attributes;
  4783.  struct Field *Fields;
  4784.  int ScrollRegion;
  4785.  int NumChars;
  4786.  int NumFields;
  4787. };
  4788.  
  4789. /* Additions to widget class record */
  4790.  
  4791. typedef struct {
  4792.  int dummy;
  4793. } GenTermClassPart;
  4794.  
  4795. /* our full class declaration */
  4796.  
  4797. typedef struct _GenTermClassRec {
  4798.  CoreClassPart core_class;
  4799.  CompositeClassPart composite_class;
  4800.  GenTermClassPart genTerm_class;
  4801. } GenTermClassRec;
  4802.  
  4803. /* new field for our widget record */
  4804.  
  4805. typedef struct _GenTermPart{
  4806.  /* resources */
  4807.  Pixel NormalForeground;        /* the colors when no attributes set */
  4808.  Pixel NormalBackground;
  4809.  XtCallbackList kbdcallback;    /* callbacks for when text is entered */
  4810.  XtCallbackList topcallback;    /* callbacks for when top of screen changes */
  4811.  XtCallbackList resizecallback; /* callbacks for when widget size changes */
  4812.  XtCallbackList lockcallback;   /* callbacks for when widget locks itself */
  4813.  Dimension width;               /* our width width and height */
  4814.  Dimension height;
  4815.  Dimension BottomMargin;        /* used distance from bottom */
  4816.  int rows;                      /* number of rows and columns to show */
  4817.  int columns;
  4818.  int SaveLines;                 /* number of lines in the virtual screen */
  4819.  char *Font;                    /* the name of the base font */
  4820.  char *AlternateFont;           /* the name of the alternate font */
  4821.  int CharHeight;                /* the current character height */
  4822.  int CharWidth;                 /* the current character width */
  4823.  Boolean AutoWrap;              /* flag if text wraps at the end of the line */
  4824.  Boolean LiteralMode;           /* flag if to escape undisplayable text */
  4825.  Pixel CursorColour;            /* the colour of our cursor */
  4826.  Boolean CursorFloats;          /* flag is cursor floats when screen moved */
  4827.  Boolean ShowCursor;            /* flag if cursor should be displayed or not */
  4828.  Boolean FieldAttributes;       /* flag if text attributes are field oriented*/
  4829.  Boolean ScrollDownClear;       /* clear lines when they scroll into page? */
  4830.  Boolean FullScroll;            /* scroll through entire virtual screen? */
  4831.  Boolean InsertMode;            /* flag if in insert mode or not */
  4832.  Boolean SaveScrollRegion;      /* save scroll region lines in virtual screen*/
  4833.  Boolean EOLStick;              /* flag if cursor sticks at end of line */
  4834.  int NumberColours;             /* number of pen colours allowed */
  4835.  int BlinkRate;                 /* the blink rate for blinking text */
  4836.  char *PenColours;              /* the pen colours */
  4837.  int DefineColour;              /* colours are defined by RGB or HSL */
  4838.  Boolean ScrollOnOutput;        /* flag if to scroll to bottom on text output*/
  4839.  Boolean CursorKeyScrollRegion; /* cursor keys are sensitive to scrollregion?*/
  4840.  Boolean MarginBell;            /* flag if margin bell is activated */
  4841.  int Margin;                    /* the size of the margin. */
  4842.  Boolean Locked;                /* flag is screen currently locked */
  4843.  Boolean IgnoreNull;            /* flag if to ignore null characters */
  4844.  
  4845. /* private state info */
  4846.  
  4847.  XFontStruct *CurrentFont;
  4848.  XFontStruct *AltFont;
  4849.  Pixel CurrentForeground;    /* currently defined foreground and background */
  4850.  Pixel CurrentBackground;
  4851.  Pixel foreground;           /* colors in use by the graphics contexts */
  4852.  Pixel background;
  4853.  char MapTable[NMAP];        /* out key mapping tables */
  4854.  struct PenColour *Colours;  /* the pen colour structures */
  4855.  int CurrentRow;             /* the current cursor position (row and col) */
  4856.  int CurrentColumn;
  4857.  int ScreenTop;              /* Top of visible screen in save screen */
  4858.  int VScreenTop;             /* Maximimum extent of ScreenTop */
  4859.  int VirtualTop;             /* relative position of screen (0-SaveLines) */
  4860.  int MaxVirtualTop;          /* Maximum extent of VirtualTop */
  4861.  int CursorOffset;           /* distance cursor is from visible screen */
  4862.  Boolean CursorOn;           /* flag is cursor is turned on */
  4863.  struct Line **VScreen;      /* the virtual screen */
  4864.  struct Line **Screen;       /* the visible screen */
  4865.  struct GraphicsContexts **Pens;  /* the drawing pens */
  4866.  struct Attribute CurrentSettings;     /* our current attribute */
  4867.  struct Attribute DefaultSettings;     /* the default attribute */
  4868.  XtIntervalId BlinkTimeOut;            /* the blink time out structure */
  4869.  int BlinkOn;                /* flag if blinking text is on or off */
  4870.  Colormap ColourMap;         /* out colour map */
  4871.  int CurrentNumRows;         /* the current number of rows in Screen */
  4872.  int CurrentNumColumns;      /* the current number of columns in Screen */
  4873.  int CurrentSaveLines;       /* the current number of rows in virtual screen */
  4874.  Boolean CursorIsStuck;      /* flag if cursor stuck at end of line */
  4875.  int ExposureCount;          /* number of exposure counts pending */
  4876.  Boolean SelectionStarted;   /* flag if currently doing a selection */
  4877.  int SelectionStartRow;      /* position selection started in */
  4878.  int SelectionStartCol;
  4879.  int SelectionEndRow;        /* position selection ended in */
  4880.  int SelectionEndCol;
  4881.  char *CurrentSelection;     /* the currently selected portion of the screen */
  4882.  int CutBuffer;              /* the cut buffer we are using */
  4883. } GenTermPart;
  4884.  
  4885. /* our full instance record */
  4886.  
  4887. typedef struct _GenTermRec
  4888. {
  4889.  CorePart core;
  4890.  CompositePart composite;
  4891.  GenTermPart genTerm;
  4892. } GenTermRec;
  4893.  
  4894. #endif _ReadGenTermP
  4895.  
  4896. #include "GenTerm.h"
  4897. @EOF
  4898. set `wc -lwc <GenTerm/GenTermP.h`
  4899. if test $1$2$3 != 1829036618
  4900. then
  4901.     echo ERROR: wc results of GenTerm/GenTermP.h are $* should be 182 903 6618
  4902. fi
  4903.  
  4904. chmod 644 GenTerm/GenTermP.h
  4905.  
  4906. exit 0
  4907.