Add new files for handling storyboards, still doesn't fully function

This commit is contained in:
Gregory John Casamento 2024-05-13 12:26:34 -04:00
parent 82dce67eb4
commit 423c9c1982
10 changed files with 508 additions and 11 deletions

View file

@ -48,6 +48,8 @@ APPKIT_EXPORT_CLASS
+ (BOOL) checkXib5: (NSData *)data;
+ (BOOL) checkStoryboard: (NSData *)data;
+ (NSKeyedUnarchiver *) unarchiverForReadingWithData: (NSData *)data;
- (void) _initCommon;

View file

@ -33,7 +33,9 @@
extern "C" {
#endif
@class NSString, NSBundle, NSMutableDictionary;
@class NSBundle;
@class NSMutableDictionary;
@class NSString;
typedef NSString *NSStoryboardName;
typedef NSString *NSStoryboardSceneIdentifier;
@ -43,7 +45,7 @@ DEFINE_BLOCK_TYPE(NSStoryboardControllerCreator, NSCoder*, id);
APPKIT_EXPORT_CLASS
@interface NSStoryboard : NSObject
{
id _transform;
NSMutableDictionary *_scenes;
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_13, GS_API_LATEST)

View file

@ -307,7 +307,9 @@ GSDisplayServer.m \
GSHelpManagerPanel.m \
GSInfoPanel.m \
GSMemoryPanel.m \
GSScene.m \
GSSlideView.m \
GSStoryboardLoader.m \
GSTextStorage.m \
GSTrackingRect.m \
GSServicesManager.m \

View file

@ -50,7 +50,7 @@
+ (float) priority
{
return 0.0;
return FLT_MAX;
}
- (BOOL) loadModelData: (NSData *)data

60
Source/GSScene.h Normal file
View file

@ -0,0 +1,60 @@
/* Interface of class GSScene
Copyright (C) 2024 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: 12-05-2024
This file is part of the GNUstep Library.
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 USA.
*/
#ifndef _GSScene_h_GNUSTEP_GUI_INCLUDE
#define _GSScene_h_GNUSTEP_GUI_INCLUDE
#import <Foundation/NSObject.h>
#import <Foundation/NSGeometry.h>
#if defined(__cplusplus)
extern "C" {
#endif
@class NSArray;
@class NSString;
@interface GSScene : NSObject <NSCoding, NSCopying>
{
NSString *_sceneID;
NSArray *_objects;
NSPoint _canvasLocation;
}
- (NSString *) sceneID;
- (void) setSceneID: (NSString *)sceneID;
- (NSArray *) objects;
- (void) setObjects: (NSArray *)objects;
- (NSPoint) canvasLocation;
- (void) setCanvasLocation: (NSPoint)point;
@end
#if defined(__cplusplus)
}
#endif
#endif /* _GSScene_h_GNUSTEP_GUI_INCLUDE */

122
Source/GSScene.m Normal file
View file

@ -0,0 +1,122 @@
/* Implementation of class GSScene
Copyright (C) 2024 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: 12-05-2024
This file is part of the GNUstep Library.
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 USA.
*/
#import <Foundation/NSKeyedArchiver.h>
#import "GSScene.h"
@implementation GSScene
- (instancetype) init
{
self = [super init];
if (self != nil)
{
_sceneID = nil;
_objects = nil;
_canvasLocation = NSMakePoint(0.0, 0.0);
}
return self;
}
- (void) dealloc
{
RELEASE(_sceneID);
RELEASE(_objects);
[super dealloc];
}
- (NSString *) sceneID
{
return _sceneID;
}
- (void) setSceneID: (NSString *)sceneID
{
ASSIGN(_sceneID, sceneID);
}
- (NSArray *) objects
{
return _objects;
}
- (void) setObjects: (NSArray *)objects
{
ASSIGNCOPY(_objects, objects);
}
- (NSPoint) canvasLocation
{
return _canvasLocation;
}
- (void) setCanvasLocation: (NSPoint)point
{
_canvasLocation = point;
}
// NSCoding
- (instancetype) initWithCoder: (NSCoder *)coder
{
if ([coder allowsKeyedCoding])
{
if ([coder containsValueForKey: @"NSSceneID"])
{
[self setSceneID: [coder decodeObjectForKey: @"NSSceneID"]];
}
if ([coder containsValueForKey: @"NSObjects"])
{
[self setObjects: [coder decodeObjectForKey: @"NSObjects"]];
}
if ([coder containsValueForKey: @"NSCanvasLocation"])
{
[self setCanvasLocation: [coder decodePointForKey: @"NSCanvasLocation"]];
}
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
}
// NSCopying
- (id) copyWithZone: (NSZone *)zone
{
GSScene *scene = [[GSScene allocWithZone: zone] init];
[scene setSceneID: [self sceneID]];
[scene setObjects: [self objects]];
[scene setCanvasLocation: [self canvasLocation]];
return scene;
}
@end

249
Source/GSStoryboardLoader.m Normal file
View file

@ -0,0 +1,249 @@
/* <title>GSXibLoader</title>
<abstract>Xib (Cocoa XML) model loader</abstract>
Copyright (C) 2010, 2011 Free Software Foundation, Inc.
Written by: Fred Kiefer <FredKiefer@gmx.de>
Created: March 2010
This file is part of the GNUstep Base Library.
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 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., 59 Temple Place, Suite 330, Boston, MA 02110 USA.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSMenu.h"
#import "AppKit/NSNib.h"
#import "GNUstepGUI/GSModelLoaderFactory.h"
#import "GNUstepGUI/GSNibLoading.h"
#import "GNUstepGUI/GSXibLoading.h"
#import "GNUstepGUI/GSXibKeyedUnarchiver.h"
#import <float.h>
@interface NSApplication (StorybardCompatibility)
- (void) _setMainMenu: (NSMenu*)aMenu;
@end
@interface NSMenu (StoryboardCompatibility)
- (BOOL) _isMainMenu;
@end
@implementation NSMenu (StoryboardCompatibility)
- (BOOL) _isMainMenu
{
if (_name)
return [_name isEqualToString:@"_NSMainMenu"];
return NO;
}
@end
@interface GSStoryboardLoader: GSModelLoader
{
}
@end
@implementation GSStoryboardLoader
+ (BOOL) canReadData: (NSData *)theData
{
char *header = calloc(1024, sizeof(char));
if (header != NULL)
{
[theData getBytes: header
length: 1024];
NSString *hdr = [[NSString alloc] initWithBytes: header
length: 1024
encoding: NSUTF8StringEncoding];
AUTORELEASE(hdr);
if ([hdr containsString: @"Cocoa.Storyboard.XIB"])
{
free(header);
return YES;
}
free(header);
}
return NO;
}
+ (NSString*) type
{
return @"storyboard";
}
+ (float) priority
{
return 5.0;
}
- (void) awake: (NSArray *)rootObjects
withContext: (NSDictionary *)context
{
NSMutableArray *topLevelObjects = [context objectForKey: NSNibTopLevelObjects];
id owner = [context objectForKey: NSNibOwner];
NSEnumerator *en;
id obj;
NSUInteger index = 0;
if ([rootObjects count] == 0)
{
NSWarnMLog(@"No root objects in XIB!");
return;
}
NSDebugLLog(@"XIB", @"First object %@", [rootObjects objectAtIndex: 0]);
NSDebugLLog(@"XIB", @"Second object %@", [rootObjects objectAtIndex: 1]);
NSDebugLLog(@"XIB", @"Third object %@", [rootObjects objectAtIndex: 2]);
// Use the owner as first root object
[(NSCustomObject*)[rootObjects objectAtIndex: 0] setRealObject: owner];
en = [rootObjects objectEnumerator];
while ((obj = [en nextObject]) != nil)
{
index++;
if ([obj respondsToSelector: @selector(nibInstantiate)])
{
obj = [obj nibInstantiate];
}
// IGNORE file's owner, first responder and NSApplication instances...
if ((obj != nil) && (index > 3))
{
[topLevelObjects addObject: obj];
// All top level objects must be released by the caller to avoid
// leaking, unless they are going to be released by other nib
// objects on behalf of the owner.
RETAIN(obj);
}
if (([obj isKindOfClass: [NSMenu class]]) &&
([obj _isMainMenu]))
{
// add the menu...
[NSApp _setMainMenu: obj];
}
}
}
- (void) awake: (NSArray *)rootObjects
inContainer: (id)objects
withContext: (NSDictionary *)context
{
[self awake: rootObjects withContext: context];
// Load connections and awaken objects
if ([objects respondsToSelector: @selector(nibInstantiate)])
{
[objects nibInstantiate];
}
}
- (BOOL) loadModelData: (NSData *)data
externalNameTable: (NSDictionary *)context
withZone: (NSZone *)zone
{
BOOL loaded = NO;
NS_DURING
{
if (data != nil)
{
NSKeyedUnarchiver *unarchiver = [GSXibKeyedUnarchiver unarchiverForReadingWithData: data];
if (unarchiver != nil)
{
IBObjectContainer *objects;
NSDebugLLog(@"XIB", @"Invoking unarchiver");
[unarchiver setObjectZone: zone];
// rootObjects = [unarchiver decodeObjectForKey: @"IBDocument.RootObjects"];
objects = [unarchiver decodeObjectForKey: @"IBDocument.Objects"];
NSLog(@"objects %@", objects);
/*
[self awake: rootObjects
inContainer: objects
withContext: context];
*/
loaded = YES;
}
else
{
NSLog(@"Could not instantiate Xib unarchiver/Unable to parse Storyboard.");
}
}
else
{
NSLog(@"Data passed to Storyboard loading method is nil.");
}
}
NS_HANDLER
{
NSLog(@"Exception occurred while loading model: %@",[localException reason]);
}
NS_ENDHANDLER
if (loaded == NO)
{
NSLog(@"Failed to load Storyboard\n");
}
return loaded;
}
- (NSData*) dataForFile: (NSString*)fileName
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDir = NO;
NSDebugLLog(@"XIB", @"Loading Storyboard `%@'...\n", fileName);
if ([mgr fileExistsAtPath: fileName isDirectory: &isDir])
{
if (isDir == NO)
{
return [NSData dataWithContentsOfFile: fileName];
}
else
{
NSLog(@"Storyboard file specified %@, is directory.", fileName);
}
}
else
{
NSLog(@"Storyboard file specified %@, could not be found.", fileName);
}
return nil;
}
@end

