home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE-.1 / HELP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-22  |  6.8 KB  |  269 lines

  1. /*
  2.  * help.c : The Help browser
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  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/Label.h>
  13. #include <X11/Xaw/Viewport.h>
  14. #include <X11/Xaw/List.h>
  15. #include <X11/Xaw/AsciiText.h>
  16. #include <X11/Xaw/Command.h>
  17. #include <X11/Xaw/Cardinals.h>
  18. #include "xarchie.h"
  19. #include "xutil.h"
  20. #include "patchlevel.h"
  21.  
  22. /*
  23.  * Functions defined here
  24.  */
  25. void initHelpActions(),initHelpPanel();
  26. void setHelpShellState();
  27.  
  28. static void popupHelpPanel();
  29. static void initHelpWidgets();
  30. static void helpCallback();
  31. static void helpDoneAction(),helpPrevAction(),helpNextAction();
  32. static void helpDownAction(),helpUpAction();
  33.  
  34. /*
  35.  * Data defined here
  36.  */
  37. static Widget helpShell;
  38. static Widget helpLabel,helpViewport,helpList,helpText;
  39. static Widget helpScrollbar;
  40. static Widget helpPrevButton,helpNextButton;
  41.  
  42. static Boolean isPoppedUp;
  43. static int numHelpTopics;
  44.  
  45. static XtActionsRec actionTable[] = {
  46.     { "help",        popupHelpPanel },
  47.     { "help-done",    helpDoneAction },
  48.     { "help-prev",    helpPrevAction },
  49.     { "help-next",    helpNextAction },
  50.     { "help-down",    helpDownAction },
  51.     { "help-up",    helpUpAction },
  52. };
  53.  
  54. static char *helpStrings[] = {
  55. #include "help-text1.h"
  56.     NULL
  57. };
  58.  
  59. static struct {
  60.     int lineno;
  61.     char *string;
  62. } helpTopicInfo[] = {
  63. #include "help-text2.h"
  64. };
  65.  
  66. static char **helpTopics;
  67.  
  68. /*    -    -    -    -    -    -    -    -    */
  69.  
  70. void
  71. initHelpActions()
  72. {
  73.     XtAppAddActions(appContext,actionTable,XtNumber(actionTable));
  74. }
  75.  
  76. void
  77. initHelpPanel()
  78. {
  79.     int i;
  80.  
  81.     /* Last entry in help-text2.h is bogus */
  82.     numHelpTopics = XtNumber(helpTopicInfo)-1;
  83.     /* Make an array with just the strings for the List widget */
  84.     helpTopics = (char **)XtCalloc(numHelpTopics,sizeof(char*));
  85.     for (i=0; i < numHelpTopics; i++)
  86.     *(helpTopics+i) = helpTopicInfo[i].string;
  87. }
  88.  
  89. static void
  90. popupHelpPanel()
  91. {
  92.     if (isPoppedUp) {
  93.     XRaiseWindow(display,XtWindow(helpShell));
  94.     return;
  95.     }
  96.     if (helpShell == NULL) {
  97.     initHelpWidgets();
  98.     }
  99.     isPoppedUp = True;
  100.     XtPopup(helpShell,XtGrabNone);
  101. }
  102.  
  103. static void
  104. initHelpWidgets()
  105. {
  106.     Widget form;
  107.     char buf[64];
  108.  
  109.     helpShell = XtCreatePopupShell("helpShell",topLevelShellWidgetClass,
  110.                    toplevel,NULL,0);
  111.     form = XtCreateManagedWidget("helpForm",formWidgetClass,
  112.                  helpShell,NULL,0);
  113.     helpLabel = XtCreateManagedWidget("helpLabel",labelWidgetClass,
  114.                       form,NULL,0);
  115. #ifdef BETA
  116.     sprintf(buf,"This is help for Xarchie %.2fb%d",VERSION,PATCHLEVEL);
  117. #else
  118.     sprintf(buf,"This is help for Xarchie %.2f.%d",VERSION,PATCHLEVEL);
  119. #endif
  120.     setWidgetLabel(helpLabel,buf);
  121.     helpViewport = XtCreateManagedWidget("helpViewport",viewportWidgetClass,
  122.                      form,NULL,0);
  123.     helpList = XtCreateManagedWidget("helpList",listWidgetClass,
  124.                      helpViewport,NULL,0);
  125.     XawListChange(helpList,helpTopics,numHelpTopics,0,True);
  126.     XtAddCallback(helpList,XtNcallback,helpCallback,NULL);
  127.     helpText = XtCreateManagedWidget("helpText",asciiTextWidgetClass,
  128.                      form,NULL,0);
  129.     (void)XtCreateManagedWidget("helpDoneButton",commandWidgetClass,
  130.                 form,NULL,0);
  131.     helpPrevButton = XtCreateManagedWidget("helpPrevButton",commandWidgetClass,
  132.                        form,NULL,0);
  133.     XtSetSensitive(helpPrevButton,False);
  134.     helpNextButton = XtCreateManagedWidget("helpNextButton",commandWidgetClass,
  135.                        form,NULL,0);
  136.     XtSetSensitive(helpNextButton,False);
  137.     (void)XtCreateManagedWidget("helpDownButton",commandWidgetClass,
  138.                 form,NULL,0);
  139.     (void)XtCreateManagedWidget("helpUpButton",commandWidgetClass,
  140.                 form,NULL,0);
  141.     XtRealizeWidget(helpShell);
  142.     (void)XSetWMProtocols(XtDisplay(helpShell),XtWindow(helpShell),
  143.               &WM_DELETE_WINDOW,1);
  144.     helpScrollbar = XtNameToWidget(helpViewport,"vertical");
  145. }
  146.  
  147. void
  148. setHelpShellState(state)
  149. int state;
  150. {
  151.     if (!isPoppedUp)
  152.     return;
  153.     switch (state) {
  154.     case NormalState:
  155.         XtMapWidget(helpShell);
  156.         break;
  157.     case IconicState:
  158.         XtUnmapWidget(helpShell);
  159.         break;
  160.     }
  161. }
  162.  
  163. /*    -    -    -    -    -    -    -    -    */
  164. /* Callback procedure */
  165.  
  166. /*ARGSUSED*/
  167. static void
  168. helpCallback(w,client_data,call_data)
  169. Widget w;
  170. XtPointer client_data;  /* not used */
  171. XtPointer call_data;    /* returnStruct */
  172. {
  173.     int topic = ((XawListReturnStruct*)call_data)->list_index;
  174.     XawTextPosition pos;
  175.     XawTextBlock block;
  176.     Arg args[2];
  177.     int i;
  178.  
  179.     block.firstPos = 0;
  180.     block.format = FMT8BIT;
  181.     /* Reset helpText */
  182.     XtSetArg(args[0],XtNstring,"");
  183.     XtSetArg(args[1],XtNeditType,XawtextEdit);
  184.     XtSetValues(helpText,args,2);
  185.     pos = (XawTextPosition)0;
  186.     XawTextDisableRedisplay(helpText);
  187.     for (i=helpTopicInfo[topic].lineno; helpStrings[i] != NULL; i++) {
  188.     /* Add helpStrings[i] to helpText */
  189.     block.ptr = helpStrings[i];
  190.     block.length = strlen(helpStrings[i]);
  191.     XawTextReplace(helpText,pos,pos,&block);
  192.     pos += block.length;
  193.     }
  194.     XawTextEnableRedisplay(helpText);
  195.     XtSetArg(args[0],XtNeditType,XawtextRead);
  196.     XtSetValues(helpText,args,1);
  197.     XtSetSensitive(helpPrevButton,(topic != 0));
  198.     XtSetSensitive(helpNextButton,(topic != numHelpTopics-1));
  199. }
  200.  
  201.  
  202. /*    -    -    -    -    -    -    -    -    */
  203. /* Action procedures */
  204.  
  205. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  206.                     Widget w; \
  207.                     XEvent *event; \
  208.                     String *params; \
  209.                     Cardinal *num_params;
  210.  
  211. /*ARGSUSED*/
  212. static
  213. ACTION_PROC(helpDoneAction)
  214. {
  215.     XtPopdown(helpShell);
  216.     isPoppedUp = False;
  217. }
  218.  
  219. /*ARGSUSED*/
  220. static
  221. ACTION_PROC(helpPrevAction)
  222. {
  223.     XawListReturnStruct *ret = XawListShowCurrent(helpList);
  224.     float percent;
  225.  
  226.     if (ret->list_index != XAW_LIST_NONE && ret->list_index != 0) {
  227.     ret->list_index -= 1;
  228.     /* Highlight the item */
  229.     XawListHighlight(helpList,ret->list_index);
  230.     /* Get the text displayed */
  231.     helpCallback(helpList,NULL,(XtPointer)ret);
  232.     /* Adjust the scrollbar so it's visible */
  233.     percent = (float)(ret->list_index-1) / (float)numHelpTopics;
  234.     XtCallCallbacks(helpScrollbar,"jumpProc",(XtPointer)&percent);
  235.     }
  236. }
  237.  
  238. /*ARGSUSED*/
  239. static
  240. ACTION_PROC(helpNextAction)
  241. {
  242.     XawListReturnStruct *ret = XawListShowCurrent(helpList);
  243.     float percent;
  244.  
  245.     if (ret->list_index != XAW_LIST_NONE &&
  246.     ret->list_index != numHelpTopics-1) {
  247.     ret->list_index += 1;
  248.     XawListHighlight(helpList,ret->list_index);
  249.     helpCallback(helpList,NULL,(XtPointer)ret);
  250.     percent = (float)(ret->list_index-1) / (float)numHelpTopics;
  251.     XtCallCallbacks(helpScrollbar,"jumpProc",(XtPointer)&percent);
  252.     }
  253. }
  254.  
  255. /*ARGSUSED*/
  256. static
  257. ACTION_PROC(helpDownAction)
  258. {
  259.     XtCallActionProc(helpText,"next-page",event,NULL,0);
  260. }
  261.  
  262. /*ARGSUSED*/
  263. static
  264. ACTION_PROC(helpUpAction)
  265. {
  266.     XtCallActionProc(helpText,"previous-page",event,NULL,0);
  267. }
  268.  
  269.