home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE1.TAR / settings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-12  |  12.2 KB  |  483 lines

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