home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-09  |  16.1 KB  |  495 lines

  1. /* $XConsortium: main.c,v 1.21 91/06/09 12:36:51 rws Exp $
  2.  *
  3.  * Copyright 1991 Massachusetts Institute of Technology
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of M.I.T. not be used in advertising or
  10.  * publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.  M.I.T. makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  */
  23.  
  24. /* xgc
  25. **
  26. ** main.c
  27. **
  28. ** Contains the bare minimum necessary to oversee the whole operation.
  29. */
  30.  
  31. #include <X11/Intrinsic.h>
  32. #include <X11/StringDefs.h>
  33. #include <X11/Xaw/Form.h>
  34. #include <X11/Xaw/Command.h>
  35. #include <X11/Xaw/AsciiText.h>
  36. #include <X11/Shell.h>
  37. #include <stdio.h>
  38.  
  39. #include "xgc.h"
  40. #include "tile"
  41. #include "main.h"
  42.  
  43. static void fill_up_commandform();
  44. extern void run_test();
  45. static void quit();
  46. static void quitAction();
  47. static void clear_test_window();
  48. static void clear_result_window();
  49. extern void start_playback();
  50. extern void read_from_keyboard();
  51. extern void toggle_recordbutton();
  52. static void set_foreground_and_background();
  53. extern ChoiceDesc *create_choice();
  54. extern void choose_defaults();
  55. extern void line_up_labels();
  56. extern Widget create_text_choice();
  57. extern void create_planemask_choice();
  58. extern void create_dashlist_choice();
  59. extern void create_testfrac_choice();
  60. extern void GC_change_foreground();
  61. extern void GC_change_background();
  62. extern void GC_change_font();
  63. extern void close_file_if_recording();
  64.  
  65. #ifdef notdef
  66. int fildes[2];            /* for pipe */
  67. FILE *outend;
  68. #endif
  69.  
  70. XStuff X;            /* GC stuff plus some global variables */
  71. Boolean recording = FALSE;    /* Whether we're recording into a file */
  72. XtAppContext appcontext;    /* To make Xt happy */
  73. static Atom wm_delete_window;
  74. static XtActionsRec actions[] = {
  75.     {"quit",    quitAction}
  76. };
  77.  
  78. static Widget bigdaddy;        /* the top level widget */
  79.        Widget topform;        /* form surrounding the whole thing */
  80.        Widget GCform;        /* form in which you choose the GC */
  81. static Widget Testform;        /* form in which you choose the test */
  82.        Widget testchoiceform;   /* form inside that */
  83.   ChoiceDesc *testchoicedesc;    /* record of what widgets are in the
  84.                    test choice form */
  85. static Widget commandform;    /* form with run, quit, clear, etc. */
  86.        Widget test;        /* where the test is run */
  87.        Widget result;           /* where the results are displayed */
  88. static Widget runbutton;    /* command for running */
  89. static Widget quitbutton;    /* command for quitting */
  90. static Widget clearbutton;    /* command for clearing the test window */
  91.        Widget recordbutton;    /* start/stop recording */
  92. static Widget playbackbutton;    /* playback from file */
  93. static Widget keyinputbutton;    /* start reading from keyboard */
  94. static Widget GCchoices[NUMCHOICES]; /* all the forms that contain stuff
  95.                         for changing GC's*/
  96.   ChoiceDesc *GCdescs[NUMCHOICES]; /* record of the widgets inside
  97.                       the choice widgets */
  98.        Widget planemaskchoice;    /* form for choosing the plane mask */
  99.        Widget dashlistchoice;    /* form for choosing the dash list */
  100. static Widget linewidthchoice;    /* form for choosing line width */
  101.        Widget linewidthtext;    /* text widget within that */
  102. static Widget fontchoice;    /* form for choosing the font */
  103.        Widget fonttext;        /* text widget within that */
  104. static Widget foregroundchoice;    /* form for choosing foreground */
  105.        Widget foregroundtext;    /* text widget within that */
  106. static Widget backgroundchoice;    /* form for choosing background */
  107.        Widget backgroundtext;    /* text widget within that */
  108. static Widget percentchoice;    /* form for choosing percentage of test */
  109.  
  110. /* main(argc.argv)
  111. ** ---------------
  112. ** Initializes the toolkit, initializes data, puts up the widgets,
  113. ** starts the event loop.
  114. */
  115.  
  116. void
  117. main(argc,argv)
  118.      int argc;
  119.      char **argv;
  120. {
  121.   static Arg shellargs[] = {
  122.     {XtNinput,           (XtArgVal) True}
  123.   };
  124.  
  125.   static Arg testformargs[] = {    
  126.     {XtNfromVert,     (XtArgVal) NULL} /* put it under GCform */
  127.   };
  128.  
  129.   static Arg commandformargs[] = {
  130.     {XtNfromVert,    (XtArgVal) NULL}, /* put it under GCform */
  131.     {XtNfromHoriz,   (XtArgVal) NULL}  /* and to the right of Testform */
  132.   };
  133.  
  134.   static Arg testargs[] = {
  135.     {XtNheight,     (XtArgVal) 400},
  136.     {XtNwidth,      (XtArgVal) 400},
  137.     {XtNfromHoriz,  (XtArgVal) NULL} /* put it to the right of GCform */
  138.   };
  139.  
  140.   static Arg resultargs[] = {
  141.     {XtNheight,     (XtArgVal) 50},
  142.     {XtNwidth,      (XtArgVal) 400},
  143.     {XtNfromHoriz,  (XtArgVal) NULL}, /* put it to the right of GCform */
  144.     {XtNfromVert,   (XtArgVal) NULL} /* and under test */
  145.   };
  146.  
  147.   static Arg gcchoiceargs[] = {
  148.     {XtNfromVert,    (XtArgVal) NULL}, /* put it under the one above it */
  149.     {XtNfromHoriz,   (XtArgVal) NULL}, /* and next to that one */
  150.     {XtNborderWidth, (XtArgVal) 0}     /* no ugly borders */
  151.   };
  152.  
  153.   static Arg testchoiceargs[] = {
  154.     {XtNborderWidth, (XtArgVal) 0}
  155.   };
  156.  
  157.   int i;            /* counter */
  158.  
  159.   /* Open the pipe */
  160.  
  161. #ifdef notdef
  162.   pipe(fildes);
  163.   outend = fdopen(fildes[0],"r");
  164. #endif
  165.  
  166.   /* Initialize toolkit stuff */
  167.  
  168.   bigdaddy = XtAppInitialize(&appcontext, "Xgc", (XrmOptionDescList) NULL,
  169.                  (Cardinal) 0, &argc, argv, (String *) NULL,
  170.                  shellargs, XtNumber(shellargs));
  171.   X.dpy = XtDisplay(bigdaddy);
  172.   XtAppAddActions(appcontext, actions, XtNumber(actions));
  173.   XtOverrideTranslations
  174.       (bigdaddy, XtParseTranslationTable("<Message>WM_PROTOCOLS: quit()"));
  175.  
  176.   /* Initialize GC stuff */
  177.  
  178.   X.scr = DefaultScreenOfDisplay(X.dpy);
  179.   X.gc = XCreateGC(X.dpy,RootWindowOfScreen(X.scr),0,(XGCValues *) NULL);
  180.   X.miscgc = XCreateGC(X.dpy,RootWindowOfScreen(X.scr),0,(XGCValues *) NULL);
  181.  
  182.   /* Find out what the foreground & background are, and update the GC
  183.   ** accordingly */
  184.  
  185.   set_foreground_and_background();
  186.  
  187.   topform = XtCreateManagedWidget("topform",formWidgetClass,bigdaddy,
  188.                   NULL,0);
  189.  
  190.   GCform = XtCreateManagedWidget("GCform",formWidgetClass,topform,
  191.                 NULL,0);
  192.  
  193.   /* create all the GCchoices forms */
  194.  
  195.   for (i=0;i<NUMCHOICES;++i) {
  196.     if (i==0)            /* on top */
  197.       gcchoiceargs[0].value = (XtArgVal) NULL;
  198.     else            /* under the last one */
  199.       gcchoiceargs[0].value = (XtArgVal) GCchoices[i-1];
  200.  
  201.     GCchoices[i] = XtCreateManagedWidget(Everything[i]->choice.text,
  202.                      formWidgetClass,GCform,
  203.                      gcchoiceargs,XtNumber(gcchoiceargs));
  204.  
  205.     /* now fill up that form */
  206.     GCdescs[i] = create_choice(GCchoices[i],Everything[i]);
  207.   }
  208.  
  209.   /* put the planemask choice under the bottom GC choice */
  210.   gcchoiceargs[0].value = (XtArgVal) GCchoices[NUMCHOICES-1];
  211.   planemaskchoice = XtCreateManagedWidget("planemask",formWidgetClass,GCform,
  212.                   gcchoiceargs,XtNumber(gcchoiceargs));
  213.   /* fill it up */
  214.   create_planemask_choice(planemaskchoice);
  215.  
  216.   /* put the dashlist choice under the planemask choice */
  217.   gcchoiceargs[0].value = (XtArgVal) planemaskchoice;
  218.   dashlistchoice = XtCreateManagedWidget("dashlist",formWidgetClass,GCform,
  219.                   gcchoiceargs,XtNumber(gcchoiceargs));
  220.   /* fill it up */
  221.   create_dashlist_choice(dashlistchoice);
  222.  
  223.   /* put the linewidth choice under the dashlist choice */
  224.   gcchoiceargs[0].value = (XtArgVal) dashlistchoice;
  225.   linewidthchoice = XtCreateManagedWidget("linewidth",formWidgetClass,GCform,
  226.                   gcchoiceargs,XtNumber(gcchoiceargs));
  227.   /* fill it up */
  228.   linewidthtext = create_text_choice(linewidthchoice,TLineWidth,2,30);
  229.  
  230.   /* put the font choice under the linewidth choice */
  231.   gcchoiceargs[0].value = (XtArgVal) linewidthchoice;
  232.   fontchoice = XtCreateManagedWidget("font",formWidgetClass,GCform,
  233.                      gcchoiceargs,XtNumber(gcchoiceargs));
  234.   /* fill it up */
  235.   fonttext = create_text_choice(fontchoice,TFont,80,300);
  236.  
  237.   gcchoiceargs[0].value = (XtArgVal) fontchoice;
  238.   foregroundchoice = XtCreateManagedWidget("foreground",formWidgetClass,GCform,
  239.                    gcchoiceargs,XtNumber(gcchoiceargs));
  240.   foregroundtext = create_text_choice(foregroundchoice,TForeground,4,50);
  241.  
  242.   gcchoiceargs[1].value = (XtArgVal) foregroundchoice;
  243.   backgroundchoice = XtCreateManagedWidget("background",formWidgetClass,GCform,
  244.                    gcchoiceargs,XtNumber(gcchoiceargs));
  245.   backgroundtext = create_text_choice(backgroundchoice,TBackground,4,50);
  246.   
  247.   gcchoiceargs[1].value = (XtArgVal) NULL;
  248.   gcchoiceargs[0].value = (XtArgVal) foregroundchoice;
  249.   percentchoice = XtCreateManagedWidget("testpercent",formWidgetClass,GCform,
  250.                  gcchoiceargs,XtNumber(gcchoiceargs));
  251.   X.percent = 1.0;
  252.   create_testfrac_choice(percentchoice);
  253.  
  254.   /* make all the labels inside the choices line up nicely */
  255.   line_up_labels(GCdescs,(int) XtNumber(GCdescs));
  256.  
  257.   /* put the test form under the GC form */
  258.   testformargs[0].value = (XtArgVal) GCform;
  259.   Testform = XtCreateManagedWidget("Testform",formWidgetClass,topform,
  260.                    testformargs,XtNumber(testformargs));
  261.   
  262.   testchoiceform = XtCreateManagedWidget("testchoiceform",formWidgetClass,
  263.                  Testform,testchoiceargs,XtNumber(testchoiceargs));
  264.   testchoicedesc = create_choice(testchoiceform,Everything[CTest]);
  265.  
  266.   commandformargs[0].value = (XtArgVal) GCform;
  267.   commandformargs[1].value = (XtArgVal) Testform;
  268.   commandform = XtCreateManagedWidget("commandform",formWidgetClass,topform,
  269.                   commandformargs,XtNumber(commandformargs));
  270.  
  271.   /* Put the appropriate command buttons in the command form */
  272.  
  273.   fill_up_commandform(commandform);
  274.  
  275.   testargs[2].value = (XtArgVal) GCform;    /* to the right of */
  276.   test = XtCreateManagedWidget("test",widgetClass,topform,
  277.                    testargs,XtNumber(testargs));
  278.  
  279.   resultargs[2].value = (XtArgVal) GCform; /* to the right of */
  280.   resultargs[3].value = (XtArgVal) test; /* under */
  281.   result = XtCreateManagedWidget("result",asciiTextWidgetClass,topform,
  282.                  resultargs,XtNumber(resultargs));
  283.  
  284.   /* Now realize all the widgets */
  285.  
  286.   XtRealizeWidget(bigdaddy);
  287.  
  288.   /* Now do things we couldn't do until we had a window available */
  289.  
  290.   X.win = XtWindow(test);
  291.   X.tile = XCreatePixmap(X.dpy,X.win,tile_width,tile_height,
  292.              DefaultDepthOfScreen(X.scr));
  293.  
  294.   X.tile = XCreatePixmapFromBitmapData(X.dpy,X.win,
  295.                        (char *)tile_bits,tile_width,
  296.                        tile_height,Black,White,
  297.                        DefaultDepthOfScreen(X.scr));
  298.   X.stipple = XCreateBitmapFromData(X.dpy,X.win,(char *)tile_bits,tile_width,
  299.                     tile_height);
  300.  
  301.   XSetStipple(X.dpy,X.gc,X.stipple);
  302.   XSetStipple(X.dpy,X.miscgc,X.stipple);
  303.  
  304.   GC_change_foreground(X.foreground,TRUE);
  305.   GC_change_background(X.background,TRUE);
  306.  
  307.   wm_delete_window = XInternAtom(X.dpy, "WM_DELETE_WINDOW", False);
  308.   (void) XSetWMProtocols(X.dpy, XtWindow(bigdaddy), &wm_delete_window, 1);
  309.  
  310.   /* Act like the user picked the first choice in each group */
  311.  
  312.   choose_defaults(GCdescs,(int)XtNumber(GCdescs));
  313.   choose_defaults(&testchoicedesc,1);
  314.   
  315.   /* Loop forever, dealing with events */
  316.  
  317.   XtAppMainLoop(appcontext);
  318. }
  319.  
  320. /* fill_up_commandform(w)
  321. ** ----------------------
  322. ** Put the appropriate command buttons in the command form (w).
  323. */
  324.  
  325. static void
  326. fill_up_commandform(w)
  327.      Widget w;
  328. {
  329.   static XtCallbackRec runcallbacklist[] = {
  330.     {(XtCallbackProc) run_test,  NULL},
  331.     {NULL,                       NULL}
  332.   };
  333.  
  334.   static XtCallbackRec quitcallbacklist[] = {
  335.     {(XtCallbackProc) quit,      NULL},
  336.     {NULL,                       NULL}
  337.   };
  338.  
  339.   static XtCallbackRec clearcallbacklist[] = {
  340.     {(XtCallbackProc) clear_test_window,    NULL},
  341.     {(XtCallbackProc) clear_result_window,  NULL},
  342.     {NULL,                                  NULL}
  343.   };
  344.  
  345.   static XtCallbackRec playbackcallbacklist[] = {
  346.     {(XtCallbackProc) start_playback,       NULL},
  347.     {NULL,                                  NULL}
  348.   };
  349.  
  350.   static XtCallbackRec keyinputcallbacklist[] = {
  351.     {(XtCallbackProc) read_from_keyboard,   NULL},
  352.     {NULL,                                  NULL}
  353.   };
  354.  
  355.   static XtCallbackRec recordcallbacklist[] = {
  356.     {(XtCallbackProc) toggle_recordbutton,  NULL},
  357.     {NULL,                                  NULL}
  358.   };
  359.  
  360.   static Arg runargs[] = {
  361.     {XtNcallback,    (XtArgVal) NULL}
  362.   };
  363.  
  364.   static Arg clearargs[] = {
  365.     {XtNcallback,    (XtArgVal) NULL},
  366.     {XtNfromVert,    (XtArgVal) NULL}, /* put it under runbutton */
  367.     {XtNvertDistance,(XtArgVal) 10}
  368.   };
  369.  
  370.   static Arg recordargs[] = {
  371.     {XtNcallback,    (XtArgVal) NULL},
  372.     {XtNfromVert,    (XtArgVal) NULL}, /* put it under clearbutton */
  373.     {XtNvertDistance,(XtArgVal) 10},
  374.     {XtNresizable,   (XtArgVal) True} /* so we can change the name */
  375.   };
  376.  
  377.   static Arg playbackargs[] = {
  378.     {XtNcallback,    (XtArgVal) NULL},
  379.     {XtNfromVert,    (XtArgVal) NULL} /* put it under recordbutton */
  380.   };
  381.  
  382.   static Arg keyinputargs[] = {
  383.     {XtNcallback,     (XtArgVal) NULL},
  384.     {XtNfromVert,    (XtArgVal) NULL} /* put it under playbackbutton */
  385.   };
  386.  
  387.   static Arg quitargs[] = {
  388.     {XtNcallback,    (XtArgVal) NULL},
  389.     {XtNfromVert,    (XtArgVal) NULL}, /* put it under keyinputbutton */
  390.     {XtNvertDistance,(XtArgVal) 10}
  391.   };
  392.  
  393.   runargs[0].value = (XtArgVal) runcallbacklist;
  394.   runbutton = XtCreateManagedWidget("Run",commandWidgetClass,
  395.                   w,runargs,XtNumber(runargs));
  396.  
  397.   clearargs[0].value = (XtArgVal) clearcallbacklist;
  398.   clearargs[1].value = (XtArgVal) runbutton; /* under */
  399.   clearbutton = XtCreateManagedWidget("Clear window",commandWidgetClass,
  400.                        w,clearargs,XtNumber(clearargs));
  401.  
  402.   recordargs[0].value = (XtArgVal) recordcallbacklist;
  403.   recordargs[1].value = (XtArgVal) clearbutton;    /* under */
  404.   recordbutton = XtCreateManagedWidget("Record",commandWidgetClass,
  405.                   w,recordargs,XtNumber(recordargs));
  406.  
  407.   playbackargs[0].value = (XtArgVal) playbackcallbacklist;
  408.   playbackargs[1].value = (XtArgVal) recordbutton; /* under */
  409.   playbackbutton = XtCreateManagedWidget("Playback",commandWidgetClass,
  410.                   w,playbackargs,XtNumber(playbackargs));
  411.  
  412.   keyinputargs[0].value = (XtArgVal) keyinputcallbacklist;
  413.   keyinputargs[1].value = (XtArgVal) playbackbutton;
  414.   keyinputbutton = XtCreateManagedWidget("Read Input",commandWidgetClass,
  415.                   w,keyinputargs,XtNumber(keyinputargs));
  416.  
  417.   quitargs[0].value = (XtArgVal) quitcallbacklist;
  418.   quitargs[1].value = (XtArgVal) keyinputbutton; /* under */
  419.   quitbutton = XtCreateManagedWidget("Quit",commandWidgetClass,
  420.                      w,quitargs,XtNumber(quitargs));
  421.     
  422. }    
  423. /* quit()
  424. ** ------
  425. ** Leave the program nicely.
  426. */
  427.  
  428. static void
  429. quit()
  430. {
  431.   close_file_if_recording();
  432.   exit(0);
  433. }
  434.  
  435. static void quitAction(w, e, p, n)
  436.     Widget w;
  437.     XEvent *e;
  438.     String *p;
  439.     Cardinal *n;
  440. {
  441.     if (e->type == ClientMessage && e->xclient.data.l[0] != wm_delete_window)
  442.     XBell(XtDisplay(w), 0);
  443.     else 
  444.     quit();
  445. }
  446.  
  447. /* clear_test_window()
  448. ** -------------------
  449. ** Clear the test window.
  450. */
  451.  
  452. static void
  453. clear_test_window()
  454. {
  455.   XClearWindow(X.dpy,XtWindow(test));
  456. }
  457.  
  458. /* clear_result_window()
  459. ** ---------------------
  460. ** Clear the result window.
  461. */
  462.  
  463. static void
  464. clear_result_window()
  465. {
  466.   XClearWindow(X.dpy,XtWindow(result));
  467. }
  468.  
  469. /* set_foreground_and_background()
  470. ** -------------------------------
  471. ** Finds the user-specified foreground and background by querying
  472. ** the resource manager, and sets state accordingly.  Also specifies
  473. ** the initial font for text tests.
  474. */
  475.  
  476. static void
  477. set_foreground_and_background()
  478. {
  479.   static XtResource resources[] = {
  480.     {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  481.        XtOffsetOf(XStuff, foreground), XtRString, XtDefaultForeground},
  482.     {XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel),
  483.        XtOffsetOf(XStuff, background), XtRString, XtDefaultBackground}
  484.   };
  485.   
  486.   XtGetApplicationResources(bigdaddy, (XtPointer) &X, resources,
  487.                 XtNumber(resources), NULL, (Cardinal) 0);
  488.  
  489.   X.gcv.foreground = X.foreground;
  490.   X.gcv.background = X.background;
  491.  
  492.   X.fontname = "6x10";
  493.   GC_change_font(X.fontname,FALSE);
  494. }
  495.