home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / curses-widgets / wpage.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.1 KB  |  149 lines

  1. /*****************************************************************************
  2. /*  FILE:        wpage.c
  3. /*  DATE:        August 1988.
  4. /*  AUTHOR:        Richard A. Culshaw.
  5. /*  DESCRIPTION:    A small demonstration program using the widgets.
  6. /* DISCLAIMER:        This file is deemed to be public-domain, on the simple
  7. /*            provisos that this section remains in this file and
  8. /*            that code using it do not do so for monetary gain.
  9. /*            Neither the author, nor the authors employees at the
  10. /*            time of developing this code, accept any liability or
  11. /*            responsibility for the use, abuse or misuse of this
  12. /*            code.
  13. /*****************************************************************************/
  14.  
  15. #include <widget.h>
  16.  
  17. WIDGET    title, nlcmd, npcmd, qcmd, finput, gfile, frac;
  18. char    filename[14];
  19. char    string[20];
  20. int    nolines;
  21. int    filepresent;
  22. FILE    *f;
  23. long    size, ftell();
  24.  
  25. int readline ()
  26. {
  27.     int   i;
  28.     int      space;
  29.     char  buffer [80];
  30.     int   count;
  31.     int   ch;
  32.  
  33.     count = 0;
  34.       while ((count < 79) && (ch = getc(f)) && (ch != '\n') && (ch != EOF))
  35.         if (ch == '\t') {
  36.         space = 8 - count % 8;
  37.         for (i=0; i<space; i++)
  38.             buffer [count++] = ' ';
  39.          }
  40.         else
  41.             buffer [count++] = ch;
  42.         buffer [count] = '\0';
  43.         if ((count > 0) || (ch == '\n'));
  44.         report (buffer);
  45.     return (ch); 
  46. }
  47.  
  48. changefrac ()
  49. {
  50.     long  cur;
  51.     char  data[5];
  52.  
  53.     cur = ftell (f);
  54.     sprintf (data, "%ld%%", cur*100L/size);
  55.     changelblwidget (frac, data, RIGHTJUST|NOHIGH);
  56. }
  57.  
  58.  
  59. nextline ()
  60. {
  61.     int ch;
  62.  
  63.     highlight (nlcmd);
  64.     ch = readline ();
  65.     dehighlight (nlcmd);
  66.     if (ch == EOF) {
  67.     deactivate (npcmd, BLANK);
  68.     deactivate (nlcmd, BLANK);
  69.     }
  70.     changefrac ();
  71.     widgetinput ();
  72. }
  73.  
  74. nextpage ()
  75. {
  76.     int   line  = 0;
  77.     int      ch;
  78.  
  79.     highlight (npcmd);
  80.     cleartextwindow ();
  81.  
  82.     while ((line++ != nolines) && ((ch = readline()) != EOF)) 
  83.     ;
  84.     
  85.     dehighlight (npcmd);
  86.     if (ch == EOF) {
  87.     deactivate (npcmd, BLANK);
  88.     deactivate (nlcmd, BLANK);
  89.     }
  90.  
  91.     changefrac ();
  92.     widgetinput ();
  93. }
  94.  
  95. quit ()
  96. {
  97.     endwidgets ();
  98. }
  99.  
  100. getfile ()
  101. {
  102.     int i;
  103.  
  104.     if (!filepresent) {
  105.         highlight (gfile);
  106.         killwidget (title);
  107.         changelblwidget (frac, "", RIGHTJUST|NOHIGH);
  108.         finput = mkinpwidget ("file", NULL, filename, 14, 0, 0, EXEC);
  109.         killwidget (finput);
  110.     }
  111.     else
  112.     filepresent = FALSE;
  113.     for (i=5; i<=20; i++)
  114.        string [i] = filename [i-5]; 
  115.     dehighlight (gfile);
  116.     f = fopen (filename, "r");
  117.     if (f == NULL) {
  118.     cleartextwindow ();    
  119.     report ("File does not exist. Try again");
  120.     getfile ();
  121.     }
  122.     title = mklblwidget (string, CENTRE, 0, 0);
  123.     fseek (f, 0L, 2);
  124.     size = ftell (f);
  125.     rewind (f);
  126.     activate (nlcmd);
  127.     activate (npcmd);
  128.     nextpage ();
  129. }
  130.  
  131. main (argc, argv)
  132. int    argc;
  133. char    *argv[];
  134. {
  135.     initialisewidgets ();
  136.     if (argc > 1) {
  137.     strcpy (filename, argv[1]);
  138.     filepresent = TRUE;
  139.     }
  140.     strcpy (string, "File=");
  141.     qcmd = mkcmdwidget ("Quit", 'q', quit, 0);
  142.     nlcmd = mkcmdwidget ("Next line", 'n', nextline, 0);
  143.     npcmd = mkcmdwidget ("Next page", 'N', nextpage, 0);
  144.     gfile = mkcmdwidget ("Get file", 'f', getfile, 0);
  145.     frac = mklblwidget ("", CENTRE, 4, 0);
  146.     nolines = opentextwindow (0, HORIZONTAL);
  147.     getfile ();
  148. }    
  149.