ResKnife/Cocoa/Plug-Ins/Hex Editor/HexWindowController.m

237 lines
8.8 KiB
Mathematica
Raw Normal View History

2002-02-02 11:48:54 +00:00
#import "HexWindowController.h"
2002-02-23 03:40:24 +00:00
#import "HexTextView.h"
2002-03-21 04:02:26 +00:00
#import "FindSheetController.h"
2002-02-02 11:48:54 +00:00
@implementation HexWindowController
- (id)initWithResource:(id)newResource
{
self = [self initWithWindowNibName:@"HexWindow"];
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-05-31 00:17:53 +00:00
undoManager = [[NSUndoManager alloc] init];
2002-02-07 03:45:43 +00:00
resource = [newResource retain];
2002-03-21 04:02:26 +00:00
bytesPerRow = 16;
2002-02-02 11:48:54 +00:00
2002-02-23 03:40:24 +00:00
// load the window from the nib file and set it's title
[self window]; // implicitly loads nib
if( ![[resource name] isEqualToString:@""] )
[[self window] setTitle:[resource name]];
2002-02-02 11:48:54 +00:00
return self;
}
2002-03-21 04:02:26 +00:00
- (id)initWithResources:(id)newResource, ...
{
2002-05-31 00:17:53 +00:00
[undoManager release];
2002-03-21 04:02:26 +00:00
return nil;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
2002-02-23 03:40:24 +00:00
[(id)resource autorelease];
[super dealloc];
}
2002-02-02 11:48:54 +00:00
- (void)windowDidLoad
{
[super windowDidLoad];
// swap text views to instances of my class instead
2002-02-23 03:40:24 +00:00
[offset swapForHexTextView];
[hex swapForHexTextView];
[ascii swapForHexTextView];
// turn off the background for the offset column (IB is broken when it comes to this)
[offset setDrawsBackground:NO];
[[offset enclosingScrollView] setDrawsBackground:NO];
// set up tab, shift-tab and enter behaviour
[hex setFieldEditor:YES];
[ascii setFieldEditor:YES];
// insert the resources' data into the text fields
[self refreshData:[resource data]];
2002-11-15 15:12:42 +00:00
[[self window] setResizeIncrements:NSMakeSize(kWindowStepWidthPerChar * kWindowStepCharsPerStep, 1)];
// min 346, step 224, norm 570, step 224, max 794
// we don't want this notification until we have a window! (Only register for notifications on the resource we're editing)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resourceNameDidChange:) name:ResourceNameDidChangeNotification object:resource];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resourceDataDidChange:) name:ResourceDataDidChangeNotification object:resource];
2002-02-02 11:48:54 +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
// finally, show the window
[self showWindow:self];
}
2002-11-15 15:12:42 +00:00
- (void)windowDidResize:(NSNotification *)notification
{
int width = [[notification object] frame].size.width;
int oldBytesPerRow = bytesPerRow;
bytesPerRow = (((width - (kWindowStepWidthPerChar * kWindowStepCharsPerStep) - 122) / (kWindowStepWidthPerChar * kWindowStepCharsPerStep)) + 1) * kWindowStepCharsPerStep;
if( bytesPerRow != oldBytesPerRow )
[offset setString:[hexDelegate offsetRepresentation:[resource data]]];
[hexScroll setFrameSize:NSMakeSize( (bytesPerRow * 21) + 5, [hexScroll frame].size.height)];
[asciiScroll setFrameOrigin:NSMakePoint( (bytesPerRow * 21) + 95, 20)];
[asciiScroll setFrameSize:NSMakeSize( (bytesPerRow * 7) + 28, [asciiScroll frame].size.height)];
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
// swap paste: menu item for my own paste submenu
NSMenu *editMenu = [[[NSApp mainMenu] itemAtIndex:2] submenu];
NSMenuItem *pasteItem = [editMenu itemAtIndex:[editMenu indexOfItemWithTarget:nil andAction:@selector(paste:)]];
[NSBundle loadNibNamed:@"PasteMenu" owner:self];
[pasteItem setEnabled:YES];
[pasteItem setKeyEquivalent:@"\0"]; // clear key equiv. (yes, really!)
[pasteItem setKeyEquivalentModifierMask:0];
[editMenu setSubmenu:pasteSubmenu forItem:pasteItem];
}
- (void)windowDidResignKey:(NSNotification *)notification
{
// swap my submenu for plain paste menu item
NSMenu *editMenu = [[[NSApp mainMenu] itemAtIndex:2] submenu];
NSMenuItem *pasteItem = [editMenu itemAtIndex:[editMenu indexOfItemWithSubmenu:pasteSubmenu]];
[editMenu setSubmenu:nil forItem:pasteItem];
[pasteItem setTarget:nil];
[pasteItem setAction:@selector(paste:)];
[pasteItem setKeyEquivalent:@"v"];
[pasteItem setKeyEquivalentModifierMask:NSCommandKeyMask];
}
2002-05-31 00:17:53 +00:00
/*
- (BOOL)windowShouldClose:(NSWindow *)sender
{
return [sender isDocumentEdited];
}*/
2002-03-21 04:02:26 +00:00
- (IBAction)showFind:(id)sender
{
// bug: HexWindowController allocs a sheet controller, but it's never disposed of
FindSheetController *sheetController = [[FindSheetController alloc] initWithWindowNibName:@"FindSheet"];
[sheetController showFindSheet:self];
}
- (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!
2002-03-31 17:02:40 +00:00
// update, Apple say this isn't their bug.
[[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];
}
- (void)resourceNameDidChange:(NSNotification *)notification
{
if( ![[(id <ResKnifeResourceProtocol>)[notification object] name] isEqualToString:@""] )
[[self window] setTitle:[(id <ResKnifeResourceProtocol>)[notification object] name]];
else [[self window] setTitle:NSLocalizedStringFromTableInBundle(@"Untitled Resource", @"Localizable", [NSBundle mainBundle], nil)];
}
- (void)resourceDataDidChange:(NSNotification *)notification
{
// ensure it's our resource which got changed (should always be true, we don't register for other resource notifications)
2002-02-23 03:40:24 +00:00
if( [notification object] == (id)resource )
[self refreshData:[resource data]];
}
2002-02-02 11:48:54 +00:00
- (void)refreshData:(NSData *)data;
{
2002-03-21 04:02:26 +00:00
NSDictionary *dictionary;
NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
2002-02-23 03:40:24 +00:00
// save selections
NSRange hexSelection = [hex selectedRange];
NSRange asciiSelection = [ascii selectedRange];
// 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];
2002-03-21 04:02:26 +00:00
// prepare attributes dictionary
[paragraph setLineBreakMode:NSLineBreakByCharWrapping];
dictionary = [NSDictionary dictionaryWithObject:paragraph forKey:NSParagraphStyleAttributeName];
2002-02-02 11:48:54 +00:00
// do stuff with data
[offset setString:[hexDelegate offsetRepresentation:data]];
[hex setString:[hexDelegate hexRepresentation:data]];
[ascii setString:[hexDelegate asciiRepresentation:data]];
2002-03-21 04:02:26 +00:00
// apply attributes
[[offset textStorage] addAttributes:dictionary range:NSMakeRange(0, [[offset textStorage] length])];
[[hex textStorage] addAttributes:dictionary range:NSMakeRange(0, [[hex textStorage] length])];
[[ascii textStorage] addAttributes:dictionary range:NSMakeRange(0, [[ascii textStorage] length])];
2002-02-23 03:40:24 +00:00
// restore selections (this is the dumbest way to do it, but it'll do for now)
2002-04-27 18:17:47 +00:00
[hex setSelectedRange:NSIntersectionRange(hexSelection, [hex selectedRange])];
[ascii setSelectedRange:NSIntersectionRange(asciiSelection, [ascii selectedRange])];
2002-02-23 03:40:24 +00:00
2002-02-02 11:48:54 +00:00
// restore delegates
[hex setDelegate:oldDelegate];
[ascii setDelegate:oldDelegate];
}
- (id)resource
{
return resource;
}
- (NSData *)data
{
2002-02-23 03:40:24 +00:00
return [resource data];
}
2002-03-21 04:02:26 +00:00
- (int)bytesPerRow
{
return bytesPerRow;
}
- (NSMenu *)pasteSubmenu
{
return pasteSubmenu;
}
2002-05-09 23:29:22 +00:00
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)sender
{
2002-05-31 00:17:53 +00:00
return undoManager;
2002-05-09 23:29:22 +00:00
}
2002-04-27 18:17:47 +00:00
@end