home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / editres / handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  24.5 KB  |  879 lines

  1. /*
  2.  * $XConsortium: handler.c,v 1.26 92/02/11 11:45:31 dave 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.  
  24. #ifdef MSDOS
  25. #include "X11/Intrinsc.h"      /* QDK 05/11/1994 12:54pm. */
  26. #else
  27. #include "X11/Intrinsic.h"
  28. #endif
  29. #include <X11/StringDefs.h>
  30.  
  31. #include <X11/Xaw/Cardinals.h>
  32. #include <X11/Xaw/List.h>
  33. #include <X11/Xaw/Panner.h>
  34. #include <X11/Xaw/Toggle.h>
  35. #include <X11/Xfuncs.h>
  36. #include <X11/Xos.h>        /* for W_OK def */
  37.  
  38. #include <stdio.h>
  39.  
  40. #include "editresP.h"
  41.  
  42. /* 
  43.  * Function Definitions.
  44.  */
  45.  
  46. extern void SetCommand(), PopupSetValues(), SetAndCenterTreeNode();
  47. extern void _TreeSelect(), _TreeRelabel(), _TreeActivate(), SetMessage();
  48. extern void _FlashActiveWidgets(), _DumpTreeToFile(), _PopupFileDialog();
  49. extern void AddString(), CreateResourceBox(), ExecuteOverAllNodes();
  50. extern void GetNamesAndClasses(), TreeToggle(), InsertWidgetFromNode();
  51. extern Boolean CheckDatabase();
  52. extern XrmQuarkList Quarkify();
  53. extern char *GetResourceValueForSetValues();
  54.  
  55. void SetResourceString(), ActivateResourceWidgets();
  56. void ActivateWidgetsAndSetResourceString();
  57. static void SetOnlyMatchingWidgets();
  58. static void CreateSetValuesCommand();
  59.  
  60. extern Widget toplevel;
  61.  
  62. /*    Function Name: Quit
  63.  *    Description: This function prints a message to stdout.
  64.  *    Arguments: w - ** UNUSED **
  65.  *                 call_data - ** UNUSED **
  66.  *                 client_data - ** UNUSED **
  67.  *    Returns: none
  68.  */
  69.  
  70. /* ARGSUSED */
  71. void
  72. Quit(w, client_data, call_data)
  73. Widget w;
  74. XtPointer call_data, client_data;
  75. {
  76.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  77.     exit(0);
  78. }
  79.  
  80. /*    Function Name: SendTree
  81.  *    Description: This function initiates the client communication.
  82.  *    Arguments: w - the widget that made the selection.
  83.  *                 value - a boolean value stored as a pointer.
  84.  *                         if True then get a new client, otherwise
  85.  *                         refresh the current client.
  86.  *                 call_data - ** UNUSED **
  87.  *    Returns: none
  88.  */
  89.  
  90. /* ARGSUSED */
  91. void
  92. SendTree(w, value, call_data)
  93. Widget w;
  94. XtPointer value, call_data;
  95. {
  96.     if ((Boolean) value)
  97.     global_client.window = None;
  98.  
  99.     if (!XtIsWidget(w))     /* Make sure that we use a "Real" widget here. */
  100.     w = XtParent(w);
  101.  
  102.     _XEditResResetStream(&(global_client.stream)); /* an empty message. */
  103.     SetCommand(w, LocalSendWidgetTree, NULL);
  104. }
  105.  
  106. /*    Function Name: FindWidget
  107.  *    Description: Maps a widget in the client to one in the currently
  108.  *                   displayed widget tree.
  109.  *    Arguments: w - the widget that invoked this action.
  110.  *                 call_data, client_data ** UNUSED **
  111.  *    Returns: none
  112.  */
  113.  
  114. /* ARGSUSED */
  115. void
  116. FindWidget(w, client_data, call_data)
  117. Widget w;
  118. XtPointer client_data, call_data;
  119. {
  120.     void _FindWidget();
  121.  
  122.     _FindWidget(XtParent(w));    /* Use parent since it is a "real"
  123.                    widget not a rect_obj. */
  124. }
  125.  
  126. /*    Function Name: InitSetValues
  127.  *    Description: This function pops up the setvalues dialog
  128.  *    Arguments: w - the widget caused this action.
  129.  *                 call_data - ** UNUSED **
  130.  *                 client_data - ** UNUSED **
  131.  *    Returns: none
  132.  */
  133.  
  134. /* ARGSUSED */
  135. void
  136. InitSetValues(w, client_data, call_data)
  137. Widget w;
  138. XtPointer call_data, client_data;
  139. {
  140.     if (!XtIsWidget(w))     /* Make sure that we use a "Real" widget here. */
  141.     w = XtParent(w);
  142.  
  143.     PopupSetValues(w, NULL);
  144. }
  145.  
  146. /*    Function Name: TreeSelect
  147.  *    Description: Selects all widgets.
  148.  *    Arguments: w - the widget caused this action.
  149.  *                 call_data - ** UNUSED **
  150.  *                 client_data - The type of thing to select.
  151.  *    Returns: none
  152.  */
  153.  
  154. /* ARGSUSED */
  155. void
  156. TreeSelect(w, client_data, call_data)
  157. Widget w;
  158. XtPointer call_data, client_data;
  159. {
  160.     SelectTypes type = (SelectTypes) client_data;
  161.  
  162.     _TreeSelect(global_tree_info, type);
  163. }
  164.  
  165. /*    Function Name: TreeRelabel
  166.  *    Description: Relabels a tree to the type specified.
  167.  *    Arguments: w - the widget caused this action.
  168.  *                 call_data - ** UNUSED **
  169.  *                 client_data - the type of label to assign to each node.
  170.  *    Returns: none
  171.  */
  172.  
  173. /* ARGSUSED */
  174. void
  175. TreeRelabel(w, client_data, call_data)
  176. Widget w;
  177. XtPointer call_data, client_data;
  178. {
  179.     LabelTypes type = (LabelTypes) client_data;
  180.  
  181.     _TreeRelabel(global_tree_info, type);
  182. }
  183.  
  184. /*    Function Name: PannerCallback
  185.  *    Description: called when the panner has moved.
  186.  *    Arguments: panner - the panner widget.
  187.  *                 closure - *** NOT USED ***.
  188.  *                 report_ptr - the panner record.
  189.  *    Returns: none.
  190.  */
  191.  
  192. /* ARGSUSED */
  193. void 
  194. PannerCallback(w, closure, report_ptr)
  195. Widget w;
  196. XtPointer closure, report_ptr;
  197. {
  198.     Arg args[2];
  199.     XawPannerReport *report = (XawPannerReport *) report_ptr;
  200.  
  201.     if (global_tree_info == NULL) 
  202.     return;
  203.  
  204.     XtSetArg (args[0], XtNx, -report->slider_x);
  205.     XtSetArg (args[1], XtNy, -report->slider_y);
  206.  
  207.     XtSetValues(global_tree_info->tree_widget, args, TWO);
  208. }
  209.  
  210. /*    Function Name: PortholeCallback
  211.  *    Description: called when the porthole or its child has
  212.  *                   changed 
  213.  *    Arguments: porthole - the porthole widget.
  214.  *                 panner_ptr - the panner widget.
  215.  *                 report_ptr - the porthole record.
  216.  *    Returns: none.
  217.  */
  218.  
  219. /* ARGSUSED */
  220. void 
  221. PortholeCallback(w, panner_ptr, report_ptr)
  222. Widget w;
  223. XtPointer panner_ptr, report_ptr;
  224. {
  225.     Arg args[10];
  226.     Cardinal n = 0;
  227.     XawPannerReport *report = (XawPannerReport *) report_ptr;
  228.     Widget panner = (Widget) panner_ptr;
  229.  
  230.     XtSetArg (args[n], XtNsliderX, report->slider_x); n++;
  231.     XtSetArg (args[n], XtNsliderY, report->slider_y); n++;
  232.     if (report->changed != (XawPRSliderX | XawPRSliderY)) {
  233.     XtSetArg (args[n], XtNsliderWidth, report->slider_width); n++;
  234.     XtSetArg (args[n], XtNsliderHeight, report->slider_height); n++;
  235.     XtSetArg (args[n], XtNcanvasWidth, report->canvas_width); n++;
  236.     XtSetArg (args[n], XtNcanvasHeight, report->canvas_height); n++;
  237.     }
  238.     XtSetValues (panner, args, n);
  239. }
  240.  
  241. /*    Function Name: FlashActiveWidgets
  242.  *    Description: called to flass all active widgets in the display.
  243.  *    Arguments: *** NOT USED ***
  244.  *    Returns: none.
  245.  */
  246.  
  247. /* ARGSUSED */
  248. void 
  249. FlashActiveWidgets(w, junk, garbage)
  250. Widget w;
  251. XtPointer junk, garbage;
  252. {
  253.     _FlashActiveWidgets(global_tree_info);
  254. }
  255.  
  256. /*    Function Name: GetResourceList
  257.  *    Description: Gets the resources lists of all active widgets.
  258.  *    Arguments: ** NOT USED **
  259.  *    Returns: none
  260.  */
  261.  
  262. /* ARGSUSED */
  263. void
  264. GetResourceList(w, junk, garbage)
  265. Widget w;
  266. XtPointer junk, garbage;
  267. {
  268.     WNode * node;
  269.     ProtocolStream * stream = &(global_client.stream);
  270.  
  271.     if (global_tree_info == NULL) {
  272.     SetMessage(global_screen_data.info_label,
  273.            "No widget Tree is avaliable.");
  274.     return;
  275.     }
  276.  
  277.     if (global_tree_info->num_nodes != 1) {
  278.     SetMessage(global_screen_data.info_label,
  279.           "This function requires exactly one (1) widget to be selected.");
  280.     return;
  281.     }
  282.  
  283.     node = global_tree_info->active_nodes[0];
  284.     if (node->resources != NULL) {
  285.     char * errors = NULL;
  286.     CreateResourceBox(node, &errors);
  287.     if (errors != NULL) {
  288.         SetMessage(global_screen_data.info_label, errors);
  289.         XtFree(errors);
  290.     }
  291.     return;
  292.     }
  293.  
  294.     /*
  295.      * No resoruces, fetch them from the client.
  296.      */
  297.  
  298.     _XEditResResetStream(stream); 
  299.     _XEditResPut16(stream, (unsigned short) 1);
  300.     InsertWidgetFromNode(stream, node);
  301.     SetCommand(global_tree_info->tree_widget, LocalGetResources, NULL);
  302. }
  303.  
  304. /*    Function Name: DumpTreeToFile
  305.  *    Description: Dumps all widgets in the tree to a file.
  306.  *    Arguments: w - the widget that activated this callback.
  307.  *                 junk, garbage - ** NOT USED **.
  308.  *    Returns: none.
  309.  */
  310.  
  311. /* ARGSUSED */
  312. void 
  313. DumpTreeToFile(w, junk, garbage)
  314. Widget w;
  315. XtPointer junk, garbage;
  316. {
  317.     _PopupFileDialog(XtParent(w), "Enter the filename:", "",
  318.              _DumpTreeToFile, (XtPointer) global_tree_info);
  319. }
  320.  
  321. /************************************************************
  322.  * 
  323.  * Callbacks for the Resource Box.
  324.  *
  325.  ************************************************************/
  326.  
  327.  
  328. /*    Function Name: AnyChosen
  329.  *    Description: Callback that is called when the "any" widget 
  330.  *                   is activated.
  331.  *    Arguments: w - the "any" widget that activated this callback.
  332.  *                 any_info_ptr - pointer to struct containing 
  333.  *                                dot and star widgets to lock.
  334.  *                 state_ptr - state of the any toggle.
  335.  *    Returns: none.
  336.  */
  337.  
  338. /* ARGSUSED */
  339. void 
  340. AnyChosen(w, any_info_ptr, state_ptr)
  341. Widget w;
  342. XtPointer any_info_ptr, state_ptr;
  343. {
  344.     AnyInfo * any_info = (AnyInfo *) any_info_ptr;
  345.     Boolean state = (Boolean) state_ptr;
  346.     Arg args[1];
  347.  
  348.     if (state) {
  349.  
  350.     if (any_info->left_count == 0) {
  351.         XtSetSensitive(any_info->left_dot, FALSE);
  352.         XtSetSensitive(any_info->left_star, FALSE);
  353.  
  354.         XtSetArg(args[0], XtNstate, TRUE);
  355.         XtSetValues(any_info->left_star, args, ONE);
  356.     }
  357.  
  358.     if ((any_info->right_count == NULL)||(*any_info->right_count == 0)) {
  359.         XtSetSensitive(any_info->right_dot, FALSE);
  360.         XtSetSensitive(any_info->right_star, FALSE);
  361.  
  362.         XtSetArg(args[0], XtNstate, TRUE);
  363.         XtSetValues(any_info->right_star, args, ONE);
  364.     }
  365.     any_info->left_count++;
  366.  
  367.     if (any_info->right_count != NULL)
  368.         (*any_info->right_count)++;
  369.     }
  370.     else {            /* state == 0 */
  371.     if (any_info->left_count > 0) 
  372.         any_info->left_count--;
  373.     if ((any_info->right_count != NULL)&&(*any_info->right_count > 0)) 
  374.         (*any_info->right_count)--;
  375.  
  376.     if (any_info->left_count == 0) {
  377.         XtSetSensitive(any_info->left_dot, TRUE);
  378.         XtSetSensitive(any_info->left_star, TRUE);
  379.  
  380.         XtSetArg(args[0], XtNstate, TRUE);
  381.         XtSetValues(any_info->left_dot, args, ONE);
  382.     }
  383.  
  384.     if ((any_info->right_count == NULL)||(*any_info->right_count == 0)) {
  385.         XtSetSensitive(any_info->right_dot, TRUE);
  386.         XtSetSensitive(any_info->right_star, TRUE);
  387.  
  388.         XtSetArg(args[0], XtNstate, TRUE);
  389.         XtSetValues(any_info->right_dot, args, ONE);
  390.     }
  391.     }
  392.     SetResourceString(NULL, (XtPointer) any_info->node, NULL);
  393.     ActivateResourceWidgets(NULL, (XtPointer) any_info->node, NULL);
  394. }
  395.  
  396. /*    Function Name: GetResourceName
  397.  *    Description: Gets the name of the current resource.
  398.  *    Arguments: res_box - the resource box.
  399.  *    Returns: the name of the currently selected resource.
  400.  */
  401.  
  402.  
  403. static char *
  404. GetResourceName(res_box)
  405. ResourceBoxInfo * res_box;
  406. {
  407.     XawListReturnStruct * list_info;
  408.     char * result;
  409.     
  410.     list_info = XawListShowCurrent(res_box->norm_list);
  411.     if ((list_info->list_index == XAW_LIST_NONE) && 
  412.     (res_box->cons_list != NULL)) {
  413.     list_info = XawListShowCurrent(res_box->cons_list);
  414.     }
  415.  
  416.     if (list_info->list_index == XAW_LIST_NONE) 
  417.     result = "unknown";
  418.     else
  419.     result = list_info->string;    
  420.  
  421.     return(result);
  422. }
  423.  
  424.  
  425. /*    Function Name: ActivateWidgetsAndSetResourceString
  426.  *    Description: Sets the new resources string, then
  427.  *                   activates all widgets that match this resource,
  428.  *    Arguments: w - the widget that activated this.
  429.  *                 node_ptr - the node that owns this resource box.
  430.  *                 call_data - passed on to other callbacks.
  431.  *    Returns: none.
  432.  *
  433.  * NOTE: I cannot just have two callback routines, since I care which
  434.  *       order that these are executed in, sigh...
  435.  */
  436.  
  437. void
  438. ActivateWidgetsAndSetResourceString(w, node_ptr, call_data)
  439. Widget w;
  440. XtPointer node_ptr, call_data;
  441. {
  442.     SetResourceString(w, node_ptr, call_data);
  443.     ActivateResourceWidgets(w, node_ptr, call_data);
  444. }
  445.  
  446. /*    Function Name: SetResourceString
  447.  *    Description: Sets the resource label to correspond to the currently
  448.  *                   chosen string.
  449.  *    Arguments: w - The widget that invoked this callback, or NULL.
  450.  *                 node_ptr - pointer to widget node contating this res box.
  451.  *                 call_data - The call data for the action that invoked
  452.  *                             this callback.
  453.  *    Returns: none.
  454.  */
  455.  
  456. void
  457. SetResourceString(w, node_ptr, junk)
  458. Widget w;
  459. XtPointer node_ptr, junk;
  460. {
  461.     static char * malloc_string; /* These are both inited to zero. */
  462.     static Cardinal malloc_size;
  463.  
  464.     WNode * node = (WNode *) node_ptr;
  465.     ResourceBoxInfo * res_box = node->resources->res_box;
  466.     char * temp, buf[BUFSIZ * 10];    /* here's hoping it's big enough. */
  467.     NameInfo * name_node = res_box->name_info;
  468.     Arg args[1];
  469.     int len;
  470.  
  471.     if ((w != NULL) && XtIsSubclass(w, toggleWidgetClass)) {
  472.     /*
  473.      * Only set resources when toggles are activated, not when they are
  474.      * deactivated. 
  475.      */
  476.     if (!((Boolean) junk))
  477.         return;
  478.     }
  479.  
  480.     buf[0] = '\0';        /* clear out string. */
  481.  
  482.     /*
  483.      * Get the widget name/class info.
  484.      */
  485.  
  486.     if ((temp = (char *) XawToggleGetCurrent(name_node->sep_leader)) != NULL)
  487.     strcat(buf, temp);
  488.  
  489.     for ( ; name_node->next != NULL ; name_node = name_node->next) {
  490.     temp = (char *) XawToggleGetCurrent(name_node->name_leader);
  491.     if ( (temp != NULL) && !streq(temp, ANY_RADIO_DATA) ) {
  492.         strcat(buf, temp);
  493.         temp = (char *) XawToggleGetCurrent(name_node->next->sep_leader);
  494.         if (temp == NULL) 
  495.         strcat(buf, "!");
  496.         else
  497.         strcat(buf, temp);
  498.     }
  499.     }
  500.         
  501.     strcat(buf, GetResourceName(res_box));
  502.     len = strlen(buf) + 2; /* Leave space for ':' and '\0' */
  503.  
  504. #ifdef notdef
  505.     XtSetArg(args[0], XtNstring, &temp);
  506.     XtGetValues(res_box->value_wid, args, ONE);
  507.     len += strlen(temp);
  508. #endif
  509.  
  510.     if (len > malloc_size) {
  511.     malloc_string = XtRealloc(malloc_string, sizeof(char) * len);
  512.     malloc_size = len;
  513.     }
  514.     
  515.     strcpy(malloc_string, buf);
  516.     strcat(malloc_string, ":");
  517. #ifdef notdef
  518.     strcat(malloc_string, temp);
  519. #endif
  520.  
  521.     XtSetArg(args[0], XtNlabel, malloc_string);
  522.     XtSetValues(res_box->res_label, args, ONE);
  523. }
  524.     
  525. /*    Function Name: ResourceListCallback
  526.  *    Description: Callback functions for the resource lists.
  527.  *    Arguments: list - the list widget that we are dealing with.
  528.  *                 node_ptr - pointer to widget node contating this res box.
  529.  *                 junk - UNUSED.
  530.  *    Returns: none
  531.  */
  532.  
  533. void
  534. ResourceListCallback(list, node_ptr, junk)
  535. Widget list;
  536. XtPointer node_ptr, junk;
  537. {
  538.     Widget o_list;
  539.     WNode * node = (WNode *) node_ptr;
  540.     ResourceBoxInfo * res_box = node->resources->res_box;
  541.  
  542.     if (list == res_box->norm_list) 
  543.     o_list = res_box->cons_list;
  544.     else
  545.     o_list = res_box->norm_list;
  546.  
  547.     if (o_list != NULL)
  548.     XawListUnhighlight(o_list);
  549.  
  550.     SetResourceString(list, node_ptr, junk);
  551. }
  552.  
  553. /*    Function Name: PopdownResBox
  554.  *    Description: Pops down the resource box.
  555.  *    Arguments: w - UNUSED
  556.  *                 shell_ptr - pointer to the shell to pop down.
  557.  *                 junk - UNUSED.
  558.  *    Returns: none
  559.  */
  560.  
  561. /* ARGSUSED */
  562. void
  563. PopdownResBox(w, shell_ptr, junk)
  564. Widget w;
  565. XtPointer shell_ptr, junk;
  566. {
  567.     Widget shell = (Widget) shell_ptr;
  568.  
  569.     XtPopdown(shell);
  570.     XtDestroyWidget(shell);
  571. }
  572.  
  573. /*    Function Name: _AppendResourceString
  574.  *    Description: Actually append the resource string to your resoruce file.
  575.  *    Arguments: w - UNUSED
  576.  *                 res_box_ptr - the resource box info.
  577.  *                 filename_ptr - a pointer to the filename;
  578.  *    Returns: none
  579.  */
  580.  
  581. /* ARGSUSED */
  582. static void
  583. _AppendResourceString(w, res_box_ptr, filename_ptr)
  584. Widget w;
  585. XtPointer res_box_ptr, filename_ptr;
  586. {
  587.     Arg args[1];
  588.     FILE * fp;
  589.     char buf[BUFSIZ], * resource_string, *filename = (char *) filename_ptr;
  590.     ResourceBoxInfo * res_box = (ResourceBoxInfo *) res_box_ptr;
  591.     char *value_ptr;
  592.  
  593.     if (filename != NULL) {
  594.     if (global_resources.allocated_save_resources_file) 
  595.         XtFree(global_resources.save_resources_file);
  596.     else
  597.         global_resources.allocated_save_resources_file = TRUE;
  598.     
  599.     global_resources.save_resources_file = XtNewString(filename);
  600.     }
  601.  
  602.     if ((fp = fopen(global_resources.save_resources_file, "a+")) == NULL) {
  603.     sprintf(buf, "Unable to open this file for writing, would %s",
  604.         "you like To try again?");
  605.     _PopupFileDialog(toplevel ,buf,
  606.             global_resources.save_resources_file,
  607.             _AppendResourceString, res_box_ptr);
  608.     return;
  609.     }
  610.  
  611.     XtSetArg(args[0], XtNlabel, &resource_string);
  612.     XtGetValues(res_box->res_label, args, ONE);
  613.  
  614.     XtSetArg(args[0], XtNstring, &value_ptr);
  615.     XtGetValues(res_box->value_wid, args, ONE);
  616.  
  617.     fprintf(fp, "%s %s\n", resource_string, value_ptr);
  618.  
  619.     fclose(fp);
  620. }
  621.  
  622. /*    Function Name: SaveResource
  623.  *    Description: Save the current resource to your resource file
  624.  *    Arguments: w - any widget in the application.
  625.  *                 res_box_ptr - the resource box info.
  626.  *                 junk - UNUSED.
  627.  *    Returns: none
  628.  */
  629.  
  630. /* ARGSUSED */
  631. void
  632. SaveResource(w, res_box_ptr, junk)
  633. Widget w;
  634. XtPointer res_box_ptr, junk;
  635. {
  636.     /* 
  637.      * If there is no filename the ask for one, otherwise just save to
  638.      * current file.
  639.      */
  640.  
  641.     if (streq(global_resources.save_resources_file, ""))
  642.     _PopupFileDialog(XtParent(w), "Enter file to dump resources into:",
  643.              global_resources.save_resources_file,
  644.              _AppendResourceString, res_box_ptr);
  645.     else
  646.     _AppendResourceString(w, res_box_ptr, NULL);
  647. }
  648.  
  649. /*    Function Name: _SetResourcesFile
  650.  *    Description: Sets the filename of the file to save the resources to.
  651.  *    Arguments: w - UNUSED
  652.  *                 junk - UNUSED
  653.  *                 filename_ptr - a pointer to the filename;
  654.  *    Returns: none
  655.  */
  656.  
  657. /* ARGSUSED */
  658. static void
  659. _SetResourcesFile(w, junk, filename_ptr)
  660. Widget w;
  661. XtPointer junk, filename_ptr;
  662. {
  663.     char buf[BUFSIZ], *filename = (char *) filename_ptr;
  664.  
  665.     if (global_resources.allocated_save_resources_file) 
  666.     XtFree(global_resources.save_resources_file);
  667.     else
  668.     global_resources.allocated_save_resources_file = TRUE;
  669.  
  670.     global_resources.save_resources_file = XtNewString(filename);
  671. }
  672.  
  673. /*    Function Name: SetFile
  674.  *    Description: Changes the current save file
  675.  *    Arguments: w - UNUSED.
  676.  *                 res_box_ptr - UNUSED.
  677.  *                 junk - UNUSED.
  678.  *    Returns: none
  679.  */
  680.  
  681. /* ARGSUSED */
  682. void
  683. SetFile(w, junk, garbage)
  684. Widget w;
  685. XtPointer junk, garbage;
  686. {
  687.     /* 
  688.      * If there is no filename the ask for one, otherwise just save to
  689.      * current file.
  690.      */
  691.  
  692.     _PopupFileDialog(XtParent(w), "Enter file to dump resources into:",
  693.              global_resources.save_resources_file,
  694.              _SetResourcesFile, NULL);
  695. }
  696.  
  697. /*    Function Name: ApplyResource
  698.  *    Description: Apply the current resource to the running application.
  699.  *    Arguments: w - any widget in the application.
  700.  *                 node_ptr - a pointer to the node containing 
  701.  *                            the current resouce box.
  702.  *                 junk - UNUSED.
  703.  *    Returns: none
  704.  */
  705.  
  706. /* ARGSUSED */
  707. void
  708. ApplyResource(w, node_ptr, junk)
  709. Widget w;
  710. XtPointer node_ptr, junk;
  711. {
  712.     ProtocolStream * stream = &(global_client.stream);
  713.     ApplyResourcesInfo info;
  714.     WNode * node = (WNode *) node_ptr;           
  715.     char * value;
  716.     unsigned short size, i;
  717.     long len;
  718.     Arg args[1];
  719.  
  720.     info.name = GetResourceName(node->resources->res_box);
  721.     info.class = "IGNORE_ME";    /* Not currently used.  */
  722.     info.stream = stream;
  723.     info.count = 0;
  724.  
  725.     XtSetArg(args[0], XtNlabel, &value);
  726.     XtGetValues(node->resources->res_box->res_label, args, ONE);
  727.  
  728.     info.database = NULL;
  729.     XrmPutLineResource(&(info.database), value);
  730.  
  731.  
  732.     _XEditResResetStream(stream);
  733.     _XEditResPutString8(stream, info.name); /* Insert name */
  734.     _XEditResPutString8(stream, XtRString); /* insert type */
  735.  
  736.     /*
  737.      * Insert value.
  738.      */
  739.  
  740.     value = GetResourceValueForSetValues(node, &size);
  741.     _XEditResPut16(stream, size);    
  742.     for (i = 0; i < size; i++) 
  743.     _XEditResPut8(stream, value[i]);
  744.     XtFree(value);
  745.     len = stream->current - stream->top;
  746.  
  747.     /* 
  748.      * Insert the widget count, overriden later. 
  749.      */
  750.  
  751.     _XEditResPut16(stream, 0); 
  752.  
  753.     ExecuteOverAllNodes(node->tree_info->top_node,
  754.             CreateSetValuesCommand, (XtPointer) &info);
  755.     
  756.     if (info.count > 0) {
  757.     *(stream->top + len++) = info.count >> XER_NBBY; /* Set the correct */
  758.     *(stream->top + len) = info.count;               /* count. */
  759.  
  760.     SetCommand(node->tree_info->tree_widget, LocalSetValues, NULL);
  761.     }
  762.     else 
  763.     SetMessage(global_screen_data.info_label,
  764.            "ApplyResource: found no matches.");
  765.     
  766.     XrmDestroyDatabase(info.database);
  767. }
  768.  
  769. /*    Function Name: CreateSetValuesCommand
  770.  *    Description: Creates the SetValues command if this widget
  771.  *                   matches the resource string in the database.
  772.  *    Arguments: node - the current node.
  773.  *                 info_ptr - the pointer to the apply info.
  774.  *    Returns: none
  775.  */
  776.  
  777. static void
  778. CreateSetValuesCommand(node, info_ptr)
  779. WNode * node;
  780. XtPointer info_ptr;
  781. {
  782.     ApplyResourcesInfo * info = (ApplyResourcesInfo *) info_ptr;
  783.     XrmNameList name_quarks;
  784.     XrmClassList class_quarks;
  785.     char ** names, **classes;
  786.  
  787.     GetNamesAndClasses(node, &names, &classes);
  788.     name_quarks = (XrmNameList) Quarkify(names, info->name);
  789.     class_quarks = (XrmNameList) Quarkify(classes, info->class);
  790.  
  791.     if (CheckDatabase(info->database, name_quarks, class_quarks)) {
  792.     InsertWidgetFromNode(info->stream, node);
  793.     info->count++;
  794.     }
  795.  
  796.     XtFree((char *)names);
  797.     XtFree((char *)classes);
  798.     XtFree((char *)name_quarks);
  799.     XtFree((char *)class_quarks);
  800. }
  801.  
  802. /*    Function Name: ActivateResourceWidgets
  803.  *    Description: Activates all widgets that match this resource.
  804.  *    Arguments: w - UNUSED.
  805.  *                 node_ptr - the node that owns this resource box.
  806.  *                 junk - UNUSED. 
  807.  *    Returns: none.
  808.  */
  809.  
  810. /* ARGSUSED */
  811. void
  812. ActivateResourceWidgets(w, node_ptr, junk)
  813. Widget w;
  814. XtPointer node_ptr, junk;
  815. {
  816.     WNode * node = (WNode *) node_ptr;           
  817.     ApplyResourcesInfo info;
  818.     char * line;
  819.     Arg args[1];
  820.  
  821.     info.name = GetResourceName(node->resources->res_box);
  822.     info.class = "IGNORE_ME";    /* Not currently used.  */
  823.  
  824.     /* 
  825.      * Unused fields.
  826.      */
  827.  
  828.     info.count = 0;
  829.     info.stream = NULL;
  830.  
  831.     XtSetArg(args[0], XtNlabel, &line);
  832.     XtGetValues(node->resources->res_box->res_label, args, ONE);
  833.  
  834.     info.database = NULL;
  835.     XrmPutLineResource(&(info.database), line);
  836.  
  837.  
  838.     ExecuteOverAllNodes(node->tree_info->top_node,
  839.             SetOnlyMatchingWidgets, (XtPointer) &info);
  840.     
  841.     XrmDestroyDatabase(info.database);
  842. }
  843.  
  844. /*    Function Name: SetOnlyMatchingWidgets
  845.  *    Description: Activates all widgets in the tree that match this
  846.  *                   resource specifiction.
  847.  *    Arguments: node - the current node.
  848.  *                 info_ptr - the pointer to the apply info.
  849.  *    Returns: none
  850.  */
  851.  
  852. static void
  853. SetOnlyMatchingWidgets(node, info_ptr)
  854. WNode * node;
  855. XtPointer info_ptr;
  856. {
  857.     ApplyResourcesInfo * info = (ApplyResourcesInfo *) info_ptr;
  858.     XrmNameList name_quarks;
  859.     XrmClassList class_quarks;
  860.     char ** names, **classes;
  861.     Boolean state;
  862.     Arg args[1];
  863.  
  864.     GetNamesAndClasses(node, &names, &classes);
  865.     name_quarks = (XrmNameList) Quarkify(names, info->name);
  866.     class_quarks = (XrmNameList) Quarkify(classes, info->class);
  867.  
  868.     state = CheckDatabase(info->database, name_quarks, class_quarks);
  869.  
  870.     XtSetArg(args[0], XtNstate, state);
  871.     XtSetValues(node->widget, args, ONE);
  872.     TreeToggle(node->widget, (XtPointer) node, (XtPointer) state);
  873.  
  874.     XtFree((char *)names);
  875.     XtFree((char *)classes);
  876.     XtFree((char *)name_quarks);
  877.     XtFree((char *)class_quarks);
  878. }
  879.