home *** CD-ROM | disk | FTP | other *** search
- /*
- * xkal.c : graphical appoinment tool
- *
- * George Ferguson, ferguson@cs.rochester.edu, 27 Oct 1990.
- *
- * $Id: xkal.c,v 1.10 90/11/12 13:14:52 ferguson Exp $
- *
- */
- static char *rcsid = "$Id: xkal.c,v 1.10 90/11/12 13:14:52 ferguson Exp $";
- #include <stdio.h>
- #include <ctype.h>
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <X11/Xmu/Drawing.h>
- #include <X11/Xaw/Form.h>
- #include <X11/Xaw/MenuButton.h>
- #include <X11/Xaw/Cardinals.h>
- #include "EzMenu.h"
- #include "app-resources.h"
- #include "resources.h"
- #include "month.h"
- #include "day.h"
- #include "schedule.h"
- #include "db.h"
- #include "util.h"
- #include "date-strings.h"
-
- /* - - - - - - - - */
- /*
- * Functions defined in this file:
- */
- int main();
- int countAppoints();
-
- static void initGraphics(), initGCs(), initWidgets();
- static void createMonthFormDatas(), setMonths();
- static void quit(), quit_nosave(), next(), prev(), today(), setNumMonths();
- static void save(), editSchedule();
- static void syntax(),listAppoints();
- static Pixmap readBitmap();
-
- /*
- * Global graphics data
- */
- Display *display;
- Screen *screen;
- Window root;
- GC dateGC1,dateGC3,dateGC12,emptyGC,shadeGC[23];
-
- /*
- * Global widget data
- */
- XtAppContext app_con;
- Widget toplevel;
- Widget bothForm,allForm;
- AppResources appResources;
- MonthFormData *monthFormData1,*monthFormData3[3],*monthFormData12[12];
-
- /*
- * Other global data
- */
- char *program;
- int currentDay,currentMon,currentYear; /* today */
- int mainDay,mainMon,mainYear; /* display */
- int appointsChanged = False;
- int numMonths;
-
- /*
- * Action binding table
- */
- static XtActionsRec cmdActionsTable[] = {
- { "xkal-quit", quit },
- { "xkal-exit", quit_nosave },
- { "xkal-save", save },
- { "xkal-next", next },
- { "xkal-prev", prev },
- { "xkal-today", today },
- { "xkal-numMonths", setNumMonths },
- { "xkal-edit-schedule", editSchedule },
- };
-
- /*
- * Widget and non-widget resources if the application defaults
- * file can't be found.
- * [ Generated automatically from Xkal.ad. ]
- */
- static String fallbackResources[] = {
- #include "Xkal.ad.h"
- NULL
- };
-
- /* - - - - - - - - */
-
- int
- main(argc, argv)
- int argc;
- char **argv;
- {
- program = *argv;
- initGraphics(&argc,argv);
- initDb();
- getCurrentDate(¤tDay,¤tMon,¤tYear);
- readAppoints(appResources.appoints);
- if (appResources.date != NULL && *(appResources.date) != '\0')
- parseDate(appResources.date,¤tDay,¤tMon,¤tYear);
- if (appResources.silent) {
- XtDestroyApplicationContext(app_con);
- exit(countAppoints(currentDay,currentMon,currentYear));
- }
- if (appResources.listOnly) {
- listAppoints(currentDay,currentMon,currentYear);
- XtDestroyApplicationContext(app_con);
- exit(countAppoints(currentDay,currentMon,currentYear));
- }
- mainDay = currentDay;
- mainMon = currentMon;
- mainYear = currentYear;
- numMonths = appResources.numMonths;
- initGCs();
- initWidgets();
- XtRealizeWidget(toplevel);
- if (currentDayFormData != NULL) {
- switch (numMonths) {
- case 1: selectDay(monthFormData1,mainDay,mainMon,mainYear);
- break;
- case 3: selectDay(monthFormData3[1],mainDay,mainMon,mainYear);
- break;
- case 12: selectDay(monthFormData12[mainMon],mainDay,mainMon,
- mainYear);
- break;
- }
- }
- XtAppMainLoop(app_con);
- }
-
- static void
- initGraphics(argcp,argv)
- int *argcp;
- char **argv;
- {
- char *rev,*strchr();
-
- toplevel = XtAppInitialize(&app_con, "Xkal",
- options, XtNumber(options),
- argcp,argv,fallbackResources,NULL,ZERO);
- if (*argcp > 1) {
- syntax(*argcp,argv);
- XtDestroyApplicationContext(app_con);
- exit(-1);
- }
- XtGetApplicationResources(toplevel,(XtPointer)&appResources,
- resources,XtNumber(resources),NULL,ZERO);
- rev = strchr(fallbackResources[0],'$');
- if (strcmp(appResources.revision,rev) != 0) {
- fprintf(stderr,"%s: app-defaults release not %s\n",program,rev);
- fprintf(stderr,"%s: you may have an outdated app-defaults file\n",
- program);
- }
- XtAppAddActions(app_con,cmdActionsTable,XtNumber(cmdActionsTable));
- display = XtDisplay(toplevel);
- screen = XtScreen(toplevel);
- root = RootWindowOfScreen(screen);
- }
-
- static void
- initGCs()
- {
- XGCValues values;
- XtGCMask mask;
- int i;
-
- values.foreground = (unsigned long)1;
- values.background = (unsigned long)0;
- values.function = GXset;
- mask = GCForeground | GCBackground | GCFunction | GCFont;
- values.font = appResources.dateFont1;
- dateGC1 = XCreateGC(display,root,mask,&values);
- values.font = appResources.dateFont3;
- dateGC3 = XCreateGC(display,root,mask,&values);
- values.font = appResources.dateFont12;
- dateGC12 = XCreateGC(display,root,mask,&values);
- values.function = GXcopy;
- values.fill_style = FillTiled;
- values.tile = readBitmap(appResources.noDayShade);
- mask = GCForeground | GCBackground | GCFunction | GCFillStyle | GCTile;
- emptyGC = XCreateGC(display,root,mask,&values);
- values.function = GXcopy;
- mask = GCFunction;
- shadeGC[0] = XCreateGC(display,root,mask,&values);
- for (i=1; i < 23; i++) {
- if (DefaultDepthOfScreen(screen) > 1) { /* COLOR */
- values.foreground = appResources.color[i];
- values.fill_style = FillSolid;
- values.function = GXcopy;
- mask = GCForeground | GCBackground | GCFunction;
- } else { /* B&W */
- values.foreground = (unsigned long)1;
- values.background = (unsigned long)0;
- values.tile = readBitmap(appResources.shade[i]);
- values.fill_style = FillTiled;
- values.function = GXcopy;
- mask = GCForeground | GCBackground | GCTile | GCFillStyle |
- GCFunction;
- }
- shadeGC[i] = XCreateGC(display,root,mask,&values);
- }
- }
-
- static void
- initWidgets()
- {
- char *s,name[256];
- int i;
-
- bothForm = XtCreateManagedWidget("bothForm",formWidgetClass,toplevel,
- NULL,ZERO);
- XtCreateManagedWidget("fileButton",menuButtonWidgetClass,bothForm,
- NULL,ZERO);
- XtCreatePopupShell("fileMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
- XtCreateManagedWidget("viewButton",menuButtonWidgetClass,bothForm,
- NULL,ZERO);
- XtCreatePopupShell("viewMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
- XtCreateManagedWidget("otherButton",menuButtonWidgetClass,bothForm,
- NULL,ZERO);
- XtCreatePopupShell("otherMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
- allForm = XtCreateManagedWidget("allMonthsForm",formWidgetClass,bothForm,
- NULL,ZERO);
- createMonthFormDatas(numMonths);
- if (appResources.bothShown)
- currentDayFormData = createDayFormData(bothForm);
- setMonths();
- }
-
- static void
- createMonthFormDatas(n)
- int n;
- {
- char *name = "monthFormXX_XX";
- int i;
-
- switch (n) {
- case 1: monthFormData1 = createMonthFormData(allForm,"monthForm1",1);
- XtManageChild(monthFormData1->form);
- break;
- case 3: for (i=0; i < 3; i++) {
- sprintf(name,"monthForm3_%d",i+1);
- monthFormData3[i] = createMonthFormData(allForm,name,3);
- XtManageChild(monthFormData3[i]->form);
- }
- break;
- case 12: for (i=0; i < 12; i++) {
- sprintf(name,"monthForm12_%d",i+1);
- monthFormData12[i] = createMonthFormData(allForm,name,12);
- XtManageChild(monthFormData12[i]->form);
- }
- break;
- }
- }
-
- static void
- setMonths()
- {
- Arg args[2];
- int i,prevMon,prevYear,nextMon,nextYear;
-
- XawFormDoLayout(allForm,False);
- switch (numMonths) {
- case 1: setMonthFormData(monthFormData1,1,mainMon,mainYear);
- break;
- case 3: prevMon = mainMon - 1;
- nextMon = mainMon + 1;
- prevYear = nextYear = mainYear;
- if (mainMon == 0) {
- prevMon = 11;
- prevYear = mainYear - 1;
- } else if (mainMon == 11) {
- nextMon = 0;
- nextYear = mainYear + 1;
- }
- setMonthFormData(monthFormData3[0],3,prevMon,prevYear);
- setMonthFormData(monthFormData3[1],3,mainMon,mainYear);
- setMonthFormData(monthFormData3[2],3,nextMon,nextYear);
- break;
- case 12: for (i=0; i < 12; i++)
- setMonthFormData(monthFormData12[i],12,i,mainYear);
- break;
- }
- XawFormDoLayout(allForm,True);
- }
-
- /* - - - - - - - - */
-
- #define ACTION_PROC(NAME) static void NAME(w,event,params,num_params) \
- Widget w; \
- XEvent *event; \
- String *params; \
- Cardinal *num_params;
-
- ACTION_PROC(quit)
- {
- if (currentDayFormData != NULL)
- checkpointAppoints(currentDayFormData);
- if (appointsChanged)
- writeAppoints(appResources.appoints);
- XtDestroyApplicationContext(app_con);
- exit(0);
- }
-
- ACTION_PROC(quit_nosave)
- {
- XtDestroyApplicationContext(app_con);
- exit(0);
- }
-
- ACTION_PROC(next)
- {
- if (numMonths == 1 || numMonths == 3) {
- if (mainMon == 11) {
- mainMon = 0;
- mainYear += 1;
- } else {
- mainMon += 1;
- }
- } else {
- mainYear += 1;
- }
- setMonths();
- }
-
- ACTION_PROC(prev)
- {
- if (numMonths == 1 || numMonths == 3) {
- if (mainMon == 0) {
- mainMon = 11;
- mainYear -= 1;
- } else {
- mainMon -= 1;
- }
- } else {
- mainYear -= 1;
- }
- setMonths();
- }
-
- ACTION_PROC(today)
- {
- mainDay = currentDay;
- mainMon = currentMon;
- mainYear = currentYear;
- setMonths();
- switch (numMonths) {
- case 1: selectDay(monthFormData1,mainDay,mainMon,mainYear);
- break;
- case 3: selectDay(monthFormData3[1],mainDay,mainMon,mainYear);
- break;
- case 12: selectDay(monthFormData12[mainMon],mainDay,mainMon,mainYear);
- break;
- }
- }
-
- ACTION_PROC(setNumMonths)
- {
- int n,i;
-
- if (*num_params != ONE) {
- fprintf(stderr,"%s: bad parms to xkal-numMonths()\n",program);
- return;
- }
- if ((n=atoi(params[0])) == numMonths)
- return;
- XawFormDoLayout(bothForm,False);
- switch (numMonths) {
- case 1: XtUnmanageChild(monthFormData1->form);
- break;
- case 3: for (i=0; i < 3; i++)
- XtUnmanageChild(monthFormData3[i]->form);
- break;
- case 12: for (i=0; i < 12; i++)
- XtUnmanageChild(monthFormData12[i]->form);
- break;
- }
- numMonths = n;
- switch (numMonths) {
- case 1: if (monthFormData1 == NULL)
- createMonthFormDatas(1);
- setMonths();
- XtManageChild(monthFormData1->form);
- break;
- case 3: if (monthFormData3[0] == NULL)
- createMonthFormDatas(3);
- setMonths();
- for (i=0; i < 3; i++)
- XtManageChild(monthFormData3[i]->form);
- break;
- case 12: if (monthFormData12[0] == NULL)
- createMonthFormDatas(12);
- setMonths();
- for (i=0; i < 12; i++)
- XtManageChild(monthFormData12[i]->form);
- break;
- }
- XawFormDoLayout(bothForm,True);
- }
-
- ACTION_PROC(save)
- {
- if (currentDayFormData != NULL)
- checkpointAppoints(currentDayFormData);
- if (appointsChanged)
- writeAppoints(appResources.appoints);
- appointsChanged = False;
- }
-
- ACTION_PROC(editSchedule)
- {
- createPopupSchedule();
- }
-
- /* - - - - - - - - */
-
- static void
- listAppoints(day,mon,year)
- int day,mon,year;
- {
- int i,dow;
- char *s;
-
- dow = computeDOW(day,mon,year);
- printf("%s %d %s %d\n",shortDayStr[dow],day,shortMonStr[mon],year+1900);
- for (i=0; i < 20; i++) {
- if ((s=lookup(i,day,mon,year)) != NULL)
- if (i % 2 == 0)
- printf("\t%2d:00 %s\n",8+(i / 2),s);
- else
- printf("\t%2d:30 %s\n",8+((i-1)/2),s);
- }
- for (i=20; i < 22; i++) {
- if ((s=lookup(i,day,mon,year)) != NULL)
- printf("\tNOTES %s\n",s);
- }
- }
-
- int
- countAppoints(day,mon,year)
- int day,mon,year;
- {
- int i,n;
-
- n = 0;
- for (i=0; i < 22; i++) {
- if (lookupAppoint(i,day,mon,year) != NULL)
- n += 1;
- }
- return(n);
- }
-
- /* - - - - - - - - */
-
- static Pixmap
- readBitmap(name)
- char *name;
- {
- Pixmap b;
- int w,h,xhot,yhot;
-
- b = XmuLocateBitmapFile(screen,name,NULL,ZERO,&w,&h,&xhot,&yhot);
- if (b == NULL) {
- fprintf(stderr,"%s: couldn't find bitmap \"%s\"\n",program,name);
- return(XCreatePixmap(display,screen,2,2,DefaultDepthOfScreen(screen)));
- } else
- return(XmuCreatePixmapFromBitmap(display,root,b,w,h,
- DefaultDepthOfScreen(screen),1,0));
- }
-
- /* - - - - - - - - */
- /*
- * syntax() : print the usage message
- */
- static void
- syntax(argc,argv)
- int argc;
- char **argv;
- {
- argv += 1;
- if (argc > 2 || (strcmp(*argv,"-help") != 0 && strcmp(*argv,"-?") != 0)) {
- fprintf(stderr,"%s: bad argument(s): ",program);
- while (--argc)
- fprintf(stderr,"%s ",*argv++);
- fprintf(stderr,"\n");
- }
- fprintf(stderr,"usage: %s",program);
- fprintf(stderr,"\t-appoints file\tuse file for appointments\n");
- fprintf(stderr,"\t\t-date date\tuse given date for `today'\n");
- fprintf(stderr,"\t\t-numMonths 1|3|12\tstart displaying 1, 3 or 12 months\n");
- fprintf(stderr,"\t\t-bothShown\tput day display next to month(s)\n");
- fprintf(stderr,"\t\t-nobothShown\tpopup days as needed\n");
- fprintf(stderr,"\t\t-listOnly\tprint appoints to stdout\n");
- fprintf(stderr,"\t\t\t-silent\texit with status == number of appoints\n");
- fprintf(stderr,"\t\t-opaqueDates\tprint day numbers opaquely\n");
- fprintf(stderr,"\t\t-noopaqueDates\tprint day numbers transparently\n");
- fprintf(stderr,"\t\t-dowLabels\tprint day of week at top of columns\n");
- fprintf(stderr,"\t\t-nodowLabels\tdon't print dow at top of columns\n");
- }
-