home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / ival / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-26  |  9.2 KB  |  377 lines  |  [TEXT/MACA]

  1. /*
  2.  * menu.c - menu handling for Interval.
  3.  *
  4.  */
  5.  
  6. #include <quickdraw.h>
  7. #include <window.h>
  8. #include <menu.h>
  9. #include <desk.h>
  10. #include <control.h>
  11. #define ALLDIM 255 /* should be in control.h, but isn't    */
  12. #include <dialog.h>
  13. #include <resource.h>
  14.  
  15. #include "ival.h"
  16.  
  17. #define APPLEMENU    1    /* Menu ID for Apple menu    */
  18. #define FILEMENU    2    /* ...File menu            */
  19. #define EDITMENU    3    /* ...Edit menu            */
  20. #define CONFMENU    4    /* ...Configuration menu    */
  21. #define LASTMENU    4    /* Number of menus        */
  22.  
  23.     /* items in the Apple menu    */
  24. #define DLGABOUT    256    /* About...        */
  25.  
  26.     /* Items in the File menu    */
  27. #define IQUIT        1    /* quit the program    */
  28.  
  29.     /* items in the Edit menu    */
  30. #define UNDOEDIT    1    /* Undo        */
  31. /*----2----*/
  32. #define CUTEDIT        3    /* Cut        */
  33. #define COPYEDIT    4    /* Copy        */
  34. #define PASTEEDIT    5    /* Paste    */
  35. #define CLEAREDIT    6    /* Clear    */
  36.  
  37.     /* items in the Configuration menu    */
  38. #define EX_TYPE        1    /* who does what        */
  39. #define IV_TYPE        2    /* interval type selection    */
  40.  
  41. MenuHandle mymenus[LASTMENU + 1]; /* menus[1..LASTMENU]    */
  42.  
  43. #define DLGEX_TYPE    257    /* ResID of the "who does what" dialog    */
  44. #define NOTEPROG     5    /* "program gives notation" item    */
  45. #define NAMEPROG     6    /* "program gives name"            */
  46. #define SNDPROG         7    /* "program gives sound"        */
  47. #define NOTESTUD     8    /* "student gives notation"        */
  48. #define NAMESTUD     9    /* "student gives name"            */
  49. #define SNDSTUD        10    /* "student gives sound" (unimplemented)*/
  50.  
  51. #define DLGMISC        258    /* Resid of the "Misc Configuration" dlg */
  52.     /* the following 3 items must have sequential numbers */
  53. #define MI_HARM         3    /* "Harmonic intervals" item        */
  54. #define MI_MEL         4    /* "Melodic intervals"            */
  55. #define MI_MIX         5    /* "mixture"                */
  56.  
  57. #define MI_ACCOK     6    /* "allow accidentals"            */
  58.  
  59. /*
  60.  * setupmenus() - Initialize the menus and desk accessories (drivers).
  61.  */
  62.  
  63. setupmenus()
  64. {
  65.     int i;
  66.  
  67.     for (i = 1; i <= LASTMENU; i++) {
  68.     mymenus[i] = GetMenu(i);    /* our menu ID's start at 1    */
  69.     }
  70.  
  71.     AddResMenu(mymenus[APPLEMENU], 'DRVR');
  72.     for (i = 1; i <= LASTMENU; i++) {
  73.     /* insert menus; 0 => put at end */
  74.     InsertMenu(mymenus[i], 0);
  75.     }
  76.     DrawMenuBar();
  77. }
  78.  
  79. /*
  80.  * docommand() - handle a command given through a menu selection.
  81.  *
  82.  * If the command is Quit, we return true, else false.
  83.  * Since the menu was highlighted by MenuSelect, we must finish
  84.  * by unhighlighting it to indicate we're done.
  85.  */
  86.  
  87. int
  88. docommand(mresult)
  89. long    mresult;    /* the command to execute (a menu item)    */
  90. {
  91.     int refnum;
  92.     int themenu;        /* menu the mouse chose        */
  93.     int theitem;        /* item in that menu        */
  94.     WindowPtr actwindow;    /* the active (front) window    */
  95.     char name[255];
  96.     int     returns;        /* value to be returned        */
  97.     GrafPtr saveport;
  98.  
  99.     returns = 0;
  100.     themenu = HiWord(mresult);
  101.     theitem = LoWord(mresult);
  102.     actwindow = FrontWindow();
  103.     switch (themenu) {
  104.     case 0:         /* no selection -- do nothing    */
  105.         break;
  106.  
  107.     case APPLEMENU:
  108.         if (theitem == 1) {    /* Tell about the program */
  109.         saydialog(DLGABOUT);
  110.         } else {        /* Run a desk accessory    */
  111.         GetPort(&saveport);
  112.         GetItem(mymenus[APPLEMENU], theitem, name);
  113.         refnum = OpenDeskAcc(name);
  114.         SetPort(saveport);
  115.         }
  116.         break;
  117.  
  118.     case FILEMENU: 
  119.         switch (theitem) {
  120.         case IQUIT:    /* quit */
  121.             returns = 1;
  122.             break;
  123.         }
  124.         break;
  125.  
  126.     case EDITMENU: 
  127.         switch (theitem) {
  128.         case UNDOEDIT:
  129.         case CUTEDIT:
  130.         case COPYEDIT:
  131.         case PASTEEDIT:
  132.         case CLEAREDIT:
  133.         if (actwindow == windoc) {    /* do nothing        */
  134.         } else {            /* a DA's window    */
  135.             SystemEdit(theitem - 1);
  136.         }
  137.         break;
  138.         }
  139.         break;
  140.  
  141.     case CONFMENU: 
  142.         switch (theitem) {
  143.         case EX_TYPE: dlgtasks(); break;
  144.         case IV_TYPE: dlgmisc(); break;
  145.         }
  146.         break;
  147.     }
  148.     HiliteMenu(0);    /* we're done -- turn off the highlighting */
  149.     return(returns);
  150. }
  151.  
  152. /*
  153.  * markmenus() - take care of checking and dimming all items in all menus.
  154.  * This routine should be the only one that fiddles with menu item
  155.  * checking and dimming.
  156.  */
  157.  
  158. markmenus(whichwindow)
  159. WindowPtr whichwindow;        /* the active window            */
  160. {
  161.     MenuHandle themenu;
  162.     char txt[64];    /* (pascal) menu item text        */
  163.     
  164.     /* Apple menu is O.K.    */
  165.     /* File menu is O.K.    */
  166.  
  167.     /*
  168.      * Edit menu: dim it if our window is active.
  169.      * Otherwise, light it.
  170.      */
  171.  
  172.     themenu = mymenus[EDITMENU];
  173.     if (whichwindow == windoc) {
  174.         DisableItem(themenu, UNDOEDIT);
  175.         DisableItem(themenu, CUTEDIT);
  176.         DisableItem(themenu, COPYEDIT);
  177.         DisableItem(themenu, PASTEEDIT);
  178.         DisableItem(themenu, CLEAREDIT);
  179.     } else {
  180.         EnableItem(themenu, UNDOEDIT);
  181.         EnableItem(themenu, CUTEDIT);
  182.         EnableItem(themenu, COPYEDIT);
  183.         EnableItem(themenu, PASTEEDIT);
  184.         EnableItem(themenu, CLEAREDIT);
  185.     }
  186.  
  187.     /*
  188.      * Configuration menu: dim it if our window is not active.
  189.      * Otherwise, light it.
  190.      */
  191.  
  192.     themenu = mymenus[CONFMENU];
  193.     if (whichwindow != windoc) {
  194.         DisableItem(themenu, EX_TYPE);
  195.         DisableItem(themenu, IV_TYPE);
  196.     } else {
  197.         EnableItem(themenu, EX_TYPE);
  198.         EnableItem(themenu, IV_TYPE);
  199.     }
  200. }
  201.  
  202. /*
  203.  * dlgtasks() - do the "What to do" dialog.
  204.  */
  205.  
  206. dlgtasks()
  207. {
  208.     DialogPtr dlg;        /* our dialog            */
  209.     short itemhit;        /* item # of the selected item    */
  210.     struct tasks studcopy;    /* local version of studtasks    */
  211.     struct tasks progcopy;    /* local version of progtasks    */
  212.  
  213.     /*
  214.      * copy the state we're going to fiddle with
  215.      * so we can leave the original unchanged, if necessary.
  216.      */
  217.  
  218.     studcopy.notate = studtasks.notate;
  219.     studcopy.name = studtasks.name;
  220.     studcopy.sound = studtasks.sound;
  221.     
  222.     progcopy.notate = progtasks.notate;
  223.     progcopy.name = progtasks.name;
  224.     progcopy.sound = progtasks.sound;
  225.  
  226.     /*
  227.      * Create and maintain the dialog,
  228.      * Always updating the state of the buttons before allowing user input.
  229.      */
  230.  
  231.     dlg = GetNewDialog(DLGEX_TYPE, (char *) 0, (long) -1);
  232.     do {
  233.     flagbutton(dlg, progcopy.notate, NOTEPROG, NOTESTUD);
  234.     flagbutton(dlg, progcopy.name, NAMEPROG, NAMESTUD);
  235.     flagbutton(dlg, progcopy.sound, SNDPROG, SNDSTUD);
  236.        
  237.     ModalDialog((ProcPtr) 0, &itemhit);
  238.     switch (itemhit) {
  239.     case NOTEPROG:
  240.         progcopy.notate = !progcopy.notate;
  241.         studcopy.notate = !progcopy.notate;
  242.         break;
  243.     case NAMEPROG:
  244.         progcopy.name = !progcopy.name;
  245.         studcopy.name = !progcopy.name;
  246.         break;
  247.     case SNDPROG:
  248.         /*
  249.          * Since the machine can't hear,
  250.          * the following lines aren't here.
  251.         progcopy.sound = !progcopy.sound;
  252.         studcopy.sound = !progcopy.sound;
  253.          */
  254.         break;
  255.     case NOTESTUD:
  256.         studcopy.notate = !studcopy.notate;
  257.         progcopy.notate = !studcopy.notate;
  258.         break;
  259.     case NAMESTUD:
  260.         studcopy.name = !studcopy.name;
  261.         progcopy.name = !studcopy.name;
  262.         break;
  263.     case SNDSTUD:
  264.         /*
  265.          * Since the machine can't hear,
  266.          * the following lines aren't here.
  267.         studcopy.sound = !studcopy.sound;
  268.         progcopy.sound = !studcopy.sound;
  269.          */
  270.         break;
  271.     }
  272.     } while (itemhit != OK && itemhit != Cancel);
  273.     DisposDialog(dlg);
  274.     
  275.     if (itemhit == Cancel) return;
  276.  
  277.     /*
  278.      * Put the specified changes into effect.
  279.      */
  280.  
  281.     studtasks.notate = studcopy.notate;
  282.     studtasks.name = studcopy.name;
  283.     studtasks.sound = studcopy.sound;
  284.     
  285.     progtasks.notate = progcopy.notate;
  286.     progtasks.name = progcopy.name;
  287.     progtasks.sound = progcopy.sound;
  288.     
  289.     nexttest();
  290. }
  291.  
  292. /*
  293.  * dlgmisc() - do the "Misc Configuration" dialog.
  294.  */
  295.  
  296. dlgmisc()
  297. {
  298.     DialogPtr dlg;        /* our dialog                */
  299.     short itemhit;        /* item # of the selected item        */
  300.     short curivbut;        /* item # associated with iv playing    */
  301.                 /* ...(== MI_*)                */
  302.     short curaccok;        /* current value of "accidentals ok"    */
  303.     short itemtype;
  304.     ControlHandle item;
  305.     Rect itembox;
  306.  
  307.     /*
  308.      * copy the state we're going to fiddle with
  309.      * so we can leave the original unchanged, if necessary.
  310.      */
  311.  
  312.     switch (any_seq) {
  313.     case SEQ_HARM: curivbut = MI_HARM; break;
  314.     case SEQ_MEL: curivbut = MI_MEL; break;
  315.     case SEQ_MIX: curivbut = MI_MIX; break;
  316.     }
  317.     curaccok = acc_ok;
  318.  
  319.     /*
  320.      * Create and maintain the dialog,
  321.      * Always updating the items' state before allowing user input.
  322.      */
  323.  
  324.     dlg = GetNewDialog(DLGMISC, (char *) 0, (long) -1);
  325.  
  326.     do {
  327.     radioset(dlg, curivbut, MI_HARM, MI_MIX);
  328.     GetDItem(dlg, MI_ACCOK, &itemtype, &item, &itembox);
  329.     SetCtlVal(item, curaccok ? 1 : 0);
  330.        
  331.     ModalDialog((ProcPtr) 0, &itemhit);
  332.     switch (itemhit) {
  333.     case MI_HARM:
  334.     case MI_MEL:
  335.     case MI_MIX:
  336.         curivbut = itemhit;
  337.         break;
  338.     case MI_ACCOK:
  339.         curaccok = !curaccok;
  340.         break;
  341.     }
  342.     } while (itemhit != OK && itemhit != Cancel);
  343.     DisposDialog(dlg);
  344.     
  345.     if (itemhit == Cancel) return;
  346.  
  347.     /*
  348.      * Put the specified changes into effect.
  349.      */
  350.     
  351.     switch (curivbut) {
  352.     case MI_HARM: any_seq = SEQ_HARM; break;
  353.     case MI_MEL: any_seq = SEQ_MEL; break;
  354.     case MI_MIX: any_seq = SEQ_MIX; break;
  355.     }
  356.     acc_ok = curaccok;
  357.     
  358.     nexttest();
  359. }
  360.  
  361. /*
  362.  * flagbutton() - set the given buttons according to the state of a flag
  363.  */
  364.  
  365. flagbutton(dlg, flag, trueitem, falseitem)
  366. DialogPtr dlg;        /* the open dialog to effect    */
  367. short flag;        /* a boolean            */
  368. short trueitem;        /* item to set if flag is true    */
  369. short falseitem;    /* item to set if flag is false    */
  370. {
  371.     if (flag) {
  372.     (void) radioswitch(dlg, trueitem, falseitem);
  373.     } else {
  374.     (void) radioswitch(dlg, falseitem, trueitem);
  375.     }
  376. }
  377.