View file

@ -68,6 +68,7 @@
#import "AppKit/NSTabView.h"
#import "AppKit/NSToolbarItem.h"
#import "AppKit/NSView.h"
#import "GSCodingFlags.h"
#define DEBUG_XIB5 0
@ -229,6 +230,7 @@ static NSArray *XmlBoolDefaultYes = nil;
@"NSStackViewContainer", @"beginningViews",
@"NSStackViewContainer", @"middleViews",
@"NSStackViewContainer", @"endViews",
@"GSScene", @"scene",
nil];
RETAIN(XmlTagToObjectClassMap);

View file

@ -68,11 +68,41 @@
#endif
}
+ (BOOL) checkStoryboard: (NSData *)data
{
#if GNUSTEP_BASE_HAVE_LIBXML
// Ensure we have a storyboard...first see if we can parse the XML...
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData: data
options: 0
error: NULL];
if (document == nil)
{
return NO;
}
else
{
// Test to see if this is an Xcode 5 XIB...
NSArray *nodes = [document nodesForXPath: @"/scene" error: NULL];
// Need at LEAST ONE scene node...we should find something a bit more
// specific to check here...
return [nodes count] != 0;
}
#else
// We now default to checking XIB 5 versions
return YES;
#endif
}
+ (NSKeyedUnarchiver *) unarchiverForReadingWithData: (NSData *)data
{
NSKeyedUnarchiver *unarchiver = nil;
if ([self checkXib5: data])
if ([self checkStoryboard: data])
{
unarchiver = [[GSXib5KeyedUnarchiver alloc] initForReadingWithData: data];
}
else if ([self checkXib5: data])
{
unarchiver = [[GSXib5KeyedUnarchiver alloc] initForReadingWithData: data];
}
@ -80,6 +110,7 @@
{
unarchiver = [[GSXibKeyedUnarchiver alloc] initForReadingWithData: data];
}
return AUTORELEASE(unarchiver);
}

