home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume21 / notes / part02 / callbacks.c
Encoding:
C/C++ Source or Header  |  1993-10-20  |  22.0 KB  |  945 lines

  1. /*
  2.  * callbacks.c
  3.  *
  4.  * notes - by Bob Smith <bob@snuffy.dracut.ma.us>
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <malloc.h>
  9.  
  10. #include <X11/IntrinsicP.h>
  11. #include <X11/Intrinsic.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/Shell.h>
  14.  
  15. #include <X11/Xaw/Box.h>
  16. #include <X11/Xaw/Cardinals.h>
  17. #include <X11/Xaw/Command.h>
  18. #include <X11/Xaw/Label.h>
  19. #include <X11/Xaw/Form.h>
  20. #include <X11/Xaw/List.h>
  21. #include <X11/Xaw/AsciiText.h>
  22.  
  23. #include "notes.h"
  24.  
  25.  
  26. /*
  27.  * external variables
  28.  */
  29. /* in main.c */
  30. extern XtAppContext app_con;
  31. extern Widget toplevel;
  32. extern Widget **Notes;
  33. extern char **Titles;
  34.  
  35.  
  36. /*
  37.  * external functions
  38.  */
  39. void sensitize_start_button();
  40.  
  41.  
  42. /*
  43.  * forward declarations
  44.  */
  45. void show_list(), destroy_list();
  46.  
  47.  
  48. /*
  49.  * null ()
  50.  * just a stub to redirect some actions to when you don't really
  51.  * want anything to happen -- used by the input dialogs to keep
  52.  * carriage return, newline, and tab from doing anything
  53.  */
  54. void
  55. null(widget, client_data, call_data)
  56.      Widget widget;
  57.      XtPointer client_data, call_data;
  58. {
  59. }
  60.  
  61.  
  62. /*
  63.  * quit ()
  64.  * self explanatory
  65.  */
  66. void
  67. quit(widget, client_data, call_data)
  68.     Widget widget;
  69.     XtPointer client_data, call_data;
  70. {
  71.     XtDestroyApplicationContext(app_con);
  72.     exit(0);
  73. }
  74.  
  75.  
  76. /*
  77.  * desensitize_all ()
  78.  */
  79. static void
  80. desensitize_all(widget, client_data, call_data)
  81.     Widget widget;
  82.     XtPointer client_data, call_data;
  83. {
  84.     Widget w, button;
  85.     int i;
  86.     XWindowAttributes atribs;
  87.  
  88.     w = XtNameToWidget(toplevel, "*Notes");
  89.     if (w) {
  90.         button = XtNameToWidget(w, "*new_button");
  91.         XtVaSetValues(button, XtNsensitive, False, NULL);
  92.         button = XtNameToWidget(w, "*dismiss_button");
  93.         XtVaSetValues(button, XtNsensitive, False, NULL);
  94.         button = XtNameToWidget(w, "*save_button");
  95.         XtVaSetValues(button, XtNsensitive, False, NULL);
  96.         button = XtNameToWidget(w, "*quit_button");
  97.         XtVaSetValues(button, XtNsensitive, False, NULL);
  98.         button = XtNameToWidget(w, "*list");
  99.         XtVaSetValues(button, XtNsensitive, False, NULL);
  100.     }
  101.  
  102.     i = 0;
  103.     while (Notes[i]) {
  104.         XGetWindowAttributes(XtDisplay(*Notes[i]),
  105.             XtWindow(*Notes[i]), &atribs);
  106.         if (atribs.map_state != IsUnmapped) {
  107.             button = XtNameToWidget(*Notes[i], "*dismiss_button");
  108.             XtVaSetValues(button, XtNsensitive, False, NULL);
  109.             button = XtNameToWidget(*Notes[i], "*title_button");
  110.             XtVaSetValues(button, XtNsensitive, False, NULL);
  111.             button = XtNameToWidget(*Notes[i], "*clear_button");
  112.             XtVaSetValues(button, XtNsensitive, False, NULL);
  113.             button = XtNameToWidget(*Notes[i], "*delete_button");
  114.             XtVaSetValues(button, XtNsensitive, False, NULL);
  115.             button = XtNameToWidget(*Notes[i], "*select_button");
  116.             XtVaSetValues(button, XtNsensitive, False, NULL);
  117.         }
  118.         i++;
  119.     }
  120. }
  121.  
  122.  
  123. /*
  124.  * sensitize_all ()
  125.  */
  126. static void
  127. sensitize_all(widget, client_data, call_data)
  128.     Widget widget;
  129.     XtPointer client_data, call_data;
  130. {
  131.     Widget w, button;
  132.     int i;
  133.     XWindowAttributes atribs;
  134.  
  135.     w = XtNameToWidget(toplevel, "*Notes");
  136.     if (w) {
  137.         button = XtNameToWidget(w, "*new_button");
  138.         XtVaSetValues(button, XtNsensitive, True, NULL);
  139.         button = XtNameToWidget(w, "*dismiss_button");
  140.         XtVaSetValues(button, XtNsensitive, True, NULL);
  141.         button = XtNameToWidget(w, "*save_button");
  142.         XtVaSetValues(button, XtNsensitive, True, NULL);
  143.         button = XtNameToWidget(w, "*quit_button");
  144.         XtVaSetValues(button, XtNsensitive, True, NULL);
  145.         button = XtNameToWidget(w, "*list");
  146.         XtVaSetValues(button, XtNsensitive, True, NULL);
  147.     }
  148.  
  149.     i = 0;
  150.     while (Notes[i]) {
  151.         XGetWindowAttributes(XtDisplay(*Notes[i]),
  152.             XtWindow(*Notes[i]), &atribs);
  153.         if (atribs.map_state != IsUnmapped) {
  154.             button = XtNameToWidget(*Notes[i], "*dismiss_button");
  155.             XtVaSetValues(button, XtNsensitive, True, NULL);
  156.             button = XtNameToWidget(*Notes[i], "*title_button");
  157.             XtVaSetValues(button, XtNsensitive, True, NULL);
  158.             button = XtNameToWidget(*Notes[i], "*clear_button");
  159.             XtVaSetValues(button, XtNsensitive, True, NULL);
  160.             button = XtNameToWidget(*Notes[i], "*delete_button");
  161.             XtVaSetValues(button, XtNsensitive, True, NULL);
  162.             button = XtNameToWidget(*Notes[i], "*select_button");
  163.             XtVaSetValues(button, XtNsensitive, True, NULL);
  164.         }
  165.         i++;
  166.     }
  167. }
  168.  
  169.  
  170. /*
  171.  * destroy_widget ()
  172.  * used by a number of the popups to get rid of themselves --
  173.  * the assumption is that the button being pressed is 2 levels
  174.  * deep into the popup we want to destroy - hence the
  175.  * usage of XtGrandParent
  176.  */
  177. void
  178. destroy_widget(widget, client_data, call_data)
  179.     Widget widget;
  180.     XtPointer client_data, call_data;
  181. {
  182.     XtPopdown(XtGrandParent(widget));
  183.     XtDestroyWidget(XtGrandParent(widget));
  184. }
  185.  
  186.  
  187. /*
  188.  * save_callback ()
  189.  * just a callback to get to write_notefile
  190.  */
  191. static void
  192. save_callback(widget, client_data, call_data)
  193.     Widget widget;
  194.     XtPointer client_data, call_data;
  195. {
  196.     write_notefile();
  197. }
  198.  
  199.  
  200. /*
  201.  * confirm_clear_note ()
  202.  * clear the note widget if confirm is pressed in the dialog box
  203.  */
  204. static void
  205. confirm_clear_note(widget, client_data, call_data)
  206.     Widget widget;
  207.     XtPointer client_data, call_data;
  208. {
  209.     XtVaSetValues((Widget) client_data, XtNstring, NULL, NULL);
  210. }
  211.  
  212.  
  213. /*
  214.  * clear_note ()
  215.  * put up a dialog asking if you really want to clear the
  216.  * associated note
  217.  */
  218. static void
  219. clear_note(widget, client_data, call_data)
  220.     Widget widget;
  221.     XtPointer client_data, call_data;
  222. {
  223.     Widget note = (Widget) client_data;
  224.     Widget popup, form, prompt;
  225.     Widget confirm_button, filler_box, cancel_button;
  226.     Dimension note_w, popup_w;
  227.     Position x, y;
  228.  
  229.     popup = XtCreatePopupShell("Clear Note",
  230.         transientShellWidgetClass, toplevel,
  231.         NULL, ZERO);
  232.  
  233.     form = XtCreateManagedWidget("form",
  234.         formWidgetClass, popup,
  235.         NULL, ZERO);
  236.  
  237.     prompt = XtVaCreateManagedWidget("prompt",
  238.         labelWidgetClass, form,
  239.         XtNlabel, "Clear this note?",
  240.         XtNborderWidth, 0,
  241.         XtNtop, XawChainTop,
  242.         XtNbottom, XawChainTop,
  243.         XtNleft, XawChainLeft,
  244.         XtNright, XawChainLeft,
  245.         NULL);
  246.  
  247.     confirm_button = XtVaCreateManagedWidget("confirm_button",
  248.         commandWidgetClass, form,
  249.         XtNlabel, "Confirm",
  250.         XtNwidth, 64,
  251.         XtNfromVert, prompt,
  252.         XtNtop, XawChainBottom,
  253.         XtNbottom, XawChainBottom,
  254.         XtNleft, XawChainLeft,
  255.         XtNright, XawChainLeft,
  256.         NULL);
  257.         AddCallback(confirm_button, confirm_clear_note, note);
  258.         AddCallback(confirm_button, destroy_widget, NULL);
  259.         AddCallback(confirm_button, sensitize_all, NULL);
  260.  
  261.     filler_box = XtVaCreateManagedWidget("filler_box",
  262.         boxWidgetClass, form,
  263.         XtNwidth, 64,
  264.         XtNborderWidth, 0,
  265.         XtNfromHoriz, confirm_button,
  266.         XtNfromVert, prompt,
  267.         XtNtop, XawChainBottom,
  268.         XtNbottom, XawChainBottom,
  269.         XtNleft, XawChainLeft,
  270.         XtNright, XawChainRight,
  271.         NULL);
  272.  
  273.     cancel_button = XtVaCreateManagedWidget("cancel_button",
  274.         commandWidgetClass, form,
  275.         XtNlabel, "Cancel",
  276.         XtNwidth, 64,
  277.         XtNfromHoriz, filler_box,
  278.         XtNfromVert, prompt,
  279.         XtNtop, XawChainBottom,
  280.         XtNbottom, XawChainBottom,
  281.         XtNleft, XawChainRight,
  282.         XtNright, XawChainRight,
  283.         NULL);
  284.         AddCallback(cancel_button, destroy_widget, NULL);
  285.         AddCallback(cancel_button, sensitize_all, NULL);
  286.  
  287.     XtRealizeWidget(popup);
  288.  
  289.     XtVaGetValues(XtGrandParent(note), XtNwidth, ¬e_w, NULL);
  290.     XtVaGetValues(popup, XtNwidth, &popup_w, NULL);
  291.     XtTranslateCoords(XtGrandParent(note), (note_w - popup_w)/2, 0, &x, &y);
  292.     XtVaSetValues(popup, XtNx, x, XtNy, y, NULL);
  293.  
  294.     XtPopup(popup, XtGrabNone);
  295. }
  296.  
  297.  
  298. /*
  299.  * confirm_delete_note ()
  300.  * delete the associated note -- unalloc_note does the real work
  301.  */
  302. static void
  303. confirm_delete_note(widget, client_data, call_data)
  304.     Widget widget;
  305.     XtPointer client_data, call_data;
  306. {
  307.     XtPopdown((Widget) client_data);
  308.     XtDestroyWidget((Widget) client_data);
  309.  
  310.     if (widget = XtNameToWidget(toplevel, "*Notes")) {
  311.         XtPopdown(widget);
  312.         XtDestroyWidget(widget);
  313.     }
  314.  
  315.     unalloc_note((Widget) client_data);
  316.  
  317.     if (widget)
  318.         show_list();
  319.  
  320. }
  321.  
  322.  
  323. /*
  324.  * delete_note ()
  325.  * put up a dialog asking if you really want to delete the
  326.  * associated note
  327.  */
  328. static void
  329. delete_note(widget, client_data, call_data)
  330.     Widget widget;
  331.     XtPointer client_data, call_data;
  332. {
  333.     Widget note = (Widget) client_data;
  334.     Widget popup, form, prompt;
  335.     Widget confirm_button, filler_box, cancel_button;
  336.     Dimension note_w, popup_w;
  337.     Position x, y;
  338.  
  339.     popup = XtCreatePopupShell("Destroy Note",
  340.         transientShellWidgetClass, toplevel,
  341.         NULL, ZERO);
  342.  
  343.     form = XtCreateManagedWidget("form",
  344.         formWidgetClass, popup,
  345.         NULL, ZERO);
  346.  
  347.     prompt = XtVaCreateManagedWidget("prompt",
  348.         labelWidgetClass, form,
  349.         XtNlabel, "Destroy this note?",
  350.         XtNborderWidth, 0,
  351.         XtNtop, XawChainTop,
  352.         XtNbottom, XawChainTop,
  353.         XtNleft, XawChainLeft,
  354.         XtNright, XawChainLeft,
  355.         NULL);
  356.  
  357.     confirm_button = XtVaCreateManagedWidget("confirm_button",
  358.         commandWidgetClass, form,
  359.         XtNlabel, "Confirm",
  360.         XtNwidth, 64,
  361.         XtNfromVert, prompt,
  362.         XtNtop, XawChainBottom,
  363.         XtNbottom, XawChainBottom,
  364.         XtNleft, XawChainLeft,
  365.         XtNright, XawChainLeft,
  366.         NULL);
  367.         AddCallback(confirm_button, confirm_delete_note, note);
  368.         AddCallback(confirm_button, destroy_widget, NULL);
  369.         AddCallback(confirm_button, sensitize_all, NULL);
  370.  
  371.     filler_box = XtVaCreateManagedWidget("filler_box",
  372.         boxWidgetClass, form,
  373.         XtNwidth, 64,
  374.         XtNborderWidth, 0,
  375.         XtNfromHoriz, confirm_button,
  376.         XtNfromVert, prompt,
  377.         XtNtop, XawChainBottom,
  378.         XtNbottom, XawChainBottom,
  379.         XtNleft, XawChainLeft,
  380.         XtNright, XawChainRight,
  381.         NULL);
  382.  
  383.     cancel_button = XtVaCreateManagedWidget("cancel_button",
  384.         commandWidgetClass, form,
  385.         XtNlabel, "Cancel",
  386.         XtNwidth, 64,
  387.         XtNfromHoriz, filler_box,
  388.         XtNfromVert, prompt,
  389.         XtNtop, XawChainBottom,
  390.         XtNbottom, XawChainBottom,
  391.         XtNleft, XawChainRight,
  392.         XtNright, XawChainRight,
  393.         NULL);
  394.         AddCallback(cancel_button, destroy_widget, NULL);
  395.         AddCallback(cancel_button, sensitize_all, NULL);
  396.  
  397.     XtRealizeWidget(popup);
  398.  
  399.     XtVaGetValues(note, XtNwidth, ¬e_w, NULL);
  400.     XtVaGetValues(popup, XtNwidth, &popup_w, NULL);
  401.     XtTranslateCoords(note, (note_w - popup_w)/2, 0, &x, &y);
  402.     XtVaSetValues(popup, XtNx, x, XtNy, y, NULL);
  403.  
  404.     XtPopup(popup, XtGrabNone);
  405. }
  406.  
  407.  
  408. /*
  409.  * confirm_new_title ()
  410.  * change the title of associated note
  411.  */
  412. static void
  413. confirm_new_title(widget, client_data, call_data)
  414.     Widget widget;
  415.     XtPointer client_data, call_data;
  416. {
  417.     Widget w;
  418.     String str;
  419.     int i;
  420.  
  421.     XtVaGetValues(XtNameToWidget(XtGrandParent(widget), "*input"),
  422.         XtNstring, &str, NULL);
  423.  
  424.     if (strlen(str)) {
  425.         i = 0;
  426.         while (Notes[i]) {
  427.             if ((Widget) client_data == *Notes[i]) {
  428.                 Titles[i] = (char *) realloc(Titles[i], strlen(str)+1);
  429.                 if (!Titles[i])
  430.                     die("Realloc in confirm_new_title failed.");
  431.                 strcpy(Titles[i], str);
  432.                 XtVaSetValues((Widget) client_data, XtNtitle, str, NULL);
  433.                 (void) sort_titles(0);
  434.                 w = XtNameToWidget(toplevel, "*Notes*list");
  435.                 if (w)
  436.                     XawListChange(w, Titles, 0, 0, False);
  437.                 break;
  438.             }
  439.             i++;
  440.         }
  441.     }
  442. }
  443.  
  444.  
  445. /*
  446.  * new_title ()
  447.  * put up a prompt asking for a new title for the
  448.  * associated note
  449.  */
  450. static void
  451. new_title(widget, client_data, call_data)
  452.     Widget widget;
  453.     XtPointer client_data, call_data;
  454. {
  455.     Widget popup, form, prompt, input;
  456.     Widget confirm_button, filler_box, cancel_button;
  457.     Dimension client_w, popup_w;
  458.     Position x, y;
  459.     String str;
  460.  
  461.     popup = XtCreatePopupShell("Change Title",
  462.         transientShellWidgetClass, toplevel,
  463.         NULL, ZERO);
  464.  
  465.     form = XtCreateManagedWidget("form",
  466.         formWidgetClass, popup,
  467.         NULL, ZERO);
  468.  
  469.     prompt = XtVaCreateManagedWidget("prompt",
  470.         labelWidgetClass, form,
  471.         XtNlabel, "Enter new title",
  472.         XtNborderWidth, 0,
  473.         XtNtop, XawChainTop,
  474.         XtNbottom, XawChainTop,
  475.         XtNleft, XawChainLeft,
  476.         XtNright, XawChainLeft,
  477.         NULL);
  478.  
  479.     XtVaGetValues((Widget) client_data, XtNtitle, &str, NULL);
  480.  
  481.     input = XtVaCreateManagedWidget("input",
  482.         asciiTextWidgetClass, form,
  483.         XtNstring, str,
  484.         XtNwidth, 202,
  485.         XtNeditType, XawtextEdit,
  486.         XtNjustify, XtJustifyLeft,
  487.         XtNfromVert, prompt,
  488.         XtNtop, XawChainTop,
  489.         XtNbottom, XawChainTop,
  490.         XtNleft, XawChainLeft,
  491.         XtNright, XawChainRight,
  492.         NULL);
  493.  
  494.     confirm_button = XtVaCreateManagedWidget("confirm_button",
  495.         commandWidgetClass, form,
  496.         XtNlabel, "Confirm",
  497.         XtNwidth, 64,
  498.         XtNfromVert, input,
  499.         XtNtop, XawChainBottom,
  500.         XtNbottom, XawChainBottom,
  501.         XtNleft, XawChainLeft,
  502.         XtNright, XawChainLeft,
  503.         NULL);
  504.         AddCallback(confirm_button, confirm_new_title, client_data);
  505.         AddCallback(confirm_button, destroy_widget, NULL);
  506.         AddCallback(confirm_button, sensitize_all, NULL);
  507.  
  508.     /* filler_box's only purpose is to provide spacing... */
  509.     filler_box = XtVaCreateManagedWidget("filler_box",
  510.         boxWidgetClass, form,
  511.         XtNwidth, 64,
  512.         XtNborderWidth, 0,
  513.         XtNfromHoriz, confirm_button,
  514.         XtNfromVert, input,
  515.         XtNtop, XawChainBottom,
  516.         XtNbottom, XawChainBottom,
  517.         XtNleft, XawChainLeft,
  518.         XtNright, XawChainRight,
  519.         NULL);
  520.  
  521.     cancel_button = XtVaCreateManagedWidget("cancel_button",
  522.         commandWidgetClass, form,
  523.         XtNlabel, "Cancel",
  524.         XtNwidth, 64,
  525.         XtNfromHoriz, filler_box,
  526.         XtNfromVert, input,
  527.         XtNtop, XawChainBottom,
  528.         XtNbottom, XawChainBottom,
  529.         XtNleft, XawChainRight,
  530.         XtNright, XawChainRight,
  531.         NULL);
  532.         AddCallback(cancel_button, destroy_widget, NULL);
  533.         AddCallback(cancel_button, sensitize_all, NULL);
  534.  
  535.     XtRealizeWidget(popup);
  536.  
  537.     XtVaGetValues((Widget) client_data, XtNwidth, &client_w, NULL);
  538.     XtVaGetValues(popup, XtNwidth, &popup_w, NULL);
  539.     XtTranslateCoords((Widget) client_data, (client_w - popup_w)/2, 0, &x, &y);
  540.     XtVaSetValues(popup, XtNx, x, XtNy, y, NULL);
  541.  
  542.     XtPopup(popup, XtGrabNone);
  543. }
  544.  
  545.  
  546. /*
  547.  * confirm_new_note ()
  548.  * create a new note -- alloc_new_note does the bulk
  549.  * of the work
  550.  */
  551. static void
  552. confirm_new_note(widget, client_data, call_data)
  553.     Widget widget;
  554.     XtPointer client_data, call_data;
  555. {
  556.     String str;
  557.     int i;
  558.  
  559.     XtVaGetValues((Widget) client_data, XtNstring, &str, NULL);
  560.  
  561.     if (strlen(str)) {
  562.         i = alloc_new_note(str);
  563.         destroy_list();
  564.         show_list();
  565.         XtPopup(*Notes[i], XtGrabNone);
  566.     }
  567. }
  568.  
  569.  
  570. /*
  571.  * new_note ()
  572.  * put up a dialog asking for the title of a new note
  573.  */
  574. static void
  575. new_note(widget, client_data, call_data)
  576.     Widget widget;
  577.     XtPointer client_data, call_data;
  578. {
  579.     Widget popup, form, prompt, input;
  580.     Widget confirm_button, filler_box, cancel_button;
  581.     Dimension top_w, popup_w;
  582.     Position x, y;
  583.  
  584.     popup = XtCreatePopupShell("New Note",
  585.         transientShellWidgetClass, toplevel,
  586.         NULL, ZERO);
  587.  
  588.     form = XtCreateManagedWidget("form",
  589.         formWidgetClass, popup,
  590.         NULL, ZERO);
  591.  
  592.     prompt = XtVaCreateManagedWidget("prompt",
  593.         labelWidgetClass, form,
  594.         XtNlabel, "Enter title of new note",
  595.         XtNborderWidth, 0,
  596.         XtNtop, XawChainTop,
  597.         XtNbottom, XawChainTop,
  598.         XtNleft, XawChainLeft,
  599.         XtNright, XawChainLeft,
  600.         NULL);
  601.  
  602.     input = XtVaCreateManagedWidget("input",
  603.         asciiTextWidgetClass, form,
  604.         XtNwidth, 202,
  605.         XtNeditType, XawtextEdit,
  606.         XtNjustify, XtJustifyLeft,
  607.         XtNfromVert, prompt,
  608.         XtNtop, XawChainTop,
  609.         XtNbottom, XawChainTop,
  610.         XtNleft, XawChainLeft,
  611.         XtNright, XawChainRight,
  612.         NULL);
  613.  
  614.     confirm_button = XtVaCreateManagedWidget("confirm_button",
  615.         commandWidgetClass, form,
  616.         XtNlabel, "Confirm",
  617.         XtNwidth, 64,
  618.         XtNfromVert, input,
  619.         XtNtop, XawChainBottom,
  620.         XtNbottom, XawChainBottom,
  621.         XtNleft, XawChainLeft,
  622.         XtNright, XawChainLeft,
  623.         NULL);
  624.         AddCallback(confirm_button, confirm_new_note, input);
  625.         AddCallback(confirm_button, destroy_widget, NULL);
  626.         AddCallback(confirm_button, sensitize_all, NULL);
  627.  
  628.     /* filler_box's only purpose is to provide spacing... */
  629.     filler_box = XtVaCreateManagedWidget("filler_box",
  630.         boxWidgetClass, form,
  631.         XtNwidth, 64,
  632.         XtNborderWidth, 0,
  633.         XtNfromHoriz, confirm_button,
  634.         XtNfromVert, input,
  635.         XtNtop, XawChainBottom,
  636.         XtNbottom, XawChainBottom,
  637.         XtNleft, XawChainLeft,
  638.         XtNright, XawChainRight,
  639.         NULL);
  640.  
  641.     cancel_button = XtVaCreateManagedWidget("cancel_button",
  642.         commandWidgetClass, form,
  643.         XtNlabel, "Cancel",
  644.         XtNwidth, 64,
  645.         XtNfromHoriz, filler_box,
  646.         XtNfromVert, input,
  647.         XtNtop, XawChainBottom,
  648.         XtNbottom, XawChainBottom,
  649.         XtNleft, XawChainRight,
  650.         XtNright, XawChainRight,
  651.         NULL);
  652.         AddCallback(cancel_button, destroy_widget, NULL);
  653.         AddCallback(cancel_button, sensitize_all, NULL);
  654.  
  655.     XtRealizeWidget(popup);
  656.  
  657.     XtVaGetValues(toplevel, XtNwidth, &top_w, NULL);
  658.     XtVaGetValues(popup, XtNwidth, &popup_w, NULL);
  659.     XtTranslateCoords(toplevel, (top_w - popup_w)/2, 0, &x, &y);
  660.     XtVaSetValues(popup, XtNx, x, XtNy, y, NULL);
  661.  
  662.     XtPopup(popup, XtGrabNone);
  663. }
  664.  
  665.  
  666. /*
  667.  * dismiss_show_note ()
  668.  * unmap a note from the display -- note it does not destroy
  669.  * the note widget
  670.  */
  671. static void 
  672. dismiss_show_note(widget, client_data, call_data)
  673.     Widget widget;
  674.     XtPointer client_data, call_data;
  675. {
  676.     XtPopdown(XtGrandParent(widget));
  677.     XtUnmapWidget(XtGrandParent(widget));
  678. }
  679.  
  680.  
  681. /*
  682.  * show_note ()
  683.  * map a note to the display
  684.  */
  685. static void
  686. show_note(widget, client_data, call_data)
  687.      Widget widget;
  688.      XtPointer client_data, call_data;
  689. {
  690.     XawListReturnStruct *item = (XawListReturnStruct*) call_data;
  691.     Widget w;
  692.     XWindowAttributes atribs;
  693.  
  694.     w = *Notes[item->list_index];
  695.     XGetWindowAttributes(XtDisplay(w), XtWindow(w), &atribs);
  696.  
  697.     if (atribs.map_state == IsUnmapped)
  698.         XtPopup(w, XtGrabNone);
  699.     else
  700.         XRaiseWindow(XtDisplay(w), XtWindow(w));
  701. }
  702.  
  703.  
  704. /*
  705.  * select_note ()
  706.  */
  707. static void
  708. select_note(widget, client_data, call_data)
  709.     Widget widget;
  710.     XtPointer client_data, call_data;
  711. {
  712.     Widget w = (Widget) client_data;
  713.     Display *d;
  714.     String str;
  715.  
  716.     d = XtDisplay(XtGrandParent(widget));
  717.     XtVaGetValues(w, XtNstring, &str, NULL);
  718.     if (strlen(str)) {
  719.         XawTextSetSelection(w, 0, strlen(str));
  720.         XStoreBytes(d, str, strlen(str));
  721.     }
  722. }
  723.  
  724.  
  725. /*
  726.  * make_note_widget ()
  727.  * dynamically allocate a widget, create a new note, and
  728.  * return a pointer to the widget -- remember that it's a
  729.  * pointer, not a widget itself
  730.  */
  731. Widget *
  732. make_note_widget(title, x, y, w, h, text)
  733.     char *title, *text;
  734.     int x, y, w, h;
  735. {
  736.     Widget *popup, form, note;
  737.     Widget dismiss_button, title_button, clear_button, delete_button;
  738.     Widget select_button;
  739.  
  740.     popup = (Widget *) malloc(sizeof(Widget));
  741.     if (!popup)
  742.         die("Malloc in make_note_widget failed.");
  743.  
  744.     *popup = XtVaCreatePopupShell(title,
  745.         transientShellWidgetClass, toplevel,
  746.         XtNx, x, XtNy, y, NULL);
  747.  
  748.     form = XtCreateManagedWidget("form",
  749.         formWidgetClass, *popup,
  750.         NULL, ZERO);
  751.  
  752.     dismiss_button = XtVaCreateManagedWidget("dismiss_button",
  753.         commandWidgetClass, form,
  754.         XtNlabel, "Dismiss",
  755.         XtNwidth, 64,
  756.         XtNtop, XawChainTop,
  757.         XtNbottom, XawChainTop,
  758.         XtNleft, XawChainLeft,
  759.         XtNright, XawChainLeft,
  760.         NULL);
  761.         AddCallback(dismiss_button, dismiss_show_note, NULL);
  762.  
  763.     note = XtVaCreateManagedWidget("note",
  764.         asciiTextWidgetClass, form,
  765.         XtNfromVert, dismiss_button,
  766.         XtNstring, text,
  767.         XtNwidth, w,
  768.         XtNheight, h,
  769.         XtNscrollVertical, XawtextScrollAlways,
  770.         XtNeditType, XawtextEdit,
  771.         XtNwrap, XawtextWrapLine,
  772.         XtNtop, XawChainTop,
  773.         XtNbottom, XawChainBottom,
  774.         XtNleft, XawChainLeft,
  775.         XtNright, XawChainRight,
  776.         NULL);
  777.  
  778.     title_button = XtVaCreateManagedWidget("title_button",
  779.         commandWidgetClass, form,
  780.         XtNfromHoriz, dismiss_button,
  781.         XtNlabel, "Title",
  782.         XtNwidth, 64,
  783.         XtNtop, XawChainTop,
  784.         XtNbottom, XawChainTop,
  785.         XtNleft, XawChainLeft,
  786.         XtNright, XawChainLeft,
  787.         NULL);
  788.         AddCallback(title_button, desensitize_all, NULL);
  789.         AddCallback(title_button, new_title, *popup);
  790.  
  791.     clear_button = XtVaCreateManagedWidget("clear_button",
  792.         commandWidgetClass, form,
  793.         XtNfromHoriz, title_button,
  794.         XtNlabel, "Clear",
  795.         XtNwidth, 64,
  796.         XtNtop, XawChainTop,
  797.         XtNbottom, XawChainTop,
  798.         XtNleft, XawChainLeft,
  799.         XtNright, XawChainLeft,
  800.         NULL);
  801.         AddCallback(clear_button, desensitize_all, NULL);
  802.         AddCallback(clear_button, clear_note, note);
  803.  
  804.     delete_button = XtVaCreateManagedWidget("delete_button",
  805.         commandWidgetClass, form,
  806.         XtNfromHoriz, clear_button,
  807.         XtNlabel, "Destroy",
  808.         XtNwidth, 64,
  809.         XtNtop, XawChainTop,
  810.         XtNbottom, XawChainTop,
  811.         XtNleft, XawChainLeft,
  812.         XtNright, XawChainLeft,
  813.         NULL);
  814.         AddCallback(delete_button, desensitize_all, NULL);
  815.         AddCallback(delete_button, delete_note, *popup);
  816.  
  817.     select_button = XtVaCreateManagedWidget("select_button",
  818.         commandWidgetClass, form,
  819.         XtNfromHoriz, delete_button,
  820.         XtNlabel, "Select",
  821.         XtNwidth, 64,
  822.         XtNtop, XawChainTop,
  823.         XtNbottom, XawChainTop,
  824.         XtNleft, XawChainLeft,
  825.         XtNright, XawChainLeft,
  826.         NULL);
  827.         AddCallback(select_button, select_note, note);
  828.  
  829.     XtRealizeWidget(*popup);
  830.  
  831.     return popup;
  832. }
  833.  
  834.  
  835. /*
  836.  * destroy_list ()
  837.  * this destroy_list is called by confirm_new_note
  838.  */
  839. void
  840. destroy_list()
  841. {
  842.     Widget w;
  843.  
  844.     w = XtNameToWidget(toplevel, "*Notes");
  845.     if (w) {
  846.         XtPopdown(w);
  847.         XtDestroyWidget(w);
  848.     }
  849. }
  850.  
  851.  
  852. /*
  853.  * show_list ()
  854.  * desensitize the toplevel startup button and put up
  855.  * the list of notes
  856.  */
  857. void
  858. show_list(widget, client_data, call_data)
  859.     Widget widget;
  860.     XtPointer client_data, call_data;
  861. {
  862.     Widget popup, form, list;
  863.     Widget dismiss_button, new_button, save_button, quit_button;
  864.     Dimension top_w, popup_w;
  865.     Position x, y;
  866.  
  867.     popup = XtCreatePopupShell("Notes",
  868.         transientShellWidgetClass, toplevel,
  869.         NULL, ZERO);
  870.  
  871.     form = XtCreateManagedWidget("form",
  872.         formWidgetClass, popup,
  873.         NULL, ZERO);
  874.  
  875.     new_button = XtVaCreateManagedWidget("new_button",
  876.         commandWidgetClass, form,
  877.         XtNwidth, 64,
  878.         XtNlabel, "New",
  879.         XtNtop, XawChainTop,
  880.         XtNbottom, XawChainTop,
  881.         XtNleft, XawChainLeft,
  882.         XtNright, XawChainLeft,
  883.         NULL);
  884.         AddCallback(new_button, desensitize_all, NULL);
  885.         AddCallback(new_button, new_note, NULL);
  886.  
  887.     dismiss_button = XtVaCreateManagedWidget("dismiss_button",
  888.         commandWidgetClass, form,
  889.         XtNfromHoriz, new_button,
  890.         XtNwidth, 64,
  891.         XtNlabel, "Dismiss",
  892.         XtNtop, XawChainTop,
  893.         XtNbottom, XawChainTop,
  894.         XtNleft, XawChainLeft,
  895.         XtNright, XawChainRight,
  896.         NULL);
  897.         AddCallback(dismiss_button, destroy_widget, NULL);
  898.         AddCallback(dismiss_button, sensitize_start_button, NULL);
  899.  
  900.     save_button = XtVaCreateManagedWidget("save_button",
  901.         commandWidgetClass, form,
  902.         XtNfromHoriz, dismiss_button,
  903.         XtNwidth, 64,
  904.         XtNlabel, "Save",
  905.         XtNtop, XawChainTop,
  906.         XtNbottom, XawChainTop,
  907.         XtNleft, XawChainRight,
  908.         XtNright, XawChainRight,
  909.         NULL);
  910.         AddCallback(save_button, save_callback, NULL);
  911.  
  912.     list = XtVaCreateManagedWidget("list",
  913.         listWidgetClass, form,
  914.         XtNfromVert, dismiss_button,
  915.         XtNwidth, 3*64+2*4+2*2,
  916.         XtNlist, Titles,
  917.         XtNtop, XawChainTop,
  918.         XtNbottom, XawChainBottom,
  919.         XtNleft, XawChainLeft,
  920.         XtNright, XawChainRight,
  921.         NULL);
  922.         AddCallback(list, show_note, NULL);
  923.  
  924.     quit_button = XtVaCreateManagedWidget("quit_button",
  925.         commandWidgetClass, form,
  926.         XtNfromVert, list,
  927.         XtNwidth, 3*64+2*4+2*2,
  928.         XtNlabel, "Quit",
  929.         XtNtop, XawChainBottom,
  930.         XtNbottom, XawChainBottom,
  931.         XtNleft, XawChainLeft,
  932.         XtNright, XawChainRight,
  933.         NULL);
  934.         AddCallback(quit_button, quit, NULL);
  935.  
  936.     XtRealizeWidget(popup);
  937.  
  938.     XtVaGetValues(toplevel, XtNwidth, &top_w, NULL);
  939.     XtVaGetValues(popup, XtNwidth, &popup_w, NULL);
  940.     XtTranslateCoords(toplevel, (top_w - popup_w)/2, 0, &x, &y);
  941.     XtVaSetValues(popup, XtNx, x, XtNy, y, NULL);
  942.  
  943.     XtPopup(popup, XtGrabNone);
  944. }
  945.