home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / crit / dlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-23  |  6.2 KB  |  293 lines  |  [TEXT/????]

  1. /*
  2.  * dlog.c - handle dialogs for the critic
  3.  *
  4.  */
  5.  
  6. #include <memory.h>
  7. #include <quickdraw.h>
  8. #include <font.h>
  9. #include <window.h>
  10. #include <osutil.h>
  11. #include <menu.h>
  12. #include <event.h>
  13. #include <textedit.h>
  14. #include <dialog.h>
  15. #include <desk.h>
  16. #include <control.h>
  17. #include <toolutil.h>
  18. #define watchCursor 4    /* Should be in TOOLUTIL.H but isn't.  */
  19. #include <resource.h>
  20. #include <segment.h>
  21. #include <packages.h>
  22. #include <stdio.h>
  23.  
  24. #include "critic.h"
  25. #include "speech.h"
  26.  
  27. /*
  28.  * Voice setup dialog stuff.
  29.  * VP_DLOG is the resource ID of the setup dialog;
  30.  * all other VP_ values are item IDs within that dialog.
  31.  * Ranges come next, followed by default values.
  32.  */
  33.  
  34. #define VP_DLOG    258        /* resource ID of our setup dialog    */
  35.  
  36. #define VP_OK    1        /* OK button                */
  37. #define VP_CAN    2        /* CANCEL button            */
  38. #define VP_MALE    5        /* male voice                */
  39. #define VP_FEM    6        /* female voice                */
  40. #define VP_NAT    7        /* "natural" inflection            */
  41. #define VP_MONO    8        /* monotonic ("robotic") inflection    */
  42. #define VP_PITCH 11        /* base pitch field            */
  43. #define VP_RATE    14        /* speaking rate field            */
  44.  
  45. #define RATMIN    85        /* mininum valid speaking rate (wpm)    */
  46. #define RATMAX    425        /* maximum ""                */
  47. #define RATWID    3        /* max width of the rate field        */
  48.  
  49. #define PITMIN    65        /* minimum valid base pitch (hz)    */
  50. #define PITMAX    500        /* maximum ""                */
  51. #define PITWID    3        /* max width of the pitch field        */
  52.  
  53. #define INFLDEF    Natural        /* default inflection            */
  54. #define SEXDEF    Male        /* default gender            */
  55. #define RATDEF    180        /* default speaking rate        */
  56. #define PITDEF    80        /* default base pitch            */
  57.  
  58. static Sex gender;        /* current gender             */
  59. static FOMode inflect;        /* current inflection Mode        */
  60. static int pitch;        /* current base pitch            */
  61. static int rate;        /* current speaking rate        */
  62.  
  63.  
  64. /*
  65.  * voicedfl() - set a default voice
  66.  */
  67.  
  68. voicedfl()
  69. {
  70.     gender = SEXDEF;
  71.     inflect = INFLDEF;
  72.     pitch = PITDEF;
  73.     rate = RATDEF;
  74.  
  75.     voiceset(gender, inflect, pitch, rate);
  76. }
  77.  
  78. /*
  79.  * setupvoice() - perform the "voice parameters" dialog
  80.  */
  81.  
  82. setupvoice()
  83. {
  84.     /*
  85.      * edited values of the fields
  86.      */
  87.  
  88.     int ratedt;        /* rate value        */
  89.     char rats[10];        /* rate string (pascal)    */
  90.     int pitedt;        /* pitch value        */
  91.     char pits[10];        /* pitch string (pascal)*/
  92.     int infledt;        /* inflection item    */
  93.     int genedt;        /* gender item        */
  94.  
  95.     int    itemhit;
  96.     DialogPtr setupptr;
  97.     int itemtype;
  98.     ControlHandle item;
  99.     Rect itembox;
  100.     
  101.     /*
  102.      * init the "edited" values
  103.      */
  104.     
  105.     ratedt = rate;
  106.     itoa(ratedt, rats);
  107.     ctop(rats);
  108.  
  109.     pitedt = pitch;
  110.     itoa(pitedt, pits);
  111.     ctop(pits);
  112.  
  113.     infledt = (inflect == Natural ? VP_NAT : VP_MONO);
  114.     genedt = (gender == Male ? VP_MALE : VP_FEM);
  115.     
  116.     /*
  117.      * open the dialog & set the fields to their initial values
  118.      */
  119.  
  120.     setupptr = GetNewDialog(VP_DLOG, NULL, (long) -1);
  121.     radioswitch(setupptr, infledt, infledt);
  122.     radioswitch(setupptr, genedt, genedt);
  123.     GetDItem(setupptr, VP_RATE, &itemtype, &item, &itembox);
  124.     SetIText(item, rats);
  125.     GetDItem(setupptr, VP_PITCH, &itemtype, &item, &itembox);
  126.     SetIText(item, pits);
  127.  
  128.     itemhit = VP_MALE;    /* (just to keep the loop going)    */
  129.     while (itemhit != VP_OK && itemhit != VP_CAN) {
  130.         /*
  131.          * carry out the dialog; get an item selection.
  132.          */
  133.         ModalDialog(NULL, &itemhit);
  134.  
  135.         /*
  136.          * take care of the buttons
  137.          */
  138.  
  139.         switch(itemhit) {
  140.         case VP_RATE:
  141.             justdigs(setupptr, itemhit, RATWID);
  142.             break;
  143.         case VP_PITCH:
  144.             justdigs(setupptr, itemhit, PITWID);
  145.             break;
  146.         case VP_MALE:
  147.         case VP_FEM:
  148.             /* not implemented -- do nothing */
  149.             break;
  150.         case VP_NAT:
  151.         case VP_MONO:
  152.             infledt = radioswitch(setupptr, itemhit, infledt);
  153.             break;
  154.         }
  155.     }
  156.     
  157.     if (itemhit == VP_OK) {
  158.  
  159.         /*
  160.          * update and verify the string field values
  161.          */
  162.  
  163.         GetDItem(setupptr, VP_RATE, &itemtype, &item, &itembox);
  164.         GetIText(item, rats);
  165.         ptoc(rats);
  166.         ratedt = atoi(rats);
  167.         if (ratedt < RATMIN) ratedt = RATMIN;
  168.         if (ratedt > RATMAX) ratedt = RATMAX;
  169.         itoa(ratedt, rats);
  170.         ctop(rats);
  171.         SetIText(item, rats);
  172.  
  173.  
  174.         GetDItem(setupptr, VP_PITCH, &itemtype, &item, &itembox);
  175.         GetIText(item, pits);
  176.         ptoc(pits);
  177.         pitedt = atoi(pits);
  178.         if (pitedt < PITMIN) pitedt = PITMIN;
  179.         if (pitedt > PITMAX) pitedt = PITMAX;
  180.         itoa(pitedt, pits);
  181.         ctop(pits);
  182.         SetIText(item, pits);
  183.  
  184.         /*
  185.          * store the selected values
  186.          */
  187.  
  188.         rate = ratedt;
  189.         pitch = pitedt;
  190.  
  191.         gender = (genedt == VP_MALE ? Male : Female);
  192.         inflect = (infledt == VP_NAT ? Natural : Robotic);
  193.         voiceset(gender, inflect, pitch, rate);
  194.     }
  195.  
  196.     /*
  197.      * release storage and remove dialog
  198.      */
  199.  
  200.     DisposDialog(setupptr);
  201. }
  202.  
  203. /*
  204.  * radioswitch() - turn one radio button off & another one on,
  205.  *  returning the item ID of the new "on" button.
  206.  */
  207.  
  208. int
  209. radioswitch(ptr, oncontrl, offcontrl)
  210. DialogPtr ptr;
  211. int oncontrl, offcontrl;    /* items to turn on and off, respectively */
  212. {
  213.     int itemtype;
  214.     ControlHandle item;
  215.     Rect itembox;
  216.  
  217.     GetDItem(ptr, offcontrl, &itemtype, &item, &itembox);
  218.     SetCtlVal(item, 0);
  219.     GetDItem(ptr, oncontrl, &itemtype, &item, &itembox);
  220.     SetCtlVal(item, 1);
  221.     return(oncontrl);
  222. }
  223.  
  224.  
  225. /*
  226.  * justdigs() - restrict the contents of an EditText item to just the given
  227.  *  number of digits.  I.E., throw out non-digit characters & characters
  228.  *  beyond the end of the field.
  229.  * The field must be less than 100 characters wide.
  230.  */
  231.  
  232. justdigs(ptr, field, width)
  233. DialogPtr ptr;
  234. int field;        /* # of the dialog item to modify    */
  235. int width;        /* max # of digits in the field    */
  236. {
  237.     int itemtype;
  238.     ControlHandle item;
  239.     Rect itembox;
  240.     static char buf[100];
  241.     char *src, *dst;    /* pointers for removing non-digits    */
  242.  
  243.     GetDItem(ptr, field, &itemtype, &item, &itembox);
  244.     GetIText(item, buf);
  245.     ptoc(buf);
  246.     
  247.     dst = &buf[0];
  248.     for (src = &buf[0]; (*dst = *src); ++src) {
  249.         if (*dst >= 0 && *dst <= '9') ++dst;
  250.     }
  251.     buf[width] = '\0';
  252.  
  253.     ctop(buf);
  254.     SetIText(item, buf);
  255. }
  256.  
  257.  
  258. /*
  259.  * itoa() - integer to ascii string
  260.  */
  261.  
  262. itoa(val, dest)
  263. int val;    /* the integer to convert    */
  264. char *dest;    /* where to put the string    */
  265. {
  266.     int isneg;        /* "it's a negative number"    */
  267.     char rightstr[21];    /* the right-justified string    */
  268.     char *p;
  269.  
  270.     isneg = 0;
  271.     if (val < 0) {
  272.         isneg = 1;
  273.         val = -val;
  274.         if (val < 0) {
  275.             /* it's the most negative number, -0    */
  276.             val = 0;
  277.         }
  278.     }
  279.  
  280.     p = &rightstr[21];
  281.     *(--p) = '\0';
  282.     do {
  283.         *(--p) = (char) (val % 10) + '0';
  284.         val /= 10;
  285.     } while (val > 0);
  286.  
  287.     if (isneg) {
  288.         *dest++ = '-';
  289.     }
  290.     while (*dest++ = *p++)
  291.         ;
  292. }
  293.