home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2059 / xkal.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  12.7 KB  |  504 lines

  1. /*
  2.  *    xkal.c : graphical appoinment tool
  3.  *
  4.  *    George Ferguson, ferguson@cs.rochester.edu,  27 Oct 1990.
  5.  *
  6.  *    $Id: xkal.c,v 1.10 90/11/12 13:14:52 ferguson Exp $
  7.  *
  8.  */
  9. static char *rcsid = "$Id: xkal.c,v 1.10 90/11/12 13:14:52 ferguson Exp $";
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <X11/Intrinsic.h>
  13. #include <X11/StringDefs.h>
  14. #include <X11/Xmu/Drawing.h>
  15. #include <X11/Xaw/Form.h>
  16. #include <X11/Xaw/MenuButton.h>
  17. #include <X11/Xaw/Cardinals.h>    
  18. #include "EzMenu.h"
  19. #include "app-resources.h"
  20. #include "resources.h"
  21. #include "month.h"
  22. #include "day.h"
  23. #include "schedule.h"
  24. #include "db.h"
  25. #include "util.h"
  26. #include "date-strings.h"
  27.  
  28. /*    -    -    -    -    -    -    -    -    */
  29. /*
  30.  * Functions defined in this file:
  31.  */
  32. int main();
  33. int countAppoints();
  34.  
  35. static void initGraphics(), initGCs(), initWidgets();
  36. static void createMonthFormDatas(), setMonths();
  37. static void quit(), quit_nosave(), next(), prev(), today(), setNumMonths();
  38. static void save(), editSchedule();
  39. static void syntax(),listAppoints();
  40. static Pixmap readBitmap();
  41.  
  42. /*
  43.  * Global graphics data
  44.  */
  45. Display *display;
  46. Screen *screen;
  47. Window root;
  48. GC dateGC1,dateGC3,dateGC12,emptyGC,shadeGC[23];
  49.  
  50. /*
  51.  * Global widget data
  52.  */
  53. XtAppContext app_con;
  54. Widget toplevel;
  55. Widget bothForm,allForm;
  56. AppResources appResources;
  57. MonthFormData *monthFormData1,*monthFormData3[3],*monthFormData12[12];
  58.  
  59. /*
  60.  * Other global data
  61.  */
  62. char *program;
  63. int currentDay,currentMon,currentYear;        /* today */
  64. int mainDay,mainMon,mainYear;            /* display */
  65. int appointsChanged = False;
  66. int numMonths;
  67.  
  68. /*
  69.  * Action binding table
  70.  */
  71. static XtActionsRec cmdActionsTable[] = {
  72.     { "xkal-quit", quit },
  73.     { "xkal-exit", quit_nosave },
  74.     { "xkal-save", save },
  75.     { "xkal-next", next },
  76.     { "xkal-prev", prev },
  77.     { "xkal-today", today },
  78.     { "xkal-numMonths", setNumMonths },
  79.     { "xkal-edit-schedule", editSchedule },
  80. };
  81.  
  82. /*
  83.  *    Widget and non-widget resources if the application defaults
  84.  *    file can't be found.
  85.  *    [ Generated automatically from Xkal.ad. ]
  86.  */
  87. static String fallbackResources[] = {
  88. #include "Xkal.ad.h"
  89.     NULL
  90. };
  91.  
  92. /*    -    -    -    -    -    -    -    -    */
  93.  
  94. int
  95. main(argc, argv)
  96. int argc;
  97. char **argv;
  98. {
  99.     program = *argv;
  100.     initGraphics(&argc,argv);
  101.     initDb();
  102.     getCurrentDate(¤tDay,¤tMon,¤tYear);
  103.     readAppoints(appResources.appoints);
  104.     if (appResources.date != NULL && *(appResources.date) != '\0')
  105.     parseDate(appResources.date,¤tDay,¤tMon,¤tYear);
  106.     if (appResources.silent) {
  107.     XtDestroyApplicationContext(app_con);
  108.     exit(countAppoints(currentDay,currentMon,currentYear));
  109.     }
  110.     if (appResources.listOnly) {
  111.     listAppoints(currentDay,currentMon,currentYear);
  112.     XtDestroyApplicationContext(app_con);
  113.     exit(countAppoints(currentDay,currentMon,currentYear));
  114.     }
  115.     mainDay = currentDay;
  116.     mainMon = currentMon;
  117.     mainYear = currentYear;
  118.     numMonths = appResources.numMonths;
  119.     initGCs();
  120.     initWidgets();
  121.     XtRealizeWidget(toplevel);
  122.     if (currentDayFormData != NULL) {
  123.     switch (numMonths) {
  124.         case 1: selectDay(monthFormData1,mainDay,mainMon,mainYear);
  125.             break;
  126.         case 3: selectDay(monthFormData3[1],mainDay,mainMon,mainYear);
  127.             break;
  128.         case 12: selectDay(monthFormData12[mainMon],mainDay,mainMon,
  129.                                 mainYear);
  130.             break;
  131.     }
  132.     }
  133.     XtAppMainLoop(app_con);
  134. }
  135.  
  136. static void
  137. initGraphics(argcp,argv)
  138. int *argcp;
  139. char **argv;
  140. {
  141.     char *rev,*strchr();
  142.  
  143.     toplevel = XtAppInitialize(&app_con, "Xkal",
  144.                    options, XtNumber(options),
  145.                    argcp,argv,fallbackResources,NULL,ZERO);
  146.     if (*argcp > 1) {
  147.     syntax(*argcp,argv);
  148.     XtDestroyApplicationContext(app_con);
  149.     exit(-1);
  150.     }
  151.     XtGetApplicationResources(toplevel,(XtPointer)&appResources,
  152.                               resources,XtNumber(resources),NULL,ZERO);
  153.     rev = strchr(fallbackResources[0],'$');
  154.     if (strcmp(appResources.revision,rev) != 0) {
  155.     fprintf(stderr,"%s: app-defaults release not %s\n",program,rev);
  156.     fprintf(stderr,"%s: you may have an outdated app-defaults file\n",
  157.                                 program);
  158.     }
  159.     XtAppAddActions(app_con,cmdActionsTable,XtNumber(cmdActionsTable));
  160.     display = XtDisplay(toplevel);
  161.     screen = XtScreen(toplevel);
  162.     root = RootWindowOfScreen(screen);
  163. }
  164.  
  165. static void
  166. initGCs()
  167. {
  168.     XGCValues values;
  169.     XtGCMask mask;
  170.     int i;
  171.  
  172.     values.foreground = (unsigned long)1;
  173.     values.background = (unsigned long)0;
  174.     values.function = GXset;
  175.     mask = GCForeground | GCBackground | GCFunction | GCFont;
  176.     values.font = appResources.dateFont1;
  177.     dateGC1 = XCreateGC(display,root,mask,&values);
  178.     values.font = appResources.dateFont3;
  179.     dateGC3 = XCreateGC(display,root,mask,&values);
  180.     values.font = appResources.dateFont12;
  181.     dateGC12 = XCreateGC(display,root,mask,&values);
  182.     values.function = GXcopy;
  183.     values.fill_style = FillTiled;
  184.     values.tile = readBitmap(appResources.noDayShade);
  185.     mask = GCForeground | GCBackground | GCFunction | GCFillStyle | GCTile;
  186.     emptyGC = XCreateGC(display,root,mask,&values);
  187.     values.function = GXcopy;
  188.     mask = GCFunction;
  189.     shadeGC[0] = XCreateGC(display,root,mask,&values);
  190.     for (i=1; i < 23; i++) {
  191.     if (DefaultDepthOfScreen(screen) > 1) {    /* COLOR */
  192.         values.foreground = appResources.color[i];
  193.         values.fill_style = FillSolid;
  194.         values.function = GXcopy;
  195.         mask = GCForeground | GCBackground | GCFunction;
  196.     } else {                /* B&W */
  197.         values.foreground = (unsigned long)1;
  198.         values.background = (unsigned long)0;
  199.         values.tile = readBitmap(appResources.shade[i]);
  200.         values.fill_style = FillTiled;
  201.         values.function = GXcopy;
  202.         mask = GCForeground | GCBackground | GCTile | GCFillStyle |
  203.                                 GCFunction;
  204.     }
  205.     shadeGC[i] = XCreateGC(display,root,mask,&values);
  206.     }
  207. }
  208.  
  209. static void
  210. initWidgets()
  211. {
  212.     char *s,name[256];
  213.     int i;
  214.  
  215.     bothForm = XtCreateManagedWidget("bothForm",formWidgetClass,toplevel,
  216.                                 NULL,ZERO);
  217.     XtCreateManagedWidget("fileButton",menuButtonWidgetClass,bothForm,
  218.                                 NULL,ZERO);
  219.     XtCreatePopupShell("fileMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
  220.     XtCreateManagedWidget("viewButton",menuButtonWidgetClass,bothForm,
  221.                                 NULL,ZERO);
  222.     XtCreatePopupShell("viewMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
  223.     XtCreateManagedWidget("otherButton",menuButtonWidgetClass,bothForm,
  224.                                 NULL,ZERO);
  225.     XtCreatePopupShell("otherMenu",ezMenuWidgetClass,bothForm,NULL,ZERO);
  226.     allForm = XtCreateManagedWidget("allMonthsForm",formWidgetClass,bothForm,
  227.                                 NULL,ZERO);
  228.     createMonthFormDatas(numMonths);
  229.     if (appResources.bothShown)
  230.     currentDayFormData = createDayFormData(bothForm);
  231.     setMonths();
  232. }
  233.  
  234. static void
  235. createMonthFormDatas(n)
  236. int n;
  237. {
  238.     char *name = "monthFormXX_XX";
  239.     int i;
  240.  
  241.     switch (n) {
  242.     case 1: monthFormData1 = createMonthFormData(allForm,"monthForm1",1);
  243.         XtManageChild(monthFormData1->form);
  244.         break;
  245.     case 3: for (i=0; i < 3; i++) {
  246.             sprintf(name,"monthForm3_%d",i+1);
  247.             monthFormData3[i] = createMonthFormData(allForm,name,3);
  248.             XtManageChild(monthFormData3[i]->form);
  249.         }
  250.         break;
  251.     case 12: for (i=0; i < 12; i++) {
  252.             sprintf(name,"monthForm12_%d",i+1);
  253.             monthFormData12[i] = createMonthFormData(allForm,name,12);
  254.             XtManageChild(monthFormData12[i]->form);
  255.         }
  256.         break;
  257.     }
  258. }
  259.  
  260. static void
  261. setMonths()
  262. {
  263.     Arg args[2];
  264.     int i,prevMon,prevYear,nextMon,nextYear;
  265.  
  266.     XawFormDoLayout(allForm,False);
  267.     switch (numMonths) {
  268.     case 1:  setMonthFormData(monthFormData1,1,mainMon,mainYear);
  269.          break;
  270.     case 3:  prevMon = mainMon - 1;
  271.          nextMon = mainMon + 1;
  272.          prevYear = nextYear = mainYear;
  273.          if (mainMon == 0) {
  274.              prevMon = 11;
  275.              prevYear = mainYear - 1;
  276.          } else if (mainMon == 11) {
  277.              nextMon = 0;
  278.              nextYear = mainYear + 1;
  279.          }
  280.          setMonthFormData(monthFormData3[0],3,prevMon,prevYear);
  281.          setMonthFormData(monthFormData3[1],3,mainMon,mainYear);
  282.          setMonthFormData(monthFormData3[2],3,nextMon,nextYear);
  283.          break;
  284.     case 12: for (i=0; i < 12; i++)
  285.              setMonthFormData(monthFormData12[i],12,i,mainYear);
  286.          break;
  287.     }
  288.     XawFormDoLayout(allForm,True);
  289. }
  290.  
  291. /*    -    -    -    -    -    -    -    -    */
  292.  
  293. #define ACTION_PROC(NAME)    static void NAME(w,event,params,num_params) \
  294.                     Widget w; \
  295.                     XEvent *event; \
  296.                     String *params; \
  297.                     Cardinal *num_params;
  298.  
  299. ACTION_PROC(quit)
  300. {
  301.     if (currentDayFormData != NULL)
  302.     checkpointAppoints(currentDayFormData);
  303.     if (appointsChanged)
  304.     writeAppoints(appResources.appoints);
  305.     XtDestroyApplicationContext(app_con);
  306.     exit(0);
  307. }
  308.  
  309. ACTION_PROC(quit_nosave)
  310. {
  311.     XtDestroyApplicationContext(app_con);
  312.     exit(0);
  313. }
  314.  
  315. ACTION_PROC(next)
  316. {
  317.     if (numMonths == 1 || numMonths == 3) {
  318.     if (mainMon == 11) {
  319.         mainMon = 0;
  320.         mainYear += 1;
  321.     } else {
  322.         mainMon += 1;
  323.     }
  324.     } else {
  325.     mainYear += 1;
  326.     }
  327.     setMonths();
  328. }
  329.  
  330. ACTION_PROC(prev)
  331. {
  332.     if (numMonths == 1 || numMonths == 3) {
  333.     if (mainMon == 0) {
  334.         mainMon = 11;
  335.         mainYear -= 1;
  336.     } else {
  337.         mainMon -= 1;
  338.     }
  339.     } else {
  340.     mainYear -= 1;
  341.     }
  342.     setMonths();
  343. }
  344.  
  345. ACTION_PROC(today)
  346. {
  347.     mainDay = currentDay;
  348.     mainMon = currentMon;
  349.     mainYear = currentYear;
  350.     setMonths();
  351.     switch (numMonths) {
  352.     case 1: selectDay(monthFormData1,mainDay,mainMon,mainYear);
  353.         break;
  354.     case 3: selectDay(monthFormData3[1],mainDay,mainMon,mainYear);
  355.         break;
  356.     case 12: selectDay(monthFormData12[mainMon],mainDay,mainMon,mainYear);
  357.          break;
  358.     }
  359. }
  360.  
  361. ACTION_PROC(setNumMonths)
  362. {
  363.     int n,i;
  364.  
  365.     if (*num_params != ONE) {
  366.     fprintf(stderr,"%s: bad parms to xkal-numMonths()\n",program);
  367.     return;
  368.     }
  369.     if ((n=atoi(params[0])) == numMonths)
  370.     return;
  371.     XawFormDoLayout(bothForm,False);
  372.     switch (numMonths) {
  373.     case 1: XtUnmanageChild(monthFormData1->form);
  374.         break;
  375.     case 3: for (i=0; i < 3; i++)
  376.             XtUnmanageChild(monthFormData3[i]->form);
  377.         break;
  378.     case 12: for (i=0; i < 12; i++)
  379.             XtUnmanageChild(monthFormData12[i]->form);
  380.         break;
  381.     }
  382.     numMonths = n;
  383.     switch (numMonths) {
  384.     case 1: if (monthFormData1 == NULL)
  385.             createMonthFormDatas(1);
  386.         setMonths();
  387.         XtManageChild(monthFormData1->form);
  388.         break;
  389.     case 3: if (monthFormData3[0] == NULL)
  390.             createMonthFormDatas(3);
  391.         setMonths();
  392.         for (i=0; i < 3; i++)
  393.             XtManageChild(monthFormData3[i]->form);
  394.         break;
  395.     case 12: if (monthFormData12[0] == NULL)
  396.              createMonthFormDatas(12);
  397.          setMonths();
  398.          for (i=0; i < 12; i++)
  399.              XtManageChild(monthFormData12[i]->form);
  400.          break;
  401.     }
  402.     XawFormDoLayout(bothForm,True);
  403. }
  404.  
  405. ACTION_PROC(save)
  406. {
  407.     if (currentDayFormData != NULL)
  408.     checkpointAppoints(currentDayFormData);
  409.     if (appointsChanged)
  410.     writeAppoints(appResources.appoints);
  411.     appointsChanged = False;
  412. }
  413.  
  414. ACTION_PROC(editSchedule)
  415. {
  416.     createPopupSchedule();
  417. }
  418.  
  419. /*    -    -    -    -    -    -    -    -    */
  420.  
  421. static void
  422. listAppoints(day,mon,year)
  423. int day,mon,year;
  424. {
  425.     int i,dow;
  426.     char *s;
  427.  
  428.     dow = computeDOW(day,mon,year);
  429.     printf("%s %d %s %d\n",shortDayStr[dow],day,shortMonStr[mon],year+1900);
  430.     for (i=0; i < 20; i++) {
  431.     if ((s=lookup(i,day,mon,year)) != NULL)
  432.         if (i % 2 == 0)
  433.         printf("\t%2d:00 %s\n",8+(i / 2),s);
  434.         else
  435.         printf("\t%2d:30 %s\n",8+((i-1)/2),s);
  436.     }
  437.     for (i=20; i < 22; i++) {
  438.     if ((s=lookup(i,day,mon,year)) != NULL)
  439.         printf("\tNOTES %s\n",s);
  440.     }
  441. }
  442.  
  443. int
  444. countAppoints(day,mon,year)
  445. int day,mon,year;
  446. {
  447.     int i,n;
  448.  
  449.     n = 0;
  450.     for (i=0; i < 22; i++) {
  451.     if (lookupAppoint(i,day,mon,year) != NULL)
  452.         n += 1;
  453.     }
  454.     return(n);
  455. }
  456.  
  457. /*    -    -    -    -    -    -    -    -    */
  458.  
  459. static Pixmap
  460. readBitmap(name)
  461. char *name;
  462. {
  463.     Pixmap b;
  464.     int w,h,xhot,yhot;
  465.  
  466.     b = XmuLocateBitmapFile(screen,name,NULL,ZERO,&w,&h,&xhot,&yhot);
  467.     if (b == NULL) {
  468.     fprintf(stderr,"%s: couldn't find bitmap \"%s\"\n",program,name);
  469.     return(XCreatePixmap(display,screen,2,2,DefaultDepthOfScreen(screen)));
  470.     } else
  471.     return(XmuCreatePixmapFromBitmap(display,root,b,w,h,
  472.                     DefaultDepthOfScreen(screen),1,0));
  473. }
  474.  
  475. /*    -    -    -    -    -    -    -    -    */
  476. /*
  477.  *    syntax() : print the usage message
  478.  */
  479. static void
  480. syntax(argc,argv)
  481. int argc;
  482. char **argv;
  483. {
  484.     argv += 1;
  485.     if (argc > 2 || (strcmp(*argv,"-help") != 0 && strcmp(*argv,"-?") != 0)) {
  486.     fprintf(stderr,"%s: bad argument(s): ",program);
  487.     while (--argc)
  488.         fprintf(stderr,"%s ",*argv++);
  489.     fprintf(stderr,"\n");
  490.     }
  491.     fprintf(stderr,"usage: %s",program);
  492.     fprintf(stderr,"\t-appoints file\tuse file for appointments\n");
  493.     fprintf(stderr,"\t\t-date date\tuse given date for `today'\n");
  494.     fprintf(stderr,"\t\t-numMonths 1|3|12\tstart displaying 1, 3 or 12 months\n");
  495.     fprintf(stderr,"\t\t-bothShown\tput day display next to month(s)\n");
  496.     fprintf(stderr,"\t\t-nobothShown\tpopup days as needed\n");
  497.     fprintf(stderr,"\t\t-listOnly\tprint appoints to stdout\n");
  498.     fprintf(stderr,"\t\t\t-silent\texit with status == number of appoints\n");
  499.     fprintf(stderr,"\t\t-opaqueDates\tprint day numbers opaquely\n");
  500.     fprintf(stderr,"\t\t-noopaqueDates\tprint day numbers transparently\n");
  501.     fprintf(stderr,"\t\t-dowLabels\tprint day of week at top of columns\n");
  502.     fprintf(stderr,"\t\t-nodowLabels\tdon't print dow at top of columns\n");
  503. }
  504.