home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / xgopher.1.3 / options.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  14.0 KB  |  616 lines

  1. /* options.c
  2.    make and handle callbacks for the Xgopher options panel */
  3.  
  4.      /*---------------------------------------------------------------*/
  5.      /* Xgopher        version 1.3     08 April 1993                  */
  6.      /*                version 1.2     20 November 1992               */
  7.      /*                version 1.1     20 April 1992                  */
  8.      /*                version 1.0     04 March 1992                  */
  9.      /* X window system client for the University of Minnesota        */
  10.      /*                                Internet Gopher System.        */
  11.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  12.      /*                Computing and Communications Services Office   */
  13.      /* Copyright 1992, 1993 by                                       */
  14.      /*           the Board of Trustees of the University of Illinois */
  15.      /* Permission is granted to freely copy and redistribute this    */
  16.      /* software with the copyright notice intact.                    */
  17.      /*---------------------------------------------------------------*/
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <X11/Intrinsic.h>
  22. #include <X11/StringDefs.h>
  23. #include <X11/Shell.h>
  24. #include <X11/Xaw/Box.h>
  25. #include <X11/Xaw/Command.h>
  26. #include <X11/Xaw/Form.h>
  27. #include <X11/Xaw/Label.h>
  28. #include <X11/Xaw/Toggle.h>
  29. #include <X11/Xaw/AsciiText.h>
  30.  
  31. #include "osdep.h"
  32. #include "gopher.h"
  33. #include "misc.h"
  34. #include "help.h"
  35. #include "panel.h"
  36. #include "bkmkfile.h"
  37. #include "appres.h"
  38. #include "options.h"
  39. #include "compatR4.h"
  40. #include "xglobals.h"
  41. #include "globals.h"
  42. #include "gui.h"
  43.  
  44.  
  45. static Widget    topLevel,
  46.         opPanel;
  47. static Widget        showWhat, appendBk, loadBk, reset;
  48. static Widget        bkSave, printCmd, imageCmd, telCmd, t32Cmd;
  49.  
  50.  
  51. #define THIS_POPUP_NAME        "optionsPopup"
  52.  
  53.                 /* default values */
  54. static popupPosResources    placement = {
  55.  
  56.     /* position at the left of the main panel, 10% down */
  57.     from_main, 0, 10, justify_top_left, justify_top_left, True, True
  58.     };
  59.  
  60.  
  61. /* toggleNotify
  62.    toggle notify callback */
  63.  
  64. static void
  65. toggleNotify(w, client_data, call_data)
  66. Widget        w;
  67. XtPointer    client_data, call_data;
  68. {
  69.     Arg    args[10];
  70.     Cardinal n;
  71.     Boolean    set;
  72.  
  73.     /* change the bitmap according to the current state of the toggle */
  74.  
  75.     n = 0;
  76.     XtSetArg(args[n], XtNstate, &set);  n++;
  77.     XtGetValues(w, args, n);
  78.  
  79.     n = 0;
  80.     XtSetArg(args[n], XtNbitmap, set ? checkPixmap : uncheckPixmap);  n++;
  81.     XtSetValues(w, args, n);
  82. }
  83.  
  84.  
  85. /* createToggleL
  86.    create a labelled toggle widget pair */
  87.  
  88. static Widget
  89. createToggleL(name, parent, below)
  90. char    *name;
  91. Widget    parent, below;
  92. {
  93.     char    tempName[50];
  94.     Widget    theBox, theToggle, theLabel;
  95.     Arg    args[10];
  96.     Cardinal n;
  97.  
  98.     strcpy(tempName, name);
  99.     strcat(tempName, "Box");
  100.     n = 0;
  101.     XtSetArg(args[n], XtNorientation, XtorientHorizontal);  n++;
  102.     if (below != NULL) {
  103.         XtSetArg(args[n], XtNfromVert, XtParent(below));  n++;
  104.     }
  105.     XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  106.     XtSetArg(args[n], XtNright,    XawChainRight);  n++;
  107.     theBox = XtCreateManagedWidget(tempName, boxWidgetClass,
  108.             parent, args, n);
  109.  
  110.     n = 0;
  111.     XtSetArg(args[n], XtNbitmap, checkPixmap);  n++;
  112.     theToggle = XtCreateManagedWidget(name, toggleWidgetClass,
  113.             theBox, args, n);
  114.     XtAddCallback(theToggle, XtNcallback, toggleNotify, NULL);
  115.  
  116.     strcpy(tempName, name);
  117.     strcat(tempName, "Label");
  118.     theLabel = XtCreateManagedWidget(tempName, labelWidgetClass,
  119.             theBox, NULL, (Cardinal) 0);
  120.     
  121.     return theToggle;
  122.     
  123. }
  124.  
  125.  
  126. /* createTextFieldL
  127.    create a labelled single-line text field widget pair */
  128.  
  129. static Widget
  130. createTextFieldL(name, parent, below)
  131. char    *name;
  132. Widget    parent, below;
  133. {
  134.     char    tempName[50];
  135.     Widget    theForm, theText, theLabel;
  136.     Arg    args[10];
  137.     Cardinal    n;
  138.     Dimension    w, h;
  139.  
  140.     strcpy(tempName, name);
  141.     strcat(tempName, "Form");
  142.     n = 0;
  143.     if (below != NULL) {
  144.         XtSetArg(args[n], XtNfromVert, XtParent(below));  n++;
  145.     }
  146.     XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  147.     XtSetArg(args[n], XtNright,    XawChainRight);  n++;
  148.     theForm = XtCreateManagedWidget(tempName, formWidgetClass,
  149.             parent, args, n);
  150.  
  151.     strcpy(tempName, name);
  152.     strcat(tempName, "Label");
  153.     n = 0;
  154.     XtSetArg(args[n], XtNbottom, XawChainBottom);  n++;
  155.     XtSetArg(args[n], XtNtop, XawChainTop);  n++;
  156.     XtSetArg(args[n], XtNright, XawChainLeft);  n++;
  157.     XtSetArg(args[n], XtNleft, XawChainLeft);  n++;
  158.     theLabel = XtCreateManagedWidget(tempName, labelWidgetClass,
  159.             theForm, args, n);
  160.     getTextSize(theLabel, 25, 1, &w, &h);
  161.     n = 0;
  162.     XtSetArg(args[n], XtNwidth, w);  n++;
  163.     XtSetArg(args[n], XtNheight, h);  n++;
  164.     XtSetValues(theLabel, args, n);
  165.     
  166.     n = 0;
  167.     XtSetArg(args[n], XtNresizable, True);  n++;
  168.     XtSetArg(args[n], XtNeditType, XawtextEdit);  n++;
  169.     XtSetArg(args[n], XtNfromHoriz, theLabel);  n++;
  170.     XtSetArg(args[n], XtNbottom, XawChainBottom);  n++;
  171.     XtSetArg(args[n], XtNtop, XawChainTop);  n++;
  172.     XtSetArg(args[n], XtNright, XawChainRight);  n++;
  173.     XtSetArg(args[n], XtNleft, XawChainLeft);  n++;
  174.     theText = XtCreateManagedWidget(name, asciiTextWidgetClass,
  175.             theForm, args, n);
  176.     setTextWidgetSize(theText, 25, 1);
  177.     XtOverrideTranslations(theText, oneLineParsed);
  178.  
  179.     return theText;
  180.     
  181. }
  182.  
  183.  
  184. /* setToggle
  185.    set the state of a toggle button explicitly */
  186.  
  187. static void
  188. setToggle(w, value)
  189. Widget    w;
  190. Boolean    value;
  191. {
  192.     Arg        args[2];
  193.     Cardinal    n;
  194.  
  195.     n = 0;
  196.     XtSetArg(args[n], XtNstate, value);  n++;
  197.     XtSetValues(w, args, n);
  198. }
  199.  
  200.  
  201. /* setText
  202.    set the contents of a text field to a string */
  203.  
  204. static void
  205. setText(w, value)
  206. Widget    w;
  207. String    value;
  208. {
  209.     Arg        args[2];
  210.     Cardinal    n;
  211.  
  212.     n = 0;
  213.     XtSetArg(args[n], XtNstring, value);  n++;
  214.     XtSetValues(w, args, n);
  215. }
  216.  
  217.  
  218. /* getToggle
  219.    set the current state of a toggle button */
  220.  
  221. static Boolean
  222. getToggle(w)
  223. Widget    w;
  224. {
  225.     Arg        args[2];
  226.     Cardinal    n;
  227.     Boolean        value;
  228.  
  229.     n = 0;
  230.     XtSetArg(args[n], XtNstate, &value);  n++;
  231.     XtGetValues(w, args, n);
  232.  
  233.     return value;
  234. }
  235.  
  236.  
  237. /* getText
  238.    get the current contents of a text field */
  239.  
  240. static char *
  241. getText(w)
  242. Widget    w;
  243. {
  244.     Arg        args[2];
  245.     Cardinal    n;
  246.     String        value;
  247.  
  248.     n = 0;
  249.     XtSetArg(args[n], XtNstring, &value);  n++;
  250.     XtGetValues(w, args, n);
  251.  
  252.     return value;
  253. }
  254.  
  255.  
  256. /* setOptionsAtEntry
  257.    set the panel to reflect the options when the panel popped up */
  258.  
  259. static void
  260. setOptionsAtEntry()
  261. {
  262.     setToggle(showWhat, (appResources->showItems == showAll));
  263.     XtCallActionProc(showWhat, "notify", NULL, NULL, 0);
  264.  
  265.     setToggle(appendBk, appResources->appendBookmarks);
  266.     XtCallActionProc(appendBk, "notify", NULL, NULL, 0);
  267.  
  268.     setToggle(loadBk, appResources->loadBookmarks);
  269.     XtCallActionProc(loadBk, "notify", NULL, NULL, 0);
  270.  
  271.     setToggle(reset, appResources->resetOptions);
  272.     XtCallActionProc(reset, "notify", NULL, NULL, 0);
  273.  
  274.     /* it's ok to do this because this file is for reading, too. */
  275.  
  276.     setText(bkSave, appResources->bookmarkFile);
  277.  
  278.     if (appResources->allowPrint) {
  279.         setText(printCmd, appResources->printCommand);
  280.     }
  281.     if (appResources->allowImage) {
  282.         setText(imageCmd, appResources->imageCommand);
  283.     }
  284.     if (appResources->allowTelnet) {
  285.         setText(telCmd, appResources->telnetCommand);
  286.     }
  287.     if (appResources->allowTn3270) {
  288.         setText(t32Cmd, appResources->tn3270Command);
  289.     }
  290. }
  291.  
  292.  
  293. /* changeOptionsAtExit
  294.    set the application options to reflect the current state of the panel */
  295.  
  296. static void
  297. changeOptionsAtExit()
  298. {
  299.     char    *value, *str;
  300.  
  301.     appResources->showItems        =
  302.                 getToggle(showWhat) ? showAll : showAvailable;
  303.     appResources->appendBookmarks    = getToggle(appendBk);
  304.     appResources->loadBookmarks    = getToggle(loadBk);
  305.     appResources->resetOptions    = getToggle(reset);
  306.  
  307.     setBkmkAppend(appResources->appendBookmarks);
  308.  
  309.     /* the following code could be a not-too-serious memory leak.
  310.        the assignment of new string values to the appResources
  311.        strings is leaving old values dangling.  The problem is
  312.        that we don't know if the original values are dynamically
  313.        allocated and can be freed or static storage.  This
  314.        isn't a real problem because there isn't that much memory
  315.        at stake. */
  316.  
  317.     /* it's ok to do this because this file is for reading, too. */
  318.  
  319.         value = getText(bkSave);
  320.         str = (char *) malloc(sizeof(char) * strlen(value) + 1);
  321.         strcpy(str, value);
  322.         appResources->bookmarkFile = str;
  323.  
  324.         setBkmkFile (value);
  325.  
  326.     if (appResources->allowPrint) {
  327.         value = getText(printCmd);
  328.         str = (char *) malloc(sizeof(char) * strlen(value) + 1);
  329.         strcpy(str, value);
  330.         appResources->printCommand = str;
  331.     }
  332.  
  333.     if (appResources->allowImage) {
  334.         value = getText(imageCmd);
  335.         str = (char *) malloc(sizeof(char) * strlen(value) + 1);
  336.         strcpy(str, value);
  337.         appResources->imageCommand = str;
  338.     }
  339.  
  340.     if (appResources->allowTelnet) {
  341.         value = getText(telCmd);
  342.         str = (char *) malloc(sizeof(char) * strlen(value) + 1);
  343.         strcpy(str, value);
  344.         appResources->telnetCommand = str;
  345.     }
  346.  
  347.     if (appResources->allowTn3270) {
  348.         value = getText(t32Cmd);
  349.         str = (char *) malloc(sizeof(char) * strlen(value) + 1);
  350.         strcpy(str, value);
  351.         appResources->tn3270Command = str;
  352.     }
  353. }
  354.  
  355.  
  356. /* displayOptionsPanel
  357.    pop up the options panel window */
  358.  
  359. void
  360. displayOptionsPanel()
  361. {
  362.     positionAPopup(opPanel, topLevel, &placement);
  363.  
  364.     XtPopup(opPanel, XtGrabNone);
  365.  
  366.     setOptionsAtEntry();
  367.  
  368.     return;
  369. }
  370.  
  371.  
  372. /* removeOptionsPanel
  373.    pop down the options panel window */
  374.  
  375. void
  376. removeOptionsPanel()
  377. {
  378.     XtPopdown(opPanel);
  379.  
  380.     return;
  381. }
  382.  
  383.  
  384. /* doneProc
  385.    done callback for options panel */
  386.  
  387. static void
  388. doneProc(w, client_data, call_data)
  389. Widget        w;
  390. XtPointer    client_data, call_data;
  391. {
  392.     /* set the options selected then pop down the options panel */
  393.  
  394.     removeOptionsPanel();
  395.  
  396.     changeOptionsAtExit();
  397.  
  398.     checkButtonState(BS_loadMarks | BS_saveMarks);
  399. }
  400.  
  401.  
  402. /* cancelProc
  403.    cancel callback for options panel */
  404.  
  405. static void
  406. cancelProc(w, client_data, call_data)
  407. Widget        w;
  408. XtPointer    client_data, call_data;
  409. {
  410.     /* do not change any of these options, but pop down the options panel */
  411.  
  412.     removeOptionsPanel();
  413. }
  414.  
  415.  
  416. /* helpProc
  417.    help callback for options panel. */
  418.  
  419. static void
  420. helpProc(w, client_data, call_data)
  421. Widget        w;
  422. XtPointer    client_data, call_data;
  423. {
  424.     char    *string;
  425.  
  426.     showHelp("options help");
  427. }
  428.  
  429.  
  430. /* optDoneActionProc
  431.    implement the done action */
  432.  
  433. static void
  434. optDoneActionProc(w, event, params, numParams)
  435. Widget        w;
  436. XEvent        *event;
  437. String        *params;
  438. Cardinal    *numParams;
  439. {
  440.     doneProc(NULL, NULL, NULL);
  441. }
  442.  
  443.  
  444. /* optCancelActionProc
  445.    implement the cancel action */
  446.  
  447. static void
  448. optCancelActionProc(w, event, params, numParams)
  449. Widget        w;
  450. XEvent        *event;
  451. String        *params;
  452. Cardinal    *numParams;
  453. {
  454.     cancelProc(NULL, NULL, NULL);
  455. }
  456.  
  457.  
  458. /* optHelpActionProc
  459.    implement the help action */
  460.  
  461. static void
  462. optHelpActionProc(w, event, params, numParams)
  463. Widget        w;
  464. XEvent        *event;
  465. String        *params;
  466. Cardinal    *numParams;
  467. {
  468.     helpProc(NULL, NULL, NULL);
  469. }
  470.  
  471.  
  472. /* makeOptionsPanel
  473.    create the elements of the xgopher options panel */
  474.  
  475. void
  476. makeOptionsPanel(top)
  477. Widget    top;
  478. {
  479.     Widget        doneButton, helpButton, cancelButton,
  480.             opForm, buttonBox;
  481.     Widget        itemsForm, previous;
  482.     Arg        args[10];
  483.     Cardinal    n;
  484.  
  485.     static XtActionsRec    actionsTable[] = {
  486.                 {"optDone",   optDoneActionProc},
  487.                 {"optCancel", optCancelActionProc},
  488.                 {"optHelp",   optHelpActionProc},
  489.                 };
  490.  
  491.     topLevel = top;
  492.  
  493.     XtAppAddActions(appcon, actionsTable, XtNumber(actionsTable));
  494.  
  495.  
  496.     /* create gopher options panel */
  497.  
  498.     n = 0;
  499.     XtSetArg(args[n], XtNtitle, "Xgopher Options");  n++;
  500.     opPanel = XtCreatePopupShell("optionsPanel", transientShellWidgetClass,
  501.                     topLevel, args, n);
  502.         /*
  503.         XtAugmentTranslations(opPanel, transTable);
  504.         */
  505.  
  506.     /* create OPTIONS form */
  507.  
  508.     opForm = XtCreateManagedWidget("optionsForm", formWidgetClass,
  509.                     opPanel, NULL, (Cardinal) 0);
  510.  
  511.  
  512.     /* create BUTTON box */
  513.  
  514.         n = 0;
  515.         XtSetArg(args[n], XtNorientation, XtorientHorizontal);  n++;
  516.         XtSetArg(args[n], XtNtop,    XawChainTop);  n++;
  517.         XtSetArg(args[n], XtNbottom,    XawChainTop);  n++;
  518.         XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  519.         XtSetArg(args[n], XtNright,    XawChainLeft);  n++;
  520.     buttonBox = XtCreateManagedWidget("buttonBox", boxWidgetClass,
  521.                     opForm, args, n);
  522.  
  523.  
  524.  
  525.     /* create ITEMS form */
  526.  
  527.         n = 0;
  528.         XtSetArg(args[n], XtNfromVert,    buttonBox);  n++;
  529.         XtSetArg(args[n], XtNtop,    XawChainTop);  n++;
  530.         XtSetArg(args[n], XtNbottom,    XawChainBottom);  n++;
  531.         XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  532.         XtSetArg(args[n], XtNright,    XawChainRight);  n++;
  533.     itemsForm = XtCreateManagedWidget("itemsForm", formWidgetClass,
  534.                     opForm, args, n);
  535.  
  536.     /* create toggles */
  537.  
  538.     showWhat = createToggleL("showWhat", itemsForm, NULL);
  539.     appendBk = createToggleL("appendBk", itemsForm, showWhat);
  540.     loadBk = createToggleL("loadBk", itemsForm, appendBk);
  541.     reset = createToggleL("reset", itemsForm, loadBk);
  542.     previous = reset;
  543.  
  544.  
  545.     /* create text fields */
  546.  
  547.     /* it's ok to do this because this file is for reading, too. */
  548.  
  549.         previous =
  550.         bkSave = createTextFieldL("bkSave", itemsForm, previous);
  551.  
  552.     if (appResources->allowPrint) {
  553.         previous =
  554.         printCmd = createTextFieldL("printCmd", itemsForm, previous);
  555.     }
  556.     if (appResources->allowImage) {
  557.         previous =
  558.         imageCmd = createTextFieldL("imageCmd", itemsForm, previous);
  559.     }
  560.     if (appResources->allowTelnet) {
  561.         previous =
  562.         telCmd = createTextFieldL("telCmd", itemsForm, previous);
  563.     }
  564.     if (appResources->allowTn3270) {
  565.         t32Cmd = createTextFieldL("t3270Cmd", itemsForm, previous);
  566.     }
  567.                 
  568.  
  569.     /* create QUIT button */
  570.  
  571.     doneButton = XtCreateManagedWidget("done", commandWidgetClass,
  572.                     buttonBox, NULL, (Cardinal) 0);
  573.         XtAddCallback(doneButton, XtNcallback, doneProc, NULL);
  574.  
  575.     /* create CANCEL button */
  576.  
  577.         n = 0;
  578.         XtSetArg(args[n], XtNfromHoriz,    doneButton);  n++;
  579.     cancelButton = XtCreateManagedWidget("cancel", commandWidgetClass,
  580.                     buttonBox, args, n);
  581.         XtAddCallback(cancelButton, XtNcallback, cancelProc, NULL);
  582.  
  583.     /* create HELP button */
  584.  
  585.         n = 0;
  586.         XtSetArg(args[n], XtNfromHoriz,    cancelButton);  n++;
  587.     helpButton = XtCreateManagedWidget("help", commandWidgetClass,
  588.                     buttonBox, args, n);
  589.         XtAddCallback(helpButton, XtNcallback, helpProc, NULL);
  590.  
  591.  
  592.         /* for ICCCM window manager protocol complience */
  593.  
  594.         XtOverrideTranslations (opPanel,
  595.             XtParseTranslationTable ("<Message>WM_PROTOCOLS: optCancel()"));
  596.         XtRealizeWidget(opPanel);
  597.         (void) XSetWMProtocols (XtDisplay(opPanel), XtWindow(opPanel),
  598.                                     &wmDeleteAtom, 1);    
  599.  
  600.  
  601.     /* find the popup placement for this shell */
  602.  
  603.     {
  604.     popupPosResources *resourcePlacement;
  605.  
  606.     resourcePlacement = getPopupPosResources(
  607.                 THIS_POPUP_NAME, POPUP_POS_CLASS, &placement);
  608.     bcopy( (char *) resourcePlacement, (char *) &placement,
  609.                 sizeof(popupPosResources) );
  610.     }
  611.  
  612.  
  613.     return;
  614. }
  615.  
  616.