home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / generic / application.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.1 KB  |  356 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* Here's a generic program that draws a black screen with a red cross
  19.  * going from corner to corner.  It uses all the generic features:
  20.  * checkpoint files, backup files, the pull-down menu, the generic
  21.  * confirmer, and a generic master gizmo.
  22.  * 
  23.  * The commands that read, write, insert, etc., files do nothing except
  24.  * set the internal name of the currently open file.  The Touch command
  25.  * in the Edit pull-down menu simulates changing the file within the
  26.  * program.
  27.  * 
  28.  * It also has a dummy write failure in the file writing routine that
  29.  * fails every fifth time to simulate out-of-space errors, etc.
  30.  * 
  31.  * All the application-specific stuff is in this file, except for an
  32.  * entry in the file pulldown.c that adds an edit pulldown with a
  33.  * "touch" command in it.
  34.  * 
  35.  * The included code should compile under both C and C++.
  36.  */
  37.  
  38. #include "generic.h"
  39. #include "gizmo.h"                      /* OPTIONAL - gizmos */
  40. #include <stdio.h>
  41. #include <gl.h>
  42. #include <device.h>
  43. #include <getopt.h>
  44.  
  45. void initgraphics(), mainloop(), drawworld();
  46.  
  47. extern int gd;
  48.  
  49. int main(int argc, char **argv)
  50. {
  51.     int ch;
  52.  
  53.     while ((ch = getopt(argc, argv, "fv")) != -1)
  54.     switch (ch) {
  55.         case 'f':        /* foreground */
  56.         foreground_flag = 1;
  57.         break;
  58.         case 'v':            /* view-only */
  59.         VIEW_ONLY = view_only = 1;
  60.         break;
  61.         case '?':
  62.         (void)fprintf(stderr, "usage: %s [-fv] [file]\n", argv[0]);
  63.         if (entries.exit) entries.exit();
  64.         exit(-1);
  65.     }
  66.     MAKE_BACKUPS = 1;                       /* OPTIONAL - backups */
  67.     if (foreground_flag) foreground();
  68.     initgraphics();
  69.     makepulldown();                         /* OPTIONAL - pulldown menu */
  70.     gd = (int)qgetfd();                  /* OPTIONAL - gizmos */
  71.     qdevice(WINQUIT);                        /* OPTIONAL - these calls catch
  72.     qdevice(WINSHUT);                         * kills from the window manager */
  73.     qdevice(KEYBD);         /* if you want the alt-key stuff */
  74.     qdevice(LEFTMOUSE);
  75.     qdevice(MOUSEX);                            /* for locate-highlight in */
  76.     qdevice(MOUSEY);                            /* the showcase libui. */
  77.     glcompat(GLC_MQUEUERATE, GLC_COMPATRATE);    /* ditto this line */
  78.     if (argc - optind >= 1)
  79.         doopen(argv[optind]);
  80.     else if (VIEW_ONLY) {
  81.     (void)fprintf(stderr, "You must name a file in view-only mode\n");
  82.     if (entries.exit) entries.exit();
  83.     exit(-1);
  84.     }
  85.     initsignal();                           /* OPTIONAL */
  86.     opengizmo(GIZMO_MASTER);                /* OPTIONAL */
  87.     makewintitle();
  88.     mainloop();
  89.     return 1;                                /* for lint's sake */
  90. }
  91.  
  92. /* Initialize the graphics for the application.  This generic application
  93.  * draws a red X across a black background.  If you use the generic pull-
  94.  * down menu, remember that your editing area is reduced by PULLDOWNHEIGHT
  95.  * in the y-direction.
  96.  */
  97.  
  98. void initgraphics()
  99. {
  100.     minsize(400, 200);
  101.     winopen(PROG_NAME);
  102.     getwindowinfo();
  103.     viewport(0, (short)(xsize - 1), 0, (short)(ysize - PULLDOWNHEIGHT));
  104.     ortho2(-1.0, 1.0, -1.0, 1.0);
  105. }
  106.  
  107. void handleredraw()
  108. {
  109.     getwindowinfo();
  110.     viewport(0, (short)(xsize - 1), 0, (short)(ysize - PULLDOWNHEIGHT));
  111.     ortho2(-1.0, 1.0, -1.0, 1.0);
  112.     remakepulldown();                /* required if you're using the pulldown */
  113.     drawworld(); drawworld();
  114. }
  115.  
  116. /* Your code should handle most of the events below except the last
  117.  * one.  It is a dummy command from the generic gizmo that touches
  118.  * the file.
  119.  */
  120.  
  121. void dispatchcmd(long cmd)
  122. {
  123.     switch (cmd) {
  124.     case NEWCMD:
  125.         handlenewcmd();
  126.         break;
  127.     case OPENCMD:
  128.         handleopencmd();
  129.         break;
  130.     case SAVECMD:
  131.         handlesavecmd();
  132.         break;
  133.     case SAVEASCMD:
  134.         handlesaveascmd();
  135.         break;
  136.     case INSERTCMD:
  137.         handleinsertcmd();
  138.         break;
  139.     case PRINTCMD:
  140.         handleprintcmd();
  141.         break;
  142.     case QUITCMD:
  143.         handlequitcmd();
  144.         break;
  145.     case HELPCMD:
  146.         handlehelpcmd();
  147.         break;
  148.     case TOUCHME:
  149.         if (view_only) {
  150.         (void)message("View only file", 0, 0, 0);
  151.         } else {
  152.         dirtyfile_flag = 1;
  153.         makewintitle();
  154.         }
  155.         break;
  156.     }
  157. }
  158.  
  159. void drawworld()
  160. {
  161.     color(BLACK); clear();
  162.     color(RED); move2(-1.0, -1.0); draw2(1.0, 1.0);
  163.     move2(1.0, -1.0); draw2(-1.0, 1.0);
  164.     drawpulldown();
  165.     swapbuffers();
  166. }
  167.  
  168. /* mainloop() is the main loop of the program that collects commands
  169.  * and dispatches to the correct routines.  In this application, commands
  170.  * come from the pull-down menu only.
  171.  */
  172.  
  173. void mainloop()
  174. {
  175.     long cmd = 0;
  176.     long val, dev;
  177.     static long ckpcount = 0;
  178.     
  179.     drawworld();
  180.     while (1) {
  181.     switch (dev = cookedqread(&val)) {
  182.         case LEFTMOUSE:
  183.             if (val == 0) break;
  184.             cmd = pulldownevent(getvaluator(MOUSEX)-xorg, getvaluator(MOUSEY)-yorg);
  185.         /* cmd == -1 if no event, or if the leftmouse is
  186.          * outside the pulldown menu.
  187.          */
  188.          break; 
  189.         case WINQUIT:
  190.         case WINSHUT:
  191.             handlequitcmd();
  192.         break;
  193.         case REDRAW:
  194.             handleredraw();
  195.         break;
  196.         case MOUSEX:
  197.         case MOUSEY:
  198.             /* possibly replace this with locate-highlight code */
  199.             cmd = 0;
  200.         break;
  201.         case 0:
  202.             ckpcount--;
  203.         break;
  204.         default:
  205.             cmd = dev;
  206.         break;
  207.     }
  208.     if (cmd) {
  209.         dispatchcmd(cmd);
  210.         drawworld();
  211.         cmd = 0;
  212.         if (ckpcount++ == 8) {    /* Write a checkpoint file every 8 events */
  213.         writecheckpointfile();
  214.         ckpcount = 0;
  215.         }
  216.     }
  217.     }
  218. }
  219.  
  220. long readfile(char *);
  221. long writefile(char *);
  222. long insertfile(char *);
  223. long errorwritefile(char *);
  224. long printfile(char *);
  225. void makecheckpointname(char *);
  226. void makebackupname(char *);
  227. void clearbuffer();
  228. void generichelp();
  229. void quit();
  230. void appexit();    /* for errors */
  231.  
  232. /* You've got to fill in slots in this structure with the application-
  233.  * specific routine names.  If you don't have a routine, just put zero
  234.  * in the slot.  You had better have a readfile and writefile, at least.
  235.  * 
  236.  * readfile() takes the file name, reads the contents into your
  237.  * internal data structures, and returns 1 if successful; 0 otherwise.
  238.  * 
  239.  * writefile() writes a file with the given name, and returns 1 if
  240.  * successful; zero otherwise.
  241.  * 
  242.  * errorwritefile() writes the file after a signal is caught.  This
  243.  * routine can be the same as writefile(), but you may want something
  244.  * more robust or different, since when this routine gets hit, there
  245.  * was probably a bad error.
  246.  * 
  247.  * insertfile() adds the contents of the named file to the application's
  248.  * internal data structures.
  249.  * 
  250.  * printfile() prints.  The application creates a print file with the
  251.  * given name, and then issues an 'lp' command, or whatever's appropriate.
  252.  * 
  253.  * makecheckpointname() creates the name of the checkpoint files.  By
  254.  * default, the file named 'foo' has a checkpoint name of 'foo.ckp'.  You
  255.  * may want to strip extensions, etc.
  256.  * 
  257.  * makebackupname() same as above, but for backup files.  The default
  258.  * backup name for 'foo' is 'foo.bak'.
  259.  * 
  260.  * clearbuffer() clears the application's data structures after something
  261.  * like a New command.
  262.  * 
  263.  * help() presents help (or indicates that none is available).
  264.  * 
  265.  * quit() cleans up stuff before quitting.
  266.  */
  267.  
  268. struct application_entries entries = {
  269.     readfile,
  270.     writefile, 
  271.     errorwritefile, 
  272.     insertfile, 
  273.     printfile, 
  274.     makecheckpointname, 
  275.     makebackupname, 
  276.     clearbuffer, 
  277.     generichelp, 
  278.     quit,
  279.     appexit
  280. };
  281.  
  282. void appexit()
  283. {
  284.     (void)fprintf(stderr, "Exit\n");
  285.     exit(0);
  286. }
  287.  
  288. void generichelp()
  289. {
  290.     (void)fprintf(stderr, "No help available\n");
  291. }
  292.  
  293. void clearbuffer()
  294. {
  295.     (void)fprintf(stderr, "Cleared the buffer\n");
  296. }
  297.  
  298. long readfile(char *filename)
  299. {
  300.     (void)fprintf(stderr, "Read file: %s\n", filename);
  301.     return 1;
  302. }
  303.  
  304. void quit()
  305. {
  306.     murdergizmos();     /* OPTIONAL */
  307. }
  308.  
  309. /* 
  310.  * writefile() generates an "error" every fifth time to test
  311.  * the code that handles write errors.
  312.  */
  313.  
  314. long writefile(char *filename)
  315. {
  316.     static long i = 0;
  317.     if (i == 4) {
  318.     (void)fprintf(stderr, "Write error\n");
  319.     i = 0;
  320.     return 0;
  321.     }
  322.     i++;
  323.     (void)fprintf(stderr, "Wrote file: %s\n", filename);
  324.     return 1;
  325. }
  326.  
  327. long errorwritefile(char *filename)
  328. {
  329.     (void)fprintf(stderr, "Caught signal: ");
  330.     return writefile(filename);
  331. }
  332.  
  333. long insertfile(char *filename)
  334. {
  335.     (void)fprintf(stderr, "Inserted file: %s\n", filename);
  336.     dirtyfile_flag = 1;
  337.     return 1;
  338. }
  339.  
  340. long printfile(char *filename)
  341. {
  342.     (void)fprintf(stderr, "Printed file: %s\n", filename);
  343.     return 1;
  344. }
  345.  
  346. void makecheckpointname(char *filename)
  347. {
  348.     (void)sprintf(filename, "%s.CKP", currentfilename);
  349. }
  350.  
  351. void makebackupname(char *filename)
  352. {
  353.     (void)sprintf(filename, "%s.BAK", currentfilename);
  354. }
  355.  
  356.