home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / calentool / part08 / ypaint.c < prev   
Encoding:
C/C++ Source or Header  |  1989-05-27  |  7.6 KB  |  279 lines

  1. /*
  2.  * $Header: ypaint.c,v 2.1 89/05/09 14:20:15 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 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.  
  29. /*
  30.  * draw calendar for a whole year.
  31.  */
  32. draw_year()
  33. {
  34.     int monthnr, daynr, boxnr, i, j, k, thismonth;
  35.     int row, col, x, y;
  36.     int busy_today[366], startbox, nrdays, yrday, extra_days;
  37.     char title[5], c[3], buf[100];
  38.     Rect *rect;
  39.     struct tm Save;
  40.     struct appt_entry appt;
  41.     int read_stat;
  42.     FILE *apts;
  43.  
  44.     lock_cursors();
  45.     /* destory future appts popup, if it exists */
  46.     if (fframe) {
  47.         window_destroy(fframe);
  48.         fframe = 0;
  49.     }
  50.     fix_current_day();
  51.  
  52.     working(TRUE);
  53.     pw_batch_on(main_pixwin);
  54.     rect = (Rect *) window_get(canvas, WIN_RECT);
  55.     /* Erase the window */
  56.     pw_writebackground(main_pixwin,0,0,rect->r_width,rect->r_height,PIX_CLR);
  57.     startx = (rect->r_width - 3*8*ybox_width)/2 + font->pf_defaultsize.x+1;
  58.     starty = (rect->r_height - 4*7*ybox_height) / 2;
  59.     sun_moon_buttons(FALSE);
  60.     print_button(TRUE);
  61.  
  62.     /* Which days have appointments? */
  63.     for (i=0; i<dysize(current.tm_year + 1900); i++)
  64.         busy_today[i] = 0;
  65.     Save = current;
  66.     current.tm_mon = JAN;
  67.     current.tm_mday = 1;
  68.     fix_current_day();
  69.     First = current;
  70.     current.tm_mon = DEC;
  71.     current.tm_mday = monthlength(DEC);
  72.     fix_current_day();
  73.     Last = current;
  74.     if ((apts = fopen(apts_pathname, "r")) == NULL)
  75.         err_rpt("can't open appointments file", FATAL);
  76.  
  77.     working(FALSE);
  78.     while ((read_stat = get_aentry(apts, &appt)) != EOF) {
  79.         if (read_stat)
  80.             continue;    /* read error */
  81.         if (appt.flags & A_COMMENT)
  82.             continue;
  83.         if ((appt.flags & MARKED_NOTE) == MARKED_NOTE)
  84.             continue;
  85.         current.tm_year = appt.year;
  86.         current.tm_mon = appt.month;
  87.         current.tm_mday = appt.day;
  88.         if (appt.flags & ALL_YEARS)
  89.             current.tm_year = First.tm_year;
  90.         if (current.tm_year == First.tm_year) {
  91.             if (appt.flags & ALL_MONTHS) {
  92.                 for (current.tm_mon = JAN; 
  93.                     current.tm_mon <= DEC; 
  94.                     current.tm_mon++) {
  95.                     if (appt.flags & ALL_DAYS) {
  96.                         for (current.tm_mday = 1; current.tm_mday <= monthlength(current.tm_mon); current.tm_mday++) {
  97.                             fix_current_day();
  98.                             busy_today[current.tm_yday]++;
  99.                         }
  100.                     } else if (appt.flags & EVERY_SOMEDAY) {
  101.                         i = Pickday(appt.flags);
  102.                         for (current.tm_mday = 1; current.tm_mday <= monthlength(current.tm_mon); current.tm_mday++) {
  103.                             fix_current_day();
  104.                             if (current.tm_wday == i)
  105.                                 if (chk_week(appt.repeat, current.tm_mday))
  106.                                     busy_today[current.tm_yday]++;
  107.                         }
  108.                     } else {
  109.                         fix_current_day();
  110.                         busy_today[current.tm_yday]++;
  111.                     }
  112.                 }
  113.             } else if (appt.flags & REPEAT) {
  114.                 if (appt.flags & EVERY_SOMEDAY) {
  115.                     i = Pickday(appt.flags);
  116.                     current.tm_mday = 1;
  117.                     fix_current_day();
  118.                     while (current.tm_wday != i) {
  119.                         current.tm_mday++;
  120.                         current.tm_wday = (current.tm_wday + 1) % 7;
  121.                     }
  122.                     fix_current_day();
  123.                     while (ymd_compare(current, Last) <= 0) {
  124.                         if (chk_week(appt.repeat, current.tm_mday))
  125.                             busy_today[current.tm_yday]++;
  126.                         current.tm_mday += 7;
  127.                         fix_current_day();
  128.                         if (current.tm_mon != appt.month)
  129.                             break;
  130.                     }
  131.                 } else {
  132.                     fix_current_day();
  133.                     while (ymd_compare(current, Last) <= 0) {
  134.                         busy_today[current.tm_yday]++;
  135.                         current.tm_mday += appt.repeat;
  136.                         fix_current_day();
  137.                     }
  138.                 }
  139.             } else {
  140.                 fix_current_day();
  141.                 if (appt.flags & DELETED)
  142.                     busy_today[current.tm_yday]--;
  143.                 else
  144.                     busy_today[current.tm_yday]++;
  145.             }
  146.         } else if ((appt.flags & REPEAT) && !(appt.flags & EVERY_SOMEDAY)) {
  147.             /* find 1st appt in this year */
  148.             while (ymd_compare(current, First) < 0) {
  149.                 current.tm_mday += appt.repeat;
  150.                 fix_current_day();
  151.             }
  152.             while (ymd_compare(current, Last) <= 0) {
  153.                 busy_today[current.tm_yday]++;
  154.                 current.tm_mday += appt.repeat;
  155.                 fix_current_day();
  156.             }
  157.         }
  158.  
  159.     }
  160.     fclose(apts);
  161.     current = First;
  162.     fix_current_day();
  163. #ifndef NO_HOLIDAYS
  164.     /*
  165.      * now that we've gone thru the appointments file,
  166.      * check to see if the user has selected any holiday
  167.      * options and add them in.
  168.      */
  169.     for (i=0; i<dysize(current.tm_year + 1900); i++) {
  170.         working(TRUE);
  171.         if (holiday_a == 1 && a_dates(&appt, holiday_a))
  172.             busy_today[i]++;
  173.         if (holiday_c == 1 && c_dates(&appt, holiday_c))
  174.             busy_today[i]++;
  175.         working(FALSE);
  176.         if (holiday_i == 1 && i_dates(&appt, holiday_i))
  177.             busy_today[i]++;
  178.         working(TRUE);
  179.         if (holiday_j == 1 && j_dates(&appt, holiday_j))
  180.             busy_today[i]++;
  181.         if (holiday_s == 1 && s_dates(&appt, holiday_s))
  182.             busy_today[i]++;
  183.         current.tm_mday++;
  184.         fix_current_day();
  185.         working(FALSE);
  186.     }
  187.     current = First;
  188.     fix_current_day();
  189. #endif    /* NO_HOLIDAYS */
  190.  
  191.     /* display year title */
  192.     sprintf(title, "%d", 1900 + First.tm_year);
  193.     pw_text(main_pixwin, (rect->r_width - bigfont->pf_defaultsize.x*strlen(title))/2, starty - bigfont->pf_defaultsize.y,
  194.       PIX_SRC, bigfont, title);
  195.  
  196.     /* display day names */
  197.     strcpy(buf, "Su Mo Tu We Th Fr Sa    ");
  198.     strcat(buf, "Su Mo Tu We Th Fr Sa    ");
  199.     strcat(buf, "Su Mo Tu We Th Fr Sa");
  200.     pw_text(main_pixwin, startx+4, starty-2, PIX_SRC, font, buf);
  201.  
  202.     /* draw months */
  203.     monthnr = 0;
  204.     yrday = 0;
  205.     extra_days = 0;
  206.     startbox = First.tm_wday;
  207.     for (row=0; row<4; row++) {
  208.         for (col=0; col<3; col++) {
  209.             x = startx + 8*ybox_width*col;
  210.             y = starty + 7*ybox_height*row;
  211.             mboxlims[monthnr].lowx = x;
  212.             mboxlims[monthnr].lowy = y;
  213.             mboxlims[monthnr].highx = x + 7*ybox_width;
  214.             mboxlims[monthnr].highy = y + 6*ybox_width;
  215.             /* draw month box and label */
  216.             pw_write(main_pixwin, x, y, 7*ybox_width, 
  217.                 ybox_height, PIX_SRC, ymonthbox_pr, 0, 0);
  218.             pw_text(main_pixwin, x+8, y+font->pf_defaultsize.y, PIX_SRC, font, 
  219.                 monthnames[row*3 + col]);
  220.             if (!extra_days) {
  221.                 nrdays = monthlength(monthnr);
  222.                 daynr = 1;
  223.             }
  224.             boxnr = 0;
  225.             for (j=0; j<5; j++) {
  226.                 y += ybox_height;
  227.                 /* foreach week in the month */
  228.                 for (k=0; k<7; k++) {
  229.                     /* foreach day in the week */
  230.                     /* draw day boxes */
  231.                     pw_write(main_pixwin, x, y, ybox_width, ybox_height, PIX_SRC, ydaybox_pr, 0, 0);
  232.                     /* label day boxes */
  233.                     if (boxnr >= startbox && daynr <= nrdays) {
  234.                         if (ymd_compare(today, current) == 0)
  235.                             /* gray box */
  236.                             pw_write(main_pixwin, x, y, ybox_width, ybox_height, PIX_SRC, ydaybox_td_pr, 0, 0);
  237.                         c[0] = (daynr<10 ? ' ' : daynr/10 + '0');
  238.                         c[1] = daynr%10 + '0';
  239.                         c[2] = '\0';
  240.                         pw_text(main_pixwin, x+4, y+14,
  241.                           PIX_SRC|PIX_DST, font, c);
  242.                         /* if appointment exists, reverse video box */
  243.                         if (busy_today[yrday] > 0)
  244.                             pw_write(main_pixwin, x+2, y+2,
  245.                               ybox_width-4, ybox_height-4,
  246.                               PIX_NOT(PIX_DST), NULL, 0, 0);
  247.                         if (daynr == nrdays)
  248.                             startbox = (k + 1) % 7;
  249.                         ++daynr;
  250.                         ++yrday;
  251.                         current.tm_mday++;
  252.                         fix_current_day();
  253.                     }
  254.                     x += ybox_width;
  255.                     ++boxnr;
  256.                     if (extra_days && daynr > nrdays) {
  257.                         /* time to start new month */
  258.                         extra_days = 0;
  259.                         nrdays = monthlength(monthnr);
  260.                         daynr = 1;
  261.                     }
  262.                 }
  263.                 x -= 7*ybox_width;
  264.             }
  265.             if (daynr <= nrdays) {
  266.                 /* leftover goes in next month */
  267.                 extra_days++;
  268.                 startbox = 0;
  269.             }
  270.             ++monthnr;
  271.         }
  272.     }
  273.     pw_batch_off(main_pixwin);
  274.     current = Save;
  275.     fix_current_day();
  276.     unlock_cursors();
  277. }
  278.  
  279.