home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / universal_plane_file_format / NewXShowPlane / callbacks.c next >
C/C++ Source or Header  |  1993-12-06  |  9KB  |  336 lines

  1.  
  2.  
  3. /*
  4. ** Generated by X-Designer Evaluation version
  5. ** This code may not be used in any program
  6. ** offered for resale or commercial use.
  7. */
  8.  
  9. /*
  10. **LIBS: -lXm -lXt -lX11
  11. */
  12.  
  13. #include <X11/Xatom.h>
  14. #include <X11/Intrinsic.h>
  15. #include <X11/Shell.h>
  16.  
  17. #include <Xm/Xm.h>
  18. #include <Xm/DrawingA.h>
  19. #include <Xm/PushB.h>
  20. #include <Xm/Text.h>
  21.  
  22. #include "widgets.h"
  23.  
  24. #include "llvs_per_plane.h"
  25.  
  26. static PLANE *pl = NULL;
  27. static PLANE_INFO *pl_info = NULL;
  28.  
  29. static Window canvas_window = NULL;
  30. static Display *canvas_display = NULL;
  31. static unsigned int canvas_width = 0, canvas_height = 0, canvas_depth = 0;
  32. static int first_time = TRUE;
  33. static XImage *ximage = NULL;
  34. static GC cgc = NULL;
  35. static Colormap colormap = NULL;
  36. static Pixel *pixel_mapping = NULL;
  37. #define WHITE_VALUE 65535.0
  38. #define NUMBER_COLORS 256
  39. #define DITHER_PIXSIZE 8
  40. static int pixelsize = 0;
  41.  
  42. static char *filename = NULL;
  43.  
  44. void 
  45. activate_proc (w, client_data, call_data )
  46. Widget w;
  47. caddr_t client_data;
  48. XmAnyCallbackStruct *call_data;
  49. {
  50.     int widget_num = (int) client_data;
  51.  
  52.     switch (widget_num)
  53.     {
  54.         /* file pulldown menu */
  55.         case SHPL_EXIT: exit(1); break;
  56.         case SHPL_OPEN: if (!XtIsManaged(openmain_widget))
  57.                   XtManageChild(openmain_widget); break;
  58.         /* file popup */
  59.         case SHPL_OPEN_OK: 
  60.                     if (filename != NULL)
  61.                     {
  62.                         XtFree(filename);
  63.                         filename = NULL;
  64.                     }
  65.                     filename = XmTextGetString(selected_file_widget);
  66.                     if (filename && canvas_window)
  67.                     {
  68.                         if (pl != NULL) {free(pl); pl = NULL;}
  69.                         if (pl_info != NULL) {free(pl_info); pl_info = NULL;}
  70.                     init_load_image(filename);
  71.                     if (pl == NULL) {
  72.                         Arg al[1];           /* Arg List */
  73.                         XmString temp;
  74.                         temp = XmStringCreateLtoR(filename,(XmStringCharSet)XmSTRING_DEFAULT_CHARSET);
  75.                         XtSetArg(al[0],XmNlabelString,temp);
  76.                         XtSetValues(errormessage_widget,al,1);
  77.                         XmStringFree (temp);
  78.                         if (!XtIsManaged(messagebox_widget))
  79.                             XtManageChild(messagebox_widget);
  80.                         }
  81.                         
  82.                         if (ximage != NULL) XDestroyImage(ximage);
  83.                     pixelsize = compute_pixel_size(pl_info,
  84.                            canvas_width,canvas_height);
  85.  
  86.                     if (canvas_depth == 1) dither_pixmap(canvas_widget);
  87.                     else if (canvas_depth == 8) grey_pixmap(canvas_widget);
  88.                         
  89.                     XClearArea(canvas_display,
  90.                              canvas_window,0,0,
  91.                              canvas_width,
  92.                              canvas_height,TRUE);
  93.                     }
  94.         case SHPL_OPEN_CANCEL: if (XtIsManaged(openmain_widget))
  95.                     XtUnmanageChild(openmain_widget); break;
  96.         case SHPL_ERROR_OK:
  97.         case SHPL_ERROR_CANCEL:
  98.                 if (XtIsManaged(messagebox_widget))
  99.                     XtUnmanageChild(messagebox_widget);
  100.                 break;
  101.  
  102.         default: break;
  103.     }
  104. }
  105.  
  106. void 
  107. expose_proc (w, client_data, call_data )
  108. Widget w;
  109. caddr_t client_data;
  110. XmDrawingAreaCallbackStruct *call_data;
  111. {
  112.     int widget_num = (int) client_data;
  113.  
  114.     switch (widget_num)
  115.     {
  116.         case SHPL_CANVAS: draw_canvas(w,call_data); break;
  117.         default: break;
  118.     }
  119. }
  120.  
  121. void 
  122. resize_proc (w, client_data, call_data )
  123. Widget w;
  124. caddr_t client_data;
  125. XmDrawingAreaCallbackStruct *call_data;
  126. {
  127.     int widget_num = (int) client_data;
  128.  
  129.     switch (widget_num)
  130.     {
  131.         case SHPL_CANVAS: resize_canvas(w,call_data); break;
  132.         default: break;
  133.     }
  134. }
  135.  
  136. void InstallColorMap(Widget w, XEvent *event, char **params, int num_param)
  137. {
  138.     XWindowAttributes  xwa;
  139.     XFlush(XtDisplay(w));
  140.     XGetWindowAttributes(XtDisplay(w), XtWindow(w), &xwa);
  141.     
  142.     if (colormap != NULL && xwa.map_installed == FALSE)
  143.          XInstallColormap(canvas_display,colormap);
  144. }
  145. void UninstallColorMap(Widget w, XEvent *event, char **params, int num_param)
  146. {
  147.     XWindowAttributes  xwa;
  148.     XFlush(XtDisplay(w));
  149.     XGetWindowAttributes(XtDisplay(w), XtWindow(w), &xwa);
  150.  
  151.     if (xwa.map_installed == TRUE && colormap != NULL)
  152.         XUninstallColormap(canvas_display,colormap);
  153. }
  154.  
  155. int compute_pixel_size(PLANE_INFO *pl_info,int width,int height)
  156. {
  157.     int im_width, im_height;
  158.     int pw, ph;
  159.  
  160.     im_width = pl_info->column_dimension;
  161.     im_height = pl_info->row_dimension;
  162.     pw = (width / ((double) im_width)) + .75;
  163.     ph = (height / ((double) im_height)) + .75;
  164.     if (pw < 1 || ph < 1) return(1);
  165.     else if (pw < ph) return(pw);
  166.     else return(ph);
  167. }
  168.  
  169. draw_canvas(Widget w,XmDrawingAreaCallbackStruct *draw_area)
  170. {
  171.     XExposeEvent *xpe = (XExposeEvent *) draw_area->event;
  172.     Window root;
  173.     int x, y;
  174.     unsigned int bw;
  175.  
  176.     if (xpe == NULL) return;
  177.     if (draw_area->window == NULL) canvas_window = XtWindow(w);
  178.     else canvas_window = draw_area->window;
  179.     if (canvas_window == NULL) return;
  180.     if (xpe->window != canvas_window) return;
  181.     if (first_time) {
  182.  
  183.         canvas_display = XtDisplay(w);
  184.  
  185.         XGetGeometry(canvas_display,canvas_window,&root,&x,&y,
  186.                  &canvas_width,&canvas_height,&bw,&canvas_depth);
  187.  
  188.         if (canvas_depth == 8)
  189.         {
  190.             int icolor;
  191.             XColor color;
  192.             double grey_level;
  193.             Arg al[1];
  194.  
  195.             colormap = XCreateColormap(canvas_display,canvas_window,
  196.                    XDefaultVisualOfScreen(XDefaultScreenOfDisplay(canvas_display)),
  197.                            AllocNone);
  198.             if (colormap == NULL)
  199.             {
  200.                 /*** error ***/
  201.             }
  202.             pixel_mapping = (Pixel *) XtCalloc(NUMBER_COLORS,sizeof(Pixel));
  203.             for (icolor = 0;icolor < NUMBER_COLORS;icolor++)
  204.             {
  205.                 grey_level = (icolor / ((double)(NUMBER_COLORS-1))) * WHITE_VALUE;
  206.                 color.blue = color.green = color.red = grey_level;
  207.                 color.flags = DoRed | DoGreen | DoBlue;
  208.                 if (XAllocColor(canvas_display,colormap,&color))
  209.                     pixel_mapping[icolor] = color.pixel;
  210.                 else
  211.                 {
  212.                     printf("XAllocColor() failed for grey_level = %f\n",grey_level);
  213.                 }
  214.             }
  215.             XSetWindowColormap(canvas_display,canvas_window,colormap);
  216.             XtSetArg(al[0],XNColormap,colormap);
  217.             XtSetValues(w,al,1);            
  218.         }
  219.  
  220.         cgc = XCreateGC(canvas_display,canvas_window,0L,NULL);
  221.         XSetState(canvas_display,cgc,0L,255L,GXcopy,AllPlanes);
  222.         XSetLineAttributes(canvas_display,cgc,2,
  223.                    LineSolid,CapNotLast,JoinMiter);
  224.  
  225.         first_time = FALSE;
  226.  
  227.     }
  228.  
  229.     if (ximage == NULL) return;
  230.     XPutImage(canvas_display,canvas_window,cgc,ximage,xpe->x, xpe->y,
  231.                              xpe->x, xpe->y,
  232.                              xpe->width, xpe->height);
  233.     XSync(canvas_display,FALSE);
  234.  
  235. }
  236.  
  237. resize_canvas(Widget w,XmDrawingAreaCallbackStruct *draw_area)
  238. {
  239.     Window root;
  240.     int x, y;
  241.     unsigned int bw;
  242.     int newpixsize;
  243.  
  244.     if (canvas_window == NULL) return;
  245.     XGetGeometry(XtDisplay(w),canvas_window,&root,&x,&y,
  246.              &canvas_width,&canvas_height,&bw,&canvas_depth);
  247.  
  248.     if (first_time) return;
  249.     if (pl == NULL) return;
  250.     newpixsize = compute_pixel_size(pl_info,canvas_width,canvas_height);
  251.     if (newpixsize != pixelsize)
  252.     {
  253.         pixelsize = newpixsize;
  254.         XDestroyImage(ximage);
  255.  
  256.         if (canvas_depth == 1) dither_pixmap(w);
  257.         else if (canvas_depth == 8) grey_pixmap(w);
  258.     }
  259.     
  260. }
  261.  
  262. init_load_image(char *filename)
  263. {
  264.     static char *ass = NULL;
  265.  
  266.     if (read_plane(&pl,&pl_info,&ass,filename) < 0) pl = NULL;
  267.     if (ass != NULL) free(ass);
  268. }
  269.  
  270. dither_pixmap(Widget w)
  271. {
  272.     Display *display;
  273.     Screen  *screen;
  274.     unsigned char *data;
  275.     static LIMITS limits;
  276.  
  277.     display = XtDisplay(w);
  278.     screen  = XDefaultScreenOfDisplay(display);
  279.  
  280.  
  281.     data = (unsigned char *)
  282.         XtCalloc((((pl_info->column_dimension*pixelsize)+7)/8)*
  283.                 pl_info->row_dimension*pixelsize,
  284.                       sizeof(unsigned char));
  285.  
  286.     ximage = XCreateImage(display,XDefaultVisualOfScreen(screen),1,
  287.                   ZPixmap,0,data,
  288.                   pl_info->column_dimension*pixelsize,
  289.                   pl_info->row_dimension*pixelsize,8,0);
  290.  
  291.     limits.level = pl_info->level;
  292.     limits.deltarow = 1;
  293.     limits.deltacol = 1;
  294.     limits.startrow = pl_info->row_location;
  295.     limits.startcol = pl_info->column_location;
  296.     limits.endrow   = pl_info->row_location + pl_info->row_dimension - 1;
  297.     limits.endcol   = pl_info->column_location + pl_info->column_dimension - 1;
  298.     XShpl_dither(pl,pl_info,&limits,0.0,0.0,pixelsize,FALSE,FALSE,
  299.              ximage);
  300. }
  301.  
  302.  
  303. grey_pixmap(Widget w)
  304. {
  305.     Display *display;
  306.     Screen  *screen;
  307.     unsigned char *data;
  308.     static LIMITS limits;
  309.  
  310.     display = XtDisplay(w);
  311.     screen  = XDefaultScreenOfDisplay(display);
  312.     data = (unsigned char *)
  313.         XtCalloc(pl_info->column_dimension*pixelsize*
  314.              pl_info->row_dimension*pixelsize,
  315.                       sizeof(unsigned char));
  316.  
  317.     ximage = XCreateImage(display,XDefaultVisualOfScreen(screen),8,
  318.                   ZPixmap,0,data,pl_info->column_dimension*pixelsize,
  319.                   pl_info->row_dimension*pixelsize,8,0);
  320.  
  321.     limits.level = pl_info->level;
  322.     limits.deltarow = 1;
  323.     limits.deltacol = 1;
  324.     limits.startrow = pl_info->row_location;
  325.     limits.startcol = pl_info->column_location;
  326.     limits.endrow   = pl_info->row_location + pl_info->row_dimension - 1;
  327.     limits.endcol   = pl_info->column_location + pl_info->column_dimension - 1;
  328.     XShpl_grey(pl,pl_info,&limits,0.0,0.0,pixelsize,FALSE,FALSE,ximage,
  329.            pixel_mapping,NUMBER_COLORS);
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336.