2020-04-23 04:43:15 +00:00
|
|
|
/* Implementation of class NSPathCell
|
|
|
|
Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
By: Gregory John Casamento
|
|
|
|
Date: Wed Apr 22 18:19:07 EDT 2020
|
|
|
|
|
|
|
|
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 "AppKit/NSPathCell.h"
|
2020-05-03 08:17:00 +00:00
|
|
|
#import "AppKit/NSWorkspace.h"
|
|
|
|
#import "AppKit/NSImage.h"
|
|
|
|
#import "AppKit/NSPathComponentCell.h"
|
2021-01-10 21:40:29 +00:00
|
|
|
#import "GSGuiPrivate.h"
|
2020-05-03 08:17:00 +00:00
|
|
|
|
2020-05-06 15:50:03 +00:00
|
|
|
static Class pathComponentCellClass;
|
2020-05-06 11:56:37 +00:00
|
|
|
|
2020-05-03 08:17:00 +00:00
|
|
|
@interface NSPathCell (Private)
|
|
|
|
+ (NSArray *) _generateCellsForURL: (NSURL *)url;
|
|
|
|
@end
|
2020-04-23 04:43:15 +00:00
|
|
|
|
2020-05-03 17:07:53 +00:00
|
|
|
@interface NSPathComponentCell (Private)
|
|
|
|
- (void) _setLastComponent: (BOOL)f;
|
|
|
|
@end
|
|
|
|
|
2020-04-23 04:43:15 +00:00
|
|
|
@implementation NSPathCell
|
|
|
|
|
2020-05-06 11:56:37 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSPathCell class])
|
|
|
|
{
|
|
|
|
[self setVersion: 1.0];
|
|
|
|
[self setPathComponentCellClass: [NSPathComponentCell class]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:53:15 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_backgroundColor);
|
|
|
|
RELEASE(_placeholderAttributedString);
|
|
|
|
RELEASE(_allowedTypes);
|
|
|
|
RELEASE(_url);
|
|
|
|
RELEASE(_pathComponentCells);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2020-04-27 05:33:49 +00:00
|
|
|
- (void)mouseEntered:(NSEvent *)event
|
|
|
|
withFrame:(NSRect)frame
|
|
|
|
inView:(NSView *)view
|
|
|
|
{
|
2020-05-06 11:56:37 +00:00
|
|
|
// This is the method where the expansion of the cell happens
|
|
|
|
// if the path component cells are shortened.
|
|
|
|
// This is currently not implemented.
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseExited:(NSEvent *)event
|
|
|
|
withFrame:(NSRect)frame
|
|
|
|
inView:(NSView *)view
|
|
|
|
{
|
2020-05-06 11:56:37 +00:00
|
|
|
// This is the method where the contraction of the cell happens
|
|
|
|
// if the path component cells are shortened.
|
|
|
|
// This is currently not implemented.
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setAllowedTypes: (NSArray *)types
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(_allowedTypes, types);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) allowedTypes
|
|
|
|
{
|
|
|
|
return _allowedTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPathStyle) pathStyle
|
|
|
|
{
|
|
|
|
return _pathStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setPathStyle: (NSPathStyle)pathStyle
|
|
|
|
{
|
|
|
|
_pathStyle = pathStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSAttributedString *) placeholderAttributedString
|
|
|
|
{
|
|
|
|
return _placeholderAttributedString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setPlaceholderAttributedString: (NSAttributedString *)string
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(_placeholderAttributedString, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) placeholderString
|
|
|
|
{
|
2020-05-06 12:11:28 +00:00
|
|
|
return [_placeholderAttributedString string];
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setPlaceholderString: (NSString *)string
|
|
|
|
{
|
2020-05-06 12:11:28 +00:00
|
|
|
NSAttributedString *as = [[NSAttributedString alloc] initWithString: string];
|
|
|
|
[self setPlaceholderAttributedString: as];
|
|
|
|
RELEASE(as);
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSColor *) backgroundColor
|
|
|
|
{
|
|
|
|
return _backgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setBackgroundColor: (NSColor *)color
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(_backgroundColor, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) pathComponentCellClass
|
|
|
|
{
|
|
|
|
return pathComponentCellClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) setPathComponentCellClass: (Class)clz
|
|
|
|
{
|
|
|
|
pathComponentCellClass = clz;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect)rectOfPathComponentCell:(NSPathComponentCell *)cell
|
|
|
|
withFrame:(NSRect)frame
|
|
|
|
inView:(NSView *)view
|
|
|
|
{
|
2020-05-06 08:39:39 +00:00
|
|
|
NSUInteger index = [_pathComponentCells indexOfObject: cell];
|
2020-05-06 11:56:37 +00:00
|
|
|
CGFloat cellWidth = (frame.size.width / (CGFloat)[_pathComponentCells count]);
|
|
|
|
return NSMakeRect(frame.origin.x + (cellWidth * (CGFloat)index),
|
|
|
|
frame.origin.y,
|
|
|
|
cellWidth,
|
|
|
|
frame.size.height);
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPathComponentCell *)pathComponentCellAtPoint:(NSPoint)point
|
|
|
|
withFrame:(NSRect)frame
|
|
|
|
inView:(NSView *)view
|
|
|
|
{
|
2020-05-06 08:39:39 +00:00
|
|
|
NSUInteger c = [_pathComponentCells count];
|
|
|
|
NSUInteger woc = frame.size.width / c;
|
|
|
|
NSUInteger item = (NSUInteger)point.x / woc;
|
|
|
|
|
|
|
|
return [_pathComponentCells objectAtIndex: item];
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPathComponentCell *) clickedPathComponentCell
|
|
|
|
{
|
2020-05-05 19:24:00 +00:00
|
|
|
return _clickedPathComponentCell;
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) pathComponentCells
|
|
|
|
{
|
|
|
|
return _pathComponentCells;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setPathComponentCells: (NSArray *)cells
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(_pathComponentCells, cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SEL) doubleAction
|
|
|
|
{
|
|
|
|
return _doubleAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDoubleAction: (SEL)action
|
|
|
|
{
|
|
|
|
_doubleAction = action;
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:18:53 +00:00
|
|
|
- (id) objectValue
|
|
|
|
{
|
|
|
|
return _objectValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setObjectValue: (id)obj
|
|
|
|
{
|
|
|
|
ASSIGN(_objectValue, obj);
|
|
|
|
[self setPathComponentCells:
|
|
|
|
[NSPathCell _generateCellsForURL: (NSURL *)_objectValue]];
|
|
|
|
}
|
|
|
|
|
2020-04-27 05:33:49 +00:00
|
|
|
- (NSURL *) URL
|
|
|
|
{
|
2020-05-06 14:38:24 +00:00
|
|
|
return [self objectValue];
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setURL: (NSURL *)url
|
|
|
|
{
|
2020-05-06 14:38:24 +00:00
|
|
|
[self setObjectValue: url];
|
2020-04-27 05:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id<NSPathCellDelegate>) delegate
|
|
|
|
{
|
|
|
|
return _delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDelegate: (id<NSPathCellDelegate>)delegate
|
|
|
|
{
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:12:23 +00:00
|
|
|
- (void) drawInteriorWithFrame: (NSRect)frame inView: (NSView *)controlView
|
|
|
|
{
|
2020-05-09 19:11:01 +00:00
|
|
|
NSEnumerator *en = [_pathComponentCells objectEnumerator];
|
|
|
|
NSPathComponentCell *cell = nil;
|
2020-05-03 08:17:00 +00:00
|
|
|
|
|
|
|
[super drawInteriorWithFrame: frame
|
|
|
|
inView: controlView];
|
2020-05-07 23:00:07 +00:00
|
|
|
|
|
|
|
while ((cell = (NSPathComponentCell *)[en nextObject]) != nil)
|
2020-05-03 08:17:00 +00:00
|
|
|
{
|
2020-05-07 23:00:07 +00:00
|
|
|
NSRect f = [self rectOfPathComponentCell: cell
|
|
|
|
withFrame: frame
|
|
|
|
inView: controlView];
|
|
|
|
[cell drawInteriorWithFrame: f
|
|
|
|
inView: controlView];
|
2020-05-03 08:17:00 +00:00
|
|
|
}
|
2020-05-02 12:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder *)coder
|
|
|
|
{
|
2020-05-06 14:38:24 +00:00
|
|
|
self = [super initWithCoder: coder];
|
2020-05-02 12:12:23 +00:00
|
|
|
if ([coder allowsKeyedCoding])
|
|
|
|
{
|
2020-05-02 17:48:12 +00:00
|
|
|
[self setPathStyle: NSPathStyleStandard];
|
|
|
|
|
2020-05-06 14:38:24 +00:00
|
|
|
if ([coder containsValueForKey: @"NSPathStyle"])
|
2020-05-02 17:48:12 +00:00
|
|
|
{
|
|
|
|
[self setPathStyle: [coder decodeIntegerForKey: @"NSPathStyle"]];
|
|
|
|
}
|
2020-05-02 12:12:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-10 21:40:29 +00:00
|
|
|
decode_NSUInteger(coder, &_pathStyle);
|
2020-05-05 12:26:13 +00:00
|
|
|
[self setPathComponentCells: [coder decodeObject]];
|
2020-05-02 12:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2020-04-23 04:43:15 +00:00
|
|
|
|
2020-05-05 12:26:13 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
|
|
|
{
|
2020-05-06 14:38:24 +00:00
|
|
|
[super encodeWithCoder: coder];
|
2020-05-05 12:26:13 +00:00
|
|
|
if ([coder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
[coder encodeInteger: [self pathStyle]
|
|
|
|
forKey: @"NSPathStyle"];
|
|
|
|
[coder encodeObject: [self pathComponentCells]
|
2020-05-05 15:03:48 +00:00
|
|
|
forKey: @"NSPathComponentCells"];
|
2020-05-05 12:26:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-10 21:40:29 +00:00
|
|
|
encode_NSUInteger(coder, &_pathStyle);
|
2020-05-05 12:26:13 +00:00
|
|
|
[coder encodeObject: [self pathComponentCells]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:12:23 +00:00
|
|
|
@end
|
2020-05-03 08:17:00 +00:00
|
|
|
|
|
|
|
@implementation NSPathCell (Private)
|
|
|
|
|
|
|
|
// Private...
|
|
|
|
+ (NSArray *) _generateCellsForURL: (NSURL *)url
|
|
|
|
{
|
|
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10];
|
2020-05-05 15:03:48 +00:00
|
|
|
|
2020-05-03 08:17:00 +00:00
|
|
|
// Create cells
|
|
|
|
if (url != nil)
|
|
|
|
{
|
|
|
|
BOOL isDir = NO;
|
|
|
|
BOOL at_root = NO;
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
2020-05-07 23:00:07 +00:00
|
|
|
NSURL *u = url;
|
2020-05-05 15:03:48 +00:00
|
|
|
|
2020-05-03 08:17:00 +00:00
|
|
|
// Decompose string...
|
2020-05-07 22:57:02 +00:00
|
|
|
while (at_root == NO)
|
2020-05-03 08:17:00 +00:00
|
|
|
{
|
|
|
|
NSPathComponentCell *cell = [[NSPathComponentCell alloc] init];
|
|
|
|
NSImage *image = nil;
|
2020-05-05 15:03:48 +00:00
|
|
|
NSString *string = [u path];
|
|
|
|
|
|
|
|
[cell setURL: u];
|
2020-05-07 03:42:49 +00:00
|
|
|
[fm fileExistsAtPath: string
|
2020-05-03 08:17:00 +00:00
|
|
|
isDirectory: &isDir];
|
|
|
|
|
2020-05-07 03:42:49 +00:00
|
|
|
if ([string isEqualToString: @"/"])
|
2020-05-03 17:07:53 +00:00
|
|
|
{
|
|
|
|
at_root = YES;
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:17:00 +00:00
|
|
|
if (isDir && at_root == NO)
|
|
|
|
{
|
|
|
|
image = [NSImage imageNamed: @"NSFolder"];
|
|
|
|
}
|
2020-05-06 20:18:53 +00:00
|
|
|
else if (isDir == YES && at_root == YES)
|
|
|
|
{
|
|
|
|
image = [NSImage imageNamed: @"NSComputer"];
|
|
|
|
}
|
2020-05-03 08:17:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
image = [[NSWorkspace sharedWorkspace] iconForFile: [[url path] lastPathComponent]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[cell setImage: image];
|
2020-05-03 17:07:53 +00:00
|
|
|
|
|
|
|
if ([array count] == 0) // the element we are adding is the last component that will show
|
|
|
|
{
|
|
|
|
[cell _setLastComponent: YES];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[cell _setLastComponent: NO];
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:17:00 +00:00
|
|
|
[array insertObject: cell
|
|
|
|
atIndex: 0];
|
2020-05-06 12:11:28 +00:00
|
|
|
RELEASE(cell);
|
2020-05-07 03:50:37 +00:00
|
|
|
string = [string stringByDeletingLastPathComponent];
|
2020-05-05 15:03:48 +00:00
|
|
|
u = [NSURL URLWithString: string
|
|
|
|
relativeToURL: nil];
|
|
|
|
if (u == nil && at_root == NO)
|
2020-05-03 08:17:00 +00:00
|
|
|
{
|
|
|
|
// Because when we remove the last path component
|
|
|
|
// all that is left is a blank... so we add the "/" so that
|
|
|
|
// it is shown.
|
2020-05-05 15:03:48 +00:00
|
|
|
u = [NSURL URLWithString: @"/"];
|
2020-05-03 08:17:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [array copy];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2020-05-03 17:07:53 +00:00
|
|
|
|
|
|
|
@implementation NSPathComponentCell (Private)
|
|
|
|
|
|
|
|
- (void) _setLastComponent: (BOOL)f
|
|
|
|
{
|
|
|
|
_lastComponent = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|