home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgas / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-22  |  7.7 KB  |  227 lines

  1. /*
  2.  * Copyright 1989 Massachusetts Institute of Technology
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided that
  6.  * the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of M.I.T. not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  M.I.T. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  16.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  18.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  19.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20.  *
  21.  * main.c
  22.  *   xgas: Copyright 1990 Larry Medwin: @(#)xgas.c    1.9 2/9/90
  23.  *   Larry Medwin -- Dec 18, 1989
  24.  *   Dave Sternlicht -- Dec 18 1990, ported from Xw to Xaw widget set.
  25.  *   Dave Sternlicht -- Dec 20 1990, creation of the Gas widget.
  26.  *   Larry Medwin -- April 5, 1991, added help stuff.
  27.  */
  28.  
  29. #include "xgas.h"
  30. #include "xgas.icon"
  31. #include <X11/Shell.h>
  32.  
  33. static String FallbackResources[] = {
  34. "*lab.height: 300",
  35. "*lab.width: 300",
  36. "*Scrollbar.height: 300",
  37. "*help.label: Missing app-defaults!",
  38. NULL
  39. };
  40.  
  41. #define Offset(field) XtOffsetOf(LabData, field)
  42. static XtResource resources[] = {
  43.   { "timestepSize", "TimestepSize", XtRFloat, sizeof(float),
  44.       Offset(timestepSize), XtRString, "3.0" },
  45.   { "delay", "Delay", XtRInt, sizeof(int),
  46.       Offset(delay), XtRImmediate, (XtPointer) 50 },
  47.   { "randomBounce", "RandomBounce", XtRFloat, sizeof(float),
  48.       Offset(randomBounce), XtRString, "0.1" },
  49.   { "equilibrium", "Equilibrium", XtRFloat, sizeof(float),
  50.       Offset(equilibrium), XtRString, "0.5" },
  51.   { "maxMolecules", "MaxMolecules", XtRInt, sizeof(int),
  52.       Offset(maxMolecules), XtRImmediate, (XtPointer) 100 },
  53.   { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  54.       Offset(foreground), XtRString, XtDefaultForeground },
  55.   { XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel),
  56.       Offset(background), XtRString, XtDefaultBackground },
  57. };
  58. #undef Offset
  59.  
  60. /*
  61.  *  Global array describing wall and corner geometry.
  62.  *    wallReflect, toRotate, fromRotate
  63.  *
  64.  * Rotation Matrix is:
  65.  *    [ cos theta    - sin theta ]
  66.  *    [ sin theta    cos theta   ]
  67.  */
  68. WallType WallParam[] =    {
  69.   /* Walls: */
  70.   {     {1,-1},  {{0,1},{-1,0}},  {{0,-1},{1,0}}},    /* TOP */
  71.   {     {-1,1},  {{-1,0},{0,-1}},  {{-1,0},{0,-1}}},    /* RIGHT */
  72.   {     {1,-1},  {{0,-1},{1,0}},  {{0,1},{-1,0}}},    /* BOTTOM */
  73.   {     {-1,1},     {{1,0},{0,1}},  {{1,0},{0,1}}},    /* LEFT */
  74.   /* Corners: */
  75.   {     {-1,-1}, {{1,0},{0,1}},  {{1,0},{0,1}}},    /* NW */
  76.   {     {-1,-1}, {{0,-1},{1,0}},  {{0,1},{-1,0}}},    /* SW */
  77.   {     {-1,-1}, {{-1,0},{0,-1}},  {{-1,0},{0,-1}}},    /* SE */
  78.   {     {-1,-1}, {{0,1},{-1,0}},  {{0,-1},{1,0}}},    /* NE */
  79. };
  80.  
  81. main( argc, argv )
  82.      int argc;
  83.      char *argv[];
  84. {
  85.   XtAppContext app;
  86.   Widget toplevel;
  87.   Widget frame;
  88.   Widget run, pause, step, quit;    /* Push Buttons */
  89.   Widget lab;
  90.   Widget help;
  91.   LabData labData;
  92.   Arg wargs[3];
  93.   int i;
  94.   Pixmap icon;
  95.   
  96.   /* TOPLEVEL */
  97.   toplevel = XtAppInitialize(&app, "XGas", NULL, 0, &argc, argv,
  98.                  FallbackResources, NULL, (Cardinal)0);
  99.   
  100.   /* Get Resources */
  101.   XtGetApplicationResources( toplevel, (XtPointer) &labData, resources,
  102.                 XtNumber( resources), (ArgList)NULL, 0);
  103.   
  104.   /* Allocate dynamic arrays, now that we have maxMolecules */
  105.   labData.molecules
  106.     = (Molecule*) malloc( (unsigned)labData.maxMolecules
  107.              *sizeof(Molecule));
  108.   labData.allPos
  109.     = (XRectangle*) malloc((unsigned)labData.maxMolecules
  110.                *2*sizeof(XRectangle));
  111.   
  112.   /* FRAMEWORK */
  113.   frame = XtCreateManagedWidget("frame", formWidgetClass,
  114.                 toplevel, NULL, 0);
  115.   
  116.   /* QUIT BUTTON */
  117.   quit = XtVaCreateManagedWidget("quit", commandWidgetClass, frame,
  118.                  NULL);
  119.   XtAddCallback(quit, XtNcallback, quit_callback, (XtPointer)NULL);
  120.   
  121.   /* RUN BUTTON */
  122.   run = XtVaCreateManagedWidget("run", toggleWidgetClass, frame, 
  123.                     XtNfromHoriz, (XtPointer)quit,
  124.                    XtNhorizDistance, 50,
  125.                 NULL);
  126.   XtAddCallback(run, XtNcallback, run_callback, (XtPointer)&labData);
  127.   
  128.   /* PAUSE BUTTON */
  129.   pause = XtVaCreateManagedWidget("pause", toggleWidgetClass, frame, 
  130.                   XtNfromHoriz, (XtPointer)run,
  131.                   XtNradioGroup, (XtPointer)run,
  132.                   NULL);
  133.   XtAddCallback(pause, XtNcallback, pause_callback, (XtPointer)&labData);
  134.   
  135.   /* STEP BUTTON */
  136.   step = XtVaCreateManagedWidget("step", commandWidgetClass, frame,
  137.                  XtNfromHoriz, (XtPointer)pause,
  138.                  NULL);
  139.   XtAddCallback(step, XtNcallback, oneTimestep, (XtPointer)&labData);
  140.   
  141.   /* HELP BUTTON */
  142.   help = XtVaCreateManagedWidget("help", commandWidgetClass, frame,
  143.                 XtNfromHoriz, (XtPointer)step,
  144.                    XtNhorizDistance, 50,
  145.                    NULL);
  146.   createHelpWidgets( help );
  147.   
  148.   /* TEMP CONTROL and TEMP DISPLAY */
  149.   labData.chamber[0].control = 
  150.     XtVaCreateManagedWidget("tempControl0", scrollbarWidgetClass, frame, 
  151.                 XtNfromVert, (XtPointer)help,
  152.                 NULL);
  153.  
  154.   /* LAB */
  155.   lab = XtVaCreateManagedWidget("lab", gasWidgetClass, frame,
  156.                XtNfromHoriz, (XtPointer)labData.chamber[0].control,
  157.                XtNfromVert, (XtPointer)help,
  158.                NULL);
  159.   XtAddCallback(lab, XtNresize, labResize, (XtPointer)&labData);
  160.   
  161.   labData.chamber[1].control = 
  162.     XtVaCreateManagedWidget("tempControl1", scrollbarWidgetClass, frame, 
  163.                 XtNfromHoriz, (XtPointer)lab,
  164.                 XtNfromVert, (XtPointer)help,
  165.                 NULL);
  166.  
  167.   XtAddCallback( labData.chamber[0].control, XtNjumpProc, 
  168.         changeTemp, (XtPointer)&labData.chamber[0]);
  169.   XtAddCallback( labData.chamber[1].control, XtNjumpProc, 
  170.         changeTemp, (XtPointer)&labData.chamber[1]);
  171.  
  172.  
  173.   labData.chamber[0].display = 
  174.     XtVaCreateManagedWidget("tempDisplay0", labelWidgetClass, frame,
  175.                 XtNfromVert, (XtPointer)labData.chamber[0].control,
  176.                 XtNlabel, (XtPointer)" 300.0K ",
  177.                 NULL);
  178.  
  179.   /* CLOCK DISPLAY */
  180.   labData.clock = XtVaCreateManagedWidget("clock", labelWidgetClass, frame,
  181.                XtNfromHoriz, (XtPointer)labData.chamber[0].display,
  182.                XtNfromVert, (XtPointer)lab,
  183.                XtNlabel, (XtPointer)" 0.000 msec ",
  184.                NULL);
  185.   
  186.   labData.chamber[1].display = 
  187.     XtVaCreateManagedWidget("tempDisplay1", labelWidgetClass, frame, 
  188.                 XtNfromHoriz, (XtPointer)labData.clock,
  189.                 XtNfromVert, (XtPointer)labData.chamber[1].control,
  190.                 XtNlabel, " 300.0 K ",
  191.                 NULL);
  192.  
  193.   /* Start things up. */
  194.   
  195.   /*   Need to create GC's before adding callbacks */
  196.   labInit( lab, &labData);
  197.   XtAddEventHandler(lab, ExposureMask, False, labExpose, (XtPointer)&labData);
  198.  
  199.  
  200.   /* resize is handled through the resize callback in the gas widget. */
  201.   XtCallCallbacks(lab, XtNresize, (XtPointer)&labData);
  202.  
  203.   XtAddEventHandler( lab, ButtonPressMask, FALSE,
  204.             addMolecules, (XtPointer)&labData);
  205.   
  206.   XtRealizeWidget( toplevel);
  207.  
  208.   /* initialize the scrollbar positions */
  209.   for (i=0; i<2; i++)
  210.     XawScrollbarSetThumb(labData.chamber[i].control, 
  211.              (float)INITTEMP / (float)MAXTEMP, -1.0);  
  212.  
  213.   /* Tell wm about icon */
  214.   icon = XCreateBitmapFromData( XtDisplay(frame),
  215.            XtWindow(frame), (char *)xgas_bits, xgas_width, xgas_height);
  216.   XtSetArg( wargs[0], XtNiconPixmap, icon);
  217.   XtSetValues( toplevel, wargs, 1);
  218.        
  219.   /* Figure out lab dimensions, create walls */
  220.  
  221.   /* Initialize temperature */
  222.   XtCallCallbacks( labData.chamber[0].control, XtNscrollProc, (XtPointer)300);
  223.   XtCallCallbacks( labData.chamber[1].control, XtNscrollProc, (XtPointer)300);
  224.   
  225.   XtAppMainLoop(app);
  226. }
  227.