2002-02-02 11:48:54 +00:00
|
|
|
#import "HexWindowController.h"
|
2002-02-06 20:57:56 +00:00
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
@implementation HexWindowController
|
|
|
|
|
2002-02-06 20:57:56 +00:00
|
|
|
NSString *ResourceChangedNotification = @"ResourceChangedNotification";
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
// causes window controller to use HexTextViews wherever it would previously use NSTextView
|
|
|
|
[HexTextView poseAsClass:[NSTextView class]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithResource:(id)newResource
|
|
|
|
{
|
|
|
|
self = [self initWithWindowNibName:@"HexWindow"];
|
2002-02-11 22:03:20 +00:00
|
|
|
if( !self ) return self;
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// one instance of your principal class will be created for every resource the user wants to edit (similar to Windows apps)
|
2002-02-07 03:45:43 +00:00
|
|
|
resource = [newResource retain];
|
2002-02-02 11:48:54 +00:00
|
|
|
|
|
|
|
// load the window
|
2002-02-11 22:03:20 +00:00
|
|
|
[self setWindowFrameAutosaveName:@"Hexadecimal Editor"];
|
2002-02-02 11:48:54 +00:00
|
|
|
// [self setShouldCascadeWindows:YES];
|
|
|
|
[self window];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2002-02-06 20:57:56 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2002-02-07 03:45:43 +00:00
|
|
|
[resource autorelease];
|
2002-02-06 20:57:56 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
- (void)windowDidLoad
|
|
|
|
{
|
|
|
|
[super windowDidLoad];
|
|
|
|
|
2002-02-06 20:57:56 +00:00
|
|
|
// we don't want this notification until we have a window!
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resourceDidChange:) name:ResourceChangedNotification object:nil];
|
2002-02-02 11:48:54 +00:00
|
|
|
|
2002-02-11 22:03:20 +00:00
|
|
|
// put other notifications here too, just for togetherness
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:[[offset enclosingScrollView] contentView]];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:[[hex enclosingScrollView] contentView]];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:[[ascii enclosingScrollView] contentView]];
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
// insert the resources' data into the text fields
|
|
|
|
[self refreshData:[(id <ResKnifeResourceProtocol>)resource data]];
|
|
|
|
|
|
|
|
// finally, show the window
|
|
|
|
[self showWindow:self];
|
2002-02-06 20:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resourceDidChange:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
// see if it's our resource which got changed (we receive notifications for any resource being changed, allowing multi-resource editors)
|
|
|
|
if( [notification object] == resource )
|
|
|
|
[self refreshData:[(id <ResKnifeResourceProtocol>)resource data]];
|
|
|
|
}
|
2002-02-02 11:48:54 +00:00
|
|
|
|
2002-02-11 22:03:20 +00:00
|
|
|
- (void)viewDidScroll:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
// get object refs for increased speed
|
|
|
|
NSClipView *object = (NSClipView *) [notification object];
|
|
|
|
NSClipView *offsetClip = [[offset enclosingScrollView] contentView];
|
|
|
|
NSClipView *hexClip = [[hex enclosingScrollView] contentView];
|
|
|
|
NSClipView *asciiClip = [[ascii enclosingScrollView] contentView];
|
|
|
|
|
|
|
|
// due to a bug in -[NSView setPostsBoundsChangedNotifications:] (it basically doesn't work), I am having to work around it by removing myself from the notification center and restoring things later on!
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewBoundsDidChangeNotification object:nil];
|
|
|
|
|
|
|
|
// when a view scrolls, update the other two
|
|
|
|
if( object != offsetClip )
|
|
|
|
{
|
|
|
|
// [offsetClip setPostsBoundsChangedNotifications:NO];
|
|
|
|
[offsetClip setBoundsOrigin:[object bounds].origin];
|
|
|
|
// [offsetClip setPostsBoundsChangedNotifications:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( object != hexClip )
|
|
|
|
{
|
|
|
|
// [hexClip setPostsBoundsChangedNotifications:NO];
|
|
|
|
[hexClip setBoundsOrigin:[object bounds].origin];
|
|
|
|
// [hexClip setPostsBoundsChangedNotifications:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( object != asciiClip )
|
|
|
|
{
|
|
|
|
// [asciiClip setPostsBoundsChangedNotifications:NO];
|
|
|
|
[asciiClip setBoundsOrigin:[object bounds].origin];
|
|
|
|
// [asciiClip setPostsBoundsChangedNotifications:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore notifications
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:offsetClip];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:hexClip];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:asciiClip];
|
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
- (void)refreshData:(NSData *)data;
|
|
|
|
{
|
2002-02-06 20:57:56 +00:00
|
|
|
// clear delegates (see HexEditorDelegate class for explanation of why)
|
2002-02-02 11:48:54 +00:00
|
|
|
id oldDelegate = [hex delegate];
|
|
|
|
[hex setDelegate:nil];
|
|
|
|
[ascii setDelegate:nil];
|
|
|
|
|
|
|
|
// do stuff with data
|
|
|
|
[offset setString:[hexDelegate offsetRepresentation:data]];
|
|
|
|
[hex setString:[hexDelegate hexRepresentation:data]];
|
|
|
|
[ascii setString:[hexDelegate asciiRepresentation:data]];
|
|
|
|
|
|
|
|
// restore delegates
|
|
|
|
[hex setDelegate:oldDelegate];
|
|
|
|
[ascii setDelegate:oldDelegate];
|
|
|
|
}
|
|
|
|
|
2002-02-06 20:57:56 +00:00
|
|
|
- (id)resource
|
|
|
|
{
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *)data
|
|
|
|
{
|
|
|
|
return [(id <ResKnifeResourceProtocol>)resource data];
|
|
|
|
}
|
|
|
|
|
2002-02-02 11:48:54 +00:00
|
|
|
@end
|