home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / calentool / part06 / mpaint.c next >
Encoding:
C/C++ Source or Header  |  1989-05-27  |  7.7 KB  |  257 lines

  1. /*
  2.  * $Header: mpaint.c,v 2.1 89/05/09 14:19:29 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 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.  
  38. /*
  39.  * Routine to draw month calendar.
  40.  */
  41.  
  42. draw_month()
  43. {
  44.     int start_dow, i, x, y, n_days, n_weeks;
  45.     int histo[7], days_in_week;
  46.     int arrow_index, last_top, index;
  47.     char c[4], title[20], *cp;
  48.     int left_border, right_border, top_border, bottom_border;
  49.     Rect *rect;
  50.     int busy_today[31];
  51.     FILE *apts;
  52.     int read_stat;
  53.     struct tm Save;
  54.     struct appt_entry appt;
  55.  
  56.     lock_cursors();
  57.     /* destory future appts popup, if it exists */
  58.     if (fframe) {
  59.         window_destroy(fframe);
  60.         fframe = 0;
  61.     }
  62.     fix_current_day();
  63.     Save = current;
  64.     current.tm_mday = 1;
  65.     fix_current_day();
  66.     working(TRUE);
  67.     start_dow = current.tm_wday;
  68.     n_days = monthlength(current.tm_mon);
  69.     for (i=0; i<7; i++)    /* Calculate # of full/partial weeks. */
  70.         histo[i] = 0;
  71.     for (i=0; i<n_days; i++)
  72.         histo[(start_dow + i) % 7] += 1;
  73.     n_weeks = 0;
  74.     for (i=0; i<7; i++)
  75.         if (histo[i] > n_weeks)
  76.             n_weeks = histo[i];
  77.  
  78.     pw_batch_on(main_pixwin);
  79.         rect = (Rect *) window_get(canvas, WIN_RECT);
  80.     /* Erase the window */
  81.         pw_writebackground(main_pixwin,0,0,rect->r_width,rect->r_height,PIX_CLR);
  82.         left_border = (rect->r_width - 7*64)/2 + 32;
  83.         top_border = (rect->r_height - n_weeks*64) / 2;
  84.         right_border = left_border + 7*64;
  85.          
  86.     sprintf(title, "%s, %d",
  87.         monthnames[current.tm_mon], 1900 + current.tm_year);
  88.         pw_text(main_pixwin, (rect->r_width - bigfont->pf_defaultsize.x*strlen(title))/2 + 32, top_border/2 +  7,
  89.           PIX_SRC, bigfont, title);
  90.     sun_moon_buttons(FALSE);
  91.     print_button(TRUE);
  92.  
  93.     for (i=0; i<31; i++)        /* Which days have appointments? */
  94.         busy_today[i] = 0;
  95.     if ((apts = fopen(apts_pathname, "r")) == NULL)
  96.         err_rpt("can't open appointments file");
  97.     First = current;
  98.     current.tm_mday = monthlength(current.tm_mon);
  99.     fix_current_day();
  100.     Last = current;
  101.     working(FALSE);
  102.     while ((read_stat = get_aentry(apts, &appt)) != EOF) {
  103.         if (read_stat)
  104.             continue;    /* read error (ignore) */
  105.         if (appt.flags & A_COMMENT)
  106.             continue;
  107.         if ((appt.flags & MARKED_NOTE) == MARKED_NOTE)
  108.             continue;
  109.         current.tm_year = appt.year;
  110.         current.tm_mon = appt.month;
  111.         current.tm_mday = appt.day;
  112.         if (appt.flags & ALL_YEARS)
  113.             current.tm_year = First.tm_year;
  114.         if (appt.flags & ALL_MONTHS)
  115.             current.tm_mon = First.tm_mon;
  116.         if ((appt.flags & EVERY_SOMEDAY) && (current.tm_mon == First.tm_mon) && (current.tm_year == First.tm_year)) {
  117.             /* find first occurance of this day this month */
  118.             current.tm_mday = First.tm_mday;
  119.             current.tm_wday = First.tm_wday;
  120.             i = Pickday(appt.flags);
  121.             while (current.tm_wday != i) {
  122.                 current.tm_mday++;
  123.                 current.tm_wday = (current.tm_wday + 1) % 7;
  124.             }
  125.             fix_current_day();
  126.             while (ymd_compare(current, Last) <= 0) {
  127.                 if (chk_week(appt.repeat, current.tm_mday)) {
  128.                     busy_today[current.tm_mday-1]++;
  129.                 }
  130.                 current.tm_mday += 7;
  131.                 fix_current_day();
  132.             }
  133.         } else if ((appt.flags & REPEAT) && !(appt.flags & ALL_DAYS)) {
  134.             if (appt.flags & EVERY_SOMEDAY)
  135.                 continue;
  136.             while (ymd_compare(current, First) < 0) {
  137.                 current.tm_mday += appt.repeat;
  138.                 fix_current_day();
  139.             }
  140.             while (ymd_compare(current, Last) <= 0) {
  141.                 busy_today[current.tm_mday-1]++;
  142.                 current.tm_mday += appt.repeat;
  143.                 fix_current_day();
  144.             }
  145.         } else if (current.tm_year == First.tm_year 
  146.             && current.tm_mon == First.tm_mon) {
  147.             if (appt.flags & ALL_DAYS)
  148.                 for (i=0; i<monthlength(First.tm_mon); i++)
  149.                     busy_today[i]++;
  150.             else if (appt.flags & DELETED)
  151.                 busy_today[appt.day-1]--;
  152.             else
  153.                 busy_today[appt.day-1]++;
  154.         }
  155.                 
  156.     }
  157.     fclose(apts);
  158.     current = First;
  159.     fix_current_day();
  160. #ifndef NO_HOLIDAYS
  161.     /*
  162.      * now that we've gone thru the appointments file,
  163.      * check to see if the user has selected any holiday
  164.      * options and add them in.
  165.      */
  166.     for (i=0; i<monthlength(First.tm_mon); i++) {
  167.         working(TRUE);
  168.         if (holiday_a == 1 && a_dates(&appt, holiday_a))
  169.             busy_today[i]++;
  170.         if (holiday_c == 1 && c_dates(&appt, holiday_c))
  171.             busy_today[i]++;
  172.         working(FALSE);
  173.         if (holiday_i == 1 && i_dates(&appt, holiday_i))
  174.             busy_today[i]++;
  175.         working(TRUE);
  176.         if (holiday_j == 1 && j_dates(&appt, holiday_j))
  177.             busy_today[i]++;
  178.         if (holiday_s == 1 && s_dates(&appt, holiday_s))
  179.             busy_today[i]++;
  180.         current.tm_mday++;
  181.         working(FALSE);
  182.     }
  183.     current = First;
  184.     fix_current_day();
  185. #endif
  186.  
  187.     y = top_border;                /* Draw all day boxes. */
  188.     x = 64*start_dow + left_border;
  189.         days_in_week = start_dow;
  190.         c[0] = ' ';
  191.         c[1] = '1';
  192.         c[2] = ' ';
  193.         c[3] = '\0';
  194.         for (i=0; i<n_days; i++){
  195.         if (ymd_compare(today, current) == 0)
  196.             /* gray box */
  197.             pw_write(main_pixwin,x,y,64,64,PIX_SRC,daybox_td_pr,0,0);
  198.         else
  199.             pw_write(main_pixwin,x,y,64,64,PIX_SRC,daybox_pr,0,0);
  200.         if (busy_today[i] > 0)
  201.             pw_write(main_pixwin, x+46, y+2, 16, 16,
  202.               PIX_SRC|PIX_DST, triangle_pr, 0, 0);
  203.                 boxlims[i].lowx = x;
  204.                 boxlims[i].lowy = y;
  205.                 boxlims[i].highx = x + 63;
  206.                 boxlims[i].highy = y + 63;
  207.         pw_text(main_pixwin,x+6,y+21,PIX_SRC|PIX_DST,bigfont,c);
  208.                 days_in_week++;
  209.         current.tm_mday++;
  210.                 if (days_in_week == 7){
  211.                         days_in_week = 0;
  212.                         x = left_border;
  213.                         y += 64;
  214.                 }
  215.         else
  216.                         x += 64;
  217.                 if (c[1] != '9')
  218.                         c[1]++;
  219.                 else {
  220.                         c[1] = '0';
  221.                         if (c[0] == ' ')
  222.                                 c[0] = '1';
  223.                         else
  224.                                 c[0]++;
  225.                 }
  226.         }
  227.         x = left_border + 27;
  228.         y = top_border - 16;
  229.     for (i=0; i<7; i++) {        /* Sun Mon ... Sat */
  230.         pw_char(main_pixwin,x,y,PIX_SRC,bigfont,daynames[i][0]);
  231.                 x += 64;
  232.         }
  233.  
  234.     bottom_border = boxlims[n_days-1].highy;
  235.  
  236.         /* Draw the "week arrows" */
  237.         arrow_index = 0;
  238.         last_top = -1;
  239.         for (i=0; i<n_days; i++)
  240.                 if (boxlims[i].lowy > last_top) {
  241.                         last_top = boxlims[i].lowy;
  242.                         week_arrows[arrow_index].active = 1;
  243.                         week_arrows[arrow_index].left = left_border - 64;
  244.                         week_arrows[arrow_index].top = last_top + 12;
  245.                         week_arrows[arrow_index].right =
  246.                           week_arrows[arrow_index].left + 43;
  247.                         week_arrows[arrow_index].bottom =
  248.                           week_arrows[arrow_index].top + 28;
  249.                         pw_write(main_pixwin, left_border-64, last_top + 12,
  250.                           43, 29, PIX_SRC, weekarrow_pr, 0, 0);
  251.                         arrow_index++;
  252.                 }
  253.     pw_batch_off(main_pixwin);
  254.     current = Save;
  255.     unlock_cursors();
  256. }
  257.