Skip to content

Instantly share code, notes, and snippets.

@nickjs
Created July 31, 2009 21:03
Show Gist options
  • Save nickjs/159450 to your computer and use it in GitHub Desktop.
Save nickjs/159450 to your computer and use it in GitHub Desktop.
Monkey Patches
CPMenuItem.j - Adds a designated initializer.
CPTableView.j - Fix a bug in -columnWithIdentifier:
CPWindowController.j - Fixes to make it work with programmatic windows.
diff --git a/AppKit/CPMenuItem.j b/AppKit/CPMenuItem.j
index b738729..c71f259 100644
--- a/AppKit/CPMenuItem.j
+++ b/AppKit/CPMenuItem.j
@@ -78,6 +78,27 @@
_CPMenuItemView _menuItemView;
}
+- (id)init
+{
+ self = [super init];
+
+ if (self)
+ {
+ _isSeparator = NO;
+
+ _isEnabled = YES;
+
+ _tag = 0;
+ _state = CPOffState;
+
+ _mnemonicLocation = CPNotFound;
+
+ _keyEquivalent = @"";
+ }
+
+ return self;
+}
+
/*!
Initializes the menu item with a title, action, and keyboard equivalent.
@param aTitle the menu item's title
@@ -87,24 +108,15 @@
*/
- (id)initWithTitle:(CPString)aTitle action:(SEL)anAction keyEquivalent:(CPString)aKeyEquivalent
{
- self = [super init];
+ self = [self init];
if (self)
{
- _isSeparator = NO;
-
_title = aTitle;
_action = anAction;
- _isEnabled = YES;
-
- _tag = 0;
- _state = CPOffState;
-
_keyEquivalent = aKeyEquivalent || @"";
_keyEquivalentModifierMask = CPPlatformActionKeyMask;
-
- _mnemonicLocation = CPNotFound;
}
return self;
@@ -771,7 +783,7 @@ var CPMenuItemIsSeparatorKey = @"CPMenuItemIsSeparatorKey",
*/
- (id)initWithCoder:(CPCoder)aCoder
{
- self = [super init];
+ self = [self init];
if (self)
{
diff --git a/AppKit/CPTableView.j b/AppKit/CPTableView.j
index 7b1ebdb..b7f0908 100644
--- a/AppKit/CPTableView.j
+++ b/AppKit/CPTableView.j
@@ -530,7 +530,7 @@ CPTableViewSolidHorizontalGridLineMask = 1 << 1;
count = NUMBER_OF_COLUMNS();
for (; index < count; ++index)
- if ([_tableColumns identifier] === anIdentifier)
+ if ([_tableColumns[index] identifier] === anIdentifier)
return index;
return CPNotFound;
diff --git a/AppKit/CPWindowController.j b/AppKit/CPWindowController.j
index f60abcf..145e839 100644
--- a/AppKit/CPWindowController.j
+++ b/AppKit/CPWindowController.j
@@ -106,6 +106,9 @@
{
_cibOwner = anOwner;
_windowCibName = aWindowCibName;
+
+ if (anOwner == self)
+ _windowCibPath = [[CPBundle mainBundle] pathForResource:_windowCibName + @".cib"];
}
return self;
@@ -127,27 +130,25 @@
/*!
Loads the window
*/
-- (BOOL)loadWindow
+- (void)loadWindow
{
if ([self isWindowLoaded])
- return YES;
+ return;
- if (![self isWindowLoading])
+ if (_cibOwner && ![self isWindowLoading])
{
_isWindowLoading = YES;
-
- [self windowWillLoad];
-
+
[CPBundle loadCibFile:[self windowCibPath]
externalNameTable:[CPDictionary dictionaryWithObject:_cibOwner forKey:CPCibOwner]
loadDelegate:self];
}
-
- return NO;
}
- (void)cibDidFinishLoading:(CPCib)aCib
{
+ _isWindowLoading = NO;
+
if (_window === nil && _document !== nil && _cibOwner === _document)
[self setWindow:[_document valueForKey:@"window"]];
@@ -165,14 +166,10 @@
*/
- (@action)showWindow:(id)aSender
{
- if (![self loadWindow])
- {
- _shouldDisplayWindowWhenLoaded = YES;
-
- return;
- }
-
var theWindow = [self window];
+
+ if ([self isWindowLoading])
+ return _shouldDisplayWindowWhenLoaded = YES;
if ([theWindow respondsToSelector:@selector(becomesKeyOnlyIfNeeded)] && [theWindow becomesKeyOnlyIfNeeded])
[theWindow orderFront:aSender];
@@ -200,7 +197,13 @@
- (CPWindow)window
{
if (!_window)
- [self loadWindow];
+ {
+ [self windowWillLoad];
+ [self loadWindow];
+
+ if (_window)
+ [self windowDidLoad];
+ }
return _window;
}
@@ -221,6 +224,8 @@
/*!
The method notifies the controller that it's window has loaded.
+
+ Subclasses should invoke super.
*/
- (void)windowDidLoad
{
@@ -231,6 +236,8 @@
/*!
The method notifies the controller that it's window is about to load.
+
+ Subclasses should invoke super.
*/
- (void)windowWillLoad
{
@@ -353,10 +360,10 @@
- (CPString)windowCibPath
{
- if (_windowCibPath)
- return _windowCibPath;
-
- return [[CPBundle bundleForClass:[_cibOwner class]] pathForResource:_windowCibName + @".cib"];
+ if (!_windowCibPath)
+ _windowCibPath = [[CPBundle bundleForClass:[_cibOwner class]] pathForResource:_windowCibName + @".cib"];
+
+ return _windowCibPath;
}
// Setting and Getting Window Attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment