home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / src / Menus.c < prev    next >
Text File  |  1993-11-18  |  11KB  |  463 lines

  1.  
  2. #pragma segment MENUS
  3.  
  4.  
  5. /*
  6. ** This source code was written by Tim Endres
  7. ** Email: time@ice.com.
  8. ** USMail: 8840 Main Street, Whitmore Lake, MI  48189
  9. **
  10. ** Some portions of this application utilize sources
  11. ** that are copyrighted by ICE Engineering, Inc., and
  12. ** ICE Engineering retains all rights to those sources.
  13. **
  14. ** Neither ICE Engineering, Inc., nor Tim Endres, 
  15. ** warrants this source code for any reason, and neither
  16. ** party assumes any responsbility for the use of these
  17. ** sources, libraries, or applications. The user of these
  18. ** sources and binaries assumes all responsbilities for
  19. ** any resulting consequences.
  20. */
  21.  
  22.  
  23. #include "tickle.h"
  24. #include "tge.h"
  25. #include "tclMac.h"
  26.  
  27. #define MAX_SCRIPT_MENU_ITEMS        32
  28.  
  29. char        *script_menu_commands[MAX_SCRIPT_MENU_ITEMS];
  30.  
  31.  
  32. create_menus()
  33.     {
  34.     int        i;
  35.     
  36.     apple_menu_hdl = NewMenu(applemenu, applemname);
  37.     AppendMenu(apple_menu_hdl, applemstr);
  38.     AddResMenu(apple_menu_hdl, (ResType)'DRVR');
  39.     InsertMenu(apple_menu_hdl, 0);
  40.  
  41.     file_menu_hdl = GetMenu((short) filemenu);
  42.     InsertMenu(file_menu_hdl, 0);
  43.  
  44.     edit_menu_hdl = GetMenu((short) editmenu);
  45.     InsertMenu(edit_menu_hdl, 0);
  46.  
  47.     tcl_menu_hdl = GetMenu((short) tcl_menu);
  48.     InsertMenu(tcl_menu_hdl, 0);
  49.     for ( i = 0 ; i < MAX_SCRIPT_MENU_ITEMS ; ++i )
  50.         script_menu_commands[i] = NULL;
  51.  
  52.     mtool_menu_hdl = GetMenu((short) mtoolmenu);
  53.     InsertMenu(mtool_menu_hdl, 0);
  54.  
  55.     ttool_menu_hdl = GetMenu((short) ttoolmenu);
  56.     InsertMenu(ttool_menu_hdl, 0);
  57.  
  58.     hc_submenu_hdl = GetMenu((short) hcsubmenu);
  59.     InsertMenu(hc_submenu_hdl, hierMenu);
  60.  
  61.     xc_submenu_hdl = GetMenu((short) xcsubmenu);
  62.     InsertMenu(xc_submenu_hdl, hierMenu);
  63.  
  64.     utool_menu_hdl = GetMenu((short) utoolmenu);
  65.     InsertMenu(utool_menu_hdl, 0);
  66.  
  67.     tar_menu_hdl = GetMenu((short) tarmenu);
  68.     InsertMenu(tar_menu_hdl, 0);
  69.  
  70.     ptar_menu_hdl = GetMenu((short) ptarmenu);
  71.     InsertMenu(ptar_menu_hdl, hierMenu);
  72.  
  73.     asd_menu_hdl = GetMenu((short) asdmenu);
  74.     InsertMenu(asd_menu_hdl, 0);
  75.  
  76.     stool_menu_hdl = GetMenu((short) stoolmenu);
  77.     InsertMenu(stool_menu_hdl, 0);
  78.  
  79.     stuffit_available = StuffItAvailable();
  80.     if (! stuffit_available)
  81.         DisableItem(stool_menu_hdl, 0);
  82.     
  83.     DrawMenuBar();
  84.     }
  85.  
  86. yield_menus(turn_on)
  87.     int        turn_on;
  88.     {
  89.     WindowPtr    myWindow;
  90.     
  91.     yielding_on = (turn_on == YIELD_ON);
  92.     
  93.     if (turn_on == YIELD_ON)
  94.         {
  95.         EnableItem(file_menu_hdl, (short)cancel_op_item);
  96.         EnableItem(file_menu_hdl, (short)pause_op_item);
  97.  
  98.         DisableItem(file_menu_hdl, (short)new_item);
  99.         DisableItem(file_menu_hdl, (short)open_item);
  100.         DisableItem(file_menu_hdl, (short)save_item);
  101.         DisableItem(file_menu_hdl, (short)save_as_item);
  102.         DisableItem(file_menu_hdl, (short)close_window_item);
  103.         DisableItem(file_menu_hdl, (short)run_script_item);
  104.         DisableItem(file_menu_hdl, (short)quit_item);
  105.         
  106.         /* DisableItem(edit_menu_hdl, (short)0); */
  107.  
  108.         DisableItem(tcl_menu_hdl, (short)0);
  109.  
  110.         DisableItem(mtool_menu_hdl, (short)0);
  111.  
  112.         DisableItem(ttool_menu_hdl, (short)0);
  113.  
  114.         DisableItem(utool_menu_hdl, (short)0);
  115.  
  116.         DisableItem(tar_menu_hdl, (short)0);
  117.  
  118.         DisableItem(stool_menu_hdl, (short)0);
  119.  
  120.         DisableItem(asd_menu_hdl, (short)0);
  121.         }
  122.     else
  123.         {
  124.         DisableItem(file_menu_hdl, (short)cancel_op_item);
  125.         DisableItem(file_menu_hdl, (short)pause_op_item);
  126.             
  127.         EnableItem(file_menu_hdl, (short)run_script_item);
  128.         EnableItem(file_menu_hdl, (short)new_item);
  129.         EnableItem(file_menu_hdl, (short)open_item);
  130.         EnableItem(file_menu_hdl, (short)quit_item);
  131.  
  132.         myWindow = FrontWindow();
  133.         if (myWindow != NULL)
  134.             {
  135.             EnableItem(file_menu_hdl, (short)close_window_item);
  136.             if (WPeek->windowKind == tgeWKind)
  137.                 tge_act_menus(myWindow);
  138.             }
  139.         
  140.         /* EnableItem(edit_menu_hdl, (short)0); */
  141.  
  142.         EnableItem(tcl_menu_hdl, (short)0);
  143.  
  144.         EnableItem(mtool_menu_hdl, (short)0);
  145.  
  146.         EnableItem(ttool_menu_hdl, (short)0);
  147.  
  148.         EnableItem(utool_menu_hdl, (short)0);
  149.  
  150.         EnableItem(tar_menu_hdl, (short)0);
  151.  
  152.         if (stuffit_available)
  153.             EnableItem(stool_menu_hdl, (short)0);
  154.  
  155.         EnableItem(asd_menu_hdl, (short)0);
  156.         }
  157.  
  158.     cancel_current_op = 0;
  159.     pause_op = 0;
  160.     
  161.     DrawMenuBar();
  162.     }
  163.  
  164.  
  165. int
  166. Cmd_ScriptMenu(clientData, interp, argc, argv)
  167.     char        *clientData;
  168.     Tcl_Interp    *interp;
  169.     int            argc;
  170.     char        **argv;
  171.     {
  172. #    pragma unused (clientData, argv)
  173.  
  174.     if (argc < 3)
  175.         {
  176.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], "\"",
  177.                         " ADD|DEL item command ?before? ", (char *) NULL);
  178.         return TCL_ERROR;
  179.         }
  180.     
  181.     if (strcmp(argv[1], "ADD") == 0)
  182.         {
  183.         if (argc != 4 && argc != 5)
  184.             {
  185.             Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], "\"",
  186.                             " ADD item command ?before? ", (char *) NULL);
  187.             return TCL_ERROR;
  188.             }
  189.         
  190.         return script_menu_add(interp, argv[2], argv[3], (argc == 5 ? argv[4] : NULL));
  191.         }
  192.     else if (strcmp(argv[1], "DEL") == 0)
  193.         {
  194.         if (argc != 3)
  195.             {
  196.             Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], "\"",
  197.                             " DEL item ", (char *) NULL);
  198.             return TCL_ERROR;
  199.             }
  200.         
  201.         return script_menu_del(interp, argv[2]);
  202.         }
  203.     else
  204.         {
  205.         Tcl_AppendResult(interp, "invalid command \"", argv[1], "\"",
  206.                         " must be ADD or DEL ", (char *) NULL);
  207.         return TCL_ERROR;
  208.         }
  209.     }
  210.  
  211. script_menu_add(interp, item_name, command, before_item)
  212.     Tcl_Interp    *interp;
  213.     char        *item_name;
  214.     char        *command;
  215.     char        *before_item;
  216.     {
  217.     int        result = TCL_OK;
  218.     int        i, numitems, item_num = -1, before_num = -1;
  219.     char    itemstr[256];
  220.  
  221.     if (tcl_menu_hdl == NULL)
  222.         {
  223.         Tcl_AppendResult(interp, " scripts menu not found ", (char *) NULL);
  224.         return TCL_ERROR;
  225.         }
  226.     
  227.     if (script_menu_commands[0] == NULL)
  228.         {
  229.         c2pstr(item_name);
  230.         SetItem(tcl_menu_hdl, 1, item_name);
  231.         p2cstr(item_name);
  232.         script_menu_commands[0] = csavestr(command);
  233.         return TCL_OK;
  234.         }
  235.     
  236.     numitems = CountMItems(tcl_menu_hdl);
  237.     for (i=1 ; i <= numitems ; ++i)
  238.         {
  239.         GetItem(tcl_menu_hdl, i, itemstr);
  240.         p2cstr(itemstr);
  241.         if (StrCmp(itemstr, item_name) == 0)
  242.             item_num = i;
  243.         else if (StrCmp(itemstr, before_item) == 0)
  244.             before_num = i;
  245.         }
  246.  
  247.     if (item_num != -1)
  248.         {
  249.         }
  250.     else if (before_num != -1)
  251.         {
  252.         item_num = before_num;
  253.         if (item_num < MAX_SCRIPT_MENU_ITEMS)
  254.             {
  255.             InsMenuItem(tcl_menu_hdl, "\pSCRIPT", item_num-1);
  256.             c2pstr(item_name);
  257.             SetItem(tcl_menu_hdl, item_num, item_name);
  258.             p2cstr(item_name);
  259.             for (i=MAX_SCRIPT_MENU_ITEMS - 1; i >= item_num; --i)
  260.                 {
  261.                 if (script_menu_commands[i-1] != NULL)
  262.                     {
  263.                     if (script_menu_commands[i] != NULL)
  264.                         free(script_menu_commands[i]);
  265.                     script_menu_commands[i] = script_menu_commands[i-1];
  266.                     script_menu_commands[i-1] = NULL;
  267.                     }
  268.                 }
  269.             }
  270.         }
  271.     else
  272.         {
  273.         item_num = numitems + 1;
  274.         if (item_num < MAX_SCRIPT_MENU_ITEMS)
  275.             {
  276.             AppendMenu(tcl_menu_hdl, "\pSCRIPT");
  277.             c2pstr(item_name);
  278.             SetItem(tcl_menu_hdl, item_num, item_name);
  279.             p2cstr(item_name);
  280.             }
  281.         }
  282.     
  283.     if (item_num < MAX_SCRIPT_MENU_ITEMS)
  284.         {
  285.         if (script_menu_commands[item_num-1] != NULL)    
  286.             free(script_menu_commands[item_num-1]);
  287.         
  288.         script_menu_commands[item_num-1] = csavestr(command);
  289.         }
  290.     
  291.     return TCL_OK;
  292.     }
  293.  
  294. script_menu_del(interp, item_name)
  295.     Tcl_Interp    *interp;
  296.     char        *item_name;
  297.     {
  298.     int        result = TCL_OK;
  299.     int        i, numitems, item_num = -1, before_num = -1;
  300.     char    itemstr[256];
  301.     
  302.     if (tcl_menu_hdl == NULL)
  303.         {
  304.         Tcl_AppendResult(interp, " scripts menu not found ", (char *) NULL);
  305.         return TCL_ERROR;
  306.         }
  307.     
  308.     numitems = CountMItems(tcl_menu_hdl);
  309.     if (numitems == 1)
  310.         {
  311.         if (script_menu_commands[0] != NULL)
  312.             {
  313.             SetItem(tcl_menu_hdl, 1, "\pNo Scripts Installed…");
  314.             free(script_menu_commands[0]);
  315.             script_menu_commands[0] = NULL;
  316.             return TCL_OK;
  317.             }
  318.         else
  319.             {
  320.             Tcl_AppendResult(interp, " no scripts installed ", (char *) NULL);
  321.             return TCL_ERROR;
  322.             }
  323.         }
  324.     
  325.     for (i=1 ; i <= numitems ; ++i)
  326.         {
  327.         GetItem(tcl_menu_hdl, i, itemstr);
  328.         p2cstr(itemstr);
  329.         if (StrCmp(itemstr, item_name) == 0)
  330.             {
  331.             item_num = i;
  332.             break;
  333.             }
  334.         }
  335.     
  336.     if (item_num == -1)
  337.         {
  338.         Tcl_AppendResult(interp, "could not find menu item \"", item_name, "\" ", (char *) NULL);
  339.         return TCL_ERROR;
  340.         }
  341.     else
  342.         {
  343.         DelMenuItem(tcl_menu_hdl, item_num);
  344.         if (script_menu_commands[item_num-1] != NULL)
  345.             free(script_menu_commands[item_num-1]);
  346.         
  347.         for (i=item_num; i<MAX_SCRIPT_MENU_ITEMS - 1; ++i)
  348.             {
  349.             if (script_menu_commands[i] != NULL)
  350.                 script_menu_commands[i-1] = script_menu_commands[i];
  351.             else
  352.                 break;
  353.             }
  354.         
  355.         if (script_menu_commands[i+1] != NULL)
  356.             free(script_menu_commands[i+1]);
  357.         script_menu_commands[i+1] = NULL;
  358.         }
  359.  
  360.     return TCL_OK;
  361.     }
  362.  
  363. scripts_menu_run(item)
  364. int        item;
  365.     {
  366.     int            result = TCL_OK;
  367.     int            tclresult,
  368.                 line;
  369.     PFI            saveproc;
  370.     Handle        saveH,
  371.                 stdoutH;
  372.     WindowPtr    myWindow = NULL;
  373.     Tcl_Interp    *interp = g_interp;
  374.     
  375.     extern int tcl_handle_output();
  376.     extern PFI Tcl_SetPrintProcedure();
  377.     
  378.     if (script_menu_commands[item-1] != NULL)
  379.         {
  380.         stdoutH = NewHandle(0);
  381.         if (! CheckOption())
  382.             {
  383.             myWindow = FrontWindow();
  384.             
  385.             if (myWindow != NULL && WPeek->windowKind == tgeWKind)
  386.                 {
  387.                 interp = (Tcl_Interp *) TGEWPtr->fobject;
  388.                 if (interp == NULL)
  389.                     {
  390.                     message_alert("Could not get window's interpretter.");
  391.                     return TCL_ERROR;
  392.                     }
  393.                 }
  394.             else
  395.                 myWindow = NULL;
  396.             }
  397.         
  398.         if (interp != (Tcl_Interp *)0)
  399.             {
  400.             saveH = tcl_Houtput_sethdl(stdoutH);
  401.             saveproc = Tcl_SetPrintProcedure(tcl_handle_output);
  402.             
  403.             tclresult = Tcl_Eval(g_interp, script_menu_commands[item-1], 0, (char **)0);
  404.                 
  405.             Tcl_SetPrintProcedure(saveproc);
  406.             tcl_Houtput_sethdl(saveH);
  407.             
  408.             if ( myWindow != NULL &&
  409.                     ( GetHandleSize(stdoutH) > 0 ||
  410.                         ( interp->result != NULL && interp->result[0] ) ) )
  411.                 {
  412.                 SetPort(myWindow);
  413.                 
  414.                 tge_kill_caret(myWindow);
  415.                 
  416.                 if (TGEWPtr->sel_start != TGEWPtr->sel_end)
  417.                     tge_invert_selection(myWindow);
  418.     
  419.                 line = tge_find_pos_line(myWindow, TGEWPtr->sel_end);
  420.                 TGEWPtr->sel_start = 
  421.                     tge_selection_line_append_pos(myWindow, line);
  422.                 TGEWPtr->sel_end = TGEWPtr->sel_start;
  423.                 
  424.                 if (GetHandleSize(stdoutH) > 0)
  425.                     tge_paste_handle( myWindow, stdoutH );
  426.                 
  427.                 if (interp->result != NULL && interp->result[0] != '\0')
  428.                     tge_paste_buffer( myWindow, interp->result, strlen(interp->result) );
  429.                 
  430.                 TGEWPtr->sel_start = TGEWPtr->sel_end -
  431.                         ( GetHandleSize(stdoutH) + 
  432.                             (interp->result != NULL ? strlen(interp->result) : 0) );
  433.                         
  434.                 SetPort(myWindow);
  435.                 tge_compute_selection(myWindow);
  436.                 tge_caret_on(myWindow);
  437.                 tge_undo_start_typing(myWindow, TGEWPtr->sel_start);
  438.                 
  439.                 SetPort(myWindow);
  440.                 tge_invert_selection(myWindow);
  441.                 }
  442.             
  443.             UInitCursor();
  444.             
  445.             result = TCL_OK;
  446.             }
  447.         else
  448.             {
  449.             result = TCL_ERROR;
  450.             }
  451.         
  452.         if (stdoutH != NULL)
  453.             DisposHandle(stdoutH);
  454.         
  455.         return result;
  456.         }
  457.     else
  458.         {
  459.         return TCL_ERROR;
  460.         }
  461.     }
  462.  
  463.