Set clicked path component cell

This commit is contained in:
Gregory John Casamento 2020-05-06 02:38:01 -04:00
parent 9dd7b6d56d
commit d7e1e06809
3 changed files with 60 additions and 7 deletions

View file

@ -40,7 +40,6 @@ extern "C" {
NSURL *_url;
NSAttributedString *_attributedTitle;
NSImage *_image;
NSString *_title;
}
- (NSURL *) URL;

View file

@ -32,6 +32,8 @@
#import "AppKit/NSMenu.h"
#import "AppKit/NSOpenPanel.h"
#import "AppKit/NSPathComponentCell.h"
#import "AppKit/NSPathControlItem.h"
#import "AppKit/NSEvent.h"
static NSNotificationCenter *nc = nil;
@ -39,6 +41,10 @@ static NSNotificationCenter *nc = nil;
- (void) _setClickedPathComponentCell: (NSPathComponentCell *)c;
@end
@interface NSPathComponentCell (PathControlPrivate)
- (NSPathControlItem *) _pathControlItem;
@end
@implementation NSPathControl
+ (void) initialize
@ -108,6 +114,22 @@ static NSNotificationCenter *nc = nil;
[_cell setDoubleAction: doubleAction];
}
- (void) _createPathItems
{
NSArray *a = [_cell pathComponentCells];
NSEnumerator *en = [a objectEnumerator];
NSPathComponentCell *c = nil;
NSMutableArray *items = [NSMutableArray arrayWithCapacity: [a count]];
while((c = [en nextObject]) != nil)
{
NSPathControlItem *pi = [c _pathControlItem];
[items addObject: pi];
}
[self setPathItems: [items copy]];
}
- (NSURL *) URL
{
return [_cell URL];
@ -116,6 +138,7 @@ static NSNotificationCenter *nc = nil;
- (void) setURL: (NSURL *)url
{
[_cell setURL: url];
[self _createPathItems];
[self setNeedsDisplay];
}
@ -175,7 +198,7 @@ static NSNotificationCenter *nc = nil;
- (NSPathControlItem *) clickedPathItem
{
return nil;
return [[self clickedPathComponentCell] _pathControlItem];
}
- (NSArray *) pathItems
@ -257,8 +280,10 @@ static NSNotificationCenter *nc = nil;
to: _target];
}
[self setURL: [cc URL]];
// Tested on OSX it doesn't do this... it apparently only chooses
// the cell, and sends the action. It doesn't reset the URL.
[[sender menu] close];
AUTORELEASE([sender menu]);
}
- (void) _doChooseMenuAction: (id)sender
@ -293,6 +318,7 @@ static NSNotificationCenter *nc = nil;
}
[[sender menu] close];
AUTORELEASE([sender menu]);
}
- (void) mouseDown: (NSEvent *)event
@ -307,7 +333,7 @@ static NSNotificationCenter *nc = nil;
{
NSPathCell *acell = (NSPathCell *)[self cell];
NSArray *array = [acell pathComponentCells];
NSMenu *menu = [[NSMenu alloc] initWithTitle: @"Select File"];
NSMenu *menu = [[NSMenu alloc] initWithTitle: nil];
NSPathComponentCell *c = nil;
NSEnumerator *en = [array objectEnumerator];
@ -323,6 +349,7 @@ static NSNotificationCenter *nc = nil;
[menu insertItem: i
atIndex: 0];
AUTORELEASE(i);
}
// Add separator
@ -336,6 +363,7 @@ static NSNotificationCenter *nc = nil;
[i setAction: @selector(_doChooseMenuAction:)];
[menu insertItem: i
atIndex: 0];
AUTORELEASE(i);
if (_delegate)
{
@ -362,6 +390,13 @@ static NSNotificationCenter *nc = nil;
}
else
{
NSArray *cells = [self pathComponentCells];
NSUInteger c = [cells count];
NSPoint loc = [event locationInWindow];
NSUInteger woc = (NSUInteger)[self frame].size.width / c;
NSUInteger itemClicked = (NSUInteger)loc.x / woc;
[_cell _setClickedPathComponentCell: [cells objectAtIndex: itemClicked]];
if (_action)
{
[self sendAction: _action
@ -372,7 +407,7 @@ static NSNotificationCenter *nc = nil;
- (NSDragOperation) draggingEntered: (id<NSDraggingInfo>)sender
{
// if (_delegate != nil)
if (_delegate != nil)
{
NSDragOperation d = [_delegate pathControl: self
validateDrop: sender];
@ -448,3 +483,20 @@ static NSNotificationCenter *nc = nil;
}
@end
@implementation NSPathComponentCell (PathControlPrivate)
- (NSPathControlItem *) _pathControlItem
{
NSPathControlItem *pi = [[NSPathControlItem alloc] init];
NSURL *u = [self URL];
NSString *path = [u path];
[pi setImage: [self image]];
[pi setURL: u];
[pi setTitle: [path lastPathComponent]];
return pi;
}
@end

View file

@ -62,12 +62,14 @@
- (NSString *) title
{
return _title;
return [_attributedTitle string];
}
- (void) setTitle: (NSString *)title
{
ASSIGNCOPY(_title, title);
NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString: title];
AUTORELEASE(attrTitle);
[self setAttributedTitle: attrTitle];
}
@end