home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / mac / tkMacMenus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  7.5 KB  |  346 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkMacMenus.c --
  3.  *
  4.  *    These calls set up and manage the menubar for the
  5.  *    Macintosh version of Tk.
  6.  *
  7.  * Copyright (c) 1995-1996 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkMacMenus.c 1.36 97/07/10 13:29:21
  13.  */
  14.  
  15. #include "tcl.h"
  16. #include "tclMacInt.h"
  17. #include "tk.h"
  18. #include "tkInt.h"
  19. #include "tkMacInt.h"
  20.  
  21. /*
  22.  * The define Status defined by Xlib.h conflicts with the function Status
  23.  * defined by Devices.h.  We undefine it here to compile.
  24.  */
  25. #undef Status
  26. #include <Devices.h>
  27. #include <Menus.h>
  28. #include <Memory.h>
  29. #include <SegLoad.h>
  30. #include <StandardFile.h>
  31. #include <ToolUtils.h>
  32. #include <Balloons.h>
  33.  
  34. #define kAppleMenu        256
  35. #define kAppleAboutItem        1
  36. #define kFileMenu        2
  37. #define kEditMenu        3
  38.  
  39. #define kSourceItem        1
  40. #define kCloseItem        2
  41. #define kQuitItem        4
  42.  
  43. #define EDIT_CUT        1
  44. #define EDIT_COPY        2
  45. #define EDIT_PASTE        3
  46. #define EDIT_CLEAR        4
  47.  
  48. MenuHandle tkAppleMenu;            /* Handle to Apple menu; needed by menubar code */
  49. MenuHandle tkFileMenu;
  50. MenuHandle tkEditMenu;
  51.  
  52. static Tcl_Interp *    gInterp;    /* Interpreter for this application. */
  53.  
  54. Tk_Window Tk_TopCoordsToWindow _ANSI_ARGS_((Tk_Window tkwin, int rootX,
  55.             int rootY, int *newX, int *newY));
  56. static void GenerateEditEvent _ANSI_ARGS_((int flag));
  57. static void SourceDialog _ANSI_ARGS_((void));
  58.  
  59. /*
  60.  *----------------------------------------------------------------------
  61.  *
  62.  * TkMacHandleMenuSelect --
  63.  *
  64.  *    Handles events that occur in the Menu bar.
  65.  *
  66.  * Results:
  67.  *    None.
  68.  *
  69.  * Side effects:
  70.  *    None.
  71.  *
  72.  *----------------------------------------------------------------------
  73.  */
  74.  
  75. void 
  76. TkMacHandleMenuSelect(
  77.     long mResult,
  78.     int optionKeyPressed)
  79. {
  80.     short theItem = LoWord(mResult);
  81.     short theMenu = HiWord(mResult);
  82.     Str255 name;
  83.     Tk_Window tkwin;
  84.     Window window;
  85.  
  86.     if (mResult == 0) {
  87.         TkMacHandleTearoffMenu();
  88.     TkMacClearMenubarActive();
  89.     return;
  90.     }
  91.  
  92.     switch (theMenu) {
  93.     
  94.     case kAppleMenu:
  95.         switch (theItem) {
  96.         case kAppleAboutItem:
  97.             {
  98.             Tcl_CmdInfo dummy;
  99.             
  100.             if (optionKeyPressed || gInterp == NULL ||
  101.                 Tcl_GetCommandInfo(gInterp,
  102.                     "tkAboutDialog", &dummy) == 0) {
  103.                 TkAboutDlg();
  104.             } else {
  105.                 Tcl_Eval(gInterp, "tkAboutDialog");
  106.             }
  107.             break;
  108.             }
  109.         default:
  110.             GetItem(tkAppleMenu, theItem, name);
  111.             HiliteMenu(0);
  112.             OpenDeskAcc(name);
  113.             return;
  114.         }
  115.         break;
  116.     case kFileMenu:
  117.         switch (theItem) {
  118.         case kSourceItem:
  119.             /* TODO: source script */
  120.             SourceDialog();
  121.             break;
  122.         case kCloseItem:
  123.             /* Send close event */
  124.             window = TkMacGetXWindow(FrontWindow());
  125.             tkwin = Tk_IdToWindow(tkDisplayList->display, window);
  126.             TkGenWMDestroyEvent(tkwin);
  127.             break;
  128.         case kQuitItem:
  129.             /* Exit */
  130.             if (optionKeyPressed || gInterp == NULL) {
  131.             Tcl_Exit(0);
  132.             } else {
  133.             Tcl_Eval(gInterp, "exit");
  134.             }
  135.             break;
  136.         }
  137.         break;
  138.     case kEditMenu:
  139.         /*
  140.          * This implementation just send keysyms
  141.          * the Tk thinks are associated with function keys that
  142.          * do Cut, Copy & Paste on a Sun keyboard.
  143.          */
  144.         GenerateEditEvent(theItem);
  145.         break;
  146.     default:
  147.         TkMacDispatchMenuEvent(theMenu, theItem);
  148.         TkMacClearMenubarActive();
  149.         break;
  150.     }
  151.  
  152.     /*
  153.      * Finally we unhighlight the menu.
  154.      */
  155.     HiliteMenu(0);
  156. } /* TkMacHandleMenuSelect */
  157.  
  158. /*
  159.  *----------------------------------------------------------------------
  160.  *
  161.  * TkMacInitMenus --
  162.  *
  163.  *    This procedure initializes the Macintosh menu bar.
  164.  *
  165.  * Results:
  166.  *    None.
  167.  *
  168.  * Side effects:
  169.  *    None.
  170.  *
  171.  *----------------------------------------------------------------------
  172.  */
  173.  
  174. void 
  175. TkMacInitMenus(
  176.     Tcl_Interp     *interp)
  177. {
  178.     gInterp = interp;
  179.  
  180.     /* 
  181.      * At this point, InitMenus() should have already been called. 
  182.      */
  183.  
  184.     if (TkMacUseMenuID(256) != TCL_OK) {
  185.         panic("Menu ID 256 is already in use!");
  186.     }
  187.     tkAppleMenu = NewMenu(256, "\p\024");
  188.     if (tkAppleMenu == NULL) {
  189.     panic("memory - menus");
  190.     }
  191.     InsertMenu(tkAppleMenu, 0);
  192.     AppendMenu(tkAppleMenu, "\pAbout Tcl & Tk…");
  193.     AppendMenu(tkAppleMenu, "\p(-");
  194.     AddResMenu(tkAppleMenu, 'DRVR');
  195.  
  196.     if (TkMacUseMenuID(kFileMenu) != TCL_OK) {
  197.         panic("Menu ID %d is already in use!", kFileMenu);
  198.     }
  199.     tkFileMenu = NewMenu(kFileMenu, "\pFile");
  200.     if (tkFileMenu == NULL) {
  201.     panic("memory - menus");
  202.     }
  203.     InsertMenu(tkFileMenu, 0);
  204.     AppendMenu(tkFileMenu, "\pSource…");
  205.     AppendMenu(tkFileMenu, "\pClose/W");
  206.     AppendMenu(tkFileMenu, "\p(-");
  207.     AppendMenu(tkFileMenu, "\pQuit/Q");
  208.  
  209.     if (TkMacUseMenuID(kEditMenu) != TCL_OK) {
  210.         panic("Menu ID %d is already in use!", kEditMenu);
  211.     }
  212.     tkEditMenu = NewMenu(kEditMenu, "\pEdit");
  213.     if (tkEditMenu == NULL) {
  214.     panic("memory - menus");
  215.     }
  216.     InsertMenu(tkEditMenu, 0);
  217.     AppendMenu(tkEditMenu, "\pCut/X");
  218.     AppendMenu(tkEditMenu, "\pCopy/C");
  219.     AppendMenu(tkEditMenu, "\pPaste/V");
  220.     AppendMenu(tkEditMenu, "\pClear");
  221.     if (TkMacUseMenuID(kHMHelpMenuID) != TCL_OK) {
  222.         panic("Help menu ID %s is already in use!", kHMHelpMenuID);
  223.     }
  224.     
  225.     DrawMenuBar();
  226.     TkMacSetHelpMenuItemCount();
  227.     
  228.     return;
  229. }
  230.  
  231. /*
  232.  *----------------------------------------------------------------------
  233.  *
  234.  * GenerateEditEvent --
  235.  *
  236.  *    Takes an edit menu item and posts the corasponding a virtual 
  237.  *    event to Tk's event queue.
  238.  *
  239.  * Results:
  240.  *    None.
  241.  *
  242.  * Side effects:
  243.  *    May place events of queue.
  244.  *
  245.  *----------------------------------------------------------------------
  246.  */
  247.  
  248. static void 
  249. GenerateEditEvent(
  250.     int flag)
  251. {
  252.     XVirtualEvent event;
  253.     Point where;
  254.     Tk_Window tkwin;
  255.  
  256.     tkwin = tkMacFocusWin;
  257.     if (tkwin == NULL) {
  258.     return;
  259.     }
  260.  
  261.     event.type = VirtualEvent;
  262.     event.serial = Tk_Display(tkwin)->request;
  263.     event.send_event = false;
  264.     event.display = Tk_Display(tkwin);
  265.     event.event = Tk_WindowId(tkwin);
  266.     event.root = XRootWindow(Tk_Display(tkwin), 0);
  267.     event.subwindow = None;
  268.     event.time = TkpGetMS();
  269.     
  270.     GetMouse(&where);
  271.     tkwin = Tk_TopCoordsToWindow(tkwin, where.h, where.v, 
  272.         &event.x, &event.y);
  273.     LocalToGlobal(&where);
  274.     event.x_root = where.h;
  275.     event.y_root = where.v;
  276.     event.state = TkMacButtonKeyState();
  277.     event.same_screen = true;
  278.     
  279.     switch (flag) {
  280.     case EDIT_CUT:
  281.         event.name = Tk_GetUid("Cut");
  282.         break;
  283.         
  284.     case EDIT_COPY:
  285.         event.name = Tk_GetUid("Copy");
  286.         break;
  287.         
  288.     case EDIT_PASTE:
  289.         event.name = Tk_GetUid("Paste");
  290.         break;
  291.         
  292.     case EDIT_CLEAR:
  293.         event.name = Tk_GetUid("Clear");
  294.         break;
  295.     }
  296.     Tk_QueueWindowEvent((XEvent *) &event, TCL_QUEUE_TAIL);
  297. }
  298.  
  299. /*
  300.  *----------------------------------------------------------------------
  301.  *
  302.  * SourceDialog --
  303.  *
  304.  *    Presents a dialog to the user for selecting a Tcl file.  The
  305.  *    selected file will be sourced into the main interpreter.
  306.  *
  307.  * Results:
  308.  *    None.
  309.  *
  310.  * Side effects:
  311.  *    None.
  312.  *
  313.  *----------------------------------------------------------------------
  314.  */
  315.  
  316. static void 
  317. SourceDialog()
  318. {
  319.     StandardFileReply reply;
  320.     OSType fileTypes[1];
  321.     OSErr err;
  322.     int length, result;
  323.     Handle path;
  324.     
  325.     if (gInterp == NULL) {
  326.     return;
  327.     }
  328.     
  329.     fileTypes[0] = 'TEXT';
  330.     StandardGetFile(NULL, 1, fileTypes, &reply);
  331.     if (reply.sfGood == false) {
  332.     return;
  333.     }
  334.     
  335.     err = FSpPathFromLocation(&reply.sfFile, &length, &path);
  336.     if (err == noErr) {
  337.     HLock(path);
  338.     result = Tcl_EvalFile(gInterp, *path);
  339.     HUnlock(path);
  340.     DisposeHandle(path);
  341.     }
  342.     if (result == TCL_ERROR) {
  343.     Tcl_BackgroundError(gInterp);
  344.     }       
  345. }
  346.