home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2302 / CalendarP.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  10.0 KB  |  348 lines

  1. /*
  2.  * Author: Jason Baietto, jason@ssd.csd.harris.com
  3.  * xdiary Copyright 1990 Harris Corporation
  4.  *
  5.  * Permission to use, copy, modify, and distribute, this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of the copyright holder be used in
  10.  * advertising or publicity pertaining to distribution of the software with
  11.  * specific, written prior permission, and that no fee is charged for further
  12.  * distribution of this software, or any modifications thereof.  The copyright
  13.  * holder makes no representations about the suitability of this software for
  14.  * any purpose.  It is provided "as is" without express or implied warranty.
  15.  *
  16.  * THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO
  18.  * EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM ITS USE,
  20.  * LOSS OF DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT,
  21.  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  22.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  */
  24.  
  25. #ifndef _CalendarWidgetP_h
  26. #define _CalendarWidgetP_h
  27.  
  28. /* CalendarP.h -- Private Include File. */
  29.  
  30. #include <X11/CoreP.h>
  31. #include <values.h>
  32. #include "Calendar.h"
  33. #include "Gravity.h"
  34. #include "DayName.h"
  35. #include "StrTable.h"
  36. #include "Date.h"
  37.  
  38. /* Widget's Class Part: */
  39. typedef struct {
  40.    int make_compiler_happy;
  41. } CalendarClassPart;
  42.  
  43. /* Widget's Full Class Record: */
  44. typedef struct _CalendarClassRec {
  45.    CoreClassPart core_class;
  46.    CalendarClassPart calendar_class;
  47. } CalendarClassRec;
  48.  
  49. /* State info to avoid scrogging the calendar. */
  50. typedef enum {
  51.    plasma,       /* Before init -- i.e. uninitialized.       */
  52.    foggy,        /* Just after initialize or resize method.  */
  53.    solid         /* Just after expose until next resize.     */
  54. } CalendarState;
  55.  
  56. #define DAYS_IN_WEEK (7)
  57. #define MAX_DAYS_IN_MONTH (31)
  58. #define MONTHS_IN_YEAR (12)
  59. #define MAX_YEAR_LEN (10)
  60.  
  61. #define ROWS (8)
  62. #define COLS (DAYS_IN_WEEK)
  63.  
  64. #define HORIZ_SEGMENTS (ROWS-2)
  65. #define VERTI_SEGMENTS (COLS-1)
  66. #define BORDER_SEGMENTS (4)
  67. #define TOTAL_SEGMENTS (HORIZ_SEGMENTS+VERTI_SEGMENTS+BORDER_SEGMENTS)
  68.  
  69. /* Calendar Widget's Instance Part: */
  70. typedef struct {
  71.  
  72. /* Public Resources */
  73.  
  74.    /* Callback list for all buttons */
  75.    XtCallbackList callback;
  76.  
  77.    /* Line width of calendar lines */
  78.    int            line_width;
  79.  
  80.    Pixel          foreground;
  81.    Pixel          background;
  82.  
  83.    Font           digit_font;
  84.    Font           weekday_font;
  85.    Font           title_font;
  86.    Font           info_font;
  87.    
  88.    /* Gravity of the digits in their boxes */
  89.    XtGravity      digit_gravity;
  90.  
  91.    /* String table for names of digits. */
  92.    StringTable    digit_names;
  93.  
  94.    /* String table for names of days of the week. */
  95.    StringTable    weekday_names;
  96.  
  97.    /* String table for names of months. */
  98.    StringTable    month_names;
  99.  
  100.    /* True means highlight specified day (if any) */
  101.    Boolean        highlight;
  102.  
  103.    /* True means show year along with month in title. */
  104.    Boolean        show_year;
  105.  
  106.    /* Weekday for leftmost column in calendar. 0=Sunday, 6=Saturday. */
  107.    int            starting_weekday;
  108.  
  109. /* Private */
  110.    
  111.    /* Date for calendar to display month for.  Initially current. */
  112.    Date          date;
  113.  
  114.    /* The current highlighted date.  Initially current. */
  115.    Date          highlight_date;
  116.  
  117.    /* Current cell dimensions */
  118.    float         real_cell_width;
  119.    float         real_cell_height;
  120.  
  121.    /* Minimum cell dimensions based on current font extents */
  122.    float         min_cell_width;
  123.    float         min_cell_height;
  124.    
  125.    /* Cell matrix of XRectangles */
  126.    XRectangle    cell_geometry[ROWS][COLS];
  127.  
  128.    /* Currently selected cell coordinate */
  129.    int           current_x_cell;
  130.    int           current_y_cell;
  131.  
  132.    /* Previous core width (updated after resize) */
  133.    int           old_window_width;
  134.    int           old_window_height;
  135.  
  136.    /* Month data for the showing date's year. */
  137.    int           month_starting_weekdays[MONTHS_IN_YEAR];
  138.    int           days_in_february;
  139.  
  140.    /* Month data for the highlight_date's year. */
  141.    int           highlight_days_in_february;
  142.  
  143.    /* Simplicity variables to avoid recomputing. */
  144.    int           month_start_cellnum;
  145.    int           month_end_cellnum;
  146.  
  147.    /* lengths of strings frequently drawn in calendar */
  148.    int           digit_name_lengths[MAX_DAYS_IN_MONTH];
  149.    int           weekday_name_lengths[DAYS_IN_WEEK];
  150.    int           month_name_lengths[MONTHS_IN_YEAR];
  151.  
  152.    /* Year information for the current calendar */
  153.    char          year_string[MAX_YEAR_LEN];
  154.    int           year_string_length;
  155.    int           current_year;
  156.  
  157.    /* Title information for the current calendar */
  158.    char *        title_string;
  159.    int           title_string_length;
  160.    XRectangle    title_geometry;
  161.  
  162.    /* Widget state info.  Used to avoid changing the date between the */
  163.    /* resize or initialization method and the expose method.          */
  164.  
  165.    /* True if a valid cell has been selected with the pointer. */
  166.    Boolean        cell_selected;
  167.  
  168.    /* Last method that has been run. */
  169.    CalendarState  state;
  170.  
  171.    /* Segments for drawing the grid. */
  172.    XSegment       segments[TOTAL_SEGMENTS];
  173.  
  174.    /* The number of segments currently in the segments array. */
  175.    int            number_segments;
  176.  
  177.    /* GCs for line/box drawing */
  178.    GC            draw_gc;
  179.    GC            undraw_gc;
  180.    GC            invert_gc;
  181.  
  182.    /* GCs and font structs for text drawing */
  183.    GC            digit_draw_gc;
  184.    GC            digit_undraw_gc;
  185.    XFontStruct * digit_fsp;
  186.  
  187.    GC            weekday_draw_gc;
  188.    GC            weekday_undraw_gc;
  189.    XFontStruct * weekday_fsp;
  190.  
  191.    GC            title_draw_gc;
  192.    GC            title_undraw_gc;
  193.    XFontStruct * title_fsp;
  194.  
  195.    GC            info_draw_gc;
  196.    GC            info_undraw_gc;
  197.    XFontStruct * info_fsp;
  198.  
  199. } CalendarPart;
  200.  
  201. /* Widget's Full Instance Record: */
  202. typedef struct _CalendarRec {
  203.    CorePart     core;
  204.    CalendarPart calendar;
  205. } CalendarRec;
  206.  
  207. extern CalendarClassRec calendarClassRec;
  208.  
  209. #define SUNDAY    0
  210. #define MONDAY    1
  211. #define TUESDAY   2
  212. #define WEDNESDAY 3
  213. #define THURSDAY  4
  214. #define FRIDAY    5
  215. #define SATURDAY  6
  216.  
  217. #define XtNDefaultWeekdayNames \
  218.     "\"Sun\",\"Mon\",\"Tue\",\"Wed\",\"Thu\",\"Fri\",\"Sat\""
  219.  
  220. #define XtNDefaultMonthNames \
  221.     "\"January\",\"February\",\"March\",\"April\", \
  222.      \"May\",\"June\",\"July\",\"August\",\"September\", \
  223.      \"October\",\"November\",\"December\""
  224.  
  225. #define XtNDefaultDigitNames \
  226.    "\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\", \
  227.    \"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"20\", \
  228.    \"21\",\"22\",\"23\",\"24\",\"25\",\"26\",\"27\",\"28\",\"29\",\"30\", \
  229.    \"31\""
  230.  
  231. #define OFF (0)
  232. #define ON (1)
  233.  
  234. #define WINDOW_HEIGHT(cell_height) ((int)((cell_height)*ROWS))
  235. #define WINDOW_WIDTH(cell_width)   ((int)((cell_width )*COLS))
  236.  
  237. #define MIN_LINE_WIDTH (1)
  238. #define MAX_LINE_WIDTH (10)
  239. #define DEFAULT_LINE_WIDTH (1)
  240.  
  241. #define INVERT_BORDER (1)
  242.  
  243. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  244. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  245. #define MAX3(a,b,c) (MAX(MAX((a),(b)),(c)))
  246.  
  247. #define MAX_TITLE_LEN (100)
  248.  
  249. /* This assumes integer data types */
  250. #define range_check(name, val, min, max) \
  251. { \
  252.    if ((int)(val) < (int)(min)) { \
  253.       fprintf(stderr, "Warning: %s=%d too small (using %d)\n", name, val, min); \
  254.       (val) = (min); \
  255.    } else if ((int)(val) > (int)(max)) { \
  256.       fprintf(stderr, "Warning: %s=%d too large (using %d)\n", name, val, max); \
  257.       (val) = (max); \
  258.    } \
  259. }
  260.  
  261. #define bound(val, min, max) \
  262.    ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
  263.  
  264. #define low_bound(val, min) \
  265.    ((val) < (min) ? (min) : (val))
  266.  
  267. #define high_bound(val, max) \
  268.    ((val) > (max) ? (max) : (val))
  269.  
  270.  
  271. #define CELLXYtoCELLNUM(x,y) (((y)-2)*COLS+(x))
  272.  
  273. #define CELLNUMtoCELLXY(n,x,y) \
  274.    ( \
  275.      (x) = (n)%7, \
  276.      (y) = ((n)/7)+2 \
  277.    )
  278.  
  279. #define CELLNUMtoDAYNUM(cn) \
  280.    ( \
  281.       ( (cn) < widget->calendar.month_start_cellnum || \
  282.         (cn) > widget->calendar.month_end_cellnum ) \
  283.       ? 0 \
  284.       : (cn) - widget->calendar.month_start_cellnum + 1 \
  285.    )
  286.  
  287. #define CELLXYtoDAYNUM(x,y) (CELLNUMtoDAYNUM(CELLXYtoCELLNUM((x),(y))))
  288.    
  289. #define JANUARY 0
  290. #define FEBRUARY 1
  291. #define MARCH 2
  292. #define APRIL 3
  293. #define MAY 4
  294. #define JUNE 5
  295. #define JULY 6
  296. #define AUGUST 7
  297. #define SEPTEMBER 8
  298. #define OCTOBER 9 
  299. #define NOVEMBER 10
  300. #define DECEMBER 11
  301.  
  302. #define A_LEAP_YEAR(year) (year%4 == 0 && !(year%100 == 0 && year%400 != 0))
  303.  
  304. /* 0=Sunday, 6=Saturday */
  305. #define DAYStoWEEKDAY(days) ((((days)%7)+6)%7)
  306.  
  307. #define ROTATE(widget, weekday) \
  308.    ( ((weekday) + (7 - (widget)->calendar.starting_weekday)) % 7 )
  309.  
  310. #define DAYS_IN_MONTH(widget) \
  311.    ( (widget)->calendar.date.month == FEBRUARY+1 \
  312.      ? (widget)->calendar.days_in_february \
  313.      : days_in_month[(widget)->calendar.date.month - 1] \
  314.    )
  315.  
  316. #define DAYS_IN_HIGHLIGHT_MONTH(widget) \
  317.    ( (widget)->calendar.highlight_date.month == FEBRUARY+1 \
  318.      ? (widget)->calendar.highlight_days_in_february \
  319.      : days_in_month[(widget)->calendar.highlight_date.month - 1] \
  320.    )
  321.  
  322. #define WIDGETtoCELLXY(widget,x,y) \
  323.    ( (widget)->calendar.highlight_date.day \
  324.      ? ( \
  325.          (x) = ( (widget)->calendar.month_start_cellnum   \
  326.                  + (widget)->calendar.highlight_date.day - 1 ) % 7, \
  327.          (y) = ( ( (widget)->calendar.month_start_cellnum   \
  328.                    + (widget)->calendar.highlight_date.day - 1 ) / 7 ) + 2 \
  329.        ) \
  330.      : ( \
  331.          (x) = 0 , \
  332.          (y) = 0   \
  333.        ) \
  334.    )
  335.  
  336. #define GEOMETRY(widget,x,y) \
  337.    ((widget)->calendar.cell_geometry[(y)][(x)])
  338.  
  339. #define SYNC(widget) \
  340.    ((widget)->calendar.date.month == (widget)->calendar.highlight_date.month \
  341.     && (widget)->calendar.date.year == (widget)->calendar.highlight_date.year)
  342.  
  343. #define MAX_PRETTY_DATE_STRING_LENGTH 256
  344.  
  345. #define CALENDAR_DEFAULT_FONT "fixed"
  346.  
  347. #endif /* _CalendarWidgetP_h */
  348.