Skip to content

Commit

Permalink
Fix for MainMenu.cib needing to be loaded manually in cib apps.
Browse files Browse the repository at this point in the history
Closes #9.

Reviewed by me.
  • Loading branch information
Francisco Tolmasky committed May 3, 2009
1 parent 586a1e3 commit 390cf2c
Show file tree
Hide file tree
Showing 12 changed files with 3,228 additions and 292 deletions.
2 changes: 2 additions & 0 deletions AppKit/AppKit.j
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@import "CPApplication.j"
@import "CPButton.j"
@import "CPButtonBar.j"
@import "CPCib.j"
@import "CPCibLoading.j"
@import "CPClipView.j"
@import "CPCollectionView.j"
@import "CPColor.j"
Expand Down
100 changes: 76 additions & 24 deletions AppKit/CPApplication.j
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
@import "CPResponder.j"
@import "CPDocumentController.j"

var CPMainCibFile = @"CPMainCibFile",
CPMainCibFileHumanFriendly = @"Main cib file base name";

CPApp = nil;

Expand Down Expand Up @@ -739,44 +741,27 @@ var _CPRunModalLoop = function(anEvent)
@class CPApplication
@return void
*/
@implementation X : CPObject
{
}
- (void)blendDidFinishLoading:(CPBundle)aBundle
{
[CPTheme setDefaultTheme:[CPTheme themeNamed:@"Aristo"]];
_CPApplicationMain()
}
@end
function CPApplicationMain(args, namedArgs)
{
var blend = [[CPBlend alloc] initWithContentsOfURL:[[CPBundle bundleForClass:[CPApplication class]] pathForResource:@"Aristo.blend"]];

[blend loadWithDelegate:[X new]];
}

function _CPApplicationMain(args, namedArgs)
function CPApplicationMain(args, namedArgs)
{
var mainBundle = [CPBundle mainBundle],
principalClass = [mainBundle principalClass];

if (!principalClass)
principalClass = [CPApplication class];

[principalClass sharedApplication];

//[NSBundle loadNibNamed:@"myMain" owner:NSApp];


//FIXME?
if (!args && !namedArgs)
{
var args = [CPApp arguments],
searchParams = window.location.search.substring(1).split("&");
namedArgs = [CPDictionary dictionary];

if([args containsObject:"debug"])
CPLogRegister(CPLogPopup);

for(var i=0; i<searchParams.length; i++)
{
var index = searchParams[i].indexOf('=');
Expand All @@ -786,9 +771,76 @@ function _CPApplicationMain(args, namedArgs)
[namedArgs setObject: searchParams[i].substring(index+1) forKey: searchParams[i].substring(0, index)];
}
}

CPApp._args = args;
CPApp._namedArgs = namedArgs;


[_CPAppBootstrapper performActions];
}

var _CPAppBootstrapperActions = nil;

@implementation _CPAppBootstrapper : CPObject
{
}

+ (void)actions
{
return [@selector(loadDefaultTheme), @selector(loadMainCibFile)];
}

+ (void)performActions
{
if (!_CPAppBootstrapperActions)
_CPAppBootstrapperActions = [self actions];

while (_CPAppBootstrapperActions.length)
{
var action = _CPAppBootstrapperActions.shift();

if (objj_msgSend(self, action))
return;
}

[CPApp run];
}

+ (BOOL)loadDefaultTheme
{
var blend = [[CPBlend alloc] initWithContentsOfURL:[[CPBundle bundleForClass:[CPApplication class]] pathForResource:@"Aristo.blend"]];

[blend loadWithDelegate:self];

return YES;
}

+ (void)blendDidFinishLoading:(CPBundle)aBundle
{
[CPTheme setDefaultTheme:[CPTheme themeNamed:@"Aristo"]];

[self performActions];
}

+ (BOOL)loadMainCibFile
{
var mainBundle = [CPBundle mainBundle],
mainCibFile = [mainBundle objectForInfoDictionaryKey:CPMainCibFile] || [mainBundle objectForInfoDictionaryKey:CPMainCibFileHumanFriendly];

if (mainCibFile)
{
[CPBundle loadCibFile:[mainBundle pathForResource:mainCibFile]
externalNameTable:[CPDictionary dictionaryWithObject:CPApp forKey:CPCibOwner]
loadDelegate:self];

return YES;
}

return NO;
}

+ (void)cibDidFinishLoading:(CPCib)aCib
{
[self performActions];
}

@end
69 changes: 57 additions & 12 deletions AppKit/Cib/CPCib.j
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,47 @@ var CPCibObjectDataKey = @"CPCibObjectDataKey";
CPData _data;
CPBundle _bundle;
BOOL _awakenCustomResources;

id _loadDelegate;
}

- (id)initWithContentsOfURL:(CPURL)aURL
{
self = [super init];

if (self)
{
_data = [CPURLConnection sendSynchronousRequest:[CPURLRequest requestWithURL:aURL] returningResponse:nil error:nil];
_awakenCustomResources = YES;
}

return self;
}

- (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle
- (id)initWithContentsOfURL:(CPURL)aURL loadDelegate:(id)aLoadDelegate
{
self = [self initWithContentsOfURL:aName];
self = [super init];

if (self)
{
_bundle = aBundle;
[CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:aURL] delegate:self];

_awakenCustomResources = YES;

_loadDelegate = aLoadDelegate;
}


return self;
}

