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

  1. /*
  2.  *    edit.c : Edit an appointment in detail
  3.  *
  4.  *    George Ferguson, ferguson@cs.rochester.edu, 27 Feb 1991.
  5.  *
  6.  *    $Id: edit.c,v 2.1 91/02/28 11:21:20 ferguson Exp $
  7.  */
  8. #include <stdio.h>
  9. #include <X11/Intrinsic.h>
  10. #include <X11/Shell.h>
  11. #include <X11/StringDefs.h>
  12. #include <X11/Xaw/Form.h>
  13. #include <X11/Xaw/Command.h>
  14. #include <X11/Xaw/Label.h>
  15. #include <X11/Xaw/AsciiText.h>
  16. #include <X11/Xaw/Cardinals.h>
  17. #include "xkal.h"
  18. #include "month.h"
  19. #include "day.h"
  20. #include "db.h"
  21. #include "util.h"
  22. #include "date-strings.h"
  23. #include "app-resources.h"            /* for notesLabel */
  24. #ifdef USE_ALERT
  25. #include "alert.h"
  26. #endif
  27.  
  28. /*
  29.  * Functions declared in this file
  30.  */
  31. void editAppoint();
  32. void focusNextEditItem(),focusPrevEditItem();
  33.  
  34. static void initEditShell(),setEditItems(),setText();
  35. static void applyProc(),revertProc(),deleteProc(),dismissProc();
  36. static Boolean getCurrentEditMsg();
  37.  
  38. /*
  39.  * Data declared in this file
  40.  */
  41. static Widget editShell,editForm;
  42. static Widget applyButton,revertButton,deleteButton,dismissButton;
  43. static Widget dowLabel,dowText,dayLabel,dayText,monLabel,monText;
  44. static Widget yearLabel,yearText,timeLabel,timeText;
  45. static Widget textLabel,textText,levelLabel,levelText;
  46. static DayFormData *currentEditFormData;
  47. static Msgrec currentEditMsg;
  48.  
  49. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  50.                     Widget w; \
  51.                     XEvent *event; \
  52.                     String *params; \
  53.                     Cardinal *num_params;
  54.  
  55. #define CALLBACK_PROC(NAME)    static void NAME(w,client_data,call_data) \
  56.                     Widget w; \
  57.                     caddr_t client_data,call_data;
  58.  
  59. /*    -    -    -    -    -    -    -    -    */
  60. /* External interface (action) procedure */
  61. /*
  62.  * editAppoint() : Pops up (possibly creating) the appointment editor,
  63.  *    and fills it with the information from the appointment corresponding
  64.  *    to the text widget taht this action is invoked from.
  65.  */
  66. /*ARGSUSED*/
  67. ACTION_PROC(editAppoint)
  68. {
  69.     Arg args[1];
  70.     int i,type;
  71.  
  72.     if (currentDayFormData == NULL)
  73.     return;
  74.     for (i=0; i < numDisplayedAppoints; i++)
  75.     if (w == currentDayFormData->items[i]->time ||
  76.         w == currentDayFormData->items[i]->text)
  77.         break;
  78.     if (i == numDisplayedAppoints)
  79.     return;
  80.     if (w == currentDayFormData->items[i]->time)
  81.     w = currentDayFormData->items[i]->text;
  82.     XtSetArg(args[0],XtNeditType,&type);
  83.     XtGetValues(w,args,ONE);
  84.     if (type != XawtextEdit) {
  85.     XBell(display,0);
  86.     return;
  87.     }
  88.     if (editShell == NULL)
  89.     initEditShell();
  90.     checkpointAppoints(currentDayFormData);
  91.     currentEditFormData = currentDayFormData;
  92.     currentEditFormData->editing = True;
  93.     if (currentEditFormData->msg[i] == NULL) {
  94.     currentEditMsg.dow = 0;
  95.     currentEditMsg.day = currentEditFormData->day;
  96.     currentEditMsg.month = currentEditFormData->month;
  97.     currentEditMsg.year = currentEditFormData->year;
  98.     postotime(i,&(currentEditMsg.hour),&(currentEditMsg.mins));
  99.     currentEditMsg.text = "";
  100.     currentEditMsg.level = appResources.defaultLevel;
  101.     } else {
  102.     currentEditMsg.dow = currentEditFormData->msg[i]->dow;
  103.     currentEditMsg.day = currentEditFormData->msg[i]->day;
  104.     currentEditMsg.month = currentEditFormData->msg[i]->month;
  105.     currentEditMsg.year = currentEditFormData->msg[i]->year;
  106.     currentEditMsg.hour = currentEditFormData->msg[i]->hour;
  107.     currentEditMsg.mins = currentEditFormData->msg[i]->mins;
  108.     currentEditMsg.text = XtNewString(currentEditFormData->msg[i]->text);
  109.     currentEditMsg.level = currentEditFormData->msg[i]->level;
  110.     }
  111.     setEditItems(¤tEditMsg);
  112.     XtPopup(editShell,XtGrabNone);
  113. }
  114.  
  115. /*    -    -    -    -    -    -    -    -    */
  116. /* Initialization procedures */
  117.  
  118. /*
  119.  * initEditShell() : Create the popup appointment editor.
  120.  */
  121. static void
  122. initEditShell()
  123. {
  124.     editShell = XtCreatePopupShell("editShell",topLevelShellWidgetClass,
  125.                             toplevel,NULL,ZERO);
  126.     editForm = XtCreateManagedWidget("editForm",formWidgetClass,
  127.                             editShell,NULL,ZERO);
  128.     applyButton = XtCreateManagedWidget("applyButton",commandWidgetClass,
  129.                             editForm,NULL,ZERO);
  130.     XtAddCallback(applyButton,"callback",applyProc,NULL);
  131.     revertButton = XtCreateManagedWidget("revertButton",commandWidgetClass,
  132.                             editForm,NULL,ZERO);
  133.     XtAddCallback(revertButton,"callback",revertProc,NULL);
  134.     deleteButton = XtCreateManagedWidget("deleteButton",commandWidgetClass,
  135.                             editForm,NULL,ZERO);
  136.     XtAddCallback(deleteButton,"callback",deleteProc,NULL);
  137.     dismissButton = XtCreateManagedWidget("dismissButton",
  138.                     commandWidgetClass,editForm,NULL,ZERO);
  139.     XtAddCallback(dismissButton,"callback",dismissProc,NULL);
  140.     dowLabel = XtCreateManagedWidget("dowLabel",labelWidgetClass,
  141.                             editForm,NULL,ZERO);
  142.     dowText = XtCreateManagedWidget("dowText",asciiTextWidgetClass,
  143.                             editForm,NULL,ZERO);
  144.     dayLabel = XtCreateManagedWidget("dayLabel",labelWidgetClass,
  145.                             editForm,NULL,ZERO);
  146.     dayText = XtCreateManagedWidget("dayText",asciiTextWidgetClass,
  147.                             editForm,NULL,ZERO);
  148.     monLabel = XtCreateManagedWidget("monLabel",labelWidgetClass,
  149.                             editForm,NULL,ZERO);
  150.     monText = XtCreateManagedWidget("monText",asciiTextWidgetClass,
  151.                             editForm,NULL,ZERO);
  152.     yearLabel = XtCreateManagedWidget("yearLabel",labelWidgetClass,
  153.                             editForm,NULL,ZERO);
  154.     yearText = XtCreateManagedWidget("yearText",asciiTextWidgetClass,
  155.                             editForm,NULL,ZERO);
  156.     timeLabel = XtCreateManagedWidget("timeLabel",labelWidgetClass,
  157.                             editForm,NULL,ZERO);
  158.     timeText = XtCreateManagedWidget("timeText",asciiTextWidgetClass,
  159.                             editForm,NULL,ZERO);
  160.     textLabel = XtCreateManagedWidget("textLabel",labelWidgetClass,
  161.                             editForm,NULL,ZERO);
  162.     textText = XtCreateManagedWidget("textText",asciiTextWidgetClass,
  163.                             editForm,NULL,ZERO);
  164.     levelLabel = XtCreateManagedWidget("levelLabel",labelWidgetClass,
  165.                             editForm,NULL,ZERO);
  166.     levelText = XtCreateManagedWidget("levelText",asciiTextWidgetClass,
  167.                             editForm,NULL,ZERO);
  168.     XtRealizeWidget(editShell);        /* so we can set titlebar */
  169. }
  170.  
  171. /*
  172.  * setEditItems() : Sets the values in the appointment editor text items
  173.  *    to values ffrom the given appoint.
  174.  */
  175. static void
  176. setEditItems(msg)
  177. Msgrec *msg;
  178. {
  179.     char buf[8];
  180.  
  181.     if (msg->dow == 0)
  182.     setText(dowText,"");
  183.     else
  184.     setText(dowText,shortDowStr[msg->dow-1]);
  185.     if (msg->day == 0)
  186.     setText(dayText,"");
  187.     else {
  188.     sprintf(buf,"%d",msg->day);
  189.     setText(dayText,buf);
  190.     }
  191.     if (msg->month == 0)
  192.     setText(monText,"");
  193.     else
  194.     setText(monText,shortMonthStr[msg->month-1]);
  195.     if (msg->year == 0)
  196.     setText(yearText,"");
  197.     else {
  198.     sprintf(buf,"%d",msg->year);
  199.     setText(yearText,buf);
  200.     }
  201.     if (msg->hour == -1)
  202.     setText(timeText,appResources.notesLabel);
  203.     else
  204.     setText(timeText,timetostr(msg->hour*60+msg->mins));
  205.     setText(textText,msg->text);
  206.     sprintf(buf,"%d",msg->level);
  207.     setText(levelText,buf);
  208. }
  209.  
  210. /*
  211.  * setText() : Set the given text item's value to the given string.
  212.  */
  213. static void
  214. setText(item,text)
  215. Widget item;
  216. char *text;
  217. {
  218.     Arg args[1];
  219.  
  220.     XtSetArg(args[0],XtNstring,text);
  221.     XtSetValues(item,args,ONE);
  222. }
  223.  
  224. /*    -    -    -    -    -    -    -    -    */
  225. /* Callback procedures */
  226.  
  227. /*
  228.  * applyProc() : Callback for apply button - Add an entry with the fields
  229.  *    given in the editor panel to the DB. Removes any other that matches
  230.  *    exactly (including the text). Resets the month display to reflect
  231.  *    the change (and the day within the month).
  232.  */
  233. /*ARGSUSED*/
  234. CALLBACK_PROC(applyProc)
  235. {
  236.     Msgrec msg;
  237.  
  238.     if (!getCurrentEditMsg(&msg))
  239.     return;
  240.     deleteEntry(msg.dow,msg.year,msg.month,msg.day,msg.hour,msg.mins,msg.text);
  241.     addEntry(msg.dow,msg.year,msg.month,msg.day,msg.hour,msg.mins,msg.text,
  242.                                 False,msg.level);
  243.     appointsChanged = True;
  244.     setDayFormData(currentEditFormData,currentEditFormData->day,
  245.                     currentEditFormData->month,
  246.                     currentEditFormData->year);
  247.     shadeButton(currentEditFormData->buttonData,(GC)NULL,0,0);
  248. }
  249.  
  250. /*
  251.  * revertProc() : Callback for revert button - Reset the text strings to
  252.  *    the way they were when editAppoint was called last. Does NOT undo
  253.  *    any changed that were made.
  254.  */
  255. /*ARGSUSED*/
  256. CALLBACK_PROC(revertProc)
  257. {
  258.     setEditItems(¤tEditMsg);
  259. }
  260.  
  261. /*
  262.  * deleteProc() : Callback for delete button - Removes any appoint that
  263.  *    exactly matches the items on the editor panel. Updates the month
  264.  *    display to reflect the change.
  265.  */
  266. /*ARGSUSED*/
  267. CALLBACK_PROC(deleteProc)
  268. {
  269.     Msgrec msg;
  270.  
  271.     checkpointAppoints(currentEditFormData);
  272.     if (!getCurrentEditMsg(&msg))
  273.     return;
  274.     if (deleteEntry(msg.dow,msg.year,msg.month,msg.day,msg.hour,msg.mins,
  275.                                 msg.text)) {
  276.     appointsChanged = True;
  277.     setDayFormData(currentEditFormData,currentEditFormData->day,
  278.                     currentEditFormData->month,
  279.                     currentEditFormData->year);
  280.     shadeButton(currentEditFormData->buttonData,(GC)NULL,0,0);
  281.     }
  282. }
  283.  
  284. /*
  285.  * dismissProc() : Callback for dismiss button - Pop down the editor.
  286.  */
  287. /*ARGSUSED*/
  288. CALLBACK_PROC(dismissProc)
  289. {
  290.     currentEditFormData->editing = False;
  291.     XtFree(currentEditMsg.text);
  292.     XtPopdown(editShell);
  293. }
  294.  
  295. /*    -    -    -    -    -    -    -    -    */
  296. /* Action procedures */
  297. /*
  298.  * focusNextEditItem() : Move input focus to next editor item, wrapping
  299.  *    at bottom.
  300.  */
  301. /*ARGSUSED*/
  302. ACTION_PROC(focusNextEditItem)
  303. {
  304.     if (w == dowText)
  305.     w = dayText;
  306.     else if (w == dayText)
  307.     w = monText;
  308.     else if (w == monText)
  309.     w = yearText;
  310.     else if (w == yearText)
  311.     w = timeText;
  312.     else if (w == timeText)
  313.     w = textText;
  314.     else if (w == textText)
  315.     w = levelText;
  316.     else if (w == levelText)
  317.     w = dowText;
  318.     else
  319.     return;
  320.     XSetInputFocus(display,XtWindow(w),RevertToParent,CurrentTime);
  321. }
  322.  
  323. /*
  324.  * focusPrevEditItem() : Move input focus to previous editor item, wrapping
  325.  *    at top.
  326.  */
  327. /*ARGSUSED*/
  328. ACTION_PROC(focusPrevEditItem)
  329. {
  330.     if (w == dowText)
  331.     w = levelText;
  332.     else if (w == dayText)
  333.     w = dowText;
  334.     else if (w == monText)
  335.     w = dayText;
  336.     else if (w == yearText)
  337.     w = monText;
  338.     else if (w == timeText)
  339.     w = yearText;
  340.     else if (w == textText)
  341.     w = timeText;
  342.     else if (w == levelText)
  343.     w = textText;
  344.     else
  345.     return;
  346.     XSetInputFocus(display,XtWindow(w),RevertToParent,CurrentTime);
  347. }
  348.  
  349. /*    -    -    -    -    -    -    -    -    */
  350. /*
  351.  * getCurrentEditMsg() : Fills in the given Msgrec with values taken from
  352.  *    the editor text items.
  353.  */
  354. static Boolean
  355. getCurrentEditMsg(msg)
  356. Msgrec *msg;
  357. {
  358.     Arg args[1];
  359.     char *s;
  360.     int i,t;
  361.  
  362.     XtSetArg(args[0],XtNstring,&s);
  363.     XtGetValues(dowText,args,ONE);
  364.     if (!*s)
  365.     msg->dow = 0;
  366.     else {
  367.     for (i=0; i < 7; i++)
  368.         if (strcasecmp(s,longDowStr[i]) == 0 ||
  369.         strcasecmp(s,shortDowStr[i]) == 0)
  370.         break;
  371.     if (i == 7) {
  372. #ifdef USE_ALERT
  373.         alert("Error: bad dow string: \"%s\".",s);
  374. #else
  375.         fprintf(stderr,"\007%s: bad dow string: \"%s\"\n",program,s);
  376. #endif
  377.         return(False);
  378.     } else
  379.         msg->dow = i+1;
  380.     }
  381.     XtGetValues(dayText,args,ONE);
  382.     msg->day = atoi(s);            /* no checking! */
  383.     XtGetValues(monText,args,ONE);
  384.     if (!*s)
  385.     msg->month = 0;
  386.     else {
  387.     for (i=0; i < 7; i++)
  388.         if (strcasecmp(s,longMonthStr[i]) == 0 ||
  389.         strcasecmp(s,shortMonthStr[i]) == 0)
  390.         break;
  391.     if (i == 7) {
  392. #ifdef USE_ALERT
  393.         alert("Error: bad month string: \"%s\".",s);
  394. #else
  395.         fprintf(stderr,"\007%s: bad month string: \"%s\"\n",program,s);
  396. #endif
  397.         return(False);
  398.     } else
  399.         msg->month = i+1;
  400.     }
  401.     XtGetValues(yearText,args,ONE);
  402.     msg->year = atoi(s);            /* no checking! */
  403.     XtGetValues(timeText,args,ONE);
  404.     if (!*s || strcasecmp(s,appResources.notesLabel) == 0)
  405.     msg->hour = msg->mins = -1;
  406.     else {
  407.     t = strtotime(s);
  408.     msg->hour = t / 60;
  409.     msg->mins = t % 60;
  410.     }
  411.     XtGetValues(textText,args,ONE);
  412.     msg->text = s;
  413.     XtGetValues(levelText,args,ONE);
  414.     msg->level = atoi(s);            /* no checking! */
  415.     return(True);
  416. }
  417.