home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume17 / calentool / part13 / event.c next >
C/C++ Source or Header  |  1991-04-06  |  9KB  |  304 lines

  1. /*
  2.  * $Header: event.c,v 2.4 91/03/27 16:45:33 billr Exp $
  3.  */
  4. /*
  5.  * event.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.  *    Main driver and month and year event routines    *
  27.  *    for main subwindow                  *
  28.  *                            *
  29.  ********************************************************/
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <suntool/sunview.h>
  34. #include <suntool/panel.h>
  35. #include <suntool/canvas.h>
  36. #include <suntool/seln.h>
  37. #include <sys/file.h>
  38. #include "ct.h"
  39. #include "event.h"
  40.  
  41. extern Frame frame;
  42. extern Frame fframe, sframe, mframe, fileframe;
  43. extern struct tm olddate, closedate;
  44. extern int update_interval, show_time;
  45. extern char timestr[];
  46. extern Icon icon;
  47. extern int monday_first, hour24;
  48. extern Pixfont *sfont;
  49. extern Seln_client s_client;
  50. extern int locked;
  51. Notify_value myframe_interposer();
  52.  
  53. void
  54. mainsw_inputevent(canvas, event)
  55. Canvas canvas;
  56. Event *event;
  57. {
  58.     if (locked)    /* no updates allowed */
  59.         return;
  60.  
  61.     /* check for L7 key and close frame if found */
  62.     if (event_id(event) == KEY_LEFT(7) && event_is_up(event)) {
  63.         close_frame();
  64. #ifdef SUN3_5
  65.     } else if (event_id(event) == KEY_LEFT(5) && event_is_up(event)) {
  66.         /* expose (L5) event */
  67.         int rootfd, myfd;
  68.         struct screen frame_screen;
  69.  
  70.         /* Get root window from frame's desktop */
  71.         myfd = (int)window_get(frame, WIN_FD);
  72.         (void)win_screenget(myfd, &frame_screen);
  73.         rootfd = open(frame_screen.scr_rootname, O_RDONLY, 0);
  74.         if (rootfd == -1) {
  75.             err_rpt("couldn't open root screen", NON_FATAL);
  76.             return;
  77.         }
  78.         if (win_fdtonumber(myfd) == win_getlink(rootfd, WL_TOPCHILD))
  79.             /* we're on top */
  80.             wmgr_bottom(myfd, rootfd);
  81.         else
  82.             wmgr_top(myfd, rootfd);
  83.         close(rootfd);
  84. #endif
  85.     } else {
  86.         switch (mainsw_state) {
  87.             case DISPLAYING_DAY:
  88.                 if (event_id(event) == KEY_LEFT(6) && event_is_up(event)) {
  89.                     if (seln_acquire(s_client, SELN_PRIMARY) != SELN_PRIMARY) {
  90.                         err_rpt("Can't acquire selection primary", NON_FATAL);
  91.                         return;
  92.                     }
  93.                     if (seln_acquire(s_client, SELN_SHELF) != SELN_SHELF) {
  94.                         err_rpt("Can't acquire selection shelf", NON_FATAL);
  95.                         return;
  96.                     }
  97.                     seln_report_event(s_client, event);
  98.                 }
  99.                 day_inputevent(canvas, event);
  100.                 break;
  101.             case DISPLAYING_WEEK:
  102.                 week_inputevent(canvas, event);
  103.                 break;
  104.             case DISPLAYING_MONTH:
  105.                 month_inputevent(canvas, event);
  106.                 break;
  107.             case DISPLAYING_YEAR:
  108.                 year_inputevent(canvas, event);
  109.                 break;
  110.         }
  111.     }
  112. }
  113.  
  114. month_inputevent(canvas, event)
  115. Canvas canvas;
  116. Event *event;
  117. {
  118.         int i, x, y, week_index, new_day;
  119.  
  120.     /* translate coordinates to pixwin space */
  121.     event = canvas_window_event(canvas, event);
  122.         x = event_x(event);
  123.         y = event_y(event);          
  124.         if (event_id(event) != MS_LEFT)
  125.                 return;
  126.  
  127.         if (event_is_up(event))  {   /* Button up. */
  128.         fix_current_day();
  129.                 if (selected_type == DAY) {
  130.                         mainsw_state = DISPLAYING_DAY;
  131.                         window_set(canvas, WIN_CURSOR, day_cursor, 0);
  132.             draw_day();
  133.                 }
  134.                 else if (selected_type == WEEK) {
  135.             mainsw_state = DISPLAYING_WEEK;
  136.             window_set(canvas, WIN_CURSOR, week_cursor, 0);
  137.             draw_week();
  138.         }
  139.         return;
  140.     }
  141.  
  142.     /* Button down. */
  143.         selected_type = NONE;
  144.         for (i=0; i<monthlength(current.tm_mon); i++) {   /* In a day? */
  145.                 if ((x >= boxlims[i].lowx) &&
  146.                     (x <= boxlims[i].highx) &&
  147.                     (y >= boxlims[i].lowy) &&
  148.                     (y <= boxlims[i].highy)) {
  149.                 current.tm_mday = i + 1;
  150.                                 selected_type = DAY;
  151.                                 pw_write(main_pixwin,boxlims[i].lowx+3,
  152.                                   boxlims[i].lowy+3,58,58,PIX_NOT(PIX_DST),NULL,0,0);
  153.                                 return;
  154.                 }              
  155.         }                      
  156.         for (i=0; i<6; i++) {              /* No.  In a week? */
  157.                 if (week_arrows[i].active == 0)
  158.                         return;
  159.                 if ((x >= week_arrows[i].left) &&
  160.                     (x <= week_arrows[i].right) &&
  161.                     (y >= week_arrows[i].top) &&
  162.                     (y <= week_arrows[i].bottom))  {
  163.             week_index = i;
  164.             current.tm_mday = -current.tm_wday + 1 + (7 * week_index);
  165.             if (monday_first) {
  166.                 current.tm_mday++;
  167.                 if (current.tm_wday == SUN)
  168.                     current.tm_mday -= 7;
  169.             }
  170.                         selected_type = WEEK;
  171.                         pw_write(main_pixwin,week_arrows[week_index].left,
  172.                           week_arrows[week_index].top,smallarrow_pr->pr_size.x,
  173.               smallarrow_pr->pr_size.y,PIX_SRC^PIX_DST,
  174.                           smallarrow_pr,0,0);
  175.                         return;
  176.                 }              
  177.         }                      
  178. }                               
  179.  
  180. year_inputevent(canvas, event)
  181. Canvas canvas;
  182. Event *event;
  183. {
  184.     int x, y, i;
  185.     static int mday;
  186.  
  187.     /* translate coordinates to pixwin space */
  188.     event = canvas_window_event(canvas, event);
  189.         x = event_x(event);
  190.         y = event_y(event);          
  191.         if (event_id(event) != MS_LEFT)
  192.                 return;
  193.         if (event_is_up(event))  {       /* Button up. */
  194.         if (selected_type == MONTH) {
  195.             mainsw_state = DISPLAYING_MONTH;
  196.             window_set(canvas, WIN_CURSOR, month_cursor, 0);
  197.             draw_month();
  198.         }
  199.         return;
  200.     }
  201.  
  202.     /* Button down. */
  203.     selected_type = NONE;
  204.         for (i=0; i<12; i++) {                   /* In a month? */
  205.                 if ((x >= mboxlims[i].lowx) &&
  206.                     (x <= mboxlims[i].highx) &&
  207.                     (y >= mboxlims[i].lowy) &&
  208.                     (y <= mboxlims[i].highy)) {
  209.             selected_type = MONTH;
  210.             current.tm_mday = 1;
  211.             current.tm_mon = i;
  212.             pw_write(main_pixwin,mboxlims[i].lowx,
  213.               mboxlims[i].lowy,7*ybox_width,ybox_height-1,PIX_NOT(PIX_DST),NULL,0,0);
  214.             break;
  215.         }
  216.     }
  217. }
  218.  
  219. Notify_value
  220. check_close(client, event, arg, when)
  221. Notify_client client;
  222. Event *event;
  223. Notify_arg arg;
  224. Notify_event_type when;
  225. {
  226.     /* check for L7 key and close frame if found */
  227.     /*** DEBUG ***/
  228.     /*
  229.     fprintf(stderr, "checking for L7: event = %d\n", event_id(event));
  230.     */
  231.     if (event_id(event) == KEY_LEFT(7) && event_is_up(event))
  232.         return (myframe_interposer(client, event, arg, when));
  233.     else
  234.         return (notify_next_event_func(client, event, arg, when));
  235. }
  236.  
  237. close_frame()
  238. {
  239.     Icon cur_icon;
  240.  
  241.     /* save some information as we close */
  242.     if (mainsw_state == DISPLAYING_DAY && day_is_open)
  243.         close_day();
  244.     /* if frame not closed yet, close it now (for the canvas) */
  245.     if (!(int)window_get(frame, FRAME_CLOSED))
  246.         window_set(frame, FRAME_CLOSED, TRUE, 0);
  247.     olddate = current;
  248.     get_today();
  249.     closedate = today;
  250.     if (fframe) {
  251.         /* kill off future appt popup */
  252.         window_destroy(fframe);
  253.         fframe = 0;
  254.     }
  255.     /* hide file frame */
  256.     window_set(fileframe, WIN_SHOW, FALSE, 0);
  257. #ifndef NO_SUN_MOON
  258.     /* kill sun/moon data frames */
  259.     if (mframe)
  260.         mframe_done(0);
  261.     if (sframe)
  262.         sframe_done(0);
  263. #endif
  264.     check_calendar();    /* update icon */
  265.     if (show_time)
  266.         /* update time label */
  267.         update_icon_time();
  268. }
  269.  
  270. /* update the time field of the current icon */
  271. update_icon_time()
  272. {
  273.     Icon cur_icon;
  274.  
  275.     format_icon_time();
  276.     cur_icon = (Icon) window_get(frame, FRAME_ICON);
  277.     icon_set(cur_icon, ICON_LABEL, timestr, 0);
  278.     window_set(frame, FRAME_ICON, cur_icon, 0);
  279. }
  280.  
  281. format_icon_time()
  282. {
  283.     if (update_interval >= 60)
  284.         /* display hh:mm */
  285.         sprintf(timestr, " %2d:%02d", today.tm_hour, today.tm_min);
  286.     else
  287.         /* display hh:mm:ss */
  288.         sprintf(timestr, " %2d:%02d:%02d", today.tm_hour, today.tm_min, today.tm_sec);
  289.     if (!hour24) {
  290.         /* display am/pm for 12-hour time */
  291.         if (today.tm_hour > 12) {
  292.             strcat(timestr, "pm");
  293.             timestr[1] = ((today.tm_hour - 12) / 10) + '0';
  294.             timestr[2] = ((today.tm_hour - 12) % 10) + '0';
  295.         } else if (today.tm_hour == 12) {
  296.             strcat(timestr, "pm");
  297.         } else {
  298.             strcat(timestr, "am");
  299.         }
  300.         if (timestr[1] == '0')
  301.             timestr[1] = ' ';
  302.     }
  303. }
  304.