home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 5 / RISCWORLD_VOL5.iso / SOFTWARE / Issue3 / FAMILY / Calendar / Demo / !Datum / c / handler next >
Encoding:
Text File  |  2003-09-20  |  7.3 KB  |  281 lines

  1. /*
  2.  * $Id: handler 1.17 2003-09-20 12:45:04+01 erikgrnh Exp $
  3.  *
  4.  * $Source: /ADFS::Csite.\044/C/User/!Calendar/Demo/!Datum/c/_RCS/handler $
  5.  */
  6.  
  7. /* include standard C definitions */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. #include "strings.h"
  14. #include "swis.h"
  15.  
  16. /* include event lib declarations */
  17.  
  18. #include "event.h"
  19. #include "wimplib.h"
  20.  
  21. /* include toolbox lib declarations */
  22.  
  23. #include "toolbox.h"
  24. #include "window.h"
  25. #include "gadgets.h"
  26. #include "menu.h"
  27. #include "scale.h"
  28. #include "quit.h"
  29. #include "proginfo.h"
  30. #include "printdbox.h"
  31. #include "iconbar.h"
  32.  
  33. /* Private includes */
  34.  
  35. #define os_error _kernel_oserror
  36. #include "CalLib:calendar.h"
  37.  
  38. /* include application definitions */
  39.  
  40. #include "main.h"
  41.  
  42. /* Support for this source */
  43.  
  44. #include "handler.h"
  45.  
  46. char const handler_c_ID[] =
  47.    "$Id: handler 1.17 2003-09-20 12:45:04+01 erikgrnh Exp $";
  48.  
  49. /* Names for the gadget numbers */
  50. enum gadget_numbers {
  51.    G_type    = 0x100, /* StringSet: Calender type */
  52.    G_day     = 0x101, /* NumRange:  Day of the month */
  53.    G_month   = 0x102, /* NumRange:  Month number */
  54.    G_mname   = 0x103, /* StringSet: Month name */
  55.    G_year    = 0x104, /* NumRange:  Year */
  56.    G_dow     = 0x105, /* StringSet: Day of the week */
  57.    G_week    = 0x106, /* NumRange:  Week number */
  58.    G_totweek = 0x107, /* Display:   Total weeks in the year */
  59.    G_doy     = 0x108, /* NumRange:  Day of the year */
  60.    G_totday  = 0x109, /* Display:   Total days in the year */
  61.    G_julday  = 0x10a, /* NumRange:  Julian Day number */
  62.    G_nothing
  63. };
  64.  
  65.  
  66. /* Set all values in accordance with the Julian Day number */
  67. static int set_all(int jday) {
  68.    int cal;
  69.    int year, month, day;
  70.    int jday1st;
  71.    int next1st;
  72.  
  73.    /* Find out which calendar type is selected */
  74.    ERROR(stringset_get_selected(1, mainwindow, G_type, &cal));
  75.    cal++;
  76.  
  77.    /* Calculate year/month/day */
  78.    errp = xcalendar_jday_to_date(cal, jday, &year, &month, &day);
  79.    if (errp != NULL) {
  80.       wimp_report_error(errp,0,"Datum");
  81.       return 1;
  82.    }
  83.  
  84.    /* Set year/month/day displays */
  85.    ERROR(numberrange_set_value(0, mainwindow, G_day   ,   day));
  86.    ERROR(numberrange_set_value(0, mainwindow, G_month , month));
  87.    ERROR(stringset_set_selected(1, mainwindow, G_mname, (char *)month-1 ));
  88.    ERROR(numberrange_set_value(0, mainwindow, G_year  ,  year));
  89.  
  90.    ERROR(numberrange_set_value(0, mainwindow, G_julday,  jday));
  91.  
  92.    /* Find day of the year: is diff with 1st Jan */
  93.    errp = xcalendar_date_to_jday(cal, year, 1, 1, &jday1st);
  94.    if (errp != NULL) {
  95.       wimp_report_error(errp,0,"Datum");
  96.       return 1;
  97.    }
  98.    ERROR(numberrange_set_value(0, mainwindow, G_doy, jday-jday1st+1));
  99.  
  100.    /* Find nr of days in the year: is diff 1st Jan this year and next year */
  101.    errp = xcalendar_date_to_jday(cal, year+1, 1, 1, &next1st);
  102.    if (errp != NULL) {
  103.       wimp_report_error(errp,0,"Datum");
  104.       return 1;
  105.    }
  106.    ERROR(numberrange_set_value(0, mainwindow, G_totday, next1st-jday1st));
  107.  
  108.    return 0;
  109. }
  110.  
  111.  
  112. /* Initialise the values in the window */
  113. static int init_window(void) {
  114.    int jday = 12345;
  115.    time_t now;
  116.    struct tm *today;
  117.  
  118.    /* Set calendar type to Gregorian */
  119.    ERROR(stringset_set_selected(1, mainwindow, G_type,
  120.             (char *)calendar_GREGORIAN-1 ));
  121.  
  122.    /* Find the date of today */
  123.    now = time(NULL);
  124.    today = localtime(&now);
  125.    errp = xcalendar_date_to_jday(calendar_GREGORIAN, today->tm_year+1900,
  126.                      today->tm_mon+1, today->tm_mday, &jday);
  127.    if (errp != NULL) {
  128.       wimp_report_error(errp,0,"Datum");
  129.       return 1;
  130.    }
  131.  
  132.    /* Set Julian Day number */
  133.    ERROR(numberrange_set_value(0, mainwindow, G_julday,  jday));
  134.  
  135.    /* Adjust all values */
  136.    (void) set_all(jday);
  137.  
  138.    return 0;
  139. }
  140.  
  141. /* handlers...*/
  142.  
  143. int tbquit_handler(int event_code, ToolboxEvent  *event, IdBlock *id_block,
  144.                    void  *handle)
  145. {
  146.   IGNORE(event_code);
  147.   IGNORE(event);
  148.   IGNORE(id_block);
  149.   IGNORE(handle);
  150.  
  151.   quit =1;
  152.   return 1;
  153. }
  154.  
  155. int quit_handler(WimpMessage *message, void *handle)
  156. {
  157.   IGNORE(message);
  158.   IGNORE(handle);
  159.  
  160.   quit =1;
  161.   return 1;
  162. }
  163.  
  164. int proginfo_show(int event_code, ToolboxEvent  *event, IdBlock *id_block,
  165.                    void  *handle)
  166. {
  167.    char version[32];
  168.    char *p, *q;
  169.  
  170.    IGNORE(handle);
  171.    IGNORE(event);
  172.    IGNORE(event_code);
  173.  
  174.    /* Find the first space in version */
  175.    p = strchr(version_c_REV, ' ');
  176.    if(p == NULL) return 1; /* No change if no value */
  177.    p++;
  178.    /* Find the next space */
  179.    q = strchr(p,' ');
  180.    strncpy(version,p,q-p); /* Copy to string */
  181.    version[q-p]='\0'; /* Terminate (strncpy does not do this) */
  182.  
  183.    /* Find the first space in date */
  184.    p = strchr(version_c_DATE, ' ');
  185.    if(p != NULL) { /* No change if no value */
  186.       p++;
  187.       /* Find the next space */
  188.       q = strchr(p,' ');
  189.       q++;
  190.       strcat(version," (");
  191.       strncat(version, p, q-p);
  192.       strcat(version,")");
  193.    } else {
  194.       strcat(version," (yyyy-mm-dd)");
  195.    }
  196.    proginfo_set_version(0,id_block->self_id,version);
  197.  
  198.    return 1;
  199. }
  200.  
  201. int toolbox_create_handler(int event_code, ToolboxEvent  *event,
  202.          IdBlock *id_block, void  *handle )
  203. {
  204.    ToolboxObjectAutoCreatedEvent *creat =
  205.          (ToolboxObjectAutoCreatedEvent *) event;
  206.  
  207.    IGNORE(event_code);
  208.    IGNORE(handle);
  209.  
  210.    if(strcmp(creat->template_name, "Window") == 0) {
  211.       mainwindow = id_block->self_id;
  212.       init_window();
  213.    }
  214.  
  215.    return 1;
  216. }
  217.  
  218. int value_changed_handler(int event_code, ToolboxEvent  *event,
  219.          IdBlock *id_block, void  *handle )
  220. {
  221.    ComponentId gadget = id_block->self_component;
  222.    int cal;
  223.    int year, month, day;
  224.    int jday;
  225.    int doy;
  226.  
  227.    IGNORE(event);
  228.    IGNORE(event_code);
  229.    IGNORE(handle);
  230.  
  231.    /* Ignore event if it is not for our window */
  232.    if(id_block->self_id != mainwindow) {
  233.       return 0;
  234.    }
  235.  
  236.    /* Find out which calendar type is selected */
  237.    ERROR(stringset_get_selected(1, mainwindow, G_type, &cal)); cal++;
  238.  
  239.    switch(gadget) {
  240.    case G_type    : /* StringSet: Calender type */
  241.       ERROR(numberrange_get_value(0, mainwindow, G_julday, &jday));
  242.       set_all(jday);
  243.       break;
  244.    case G_day     : /* NumRange:  Day of the month */
  245.    case G_month   : /* NumRange:  Month number */
  246.    case G_year    : /* NumRange:  Year */
  247.       ERROR(numberrange_get_value(0, mainwindow, G_day, &day));
  248.       ERROR(numberrange_get_value(0, mainwindow, G_month, &month));
  249.       ERROR(numberrange_get_value(0, mainwindow, G_year, &year));
  250.       xcalendar_date_to_jday(cal, year, month, day, &jday);
  251.       set_all(jday);
  252.       break;
  253.    case G_mname   : /* StringSet: Month name */
  254.       ERROR(numberrange_get_value(0, mainwindow, G_day, &day));
  255.       ERROR(stringset_get_selected(1, mainwindow, G_mname, &month)); month++;
  256.       ERROR(numberrange_get_value(0, mainwindow, G_year, &year));
  257.       xcalendar_date_to_jday(cal, year, month, day, &jday);
  258.       set_all(jday);
  259.       break;
  260.    case G_dow     : /* StringSet: Day of the week */
  261.       break;
  262.    case G_week    : /* NumRange:  Week number */
  263.       break;
  264.    case G_doy     : /* NumRange:  Day of the year */
  265.       ERROR(numberrange_get_value(0, mainwindow, G_doy, &doy));
  266.       ERROR(numberrange_get_value(0, mainwindow, G_year, &year));
  267.       xcalendar_date_to_jday(cal, year, 1, 1, &jday);
  268.       set_all(jday+doy-1);
  269.       break;
  270.    case G_julday  : /* NumRange:  Julian Day number */
  271.       ERROR(numberrange_get_value(0, mainwindow, G_julday, &jday));
  272.       set_all(jday);
  273.       break;
  274.    default:
  275.       return 0;
  276.    }
  277.  
  278.    return 1;
  279. }
  280.  
  281.