home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / calentool / part14 / ypaint.c
Encoding:
C/C++ Source or Header  |  1991-04-06  |  9.5 KB  |  357 lines

  1. /*
  2.  * $Header: ypaint.c,v 2.5 91/03/27 16:46:51 billr Exp $
  3.  */
  4. /*
  5.  * ypaint.c
  6.  *
  7.  * Author: Bill Randle, Tektronix, Inc. <billr@saab.CNA.TEK.COM>
  8.  *
  9.  * Copyright (C) 1988, 1989, 1991 Tektronix, Inc.  All Rights Reserved
  10.  *
  11.  * Permission is hereby granted to use and modify this code in source
  12.  * or binary form as long as it is not sold for profit and this copyright
  13.  * notice remains intact.
  14.  */
  15. /***************************************************
  16.  *                           *
  17.  *    Artistic routines that draw in the main    *
  18.  * subwindow for the year display.           *
  19.  *                           *
  20.  ***************************************************/
  21.  
  22. #include <suntool/sunview.h>
  23. #include <suntool/canvas.h>
  24. #include <ctype.h>
  25. #include <stdio.h>
  26. #include "ct.h"
  27. #include "paint.h"
  28. #ifndef NO_HOLIDAYS
  29. extern struct appt_entry a_appts[], c_appts[];
  30. extern struct appt_entry i_appts[], j_appts[];
  31. extern struct appt_entry s_appts[];
  32. #endif
  33.  
  34. /*
  35.  * draw calendar for a whole year.
  36.  */
  37. draw_year()
  38. {
  39.     int monthnr, daynr, boxnr, i, j, k, thismonth;
  40.     int row, col, x, y;
  41.     int busy_today[367], startbox, nrdays, yrday, extra_days;
  42.     char title[5], c[3], buf[100];
  43.     Rect *rect;
  44.     struct tm Save;
  45.     struct appt_entry appt;
  46.     int read_stat;
  47.     FILE *apts;
  48.     int runl;
  49.  
  50.     lock_cursors();
  51.     /* destory future appts popup, if it exists */
  52.     if (fframe) {
  53.         window_destroy(fframe);
  54.         fframe = 0;
  55.     }
  56.     fix_current_day();
  57.  
  58.     working(TRUE);
  59.     pw_batch_on(main_pixwin);
  60.     rect = (Rect *) window_get(canvas, WIN_RECT);
  61.     /* Erase the window */
  62.     pw_writebackground(main_pixwin,0,0,rect->r_width,rect->r_height,PIX_CLR);
  63.     startx = (rect->r_width - 3*8*ybox_width)/2 + font->pf_defaultsize.x+1;
  64.     starty = rect->r_height - 4*8*ybox_height + ybox_height/2;
  65.     sun_moon_buttons(FALSE);
  66.     print_button(TRUE);
  67.  
  68.     /* Which days have appointments? */
  69.     for (i=1; i<=dysize(current.tm_year + 1900); i++)
  70.         busy_today[i] = 0;
  71.     Save = current;
  72.     current.tm_mon = JAN;
  73.     current.tm_mday = 1;
  74.     fix_current_day();
  75.     First = current;
  76.     current.tm_mon = DEC;
  77.     current.tm_mday = monthlength(DEC);
  78.     fix_current_day();
  79.     Last = current;
  80.     if ((apts = fopen(apts_pathname, "r")) == NULL)
  81.         err_rpt("can't open appointments file", FATAL);
  82.  
  83.     working(FALSE);
  84.     while ((read_stat = get_aentry(apts, &appt, FALSE, 1, 0)) != EOF) {
  85.         if (read_stat)
  86.             continue;    /* read error */
  87.         if (appt.flags & A_COMMENT)
  88.             continue;
  89.         if ((appt.flags & MARKED_NOTE) == MARKED_NOTE)
  90.             continue;
  91.         current.tm_year = appt.year;
  92.         current.tm_mon = appt.month;
  93.         current.tm_mday = appt.day;
  94.         if (appt.flags & ALL_YEARS)
  95.             current.tm_year = First.tm_year;
  96.         if (current.tm_year == First.tm_year) {
  97.             if (appt.flags & ALL_MONTHS) {
  98.                 for (current.tm_mon = JAN; 
  99.                     current.tm_mon <= DEC; 
  100.                     current.tm_mon++) {
  101.                     if ((appt.flags & ALL_DAYS)
  102.                         || (appt.flags & EVERY_MON_FRI)) {
  103.                         for (current.tm_mday = 1; current.tm_mday <=
  104.                             monthlength(current.tm_mon); current.tm_mday++) {
  105.                             fix_current_day();
  106.                             if (current.tm_wday >= MON && current.tm_wday <=FRI)
  107.                                 busy_today[current.tm_yday]++;
  108.                             else if (appt.flags & ALL_DAYS)
  109.                                 /* pick up Sat and Sun */
  110.                                 busy_today[current.tm_yday]++;
  111.                         }
  112.                     } else if (appt.flags & EVERY_SOMEDAY) {
  113.                         i = Pickday(appt.flags);
  114.                         for (current.tm_mday = 1; current.tm_mday <=
  115.                             monthlength(current.tm_mon); current.tm_mday++) {
  116.                             fix_current_day();
  117.                             if (current.tm_wday == i)
  118.                                 if (chk_week(appt.repeat, current.tm_mday))
  119.                                     busy_today[current.tm_yday]++;
  120.                         }
  121.                     } else {
  122.                         fix_current_day();
  123.                         busy_today[current.tm_yday]++;
  124.                     }
  125.                 }
  126.             } else if ((appt.flags & ALL_DAYS)
  127.                 || (appt.flags & EVERY_MON_FRI)) {
  128.                 for (current.tm_mday = 1; current.tm_mday <=
  129.                     monthlength(current.tm_mon); current.tm_mday++) {
  130.                     fix_current_day();
  131.                     if (current.tm_wday >= MON && current.tm_wday <=FRI)
  132.                         busy_today[current.tm_yday]++;
  133.                     else if (appt.flags & ALL_DAYS)
  134.                         /* pick up Sat and Sun */
  135.                         busy_today[current.tm_yday]++;
  136.                 }
  137.             } else if (appt.flags & REPEAT) {
  138.                 if (appt.flags & EVERY_SOMEDAY) {
  139.                     current.tm_mday = 1;
  140.                     find_date(&appt);
  141.                     if (appt.flags & RUN)
  142.                         runl = appt.runlength;
  143.                     else
  144.                         runl = 1;
  145.                     while (ymd_compare(current, Last) <= 0 && runl) {
  146.                         if (chk_week(appt.repeat, current.tm_mday)) {
  147.                             if (runl)
  148.                                 busy_today[current.tm_yday]++;
  149.                             if (appt.flags & RUN)
  150.                                 --runl;
  151.                         }
  152.                         current.tm_mday += 7;
  153.                         fix_current_day();
  154.                         if (current.tm_mon != appt.month && !(appt.flags & RUN))
  155.                             break;
  156.                     }
  157.                 } else {
  158.                     fix_current_day();
  159.                     if (appt.flags & RUN)
  160.                         runl = appt.runlength;
  161.                     else
  162.                         runl = 1;
  163.                     while (ymd_compare(current, Last) <= 0 && runl) {
  164.                         if (runl) {
  165.                             busy_today[current.tm_yday]++;
  166.                             current.tm_mday += appt.repeat;
  167.                             fix_current_day();
  168.                             if (appt.flags & RUN)
  169.                                 --runl;
  170.                         }
  171.                     }
  172.                 }
  173.             } else {
  174.                 fix_current_day();
  175.                 if (appt.flags & DELETED)
  176.                     busy_today[current.tm_yday]--;
  177.                 else
  178.                     busy_today[current.tm_yday]++;
  179.             }
  180.         } else if ((appt.flags & REPEAT) || (appt.flags & EVERY_SOMEDAY)) {
  181.             /* find 1st appt in this year */
  182.             if (appt.flags & RUN)
  183.                 runl = appt.runlength;
  184.             else
  185.                 runl = 1;
  186.             if (appt.flags & EVERY_SOMEDAY)
  187.                 find_date(&appt);
  188.             while (ymd_compare(current, First) < 0 && runl) {
  189.                 if (appt.flags & RUN)
  190.                     --runl;
  191.                 if (runl) {
  192.                         if (appt.flags & EVERY_SOMEDAY) {
  193.                             current.tm_mday += 7;
  194.                             find_date(&appt);
  195.                         } else {
  196.                             current.tm_mday += appt.repeat;
  197.                             fix_current_day();
  198.                         }
  199.                 }
  200.             }
  201.             while (ymd_compare(current, Last) <= 0 && runl) {
  202.                 if (runl) {
  203.                     busy_today[current.tm_yday]++;
  204.                     if (appt.flags & RUN)
  205.                         --runl;
  206.                     if (appt.flags & EVERY_SOMEDAY) {
  207.                         current.tm_mday += 7;
  208.                         find_date(&appt);
  209.                     } else {
  210.                         current.tm_mday += appt.repeat;
  211.                         fix_current_day();
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.     }
  217.     fclose(apts);
  218.     current = First;
  219.     fix_current_day();
  220. #ifndef NO_HOLIDAYS
  221.     /*
  222.      * now that we've gone thru the appointments file,
  223.      * check to see if the user has selected any holiday
  224.      * options and add them in.
  225.      */
  226.     for (i=0; i<dysize(current.tm_year + 1900); i++) {
  227.         working(TRUE);
  228.         if (holiday_a == 1) {
  229.             j = a_dates(holiday_a);
  230.             for (k=0; k<j; k++)
  231.                 if (ymd2_compare(¤t, &a_appts[k]) == 0)
  232.                     busy_today[i]++;
  233.         }
  234.         if (holiday_c == 1) {
  235.             j = c_dates(holiday_c);
  236.             for (k=0; k<j; k++)
  237.                 if (ymd2_compare(¤t, &c_appts[k]) == 0)
  238.                     busy_today[i]++;
  239.         }
  240.         working(FALSE);
  241.         if (holiday_i == 1) {
  242.             j = i_dates(holiday_i);
  243.             for (k=0; k<j; k++)
  244.                 if (ymd2_compare(¤t, &i_appts[k]) == 0)
  245.                     busy_today[i]++;
  246.         }
  247.         working(TRUE);
  248.         if (holiday_j == 1) {
  249.             j = j_dates(holiday_j);
  250.             for (k=0; k<j; k++)
  251.                 if (ymd2_compare(¤t, &j_appts[k]) == 0)
  252.                     busy_today[i]++;
  253.         }
  254.         if (holiday_s == 1) {
  255.             j = s_dates(holiday_s);
  256.             for (k=0; k<j; k++)
  257.                 if (ymd2_compare(¤t, &s_appts[k]) == 0)
  258.                     busy_today[i]++;
  259.         }
  260.         current.tm_mday++;
  261.         fix_current_day();
  262.         working(FALSE);
  263.     }
  264.     current = First;
  265.     fix_current_day();
  266. #endif    /* NO_HOLIDAYS */
  267.  
  268.     /* display year title */
  269.     sprintf(title, "%d", 1900 + First.tm_year);
  270.     pw_text(main_pixwin, (rect->r_width - bigfont->pf_defaultsize.x*strlen(title))/2, starty - bigfont->pf_defaultsize.y,
  271.       PIX_SRC, bigfont, title);
  272.  
  273.     /* display day names */
  274.     if (monday_first) {
  275.         strcpy(buf, "Mo Tu We Th Fr Sa Su    ");
  276.         strcat(buf, "Mo Tu We Th Fr Sa Su    ");
  277.         strcat(buf, "Mo Tu We Th Fr Sa Su");
  278.     } else {
  279.         strcpy(buf, "Su Mo Tu We Th Fr Sa    ");
  280.         strcat(buf, "Su Mo Tu We Th Fr Sa    ");
  281.         strcat(buf, "Su Mo Tu We Th Fr Sa");
  282.     }
  283.     pw_text(main_pixwin, startx+4, starty-2, PIX_SRC, font, buf);
  284.  
  285.     /* draw months */
  286.     monthnr = 0;
  287.     yrday = 0;
  288.     extra_days = 0;
  289.     if (monday_first) {
  290.         if (First.tm_wday == SUN)
  291.             startbox = 6;
  292.         else
  293.             startbox = First.tm_wday - 1;
  294.     } else
  295.         startbox = First.tm_wday;
  296.     for (row=0; row<4; row++) {
  297.         for (col=0; col<3; col++) {
  298.             x = startx + 8*ybox_width*col;
  299.             y = starty + 8*ybox_height*row;
  300.             mboxlims[monthnr].lowx = x;
  301.             mboxlims[monthnr].lowy = y;
  302.             mboxlims[monthnr].highx = x + 7*ybox_width;
  303.             mboxlims[monthnr].highy = y + 7*ybox_height;
  304.             /* draw month box and label */
  305.             pw_write(main_pixwin, x, y, 8*ybox_width, 
  306.                 ybox_height, PIX_SRC, ymonthbox_pr, 0, 0);
  307.             pw_text(main_pixwin, x+8, y+font->pf_defaultsize.y, PIX_SRC, font, 
  308.                 monthnames[row*3 + col]);
  309.             if (!extra_days) {
  310.                 nrdays = monthlength(monthnr);
  311.                 daynr = 1;
  312.             }
  313.             boxnr = 0;
  314.             for (j=0; j<6; j++) {
  315.                 y += ybox_height;
  316.                 /* foreach week in the month */
  317.                 for (k=0; k<7; k++) {
  318.                     /* foreach day in the week */
  319.                     /* draw day boxes */
  320.                     pw_write(main_pixwin, x, y, ybox_width, ybox_height, PIX_SRC, ydaybox_pr, 0, 0);
  321.                     /* label day boxes */
  322.                     if (boxnr >= startbox && daynr <= nrdays) {
  323.                         if (ymd_compare(today, current) == 0)
  324.                             /* gray box */
  325.                             pw_write(main_pixwin, x, y, ybox_width, ybox_height, PIX_SRC, ydaybox_td_pr, 0, 0);
  326.                         c[0] = (daynr<10 ? ' ' : daynr/10 + '0');
  327.                         c[1] = daynr%10 + '0';
  328.                         c[2] = '\0';
  329.                         pw_text(main_pixwin, x+4, y+14,
  330.                           PIX_SRC|PIX_DST, font, c);
  331.                         /* if appointment exists, reverse video box */
  332.                         if (busy_today[yrday] > 0)
  333.                             pw_write(main_pixwin, x+2, y+2,
  334.                               ybox_width-4, ybox_height-4,
  335.                               PIX_NOT(PIX_DST), NULL, 0, 0);
  336.                         if (daynr == nrdays)
  337.                             startbox = (k + 1) % 7;
  338.                         ++daynr;
  339.                         ++yrday;
  340.                         current.tm_mday++;
  341.                         fix_current_day();
  342.                     }
  343.                     x += ybox_width;
  344.                     ++boxnr;
  345.                 }
  346.                 x -= 7*ybox_width;
  347.             }
  348.             ++monthnr;
  349.         }
  350.     }
  351.     pw_batch_off(main_pixwin);
  352.     current = Save;
  353.     fix_current_day();
  354.     unlock_cursors();
  355. }
  356.  
  357.