ResKnife/Cocoa/Classes/InfoWindowController.m

176 lines
6.6 KiB
Mathematica
Raw Normal View History

2001-10-19 19:41:13 +00:00
#import "InfoWindowController.h"
#import <Carbon/Carbon.h> // Actually I only need CarbonCore.framework, but <Carbon/CarbonCore.h> and <CarbonCore/CarbonCore.h> don't work, so I don't know what else to do
#import "ResourceDocument.h"
#import "Resource.h"
#import "ApplicationDelegate.h"
2002-02-23 03:40:24 +00:00
#import "NSOutlineView-SelectedItems.h"
2002-04-02 21:57:17 +00:00
#import "MoreFilesX.h"
2001-10-19 19:41:13 +00:00
@implementation InfoWindowController
2002-02-11 01:22:17 +00:00
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
2001-10-19 19:41:13 +00:00
- (void)windowDidLoad
{
[super windowDidLoad];
// set window to only accept key when editing text boxes
[(NSPanel *)[self window] setBecomesKeyOnlyIfNeeded:YES];
2002-02-07 07:37:51 +00:00
// retain views for swapping in and out
2003-04-03 15:38:42 +00:00
[[documentView retain] removeFromSuperview];
[[resourceView retain] removeFromSuperview];
2002-02-07 07:37:51 +00:00
2001-10-19 19:41:13 +00:00
[self setMainWindow:[NSApp mainWindow]];
[self updateInfoWindow];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainWindowChanged:) name:NSWindowDidBecomeMainNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedResourceChanged:) name:NSOutlineViewSelectionDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resourceAttributesDidChange:) name:ResourceAttributesDidChangeNotification object:nil];
2002-04-02 21:57:17 +00:00
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentInfoDidChange:) name:DocumentInfoDidChangeNotification object:nil];
2001-10-19 19:41:13 +00:00
}
/*!
@method updateInfoWindow
@updated 2003-11-06 NGS: Fixed creator/type handling.
@updated 2003-10-26 NGS: Now asks app delegate for icon instead of NSWorkspace.
@updated 2003-10-26 NGS: Improved document name & icon display.
*/
- (void)updateInfoWindow
2001-10-19 19:41:13 +00:00
{
[nameView setEditable:(selectedResource != nil)];
[nameView setDrawsBackground:(selectedResource != nil)];
if(selectedResource)
2001-10-19 19:41:13 +00:00
{
// set UI values
[[self window] setTitle:NSLocalizedString(@"Resource Info",nil)];
2001-10-19 19:41:13 +00:00
[nameView setStringValue:[selectedResource name]];
[iconView setImage:[(ApplicationDelegate *)[NSApp delegate] iconForResourceType:[selectedResource type]]];
2001-10-19 19:41:13 +00:00
[[attributesMatrix cellAtRow:changedBox column:0] setState:[[selectedResource attributes] shortValue] & resChanged];
[[attributesMatrix cellAtRow:preloadBox column:0] setState:[[selectedResource attributes] shortValue] & resPreload];
[[attributesMatrix cellAtRow:protectedBox column:0] setState:[[selectedResource attributes] shortValue] & resProtected];
[[attributesMatrix cellAtRow:lockedBox column:0] setState:[[selectedResource attributes] shortValue] & resLocked];
[[attributesMatrix cellAtRow:purgableBox column:0] setState:[[selectedResource attributes] shortValue] & resPurgeable];
[[attributesMatrix cellAtRow:systemHeapBox column:0] setState:[[selectedResource attributes] shortValue] & resSysHeap];
// swap box
[placeholderView setContentView:resourceView];
2001-10-19 19:41:13 +00:00
}
else if(currentDocument != nil)
2001-10-19 19:41:13 +00:00
{
2002-04-02 21:57:17 +00:00
// get sizes of forks as they are on disk
UInt64 dataLogicalSize = 0, rsrcLogicalSize = 0;
FSRef *fileRef = (FSRef *) NewPtrClear(sizeof(FSRef));
if(fileRef && [currentDocument fileName])
2002-04-02 21:57:17 +00:00
{
2009-11-09 10:17:40 +00:00
OSStatus error = FSPathMakeRef((unsigned char *)[[currentDocument fileName] fileSystemRepresentation], fileRef, nil);
if(!error) FSGetForkSizes(fileRef, &dataLogicalSize, &rsrcLogicalSize);
2002-04-02 21:57:17 +00:00
}
if(fileRef) DisposePtr((Ptr) fileRef);
2002-04-02 21:57:17 +00:00
// set info window elements to correct values
[[self window] setTitle:NSLocalizedString(@"Document Info",nil)];
if([currentDocument fileName]) // document has been saved
{
[iconView setImage:[[NSWorkspace sharedWorkspace] iconForFile:[currentDocument fileName]]];
[nameView setStringValue:[[currentDocument fileName] lastPathComponent]];
}
else // new, untitled document
{
[iconView setImage:[NSImage imageNamed:@"Resource file"]];
[nameView setStringValue:[currentDocument displayName]];
}
#warning FIXME: the creator and type codes need to be swapped on intel
[[filePropertyForm cellAtIndex:0] setStringValue:[[[NSString alloc] initWithData:[currentDocument creator] encoding:NSMacOSRomanStringEncoding] autorelease]];
[[filePropertyForm cellAtIndex:1] setStringValue:[[[NSString alloc] initWithData:[currentDocument type] encoding:NSMacOSRomanStringEncoding] autorelease]];
2002-04-02 21:57:17 +00:00
// [[filePropertyForm cellAtIndex:2] setObjectValue:[NSNumber numberWithUnsignedLongLong:dataLogicalSize]];
// [[filePropertyForm cellAtIndex:3] setObjectValue:[NSNumber numberWithUnsignedLongLong:rsrcLogicalSize]];
2002-04-27 18:17:47 +00:00
[[filePropertyForm cellAtIndex:2] setStringValue:[[NSNumber numberWithUnsignedLongLong:dataLogicalSize] description]];
[[filePropertyForm cellAtIndex:3] setStringValue:[[NSNumber numberWithUnsignedLongLong:rsrcLogicalSize] description]];
// swap box
2002-02-23 03:40:24 +00:00
[placeholderView setContentView:documentView];
2001-10-19 19:41:13 +00:00
}
else
{
[iconView setImage:nil];
[nameView setStringValue:@""];
[placeholderView setContentView:nil];
}
2001-10-19 19:41:13 +00:00
}
- (void)setMainWindow:(NSWindow *)mainWindow
{
NSWindowController *controller = [mainWindow windowController];
if([[controller document] isKindOfClass:[ResourceDocument class]])
2001-10-19 19:41:13 +00:00
currentDocument = [controller document];
else currentDocument = nil;
if(currentDocument)
2002-05-09 23:29:22 +00:00
selectedResource = [[currentDocument outlineView] selectedItem];
else selectedResource = [controller resource];
2001-10-19 19:41:13 +00:00
[self updateInfoWindow];
}
- (void)mainWindowChanged:(NSNotification *)notification
{
[self setMainWindow:[notification object]];
}
- (void)selectedResourceChanged:(NSNotification *)notification
{
if(![[nameView stringValue] isEqualToString:[selectedResource name]])
[self nameDidChange:nameView];
selectedResource = (Resource *) [[notification object] selectedItem];
2001-10-19 19:41:13 +00:00
[self updateInfoWindow];
}
2002-04-02 21:57:17 +00:00
- (void)documentInfoDidChange:(NSNotification *)notification
{
#pragma unused(notification)
2002-04-02 21:57:17 +00:00
[self updateInfoWindow];
}
2001-10-19 19:41:13 +00:00
- (IBAction)attributesChanged:(id)sender
{
short attr = 0x0001 << [sender selectedRow]+1;
short number = ([[selectedResource attributes] shortValue] ^ attr);
[selectedResource setAttributes:[NSNumber numberWithShort:number]];
}
- (IBAction)nameDidChange:(id)sender
{
[selectedResource setName:[nameView stringValue]];
}
- (void)resourceAttributesDidChange:(NSNotification *)notification;
{
2001-10-19 19:41:13 +00:00
[self updateInfoWindow];
}
+ (id)sharedInfoWindowController
{
static InfoWindowController *sharedInfoWindowController = nil;
if(!sharedInfoWindowController)
sharedInfoWindowController = [[InfoWindowController allocWithZone:[self zone]] initWithWindowNibName:@"InfoWindow"];
2001-10-19 19:41:13 +00:00
return sharedInfoWindowController;
}
2002-05-31 17:31:41 +00:00
@end
@implementation NSWindowController (InfoWindowAdditions)
- (Resource *)resource
{
return nil;
}
2002-04-27 18:17:47 +00:00
@end