home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3065 / month.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-15  |  6.8 KB  |  262 lines

  1. /*
  2.  *    month.c : Widget creation and action binding for the month
  3.  *          forms, and their sub-widgets.
  4.  *
  5.  *      George Ferguson, ferguson@cs.rochester.edu, 27 Oct 1990.
  6.  *    Version 1.1 - 27 Feb 1991.
  7.  *
  8.  *    $Id: month.c,v 2.1 91/02/28 11:21:22 ferguson Exp $
  9.  */
  10. #include <X11/Intrinsic.h>
  11. #include <X11/Shell.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/Xaw/Form.h>
  14. #include <X11/Xaw/Command.h>
  15. #include <X11/Xaw/Toggle.h>
  16. #include <X11/Xaw/Label.h>
  17. #include <X11/Xaw/AsciiText.h>
  18. #include <X11/Xaw/Cardinals.h>
  19. #include "xkal.h"
  20. #include "month.h"
  21. #include "day.h"
  22. #include "db.h"
  23. #include "app-resources.h"
  24. #include "date-strings.h"
  25.  
  26. /*
  27.  * Functions defined in this file
  28.  */
  29. MonthFormData *createMonthFormData();
  30. void setMonthFormData();
  31. void selectDay();
  32. void shadeButton();
  33.  
  34. static void dayButtonCallbackProc();
  35. static void getGCAndXY();
  36.  
  37. /*
  38.  * Data defined in this file
  39.  */
  40. static Widget radioGroup;
  41.  
  42. #define CALLBACK_PROC(NAME)     static void NAME(w,client_data,call_data) \
  43.                     Widget w; \
  44.                     caddr_t client_data,call_data;
  45.  
  46.  
  47. /*    -    -    -    -    -    -    -    -    */
  48. /*
  49.  * createMonthFormData() : Returns a monthFormData whose Form is unmanaged.
  50.  */
  51. MonthFormData *
  52. createMonthFormData(parent,name,num)
  53. Widget parent;
  54. char *name;
  55. int num;        /* 1,2,3,12 */
  56. {
  57.     MonthFormData *m;
  58.     DayButtonData *d;
  59.     Widget widget;
  60.     Arg args[3];
  61.     char text[16];
  62.     int row,col,index;
  63.     int w,h;
  64.  
  65.     m = XtNew(MonthFormData);
  66.     m->form = XtCreateWidget(name,formWidgetClass,parent,NULL,ZERO);
  67.     switch (num) {
  68.     case 1: w = appResources.dateWidth1;
  69.         h = appResources.dateHeight1;
  70.         break;
  71.     case 2: w = appResources.dateWidth2;
  72.         h = appResources.dateHeight2;
  73.         break;
  74.     case 3: w = appResources.dateWidth3;
  75.         h = appResources.dateHeight3;
  76.         break;
  77.     case 12: w = appResources.dateWidth12;
  78.          h = appResources.dateHeight12;
  79.          break;
  80.     }
  81.     sprintf(text,"monthLabel%d",num);
  82.     m->label = XtCreateManagedWidget(text,labelWidgetClass,m->form,NULL,ZERO);
  83.     if (appResources.dowLabels) {
  84.     sprintf(text,"dowLabel%d",num);
  85.     XtSetArg(args[0],XtNfromVert,m->label);
  86.     XtSetArg(args[1],XtNfromHoriz,NULL);
  87.     index = appResources.dowOffset;
  88.     for (col=0; col < 7; col++) {
  89.         XtSetArg(args[2],XtNlabel,shortDowStr[index]);
  90.         widget = XtCreateManagedWidget(text,labelWidgetClass,m->form,
  91.                                 args,THREE);
  92.         index = (index == 6) ? 0 : index+1;
  93.         XtSetArg(args[1],XtNfromHoriz,widget);
  94.     }
  95.     XtSetArg(args[0],XtNfromVert,widget);
  96.     } else {
  97.     XtSetArg(args[0],XtNfromVert,m->label);
  98.     }
  99.     sprintf(text,"dayButton%d",num);
  100.     for (row=0; row < 6; row++) {
  101.     XtSetArg(args[1],XtNfromHoriz,NULL);
  102.     for (col=0; col < 7; col++) {
  103.         d = m->days[row*7+col] = XtNew(DayButtonData);
  104.         d->button = XtCreateManagedWidget(text,toggleWidgetClass,
  105.                             m->form,args,TWO);
  106.         XtAddCallback(d->button,"callback",dayButtonCallbackProc,d);
  107.         XtSetArg(args[2],XtNradioData,d->button);
  108.         XtSetValues(d->button,args+2,ONE);
  109.         if (radioGroup == NULL)
  110.         radioGroup = d->button;
  111.         XawToggleChangeRadioGroup(d->button,radioGroup);
  112.         d->pixmap = XCreatePixmap(display,root,w,h,
  113.                         DefaultDepthOfScreen(screen));
  114.  
  115.         XtSetArg(args[1],XtNfromHoriz,d->button);
  116.     }
  117.         XtSetArg(args[0],XtNfromVert,d->button);
  118.     }
  119.     return(m);
  120. }
  121.  
  122. /*
  123.  * setMonthFormData() : Draw the individual days in the month, including
  124.  *    the date and shading for criticality.
  125.  */
  126. void
  127. setMonthFormData(m,num,month,year)
  128. MonthFormData *m;
  129. int num,month,year;
  130. {
  131.     DayButtonData *d;
  132.     Arg args[1];
  133.     GC gc;
  134.     char text[16];
  135.     int first,numDays;
  136.     int i,x,y;
  137.  
  138.     getGCAndXY(num,&gc,&x,&y);
  139.     XawFormDoLayout(m->form,False);
  140.     XawToggleUnsetCurrent(radioGroup);
  141.     sprintf(text,"%s %d",longMonthStr[month-1],year);
  142.     XtSetArg(args[0],XtNlabel,text);
  143.     XtSetValues(m->label,args,ONE);
  144.     first = firstDOW(month,year)-1-appResources.dowOffset;
  145.     numDays = lastDay(month,year);
  146.     for (i=0; i < 42; i++) {
  147.     d = m->days[i];
  148.     if (i < first || i >= first+numDays) {
  149.         d->day = 0;
  150.         XFillRectangle(display,d->pixmap,emptyGC,0,0,50,50);
  151.         XtSetArg(args[0],XtNbitmap,d->pixmap);
  152.         XtSetValues(d->button,args,ONE);
  153.     } else {
  154.         d->day = i-first+1;
  155.         d->month = month;
  156.         d->year = year;
  157.         shadeButton(d,gc,x,y);
  158.     }
  159.     }
  160.     XawFormDoLayout(m->form,True);
  161.     m->month = month;
  162.     m->year = year;
  163. }
  164.  
  165. /*
  166.  * setGCAndXY() : Depending on num, parses the appropriate geometry
  167.  *    string into *xp and *yp, and puts the correct GC* into gcp.
  168.  *    This function is here so it can be outside the loop in
  169.  *    setMonthFormData(), but really belongs in shadeButton.
  170.  */
  171. static void
  172. getGCAndXY(num,gcp,xp,yp)
  173. GC *gcp;
  174. int *xp,*yp;
  175. {
  176.     int w,h;
  177.  
  178.     switch (num) {
  179.     case 1: XParseGeometry(appResources.datePosition1,xp,yp,&w,&h);
  180.         *gcp = dateGC1;
  181.         break;
  182.     case 2: XParseGeometry(appResources.datePosition2,xp,yp,&w,&h);
  183.         *gcp = dateGC2;
  184.         break;
  185.     case 3: XParseGeometry(appResources.datePosition3,xp,yp,&w,&h);
  186.         *gcp = dateGC3;
  187.         break;
  188.     case 12: XParseGeometry(appResources.datePosition12,xp,yp,&w,&h);
  189.          *gcp = dateGC12;
  190.          break;
  191.     }
  192. }
  193.  
  194. /*
  195.  * shadeButton() : Shades a dayButton. This is global so it can be called
  196.  *    after the appointments have changed without redrawing everything.
  197.  */
  198. void
  199. shadeButton(d,gc,x,y)
  200. DayButtonData *d;
  201. GC gc;
  202. int x,y;
  203. {
  204.     Arg args[1];
  205.     char text[4];
  206.     int n;
  207.  
  208.     if (gc == (GC)NULL)
  209.     getGCAndXY(appResources.numMonths,&gc,&x,&y);
  210.     n = lookupEntry(d->day,d->month,d->year,-1,-1,-1,NULL,True);
  211.     if (n > appResources.maxLevel)
  212.     n = appResources.maxLevel;
  213.     XFillRectangle(display,d->pixmap,shadeGC[n],0,0,
  214.             appResources.dateWidth1,appResources.dateHeight1);
  215.     sprintf(text,"%d",d->day);
  216.     if (appResources.opaqueDates)
  217.     XDrawImageString(display,d->pixmap,gc,x,y,text,strlen(text));
  218.     else
  219.     XDrawString(display,d->pixmap,gc,x,y,text,strlen(text));
  220.     XtSetArg(args[0],XtNbitmap,d->pixmap);
  221.     XtSetValues(d->button,args,ONE);
  222. }
  223.  
  224. /*
  225.  * selectDay() : Used to highlight a day (usually on startup or from
  226.  *    the "today" action) without the user having clicked on it.
  227.  */
  228. void
  229. selectDay(m,day,mon,year)
  230. MonthFormData *m;
  231. int day,mon,year;
  232. {
  233.     int which;
  234.  
  235.     which = firstDOW(mon,year)-appResources.dowOffset+day-2;
  236.     XawToggleSetCurrent(radioGroup,m->days[which]->button);
  237. }
  238.  
  239. /*
  240.  * dayButtonCallbackProc() : The callback when a day is clicked on (or
  241.  *    at least when the "notify" action is invoked). Pops up a new
  242.  *    dayForm is there isn't currently one, and sets it or the current
  243.  *    one to the new day's appoints.
  244.  */
  245. /*ARGSUSED*/
  246. CALLBACK_PROC(dayButtonCallbackProc)
  247. {
  248.     DayButtonData *d = (DayButtonData *)client_data;
  249.  
  250.     if (d->day == 0)
  251.     return;
  252.     if (currentDayFormData == NULL)
  253.     currentDayFormData = createPopupDayFormData();
  254.     else
  255.     checkpointAppoints(currentDayFormData);
  256.     currentDayFormData->buttonData = d;
  257.     if (XawToggleGetCurrent(radioGroup) != NULL)
  258.     setDayFormData(currentDayFormData,d->day,d->month,d->year);
  259.     else
  260.     clearDayFormData(currentDayFormData);
  261. }
  262.