home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / master12.zip / mastering / pushbtns.c < prev    next >
C/C++ Source or Header  |  1992-08-21  |  10KB  |  375 lines

  1. /*
  2.  * Include File Declarations
  3.  */
  4. #include <Xm/MainW.h>
  5. #include <Xm/PushBG.h>
  6. #include <Xm/PushB.h>
  7. #include <Xm/CascadeB.h>
  8. #include <Xm/DialogS.h>
  9. #include <Xm/MessageB.h>
  10. #include <Xm/RowColumn.h>
  11. #include <Xm/Label.h>
  12. #include <Xm/Frame.h>
  13. #include <Xm/DrawingA.h>
  14. #include "bitmaps/flagup"
  15. #include "bitmaps/flagdown"
  16.  
  17. #define XBMu_BITS    flagup_bits
  18. #define XBMu_WIDTH   flagup_width
  19. #define XBMu_HEIGHT  flagup_height
  20. #define XBMd_BITS    flagdown_bits
  21. #define XBMd_WIDTH   flagdown_width
  22. #define XBMd_HEIGHT  flagdown_height
  23. #define MAX_ARGS     20
  24. #define BUFFERSZ     1000
  25.  
  26. /*
  27.  * Global Variables
  28.  */
  29.  
  30. static XmStringCharSet charset = (XmStringCharSet) XmFONTLIST_DEFAULT_TAG;
  31.  
  32. Widget  appshell, drawarea;
  33. Widget  pushbtn, pushbtng;
  34. Widget  ablabel, abglabel;
  35. Arg     args [20];
  36. int     n;
  37. Pixmap  px_up;
  38. Pixmap  px_down;
  39. Pixmap  gbr;
  40.  
  41. /*-------------------------------------------------------------
  42. ** Forward Declarations
  43. */
  44.  
  45. void   main();
  46. void   activateCB1 ();
  47. void   activateCB2 ();
  48. Widget Create_Draw_Dialog ();
  49. Widget CreateHelp ();
  50. void   HelpCB ();
  51. void   QuitCB ();
  52.  
  53. static XtArgVal GetPixel(colorstr)
  54. char *colorstr;
  55. {
  56.   XrmValue from, to;
  57.  
  58.   from.size = strlen(colorstr) +1;
  59.   if (from.size < sizeof(String)) from.size = sizeof(String);
  60.   from.addr = colorstr;
  61.   to.addr = NULL;
  62.   XtConvert(appshell, XmRString, &from, XmRPixel, &to);
  63.  
  64.   return ((XtArgVal) *((XtArgVal *) to.addr));
  65. }
  66.  
  67. /*-------------------------------------------------------------
  68. **  HelpCB      - callback for help button
  69. */
  70. void HelpCB (w, client_data, call_data) 
  71. Widget     w;            /*  widget id               */
  72. XtPointer  client_data;  /*  data from application   */
  73. XtPointer  call_data;    /*  data from widget class  */
  74. {
  75.   Widget   message_box;  /*  message box             */
  76.  
  77.   /*  Create help window. */
  78.   message_box = CreateHelp (w);
  79.  
  80.   /*  Display help window. */
  81.   XtManageChild (message_box);
  82. }
  83.  
  84. /*-------------------------------------------------------------
  85. **  CreateHelp    - create help window
  86. */
  87. Widget CreateHelp (parent) 
  88.   Widget       parent;             /*  parent widget   */
  89. {
  90.   Widget       button;
  91.   Widget       message_box;        /*  Message Dialog  */
  92.   Arg          args[MAX_ARGS];     /*  arg list        */
  93.   register int n;                  /*  arg count       */
  94.   static char  message[BUFFERSZ];  /*  help text       */
  95.   XmString     title_string = NULL;
  96.   XmString     message_string = NULL;
  97.   XmString     button_string = NULL;
  98.  
  99.   /*  Generate message to display. */
  100.   sprintf (message, "\
  101. This program uses a pushbutton and a pushbutton gadget to pop up\n\
  102. an XmDialogShell.  When you press one of the buttons, a dialog box\n\
  103. pops up with a drawing area widget that displays a canned drawing area.\n\
  104. You can display as many of these as you want.  To terminate the program,\n\
  105. press the 'File' button and then the 'Exit' button.\0");
  106.  
  107.   /* Create the compound strings */
  108.   message_string = XmStringCreateLtoR (message, charset);
  109.   button_string = XmStringCreateLtoR ("Close", charset);
  110.   title_string = XmStringCreateLtoR ("pushbtns help", charset);
  111.  
  112.  
  113.   /*  Create message box dialog. */
  114.   n = 0;
  115.   XtSetArg (args[n], XmNdialogTitle, title_string);  n++;
  116.   XtSetArg (args[n], XmNokLabelString, button_string);  n++;
  117.   XtSetArg (args[n], XmNmessageString, message_string);  n++;
  118.   message_box = XmCreateMessageDialog (parent, "helpbox", args, n);
  119.  
  120.   button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
  121.   XtUnmanageChild (button);
  122.   button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
  123.   XtUnmanageChild (button);
  124.  
  125.  
  126.   /*  Free strings and return message box. */
  127.   if (title_string) XmStringFree (title_string);
  128.   if (message_string) XmStringFree (message_string);
  129.   if (button_string) XmStringFree (button_string);
  130.   return (message_box);
  131. }
  132.  
  133.  
  134. /*-------------------------------------------------------------
  135. **  QuitCB      - callback for quit button
  136. */
  137. void QuitCB (w, client_data, call_data) 
  138. Widget     w;            /*  widget id               */
  139. XtPointer  client_data;  /*  data from application   */
  140. XtPointer  call_data;    /*  data from widget class  */
  141. {
  142.  
  143.  
  144.   /*  Terminate the application. */
  145.   exit (0);
  146. }
  147.  
  148. /***********************************************************************
  149.  ***               Create_Draw_Dialog             ***
  150.  ***********************************************************************/
  151. Widget Create_Draw_Dialog (parent)
  152.  Widget parent;
  153.  {
  154.  Widget dlog_shell;
  155.  
  156. /*
  157.  * Create a dialog shell
  158.  */ 
  159.  dlog_shell = XmCreateDialogShell (parent, "dlog_shell", NULL, 0);
  160.  
  161. /*
  162.  * Create a drawing area
  163.  */ 
  164.  
  165.  n = 0;
  166.  XtSetArg(args[n], XmNwidth, 155); n++;
  167.  XtSetArg(args[n], XmNheight, 155); n++;
  168.  drawarea = XmCreateDrawingArea (dlog_shell, "drawarea", args, n);
  169.  XtManageChild(drawarea);
  170.  
  171.  }
  172.  
  173. /***********************************************************************
  174.  ***               activateCB1             ***
  175.  ***********************************************************************/
  176. void activateCB1(w, client_data, call_data)
  177.  Widget    w;
  178.  XtPointer client_data;
  179.  XtPointer call_data;
  180.  {
  181.  Widget draw_area_dialog;
  182.  
  183.  draw_area_dialog = Create_Draw_Dialog (appshell);
  184.   
  185.  n=0;
  186.  XtSetArg(args[n], XmNbackgroundPixmap, px_up); n++;
  187.  XtSetValues (drawarea, args, n);
  188.  printf("DrawingArea background set to flag up.\n");
  189.  }
  190.  
  191. /***********************************************************************
  192.  ***               activateCB2             ***
  193.  ***********************************************************************/
  194. void activateCB2(w, client_data, call_data)
  195.  Widget    w;
  196.  XtPointer client_data;
  197.  XtPointer call_data;
  198.  {
  199.  Widget draw_area_dialog;
  200.  
  201.  draw_area_dialog = Create_Draw_Dialog (appshell);
  202.   
  203.  n=0;
  204.  XtSetArg(args[n], XmNbackgroundPixmap, px_down); n++;
  205.  XtSetValues (drawarea, args, n);
  206.  printf("DrawingArea background set to flag down.\n");
  207.  }
  208.  
  209. /***********************************************************************
  210.  ***               MAIN                ***
  211.  ***********************************************************************/
  212. void main(argc, argv)
  213.  unsigned int argc;
  214.  char **argv;
  215. {
  216.   Widget       button, cascade, frame;
  217.   Widget       rowcol, main_window, menu_bar, menu_pane;
  218.   char         *text1 = "Pushbutton";
  219.   char         *text2 = "Pushbutton Gadget";
  220.   XmString     label_text1, label_text2, pblabel, pbglabel;
  221.   XtAppContext app_context;
  222.   Display      *display;
  223.    
  224. /*
  225.  * Initialize the appshell shell widget
  226.  */
  227.   appshell = XtAppInitialize(&app_context, "Pushbtns", NULL, 0, &argc,
  228.                              argv, NULL, args, 0);
  229.  
  230. /*
  231.  * Get the display ID for the bitmap to pixmap conversion
  232.  */
  233.  display = XtDisplay (appshell);
  234.  
  235.  /* Create main window.
  236.  */
  237.  n = 0;
  238.  main_window = XmCreateMainWindow (appshell, "main1", args, n);
  239.  XtManageChild (main_window);
  240.  
  241.  /* Create menu bar in main window. */
  242.  n = 0;
  243.  menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, n); 
  244.  XtManageChild (menu_bar);
  245.  
  246.  /* Create File pulldown menu. */
  247.  n = 0;
  248.  menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
  249.  
  250.  n = 0;
  251.  button = XmCreatePushButtonGadget (menu_pane, "Exit", args, n);
  252.  XtManageChild (button);
  253.  XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
  254.  
  255.  n = 0;
  256.  XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
  257.  cascade = XmCreateCascadeButton (menu_bar, "File", args, n);
  258.  XtManageChild (cascade);
  259.  
  260.  /* Create Help button. */
  261.  n = 0;
  262.  cascade = XmCreateCascadeButton (menu_bar, "Help", args, n);
  263.  XtManageChild (cascade);
  264.  XtAddCallback (cascade, XmNactivateCallback, HelpCB, NULL);
  265.  
  266.  n = 0;
  267.  XtSetArg (args[n], XmNmenuHelpWidget, cascade); n++;
  268.  XtSetValues (menu_bar, args, n);
  269.  
  270. /*
  271.  * Create a frame widget
  272.  */
  273.  
  274.  n = 0;
  275.  XtSetArg(args[n], XmNmarginWidth, 15); n++;
  276.  XtSetArg(args[n], XmNmarginHeight, 15); n++;
  277.  frame = XmCreateFrame (main_window, "frame", args, n);
  278.  XtManageChild(frame);
  279.  
  280. /*  Set main window areas  */
  281.  XmMainWindowSetAreas (main_window, menu_bar, NULL, NULL, NULL, frame);
  282.  
  283. /*
  284.  * Get pixmaps from bitmaps
  285.  */
  286.  
  287.   px_up =XCreatePixmapFromBitmapData(display, 
  288.     DefaultRootWindow(display),
  289.     XBMu_BITS, XBMu_WIDTH,XBMu_HEIGHT,
  290.     GetPixel("red"),
  291.     GetPixel("white"), 
  292.     DefaultDepth(display,DefaultScreen(display)));
  293.  
  294.   px_down = XCreatePixmapFromBitmapData(display, 
  295.     DefaultRootWindow(display),
  296.     XBMd_BITS, XBMd_WIDTH,XBMd_HEIGHT,
  297.     GetPixel("red"),
  298.     GetPixel("white"), 
  299.     DefaultDepth(display,DefaultScreen(display)));
  300.  
  301. /*
  302.  * Create a row column manager
  303. */
  304.   n = 0;
  305.   XtSetArg(args[n], XmNnumColumns, 2); n++;
  306.   XtSetArg(args[n], XmNpacking, XmPACK_COLUMN); n++;
  307.   XtSetArg(args[n], XmNspacing, 5); n++;
  308.   rowcol = XmCreateRowColumn(frame, "rowcol", args, n);
  309.   XtManageChild(rowcol);
  310.   
  311. /*
  312.  * Set up compound strings for the label widgets and button labels
  313.  */
  314.   label_text1 = XmStringCreateLtoR(text1, XmFONTLIST_DEFAULT_TAG);
  315.   label_text2 = XmStringCreateLtoR(text2, XmFONTLIST_DEFAULT_TAG);
  316.   pblabel = XmStringCreateLtoR("FLAG UP", XmFONTLIST_DEFAULT_TAG);
  317.   pbglabel = XmStringCreateLtoR("FLAG DOWN", XmFONTLIST_DEFAULT_TAG);
  318.   
  319. /*
  320.  * Create a label widget for the push button
  321. */
  322.   n = 0;
  323.   XtSetArg(args[n], XmNlabelString, label_text1); n++;
  324.   ablabel = XmCreateLabel(rowcol, "ablabel", args, n);
  325.   XtManageChild(ablabel);
  326.   XmStringFree (label_text1);
  327.   
  328. /*
  329.  * Create a push button
  330. */
  331.   n = 0;
  332.   XtSetArg(args[n], XmNlabelString, pblabel); n++;
  333.   pushbtn = XmCreatePushButton(rowcol, "pushbtn", args, n);
  334.   XtManageChild(pushbtn);
  335.   XmStringFree (pblabel);
  336.   
  337. /*
  338.  * Add Callback for this button
  339.  */ 
  340.  XtAddCallback (pushbtn, XmNactivateCallback, activateCB1, NULL);
  341.  
  342. /*
  343.  * Create a label widget for the pushbutton gadget
  344. */
  345.   n = 0;
  346.   XtSetArg(args[n], XmNlabelString, label_text2); n++;
  347.   abglabel = XmCreateLabel(rowcol, "abglabel", args, n);
  348.   XtManageChild(abglabel);
  349.   XmStringFree (label_text2);
  350.   
  351. /*
  352.  * Create a pushbutton gadget
  353. */
  354.   n = 0;
  355.   XtSetArg(args[n], XmNlabelString, pbglabel); n++;
  356.   pushbtng = XmCreatePushButtonGadget (rowcol, "pushbtng", args, n);
  357.   XtManageChild(pushbtng);
  358.   XmStringFree (pbglabel);
  359.  
  360. /*
  361.  * Add Callback for this button
  362.  */ 
  363.  XtAddCallback (pushbtng, XmNactivateCallback, activateCB2, NULL);
  364.  
  365. /*
  366.  * Realize the appshell widget, which displays all children
  367.  */ 
  368.   XtRealizeWidget(appshell);
  369.  
  370. /*
  371.  * Go into a loop and wait for input
  372.  */
  373.   XtAppMainLoop(app_context);
  374. }
  375.