home *** CD-ROM | disk | FTP | other *** search
- /*
- * FIG : Facility for Interactive Generation of figures
- *
- * Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
- * January 1985.
- * 1st revision : Aug 1985.
- *
- * %W% %G%
- */
- #include "fig.h"
- #include "resources.h"
- #include "object.h"
- #include "paintop.h"
-
- extern int CANVAS_WIDTH, CANVAS_HEIGHT;
- extern F_compound objects;
-
- extern int errno;
- extern int pointmarker_shown;
- extern int compoundbox_shown;
- extern appresStruct appres;
-
- extern char *sys_errlist[];
- extern int sys_nerr, errno;
-
- write_bitmap(file_name)
- char *file_name;
- {
- FILE *fp;
- struct stat file_status;
- char string[180];
-
- if (*file_name == 0) {
- put_msg("No file");
- return;
- }
- if (stat(file_name, &file_status) == 0) { /* file exists */
- if (file_status.st_mode & S_IFDIR) {
- put_msg("\"%s\" is a directory", file_name);
- return;
- }
- if (file_status.st_mode & S_IWRITE) { /* writing is permitted */
- if (file_status.st_uid != geteuid()) {
- put_msg("\"%s\" permission is denied", file_name);
- return;
- }
- else {
- sprintf(string, "\"%s\" File exists. Please click the LEFT button to COMFIRM overwrite. To cancel, click the MIDDLE or RIGHT button.", file_name);
- if (wmgr_confirm(canvas_win, string) != -1) {
- put_msg("No write.");
- return;
- }
- }
- }
- else {
- put_msg("\"%s\" File is read only", file_name);
- return;
- }
- }
- else if (errno != ENOENT) /* file does exist but stat fails */
- return;
-
- create_n_write_bitmap(file_name); /* write the bitmap file */
- }
-
- static Boolean havegcs = False;
- static GC sav_fill_gc[NUMFILLPATS];
- static GC sav_un_fill_gc[NUMFILLPATS];
- static unsigned long save_fg_color;
- static unsigned long save_bg_color;
-
- create_n_write_bitmap(filename)
- char *filename;
- {
- int box, marker, xmin, ymin, xmax, ymax;
- int width, height;
- Window sav_canvas, oneplane_win;
- int sav_pointmarker_shown;
- Pixmap largepm,bitmap;
- extern F_compound objects;
- XVisualInfo *visual,vinfo_template,*vis;
- long vinfo_mask;
- int nitems,i;
- GC xgc;
-
- /* Assume that there is at least one object */
- compound_bound(&objects, &xmin, &ymin, &xmax, &ymax);
-
- if (appres.DEBUG) {
- draw_rectbox(xmin, ymin, xmax, ymax, INV_PAINT);
- }
-
- /* provide a small margin */
- if ((xmin -= 10) < 0)
- xmin = 0;
- if ((ymin -= 10) < 0)
- ymin = 0;
- if ((xmax += 10) > CANVAS_WIDTH)
- xmax = CANVAS_WIDTH;
- if ((ymax += 10) > CANVAS_HEIGHT)
- ymax = CANVAS_HEIGHT;
-
- width = xmax-xmin+1;
- height = ymax-ymin+1;
-
- /* choose foreground/background colors as 1 and 0 respectively */
- /* that way we can just copy the lowest plane to make the bitmap */
-
- XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) 1);
- XSetForeground(tool_d, gccache[PAINT], (unsigned long) 1);
- XSetBackground(tool_d, gccache[PAINT], (unsigned long) 0);
- XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) 1);
- XSetForeground(tool_d, gccache[ERASE], (unsigned long) 0);
- XSetBackground(tool_d, gccache[ERASE], (unsigned long) 0);
- save_fg_color = x_fg_color.pixel; /* save current colors */
- save_bg_color = x_bg_color.pixel;
- x_fg_color.pixel = 1; /* set fore=1, back=0 */
- x_bg_color.pixel = 0;
- if (!havegcs)
- {
- havegcs = True;
- for (i=0; i<NUMFILLPATS; i++) /* save current fill gc's */
- {
- sav_fill_gc[i] = fill_gc[i];
- sav_un_fill_gc[i] = un_fill_gc[i];
- }
- init_fill_gc(); /* make some with 0/1 for colors */
- }
- else
- for (i=0; i<NUMFILLPATS; i++)
- {
- xgc = sav_fill_gc[i]; /* swap our gc's with orig */
- sav_fill_gc[i] = fill_gc[i];
- fill_gc[i] = xgc;
- xgc = sav_un_fill_gc[i];
- sav_un_fill_gc[i] = un_fill_gc[i];
- un_fill_gc[i] = xgc;
- }
-
- /* create pixmap from (0,0) to (xmax,ymax) */
- largepm = XCreatePixmap(tool_d, canvas_win, xmax+1, ymax+1,
- DefaultDepthOfScreen(tool_s));
- /* clear it */
- XFillRectangle(tool_d, largepm, gccache[ERASE], xmin,ymin,width,height);
- sav_canvas = canvas_win; /* save current canvas window id */
- canvas_win = largepm; /* make the canvas our pixmap */
- sav_pointmarker_shown = pointmarker_shown; /* save the point marker */
- pointmarker_shown = False;
- redisplay_objects(&objects); /* draw the figure into the pixmap */
- XFlush(tool_d);
- canvas_win = sav_canvas; /* go back to the real canvas */
- pointmarker_shown = sav_pointmarker_shown; /* restore point marker */
- bitmap = XCreatePixmap(tool_d, canvas_win, width, height,
- DefaultDepthOfScreen(tool_s));
- /* now copy one plane of the pixmap to a bitmap of the correct size */
- XCopyPlane(tool_d, largepm, bitmap, gccache[PAINT],
- xmin, ymin, width, height, 0, 0, 1);
- x_fg_color.pixel = save_fg_color; /* put colors back to normal */
- x_bg_color.pixel = save_bg_color;
- XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) AllPlanes);
- XSetForeground(tool_d, gccache[PAINT], x_fg_color.pixel);
- XSetBackground(tool_d, gccache[PAINT], x_bg_color.pixel);
- XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) AllPlanes);
- XSetForeground(tool_d, gccache[ERASE], x_bg_color.pixel);
- XSetBackground(tool_d, gccache[ERASE], x_bg_color.pixel);
- for (i=0; i<NUMFILLPATS; i++) /* swap back the fill gc's */
- {
- xgc = sav_fill_gc[i];
- sav_fill_gc[i] = fill_gc[i];
- fill_gc[i] = xgc;
- xgc = sav_un_fill_gc[i];
- sav_un_fill_gc[i] = un_fill_gc[i];
- un_fill_gc[i] = xgc;
- }
- if (XWriteBitmapFile(tool_d, filename, bitmap, width, height, -1, -1)
- != BitmapSuccess)
- put_msg("Couldn't write bitmap file");
- else
- put_msg("Bitmap written to %s",filename);
- XFreePixmap(tool_d, largepm);
- XFreePixmap(tool_d, bitmap);
- }
-