CPUndoManager -added support for setActionName to enable the undo/redo menu to automatically update with the value passed as the action name. Test file included.

This commit is contained in:
Blair Duncan
2010-09-18 20:30:43 -07:00
committed by Francisco Ryan Tolmasky I
parent b1b0cea27e
commit 212feff636
9 changed files with 600 additions and 6 deletions
+93 -6
View File
@@ -47,6 +47,7 @@ var _CPUndoGroupingPool = [],
{
_CPUndoGrouping _parent;
CPMutableArray _invocations;
CPString _actionName;
}
+ (void)_poolUndoGrouping:(_CPUndoGrouping)anUndoGrouping
@@ -82,6 +83,7 @@ var _CPUndoGroupingPool = [],
{
_parent = anUndoGrouping;
_invocations = [];
_actionName = @"";
}
return self;
@@ -124,10 +126,21 @@ var _CPUndoGroupingPool = [],
[_invocations[index] invoke];
}
- (void)setActionName:(CPString)aName
{
_actionName = aName;
}
- (CPString)actionName
{
return _actionName;
}
@end
var _CPUndoGroupingParentKey = @"_CPUndoGroupingParentKey",
_CPUndoGroupingInvocationsKey = @"_CPUndoGroupingInvocationsKey";
_CPUndoGroupingInvocationsKey = @"_CPUndoGroupingInvocationsKey"
_CPUndoGroupingActionNameKey = @"_CPUndoGroupingActionNameKey";
@implementation _CPUndoGrouping (CPCoder)
@@ -139,6 +152,7 @@ var _CPUndoGroupingParentKey = @"_CPUndoGroupingParentKey",
{
_parent = [aCoder decodeObjectForKey:_CPUndoGroupingParentKey];
_invocations = [aCoder decodeObjectForKey:_CPUndoGroupingInvocationsKey];
_actionName = [aCoder decodeObjectForKey:_CPUndoGroupingActionNameKey];
}
return self;
@@ -148,6 +162,7 @@ var _CPUndoGroupingParentKey = @"_CPUndoGroupingParentKey",
{
[aCoder encodeObject:_parent forKey:_CPUndoGroupingParentKey];
[aCoder encodeObject:_invocations forKey:_CPUndoGroupingInvocationsKey];
[aCoder encodeObject:_actionName forKey:_CPUndoGroupingActionNameKey];
}
@end
@@ -177,7 +192,6 @@ var _CPUndoGroupingParentKey = @"_CPUndoGroupingParentKey",
int _levelsOfUndo;
id _currentGrouping;
int _state;
CPString _actionName;
id _preparedTarget;
id _undoManagerProxy;
@@ -349,7 +363,8 @@ if (_currentGroup == nil)
[defaultCenter postNotificationName:CPUndoManagerWillUndoChangeNotification
object:self];
var undoGrouping = _undoStack.pop();
var name = [[_undoStack lastObject] actionName];
undoGrouping = _undoStack.pop();
_state = CPUndoManagerUndoing;
@@ -361,6 +376,8 @@ if (_currentGroup == nil)
_state = CPUndoManagerNormal;
[[_redoStack lastObject] setActionName:name];
[defaultCenter postNotificationName:CPUndoManagerDidUndoChangeNotification
object:self];
}
@@ -388,6 +405,7 @@ if (_currentGroup == nil)
object:self];
var oldUndoGrouping = _currentGrouping,
name = [[_redoStack lastObject] actionName],
undoGrouping = _redoStack.pop();
_currentGrouping = nil;
@@ -402,6 +420,7 @@ if (_currentGroup == nil)
_currentGrouping = oldUndoGrouping;
_state = CPUndoManagerNormal;
[[_undoStack lastObject] setActionName:name];
[defaultCenter postNotificationName:CPUndoManagerDidRedoChangeNotification object:self];
}
@@ -620,7 +639,8 @@ if (_currentGroup == nil)
*/
- (void)setActionName:(CPString)anActionName
{
_actionName = anActionName;
if ((anActionName != nil) && (_currentGrouping != nil))
[_currentGrouping setActionName:anActionName];
}
/*!
@@ -631,9 +651,40 @@ if (_currentGroup == nil)
*/
- (CPString)redoActionName
{
return [self canRedo] ? _actionName : nil;
if ([self canRedo] == NO)
{
return nil;
}
return [[_redoStack lastObject] actionName];
}
/*!
Returns the full localized title of the actions to be displayed
as a menu item. This method first invokes [-redoActionName] and
passes it to [-redoMenuTitleForUndoActionName:] and returns the result.
*/
- (CPString)redoMenuItemTitle
{
return [self redoMenuTitleForUndoActionName:[self redoActionName]];
}
/*!
Returns the localized title of the actions to be displayed
as a menu item identified by actionName, by appending a
localized command string like @"Redo <localized(actionName)>".
*/
- (CPString)redoMenuTitleForUndoActionName:(CPString)actionName
{
//FIXME: The terms @"Redo" and @"Redo %@" should be localized.
if (actionName && ![actionName isEqual: @""])
return [CPString stringWithFormat: @"Redo %@", actionName];
return @"Redo";
}
/*!
If the receiver can perform an undo, this method returns the action
name previously associated with the top grouping with
@@ -642,9 +693,45 @@ if (_currentGroup == nil)
*/
- (CPString)undoActionName
{
return [self canUndo] ? _actionName : nil;
if ([self canUndo] == NO)
return nil;
return [[_undoStack lastObject] actionName];
}
/*!
Returns the full localized title of the actions to be displayed
as a menu item. This method first invokes [-undoActionName] and
passes it to [-undoMenuTitleForUndoActionName:] and returns the result.
*/
- (CPString)undoMenuItemTitle
{
return [self undoMenuTitleForUndoActionName:[self undoActionName]];
}
/*!
Returns the localized title of the actions to be displayed
as a menu item identified by actionName, by appending a
localized command string like @"Undo <localized(actionName)>".
*/
- (CPString)undoMenuTitleForUndoActionName:(CPString)actionName
{
// FIXME: The terms @"Undo" and @"Undo %@" should be localized.
if (actionName && ![actionName isEqual:@""])
return [CPString stringWithFormat: @"Undo %@", actionName];
return @"Undo";
}
// Working With Run Loops
/*!
Returns the CPRunLoopModes in which the receiver registers
@@ -0,0 +1,246 @@
@implementation AppController : CPObject
{
CPTextField label;
CPSlider theSlider;
CPUndoManager undoManager @accessors;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[label setStringValue:@"Hello World!"];
[label setFont:[CPFont boldSystemFontOfSize:24.0]];
[label sizeToFit];
[label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[label setCenter:[contentView center]];
[contentView addSubview:label];
theSlider = [[CPSlider alloc] initWithFrame:CGRectMake(100,100,180,24)];
[theSlider setMinValue:36];
[theSlider setMaxValue:238];
[theSlider setObjectValue:([theSlider minValue] + [theSlider maxValue])/2];
[theSlider setTarget:self];
[theSlider setAction:@selector(doSlider:)];
[contentView addSubview:theSlider];
[theWindow orderFront:self];
// Uncomment the following line to turn on the standard menu bar.
[CPMenu setMenuBarVisible:YES];
// setup the menus to manage
undoManager = [[CPUndoManager alloc] init];
var editmenuItem = [[[CPApplication sharedApplication] mainMenu] itemWithTitle: @"Edit"];
[[editmenuItem submenu] setAutoenablesItems:YES];
var undoMenuItem = [[editmenuItem submenu] itemWithTitle:@"Undo"],
[undoMenuItem setTag:@"Undo"],
[undoMenuItem setTarget:self];
var redoMenuItem = [[editmenuItem submenu] itemWithTitle:@"Redo"];
[redoMenuItem setTag:@"Redo"],
[redoMenuItem setTarget:self];
}
- (void)doSlider:sender
{
[self setSliderValue:[sender objectValue]];
}
- (void)setSliderValue:aValue
{
[[undoManager prepareWithInvocationTarget:self] setSliderValue:[label objectValue]];
[theSlider setObjectValue:aValue];
[undoManager setActionName:@"slideto: " + aValue];
[label setObjectValue:aValue];
[label sizeToFit];
}
- (void)undo:(id)sender
{
[undoManager undo];
[[sender _menuItemView] highlight:NO];
}
- (void)redo:(id)sender
{
[undoManager redo];
[[sender _menuItemView] highlight:NO];
}
//this will be called whenver the user clicks on an autoenabled menu
- (BOOL)validateMenuItem:(id)anItem
{
switch([anItem tag])
{
case @"Undo" :
[anItem setTitle:[undoManager undoMenuItemTitle]];
return([undoManager canUndo]);
break;
case @"Redo" :
[anItem setTitle:[undoManager redoMenuItemTitle]];
return([undoManager canRedo]);
break;
default:
return [anItem isEnabled];
}
}
@end
////////////// autoenablesItems from here http://github.com/farcaller/cappuccino/commit/5d1af7192d2afe5b938c5bcdd197f15c79811b7f
////////////// Currently waiting to be pulled into master
@implementation CPMenu(autoenablesItems)
- (void)update
{
var cnt = [_items count];
for(var i=0; i<cnt; ++i)
{
var item = [_items objectAtIndex:i];
if([[item target] respondsToSelector:@selector(validateMenuItem:)])
[item _setEnabledByMenu:[[item target] validateMenuItem:item]];
else
[item _setEnabledByMenu:[item isEnabled]]; /// BD Change from NO
}
}
@end
@implementation CPMenuItem(autoenablesItems)
// a private function to toggle availability as part of update check
- (void)_setEnabledByMenu:(BOOL)isEnabled
{
if (![_menu autoenablesItems])
return;
_isEnabled = isEnabled;
[_menuItemView setDirty];
}
// Performs autoenable processing, returns isEnabled to propagate state to NativeHost
- (BOOL)_performAutoenable
{
if ([_menu autoenablesItems])
{
if (_target)
{
if ([_target respondsToSelector:@selector(validateMenuItem:)])
{
var isEnabled = [_target validateMenuItem:self];
_isEnabled = isEnabled;
[_menuItemView setDirty];
return _isEnabled;
}
else
return YES; // enabled by default
}
else
{
// XXX: will toggle state as part of _target check, required to sync stane with NativeHost
_isEnabled = isEnabled;
[_menuItemView setDirty];
return NO;
}
}
else
{
// XXX: should it return NO, if target is nil?..
return [self isEnabled];
}
}
@end
@implementation _CPMenuManager(autoenablesItems)
- (void)showMenu:(CPMenu)newMenu fromMenu:(CPMenu)baseMenu atPoint:(CGPoint)aGlobalLocation
{
var count = _menuContainerStack.length,
index = count;
// Hide all menus up to the base menu...
while (index--)
{
var menuContainer = _menuContainerStack[index],
menu = [menuContainer menu];
// If we reach the base menu, or this menu is already being shown, break.
if (menu === baseMenu)
break;
// If this menu is already being shown, unhighlight and return.
if (menu === newMenu)//&& [menu supermenu] === baseMenu)
{
[newMenu _highlightItemAtIndex:CPNotFound];
return;
}
[menuContainer orderOut:self];
[menuContainer setMenu:nil];
[_CPMenuWindow poolMenuWindow:menuContainer];
[_menuContainerStack removeObjectAtIndex:index];
}
if (!newMenu)
return;
if([newMenu autoenablesItems])
[newMenu update];
// Unhighlight any previously highlighted item.
[newMenu _highlightItemAtIndex:CPNotFound];
var menuWindow = [_CPMenuWindow menuWindowWithMenu:newMenu font:[_menuContainerStack[0] font]];
[_menuContainerStack addObject:menuWindow];
[menuWindow setConstraintRect:_constraintRect];
// If our parent menu is a menu bar...
if (baseMenu === [self trackingMenu] && [[self trackingMenuContainer] isMenuBar])
[menuWindow setBackgroundStyle:_CPMenuWindowMenuBarBackgroundStyle];
else
[menuWindow setBackgroundStyle:_CPMenuWindowPopUpBackgroundStyle];
[menuWindow setFrameOrigin:aGlobalLocation];
[menuWindow orderFront:self];
}
@end
//////////////////////////// end of autoenablesItems
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Main cib file base name</key>
<string>MainMenu.cib</string>
<key>CPBundleName</key>
<string>iApplication</string>
<key>CPPrincipalClass</key>
<string>CPApplication</string>
</dict>
</plist>
@@ -0,0 +1,93 @@
/*
* Jakefile
* UndoRedoWithMenuUpdate
*
* Created by C. Blair Duncan on September 15, 2010.
* Copyright 2010, BBDO All rights reserved.
*/
var ENV = require("system").env,
FILE = require("file"),
JAKE = require("jake"),
task = JAKE.task,
FileList = JAKE.FileList,
app = require("cappuccino/jake").app,
configuration = ENV["CONFIG"] || ENV["CONFIGURATION"] || ENV["c"] || "Debug",
OS = require("os");
app ("UndoRedoWithMenuUpdate", function(task)
{
task.setBuildIntermediatesPath(FILE.join("Build", "UndoRedoWithMenuUpdate.build", configuration));
task.setBuildPath(FILE.join("Build", configuration));
task.setProductName("UndoRedoWithMenuUpdate");
task.setIdentifier("com.yourcompany.UndoRedoWithMenuUpdate");
task.setVersion("1.0");
task.setAuthor("BBDO");
task.setEmail("feedback @nospam@ yourcompany.com");
task.setSummary("UndoRedoWithMenuUpdate");
task.setSources((new FileList("**/*.j")).exclude(FILE.join("Build", "**")));
task.setResources(new FileList("Resources/**"));
task.setIndexFilePath("index.html");
task.setInfoPlistPath("Info.plist");
if (configuration === "Debug")
task.setCompilerFlags("-DDEBUG -g");
else
task.setCompilerFlags("-O");
});
task ("default", ["UndoRedoWithMenuUpdate"], function()
{
printResults(configuration);
});
task ("build", ["default"]);
task ("debug", function()
{
ENV["CONFIGURATION"] = "Debug";
JAKE.subjake(["."], "build", ENV);
});
task ("release", function()
{
ENV["CONFIGURATION"] = "Release";
JAKE.subjake(["."], "build", ENV);
});
task ("run", ["debug"], function()
{
OS.system(["open", FILE.join("Build", "Debug", "UndoRedoWithMenuUpdate", "index.html")]);
});
task ("run-release", ["release"], function()
{
OS.system(["open", FILE.join("Build", "Release", "UndoRedoWithMenuUpdate", "index.html")]);
});
task ("deploy", ["release"], function()
{
FILE.mkdirs(FILE.join("Build", "Deployment", "UndoRedoWithMenuUpdate"));
OS.system(["press", "-f", FILE.join("Build", "Release", "UndoRedoWithMenuUpdate"), FILE.join("Build", "Deployment", "UndoRedoWithMenuUpdate")]);
printResults("Deployment")
});
task ("desktop", ["release"], function()
{
FILE.mkdirs(FILE.join("Build", "Desktop", "UndoRedoWithMenuUpdate"));
require("cappuccino/nativehost").buildNativeHost(FILE.join("Build", "Release", "UndoRedoWithMenuUpdate"), FILE.join("Build", "Desktop", "UndoRedoWithMenuUpdate", "UndoRedoWithMenuUpdate.app"));
printResults("Desktop")
});
task ("run-desktop", ["desktop"], function()
{
OS.system([FILE.join("Build", "Desktop", "UndoRedoWithMenuUpdate", "UndoRedoWithMenuUpdate.app", "Contents", "MacOS", "NativeHost"), "-i"]);
});
function printResults(configuration)
{
print("----------------------------");
print(configuration+" app built at path: "+FILE.join("Build", configuration, "UndoRedoWithMenuUpdate"));
print("----------------------------");
}
File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
<!--
index-debug.html
iApplication
Created by C. Blair Duncan on May 20, 2010.
Copyright 2010, BBDO All rights reserved.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>iApplication</title>
<script type = "text/javascript">
OBJJ_MAIN_FILE = "main.j";
OBJJ_INCLUDE_PATHS = ["Frameworks/Debug", "Frameworks", "SomethingElse"];
</script>
<script src = "Frameworks/Debug/Objective-J/Objective-J.js" type = "text/javascript"></script>
<style type = "text/css">
body{margin:0; padding:0;}
#container {position: absolute; top:50%; left:50%;}
#content {width:800px; text-align:center; margin-left: -400px; height:50px; margin-top:-25px; line-height: 50px;}
#content {font-family: "Helvetica", "Arial", sans-serif; font-size: 18px; color: black; text-shadow: 0px 1px 0px white; }
#loadgraphic {margin-right: 0.2em; margin-bottom:-2px;}
</style>
<!--[if lt IE 7]>
<STYLE type="text/css">
#container { position: relative; top: 50%; }
#content { position: relative;}
</STYLE>
<![endif]-->
</head>
<body style="">
<div id="loadingcontainer" style=" background-color: #eeeeee; overflow:hidden; width:100%; height:100%; position: absolute; top: 0; left: 0;">
<script type = "text/javascript">
document.write("<div id='container'><p id='content'>" +
"<img id='loadgraphic' width='16' height='16' src='Resources/spinner.gif' /> " +
"Loading iApplication...</p></div>");
</script>
<noscript>
<div id="container">
<div style="width: 440px; padding: 10px 25px 20px 25px; font-family: sans-serif; background-color: #ffffff; position: relative; left: -245px; top: -120px; text-align: center; -moz-border-radius: 20px; -webkit-border-radius: 20px; color: #555555">
<p style="line-height: 1.4em;">JavaScript is required for this site to work correctly but is either disabled or not supported by your browser.</p>
<p style="font-size:120%; padding:10px;"><a href="http://cappuccino.org/noscript">Show me how to enable JavaScript</a></p>
<p style="font-size:80%;">You may want to upgrade to a newer browser while you're at it:</p>
<ul style="margin:0;padding:0; text-align: center; font-size:80%;" >
<li style="display: inline;"><a href="http://www.apple.com/safari/download/">Safari</a></li>
<li style="display: inline;"><a href="http://www.mozilla.com/en-US/firefox/">Firefox</a></li>
<li style="display: inline;"><a href="http://www.google.com/chrome/">Chrome</a></li>
<li style="display: inline;"><a href="http://www.opera.com/download/">Opera</a></li>
<li style="display: inline;"><a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a></li>
</ul>
</div>
</div>
</noscript>
</div>
</body>
</html>
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
<!--
index.html
iApplication
Created by C. Blair Duncan on May 20, 2010.
Copyright 2010, BBDO All rights reserved.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>iApplication</title>
<script type = "text/javascript">
OBJJ_MAIN_FILE = "main.j";
</script>
<script src = "Frameworks/Objective-J/Objective-J.js" type = "text/javascript"></script>
<style type = "text/css">
body{margin:0; padding:0;}
#container {position: absolute; top:50%; left:50%;}
#content {width:800px; text-align:center; margin-left: -400px; height:50px; margin-top:-25px; line-height: 50px;}
#content {font-family: "Helvetica", "Arial", sans-serif; font-size: 18px; color: black; text-shadow: 0px 1px 0px white; }
#loadgraphic {margin-right: 0.2em; margin-bottom:-2px;}
</style>
<!--[if lt IE 7]>
<STYLE type="text/css">
#container { position: relative; top: 50%; }
#content { position: relative;}
</STYLE>
<![endif]-->
</head>
<body style="">
<div id="loadingcontainer" style=" background-color: #eeeeee; overflow:hidden; width:100%; height:100%; position: absolute; top: 0; left: 0;">
<script type = "text/javascript">
document.write("<div id='container'><p id='content'>" +
"<img id='loadgraphic' width='16' height='16' src='Resources/spinner.gif' /> " +
"Loading iApplication...</p></div>");
</script>
<noscript>
<div id="container">
<div style="width: 440px; padding: 10px 25px 20px 25px; font-family: sans-serif; background-color: #ffffff; position: relative; left: -245px; top: -120px; text-align: center; -moz-border-radius: 20px; -webkit-border-radius: 20px; color: #555555">
<p style="line-height: 1.4em;">JavaScript is required for this site to work correctly but is either disabled or not supported by your browser.</p>
<p style="font-size:120%; padding:10px;"><a href="http://cappuccino.org/noscript">Show me how to enable JavaScript</a></p>
<p style="font-size:80%;">You may want to upgrade to a newer browser while you're at it:</p>
<ul style="margin:0;padding:0; text-align: center; font-size:80%;" >
<li style="display: inline;"><a href="http://www.apple.com/safari/download/">Safari</a></li>
<li style="display: inline;"><a href="http://www.mozilla.com/en-US/firefox/">Firefox</a></li>
<li style="display: inline;"><a href="http://www.google.com/chrome/">Chrome</a></li>
<li style="display: inline;"><a href="http://www.opera.com/download/">Opera</a></li>
<li style="display: inline;"><a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a></li>
</ul>
</div>
</div>
</noscript>
</div>
</body>
</html>
@@ -0,0 +1,18 @@
/*
* AppController.j
* iApplication
*
* Created by C. Blair Duncan on May 20, 2010.
* Copyright 2010, BBDO All rights reserved.
*/
@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import "AppController.j"
function main(args, namedArgs)
{
CPApplicationMain(args, namedArgs);
}