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

  1. /*
  2.  * settings.c : Set program parameters on a popup panel
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 21 Aug 1991.
  5.  *
  6.  */
  7. #include <stdio.h>
  8. #include <X11/Intrinsic.h>
  9. #include <X11/Shell.h>
  10. #include <X11/StringDefs.h>
  11. #include <X11/Xaw/Form.h>
  12. #include <X11/Xaw/MenuButton.h>
  13. #include <X11/Xaw/Label.h>
  14. #include <X11/Xaw/AsciiText.h>
  15. #include <EzMenu/EzMenu.h>    
  16. #include <X11/Xaw/Cardinals.h>
  17. #include "xarchie.h"
  18. #include "types.h"
  19. #include "appres.h"
  20. #include "alert.h"
  21. #include "confirm.h"
  22.  
  23. /*
  24.  * Functions declared in this file
  25.  */
  26. void settingsAction();
  27. void applySettingsAction(),defaultSettingsAction(),doneSettingsAction();
  28. void setSearchTypeAction(),setSortTypeAction();
  29. void setHostAction(),setNiceLevelAction();
  30. void setSearchTypeNowAction(),setSortTypeNowAction();
  31. void setHostNowAction(),setNiceLevelNowAction();
  32.  
  33. static void initSettingsShell(),initSettingsItems();
  34. static void textEventProc();
  35. static void setCurrentSearchType(),setCurrentSortType();
  36. static void setChangedFlag();
  37.  
  38. /*
  39.  * Data declared in this file
  40.  */
  41. static Widget settingsShell;
  42. static Widget applyButton;
  43. static Widget searchLabel;
  44. static Widget sortLabel;
  45. static Widget hostText;
  46. static Widget maxHitsText;
  47. static Widget timeoutText;
  48. static Widget retriesText;
  49. static Widget niceText;
  50.  
  51. static SearchType currentSearchType;
  52. static SortType currentSortType;
  53. static Boolean settingsChanged,isPoppedUp;
  54.  
  55. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  56.                     Widget w; \
  57.                     XEvent *event; \
  58.                     String *params; \
  59.                     Cardinal *num_params;
  60.  
  61. /*    -    -    -    -    -    -    -    -    */
  62. /* External interface (action) procedure */
  63. /*
  64.  * settingsAction() : Pops up (possibly creating) the settings editor,
  65.  *    and fills it with the information from the current values of
  66.  *    the application settings.
  67.  */
  68. /*ARGSUSED*/
  69. ACTION_PROC(settingsAction)
  70. {
  71.     if (settingsShell == NULL)
  72.     initSettingsShell();
  73.     if (isPoppedUp) {
  74.     XRaiseWindow(display,XtWindow(settingsShell));
  75.     } else {
  76.     initSettingsItems();
  77.     XtPopup(settingsShell,XtGrabNone);
  78.     isPoppedUp = True;
  79.     }
  80. }
  81.  
  82. /*    -    -    -    -    -    -    -    -    */
  83. /* Initialization procedures */
  84. /*
  85.  * initSettingsShell() : Create the popup settings editor.
  86.  */
  87. static void
  88. initSettingsShell()
  89. {
  90.     isPoppedUp = False;
  91.     initWidgetsFromString(appResources.settingsWidgets,".settingsWidgets");
  92.     /* check for the necessary widgets and set the global variables */
  93.     if ((settingsShell=XtNameToWidget(toplevel,"*settingsShell")) == NULL)
  94.         fail0("didn't create widget \"settingsShell\"");
  95.     /* set globals for optional widgets */
  96.     applyButton = XtNameToWidget(toplevel,"settingsShell*applyButton");
  97.     searchLabel = XtNameToWidget(toplevel,"settingsShell*searchLabel");
  98.     sortLabel = XtNameToWidget(toplevel,"settingsShell*sortLabel");
  99.     hostText = XtNameToWidget(toplevel,"settingsShell*hostText");
  100.     maxHitsText = XtNameToWidget(toplevel,"settingsShell*maxHitsText");
  101.     timeoutText = XtNameToWidget(toplevel,"settingsShell*timeoutText");
  102.     retriesText = XtNameToWidget(toplevel,"settingsShell*retriesText");
  103.     niceText = XtNameToWidget(toplevel,"settingsShell*niceText");
  104.     /* add event handler for detecting changes */
  105.     if (hostText != NULL)
  106.     XtAddEventHandler(hostText,KeyPressMask|ButtonPressMask,False,
  107.               textEventProc,(XtPointer)NULL);
  108.     if (maxHitsText != NULL)
  109.     XtAddEventHandler(maxHitsText,KeyPressMask|ButtonPressMask,False,
  110.               textEventProc,(XtPointer)NULL); 
  111.     if (timeoutText != NULL)
  112.     XtAddEventHandler(timeoutText,KeyPressMask|ButtonPressMask,False,
  113.               textEventProc,(XtPointer)NULL);
  114.     if (retriesText != NULL)
  115.     XtAddEventHandler(retriesText,KeyPressMask|ButtonPressMask,False,
  116.               textEventProc,(XtPointer)NULL);
  117.     if (niceText != NULL)
  118.     XtAddEventHandler(niceText,KeyPressMask|ButtonPressMask,False,
  119.               textEventProc,(XtPointer)NULL);
  120. }
  121.  
  122. /*    -    -    -    -    -    -    -    -    */
  123. /*
  124.  * initSettingsItems() : Sets the values in the settings editor from the
  125.  *    current state of the application resources.
  126.  */
  127. static void
  128. initSettingsItems()
  129. {
  130.     char buf[8];
  131.  
  132.     setCurrentSearchType(appResources.searchType);
  133.     setCurrentSortType(appResources.sortType);
  134.     setText(hostText,appResources.archieHost);
  135.     sprintf(buf,"%d",appResources.maxHits);
  136.     setText(maxHitsText,buf);
  137.     setText(hostText,appResources.archieHost);
  138.     sprintf(buf,"%d",appResources.timeout);
  139.     setText(timeoutText,buf);
  140.     sprintf(buf,"%d",appResources.retries);
  141.     setText(retriesText,buf);
  142.     sprintf(buf,"%d",appResources.niceLevel);
  143.     setText(niceText,buf);
  144.     setChangedFlag(False);
  145. }
  146.  
  147. /*    -    -    -    -    -    -    -    -    */
  148. /* Callback procedures */
  149. /*
  150.  * applyCallback() : Callback for apply button - Set the application resources
  151.  *    from the items on the settings editor panel. Some of these require
  152.  *    special action when changed, and this routine does that.
  153.  */
  154. /*ARGSUSED*/
  155. ACTION_PROC(applySettingsAction)
  156. {
  157.     Arg args[1];
  158.     char *s;
  159.     int n;
  160.  
  161.     appResources.searchType = currentSearchType;
  162.     appResources.sortType = currentSortType;
  163.     if (hostText != NULL) {
  164.     XtSetArg(args[0],XtNstring,&s);
  165.     XtGetValues(hostText,args,ONE);
  166.     /* memory leak: we can't free the original, so we can't free any! */
  167.     appResources.archieHost = XtNewString(s);
  168.     }
  169.     if (maxHitsText != NULL) {
  170.     XtSetArg(args[0],XtNstring,&s);
  171.     XtGetValues(maxHitsText,args,ONE);
  172.     appResources.maxHits = atoi(s);
  173.     }
  174.     if (timeoutText != NULL) {
  175.     XtSetArg(args[0],XtNstring,&s);
  176.     XtGetValues(timeoutText,args,ONE);
  177.     appResources.timeout = atoi(s);
  178.     }
  179.     if (retriesText != NULL) {
  180.     XtSetArg(args[0],XtNstring,&s);
  181.     XtGetValues(retriesText,args,ONE);
  182.     appResources.retries = atoi(s);
  183.     }
  184.     if (niceText != NULL) {
  185.     XtSetArg(args[0],XtNstring,&s);
  186.     XtGetValues(niceText,args,ONE);
  187.     n = atoi(s);
  188.     if (n < -32765)        /* leave -32766 to -32768 alone */
  189.         n = -32765;
  190.     else if (n > 32767)
  191.         n = 32767;
  192.     appResources.niceLevel = n;
  193.     }
  194.     setChangedFlag(False);
  195. }
  196.  
  197. /*
  198.  * defaultCallback() : Callback for default button - Reset the items
  199.  *      to their default values.
  200.  */
  201. /*ARGSUSED*/
  202. ACTION_PROC(defaultSettingsAction)
  203. {
  204.     setCurrentSearchType(GfExact);
  205.     setCurrentSortType(GfDefault);
  206.     setText(hostText,"archie.mcgill.ca");
  207.     setText(maxHitsText,"99");
  208.     setText(timeoutText,"4");
  209.     setText(retriesText,"3");
  210.     setText(niceText,"0");
  211.     setChangedFlag(True);
  212. }
  213.  
  214. /*
  215.  * doneCallback() : Callback for done button - Pop down the editor.
  216.  */
  217. /*ARGSUSED*/
  218. ACTION_PROC(doneSettingsAction)
  219. {
  220.     if (!settingsChanged || confirm0("Discard changes to settings?")) {
  221.     XtPopdown(settingsShell);
  222.     isPoppedUp = False;
  223.     }
  224. }
  225.  
  226. /*
  227.  * setSearchTypeAction() : Action procedure to set the search type.
  228.  */
  229. /*ARGSUSED*/
  230. ACTION_PROC(setSearchTypeAction)
  231. {
  232.     XrmValue from,to;
  233.  
  234.     if (*num_params != ONE) {
  235.     alert0("Incorrect number of arguments to set-search-type()");
  236.     return;
  237.     }
  238.     from.addr = *params;
  239.     from.size = sizeof(String);
  240.     to.addr = NULL;
  241.     XtConvertAndStore(w,XtRString,&from,GfRSearchType,&to);
  242.     if (to.addr != NULL)
  243.     setCurrentSearchType((SearchType)(*(to.addr)));
  244.     setChangedFlag(True);
  245. }
  246.  
  247. /*
  248.  * setSortTypeAction() : Action procedure to set the sort type.
  249.  */
  250. /*ARGSUSED*/
  251. ACTION_PROC(setSortTypeAction)
  252. {
  253.     XrmValue from,to;
  254.  
  255.     if (*num_params != ONE) {
  256.     alert0("Incorrect number of arguments to set-sort-type()");
  257.     return;
  258.     }
  259.     from.addr = *params;
  260.     from.size = sizeof(String);
  261.     to.addr = NULL;
  262.     XtConvert(w,XtRString,&from,GfRSortType,&to);
  263.     if (to.addr != NULL)
  264.     setCurrentSortType((SortType)(*(to.addr)));
  265.     setChangedFlag(True);
  266. }
  267.  
  268. /*
  269.  * setHostAction() : Action procedure to set the host.
  270.  */
  271. /*ARGSUSED*/
  272. ACTION_PROC(setHostAction)
  273. {
  274.     if (*num_params != ONE) {
  275.     alert0("Incorrect number of arguments to set-host()");
  276.     return;
  277.     }
  278.     if (hostText == NULL) {
  279.     alert0("set-host() has no effect since hostText was not created");
  280.     return;
  281.     }
  282.     setText(hostText,*params);
  283.     setChangedFlag(True);
  284. }
  285.  
  286. /*
  287.  * setNiceLevelAction() : Action procedure to set rdgram_priority
  288.  */
  289. /*ARGSUSED*/
  290. ACTION_PROC(setNiceLevelAction)
  291. {
  292.     int n;
  293.  
  294.     if (*num_params != ONE) {
  295.     alert0("Incorrect number of arguments to set-nice-level()");
  296.     return;
  297.     }
  298.     if (niceText == NULL) {
  299.        alert0("set-nice-level() has no effect since niceText was not created");
  300.     return;
  301.     }
  302.     n = atoi(*params);
  303.     if (n < -32765) {
  304.     alert1("Nice level too negative: %d",n);
  305.     setText(niceText,"-32765");
  306.     } else if (n > 32767) {
  307.     alert1("Nice level too positive: %d",n);
  308.     setText(niceText,"32767");
  309.     } else {
  310.     setText(niceText,*params);
  311.     }
  312.     setChangedFlag(True);
  313. }
  314.  
  315. /*    -    -    -    -    -    -    -    -    */
  316. /* These actions are like their non-Now counterparts, expect that
  317.  * (a) they set appResources immediately rather than waiting for
  318.  *     apply-settings() to be called, and
  319.  * (b) they do not set the changedFlag since they have made the change
  320.  *     globally already.
  321.  * Still, they really aren't meant to be used when the settingsPanel is
  322.  * being displayed.
  323.  */
  324.  
  325. /*ARGSUSED*/
  326. ACTION_PROC(setSearchTypeNowAction)
  327. {
  328.     XrmValue from,to;
  329.     SearchType type;
  330.  
  331.     if (*num_params != ONE) {
  332.     alert0("Incorrect number of arguments to set-search-type-now()");
  333.     return;
  334.     }
  335.     from.addr = *params;
  336.     from.size = sizeof(String);
  337.     to.addr = NULL;
  338.     XtConvertAndStore(w,XtRString,&from,GfRSearchType,&to);
  339.     if (to.addr != NULL) {
  340.     type = (SearchType)(*(to.addr));
  341.     appResources.searchType = type;
  342.     setCurrentSearchType(type);
  343.     }
  344. }
  345.  
  346. /*ARGSUSED*/
  347. ACTION_PROC(setSortTypeNowAction)
  348. {
  349.     XrmValue from,to;
  350.     SortType type;
  351.  
  352.     if (*num_params != ONE) {
  353.     alert0("Incorrect number of arguments to set-sort-type-now()");
  354.     return;
  355.     }
  356.     from.addr = *params;
  357.     from.size = sizeof(String);
  358.     to.addr = NULL;
  359.     XtConvertAndStore(w,XtRString,&from,GfRSortType,&to);
  360.     if (to.addr != NULL) {
  361.     type = (SortType)(*(to.addr));
  362.     appResources.sortType = type;
  363.     setCurrentSortType(type);
  364.     }
  365. }
  366.  
  367. /*ARGSUSED*/
  368. ACTION_PROC(setHostNowAction)
  369. {
  370.     if (*num_params != ONE) {
  371.     alert0("Incorrect number of arguments to set-host-now()");
  372.     return;
  373.     }
  374.     /* Memory leak */
  375.     appResources.archieHost = XtNewString(*params);
  376.     setText(hostText,*params);
  377. }
  378.  
  379. /*ARGSUSED*/
  380. ACTION_PROC(setNiceLevelNowAction)
  381. {
  382.     int n;
  383.  
  384.     if (*num_params != ONE) {
  385.     alert0("Incorrect number of arguments to set-nice-level-now()");
  386.     return;
  387.     }
  388.     n = atoi(*params);
  389.     if (n < -32765) {
  390.     alert1("Nice level too negative: %d -- not set",n);
  391.     } else if (n > 32767) {
  392.     alert1("Nice level too positive: %d -- not set",n);
  393.     } else {
  394.     appResources.niceLevel = n;
  395.     setText(niceText,*params);
  396.     }
  397. }
  398.  
  399. /*    -    -    -    -    -    -    -    -    */
  400. /*
  401.  * textEventProc() : Called whenever the user types in any Text item.
  402.  *      Note that this does NOT detect, eg., selection pastes, as
  403.  *    documented in the BUGS section of the man page.
  404.  */
  405. /*ARGSUSED*/
  406. static void
  407. textEventProc(w,client_data,event,continue_flag)
  408. Widget w;
  409. XtPointer client_data;
  410. XEvent *event;
  411. Boolean *continue_flag;
  412. {
  413.     setChangedFlag(True);
  414. }
  415.  
  416. /*    -    -    -    -    -    -    -    -    */
  417.  
  418. static void
  419. setCurrentSearchType(type)
  420. SearchType type;
  421. {
  422.     char *s;
  423.  
  424.     currentSearchType = type;
  425.     switch (type) {
  426.     case GfExact :   s = GfNExact; break;
  427.     case GfSubstr :  s = GfNSubstr; break;
  428.     case GfSubcase : s = GfNSubcase; break;
  429.     case GfRegexp :  s = GfNRegexp; break;
  430.     case GfExactSubstr :  s = GfNExactSubstr; break;
  431.     case GfExactSubcase : s = GfNExactSubcase; break;
  432.     case GfExactRegexp :  s = GfNExactRegexp; break;
  433.     }
  434.     setLabel(searchLabel,s);
  435. }
  436.  
  437. static void
  438. setCurrentSortType(type)
  439. SortType type;
  440. {
  441.     char *s;
  442.  
  443.     currentSortType = type;
  444.     switch (type) {
  445.     case GfDefault : s = GfNDefault; break;
  446.     case GfInvdate : s = GfNInvdate; break;
  447.     }
  448.     setLabel(sortLabel,s);
  449. }
  450.  
  451. static void
  452. setChangedFlag(value)
  453. Boolean value;
  454. {
  455.     if (applyButton != NULL)
  456.     XtSetSensitive(applyButton,value);
  457.     settingsChanged = value;
  458. }
  459.