- (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle loadDelegate:(id)aLoadDelegate
{
// If aBundle is nil, use mainBundle, but ONLY for searching for the nib, not for resources later.
self = [self initWithContentsOfURL:[aBundle || [CPBundle mainBundle] pathForResource:aName] loadDelegate:aLoadDelegate];

if (self)
_bundle = aBundle;

return self;
}

Expand All @@ -82,7 +99,13 @@ var CPCibObjectDataKey = @"CPCibObjectDataKey";

- (BOOL)instantiateCibWithExternalNameTable:(CPDictionary)anExternalNameTable
{
var unarchiver = [[_CPCibKeyedUnarchiver alloc] initForReadingWithData:_data bundle:_bundle awakenCustomResources:_awakenCustomResources],
var bundle = _bundle,
owner = [anExternalNameTable objectForKey:CPCibOwner];

if (!bundle && owner)
bundle = [CPBundle bundleForClass:[owner class]];

var unarchiver = [[_CPCibKeyedUnarchiver alloc] initForReadingWithData:_data bundle:bundle awakenCustomResources:_awakenCustomResources],
replacementClasses = [anExternalNameTable objectForKey:CPCibReplacementClasses];

if (replacementClasses)
Expand All @@ -99,8 +122,7 @@ var CPCibObjectDataKey = @"CPCibObjectDataKey";
if (!objectData || ![objectData isKindOfClass:[_CPCibObjectData class]])
return NO;

var owner = [anExternalNameTable objectForKey:CPCibOwner],
topLevelObjects = [anExternalNameTable objectForKey:CPCibTopLevelObjects];
var topLevelObjects = [anExternalNameTable objectForKey:CPCibTopLevelObjects];

[objectData instantiateWithOwner:owner topLevelObjects:topLevelObjects]
[objectData establishConnectionsWithOwner:owner topLevelObjects:topLevelObjects];
Expand All @@ -122,8 +144,31 @@ var CPCibObjectDataKey = @"CPCibObjectDataKey";

- (BOOL)instantiateCibWithOwner:(id)anOwner topLevelObjects:(CPArray)topLevelObjects
{
[CPDictionary dictionaryWithObjectsAndKeys:anOwner, CPCibOwner, topLevelObjects, CPCibTopLevelObjects];
return [self instantiate];
return [self instantiateCibWithExternalNameTable:[CPDictionary dictionaryWithObjectsAndKeys:anOwner, CPCibOwner, topLevelObjects, CPCibTopLevelObjects]];
}

@end

@implementation CPCib (CPURLConnectionDelegate)

- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
{
_data = [CPData dataWithString:data];

if ([_loadDelegate respondsToSelector:@selector(cibDidFinishLoading:)])
[_loadDelegate cibDidFinishLoading:self];
}

- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
{
alert("cib: connection failed.");

_loadDelegate = nil;
}

- (void)connectionDidFinishLoading:(CPURLConnection)aConnection
{
_loadDelegate = nil;
}

@end
76 changes: 76 additions & 0 deletions AppKit/Cib/CPCibLoading.j
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* _CPCibLoading.j
* AppKit
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

@import <Foundation/CPBundle.j>
@import <Foundation/CPDictionary.j>
@import <Foundation/CPString.j>


var CPCibOwner = @"CPCibOwner";

var LoadInfoForCib = {};

@implementation CPObject (CPCibLoading)

- (void)awakeFromCib
{
}

@end

@implementation CPBundle (CPCibLoading)

+ (void)loadCibFile:(CPString)anAbsolutePath externalNameTable:(CPDictionary)aNameTable loadDelegate:aDelegate
{
var cib = [[CPCib alloc] initWithContentsOfURL:anAbsolutePath loadDelegate:self];

LoadInfoForCib[[cib hash]] = { loadDelegate:aDelegate, externalNameTable:aNameTable };
}

+ (void)loadCibNamed:(CPString)aName owner:(id)anOwner loadDelegate:(id)aDelegate
{
// Path is based solely on anOwner:
var bundle = anOwner ? [CPBundle bundleForClass:[anOwner class]] : [CPBundle mainBundle],
path = [bundle pathForResource:aName];

[self loadCibFile:path externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner] loadDelegate:aDelegate];
}

- (void)loadCibFile:(CPString)aFileName externalNameTable:(CPDictionary)aNameTable loadDelegate:(id)aDelegate
{
var cib = [[CPCib alloc] initWithCibNamed:aFileName bundle:self loadDelegate:[self class]];

LoadInfoForCib[[cib hash]] = { loadDelegate:aDelegate, externalNameTable:aNameTable };
}

+ (void)cibDidFinishLoading:(CPCib)aCib
{
var loadInfo = LoadInfoForCib[[aCib hash]];

delete LoadInfoForCib[[aCib hash]];

[aCib instantiateCibWithExternalNameTable:loadInfo.externalNameTable];

[loadInfo.loadDelegate cibDidFinishLoading:aCib];
}

@end
2 changes: 1 addition & 1 deletion AppKit/Cib/_CPCibKeyedUnarchiver.j
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
return _objects[aUID];
}

@end
@end

0 comments on commit 390cf2c

Please sign in to comment.