home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3063 / alert.c next >
Encoding:
C/C++ Source or Header  |  1991-03-15  |  2.6 KB  |  107 lines

  1. /*
  2.  *    alert.c : A popup click-to-remove message box.
  3.  *
  4.  *    George Ferguson, ferguson@cs.rochester.edu, 17 July 1990.
  5.  *
  6.  *    $Id: alert.c,v 1.1 91/02/28 11:20:47 ferguson Exp $
  7.  */
  8.  
  9. #include <X11/Intrinsic.h>
  10. #include <X11/StringDefs.h>
  11. #include <X11/Shell.h>
  12. #include <X11/Xaw/Dialog.h>
  13. #include <X11/Xaw/Cardinals.h>
  14. #include <stdio.h>
  15. #include <varargs.h>
  16. extern Widget toplevel;            /* this is the only external */
  17.  
  18. /*
  19.  * Functions defined here
  20.  */
  21. void alert();            /* main public routine */
  22. void alertOk();            /* public action procedure */
  23.  
  24. static Widget popup,alerter;
  25. static Boolean alertDone;
  26.  
  27. void
  28. alert(va_alist)
  29. va_dcl
  30. {
  31.     va_list vptr;
  32.     char prompt[BUFSIZ],*fmt;
  33.     Arg args[2];
  34.     XEvent event;
  35.     Window rwin,child;
  36.     int rx,ry,cx,cy,but,x,y;
  37.     Dimension w,h;
  38.  
  39.     if (popup == NULL) {
  40.     popup = XtCreatePopupShell("popup",transientShellWidgetClass,
  41.                             toplevel,NULL,ZERO);
  42.     alerter = XtCreateManagedWidget("alert",dialogWidgetClass,
  43.                             popup,NULL,ZERO);
  44.     XawDialogAddButton(alerter,"okButton",alertOk,NULL);
  45.     XtRealizeWidget(popup);                            
  46.     }
  47. /*
  48.  * This vsprintf() code from Dan Heller (argv@sun.com).
  49.  */
  50.     va_start(vptr);
  51.     fmt = va_arg(vptr,char *);
  52. #if defined(sun) || defined(SYSV) || defined(VPRINTF)
  53.     vsprintf(prompt, fmt, vptr);
  54. #else /* !VPRINTF */
  55.     {
  56.         /* we're on a BSD machine that has no vsprintf() */
  57.         FILE foo;
  58.         foo._cnt = BUFSIZ;
  59.         foo._base = foo._ptr = prompt; /* may have to cast(unsigned char *) */
  60.         foo._flag = _IOWRT+_IOSTRG;
  61.         (void) _doprnt(fmt, vptr, &foo);
  62.         *foo._ptr = '\0'; /* plant terminating null character */
  63.     }
  64. #endif /* VPRINTF */
  65.     va_end (vptr);
  66.  
  67.     XtSetArg(args[0],XtNlabel,prompt);
  68.     XtSetValues(alerter,args,ONE);
  69.     XtSetArg(args[0],XtNwidth,&w);
  70.     XtSetArg(args[1],XtNheight,&h);
  71.     XtGetValues(popup,args,TWO);
  72.     XQueryPointer(XtDisplay(toplevel),XtWindow(toplevel),
  73.                     &rwin,&child,&rx,&ry,&cx,&cy,&but);
  74.     x = rx-w/2;
  75.     if (x < 0)
  76.     x = 0;
  77.     else if (x > WidthOfScreen(XtScreen(toplevel))-w)
  78.     x = WidthOfScreen(XtScreen(toplevel))-w;
  79.     y = ry-h/2;
  80.     if (y < 0)
  81.     y = 0;
  82.     else if (y > HeightOfScreen(XtScreen(toplevel))-h)
  83.     y = WidthOfScreen(XtScreen(toplevel))-h;
  84.     XtSetArg(args[0],XtNx,x);
  85.     XtSetArg(args[1],XtNy,y);
  86.     XtSetValues(popup,args,TWO);
  87.  
  88.     XBell(XtDisplay(toplevel),0);
  89.     alertDone = False;
  90.     XtPopup(popup,XtGrabExclusive);
  91.     while (!alertDone) {
  92.     XtAppNextEvent(XtWidgetToApplicationContext(toplevel),&event);
  93.     XtDispatchEvent(&event);
  94.     }
  95.     XtPopdown(popup);
  96. }
  97.  
  98. void
  99. alertOk(w,event,params,num_params)
  100. Widget w;
  101. XEvent *event;
  102. String *params;
  103. Cardinal *num_params;
  104. {
  105.     alertDone = True;
  106. }
  107.