home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE0.TAR / confirm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-21  |  3.0 KB  |  124 lines

  1. /*
  2.  * confirm.c : A popup yes/no confirmer
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 21 Aug 1991.
  5.  *
  6.  */
  7.  
  8. #include <X11/Intrinsic.h>
  9. #include <X11/StringDefs.h>
  10. #include <X11/Shell.h>
  11. #include <X11/Xaw/Dialog.h>
  12. #include <X11/Xaw/Cardinals.h>
  13. #include <stdio.h>
  14. extern Widget toplevel;            /* this is the only external */
  15.  
  16. /*
  17.  * Functions defined here
  18.  */
  19. Boolean confirm0();            /* main public routines */
  20. Boolean confirm1();
  21. void confirmYes(), confirmNo();        /* public action procedures */
  22.  
  23. static Widget confirmShell,confirmDialog;
  24. static Boolean confirmDone;
  25. static Boolean confirmResult;
  26.  
  27. Boolean
  28. confirm0(str)
  29. char *str;
  30. {
  31.     Arg args[2];
  32.     XEvent event;
  33.     Window rwin,child;
  34.     int rx,ry,cx,cy,x,y;
  35.     unsigned int but;
  36.     Dimension w,h;
  37.  
  38.     /* Go synchronous or the widgets don't resize properly */
  39.     XSynchronize(XtDisplay(toplevel),True);
  40.     /* If the popup isn't created yet, then create it */
  41.     if (confirmShell == NULL) {
  42.     confirmShell = XtCreatePopupShell("confirmShell",
  43.                       transientShellWidgetClass,
  44.                       toplevel,NULL,ZERO);
  45.     confirmDialog = XtCreateManagedWidget("confirmDialog",
  46.                           dialogWidgetClass,
  47.                           confirmShell,NULL,ZERO);
  48.     XawDialogAddButton(confirmDialog,"yesButton",confirmYes,NULL);
  49.     XawDialogAddButton(confirmDialog,"noButton",confirmNo,NULL);
  50.     XtRealizeWidget(confirmShell);
  51.     }
  52.     /* Set the label */
  53.     XtSetArg(args[0],XtNlabel,str);
  54.     XtSetValues(confirmDialog,args,ONE);
  55.     /* Center the popup over the mouse */
  56.     XtSetArg(args[0],XtNwidth,&w);
  57.     XtSetArg(args[1],XtNheight,&h);
  58.     XtGetValues(confirmShell,args,TWO);
  59.     XQueryPointer(XtDisplay(toplevel),XtWindow(toplevel),
  60.                     &rwin,&child,&rx,&ry,&cx,&cy,&but);
  61.     x = rx-w/2;
  62.     if (x < 0)
  63.     x = 0;
  64.     else if (x > WidthOfScreen(XtScreen(toplevel))-w)
  65.     x = WidthOfScreen(XtScreen(toplevel))-w;
  66.     y = ry-h/2;
  67.     if (y < 0)
  68.     y = 0;
  69.     else if (y > HeightOfScreen(XtScreen(toplevel))-h)
  70.     y = WidthOfScreen(XtScreen(toplevel))-h;
  71.     XtSetArg(args[0],XtNx,x);
  72.     XtSetArg(args[1],XtNy,y);
  73.     XtSetValues(confirmShell,args,TWO);
  74.     /* Beep */
  75.     XBell(XtDisplay(toplevel),0);
  76.     /* Pop it up and block until one of the buttons is clicked */
  77.     confirmDone = False;
  78.     XtPopup(confirmShell,XtGrabExclusive);
  79.     while (!confirmDone) {
  80.     XtAppNextEvent(XtWidgetToApplicationContext(toplevel),&event);
  81.     XtDispatchEvent(&event);
  82.     }
  83.     /* Okay, pop it down */
  84.     XtPopdown(confirmShell);
  85.     /* Back to normal */
  86.     XSynchronize(XtDisplay(toplevel),False);
  87.     /* And here's the selected value */
  88.     return(confirmResult);
  89. }
  90.  
  91. Boolean
  92. confirm1(fmt,arg)
  93. char *fmt,*arg;
  94. {
  95.     char buf[256];
  96.  
  97.     sprintf(buf,fmt,arg);
  98.     return(confirm0(buf));
  99. }
  100.  
  101. /*ARGSUSED*/
  102. void
  103. confirmYes(w,event,params,num_params)
  104. Widget w;
  105. XEvent *event;
  106. String *params;
  107. Cardinal *num_params;
  108. {
  109.     confirmResult = True;
  110.     confirmDone = True;
  111. }
  112.  
  113. /*ARGSUSED*/
  114. void
  115. confirmNo(w,event,params,num_params)
  116. Widget w;
  117. XEvent *event;
  118. String *params;
  119. Cardinal *num_params;
  120. {
  121.     confirmResult = False;
  122.     confirmDone = True;
  123. }
  124.