home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE3.TAR / xarchie-2.0.1 / file-panel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-22  |  5.7 KB  |  271 lines

  1. /*
  2.  * file-panel.c : The save-load-write panel
  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/MenuButton.h>
  13. #include <X11/Xaw/Label.h>
  14. #ifdef FILECHOOSER
  15. # include <FChooser.h>
  16. #endif
  17. #include "config.h"
  18. #ifdef HAVE_SYS_PARAM_H
  19. #include <sys/param.h>
  20. #endif
  21. #include "xarchie.h"
  22. #include "db.h"
  23. #include "browser.h"
  24. #include "appres.h"
  25. #include "saveload.h"
  26. #include "file-panel.h"
  27. #include "fchooser.h"
  28. #include "status.h"
  29. #include "xutil.h"
  30. extern DbEntry *db;
  31.  
  32. /*
  33.  * Functions defined here
  34.  */
  35. void initFilePanelActions();
  36. void updateFileWriteMode();
  37. void setFileShellState();
  38.  
  39. static void fileSaveAction(),fileLoadAction(),fileWriteAction();
  40. static void popupFilePanel(),initFileWidgets();
  41. static void filePanelOk(),filePanelCancel();
  42. /*
  43. static FileChooserOkProc filePanelOk;
  44. static FileChooserCancelProc filePanelCancel;
  45. */
  46. static void writeModeWidgetsShown();
  47.  
  48. /*
  49.  * Data defined here
  50.  */
  51. static FileChooserInfo *fcinfo;
  52. static Widget fileShell,filePanelLabel;
  53. static Widget filePanelWriteModeButton,filePanelWriteModeLabel;
  54. static Boolean fileGoing,isPoppedUp;
  55. static int fileOp;
  56.  
  57. static XtActionsRec actionTable[] = {
  58.     { "file-save",        fileSaveAction },
  59.     { "file-load",        fileLoadAction },
  60.     { "file-write",        fileWriteAction },
  61. };
  62.  
  63. void
  64. initFilePanelActions()
  65. {
  66.     XtAppAddActions(appContext,actionTable,XtNumber(actionTable));
  67. }
  68.  
  69. /*    -    -    -    -    -    -    -    -    */
  70. /* Action Procedures */
  71.  
  72. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  73.                     Widget w; \
  74.                     XEvent *event; \
  75.                     String *params; \
  76.                     Cardinal *num_params;
  77.  
  78. /*ARGSUSED*/
  79. static
  80. ACTION_PROC(fileSaveAction)
  81. {
  82.     popupFilePanel(FILE_SAVE);
  83. }
  84.  
  85. /*ARGSUSED*/
  86. static
  87. ACTION_PROC(fileLoadAction)
  88. {
  89.     popupFilePanel(FILE_LOAD);
  90. }
  91.  
  92. /*ARGSUSED*/
  93. static
  94. ACTION_PROC(fileWriteAction)
  95. {
  96.     popupFilePanel(FILE_WRITE);
  97. }
  98.  
  99. static void
  100. popupFilePanel(op)
  101. int op;
  102. {
  103.     /* Ignore if the browser is being changed */
  104.     if (getBrowserState() != BROWSER_READY) {
  105.     XBell(display,0);
  106.     return;
  107.     }
  108.     /* Create if this is the first time */
  109.     if (fileShell == NULL) {
  110.     setBusyStatus(True);
  111.     initFileWidgets();
  112.     setBusyStatus(False);
  113.     }
  114. #ifdef FILECHOOSER
  115.       else {
  116.       status0("Re-initializing File Selector...");
  117.       XfwfFileChooserRefresh((XfwfFileChooserWidget)(fcinfo->fcw));
  118.     }
  119. #endif FILECHOOSER
  120.     switch (op) {
  121.     case FILE_SAVE:
  122.         setWidgetLabel(filePanelLabel,"Save to file:");
  123.         writeModeWidgetsShown(False);
  124.         break;
  125.     case FILE_LOAD:
  126.         setWidgetLabel(filePanelLabel,"Load from file:");
  127.         writeModeWidgetsShown(False);
  128.         break;
  129.     case FILE_WRITE:
  130.         setWidgetLabel(filePanelLabel,"Write to file:");
  131.         writeModeWidgetsShown(True);
  132.         break;
  133.     }
  134.     fileOp = op;
  135.     if (isPoppedUp) {
  136.     XRaiseWindow(display,XtWindow(fileShell));
  137.     } else {
  138.     isPoppedUp = True;
  139.     XtPopup(fileShell,XtGrabNone);
  140.     }
  141. }
  142.  
  143. static void
  144. initFileWidgets()
  145. {
  146.     Widget form;
  147.     Arg args[1];
  148.  
  149.     fileShell = XtCreatePopupShell("filePanelShell",topLevelShellWidgetClass,
  150.                    toplevel,NULL,0);
  151.     form = XtCreateManagedWidget("filePanelForm",formWidgetClass,
  152.                  fileShell,NULL,0);
  153.     filePanelLabel = XtCreateManagedWidget("filePanelLabel",labelWidgetClass,
  154.                        form,NULL,0);
  155.     filePanelWriteModeButton =
  156.     XtCreateManagedWidget("filePanelWriteModeButton",menuButtonWidgetClass,
  157.                   form,NULL,0);
  158.     filePanelWriteModeLabel =
  159.     XtCreateManagedWidget("filePanelWriteModeLabel",labelWidgetClass,
  160.                   form,NULL,0);
  161.     fcinfo = createFileChooser(fileShell,form,"filePanel",filePanelOk,
  162.                    filePanelCancel,(XtPointer)fileShell);
  163.     /* Adjust vertical layout */
  164.     XtSetArg(args[0],XtNfromVert,filePanelWriteModeButton);
  165. #ifdef FILECHOOSER
  166.     XtSetValues(fcinfo->fcw,args,1);
  167. #else
  168.     XtSetValues(fcinfo->text,args,1);
  169. #endif
  170.     /* Realize them all */
  171.     XtRealizeWidget(fileShell);
  172.     /* Register window for WM */
  173.     (void)XSetWMProtocols(XtDisplay(fileShell),XtWindow(fileShell),
  174.               &WM_DELETE_WINDOW,1);
  175.     updateFileWriteMode(appResources.fileWriteOnePerLine);
  176. }
  177.  
  178. static void
  179. writeModeWidgetsShown(state)
  180. Boolean state;
  181. {
  182.     if (state) {
  183.     XtMapWidget(filePanelWriteModeButton);
  184.     XtMapWidget(filePanelWriteModeLabel);
  185.     } else {
  186.     XtUnmapWidget(filePanelWriteModeButton);
  187.     XtUnmapWidget(filePanelWriteModeLabel);
  188.     }
  189. }
  190.  
  191. void
  192. setFileShellState(state)
  193. int state;
  194. {
  195.     if (!isPoppedUp)
  196.     return;
  197.     switch (state) {
  198.     case NormalState:
  199.         XtMapWidget(fileShell);
  200.         break;
  201.     case IconicState:
  202.         XtUnmapWidget(fileShell);
  203.         break;
  204.     }
  205. }
  206.  
  207. /*    -    -    -    -    -    -    -    -    */
  208. /* Callbacks from the FileChooser */
  209.  
  210. /*ARGSUSED*/
  211. static void
  212. filePanelOk(fcinfo,filename,client_data)
  213. FileChooserInfo *fcinfo;
  214. char *filename;
  215. XtPointer client_data;        /* shell Widget */
  216. {
  217.     Widget shell = (Widget)client_data;
  218.     int status;
  219.  
  220.     if (fileGoing) {
  221.     XBell(display,0);
  222.     return;
  223.     }
  224.     fileGoing = True;
  225.     switch (fileOp) {
  226.     case FILE_SAVE:
  227.         status = save(db,filename);
  228.         break;
  229.     case FILE_LOAD:
  230.         status = load(db,filename);
  231.         break;
  232.     case FILE_WRITE:
  233.         status = writeToFile(db,filename,appResources.fileWriteOnePerLine);
  234.         break;
  235.     }
  236.     fileGoing = False;
  237.     if (status) {
  238.     XtPopdown(shell);
  239.     isPoppedUp = False;
  240.     }
  241. }
  242.  
  243. /*ARGSUSED*/
  244. static void
  245. filePanelCancel(fcinfo,client_data)
  246. FileChooserInfo *fcinfo;
  247. XtPointer client_data;        /* shell Widget */
  248. {
  249.     Widget shell = (Widget)client_data;
  250.  
  251.     if (fileGoing) {
  252.     XBell(display,0);
  253.     return;
  254.     }
  255.     XtPopdown(shell);
  256.     isPoppedUp = False;
  257. }
  258.  
  259. /*    -    -    -    -    -    -    -    -    */
  260. /* Used by the menus */
  261.  
  262. void
  263. updateFileWriteMode(flag)
  264. Boolean flag;
  265. {
  266.     if (flag)
  267.     setWidgetLabel(filePanelWriteModeLabel,"One entry per line");
  268.     else
  269.     setWidgetLabel(filePanelWriteModeLabel,"Pretty-printed");
  270. }
  271.