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 / xbitmap2.c < prev    next >
C/C++ Source or Header  |  1992-07-08  |  12KB  |  384 lines

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