home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part05 / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-02  |  5.4 KB  |  183 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "object.h"
  13. #include "paintop.h"
  14.  
  15. extern int        CANVAS_WIDTH, CANVAS_HEIGHT;
  16. extern F_compound    objects;
  17.  
  18. extern int        errno;
  19. extern int        pointmarker_shown;
  20. extern int        compoundbox_shown;
  21. extern appresStruct    appres;
  22.  
  23. extern char        *sys_errlist[];
  24. extern int        sys_nerr, errno;
  25.  
  26. write_bitmap(file_name)
  27. char    *file_name;
  28.     {
  29.     FILE            *fp;
  30.     struct stat         file_status;
  31.     char             string[180];
  32.  
  33.     if (*file_name == 0) {
  34.         put_msg("No file");
  35.         return;
  36.         }
  37.     if (stat(file_name, &file_status) == 0) { /* file exists */
  38.         if (file_status.st_mode & S_IFDIR) {
  39.         put_msg("\"%s\" is a directory", file_name);
  40.         return;
  41.         }
  42.         if (file_status.st_mode & S_IWRITE) { /* writing is permitted */
  43.         if (file_status.st_uid != geteuid()) {
  44.             put_msg("\"%s\" permission is denied", file_name);
  45.             return;
  46.             }
  47.         else {
  48.             sprintf(string, "\"%s\" File exists. Please click the LEFT button to COMFIRM overwrite. To cancel, click the MIDDLE or RIGHT button.", file_name);
  49.             if (wmgr_confirm(canvas_win, string) != -1) {
  50.             put_msg("No write.");
  51.             return;
  52.             }
  53.             }
  54.         }
  55.         else {
  56.         put_msg("\"%s\" File is read only", file_name);
  57.         return;
  58.         }
  59.         }
  60.     else if (errno != ENOENT)    /* file does exist but stat fails */
  61.         return; 
  62.  
  63.     create_n_write_bitmap(file_name);    /* write the bitmap file */
  64.     }
  65.  
  66. static    Boolean havegcs = False;
  67. static    GC sav_fill_gc[NUMFILLPATS];
  68. static    GC sav_un_fill_gc[NUMFILLPATS];
  69. static    unsigned long save_fg_color;
  70. static    unsigned long save_bg_color;
  71.  
  72. create_n_write_bitmap(filename)
  73. char *filename;
  74.     {
  75.     int    box, marker, xmin, ymin, xmax, ymax;
  76.     int    width, height;
  77.     Window    sav_canvas, oneplane_win;
  78.     int    sav_pointmarker_shown;
  79.     Pixmap    largepm,bitmap;
  80.     extern    F_compound objects;
  81.     XVisualInfo  *visual,vinfo_template,*vis;
  82.     long    vinfo_mask;
  83.     int    nitems,i;
  84.     GC    xgc;
  85.  
  86.     /* Assume that there is at least one object */
  87.     compound_bound(&objects, &xmin, &ymin, &xmax, &ymax);
  88.  
  89.     if (appres.DEBUG) {
  90.         draw_rectbox(xmin, ymin, xmax, ymax, INV_PAINT);
  91.         }
  92.  
  93.     /* provide a small margin */
  94.     if ((xmin -= 10) < 0)
  95.         xmin = 0;
  96.     if ((ymin -= 10) < 0)
  97.         ymin = 0;
  98.     if ((xmax += 10) > CANVAS_WIDTH)
  99.         xmax = CANVAS_WIDTH;
  100.     if ((ymax += 10) > CANVAS_HEIGHT)
  101.         ymax = CANVAS_HEIGHT;
  102.  
  103.     width = xmax-xmin+1;
  104.     height = ymax-ymin+1;
  105.  
  106.     /* choose foreground/background colors as 1 and 0 respectively */
  107.     /* that way we can just copy the lowest plane to make the bitmap */
  108.  
  109.     XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) 1);
  110.     XSetForeground(tool_d, gccache[PAINT], (unsigned long) 1);
  111.     XSetBackground(tool_d, gccache[PAINT], (unsigned long) 0);
  112.     XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) 1);
  113.     XSetForeground(tool_d, gccache[ERASE], (unsigned long) 0);
  114.     XSetBackground(tool_d, gccache[ERASE], (unsigned long) 0);
  115.     save_fg_color = x_fg_color.pixel;    /* save current colors */
  116.     save_bg_color = x_bg_color.pixel;
  117.     x_fg_color.pixel = 1;            /* set fore=1, back=0 */
  118.     x_bg_color.pixel = 0;
  119.     if (!havegcs)
  120.         {
  121.         havegcs = True;
  122.         for (i=0; i<NUMFILLPATS; i++)    /* save current fill gc's */
  123.         {
  124.         sav_fill_gc[i] = fill_gc[i];
  125.         sav_un_fill_gc[i] = un_fill_gc[i];
  126.         }
  127.         init_fill_gc();        /* make some with 0/1 for colors */
  128.         }
  129.     else
  130.         for (i=0; i<NUMFILLPATS; i++)
  131.         {
  132.         xgc = sav_fill_gc[i];        /* swap our gc's with orig */
  133.         sav_fill_gc[i] = fill_gc[i];
  134.         fill_gc[i] = xgc;
  135.         xgc = sav_un_fill_gc[i];
  136.         sav_un_fill_gc[i] = un_fill_gc[i];
  137.         un_fill_gc[i] = xgc;
  138.         }
  139.     
  140.     /* create pixmap from (0,0) to (xmax,ymax) */
  141.     largepm = XCreatePixmap(tool_d, canvas_win, xmax+1, ymax+1, 
  142.                 DefaultDepthOfScreen(tool_s));
  143.     /* clear it */
  144.     XFillRectangle(tool_d, largepm, gccache[ERASE], xmin,ymin,width,height);
  145.     sav_canvas = canvas_win;    /* save current canvas window id */
  146.     canvas_win = largepm;        /* make the canvas our pixmap */
  147.     sav_pointmarker_shown = pointmarker_shown;  /* save the point marker */
  148.     pointmarker_shown = False;
  149.     redisplay_objects(&objects);    /* draw the figure into the pixmap */
  150.     XFlush(tool_d);
  151.     canvas_win = sav_canvas;    /* go back to the real canvas */
  152.     pointmarker_shown = sav_pointmarker_shown; /* restore point marker */
  153.     bitmap = XCreatePixmap(tool_d, canvas_win, width, height, 
  154.             DefaultDepthOfScreen(tool_s));
  155.     /* now copy one plane of the pixmap to a bitmap of the correct size */
  156.     XCopyPlane(tool_d, largepm, bitmap, gccache[PAINT], 
  157.             xmin, ymin, width, height, 0, 0, 1);
  158.     x_fg_color.pixel = save_fg_color;    /* put colors back to normal */
  159.     x_bg_color.pixel = save_bg_color;
  160.     XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) AllPlanes);
  161.     XSetForeground(tool_d, gccache[PAINT], x_fg_color.pixel);
  162.     XSetBackground(tool_d, gccache[PAINT], x_bg_color.pixel);
  163.     XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) AllPlanes);
  164.     XSetForeground(tool_d, gccache[ERASE], x_bg_color.pixel);
  165.     XSetBackground(tool_d, gccache[ERASE], x_bg_color.pixel);
  166.     for (i=0; i<NUMFILLPATS; i++)        /* swap back the fill gc's */
  167.         {
  168.         xgc = sav_fill_gc[i];
  169.         sav_fill_gc[i] = fill_gc[i];
  170.         fill_gc[i] = xgc;
  171.         xgc = sav_un_fill_gc[i];
  172.         sav_un_fill_gc[i] = un_fill_gc[i];
  173.         un_fill_gc[i] = xgc;
  174.         }
  175.     if (XWriteBitmapFile(tool_d, filename, bitmap, width, height, -1, -1)
  176.         != BitmapSuccess)
  177.         put_msg("Couldn't write bitmap file");
  178.     else
  179.         put_msg("Bitmap written to %s",filename);
  180.     XFreePixmap(tool_d, largepm);
  181.     XFreePixmap(tool_d, bitmap);
  182.     }
  183.