home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / EDITOR / PT20PC.ZIP / WINDCOM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-04  |  11.8 KB  |  483 lines

  1. #include "pt.h"
  2. #include "ctype.h"
  3. #include "string.h"
  4. #include "io.h"
  5.  
  6. void pascal
  7. /* XTAG:doNewWindow */
  8. doNewWindow(fn, fullScreen)
  9.     int fn;
  10. {
  11.     extern unsigned char msgBuffer[];
  12.     extern unsigned char textBuffer[];
  13.     extern int scrRows, scrCols;
  14.     extern int menuLine;
  15.  
  16.     register unsigned char *fileName;
  17.     int n, row1, row2, col1, col2;
  18.  
  19.     if( fullScreen ) {
  20.         row1 = row2 = (menuLine > 0 ? 1 : 0);
  21.         col1 = col2 = 0;
  22.     } else if( getBox(0, 0, &row1, &col1, &row2, &col2, 0) == 2 )
  23.         goto cancelWindow;
  24.  
  25.     if( row1 == row2 && col1 == col2 ) {
  26.         row2 = scrRows-1;
  27.         if( menuLine < 0 )
  28.             --row2;
  29.         col2 = scrCols-1;
  30.     }
  31.     /* do not allow very small windows */
  32.     if( row2 < row1+2 || col2 < col1+10 ) {
  33.         msg("Size too small, no window created", 1);
  34.         return;
  35.     }
  36.     if( fn == FNEWWINDOW )
  37.         fileName = getFileName("File to load into the new window: ");
  38.     else    /* fn == FNEWSEL -- file name is the selection */
  39.         fileName = getSelection(&textBuffer[0]);
  40.     if( fileName == NULL ) {
  41. cancelWindow:
  42.         msg("Window create cancelled", 1);
  43.         return;
  44.     }
  45.     if( fileName[0] == '\0' ) {
  46.         strcpy(fileName, "UnNamed.a");
  47.         (void)makeName(fileName);
  48.     }
  49.     fileName = noWhiteSpace(fileName);
  50.     strcpy(textBuffer, fileName);
  51.     if( access(textBuffer, 0) == -1 ) {
  52.         sprintf(msgBuffer, "y to create «%s»: ", textBuffer);
  53.         fileName = getInput(msgBuffer, "y", 1);
  54.         if( fileName == NULL || tolower(*fileName) != 'y' )
  55.             goto cancelWindow;
  56.         n = creatls(textBuffer, 0);
  57.         if( n < 0 ) {
  58.             sprintf(msgBuffer, "Cannot create «%s»: ",
  59.                 textBuffer);
  60.             msg(msgBuffer, 3);
  61.             goto cancelWindow;
  62.         } else
  63.             closels(n);
  64.     }
  65.     createWindow(textBuffer, row1, col1, row2, col2, CRTOP, 0);
  66. }
  67.  
  68. unsigned char * pascal
  69. /* XTAG:noWhiteSpace */
  70. noWhiteSpace(fileName)
  71.     register unsigned char *fileName;
  72. {
  73.     register int n;
  74.  
  75.     if( fileName != NULL || fileName[0] == '\0' ) {
  76.         /* eliminate white space around fileName */
  77.  
  78.         /* first white space in the beginning */
  79.         while( isspace(*fileName) )
  80.             ++fileName;
  81.  
  82.         /* now white space at the end */
  83.         n = strlen(fileName) - 1;
  84.         while( isspace(fileName[n]) )
  85.             --n;
  86.         fileName[n+1] = '\0';
  87.     }
  88.     return fileName;
  89. }
  90.  
  91. void pascal
  92. /* XTAG:bottomFile */
  93. bottomFile(w)
  94.     register struct window *w;
  95. {
  96.     extern int debug;
  97.  
  98.     long cp;
  99.     int fid, j;
  100.     register int i;
  101.  
  102.     if( w == NULL )
  103.         return;
  104.     /* remember where we came from */
  105.     w->rowLastline = w->numTopline;
  106.     cp = w->posBotline;
  107.  
  108.     fid = w->fileId;    /* we'll use this a lot */
  109.  
  110.     /* find the last line of the file */
  111.     i = 0;
  112.     while( 1 ) {
  113.         j = 1;
  114.         cp = nextLine(fid, cp, &j);
  115.         /* if j==0, we could not go down a line */
  116.         /* so we are at the end */
  117.         if( j == 0 )
  118.             break;
  119.         ++i;
  120.     }
  121.     ++i;    /* one more line so EOF mark shows */
  122.  
  123.     /* now move the window down and redraw it */
  124.     j = i;    /* since i is a register variable, we must use j here */
  125.     w->posTopline = nextLine(fid, w->posTopline, &j);
  126.     w->posBotline = cp;
  127.     w->numTopline += j;
  128.     w->numBotline += j;
  129.     w->indent = 0;
  130.     redrawWindow(w);
  131. }
  132.  
  133. void pascal
  134. /* XTAG:loadWindow */
  135. loadWindow(w, fn)
  136.     register struct window *w;
  137.     int fn;
  138. {
  139.     extern struct window *selWindow;
  140.     extern long selBegin, selEnd;
  141.     extern unsigned char msgBuffer[];
  142.     extern unsigned char textBuffer[];
  143.     extern struct openFile *files;
  144.  
  145.     int n;
  146.     unsigned char *fileName, saveName[64];
  147.  
  148.     if( fn == FLOADFILE )
  149.         fileName = getFileName("File to load in window: ");
  150.     else    /* fn == FLOADSEL -- get file name from the selection */
  151.         fileName = getSelection(&textBuffer[0]);
  152.     if( fileName == NULL )
  153.         goto cancelFileLoad;
  154.     if( fileName[0] == '\0' ) {
  155.         strcpy(fileName, "UnNamed.a");
  156.         makeName(fileName);
  157.     }
  158.     fileName = noWhiteSpace(fileName);
  159.     strcpy(saveName, fileName);
  160.     n = 0;    /* we did NOT create 'saveName' */
  161.     if( access(saveName, 0) == -1 ) {
  162.         sprintf(msgBuffer, "y to create «%s»: ", saveName);
  163.         fileName = getInput(msgBuffer, "y", 1);
  164.         if( fileName == NULL || tolower(*fileName) != 'y' )
  165.             goto cancelFileLoad;
  166.         n = creatls(saveName, 0);
  167.         if( n < 0 ) {
  168.             sprintf(msgBuffer, "Cannot create «%s»: ", saveName);
  169.             msg(msgBuffer, 3);
  170.             goto cancelFileLoad;
  171.         } else
  172.             closels(n);
  173.         n = 1;    /* we did create 'saveName' */
  174.     }
  175.     if( closeFile(w->fileId, 1) == -1 ) {
  176.         if( n )    /* delete 'saveName' if we created it */
  177.             delete(saveName);
  178. cancelFileLoad:
  179.         msg("File load cancelled", 1);
  180.         return;
  181.     }
  182.     w->fileId = getFileId(saveName);
  183.     w->nameOffset = getBaseName(files[w->fileId].origName);
  184.     w->posTopline = 0;
  185.     w->numTopline = 1;
  186.     w->posCurLast = 0;
  187.     w->lastPosTop = 0;
  188.     w->rowCurLast = 0;
  189.     w->indent = 0;
  190.     if( w == selWindow ) {
  191.         selBegin = selEnd = 0;
  192.         if( readChar(w->fileId, 0L) == '\r' ) {
  193.             if( readChar(w->fileId, 1L) == '\n' )
  194.                 selEnd = 1;
  195.         }
  196.     }
  197.     /* get the state of unixMode */
  198.     w->state = getUnixState(w->fileId);
  199.     redrawWindow(w);
  200. }
  201.  
  202. void pascal
  203. /* XTAG:hideWindow */
  204. hideWindow(w)
  205.     register struct window *w;
  206. {
  207.     extern struct window *windowList;
  208.     extern struct window *activeWindow;
  209.     extern struct window *hiddenList;
  210.     extern struct window *selWindow;
  211.     extern long selBegin, selEnd;
  212.     extern int scrRows, scrCols;
  213.     
  214.     if( w == NULL )
  215.         return;
  216.  
  217.     /* unlink the window from the list of active windows */
  218.     if( w->prevWindow != NULL )
  219.         w->prevWindow->nextWindow = w->nextWindow;
  220.     else    /* must be first on the list -- the top window */
  221.         windowList = w->nextWindow;
  222.     if( w->nextWindow != NULL )
  223.         w->nextWindow->prevWindow = w->prevWindow;
  224.  
  225.     /* stack w onto the hidden list */
  226.     w->nextWindow = hiddenList;
  227.     hiddenList = w;
  228.  
  229.     /* is the selection in this window? */
  230.     if( w == selWindow ) {
  231.         /* move the selection to the top window */
  232.         selWindow = windowList;
  233.         if( selWindow != NULL ) {
  234.             selBegin = 0;
  235.             selEnd = 0;
  236.             if( readChar(selWindow->fileId, 0L) == '\r' ) {
  237.                 if( readChar(selWindow->fileId, 1L) == '\n' )
  238.                     selEnd = 1;
  239.             }
  240.         }
  241.     }
  242.  
  243.     /* is this the active window? */
  244.     if( w == activeWindow )
  245.         activeWindow = windowList;
  246.  
  247.     /* redraw the screen */
  248.     redrawBox(0, 0, scrRows-1, scrCols-1);
  249.     updateScreen(0, scrRows-1);
  250. }
  251.  
  252. void pascal
  253. /* XTAG:doContextMenu */
  254. doContextMenu()
  255. {
  256.     extern struct window *windowList;
  257.     extern struct window *hiddenList;
  258.     extern int menuRow, menuCol;
  259.     extern struct menuBlock far *menus[];
  260.     extern struct openFile *files;
  261.     extern int pathNames;
  262.     
  263.     int i, n, k;
  264.     struct window *newW;
  265.     register struct window *w2;
  266.  
  267.     /* put in the title of the menu */
  268.     menus[0]->cmdName[0] = (unsigned char far *)"TOPLIST";
  269.     menus[0]->cmdNumber[0] = 0;
  270.  
  271.     /* go through the list of visible windows */
  272.     n = 1;
  273.     i = 1;
  274.     w2 = windowList;
  275.     while( w2 != NULL && n < MAXMENUITEMS ) {
  276.         k = (pathNames ? 0 : w2->nameOffset);
  277.         menus[0]->cmdName[n] = (unsigned char far *)
  278.                     &(files[w2->fileId].origName[k]);
  279.         menus[0]->cmdNumber[n] = i;
  280.         w2 = w2->nextWindow;
  281.         ++n;
  282.         ++i;
  283.     }
  284.  
  285.     if( hiddenList != NULL && n < MAXMENUITEMS ) {
  286.         /* put in a divider line */
  287.         menus[0]->cmdName[n] = (unsigned char far *)"════════════";
  288.         menus[0]->cmdNumber[n] = 0;
  289.         ++n;
  290.     }
  291.  
  292.     /* now go through the list of hidden windows */
  293.     w2 = hiddenList;
  294.     /* use "command" numbers in the 300's to indicate hidden */
  295.     /* windows.  This avoids conflicts with the real command */
  296.     /* number from 1 to ~150.  For ~100 to ~150 they are option */
  297.     /* setting commands and so they ignore the string you send in. */
  298.     i = 301;    /* number the entries */
  299.     while( w2 != NULL && n < MAXMENUITEMS ) {
  300.         k = (pathNames ? 0 : w2->nameOffset);
  301.         menus[0]->cmdName[n] =
  302.             (unsigned char far *)&(files[w2->fileId].origName[k]);
  303.         menus[0]->cmdNumber[n] = i;
  304.         w2 = w2->nextWindow;
  305.         ++n;
  306.         ++i;
  307.     }
  308.  
  309.     /* now display the menu */
  310.     menus[0]->nItems = n;
  311.     i = menu(menus[0], menuRow, menuCol);
  312.  
  313.     /* find out which window to top (or unhide and top) */
  314.     if( i == 0 )
  315.         return;
  316.     if( i > 300 ) {    /* unhide and top a hidden window */
  317.         i -= 301;
  318.         if( i == 0 ) {
  319.             newW = hiddenList;
  320.             hiddenList = hiddenList->nextWindow;
  321.         } else {
  322.             w2 = hiddenList;
  323.             while( --i > 0 )
  324.                 w2 = w2->nextWindow;
  325.             newW = w2->nextWindow;
  326.             /* unlink it from the hidden list */
  327.             w2->nextWindow = newW->nextWindow;
  328.         }
  329.         /* put it on the windowList */
  330.         newW->nextWindow = windowList;
  331.         newW->prevWindow = NULL;
  332.         if( windowList != NULL )
  333.             windowList->prevWindow = newW;
  334.         windowList = newW;
  335.     } else {    /* top a visible window */
  336.         newW = windowList;
  337.         while( --i > 0 )
  338.             newW = newW->nextWindow;
  339.     }
  340.     doTopWindow(newW, 0);
  341. }
  342.  
  343. void pascal
  344. /* XTAG:zoomWindow */
  345. zoomWindow(w)
  346.     register struct window *w;
  347. {
  348.     extern struct window *windowList;
  349.     extern int scrRows, scrCols;
  350.  
  351.     /* top window, if not already on top */
  352.     if( w != windowList )
  353.         topWindow(w);
  354.  
  355.     /* if zoomed then make regular else zoom */
  356.     if( w->row1==1 && w->row2==(scrRows-1)
  357.      && w->col1==0 && w->col2==(scrCols-1) ) {
  358.         w->row1 = w->saveRow1;
  359.         w->col1 = w->saveCol1;
  360.         w->row2 = w->saveRow2;
  361.         w->col2 = w->saveCol2;
  362.     } else {
  363.         w->saveRow1 = w->row1;
  364.         w->saveCol1 = w->col1;
  365.         w->saveRow2 = w->row2;
  366.         w->saveCol2 = w->col2;
  367.  
  368.         w->row1 = 1;
  369.         w->col1 = 0;
  370.         w->row2 = (scrRows-1);
  371.         w->col2 = (scrCols-1);
  372.     }
  373.     redrawBox(0, 0, (scrRows-1), (scrCols-1));
  374.     updateScreen(0, (scrRows-1));
  375. }
  376.  
  377. void pascal
  378. /* XTAG:doTopWindow */
  379. doTopWindow(w, howToTop)
  380.     register struct window *w;
  381.     int howToTop;
  382. {
  383.     extern int scrRows, scrCols;
  384.     extern struct window *activeWindow;
  385.     extern struct window *windowList;
  386.     extern int debug;
  387.     
  388.     int row1, row2;
  389.     struct window *w1;
  390.  
  391.     if( w == NULL )
  392.         return;
  393.  
  394.     switch( howToTop ) {
  395.  
  396.     case 0:        /* top only */
  397.         if( w != windowList )
  398.             topWindow(w);
  399.         else {
  400.             activeWindow = w;
  401.             redoBorders(0);
  402.             /* break rather than return here so the the */
  403.             /* unhide will work correctly */
  404.             break;
  405.         }
  406.         break;
  407.  
  408.     case 1:    /* top if not already on top, else bottom */
  409.         if( w != windowList ) {
  410.             topWindow(w);
  411.             break;
  412.         } /* else drop through to bottom code */
  413.  
  414.     case 2:        /* bottom only */
  415.         /* find the end of the window list */
  416.         w1 = w;
  417.         while( w1->nextWindow != NULL )
  418.             w1 = w1->nextWindow;
  419.         if( w == w1 )    /* already on the bottom? */
  420.             break;
  421.         /* unlink from window list */
  422.         /* we know w->nextWindow != NULL, else would be last */
  423.         w->nextWindow->prevWindow = w->prevWindow;
  424.         if( w->prevWindow != NULL )
  425.             w->prevWindow->nextWindow = w->nextWindow;
  426.         else        /* must be first on the list */
  427.             windowList = w->nextWindow;
  428.             /* windowList->prevWindow set to NULL above */
  429.         /* link it on to the end */
  430.         w1->nextWindow = w;
  431.         w->prevWindow = w1;
  432.         w->nextWindow = NULL;
  433.         if( w == activeWindow )
  434.             activeWindow = windowList;
  435.         break;
  436.  
  437.     case 3:        /* swap the top two windows */
  438.         if( windowList == NULL )    /* no windows to swap */
  439.             break;
  440.         w1 = windowList->nextWindow;
  441.         if( w1 == NULL )    /* not two windows to swap */
  442.             break;
  443.         /* swap the windows */
  444.         w = w1->nextWindow;
  445.         windowList->nextWindow = w;
  446.         if( w != NULL )
  447.             w->prevWindow = windowList;
  448.         w1->nextWindow = windowList;
  449.         windowList->prevWindow = w1;
  450.         w1->prevWindow = NULL;
  451.         activeWindow = windowList = w1;
  452.         break;
  453.     }
  454.  
  455.     /* set up the screen map for the old window location */
  456.     /* and clear the area where the window was */
  457.     setMap(0, 0, scrRows-1, scrCols-1, 1, 0x07);
  458.  
  459.     /* redraw all the windows that might have changed */
  460.     w1 = windowList;
  461.     /* try to minimize redrawing when there are full screens */
  462.     row1 = scrRows;
  463.     row2 =-1;
  464.     while( w1 != NULL ) {
  465.         /* have these rows already been written? */
  466.         if( w1->row1 >= row1 && w1->row2 <= row2 )
  467.             goto nextOne;
  468.         /* if the window is full width, remember the rows */
  469.         if( w1->col1 == 0 && w1->col2 == (scrCols-1) ) {
  470.             /* will this cover some rows below row1? */
  471.             if( w1->row1 < row1 )
  472.                 row1 = w1->row1;
  473.             /* will this cover some rows above row2? */
  474.             if( w1->row2 > row2 )
  475.                 row2 = w1->row2;
  476.         }
  477.         drawWindow(w1);
  478.     nextOne:
  479.         w1 = w1->nextWindow;
  480.     }
  481.     updateScreen(0, scrRows-1);
  482. }
  483.