home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff214.lzh / MandelVroom / src / showhelp.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  12KB  |  425 lines

  1. /*
  2.  * browser - Rummage around on disks.
  3.  *
  4.  *      copyright (c) 1986, Mike Meyer
  5.  *
  6.  * Permission is hereby granted to distribute this program, so long as this
  7.  * source file is distributed with it, and this copyright notice is not
  8.  * removed from the file.
  9.  *
  10.  * Hacked up to be used as file displayer within MandelVroom.  By Kevin
  11.  * Clague
  12.  */
  13.  
  14. #include "mandp.h"
  15.  
  16. #define INTUITION_REV           1L
  17. #define GRAPHICS_REV            1L
  18.  
  19. #define LONGEST_NAME            80      /* Longest file name we can deal with */
  20. #define LONGEST_LINE            256     /* Longest line we will deal with */
  21. #define AVG_LINE_LENGTH         40      /* A guess, tune it if you need to */
  22.  
  23. #define UP_GADGET               ((unsigned short) 0)
  24. #define DOWN_GADGET             ((unsigned short) 1)
  25. #define SCROLL_GADGET           ((unsigned short) 2)
  26. #define GWIDTH                  16      /* Width of my two gadgets */
  27. #define GHEIGHT                 9       /* and their heights */
  28.  
  29. #define FIRST 18L
  30.  
  31. /*
  32.  * Pictures for the up and down arrows
  33.  */
  34. USHORT arrows[2][GHEIGHT] = {
  35.         {0xFE7F,
  36.          0xFC3F,
  37.          0xF81F,
  38.          0xF00F,        /* Up */
  39.          0xFE7F,
  40.          0xFE7F,
  41.          0xFE7F,
  42.          0xFE7F,
  43.          0xFE7F
  44.         },{
  45.          0xFE7F,
  46.          0xFE7F,
  47.          0xFE7F,
  48.          0xFE7F,        /* Down */
  49.          0xFE7F,
  50.          0xF00F,
  51.          0xF81F,
  52.          0xFC3F,
  53.          0xFE7F}
  54.         } ;
  55.  
  56. /*
  57.  * Now, the Image structures that use the arrows
  58.  */
  59. struct Image Arrow_Image[2] = {
  60.         {0, 0, GWIDTH, GHEIGHT, 1, NULL, 1, 0, NULL}, /* Up */
  61.         {0, 0, GWIDTH, GHEIGHT, 1, NULL, 1, 0, NULL} /* Down */
  62.         };
  63. /*
  64.  * Now, my Gadget structures
  65.  */
  66. static struct PropInfo prop;
  67. static struct Image prop_img;
  68.  
  69. static struct Gadget Scroll_Gadget = {
  70.         /*(struct Gadget *)*/ NULL,          /* End of Gadgets */
  71.         0,10+GHEIGHT,                        /* Left, Top */
  72.         GWIDTH, -((GHEIGHT*2)+11),
  73.         GRELHEIGHT | GADGHCOMP,
  74.         GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY, /* Messages when released */
  75.         PROPGADGET,
  76.         (APTR) &prop_img,
  77.         (APTR) NULL,                        /* No rendering image, using HCOMP */
  78.         /*(struct IntuiText *)*/ NULL,
  79.         0L,                                 /* No mutex */
  80.         (APTR) &prop,
  81.         HELPSCROLL,                         /* Yes, this is the scroll gadget */
  82.         (APTR) NULL                         /* And nothing of mine */
  83. };
  84.  
  85. static struct Gadget Up_Gadget = {
  86.         &Scroll_Gadget,                     /* next gadget is scroll */
  87.         0,10,                               /* Left, Top */
  88.         GWIDTH, GHEIGHT,
  89.         GADGIMAGE | GADGHCOMP,
  90.         GADGIMMEDIATE,                      /* Messages when released */
  91.         BOOLGADGET,                         /* These be boolean gadgets */
  92.         (APTR) &(Arrow_Image[UP_GADGET]),
  93.         (APTR) NULL,                        /* No rendering image, using HCOMP */
  94.         /*(struct IntuiText *)*/ NULL,
  95.         0L,                                 /* No mutex */
  96.         (APTR) NULL,                        /* Nothing special */
  97.         HELPUP,                             /* Yes, this is the up gadget */
  98.         (APTR) NULL                         /* And nothing of mine */
  99. };
  100.  
  101. static struct Gadget Down_Gadget = {
  102.         &Up_Gadget,                         /* Next gadget is Up_Gadget */
  103.         0,  -GHEIGHT,                       /* Left, Top */
  104.         GWIDTH, GHEIGHT,
  105.         GRELBOTTOM | GADGIMAGE |            /* Standard bottom border gadget */
  106.         GADGHCOMP,
  107.         GADGIMMEDIATE | BOTTOMBORDER,       /* Messages when released */
  108.         BOOLGADGET,                         /* These be boolean gadgets */
  109.         (APTR) &(Arrow_Image[DOWN_GADGET]),
  110.         (APTR) NULL,                        /* No rendering image, using HCOMP */
  111.         /*(struct IntuiText *)*/ NULL,
  112.         0L,                                 /* No mutex */
  113.         (APTR) NULL,                        /* Nothing special */
  114.         HELPDOWN,                           /* Yes, this is the up gadget */
  115.         (APTR) NULL                         /* And nothing of mine */
  116.         };
  117.  
  118. /*
  119.  * Now, the window for it all
  120.  */
  121. static struct NewWindow New_Window = {
  122. #ifdef DEBUG
  123.         0, 0, 320, 150,                 /* smaller window to left printf's show up */
  124. #else
  125.         0, 1, 320, 199,                 /* Full screen */
  126. #endif
  127.         -1L, -1L,                               /* Default pens */
  128.         NULL,                           /* Window closes and gadgets */
  129.         ACTIVATE                        /* Standard window */
  130.         | SMART_REFRESH | NOCAREREFRESH | SIZEBBOTTOM
  131.         | WINDOWSIZING | WINDOWDEPTH | WINDOWCLOSE | WINDOWDRAG,
  132.         &Down_Gadget,                   /* Add my gadgets */
  133.         /*(struct Image *)*/ NULL,
  134.         (UBYTE *) "MandelVroom Help Window",  /* Title */
  135.         /*(struct Screen *)*/NULL,
  136.         /*(struct BitMap *)*/NULL,
  137.         100, 40,                        /* Minimum sizes */
  138.         -1, -1,                         /* Maximum sizes */
  139.         CUSTOMSCREEN
  140.         } ;
  141.  
  142. /*
  143.  * My very own variables (mostly for done)
  144.  */
  145.        struct Window    *HelpWind = NULL ;
  146. static FILE             *infile = NULL ;        /* Current input file */
  147. static void             Page_File();
  148.        char             HelpOpen;
  149.  
  150. /*
  151.  * Finally, declare the string twiddling functions as voids
  152.  */
  153. void    strcat(), strcpy(), strncat();
  154.  
  155. AllocArrows()
  156. {
  157.   extern USHORT *MakeChipSprite();
  158.  
  159.   Arrow_Image[0].ImageData = MakeChipSprite( arrows[0], GHEIGHT );
  160.   Arrow_Image[1].ImageData = MakeChipSprite( arrows[1], GHEIGHT );
  161. }
  162.  
  163. FreeArrows()
  164. {
  165.   USHORT *Temp;
  166.  
  167.   Temp = Arrow_Image[0].ImageData;
  168.  
  169.   if (Temp)
  170.     FreeMem( (char *) Temp, (long) sizeof(arrows[0]));
  171.   Arrow_Image[0].ImageData = NULL;
  172.  
  173.   Temp = Arrow_Image[1].ImageData;
  174.  
  175.   if (Temp)
  176.     FreeMem( (char *) Temp, (long) sizeof(arrows[0]));
  177.   Arrow_Image[1].ImageData = NULL;
  178. }
  179.  
  180. /*
  181.  * Display_File - given a directory path and file name, put the first page of
  182.  *      the file in the window.
  183.  */
  184.  
  185. long aprox_lines, file_size;
  186. long Page_Length = 22L;
  187.  
  188. static int old_off, new_off;
  189.  
  190. static
  191. Display_File(dir, name)
  192. char *dir, *name;
  193. {
  194.   static char     File_Name[LONGEST_NAME];
  195.   FILE *fopen();
  196.   long ftell();
  197.   long i;
  198.  
  199.   if (HelpWind == NULL)
  200.     return;
  201.  
  202.   old_off = -1;
  203.  
  204.   /* Get the file name */
  205.   strcpy(File_Name, dir);
  206.   strcat(File_Name, name);
  207.  
  208.   if (infile != NULL)
  209.     fclose(infile);
  210.  
  211.   if ((infile = fopen(File_Name, "r")) == NULL) {
  212.     CloseHelpWind(20, "can't open file") ;
  213.     return;
  214.   }
  215.  
  216.   /* set up the prop gadget for scrolling */
  217.   fseek(infile, 0L, 2);
  218.   file_size = ftell(infile);
  219.   aprox_lines = file_size / AVG_LINE_LENGTH;
  220.   prop.Flags = FREEVERT | AUTOKNOB;
  221.   if (Page_Length >= aprox_lines)
  222.     i = 0xAAAA; /* guess 66% for small files */
  223.   else
  224.     i = (Page_Length * 0x10000) / aprox_lines; /* FFFF=100% - 0000=0% */
  225.  
  226.   prop.VertBody = i;
  227.   prop.VertPot = 0;       /* always start at begin of file */
  228.   fseek(infile, 0L, 0);
  229.  
  230.   Page_File(HELPUP);         /* Down from page 0 */
  231. }
  232.  
  233. /*
  234.  * Page_File - move the file up or down one "page"
  235.  */
  236. void
  237. Page_File(direction)
  238. int direction;
  239. {
  240.   register long where;
  241.   static char     buffer[LONGEST_LINE];
  242.   static char edited[84]; /* allow room for a tab at end */
  243.   int end_flag = 0;
  244.   long tabs, size, Line_Length;
  245.   int i,j;
  246.   long new_pos;
  247.   char *p;
  248.  
  249.   register struct Window *Window;
  250.  
  251.   Window = HelpWind;
  252.  
  253.   if (infile == NULL) return ;
  254.  
  255.   Page_Length = (Window -> Height - 20) / 8 ;
  256.   Line_Length = (Window -> Width - (3+FIRST)) / 8;
  257.  
  258.   switch (direction) {
  259.  
  260.    case HELPUP:         /* Seek back one page */
  261.         if (ftell(infile) < AVG_LINE_LENGTH * (Page_Length + 2))
  262.           fseek(infile, 0L, 0);
  263.         else {
  264.           fseek(infile, (long) -Page_Length * AVG_LINE_LENGTH, 1) ;
  265.           fgets(buffer, LONGEST_LINE, infile) ;
  266.         }
  267.         break;
  268.  
  269.    case HELPDOWN:
  270.         break;
  271.  
  272.    case HELPSCROLL:
  273.         /* compute new position based on the users scroll bar pot */
  274.         new_pos = (file_size * prop.VertPot) / 0x10000;
  275.  
  276.         /* if at end of file, back up 1/2 page */
  277.         if (new_pos >= file_size)
  278.           new_pos = file_size - ((Page_Length / 2) * AVG_LINE_LENGTH);
  279.           fseek(infile, new_pos, 0);
  280.  
  281.           /* discard a partial line */
  282.           if (new_pos)
  283.             fgets(buffer, LONGEST_LINE, infile);
  284.         new_off = ftell(infile);
  285.  
  286.         if (new_off == old_off)
  287.           return;
  288.  
  289.         old_off = new_off;
  290.         break;
  291.  
  292.    default:
  293.         CloseHelpWind(20, "Illegal argument to Page_File");
  294.         return;
  295.   }
  296.  
  297.   SetAPen(Window->RPort, NORMALPEN);
  298.   SetDrMd(Window->RPort, JAM1);
  299.   RectFill(Window->RPort, GWIDTH, 10,
  300.                           Window->Width-2, Window->Height-10);
  301.   RectFill(Window->RPort, GWIDTH, Window->Height-10,
  302.                           Window->Width-12-(YScale*4),
  303.                           Window->Height);
  304.   BorderWindow( Window );
  305.  
  306.   /* now put out one page's worth of the file's data */
  307.   for (where = 17, j = Page_Length; j--; where += 8) {
  308.  
  309.     /* blank the buffer first */
  310.     for (i = 0; i < 80; i++)
  311.       edited[i] = ' ';
  312.  
  313.     if (!end_flag) {
  314.  
  315.       if (fgets(buffer, LONGEST_LINE, infile) == NULL)
  316.         end_flag = TRUE;
  317.       else {
  318.         size = strlen(buffer);
  319.  
  320.         /* remove the newline */
  321.         buffer[size-1] = '\0';
  322.         size--;
  323.  
  324.         p = buffer;
  325.         size = 0;
  326.  
  327.         /* edit the buffer for tabs and non-printables */
  328.         while(*p) {
  329.  
  330.           if (*p == '\t') {
  331.             do {
  332.               edited[size++] = ' ';
  333.             } while(size&3);
  334.           } else
  335.  
  336.           if (*p < ' ') {
  337.             edited[size++] = '^';
  338.             edited[size++] = *p + '@';
  339.  
  340.           } else
  341.             edited[size++] = *p;
  342.  
  343.           p++;
  344.  
  345.           /* mark the line as longer than the window */
  346.           if (size >= Line_Length) {
  347.             edited[size-1] = '>';
  348.             break;
  349.           }
  350.         }
  351.       }
  352.     }
  353.  
  354.     SetAPen(Window->RPort, SHADOWPEN);
  355.     Move(Window -> RPort, FIRST+1, where+1) ;
  356.     Text(Window -> RPort, edited, Line_Length);
  357.  
  358.     SetAPen(Window->RPort, HIGHLIGHTPEN);
  359.     Move(Window -> RPort, FIRST, where) ;
  360.     Text(Window -> RPort, edited, Line_Length);
  361.  
  362.   } /* for 1 to size of window */
  363. }
  364.  
  365. ShowHelp( DirName, FileName )
  366.   char *DirName, *FileName;
  367. {
  368.   register struct IntuiMessage    *message;
  369.   register unsigned short         class, code ;
  370.  
  371.   if (HelpWind == NULL) {
  372.  
  373.     /* set up the scroll bar */
  374.     prop.Flags = FREEVERT | AUTOKNOB;
  375.     prop.VertBody = 0x1000;
  376.  
  377.     New_Window.Screen = screen;
  378.  
  379.     HelpWind = OpenMyWind(&New_Window,screen, NULL, 320, 200);
  380.  
  381.     if (HelpWind == NULL){
  382.       CloseHelpWind(20, "can't open the window") ;
  383.       return;
  384.     }
  385.  
  386.     if (HelpWind == NULL) return;
  387.  
  388.     AddGList( HelpWind, &Down_Gadget, -1, -1);
  389.  
  390.     RefreshGadgets( &Down_Gadget, HelpWind, NULL );
  391.   }
  392.  
  393.   SetAPen(HelpWind->RPort, NORMALPEN);
  394.   SetDrMd(HelpWind->RPort, JAM1);
  395.   RectFill(HelpWind->RPort, GWIDTH, 10,
  396.                           HelpWind->Width-2, HelpWind->Height-10);
  397.   RectFill(HelpWind->RPort, GWIDTH, HelpWind->Height-10,
  398.                           HelpWind->Width-12-(YScale*4),
  399.                           HelpWind->Height);
  400.   BorderWindow( HelpWind );
  401.  
  402.   Display_File(DirName, FileName) ;
  403. }
  404.  
  405. /*
  406.  * done - just close everything that's open, and exit.
  407.  */
  408. CloseHelpWind(how, why)
  409. int how;
  410. char *why;
  411. {
  412.   if (HelpWind) {
  413.     CloseMyWind(HelpWind, NULL) ;
  414.     HelpWind = NULL;
  415.  
  416.     if (infile) {
  417.       fclose(infile) ;
  418.       infile = NULL;
  419.     }
  420.     if (why)
  421.       DispErrMsg(why,0);
  422.   }
  423. }
  424.  
  425.