diff options
author | das <das> | 2007-11-04 10:39:14 (GMT) |
---|---|---|
committer | das <das> | 2007-11-04 10:39:14 (GMT) |
commit | 49748bb838ec7ec248cbd630b0bb5a55834632b7 (patch) | |
tree | 23c6df6d7ac4f9c4e7d068535f5b3e5d49e840b6 /macosx/tkMacOSXMenus.c | |
parent | 714a97699dd9691c81c6ece3c994875c555d4ea7 (diff) | |
download | tk-49748bb838ec7ec248cbd630b0bb5a55834632b7.zip tk-49748bb838ec7ec248cbd630b0bb5a55834632b7.tar.gz tk-49748bb838ec7ec248cbd630b0bb5a55834632b7.tar.bz2 |
* macosx/tkMacOSXMenus.c: add "Run Widget Demo" menu item to the
default Edit menu along with associated carbon event handler enabling
the item only if demo files are installed; cleanup handling of "About"
and "Source" menu items.
Diffstat (limited to 'macosx/tkMacOSXMenus.c')
-rw-r--r-- | macosx/tkMacOSXMenus.c | 197 |
1 files changed, 143 insertions, 54 deletions
diff --git a/macosx/tkMacOSXMenus.c b/macosx/tkMacOSXMenus.c index 4f991d2..b29ed45 100644 --- a/macosx/tkMacOSXMenus.c +++ b/macosx/tkMacOSXMenus.c @@ -11,7 +11,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkMacOSXMenus.c,v 1.18 2007/06/29 03:20:02 das Exp $ + * RCS: @(#) $Id: tkMacOSXMenus.c,v 1.19 2007/11/04 10:39:14 das Exp $ */ #include "tkMacOSXPrivate.h" @@ -22,7 +22,8 @@ #define kEditMenu 3 #define kSourceItem 1 -#define kCloseItem 2 +#define kDemoItem 2 +#define kCloseItem 3 #define EDIT_CUT 1 #define EDIT_COPY 2 @@ -33,15 +34,53 @@ MenuRef tkAppleMenu; MenuRef tkFileMenu; MenuRef tkEditMenu; -static Tcl_Interp * gInterp; /* Interpreter for this application. */ +static Tcl_Interp * gInterp = NULL; /* Standard menu interpreter. */ +static EventHandlerRef menuEventHandlerRef = NULL; static void GenerateEditEvent(int flag); -static void SourceDialog(void); +static Tcl_Obj* GetWidgetDemoPath(Tcl_Interp *interp); +static OSStatus MenuEventHandlerProc(EventHandlerCallRef callRef, + EventRef event, void *userData); + /* *---------------------------------------------------------------------- * + * GetWidgetDemoPath -- + * + * Get path to the widget demo. + * + * Results: + * pathObj with ref count 0. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +Tcl_Obj* +GetWidgetDemoPath( + Tcl_Interp *interp) +{ + Tcl_Obj *libpath , *result = NULL; + + libpath = Tcl_GetVar2Ex(gInterp, "tk_library", NULL, TCL_GLOBAL_ONLY); + if (libpath) { + Tcl_Obj *demo[2] = { Tcl_NewStringObj("demos", 5), + Tcl_NewStringObj("widget", 6) }; + + Tcl_IncrRefCount(libpath); + result = Tcl_FSJoinToPath(libpath, 2, demo); + Tcl_DecrRefCount(libpath); + } + return result; +} + +/* + *---------------------------------------------------------------------- + * * TkMacOSXHandleMenuSelect -- * * Handles events that occur in the Menu bar. @@ -81,8 +120,11 @@ TkMacOSXHandleMenuSelect( "tkAboutDialog", &dummy) == 0) { TkAboutDlg(); } else { - Tcl_EvalEx(gInterp, "tkAboutDialog", -1, - TCL_EVAL_GLOBAL); + if (Tcl_EvalEx(gInterp, "tkAboutDialog", -1, + TCL_EVAL_GLOBAL) != TCL_OK) { + Tcl_BackgroundError(gInterp); + } + Tcl_ResetResult(gInterp); } break; } @@ -91,11 +133,41 @@ TkMacOSXHandleMenuSelect( case kFileMenu: switch (theItem) { case kSourceItem: - /* - * TODO: source script - */ + if (gInterp) { + if(Tcl_EvalEx(gInterp, "tk_getOpenFile -filetypes {" + "{{TCL Scripts} {.tcl} TEXT} " + "{{Text Files} {} TEXT}}", -1, TCL_EVAL_GLOBAL) + == TCL_OK) { + Tcl_Obj *path = Tcl_GetObjResult(gInterp); + int len; + + Tcl_GetStringFromObj(path, &len); + if (len) { + Tcl_IncrRefCount(path); + if (Tcl_FSEvalFile(gInterp, path) + == TCL_ERROR) { + Tcl_BackgroundError(gInterp); + } + Tcl_DecrRefCount(path); + } + } + Tcl_ResetResult(gInterp); + } + break; + case kDemoItem: + if (gInterp) { + Tcl_Obj *path = GetWidgetDemoPath(gInterp); - SourceDialog(); + if (path) { + Tcl_IncrRefCount(path); + if (Tcl_FSEvalFile(gInterp, path) + == TCL_ERROR) { + Tcl_BackgroundError(gInterp); + } + Tcl_DecrRefCount(path); + Tcl_ResetResult(gInterp); + } + } break; case kCloseItem: /* Send close event */ @@ -127,6 +199,53 @@ TkMacOSXHandleMenuSelect( /* *---------------------------------------------------------------------- * + * MenuEventHandlerProc -- + * + * One-time handler of kEventMenuEnableItems for the edit menu. + * + * Results: + * OS status code. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +static OSStatus +MenuEventHandlerProc( + EventHandlerCallRef callRef, + EventRef event, + void *userData) +{ + OSStatus result = eventNotHandledErr, err; + int menuContext; + + err = ChkErr(GetEventParameter, event, kEventParamMenuContext, typeUInt32, + NULL, sizeof(menuContext), NULL, &menuContext); + if (err == noErr && (menuContext & kMenuContextMenuBarTracking)) { + if (gInterp) { + Tcl_Obj *path = GetWidgetDemoPath(gInterp); + + if (path) { + Tcl_IncrRefCount(path); + if (Tcl_FSAccess(path, R_OK) == 0) { + EnableMenuItem(tkFileMenu, kDemoItem); + } + Tcl_DecrRefCount(path); + } + } + ChkErr(RemoveEventHandler, menuEventHandlerRef); + menuEventHandlerRef = NULL; + result = noErr; + } + + return result; +} + +/* + *---------------------------------------------------------------------- + * * TkMacOSXInitMenus -- * * This procedure initializes the Macintosh menu bar. @@ -145,8 +264,12 @@ TkMacOSXInitMenus( Tcl_Interp *interp) { OSStatus err; - gInterp = interp; + EventHandlerUPP menuEventHandlerUPP; + const EventTypeSpec menuEventTypes[] = { + {kEventClassMenu, kEventMenuEnableItems}, + }; + gInterp = interp; if (TkMacOSXUseMenuID(kAppleMenu) != TCL_OK) { Tcl_Panic("Menu ID %d is already in use!", kAppleMenu); } @@ -170,8 +293,15 @@ TkMacOSXInitMenus( } SetMenuTitle(tkFileMenu, "\pFile"); InsertMenu(tkFileMenu, 0); - AppendMenu(tkFileMenu, "\pSource\xc9"); - AppendMenu(tkFileMenu, "\pClose/W"); + InsertMenuItem(tkFileMenu, "\pSource\xc9", kSourceItem - 1); + InsertMenuItem(tkFileMenu, "\pRun Widget Demo", kDemoItem - 1); + InsertMenuItem(tkFileMenu, "\pClose/W", kCloseItem - 1); + DisableMenuItem(tkFileMenu, kDemoItem); + menuEventHandlerUPP = NewEventHandlerUPP(MenuEventHandlerProc); + ChkErr(InstallEventHandler, GetMenuEventTarget(tkFileMenu), + menuEventHandlerUPP, GetEventTypeCount(menuEventTypes), + menuEventTypes, NULL, &menuEventHandlerRef); + DisposeEventHandlerUPP(menuEventHandlerUPP); if (TkMacOSXUseMenuID(kEditMenu) != TCL_OK) { Tcl_Panic("Menu ID %d is already in use!", kEditMenu); @@ -270,44 +400,3 @@ GenerateEditEvent( } Tk_QueueWindowEvent((XEvent *) &event, TCL_QUEUE_TAIL); } - -/* - *---------------------------------------------------------------------- - * - * SourceDialog -- - * - * Presents a dialog to the user for selecting a Tcl file. The - * selected file will be sourced into the main interpreter. - * - * Results: - * None. - * - * Side effects: - * None. - * - *---------------------------------------------------------------------- - */ - -static void -SourceDialog(void) -{ - int result; - const char *path; - const char *openCmd = "tk_getOpenFile -filetypes {\ - {{TCL Scripts} {.tcl} TEXT} {{Text Files} {} TEXT}}"; - - if (gInterp == NULL) { - return; - } - if (Tcl_EvalEx(gInterp, openCmd, -1, TCL_EVAL_GLOBAL) != TCL_OK) { - return; - } - path = Tcl_GetStringResult(gInterp); - if (strlen(path) == 0) { - return; - } - result = Tcl_EvalFile(gInterp, path); - if (result == TCL_ERROR) { - Tcl_BackgroundError(gInterp); - } -} |