home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / editres / geometry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  13.3 KB  |  489 lines

  1. /*
  2.  * $XConsortium: geometry.c,v 1.13 91/04/30 15:28:16 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:  Chris D. Peterson, MIT X Consortium
  24.  */
  25.  
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #include <stdio.h>
  30.  
  31. #include <X11/Xaw/Cardinals.h>    
  32.  
  33. #include "editresP.h"
  34.  
  35. extern void SetMessage(), SetCommand(), SetAndCenterTreeNode(), AddString();
  36. extern void GetAllStrings(), InsertWidgetFromNode();
  37. extern int HandleXErrors();
  38. extern WNode * FindNode();
  39.  
  40. static WNode *FindWidgetFromWindow(), *FindWidgetFromWindowGivenNode();
  41. static void CreateFlashWidget(), FlashWidgets();
  42. static void AddToFlashList(), _AddToFlashList();
  43. static void FlashWidgetsOn(), FlashWidgetsOff(), FlashWidgetsCleanup();
  44.  
  45. /*    Function Name: _FindWidget
  46.  *    Description: Finds a widget in the tree and shows it to the user.
  47.  *    Arguments: w - any widget in the application.
  48.  *    Returns: none.
  49.  */
  50.  
  51. void 
  52. _FindWidget(w)
  53. Widget w;
  54. {
  55.     char msg[BUFSIZ];
  56.     WNode * node;
  57.     Window win, GetClientWindow();
  58.     int x, y;            /* location of event in root coordinates. */
  59.  
  60.     sprintf(msg, "Click on any widget in the client.\nEditres will %s",
  61.         "select that widget in the tree display.");
  62.  
  63.     SetMessage(global_screen_data.info_label, msg);
  64.  
  65.     if ( (win = GetClientWindow(w, &x, &y)) != None) {
  66.     node = FindWidgetFromWindow(global_tree_info, win);
  67.     if (node != NULL) {
  68.         ProtocolStream * stream = &(global_client.stream);        
  69.         
  70.         _XEditResResetStream(stream);
  71.         InsertWidgetFromNode(stream, node);
  72.         _XEditResPut16(stream, (short) x);
  73.         _XEditResPut16(stream, (short) y);
  74.         SetCommand(w, LocalFindChild, NULL);
  75.         return;
  76.     }
  77.     }
  78.  
  79.     SetMessage(global_screen_data.info_label, 
  80.       "That window does not appear to be\nin the currently displayed client.");
  81. }
  82.  
  83. /*    Function Name: FindWidgetFromWindow
  84.  *    Description: finds a widget in the current tree given its window id.
  85.  *    Arguments: tree_info - information about this tree.
  86.  *                 win - window to search for.
  87.  *    Returns: node - the node corrosponding to this widget.
  88.  */
  89.  
  90. static WNode * 
  91. FindWidgetFromWindow(tree_info, win)
  92. TreeInfo * tree_info;
  93. Window win;
  94. {
  95.     if (tree_info == NULL)
  96.     return(NULL);
  97.  
  98.     return(FindWidgetFromWindowGivenNode(tree_info->top_node, win));
  99. }
  100.  
  101. /*    Function Name: FindWidgetFromWindowGivenNode
  102.  *    Description: finds a widget in the current tree given its window id.
  103.  *    Arguments: node - current node.
  104.  *                 win - window to search for.
  105.  *    Returns: node - the node corrosponding to this widget.
  106.  */
  107.  
  108. static WNode *
  109. FindWidgetFromWindowGivenNode(node, win)
  110. WNode * node;
  111. Window win;
  112. {
  113.     int i;
  114.     WNode * ret_node;
  115.  
  116.     if (node->window == win)
  117.     return(node);
  118.  
  119.     for (i = 0; i < node->num_children; i++) {
  120.     ret_node = FindWidgetFromWindowGivenNode(node->children[i], win);
  121.     if (ret_node != NULL)
  122.         return(ret_node);
  123.     }
  124.     return(NULL);
  125. }
  126.  
  127. /*    Function Name: DisplayChild
  128.  *    Description: Displays the child node returned by the client
  129.  *    Arguments: event - the event from the client.
  130.  *    Returns: none.
  131.  */
  132.  
  133. void
  134. DisplayChild(event)
  135. Event * event;
  136. {
  137.     FindChildEvent * find_event = (FindChildEvent *) event;
  138.     WNode * node;
  139.     char msg[BUFSIZ];
  140.     void _FlashActiveWidgets();
  141.  
  142.     node = FindNode(global_tree_info->top_node, find_event->widgets.ids,
  143.             find_event->widgets.num_widgets);
  144.  
  145.     if (node == NULL) {
  146.     sprintf(msg, "Editres Internal Error: Unable to FindNode.\n");
  147.     SetMessage(global_screen_data.info_label, msg);
  148.     return;    
  149.     }
  150.  
  151.     SetAndCenterTreeNode(node);
  152.  
  153.     node = node->tree_info->top_node;
  154.  
  155.     sprintf(msg, "Widget Tree for client %s(%s).", node->name, node->class);
  156.     SetMessage(global_screen_data.info_label, msg);
  157.  
  158.     _FlashActiveWidgets(global_tree_info);
  159. }
  160.  
  161. /*    Function Name: _FlashActiveWidgets
  162.  *    Description: Highlights all active widgets in the tree.
  163.  *    Arguments: tree_info - information about the current tree.
  164.  *    Returns: none.
  165.  */
  166.  
  167. void
  168. _FlashActiveWidgets(tree_info)
  169. TreeInfo * tree_info;
  170. {
  171.     int i;
  172.     ProtocolStream * stream = &(global_client.stream);
  173.  
  174.     if (tree_info == NULL) {
  175.     SetMessage(global_screen_data.info_label,
  176.            "No widget Tree is avaliable.");
  177.     return;
  178.     }
  179.  
  180.     if (tree_info->num_nodes == 0) {
  181.     SetMessage(global_screen_data.info_label,"There are no active nodes.");
  182.     return;
  183.     }
  184.     
  185.     _XEditResResetStream(stream); 
  186.     /*
  187.      * Insert the number of widgets. 
  188.      */
  189.     _XEditResPut16(stream, (unsigned short) tree_info->num_nodes);
  190.  
  191.     for (i = 0; i < tree_info->num_nodes; i++) 
  192.     InsertWidgetFromNode(stream, global_tree_info->active_nodes[i]);
  193.  
  194.     SetCommand(tree_info->tree_widget, LocalFlashWidget, NULL);
  195. }
  196.  
  197. /*    Function Name: HandleFlashWidget
  198.  *    Description: Is called when client has returned geometry of all widget
  199.  *                   to flash.
  200.  *    Arguments: event - the event containing the client info.
  201.  *    Returns: none.
  202.  */
  203.  
  204. char *
  205. HandleFlashWidget(event)
  206. Event * event;
  207. {
  208.     GetGeomEvent * geom_event = (GetGeomEvent *) event;
  209.     char * errors = NULL;
  210.     int i;
  211.  
  212.     for (i = 0; i < (int)geom_event->num_entries; i++) 
  213.     AddToFlashList(global_tree_info, geom_event->info + i, &errors);
  214.  
  215.     FlashWidgets(global_tree_info);
  216.  
  217.     return(errors);
  218. }
  219.  
  220. /*    Function Name: AddWidgetToFlashList
  221.  *    Description: Adds a widget to the list of widget to flash.
  222.  *    Arguments: tree_info - info about this tree.
  223.  *                 geom_info - the info from the client about this widget.
  224.  *                 errors - a string containing the errors.
  225.  *    Returns: none
  226.  */
  227.  
  228. static void
  229. AddToFlashList(tree_info, geom_info, errors)
  230. TreeInfo * tree_info;
  231. GetGeomInfo * geom_info;
  232. char ** errors;
  233. {
  234.     WNode * node;
  235.     char buf[BUFSIZ];
  236.  
  237.     node = FindNode(tree_info->top_node, 
  238.             geom_info->widgets.ids, geom_info->widgets.num_widgets);
  239.  
  240.     if (node == NULL) {
  241.     sprintf(buf, "Editres Internal Error: Unable to FindNode.\n");
  242.     AddString(errors, buf); 
  243.     return;    
  244.     }
  245.  
  246.     if (geom_info->error) {
  247.     AddString(errors, geom_info->message); 
  248.     return;    
  249.     }
  250.  
  251.     if (!geom_info->visable) {
  252.     sprintf(buf, "%s(0x%lx) - This widget is not mapped\n",
  253.         node->name, node->id);
  254.     AddString(errors, buf); 
  255.     return;
  256.     }
  257.  
  258.     _AddToFlashList(tree_info, errors, node, 
  259.             geom_info->x, geom_info->y, 
  260.             geom_info->width + geom_info->border_width, 
  261.             geom_info->height + geom_info->border_width);
  262. }
  263.  
  264. /*    Function Name: _AddToFlashList
  265.  *    Description: adds the window to the current client's flash list.
  266.  *    Arguments: errors - a string to stuff any errors encountered.
  267.  *                 node - the node associated with this object.
  268.  *                 x, y - location of the flash widget in root coords.
  269.  *                 width, height - size of the flash widget.
  270.  *    Returns: none.
  271.  */
  272.  
  273. static void
  274. _AddToFlashList(tree_info, errors, node, x, y, width, height)
  275. TreeInfo * tree_info;
  276. char ** errors;
  277. WNode * node;
  278. int x, y;
  279. unsigned int width, height;
  280. {
  281.     Display * dpy = XtDisplay(tree_info->tree_widget);
  282.     Window window = (Window) node->window;
  283.     XWindowAttributes attrs;
  284.  
  285.     if (window == EDITRES_IS_OBJECT)
  286.     window = node->parent->window;
  287.  
  288.     if (window == EDITRES_IS_UNREALIZED) {
  289.     char buf[BUFSIZ];
  290.  
  291.     if (node->window == EDITRES_IS_OBJECT) 
  292.         sprintf(buf, "%s(0x%lx) - This object's parent is unrealized\n", 
  293.             node->name, node->id);        
  294.     else
  295.         sprintf(buf, "%s(0x%lx) - This widget is unrealized\n", 
  296.             node->name, node->id);
  297.  
  298.     AddString(errors, buf); 
  299.     return;
  300.     }
  301.  
  302.     global_error_code = NO_ERROR;                 /* Reset Error code. */
  303.     global_old_error_handler = XSetErrorHandler(HandleXErrors);
  304.     global_serial_num = NextRequest(dpy);
  305.  
  306.     XGetWindowAttributes(dpy, window, &attrs);
  307.  
  308.     XSync(dpy, FALSE);
  309.     XSetErrorHandler(global_old_error_handler);
  310.     if (global_error_code == NO_WINDOW) {
  311.     char buf[BUFSIZ];
  312.  
  313.     sprintf(buf, "%s(0x%lx) - This widget's window no longer exists.\n", 
  314.         node->name, node->id);
  315.     AddString(errors, buf); 
  316.     return;
  317.     }   
  318.  
  319.     if (attrs.map_state != IsViewable) {
  320.     char buf[BUFSIZ];
  321.  
  322.     sprintf(buf, "%s(0x%lx) - This widget is not mapped.\n",
  323.         node->name, node->id);
  324.     AddString(errors, buf); 
  325.     return;
  326.     }   
  327.  
  328.     CreateFlashWidget(tree_info, x, y, width, height);
  329. }
  330.  
  331. /*    Function Name: CreateFlashWidget
  332.  *    Description: Creates a widget of the size specified that
  333.  *                   will flash on the display, and adds it to the list
  334.  *                   of widgets to flash.
  335.  *    Arguments: tree_info - the tree information structure.
  336.  *                 x,y,width, height - size and location of the flash widget.
  337.  *    Returns: none.
  338.  */
  339.     
  340. #define MORE_FLASH_WIDGETS 5
  341.  
  342. static void
  343. CreateFlashWidget(tree_info, x, y, width, height)
  344. TreeInfo * tree_info;
  345. int x, y;
  346. unsigned int width, height;
  347. {
  348.     Widget shell;
  349.     Arg args[3];
  350.     Cardinal num = 0;
  351.     Dimension bw;
  352.  
  353.     XtSetArg(args[num], XtNx, x); num++;
  354.     XtSetArg(args[num], XtNy, y); num++;
  355.     XtSetArg(args[num], XtNbackground, global_resources.flash_color); num++;
  356.  
  357.     shell = XtCreatePopupShell("flash", overrideShellWidgetClass, 
  358.                    tree_info->tree_widget, args, num);
  359.  
  360.     num = 0;
  361.     XtSetArg(args[num], XtNborderWidth, &bw); num++;
  362.     XtGetValues(shell, args, num);
  363.     
  364.     bw *= 2;
  365.  
  366.     num = 0;
  367.     XtSetArg(args[num], XtNwidth, (width - bw)); num++;
  368.     XtSetArg(args[num], XtNheight, (height - bw)); num++;
  369.     XtSetValues(shell, args, num);    
  370.     
  371.     if (tree_info->num_flash_widgets + 1 > tree_info->alloc_flash_widgets) {
  372.     tree_info->alloc_flash_widgets += MORE_FLASH_WIDGETS;
  373.     tree_info->flash_widgets =
  374.         (Widget *) XtRealloc((char *)tree_info->flash_widgets,
  375.                   sizeof(Widget) * tree_info->alloc_flash_widgets);
  376.     }
  377.  
  378.     tree_info->flash_widgets[tree_info->num_flash_widgets] = shell;
  379.     tree_info->num_flash_widgets++;
  380. }
  381.  
  382. /*    Function Name: FlashWidgets
  383.  *    Description: Starts the widgets flashing.
  384.  *    Arguments: tree_info - the info about the tree (contains flash list)
  385.  *    Returns: none
  386.  */
  387.  
  388. static void
  389. FlashWidgets(tree_info)
  390. TreeInfo * tree_info;
  391. {
  392.     int i;
  393.     unsigned long wait, half_flash;
  394.     XtAppContext ac = XtWidgetToApplicationContext(tree_info->tree_widget);
  395.  
  396.     if (tree_info->flash_widgets == NULL) /* no widgets to flash. */
  397.     return;
  398.  
  399.     wait = half_flash = global_resources.flash_time/2;
  400.     for (i = 1; i < global_resources.num_flashes; i++) {
  401.     XtAppAddTimeOut(ac, wait, FlashWidgetsOff,(XtPointer)tree_info);
  402.     wait += half_flash;
  403.     XtAppAddTimeOut(ac, wait, FlashWidgetsOn,(XtPointer)tree_info);
  404.     wait += half_flash;
  405.     }
  406.  
  407.     wait += half_flash;
  408.     XtAppAddTimeOut(ac, wait, FlashWidgetsCleanup, (XtPointer)tree_info);
  409.  
  410.     FlashWidgetsOn((XtPointer) tree_info, (XtIntervalId *) NULL);
  411. }
  412.     
  413. /*    Function Name: FlashWidgetsOn
  414.  *    Description: Turns on all the Flash Widgets.
  415.  *    Arguments: info_ptr - pointer to the tree info.
  416.  *                 id - *** UNUSED ***.
  417.  *    Returns: none
  418.  */
  419.  
  420. /* ARGSUSED */
  421. static void
  422. FlashWidgetsOn(info_ptr, id)
  423. XtPointer info_ptr;
  424. XtIntervalId * id;
  425. {
  426.  
  427.     int i;
  428.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  429.     
  430.     for (i = 0; i < tree_info->num_flash_widgets; i++) {
  431.     XtRealizeWidget(tree_info->flash_widgets[i]);
  432.     XMapRaised(XtDisplay(tree_info->flash_widgets[i]),
  433.            XtWindow(tree_info->flash_widgets[i]));
  434.     }
  435. }
  436.  
  437. /*    Function Name: FlashWidgetsOff
  438.  *    Description: Turns off all the Flash Widgets.
  439.  *    Arguments: info_ptr - pointer to the tree info.
  440.  *                 id - *** UNUSED ***.
  441.  *    Returns: none
  442.  */
  443.  
  444. /* ARGSUSED */
  445. static void
  446. FlashWidgetsOff(info_ptr, id)
  447. XtPointer info_ptr;
  448. XtIntervalId * id;
  449. {
  450.     int i;
  451.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  452.     
  453.     for (i = 0; i < tree_info->num_flash_widgets; i++)
  454.     XtUnmapWidget(tree_info->flash_widgets[i]);
  455. }
  456.  
  457. /*    Function Name: FlashWidgetsCleanup
  458.  *    Description: Destroys all the Flash Widgets.
  459.  *    Arguments: info_ptr - pointer to the tree info.
  460.  *                 id - *** UNUSED ***.
  461.  *    Returns: none
  462.  */
  463.  
  464. /* ARGSUSED */
  465. static void
  466. FlashWidgetsCleanup(info_ptr, id)
  467. XtPointer info_ptr;
  468. XtIntervalId * id;
  469. {
  470.     int i;
  471.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  472.  
  473. /*
  474.  * Unmap 'em first for consistency.
  475.  */
  476.     
  477.     for (i = 0; i < tree_info->num_flash_widgets; i++)
  478.     XtUnmapWidget(tree_info->flash_widgets[i]);
  479.  
  480.     XFlush(XtDisplay(tree_info->tree_widget));
  481.  
  482.     for (i = 0; i < tree_info->num_flash_widgets; i++) 
  483.     XtDestroyWidget(tree_info->flash_widgets[i]);
  484.  
  485.     XtFree((char *)tree_info->flash_widgets);
  486.     tree_info->flash_widgets = NULL;
  487.     tree_info->num_flash_widgets = tree_info->alloc_flash_widgets = 0;
  488. }
  489.