home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / moveicon / showpix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.8 KB  |  279 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <suntool/sunview.h>
  5. #include <suntool/canvas.h>
  6. #include "bitmap.h"
  7.  
  8. /*
  9.  * Typedef'ing things...
  10.  */
  11. typedef struct pixrect    Pixrect;
  12.  
  13. /*
  14.  * Unique menu identifiers 
  15.  */
  16. #define FILEN            ((caddr_t) 1)
  17. #define MORE            ((caddr_t) 2)
  18. #define QUIT            ((caddr_t) 3)
  19.  
  20. #define MAX_BITMAPS        256
  21. #define NULL_PR            ((Pixrect *) NULL)
  22. #define canvas_width(canvas)    (int) window_get(canvas, CANVAS_WIDTH)
  23. #define canvas_height(canvas)    (int) window_get(canvas, CANVAS_HEIGHT)
  24.  
  25. #define NUM_ROW            5
  26. #define NUM_COLUMN        5
  27. #define ROW_GAP            10
  28. #define ROW_HEIGHT        64
  29. #define COLUMN_GAP        10
  30. #define COLUMN_WIDTH        64
  31. #define DISPLAY_MARGIN        10
  32. #define DISPLAY_WIDTH        ((COLUMN_WIDTH + COLUMN_GAP) * NUM_COLUMN)
  33. #define DISPLAY_HEIGHT        ((ROW_HEIGHT + ROW_GAP) * NUM_ROW)
  34. #define WINDOW_MARGIN        10
  35. #define WINDOW_WIDTH        (DISPLAY_WIDTH + 2 * DISPLAY_MARGIN + \
  36.                     2 * WINDOW_MARGIN)
  37. #define WINDOW_HEIGHT        (DISPLAY_HEIGHT + 2 * DISPLAY_MARGIN + \
  38.                     2 * WINDOW_MARGIN)
  39.  
  40. /*
  41.  * The tool's icon
  42.  */
  43. static short    icon_data[] = {
  44. #include "showpix.icon"
  45. };
  46. mpr_static(icon_pixrect, 64, 64, 1, icon_data);
  47.  
  48. /*
  49.  * The data for grey background
  50.  */
  51. static short    grey_data[] = {
  52.         0xAAAA,0x5555,0xAAAA,0x5555,
  53.     0xAAAA,0x5555,0xAAAA,0x5555,
  54.     0xAAAA,0x5555,0xAAAA,0x5555,
  55.     0xAAAA,0x5555,0xAAAA,0x5555
  56. };
  57.  
  58. /*
  59.  * The data for white background
  60.  */
  61. static short    white_data[] = {
  62.             0x0000, 0x0000, 0x0000, 0x0000,
  63.             0x0000, 0x0000, 0x0000, 0x0000,
  64.             0x0000, 0x0000, 0x0000, 0x0000,
  65.             0x0000, 0x0000, 0x0000, 0x0000
  66. };
  67.  
  68. /*
  69.  * Local variables
  70.  */
  71. Bitmap        *bitmaps[MAX_BITMAPS];
  72. static int    nbitmap;
  73. static Menu    menu;
  74. static int    display_width = DISPLAY_WIDTH;
  75. static int    display_height = DISPLAY_HEIGHT;
  76. static int    canvas_resized = FALSE;
  77. static int    ending_bitmap = 0;
  78. static Pixrect    *bkg_pr;
  79.  
  80. static void
  81. usage()
  82. {
  83.     printf("usage: showpix [ -grey ] bitmap1 [ bitmap2 ... ]\n");
  84.     exit(1);
  85. } /* end usage() */
  86.  
  87. static void
  88. punt(str)
  89. char    *str;
  90. {
  91.     fprintf(stderr, "%s\n", str);
  92.     exit(1);
  93. } /* end punt() */
  94.  
  95. /*
  96.  * Load all bitmaps specified on command line.
  97.  */
  98. static void
  99. load_bitmaps(argc, argv)
  100. int    argc;
  101. char    **argv;
  102. {
  103.     register int    i;
  104.     struct stat    statbuf;
  105.     Bitmap        *tmp_bitmap;
  106.  
  107.     /* process each argument */
  108.     for (i = 0, nbitmap = 0; i < argc; i++) {
  109.         /* check that this is a regular file */
  110.         stat(argv[i], &statbuf);
  111.         if (statbuf.st_mode & S_IFMT != TRUE) {
  112.             fprintf(stderr, "File %s not a plain file.\n", argv[i]);
  113.             continue;
  114.         } 
  115.         /* load the bitmap */
  116.         if ((tmp_bitmap = bm_load(argv[i])) == NULL_BM) {
  117.             fprintf(stderr, "Error loading %s\n", argv[i]);
  118.             continue;
  119.         } else {
  120.             bitmaps[nbitmap++] = tmp_bitmap;
  121.         }
  122.     } /* end for */
  123. } /* end load_bitmaps() */
  124.  
  125. /* 
  126.  * Print the bitmap pixrects into the canvas pixwin.
  127.  */
  128. static void
  129. paint_bitmaps(canvas)
  130. Canvas    canvas;
  131. {
  132.     register int    i, dst_x, dst_y, off_x;
  133.     Pixwin        *canvas_pw;
  134.     Pixrect        *canvas_mpr;
  135.  
  136.     /* initalialize canvas pixwin */
  137.     canvas_pw = canvas_pixwin(canvas);
  138.     /* get the memory pixrect */
  139.     canvas_mpr = canvas_pw->pw_prretained;
  140.     /* replicate background source pixrect over background pixwin */
  141.     pr_replrop(canvas_mpr, 0, 0, display_width, display_height, PIX_SRC, 
  142.         bkg_pr, 0, 0);
  143.     /* initialize vertical offset into memory pixrect */
  144.     dst_y = ROW_GAP;
  145.     /* copy bitmaps into memory pixrect */
  146.     for (i = ending_bitmap; i < nbitmap; ) {
  147.         if (dst_y + bitmaps[i]->height > display_height) break;
  148.         /* initialize horizontal offset into memory pixrect */
  149.         dst_x = COLUMN_GAP;
  150.         off_x = 0;
  151.         while (i < nbitmap) {
  152.                 if (dst_x + bitmaps[i]->width > display_width) break;
  153.             pr_rop(canvas_mpr, dst_x, dst_y, bitmaps[i]->width, 
  154.                 bitmaps[i]->height, PIX_SRC, 
  155.                 bitmaps[i]->bitmap_pr, 0, 0);
  156.             dst_x += (bitmaps[i]->width + COLUMN_GAP);
  157.             if (bitmaps[i]->height > off_x) {
  158.                 off_x = bitmaps[i]->height;
  159.             }
  160.             i++;
  161.         } /* end while */
  162.         dst_y += (off_x + ROW_GAP);
  163.     } /* end while */
  164.     /* refresh the screen image */
  165.     pw_write(canvas_pw, 0, 0, display_width, display_height, PIX_SRC,
  166.         canvas_mpr, 0, 0);
  167.     /* if we have hit the end, cycle around */
  168.     ending_bitmap = (i < nbitmap) ? i : 0;
  169. } /* end paint_bitmaps() */
  170.  
  171. static void
  172. canvas_resize_proc(canvas, width, height)
  173. Canvas    canvas;
  174. int    width;
  175. int    height;
  176. {
  177.     /* reset height && width */
  178.     display_height = canvas_height(canvas);
  179.     display_width = canvas_width(canvas);
  180.     /* cycle to beginning */
  181.     ending_bitmap = 0;
  182.     /* repaint the bitmaps */
  183.     paint_bitmaps(canvas);
  184. } /* end canvas_resize_proc() */
  185.  
  186. static void
  187. canvas_event_proc(canvas, eventp, arg)
  188. Canvas    canvas;
  189. Event    *eventp;
  190. caddr_t    arg;
  191. {
  192.     caddr_t    mi;    /* a menu item */
  193.  
  194.     /* handle the mouse button events */
  195.     switch(event_id(eventp)) {
  196.         case MS_LEFT:        /* handle left button events */
  197.             break;
  198.         case MS_MIDDLE:        /* handle middle button events */
  199.             break;
  200.         case MS_RIGHT:        /* handle right button events */
  201.             mi = menu_show(menu, canvas, eventp, 0);
  202.             if (mi == NULL) break;
  203.             switch (mi) {
  204.                 case FILEN:
  205.                     /* get new file name(s) */
  206.                     break;
  207.                 case MORE:
  208.                     /* show next page */
  209.                     paint_bitmaps(canvas);
  210.                     break;
  211.                 case QUIT:
  212.                     /* destroy all windows && exit */
  213.                     window_done(canvas);
  214.                     break;
  215.             } /* end switch */
  216.             break;
  217.     } /* end switch */
  218. } /* end canvas_event_proc() */
  219.  
  220. main(argc, argv)
  221. int    argc;
  222. char    **argv;
  223. {
  224.     Canvas        canvas;
  225.     Frame        base_frame;
  226.  
  227.     /* check for usage */
  228.     if (argc < 2) usage();
  229.     /* skip over the program name */
  230.     argc--;
  231.     argv++;
  232.     /* check to see what kind of background they want */
  233.     if (strcmp(*argv, "-grey") == 0) {
  234.         bkg_pr = mem_point(16, 16, 1, grey_data); 
  235.         /* skip over the background option */
  236.         argv++;
  237.         if (argc-- < 1) usage();
  238.     } else {
  239.         bkg_pr = mem_point(16, 16, 1, white_data);
  240.     }
  241.     /* create the root window */
  242.     if ((base_frame = window_create(NULL, FRAME, 
  243.         WIN_WIDTH,            WINDOW_WIDTH,
  244.         WIN_HEIGHT,            WINDOW_HEIGHT,
  245.         FRAME_SHOW_LABEL,        FALSE,
  246.         FRAME_ICON,            icon_create(ICON_IMAGE, &icon_pixrect),
  247.         0)) == NULL) {
  248.         punt("Error creating base frame.");
  249.     }
  250.     /* create its child */
  251.     if ((canvas = window_create(base_frame, CANVAS, 
  252.         CANVAS_WIDTH,        DISPLAY_WIDTH,
  253.         CANVAS_HEIGHT,        DISPLAY_HEIGHT,
  254.         CANVAS_MARGIN,        DISPLAY_MARGIN,
  255.         CANVAS_AUTO_EXPAND,        TRUE,
  256.         CANVAS_AUTO_SHRINK,        TRUE,
  257.         CANVAS_FIXED_IMAGE,        FALSE,
  258.         CANVAS_RETAINED,        TRUE,
  259.         CANVAS_RESIZE_PROC,        canvas_resize_proc,
  260.         WIN_EVENT_PROC,        canvas_event_proc,
  261.         0)) == NULL) {
  262.         punt("Error creating canvas.");
  263.     }
  264.     /* create a menu w/ the three menu items (FILEN, MORE && QUIT) */
  265.     if ((menu = menu_create(
  266.         MENU_ITEM, MENU_STRING_ITEM,     "File ...", FILEN, 0,
  267.         MENU_ITEM, MENU_STRING_ITEM,     "More", MORE, 0,
  268.         MENU_ITEM, MENU_STRING_ITEM,     "Quit", QUIT, 0,
  269.         MENU_INITIAL_SELECTION_SELECTED,    TRUE,
  270.         MENU_INITIAL_SELECTION,             MENU_SELECTED,
  271.         0)) == NULL) {
  272.         punt("Error creating menu.");
  273.     }
  274.     /* load the bitmaps */
  275.     load_bitmaps(argc, argv);
  276.     /* start the program */
  277.     window_main_loop(base_frame);
  278. } /* end main() */
  279.