home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xephem / part20 / dbmenu.c next >
Encoding:
C/C++ Source or Header  |  1993-05-15  |  7.0 KB  |  291 lines

  1. /* code to manage the stuff on the "database" menu.
  2.  */
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7. #include <errno.h>
  8. #if defined(__STDC__)
  9. #include <stdlib.h>
  10. #endif
  11. #include <Xm/Xm.h>
  12. #include <Xm/Form.h>
  13. #include <Xm/Frame.h>
  14. #include <Xm/Label.h>
  15. #include <Xm/PushB.h>
  16. #include <Xm/ToggleB.h>
  17. #include <Xm/RowColumn.h>
  18. #include <Xm/SelectioB.h>
  19. #include "astro.h"
  20. #include "circum.h"
  21.  
  22. #if defined(__STDC__) || defined(__cplusplus)
  23. #define P_(s) s
  24. #else
  25. #define P_(s) ()
  26. #endif
  27.  
  28. extern Obj *db_next P_((Obj *op, HowNext how));
  29. extern int db_n P_((void));
  30. extern int db_read P_((FILE *fp, int append));
  31. extern void all_newdb P_((int appended));
  32. extern void get_xmstring P_((Widget w, char *resource, char **txtp));
  33. extern void hlp_dialog P_((char *tag, char *deflt[], int ndeflt));
  34. extern void prompt_map_cb P_((Widget w, XtPointer client, XtPointer call));
  35. extern void set_xmstring P_((Widget w, char *resource, char *txt));
  36. extern void watch_cursor P_((int want));
  37. extern void xe_msg P_((char *msg, int app_modal));
  38.  
  39. void db_manage P_((void));
  40. void db_cursor P_((Cursor c));
  41. static void db_create_form P_((void));
  42. static void db_set_title P_((void));
  43. static FILE *fileopen P_((char *name));
  44. static void db_cb P_((Widget w, XtPointer client, XtPointer data));
  45. static void db_help_cb P_((Widget w, XtPointer client, XtPointer data));
  46.  
  47. #undef P_
  48.  
  49. extern char *sys_errlist[];
  50. extern Widget    toplevel_w;
  51. extern char *myclass;
  52. #define    XtD    XtDisplay(toplevel_w)
  53.  
  54. static Widget dbform_w;
  55.  
  56. static char dbfdef[] = "ephem.db";      /* default database file name */
  57.  
  58. void
  59. db_manage()
  60. {
  61.     if (!dbform_w)
  62.         db_create_form();
  63.  
  64.     if (XtIsManaged(dbform_w))
  65.         XtUnmanageChild (dbform_w);
  66.     else {
  67.         db_set_title();
  68.         XtManageChild (dbform_w);
  69.     }
  70. }
  71.  
  72. /* called to put up or remove the watch cursor.  */
  73. void
  74. db_cursor (c)
  75. Cursor c;
  76. {
  77.     Window win;
  78.  
  79.     if (dbform_w && (win = XtWindow(dbform_w))) {
  80.         Display *dsp = XtDisplay(dbform_w);
  81.         if (c)
  82.         XDefineCursor (dsp, win, c);
  83.         else
  84.         XUndefineCursor (dsp, win);
  85.     }
  86. }
  87.  
  88. /* create a prompt dialog to allow user to enter a db file name. */
  89. static void
  90. db_create_form ()
  91. {
  92.     Widget w;
  93.     XmString title;
  94.     Arg args[20];
  95.     char *deffn;
  96.     int n;
  97.     
  98.     title = XmStringCreateLtoR ("xephem Data Base",
  99.                             XmSTRING_DEFAULT_CHARSET);
  100.     n = 0;
  101.     XtSetArg(args[n], XmNdefaultPosition, False);  n++;
  102.     XtSetArg(args[n], XmNdialogTitle, title);  n++;
  103.     XtSetArg(args[n], XmNautoUnmanage, False);  n++;
  104.     dbform_w = XmCreatePromptDialog(toplevel_w, "DBPromptD", args, n);
  105.     XtAddCallback (dbform_w, XmNmapCallback, prompt_map_cb, NULL);
  106.     XmStringFree (title);
  107.  
  108.     get_xmstring (dbform_w, XmNtextString, &deffn);
  109.     if (strlen (deffn) == 0)
  110.         set_xmstring (dbform_w, XmNtextString, dbfdef);
  111.     XtFree (deffn);
  112.  
  113.     /* use the Ok button to mean Append */
  114.     XtAddCallback (dbform_w, XmNokCallback, db_cb, NULL);
  115.     set_xmstring (dbform_w, XmNokLabelString, "Append");
  116.  
  117.     /* use the Apply button to mean Replace */
  118.     XtAddCallback (dbform_w, XmNapplyCallback, db_cb, NULL);
  119.     XtManageChild (XmSelectionBoxGetChild(dbform_w, XmDIALOG_APPLY_BUTTON));
  120.     set_xmstring (dbform_w, XmNapplyLabelString, "Replace");
  121.  
  122.     /* allow cancel to unmanage and call it Close */
  123.     XtAddCallback (dbform_w, XmNcancelCallback, db_cb, NULL);
  124.     set_xmstring (dbform_w, XmNcancelLabelString, "Close");
  125.  
  126.     /* allow for help */
  127.     /* connecting to XmNhelpCallback doesn't work */
  128.     XtAddCallback (XmSelectionBoxGetChild(dbform_w, XmDIALOG_HELP_BUTTON),
  129.                     XmNactivateCallback, db_help_cb, NULL);
  130.  
  131. #if XmVersion >= 1001
  132.     w = XmSelectionBoxGetChild (dbform_w, XmDIALOG_TEXT);
  133.     XmProcessTraversal (w, XmTRAVERSE_CURRENT);
  134.     XmProcessTraversal (w, XmTRAVERSE_CURRENT); /* yes, twice!! */
  135. #endif
  136. }
  137.  
  138. /* set up the title to the dialog.
  139.  * this is a cheap way to indicate the number of objects in the db.
  140.  */
  141. static void
  142. db_set_title()
  143. {
  144.     char title[1024];
  145.     Obj *op;
  146.     int ne=0, np=0, nh=0;
  147.     int nc=0, ng=0, nn=0, npn=0, nq=0, ns=0, no=0;
  148.     int npl=0;
  149.     int t=0;
  150.  
  151.     for (op= db_next((Obj *)NULL, OBJS_ALL); op; op= db_next(op, OBJS_ALL)){
  152.         switch (op->type) {
  153.         case FIXED:
  154.         switch (op->f_class) {
  155.         case 'C': case 'U': case 'O': nc++; t++; break;
  156.         case 'G': case 'H': case 'A': ng++; t++; break;
  157.         case 'N': case 'F': case 'K': nn++; t++; break;
  158.         case 'P': npn++; t++; break;
  159.         case 'Q': nq++; t++; break;
  160.         case 'T': case 'B': case 'D': case 'M': case 'S': case 'V': 
  161.             ns++; t++; break;
  162.         default: no++; t++; break;
  163.         }
  164.         break;
  165.         case ELLIPTICAL: ne++; t++; break;
  166.         case HYPERBOLIC: nh++; t++; break;
  167.         case PARABOLIC: np++; t++; break;
  168.         case PLANET: npl++; t++; break;
  169.         case UNDEFOBJ: break;
  170.         default:
  171.         printf ("Unknown object type: %d\n", op->type);
  172.         exit (1);
  173.         }
  174.     }
  175.  
  176.     if (db_n() - t > 2) {
  177.         printf (">2 objects unaccounted for: t=%d n=%d\n", t, db_n());
  178.         exit(1);
  179.     }
  180.  
  181.     (void) sprintf (title,
  182. "%6d Sol -- elliptical\n\
  183. %6d Sol -- hyperbolic\n\
  184. %6d Sol -- parabolic\n\
  185. %6d Sol -- planets+sun+moon\n\
  186. %6d Clusters (C,U,O)\n\
  187. %6d Galaxies (G,H,A)\n\
  188. %6d Planetary Nebulea (P)\n\
  189. %6d Nebulea (N,F,K)\n\
  190. %6d Quasars (Q)\n\
  191. %6d Stars (S,V,D,B,M,T)\n\
  192. %6d Undefined\n\
  193. ------\n\
  194. %6d Total objects in memory\n\
  195.  \n\
  196. Enter name of database file to read:",
  197.         ne, nh, np, npl, nc, ng, npn, nn, nq, ns, no, t);
  198.     set_xmstring (dbform_w, XmNselectionLabelString, title);
  199. }
  200.  
  201. /* try to open name for read access.
  202.  * if successful, return FILE *, else print a message and return NULL.
  203.  */
  204. static FILE *
  205. fileopen (name)
  206. char *name;
  207. {
  208.     FILE *fp;
  209.     
  210.     fp = fopen (name, "r");
  211.     if (!fp) {
  212.         char msg[128];
  213.         (void) sprintf (msg, "Can not open %.75s: %.25s", name,
  214.                             sys_errlist[errno]);
  215.         xe_msg (msg, 1);
  216.     }
  217.     return (fp);
  218. }
  219.  
  220. /* callback from any of the buttons */
  221. /* ARGSUSED */
  222. static void
  223. db_cb (w, client, data)
  224. Widget w;
  225. XtPointer client;
  226. XtPointer data;
  227. {
  228.     static char me[] = "db_cb()";
  229.     XmSelectionBoxCallbackStruct *s = (XmSelectionBoxCallbackStruct *)data;
  230.     char *sp;
  231.     FILE *fp;
  232.  
  233.     watch_cursor(1);
  234.  
  235.     switch (s->reason) {
  236.     case XmCR_OK: /* append */
  237.         XmStringGetLtoR (s->value, XmSTRING_DEFAULT_CHARSET, &sp);
  238.         fp = fileopen (sp);
  239.         if (fp) {
  240.         if (db_read (fp, 1) < 0) {
  241.             char msg[128];
  242.             (void) sprintf (msg, "Error reading `%.100s'", sp);
  243.             xe_msg (msg, 1);
  244.         }
  245.         all_newdb(1);
  246.         (void) fclose (fp);
  247.         }
  248.         XtFree (sp);
  249.         db_set_title();
  250.         break;
  251.     case XmCR_APPLY:    /* replace */
  252.         XmStringGetLtoR (s->value, XmSTRING_DEFAULT_CHARSET, &sp);
  253.         fp = fileopen (sp);
  254.         if (fp) {
  255.         if (db_read (fp, 0) < 0) {
  256.             char msg[128];
  257.             (void) sprintf (msg, "Error reading `%.100s'", sp);
  258.             xe_msg (msg, 1);
  259.         }
  260.         all_newdb(0);
  261.         (void) fclose (fp);
  262.         }
  263.         XtFree (sp);
  264.         db_set_title();
  265.         break;
  266.     case XmCR_CANCEL:
  267.         XtUnmanageChild (w);
  268.         break;
  269.     default:
  270.         printf ("%s: Unknown reason = 0x%x\n", me, s->reason);
  271.         exit(1);
  272.     }
  273.  
  274.     watch_cursor(0);
  275. }
  276.  
  277. /* ARGSUSED */
  278. static void
  279. db_help_cb (w, client, data)
  280. Widget w;
  281. XtPointer client;
  282. XtPointer data;
  283. {
  284.     static char *msg[] = {
  285. "This displays a count of the various types of objects currently in memory.",
  286. "Database files may be read in to replace to add to this list."
  287. };
  288.  
  289.     hlp_dialog ("DataBase menu", msg, XtNumber(msg));
  290. }
  291.