home *** CD-ROM | disk | FTP | other *** search
- /* code to manage the stuff on the "database" menu.
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- #include <errno.h>
- #if defined(__STDC__)
- #include <stdlib.h>
- #endif
- #include <Xm/Xm.h>
- #include <Xm/Form.h>
- #include <Xm/Frame.h>
- #include <Xm/Label.h>
- #include <Xm/PushB.h>
- #include <Xm/ToggleB.h>
- #include <Xm/RowColumn.h>
- #include <Xm/SelectioB.h>
- #include "astro.h"
- #include "circum.h"
-
- #if defined(__STDC__) || defined(__cplusplus)
- #define P_(s) s
- #else
- #define P_(s) ()
- #endif
-
- extern Obj *db_next P_((Obj *op, HowNext how));
- extern int db_n P_((void));
- extern int db_read P_((FILE *fp, int append));
- extern void all_newdb P_((int appended));
- extern void get_xmstring P_((Widget w, char *resource, char **txtp));
- extern void hlp_dialog P_((char *tag, char *deflt[], int ndeflt));
- extern void prompt_map_cb P_((Widget w, XtPointer client, XtPointer call));
- extern void set_xmstring P_((Widget w, char *resource, char *txt));
- extern void watch_cursor P_((int want));
- extern void xe_msg P_((char *msg, int app_modal));
-
- void db_manage P_((void));
- void db_cursor P_((Cursor c));
- static void db_create_form P_((void));
- static void db_set_title P_((void));
- static FILE *fileopen P_((char *name));
- static void db_cb P_((Widget w, XtPointer client, XtPointer data));
- static void db_help_cb P_((Widget w, XtPointer client, XtPointer data));
-
- #undef P_
-
- extern char *sys_errlist[];
- extern Widget toplevel_w;
- extern char *myclass;
- #define XtD XtDisplay(toplevel_w)
-
- static Widget dbform_w;
-
- static char dbfdef[] = "ephem.db"; /* default database file name */
-
- void
- db_manage()
- {
- if (!dbform_w)
- db_create_form();
-
- if (XtIsManaged(dbform_w))
- XtUnmanageChild (dbform_w);
- else {
- db_set_title();
- XtManageChild (dbform_w);
- }
- }
-
- /* called to put up or remove the watch cursor. */
- void
- db_cursor (c)
- Cursor c;
- {
- Window win;
-
- if (dbform_w && (win = XtWindow(dbform_w))) {
- Display *dsp = XtDisplay(dbform_w);
- if (c)
- XDefineCursor (dsp, win, c);
- else
- XUndefineCursor (dsp, win);
- }
- }
-
- /* create a prompt dialog to allow user to enter a db file name. */
- static void
- db_create_form ()
- {
- Widget w;
- XmString title;
- Arg args[20];
- char *deffn;
- int n;
-
- title = XmStringCreateLtoR ("xephem Data Base",
- XmSTRING_DEFAULT_CHARSET);
- n = 0;
- XtSetArg(args[n], XmNdefaultPosition, False); n++;
- XtSetArg(args[n], XmNdialogTitle, title); n++;
- XtSetArg(args[n], XmNautoUnmanage, False); n++;
- dbform_w = XmCreatePromptDialog(toplevel_w, "DBPromptD", args, n);
- XtAddCallback (dbform_w, XmNmapCallback, prompt_map_cb, NULL);
- XmStringFree (title);
-
- get_xmstring (dbform_w, XmNtextString, &deffn);
- if (strlen (deffn) == 0)
- set_xmstring (dbform_w, XmNtextString, dbfdef);
- XtFree (deffn);
-
- /* use the Ok button to mean Append */
- XtAddCallback (dbform_w, XmNokCallback, db_cb, NULL);
- set_xmstring (dbform_w, XmNokLabelString, "Append");
-
- /* use the Apply button to mean Replace */
- XtAddCallback (dbform_w, XmNapplyCallback, db_cb, NULL);
- XtManageChild (XmSelectionBoxGetChild(dbform_w, XmDIALOG_APPLY_BUTTON));
- set_xmstring (dbform_w, XmNapplyLabelString, "Replace");
-
- /* allow cancel to unmanage and call it Close */
- XtAddCallback (dbform_w, XmNcancelCallback, db_cb, NULL);
- set_xmstring (dbform_w, XmNcancelLabelString, "Close");
-
- /* allow for help */
- /* connecting to XmNhelpCallback doesn't work */
- XtAddCallback (XmSelectionBoxGetChild(dbform_w, XmDIALOG_HELP_BUTTON),
- XmNactivateCallback, db_help_cb, NULL);
-
- #if XmVersion >= 1001
- w = XmSelectionBoxGetChild (dbform_w, XmDIALOG_TEXT);
- XmProcessTraversal (w, XmTRAVERSE_CURRENT);
- XmProcessTraversal (w, XmTRAVERSE_CURRENT); /* yes, twice!! */
- #endif
- }
-
- /* set up the title to the dialog.
- * this is a cheap way to indicate the number of objects in the db.
- */
- static void
- db_set_title()
- {
- char title[1024];
- Obj *op;
- int ne=0, np=0, nh=0;
- int nc=0, ng=0, nn=0, npn=0, nq=0, ns=0, no=0;
- int npl=0;
- int t=0;
-
- for (op= db_next((Obj *)NULL, OBJS_ALL); op; op= db_next(op, OBJS_ALL)){
- switch (op->type) {
- case FIXED:
- switch (op->f_class) {
- case 'C': case 'U': case 'O': nc++; t++; break;
- case 'G': case 'H': case 'A': ng++; t++; break;
- case 'N': case 'F': case 'K': nn++; t++; break;
- case 'P': npn++; t++; break;
- case 'Q': nq++; t++; break;
- case 'T': case 'B': case 'D': case 'M': case 'S': case 'V':
- ns++; t++; break;
- default: no++; t++; break;
- }
- break;
- case ELLIPTICAL: ne++; t++; break;
- case HYPERBOLIC: nh++; t++; break;
- case PARABOLIC: np++; t++; break;
- case PLANET: npl++; t++; break;
- case UNDEFOBJ: break;
- default:
- printf ("Unknown object type: %d\n", op->type);
- exit (1);
- }
- }
-
- if (db_n() - t > 2) {
- printf (">2 objects unaccounted for: t=%d n=%d\n", t, db_n());
- exit(1);
- }
-
- (void) sprintf (title,
- "%6d Sol -- elliptical\n\
- %6d Sol -- hyperbolic\n\
- %6d Sol -- parabolic\n\
- %6d Sol -- planets+sun+moon\n\
- %6d Clusters (C,U,O)\n\
- %6d Galaxies (G,H,A)\n\
- %6d Planetary Nebulea (P)\n\
- %6d Nebulea (N,F,K)\n\
- %6d Quasars (Q)\n\
- %6d Stars (S,V,D,B,M,T)\n\
- %6d Undefined\n\
- ------\n\
- %6d Total objects in memory\n\
- \n\
- Enter name of database file to read:",
- ne, nh, np, npl, nc, ng, npn, nn, nq, ns, no, t);
- set_xmstring (dbform_w, XmNselectionLabelString, title);
- }
-
- /* try to open name for read access.
- * if successful, return FILE *, else print a message and return NULL.
- */
- static FILE *
- fileopen (name)
- char *name;
- {
- FILE *fp;
-
- fp = fopen (name, "r");
- if (!fp) {
- char msg[128];
- (void) sprintf (msg, "Can not open %.75s: %.25s", name,
- sys_errlist[errno]);
- xe_msg (msg, 1);
- }
- return (fp);
- }
-
- /* callback from any of the buttons */
- /* ARGSUSED */
- static void
- db_cb (w, client, data)
- Widget w;
- XtPointer client;
- XtPointer data;
- {
- static char me[] = "db_cb()";
- XmSelectionBoxCallbackStruct *s = (XmSelectionBoxCallbackStruct *)data;
- char *sp;
- FILE *fp;
-
- watch_cursor(1);
-
- switch (s->reason) {
- case XmCR_OK: /* append */
- XmStringGetLtoR (s->value, XmSTRING_DEFAULT_CHARSET, &sp);
- fp = fileopen (sp);
- if (fp) {
- if (db_read (fp, 1) < 0) {
- char msg[128];
- (void) sprintf (msg, "Error reading `%.100s'", sp);
- xe_msg (msg, 1);
- }
- all_newdb(1);
- (void) fclose (fp);
- }
- XtFree (sp);
- db_set_title();
- break;
- case XmCR_APPLY: /* replace */
- XmStringGetLtoR (s->value, XmSTRING_DEFAULT_CHARSET, &sp);
- fp = fileopen (sp);
- if (fp) {
- if (db_read (fp, 0) < 0) {
- char msg[128];
- (void) sprintf (msg, "Error reading `%.100s'", sp);
- xe_msg (msg, 1);
- }
- all_newdb(0);
- (void) fclose (fp);
- }
- XtFree (sp);
- db_set_title();
- break;
- case XmCR_CANCEL:
- XtUnmanageChild (w);
- break;
- default:
- printf ("%s: Unknown reason = 0x%x\n", me, s->reason);
- exit(1);
- }
-
- watch_cursor(0);
- }
-
- /* ARGSUSED */
- static void
- db_help_cb (w, client, data)
- Widget w;
- XtPointer client;
- XtPointer data;
- {
- static char *msg[] = {
- "This displays a count of the various types of objects currently in memory.",
- "Database files may be read in to replace to add to this list."
- };
-
- hlp_dialog ("DataBase menu", msg, XtNumber(msg));
- }
-