diff options
author | dkf <dkf@noemail.net> | 2009-07-06 15:45:53 (GMT) |
---|---|---|
committer | dkf <dkf@noemail.net> | 2009-07-06 15:45:53 (GMT) |
commit | a4a2be72d7995db36b251e6e298c48b24bad36a9 (patch) | |
tree | 1c21cfaff509a74819bb53366028744a0fa2c822 /macosx | |
parent | 4bcc4f1a52e3b29998d3cd0ef7b3a4cbd6507c87 (diff) | |
download | tk-a4a2be72d7995db36b251e6e298c48b24bad36a9.zip tk-a4a2be72d7995db36b251e6e298c48b24bad36a9.tar.gz tk-a4a2be72d7995db36b251e6e298c48b24bad36a9.tar.bz2 |
Formatting cleanup, mark files as Objective-C.
FossilOrigin-Name: e03c9f2b4eb370711f0e63c3d512e5ca1ce67aef
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/tkMacOSXDialog.c | 336 | ||||
-rw-r--r-- | macosx/tkMacOSXMenu.c | 259 |
2 files changed, 365 insertions, 230 deletions
diff --git a/macosx/tkMacOSXDialog.c b/macosx/tkMacOSXDialog.c index fe39d8c..1244a4a 100644 --- a/macosx/tkMacOSXDialog.c +++ b/macosx/tkMacOSXDialog.c @@ -7,10 +7,10 @@ * Copyright 2001-2009, Apple Inc. * Copyright (c) 2006-2009 Daniel A. Steffen <das@users.sourceforge.net> * - * See the file "license.terms" for information on usage and redistribution - * of this file, and for a DISCLAIMER OF ALL WARRANTIES. + * See the file "license.terms" for information on usage and redistribution of + * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkMacOSXDialog.c,v 1.44 2009/06/29 14:35:01 das Exp $ + * RCS: @(#) $Id: tkMacOSXDialog.c,v 1.45 2009/07/06 15:45:54 dkf Exp $ */ #include "tkMacOSXPrivate.h" @@ -102,9 +102,9 @@ static const NSAlertStyle alertStyles[] = { }; /* - * Need to map from 'alertButtonStrings' and its corresponding integer, - * index to the native button index, which is 1, 2, 3, from right to left. - * This is necessary to do for each separate '-type' of button sets. + * Need to map from 'alertButtonStrings' and its corresponding integer, index + * to the native button index, which is 1, 2, 3, from right to left. This is + * necessary to do for each separate '-type' of button sets. */ static const short alertButtonIndexAndTypeToNativeButtonIndex[][7] = { @@ -130,7 +130,7 @@ static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = { [TYPE_YESNO] = {5, 6, 0}, [TYPE_YESNOCANCEL] = {5, 6, 4}, }; - + #pragma mark TKApplication(TKDialog) @interface NSColorPanel(TKDialog) @@ -138,12 +138,15 @@ static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = { @end @implementation TKApplication(TKDialog) -- (void)tkFilePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode - contextInfo:(void *)contextInfo { + +- (void) tkFilePanelDidEnd: (NSSavePanel *) panel + returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo +{ FilePanelCallbackInfo *callbackInfo = contextInfo; if (returnCode == NSFileHandlingPanelOKButton) { Tcl_Obj *resultObj; + if (callbackInfo->multiple) { resultObj = Tcl_NewListObj(0, NULL); for (NSString *name in [(NSOpenPanel*)panel filenames]) { @@ -157,6 +160,7 @@ static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = { Tcl_Obj **objv, **tmpv; int objc, result = Tcl_ListObjGetElements(callbackInfo->interp, callbackInfo->cmdObj, &objc, &objv); + if (result == TCL_OK && objc) { tmpv = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * (objc + 2)); memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc); @@ -179,18 +183,22 @@ static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = { ckfree((char*) callbackInfo); } } -- (void)tkAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode - contextInfo:(void *)contextInfo { + +- (void) tkAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode + contextInfo: (void *) contextInfo +{ AlertCallbackInfo *callbackInfo = contextInfo; if (returnCode != NSAlertErrorReturn) { Tcl_Obj *resultObj = Tcl_NewStringObj(alertButtonStrings[ alertNativeButtonIndexAndTypeToButtonIndex[callbackInfo-> typeIndex][returnCode - NSAlertFirstButtonReturn]], -1); + if (callbackInfo->cmdObj) { Tcl_Obj **objv, **tmpv; int objc, result = Tcl_ListObjGetElements(callbackInfo->interp, callbackInfo->cmdObj, &objc, &objv); + if (result == TCL_OK && objc) { tmpv = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * (objc + 2)); memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc); @@ -264,28 +272,26 @@ Tk_ChooseColorObjCmd( value = Tcl_GetString(objv[i + 1]); switch (index) { - case COLOR_INITIAL: { - XColor *colorPtr; + case COLOR_INITIAL: { + XColor *colorPtr; - colorPtr = Tk_GetColor(interp, tkwin, value); - if (colorPtr == NULL) { - goto end; - } - initialColor = TkMacOSXGetNSColor(NULL, colorPtr->pixel); - Tk_FreeColor(colorPtr); - break; - } - case COLOR_PARENT: { - parent = Tk_NameToWindow(interp, value, tkwin); - if (parent == NULL) { - goto end; - } - break; + colorPtr = Tk_GetColor(interp, tkwin, value); + if (colorPtr == NULL) { + goto end; } - case COLOR_TITLE: { - title = value; - break; + initialColor = TkMacOSXGetNSColor(NULL, colorPtr->pixel); + Tk_FreeColor(colorPtr); + break; + } + case COLOR_PARENT: + parent = Tk_NameToWindow(interp, value, tkwin); + if (parent == NULL) { + goto end; } + break; + case COLOR_TITLE: + title = value; + break; } } colorPanel = [NSColorPanel sharedColorPanel]; @@ -322,6 +328,7 @@ Tk_ChooseColorObjCmd( Tcl_ResetResult(interp); } result = TCL_OK; + end: return result; } @@ -493,7 +500,8 @@ Tk_GetOpenFileObjCmd( if (typeVariablePtr && result == TCL_OK) { Tcl_SetVar(interp, Tcl_GetString(typeVariablePtr), "", 0); } -end: + + end: TkFreeFileFilters(&fl); return result; } @@ -658,7 +666,8 @@ Tk_GetSaveFileObjCmd( contextInfo:callbackInfo]; } result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR; -end: + + end: TkFreeFileFilters(&fl); return result; } @@ -779,7 +788,8 @@ Tk_ChooseDirectoryObjCmd( contextInfo:callbackInfo]; } result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR; -end: + + end: return result; } @@ -803,20 +813,29 @@ void TkAboutDlg(void) { NSImage *image; - NSString *path = [NSApp tkFrameworkImagePath:@"Tk.tiff"]; + NSString *path = [NSApp tkFrameworkImagePath: @"Tk.tiff"]; + if (path) { image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease]; } else { image = [NSApp applicationIconImage]; } + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateFormat:@"Y"]; + NSString *year = [dateFormatter stringFromDate:[NSDate date]]; + [dateFormatter release]; - NSMutableParagraphStyle *style = [[[NSParagraphStyle defaultParagraphStyle] - mutableCopy] autorelease]; + + NSMutableParagraphStyle *style = + [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] + autorelease]; + [style setAlignment:NSCenterTextAlignment]; + NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: @"Tcl & Tk", @"ApplicationName", @"Tcl " TCL_VERSION " & Tk " TK_VERSION, @"ApplicationVersion", @@ -827,12 +846,12 @@ TkAboutDlg(void) [[[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"%1$C 1987-%2$@ Tcl Core Team." "\n\n" - "%1$C 2002-%2$@ Daniel A. Steffen." "\n\n" - "%1$C 2001-2009 Apple Inc." "\n\n" - "%1$C 2001-2002 Jim Ingham & Ian Reid" "\n\n" - "%1$C 1998-2000 Jim Ingham & Ray Johnson" "\n\n" - "%1$C 1998-2000 Scriptics Inc." "\n\n" - "%1$C 1996-1997 Sun Microsystems Inc.", 0xA9, year] attributes: + "%1$C 2002-%2$@ Daniel A. Steffen." "\n\n" + "%1$C 2001-2009 Apple Inc." "\n\n" + "%1$C 2001-2002 Jim Ingham & Ian Reid" "\n\n" + "%1$C 1998-2000 Jim Ingham & Ray Johnson" "\n\n" + "%1$C 1998-2000 Scriptics Inc." "\n\n" + "%1$C 1996-1997 Sun Microsystems Inc.", 0xA9, year] attributes: [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]] autorelease], @"Credits", nil]; @@ -978,13 +997,12 @@ Tk_MessageBoxObjCmd( } if (indexDefaultOption) { /* - * Any '-default' option needs to know the '-type' option, which is why - * we do this here. + * Any '-default' option needs to know the '-type' option, which is + * why we do this here. */ if (Tcl_GetIndexFromObj(interp, objv[indexDefaultOption + 1], - alertButtonStrings, "value", TCL_EXACT, &index) - != TCL_OK) { + alertButtonStrings, "value", TCL_EXACT, &index) != TCL_OK) { goto end; } @@ -1009,15 +1027,18 @@ Tk_MessageBoxObjCmd( buttons = [alert buttons]; for (NSButton *b in buttons) { NSString *ke = [b keyEquivalent]; + if (([ke isEqualToString:@"\r"] || [ke isEqualToString:@"\033"]) && ![b keyEquivalentModifierMask]) { [b setKeyEquivalent:@""]; } } - [[buttons objectAtIndex:[buttons count]-1] setKeyEquivalent:@"\033"]; - [[buttons objectAtIndex:defaultNativeButtonIndex-1] setKeyEquivalent:@"\r"]; + [[buttons objectAtIndex: [buttons count]-1] setKeyEquivalent: @"\033"]; + [[buttons objectAtIndex: defaultNativeButtonIndex-1] + setKeyEquivalent: @"\r"]; if (cmdObj) { - callbackInfo = (AlertCallbackInfo *) ckalloc(sizeof(AlertCallbackInfo)); + callbackInfo = (AlertCallbackInfo *) + ckalloc(sizeof(AlertCallbackInfo)); if (Tcl_IsShared(cmdObj)) { cmdObj = Tcl_DuplicateObj(cmdObj); } @@ -1039,7 +1060,7 @@ Tk_MessageBoxObjCmd( contextInfo:callbackInfo]; } result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR; -end: + end: [alert release]; return result; } @@ -1060,19 +1081,25 @@ typedef struct FontchooserData { Tcl_Obj *cmdObj; Tk_Window parent; } FontchooserData; + enum FontchooserEvent { FontchooserClosed, FontchooserSelection }; -static void FontchooserEvent(int kind); -static Tcl_Obj *FontchooserCget(FontchooserData *fcdPtr, int optionIndex); -static int FontchooserConfigureCmd(ClientData clientData, Tcl_Interp *interp, - int objc, Tcl_Obj *const objv[]); -static int FontchooserShowCmd(ClientData clientData, Tcl_Interp *interp, - int objc, Tcl_Obj *const objv[]); -static int FontchooserHideCmd(ClientData clientData, Tcl_Interp *interp, - int objc, Tcl_Obj *const objv[]); -static void FontchooserParentEventHandler(ClientData clientData, - XEvent *eventPtr); -static void DeleteFontchooserData(ClientData clientData, Tcl_Interp *interp); +static void FontchooserEvent(int kind); +static Tcl_Obj * FontchooserCget(FontchooserData *fcdPtr, + int optionIndex); +static int FontchooserConfigureCmd(ClientData clientData, + Tcl_Interp *interp, int objc, + Tcl_Obj *const objv[]); +static int FontchooserShowCmd(ClientData clientData, + Tcl_Interp *interp, int objc, + Tcl_Obj *const objv[]); +static int FontchooserHideCmd(ClientData clientData, + Tcl_Interp *interp, int objc, + Tcl_Obj *const objv[]); +static void FontchooserParentEventHandler(ClientData clientData, + XEvent *eventPtr); +static void DeleteFontchooserData(ClientData clientData, + Tcl_Interp *interp); MODULE_SCOPE const TkEnsemble tkFontchooserEnsemble[]; const TkEnsemble tkFontchooserEnsemble[] = { @@ -1095,10 +1122,14 @@ enum FontchooserOption { }; @implementation TKApplication(TKFontPanel) -- (void)changeFont:(id)sender { + +- (void) changeFont: (id) sender +{ NSFontManager *fm = [NSFontManager sharedFontManager]; + if ([fm currentFontAction] == NSViaPanelFontAction) { NSFont *font = [fm convertFont:fontPanelFont]; + if (![fontPanelFont isEqual:font]) { [fontPanelFont release]; fontPanelFont = [font retain]; @@ -1106,20 +1137,27 @@ enum FontchooserOption { } } } -- (void)changeAttributes:(id)sender { + +- (void) changeAttributes: (id) sender +{ NSDictionary *attributes = [sender convertAttributes: fontPanelFontAttributes]; + if (![fontPanelFontAttributes isEqual:attributes]) { [fontPanelFontAttributes setDictionary:attributes]; FontchooserEvent(FontchooserSelection); } } -- (NSUInteger) validModesForFontPanel: (NSFontPanel *) fontPanel { + +- (NSUInteger) validModesForFontPanel: (NSFontPanel *) fontPanel +{ return (NSFontPanelStandardModesMask & ~NSFontPanelAllEffectsModeMask) | NSFontPanelUnderlineEffectModeMask | NSFontPanelStrikethroughEffectModeMask; } -- (void)windowDidOrderOffScreen:(NSNotification *)notification { + +- (void) windowDidOrderOffScreen: (NSNotification *) notification +{ #ifdef TK_MAC_DEBUG_NOTIFICATIONS TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification); #endif @@ -1159,35 +1197,35 @@ FontchooserEvent( } fcdPtr = Tcl_GetAssocData(fontchooserInterp, "::tk::fontchooser", NULL); switch (kind) { - case FontchooserClosed: - if (fcdPtr->parent != None) { - TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserVisibility"); - fontchooserInterp = NULL; - } - break; - case FontchooserSelection: - fontObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes( - fontPanelFont, fontPanelFontAttributes); - if (fontObj) { - if (fcdPtr->cmdObj) { - int objc, result; - Tcl_Obj **objv, **tmpv; - - result = Tcl_ListObjGetElements(fontchooserInterp, - fcdPtr->cmdObj, &objc, &objv); - if (result == TCL_OK) { - tmpv = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * - (unsigned)(objc + 2)); - memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc); - tmpv[objc] = fontObj; - TkBackgroundEvalObjv(fontchooserInterp, objc + 1, tmpv, - TCL_EVAL_GLOBAL); - ckfree((char *)tmpv); - } + case FontchooserClosed: + if (fcdPtr->parent != None) { + TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserVisibility"); + fontchooserInterp = NULL; + } + break; + case FontchooserSelection: + fontObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes( + fontPanelFont, fontPanelFontAttributes); + if (fontObj) { + if (fcdPtr->cmdObj) { + int objc, result; + Tcl_Obj **objv, **tmpv; + + result = Tcl_ListObjGetElements(fontchooserInterp, + fcdPtr->cmdObj, &objc, &objv); + if (result == TCL_OK) { + tmpv = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * + (unsigned)(objc + 2)); + memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc); + tmpv[objc] = fontObj; + TkBackgroundEvalObjv(fontchooserInterp, objc + 1, tmpv, + TCL_EVAL_GLOBAL); + ckfree((char *)tmpv); } - TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserFontChanged"); } - break; + TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserFontChanged"); + } + break; } } @@ -1217,47 +1255,41 @@ FontchooserCget( Tcl_Obj *resObj = NULL; switch(optionIndex) { - case FontchooserParent: { - if (fcdPtr->parent != None) { - resObj = Tcl_NewStringObj( - ((TkWindow*)fcdPtr->parent)->pathName, -1); - } else { - resObj = Tcl_NewStringObj(".", 1); - } - break; - } - case FontchooserTitle: { - if (fcdPtr->titleObj) { - resObj = fcdPtr->titleObj; - } else { - resObj = Tcl_NewObj(); - } - break; - } - case FontchooserFont: { - resObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes( - fontPanelFont, fontPanelFontAttributes); - if (!resObj) { - resObj = Tcl_NewObj(); - } - break; + case FontchooserParent: + if (fcdPtr->parent != None) { + resObj = Tcl_NewStringObj( + ((TkWindow *) fcdPtr->parent)->pathName, -1); + } else { + resObj = Tcl_NewStringObj(".", 1); } - case FontchooserCmd: { - if (fcdPtr->cmdObj) { - resObj = fcdPtr->cmdObj; - } else { - resObj = Tcl_NewObj(); - } - break; + break; + case FontchooserTitle: + if (fcdPtr->titleObj) { + resObj = fcdPtr->titleObj; + } else { + resObj = Tcl_NewObj(); } - case FontchooserVisible: { - resObj = Tcl_NewBooleanObj([[[NSFontManager sharedFontManager] - fontPanel:NO] isVisible]); - break; + break; + case FontchooserFont: + resObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes( + fontPanelFont, fontPanelFontAttributes); + if (!resObj) { + resObj = Tcl_NewObj(); } - default: { + break; + case FontchooserCmd: + if (fcdPtr->cmdObj) { + resObj = fcdPtr->cmdObj; + } else { resObj = Tcl_NewObj(); } + break; + case FontchooserVisible: + resObj = Tcl_NewBooleanObj([[[NSFontManager sharedFontManager] + fontPanel:NO] isVisible]); + break; + default: + resObj = Tcl_NewObj(); } return resObj; } @@ -1298,6 +1330,7 @@ FontchooserConfigureCmd( if (objc == 1) { Tcl_Obj *keyObj, *valueObj; Tcl_Obj *dictObj = Tcl_NewDictObj(); + for (i = 0; r == TCL_OK && fontchooserOptionStrings[i] != NULL; ++i) { keyObj = Tcl_NewStringObj(fontchooserOptionStrings[i], -1); valueObj = FontchooserCget(fcdPtr, i); @@ -1311,31 +1344,36 @@ FontchooserConfigureCmd( for (i = 1; i < objc; i += 2) { int optionIndex, len; + if (Tcl_GetIndexFromObj(interp, objv[i], fontchooserOptionStrings, "option", 0, &optionIndex) != TCL_OK) { return TCL_ERROR; } if (objc == 2) { - /* With one option and no arg, return the current value */ + /* + * With one option and no arg, return the current value. + */ + Tcl_SetObjResult(interp, FontchooserCget(fcdPtr, optionIndex)); return TCL_OK; } if (i + 1 == objc) { Tcl_AppendResult(interp, "value for \"", - Tcl_GetString(objv[i]), "\" missing", NULL); + Tcl_GetString(objv[i]), "\" missing", NULL); return TCL_ERROR; } switch (optionIndex) { case FontchooserVisible: { const char *msg = "cannot change read-only option " - "\"-visible\": use the show or hide command"; + "\"-visible\": use the show or hide command"; Tcl_SetObjResult(interp, Tcl_NewStringObj(msg, sizeof(msg)-1)); return TCL_ERROR; } case FontchooserParent: { Tk_Window parent = Tk_NameToWindow(interp, - Tcl_GetString(objv[i+1]), tkwin); + Tcl_GetString(objv[i+1]), tkwin); + if (parent == None) { return TCL_ERROR; } @@ -1367,26 +1405,28 @@ FontchooserConfigureCmd( Tcl_GetStringFromObj(objv[i+1], &len); if (len) { Tk_Font f = Tk_AllocFontFromObj(interp, tkwin, objv[i+1]); - if (f) { - [fontPanelFont autorelease]; - fontPanelFont = [TkMacOSXNSFontForFont(f) retain]; - [fontPanelFontAttributes setDictionary: - TkMacOSXNSFontAttributesForFont(f)]; - [fontPanelFontAttributes removeObjectsForKeys:[NSArray - arrayWithObjects:NSFontAttributeName, - NSLigatureAttributeName, NSKernAttributeName, - nil]]; - Tk_FreeFont(f); - } else { + + if (!f) { return TCL_ERROR; } + [fontPanelFont autorelease]; + fontPanelFont = [TkMacOSXNSFontForFont(f) retain]; + [fontPanelFontAttributes setDictionary: + TkMacOSXNSFontAttributesForFont(f)]; + [fontPanelFontAttributes removeObjectsForKeys:[NSArray + arrayWithObjects:NSFontAttributeName, + NSLigatureAttributeName, NSKernAttributeName, + nil]]; + Tk_FreeFont(f); } else { [fontPanelFont release]; fontPanelFont = nil; [fontPanelFontAttributes removeAllObjects]; } + NSFontManager *fm = [NSFontManager sharedFontManager]; NSFontPanel *fp = [fm fontPanel:NO]; + [fp setPanelFont:fontPanelFont isMultiple:NO]; [fm setSelectedFont:fontPanelFont isMultiple:NO]; [fm setSelectedAttributes:fontPanelFontAttributes @@ -1499,8 +1539,8 @@ FontchooserHideCmd( * * FontchooserParentEventHandler -- * - * Event handler for StructureNotify events on the font chooser's - * parent window. + * Event handler for StructureNotify events on the font chooser's parent + * window. * * Results: * None. @@ -1531,8 +1571,8 @@ FontchooserParentEventHandler( * * DeleteFontchooserData -- * - * Clean up the font chooser configuration data when the interp - * is destroyed. + * Clean up the font chooser configuration data when the interp is + * destroyed. * * Results: * None. @@ -1601,7 +1641,7 @@ TkInitFontchooser( /* * Local Variables: - * mode: c + * mode: objc * c-basic-offset: 4 * fill-column: 79 * coding: utf-8 diff --git a/macosx/tkMacOSXMenu.c b/macosx/tkMacOSXMenu.c index 1f59f71..8fd7136 100644 --- a/macosx/tkMacOSXMenu.c +++ b/macosx/tkMacOSXMenu.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkMacOSXMenu.c,v 1.52 2009/06/29 14:35:01 das Exp $ + * RCS: @(#) $Id: tkMacOSXMenu.c,v 1.53 2009/07/06 15:45:54 dkf Exp $ */ #include "tkMacOSXPrivate.h" @@ -32,6 +32,7 @@ #define ENTRY_WINDOWS_MENU ENTRY_PLATFORM_FLAG3 #define sl(s) ((int) (sizeof(s "") - 1)) + #define SPECIALMENU(n, f) {.name = "." #n, .len = sl(#n) + 1, \ .flag = ENTRY_##f##_MENU } static const struct { @@ -43,6 +44,7 @@ static const struct { {NULL} }; #undef SPECIALMENU + #define MODIFIER(n, f) {.name = #n, .len = sl(#n), .mask = f } static const struct { const char *name; const size_t len; const NSUInteger mask; @@ -59,6 +61,7 @@ static const struct { {NULL} }; #undef MODIFIER + #define ACCEL(n, c) {.name = #n, .len = sl(#n), .ch = c } static const struct { const char *name; const size_t len; const UniChar ch; @@ -108,10 +111,10 @@ static int ModifierCharWidth(Tk_Font tkfont); #pragma mark TKMenu @interface TKMenu(TKMenuPrivate) -- (id)initWithTkMenu:(TkMenu *)tkMenu; -- (TkMenu *)tkMenu; -- (int)tkIndexOfItem:(NSMenuItem *)menuItem; -- (void)insertItem:(NSMenuItem *)newItem atTkIndex:(NSInteger)index; +- (id) initWithTkMenu: (TkMenu *) tkMenu; +- (TkMenu *) tkMenu; +- (int) tkIndexOfItem: (NSMenuItem *) menuItem; +- (void) insertItem: (NSMenuItem *) newItem atTkIndex: (NSInteger) index; @end #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 @@ -123,17 +126,21 @@ static int ModifierCharWidth(Tk_Font tkfont); @end @implementation TKMenu -- (void)setSpecial:(NSUInteger)special { +- (void) setSpecial: (NSUInteger) special +{ NSAssert(!_tkSpecial, @"Cannot change specialness of a special menu"); _tkSpecial = special; } -- (BOOL)isSpecial:(NSUInteger)special { +- (BOOL) isSpecial: (NSUInteger) special +{ return (_tkSpecial == special); } @end @implementation TKMenu(TKMenuPrivate) -- (id)initWithTitle:(NSString *)aTitle { + +- (id) initWithTitle: (NSString *) aTitle +{ self = [super initWithTitle:aTitle]; if (self) { _tkMenu = NULL; @@ -144,9 +151,12 @@ static int ModifierCharWidth(Tk_Font tkfont); } return self; } -- (id)initWithTkMenu:(TkMenu *)tkMenu { + +- (id) initWithTkMenu: (TkMenu *) tkMenu +{ NSString *title = [[NSString alloc] initWithUTF8String: Tk_PathName(tkMenu->tkwin)]; + self = [self initWithTitle:title]; [title release]; if (self) { @@ -154,8 +164,11 @@ static int ModifierCharWidth(Tk_Font tkfont); } return self; } -- (id)copyWithZone:(NSZone *)zone { + +- (id) copyWithZone: (NSZone *) zone +{ TKMenu *copy = [super copyWithZone:zone]; + NSAssert(_tkMenu == nil, @"Cannot copy tkMenu"); copy->_tkMenu = _tkMenu; copy->_tkOffset = _tkOffset; @@ -163,17 +176,25 @@ static int ModifierCharWidth(Tk_Font tkfont); copy->_tkSpecial = _tkSpecial; return copy; } -- (TkMenu *)tkMenu { + +- (TkMenu *) tkMenu +{ return _tkMenu; } -- (int)tkIndexOfItem:(NSMenuItem *)menuItem { + +- (int) tkIndexOfItem: (NSMenuItem *) menuItem +{ return [self indexOfItem:menuItem] - _tkOffset; } -- (void)insertItem:(NSMenuItem *)newItem atTkIndex:(NSInteger)index { + +- (void) insertItem: (NSMenuItem *) newItem atTkIndex: (NSInteger) index +{ [super insertItem:newItem atIndex:index + _tkOffset]; _tkItemCount++; } -- (void)insertItem:(NSMenuItem *)newItem atIndex:(NSInteger)index { + +- (void) insertItem: (NSMenuItem *) newItem atIndex: (NSInteger) index +{ if (_tkMenu && index >= 0) { if ((NSUInteger)index <= _tkOffset) { _tkOffset++; @@ -184,7 +205,9 @@ static int ModifierCharWidth(Tk_Font tkfont); } [super insertItem:newItem atIndex:index]; } -- (void)removeItemAtIndex:(NSInteger)index { + +- (void) removeItemAtIndex: (NSInteger) index +{ if (_tkMenu && index >= 0) { if ((NSUInteger)index < _tkOffset) { _tkOffset--; @@ -194,9 +217,12 @@ static int ModifierCharWidth(Tk_Font tkfont); } [super removeItemAtIndex:index]; } -- (NSMenuItem *)newTkMenuItem:(TkMenuEntry *)mePtr { + +- (NSMenuItem *) newTkMenuItem: (TkMenuEntry *) mePtr +{ NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"" - action:@selector(tkMenuItemInvoke:) keyEquivalent:@""]; + action:@selector(tkMenuItemInvoke:) keyEquivalent:@""]; + [menuItem setTarget:self]; [menuItem setTag:(NSInteger)mePtr]; return menuItem; @@ -205,25 +231,34 @@ static int ModifierCharWidth(Tk_Font tkfont); @implementation TKMenu(TKMenuActions) // target methods -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { + +- (BOOL) validateMenuItem: (NSMenuItem *) menuItem +{ return [menuItem isEnabled]; } -- (void)tkMenuItemInvoke:(id)sender { + +- (void) tkMenuItemInvoke: (id) sender +{ /* * With the delegate matching key equivalents, when a menu action is sent * in response to a key equivalent, sender is the whole menu and not the - * the specific menu item, use this to ignore key equivalents for our menus - * (as Tk handles them directly via bindings). + * the specific menu item, use this to ignore key equivalents for our + * menus (as Tk handles them directly via bindings). */ + if ([sender isKindOfClass:[NSMenuItem class]]) { NSMenuItem *menuItem = (NSMenuItem *)sender; TkMenu *menuPtr = (TkMenu *)_tkMenu; TkMenuEntry *mePtr = (TkMenuEntry *)[menuItem tag]; + if (menuPtr && mePtr) { Tcl_Interp *interp = menuPtr->interp; + Tcl_Preserve(interp); Tcl_Preserve(menuPtr); + int result = TkInvokeMenu(interp, menuPtr, mePtr->index); + if (result != TCL_OK && result != TCL_CONTINUE && result != TCL_BREAK) { Tcl_AddErrorInfo(interp, "\n (menu invoke)"); @@ -243,20 +278,26 @@ static int ModifierCharWidth(Tk_Font tkfont); ((km) & NSControlKeyMask) != ((m) & NSControlKeyMask) || \ (((km) & NSShiftKeyMask) != ((m) & NSShiftKeyMask) && \ ((m) & NSFunctionKeyMask))) ? NO : YES) -- (BOOL)menuHasKeyEquivalent:(NSMenu*)menu forEvent:(NSEvent*)event - target:(id*)target action:(SEL*)action { + +- (BOOL) menuHasKeyEquivalent: (NSMenu *) menu forEvent: (NSEvent *) event + target: (id *) target action: (SEL *) action +{ NSString *key = [event charactersIgnoringModifiers]; NSUInteger modifiers = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask; + if (modifiers == (NSCommandKeyMask | NSShiftKeyMask) && [key compare:@"?"] == NSOrderedSame) { return NO; } + NSArray *itemArray = [self itemArray]; + for (NSMenuItem *item in itemArray) { if ([item isEnabled] && [[item keyEquivalent] compare:key] == NSOrderedSame) { NSUInteger keyEquivModifiers = [item keyEquivalentModifierMask]; + if (keyEquivModifiersMatch(keyEquivModifiers, modifiers)) { *target = [item target]; *action = [item action]; @@ -266,31 +307,43 @@ static int ModifierCharWidth(Tk_Font tkfont); } return NO; } -- (void)menuWillOpen:(NSMenu *)menu { + +- (void) menuWillOpen: (NSMenu *) menu +{ if (_tkMenu) { //RecursivelyClearActiveMenu(_tkMenu); GenerateMenuSelectEvent((TKMenu *)[self supermenu], [self itemInSupermenu]); } } -- (void)menuDidClose:(NSMenu *)menu { + +- (void) menuDidClose: (NSMenu *) menu +{ if (_tkMenu) { RecursivelyClearActiveMenu(_tkMenu); } } -- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item { + +- (void) menu: (NSMenu *) menu willHighlightItem: (NSMenuItem *) item +{ if (_tkMenu) { GenerateMenuSelectEvent(self, item); } } -- (void)menuNeedsUpdate:(NSMenu*)menu { - TkMenu *menuPtr = (TkMenu *)_tkMenu; + +- (void) menuNeedsUpdate: (NSMenu *) menu +{ + TkMenu *menuPtr = (TkMenu *) _tkMenu; + if (menuPtr) { Tcl_Interp *interp = menuPtr->interp; + Tcl_Preserve(interp); Tcl_Preserve(menuPtr); + int result = TkPostCommand(_tkMenu); - if (result != TCL_OK && result != TCL_CONTINUE && result != TCL_BREAK) { + + if (result!=TCL_OK && result!=TCL_CONTINUE && result!=TCL_BREAK) { Tcl_AddErrorInfo(interp, "\n (menu preprocess)"); Tcl_BackgroundException(interp, result); } @@ -303,18 +356,22 @@ static int ModifierCharWidth(Tk_Font tkfont); #pragma mark TKApplication(TKMenu) @interface NSApplication(TKMenu) -- (void)setAppleMenu:(NSMenu *)menu; +- (void) setAppleMenu: (NSMenu *) menu; @end @implementation TKApplication(TKMenu) -- (void)menuBeginTracking:(NSNotification *)notification { + +- (void) menuBeginTracking: (NSNotification *) notification +{ #ifdef TK_MAC_DEBUG_NOTIFICATIONS TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification); #endif //TkMacOSXClearMenubarActive(); //TkMacOSXPreprocessMenu(); } -- (void)menuEndTracking:(NSNotification *)notification { + +- (void) menuEndTracking: (NSNotification *) notification +{ #ifdef TK_MAC_DEBUG_NOTIFICATIONS TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification); #endif @@ -322,16 +379,22 @@ static int ModifierCharWidth(Tk_Font tkfont); TkMacOSXClearMenubarActive(); } } -- (void)tkSetMainMenu:(TKMenu *)menu { + +- (void) tkSetMainMenu: (TKMenu *) menu +{ if (gNoTkMenus) { return; } + TKMenu *applicationMenu = nil; + if (menu) { NSMenuItem *applicationMenuItem = [menu numberOfItems] ? [menu itemAtIndex:0] : nil; + if (![menu isSpecial:tkMainMenu]) { TkMenuEntry *mePtr = (TkMenuEntry *)[applicationMenuItem tag]; + if (!mePtr || !(mePtr->entryFlags & ENTRY_APPLE_MENU)) { applicationMenuItem = [NSMenuItem itemWithSubmenu: [[_defaultApplicationMenu copy] autorelease]]; @@ -346,14 +409,18 @@ static int ModifierCharWidth(Tk_Font tkfont); } [applicationMenu setSpecial:tkApplicationMenu]; } + NSArray *itemArray = [menu itemArray]; + for (NSMenuItem *item in itemArray) { TkMenuEntry *mePtr = (TkMenuEntry *)[item tag]; TKMenu *submenu = (TKMenu *)[item submenu]; + if (mePtr && submenu) { if ((mePtr->entryFlags & ENTRY_WINDOWS_MENU) && ![submenu isSpecial:tkWindowsMenu]) { NSInteger index = 0; + for (NSMenuItem *i in _defaultWindowsMenuItems) { [submenu insertItem:[[i copy] autorelease] atIndex: index++]; @@ -363,6 +430,7 @@ static int ModifierCharWidth(Tk_Font tkfont); } else if ((mePtr->entryFlags & ENTRY_HELP_MENU) && ![submenu isSpecial:tkHelpMenu]) { NSInteger index = 0; + for (NSMenuItem *i in _defaultHelpMenuItems) { [submenu insertItem:[[i copy] autorelease] atIndex: index++]; @@ -375,7 +443,10 @@ static int ModifierCharWidth(Tk_Font tkfont); menu = _defaultMainMenu; applicationMenu = _defaultApplicationMenu; } - NSMenuItem *servicesMenuItem = [applicationMenu itemWithTitle:@"Services"]; + + NSMenuItem *servicesMenuItem = + [applicationMenu itemWithTitle:@"Services"]; + if (servicesMenuItem && [servicesMenuItem submenu] != _servicesMenu) { [[_servicesMenu itemInSupermenu] setSubmenu:nil]; [servicesMenuItem setSubmenu:_servicesMenu]; @@ -399,8 +470,8 @@ static int ModifierCharWidth(Tk_Font tkfont); * Returns a standard Tcl error. * * Side effects: - * Allocates a NSMenu and puts it into the platformData field - * of the menuPtr. + * Allocates a NSMenu and puts it into the platformData field of the + * menuPtr. * *---------------------------------------------------------------------- */ @@ -472,8 +543,12 @@ TkpMenuNewEntry( } mePtr->platformEntryData = (TkMenuPlatformEntryData) TkMacOSXMakeUncollectable(menuItem); - /* Caller TkMenuEntry() already did this same insertion into the generic - * TkMenu so we just match it for the platform menu. */ + + /* + * Caller TkMenuEntry() already did this same insertion into the generic + * TkMenu so we just match it for the platform menu. + */ + [menu insertItem:menuItem atTkIndex:mePtr->index]; return TCL_OK; } @@ -521,6 +596,7 @@ TkpConfigureMenuEntry( } else if (mePtr->bitmapPtr != None) { Pixmap bitmap = Tk_GetBitmapFromObj(mePtr->menuPtr->tkwin, mePtr->bitmapPtr); + Tk_SizeOfBitmap(mePtr->menuPtr->display, bitmap, &imageWidth, &imageHeight); image = TkMacOSXGetNSImageWithBitmap(mePtr->menuPtr->display, bitmap, @@ -543,8 +619,9 @@ TkpConfigureMenuEntry( attributes = TkMacOSXNSFontAttributesForFont(Tk_GetFontFromObj( mePtr->menuPtr->tkwin, fontPtr)); if (gc->foreground != defaultFg || gc->background != defaultBg) { - NSColor *color = TkMacOSXGetNSColor(gc, gc->foreground != defaultFg ? - gc->foreground : gc->background); + NSColor *color = TkMacOSXGetNSColor(gc, + gc->foreground!=defaultFg? gc->foreground:gc->background); + attributes = [[attributes mutableCopy] autorelease]; [(NSMutableDictionary *)attributes setObject:color forKey:NSForegroundColorAttributeName]; @@ -575,10 +652,12 @@ TkpConfigureMenuEntry( submenu = (TKMenu *) menuRefPtr->menuPtr->platformData; if ([submenu supermenu] && [menuItem submenu] != submenu) { /* - * This happens during a clone, where the parent menu is cloned - * before its children, so just ignore this temprary setting, - * it will be changed shortly (c.f. tkMenu.c CloneMenu()) - */ + * This happens during a clone, where the parent menu is + * cloned before its children, so just ignore this temprary + * setting, it will be changed shortly (c.f. tkMenu.c + * CloneMenu()) + */ + submenu = nil; } else { [submenu setTitle:title]; @@ -611,8 +690,9 @@ TkpDestroyMenuEntry( { if (mePtr->platformEntryData && mePtr->menuPtr->platformData) { TKMenu *menu = (TKMenu *) mePtr->menuPtr->platformData; - NSMenuItem *menuItem = (NSMenuItem *)mePtr->platformEntryData; + NSMenuItem *menuItem = (NSMenuItem *) mePtr->platformEntryData; NSInteger index = [menu indexOfItem:menuItem]; + if (index > -1) { [menu removeItemAtIndex:index]; } @@ -646,26 +726,30 @@ TkpPostMenu( int y) /* The global y-coordinate */ { NSWindow *win = [NSApp keyWindow]; - if (win) { - inPostMenu = 1; - int oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE); - NSView *view = [win contentView]; - NSRect frame = NSMakeRect(x + 9, tkMacOSXZeroScreenHeight - y - 9, 1, 1); - frame.origin = [view convertPoint: - [win convertScreenToBase:frame.origin] fromView:nil]; - NSMenu *menu = (NSMenu *) menuPtr->platformData; - NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] - initTextCell:@"" pullsDown:NO]; - [popUpButtonCell setMenu:menu]; - [popUpButtonCell selectItem:nil]; - [popUpButtonCell performClickWithFrame:frame inView:view]; - [popUpButtonCell release]; - Tcl_SetServiceMode(oldMode); - inPostMenu = 0; - return TCL_OK; - } else { + if (!win) { return TCL_ERROR; } + + inPostMenu = 1; + + int oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE); + NSView *view = [win contentView]; + NSRect frame = NSMakeRect(x + 9, tkMacOSXZeroScreenHeight - y - 9, 1, 1); + + frame.origin = [view convertPoint: + [win convertScreenToBase:frame.origin] fromView:nil]; + + NSMenu *menu = (NSMenu *) menuPtr->platformData; + NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] + initTextCell:@"" pullsDown:NO]; + + [popUpButtonCell setMenu:menu]; + [popUpButtonCell selectItem:nil]; + [popUpButtonCell performClickWithFrame:frame inView:view]; + [popUpButtonCell release]; + Tcl_SetServiceMode(oldMode); + inPostMenu = 0; + return TCL_OK; } /* @@ -723,8 +807,10 @@ TkpSetMainMenubar( { static Tcl_Interp *currentInterp = NULL; TKMenu *menu = nil; + if (menuName) { TkWindow *winPtr = (TkWindow *) tkwin; + if (winPtr->wmInfoPtr && winPtr->wmInfoPtr->menuPtr && winPtr->wmInfoPtr->menuPtr->masterMenuPtr && winPtr->wmInfoPtr->menuPtr->masterMenuPtr->tkwin && @@ -734,6 +820,7 @@ TkpSetMainMenubar( } else { TkMenuReferences *menuRefPtr = TkFindMenuReferences(interp, menuName); + if (menuRefPtr && menuRefPtr->menuPtr && menuRefPtr->menuPtr->platformData) { menu = (TKMenu *) menuRefPtr->menuPtr->platformData; @@ -751,9 +838,9 @@ TkpSetMainMenubar( * * CheckForSpecialMenu -- * - * Given a menu, check to see whether or not it is a cascade in - * a menubar with one of the special names .apple, .help or .window - * If it is, the entry that points to this menu will be marked. + * Given a menu, check to see whether or not it is a cascade in a menubar + * with one of the special names .apple, .help or .window If it is, the + * entry that points to this menu will be marked. * * Results: * None. @@ -772,7 +859,8 @@ CheckForSpecialMenu( return; } for (TkMenuEntry *cascadeEntryPtr = menuPtr->menuRefPtr->parentEntryPtr; - cascadeEntryPtr; cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr) { + cascadeEntryPtr; + cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr) { if (cascadeEntryPtr->menuPtr->menuType == MENUBAR && cascadeEntryPtr->menuPtr->masterMenuPtr->tkwin) { TkMenu *masterMenuPtr = cascadeEntryPtr->menuPtr->masterMenuPtr; @@ -815,7 +903,7 @@ CheckForSpecialMenu( *---------------------------------------------------------------------- */ -static NSString* +static NSString * ParseAccelerator( const char *accel, NSUInteger *maskPtr) @@ -893,12 +981,14 @@ ParseAccelerator( */ static int -ModifierCharWidth(Tk_Font tkfont) +ModifierCharWidth( + Tk_Font tkfont) { static NSString *cmdChar = nil; if (!cmdChar) { unichar cmd = kCommandUnicode; + cmdChar = [[NSString alloc] initWithCharacters:&cmd length:1]; } return [cmdChar sizeWithAttributes: @@ -1023,6 +1113,7 @@ TkpComputeStandardMenuGeometry( } else if (mePtr->bitmapPtr) { Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr); + Tk_SizeOfBitmap(menuPtr->display, bitmap, &width, &height); haveImage = 1; } @@ -1145,6 +1236,7 @@ GenerateMenuSelectEvent( if (menuPtr) { int index = [menu tkIndexOfItem:menuItem]; + if (index < 0 || index >= menuPtr->numEntries || (menuPtr->entries[index])->state == ENTRY_DISABLED) { TkActivateMenuEntry(menuPtr, -1); @@ -1222,16 +1314,15 @@ RecursivelyClearActiveMenu( TkMenu *menuPtr) /* The menu to reset. */ { int i; - TkMenuEntry *mePtr; TkActivateMenuEntry(menuPtr, -1); for (i = 0; i < menuPtr->numEntries; i++) { - mePtr = menuPtr->entries[i]; - if (mePtr->type == CASCADE_ENTRY) { - if ((mePtr->childMenuRefPtr != NULL) - && (mePtr->childMenuRefPtr->menuPtr != NULL)) { - RecursivelyClearActiveMenu(mePtr->childMenuRefPtr->menuPtr); - } + TkMenuEntry *mePtr = menuPtr->entries[i]; + + if (mePtr->type == CASCADE_ENTRY + && (mePtr->childMenuRefPtr != NULL) + && (mePtr->childMenuRefPtr->menuPtr != NULL)) { + RecursivelyClearActiveMenu(mePtr->childMenuRefPtr->menuPtr); } } } @@ -1256,8 +1347,10 @@ void TkMacOSXClearMenubarActive(void) { NSMenu *mainMenu = [NSApp mainMenu]; + if (mainMenu && [mainMenu isKindOfClass:[TKMenu class]]) { - TkMenu *menuPtr = [(TKMenu *)mainMenu tkMenu]; + TkMenu *menuPtr = [(TKMenu *) mainMenu tkMenu]; + if (menuPtr && menuPtr->numEntries && menuPtr->entries) { RecursivelyClearActiveMenu(menuPtr); } @@ -1310,7 +1403,9 @@ TkpMenuInit(void) TkColor *tkColPtr; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; -#define observe(n, s) [nc addObserver:NSApp selector:@selector(s) name:(n) object:nil] + +#define observe(n, s) \ + [nc addObserver:NSApp selector:@selector(s) name:(n) object:nil] observe(NSMenuDidBeginTrackingNotification, menuBeginTracking:); observe(NSMenuDidEndTrackingNotification, menuEndTracking:); #undef observe @@ -1476,9 +1571,9 @@ TkpDrawMenuEntry( int width, /* Width of the entry rectangle */ int height, /* Height of the current rectangle */ int strictMotif, /* Boolean flag */ - int drawArrow) /* Whether or not to draw the cascade - * arrow for cascade items. Only applies - * to Windows. */ + int drawArrow) /* Whether or not to draw the cascade arrow + * for cascade items. Only applies to + * Windows. */ { } @@ -1630,7 +1725,7 @@ TkMacOSXMenuClick(void) /* * Local Variables: - * mode: c + * mode: objc * c-basic-offset: 4 * fill-column: 79 * coding: utf-8 |