home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE2.TAR / settings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-22  |  13.3 KB  |  516 lines

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