コード例 #1
0
ファイル: main.c プロジェクト: billpage/open-axiom
int
main(int argc, char **argv)
{

   XGCValues    values;

   XEvent       report;
   int          x0=0, y0=0, width=300, height=200, border=3;

   char         *fontname = "6x13";
   XFontStruct  *dsp_font;

   Window       menu;
   char         *str1 = "     Print out the PostScript file?    ",
                *str0 = "Generate a PostScript file (OUTPUT.ps)?";
   int          m_width, m_height, flag=yes;

   /* open display */
   if ((dsply = XOpenDisplay(NULL)) == NULL) {
     printf("Can't open display NULL\n");
     exit(-1);
   }

   scrn = DefaultScreen(dsply);

   /* access font */
   Gdraws_load_font(&dsp_font, fontname);

   values.background = WhitePixel(dsply, scrn);
   values.foreground = BlackPixel(dsply, scrn);

   gc = XCreateGC(dsply, RootWindow(dsply, scrn),
                  (GCForeground | GCBackground), &values);
   PSGlobalInit();      /* must initiate before using G/PS functions */
   PSCreateContext(gc, "gc", psNormalWidth, psButtCap,
                   psMiterJoin, psWhite, psBlack);
   XSetFont(dsply, gc, dsp_font->fid);
   gc1 = XCreateGC(dsply, RootWindow(dsply, scrn),
                   (GCForeground | GCBackground), &values);
   PSCreateContext(gc1, "gc1", psNormalWidth, psButtCap, psMiterJoin,
                   psWhite, psBlack);
   XSetFont(dsply, gc1, dsp_font->fid);

   if (!(viewport = (viewPoints *)malloc(sizeof(viewPoints)))) {
     fprintf(stderr,"Ran out of memory (malloc) trying to create a viewport.\n");
     exit(-1);
   }

   viewport->titleWindow = XCreateSimpleWindow(dsply, RootWindow(dsply,scrn),
                                               x0, y0, width+6,
                                               height+32+height/4, border,
                                               BlackPixel(dsply, scrn),
                                               WhitePixel(dsply, scrn));

   viewport->viewWindow = XCreateSimpleWindow(dsply, viewport->titleWindow,
                                              x0, y0+20, width, height, border,
                                              BlackPixel(dsply, scrn),
                                              WhitePixel(dsply, scrn));

   strcpy(viewport->title, "what is a test title?");

   m_width = width; m_height = height/4;
   menu = XCreateSimpleWindow(dsply, viewport->titleWindow, x0, y0+20+height+6,
                              m_width, m_height, border,
                              BlackPixel(dsply,scrn), WhitePixel(dsply,scrn));

   XSelectInput(dsply, viewport->viewWindow,
                KeyPressMask|ButtonPressMask|ExposureMask);
   XSelectInput(dsply, viewport->titleWindow, KeyPressMask|ExposureMask);
   XSelectInput(dsply, menu, KeyPressMask|ButtonPressMask|ExposureMask);

   XMapWindow(dsply, viewport->viewWindow);
   XMapWindow(dsply, viewport->titleWindow);
   XFlush(dsply);

   while (yes) {
      XNextEvent(dsply, &report);
      switch (report.type) {

         case Expose:
                if (report.xexpose.window==viewport->titleWindow) {
                  if (GDrawImageString(gc, viewport->titleWindow,
                                       20, 15, viewport->title,
                                       strlen(viewport->title),X) == psError)
                    printf("screen draw image string failed.\n");
                  if (Gdraws_data(X) == psError)
                    printf("screen Gdraws_data failed.\n");
                }
                if (report.xexpose.window==viewport->viewWindow) {
                  if (Gdraws_data(X) == psError)
                    printf("screen Gdraws_data failed.\n");
                }
                else if (report.xexpose.window==menu) {
                  if (flag)
                    Gdraws_draw_menu(menu, str0, m_width, m_height);
                  else
                    Gdraws_draw_menu(menu, str1, m_width, m_height);
                }
                break;

         case ButtonPress:
                if (report.xbutton.window==viewport->viewWindow) {
                  XMapWindow(dsply, menu);
                  XFlush(dsply);
                }
                else if (report.xbutton.window==menu && flag) {
                       XUnmapWindow(dsply, menu);
                       if (Gdraws_pressed_yes(m_width, m_height, report.xbutton.x,
                           report.xbutton.y)) {
                         flag=no;
                         XMapWindow(dsply, menu);
                         PSInit(viewport->viewWindow, viewport->titleWindow);
                         if (Gdraws_data(PS) != psError)
                           PSCreateFile(3,viewport->viewWindow,
                                        viewport->titleWindow,viewport->title);
                         else printf("PS Gdraws_data failed.\n");
                       }
                     }
                else if (report.xbutton.window==menu && !flag) {
                       XUnmapWindow(dsply, menu);
                       if (Gdraws_pressed_yes(m_width, m_height, report.xbutton.x,
                           report.xbutton.y))
                         system("print OUTPUT.ps");
                       flag = yes;
                     }
                break;

         case KeyPress:
                if (report.xkey.window==viewport->viewWindow ||
                    report.xkey.window==viewport->titleWindow) {
                  XFreeGC(dsply, gc);
                  XFreeGC(dsply, gc1);
                  XCloseDisplay(dsply);
                  PSClose();
                  exit(1);
                }
                else if (report.xkey.window==menu) XUnmapWindow(dsply, menu);

         default:
                break;
      }
   }
   return 0;
}
コード例 #2
0
ファイル: main2d.c プロジェクト: bfauser/fricas
int
main(void)
{

  XGCValues       controlGCVals;
  int             i,code;
  view2DStruct    viewData;

  char property[256];
  char *prop = &property[0];
  char *str_type[20];
  XrmValue value;


  /**** Set up display ****/
  if ((dsply = XOpenDisplay(getenv("DISPLAY"))) == NULL)
    fprintf(stderr,"Could not open the display.\n");
  scrn  = DefaultScreen(dsply);
  rtWindow  = RootWindow(dsply,scrn);

  /**** link Xwindows to viewports - X10 feature ****/
  table        = XCreateAssocTable(nbuckets);

  /**** Create FriCAS color map ****/
  totalColors = XInitSpadFill(dsply,scrn,&colorMap,
                              &totalHues,&totalSolidShades,
                              &totalDitheredAndSolids,&totalShades);

  if (totalColors < 0) {
    fprintf(stderr,">>Error: Could not allocate all the necessary colors.\n");
    exitWithAck(RootWindow(dsply,scrn),Window,-1);
  }

  mergeDatabases();


  /*** Determine whether monochrome or color is used ***/
  if (XrmGetResource(rDB,"Axiom.2D.monochrome","",str_type,&value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop, "off");

  mono = ((totalSolid == 2) || (strcmp(prop,"on") == 0));

  if (XrmGetResource(rDB,"Axiom.2D.inverse","",str_type,&value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop, "off");

  if (mono)
    if (strcmp(prop,"on") == 0) {             /* 0 if equal (inverse video) */
      foregroundColor = WhitePixel(dsply,scrn);
      backgroundColor = BlackPixel(dsply,scrn);
    } else {                                  /* off (no inverse video) */
      foregroundColor = BlackPixel(dsply,scrn);
      backgroundColor = WhitePixel(dsply,scrn);
    }
  else   /* inverse of inverse in color (for some strange reason) */
    if (strcmp(prop,"on") == 0) {         /* 0 if equal (inverse video) */
      foregroundColor = WhitePixel(dsply,scrn);
      backgroundColor = BlackPixel(dsply,scrn);
    } else {                                  /* off (no inverse video) */
      foregroundColor = BlackPixel(dsply,scrn);
      backgroundColor = WhitePixel(dsply,scrn);
    }


  /* read default file name for postScript output */
  if (XrmGetResource(rDB,
                     "Axiom.2D.postscriptFile",
                     "",
                     str_type, &value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop, "axiom2D.ps");

  PSfilename = (char *)malloc(strlen(prop)+1);
  strcpy(PSfilename,prop);



  /**** Open global fonts ****/
  serverFont = XQueryFont(dsply,XGContextFromGC(DefaultGC(dsply,scrn)));

  if (XrmGetResource(rDB,
                     "Axiom.2D.messageFont",
                     "Axiom.2D.Font",
                     str_type, &value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,messageFontDefault);
  if ((globalFont = XLoadQueryFont(dsply, prop)) == NULL) {
    fprintf(stderr,
            "Warning:  could not get the %s font for messageFont\n",prop);
    globalFont = serverFont;
  }

  if (XrmGetResource(rDB,
                     "Axiom.2D.buttonFont",
                     "Axiom.2D.Font",
                     str_type, &value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,buttonFontDefault);
  if ((buttonFont = XLoadQueryFont(dsply, prop)) == NULL) {
    fprintf(stderr,
            "Warning:  could not get the %s font for buttonFont\n",prop);
    buttonFont = serverFont;
  }

  if (XrmGetResource(rDB,
                     "Axiom.2D.headerFont",
                     "Axiom.2D.Font",
                     str_type, &value) == True)
     (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,headerFontDefault);

  if ((headerFont = XLoadQueryFont(dsply, prop)) == NULL) {
    fprintf(stderr,
            "Warning:  could not get the %s font for headerFont\n",prop);
    headerFont = serverFont;
  }

  if (XrmGetResource(rDB,
                     "Axiom.2D.titleFont",
                     "Axiom.2D.Font",
                     str_type,&value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,titleFontDefault);

  if ((titleFont = XLoadQueryFont(dsply, prop)) == NULL) {
    fprintf(stderr,
            "Warning:  could not get the %s font for titleFont\n",prop);
    titleFont = serverFont;
  }

  if (XrmGetResource(rDB,
                     "Axiom.2D.graphFont",
                     "Axiom.2D.Font",
                     str_type,&value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,graphFontDefault);

  if ((graphFont = XLoadQueryFont(dsply, prop)) == NULL) {
    fprintf(stderr,
            "Warning:  could not get the %s font for graphFont\n",prop);
    graphFont = serverFont;
  }

  if (XrmGetResource(rDB,
                     "Axiom.2D.unitFont",
                     "Axiom.2D.Font",
                     str_type,&value) == True)
    (void) strncpy(prop,value.addr,(int)value.size);
  else
    (void) strcpy(prop,unitFontDefault);

  if ((unitFont = XLoadQueryFont(dsply, prop)) == NULL) {
     fprintf(stderr,
             "Warning:  could not get the %s font for unitFont\n",prop);
     unitFont = serverFont;
  }


  /**** Create widely used Graphic Contexts ****/
  PSGlobalInit();
  /* must initiate before using any G/PS functions
     need character name: used as postscript GC variable
     need to create ps GCs for all GCs used by drawings in viewWindow */

  /* globalGC1 */

  controlGCVals.foreground = monoColor(axesColorDefault);
  controlGCVals.background = backgroundColor;
  globalGC1 = XCreateGC(dsply,rtWindow,GCForeground | GCBackground ,
                        &controlGCVals);
  carefullySetFont(globalGC1,globalFont);


  /* create the equivalent GCs for ps */
  PSCreateContext(globalGC1, "globalGC1", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* controlMessageGC */

  controlGCVals.foreground = controlMessageColor;
  controlMessageGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground
                               ,&controlGCVals);
  carefullySetFont(controlMessageGC,globalFont);

  /* globalGC2 */

  controlGCVals.foreground = monoColor(labelColor);
  controlGCVals.background = backgroundColor;
  globalGC2 = XCreateGC(dsply,rtWindow,GCForeground | GCBackground,
                        &controlGCVals);
  carefullySetFont(globalGC2,buttonFont);
  PSCreateContext(globalGC2, "globalGC2", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* trashGC  */

  trashGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);
  carefullySetFont(trashGC,buttonFont);
  PSCreateContext(trashGC, "trashGC", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* globGC */

  globGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);
  carefullySetFont(globGC,headerFont);
  PSCreateContext(globGC, "globGC", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* anotherGC  */

  controlGCVals.line_width = colorWidth;
  anotherGC  = XCreateGC(dsply,rtWindow,GCBackground,&controlGCVals);
  carefullySetFont(anotherGC,titleFont);
  PSCreateContext(anotherGC, "anotherGC", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* processGC */

  gcVals.background = backgroundColor;
  processGC         = XCreateGC(dsply,rtWindow,GCBackground ,&gcVals);
  carefullySetFont(processGC,buttonFont);

  /* graphGC */

  graphGC           = XCreateGC(dsply,rtWindow,GCBackground,&gcVals);
  carefullySetFont(graphGC,graphFont);
  PSCreateContext(graphGC, "graphGC", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /* unitGC */

  unitGC            = XCreateGC(dsply,rtWindow,GCBackground ,&gcVals);
  carefullySetFont(unitGC,unitFont);
  PSCreateContext(unitGC, "unitGC", psNormalWidth, psButtCap,
                  psMiterJoin, psWhite, psBlack);

  /**** Initialize Graph States ****/

  for (i=0; i<maxGraphs; i++) {
    graphStateArray[i].scaleX = 0.9;
    graphStateArray[i].scaleY = 0.9;
    graphStateArray[i].deltaX = 0.0;
    graphStateArray[i].deltaY = 0.0;
    graphStateArray[i].centerX = 0.0;
    graphStateArray[i].centerY = 0.0;
    graphStateArray[i].pointsOn  = yes;
    graphStateArray[i].connectOn = yes;
    graphStateArray[i].splineOn  = no;
    graphStateArray[i].axesOn    = yes;
    graphStateArray[i].unitsOn   = no;
    graphStateArray[i].showing   = no;
    graphStateArray[i].selected  = no;
    graphStateBackupArray[i] = graphStateArray[i];
  }

  /**** Get Data from the Viewport Manager ****/

  i = 123;
  code=check(write(Socket,&i,intSize));

  /* Check if I am getting stuff from FriCAS or, if I am viewAlone. */
  readViewman(&viewAloned,intSize);
  readViewman(&viewData,sizeof(view2DStruct));
  readViewman(&i,intSize);

  if (!(viewData.title = (char *)malloc(i))) {
    fprintf(stderr,
            "ERROR: Ran out of memory trying to receive the title.\n");
    exitWithAck(RootWindow(dsply,scrn),Window,-1);
  }
  readViewman(viewData.title,i);

  for (i=0; i<maxGraphs; i++) {
    readViewman(&(graphArray[i].key),intSize);
    if (graphArray[i].key) {            /** this graph slot has data **/
      getGraphFromViewman(i);
    } /* if graph exists (graphArray[i].key is not zero) */
  } /* for i in graphs */

  viewport = makeView2D(&viewData);
  control = viewport->controlPanel;

  bsdSignal(SIGTERM,goodbye,DontRestartSystemCalls);

  /* send acknowledgement to viewport manager */
  i = 345;
  check(write(Socket,&(viewport->viewWindow),sizeof(Window)));

  processEvents();

  goodbye(-1);
  return(0);  /* control never reaches here but compiler complains */
} /* main() */