View file

@ -35,6 +35,7 @@
#import "AppKit/NSApplication.h"
#import "AppKit/NSNib.h"
#import "AppKit/NSNibLoading.h"
#import "AppKit/NSStoryboard.h"
#import "AppKit/NSWindowController.h"
#import "AppKit/NSViewController.h"
@ -42,7 +43,7 @@
#import "AppKit/NSNibDeclarations.h"
#import "GNUstepGUI/GSModelLoaderFactory.h"
#import "GSStoryboardTransform.h"
#import "GNUstepGUI/GSXibKeyedUnarchiver.h"
#import "GSFastEnumeration.h"
static NSStoryboard *__mainStoryboard = nil;
@ -110,10 +111,32 @@ static NSStoryboard *__mainStoryboard = nil;
self = [super init];
if (self != nil)
{
NSString *path = [bundle pathForResource: name
ofType: @"storyboard"];
NSData *data = [NSData dataWithContentsOfFile: path];
_transform = [[GSStoryboardTransform alloc] initWithData: data];
BOOL success = NO;
success = [bundle loadNibFile: name
externalNameTable: nil
withZone: NSDefaultMallocZone()];
if (success)
{
}
/*
if (unarchiver != nil)
{
NSArray *rootObjects;
IBObjectContainer *objects;
NSDebugLLog(@"XIB", @"Invoking unarchiver");
[unarchiver setObjectZone: zone];
rootObjects = [unarchiver decodeObjectForKey: @"IBDocument.RootObjects"];
objects = [unarchiver decodeObjectForKey: @"IBDocument.Objects"];
NSDebugLLog(@"XIB", @"rootObjects %@", rootObjects);
[self awake: rootObjects
inContainer: objects
withContext: context];
loaded = YES;
}
*/
}
return self;
}
@ -142,7 +165,7 @@ static NSStoryboard *__mainStoryboard = nil;
// Instance methods...
- (void) dealloc
{
RELEASE(_transform);
RELEASE(_scenes);
[super dealloc];
}
@ -158,7 +181,7 @@ static NSStoryboard *__mainStoryboard = nil;
- (id) instantiateInitialControllerWithCreator: (NSStoryboardControllerCreator)block
{
return [self instantiateControllerWithIdentifier: [_transform initialViewControllerId]
return [self instantiateControllerWithIdentifier: @"" // [_transform initialViewControllerId]
creator: block];
}
@ -172,6 +195,8 @@ static NSStoryboard *__mainStoryboard = nil;
creator: (NSStoryboardControllerCreator)block
{
id result = nil;
/*
NSMutableArray *topLevelObjects = [NSMutableArray arrayWithCapacity: 5];
NSDictionary *table = [NSDictionary dictionaryWithObjectsAndKeys: topLevelObjects,
NSNibTopLevelObjects,
@ -260,6 +285,8 @@ static NSStoryboard *__mainStoryboard = nil;
{
CALL_BLOCK(block, self);
}
*/
return result;
}
@end