home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / yabv / part01 next >
Text File  |  1990-08-21  |  5KB  |  248 lines

  1. Path: uunet!decwrl!wuarchive!cs.utexas.edu!sun-barr!newstop!sun!hpmtlrt.hp.com
  2. From: thompson_r@hpmtlrt.hp.com (Robert Thompson)
  3. Newsgroups: comp.sources.x
  4. Subject: v08i092: Yet Another Bitmap Viewer, Part01/01
  5. Message-ID: <141103@sun.Eng.Sun.COM>
  6. Date: 22 Aug 90 01:58:20 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 237
  9. Approved: argv@sun.com
  10.  
  11. Submitted-by: Robert Thompson <thompson_r@hpmtlrt.hp.com>
  12. Posting-number: Volume 8, Issue 92
  13. Archive-name: yabv/part01
  14.  
  15. [ Need I say more? :-)  --dan ]
  16.  
  17. #---------------------------------- cut here ----------------------------------
  18. # This is a shell archive.  Remove anything before this line,
  19. # then unpack it by saving it in a file and typing "sh file".
  20. #
  21. # Wrapped by  <thompson_r@hpmtlrt> on Mon Aug 20 14:28:20 1990
  22. #
  23. # This archive contains:
  24. #    Imakefile    Makefile    bmv.c        patchlevel.h    
  25. #
  26.  
  27. LANG=""; export LANG
  28. PATH=/bin:/usr/bin:$PATH; export PATH
  29.  
  30. echo x - Imakefile
  31. cat >Imakefile <<'@EOF'
  32. LOCAL_LIBRARIES = $(XLIB)
  33.            SRCS = bmv.c
  34.            OBJS = bmv.o
  35.  
  36. ComplexProgramTarget(bmv)
  37. @EOF
  38.  
  39. chmod 666 Imakefile
  40.  
  41. echo x - Makefile
  42. cat >Makefile <<'@EOF'
  43. SRCS=    bmv.c
  44. OBJS=    bmv.o
  45. CC=    cc
  46. CFLAGS=    -O 
  47. LIBS=    -lX11
  48.  
  49. all:    bmv
  50.  
  51. bmv:
  52.     cc $(CFLAGS) $@.c -o $@ $(LIBS)
  53.  
  54. @EOF
  55.  
  56. chmod 666 Makefile
  57.  
  58. echo x - bmv.c
  59. cat >bmv.c <<'@EOF'
  60. #include <X11/XHPlib.h>
  61. #include <X11/X.h>
  62. #include <X11/Xutil.h>
  63. #include <stdio.h>
  64. #include "patchlevel.h"
  65.  
  66. typedef struct {
  67.     Pixmap    pmap;
  68.     char    name[64];
  69.     int    x;
  70.     int    y;
  71.     int    width;
  72.     int    height;
  73. } Image;
  74.  
  75. Image    image[256];
  76. int    numbms;
  77.  
  78. main(argc,argv)
  79. int    argc;
  80. char    **argv;
  81. {
  82.     register int    counter,status;
  83.     int    xh,yh;
  84.     int    maxheight = 0;
  85.     int    nextx = 0;
  86.     int    nexty = 30;
  87.     int    lasty = 0;
  88.     GC    gc;
  89.     Window    w;
  90.     Display    *disp;
  91.     XGCValues    values;
  92.     Font        font;
  93.  
  94.     if (argc < 2)
  95.         {
  96.         printf ("usage: %s file1 [file 2] ... [file 256]\n",argv[0]);
  97.         exit(0);
  98.         }
  99.     else 
  100.         numbms = argc;
  101.  
  102.  
  103.     disp = XOpenDisplay("");
  104.  
  105.     font = XLoadFont(disp,"vtbold");
  106.  
  107.     w = XCreateSimpleWindow(disp,
  108.         RootWindow(disp,DefaultScreen(disp)),
  109.         0,0,900,800,    
  110.         10,    
  111.         0x000000,
  112.         0x2a5a7b);
  113.     
  114.     XStoreName(disp,w,"Bitmap Viewer v1.0");
  115.     XSetIconName(disp,w,"BMV V1.0");
  116.  
  117.     values.foreground = 0x000000;
  118.     values.background = 0xffffff;
  119.     values.function   = GXcopy;
  120.     values.font      = font;
  121.  
  122.     gc = XCreateGC(disp,w,
  123.         GCFunction|GCForeground|GCBackground|GCFont,&values);
  124.  
  125.     for (counter=1;counter < argc;counter++) {
  126.  
  127.     status = XReadBitmapFile(disp,w,argv[counter],
  128.             &image[counter].width,
  129.             &image[counter].height,
  130.             &image[counter].pmap,&xh,&yh);
  131.  
  132.     if (status != BitmapSuccess)
  133.         fprintf (stderr,"Could not load %s\n",argv[counter]);
  134.     else
  135.         {
  136.         if (nextx > 900 - image[counter].width)
  137.             {
  138.             nexty = lasty + maxheight + 2;
  139.             lasty = nexty;
  140.             nextx = 0;
  141.             maxheight = 0;
  142.             }
  143.  
  144.         strcpy(image[counter].name,argv[counter]);
  145.         XCopyPlane(disp, image[counter].pmap,w, gc, 0,0,
  146.             image[counter].width,
  147.             image[counter].height,
  148.             nextx,nexty,
  149.             1);
  150.  
  151.         image[counter].x = nextx;
  152.         image[counter].y = nexty;
  153.  
  154.         maxheight = (image[counter].height > maxheight ? 
  155.                 image[counter].height:maxheight);
  156.  
  157.         nextx += image[counter].width + 2;
  158.         }
  159.     }
  160.  
  161.     XMapWindow(disp,w);
  162.     XSelectInput(disp,w, ButtonPressMask|ExposureMask|
  163.         VisibilityChangeMask|KeyPressMask);
  164.  
  165.     for (;;)
  166.         {
  167.         XEvent    ev;
  168.         XNextEvent(disp,&ev);
  169.         switch (ev.type) {
  170.             case ButtonPress:
  171.                 if (!FindBitmap(disp,w,ev,gc))
  172.                     break;
  173.             case KeyPress:
  174.                 {
  175.                 for (counter = 1;counter < argc;counter ++)
  176.                 XFreePixmap(disp,image[counter].pmap);
  177.                 exit(0);
  178.                 }
  179.             case MapNotify:
  180.             case Expose:
  181.                 for (counter = 1;counter < argc;counter ++)
  182.                 {
  183.                 XCopyPlane(disp,
  184.                     image[counter].pmap,w,
  185.                     gc,
  186.                     0,0,
  187.                     image[counter].width,
  188.                     image[counter].height,
  189.                     image[counter].x,
  190.                     image[counter].y,
  191.                     1);
  192.                 }
  193.                 break;
  194.             }    
  195.         }
  196. }
  197.  
  198. int FindBitmap(disp,win,event,gc)
  199. Display    *disp;
  200. Window    win;
  201. XEvent    event;
  202. GC    gc;
  203. {
  204.     int    x = event.xbutton.x;
  205.     int    y = event.xbutton.y;
  206.     register int i;
  207.     char    buf[128];
  208.  
  209.     for (i = 1;i < numbms;i++)
  210.         if (    x >= image[i].x         && 
  211.             y >= image[i].y            &&
  212.             x <= image[i].x+image[i].width    &&
  213.             y <= image[i].y+image[i].height) break;
  214.  
  215.     switch (event.xbutton.button)
  216.         {
  217.         case 1:
  218.             XClearArea(disp,win,0,0,900,29);
  219.             sprintf(buf,"That bitmap is called: %s",image[i].name);
  220.             XDrawString(disp,win,gc,20,20,buf,strlen(buf));
  221.             break;
  222.         case 2:
  223.             sprintf (buf,"bitmap -nodashed %s",image[i].name);
  224.             system(buf);
  225.             break;
  226.         case 3:
  227.             return (-1);
  228.         }
  229.     return(0);
  230. }
  231. @EOF
  232.  
  233. chmod 666 bmv.c
  234.  
  235. echo x - patchlevel.h
  236. cat >patchlevel.h <<'@EOF'
  237. #define PATCHLEVEL 0
  238. @EOF
  239.  
  240. chmod 666 patchlevel.h
  241.  
  242. exit 0
  243.  
  244. dan
  245. ----------------------------------------------------
  246. O'Reilly && Associates   argv@sun.com / argv@ora.com
  247. Opinions expressed reflect those of the author only.
  248.