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

  1. /*
  2.  * Copyright 1989, 1992 O'Reilly and Associates, Inc.
  3.  * See ../Copyright for complete rights and liability information.
  4.  */
  5.  
  6.  
  7. /* 
  8.  *  xmenu1.c - simple spring-loaded menu
  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 <X11/StringDefs.h>
  21.  
  22. #include <X11/Shell.h>
  23.  
  24. /*
  25.  * Public include files for widgets used in this file.
  26.  */
  27. #include <X11/Xaw/Command.h>
  28. #include <X11/Xaw/Box.h>
  29. #include <X11/Xaw/Label.h>
  30.  
  31. /*
  32.  * The popup shell ID is global because both dialog and pshell
  33.  * are needed in the dialogDone callback, and both can't be
  34.  * passed in without creating a structure.
  35.  */
  36. Widget pshell;
  37.  
  38. /*ARGSUSED*/
  39. void PlaceMenu(w, event)
  40. Widget w;
  41. XButtonEvent *event;
  42. {
  43.     /* should make sure coordinates allow menu to fit on screen */
  44.     
  45.     /* move submenu shell to slightly left and above button 
  46.      * press position */
  47.     XtVaSetValues(pshell, 
  48.             XtNx, event->x_root - 10,
  49.             XtNy, event->y_root - 10,
  50.             NULL);
  51. }
  52.  
  53. /*
  54.  * quit button callback function
  55.  */
  56. /*ARGSUSED*/
  57. void Quit(w, client_data, call_data)
  58. Widget w;
  59. XtPointer client_data, call_data;
  60.     exit(0); 
  61. }
  62.  
  63. /*
  64.  * menu pane button callback function
  65.  */
  66. /*ARGSUSED*/
  67. void PaneChosen(w, client_data, call_data)
  68. Widget w;
  69. XtPointer client_data;   /* cast to pane_number */
  70. XtPointer call_data;
  71.     int pane_number = (int) client_data;
  72.     printf("Pane %d chosen.\n", pane_number);
  73.     XtPopdown(pshell);
  74. }
  75.  
  76. main(argc, argv)
  77. int argc;
  78. char **argv;
  79. {
  80.     XtAppContext app_context;
  81.     Widget topLevel, box, quit, label, menulabel, menubox, menupane[10];
  82.     int i;
  83.     String buf[50];
  84.  
  85.     static XtActionsRec trial_actions[] = {
  86.         {"placeMenu", PlaceMenu},
  87.     };
  88.  
  89.     XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);
  90.  
  91.     topLevel = XtVaAppInitialize(
  92.         &app_context,       /* Application context */
  93.         "XMenu1",           /* application class name */
  94.         NULL, 0,            /* command line option list */
  95.         &argc, argv,        /* command line args */
  96.         NULL,               /* for missing app-defaults file */
  97.         NULL);              /* terminate varargs list */
  98.  
  99.     box = XtCreateManagedWidget(
  100.         "box",      /* widget name */
  101.         boxWidgetClass, /* widget class */
  102.         topLevel,       /* parent widget*/
  103.         NULL,       /* argument list*/
  104.         0           /* arglist size */
  105.         );
  106.  
  107.     label = XtCreateManagedWidget(
  108.         "label",            /* widget name */
  109.         labelWidgetClass,       /* widget class */
  110.         box,            /* parent widget*/
  111.         NULL,           /* argument list*/
  112.         0               /* arglist size */
  113.         );
  114.  
  115.     quit = XtCreateManagedWidget(
  116.         "quit",         /* widget name */
  117.         commandWidgetClass,     /* widget class */
  118.         box,            /* parent widget*/
  119.         NULL,           /* argument list*/
  120.         0               /* arglist size */
  121.         );
  122.  
  123.     pshell = XtCreatePopupShell(
  124.     "pshell",        /* widget name */
  125.         transientShellWidgetClass, /* widget class */
  126.     topLevel,        /* parent widget */
  127.         NULL,            /* argument list */
  128.         0            /* arglist size */
  129.         );
  130.  
  131.     menubox = XtCreateManagedWidget(
  132.         "menubox",              /* widget name   */
  133.         boxWidgetClass,         /* widget class */
  134.         pshell,                 /* parent widget*/
  135.         NULL,                   /* argument list*/
  136.         0                   /* arglist size */
  137.         );
  138.     
  139.     menulabel = XtCreateManagedWidget(
  140.         "menulabel",            /* widget name   */
  141.         labelWidgetClass,       /* widget class */
  142.         menubox,                /* parent widget*/
  143.         NULL,                   /* argument list*/
  144.         0                   /* arglist size */
  145.         );
  146.  
  147.     for (i = 0; i < 10; i++) {
  148.       sprintf(buf, "menupane%d", i);
  149.       menupane[i] = XtCreateManagedWidget(buf,   /* widget name   */
  150.                     commandWidgetClass, menubox, NULL, 0);
  151.  
  152.       XtAddCallback(menupane[i], XtNcallback, PaneChosen, i);
  153.     }
  154.  
  155.     XtAppAddActions(app_context, trial_actions, XtNumber(trial_actions));
  156.  
  157.     XtAddCallback(quit, XtNcallback, Quit, NULL);
  158.  
  159.     XtRealizeWidget(topLevel);
  160.  
  161.     XtAppMainLoop(app_context);
  162. }
  163.