home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / calentool / part15 / mpaint.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-06  |  9.4 KB  |  338 lines

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