Add cell generation and images

This commit is contained in:
Gregory John Casamento 2020-05-03 04:17:00 -04:00
parent 0c05c151a8
commit 9a70a452b5
4 changed files with 125 additions and 43 deletions

View file

@ -61,7 +61,6 @@
#import "AppKit/NSTabView.h"
#import "AppKit/NSToolbarItem.h"
#import "AppKit/NSView.h"
#import "AppKit/NSWorkspace.h"
#import "GSCodingFlags.h"
#define DEBUG_XIB5 0
@ -158,6 +157,9 @@ static NSString *ApplicationClass = nil;
@end
@interface NSPathCell (Private)
+ (NSArray *) _generateCellsForURL: (NSURL *)url;
@end
@implementation GSXib5KeyedUnarchiver
@ -2764,6 +2766,10 @@ didStartElement: (NSString*)elementName
{
num = [NSNumber numberWithInteger: NSPathStyleNavigationBar];
}
else // if not specified then assume standard...
{
num = [NSNumber numberWithInteger: NSPathStyleStandard];
}
return num;
}
@ -2771,7 +2777,6 @@ didStartElement: (NSString*)elementName
- (id) decodePathComponentCells: (GSXibElement *)element
{
GSXibElement *e = nil;
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10];
NSString *string = nil;
NSURL *url = nil;
@ -2792,43 +2797,7 @@ didStartElement: (NSString*)elementName
url = [NSURL URLWithString: string
relativeToURL: nil];
// Create cells
if (url != nil)
{
BOOL isDir = NO;
NSFileManager *fm = [NSFileManager defaultManager];
// Decompose string...
do
{
NSPathComponentCell *cell = [[NSPathComponentCell alloc] init];
NSImage *image = nil;
AUTORELEASE(cell);
[cell setURL: url];
[fm fileExistsAtPath: [url path]
isDirectory: &isDir];
if (isDir)
{
image = [NSImage imageNamed: @"NSFolder"];
}
else
{
image = [[NSWorkspace sharedWorkspace] iconForFile: [[url path] lastPathComponent]];
}
[cell setImage: image];
string = [string stringByDeletingLastPathComponent];
[array insertObject: cell
atIndex: 0];
url = [NSURL URLWithString: string
relativeToURL: nil];
}
while ([string isEqualToString: @""] == NO);
}
return [array copy];
return [NSPathCell _generateCellsForURL: url];
}
- (id) objectForXib: (GSXibElement*)element

View file

@ -23,6 +23,13 @@
*/
#import "AppKit/NSPathCell.h"
#import "AppKit/NSWorkspace.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSPathComponentCell.h"
@interface NSPathCell (Private)
+ (NSArray *) _generateCellsForURL: (NSURL *)url;
@end
@implementation NSPathCell
@ -155,6 +162,7 @@
- (void) setURL: (NSURL *)url
{
ASSIGNCOPY(_url, url);
[self setPathComponentCells: [NSPathCell _generateCellsForURL: url]];
}
- (id<NSPathCellDelegate>) delegate
@ -169,7 +177,25 @@
- (void) drawInteriorWithFrame: (NSRect)frame inView: (NSView *)controlView
{
NSLog(@"Drawing");
NSUInteger count = [_pathComponentCells count];
[super drawInteriorWithFrame: frame
inView: controlView];
if (count > 0)
{
NSEnumerator *en = [_pathComponentCells objectEnumerator];
NSPathComponentCell *cell = nil;
CGFloat cell_width = (frame.size.width / (CGFloat)[_pathComponentCells count]);
CGFloat current_x = 0.0;
while((cell = (NSPathComponentCell *)[en nextObject]) != nil)
{
NSRect f = NSMakeRect(current_x, 0.0, cell_width, frame.size.height);
[cell drawInteriorWithFrame: f
inView: controlView];
current_x += cell_width;
}
}
}
- (id) initWithCoder: (NSCoder *)coder
@ -196,3 +222,65 @@
}
@end
@implementation NSPathCell (Private)
// Private...
+ (NSArray *) _generateCellsForURL: (NSURL *)url
{
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10];
// Create cells
if (url != nil)
{
NSString *string = nil;
BOOL isDir = NO;
BOOL at_root = NO;
NSFileManager *fm = [NSFileManager defaultManager];
// Decompose string...
do
{
NSPathComponentCell *cell = [[NSPathComponentCell alloc] init];
NSImage *image = nil;
if ([[url path] isEqualToString: @"/"])
{
at_root = YES;
}
AUTORELEASE(cell);
[cell setURL: url];
[fm fileExistsAtPath: [url path]
isDirectory: &isDir];
if (isDir && at_root == NO)
{
image = [NSImage imageNamed: @"NSFolder"];
}
else
{
image = [[NSWorkspace sharedWorkspace] iconForFile: [[url path] lastPathComponent]];
}
[cell setImage: image];
string = [string stringByDeletingLastPathComponent];
[array insertObject: cell
atIndex: 0];
url = [NSURL URLWithString: string
relativeToURL: nil];
if (url == nil && at_root == NO)
{
// Because when we remove the last path component
// all that is left is a blank... so we add the "/" so that
// it is shown.
url = [NSURL URLWithString: @"/"];
}
}
while (at_root == NO);
}
return [array copy];
}
@end

View file

@ -23,9 +23,11 @@
*/
#import <Foundation/NSURL.h>
#import <Foundation/NSGeometry.h>
#import "AppKit/NSPathComponentCell.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSStringDrawing.h"
@implementation NSPathComponentCell
@ -49,5 +51,28 @@
ASSIGNCOPY(_url, url);
}
@end
- (void) drawInteriorWithFrame: (NSRect)f
inView: (NSView *)v
{
NSString *string = [[_url path] lastPathComponent];
NSRect textFrame = f;
NSRect imgFrame = f; // NSMakeRect(0.0, 0.0, 20.0, 20.0);
// Modify positions...
textFrame.origin.x += 23.0; // the width of the image plus a few pixels.
textFrame.origin.y += 3; // center with the image...
imgFrame.size.width = 20.0;
imgFrame.size.height = 20.0;
// string = (string != nil) ? string : @"/";
[super drawInteriorWithFrame: f inView: v];
// Draw the image...
[_image drawInRect: imgFrame];
// Draw the text...
[[NSColor whiteColor] set];
[string drawAtPoint: textFrame.origin
withAttributes: nil];
}
@end

View file

@ -168,12 +168,12 @@
[self setNeedsDisplay];
}
/*
- (void) drawRect: (NSRect)frame
{
[super drawRect: frame];
[_backgroundColor set];
NSRectFill(frame);
}
*/
- (instancetype) initWithCoder: (NSKeyedUnarchiver *)coder
{