home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / bitmap / Dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  5.7 KB  |  197 lines

  1. /*
  2.  * $XConsortium: Dialog.c,v 1.10 91/07/24 15:46:20 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Davor Matic, MIT X Consortium
  24.  */
  25.  
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #include <X11/Xaw/Dialog.h>
  30. #include <X11/Xaw/Command.h>
  31. #include <X11/Xmu/CharSet.h>
  32.     
  33. #include "Dialog.h"
  34.  
  35. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  36. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  37.  
  38. static void SetDialogButton();
  39.  
  40. static XtActionsRec actions_table[] = {
  41.   {"set-dialog-button", SetDialogButton},
  42. };
  43.  
  44. static DialogButton dialog_buttons[] = {
  45.     {"yes", Yes},
  46.     {"no", No},
  47.     {"maybe", Maybe},
  48.     {"okay", Okay},
  49.     {"abort", Abort},
  50.     {"cancel", Cancel},
  51.     {"retry", Retry},
  52. };
  53.  
  54. static unsigned long selected;
  55.  
  56. /* ARGSUSED */
  57. static void SetSelected(w, clientData, callData) /* ARGSUSED */
  58.      Widget w;
  59.      XtPointer clientData, callData;
  60. {
  61.     String name = (String)clientData;
  62.     int i;
  63.     
  64.     for (i = 0; i < XtNumber(dialog_buttons); i++) 
  65.     if (!strcmp(dialog_buttons[i].name, name)) 
  66.         selected |= dialog_buttons[i].flag;
  67. }
  68.  
  69. /* ARGSUSED */
  70. static void SetDialogButton(w, event, argv, argc)
  71.      Widget w;         /* not used */
  72.      XEvent *event;    /* not used */
  73.      String *argv;
  74.      Cardinal *argc;  
  75. {
  76.   char button_name[80];
  77.   XtPointer dummy = NULL;
  78.   int i;
  79.  
  80.   for (i = 0; i < *argc; i++) {
  81.     XmuCopyISOLatin1Lowered (button_name, argv[i]);
  82.     SetSelected(w, button_name, dummy);
  83.   }
  84. }
  85.  
  86. static Boolean firstTime = True;
  87.  
  88. Dialog CreateDialog(top_widget, name, options)
  89.      Widget top_widget;
  90.      String name;
  91.      unsigned long options;
  92. {
  93.     int i;
  94.     Dialog popup;
  95.  
  96.     popup = (Dialog) XtMalloc(sizeof(_Dialog));
  97.  
  98.     if (popup) {
  99.         if (firstTime) {
  100.       XtAddActions(actions_table, XtNumber(actions_table));
  101.       firstTime = False;
  102.     }
  103.     popup->top_widget = top_widget;
  104.     popup->shell_widget = XtCreatePopupShell(name, 
  105.                          transientShellWidgetClass, 
  106.                          top_widget, NULL, 0);
  107.     popup->dialog_widget = XtCreateManagedWidget("dialog", 
  108.                              dialogWidgetClass,
  109.                              popup->shell_widget, 
  110.                              NULL, 0);
  111.     for (i = 0; i < XtNumber(dialog_buttons); i++)
  112.         if (options & dialog_buttons[i].flag)
  113.         XawDialogAddButton(popup->dialog_widget, 
  114.                    dialog_buttons[i].name, 
  115.                    SetSelected, dialog_buttons[i].name);
  116.     popup->options = options;
  117.     return popup;
  118.     }
  119.     else
  120.     return NULL;
  121. }
  122.  
  123. void PopdownDialog(popup, answer)
  124.     Dialog popup;
  125.     String *answer;
  126. {
  127.     if (answer)
  128.     *answer = XawDialogGetValueString(popup->dialog_widget);
  129.     
  130.     XtPopdown(popup->shell_widget);
  131. }
  132.  
  133. unsigned long PopupDialog(popup, message, suggestion, answer, grab)
  134.     Dialog popup;
  135.     String message, suggestion, *answer;
  136.     XtGrabKind grab;
  137. {
  138.   Position popup_x, popup_y, top_x, top_y;
  139.   Dimension popup_width, popup_height, top_width, top_height, border_width;
  140.   int n;
  141.   Arg wargs[4];
  142.  
  143.   n = 0;
  144.   XtSetArg(wargs[n], XtNlabel, message); n++;
  145.   XtSetArg(wargs[n], XtNvalue, suggestion); n++;
  146.   XtSetValues(popup->dialog_widget, wargs, n);
  147.  
  148.   XtRealizeWidget(popup->shell_widget);
  149.  
  150.   n = 0;
  151.   XtSetArg(wargs[n], XtNx, &top_x); n++;
  152.   XtSetArg(wargs[n], XtNy, &top_y); n++;
  153.   XtSetArg(wargs[n], XtNwidth, &top_width); n++;
  154.   XtSetArg(wargs[n], XtNheight, &top_height); n++;
  155.   XtGetValues(popup->top_widget, wargs, n);
  156.  
  157.   n = 0;
  158.   XtSetArg(wargs[n], XtNwidth, &popup_width); n++;
  159.   XtSetArg(wargs[n], XtNheight, &popup_height); n++;
  160.   XtSetArg(wargs[n], XtNborderWidth, &border_width); n++;
  161.   XtGetValues(popup->shell_widget, wargs, n);
  162.  
  163.   popup_x = max(0, 
  164.     min(top_x + ((Position)top_width - (Position)popup_width) / 2, 
  165.         (Position)DisplayWidth(XtDisplay(popup->shell_widget), 
  166.            DefaultScreen(XtDisplay(popup->shell_widget))) -
  167.         (Position)popup_width - 2 * (Position)border_width));
  168.   popup_y = max(0, 
  169.     min(top_y + ((Position)top_height - (Position)popup_height) / 2,
  170.         (Position)DisplayHeight(XtDisplay(popup->shell_widget), 
  171.             DefaultScreen(XtDisplay(popup->shell_widget))) -
  172.         (Position)popup_height - 2 * (Position)border_width));
  173.   n = 0;
  174.   XtSetArg(wargs[n], XtNx, popup_x); n++;
  175.   XtSetArg(wargs[n], XtNy, popup_y); n++;
  176.   XtSetValues(popup->shell_widget, wargs, n);
  177.  
  178.   selected = None;
  179.  
  180.   XtPopup(popup->shell_widget, grab);
  181.   XWarpPointer(XtDisplay(popup->shell_widget), 
  182.            XtWindow(popup->top_widget),
  183.            XtWindow(popup->shell_widget), 
  184.            0, 0, top_width, top_height,
  185.            popup_width / 2, popup_height / 2);
  186.  
  187.   while ((selected & popup->options) == None) {
  188.       XEvent event;
  189.       XtNextEvent(&event);
  190.       XtDispatchEvent(&event);
  191.   }
  192.  
  193.   PopdownDialog(popup, answer);
  194.  
  195.   return (selected & popup->options);
  196. }
  197.