home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / motifpg2.zip / ch04 / xbitmap3.c < prev    next >
C/C++ Source or Header  |  1992-07-08  |  12KB  |  389 lines

  1. /*
  2.  * Copyright 1989, 1992 O'Reilly and Associates, Inc.
  3.  * See ../Copyright for complete rights and liability information.
  4.  */
  5.  
  6. /* 
  7.  *  xbitmap3.c - bitmap in main window with small pixmaps
  8.  *               implemented with translations and actions
  9.  */
  10.  
  11. /*
  12.  *  So that we can use fprintf:
  13.  */
  14. #include <stdio.h>
  15.  
  16. /* 
  17.  * Standard Toolkit include files:
  18.  */
  19. #include <X11/Intrinsic.h>
  20. #include <Xm/Xm.h>
  21.  
  22. /*
  23.  * Public include files for widgets used in this file.
  24.  */
  25. #include <Xm/PanedW.h>    /* paned window */
  26. #include <Xm/PushB.h>     /* push button */
  27. #include <Xm/MessageB.h>  /* message box */
  28. #include <Xm/CascadeB.h>  /* cascade button */
  29. #include <Xm/DrawingA.h>  /* drawing area */
  30. #include <Xm/RowColumn.h> /* row column */
  31. #include <Xm/Frame.h>     /* frame */
  32. #include <Xm/ScrolledW.h> /* scrolled window */
  33.  
  34. #include "BitmapEdit.h"
  35.  
  36. #define DRAWN 1
  37. #define UNDRAWN 0
  38.  
  39. struct {
  40.     GC draw_gc, undraw_gc;
  41.     Pixmap normal_bitmap, reverse_bitmap;
  42.     Widget showNormalBitmap, showReverseBitmap;
  43.     String filename;    /* filename to read and write */
  44.     Dimension pixmap_width_in_cells, pixmap_height_in_cells;
  45. } bitmap_stuff;
  46.  
  47. static void CellToggled();
  48.  
  49. /* ARGSUSED */
  50. static void
  51. RedrawSmallPicture(w, event, params, num_params)
  52. Widget w;
  53. XExposeEvent *event;
  54. String *params;
  55. Cardinal *num_params;
  56. {
  57.     Pixmap pixmap;
  58.  
  59.     if (w == bitmap_stuff.showNormalBitmap)
  60.         pixmap = bitmap_stuff.normal_bitmap;
  61.     else
  62.         pixmap = bitmap_stuff.reverse_bitmap;
  63.  
  64.     if (DefaultDepthOfScreen(XtScreen(w)) == 1)
  65.         XCopyArea(XtDisplay(w), pixmap, XtWindow(w),
  66.                 DefaultGCOfScreen(XtScreen(w)), event->x, event->y, 
  67.                 event->width, event->height, event->x, event->y);
  68.     else
  69.         XCopyPlane(XtDisplay(w), pixmap, XtWindow(w),
  70.                 DefaultGCOfScreen(XtScreen(w)), event->x, event->y, 
  71.                 event->width, event->height, event->x, event->y, 1);
  72. }
  73.  
  74. /*
  75.  * The printout routine writes the data into a standard X11 bitmap file.
  76.  */
  77. /* ARGSUSED */
  78. static void 
  79. PrintOut(widget, client_data, call_data)
  80. Widget widget;
  81. XtPointer client_data;   /* unused */
  82. XtPointer call_data;    /* unused */
  83. {
  84.     XWriteBitmapFile(XtDisplay(widget), bitmap_stuff.filename, 
  85.             bitmap_stuff.normal_bitmap,
  86.             bitmap_stuff.pixmap_width_in_cells, 
  87.             bitmap_stuff.pixmap_height_in_cells, 0, 0);
  88. }
  89.  
  90. /* 
  91.  * callback to pop up help dialog widget 
  92.  */
  93. /*ARGSUSED*/
  94. void ShowHelp(w, client_data, call_data)
  95. Widget w;
  96. XtPointer client_data;
  97. XtPointer call_data;
  98. {
  99.     Widget dialog = (Widget) client_data;
  100.     XtManageChild(dialog);
  101. }
  102.  
  103. /*
  104.  * quit button callback function
  105.  */
  106. /*ARGSUSED*/
  107. void Quit(w, client_data, call_data)
  108. Widget w;
  109. XtPointer client_data, call_data;
  110.     exit(0); 
  111. }
  112.  
  113. main(argc, argv)
  114. int argc;
  115. char **argv;
  116. {
  117.     XtAppContext app_context;
  118.     Widget topLevel, mainWindow, menuBar;
  119.     Widget fileButton, fileMenu, quit, helpButton, helpMenu, help, helpBox;
  120.     Widget temp;
  121.     Widget bigBitmap, output, smallPixmapBox;
  122.     Widget scrolledWin, frame1, frame2;
  123.  
  124.     /* never call a Widget variable "exit"! */
  125.     extern exit();
  126.  
  127.     static XrmOptionDescRec table[] = {
  128.         {"-pw",            "*pixmapWidthInCells",        XrmoptionSepArg, NULL},
  129.         {"-pixmapwidth",   "*pixmapWidthInCells",        XrmoptionSepArg, NULL},
  130.         {"-ph",            "*pixmapHeightInCells",       XrmoptionSepArg, NULL},
  131.         {"-pixmapheight",  "*pixmapHeightInCells",       XrmoptionSepArg, NULL},
  132.         {"-cellsize",      "*cellSizeInPixels",           XrmoptionSepArg, NULL},
  133.  
  134.     };
  135.     
  136.     static XtActionsRec actions[] = {
  137.         {"RedrawSmallPicture", RedrawSmallPicture},
  138.     };
  139.  
  140.     /*static char trans[] = "<Expose>: RedrawSmallPicture()";*/
  141.  
  142.     XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);
  143.  
  144.     topLevel = XtVaAppInitialize(
  145.             &app_context,       /* Application context */
  146.             "XBitmap3",     /* Application class */
  147.             table, XtNumber(table),   /* command line option list */
  148.             &argc, argv,        /* command line args */
  149.             NULL,               /* for missing app-defaults file */
  150.             NULL);              /* terminate varargs list */
  151.  
  152.     if (argv[1] != NULL)
  153.          bitmap_stuff.filename = argv[1];
  154.     else {
  155.          fprintf(stderr, "xbitmap: must specify filename on command line\n");
  156.          exit(1);
  157.     }
  158.  
  159.     /* create main window */
  160.     mainWindow = XtVaCreateManagedWidget(
  161.             "mainWindow",           /* widget name */
  162.             xmPanedWindowWidgetClass,   /* widget class */
  163.             topLevel,           /* parent widget*/
  164.             NULL);                  /* terminate varargs list */
  165.  
  166.     /* create menu bar along top inside of main window */
  167.     menuBar = XmCreateMenuBar(
  168.             mainWindow, /* parent widget*/
  169.             "menuBar",  /* widget name */
  170.             NULL,       /* no arguments needed */
  171.             0);         /* no arguments needed */
  172.     XtManageChild(menuBar);
  173.  
  174.     scrolledWin = XtVaCreateManagedWidget("scrolledWin", 
  175.             xmScrolledWindowWidgetClass, mainWindow, 
  176.             NULL);
  177.  
  178.     bigBitmap = XtVaCreateManagedWidget("bigBitmap", 
  179.             bitmapEditWidgetClass, scrolledWin, 
  180.             NULL);
  181.  
  182.     XtVaGetValues(bigBitmap,
  183.             XtNpixmapWidthInCells, &bitmap_stuff.pixmap_width_in_cells,
  184.             XtNpixmapHeightInCells, &bitmap_stuff.pixmap_height_in_cells,
  185.             NULL);
  186.  
  187.     /*
  188.      *  CREATE FILE MENU AND CHILDREN
  189.      */
  190.  
  191.     /* create button that will pop up the menu */
  192.     fileButton = XtVaCreateManagedWidget(
  193.             "fileButton",           /* widget name */
  194.             xmCascadeButtonWidgetClass, /* widget class */
  195.             menuBar,            /* parent widget*/
  196.             NULL);                  /* terminate varargs list */
  197.  
  198.     /* create menu (really a Shell widget and RowColumn widget combo) */
  199.     fileMenu = XmCreatePulldownMenu(
  200.             menuBar,    /* parent widget*/
  201.             "fileMenu", /* widget name */
  202.             NULL,       /* no argument list needed */
  203.             0);         /* no argument list needed */
  204.  
  205.     /*
  206.      *  CREATE BUTTON TO OUTPUT BITMAP
  207.      */
  208.  
  209.     /* create button that will pop up the menu */
  210.     output = XtVaCreateManagedWidget(
  211.             "output",           /* widget name */
  212.             xmPushButtonWidgetClass,    /* widget class */
  213.             fileMenu,           /* parent widget*/
  214.             NULL);                  /* terminate varargs list */
  215.  
  216.     XtAddCallback(output, XmNactivateCallback, PrintOut, 0);
  217.  
  218.     /* create the quit button up in the menu */
  219.     quit = XtVaCreateManagedWidget(
  220.             "quit",             /* widget name */
  221.             xmPushButtonWidgetClass,    /* widget class */
  222.             fileMenu,           /* parent widget*/
  223.             NULL);                  /* terminate varargs list */
  224.     
  225.     /* 
  226.      * Specify which menu fileButton will pop up.
  227.      */
  228.     XtVaSetValues(fileButton,
  229.             XmNsubMenuId, fileMenu,
  230.             NULL);
  231.  
  232.     /* arrange for quit button to call function that exits. */
  233.     XtAddCallback(quit, XmNactivateCallback, Quit, 0);
  234.  
  235.     /*
  236.      *  CREATE HELP BUTTON AND BOX
  237.      */
  238.  
  239.     /* create button that will bring up help menu */
  240.     helpButton = XtVaCreateManagedWidget( "helpButton",
  241.         xmCascadeButtonWidgetClass, menuBar, NULL);
  242.  
  243.     /* tell menuBar which is the help button (will be specially positioned) */
  244.     XtVaSetValues(menuBar,
  245.           XmNmenuHelpWidget, helpButton,
  246.           NULL);
  247.  
  248.     /* create menu (really a Shell widget and RowColumn widget combo) */
  249.     helpMenu = XmCreatePulldownMenu( menuBar,
  250.             "helpMenu", NULL, 0);
  251.  
  252.     /* create the help button up in the menu */
  253.     help = XtVaCreateManagedWidget( "help",
  254.             xmPushButtonWidgetClass, helpMenu, NULL);
  255.  
  256.     /* 
  257.      * Specify which menu helpButton will pop up.
  258.      */
  259.     XtVaSetValues(helpButton,
  260.           XmNsubMenuId, helpMenu,
  261.           NULL);
  262.  
  263.     /* create popup that will contain help */
  264.     helpBox = XmCreateMessageDialog(
  265.             help,    /* parent widget*/
  266.             "helpBox",  /* widget name   */
  267.             NULL,       /* no arguments needed */
  268.             0);         /* no arguments needed */
  269.  
  270.     temp = XmMessageBoxGetChild (helpBox, XmDIALOG_CANCEL_BUTTON);
  271.     XtUnmanageChild (temp);
  272.     temp = XmMessageBoxGetChild (helpBox, XmDIALOG_HELP_BUTTON);
  273.     XtUnmanageChild (temp);
  274.  
  275.     /* arrange for getHelp button to pop up helpBox */
  276.     XtAddCallback(help, XmNactivateCallback, ShowHelp, helpBox);
  277.  
  278.     /*
  279.      * Create box containing two small bitmaps.
  280.      */
  281.  
  282.     /* create pixmaps and GCs needed for expose strategy */
  283.     SetUpThings(topLevel);
  284.  
  285.     smallPixmapBox = XtVaCreateManagedWidget("smallPixmapBox", 
  286.             xmRowColumnWidgetClass, mainWindow, 
  287.             NULL);
  288.  
  289.     frame1 = XtVaCreateManagedWidget("frameNormal", 
  290.             xmFrameWidgetClass, smallPixmapBox, 
  291.             NULL);
  292.  
  293.     bitmap_stuff.showNormalBitmap = XtVaCreateManagedWidget("showNormalBitmap", 
  294.             xmPrimitiveWidgetClass, frame1, 
  295.             /*XmNtranslations, XtParseTranslationTable(trans),*/
  296.             XmNwidth, bitmap_stuff.pixmap_width_in_cells,
  297.             XmNheight, bitmap_stuff.pixmap_height_in_cells,
  298.             NULL);
  299.  
  300.     frame2 = XtVaCreateManagedWidget("frameReverse", 
  301.         xmFrameWidgetClass, smallPixmapBox, 
  302.         NULL);
  303.  
  304.     bitmap_stuff.showReverseBitmap = XtVaCreateManagedWidget("showReverseBitmap", 
  305.         xmPrimitiveWidgetClass, frame2, 
  306.         /*XmNtranslations, XtParseTranslationTable(trans),*/
  307.         XmNwidth, bitmap_stuff.pixmap_width_in_cells,
  308.         XmNheight, bitmap_stuff.pixmap_height_in_cells,
  309.         NULL);
  310.  
  311.     XtAddCallback(bigBitmap, XtNtoggleCallback, CellToggled, 0);
  312.  
  313.     XtAppAddActions(app_context, actions, XtNumber(actions));
  314.  
  315.  
  316.     XtRealizeWidget(topLevel);
  317.  
  318.     XtAppMainLoop(app_context);
  319.   }
  320.  
  321. SetUpThings(w)
  322. Widget w;
  323. {
  324.     XGCValues values;
  325.  
  326.  
  327.     bitmap_stuff.normal_bitmap = XCreatePixmap(XtDisplay(w), 
  328.             RootWindowOfScreen(XtScreen(w)),
  329.             bitmap_stuff.pixmap_width_in_cells, bitmap_stuff.pixmap_height_in_cells, 1);
  330.  
  331.     bitmap_stuff.reverse_bitmap = XCreatePixmap(XtDisplay(w), 
  332.             RootWindowOfScreen(XtScreen(w)),
  333.             bitmap_stuff.pixmap_width_in_cells, bitmap_stuff.pixmap_height_in_cells, 1);
  334.  
  335.     values.foreground = 1;
  336.     values.background = 0;
  337.     /* note that normal_bitmap is used as the drawable because it
  338.      * is one bit deep.  The root window may not be one bit deep */
  339.     bitmap_stuff.draw_gc = XCreateGC(XtDisplay(w),  bitmap_stuff.normal_bitmap,
  340.             GCForeground | GCBackground, &values);
  341.  
  342.     values.foreground = 0;
  343.     values.background = 1;
  344.     bitmap_stuff.undraw_gc = XCreateGC(XtDisplay(w), 
  345.             bitmap_stuff.normal_bitmap,
  346.             GCForeground | GCBackground, &values);
  347.  
  348.     /* pixmaps must be cleared - may contain garbage */
  349.     XFillRectangle(XtDisplay(w), 
  350.             bitmap_stuff.reverse_bitmap, bitmap_stuff.draw_gc,
  351.             0, 0, bitmap_stuff.pixmap_width_in_cells + 1, 
  352.             bitmap_stuff.pixmap_height_in_cells + 1);
  353.     XFillRectangle(XtDisplay(w), 
  354.             bitmap_stuff.normal_bitmap, bitmap_stuff.undraw_gc,
  355.             0, 0, bitmap_stuff.pixmap_width_in_cells + 1, 
  356.             bitmap_stuff.pixmap_height_in_cells + 1);
  357. }
  358.  
  359. /* ARGSUSED */
  360. static void
  361. CellToggled(w, client_data, call_data)
  362. Widget w;
  363. XtPointer client_data;  /* unused */
  364. XtPointer call_data;    /* will be cast to cur_info */
  365. {
  366.     /* cast pointer to needed type: */
  367.     BitmapEditPointInfo *cur_info = (BitmapEditPointInfo *) call_data;
  368.     /* 
  369.      * Note, BitmapEditPointInfo is defined in BitmapEdit.h 
  370.      */
  371.     XExposeEvent pseudo;
  372.  
  373.     XDrawPoint(XtDisplay(w), bitmap_stuff.normal_bitmap, 
  374.             ((cur_info->mode == DRAWN) ? bitmap_stuff.draw_gc : 
  375.             bitmap_stuff.undraw_gc), cur_info->newx, cur_info->newy);
  376.     XDrawPoint(XtDisplay(w), bitmap_stuff.reverse_bitmap, 
  377.             ((cur_info->mode == DRAWN) ? bitmap_stuff.undraw_gc : 
  378.             bitmap_stuff.draw_gc), cur_info->newx, cur_info->newy); 
  379.  
  380.     pseudo.x = cur_info->newx;
  381.     pseudo.y = cur_info->newy;
  382.     pseudo.width = pseudo.height = 1;
  383.  
  384.     /* call RedrawSmallPicture, passing it a simulated event */
  385.     RedrawSmallPicture(bitmap_stuff.showNormalBitmap, &pseudo);
  386.     RedrawSmallPicture(bitmap_stuff.showReverseBitmap, &pseudo);
  387. }
  388.