home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume10 / xt-examples / part01 / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-04  |  5.4 KB  |  200 lines

  1. /***********************************************************
  2. Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute these examples for any
  7. purpose and without fee is hereby granted, provided that the above
  8. copyright notice appear in all copies and that both that copyright
  9. notice and this permission notice appear in supporting documentation,
  10. and that the name of Digital not be used in advertising or publicity
  11. pertaining to distribution of the software without specific, written
  12. prior permission.
  13.  
  14. DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
  17. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  18. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  19. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  20. OR PERFORMANCE OF THIS SOFTWARE.
  21.  
  22. ******************************************************************/
  23.  
  24. #include <X11/Intrinsic.h>
  25. #include <X11/StringDefs.h>
  26. #include <X11/Shell.h>
  27.  
  28. #include "Box.h"
  29. #include "Label.h"
  30. #include "Pushbutton.h"
  31. #include "Menu.h"
  32. #include "MenuItem.h"
  33.  
  34. void Goodbye(widget, clientData, callData)
  35.     Widget widget;
  36.     caddr_t clientData, callData;
  37. {
  38.     exit(0);
  39. }
  40.  
  41. void Widen(widget, clientData, callData)
  42.     Widget widget;
  43.     caddr_t clientData, callData;
  44. {
  45.     Widget w = (Widget) clientData;
  46.     Arg arg[25];
  47.     int n;
  48.     String label;
  49.     char buffer[100];
  50.  
  51.     n = 0;
  52.     XtSetArg(arg[n], XtNlabel, &label);    n++;
  53.  
  54.     XtGetValues(w, arg, n);
  55.     strcpy(&buffer[1], label);
  56.     buffer[0] = buffer[1];
  57.  
  58.     n = 0;
  59.     XtSetArg(arg[n], XtNlabel, buffer);    n++;
  60.     XtSetArg(arg[n], XtNwidth, 0);    n++;
  61.     XtSetArg(arg[n], XtNheight, 0);    n++;
  62.  
  63.     XtSetValues(w, arg, n);
  64. }
  65.  
  66. void Narrow(widget, clientData, callData)
  67.     Widget widget;
  68.     caddr_t clientData, callData;
  69. {
  70.     Widget w = (Widget) clientData;
  71.     Arg arg[25];
  72.     int n;
  73.     String label;
  74.     char buffer[100];
  75.  
  76.     n = 0;
  77.     XtSetArg(arg[n], XtNlabel, &label);    n++;
  78.  
  79.     XtGetValues(w, arg, n);
  80.     strcpy(buffer, &label[1]);
  81.  
  82.     n = 0;
  83.     XtSetArg(arg[n], XtNlabel, buffer);    n++;
  84.     XtSetArg(arg[n], XtNwidth, 0);    n++;
  85.     XtSetArg(arg[n], XtNheight, 0);    n++;
  86.  
  87.     XtSetValues(w, arg, n);
  88. }
  89.  
  90. void Shuffle(widget, clientData, callData)
  91.     Widget widget;
  92.     caddr_t clientData, callData;
  93. {
  94.     Widget w = (Widget) clientData;
  95.     Arg arg[25];
  96.     int n;
  97.  
  98.     n = 0;
  99.     XtSetArg(arg[n], XtNinsertBefore, 1);    n++;
  100.     XtSetValues(w, arg, n);
  101. }
  102.  
  103. Widget menushell;
  104.  
  105. static void RepositionMenu(w, event, params, num_params)
  106.     Widget w;
  107.     XEvent *event;
  108.     String *params;
  109.     Cardinal *num_params;
  110. {
  111.     XButtonEvent *b = &event->xbutton;
  112.     Dimension width, height, border;
  113.     Arg arg[25];
  114.     int n;
  115.  
  116.     if (event->xany.type != ButtonPress) return;
  117.  
  118.     XtRealizeWidget(menushell);
  119.  
  120.     n = 0;
  121.     XtSetArg(arg[n], XtNwidth, &width);        n++;
  122.     XtSetArg(arg[n], XtNheight, &height);    n++;
  123.     XtSetArg(arg[n], XtNborderWidth, &border);    n++;
  124.     XtGetValues(menushell, arg, n);
  125.  
  126.     n = 0;
  127.     XtSetArg(arg[n], XtNx, b->x_root - width/2 - border);    n++;
  128.     XtSetArg(arg[n], XtNy, b->y_root - height/2 - border);    n++;
  129.     XtSetValues(menushell, arg, n);
  130. }
  131.  
  132. XtActionsRec actions[] = {
  133.     {"RepositionMenu", RepositionMenu}
  134. };
  135.  
  136. static char *menuTranslations = 
  137.     "<Btn1Down>        : RepositionMenu() MenuPopup(menuShell) unhighlight()";
  138.  
  139.  
  140. int main(argc, argv)
  141.     unsigned int argc;
  142.     char **argv;
  143. {
  144.     Widget toplevel, label, pushbutton, box, menu, exititem, first, widen, narrow, shuffle;
  145.     Arg arg[25];
  146.     unsigned int n;
  147.     XtAppContext app;
  148.  
  149.     toplevel = XtAppInitialize(&app, "Menu",
  150.         (XrmOptionDescList) NULL, 0, &argc, argv,
  151.         (String *) NULL, (ArgList) NULL, 0);
  152.  
  153.     XtAppAddActions(app, actions, XtNumber(actions));
  154.  
  155.     box = XtCreateManagedWidget("box", boxWidgetClass, toplevel,
  156.     (Arg *) NULL, 0);
  157.  
  158.     n = 0;
  159.     XtSetArg(arg[n], XtNx, 10);    n++;
  160.     XtSetArg(arg[n], XtNy, 10);    n++;
  161.     XtSetArg(arg[n], XtNlabel, "Goodbye, world");    n++;
  162.  
  163.     label = XtCreateManagedWidget("label", labelWidgetClass, box,
  164.     arg, n);
  165.  
  166.     n = 0;
  167.     XtSetArg(arg[n], XtNx, 10); n++;
  168.     XtSetArg(arg[n], XtNy, 40); n++;
  169.     XtSetArg(arg[n], XtNlabel, "Exit menu");    n++;
  170.  
  171.     pushbutton = XtCreateManagedWidget("pushbutton", pushbuttonWidgetClass,
  172.     box, arg, n);
  173.  
  174.     XtOverrideTranslations(pushbutton,
  175.         XtParseTranslationTable(menuTranslations));
  176.  
  177.     n = 0;
  178.     XtSetArg(arg[n], XtNallowShellResize, TRUE);    n++;
  179.  
  180.     menushell = XtCreatePopupShell("menuShell",
  181.         overrideShellWidgetClass, pushbutton, arg, n);
  182.  
  183.     menu = XtCreateManagedWidget("menu", menuWidgetClass,
  184.         menushell, NULL, 0);
  185.  
  186.     first = XtCreateManagedWidget("aaaaa", menuItemWidgetClass, menu, NULL, 0);
  187.     widen = XtCreateManagedWidget("widen", menuItemWidgetClass, menu, NULL, 0);
  188.     narrow = XtCreateManagedWidget("narrow", menuItemWidgetClass, menu, NULL, 0);
  189.     shuffle = XtCreateManagedWidget("shuffle", menuItemWidgetClass, menu, NULL, 0);
  190.     exititem = XtCreateManagedWidget("exit", menuItemWidgetClass, menu, NULL, 0);
  191.  
  192.     XtAddCallback(widen, XtNcallback, Widen, first);
  193.     XtAddCallback(narrow, XtNcallback, Narrow, first);
  194.     XtAddCallback(shuffle, XtNcallback, Shuffle, first);
  195.     XtAddCallback(exititem, XtNcallback, Goodbye, NULL);
  196.  
  197.     XtRealizeWidget(toplevel);
  198.     XtAppMainLoop(app);
  199. }
  200.